1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.snapshot;
20
21 import static org.junit.Assert.assertEquals;
22 import java.io.IOException;
23 import java.util.concurrent.atomic.AtomicInteger;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.HTableDescriptor;
28 import org.apache.hadoop.hbase.TableName;
29 import org.apache.hadoop.hbase.TableNameTestRule;
30 import org.apache.hadoop.hbase.coprocessor.BaseMasterObserver;
31 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
32 import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
33 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
34 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
35 import org.apache.hadoop.hbase.testclassification.MediumTests;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.experimental.categories.Category;
41
42 @Category({ MediumTests.class })
43 public class TestSnapshotClientRetries {
44 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
45 private static final Log LOG = LogFactory.getLog(TestSnapshotClientRetries.class);
46
47 @Rule public TableNameTestRule testTable = new TableNameTestRule();
48
49 @Before
50 public void setUp() throws Exception {
51 TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
52 MasterSyncObserver.class.getName());
53 TEST_UTIL.startMiniCluster(1);
54 }
55
56 @After
57 public void tearDown() throws Exception {
58 TEST_UTIL.shutdownMiniCluster();
59 }
60
61 @Test(timeout = 60000, expected=SnapshotExistsException.class)
62 public void testSnapshotAlreadyExist() throws Exception {
63 final String snapshotName = "testSnapshotAlreadyExist";
64 TEST_UTIL.createTable(testTable.getTableName(), "f");
65 TEST_UTIL.getHBaseAdmin().snapshot(snapshotName, testTable.getTableName());
66 snapshotAndAssertOneRetry(snapshotName, testTable.getTableName());
67 }
68
69 @Test(timeout = 60000, expected=SnapshotDoesNotExistException.class)
70 public void testCloneNonExistentSnapshot() throws Exception {
71 final String snapshotName = "testCloneNonExistentSnapshot";
72 cloneAndAssertOneRetry(snapshotName, testTable.getTableName());
73 }
74
75 public static class MasterSyncObserver extends BaseMasterObserver {
76 volatile AtomicInteger snapshotCount = null;
77 volatile AtomicInteger cloneCount = null;
78
79 @Override
80 public void preSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
81 final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
82 throws IOException {
83 if (snapshotCount != null) {
84 snapshotCount.incrementAndGet();
85 }
86 }
87
88 @Override
89 public void preCloneSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
90 final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
91 throws IOException {
92 if (cloneCount != null) {
93 cloneCount.incrementAndGet();
94 }
95 }
96 }
97
98 public void snapshotAndAssertOneRetry(final String snapshotName, final TableName tableName)
99 throws Exception {
100 MasterSyncObserver observer = getMasterSyncObserver();
101 observer.snapshotCount = new AtomicInteger(0);
102 TEST_UTIL.getHBaseAdmin().snapshot(snapshotName, tableName);
103 assertEquals(1, observer.snapshotCount.get());
104 }
105
106 public void cloneAndAssertOneRetry(final String snapshotName, final TableName tableName)
107 throws Exception {
108 MasterSyncObserver observer = getMasterSyncObserver();
109 observer.cloneCount = new AtomicInteger(0);
110 TEST_UTIL.getHBaseAdmin().cloneSnapshot(snapshotName, tableName);
111 assertEquals(1, observer.cloneCount.get());
112 }
113
114 private MasterSyncObserver getMasterSyncObserver() {
115 return (MasterSyncObserver)TEST_UTIL.getHBaseCluster().getMaster()
116 .getMasterCoprocessorHost().findCoprocessor(MasterSyncObserver.class.getName());
117 }
118 }