use of org.apache.hadoop.ozone.om.helpers.OpenKeySession in project ozone by apache.
the class TestKeyManagerImpl method openKeyWithMultipleBlocks.
@Test
public void openKeyWithMultipleBlocks() throws IOException {
OmKeyArgs keyArgs = createBuilder().setKeyName(UUID.randomUUID().toString()).setDataSize(scmBlockSize * 10).build();
OpenKeySession keySession = writeClient.openKey(keyArgs);
OmKeyInfo keyInfo = keySession.getKeyInfo();
Assert.assertEquals(10, keyInfo.getLatestVersionLocations().getLocationList().size());
}
use of org.apache.hadoop.ozone.om.helpers.OpenKeySession in project ozone by apache.
the class TestKeyManagerImpl method testCheckAccessForFileKey.
@Test
public void testCheckAccessForFileKey() throws Exception {
// GIVEN
OmKeyArgs keyArgs = createBuilder().setKeyName("testdir/deep/NOTICE.txt").build();
OpenKeySession keySession = writeClient.createFile(keyArgs, false, true);
keyArgs.setLocationInfoList(keySession.getKeyInfo().getLatestVersionLocations().getLocationList());
writeClient.commitKey(keyArgs, keySession.getId());
reset(mockScmContainerClient);
OzoneObj fileKey = OzoneObjInfo.Builder.fromKeyArgs(keyArgs).setStoreType(OzoneObj.StoreType.OZONE).build();
RequestContext context = currentUserReads();
// WHEN
boolean access = keyManager.checkAccess(fileKey, context);
// THEN
Assert.assertTrue(access);
verify(mockScmContainerClient, never()).getContainerWithPipelineBatch(any());
}
use of org.apache.hadoop.ozone.om.helpers.OpenKeySession in project ozone by apache.
the class TestKeyManagerImpl method testLookupKeyWithLocation.
@Test
public void testLookupKeyWithLocation() throws IOException {
String keyName = RandomStringUtils.randomAlphabetic(5);
OmKeyArgs keyArgs = createBuilder().setKeyName(keyName).setSortDatanodesInPipeline(true).build();
// lookup for a non-existent key
try {
keyManager.lookupKey(keyArgs, null);
Assert.fail("Lookup key should fail for non existent key");
} catch (OMException ex) {
if (ex.getResult() != OMException.ResultCodes.KEY_NOT_FOUND) {
throw ex;
}
}
// create a key
OpenKeySession keySession = writeClient.createFile(keyArgs, false, false);
// randomly select 3 datanodes
List<DatanodeDetails> nodeList = new ArrayList<>();
nodeList.add((DatanodeDetails) scm.getClusterMap().getNode(0, null, null, null, null, 0));
nodeList.add((DatanodeDetails) scm.getClusterMap().getNode(1, null, null, null, null, 0));
nodeList.add((DatanodeDetails) scm.getClusterMap().getNode(2, null, null, null, null, 0));
Assume.assumeFalse(nodeList.get(0).equals(nodeList.get(1)));
Assume.assumeFalse(nodeList.get(0).equals(nodeList.get(2)));
// create a pipeline using 3 datanodes
Pipeline pipeline = scm.getPipelineManager().createPipeline(RatisReplicationConfig.getInstance(ReplicationFactor.THREE), nodeList);
List<OmKeyLocationInfo> locationInfoList = new ArrayList<>();
List<OmKeyLocationInfo> locationList = keySession.getKeyInfo().getLatestVersionLocations().getLocationList();
Assert.assertEquals(1, locationList.size());
locationInfoList.add(new OmKeyLocationInfo.Builder().setPipeline(pipeline).setBlockID(new BlockID(locationList.get(0).getContainerID(), locationList.get(0).getLocalID())).build());
keyArgs.setLocationInfoList(locationInfoList);
writeClient.commitKey(keyArgs, keySession.getId());
ContainerInfo containerInfo = new ContainerInfo.Builder().setContainerID(1L).setPipelineID(pipeline.getId()).build();
List<ContainerWithPipeline> containerWithPipelines = Arrays.asList(new ContainerWithPipeline(containerInfo, pipeline));
when(mockScmContainerClient.getContainerWithPipelineBatch(Arrays.asList(1L))).thenReturn(containerWithPipelines);
OmKeyInfo key = keyManager.lookupKey(keyArgs, null);
Assert.assertEquals(key.getKeyName(), keyName);
List<OmKeyLocationInfo> keyLocations = key.getLatestVersionLocations().getLocationList();
DatanodeDetails leader = keyLocations.get(0).getPipeline().getFirstNode();
DatanodeDetails follower1 = keyLocations.get(0).getPipeline().getNodes().get(1);
DatanodeDetails follower2 = keyLocations.get(0).getPipeline().getNodes().get(2);
Assert.assertNotEquals(leader, follower1);
Assert.assertNotEquals(follower1, follower2);
// lookup key, leader as client
OmKeyInfo key1 = keyManager.lookupKey(keyArgs, leader.getIpAddress());
Assert.assertEquals(leader, key1.getLatestVersionLocations().getLocationList().get(0).getPipeline().getClosestNode());
// lookup key, follower1 as client
OmKeyInfo key2 = keyManager.lookupKey(keyArgs, follower1.getIpAddress());
Assert.assertEquals(follower1, key2.getLatestVersionLocations().getLocationList().get(0).getPipeline().getClosestNode());
// lookup key, follower2 as client
OmKeyInfo key3 = keyManager.lookupKey(keyArgs, follower2.getIpAddress());
Assert.assertEquals(follower2, key3.getLatestVersionLocations().getLocationList().get(0).getPipeline().getClosestNode());
// lookup key, random node as client
OmKeyInfo key4 = keyManager.lookupKey(keyArgs, "/d=default-drack/127.0.0.1");
Assert.assertEquals(leader, key4.getLatestVersionLocations().getLocationList().get(0).getPipeline().getClosestNode());
}
use of org.apache.hadoop.ozone.om.helpers.OpenKeySession in project ozone by apache.
the class TestKeyManagerImpl method testLatestLocationVersion.
@Test
public void testLatestLocationVersion() throws IOException {
String keyName = RandomStringUtils.randomAlphabetic(5);
OmKeyArgs keyArgs = createBuilder(VERSIONED_BUCKET_NAME).setKeyName(keyName).setLatestVersionLocation(true).build();
// lookup for a non-existent key
try {
keyManager.lookupKey(keyArgs, null);
Assert.fail("Lookup key should fail for non existent key");
} catch (OMException ex) {
if (ex.getResult() != OMException.ResultCodes.KEY_NOT_FOUND) {
throw ex;
}
}
// create a key
OpenKeySession keySession = writeClient.createFile(keyArgs, false, false);
// randomly select 3 datanodes
List<DatanodeDetails> nodeList = new ArrayList<>();
nodeList.add((DatanodeDetails) scm.getClusterMap().getNode(0, null, null, null, null, 0));
nodeList.add((DatanodeDetails) scm.getClusterMap().getNode(1, null, null, null, null, 0));
nodeList.add((DatanodeDetails) scm.getClusterMap().getNode(2, null, null, null, null, 0));
Assume.assumeFalse(nodeList.get(0).equals(nodeList.get(1)));
Assume.assumeFalse(nodeList.get(0).equals(nodeList.get(2)));
// create a pipeline using 3 datanodes
Pipeline pipeline = scm.getPipelineManager().createPipeline(RatisReplicationConfig.getInstance(ReplicationFactor.THREE), nodeList);
List<OmKeyLocationInfo> locationInfoList = new ArrayList<>();
List<OmKeyLocationInfo> locationList = keySession.getKeyInfo().getLatestVersionLocations().getLocationList();
Assert.assertEquals(1, locationList.size());
locationInfoList.add(new OmKeyLocationInfo.Builder().setPipeline(pipeline).setBlockID(new BlockID(locationList.get(0).getContainerID(), locationList.get(0).getLocalID())).build());
keyArgs.setLocationInfoList(locationInfoList);
writeClient.commitKey(keyArgs, keySession.getId());
// Mock out the pipelines from the SCM
ContainerInfo containerInfo = new ContainerInfo.Builder().setContainerID(1L).setPipelineID(pipeline.getId()).build();
List<ContainerWithPipeline> containerWithPipelines = Arrays.asList(new ContainerWithPipeline(containerInfo, pipeline));
when(mockScmContainerClient.getContainerWithPipelineBatch(Arrays.asList(1L))).thenReturn(containerWithPipelines);
OmKeyInfo key = keyManager.lookupKey(keyArgs, null);
Assert.assertEquals(key.getKeyLocationVersions().size(), 1);
keySession = writeClient.createFile(keyArgs, true, true);
writeClient.commitKey(keyArgs, keySession.getId());
// Test lookupKey (latestLocationVersion == true)
key = keyManager.lookupKey(keyArgs, null);
Assert.assertEquals(key.getKeyLocationVersions().size(), 1);
// Test ListStatus (latestLocationVersion == true)
List<OzoneFileStatus> fileStatuses = keyManager.listStatus(keyArgs, false, "", 1);
Assert.assertEquals(fileStatuses.size(), 1);
Assert.assertEquals(fileStatuses.get(0).getKeyInfo().getKeyLocationVersions().size(), 1);
// Test GetFileStatus (latestLocationVersion == true)
OzoneFileStatus ozoneFileStatus = keyManager.getFileStatus(keyArgs, null);
Assert.assertEquals(ozoneFileStatus.getKeyInfo().getKeyLocationVersions().size(), 1);
// Test LookupFile (latestLocationVersion == true)
key = keyManager.lookupFile(keyArgs, null);
Assert.assertEquals(key.getKeyLocationVersions().size(), 1);
keyArgs = createBuilder(VERSIONED_BUCKET_NAME).setKeyName(keyName).setLatestVersionLocation(false).build();
// Test lookupKey (latestLocationVersion == false)
key = keyManager.lookupKey(keyArgs, null);
Assert.assertEquals(key.getKeyLocationVersions().size(), 2);
// Test ListStatus (latestLocationVersion == false)
fileStatuses = keyManager.listStatus(keyArgs, false, "", 100);
Assert.assertEquals(fileStatuses.size(), 1);
Assert.assertEquals(fileStatuses.get(0).getKeyInfo().getKeyLocationVersions().size(), 2);
// Test GetFileStatus (latestLocationVersion == false)
ozoneFileStatus = keyManager.getFileStatus(keyArgs, null);
Assert.assertEquals(ozoneFileStatus.getKeyInfo().getKeyLocationVersions().size(), 2);
// Test LookupFile (latestLocationVersion == false)
key = keyManager.lookupFile(keyArgs, null);
Assert.assertEquals(key.getKeyLocationVersions().size(), 2);
// Test ListKeys (latestLocationVersion is always true for ListKeys)
List<OmKeyInfo> keyInfos = keyManager.listKeys(keyArgs.getVolumeName(), keyArgs.getBucketName(), "", keyArgs.getKeyName(), 100);
Assert.assertEquals(keyInfos.size(), 1);
Assert.assertEquals(keyInfos.get(0).getKeyLocationVersions().size(), 1);
}
use of org.apache.hadoop.ozone.om.helpers.OpenKeySession in project ozone by apache.
the class TestKeyManagerImpl method testLookupFile.
@Test
public void testLookupFile() throws IOException {
String keyName = RandomStringUtils.randomAlphabetic(5);
OmKeyArgs keyArgs = createBuilder().setKeyName(keyName).build();
// lookup for a non-existent file
try {
keyManager.lookupFile(keyArgs, null);
Assert.fail("Lookup file should fail for non existent file");
} catch (OMException ex) {
if (ex.getResult() != OMException.ResultCodes.FILE_NOT_FOUND) {
throw ex;
}
}
// create a file
OpenKeySession keySession = writeClient.createFile(keyArgs, false, false);
keyArgs.setLocationInfoList(keySession.getKeyInfo().getLatestVersionLocations().getLocationList());
writeClient.commitKey(keyArgs, keySession.getId());
Assert.assertEquals(keyManager.lookupFile(keyArgs, null).getKeyName(), keyName);
// lookup for created file
keyArgs = createBuilder().setKeyName("").build();
try {
keyManager.lookupFile(keyArgs, null);
Assert.fail("Lookup file should fail for a directory");
} catch (OMException ex) {
if (ex.getResult() != OMException.ResultCodes.NOT_A_FILE) {
throw ex;
}
}
}
Aggregations