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
;
22 import java
.io
.IOException
;
23 import java
.util
.Optional
;
24 import java
.util
.concurrent
.ExecutionException
;
25 import java
.util
.concurrent
.TimeUnit
;
26 import org
.apache
.hadoop
.conf
.Configuration
;
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
.coprocessor
.CoprocessorHost
;
32 import org
.apache
.hadoop
.hbase
.coprocessor
.ObserverContext
;
33 import org
.apache
.hadoop
.hbase
.coprocessor
.RegionCoprocessor
;
34 import org
.apache
.hadoop
.hbase
.coprocessor
.RegionCoprocessorEnvironment
;
35 import org
.apache
.hadoop
.hbase
.coprocessor
.RegionObserver
;
36 import org
.apache
.hadoop
.hbase
.regionserver
.StorefileRefresherChore
;
37 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
38 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
39 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
40 import org
.apache
.hadoop
.hbase
.util
.FutureUtils
;
41 import org
.junit
.After
;
42 import org
.junit
.AfterClass
;
43 import org
.junit
.BeforeClass
;
44 import org
.junit
.ClassRule
;
45 import org
.junit
.Test
;
46 import org
.junit
.experimental
.categories
.Category
;
48 @Category({ ClientTests
.class, MediumTests
.class })
49 public class TestAsyncTableUseMetaReplicas
{
52 public static final HBaseClassTestRule CLASS_RULE
=
53 HBaseClassTestRule
.forClass(TestAsyncTableUseMetaReplicas
.class);
55 private static final HBaseTestingUtil UTIL
= new HBaseTestingUtil();
57 private static TableName TABLE_NAME
= TableName
.valueOf("Replica");
59 private static byte[] FAMILY
= Bytes
.toBytes("Family");
61 private static byte[] QUALIFIER
= Bytes
.toBytes("Qual");
63 private static byte[] ROW
= Bytes
.toBytes("Row");
65 private static byte[] VALUE
= Bytes
.toBytes("Value");
67 private static volatile boolean FAIL_PRIMARY_SCAN
= false;
69 public static final class FailPrimaryMetaScanCp
implements RegionObserver
, RegionCoprocessor
{
72 public Optional
<RegionObserver
> getRegionObserver() {
73 return Optional
.of(this);
77 public void preScannerOpen(ObserverContext
<RegionCoprocessorEnvironment
> c
, Scan scan
)
79 RegionInfo region
= c
.getEnvironment().getRegionInfo();
80 if (FAIL_PRIMARY_SCAN
&& TableName
.isMetaTableName(region
.getTable()) &&
81 region
.getReplicaId() == RegionReplicaUtil
.DEFAULT_REPLICA_ID
) {
82 throw new IOException("Inject error");
88 public static void setUp() throws Exception
{
89 Configuration conf
= UTIL
.getConfiguration();
90 conf
.setInt(StorefileRefresherChore
.REGIONSERVER_STOREFILE_REFRESH_PERIOD
, 1000);
91 conf
.setStrings(CoprocessorHost
.REGION_COPROCESSOR_CONF_KEY
,
92 FailPrimaryMetaScanCp
.class.getName());
93 UTIL
.startMiniCluster(3);
94 HBaseTestingUtil
.setReplicas(UTIL
.getAdmin(), TableName
.META_TABLE_NAME
, 3);
95 try (ConnectionRegistry registry
= ConnectionRegistryFactory
.getRegistry(conf
)) {
96 RegionReplicaTestHelper
.waitUntilAllMetaReplicasAreReady(UTIL
, registry
);
98 try (Table table
= UTIL
.createTable(TABLE_NAME
, FAMILY
)) {
99 table
.put(new Put(ROW
).addColumn(FAMILY
, QUALIFIER
, VALUE
));
101 UTIL
.flush(TableName
.META_TABLE_NAME
);
102 // wait for the store file refresh so we can read the region location from secondary meta
108 public static void tearDown() throws Exception
{
109 UTIL
.shutdownMiniCluster();
113 public void tearDownAfterTest() {
114 // make sure we do not mess up cleanup code.
115 FAIL_PRIMARY_SCAN
= false;
118 private void testRead(boolean useMetaReplicas
)
119 throws IOException
, InterruptedException
, ExecutionException
{
120 FAIL_PRIMARY_SCAN
= true;
121 Configuration conf
= new Configuration(UTIL
.getConfiguration());
122 conf
.setBoolean(HConstants
.USE_META_REPLICAS
, useMetaReplicas
);
123 conf
.setLong(HConstants
.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT
, TimeUnit
.SECONDS
.toMicros(1));
124 try (AsyncConnection conn
= ConnectionFactory
.createAsyncConnection(conf
).get()) {
125 Result result
= FutureUtils
.get(conn
.getTableBuilder(TABLE_NAME
)
126 .setOperationTimeout(3, TimeUnit
.SECONDS
).build().get(new Get(ROW
)));
127 assertArrayEquals(VALUE
, result
.getValue(FAMILY
, QUALIFIER
));
131 @Test(expected
= RetriesExhaustedException
.class)
132 public void testNotUseMetaReplicas()
133 throws IOException
, InterruptedException
, ExecutionException
{
138 public void testUseMetaReplicas() throws IOException
, InterruptedException
, ExecutionException
{