use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeyRequest in project ozone by apache.
the class OMKeyRenameRequest method preExecute.
@Override
public OMRequest preExecute(OzoneManager ozoneManager) throws IOException {
RenameKeyRequest renameKeyRequest = getOmRequest().getRenameKeyRequest();
Preconditions.checkNotNull(renameKeyRequest);
// Verify key name
final boolean checkKeyNameEnabled = ozoneManager.getConfiguration().getBoolean(OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_KEY, OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_DEFAULT);
if (checkKeyNameEnabled) {
OmUtils.validateKeyName(renameKeyRequest.getToKeyName());
}
KeyArgs renameKeyArgs = renameKeyRequest.getKeyArgs();
String srcKey = renameKeyArgs.getKeyName();
String dstKey = renameKeyRequest.getToKeyName();
if (getBucketLayout().isFileSystemOptimized()) {
srcKey = validateAndNormalizeKey(ozoneManager.getEnableFileSystemPaths(), srcKey, getBucketLayout());
dstKey = validateAndNormalizeKey(ozoneManager.getEnableFileSystemPaths(), dstKey, getBucketLayout());
}
// Set modification time & srcKeyName.
KeyArgs.Builder newKeyArgs = renameKeyArgs.toBuilder().setModificationTime(Time.now()).setKeyName(srcKey);
return getOmRequest().toBuilder().setRenameKeyRequest(renameKeyRequest.toBuilder().setToKeyName(dstKey).setKeyArgs(newKeyArgs)).setUserInfo(getUserIfNotExists(ozoneManager)).build();
}
use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeyRequest in project ozone by apache.
the class TestOMKeyRenameRequest method createRenameKeyRequest.
/**
* Create OMRequest which encapsulates RenameKeyRequest.
* @return OMRequest
*/
private OMRequest createRenameKeyRequest(String toKeyName) {
KeyArgs keyArgs = KeyArgs.newBuilder().setKeyName(keyName).setVolumeName(volumeName).setBucketName(bucketName).build();
RenameKeyRequest renameKeyRequest = RenameKeyRequest.newBuilder().setKeyArgs(keyArgs).setToKeyName(toKeyName).build();
return OMRequest.newBuilder().setClientId(UUID.randomUUID().toString()).setRenameKeyRequest(renameKeyRequest).setCmdType(OzoneManagerProtocolProtos.Type.RenameKey).build();
}
use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeyRequest in project ozone by apache.
the class OMKeyRenameRequest method validateAndUpdateCache.
@Override
@SuppressWarnings("methodlength")
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
RenameKeyRequest renameKeyRequest = getOmRequest().getRenameKeyRequest();
OzoneManagerProtocolProtos.KeyArgs keyArgs = renameKeyRequest.getKeyArgs();
Map<String, String> auditMap = buildAuditMap(keyArgs, renameKeyRequest);
String volumeName = keyArgs.getVolumeName();
String bucketName = keyArgs.getBucketName();
String fromKeyName = keyArgs.getKeyName();
String toKeyName = renameKeyRequest.getToKeyName();
OMMetrics omMetrics = ozoneManager.getMetrics();
omMetrics.incNumKeyRenames();
AuditLogger auditLogger = ozoneManager.getAuditLogger();
OMResponse.Builder omResponse = OmResponseUtil.getOMResponseBuilder(getOmRequest());
OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
boolean acquiredLock = false;
OMClientResponse omClientResponse = null;
IOException exception = null;
OmKeyInfo fromKeyValue = null;
String toKey = null, fromKey = null;
Result result = null;
try {
if (toKeyName.length() == 0 || fromKeyName.length() == 0) {
throw new OMException("Key name is empty", OMException.ResultCodes.INVALID_KEY_NAME);
}
keyArgs = resolveBucketLink(ozoneManager, keyArgs, auditMap);
volumeName = keyArgs.getVolumeName();
bucketName = keyArgs.getBucketName();
// 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);
checkKeyAcls(ozoneManager, volumeName, bucketName, toKeyName, IAccessAuthorizer.ACLType.CREATE, OzoneObj.ResourceType.KEY);
acquiredLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
// Validate bucket and volume exists or not.
validateBucketAndVolume(omMetadataManager, volumeName, bucketName);
// Check if toKey exists
fromKey = omMetadataManager.getOzoneKey(volumeName, bucketName, fromKeyName);
toKey = omMetadataManager.getOzoneKey(volumeName, bucketName, toKeyName);
OmKeyInfo toKeyValue = omMetadataManager.getKeyTable(getBucketLayout()).get(toKey);
if (toKeyValue != null) {
throw new OMException("Key already exists " + toKeyName, OMException.ResultCodes.KEY_ALREADY_EXISTS);
}
// fromKeyName should exist
fromKeyValue = omMetadataManager.getKeyTable(getBucketLayout()).get(fromKey);
if (fromKeyValue == null) {
// TODO: Add support for renaming open key
throw new OMException("Key not found " + fromKey, KEY_NOT_FOUND);
}
fromKeyValue.setUpdateID(trxnLogIndex, ozoneManager.isRatisEnabled());
fromKeyValue.setKeyName(toKeyName);
// Set modification time
fromKeyValue.setModificationTime(keyArgs.getModificationTime());
// 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));
omClientResponse = new OMKeyRenameResponse(omResponse.setRenameKeyResponse(RenameKeyResponse.newBuilder()).build(), fromKeyName, toKeyName, fromKeyValue, getBucketLayout());
result = Result.SUCCESS;
} catch (IOException ex) {
result = Result.FAILURE;
exception = ex;
omClientResponse = new OMKeyRenameResponse(createErrorOMResponse(omResponse, exception), getBucketLayout());
} finally {
addResponseToDoubleBuffer(trxnLogIndex, omClientResponse, omDoubleBufferHelper);
if (acquiredLock) {
omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volumeName, bucketName);
}
}
auditLog(auditLogger, buildAuditMessage(OMAction.RENAME_KEY, auditMap, exception, getOmRequest().getUserInfo()));
switch(result) {
case SUCCESS:
LOG.debug("Rename Key is successfully completed for volume:{} bucket:{}" + " fromKey:{} toKey:{}. ", volumeName, bucketName, fromKeyName, toKeyName);
break;
case FAILURE:
ozoneManager.getMetrics().incNumKeyRenameFails();
LOG.error("Rename key failed for volume:{} bucket:{} fromKey:{} " + "toKey:{}. Key: {} not found.", volumeName, bucketName, fromKeyName, toKeyName, fromKeyName);
break;
default:
LOG.error("Unrecognized Result for OMKeyRenameRequest: {}", renameKeyRequest);
}
return omClientResponse;
}
use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeyRequest in project ozone by apache.
the class OMKeyRenameRequestWithFSO method validateAndUpdateCache.
@Override
@SuppressWarnings("methodlength")
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
RenameKeyRequest renameKeyRequest = getOmRequest().getRenameKeyRequest();
KeyArgs keyArgs = renameKeyRequest.getKeyArgs();
Map<String, String> auditMap = buildAuditMap(keyArgs, renameKeyRequest);
String volumeName = keyArgs.getVolumeName();
String bucketName = keyArgs.getBucketName();
String fromKeyName = keyArgs.getKeyName();
String toKeyName = renameKeyRequest.getToKeyName();
OMMetrics omMetrics = ozoneManager.getMetrics();
omMetrics.incNumKeyRenames();
AuditLogger auditLogger = ozoneManager.getAuditLogger();
OMResponse.Builder omResponse = OmResponseUtil.getOMResponseBuilder(getOmRequest());
OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
boolean acquiredLock = false;
OMClientResponse omClientResponse = null;
IOException exception = null;
OmKeyInfo fromKeyValue;
Result result;
try {
if (toKeyName.length() == 0 || fromKeyName.length() == 0) {
throw new OMException("Key name is empty", OMException.ResultCodes.INVALID_KEY_NAME);
}
keyArgs = resolveBucketLink(ozoneManager, keyArgs, auditMap);
volumeName = keyArgs.getVolumeName();
bucketName = keyArgs.getBucketName();
// check Acls to see if user has access to perform delete operation on
// old key and create operation on new key
// check Acl fromKeyName
checkACLsWithFSO(ozoneManager, volumeName, bucketName, fromKeyName, IAccessAuthorizer.ACLType.DELETE);
// check Acl toKeyName
checkKeyAcls(ozoneManager, volumeName, bucketName, toKeyName, IAccessAuthorizer.ACLType.CREATE, OzoneObj.ResourceType.KEY);
acquiredLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
// Validate bucket and volume exists or not.
validateBucketAndVolume(omMetadataManager, volumeName, bucketName);
// Check if fromKey exists
OzoneFileStatus fromKeyFileStatus = OMFileRequest.getOMKeyInfoIfExists(omMetadataManager, volumeName, bucketName, fromKeyName, 0);
// case-1) fromKeyName should exist, otw throws exception
if (fromKeyFileStatus == null) {
// TODO: Add support for renaming open key
throw new OMException("Key not found " + fromKeyName, KEY_NOT_FOUND);
}
// source existed
fromKeyValue = fromKeyFileStatus.getKeyInfo();
boolean isRenameDirectory = fromKeyFileStatus.isDirectory();
// case-2) Cannot rename a directory to its own subdirectory
OMFileRequest.verifyToDirIsASubDirOfFromDirectory(fromKeyName, toKeyName, fromKeyFileStatus.isDirectory());
OzoneFileStatus toKeyFileStatus = OMFileRequest.getOMKeyInfoIfExists(omMetadataManager, volumeName, bucketName, toKeyName, 0);
// Check if toKey exists.
if (toKeyFileStatus != null) {
// Destination exists and following are different cases:
OmKeyInfo toKeyValue = toKeyFileStatus.getKeyInfo();
if (fromKeyValue.getKeyName().equals(toKeyValue.getKeyName())) {
// TODO: Discuss do we need to throw exception for file as well.
if (toKeyFileStatus.isFile()) {
result = Result.SUCCESS;
} else {
throw new OMException("Key already exists " + toKeyName, OMException.ResultCodes.KEY_ALREADY_EXISTS);
}
} else if (toKeyFileStatus.isDirectory()) {
// case-4) If dst is a directory then rename source as sub-path of it
// For example: rename /source to /dst will lead to /dst/source
String fromFileName = OzoneFSUtils.getFileName(fromKeyName);
String newToKeyName = OzoneFSUtils.appendFileNameToKeyPath(toKeyName, fromFileName);
OzoneFileStatus newToOzoneFileStatus = OMFileRequest.getOMKeyInfoIfExists(omMetadataManager, volumeName, bucketName, newToKeyName, 0);
if (newToOzoneFileStatus != null) {
// case-5) If new destin '/dst/source' exists then throws exception
throw new OMException(String.format("Failed to rename %s to %s, file already exists or not " + "empty!", fromKeyName, newToKeyName), OMException.ResultCodes.KEY_ALREADY_EXISTS);
}
omClientResponse = renameKey(toKeyValue.getObjectID(), trxnLogIndex, fromKeyValue, isRenameDirectory, newToKeyName, keyArgs.getModificationTime(), omResponse, ozoneManager);
result = Result.SUCCESS;
} else {
// key already exists exception.
throw new OMException("Failed to rename, key already exists " + toKeyName, OMException.ResultCodes.KEY_ALREADY_EXISTS);
}
} else {
// Destination doesn't exist and the cases are:
// case-7) Check whether dst parent dir exists or not. If parent
// doesn't exist then throw exception, otw the source can be renamed to
// destination path.
long toKeyParentId = OMFileRequest.getToKeyNameParentId(volumeName, bucketName, toKeyName, fromKeyName, omMetadataManager);
omClientResponse = renameKey(toKeyParentId, trxnLogIndex, fromKeyValue, isRenameDirectory, toKeyName, keyArgs.getModificationTime(), omResponse, ozoneManager);
result = Result.SUCCESS;
}
} catch (IOException ex) {
result = Result.FAILURE;
exception = ex;
omClientResponse = new OMKeyRenameResponseWithFSO(createErrorOMResponse(omResponse, exception), getBucketLayout());
} finally {
addResponseToDoubleBuffer(trxnLogIndex, omClientResponse, omDoubleBufferHelper);
if (acquiredLock) {
omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volumeName, bucketName);
}
}
auditLog(auditLogger, buildAuditMessage(OMAction.RENAME_KEY, auditMap, exception, getOmRequest().getUserInfo()));
switch(result) {
case SUCCESS:
LOG.debug("Rename Key is successfully completed for volume:{} bucket:{}" + " fromKey:{} toKey:{}. ", volumeName, bucketName, fromKeyName, toKeyName);
break;
case FAILURE:
ozoneManager.getMetrics().incNumKeyRenameFails();
LOG.error("Rename key failed for volume:{} bucket:{} fromKey:{} " + "toKey:{}. Key: {} not found.", volumeName, bucketName, fromKeyName, toKeyName, fromKeyName);
break;
default:
LOG.error("Unrecognized Result for OMKeyRenameRequest: {}", renameKeyRequest);
}
return omClientResponse;
}
use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeyRequest in project ozone by apache.
the class TestOMKeyRenameRequestWithFSO method createRenameKeyRequest.
/**
* Create OMRequest which encapsulates RenameKeyRequest.
*
* @return OMRequest
*/
private OMRequest createRenameKeyRequest(String toKeyName, String fromKeyName) {
KeyArgs keyArgs = KeyArgs.newBuilder().setKeyName(fromKeyName).setVolumeName(volumeName).setBucketName(bucketName).build();
RenameKeyRequest renameKeyRequest = RenameKeyRequest.newBuilder().setKeyArgs(keyArgs).setToKeyName(toKeyName).build();
return OMRequest.newBuilder().setClientId(UUID.randomUUID().toString()).setRenameKeyRequest(renameKeyRequest).setCmdType(OzoneManagerProtocolProtos.Type.RenameKey).build();
}
Aggregations