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.io.compress.Compression;
32  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
33  import org.apache.hadoop.hbase.util.test.LoadTestDataGenerator;
34  
35  /**
36   * A command-line tool that spins up a local process-based cluster, loads
37   * some data, restarts the regionserver holding hbase:meta, and verifies that the
38   * cluster recovers.
39   */
40  public class RestartMetaTest extends AbstractHBaseTool {
41  
42    private static final Log LOG = LogFactory.getLog(RestartMetaTest.class);
43  
44    /** The number of region servers used if not specified */
45    private static final int DEFAULT_NUM_RS = 2;
46  
47    /** Table name for the test */
48    private static TableName TABLE_NAME =
49        TableName.valueOf("load_test");
50  
51    /** The number of seconds to sleep after loading the data */
52    private static final int SLEEP_SEC_AFTER_DATA_LOAD = 5;
53  
54    /** The actual number of region servers */
55    private int numRegionServers;
56  
57    private static final String OPT_NUM_RS = "num_rs";
58  
59    private static final int NUM_DATANODES = 3;
60  
61    /** Loads data into the table using the multi-threaded writer. */
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      // print out the arguments
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      // start the writers
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, NUM_DATANODES, numRegionServers);
94      hbaseCluster.startMiniDFS();
95  
96      // start the process based HBase cluster
97      hbaseCluster.startHBase();
98  
99      // create tables if needed
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 hbase: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, TableName.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     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     numRegionServers = Integer.parseInt(cmd.getOptionValue(OPT_NUM_RS,
146         String.valueOf(DEFAULT_NUM_RS)));
147   }
148 
149   public static void main(String[] args) {
150     new RestartMetaTest().doStaticMain(args);
151   }
152 
153 }