View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.testclassification.IntegrationTests;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.hbase.util.LoadTestTool;
32  import org.apache.hadoop.util.ToolRunner;
33  import org.junit.Assert;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  import com.google.common.collect.Sets;
38  
39  /**
40   * A base class for tests that do something with the cluster while running
41   * {@link LoadTestTool} to write and verify some data.
42   */
43  @Category(IntegrationTests.class)
44  public class IntegrationTestIngest extends IntegrationTestBase {
45    public static final char HIPHEN = '-';
46    private static final int SERVER_COUNT = 1; // number of slaves for the smallest cluster
47    private static final long DEFAULT_RUN_TIME = 20 * 60 * 1000;
48    private static final long JUNIT_RUN_TIME = 10 * 60 * 1000;
49  
50    /** A soft limit on how long we should run */
51    protected static final String RUN_TIME_KEY = "hbase.%s.runtime";
52  
53    protected static final String NUM_KEYS_PER_SERVER_KEY = "num_keys_per_server";
54    protected static final long DEFAULT_NUM_KEYS_PER_SERVER = 2500;
55  
56    protected static final String NUM_WRITE_THREADS_KEY = "num_write_threads";
57    protected static final int DEFAULT_NUM_WRITE_THREADS = 20;
58  
59    protected static final String NUM_READ_THREADS_KEY = "num_read_threads";
60    protected static final int DEFAULT_NUM_READ_THREADS = 20;
61  
62    protected static final Log LOG = LogFactory.getLog(IntegrationTestIngest.class);
63    protected IntegrationTestingUtility util;
64    protected HBaseCluster cluster;
65    protected LoadTestTool loadTool;
66  
67    protected String[] LOAD_TEST_TOOL_INIT_ARGS = {
68        LoadTestTool.OPT_COMPRESSION,
69        LoadTestTool.OPT_DATA_BLOCK_ENCODING,
70        LoadTestTool.OPT_INMEMORY,
71        LoadTestTool.OPT_ENCRYPTION,
72        LoadTestTool.OPT_NUM_REGIONS_PER_SERVER,
73    };
74  
75    @Override
76    public void setUpCluster() throws Exception {
77      util = getTestingUtil(getConf());
78      LOG.debug("Initializing/checking cluster has " + SERVER_COUNT + " servers");
79      util.initializeCluster(SERVER_COUNT);
80      LOG.debug("Done initializing/checking cluster");
81      cluster = util.getHBaseClusterInterface();
82      deleteTableIfNecessary();
83      loadTool = new LoadTestTool();
84      loadTool.setConf(util.getConfiguration());
85      // Initialize load test tool before we start breaking things;
86      // LoadTestTool init, even when it is a no-op, is very fragile.
87      initTable();
88    }
89  
90    protected void initTable() throws IOException {
91      int ret = loadTool.run(getArgsForLoadTestToolInitTable());
92      Assert.assertEquals("Failed to initialize LoadTestTool", 0, ret);
93    }
94  
95    @Override
96    public int runTestFromCommandLine() throws Exception {
97      internalRunIngestTest(DEFAULT_RUN_TIME);
98      return 0;
99    }
100 
101   @Test
102   public void testIngest() throws Exception {
103     runIngestTest(JUNIT_RUN_TIME, 2500, 10, 1024, 10, 20);
104   }
105 
106   protected void internalRunIngestTest(long runTime) throws Exception {
107     String clazz = this.getClass().getSimpleName();
108     long numKeysPerServer = conf.getLong(String.format("%s.%s", clazz, NUM_KEYS_PER_SERVER_KEY),
109       DEFAULT_NUM_KEYS_PER_SERVER);
110     int numWriteThreads = conf.getInt(
111       String.format("%s.%s", clazz, NUM_WRITE_THREADS_KEY), DEFAULT_NUM_WRITE_THREADS);
112     int numReadThreads = conf.getInt(
113       String.format("%s.%s", clazz, NUM_READ_THREADS_KEY), DEFAULT_NUM_READ_THREADS);
114     runIngestTest(runTime, numKeysPerServer, 10, 1024, numWriteThreads, numReadThreads);
115   }
116 
117   @Override
118   public String getTablename() {
119     String clazz = this.getClass().getSimpleName();
120     return conf.get(String.format("%s.%s", clazz, LoadTestTool.OPT_TABLE_NAME), clazz);
121   }
122 
123   @Override
124   protected Set<String> getColumnFamilies() {
125     return Sets.newHashSet(Bytes.toString(LoadTestTool.COLUMN_FAMILY));
126   }
127 
128   private void deleteTableIfNecessary() throws IOException {
129     if (util.getHBaseAdmin().tableExists(getTablename())) {
130       util.deleteTable(Bytes.toBytes(getTablename()));
131     }
132   }
133 
134   protected void runIngestTest(long defaultRunTime, long keysPerServerPerIter, int colsPerKey,
135       int recordSize, int writeThreads, int readThreads) throws Exception {
136 
137     LOG.info("Running ingest");
138     LOG.info("Cluster size:" + util.getHBaseClusterInterface().getClusterStatus().getServersSize());
139 
140     long start = System.currentTimeMillis();
141     String runtimeKey = String.format(RUN_TIME_KEY, this.getClass().getSimpleName());
142     long runtime = util.getConfiguration().getLong(runtimeKey, defaultRunTime);
143     long startKey = 0;
144 
145     long numKeys = getNumKeys(keysPerServerPerIter);
146     while (System.currentTimeMillis() - start < 0.9 * runtime) {
147       LOG.info("Intended run time: " + (runtime/60000) + " min, left:" +
148           ((runtime - (System.currentTimeMillis() - start))/60000) + " min");
149 
150       int ret = -1;
151       ret = loadTool.run(getArgsForLoadTestTool("-write",
152           String.format("%d:%d:%d", colsPerKey, recordSize, writeThreads), startKey, numKeys));
153       if (0 != ret) {
154         String errorMsg = "Load failed with error code " + ret;
155         LOG.error(errorMsg);
156         Assert.fail(errorMsg);
157       }
158 
159       ret = loadTool.run(getArgsForLoadTestTool("-update", String.format("60:%d:1", writeThreads),
160           startKey, numKeys));
161       if (0 != ret) {
162         String errorMsg = "Update failed with error code " + ret;
163         LOG.error(errorMsg);
164         Assert.fail(errorMsg);
165       }
166 
167       ret = loadTool.run(getArgsForLoadTestTool("-read", String.format("100:%d", readThreads)
168         , startKey, numKeys));
169       if (0 != ret) {
170         String errorMsg = "Verification failed with error code " + ret;
171         LOG.error(errorMsg);
172         Assert.fail(errorMsg);
173       }
174       startKey += numKeys;
175     }
176   }
177 
178   protected String[] getArgsForLoadTestToolInitTable() {
179     List<String> args = new ArrayList<String>();
180     args.add("-tn");
181     args.add(getTablename());
182     // pass all remaining args from conf with keys <test class name>.<load test tool arg>
183     String clazz = this.getClass().getSimpleName();
184     for (String arg : LOAD_TEST_TOOL_INIT_ARGS) {
185       String val = conf.get(String.format("%s.%s", clazz, arg));
186       if (val != null) {
187         args.add("-" + arg);
188         args.add(val);
189       }
190     }
191     args.add("-init_only");
192     return args.toArray(new String[args.size()]);
193   }
194 
195   protected String[] getArgsForLoadTestTool(String mode, String modeSpecificArg, long startKey,
196       long numKeys) {
197     List<String> args = new ArrayList<String>();
198     args.add("-tn");
199     args.add(getTablename());
200     args.add(mode);
201     args.add(modeSpecificArg);
202     args.add("-start_key");
203     args.add(String.valueOf(startKey));
204     args.add("-num_keys");
205     args.add(String.valueOf(numKeys));
206     args.add("-skip_init");
207 
208     return args.toArray(new String[args.size()]);
209   }
210 
211   /** Estimates a data size based on the cluster size */
212   protected long getNumKeys(long keysPerServer)
213       throws IOException {
214     int numRegionServers = cluster.getClusterStatus().getServersSize();
215     return keysPerServer * numRegionServers;
216   }
217 
218   public static void main(String[] args) throws Exception {
219     Configuration conf = HBaseConfiguration.create();
220     IntegrationTestingUtility.setUseDistributedCluster(conf);
221     int ret = ToolRunner.run(conf, new IntegrationTestIngest(), args);
222     System.exit(ret);
223   }
224 }