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 long minColsPerKey = 5;
66 long 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 MultiThreadedWriter writer = new MultiThreadedWriter(conf, TABLE_NAME,
81 LoadTestTool.COLUMN_FAMILY);
82 writer.setMultiPut(true);
83 writer.setColumnsPerKey(minColsPerKey, maxColsPerKey);
84 writer.setDataSize(minColDataSize, maxColDataSize);
85 writer.start(startKey, endKey, numThreads);
86 System.out.printf("Started loading data...");
87 writer.waitForFinish();
88 System.out.printf("Finished loading data...");
89 }
90
91 @Override
92 protected int doWork() throws Exception {
93 ProcessBasedLocalHBaseCluster hbaseCluster =
94 new ProcessBasedLocalHBaseCluster(conf, hbaseHome, numRegionServers);
95
96
97 hbaseCluster.start();
98
99
100 HBaseTestingUtility.createPreSplitLoadTestTable(conf, TABLE_NAME,
101 LoadTestTool.COLUMN_FAMILY, Compression.Algorithm.NONE,
102 DataBlockEncoding.NONE);
103
104 LOG.debug("Loading data....\n\n");
105 loadData();
106
107 LOG.debug("Sleeping for " + SLEEP_SEC_AFTER_DATA_LOAD +
108 " seconds....\n\n");
109 Threads.sleep(5 * SLEEP_SEC_AFTER_DATA_LOAD);
110
111 int metaRSPort = HBaseTestingUtility.getMetaRSPort(conf);
112
113 LOG.debug("Killing META region server running on port " + metaRSPort);
114 hbaseCluster.killRegionServer(metaRSPort);
115 Threads.sleep(2000);
116
117 LOG.debug("Restarting region server running on port metaRSPort");
118 hbaseCluster.startRegionServer(metaRSPort);
119 Threads.sleep(2000);
120
121 LOG.debug("Trying to scan meta");
122
123 HTable metaTable = new HTable(conf, HConstants.META_TABLE_NAME);
124 ResultScanner scanner = metaTable.getScanner(new Scan());
125 Result result;
126 while ((result = scanner.next()) != null) {
127 LOG.info("Region assignment from META: "
128 + Bytes.toStringBinary(result.getRow())
129 + " => "
130 + Bytes.toStringBinary(result.getFamilyMap(HConstants.CATALOG_FAMILY)
131 .get(HConstants.SERVER_QUALIFIER)));
132 }
133 return 0;
134 }
135
136 @Override
137 protected void addOptions() {
138 addRequiredOptWithArg(OPT_HBASE_HOME, "HBase home directory");
139 addOptWithArg(OPT_NUM_RS, "Number of Region Servers");
140 addOptWithArg(LoadTestTool.OPT_DATA_BLOCK_ENCODING,
141 LoadTestTool.OPT_DATA_BLOCK_ENCODING_USAGE);
142 }
143
144 @Override
145 protected void processOptions(CommandLine cmd) {
146 hbaseHome = cmd.getOptionValue(OPT_HBASE_HOME);
147 if (hbaseHome == null || !new File(hbaseHome).isDirectory()) {
148 throw new IllegalArgumentException("Invalid HBase home directory: " +
149 hbaseHome);
150 }
151
152 LOG.info("Using HBase home directory " + hbaseHome);
153 numRegionServers = Integer.parseInt(cmd.getOptionValue(OPT_NUM_RS,
154 String.valueOf(DEFAULT_NUM_RS)));
155 }
156
157 public static void main(String[] args) {
158 new RestartMetaTest().doStaticMain(args);
159 }
160
161 }