View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.util;
18  
19  import java.io.IOException;
20  
21  import org.apache.commons.cli.CommandLine;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.HBaseTestingUtility;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.TableName;
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.client.Table;
32  import org.apache.hadoop.hbase.io.compress.Compression;
33  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
34  import org.apache.hadoop.hbase.util.test.LoadTestDataGenerator;
35  
36  /**
37   * A command-line tool that spins up a local process-based cluster, loads
38   * some data, restarts the regionserver holding hbase:meta, and verifies that the
39   * cluster recovers.
40   */
41  public class RestartMetaTest extends AbstractHBaseTool {
42  
43    private static final Log LOG = LogFactory.getLog(RestartMetaTest.class);
44  
45    /** The number of region servers used if not specified */
46    private static final int DEFAULT_NUM_RS = 2;
47  
48    /** Table name for the test */
49    private static TableName TABLE_NAME =
50        TableName.valueOf("load_test");
51  
52    /** The number of seconds to sleep after loading the data */
53    private static final int SLEEP_SEC_AFTER_DATA_LOAD = 5;
54  
55    /** The actual number of region servers */
56    private int numRegionServers;
57  
58    private static final String OPT_NUM_RS = "num_rs";
59  
60    private static final int NUM_DATANODES = 3;
61  
62    /** Loads data into the table using the multi-threaded writer. */
63    private void loadData() throws IOException {
64      long startKey = 0;
65      long endKey = 100000;
66      int minColsPerKey = 5;
67      int maxColsPerKey = 15;
68      int minColDataSize = 256;
69      int maxColDataSize = 256 * 3;
70      int numThreads = 10;
71  
72      // print out the arguments
73      System.out.printf("Key range %d .. %d\n", startKey, endKey);
74      System.out.printf("Number of Columns/Key: %d..%d\n", minColsPerKey,
75          maxColsPerKey);
76      System.out.printf("Data Size/Column: %d..%d bytes\n", minColDataSize,
77          maxColDataSize);
78      System.out.printf("Client Threads: %d\n", numThreads);
79  
80      // start the writers
81      LoadTestDataGenerator dataGen = new MultiThreadedAction.DefaultDataGenerator(
82        minColDataSize, maxColDataSize, minColsPerKey, maxColsPerKey, LoadTestTool.COLUMN_FAMILY);
83      MultiThreadedWriter writer = new MultiThreadedWriter(dataGen, conf, TABLE_NAME);
84      writer.setMultiPut(true);
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, NUM_DATANODES, numRegionServers);
95      hbaseCluster.startMiniDFS();
96  
97      // start the process based HBase cluster
98      hbaseCluster.startHBase();
99  
100     // create tables if needed
101     HBaseTestingUtility.createPreSplitLoadTestTable(conf, TABLE_NAME,
102         LoadTestTool.COLUMN_FAMILY, Compression.Algorithm.NONE,
103         DataBlockEncoding.NONE);
104 
105     LOG.debug("Loading data....\n\n");
106     loadData();
107 
108     LOG.debug("Sleeping for " + SLEEP_SEC_AFTER_DATA_LOAD +
109         " seconds....\n\n");
110     Threads.sleep(5 * SLEEP_SEC_AFTER_DATA_LOAD);
111 
112     int metaRSPort = HBaseTestingUtility.getMetaRSPort(conf);
113 
114     LOG.debug("Killing hbase:meta region server running on port " + metaRSPort);
115     hbaseCluster.killRegionServer(metaRSPort);
116     Threads.sleep(2000);
117 
118     LOG.debug("Restarting region server running on port metaRSPort");
119     hbaseCluster.startRegionServer(metaRSPort);
120     Threads.sleep(2000);
121 
122     LOG.debug("Trying to scan meta");
123 
124     Table metaTable = new HTable(conf, TableName.META_TABLE_NAME);
125     ResultScanner scanner = metaTable.getScanner(new Scan());
126     Result result;
127     while ((result = scanner.next()) != null) {
128       LOG.info("Region assignment from META: "
129           + Bytes.toStringBinary(result.getRow())
130           + " => "
131           + Bytes.toStringBinary(result.getFamilyMap(HConstants.CATALOG_FAMILY)
132               .get(HConstants.SERVER_QUALIFIER)));
133     }
134     return 0;
135   }
136 
137   @Override
138   protected void addOptions() {
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     numRegionServers = Integer.parseInt(cmd.getOptionValue(OPT_NUM_RS,
147         String.valueOf(DEFAULT_NUM_RS)));
148   }
149 
150   public static void main(String[] args) {
151     new RestartMetaTest().doStaticMain(args);
152   }
153 
154 }