Search in sources :

Example 1 with SetBucketPropertyRequest

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;
    }
}
Also used : OmBucketInfo(org.apache.hadoop.ozone.om.helpers.OmBucketInfo) AuditLogger(org.apache.hadoop.ozone.audit.AuditLogger) OMClientResponse(org.apache.hadoop.ozone.om.response.OMClientResponse) OmBucketArgs(org.apache.hadoop.ozone.om.helpers.OmBucketArgs) BucketArgs(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.BucketArgs) IOException(java.io.IOException) OMResponse(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse) OMMetrics(org.apache.hadoop.ozone.om.OMMetrics) OzoneManagerProtocolProtos(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos) OmBucketArgs(org.apache.hadoop.ozone.om.helpers.OmBucketArgs) OMMetadataManager(org.apache.hadoop.ozone.om.OMMetadataManager) OMBucketSetOwnerResponse(org.apache.hadoop.ozone.om.response.bucket.OMBucketSetOwnerResponse) OMException(org.apache.hadoop.ozone.om.exceptions.OMException) SetBucketPropertyRequest(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.SetBucketPropertyRequest)

Example 2 with SetBucketPropertyRequest

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;
    }
}
Also used : OmBucketInfo(org.apache.hadoop.ozone.om.helpers.OmBucketInfo) AuditLogger(org.apache.hadoop.ozone.audit.AuditLogger) StorageType(org.apache.hadoop.hdds.protocol.StorageType) OMClientResponse(org.apache.hadoop.ozone.om.response.OMClientResponse) OmVolumeArgs(org.apache.hadoop.ozone.om.helpers.OmVolumeArgs) OmBucketArgs(org.apache.hadoop.ozone.om.helpers.OmBucketArgs) BucketArgs(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.BucketArgs) OMBucketSetPropertyResponse(org.apache.hadoop.ozone.om.response.bucket.OMBucketSetPropertyResponse) IOException(java.io.IOException) OMMetrics(org.apache.hadoop.ozone.om.OMMetrics) OMResponse(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse) OzoneManagerProtocolProtos(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos) OmBucketArgs(org.apache.hadoop.ozone.om.helpers.OmBucketArgs) OMMetadataManager(org.apache.hadoop.ozone.om.OMMetadataManager) OMException(org.apache.hadoop.ozone.om.exceptions.OMException) SetBucketPropertyRequest(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.SetBucketPropertyRequest)

Aggregations

IOException (java.io.IOException)2 AuditLogger (org.apache.hadoop.ozone.audit.AuditLogger)2 OMMetadataManager (org.apache.hadoop.ozone.om.OMMetadataManager)2 OMMetrics (org.apache.hadoop.ozone.om.OMMetrics)2 OMException (org.apache.hadoop.ozone.om.exceptions.OMException)2 OmBucketArgs (org.apache.hadoop.ozone.om.helpers.OmBucketArgs)2 OmBucketInfo (org.apache.hadoop.ozone.om.helpers.OmBucketInfo)2 OMClientResponse (org.apache.hadoop.ozone.om.response.OMClientResponse)2 OzoneManagerProtocolProtos (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos)2 BucketArgs (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.BucketArgs)2 OMResponse (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse)2 SetBucketPropertyRequest (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.SetBucketPropertyRequest)2 StorageType (org.apache.hadoop.hdds.protocol.StorageType)1 OmVolumeArgs (org.apache.hadoop.ozone.om.helpers.OmVolumeArgs)1 OMBucketSetOwnerResponse (org.apache.hadoop.ozone.om.response.bucket.OMBucketSetOwnerResponse)1 OMBucketSetPropertyResponse (org.apache.hadoop.ozone.om.response.bucket.OMBucketSetPropertyResponse)1