1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
41
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;
47 private static final long DEFAULT_RUN_TIME = 20 * 60 * 1000;
48 private static final long JUNIT_RUN_TIME = 10 * 60 * 1000;
49
50
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 LoadTestTool.OPT_REGION_REPLICATION,
74 };
75
76 @Override
77 public void setUpCluster() throws Exception {
78 util = getTestingUtil(getConf());
79 LOG.debug("Initializing/checking cluster has " + SERVER_COUNT + " servers");
80 util.initializeCluster(SERVER_COUNT);
81 LOG.debug("Done initializing/checking cluster");
82 cluster = util.getHBaseClusterInterface();
83 deleteTableIfNecessary();
84 loadTool = new LoadTestTool();
85 loadTool.setConf(util.getConfiguration());
86
87
88 initTable();
89 }
90
91 protected void initTable() throws IOException {
92 int ret = loadTool.run(getArgsForLoadTestToolInitTable());
93 Assert.assertEquals("Failed to initialize LoadTestTool", 0, ret);
94 }
95
96 @Override
97 public int runTestFromCommandLine() throws Exception {
98 internalRunIngestTest(DEFAULT_RUN_TIME);
99 return 0;
100 }
101
102 @Test
103 public void testIngest() throws Exception {
104 runIngestTest(JUNIT_RUN_TIME, 2500, 10, 1024, 10, 20);
105 }
106
107 protected void internalRunIngestTest(long runTime) throws Exception {
108 String clazz = this.getClass().getSimpleName();
109 long numKeysPerServer = conf.getLong(String.format("%s.%s", clazz, NUM_KEYS_PER_SERVER_KEY),
110 DEFAULT_NUM_KEYS_PER_SERVER);
111 int numWriteThreads = conf.getInt(
112 String.format("%s.%s", clazz, NUM_WRITE_THREADS_KEY), DEFAULT_NUM_WRITE_THREADS);
113 int numReadThreads = conf.getInt(
114 String.format("%s.%s", clazz, NUM_READ_THREADS_KEY), DEFAULT_NUM_READ_THREADS);
115 runIngestTest(runTime, numKeysPerServer, 10, 1024, numWriteThreads, numReadThreads);
116 }
117
118 @Override
119 public TableName getTablename() {
120 String clazz = this.getClass().getSimpleName();
121 return TableName.valueOf(
122 conf.get(String.format("%s.%s", clazz, LoadTestTool.OPT_TABLE_NAME), clazz));
123 }
124
125 @Override
126 protected Set<String> getColumnFamilies() {
127 return Sets.newHashSet(Bytes.toString(LoadTestTool.COLUMN_FAMILY));
128 }
129
130 private void deleteTableIfNecessary() throws IOException {
131 if (util.getHBaseAdmin().tableExists(getTablename())) {
132 util.deleteTable(getTablename());
133 }
134 }
135
136 protected void runIngestTest(long defaultRunTime, long keysPerServerPerIter, int colsPerKey,
137 int recordSize, int writeThreads, int readThreads) throws Exception {
138
139 LOG.info("Running ingest");
140 LOG.info("Cluster size:" + util.getHBaseClusterInterface().getClusterStatus().getServersSize());
141
142 long start = System.currentTimeMillis();
143 String runtimeKey = String.format(RUN_TIME_KEY, this.getClass().getSimpleName());
144 long runtime = util.getConfiguration().getLong(runtimeKey, defaultRunTime);
145 long startKey = 0;
146
147 long numKeys = getNumKeys(keysPerServerPerIter);
148 while (System.currentTimeMillis() - start < 0.9 * runtime) {
149 LOG.info("Intended run time: " + (runtime/60000) + " min, left:" +
150 ((runtime - (System.currentTimeMillis() - start))/60000) + " min");
151
152 int ret = -1;
153 ret = loadTool.run(getArgsForLoadTestTool("-write",
154 String.format("%d:%d:%d", colsPerKey, recordSize, writeThreads), startKey, numKeys));
155 if (0 != ret) {
156 String errorMsg = "Load failed with error code " + ret;
157 LOG.error(errorMsg);
158 Assert.fail(errorMsg);
159 }
160
161 ret = loadTool.run(getArgsForLoadTestTool("-update", String.format("60:%d:1", writeThreads),
162 startKey, numKeys));
163 if (0 != ret) {
164 String errorMsg = "Update failed with error code " + ret;
165 LOG.error(errorMsg);
166 Assert.fail(errorMsg);
167 }
168
169 ret = loadTool.run(getArgsForLoadTestTool("-read", String.format("100:%d", readThreads)
170 , startKey, numKeys));
171 if (0 != ret) {
172 String errorMsg = "Verification failed with error code " + ret;
173 LOG.error(errorMsg);
174 Assert.fail(errorMsg);
175 }
176 startKey += numKeys;
177 }
178 }
179
180 protected String[] getArgsForLoadTestToolInitTable() {
181 List<String> args = new ArrayList<String>();
182 args.add("-tn");
183 args.add(getTablename().getNameAsString());
184
185 String clazz = this.getClass().getSimpleName();
186 for (String arg : LOAD_TEST_TOOL_INIT_ARGS) {
187 String val = conf.get(String.format("%s.%s", clazz, arg));
188 if (val != null) {
189 args.add("-" + arg);
190 args.add(val);
191 }
192 }
193 args.add("-init_only");
194 return args.toArray(new String[args.size()]);
195 }
196
197 protected String[] getArgsForLoadTestTool(String mode, String modeSpecificArg, long startKey,
198 long numKeys) {
199 List<String> args = new ArrayList<String>();
200 args.add("-tn");
201 args.add(getTablename().getNameAsString());
202 args.add(mode);
203 args.add(modeSpecificArg);
204 args.add("-start_key");
205 args.add(String.valueOf(startKey));
206 args.add("-num_keys");
207 args.add(String.valueOf(numKeys));
208 args.add("-skip_init");
209
210 return args.toArray(new String[args.size()]);
211 }
212
213
214 protected long getNumKeys(long keysPerServer)
215 throws IOException {
216 int numRegionServers = cluster.getClusterStatus().getServersSize();
217 return keysPerServer * numRegionServers;
218 }
219
220 public static void main(String[] args) throws Exception {
221 Configuration conf = HBaseConfiguration.create();
222 IntegrationTestingUtility.setUseDistributedCluster(conf);
223 int ret = ToolRunner.run(conf, new IntegrationTestIngest(), args);
224 System.exit(ret);
225 }
226 }