Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.fs.FileEncryptionInfo;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
import org.apache.hadoop.ozone.OzoneAcl;
import org.apache.hadoop.ozone.OzoneConsts;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.KeyInfo;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.KeyLocationList;
import org.apache.hadoop.ozone.protocolPB.OMPBHelper;
Expand All @@ -52,6 +54,8 @@ public final class OmKeyInfo extends WithObjectID {
private HddsProtos.ReplicationType type;
private HddsProtos.ReplicationFactor factor;
private FileEncryptionInfo encInfo;
private String fileName; // leaf node name
private long parentObjectID; // pointer to parent directory

/**
* ACL Information.
Expand Down Expand Up @@ -94,6 +98,22 @@ public final class OmKeyInfo extends WithObjectID {
this.updateID = updateID;
}

@SuppressWarnings("parameternumber")
OmKeyInfo(String volumeName, String bucketName, String keyName,
String fileName, List<OmKeyLocationInfoGroup> versions,
long dataSize, long creationTime, long modificationTime,
HddsProtos.ReplicationType type,
HddsProtos.ReplicationFactor factor,
Map<String, String> metadata,
FileEncryptionInfo encInfo, List<OzoneAcl> acls,
long parentObjectID, long objectID, long updateID) {
this(volumeName, bucketName, keyName, versions, dataSize,
creationTime, modificationTime, type, factor, metadata, encInfo,
acls, objectID, updateID);
this.fileName = fileName;
this.parentObjectID = parentObjectID;
}

public String getVolumeName() {
return volumeName;
}
Expand Down Expand Up @@ -126,6 +146,23 @@ public void setDataSize(long size) {
this.dataSize = size;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getFileName() {
return fileName;
}

public void setParentObjectID(long parentObjectID) {
this.parentObjectID = parentObjectID;
}

public long getParentObjectID() {
return parentObjectID;
}


public synchronized OmKeyLocationInfoGroup getLatestVersionLocations() {
return keyLocationVersions.size() == 0? null :
keyLocationVersions.get(keyLocationVersions.size() - 1);
Expand Down Expand Up @@ -267,6 +304,9 @@ public static class Builder {
private List<OzoneAcl> acls;
private long objectID;
private long updateID;
// not persisted to DB. FileName will be the last element in path keyName.
private String fileName;
private long parentObjectID;

public Builder() {
this.metadata = new HashMap<>();
Expand Down Expand Up @@ -369,11 +409,22 @@ public Builder setUpdateID(long id) {
return this;
}

public Builder setFileName(String keyFileName) {
this.fileName = keyFileName;
return this;
}

public Builder setParentObjectID(long parentID) {
this.parentObjectID = parentID;
return this;
}

public OmKeyInfo build() {
return new OmKeyInfo(
volumeName, bucketName, keyName, omKeyLocationInfoGroups,
dataSize, creationTime, modificationTime, type, factor, metadata,
encInfo, acls, objectID, updateID);
volumeName, bucketName, keyName, fileName,
omKeyLocationInfoGroups, dataSize, creationTime,
modificationTime, type, factor, metadata, encInfo, acls,
parentObjectID, objectID, updateID);
}
}

Expand Down Expand Up @@ -413,7 +464,8 @@ public KeyInfo getProtobuf(boolean ignorePipeline) {
.addAllMetadata(KeyValueUtil.toProtobuf(metadata))
.addAllAcls(OzoneAclUtil.toProtobuf(acls))
.setObjectID(objectID)
.setUpdateID(updateID);
.setUpdateID(updateID)
.setParentID(parentObjectID);
if (encInfo != null) {
kb.setFileEncryptionInfo(OMPBHelper.convert(encInfo));
}
Expand Down Expand Up @@ -451,6 +503,11 @@ public static OmKeyInfo getFromProtobuf(KeyInfo keyInfo) {
if (keyInfo.hasUpdateID()) {
builder.setUpdateID(keyInfo.getUpdateID());
}
if (keyInfo.hasParentID()) {
builder.setParentObjectID(keyInfo.getParentID());
}
// not persisted to DB. FileName will be filtered out from keyName
builder.setFileName(OzoneFSUtils.getFileName(keyInfo.getKeyName()));
return builder.build();
}

Expand All @@ -464,6 +521,8 @@ public String getObjectInfo() {
", creationTime='" + creationTime + '\'' +
", type='" + type + '\'' +
", factor='" + factor + '\'' +
", objectID='" + objectID + '\'' +
", parentID='" + parentObjectID + '\'' +
'}';
}

Expand All @@ -489,12 +548,13 @@ public boolean equals(Object o) {
Objects.equals(metadata, omKeyInfo.metadata) &&
Objects.equals(acls, omKeyInfo.acls) &&
objectID == omKeyInfo.objectID &&
updateID == omKeyInfo.updateID;
updateID == omKeyInfo.updateID &&
parentObjectID == omKeyInfo.parentObjectID;
}

@Override
public int hashCode() {
return Objects.hash(volumeName, bucketName, keyName);
return Objects.hash(volumeName, bucketName, keyName, parentObjectID);
}

/**
Expand Down Expand Up @@ -540,4 +600,11 @@ public OmKeyInfo copyObject() {
public void clearFileEncryptionInfo() {
this.encInfo = null;
}

public String getPath() {
if (StringUtils.isBlank(getFileName())) {
return getKeyName();
}
return getParentObjectID() + OzoneConsts.OM_KEY_PREFIX + getFileName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.fs.ozone;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.utils.db.Table;
import org.apache.hadoop.hdds.utils.db.TableIterator;
import org.apache.hadoop.ozone.MiniOzoneCluster;
import org.apache.hadoop.ozone.OzoneConsts;
import org.apache.hadoop.ozone.TestDataUtil;
import org.apache.hadoop.ozone.client.OzoneBucket;
import org.apache.hadoop.ozone.om.OMConfigKeys;
import org.apache.hadoop.ozone.om.OMMetadataManager;
import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
import org.apache.hadoop.util.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.FS_TRASH_INTERVAL_KEY;
import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_FS_ITERATE_BATCH_SIZE;

/**
* Test verifies the entries and operations in file table, open file table etc.
*/
public class TestOzoneFileOps {

@Rule
public Timeout timeout = new Timeout(300000);

private static final Logger LOG =
LoggerFactory.getLogger(TestOzoneFileOps.class);

private MiniOzoneCluster cluster;
private FileSystem fs;
private String volumeName;
private String bucketName;

@Before
public void setupOzoneFileSystem()
throws IOException, TimeoutException, InterruptedException {
OzoneConfiguration conf = new OzoneConfiguration();
conf.setInt(FS_TRASH_INTERVAL_KEY, 1);
conf.set(OMConfigKeys.OZONE_OM_LAYOUT_VERSION, "V1");
conf.setBoolean(OMConfigKeys.OZONE_OM_ENABLE_FILESYSTEM_PATHS, true);
cluster = MiniOzoneCluster.newBuilder(conf)
.setNumDatanodes(3)
.build();
cluster.waitForClusterToBeReady();
// create a volume and a bucket to be used by OzoneFileSystem
OzoneBucket bucket = TestDataUtil.createVolumeAndBucket(cluster);
volumeName = bucket.getVolumeName();
bucketName = bucket.getName();

String rootPath = String.format("%s://%s.%s/",
OzoneConsts.OZONE_URI_SCHEME, bucket.getName(),
bucket.getVolumeName());

// Set the fs.defaultFS and start the filesystem
conf.set(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY, rootPath);
// Set the number of keys to be processed during batch operate.
conf.setInt(OZONE_FS_ITERATE_BATCH_SIZE, 5);
fs = FileSystem.get(conf);
}

@After
public void tearDown() {
IOUtils.closeQuietly(fs);
if (cluster != null) {
cluster.shutdown();
}
}

@Test(timeout = 300_000)
public void testCreateFile() throws Exception {
// Op 1. create dir -> /d1/d2/d3/d4/
Path parent = new Path("/d1/d2/");
Path file = new Path(parent, "file1");
FSDataOutputStream outputStream = fs.create(file);
String openFileKey = "";

OMMetadataManager omMgr = cluster.getOzoneManager().getMetadataManager();
OmBucketInfo omBucketInfo = omMgr.getBucketTable().get(
omMgr.getBucketKey(volumeName, bucketName));
Assert.assertNotNull("Failed to find bucketInfo", omBucketInfo);

ArrayList<String> dirKeys = new ArrayList<>();
long d1ObjectID = verifyDirKey(omBucketInfo.getObjectID(), "d1", "/d1",
dirKeys, omMgr);
long d2ObjectID = verifyDirKey(d1ObjectID, "d2", "/d1/d2", dirKeys,
omMgr);
openFileKey = d2ObjectID + OzoneConsts.OM_KEY_PREFIX + file.getName();

// verify entries in directory table
TableIterator<String, ? extends
Table.KeyValue<String, OmDirectoryInfo>> iterator =
omMgr.getDirectoryTable().iterator();
iterator.seekToFirst();
int count = dirKeys.size();
Assert.assertEquals("Unexpected directory table entries!", 2, count);
while (iterator.hasNext()) {
count--;
Table.KeyValue<String, OmDirectoryInfo> value = iterator.next();
verifyKeyFormat(value.getKey(), dirKeys);
}
Assert.assertEquals("Unexpected directory table entries!", 0, count);

// verify entries in open key table
TableIterator<String, ? extends
Table.KeyValue<String, OmKeyInfo>> keysItr =
omMgr.getOpenKeyTable().iterator();
keysItr.seekToFirst();

while (keysItr.hasNext()) {
count++;
Table.KeyValue<String, OmKeyInfo> value = keysItr.next();
verifyOpenKeyFormat(value.getKey(), openFileKey);
}
Assert.assertEquals("Unexpected file table entries!", 1, count);

// trigger CommitKeyRequest
outputStream.close();

Assert.assertTrue("Failed to commit the open file:" + openFileKey,
omMgr.getOpenKeyTable().isEmpty());

OmKeyInfo omKeyInfo = omMgr.getKeyTable().get(openFileKey);
Assert.assertNotNull("Invalid Key!", omKeyInfo);
}


/**
* Verify key name format and the DB key existence in the expected dirKeys
* list.
*
* @param key table keyName
* @param dirKeys expected keyName
*/
private void verifyKeyFormat(String key, ArrayList<String> dirKeys) {
String[] keyParts = StringUtils.split(key,
OzoneConsts.OM_KEY_PREFIX.charAt(0));
Assert.assertEquals("Invalid KeyName", 2, keyParts.length);
boolean removed = dirKeys.remove(key);
Assert.assertTrue("Key:" + key + " doesn't exists in directory table!",
removed);
}

/**
* Verify key name format and the DB key existence in the expected
* openFileKeys list.
*
* @param key table keyName
* @param openFileKey expected keyName
*/
private void verifyOpenKeyFormat(String key, String openFileKey) {
String[] keyParts = StringUtils.split(key,
OzoneConsts.OM_KEY_PREFIX.charAt(0));
Assert.assertEquals("Invalid KeyName:" + key, 3, keyParts.length);
String[] expectedOpenFileParts = StringUtils.split(openFileKey,
OzoneConsts.OM_KEY_PREFIX.charAt(0));
Assert.assertEquals("ParentId/Key:" + expectedOpenFileParts[0]
+ " doesn't exists in openFileTable!",
expectedOpenFileParts[0] + OzoneConsts.OM_KEY_PREFIX
+ expectedOpenFileParts[1],
keyParts[0] + OzoneConsts.OM_KEY_PREFIX + keyParts[1]);
}

long verifyDirKey(long parentId, String dirKey, String absolutePath,
ArrayList<String> dirKeys, OMMetadataManager omMgr)
throws Exception {
String dbKey = parentId + "/" + dirKey;
dirKeys.add(dbKey);
OmDirectoryInfo dirInfo = omMgr.getDirectoryTable().get(dbKey);
Assert.assertNotNull("Failed to find " + absolutePath +
" using dbKey: " + dbKey, dirInfo);
Assert.assertEquals("Parent Id mismatches", parentId,
dirInfo.getParentObjectID());
Assert.assertEquals("Mismatches directory name", dirKey,
dirInfo.getName());
Assert.assertTrue("Mismatches directory creation time param",
dirInfo.getCreationTime() > 0);
Assert.assertEquals("Mismatches directory modification time param",
dirInfo.getCreationTime(), dirInfo.getModificationTime());
return dirInfo.getObjectID();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ message KeyInfo {
repeated OzoneAclInfo acls = 13;
optional uint64 objectID = 14;
optional uint64 updateID = 15;
optional uint64 parentID = 16;
}

message DirectoryInfo {
Expand Down
Loading