Search in sources :

Example 1 with OMAllocateBlockResponse

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

the class OMAllocateBlockRequest method validateAndUpdateCache.

@Override
public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
    OzoneManagerProtocolProtos.AllocateBlockRequest allocateBlockRequest = getOmRequest().getAllocateBlockRequest();
    OzoneManagerProtocolProtos.KeyArgs keyArgs = allocateBlockRequest.getKeyArgs();
    OzoneManagerProtocolProtos.KeyLocation blockLocation = allocateBlockRequest.getKeyLocation();
    Preconditions.checkNotNull(blockLocation);
    String volumeName = keyArgs.getVolumeName();
    String bucketName = keyArgs.getBucketName();
    String keyName = keyArgs.getKeyName();
    long clientID = allocateBlockRequest.getClientID();
    OMMetrics omMetrics = ozoneManager.getMetrics();
    omMetrics.incNumBlockAllocateCalls();
    AuditLogger auditLogger = ozoneManager.getAuditLogger();
    Map<String, String> auditMap = buildKeyArgsAuditMap(keyArgs);
    auditMap.put(OzoneConsts.CLIENT_ID, String.valueOf(clientID));
    OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager();
    String openKeyName = null;
    OMResponse.Builder omResponse = OmResponseUtil.getOMResponseBuilder(getOmRequest());
    OMClientResponse omClientResponse = null;
    OmKeyInfo openKeyInfo = null;
    IOException exception = null;
    OmBucketInfo omBucketInfo = null;
    boolean acquiredLock = false;
    try {
        keyArgs = resolveBucketLink(ozoneManager, keyArgs, auditMap);
        volumeName = keyArgs.getVolumeName();
        bucketName = keyArgs.getBucketName();
        // check Acl
        checkKeyAclsInOpenKeyTable(ozoneManager, volumeName, bucketName, keyName, IAccessAuthorizer.ACLType.WRITE, allocateBlockRequest.getClientID());
        validateBucketAndVolume(omMetadataManager, volumeName, bucketName);
        // Here we don't acquire bucket/volume lock because for a single client
        // allocateBlock is called in serial fashion.
        openKeyName = omMetadataManager.getOpenKey(volumeName, bucketName, keyName, clientID);
        openKeyInfo = omMetadataManager.getOpenKeyTable(getBucketLayout()).get(openKeyName);
        if (openKeyInfo == null) {
            throw new OMException("Open Key not found " + openKeyName, KEY_NOT_FOUND);
        }
        List<OmKeyLocationInfo> newLocationList = Collections.singletonList(OmKeyLocationInfo.getFromProtobuf(blockLocation));
        acquiredLock = omMetadataManager.getLock().acquireWriteLock(BUCKET_LOCK, volumeName, bucketName);
        omBucketInfo = getBucketInfo(omMetadataManager, volumeName, bucketName);
        // check bucket and volume quota
        long preAllocatedKeySize = newLocationList.size() * ozoneManager.getScmBlockSize();
        long hadAllocatedKeySize = openKeyInfo.getLatestVersionLocations().getLocationList().size() * ozoneManager.getScmBlockSize();
        ReplicationConfig repConfig = openKeyInfo.getReplicationConfig();
        long totalAllocatedSpace = QuotaUtil.getReplicatedSize(preAllocatedKeySize, repConfig) + QuotaUtil.getReplicatedSize(hadAllocatedKeySize, repConfig);
        checkBucketQuotaInBytes(omBucketInfo, totalAllocatedSpace);
        // Append new block
        openKeyInfo.appendNewBlocks(newLocationList, false);
        // Set modification time.
        openKeyInfo.setModificationTime(keyArgs.getModificationTime());
        // Set the UpdateID to current transactionLogIndex
        openKeyInfo.setUpdateID(trxnLogIndex, ozoneManager.isRatisEnabled());
        // Add to cache.
        omMetadataManager.getOpenKeyTable(getBucketLayout()).addCacheEntry(new CacheKey<>(openKeyName), new CacheValue<>(Optional.of(openKeyInfo), trxnLogIndex));
        omResponse.setAllocateBlockResponse(AllocateBlockResponse.newBuilder().setKeyLocation(blockLocation).build());
        omClientResponse = new OMAllocateBlockResponse(omResponse.build(), openKeyInfo, clientID, getBucketLayout());
        LOG.debug("Allocated block for Volume:{}, Bucket:{}, OpenKey:{}", volumeName, bucketName, openKeyName);
    } catch (IOException ex) {
        omMetrics.incNumBlockAllocateCallFails();
        exception = ex;
        omClientResponse = new OMAllocateBlockResponse(createErrorOMResponse(omResponse, exception), getBucketLayout());
        LOG.error("Allocate Block failed. Volume:{}, Bucket:{}, OpenKey:{}. " + "Exception:{}", volumeName, bucketName, openKeyName, exception);
    } finally {
        addResponseToDoubleBuffer(trxnLogIndex, omClientResponse, omDoubleBufferHelper);
        if (acquiredLock) {
            omMetadataManager.getLock().releaseWriteLock(BUCKET_LOCK, volumeName, bucketName);
        }
    }
    auditLog(auditLogger, buildAuditMessage(OMAction.ALLOCATE_BLOCK, auditMap, exception, getOmRequest().getUserInfo()));
    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) ReplicationConfig(org.apache.hadoop.hdds.client.ReplicationConfig) IOException(java.io.IOException) OMAllocateBlockResponse(org.apache.hadoop.ozone.om.response.key.OMAllocateBlockResponse) OmKeyLocationInfo(org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo) OMMetrics(org.apache.hadoop.ozone.om.OMMetrics) OMResponse(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse) OzoneManagerProtocolProtos(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos) KeyArgs(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.KeyArgs) OMMetadataManager(org.apache.hadoop.ozone.om.OMMetadataManager) OmKeyInfo(org.apache.hadoop.ozone.om.helpers.OmKeyInfo) AllocateBlockRequest(org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.AllocateBlockRequest) OMException(org.apache.hadoop.ozone.om.exceptions.OMException)

Aggregations

IOException (java.io.IOException)1 ReplicationConfig (org.apache.hadoop.hdds.client.ReplicationConfig)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 OmKeyLocationInfo (org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo)1 OMClientResponse (org.apache.hadoop.ozone.om.response.OMClientResponse)1 OMAllocateBlockResponse (org.apache.hadoop.ozone.om.response.key.OMAllocateBlockResponse)1 OzoneManagerProtocolProtos (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos)1 AllocateBlockRequest (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.AllocateBlockRequest)1 KeyArgs (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.KeyArgs)1 OMResponse (org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse)1