1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import java.io.IOException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.HBaseConfiguration;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.SmallTests;
31 import org.apache.hadoop.hbase.ipc.HMasterInterface;
32 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
33 import org.apache.hadoop.hbase.snapshot.HSnapshotDescription;
34 import org.junit.Test;
35 import org.junit.experimental.categories.Category;
36 import org.mockito.Mockito;
37
38 import com.google.protobuf.RpcController;
39
40
41
42
43 @Category(SmallTests.class)
44 public class TestSnapshotsFromAdmin {
45
46 private static final Log LOG = LogFactory.getLog(TestSnapshotsFromAdmin.class);
47
48
49
50
51
52
53 @Test(timeout = 60000)
54 public void testBackoffLogic() throws Exception {
55 final int maxWaitTime = 7500;
56 final int numRetries = 10;
57 final int pauseTime = 500;
58
59
60 long ignoreExpectedTime = 0;
61 for (int i = 0; i < 6; i++) {
62 ignoreExpectedTime += HConstants.RETRY_BACKOFF[i] * pauseTime;
63 }
64
65 final long time = pauseTime * 3 + ((maxWaitTime / numRetries) * 3) + 300;
66 assertTrue("Capped snapshot wait time isn't less that the uncapped backoff time "
67 + "- further testing won't prove anything.", time < ignoreExpectedTime);
68
69
70 HConnectionManager.HConnectionImplementation mockConnection = Mockito
71 .mock(HConnectionManager.HConnectionImplementation.class);
72 Configuration conf = HBaseConfiguration.create();
73
74 conf.setInt("hbase.client.retries.number", numRetries);
75 conf.setLong("hbase.client.pause", pauseTime);
76
77 HMasterInterface mockMaster = Mockito.mock(HMasterInterface.class);
78 Mockito.when(mockConnection.getConfiguration()).thenReturn(conf);
79 Mockito.when(mockConnection.getMaster()).thenReturn(mockMaster);
80
81 Mockito
82 .when(
83 mockMaster.snapshot(
84 Mockito.any(HSnapshotDescription.class))).thenReturn((long)maxWaitTime);
85
86 Mockito.when(
87 mockMaster.isSnapshotDone(
88 Mockito.any(HSnapshotDescription.class))).thenReturn(false, false,
89 false, false, false, true);
90
91
92 HBaseAdmin admin = new HBaseAdmin(mockConnection);
93 String snapshot = "snasphot";
94 String table = "table";
95
96 long start = System.currentTimeMillis();
97 admin.snapshot(snapshot, table);
98 long finish = System.currentTimeMillis();
99 long elapsed = (finish - start);
100 assertTrue("Elapsed time:" + elapsed + " is more than expected max:" + time, elapsed <= time);
101 }
102
103
104
105
106
107
108 @Test
109 public void testValidateSnapshotName() throws IOException {
110 HConnectionManager.HConnectionImplementation mockConnection = Mockito
111 .mock(HConnectionManager.HConnectionImplementation.class);
112 Configuration conf = HBaseConfiguration.create();
113 Mockito.when(mockConnection.getConfiguration()).thenReturn(conf);
114 HBaseAdmin admin = new HBaseAdmin(mockConnection);
115 SnapshotDescription.Builder builder = SnapshotDescription.newBuilder();
116
117 failSnapshotStart(admin, builder.setName(HConstants.SNAPSHOT_DIR_NAME).build());
118 failSnapshotStart(admin, builder.setName("-snapshot").build());
119 failSnapshotStart(admin, builder.setName("snapshot fails").build());
120 failSnapshotStart(admin, builder.setName("snap$hot").build());
121
122 failSnapshotStart(admin, builder.setName("snapshot").setTable(".table").build());
123 failSnapshotStart(admin, builder.setName("snapshot").setTable("-table").build());
124 failSnapshotStart(admin, builder.setName("snapshot").setTable("table fails").build());
125 failSnapshotStart(admin, builder.setName("snapshot").setTable("tab%le").build());
126 }
127
128 private void failSnapshotStart(HBaseAdmin admin, SnapshotDescription snapshot) throws IOException {
129 try {
130 admin.snapshot(snapshot);
131 fail("Snapshot should not have succeed with name:" + snapshot.getName());
132 } catch (IllegalArgumentException e) {
133 LOG.debug("Correctly failed to start snapshot:" + e.getMessage());
134 }
135 }
136 }