Search in sources :

Example 1 with OMKeyDeleteResponse

use of org.apache.hadoop.ozone.om.response.key.OMKeyDeleteResponse in project ozone by apache.

the class OMKeyDeleteRequest method validateAndUpdateCache.

public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper, BucketLayout bucketLayout) {
    DeleteKeyRequest deleteKeyRequest = getOmRequest().getDeleteKeyRequest();
    OzoneManagerProtocolProtos.KeyArgs keyArgs = deleteKeyRequest.getKeyArgs();
    Map<String, String> auditMap = buildKeyArgsAuditMap(keyArgs);
    String volumeName = keyArgs.getVolumeName();
    String bucketName = keyArgs.getBucketName();
    String keyName = keyArgs.getKeyName();
    OMMetrics omMetrics = ozoneManager.getMetrics();
    omMetrics.incNumKeyDeletes();
    AuditLogger auditLogger = ozoneManager.getAuditLogger();
    OzoneManagerProtocolProtos.UserInfo userInfo = getOmRequest().getUserInfo();
    OMResponse.Builder omResponse = OmResponseUtil.getOMResponseBuilder(getOmRequest());
    OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
    IOException exception = null;
    boolean acquiredLock = false;
    OMClientResponse omClientResponse = null;
    Result result = null;
    OmBucketInfo omBucketInfo = OmBucketInfo.newBuilder().setVolumeName(volumeName).setBucketName(bucketName).setCreationTime(Time.now()).setBucketLayout(bucketLayout).build();
    try {
        keyArgs = resolveBucketLink(ozoneManager, keyArgs, auditMap);
        volumeName = keyArgs.getVolumeName();
        bucketName = keyArgs.getBucketName();
        // check Acl
        checkKeyAcls(ozoneManager, volumeName, bucketName, keyName, IAccessAuthorizer.ACLType.DELETE, OzoneObj.ResourceType.KEY);
        String objectKey = omMetadataManager.getOzoneKey(volumeName, bucketName, keyName);
        acquiredLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
        // Validate bucket and volume exists or not.
        validateBucketAndVolume(omMetadataManager, volumeName, bucketName);
        OmKeyInfo omKeyInfo = omMetadataManager.getKeyTable(bucketLayout).get(objectKey);
        if (omKeyInfo == null) {
            throw new OMException("Key not found", KEY_NOT_FOUND);
        }
        // Set the UpdateID to current transactionLogIndex
        omKeyInfo.setUpdateID(trxnLogIndex, ozoneManager.isRatisEnabled());
        // Update table cache.
        omMetadataManager.getKeyTable(getBucketLayout()).addCacheEntry(new CacheKey<>(omMetadataManager.getOzoneKey(volumeName, bucketName, keyName)), new CacheValue<>(Optional.absent(), trxnLogIndex));
        omBucketInfo = getBucketInfo(omMetadataManager, volumeName, bucketName);
        long quotaReleased = sumBlockLengths(omKeyInfo);
        omBucketInfo.incrUsedBytes(-quotaReleased);
        omBucketInfo.incrUsedNamespace(-1L);
        // No need to add cache entries to delete table. As delete table will
        // be used by DeleteKeyService only, not used for any client response
        // validation, so we don't need to add to cache.
        // TODO: Revisit if we need it later.
        omClientResponse = new OMKeyDeleteResponse(omResponse.setDeleteKeyResponse(DeleteKeyResponse.newBuilder()).build(), omKeyInfo, ozoneManager.isRatisEnabled(), omBucketInfo.copyObject());
        result = Result.SUCCESS;
    } catch (IOException ex) {
        result = Result.FAILURE;
        exception = ex;
        omClientResponse = new OMKeyDeleteResponse(createErrorOMResponse(omResponse, exception), getBucketLayout());
    } finally {
        addResponseToDoubleBuffer(trxnLogIndex, omClientResponse, omDoubleBufferHelper);
        if (acquiredLock) {
            omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volumeName, bucketName);
        }
    }
    // Performing audit logging outside of the lock.
    auditLog(auditLogger, buildAuditMessage(OMAction.DELETE_KEY, auditMap, exception, userInfo));
    switch(result) {
        case SUCCESS:
            omMetrics.decNumKeys();
            LOG.debug("Key deleted. Volume:{}, Bucket:{}, Key:{}", volumeName, bucketName, keyName);
            break;
        case FAILURE:
            omMetrics.incNumKeyDeleteFails();
            LOG.error("Key delete failed. Volume:{}, Bucket:{}, Key:{}.", volumeName, bucketName, keyName, exception);
            break;
        default:
            LOG.error("Unrecognized Result for OMKeyDeleteRequest: {}", deleteKeyRequest);
    }
    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) 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) OMKeyDeleteResponse(org.apache.hadoop.ozone.om.response.key.OMKeyDeleteResponse) OMMetadataManager(org.apache.hadoop.ozone.om.OMMetadataManager) OmKeyInfo(org.apache.hadoop.ozone.om.helpers.OmKeyInfo) OMException(org.apache.hadoop.ozone.om.exceptions.OMException) DeleteKeyRequest(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.DeleteKeyRequest)

Aggregations

IOException (java.io.IOException)1 AuditLogger (org.apache.hadoop.ozone.audit.AuditLogger)1 OMMetadataManager (org.apache.hadoop.ozone.om.OMMetadataManager)1 OMMetrics (org.apache.hadoop.ozone.om.OMMetrics)1 OMException (org.apache.hadoop.ozone.om.exceptions.OMException)1 OmBucketInfo (org.apache.hadoop.ozone.om.helpers.OmBucketInfo)1 OmKeyInfo (org.apache.hadoop.ozone.om.helpers.OmKeyInfo)1 OMClientResponse (org.apache.hadoop.ozone.om.response.OMClientResponse)1 OMKeyDeleteResponse (org.apache.hadoop.ozone.om.response.key.OMKeyDeleteResponse)1 OzoneManagerProtocolProtos (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos)1 DeleteKeyRequest (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.DeleteKeyRequest)1 OMResponse (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse)1