Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private void testDeleteCreatesFakeParentDir() throws Exception {
}


private void testRecursiveDelete() throws Exception {
protected void testRecursiveDelete() throws Exception {
Path grandparent = new Path("/gdir1");

for (int i = 1; i <= 10; i++) {
Expand Down Expand Up @@ -451,7 +451,7 @@ private void checkPath(Path path) {
}
}

private void testFileDelete() throws Exception {
protected void testFileDelete() throws Exception {
Path grandparent = new Path("/testBatchDelete");
Path parent = new Path(grandparent, "parent");
Path childFolder = new Path(parent, "childFolder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ public void testFileSystem() throws Exception {

testSeekOnFileLength();
tableCleanup();

testFileDelete();
tableCleanup();

testDeleteRoot();
tableCleanup();

testRecursiveDelete();
tableCleanup();
}

/**
Comment thread
rakeshadr marked this conversation as resolved.
Expand All @@ -484,69 +493,23 @@ public void testFileSystem() throws Exception {
* @throws IOException DB failure
*/
protected void tableCleanup() throws IOException {
OMMetadataManager metadataMgr = cluster.getOzoneManager()
.getMetadataManager();
TableIterator<String, ? extends
Table.KeyValue<String, OmDirectoryInfo>> dirTableIterator =
metadataMgr.getDirectoryTable().iterator();
dirTableIterator.seekToFirst();
ArrayList <String> dirList = new ArrayList<>();
while (dirTableIterator.hasNext()) {
String key = dirTableIterator.key();
if (StringUtils.isNotBlank(key)) {
dirList.add(key);
}
dirTableIterator.next();
}
Path root = new Path("/");
FileStatus[] fileStatuses = fs.listStatus(root);

Iterator<Map.Entry<CacheKey<String>, CacheValue<OmDirectoryInfo>>>
cacheIterator = metadataMgr.getDirectoryTable().cacheIterator();
while(cacheIterator.hasNext()){
cacheIterator.next();
cacheIterator.remove();
if (fileStatuses == null) {
return;
}

for (String dirKey : dirList) {
metadataMgr.getDirectoryTable().delete(dirKey);
Assert.assertNull("Unexpected entry!",
metadataMgr.getDirectoryTable().get(dirKey));
for (FileStatus fStatus : fileStatuses) {
fs.delete(fStatus.getPath(), true);
}

Assert.assertTrue("DirTable is not empty",
metadataMgr.getDirectoryTable().isEmpty());

Assert.assertFalse(metadataMgr.getDirectoryTable().cacheIterator()
.hasNext());

TableIterator<String, ? extends
Table.KeyValue<String, OmKeyInfo>> keyTableIterator =
metadataMgr.getKeyTable().iterator();
keyTableIterator.seekToFirst();
ArrayList <String> fileList = new ArrayList<>();
while (keyTableIterator.hasNext()) {
String key = keyTableIterator.key();
if (StringUtils.isNotBlank(key)) {
fileList.add(key);
}
keyTableIterator.next();
fileStatuses = fs.listStatus(root);
if (fileStatuses != null) {
Assert.assertEquals("Delete root failed!", 0, fileStatuses.length);
rootItemCount = 0;
return;
}

Iterator<Map.Entry<CacheKey<String>, CacheValue<OmKeyInfo>>>
keyCacheIterator = metadataMgr.getKeyTable().cacheIterator();
while(keyCacheIterator.hasNext()){
keyCacheIterator.next();
keyCacheIterator.remove();
}

for (String fileKey : fileList) {
metadataMgr.getKeyTable().delete(fileKey);
Assert.assertNull("Unexpected entry!",
metadataMgr.getKeyTable().get(fileKey));
}

Assert.assertTrue("KeyTable is not empty",
metadataMgr.getKeyTable().isEmpty());

rootItemCount = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.hadoop.ozone.om.request.key.OMKeyCommitRequestV1;
import org.apache.hadoop.ozone.om.request.key.OMKeyCreateRequest;
import org.apache.hadoop.ozone.om.request.key.OMKeyDeleteRequest;
import org.apache.hadoop.ozone.om.request.key.OMKeyDeleteRequestV1;
import org.apache.hadoop.ozone.om.request.key.OMKeyPurgeRequest;
import org.apache.hadoop.ozone.om.request.key.OMKeyRenameRequest;
import org.apache.hadoop.ozone.om.request.key.OMKeyRenameRequestV1;
Expand Down Expand Up @@ -146,6 +147,9 @@ public static OMClientRequest createClientRequest(OMRequest omRequest) {
}
return new OMKeyCommitRequest(omRequest);
case DeleteKey:
if (omLayoutVersionV1) {
return new OMKeyDeleteRequestV1(omRequest);
}
return new OMKeyDeleteRequest(omRequest);
case DeleteKeys:
return new OMKeysDeleteRequest(omRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.ozone.om.request.key;

import com.google.common.base.Optional;
import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
import org.apache.hadoop.ozone.audit.AuditLogger;
import org.apache.hadoop.ozone.audit.OMAction;
import org.apache.hadoop.ozone.om.OMMetadataManager;
import org.apache.hadoop.ozone.om.OMMetrics;
import org.apache.hadoop.ozone.om.OzoneManager;
import org.apache.hadoop.ozone.om.exceptions.OMException;
import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
import org.apache.hadoop.ozone.om.helpers.OmVolumeArgs;
import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
import org.apache.hadoop.ozone.om.ratis.utils.OzoneManagerDoubleBufferHelper;
import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
import org.apache.hadoop.ozone.om.request.util.OmResponseUtil;
import org.apache.hadoop.ozone.om.response.OMClientResponse;
import org.apache.hadoop.ozone.om.response.key.OMKeyDeleteResponseV1;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.DeleteKeyRequest;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.DeleteKeyResponse;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse;
import org.apache.hadoop.ozone.security.acl.IAccessAuthorizer;
import org.apache.hadoop.ozone.security.acl.OzoneObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.KEY_NOT_FOUND;
import static org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;

/**
* Handles DeleteKey request layout version V1.
*/
public class OMKeyDeleteRequestV1 extends OMKeyDeleteRequest {

private static final Logger LOG =
LoggerFactory.getLogger(OMKeyDeleteRequestV1.class);

public OMKeyDeleteRequestV1(OMRequest omRequest) {
super(omRequest);
}

@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();

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;
OmVolumeArgs omVolumeArgs = null;
OmBucketInfo omBucketInfo = null;
try {
keyArgs = resolveBucketLink(ozoneManager, keyArgs, auditMap);
volumeName = keyArgs.getVolumeName();
bucketName = keyArgs.getBucketName();

// check Acl
checkKeyAcls(ozoneManager, volumeName, bucketName, keyName,
IAccessAuthorizer.ACLType.DELETE, OzoneObj.ResourceType.KEY);

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_NOT_FOUND);
Comment thread
rakeshadr marked this conversation as resolved.
Outdated
}

OmKeyInfo omKeyInfo = keyStatus.getKeyInfo();

// Set the UpdateID to current transactionLogIndex
omKeyInfo.setUpdateID(trxnLogIndex, ozoneManager.isRatisEnabled());

String ozonePathKey = omMetadataManager.getOzonePathKey(
omKeyInfo.getParentObjectID(), omKeyInfo.getFileName());

if (keyStatus.isDirectory()) {
// Update dir cache.
omMetadataManager.getDirectoryTable().addCacheEntry(
new CacheKey<>(ozonePathKey),
new CacheValue<>(Optional.absent(), trxnLogIndex));
} else {
// Update table cache.
omMetadataManager.getKeyTable().addCacheEntry(
new CacheKey<>(ozonePathKey),
new CacheValue<>(Optional.absent(), trxnLogIndex));
}

omVolumeArgs = getVolumeInfo(omMetadataManager, volumeName);
omBucketInfo = getBucketInfo(omMetadataManager, volumeName, bucketName);

long quotaReleased = sumBlockLengths(omKeyInfo);
Comment thread
rakeshadr marked this conversation as resolved.
// update usedBytes atomically.
omVolumeArgs.getUsedBytes().add(-quotaReleased);
omBucketInfo.getUsedBytes().add(-quotaReleased);

// 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 OMKeyDeleteResponseV1(omResponse
.setDeleteKeyResponse(DeleteKeyResponse.newBuilder()).build(),
omKeyInfo, ozoneManager.isRatisEnabled(), omVolumeArgs,
omBucketInfo, keyStatus.isDirectory());

result = Result.SUCCESS;
} catch (IOException ex) {
result = Result.FAILURE;
exception = ex;
omClientResponse = new OMKeyDeleteResponseV1(
createErrorOMResponse(omResponse, exception));
} 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,16 @@ public void addToDBBatch(OMMetadataManager omMetadataManager,
omMetadataManager.getBucketKey(omVolumeArgs.getVolume(),
omBucketInfo.getBucketName()), omBucketInfo);
}

protected OmKeyInfo getOmKeyInfo() {
return omKeyInfo;
}

protected OmBucketInfo getOmBucketInfo() {
return omBucketInfo;
}

protected OmVolumeArgs getOmVolumeArgs() {
return omVolumeArgs;
}
}
Loading