1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.test;
20
21 import com.google.common.base.Joiner;
22 import org.apache.commons.cli.CommandLine;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.conf.Configured;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.HBaseConfiguration;
29 import org.apache.hadoop.hbase.HRegionLocation;
30 import org.apache.hadoop.hbase.IntegrationTestingUtility;
31 import org.apache.hadoop.hbase.ServerName;
32 import org.apache.hadoop.hbase.TableName;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.client.Connection;
35 import org.apache.hadoop.hbase.client.ConnectionFactory;
36 import org.apache.hadoop.hbase.client.replication.ReplicationAdmin;
37 import org.apache.hadoop.hbase.client.Admin;
38 import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
39 import org.apache.hadoop.util.Tool;
40 import org.apache.hadoop.util.ToolRunner;
41
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.Set;
45 import java.util.TreeSet;
46 import java.util.UUID;
47
48
49
50
51
52
53
54
55 public class IntegrationTestReplication extends IntegrationTestBigLinkedList {
56 protected String sourceClusterIdString;
57 protected String sinkClusterIdString;
58 protected int numIterations;
59 protected int numMappers;
60 protected long numNodes;
61 protected String outputDir;
62 protected int numReducers;
63 protected int generateVerifyGap;
64 protected Integer width;
65 protected Integer wrapMultiplier;
66
67 private final String SOURCE_CLUSTER_OPT = "sourceCluster";
68 private final String DEST_CLUSTER_OPT = "destCluster";
69 private final String ITERATIONS_OPT = "iterations";
70 private final String NUM_MAPPERS_OPT = "numMappers";
71 private final String OUTPUT_DIR_OPT = "outputDir";
72 private final String NUM_REDUCERS_OPT = "numReducers";
73
74
75
76
77
78 private final String GENERATE_VERIFY_GAP_OPT = "generateVerifyGap";
79
80
81
82
83
84 private final String WIDTH_OPT = "width";
85
86
87
88
89
90 private final String WRAP_MULTIPLIER_OPT = "wrapMultiplier";
91
92
93
94
95
96
97 private final String NUM_NODES_OPT = "numNodes";
98
99 private final int DEFAULT_NUM_MAPPERS = 1;
100 private final int DEFAULT_NUM_REDUCERS = 1;
101 private final int DEFAULT_NUM_ITERATIONS = 1;
102 private final int DEFAULT_GENERATE_VERIFY_GAP = 60;
103 private final int DEFAULT_WIDTH = 1000000;
104 private final int DEFAULT_WRAP_MULTIPLIER = 25;
105 private final int DEFAULT_NUM_NODES = DEFAULT_WIDTH * DEFAULT_WRAP_MULTIPLIER;
106
107
108
109
110
111 protected class ClusterID {
112 private final Configuration configuration;
113 private Connection connection = null;
114
115
116
117
118
119
120
121
122 public ClusterID(Configuration base,
123 String key) {
124 configuration = new Configuration(base);
125 String[] parts = key.split(":");
126 configuration.set(HConstants.ZOOKEEPER_QUORUM, parts[0]);
127 configuration.set(HConstants.ZOOKEEPER_CLIENT_PORT, parts[1]);
128 configuration.set(HConstants.ZOOKEEPER_ZNODE_PARENT, parts[2]);
129 }
130
131 @Override
132 public String toString() {
133 return Joiner.on(":").join(configuration.get(HConstants.ZOOKEEPER_QUORUM),
134 configuration.get(HConstants.ZOOKEEPER_CLIENT_PORT),
135 configuration.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
136 }
137
138 public Configuration getConfiguration() {
139 return this.configuration;
140 }
141
142 public Connection getConnection() throws Exception {
143 if (this.connection == null) {
144 this.connection = ConnectionFactory.createConnection(this.configuration);
145 }
146 return this.connection;
147 }
148
149 public void closeConnection() throws Exception {
150 this.connection.close();
151 this.connection = null;
152 }
153
154 public boolean equals(ClusterID other) {
155 return this.toString().equalsIgnoreCase(other.toString());
156 }
157 }
158
159
160
161
162
163
164
165 protected class VerifyReplicationLoop extends Configured implements Tool {
166 private final Log LOG = LogFactory.getLog(VerifyReplicationLoop.class);
167 protected ClusterID source;
168 protected ClusterID sink;
169
170 IntegrationTestBigLinkedList integrationTestBigLinkedList;
171
172
173
174
175
176
177
178
179
180 protected void setupTablesAndReplication() throws Exception {
181 TableName tableName = getTableName(source.getConfiguration());
182
183 ClusterID[] clusters = {source, sink};
184
185
186 for (ClusterID cluster : clusters) {
187 Admin admin = cluster.getConnection().getAdmin();
188
189 if (admin.tableExists(tableName)) {
190 if (admin.isTableEnabled(tableName)) {
191 admin.disableTable(tableName);
192 }
193
194
195
196
197
198
199
200
201
202 Set<ServerName> regionServers = new TreeSet<>();
203 for (HRegionLocation rl :
204 cluster.getConnection().getRegionLocator(tableName).getAllRegionLocations()) {
205 regionServers.add(rl.getServerName());
206 }
207
208 for (ServerName server : regionServers) {
209 source.getConnection().getAdmin().rollWALWriter(server);
210 }
211
212 admin.deleteTable(tableName);
213 }
214 }
215
216
217 Generator generator = new Generator();
218 generator.setConf(source.getConfiguration());
219 generator.createSchema();
220
221
222 if (!source.equals(sink)) {
223 ReplicationAdmin replicationAdmin = new ReplicationAdmin(source.getConfiguration());
224
225 for (String oldPeer : replicationAdmin.listPeerConfigs().keySet()) {
226 replicationAdmin.removePeer(oldPeer);
227 }
228
229
230 ReplicationPeerConfig peerConfig = new ReplicationPeerConfig();
231 peerConfig.setClusterKey(sink.toString());
232
233
234 HashMap<TableName, ArrayList<String>> toReplicate = new HashMap<>();
235 toReplicate.put(tableName, new ArrayList<String>(0));
236
237 replicationAdmin.addPeer("TestPeer", peerConfig, toReplicate);
238
239 replicationAdmin.enableTableRep(tableName);
240 replicationAdmin.close();
241 }
242
243 for (ClusterID cluster : clusters) {
244 cluster.closeConnection();
245 }
246 }
247
248 protected void waitForReplication() throws Exception {
249
250
251 Thread.sleep(generateVerifyGap * 1000);
252 }
253
254
255
256
257
258
259
260 protected void runGenerator() throws Exception {
261 Path outputPath = new Path(outputDir);
262 UUID uuid = UUID.randomUUID();
263 Path generatorOutput = new Path(outputPath, uuid.toString());
264
265 Generator generator = new Generator();
266 generator.setConf(source.getConfiguration());
267
268 int retCode = generator.run(numMappers, numNodes, generatorOutput, width, wrapMultiplier);
269 if (retCode > 0) {
270 throw new RuntimeException("Generator failed with return code: " + retCode);
271 }
272 }
273
274
275
276
277
278
279
280
281
282
283 protected void runVerify(long expectedNumNodes) throws Exception {
284 Path outputPath = new Path(outputDir);
285 UUID uuid = UUID.randomUUID();
286 Path iterationOutput = new Path(outputPath, uuid.toString());
287
288 Verify verify = new Verify();
289 verify.setConf(sink.getConfiguration());
290
291 int retCode = verify.run(iterationOutput, numReducers);
292 if (retCode > 0) {
293 throw new RuntimeException("Verify.run failed with return code: " + retCode);
294 }
295
296 if (!verify.verify(expectedNumNodes)) {
297 throw new RuntimeException("Verify.verify failed");
298 }
299
300 LOG.info("Verify finished with success. Total nodes=" + expectedNumNodes);
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 @Override
317 public int run(String[] args) throws Exception {
318 source = new ClusterID(getConf(), sourceClusterIdString);
319 sink = new ClusterID(getConf(), sinkClusterIdString);
320
321 setupTablesAndReplication();
322 int expectedNumNodes = 0;
323 for (int i = 0; i < numIterations; i++) {
324 LOG.info("Starting iteration = " + i);
325
326 expectedNumNodes += numMappers * numNodes;
327
328 runGenerator();
329 waitForReplication();
330 runVerify(expectedNumNodes);
331 }
332
333
334
335
336
337 return 0;
338 }
339 }
340
341 @Override
342 protected void addOptions() {
343 super.addOptions();
344 addRequiredOptWithArg("s", SOURCE_CLUSTER_OPT,
345 "Cluster ID of the source cluster (e.g. localhost:2181:/hbase)");
346 addRequiredOptWithArg("r", DEST_CLUSTER_OPT,
347 "Cluster ID of the sink cluster (e.g. localhost:2182:/hbase)");
348 addRequiredOptWithArg("d", OUTPUT_DIR_OPT,
349 "Temporary directory where to write keys for the test");
350
351
352 addOptWithArg("nm", NUM_MAPPERS_OPT,
353 "Number of mappers (default: " + DEFAULT_NUM_MAPPERS + ")");
354 addOptWithArg("nr", NUM_REDUCERS_OPT,
355 "Number of reducers (default: " + DEFAULT_NUM_MAPPERS + ")");
356 addOptWithArg("n", NUM_NODES_OPT,
357 "Number of nodes. This should be a multiple of width * wrapMultiplier." +
358 " (default: " + DEFAULT_NUM_NODES + ")");
359 addOptWithArg("i", ITERATIONS_OPT, "Number of iterations to run (default: " +
360 DEFAULT_NUM_ITERATIONS + ")");
361 addOptWithArg("t", GENERATE_VERIFY_GAP_OPT,
362 "Gap between generate and verify steps in seconds (default: " +
363 DEFAULT_GENERATE_VERIFY_GAP + ")");
364 addOptWithArg("w", WIDTH_OPT,
365 "Width of the linked list chain (default: " + DEFAULT_WIDTH + ")");
366 addOptWithArg("wm", WRAP_MULTIPLIER_OPT, "How many times to wrap around (default: " +
367 DEFAULT_WRAP_MULTIPLIER + ")");
368 }
369
370 @Override
371 protected void processOptions(CommandLine cmd) {
372 processBaseOptions(cmd);
373
374 sourceClusterIdString = cmd.getOptionValue(SOURCE_CLUSTER_OPT);
375 sinkClusterIdString = cmd.getOptionValue(DEST_CLUSTER_OPT);
376 outputDir = cmd.getOptionValue(OUTPUT_DIR_OPT);
377
378
379 numMappers = parseInt(cmd.getOptionValue(NUM_MAPPERS_OPT,
380 Integer.toString(DEFAULT_NUM_MAPPERS)),
381 1, Integer.MAX_VALUE);
382 numReducers = parseInt(cmd.getOptionValue(NUM_REDUCERS_OPT,
383 Integer.toString(DEFAULT_NUM_REDUCERS)),
384 1, Integer.MAX_VALUE);
385 numNodes = parseInt(cmd.getOptionValue(NUM_NODES_OPT, Integer.toString(DEFAULT_NUM_NODES)),
386 1, Integer.MAX_VALUE);
387 generateVerifyGap = parseInt(cmd.getOptionValue(GENERATE_VERIFY_GAP_OPT,
388 Integer.toString(DEFAULT_GENERATE_VERIFY_GAP)),
389 1, Integer.MAX_VALUE);
390 numIterations = parseInt(cmd.getOptionValue(ITERATIONS_OPT,
391 Integer.toString(DEFAULT_NUM_ITERATIONS)),
392 1, Integer.MAX_VALUE);
393 width = parseInt(cmd.getOptionValue(WIDTH_OPT, Integer.toString(DEFAULT_WIDTH)),
394 1, Integer.MAX_VALUE);
395 wrapMultiplier = parseInt(cmd.getOptionValue(WRAP_MULTIPLIER_OPT,
396 Integer.toString(DEFAULT_WRAP_MULTIPLIER)),
397 1, Integer.MAX_VALUE);
398
399 if (numNodes % (width * wrapMultiplier) != 0) {
400 throw new RuntimeException("numNodes must be a multiple of width and wrap multiplier");
401 }
402 }
403
404 @Override
405 public int runTestFromCommandLine() throws Exception {
406 VerifyReplicationLoop tool = new VerifyReplicationLoop();
407 tool.integrationTestBigLinkedList = this;
408 return ToolRunner.run(getConf(), tool, null);
409 }
410
411 public static void main(String[] args) throws Exception {
412 Configuration conf = HBaseConfiguration.create();
413 IntegrationTestingUtility.setUseDistributedCluster(conf);
414 int ret = ToolRunner.run(conf, new IntegrationTestReplication(), args);
415 System.exit(ret);
416 }
417 }