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