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