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;
}
Aggregations