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