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 */
018package org.apache.hadoop.hbase.coprocessor.example;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import java.io.InterruptedIOException;
024import java.io.UncheckedIOException;
025import java.util.stream.IntStream;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Get;
030import org.apache.hadoop.hbase.client.Increment;
031import org.apache.hadoop.hbase.client.Result;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.regionserver.CompactingMemStore;
034import org.apache.hadoop.hbase.util.Bytes;
035
036public class WriteHeavyIncrementObserverTestBase {
037
038  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
039
040  protected static TableName NAME = TableName.valueOf("TestCP");
041
042  protected static byte[] FAMILY = Bytes.toBytes("cf");
043
044  protected static byte[] ROW = Bytes.toBytes("row");
045
046  protected static byte[] CQ1 = Bytes.toBytes("cq1");
047
048  protected static byte[] CQ2 = Bytes.toBytes("cq2");
049
050  protected static Table TABLE;
051
052  protected static long UPPER = 1000;
053
054  protected static int THREADS = 10;
055
056  public static void setUp() throws Exception {
057    UTIL.getConfiguration().setLong(HConstants.HREGION_MEMSTORE_FLUSH_SIZE, 64 * 1024L);
058    UTIL.getConfiguration().setLong("hbase.hregion.memstore.flush.size.limit", 1024L);
059    UTIL.getConfiguration().setDouble(CompactingMemStore.IN_MEMORY_FLUSH_THRESHOLD_FACTOR_KEY,
060      0.014);
061    UTIL.startMiniCluster(3);
062  }
063
064  private static void increment(int sleepSteps) throws IOException {
065    for (long i = 1; i <= UPPER; i++) {
066      TABLE.increment(new Increment(ROW).addColumn(FAMILY, CQ1, i).addColumn(FAMILY, CQ2, 2 * i));
067      if (sleepSteps > 0 && i % sleepSteps == 0) {
068        try {
069          Thread.sleep(10);
070        } catch (InterruptedException e) {
071          throw (IOException) new InterruptedIOException().initCause(e);
072        }
073      }
074    }
075  }
076
077  protected final void assertSum() throws IOException {
078    Result result = TABLE.get(new Get(ROW).addColumn(FAMILY, CQ1).addColumn(FAMILY, CQ2));
079    assertEquals(THREADS * (1 + UPPER) * UPPER / 2, Bytes.toLong(result.getValue(FAMILY, CQ1)));
080    assertEquals(THREADS * (1 + UPPER) * UPPER, Bytes.toLong(result.getValue(FAMILY, CQ2)));
081  }
082
083  protected final void doIncrement(int sleepSteps) throws InterruptedException {
084    Thread[] threads = IntStream.range(0, THREADS).mapToObj(i -> new Thread(() -> {
085      try {
086        increment(sleepSteps);
087      } catch (IOException e) {
088        throw new UncheckedIOException(e);
089      }
090    }, "increment-" + i)).toArray(Thread[]::new);
091    for (Thread thread : threads) {
092      thread.start();
093    }
094    for (Thread thread : threads) {
095      thread.join();
096    }
097  }
098}