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.jupiter.api.Assertions.assertArrayEquals; 021import static org.junit.jupiter.api.Assertions.assertThrows; 022 023import java.io.IOException; 024import java.util.Optional; 025import java.util.concurrent.ExecutionException; 026import java.util.concurrent.TimeUnit; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 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.security.User; 038import org.apache.hadoop.hbase.testclassification.ClientTests; 039import org.apache.hadoop.hbase.testclassification.MediumTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.apache.hadoop.hbase.util.FutureUtils; 042import org.junit.jupiter.api.AfterAll; 043import org.junit.jupiter.api.AfterEach; 044import org.junit.jupiter.api.BeforeAll; 045import org.junit.jupiter.api.Tag; 046import org.junit.jupiter.api.Test; 047 048@Tag(ClientTests.TAG) 049@Tag(MediumTests.TAG) 050public class TestAsyncTableUseMetaReplicas { 051 052 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 053 054 private static TableName TABLE_NAME = TableName.valueOf("Replica"); 055 056 private static byte[] FAMILY = Bytes.toBytes("Family"); 057 058 private static byte[] QUALIFIER = Bytes.toBytes("Qual"); 059 060 private static byte[] ROW = Bytes.toBytes("Row"); 061 062 private static byte[] VALUE = Bytes.toBytes("Value"); 063 064 private static volatile boolean FAIL_PRIMARY_SCAN = false; 065 066 public static final class FailPrimaryMetaScanCp implements RegionObserver, RegionCoprocessor { 067 068 @Override 069 public Optional<RegionObserver> getRegionObserver() { 070 return Optional.of(this); 071 } 072 073 @Override 074 public void preScannerOpen(ObserverContext<? extends RegionCoprocessorEnvironment> c, Scan scan) 075 throws IOException { 076 RegionInfo region = c.getEnvironment().getRegionInfo(); 077 if ( 078 FAIL_PRIMARY_SCAN && TableName.isMetaTableName(region.getTable()) 079 && region.getReplicaId() == RegionReplicaUtil.DEFAULT_REPLICA_ID 080 ) { 081 throw new IOException("Inject error"); 082 } 083 } 084 } 085 086 @BeforeAll 087 public static void setUp() throws Exception { 088 Configuration conf = UTIL.getConfiguration(); 089 conf.setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 1000); 090 conf.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, 091 FailPrimaryMetaScanCp.class.getName()); 092 UTIL.startMiniCluster(3); 093 HBaseTestingUtil.setReplicas(UTIL.getAdmin(), TableName.META_TABLE_NAME, 3); 094 try (ConnectionRegistry registry = ConnectionRegistryFactory.create(conf, User.getCurrent())) { 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 @AfterAll 107 public static void tearDown() throws Exception { 108 UTIL.shutdownMiniCluster(); 109 } 110 111 @AfterEach 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 131 public void testNotUseMetaReplicas() 132 throws IOException, InterruptedException, ExecutionException { 133 assertThrows(RetriesExhaustedException.class, () -> testRead(false)); 134 } 135 136 @Test 137 public void testUseMetaReplicas() throws IOException, InterruptedException, ExecutionException { 138 testRead(true); 139 } 140}