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