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