1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.util;
18
19 import java.io.File;
20 import java.io.IOException;
21
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.hbase.HBaseTestingUtility;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.client.HTable;
28 import org.apache.hadoop.hbase.client.Result;
29 import org.apache.hadoop.hbase.client.ResultScanner;
30 import org.apache.hadoop.hbase.client.Scan;
31 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
32 import org.apache.hadoop.hbase.io.hfile.Compression;
33
34
35
36
37
38
39 public class RestartMetaTest extends AbstractHBaseTool {
40
41 private static final Log LOG = LogFactory.getLog(RestartMetaTest.class);
42
43
44 private static final int DEFAULT_NUM_RS = 2;
45
46
47 private static byte[] TABLE_NAME = Bytes.toBytes("load_test");
48
49
50 private static final int SLEEP_SEC_AFTER_DATA_LOAD = 5;
51
52
53 private int numRegionServers;
54
55
56 private String hbaseHome;
57
58 private static final String OPT_HBASE_HOME = "hbase_home";
59 private static final String OPT_NUM_RS = "num_rs";
60
61
62 private void loadData() throws IOException {
63 long startKey = 0;
64 long endKey = 100000;
65 int minColsPerKey = 5;
66 int maxColsPerKey = 15;
67 int minColDataSize = 256;
68 int maxColDataSize = 256 * 3;
69 int numThreads = 10;
70
71
72 System.out.printf("Key range %d .. %d\n", startKey, endKey);
73 System.out.printf("Number of Columns/Key: %d..%d\n", minColsPerKey,
74 maxColsPerKey);
75 System.out.printf("Data Size/Column: %d..%d bytes\n", minColDataSize,
76 maxColDataSize);
77 System.out.printf("Client Threads: %d\n", numThreads);
78
79
80 LoadTestDataGenerator dataGen = new MultiThreadedAction.DefaultDataGenerator(
81 minColDataSize, maxColDataSize, minColsPerKey, maxColsPerKey, LoadTestTool.COLUMN_FAMILY);
82 MultiThreadedWriter writer = new MultiThreadedWriter(dataGen, conf, TABLE_NAME);
83 writer.setMultiPut(true);
84 writer.start(startKey, endKey, numThreads);
85 System.out.printf("Started loading data...");
86 writer.waitForFinish();
87 System.out.printf("Finished loading data...");
88 }
89
90 @Override
91 protected int doWork() throws Exception {
92 ProcessBasedLocalHBaseCluster hbaseCluster =
93 new ProcessBasedLocalHBaseCluster(conf, hbaseHome, numRegionServers);
94
95
96 hbaseCluster.start();
97
98
99 HBaseTestingUtility.createPreSplitLoadTestTable(conf, TABLE_NAME,
100 LoadTestTool.COLUMN_FAMILY, Compression.Algorithm.NONE,
101 DataBlockEncoding.NONE);
102
103 LOG.debug("Loading data....\n\n");
104 loadData();
105
106 LOG.debug("Sleeping for " + SLEEP_SEC_AFTER_DATA_LOAD +
107 " seconds....\n\n");
108 Threads.sleep(5 * SLEEP_SEC_AFTER_DATA_LOAD);
109
110 int metaRSPort = HBaseTestingUtility.getMetaRSPort(conf);
111
112 LOG.debug("Killing META region server running on port " + metaRSPort);
113 hbaseCluster.killRegionServer(metaRSPort);
114 Threads.sleep(2000);
115
116 LOG.debug("Restarting region server running on port metaRSPort");
117 hbaseCluster.startRegionServer(metaRSPort);
118 Threads.sleep(2000);
119
120 LOG.debug("Trying to scan meta");
121
122 HTable metaTable = new HTable(conf, HConstants.META_TABLE_NAME);
123 ResultScanner scanner = metaTable.getScanner(new Scan());
124 Result result;
125 while ((result = scanner.next()) != null) {
126 LOG.info("Region assignment from META: "
127 + Bytes.toStringBinary(result.getRow())
128 + " => "
129 + Bytes.toStringBinary(result.getFamilyMap(HConstants.CATALOG_FAMILY)
130 .get(HConstants.SERVER_QUALIFIER)));
131 }
132 return 0;
133 }
134
135 @Override
136 protected void addOptions() {
137 addRequiredOptWithArg(OPT_HBASE_HOME, "HBase home directory");
138 addOptWithArg(OPT_NUM_RS, "Number of Region Servers");
139 addOptWithArg(LoadTestTool.OPT_DATA_BLOCK_ENCODING,
140 LoadTestTool.OPT_DATA_BLOCK_ENCODING_USAGE);
141 }
142
143 @Override
144 protected void processOptions(CommandLine cmd) {
145 hbaseHome = cmd.getOptionValue(OPT_HBASE_HOME);
146 if (hbaseHome == null || !new File(hbaseHome).isDirectory()) {
147 throw new IllegalArgumentException("Invalid HBase home directory: " +
148 hbaseHome);
149 }
150
151 LOG.info("Using HBase home directory " + hbaseHome);
152 numRegionServers = Integer.parseInt(cmd.getOptionValue(OPT_NUM_RS,
153 String.valueOf(DEFAULT_NUM_RS)));
154 }
155
156 public static void main(String[] args) {
157 new RestartMetaTest().doStaticMain(args);
158 }
159
160 }