From 8806f23ee4a83413fd020d532135a347c1b28280 Mon Sep 17 00:00:00 2001 From: Zhihong Yu Date: Thu, 16 May 2013 21:31:05 +0000 Subject: [PATCH] HBASE-8461 Provide the ability to delete multiple snapshots through single command (Ted Yu) git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1483574 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop/hbase/client/HBaseAdmin.java | 59 +++++++++++++++++++ .../hbase/client/TestSnapshotFromClient.java | 39 ++++++++++++ 2 files changed, 98 insertions(+) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java index d1732f108c8..0972aeb98d5 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java @@ -2552,6 +2552,35 @@ public class HBaseAdmin implements Abortable, Closeable { }); } + /** + * 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 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 listSnapshots(Pattern pattern) throws IOException { + List matched = new LinkedList(); + List 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 @@ -2582,6 +2611,36 @@ public class HBaseAdmin implements Abortable, Closeable { }); } + /** + * 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 snapshots = listSnapshots(pattern); + for (final SnapshotDescription snapshot : snapshots) { + // do the delete + execute(new MasterAdminCallable() { + @Override + public Void call() throws ServiceException { + masterAdmin.deleteSnapshot( + null, + DeleteSnapshotRequest.newBuilder().setSnapshot(snapshot).build()); + return null; + } + }); + } + } + /** * @see {@link #execute(MasterAdminCallable)} */ diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java index 2907e39a1a8..671e76ec98e 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java @@ -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; @@ -140,6 +142,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 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