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