1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.wal;
20
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Random;
27 import java.util.Set;
28 import java.util.concurrent.TimeUnit;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.hbase.classification.InterfaceAudience;
33 import org.apache.hadoop.conf.Configuration;
34 import org.apache.hadoop.conf.Configured;
35 import org.apache.hadoop.fs.FileStatus;
36 import org.apache.hadoop.fs.FileSystem;
37 import org.apache.hadoop.fs.Path;
38 import org.apache.hadoop.hbase.Cell;
39 import org.apache.hadoop.hbase.HBaseConfiguration;
40 import org.apache.hadoop.hbase.HBaseTestingUtility;
41 import org.apache.hadoop.hbase.HColumnDescriptor;
42 import org.apache.hadoop.hbase.HConstants;
43 import org.apache.hadoop.hbase.HRegionInfo;
44 import org.apache.hadoop.hbase.HTableDescriptor;
45 import org.apache.hadoop.hbase.MockRegionServerServices;
46 import org.apache.hadoop.hbase.TableName;
47 import org.apache.hadoop.hbase.client.Put;
48 import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
49 import org.apache.hadoop.hbase.regionserver.HRegion;
50 import org.apache.hadoop.hbase.regionserver.LogRoller;
51 import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl;
52 import org.apache.hadoop.hbase.trace.HBaseHTraceConfiguration;
53 import org.apache.hadoop.hbase.trace.SpanReceiverHost;
54 import org.apache.hadoop.hbase.wal.WALProvider.Writer;
55 import org.apache.hadoop.hbase.util.Bytes;
56 import org.apache.hadoop.hbase.util.FSUtils;
57 import org.apache.hadoop.hbase.util.Threads;
58 import org.apache.hadoop.util.Tool;
59 import org.apache.hadoop.util.ToolRunner;
60 import org.apache.htrace.Sampler;
61 import org.apache.htrace.Trace;
62 import org.apache.htrace.TraceScope;
63 import org.apache.htrace.impl.ProbabilitySampler;
64
65 import com.yammer.metrics.core.Histogram;
66 import com.yammer.metrics.core.Meter;
67 import com.yammer.metrics.core.MetricsRegistry;
68 import com.yammer.metrics.reporting.ConsoleReporter;
69
70
71 import org.apache.hadoop.hbase.regionserver.wal.SecureProtobufLogReader;
72 import org.apache.hadoop.hbase.regionserver.wal.SecureProtobufLogWriter;
73 import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
74 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
75
76
77
78
79
80
81 @InterfaceAudience.Private
82 public final class WALPerformanceEvaluation extends Configured implements Tool {
83 private static final Log LOG = LogFactory.getLog(WALPerformanceEvaluation.class.getName());
84 private final MetricsRegistry metrics = new MetricsRegistry();
85 private final Meter syncMeter =
86 metrics.newMeter(WALPerformanceEvaluation.class, "syncMeter", "syncs", TimeUnit.MILLISECONDS);
87 private final Histogram syncHistogram =
88 metrics.newHistogram(WALPerformanceEvaluation.class, "syncHistogram", "nanos-between-syncs",
89 true);
90 private final Histogram syncCountHistogram =
91 metrics.newHistogram(WALPerformanceEvaluation.class, "syncCountHistogram", "countPerSync",
92 true);
93 private final Meter appendMeter =
94 metrics.newMeter(WALPerformanceEvaluation.class, "appendMeter", "bytes",
95 TimeUnit.MILLISECONDS);
96 private final Histogram latencyHistogram =
97 metrics.newHistogram(WALPerformanceEvaluation.class, "latencyHistogram", "nanos", true);
98
99 private final MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl();
100
101 private HBaseTestingUtility TEST_UTIL;
102
103 static final String TABLE_NAME = "WALPerformanceEvaluation";
104 static final String QUALIFIER_PREFIX = "q";
105 static final String FAMILY_PREFIX = "cf";
106
107 private int numQualifiers = 1;
108 private int valueSize = 512;
109 private int keySize = 16;
110
111 @Override
112 public void setConf(Configuration conf) {
113 super.setConf(conf);
114 TEST_UTIL = new HBaseTestingUtility(conf);
115 }
116
117
118
119
120
121
122 class WALPutBenchmark implements Runnable {
123 private final long numIterations;
124 private final int numFamilies;
125 private final boolean noSync;
126 private final HRegion region;
127 private final int syncInterval;
128 private final HTableDescriptor htd;
129 private final Sampler loopSampler;
130
131 WALPutBenchmark(final HRegion region, final HTableDescriptor htd,
132 final long numIterations, final boolean noSync, final int syncInterval,
133 final double traceFreq) {
134 this.numIterations = numIterations;
135 this.noSync = noSync;
136 this.syncInterval = syncInterval;
137 this.numFamilies = htd.getColumnFamilies().length;
138 this.region = region;
139 this.htd = htd;
140 String spanReceivers = getConf().get("hbase.trace.spanreceiver.classes");
141 if (spanReceivers == null || spanReceivers.isEmpty()) {
142 loopSampler = Sampler.NEVER;
143 } else {
144 if (traceFreq <= 0.0) {
145 LOG.warn("Tracing enabled but traceFreq=0.");
146 loopSampler = Sampler.NEVER;
147 } else if (traceFreq >= 1.0) {
148 loopSampler = Sampler.ALWAYS;
149 if (numIterations > 1000) {
150 LOG.warn("Full tracing of all iterations will produce a lot of data. Be sure your"
151 + " SpanReceiver can keep up.");
152 }
153 } else {
154 getConf().setDouble("hbase.sampler.fraction", traceFreq);
155 loopSampler = new ProbabilitySampler(new HBaseHTraceConfiguration(getConf()));
156 }
157 }
158 }
159
160 @Override
161 public void run() {
162 byte[] key = new byte[keySize];
163 byte[] value = new byte[valueSize];
164 Random rand = new Random(Thread.currentThread().getId());
165 WAL wal = region.getWAL();
166
167 TraceScope threadScope =
168 Trace.startSpan("WALPerfEval." + Thread.currentThread().getName());
169 try {
170 long startTime = System.currentTimeMillis();
171 int lastSync = 0;
172 for (int i = 0; i < numIterations; ++i) {
173 assert Trace.currentSpan() == threadScope.getSpan() : "Span leak detected.";
174 TraceScope loopScope = Trace.startSpan("runLoopIter" + i, loopSampler);
175 try {
176 long now = System.nanoTime();
177 Put put = setupPut(rand, key, value, numFamilies);
178 WALEdit walEdit = new WALEdit();
179 addFamilyMapToWALEdit(put.getFamilyCellMap(), walEdit);
180 HRegionInfo hri = region.getRegionInfo();
181 final WALKey logkey =
182 new WALKey(hri.getEncodedNameAsBytes(), hri.getTable(), now, mvcc);
183 wal.append(htd, hri, logkey, walEdit, true);
184 if (!this.noSync) {
185 if (++lastSync >= this.syncInterval) {
186 wal.sync();
187 lastSync = 0;
188 }
189 }
190 latencyHistogram.update(System.nanoTime() - now);
191 } finally {
192 loopScope.close();
193 }
194 }
195 long totalTime = (System.currentTimeMillis() - startTime);
196 logBenchmarkResult(Thread.currentThread().getName(), numIterations, totalTime);
197 } catch (Exception e) {
198 LOG.error(getClass().getSimpleName() + " Thread failed", e);
199 } finally {
200 threadScope.close();
201 }
202 }
203 }
204
205 @Override
206 public int run(String[] args) throws Exception {
207 Path rootRegionDir = null;
208 int numThreads = 1;
209 long numIterations = 1000000;
210 int numFamilies = 1;
211 int syncInterval = 0;
212 boolean noSync = false;
213 boolean verify = false;
214 boolean verbose = false;
215 boolean cleanup = true;
216 boolean noclosefs = false;
217 long roll = Long.MAX_VALUE;
218 boolean compress = false;
219 String cipher = null;
220 int numRegions = 1;
221 String spanReceivers = getConf().get("hbase.trace.spanreceiver.classes");
222 boolean trace = spanReceivers != null && !spanReceivers.isEmpty();
223 double traceFreq = 1.0;
224
225 for (int i = 0; i < args.length; i++) {
226 String cmd = args[i];
227 if (cmd.equals("-threads")) {
228 numThreads = Integer.parseInt(args[++i]);
229 } else if (cmd.equals("-iterations")) {
230 numIterations = Long.parseLong(args[++i]);
231 } else if (cmd.equals("-path")) {
232 rootRegionDir = new Path(args[++i]);
233 } else if (cmd.equals("-families")) {
234 numFamilies = Integer.parseInt(args[++i]);
235 } else if (cmd.equals("-qualifiers")) {
236 numQualifiers = Integer.parseInt(args[++i]);
237 } else if (cmd.equals("-keySize")) {
238 keySize = Integer.parseInt(args[++i]);
239 } else if (cmd.equals("-valueSize")) {
240 valueSize = Integer.parseInt(args[++i]);
241 } else if (cmd.equals("-syncInterval")) {
242 syncInterval = Integer.parseInt(args[++i]);
243 } else if (cmd.equals("-nosync")) {
244 noSync = true;
245 } else if (cmd.equals("-verify")) {
246 verify = true;
247 } else if (cmd.equals("-verbose")) {
248 verbose = true;
249 } else if (cmd.equals("-nocleanup")) {
250 cleanup = false;
251 } else if (cmd.equals("-noclosefs")) {
252 noclosefs = true;
253 } else if (cmd.equals("-roll")) {
254 roll = Long.parseLong(args[++i]);
255 } else if (cmd.equals("-compress")) {
256 compress = true;
257 } else if (cmd.equals("-encryption")) {
258 cipher = args[++i];
259 } else if (cmd.equals("-regions")) {
260 numRegions = Integer.parseInt(args[++i]);
261 } else if (cmd.equals("-traceFreq")) {
262 traceFreq = Double.parseDouble(args[++i]);
263 } else if (cmd.equals("-h")) {
264 printUsageAndExit();
265 } else if (cmd.equals("--help")) {
266 printUsageAndExit();
267 } else {
268 System.err.println("UNEXPECTED: " + cmd);
269 printUsageAndExit();
270 }
271 }
272
273 if (compress) {
274 Configuration conf = getConf();
275 conf.setBoolean(HConstants.ENABLE_WAL_COMPRESSION, true);
276 }
277
278 if (cipher != null) {
279
280 Configuration conf = getConf();
281 conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
282 conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase");
283 conf.setClass("hbase.regionserver.hlog.reader.impl", SecureProtobufLogReader.class,
284 WAL.Reader.class);
285 conf.setClass("hbase.regionserver.hlog.writer.impl", SecureProtobufLogWriter.class,
286 Writer.class);
287 conf.setBoolean(HConstants.ENABLE_WAL_ENCRYPTION, true);
288 conf.set(HConstants.CRYPTO_WAL_ALGORITHM_CONF_KEY, cipher);
289 }
290
291 if (numThreads < numRegions) {
292 LOG.warn("Number of threads is less than the number of regions; some regions will sit idle.");
293 }
294
295
296
297 getConf().setInt(HConstants.REGION_SERVER_HANDLER_COUNT, numThreads);
298
299
300
301 FSUtils.setFsDefault(getConf(), FSUtils.getRootDir(getConf()));
302 FileSystem fs = FileSystem.get(getConf());
303 LOG.info("FileSystem: " + fs);
304
305 SpanReceiverHost receiverHost = trace ? SpanReceiverHost.getInstance(getConf()) : null;
306 final Sampler<?> sampler = trace ? Sampler.ALWAYS : Sampler.NEVER;
307 TraceScope scope = Trace.startSpan("WALPerfEval", sampler);
308
309 try {
310 if (rootRegionDir == null) {
311 rootRegionDir = TEST_UTIL.getDataTestDirOnTestFS("WALPerformanceEvaluation");
312 }
313 rootRegionDir = rootRegionDir.makeQualified(fs);
314 cleanRegionRootDir(fs, rootRegionDir);
315 FSUtils.setRootDir(getConf(), rootRegionDir);
316 final WALFactory wals = new WALFactory(getConf(), null, "wals");
317 final HRegion[] regions = new HRegion[numRegions];
318 final Runnable[] benchmarks = new Runnable[numRegions];
319 final MockRegionServerServices mockServices = new MockRegionServerServices(getConf());
320 final LogRoller roller = new LogRoller(mockServices, mockServices);
321 Threads.setDaemonThreadRunning(roller.getThread(), "WALPerfEval.logRoller");
322
323 try {
324 for(int i = 0; i < numRegions; i++) {
325
326
327 final HTableDescriptor htd = createHTableDescriptor(i, numFamilies);
328 regions[i] = openRegion(fs, rootRegionDir, htd, wals, roll, roller);
329 benchmarks[i] = Trace.wrap(new WALPutBenchmark(regions[i], htd, numIterations, noSync,
330 syncInterval, traceFreq));
331 }
332 ConsoleReporter.enable(this.metrics, 30, TimeUnit.SECONDS);
333 long putTime = runBenchmark(benchmarks, numThreads);
334 logBenchmarkResult("Summary: threads=" + numThreads + ", iterations=" + numIterations +
335 ", syncInterval=" + syncInterval, numIterations * numThreads, putTime);
336
337 for (int i = 0; i < numRegions; i++) {
338 if (regions[i] != null) {
339 closeRegion(regions[i]);
340 regions[i] = null;
341 }
342 }
343 if (verify) {
344 LOG.info("verifying written log entries.");
345 Path dir = new Path(FSUtils.getWALRootDir(getConf()),
346 DefaultWALProvider.getWALDirectoryName("wals"));
347 long editCount = 0;
348 FileStatus [] fsss = fs.listStatus(dir);
349 if (fsss.length == 0) throw new IllegalStateException("No WAL found");
350 for (FileStatus fss: fsss) {
351 Path p = fss.getPath();
352 if (!fs.exists(p)) throw new IllegalStateException(p.toString());
353 editCount += verify(wals, p, verbose);
354 }
355 long expected = numIterations * numThreads;
356 if (editCount != expected) {
357 throw new IllegalStateException("Counted=" + editCount + ", expected=" + expected);
358 }
359 }
360 } finally {
361 mockServices.stop("test clean up.");
362 for (int i = 0; i < numRegions; i++) {
363 if (regions[i] != null) {
364 closeRegion(regions[i]);
365 }
366 }
367 if (null != roller) {
368 LOG.info("shutting down log roller.");
369 Threads.shutdown(roller.getThread());
370 }
371 wals.shutdown();
372
373 if (cleanup) cleanRegionRootDir(fs, rootRegionDir);
374 }
375 } finally {
376
377 if (!noclosefs) fs.close();
378 scope.close();
379 if (receiverHost != null) receiverHost.closeReceivers();
380 }
381
382 return(0);
383 }
384
385 private static HTableDescriptor createHTableDescriptor(final int regionNum,
386 final int numFamilies) {
387 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TABLE_NAME + ":" + regionNum));
388 for (int i = 0; i < numFamilies; ++i) {
389 HColumnDescriptor colDef = new HColumnDescriptor(FAMILY_PREFIX + i);
390 htd.addFamily(colDef);
391 }
392 return htd;
393 }
394
395
396
397
398
399
400
401
402
403 private long verify(final WALFactory wals, final Path wal, final boolean verbose)
404 throws IOException {
405 WAL.Reader reader = wals.createReader(wal.getFileSystem(getConf()), wal);
406 long count = 0;
407 Map<String, Long> sequenceIds = new HashMap<String, Long>();
408 try {
409 while (true) {
410 WAL.Entry e = reader.next();
411 if (e == null) {
412 LOG.debug("Read count=" + count + " from " + wal);
413 break;
414 }
415 count++;
416 long seqid = e.getKey().getLogSeqNum();
417 if (sequenceIds.containsKey(Bytes.toString(e.getKey().getEncodedRegionName()))) {
418
419 if (sequenceIds.get(Bytes.toString(e.getKey().getEncodedRegionName())) >= seqid) {
420 throw new IllegalStateException("wal = " + wal.getName() + ", " + "previous seqid = "
421 + sequenceIds.get(Bytes.toString(e.getKey().getEncodedRegionName()))
422 + ", current seqid = " + seqid);
423 }
424 }
425
426 sequenceIds.put(Bytes.toString(e.getKey().getEncodedRegionName()), seqid);
427 if (verbose) LOG.info("seqid=" + seqid);
428 }
429 } finally {
430 reader.close();
431 }
432 return count;
433 }
434
435 private static void logBenchmarkResult(String testName, long numTests, long totalTime) {
436 float tsec = totalTime / 1000.0f;
437 LOG.info(String.format("%s took %.3fs %.3fops/s", testName, tsec, numTests / tsec));
438
439 }
440
441 private void printUsageAndExit() {
442 System.err.printf("Usage: bin/hbase %s [options]\n", getClass().getName());
443 System.err.println(" where [options] are:");
444 System.err.println(" -h|-help Show this help and exit.");
445 System.err.println(" -threads <N> Number of threads writing on the WAL.");
446 System.err.println(" -regions <N> Number of regions to open in the WAL. Default: 1");
447 System.err.println(" -iterations <N> Number of iterations per thread.");
448 System.err.println(" -path <PATH> Path where region's root directory is created.");
449 System.err.println(" -families <N> Number of column families to write.");
450 System.err.println(" -qualifiers <N> Number of qualifiers to write.");
451 System.err.println(" -keySize <N> Row key size in byte.");
452 System.err.println(" -valueSize <N> Row/Col value size in byte.");
453 System.err.println(" -nocleanup Do NOT remove test data when done.");
454 System.err.println(" -noclosefs Do NOT close the filesystem when done.");
455 System.err.println(" -nosync Append without syncing");
456 System.err.println(" -syncInterval <N> Append N edits and then sync. " +
457 "Default=0, i.e. sync every edit.");
458 System.err.println(" -verify Verify edits written in sequence");
459 System.err.println(" -verbose Output extra info; " +
460 "e.g. all edit seq ids when verifying");
461 System.err.println(" -roll <N> Roll the way every N appends");
462 System.err.println(" -encryption <A> Encrypt the WAL with algorithm A, e.g. AES");
463 System.err.println(" -traceFreq <N> Rate of trace sampling. Default: 1.0, " +
464 "only respected when tracing is enabled, ie -Dhbase.trace.spanreceiver.classes=...");
465 System.err.println("");
466 System.err.println("Examples:");
467 System.err.println("");
468 System.err.println(" To run 100 threads on hdfs with log rolling every 10k edits and " +
469 "verification afterward do:");
470 System.err.println(" $ ./bin/hbase org.apache.hadoop.hbase.wal." +
471 "WALPerformanceEvaluation \\");
472 System.err.println(" -conf ./core-site.xml -path hdfs://example.org:7000/tmp " +
473 "-threads 100 -roll 10000 -verify");
474 System.exit(1);
475 }
476
477 private final Set<WAL> walsListenedTo = new HashSet<WAL>();
478
479 private HRegion openRegion(final FileSystem fs, final Path dir, final HTableDescriptor htd,
480 final WALFactory wals, final long whenToRoll, final LogRoller roller) throws IOException {
481
482 HRegionInfo regionInfo = new HRegionInfo(htd.getTableName());
483
484 final WAL wal =
485 wals.getWAL(regionInfo.getEncodedNameAsBytes(), regionInfo.getTable().getNamespace());
486
487 if (walsListenedTo.add(wal)) {
488 roller.addWAL(wal);
489 wal.registerWALActionsListener(new WALActionsListener.Base() {
490 private int appends = 0;
491
492 @Override
493 public void visitLogEntryBeforeWrite(HTableDescriptor htd, WALKey logKey,
494 WALEdit logEdit) {
495 this.appends++;
496 if (this.appends % whenToRoll == 0) {
497 LOG.info("Rolling after " + appends + " edits");
498
499
500
501 DefaultWALProvider.requestLogRoll(wal);
502 }
503 }
504
505 @Override
506 public void postSync(final long timeInNanos, final int handlerSyncs) {
507 syncMeter.mark();
508 syncHistogram.update(timeInNanos);
509 syncCountHistogram.update(handlerSyncs);
510 }
511
512 @Override
513 public void postAppend(final long size, final long elapsedTime, final WALKey logkey,
514 final WALEdit logEdit) {
515 appendMeter.mark(size);
516 }
517 });
518 }
519
520 return HRegion.createHRegion(regionInfo, dir, getConf(), htd, wal);
521 }
522
523 private void closeRegion(final HRegion region) throws IOException {
524 if (region != null) {
525 region.close();
526 WAL wal = region.getWAL();
527 if (wal != null) {
528 wal.shutdown();
529 }
530 }
531 }
532
533 private void cleanRegionRootDir(final FileSystem fs, final Path dir) throws IOException {
534 if (fs.exists(dir)) {
535 fs.delete(dir, true);
536 }
537 }
538
539 private Put setupPut(Random rand, byte[] key, byte[] value, final int numFamilies) {
540 rand.nextBytes(key);
541 Put put = new Put(key);
542 for (int cf = 0; cf < numFamilies; ++cf) {
543 for (int q = 0; q < numQualifiers; ++q) {
544 rand.nextBytes(value);
545 put.add(Bytes.toBytes(FAMILY_PREFIX + cf), Bytes.toBytes(QUALIFIER_PREFIX + q), value);
546 }
547 }
548 return put;
549 }
550
551 private void addFamilyMapToWALEdit(Map<byte[], List<Cell>> familyMap,
552 WALEdit walEdit) {
553 for (List<Cell> edits : familyMap.values()) {
554 for (Cell cell : edits) {
555 walEdit.add(cell);
556 }
557 }
558 }
559
560 private long runBenchmark(Runnable[] runnable, final int numThreads) throws InterruptedException {
561 Thread[] threads = new Thread[numThreads];
562 long startTime = System.currentTimeMillis();
563 for (int i = 0; i < numThreads; ++i) {
564 threads[i] = new Thread(runnable[i%runnable.length], "t" + i + ",r" + (i%runnable.length));
565 threads[i].start();
566 }
567 for (Thread t : threads) t.join();
568 long endTime = System.currentTimeMillis();
569 return(endTime - startTime);
570 }
571
572
573
574
575
576
577
578
579 static int innerMain(final Configuration c, final String [] args) throws Exception {
580 return ToolRunner.run(c, new WALPerformanceEvaluation(), args);
581 }
582
583 public static void main(String[] args) throws Exception {
584 System.exit(innerMain(HBaseConfiguration.create(), args));
585 }
586 }