use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.SetBucketPropertyRequest in project ozone by apache.
the class OMBucketSetOwnerRequest method validateAndUpdateCache.
@Override
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long transactionLogIndex, OzoneManagerDoubleBufferHelper ozoneManagerDoubleBufferHelper) {
SetBucketPropertyRequest setBucketPropertyRequest = getOmRequest().getSetBucketPropertyRequest();
Preconditions.checkNotNull(setBucketPropertyRequest);
OMResponse.Builder omResponse = OmResponseUtil.getOMResponseBuilder(getOmRequest());
if (!setBucketPropertyRequest.getBucketArgs().hasOwnerName()) {
omResponse.setStatus(OzoneManagerProtocolProtos.Status.INVALID_REQUEST).setSuccess(false);
return new OMBucketSetOwnerResponse(omResponse.build());
}
OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
OMMetrics omMetrics = ozoneManager.getMetrics();
omMetrics.incNumBucketUpdates();
BucketArgs bucketArgs = setBucketPropertyRequest.getBucketArgs();
OmBucketArgs omBucketArgs = OmBucketArgs.getFromProtobuf(bucketArgs);
String volumeName = bucketArgs.getVolumeName();
String bucketName = bucketArgs.getBucketName();
String newOwner = bucketArgs.getOwnerName();
String oldOwner = null;
AuditLogger auditLogger = ozoneManager.getAuditLogger();
OzoneManagerProtocolProtos.UserInfo userInfo = getOmRequest().getUserInfo();
IOException exception = null;
boolean acquiredBucketLock = false, success = true;
OMClientResponse omClientResponse = null;
try {
// check Acl
if (ozoneManager.getAclsEnabled()) {
checkAcls(ozoneManager, OzoneObj.ResourceType.BUCKET, OzoneObj.StoreType.OZONE, IAccessAuthorizer.ACLType.WRITE_ACL, volumeName, bucketName, null);
}
// acquire lock.
acquiredBucketLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
String bucketKey = omMetadataManager.getBucketKey(volumeName, bucketName);
OmBucketInfo omBucketInfo = omMetadataManager.getBucketTable().get(bucketKey);
// Check if bucket exist
if (omBucketInfo == null) {
LOG.debug("Bucket: {} not found ", bucketName);
throw new OMException("Bucket doesnt exist", OMException.ResultCodes.BUCKET_NOT_FOUND);
}
oldOwner = omBucketInfo.getOwner();
if (oldOwner.equals(newOwner)) {
LOG.warn("Bucket '{}/{}' owner is already user '{}'.", volumeName, bucketName, oldOwner);
omResponse.setStatus(OzoneManagerProtocolProtos.Status.OK).setMessage("Bucket '" + volumeName + "/" + bucketName + "' owner is already '" + newOwner + "'.").setSuccess(false);
omResponse.setSetBucketPropertyResponse(SetBucketPropertyResponse.newBuilder().setResponse(false).build());
omClientResponse = new OMBucketSetOwnerResponse(omResponse.build());
return omClientResponse;
}
omBucketInfo.setOwner(newOwner);
LOG.debug("Updating bucket owner to {} for bucket: {} in volume: {}", newOwner, bucketName, volumeName);
omBucketInfo.setModificationTime(setBucketPropertyRequest.getModificationTime());
// Set the updateID to current transaction log index
omBucketInfo.setUpdateID(transactionLogIndex, ozoneManager.isRatisEnabled());
// Update table cache.
omMetadataManager.getBucketTable().addCacheEntry(new CacheKey<>(bucketKey), new CacheValue<>(Optional.of(omBucketInfo), transactionLogIndex));
omResponse.setSetBucketPropertyResponse(SetBucketPropertyResponse.newBuilder().setResponse(true).build());
omClientResponse = new OMBucketSetOwnerResponse(omResponse.build(), omBucketInfo);
} catch (IOException ex) {
success = false;
exception = ex;
omClientResponse = new OMBucketSetOwnerResponse(createErrorOMResponse(omResponse, exception));
} finally {
addResponseToDoubleBuffer(transactionLogIndex, omClientResponse, ozoneManagerDoubleBufferHelper);
if (acquiredBucketLock) {
omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volumeName, bucketName);
}
}
// Performing audit logging outside of the lock.
auditLog(auditLogger, buildAuditMessage(OMAction.SET_OWNER, omBucketArgs.toAuditMap(), exception, userInfo));
// return response.
if (success) {
LOG.debug("Successfully changed Owner of Bucket {}/{} from {} -> {}", volumeName, bucketName, oldOwner, newOwner);
return omClientResponse;
} else {
LOG.error("Setting Owner failed for bucket:{} in volume:{}", bucketName, volumeName, exception);
omMetrics.incNumBucketUpdateFails();
return omClientResponse;
}
}
use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.SetBucketPropertyRequest in project ozone by apache.
the class OMBucketSetPropertyRequest method validateAndUpdateCache.
@Override
@SuppressWarnings("methodlength")
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long transactionLogIndex, OzoneManagerDoubleBufferHelper ozoneManagerDoubleBufferHelper) {
SetBucketPropertyRequest setBucketPropertyRequest = getOmRequest().getSetBucketPropertyRequest();
Preconditions.checkNotNull(setBucketPropertyRequest);
OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
OMMetrics omMetrics = ozoneManager.getMetrics();
omMetrics.incNumBucketUpdates();
BucketArgs bucketArgs = setBucketPropertyRequest.getBucketArgs();
OmBucketArgs omBucketArgs = OmBucketArgs.getFromProtobuf(bucketArgs);
String volumeName = bucketArgs.getVolumeName();
String bucketName = bucketArgs.getBucketName();
OMResponse.Builder omResponse = OmResponseUtil.getOMResponseBuilder(getOmRequest());
OmBucketInfo omBucketInfo = null;
AuditLogger auditLogger = ozoneManager.getAuditLogger();
OzoneManagerProtocolProtos.UserInfo userInfo = getOmRequest().getUserInfo();
IOException exception = null;
boolean acquiredBucketLock = false, success = true;
OMClientResponse omClientResponse = null;
try {
// check Acl
if (ozoneManager.getAclsEnabled()) {
checkAcls(ozoneManager, OzoneObj.ResourceType.BUCKET, OzoneObj.StoreType.OZONE, IAccessAuthorizer.ACLType.WRITE, volumeName, bucketName, null);
}
// acquire lock.
acquiredBucketLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
String bucketKey = omMetadataManager.getBucketKey(volumeName, bucketName);
OmBucketInfo dbBucketInfo = omMetadataManager.getBucketTable().get(bucketKey);
// Check if bucket exist
if (dbBucketInfo == null) {
LOG.debug("bucket: {} not found ", bucketName);
throw new OMException("Bucket doesn't exist", OMException.ResultCodes.BUCKET_NOT_FOUND);
}
OmBucketInfo.Builder bucketInfoBuilder = OmBucketInfo.newBuilder();
bucketInfoBuilder.setVolumeName(dbBucketInfo.getVolumeName()).setBucketName(dbBucketInfo.getBucketName()).setObjectID(dbBucketInfo.getObjectID()).setUpdateID(transactionLogIndex);
bucketInfoBuilder.addAllMetadata(KeyValueUtil.getFromProtobuf(bucketArgs.getMetadataList()));
// Check StorageType to update
StorageType storageType = omBucketArgs.getStorageType();
if (storageType != null) {
bucketInfoBuilder.setStorageType(storageType);
LOG.debug("Updating bucket storage type for bucket: {} in volume: {}", bucketName, volumeName);
} else {
bucketInfoBuilder.setStorageType(dbBucketInfo.getStorageType());
}
// Check Versioning to update
Boolean versioning = omBucketArgs.getIsVersionEnabled();
if (versioning != null) {
bucketInfoBuilder.setIsVersionEnabled(versioning);
LOG.debug("Updating bucket versioning for bucket: {} in volume: {}", bucketName, volumeName);
} else {
bucketInfoBuilder.setIsVersionEnabled(dbBucketInfo.getIsVersionEnabled());
}
// Check quotaInBytes and quotaInNamespace to update
String volumeKey = omMetadataManager.getVolumeKey(volumeName);
OmVolumeArgs omVolumeArgs = omMetadataManager.getVolumeTable().get(volumeKey);
if (checkQuotaBytesValid(omMetadataManager, omVolumeArgs, omBucketArgs)) {
bucketInfoBuilder.setQuotaInBytes(omBucketArgs.getQuotaInBytes());
} else {
bucketInfoBuilder.setQuotaInBytes(dbBucketInfo.getQuotaInBytes());
}
if (checkQuotaNamespaceValid(omVolumeArgs, omBucketArgs)) {
bucketInfoBuilder.setQuotaInNamespace(omBucketArgs.getQuotaInNamespace());
} else {
bucketInfoBuilder.setQuotaInNamespace(dbBucketInfo.getQuotaInNamespace());
}
bucketInfoBuilder.setCreationTime(dbBucketInfo.getCreationTime());
bucketInfoBuilder.setModificationTime(setBucketPropertyRequest.getModificationTime());
// Set acls from dbBucketInfo if it has any.
if (dbBucketInfo.getAcls() != null) {
bucketInfoBuilder.setAcls(dbBucketInfo.getAcls());
}
// Set the objectID to dbBucketInfo objectID, if present
if (dbBucketInfo.getObjectID() != 0) {
bucketInfoBuilder.setObjectID(dbBucketInfo.getObjectID());
}
// Set the updateID to current transaction log index
bucketInfoBuilder.setUpdateID(transactionLogIndex);
// Quota used remains unchanged
bucketInfoBuilder.setUsedBytes(dbBucketInfo.getUsedBytes());
bucketInfoBuilder.setUsedNamespace(dbBucketInfo.getUsedNamespace());
omBucketInfo = bucketInfoBuilder.build();
// Update table cache.
omMetadataManager.getBucketTable().addCacheEntry(new CacheKey<>(bucketKey), new CacheValue<>(Optional.of(omBucketInfo), transactionLogIndex));
omResponse.setSetBucketPropertyResponse(SetBucketPropertyResponse.newBuilder().build());
omClientResponse = new OMBucketSetPropertyResponse(omResponse.build(), omBucketInfo);
} catch (IOException ex) {
success = false;
exception = ex;
omClientResponse = new OMBucketSetPropertyResponse(createErrorOMResponse(omResponse, exception));
} finally {
addResponseToDoubleBuffer(transactionLogIndex, omClientResponse, ozoneManagerDoubleBufferHelper);
if (acquiredBucketLock) {
omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volumeName, bucketName);
}
}
// Performing audit logging outside of the lock.
auditLog(auditLogger, buildAuditMessage(OMAction.UPDATE_BUCKET, omBucketArgs.toAuditMap(), exception, userInfo));
// return response.
if (success) {
LOG.debug("Setting bucket property for bucket:{} in volume:{}", bucketName, volumeName);
return omClientResponse;
} else {
LOG.error("Setting bucket property failed for bucket:{} in volume:{}", bucketName, volumeName, exception);
omMetrics.incNumBucketUpdateFails();
return omClientResponse;
}
}
Aggregations