Search in sources :

Example 1 with RenameKeysArgs

use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysArgs in project ozone by apache.

the class OMKeysRenameRequest method validateAndUpdateCache.

@Override
@SuppressWarnings("methodlength")
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
    RenameKeysRequest renameKeysRequest = getOmRequest().getRenameKeysRequest();
    RenameKeysArgs renameKeysArgs = renameKeysRequest.getRenameKeysArgs();
    String volumeName = renameKeysArgs.getVolumeName();
    String bucketName = renameKeysArgs.getBucketName();
    OMClientResponse omClientResponse = null;
    List<RenameKeysMap> unRenamedKeys = new ArrayList<>();
    // fromKeyName -> toKeyName
    Map<String, String> renamedKeys = new HashMap<>();
    Map<String, OmKeyInfo> fromKeyAndToKeyInfo = new HashMap<>();
    OMMetrics omMetrics = ozoneManager.getMetrics();
    omMetrics.incNumKeyRenames();
    AuditLogger auditLogger = ozoneManager.getAuditLogger();
    OMResponse.Builder omResponse = OmResponseUtil.getOMResponseBuilder(getOmRequest());
    OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
    IOException exception = null;
    OmKeyInfo fromKeyValue = null;
    Result result = null;
    Map<String, String> auditMap = new LinkedHashMap<>();
    String fromKeyName = null;
    String toKeyName = null;
    boolean acquiredLock = false;
    boolean renameStatus = true;
    try {
        ResolvedBucket bucket = ozoneManager.resolveBucketLink(Pair.of(volumeName, bucketName), this);
        bucket.audit(auditMap);
        volumeName = bucket.realVolume();
        bucketName = bucket.realBucket();
        acquiredLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
        // Validate bucket and volume exists or not.
        validateBucketAndVolume(omMetadataManager, volumeName, bucketName);
        String volumeOwner = getVolumeOwner(omMetadataManager, volumeName);
        for (RenameKeysMap renameKey : renameKeysArgs.getRenameKeysMapList()) {
            fromKeyName = renameKey.getFromKeyName();
            toKeyName = renameKey.getToKeyName();
            RenameKeysMap.Builder unRenameKey = RenameKeysMap.newBuilder();
            if (toKeyName.length() == 0 || fromKeyName.length() == 0) {
                renameStatus = false;
                unRenamedKeys.add(unRenameKey.setFromKeyName(fromKeyName).setToKeyName(toKeyName).build());
                LOG.error("Key name is empty fromKeyName {} toKeyName {}", fromKeyName, toKeyName);
                continue;
            }
            try {
                // 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, volumeOwner);
                checkKeyAcls(ozoneManager, volumeName, bucketName, toKeyName, IAccessAuthorizer.ACLType.CREATE, OzoneObj.ResourceType.KEY, volumeOwner);
            } catch (Exception ex) {
                renameStatus = false;
                unRenamedKeys.add(unRenameKey.setFromKeyName(fromKeyName).setToKeyName(toKeyName).build());
                LOG.error("Acl check failed for fromKeyName {} toKeyName {}", fromKeyName, toKeyName, ex);
                continue;
            }
            // Check if toKey exists
            String fromKey = omMetadataManager.getOzoneKey(volumeName, bucketName, fromKeyName);
            String toKey = omMetadataManager.getOzoneKey(volumeName, bucketName, toKeyName);
            OmKeyInfo toKeyValue = omMetadataManager.getKeyTable(getBucketLayout()).get(toKey);
            if (toKeyValue != null) {
                renameStatus = false;
                unRenamedKeys.add(unRenameKey.setFromKeyName(fromKeyName).setToKeyName(toKeyName).build());
                LOG.error("Received a request name of new key {} already exists", toKeyName);
            }
            // fromKeyName should exist
            fromKeyValue = omMetadataManager.getKeyTable(getBucketLayout()).get(fromKey);
            if (fromKeyValue == null) {
                renameStatus = false;
                unRenamedKeys.add(unRenameKey.setFromKeyName(fromKeyName).setToKeyName(toKeyName).build());
                LOG.error("Received a request to rename a Key does not exist {}", fromKey);
                continue;
            }
            fromKeyValue.setUpdateID(trxnLogIndex, ozoneManager.isRatisEnabled());
            fromKeyValue.setKeyName(toKeyName);
            // Set modification time
            fromKeyValue.setModificationTime(Time.now());
            // 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));
            renamedKeys.put(fromKeyName, toKeyName);
            fromKeyAndToKeyInfo.put(fromKeyName, fromKeyValue);
        }
        OmRenameKeys newOmRenameKeys = new OmRenameKeys(volumeName, bucketName, null, fromKeyAndToKeyInfo);
        omClientResponse = new OMKeysRenameResponse(omResponse.setRenameKeysResponse(RenameKeysResponse.newBuilder().setStatus(renameStatus).addAllUnRenamedKeys(unRenamedKeys)).setStatus(renameStatus ? OK : PARTIAL_RENAME).setSuccess(renameStatus).build(), newOmRenameKeys);
        result = Result.SUCCESS;
    } catch (IOException ex) {
        result = Result.FAILURE;
        exception = ex;
        createErrorOMResponse(omResponse, ex);
        omResponse.setRenameKeysResponse(RenameKeysResponse.newBuilder().setStatus(renameStatus).addAllUnRenamedKeys(unRenamedKeys).build());
        omClientResponse = new OMKeysRenameResponse(omResponse.build());
    } finally {
        addResponseToDoubleBuffer(trxnLogIndex, omClientResponse, omDoubleBufferHelper);
        if (acquiredLock) {
            omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volumeName, bucketName);
        }
    }
    auditMap = buildAuditMap(auditMap, renamedKeys, unRenamedKeys);
    auditLog(auditLogger, buildAuditMessage(OMAction.RENAME_KEYS, auditMap, exception, getOmRequest().getUserInfo()));
    switch(result) {
        case SUCCESS:
            LOG.debug("Rename Keys is successfully completed for auditMap:{}.", auditMap);
            break;
        case FAILURE:
            ozoneManager.getMetrics().incNumKeyRenameFails();
            LOG.error("Rename keys failed for auditMap:{}.", auditMap);
            break;
        default:
            LOG.error("Unrecognized Result for OMKeysRenameRequest: {}", renameKeysRequest);
    }
    return omClientResponse;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RenameKeysMap(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap) ArrayList(java.util.ArrayList) OMMetrics(org.apache.hadoop.ozone.om.OMMetrics) LinkedHashMap(java.util.LinkedHashMap) RenameKeysArgs(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysArgs) OmKeyInfo(org.apache.hadoop.ozone.om.helpers.OmKeyInfo) OmRenameKeys(org.apache.hadoop.ozone.om.helpers.OmRenameKeys) RenameKeysRequest(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysRequest) ResolvedBucket(org.apache.hadoop.ozone.om.ResolvedBucket) AuditLogger(org.apache.hadoop.ozone.audit.AuditLogger) OMClientResponse(org.apache.hadoop.ozone.om.response.OMClientResponse) IOException(java.io.IOException) IOException(java.io.IOException) OMResponse(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse) OMKeysRenameResponse(org.apache.hadoop.ozone.om.response.key.OMKeysRenameResponse) OMMetadataManager(org.apache.hadoop.ozone.om.OMMetadataManager)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)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 ResolvedBucket (org.apache.hadoop.ozone.om.ResolvedBucket)1 OmKeyInfo (org.apache.hadoop.ozone.om.helpers.OmKeyInfo)1 OmRenameKeys (org.apache.hadoop.ozone.om.helpers.OmRenameKeys)1 OMClientResponse (org.apache.hadoop.ozone.om.response.OMClientResponse)1 OMKeysRenameResponse (org.apache.hadoop.ozone.om.response.key.OMKeysRenameResponse)1 OMResponse (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse)1 RenameKeysArgs (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysArgs)1 RenameKeysMap (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap)1 RenameKeysRequest (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysRequest)1