001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.trace;
020
021import io.opentelemetry.api.common.AttributeKey;
022import io.opentelemetry.api.common.Attributes;
023import io.opentelemetry.api.trace.Span;
024import io.opentelemetry.context.Scope;
025import java.io.IOException;
026import java.util.Random;
027import java.util.concurrent.ExecutorService;
028import java.util.concurrent.Executors;
029import java.util.concurrent.LinkedBlockingQueue;
030import java.util.concurrent.TimeUnit;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.hbase.HBaseConfiguration;
033import org.apache.hadoop.hbase.IntegrationTestingUtility;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.client.Admin;
036import org.apache.hadoop.hbase.client.BufferedMutator;
037import org.apache.hadoop.hbase.client.Get;
038import org.apache.hadoop.hbase.client.Put;
039import org.apache.hadoop.hbase.client.Result;
040import org.apache.hadoop.hbase.client.ResultScanner;
041import org.apache.hadoop.hbase.client.Scan;
042import org.apache.hadoop.hbase.client.Table;
043import org.apache.hadoop.hbase.testclassification.IntegrationTests;
044import org.apache.hadoop.hbase.util.AbstractHBaseTool;
045import org.apache.hadoop.hbase.util.Bytes;
046import org.apache.hadoop.util.ToolRunner;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049
050import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
051
052@Category(IntegrationTests.class)
053public class IntegrationTestSendTraceRequests extends AbstractHBaseTool {
054
055  public static final String TABLE_ARG = "t";
056  public static final String CF_ARG = "f";
057
058  public static final String TABLE_NAME_DEFAULT = "SendTracesTable";
059  public static final String COLUMN_FAMILY_DEFAULT = "D";
060  private TableName tableName = TableName.valueOf(TABLE_NAME_DEFAULT);
061  private byte[] familyName = Bytes.toBytes(COLUMN_FAMILY_DEFAULT);
062  private IntegrationTestingUtility util;
063  private Random random = new Random();
064  private Admin admin;
065
066  public static void main(String[] args) throws Exception {
067    Configuration configuration = HBaseConfiguration.create();
068    IntegrationTestingUtility.setUseDistributedCluster(configuration);
069    IntegrationTestSendTraceRequests tool = new IntegrationTestSendTraceRequests();
070    ToolRunner.run(configuration, tool, args);
071  }
072
073  @Override
074  protected void addOptions() {
075    addOptWithArg(TABLE_ARG, "The table name to target.  Will be created if not there already.");
076    addOptWithArg(CF_ARG, "The family to target");
077  }
078
079  @Override
080  public void processOptions(CommandLine cmd) {
081    String tableNameString = cmd.getOptionValue(TABLE_ARG, TABLE_NAME_DEFAULT);
082    String familyString = cmd.getOptionValue(CF_ARG, COLUMN_FAMILY_DEFAULT);
083
084    this.tableName = TableName.valueOf(tableNameString);
085    this.familyName = Bytes.toBytes(familyString);
086  }
087
088  @Override
089  public int doWork() throws Exception {
090    internalDoWork();
091    return 0;
092  }
093
094  @Test
095  public void internalDoWork() throws Exception {
096    util = createUtil();
097    admin = util.getAdmin();
098
099    deleteTable();
100    createTable();
101    LinkedBlockingQueue<Long> rks = insertData();
102
103    ExecutorService service = Executors.newFixedThreadPool(20);
104    doScans(service, rks);
105    doGets(service, rks);
106
107    service.shutdown();
108    service.awaitTermination(100, TimeUnit.SECONDS);
109    Thread.sleep(90000);
110    util.restoreCluster();
111    util = null;
112  }
113
114  private void doScans(ExecutorService service, final LinkedBlockingQueue<Long> rks) {
115    for (int i = 0; i < 100; i++) {
116      Runnable runnable = new Runnable() {
117        private final LinkedBlockingQueue<Long> rowKeyQueue = rks;
118
119        @Override
120        public void run() {
121          ResultScanner rs = null;
122          Span span = TraceUtil.getGlobalTracer().spanBuilder("Scan").startSpan();
123          try (Scope scope = span.makeCurrent()) {
124            Table ht = util.getConnection().getTable(tableName);
125            Scan s = new Scan();
126            s.withStartRow(Bytes.toBytes(rowKeyQueue.take()));
127            s.setBatch(7);
128            rs = ht.getScanner(s);
129            // Something to keep the jvm from removing the loop.
130            long accum = 0;
131
132            for (int x = 0; x < 1000; x++) {
133              Result r = rs.next();
134              accum |= Bytes.toLong(r.getRow());
135            }
136
137            span.addEvent("Accum result = " + accum);
138
139            ht.close();
140            ht = null;
141          } catch (IOException e) {
142            e.printStackTrace();
143            span.addEvent("exception",
144              Attributes.of(AttributeKey.stringKey("exception"), e.getClass().getSimpleName()));
145          } catch (Exception e) {
146          } finally {
147            span.end();
148            if (rs != null) {
149              rs.close();
150            }
151          }
152        }
153      };
154      service.submit(runnable);
155    }
156  }
157
158  private void doGets(ExecutorService service, final LinkedBlockingQueue<Long> rowKeys)
159      throws IOException {
160    for (int i = 0; i < 100; i++) {
161      Runnable runnable = new Runnable() {
162        private final LinkedBlockingQueue<Long> rowKeyQueue = rowKeys;
163
164        @Override
165        public void run() {
166
167
168          Table ht = null;
169          try {
170            ht = util.getConnection().getTable(tableName);
171          } catch (IOException e) {
172            e.printStackTrace();
173          }
174
175          long accum = 0;
176          for (int x = 0; x < 5; x++) {
177            Span span = TraceUtil.getGlobalTracer().spanBuilder("gets").startSpan();
178            try (Scope scope = span.makeCurrent()) {
179              long rk = rowKeyQueue.take();
180              Result r1 = ht.get(new Get(Bytes.toBytes(rk)));
181              if (r1 != null) {
182                accum |= Bytes.toLong(r1.getRow());
183              }
184              Result r2 = ht.get(new Get(Bytes.toBytes(rk)));
185              if (r2 != null) {
186                accum |= Bytes.toLong(r2.getRow());
187              }
188              span.addEvent("Accum = " + accum);
189
190            } catch (IOException|InterruptedException ie) {
191              // IGNORED
192            } finally {
193              span.end();
194            }
195          }
196
197        }
198      };
199      service.submit(runnable);
200    }
201  }
202
203  private void createTable() throws IOException {
204    Span span = TraceUtil.getGlobalTracer().spanBuilder("createTable").startSpan();
205    try (Scope scope = span.makeCurrent()) {
206      util.createTable(tableName, familyName);
207    } finally {
208      span.end();
209    }
210  }
211
212  private void deleteTable() throws IOException {
213    Span span = TraceUtil.getGlobalTracer().spanBuilder("deleteTable").startSpan();
214    try (Scope scope = span.makeCurrent()) {
215      if (admin.tableExists(tableName)) {
216        util.deleteTable(tableName);
217      }
218    } finally {
219      span.end();
220    }
221  }
222
223  private LinkedBlockingQueue<Long> insertData() throws IOException, InterruptedException {
224    LinkedBlockingQueue<Long> rowKeys = new LinkedBlockingQueue<>(25000);
225    BufferedMutator ht = util.getConnection().getBufferedMutator(this.tableName);
226    byte[] value = new byte[300];
227    for (int x = 0; x < 5000; x++) {
228      Span span = TraceUtil.getGlobalTracer().spanBuilder("insertData").startSpan();
229      try (Scope scope = span.makeCurrent()) {
230        for (int i = 0; i < 5; i++) {
231          long rk = random.nextLong();
232          rowKeys.add(rk);
233          Put p = new Put(Bytes.toBytes(rk));
234          for (int y = 0; y < 10; y++) {
235            random.nextBytes(value);
236            p.addColumn(familyName, Bytes.toBytes(random.nextLong()), value);
237          }
238          ht.mutate(p);
239        }
240        if ((x % 1000) == 0) {
241          admin.flush(tableName);
242        }
243      } finally {
244        span.end();
245      }
246    }
247    admin.flush(tableName);
248    return rowKeys;
249  }
250
251  private IntegrationTestingUtility createUtil() throws Exception {
252    Configuration conf = getConf();
253    if (this.util == null) {
254      IntegrationTestingUtility u;
255      if (conf == null) {
256        u = new IntegrationTestingUtility();
257      } else {
258        u = new IntegrationTestingUtility(conf);
259      }
260      util = u;
261      util.initializeCluster(1);
262
263    }
264    return this.util;
265  }
266}