HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / RegionReplicationLagEvaluation.java
blob6a54adcaced30e26a94dfa8ad102576249ec050a
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase;
20 import com.google.errorprone.annotations.RestrictedApi;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.concurrent.ThreadLocalRandom;
24 import java.util.concurrent.TimeUnit;
25 import org.apache.hadoop.conf.Configured;
26 import org.apache.hadoop.hbase.client.Admin;
27 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
28 import org.apache.hadoop.hbase.client.Connection;
29 import org.apache.hadoop.hbase.client.ConnectionFactory;
30 import org.apache.hadoop.hbase.client.ConnectionUtils;
31 import org.apache.hadoop.hbase.client.Consistency;
32 import org.apache.hadoop.hbase.client.Get;
33 import org.apache.hadoop.hbase.client.Put;
34 import org.apache.hadoop.hbase.client.Result;
35 import org.apache.hadoop.hbase.client.Table;
36 import org.apache.hadoop.hbase.client.TableDescriptor;
37 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
38 import org.apache.hadoop.hbase.metrics.impl.FastLongHistogram;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.apache.hadoop.hbase.util.Threads;
41 import org.apache.hadoop.util.Tool;
42 import org.apache.hadoop.util.ToolRunner;
43 import org.apache.yetus.audience.InterfaceAudience;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
48 import org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser;
49 import org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter;
50 import org.apache.hbase.thirdparty.org.apache.commons.cli.Options;
52 /**
53 * A tool to evaluating the lag between primary replica and secondary replica.
54 * <p/>
55 * It simply adds a row to the primary replica, and then check how long before we can read it from
56 * the secondary replica.
58 @InterfaceAudience.Private
59 public class RegionReplicationLagEvaluation extends Configured implements Tool {
61 private static final Logger LOG = LoggerFactory.getLogger(RegionReplicationLagEvaluation.class);
63 public static final String TABLE_NAME = "TestLagTable";
65 public static final String FAMILY_NAME = "info";
67 public static final String QUALIFIER_NAME = "qual";
69 public static final int VALUE_LENGTH = 256;
71 public static final int ROW_LENGTH = 16;
73 private static final Options OPTIONS = new Options().addOption("t", "table", true, "Table name")
74 .addOption("rlen", "rlength", true, "The length of row key")
75 .addOption("vlen", "vlength", true, "The length of value")
76 .addRequiredOption("r", "rows", true, "Number of rows to test");
78 private FastLongHistogram histogram = new FastLongHistogram();
80 @RestrictedApi(explanation = "Should only be called in tests", link = "",
81 allowedOnPath = ".*/src/test/.*")
82 FastLongHistogram getHistogram() {
83 return histogram;
86 @Override
87 public int run(String[] args) throws Exception {
88 TableName tableName;
89 int rlen;
90 int vlen;
91 int rows;
92 try {
93 CommandLine cli = new DefaultParser().parse(OPTIONS, args);
94 tableName = TableName.valueOf(cli.getOptionValue("t", TABLE_NAME));
95 rlen = Integer.parseInt(cli.getOptionValue("rlen", String.valueOf(ROW_LENGTH)));
96 vlen = Integer.parseInt(cli.getOptionValue("vlen", String.valueOf(VALUE_LENGTH)));
97 rows = Integer.parseInt(cli.getOptionValue("r"));
98 } catch (Exception e) {
99 LOG.warn("Error parsing command line options", e);
100 HelpFormatter formatter = new HelpFormatter();
101 formatter.printHelp(getClass().getName(), OPTIONS);
102 return -1;
104 exec(tableName, rlen, vlen, rows);
105 return 0;
108 private void createTable(Admin admin, TableName tableName) throws IOException {
109 TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName)
110 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_NAME)).setRegionReplication(2)
111 .build();
112 admin.createTable(td);
115 private void checkLag(Table table, int rlen, int vlen, int rows) throws IOException {
116 ThreadLocalRandom rand = ThreadLocalRandom.current();
117 byte[] family = Bytes.toBytes(FAMILY_NAME);
118 byte[] qualifier = Bytes.toBytes(QUALIFIER_NAME);
119 byte[] row = new byte[rlen];
120 byte[] value = new byte[vlen];
121 LOG.info("Test replication lag on table {} with {} rows", table.getName(), rows);
122 for (int i = 0; i < rows; i++) {
123 rand.nextBytes(row);
124 rand.nextBytes(value);
125 table.put(new Put(row).addColumn(family, qualifier, value));
126 // get from secondary replica
127 Get get = new Get(row).setConsistency(Consistency.TIMELINE).setReplicaId(1);
128 long startNs = System.nanoTime();
129 for (int retry = 0;; retry++) {
130 Result result = table.get(get);
131 byte[] gotValue = result.getValue(family, qualifier);
132 if (Arrays.equals(value, gotValue)) {
133 break;
135 long pauseTimeMs = Math.min(ConnectionUtils.getPauseTime(1, retry), 1000);
136 Threads.sleepWithoutInterrupt(pauseTimeMs);
138 long lagMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
139 histogram.add(lagMs, 1);
141 LOG.info("Test finished, min lag {} ms, max lag {} ms, mean lag {} ms", histogram.getMin(),
142 histogram.getMax(), histogram.getMean());
143 long[] q = histogram.getQuantiles(FastLongHistogram.DEFAULT_QUANTILES);
144 for (int i = 0; i < q.length; i++) {
145 LOG.info("{}% lag: {} ms", FastLongHistogram.DEFAULT_QUANTILES[i] * 100, q[i]);
149 private void exec(TableName tableName, int rlen, int vlen, int rows) throws IOException {
150 try (Connection conn = ConnectionFactory.createConnection(getConf())) {
151 try (Admin admin = conn.getAdmin()) {
152 if (!admin.tableExists(tableName)) {
153 createTable(admin, tableName);
156 try (Table table = conn.getTable(tableName)) {
157 checkLag(table, rlen, vlen, rows);
162 public static void main(String[] args) throws Exception {
163 int res =
164 ToolRunner.run(HBaseConfiguration.create(), new RegionReplicationLagEvaluation(), args);
165 System.exit(res);