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.util.Arrays;
024import java.util.List;
025import java.util.concurrent.ThreadLocalRandom;
026import java.util.stream.Collectors;
027import java.util.stream.IntStream;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.testclassification.ClientTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.log4j.Level;
036import org.apache.log4j.LogManager;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043@Category({ MediumTests.class, ClientTests.class })
044public class TestAsyncTableBatchRetryImmediately {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestAsyncTableBatchRetryImmediately.class);
049
050  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
051
052  private static TableName TABLE_NAME = TableName.valueOf("async");
053
054  private static byte[] FAMILY = Bytes.toBytes("cf");
055
056  private static byte[] QUAL = Bytes.toBytes("cq");
057
058  private static byte[] VALUE_PREFIX = new byte[768];
059
060  private static int COUNT = 1000;
061
062  private static AsyncConnection CONN;
063
064  @BeforeClass
065  public static void setUp() throws Exception {
066    // disable the debug log to avoid flooding the output
067    LogManager.getLogger(AsyncRegionLocatorHelper.class).setLevel(Level.INFO);
068    UTIL.getConfiguration().setLong(HConstants.HBASE_SERVER_SCANNER_MAX_RESULT_SIZE_KEY, 1024);
069    UTIL.startMiniCluster(1);
070    Table table = UTIL.createTable(TABLE_NAME, FAMILY);
071    UTIL.waitTableAvailable(TABLE_NAME);
072    ThreadLocalRandom.current().nextBytes(VALUE_PREFIX);
073    for (int i = 0; i < COUNT; i++) {
074      table.put(new Put(Bytes.toBytes(i)).addColumn(FAMILY, QUAL,
075        Bytes.add(VALUE_PREFIX, Bytes.toBytes(i))));
076    }
077    CONN = ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get();
078  }
079
080  @AfterClass
081  public static void tearDown() throws Exception {
082    CONN.close();
083    UTIL.shutdownMiniCluster();
084  }
085
086  @Test
087  public void test() {
088    AsyncTable<?> table = CONN.getTable(TABLE_NAME);
089    // if we do not deal with RetryImmediatelyException, we will timeout here since we need to retry
090    // hundreds times.
091    List<Get> gets = IntStream.range(0, COUNT).mapToObj(i -> new Get(Bytes.toBytes(i)))
092      .collect(Collectors.toList());
093    List<Result> results = table.getAll(gets).join();
094    for (int i = 0; i < COUNT; i++) {
095      byte[] value = results.get(i).getValue(FAMILY, QUAL);
096      assertEquals(VALUE_PREFIX.length + 4, value.length);
097      assertArrayEquals(VALUE_PREFIX, Arrays.copyOf(value, VALUE_PREFIX.length));
098      assertEquals(i, Bytes.toInt(value, VALUE_PREFIX.length));
099    }
100  }
101}