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