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