Search in sources :

Example 1 with OMKeyDeleteResponseWithFSO

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

the class OMKeyDeleteRequestWithFSO method validateAndUpdateCache.

@Override
@SuppressWarnings("methodlength")
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
    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();
    boolean recursive = keyArgs.getRecursive();
    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 = null;
    try {
        keyArgs = resolveBucketLink(ozoneManager, keyArgs, auditMap);
        volumeName = keyArgs.getVolumeName();
        bucketName = keyArgs.getBucketName();
        checkACLsWithFSO(ozoneManager, volumeName, bucketName, keyName, IAccessAuthorizer.ACLType.DELETE);
        acquiredLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
        // Validate bucket and volume exists or not.
        validateBucketAndVolume(omMetadataManager, volumeName, bucketName);
        OzoneFileStatus keyStatus = OMFileRequest.getOMKeyInfoIfExists(omMetadataManager, volumeName, bucketName, keyName, 0);
        if (keyStatus == null) {
            throw new OMException("Key not found. Key:" + keyName, KEY_NOT_FOUND);
        }
        OmKeyInfo omKeyInfo = keyStatus.getKeyInfo();
        // New key format for the fileTable & dirTable.
        // For example, the user given key path is '/a/b/c/d/e/file1', then in DB
        // keyName field stores only the leaf node name, which is 'file1'.
        String fileName = OzoneFSUtils.getFileName(keyName);
        omKeyInfo.setKeyName(fileName);
        // Set the UpdateID to current transactionLogIndex
        omKeyInfo.setUpdateID(trxnLogIndex, ozoneManager.isRatisEnabled());
        String ozonePathKey = omMetadataManager.getOzonePathKey(omKeyInfo.getParentObjectID(), omKeyInfo.getFileName());
        if (keyStatus.isDirectory()) {
            // Check if there are any sub path exists under the user requested path
            if (!recursive && OMFileRequest.hasChildren(omKeyInfo, omMetadataManager)) {
                throw new OMException("Directory is not empty. Key:" + keyName, DIRECTORY_NOT_EMPTY);
            }
            // Update dir cache.
            omMetadataManager.getDirectoryTable().addCacheEntry(new CacheKey<>(ozonePathKey), new CacheValue<>(Optional.absent(), trxnLogIndex));
        } else {
            // Update table cache.
            omMetadataManager.getKeyTable(getBucketLayout()).addCacheEntry(new CacheKey<>(ozonePathKey), new CacheValue<>(Optional.absent(), trxnLogIndex));
        }
        omBucketInfo = getBucketInfo(omMetadataManager, volumeName, bucketName);
        // TODO: HDDS-4565: consider all the sub-paths if the path is a dir.
        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 OMKeyDeleteResponseWithFSO(omResponse.setDeleteKeyResponse(DeleteKeyResponse.newBuilder()).build(), keyName, omKeyInfo, ozoneManager.isRatisEnabled(), omBucketInfo.copyObject(), keyStatus.isDirectory());
        result = Result.SUCCESS;
    } catch (IOException ex) {
        result = Result.FAILURE;
        exception = ex;
        omClientResponse = new OMKeyDeleteResponseWithFSO(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) OMKeyDeleteResponseWithFSO(org.apache.hadoop.ozone.om.response.key.OMKeyDeleteResponseWithFSO) 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) OMMetadataManager(org.apache.hadoop.ozone.om.OMMetadataManager) OmKeyInfo(org.apache.hadoop.ozone.om.helpers.OmKeyInfo) OzoneFileStatus(org.apache.hadoop.ozone.om.helpers.OzoneFileStatus) 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 OzoneFileStatus (org.apache.hadoop.ozone.om.helpers.OzoneFileStatus)1 OMClientResponse (org.apache.hadoop.ozone.om.response.OMClientResponse)1 OMKeyDeleteResponseWithFSO (org.apache.hadoop.ozone.om.response.key.OMKeyDeleteResponseWithFSO)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