use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap in project ozone by apache.
the class TestOMKeysRenameRequest method testKeysRenameRequestFail.
@Test
public void testKeysRenameRequestFail() throws Exception {
OMRequest modifiedOmRequest = createRenameKeyRequest(true);
OMKeysRenameRequest omKeysRenameRequest = new OMKeysRenameRequest(modifiedOmRequest);
OMClientResponse omKeysRenameResponse = omKeysRenameRequest.validateAndUpdateCache(ozoneManager, 100L, ozoneManagerDoubleBufferHelper);
Assert.assertFalse(omKeysRenameResponse.getOMResponse().getSuccess());
Assert.assertEquals(OzoneManagerProtocolProtos.Status.PARTIAL_RENAME, omKeysRenameResponse.getOMResponse().getStatus());
// The keys(key0 to key9)can be renamed success.
for (int i = 0; i < count; i++) {
// Original key should be deleted, toKey should exist.
OmKeyInfo omKeyInfo = omMetadataManager.getKeyTable(getBucketLayout()).get(omMetadataManager.getOzoneKey(volumeName, bucketName, parentDir.concat("/key" + i)));
Assert.assertNull(omKeyInfo);
omKeyInfo = omMetadataManager.getKeyTable(getBucketLayout()).get(omMetadataManager.getOzoneKey(volumeName, bucketName, parentDir.concat("/newKey" + i)));
Assert.assertNotNull(omKeyInfo);
}
// The key not rename should be in unRenamedKeys.
RenameKeysMap unRenamedKeys = omKeysRenameResponse.getOMResponse().getRenameKeysResponse().getUnRenamedKeys(0);
Assert.assertEquals("testKey", unRenamedKeys.getFromKeyName());
}
use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap in project ozone by apache.
the class OMKeysRenameRequest method buildAuditMap.
/**
* Build audit map for RenameKeys request.
*
* @param auditMap
* @param renamedKeys
* @param unRenameKeys
* @return
*/
private Map<String, String> buildAuditMap(Map<String, String> auditMap, Map<String, String> renamedKeys, List<RenameKeysMap> unRenameKeys) {
Map<String, String> unRenameKeysMap = new HashMap<>();
for (RenameKeysMap renameKeysMap : unRenameKeys) {
unRenameKeysMap.put(renameKeysMap.getFromKeyName(), renameKeysMap.getToKeyName());
}
auditMap.put(RENAMED_KEYS_MAP, renamedKeys.toString());
auditMap.put(UNRENAMED_KEYS_MAP, unRenameKeysMap.toString());
return auditMap;
}
use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap in project ozone by apache.
the class OMKeysRenameRequest method validateAndUpdateCache.
@Override
@SuppressWarnings("methodlength")
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
RenameKeysRequest renameKeysRequest = getOmRequest().getRenameKeysRequest();
RenameKeysArgs renameKeysArgs = renameKeysRequest.getRenameKeysArgs();
String volumeName = renameKeysArgs.getVolumeName();
String bucketName = renameKeysArgs.getBucketName();
OMClientResponse omClientResponse = null;
List<RenameKeysMap> unRenamedKeys = new ArrayList<>();
// fromKeyName -> toKeyName
Map<String, String> renamedKeys = new HashMap<>();
Map<String, OmKeyInfo> fromKeyAndToKeyInfo = new HashMap<>();
OMMetrics omMetrics = ozoneManager.getMetrics();
omMetrics.incNumKeyRenames();
AuditLogger auditLogger = ozoneManager.getAuditLogger();
OMResponse.Builder omResponse = OmResponseUtil.getOMResponseBuilder(getOmRequest());
OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
IOException exception = null;
OmKeyInfo fromKeyValue = null;
Result result = null;
Map<String, String> auditMap = new LinkedHashMap<>();
String fromKeyName = null;
String toKeyName = null;
boolean acquiredLock = false;
boolean renameStatus = true;
try {
ResolvedBucket bucket = ozoneManager.resolveBucketLink(Pair.of(volumeName, bucketName), this);
bucket.audit(auditMap);
volumeName = bucket.realVolume();
bucketName = bucket.realBucket();
acquiredLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
// Validate bucket and volume exists or not.
validateBucketAndVolume(omMetadataManager, volumeName, bucketName);
String volumeOwner = getVolumeOwner(omMetadataManager, volumeName);
for (RenameKeysMap renameKey : renameKeysArgs.getRenameKeysMapList()) {
fromKeyName = renameKey.getFromKeyName();
toKeyName = renameKey.getToKeyName();
RenameKeysMap.Builder unRenameKey = RenameKeysMap.newBuilder();
if (toKeyName.length() == 0 || fromKeyName.length() == 0) {
renameStatus = false;
unRenamedKeys.add(unRenameKey.setFromKeyName(fromKeyName).setToKeyName(toKeyName).build());
LOG.error("Key name is empty fromKeyName {} toKeyName {}", fromKeyName, toKeyName);
continue;
}
try {
// check Acls to see if user has access to perform delete operation
// on old key and create operation on new key
checkKeyAcls(ozoneManager, volumeName, bucketName, fromKeyName, IAccessAuthorizer.ACLType.DELETE, OzoneObj.ResourceType.KEY, volumeOwner);
checkKeyAcls(ozoneManager, volumeName, bucketName, toKeyName, IAccessAuthorizer.ACLType.CREATE, OzoneObj.ResourceType.KEY, volumeOwner);
} catch (Exception ex) {
renameStatus = false;
unRenamedKeys.add(unRenameKey.setFromKeyName(fromKeyName).setToKeyName(toKeyName).build());
LOG.error("Acl check failed for fromKeyName {} toKeyName {}", fromKeyName, toKeyName, ex);
continue;
}
// Check if toKey exists
String fromKey = omMetadataManager.getOzoneKey(volumeName, bucketName, fromKeyName);
String toKey = omMetadataManager.getOzoneKey(volumeName, bucketName, toKeyName);
OmKeyInfo toKeyValue = omMetadataManager.getKeyTable(getBucketLayout()).get(toKey);
if (toKeyValue != null) {
renameStatus = false;
unRenamedKeys.add(unRenameKey.setFromKeyName(fromKeyName).setToKeyName(toKeyName).build());
LOG.error("Received a request name of new key {} already exists", toKeyName);
}
// fromKeyName should exist
fromKeyValue = omMetadataManager.getKeyTable(getBucketLayout()).get(fromKey);
if (fromKeyValue == null) {
renameStatus = false;
unRenamedKeys.add(unRenameKey.setFromKeyName(fromKeyName).setToKeyName(toKeyName).build());
LOG.error("Received a request to rename a Key does not exist {}", fromKey);
continue;
}
fromKeyValue.setUpdateID(trxnLogIndex, ozoneManager.isRatisEnabled());
fromKeyValue.setKeyName(toKeyName);
// Set modification time
fromKeyValue.setModificationTime(Time.now());
// Add to cache.
// fromKey should be deleted, toKey should be added with newly updated
// omKeyInfo.
Table<String, OmKeyInfo> keyTable = omMetadataManager.getKeyTable(getBucketLayout());
keyTable.addCacheEntry(new CacheKey<>(fromKey), new CacheValue<>(Optional.absent(), trxnLogIndex));
keyTable.addCacheEntry(new CacheKey<>(toKey), new CacheValue<>(Optional.of(fromKeyValue), trxnLogIndex));
renamedKeys.put(fromKeyName, toKeyName);
fromKeyAndToKeyInfo.put(fromKeyName, fromKeyValue);
}
OmRenameKeys newOmRenameKeys = new OmRenameKeys(volumeName, bucketName, null, fromKeyAndToKeyInfo);
omClientResponse = new OMKeysRenameResponse(omResponse.setRenameKeysResponse(RenameKeysResponse.newBuilder().setStatus(renameStatus).addAllUnRenamedKeys(unRenamedKeys)).setStatus(renameStatus ? OK : PARTIAL_RENAME).setSuccess(renameStatus).build(), newOmRenameKeys);
result = Result.SUCCESS;
} catch (IOException ex) {
result = Result.FAILURE;
exception = ex;
createErrorOMResponse(omResponse, ex);
omResponse.setRenameKeysResponse(RenameKeysResponse.newBuilder().setStatus(renameStatus).addAllUnRenamedKeys(unRenamedKeys).build());
omClientResponse = new OMKeysRenameResponse(omResponse.build());
} finally {
addResponseToDoubleBuffer(trxnLogIndex, omClientResponse, omDoubleBufferHelper);
if (acquiredLock) {
omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volumeName, bucketName);
}
}
auditMap = buildAuditMap(auditMap, renamedKeys, unRenamedKeys);
auditLog(auditLogger, buildAuditMessage(OMAction.RENAME_KEYS, auditMap, exception, getOmRequest().getUserInfo()));
switch(result) {
case SUCCESS:
LOG.debug("Rename Keys is successfully completed for auditMap:{}.", auditMap);
break;
case FAILURE:
ozoneManager.getMetrics().incNumKeyRenameFails();
LOG.error("Rename keys failed for auditMap:{}.", auditMap);
break;
default:
LOG.error("Unrecognized Result for OMKeysRenameRequest: {}", renameKeysRequest);
}
return omClientResponse;
}
use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap in project ozone by apache.
the class OzoneManagerProtocolClientSideTranslatorPB method renameKeys.
@Override
public void renameKeys(OmRenameKeys omRenameKeys) throws IOException {
List<RenameKeysMap> renameKeyList = new ArrayList<>();
for (Map.Entry<String, String> entry : omRenameKeys.getFromAndToKey().entrySet()) {
RenameKeysMap.Builder renameKey = RenameKeysMap.newBuilder().setFromKeyName(entry.getKey()).setToKeyName(entry.getValue());
renameKeyList.add(renameKey.build());
}
RenameKeysArgs.Builder renameKeyArgs = RenameKeysArgs.newBuilder().setVolumeName(omRenameKeys.getVolume()).setBucketName(omRenameKeys.getBucket()).addAllRenameKeysMap(renameKeyList);
RenameKeysRequest.Builder reqKeys = RenameKeysRequest.newBuilder().setRenameKeysArgs(renameKeyArgs.build());
OMRequest omRequest = createOMRequest(Type.RenameKeys).setRenameKeysRequest(reqKeys.build()).build();
handleError(submitRequest(omRequest));
}
Aggregations