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