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