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.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.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 HBaseTestingUtil UTIL = new HBaseTestingUtil();
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<? extends 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    HBaseTestingUtil.setReplicas(UTIL.getAdmin(), TableName.META_TABLE_NAME, 3);
098    try (ConnectionRegistry registry = ConnectionRegistryFactory.create(conf, User.getCurrent())) {
099      RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(UTIL, registry);
100    }
101    try (Table table = UTIL.createTable(TABLE_NAME, FAMILY)) {
102      table.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, VALUE));
103    }
104    UTIL.flush(TableName.META_TABLE_NAME);
105    // wait for the store file refresh so we can read the region location from secondary meta
106    // replicas
107    Thread.sleep(2000);
108  }
109
110  @AfterClass
111  public static void tearDown() throws Exception {
112    UTIL.shutdownMiniCluster();
113  }
114
115  @After
116  public void tearDownAfterTest() {
117    // make sure we do not mess up cleanup code.
118    FAIL_PRIMARY_SCAN = false;
119  }
120
121  private void testRead(boolean useMetaReplicas)
122    throws IOException, InterruptedException, ExecutionException {
123    FAIL_PRIMARY_SCAN = true;
124    Configuration conf = new Configuration(UTIL.getConfiguration());
125    conf.setBoolean(HConstants.USE_META_REPLICAS, useMetaReplicas);
126    conf.setLong(HConstants.HBASE_CLIENT_META_REPLICA_SCAN_TIMEOUT, TimeUnit.SECONDS.toMicros(1));
127    try (AsyncConnection conn = ConnectionFactory.createAsyncConnection(conf).get()) {
128      Result result = FutureUtils.get(conn.getTableBuilder(TABLE_NAME)
129        .setOperationTimeout(3, TimeUnit.SECONDS).build().get(new Get(ROW)));
130      assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER));
131    }
132  }
133
134  @Test(expected = RetriesExhaustedException.class)
135  public void testNotUseMetaReplicas()
136    throws IOException, InterruptedException, ExecutionException {
137    testRead(false);
138  }
139
140  @Test
141  public void testUseMetaReplicas() throws IOException, InterruptedException, ExecutionException {
142    testRead(true);
143  }
144}