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.client; 019 020import static org.junit.Assert.assertArrayEquals; 021import static org.junit.Assert.assertEquals; 022 023import java.io.IOException; 024import java.util.Optional; 025import java.util.concurrent.ExecutionException; 026import java.util.concurrent.TimeUnit; 027import java.util.concurrent.atomic.AtomicInteger; 028import org.apache.commons.io.IOUtils; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.HBaseTestingUtility; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.coprocessor.ObserverContext; 033import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 034import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 035import org.apache.hadoop.hbase.coprocessor.RegionObserver; 036import org.apache.hadoop.hbase.testclassification.ClientTests; 037import org.apache.hadoop.hbase.testclassification.MediumTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.apache.hadoop.hbase.util.Threads; 040import org.junit.AfterClass; 041import org.junit.Before; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Rule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047import org.junit.rules.TestName; 048 049@Category({ MediumTests.class, ClientTests.class }) 050public class TestAsyncTableNoncedRetry { 051 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestAsyncTableNoncedRetry.class); 055 056 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 057 058 private static TableName TABLE_NAME = TableName.valueOf("async"); 059 060 private static byte[] FAMILY = Bytes.toBytes("cf"); 061 062 private static byte[] QUALIFIER = Bytes.toBytes("cq"); 063 064 private static byte[] VALUE = Bytes.toBytes("value"); 065 066 private static AsyncConnection ASYNC_CONN; 067 068 @Rule 069 public TestName testName = new TestName(); 070 071 private byte[] row; 072 073 private static AtomicInteger CALLED = new AtomicInteger(); 074 075 private static long SLEEP_TIME = 2000; 076 077 public static final class SleepOnceCP implements RegionObserver, RegionCoprocessor { 078 079 @Override 080 public Optional<RegionObserver> getRegionObserver() { 081 return Optional.of(this); 082 } 083 084 @Override 085 public Result postAppend(ObserverContext<RegionCoprocessorEnvironment> c, Append append, 086 Result result) throws IOException { 087 if (CALLED.getAndIncrement() == 0) { 088 Threads.sleepWithoutInterrupt(SLEEP_TIME); 089 } 090 return RegionObserver.super.postAppend(c, append, result); 091 } 092 093 @Override 094 public Result postIncrement(ObserverContext<RegionCoprocessorEnvironment> c, 095 Increment increment, Result result) throws IOException { 096 if (CALLED.getAndIncrement() == 0) { 097 Threads.sleepWithoutInterrupt(SLEEP_TIME); 098 } 099 return RegionObserver.super.postIncrement(c, increment, result); 100 } 101 } 102 103 @BeforeClass 104 public static void setUpBeforeClass() throws Exception { 105 TEST_UTIL.startMiniCluster(1); 106 TEST_UTIL.getAdmin() 107 .createTable(TableDescriptorBuilder.newBuilder(TABLE_NAME) 108 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)) 109 .setCoprocessor(SleepOnceCP.class.getName()).build()); 110 TEST_UTIL.waitTableAvailable(TABLE_NAME); 111 ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get(); 112 } 113 114 @AfterClass 115 public static void tearDownAfterClass() throws Exception { 116 IOUtils.closeQuietly(ASYNC_CONN); 117 TEST_UTIL.shutdownMiniCluster(); 118 } 119 120 @Before 121 public void setUp() throws IOException, InterruptedException { 122 row = Bytes.toBytes(testName.getMethodName().replaceAll("[^0-9A-Za-z]", "_")); 123 CALLED.set(0); 124 } 125 126 @Test 127 public void testAppend() throws InterruptedException, ExecutionException { 128 assertEquals(0, CALLED.get()); 129 AsyncTable<?> table = ASYNC_CONN.getTableBuilder(TABLE_NAME) 130 .setRpcTimeout(SLEEP_TIME / 2, TimeUnit.MILLISECONDS).build(); 131 Result result = table.append(new Append(row).addColumn(FAMILY, QUALIFIER, VALUE)).get(); 132 // make sure we called twice and the result is still correct 133 assertEquals(2, CALLED.get()); 134 assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER)); 135 } 136 137 @Test 138 public void testIncrement() throws InterruptedException, ExecutionException { 139 assertEquals(0, CALLED.get()); 140 AsyncTable<?> table = ASYNC_CONN.getTableBuilder(TABLE_NAME) 141 .setRpcTimeout(SLEEP_TIME / 2, TimeUnit.MILLISECONDS).build(); 142 assertEquals(1L, table.incrementColumnValue(row, FAMILY, QUALIFIER, 1L).get().longValue()); 143 // make sure we called twice and the result is still correct 144 assertEquals(2, CALLED.get()); 145 } 146}