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
.concurrent
.ThreadLocalRandom
;
26 import java
.util
.stream
.Collectors
;
27 import java
.util
.stream
.IntStream
;
28 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
29 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
30 import org
.apache
.hadoop
.hbase
.HConstants
;
31 import org
.apache
.hadoop
.hbase
.TableName
;
32 import org
.apache
.hadoop
.hbase
.logging
.Log4jUtils
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
34 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
35 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
36 import org
.junit
.AfterClass
;
37 import org
.junit
.BeforeClass
;
38 import org
.junit
.ClassRule
;
39 import org
.junit
.Test
;
40 import org
.junit
.experimental
.categories
.Category
;
42 @Category({ MediumTests
.class, ClientTests
.class })
43 public class TestAsyncTableBatchRetryImmediately
{
46 public static final HBaseClassTestRule CLASS_RULE
=
47 HBaseClassTestRule
.forClass(TestAsyncTableBatchRetryImmediately
.class);
49 private static final HBaseTestingUtil UTIL
= new HBaseTestingUtil();
51 private static TableName TABLE_NAME
= TableName
.valueOf("async");
53 private static byte[] FAMILY
= Bytes
.toBytes("cf");
55 private static byte[] QUAL
= Bytes
.toBytes("cq");
57 private static byte[] VALUE_PREFIX
= new byte[768];
59 private static int COUNT
= 1000;
61 private static AsyncConnection CONN
;
63 private static String LOG_LEVEL
;
66 public static void setUp() throws Exception
{
67 // disable the debug log to avoid flooding the output
68 LOG_LEVEL
= Log4jUtils
.getEffectiveLevel(AsyncRegionLocatorHelper
.class.getName());
69 Log4jUtils
.setLogLevel(AsyncRegionLocatorHelper
.class.getName(), "INFO");
70 UTIL
.getConfiguration().setLong(HConstants
.HBASE_SERVER_SCANNER_MAX_RESULT_SIZE_KEY
, 1024);
71 UTIL
.startMiniCluster(1);
72 Table table
= UTIL
.createTable(TABLE_NAME
, FAMILY
);
73 UTIL
.waitTableAvailable(TABLE_NAME
);
74 ThreadLocalRandom
.current().nextBytes(VALUE_PREFIX
);
75 for (int i
= 0; i
< COUNT
; i
++) {
76 table
.put(new Put(Bytes
.toBytes(i
)).addColumn(FAMILY
, QUAL
,
77 Bytes
.add(VALUE_PREFIX
, Bytes
.toBytes(i
))));
79 CONN
= ConnectionFactory
.createAsyncConnection(UTIL
.getConfiguration()).get();
83 public static void tearDown() throws Exception
{
84 if (LOG_LEVEL
!= null) {
85 Log4jUtils
.setLogLevel(AsyncRegionLocatorHelper
.class.getName(), LOG_LEVEL
);
88 UTIL
.shutdownMiniCluster();
93 AsyncTable
<?
> table
= CONN
.getTable(TABLE_NAME
);
94 // if we do not deal with RetryImmediatelyException, we will timeout here since we need to retry
96 List
<Get
> gets
= IntStream
.range(0, COUNT
).mapToObj(i
-> new Get(Bytes
.toBytes(i
)))
97 .collect(Collectors
.toList());
98 List
<Result
> results
= table
.getAll(gets
).join();
99 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
));