Search in sources :

Example 1 with RenameKeysMap

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

the class TestOMKeysRenameRequest method testKeysRenameRequestFail.

@Test
public void testKeysRenameRequestFail() throws Exception {
    OMRequest modifiedOmRequest = createRenameKeyRequest(true);
    OMKeysRenameRequest omKeysRenameRequest = new OMKeysRenameRequest(modifiedOmRequest);
    OMClientResponse omKeysRenameResponse = omKeysRenameRequest.validateAndUpdateCache(ozoneManager, 100L, ozoneManagerDoubleBufferHelper);
    Assert.assertFalse(omKeysRenameResponse.getOMResponse().getSuccess());
    Assert.assertEquals(OzoneManagerProtocolProtos.Status.PARTIAL_RENAME, omKeysRenameResponse.getOMResponse().getStatus());
    // The keys(key0 to key9)can be renamed success.
    for (int i = 0; i < count; i++) {
        // Original key should be deleted, toKey should exist.
        OmKeyInfo omKeyInfo = omMetadataManager.getKeyTable(getBucketLayout()).get(omMetadataManager.getOzoneKey(volumeName, bucketName, parentDir.concat("/key" + i)));
        Assert.assertNull(omKeyInfo);
        omKeyInfo = omMetadataManager.getKeyTable(getBucketLayout()).get(omMetadataManager.getOzoneKey(volumeName, bucketName, parentDir.concat("/newKey" + i)));
        Assert.assertNotNull(omKeyInfo);
    }
    // The key not rename should be in unRenamedKeys.
    RenameKeysMap unRenamedKeys = omKeysRenameResponse.getOMResponse().getRenameKeysResponse().getUnRenamedKeys(0);
    Assert.assertEquals("testKey", unRenamedKeys.getFromKeyName());
}
Also used : OMRequest(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest) OMClientResponse(org.apache.hadoop.ozone.om.response.OMClientResponse) RenameKeysMap(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap) OmKeyInfo(org.apache.hadoop.ozone.om.helpers.OmKeyInfo) Test(org.junit.Test)

Example 2 with RenameKeysMap

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

the class OMKeysRenameRequest method buildAuditMap.

/**
 * Build audit map for RenameKeys request.
 *
 * @param auditMap
 * @param renamedKeys
 * @param unRenameKeys
 * @return
 */
private Map<String, String> buildAuditMap(Map<String, String> auditMap, Map<String, String> renamedKeys, List<RenameKeysMap> unRenameKeys) {
    Map<String, String> unRenameKeysMap = new HashMap<>();
    for (RenameKeysMap renameKeysMap : unRenameKeys) {
        unRenameKeysMap.put(renameKeysMap.getFromKeyName(), renameKeysMap.getToKeyName());
    }
    auditMap.put(RENAMED_KEYS_MAP, renamedKeys.toString());
    auditMap.put(UNRENAMED_KEYS_MAP, unRenameKeysMap.toString());
    return auditMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RenameKeysMap(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap)

Example 3 with RenameKeysMap

use of org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap 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)

Example 4 with RenameKeysMap

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

the class OzoneManagerProtocolClientSideTranslatorPB method renameKeys.

@Override
public void renameKeys(OmRenameKeys omRenameKeys) throws IOException {
    List<RenameKeysMap> renameKeyList = new ArrayList<>();
    for (Map.Entry<String, String> entry : omRenameKeys.getFromAndToKey().entrySet()) {
        RenameKeysMap.Builder renameKey = RenameKeysMap.newBuilder().setFromKeyName(entry.getKey()).setToKeyName(entry.getValue());
        renameKeyList.add(renameKey.build());
    }
    RenameKeysArgs.Builder renameKeyArgs = RenameKeysArgs.newBuilder().setVolumeName(omRenameKeys.getVolume()).setBucketName(omRenameKeys.getBucket()).addAllRenameKeysMap(renameKeyList);
    RenameKeysRequest.Builder reqKeys = RenameKeysRequest.newBuilder().setRenameKeysArgs(renameKeyArgs.build());
    OMRequest omRequest = createOMRequest(Type.RenameKeys).setRenameKeysRequest(reqKeys.build()).build();
    handleError(submitRequest(omRequest));
}
Also used : OMRequest(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest) RenameKeysMap(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap) RenameKeysArgs(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysArgs) ArrayList(java.util.ArrayList) RenameKeysRequest(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysRequest) ByteString(com.google.protobuf.ByteString) Map(java.util.Map) RenameKeysMap(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap)

Aggregations

RenameKeysMap (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysMap)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 OmKeyInfo (org.apache.hadoop.ozone.om.helpers.OmKeyInfo)2 OMClientResponse (org.apache.hadoop.ozone.om.response.OMClientResponse)2 OMRequest (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest)2 RenameKeysArgs (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysArgs)2 RenameKeysRequest (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenameKeysRequest)2 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 Map (java.util.Map)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 OmRenameKeys (org.apache.hadoop.ozone.om.helpers.OmRenameKeys)1 OMKeysRenameResponse (org.apache.hadoop.ozone.om.response.key.OMKeysRenameResponse)1 OMResponse (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse)1 Test (org.junit.Test)1