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 (
081        FAIL_PRIMARY_SCAN && TableName.isMetaTableName(region.getTable())
082          && region.getReplicaId() == RegionReplicaUtil.DEFAULT_REPLICA_ID
083      ) {
084        throw new IOException("Inject error");
085      }
086    }
087  }
088
089  @BeforeClass
090  public static void setUp() throws Exception {
091    Configuration conf = UTIL.getConfiguration();
092    conf.setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 1000);
093    conf.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
094      FailPrimaryMetaScanCp.class.getName());
095    UTIL.startMiniCluster(3);
096    HBaseTestingUtility.setReplicas(UTIL.getAdmin(), TableName.META_TABLE_NAME, 3);
097    try (ConnectionRegistry registry = ConnectionRegistryFactory.getRegistry(conf)) {
098      RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(UTIL, registry);
099    }
100    try (Table table = UTIL.createTable(TABLE_NAME, FAMILY)) {
101      table.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, VALUE));
102    }
103    UTIL.flush(TableName.META_TABLE_NAME);
104    // wait for the store file refresh so we can read the region location from secondary meta
105    // replicas
106    Thread.sleep(2000);
107  }
108
109  @AfterClass
110  public static void tearDown() throws Exception {
111    UTIL.shutdownMiniCluster();
112  }
113
114  @After
115  public void tearDownAfterTest() {
116    // make sure we do not mess up cleanup code.
117    FAIL_PRIMARY_SCAN = false;
118  }
119
120  private void testRead(boolean useMetaReplicas)
121    throws IOException, InterruptedException, ExecutionException {
122    FAIL_PRIMARY_SCAN = true;
123    Configuration conf = new Configuration(UTIL.getConfiguration());
124    conf.setBoolean(HConstants.USE_META_REPLICAS, useMetaReplicas);
125    conf.setLong(HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT, TimeUnit.SECONDS.toMicros(1));
126    try (AsyncConnection conn = ConnectionFactory.createAsyncConnection(conf).get()) {
127      Result result = FutureUtils.get(conn.getTableBuilder(TABLE_NAME)
128        .setOperationTimeout(3, TimeUnit.SECONDS).build().get(new Get(ROW)));
129      assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER));
130    }
131  }
132
133  @Test(expected = RetriesExhaustedException.class)
134  public void testNotUseMetaReplicas()
135    throws IOException, InterruptedException, ExecutionException {
136    testRead(false);
137  }
138
139  @Test
140  public void testUseMetaReplicas() throws IOException, InterruptedException, ExecutionException {
141    testRead(true);
142  }
143}