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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotEquals;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertNull;
024import static org.junit.jupiter.api.Assertions.assertTrue;
025
026import java.io.IOException;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.client.Connection;
029import org.apache.hadoop.hbase.client.ConnectionFactory;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.master.HMaster;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.MiscTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.Pair;
036import org.junit.jupiter.api.AfterAll;
037import org.junit.jupiter.api.BeforeAll;
038import org.junit.jupiter.api.Tag;
039import org.junit.jupiter.api.Test;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043/**
044 * Test {@link org.apache.hadoop.hbase.TestMetaTableForReplica}.
045 */
046@Tag(MiscTests.TAG)
047@Tag(MediumTests.TAG)
048@SuppressWarnings("deprecation")
049public class TestMetaTableForReplica {
050
051  private static final Logger LOG = LoggerFactory.getLogger(TestMetaTableForReplica.class);
052  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
053  private static Connection connection;
054
055  @BeforeAll
056  public static void beforeClass() throws Exception {
057    Configuration c = UTIL.getConfiguration();
058    // quicker heartbeat interval for faster DN death notification
059    c.setInt("hbase.ipc.client.connect.max.retries", 1);
060    c.setInt(HConstants.ZK_SESSION_TIMEOUT, 1000);
061    // Start cluster having non-default hbase meta table name
062    UTIL.startMiniCluster(3);
063    connection = ConnectionFactory.createConnection(c);
064  }
065
066  @AfterAll
067  public static void afterClass() throws Exception {
068    connection.close();
069    UTIL.shutdownMiniCluster();
070  }
071
072  @Test
073  public void testStateOfMetaForReplica() {
074    HMaster m = UTIL.getMiniHBaseCluster().getMaster();
075    assertTrue(m.waitForMetaOnline());
076  }
077
078  @Test
079  public void testMetaTableNameForReplicaWithoutSuffix() throws IOException {
080    testNameOfMetaForReplica();
081    testGetNonExistentRegionFromMetaFromReplica();
082    testGetExistentRegionFromMetaFromReplica();
083  }
084
085  private void testNameOfMetaForReplica() {
086    // Check the correctness of the meta table for replica
087    String metaTableName = TableName.META_TABLE_NAME.getNameWithNamespaceInclAsString();
088    assertNotNull(metaTableName);
089
090    // Check if name of the meta table for replica is same as the default meta table
091    assertEquals(0,
092      TableName.META_TABLE_NAME.compareTo(TableName.getDefaultNameOfMetaForReplica()));
093  }
094
095  private void testGetNonExistentRegionFromMetaFromReplica() throws IOException {
096    LOG.info("Started testGetNonExistentRegionFromMetaFromReplica");
097    Pair<RegionInfo, ServerName> pair =
098      MetaTableAccessor.getRegion(connection, Bytes.toBytes("nonexistent-region"));
099    assertNull(pair);
100    LOG.info("Finished testGetNonExistentRegionFromMetaFromReplica");
101  }
102
103  private void testGetExistentRegionFromMetaFromReplica() throws IOException {
104    final TableName tableName = TableName.valueOf("testMetaTableNameForReplicaWithoutSuffix");
105    LOG.info("Started " + tableName);
106    UTIL.createTable(tableName, HConstants.CATALOG_FAMILY);
107    assertEquals(1, MetaTableAccessor.getTableRegions(connection, tableName).size());
108  }
109
110  @Test
111  public void testMetaTableNameForReplicaWithSuffix() {
112    // TableName.META_TABLE_NAME is assigned in the class initializer, before a test can set a
113    // configuration, so assert on the method the initializer itself calls.
114    Configuration conf = HBaseConfiguration.create();
115    conf.set(HConstants.HBASE_META_TABLE_SUFFIX, "replica1");
116    TableName withSuffix = TableName.initializeHbaseMetaTableName(conf);
117
118    assertNotEquals(TableName.getDefaultNameOfMetaForReplica(), withSuffix,
119      "a configured suffix should not produce the default meta table name");
120    assertEquals(TableName.valueOf(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR, "meta_replica1"),
121      withSuffix, "meta table name should carry the configured suffix");
122  }
123
124}