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 java.io.IOException; 022import java.util.Optional; 023import java.util.concurrent.ExecutionException; 024import java.util.concurrent.TimeUnit; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; 031import org.apache.hadoop.hbase.coprocessor.ObserverContext; 032import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 033import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 034import org.apache.hadoop.hbase.coprocessor.RegionObserver; 035import org.apache.hadoop.hbase.regionserver.StorefileRefresherChore; 036import org.apache.hadoop.hbase.testclassification.ClientTests; 037import org.apache.hadoop.hbase.testclassification.MediumTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.apache.hadoop.hbase.util.FutureUtils; 040import org.junit.After; 041import org.junit.AfterClass; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046 047@Category({ ClientTests.class, MediumTests.class }) 048public class TestAsyncTableUseMetaReplicas { 049 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestAsyncTableUseMetaReplicas.class); 053 054 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 055 056 private static TableName TABLE_NAME = TableName.valueOf("Replica"); 057 058 private static byte[] FAMILY = Bytes.toBytes("Family"); 059 060 private static byte[] QUALIFIER = Bytes.toBytes("Qual"); 061 062 private static byte[] ROW = Bytes.toBytes("Row"); 063 064 private static byte[] VALUE = Bytes.toBytes("Value"); 065 066 private static volatile boolean FAIL_PRIMARY_SCAN = false; 067 068 public static final class FailPrimaryMetaScanCp implements RegionObserver, RegionCoprocessor { 069 070 @Override 071 public Optional<RegionObserver> getRegionObserver() { 072 return Optional.of(this); 073 } 074 075 @Override 076 public void preScannerOpen(ObserverContext<RegionCoprocessorEnvironment> c, Scan scan) 077 throws IOException { 078 RegionInfo region = c.getEnvironment().getRegionInfo(); 079 if (FAIL_PRIMARY_SCAN && TableName.isMetaTableName(region.getTable()) && 080 region.getReplicaId() == RegionReplicaUtil.DEFAULT_REPLICA_ID) { 081 throw new IOException("Inject error"); 082 } 083 } 084 } 085 086 @BeforeClass 087 public static void setUp() throws Exception { 088 Configuration conf = UTIL.getConfiguration(); 089 conf.setInt(HConstants.META_REPLICAS_NUM, 3); 090 conf.setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 1000); 091 conf.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, 092 FailPrimaryMetaScanCp.class.getName()); 093 UTIL.startMiniCluster(3); 094 try (ConnectionRegistry registry = ConnectionRegistryFactory.getRegistry(conf)) { 095 RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(UTIL, registry); 096 } 097 try (Table table = UTIL.createTable(TABLE_NAME, FAMILY)) { 098 table.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, VALUE)); 099 } 100 UTIL.flush(TableName.META_TABLE_NAME); 101 // wait for the store file refresh so we can read the region location from secondary meta 102 // replicas 103 Thread.sleep(2000); 104 } 105 106 @AfterClass 107 public static void tearDown() throws Exception { 108 UTIL.shutdownMiniCluster(); 109 } 110 111 @After 112 public void tearDownAfterTest() { 113 // make sure we do not mess up cleanup code. 114 FAIL_PRIMARY_SCAN = false; 115 } 116 117 private void testRead(boolean useMetaReplicas) 118 throws IOException, InterruptedException, ExecutionException { 119 FAIL_PRIMARY_SCAN = true; 120 Configuration conf = new Configuration(UTIL.getConfiguration()); 121 conf.setBoolean(HConstants.USE_META_REPLICAS, useMetaReplicas); 122 conf.setLong(HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT, TimeUnit.SECONDS.toMicros(1)); 123 try (AsyncConnection conn = ConnectionFactory.createAsyncConnection(conf).get()) { 124 Result result = FutureUtils.get(conn.getTableBuilder(TABLE_NAME) 125 .setOperationTimeout(3, TimeUnit.SECONDS).build().get(new Get(ROW))); 126 assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER)); 127 } 128 } 129 130 @Test(expected = RetriesExhaustedException.class) 131 public void testNotUseMetaReplicas() 132 throws IOException, InterruptedException, ExecutionException { 133 testRead(false); 134 } 135 136 @Test 137 public void testUseMetaReplicas() throws IOException, InterruptedException, ExecutionException { 138 testRead(true); 139 } 140}