2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.client
;
20 import static org
.junit
.Assert
.assertArrayEquals
;
21 import static org
.junit
.Assert
.assertEquals
;
23 import java
.util
.Arrays
;
24 import java
.util
.List
;
25 import java
.util
.stream
.Collectors
;
26 import java
.util
.stream
.IntStream
;
27 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
28 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
29 import org
.apache
.hadoop
.hbase
.HConstants
;
30 import org
.apache
.hadoop
.hbase
.TableName
;
31 import org
.apache
.hadoop
.hbase
.logging
.Log4jUtils
;
32 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
34 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
35 import org
.junit
.AfterClass
;
36 import org
.junit
.BeforeClass
;
37 import org
.junit
.ClassRule
;
38 import org
.junit
.Test
;
39 import org
.junit
.experimental
.categories
.Category
;
41 @Category({ MediumTests
.class, ClientTests
.class })
42 public class TestAsyncTableBatchRetryImmediately
{
45 public static final HBaseClassTestRule CLASS_RULE
=
46 HBaseClassTestRule
.forClass(TestAsyncTableBatchRetryImmediately
.class);
48 private static final HBaseTestingUtil UTIL
= new HBaseTestingUtil();
50 private static TableName TABLE_NAME
= TableName
.valueOf("async");
52 private static byte[] FAMILY
= Bytes
.toBytes("cf");
54 private static byte[] QUAL
= Bytes
.toBytes("cq");
56 private static byte[] VALUE_PREFIX
= new byte[768];
58 private static int COUNT
= 1000;
60 private static AsyncConnection CONN
;
62 private static String LOG_LEVEL
;
65 public static void setUp() throws Exception
{
66 // disable the debug log to avoid flooding the output
67 LOG_LEVEL
= Log4jUtils
.getEffectiveLevel(AsyncRegionLocatorHelper
.class.getName());
68 Log4jUtils
.setLogLevel(AsyncRegionLocatorHelper
.class.getName(), "INFO");
69 UTIL
.getConfiguration().setLong(HConstants
.HBASE_SERVER_SCANNER_MAX_RESULT_SIZE_KEY
, 1024);
70 UTIL
.startMiniCluster(1);
71 Table table
= UTIL
.createTable(TABLE_NAME
, FAMILY
);
72 UTIL
.waitTableAvailable(TABLE_NAME
);
73 Bytes
.random(VALUE_PREFIX
);
74 for (int i
= 0; i
< COUNT
; i
++) {
75 table
.put(new Put(Bytes
.toBytes(i
)).addColumn(FAMILY
, QUAL
,
76 Bytes
.add(VALUE_PREFIX
, Bytes
.toBytes(i
))));
78 CONN
= ConnectionFactory
.createAsyncConnection(UTIL
.getConfiguration()).get();
82 public static void tearDown() throws Exception
{
83 if (LOG_LEVEL
!= null) {
84 Log4jUtils
.setLogLevel(AsyncRegionLocatorHelper
.class.getName(), LOG_LEVEL
);
87 UTIL
.shutdownMiniCluster();
92 AsyncTable
<?
> table
= CONN
.getTable(TABLE_NAME
);
93 // if we do not deal with RetryImmediatelyException, we will timeout here since we need to retry
95 List
<Get
> gets
= IntStream
.range(0, COUNT
).mapToObj(i
-> new Get(Bytes
.toBytes(i
)))
96 .collect(Collectors
.toList());
97 List
<Result
> results
= table
.getAll(gets
).join();
98 for (int i
= 0; i
< COUNT
; i
++) {
99 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
));