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 TestSnapshotFromAdmin {
45
46 private static final Log LOG = LogFactory.getLog(TestSnapshotFromAdmin.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
87 Mockito.when(
88 mockMaster.isSnapshotDone(
89 Mockito.any(HSnapshotDescription.class))).thenReturn(false, false,
90 false, false, false, true);
91
92
93 HBaseAdmin admin = new HBaseAdmin(mockConnection);
94 String snapshot = "snapshot";
95 String table = "table";
96
97 long start = System.currentTimeMillis();
98 admin.snapshot(snapshot, table);
99 long finish = System.currentTimeMillis();
100 long elapsed = (finish - start);
101 assertTrue("Elapsed time:" + elapsed + " is more than expected max:" + time, elapsed <= time);
102 admin.close();
103 }
104
105
106
107
108
109
110 @Test
111 public void testValidateSnapshotName() throws Exception {
112 HConnectionManager.HConnectionImplementation mockConnection = Mockito
113 .mock(HConnectionManager.HConnectionImplementation.class);
114 Configuration conf = HBaseConfiguration.create();
115 Mockito.when(mockConnection.getConfiguration()).thenReturn(conf);
116 HBaseAdmin admin = new HBaseAdmin(mockConnection);
117 SnapshotDescription.Builder builder = SnapshotDescription.newBuilder();
118
119 failSnapshotStart(admin, builder.setName(HConstants.SNAPSHOT_DIR_NAME).build());
120 failSnapshotStart(admin, builder.setName("-snapshot").build());
121 failSnapshotStart(admin, builder.setName("snapshot fails").build());
122 failSnapshotStart(admin, builder.setName("snap$hot").build());
123
124 failSnapshotStart(admin, builder.setName("snapshot").setTable(".table").build());
125 failSnapshotStart(admin, builder.setName("snapshot").setTable("-table").build());
126 failSnapshotStart(admin, builder.setName("snapshot").setTable("table fails").build());
127 failSnapshotStart(admin, builder.setName("snapshot").setTable("tab%le").build());
128
129
130 HMasterInterface master = Mockito.mock(HMasterInterface.class);
131 Mockito.when(mockConnection.getMaster()).thenReturn(master);
132
133 Mockito.when(
134 master.snapshot(Mockito.any(HSnapshotDescription.class))).thenReturn((long)0);
135 Mockito.when(
136 master.isSnapshotDone(
137 Mockito.any(HSnapshotDescription.class))).thenReturn(true);
138
139
140 admin.snapshot(builder.setName("snapshot").setTable("table").build());
141 }
142
143 private void failSnapshotStart(HBaseAdmin admin, SnapshotDescription snapshot) throws IOException {
144 try {
145 admin.snapshot(snapshot);
146 fail("Snapshot should not have succeed with name:" + snapshot.getName());
147 } catch (IllegalArgumentException e) {
148 LOG.debug("Correctly failed to start snapshot:" + e.getMessage());
149 }
150 }
151 }