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 java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.client.TestMetaWithReplicasShutdownHandling;
023import org.apache.hadoop.hbase.regionserver.StorefileRefresherChore;
024import org.apache.hadoop.hbase.testclassification.IntegrationTests;
025import org.apache.hadoop.hbase.zookeeper.ZKUtil;
026import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
027import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
028import org.junit.AfterClass;
029import org.junit.BeforeClass;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
034
035/**
036 * An integration test that starts the cluster with three replicas for the meta It then creates a
037 * table, flushes the meta, kills the server holding the primary. After that a client issues put/get
038 * requests on the created table - the other replicas of the meta would be used to get the location
039 * of the region of the created table.
040 */
041@Category(IntegrationTests.class)
042public class IntegrationTestMetaReplicas {
043
044  /**
045   * Util to get at the cluster.
046   */
047  private static IntegrationTestingUtility util;
048
049  @BeforeClass
050  public static void setUp() throws Exception {
051    // Set up the integration test util
052    if (util == null) {
053      util = new IntegrationTestingUtility();
054    }
055    util.getConfiguration().setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD,
056      1000);
057    // Make sure there are three servers.
058    util.initializeCluster(3);
059    HBaseTestingUtil.setReplicas(util.getAdmin(), TableName.META_TABLE_NAME, 3);
060    ZKWatcher zkw = util.getZooKeeperWatcher();
061    Configuration conf = util.getConfiguration();
062    String baseZNode =
063      conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
064    String primaryMetaZnode =
065      ZNodePaths.joinZNode(baseZNode, conf.get("zookeeper.znode.metaserver", "meta-region-server"));
066    // check that the data in the znode is parseable (this would also mean the znode exists)
067    byte[] data = ZKUtil.getData(zkw, primaryMetaZnode);
068    ProtobufUtil.toServerName(data);
069    waitUntilZnodeAvailable(1);
070    waitUntilZnodeAvailable(2);
071  }
072
073  @AfterClass
074  public static void teardown() throws Exception {
075    // Clean everything up.
076    util.restoreCluster();
077    util = null;
078  }
079
080  private static void waitUntilZnodeAvailable(int replicaId) throws Exception {
081    String znode = util.getZooKeeperWatcher().getZNodePaths().getZNodeForReplica(replicaId);
082    int i = 0;
083    while (i < 1000) {
084      if (ZKUtil.checkExists(util.getZooKeeperWatcher(), znode) == -1) {
085        Thread.sleep(100);
086        i++;
087      } else break;
088    }
089    if (i == 1000) throw new IOException("znode for meta replica " + replicaId + " not available");
090  }
091
092  @Test
093  public void testShutdownHandling() throws Exception {
094    // This test creates a table, flushes the meta (with 3 replicas), kills the
095    // server holding the primary meta replica. Then it does a put/get into/from
096    // the test table. The put/get operations would use the replicas to locate the
097    // location of the test table's region
098    TestMetaWithReplicasShutdownHandling.shutdownMetaAndDoValidations(util);
099  }
100
101  public static void main(String[] args) throws Exception {
102    setUp();
103    new IntegrationTestMetaReplicas().testShutdownHandling();
104    teardown();
105  }
106}