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.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.TableNotFoundException;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.apache.hadoop.hbase.zookeeper.ZKUtil;
032import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
033import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
034import org.junit.jupiter.api.BeforeAll;
035import org.junit.jupiter.api.Tag;
036import org.junit.jupiter.api.Test;
037
038import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
039
040@Tag(MiscTests.TAG)
041@Tag(MediumTests.TAG)
042public class TestMetaWithReplicasBasic extends MetaWithReplicasTestBase {
043
044  @BeforeAll
045  public static void setUp() throws Exception {
046    startCluster();
047  }
048
049  @Test
050  public void testMetaHTDReplicaCount() throws Exception {
051    assertEquals(3,
052      TEST_UTIL.getAdmin().getDescriptor(TableName.META_TABLE_NAME).getRegionReplication());
053  }
054
055  @Test
056  public void testZookeeperNodesForReplicas() throws Exception {
057    // Checks all the znodes exist when meta's replicas are enabled
058    ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
059    Configuration conf = TEST_UTIL.getConfiguration();
060    String baseZNode =
061      conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
062    String primaryMetaZnode =
063      ZNodePaths.joinZNode(baseZNode, conf.get("zookeeper.znode.metaserver", "meta-region-server"));
064    // check that the data in the znode is parseable (this would also mean the znode exists)
065    byte[] data = ZKUtil.getData(zkw, primaryMetaZnode);
066    ProtobufUtil.toServerName(data);
067    for (int i = 1; i < 3; i++) {
068      String secZnode = ZNodePaths.joinZNode(baseZNode,
069        conf.get("zookeeper.znode.metaserver", "meta-region-server") + "-" + i);
070      String str = zkw.getZNodePaths().getZNodeForReplica(i);
071      assertTrue(str.equals(secZnode));
072      // check that the data in the znode is parseable (this would also mean the znode exists)
073      data = ZKUtil.getData(zkw, secZnode);
074      ProtobufUtil.toServerName(data);
075    }
076  }
077
078  @Test
079  public void testAccessingUnknownTables() throws Exception {
080    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
081    conf.setBoolean(HConstants.USE_META_REPLICAS, true);
082    Table table = TEST_UTIL.getConnection().getTable(name.getTableName());
083    Get get = new Get(Bytes.toBytes("foo"));
084    assertThrows(TableNotFoundException.class, () -> table.get(get));
085  }
086}