ashishkumar50 commented on code in PR #4842:
URL: https://github.com/apache/ozone/pull/4842#discussion_r1222896529


##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/volume/DeleteVolumeHandler.java:
##########
@@ -18,27 +18,220 @@
 
 package org.apache.hadoop.ozone.shell.volume;
 
+import com.google.common.base.Strings;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.ozone.OmUtils;
+import org.apache.hadoop.ozone.client.OzoneBucket;
 import org.apache.hadoop.ozone.client.OzoneClient;
+import org.apache.hadoop.ozone.client.OzoneKey;
+import org.apache.hadoop.ozone.client.OzoneVolume;
 import org.apache.hadoop.ozone.shell.OzoneAddress;
 
+import picocli.CommandLine;
 import picocli.CommandLine.Command;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static 
org.apache.hadoop.fs.CommonConfigurationKeysPublic.FS_TRASH_INTERVAL_KEY;
+import static org.apache.hadoop.fs.FileSystem.FS_DEFAULT_NAME_KEY;
+import static org.apache.hadoop.hdds.scm.net.NetConstants.PATH_SEPARATOR_STR;
+import static org.apache.hadoop.ozone.OzoneConsts.OZONE_OFS_URI_SCHEME;
 
 /**
  * Executes deleteVolume call for the shell.
  */
 @Command(name = "delete",
-    description = "deletes a volume if it is empty")
+    description = "deletes a volume")
 public class DeleteVolumeHandler extends VolumeHandler {
+  @CommandLine.Option(
+      names = {"-skipTrash"},
+      description = "Delete volume without trash"
+  )
+  private boolean bSkipTrash = false;
+  @CommandLine.Option(
+      names = {"-r"},
+      description = "Delete volume recursively"
+  )
+  private boolean bRecursive = false;
+  @CommandLine.Option(
+      names = {"-id", "--om-service-id"},
+      description = "Ozone Manager Service ID"
+  )
+  private String omServiceId;
+  private ExecutorService executor;
+  private List<String> bucketIdList = new ArrayList<>();
+  private AtomicInteger cleanedBucketCounter =
+      new AtomicInteger();
+  private int totalBucketCount;
+  private OzoneVolume vol;
+  private AtomicInteger numberOfBucketsCleaned = new AtomicInteger(0);
+  private volatile Throwable exception;
+  private static final int MAX_KEY_DELETE_BATCH_SIZE = 1000;
 
   @Override
   protected void execute(OzoneClient client, OzoneAddress address)
       throws IOException {
 
     String volumeName = address.getVolumeName();
-
+    try {
+      if (bRecursive) {
+        if (!bSkipTrash) {
+          out().printf("Use -skipTrash for recursive volume delete%n");
+          return;
+        }
+        if (OmUtils.isServiceIdsDefined(getConf()) &&
+            Strings.isNullOrEmpty(omServiceId)) {
+          out().printf("OmServiceID not provided, provide using " +
+              "-id <OM_SERVICE_ID>%n");
+          return;
+        }
+        vol = client.getObjectStore().getVolume(volumeName);
+        deleteVolumeRecursive();
+      }
+    } catch (InterruptedException e) {
+      out().printf("Exception while deleting volume recursively%n");
+      return;
+    }
     client.getObjectStore().deleteVolume(volumeName);
     out().printf("Volume %s is deleted%n", volumeName);
   }
+
+  private void deleteVolumeRecursive()
+      throws InterruptedException {
+    // Get all the buckets for given volume
+    Iterator<? extends OzoneBucket> bucketIterator =
+        vol.listBuckets(null);
+
+    while (bucketIterator.hasNext()) {
+      OzoneBucket bucket = bucketIterator.next();
+      bucketIdList.add(bucket.getName());
+      totalBucketCount++;
+    }
+    doCleanBuckets();
+  }
+
+  /**
+   * Clean OBS bucket recursively.
+   *
+   * @param  bucket OzoneBucket
+   * @return boolean
+   */
+  private boolean cleanOBSBucket(OzoneBucket bucket) {
+    ArrayList<String> keys = new ArrayList<>();
+    try {
+      if (!bucket.isLink()) {
+        Iterator<? extends OzoneKey> iterator = bucket.listKeys(null);
+        while (iterator.hasNext()) {
+          keys.add(iterator.next().getName());
+          if (MAX_KEY_DELETE_BATCH_SIZE == keys.size()) {
+            bucket.deleteKeys(keys);
+            keys.clear();
+          }
+        }
+        // delete if any remaining keys left
+        if (keys.size() > 0) {
+          bucket.deleteKeys(keys);
+        }
+      }
+      vol.deleteBucket(bucket.getName());
+      numberOfBucketsCleaned.getAndIncrement();
+      return true;
+    } catch (Exception e) {
+      LOG.error("Could not clean bucket ", e);
+      return false;
+    }
+  }
+
+  /**
+   * Clean Legacy/FSO bucket recursively.
+   *
+   * @param  bucket OzoneBucket
+   * @return boolean
+   */
+  private boolean cleanFSBucket(OzoneBucket bucket) {
+    try {
+      String hostPrefix = OZONE_OFS_URI_SCHEME + "://";
+      if (!Strings.isNullOrEmpty(omServiceId)) {
+        hostPrefix += omServiceId + PATH_SEPARATOR_STR;
+      }
+      String ofsPrefix = hostPrefix + vol.getName() + PATH_SEPARATOR_STR +
+          bucket.getName();
+      final Path path = new Path(ofsPrefix);
+      OzoneConfiguration clientConf = new OzoneConfiguration(getConf());
+      clientConf.set(FS_DEFAULT_NAME_KEY, hostPrefix);
+      clientConf.setInt(FS_TRASH_INTERVAL_KEY, 0);

Review Comment:
   Removed FS_TRASH_INTERVAL_KEY set as not required.



-- 
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]

Reply via email to