devmadhuu commented on code in PR #4675:
URL: https://github.com/apache/ozone/pull/4675#discussion_r1192190872
##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/keys/DeleteKeyHandler.java:
##########
@@ -35,16 +48,117 @@
description = "deletes an existing key")
public class DeleteKeyHandler extends KeyHandler {
+ @CommandLine.Option(names = "--skipTrash",
+ description = "Specify whether to skip Trash ")
+ private boolean skipTrash = false;
+
+ private static final Path CURRENT = new Path("Current");
+
@Override
protected void execute(OzoneClient client, OzoneAddress address)
throws IOException, OzoneClientException {
String volumeName = address.getVolumeName();
String bucketName = address.getBucketName();
- String keyName = address.getKeyName();
-
OzoneVolume vol = client.getObjectStore().getVolume(volumeName);
OzoneBucket bucket = vol.getBucket(bucketName);
- bucket.deleteKey(keyName);
+ String keyName = address.getKeyName();
+
+ if (bucket.getBucketLayout().isFileSystemOptimized()) {
+ // Handle FSO delete key which supports trash also
+ deleteFSOKey(bucket, keyName);
+ } else {
+ bucket.deleteKey(keyName);
+ }
+ }
+
+ private void deleteFSOKey(OzoneBucket bucket, String keyName)
+ throws IOException {
+ float hadoopTrashInterval = getConf().getFloat(
+ FS_TRASH_INTERVAL_KEY, FS_TRASH_INTERVAL_DEFAULT);
+ long trashInterval =
+ (long) (getConf().getFloat(
+ OMConfigKeys.OZONE_FS_TRASH_INTERVAL_KEY,
+ hadoopTrashInterval) * 10000);
+
+ // If Bucket layout is FSO and Trash is enabled
+ // In this case during delete operation move key to trash
+ if (trashInterval > 0 && !skipTrash &&
+ !keyName.contains(TRASH_PREFIX)) {
+ keyName = OzoneFSUtils.removeTrailingSlashIfNeeded(keyName);
+ // Check if key exists in Ozone
+ boolean bKeyExist = isKeyExist(bucket, keyName);
+ if (!bKeyExist) {
Review Comment:
can we directly check the if condition on "`isKeyExist`" method if
"bKeyExist" not referenced ?
##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/keys/DeleteKeyHandler.java:
##########
@@ -35,16 +48,117 @@
description = "deletes an existing key")
public class DeleteKeyHandler extends KeyHandler {
+ @CommandLine.Option(names = "--skipTrash",
+ description = "Specify whether to skip Trash ")
+ private boolean skipTrash = false;
+
+ private static final Path CURRENT = new Path("Current");
+
@Override
protected void execute(OzoneClient client, OzoneAddress address)
throws IOException, OzoneClientException {
String volumeName = address.getVolumeName();
String bucketName = address.getBucketName();
- String keyName = address.getKeyName();
-
OzoneVolume vol = client.getObjectStore().getVolume(volumeName);
OzoneBucket bucket = vol.getBucket(bucketName);
- bucket.deleteKey(keyName);
+ String keyName = address.getKeyName();
+
+ if (bucket.getBucketLayout().isFileSystemOptimized()) {
+ // Handle FSO delete key which supports trash also
+ deleteFSOKey(bucket, keyName);
+ } else {
+ bucket.deleteKey(keyName);
+ }
+ }
+
+ private void deleteFSOKey(OzoneBucket bucket, String keyName)
+ throws IOException {
+ float hadoopTrashInterval = getConf().getFloat(
+ FS_TRASH_INTERVAL_KEY, FS_TRASH_INTERVAL_DEFAULT);
+ long trashInterval =
+ (long) (getConf().getFloat(
+ OMConfigKeys.OZONE_FS_TRASH_INTERVAL_KEY,
+ hadoopTrashInterval) * 10000);
+
+ // If Bucket layout is FSO and Trash is enabled
+ // In this case during delete operation move key to trash
+ if (trashInterval > 0 && !skipTrash &&
Review Comment:
Since we are adding the skipTrash flag here, just a minor suggestion to
reduce one operation in if condition for trashInterval and check like below:
if (!skipTrash && trashInterval > 0 && !keyName.contains(TRASH_PREFIX)) as
skipTrash itself may be false.
##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/keys/DeleteKeyHandler.java:
##########
@@ -35,16 +48,117 @@
description = "deletes an existing key")
public class DeleteKeyHandler extends KeyHandler {
+ @CommandLine.Option(names = "--skipTrash",
+ description = "Specify whether to skip Trash ")
+ private boolean skipTrash = false;
+
+ private static final Path CURRENT = new Path("Current");
+
@Override
protected void execute(OzoneClient client, OzoneAddress address)
throws IOException, OzoneClientException {
String volumeName = address.getVolumeName();
String bucketName = address.getBucketName();
- String keyName = address.getKeyName();
-
OzoneVolume vol = client.getObjectStore().getVolume(volumeName);
OzoneBucket bucket = vol.getBucket(bucketName);
- bucket.deleteKey(keyName);
+ String keyName = address.getKeyName();
+
+ if (bucket.getBucketLayout().isFileSystemOptimized()) {
+ // Handle FSO delete key which supports trash also
+ deleteFSOKey(bucket, keyName);
+ } else {
+ bucket.deleteKey(keyName);
+ }
+ }
+
+ private void deleteFSOKey(OzoneBucket bucket, String keyName)
+ throws IOException {
+ float hadoopTrashInterval = getConf().getFloat(
+ FS_TRASH_INTERVAL_KEY, FS_TRASH_INTERVAL_DEFAULT);
+ long trashInterval =
+ (long) (getConf().getFloat(
+ OMConfigKeys.OZONE_FS_TRASH_INTERVAL_KEY,
+ hadoopTrashInterval) * 10000);
+
+ // If Bucket layout is FSO and Trash is enabled
+ // In this case during delete operation move key to trash
+ if (trashInterval > 0 && !skipTrash &&
+ !keyName.contains(TRASH_PREFIX)) {
+ keyName = OzoneFSUtils.removeTrailingSlashIfNeeded(keyName);
+ // Check if key exists in Ozone
+ boolean bKeyExist = isKeyExist(bucket, keyName);
+ if (!bKeyExist) {
+ out().printf("Key not found %s %n", keyName);
+ return;
+ }
+
+ if (bucket.getFileStatus(keyName).isDirectory()) {
+ List<OzoneFileStatus> ozoneFileStatusList =
+ bucket.listStatus(keyName, false, "", 1);
+ if (ozoneFileStatusList != null && !ozoneFileStatusList.isEmpty()) {
+ out().printf("Directory is not empty %n");
+ return;
+ }
+ }
+
+ final String username =
+ UserGroupInformation.getCurrentUser().getShortUserName();
+ Path trashRoot = new Path(OZONE_URI_DELIMITER, TRASH_PREFIX);
+ Path userTrash = new Path(trashRoot, username);
+ Path userTrashCurrent = new Path(userTrash, CURRENT);
+
+ String trashDirectory = (keyName.contains("/")
+ ? new Path(userTrashCurrent, keyName.substring(0,
+ keyName.lastIndexOf("/")))
+ : userTrashCurrent).toUri().getPath();
+
+ String toKeyName = new Path(userTrashCurrent, keyName).toUri().getPath();
+ bKeyExist = isKeyExist(bucket, toKeyName);
+
+ if (bKeyExist) {
+ if (bucket.getFileStatus(toKeyName).isDirectory()) {
+ // if directory already exist in trash, just delete the directory
+ bucket.deleteKey(keyName);
+ return;
+ }
+ // Key already exists in trash, Append timestamp with keyName
+ // And move into trash
+ // Same behaviour as filesystem trash
+ toKeyName += Time.now();
+ }
+
+ // Check whether trash directory already exist inside bucket
+ bKeyExist = isKeyExist(bucket, trashDirectory);
+ if (!bKeyExist) {
+ // Trash directory doesn't exist
+ // Create directory inside trash
+ bucket.createDirectory(trashDirectory);
+ }
+
+ // Rename key to move inside trash folder
+ bucket.renameKey(keyName, toKeyName);
+ out().printf("Key moved inside Trash: %s %n", toKeyName);
+ } else if (trashInterval > 0 && !skipTrash &&
+ keyName.contains(TRASH_PREFIX)) {
+ // Delete from trash not possible when user didn't do skipTrash
+ out().printf("Use --skipTrash to delete key from Trash %n");
+ } else {
+ bucket.deleteKey(keyName);
+ }
+ }
+
+ private boolean isKeyExist(OzoneBucket bucket, String keyName) {
+ OzoneKeyDetails keyDetails;
+ try {
+ // check whether key exist
+ keyDetails = bucket.getKey(keyName);
+ } catch (IOException e) {
+ return false;
+ }
+ if (keyDetails == null) {
+ return false;
+ }
+ return true;
Review Comment:
can we change with one line like below:
return (keyDetails != null)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]