You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by te...@apache.org on 2013/05/16 23:34:20 UTC

svn commit: r1483576 - in /hbase/branches/0.95: hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java

Author: tedyu
Date: Thu May 16 21:34:20 2013
New Revision: 1483576

URL: http://svn.apache.org/r1483576
Log:
HBASE-8461 Provide the ability to delete multiple snapshots through single command (Ted Yu)


Modified:
    hbase/branches/0.95/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java
    hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java

Modified: hbase/branches/0.95/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.95/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java?rev=1483576&r1=1483575&r2=1483576&view=diff
==============================================================================
--- hbase/branches/0.95/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java (original)
+++ hbase/branches/0.95/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java Thu May 16 21:34:20 2013
@@ -2553,6 +2553,35 @@ public class HBaseAdmin implements Abort
   }
 
   /**
+   * List all the completed snapshots matching the given regular expression.
+   *
+   * @param regex The regular expression to match against
+   * @return - returns a List of SnapshotDescription
+   * @throws IOException if a remote or network exception occurs
+   */
+  public List<SnapshotDescription> listSnapshots(String regex) throws IOException {
+    return listSnapshots(Pattern.compile(regex));
+  }
+  
+  /**
+   * List all the completed snapshots matching the given pattern.
+   *
+   * @param pattern The compiled regular expression to match against
+   * @return - returns a List of SnapshotDescription
+   * @throws IOException if a remote or network exception occurs
+   */
+  public List<SnapshotDescription> listSnapshots(Pattern pattern) throws IOException {
+    List<SnapshotDescription> matched = new LinkedList<SnapshotDescription>();
+    List<SnapshotDescription> snapshots = listSnapshots();
+    for (SnapshotDescription snapshot : snapshots) {
+      if (pattern.matcher(snapshot.getName()).matches()) {
+        matched.add(snapshot);
+      }
+    }
+    return matched;
+  }
+  
+  /**
    * Delete an existing snapshot.
    * @param snapshotName name of the snapshot
    * @throws IOException if a remote or network exception occurs
@@ -2583,6 +2612,36 @@ public class HBaseAdmin implements Abort
   }
 
   /**
+   * Delete existing snapshots whose names match the pattern passed.
+   * @param regex The regular expression to match against
+   * @throws IOException if a remote or network exception occurs
+   */
+  public void deleteSnapshots(final String regex) throws IOException {
+    deleteSnapshots(Pattern.compile(regex));
+  }
+  
+  /**
+   * Delete existing snapshots whose names match the pattern passed.
+   * @param pattern pattern for names of the snapshot to match
+   * @throws IOException if a remote or network exception occurs
+   */
+  public void deleteSnapshots(final Pattern pattern) throws IOException {
+    List<SnapshotDescription> snapshots = listSnapshots(pattern);
+    for (final SnapshotDescription snapshot : snapshots) {
+      // do the delete
+      execute(new MasterAdminCallable<Void>() {
+        @Override
+        public Void call() throws ServiceException {
+          masterAdmin.deleteSnapshot(
+            null,
+            DeleteSnapshotRequest.newBuilder().setSnapshot(snapshot).build());
+          return null;
+        }
+      });
+    }
+  }
+  
+  /**
    * @see {@link #execute(MasterAdminCallable<V>)}
    */
   private abstract static class MasterAdminCallable<V> implements Callable<V>{

Modified: hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java?rev=1483576&r1=1483575&r2=1483576&view=diff
==============================================================================
--- hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java (original)
+++ hbase/branches/0.95/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java Thu May 16 21:34:20 2013
@@ -17,12 +17,14 @@
  */
 package org.apache.hadoop.hbase.client;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
 import java.io.IOException;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -141,6 +143,43 @@ public class TestSnapshotFromClient {
   }
 
   /**
+   * Test HBaseAdmin#deleteSnapshots(String) which deletes snapshots whose names match the parameter
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void testSnapshotDeletionWithRegex() throws Exception {
+    HBaseAdmin admin = UTIL.getHBaseAdmin();
+    // make sure we don't fail on listing snapshots
+    SnapshotTestingUtils.assertNoSnapshots(admin);
+
+    // put some stuff in the table
+    HTable table = new HTable(UTIL.getConfiguration(), TABLE_NAME);
+    UTIL.loadTable(table, TEST_FAM);
+    table.close();
+    
+    byte[] snapshot1 = Bytes.toBytes("TableSnapshot1");
+    admin.snapshot(snapshot1, TABLE_NAME);
+    LOG.debug("Snapshot1 completed.");
+    
+    byte[] snapshot2 = Bytes.toBytes("TableSnapshot2");
+    admin.snapshot(snapshot2, TABLE_NAME);
+    LOG.debug("Snapshot2 completed.");
+    
+    String snapshot3 = "3rdTableSnapshot";
+    admin.snapshot(Bytes.toBytes(snapshot3), TABLE_NAME);
+    LOG.debug(snapshot3 + " completed.");
+
+    // delete the first two snapshots
+    admin.deleteSnapshots("TableSnapshot.*");
+    List<SnapshotDescription> snapshots = admin.listSnapshots();
+    assertEquals(1, snapshots.size());
+    assertEquals(snapshots.get(0).getName(), snapshot3);
+    
+    admin.deleteSnapshot(snapshot3);
+    admin.close();
+  }
+  /**
    * Test snapshotting a table that is offline
    * @throws Exception
    */