View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.client.TestMetaWithReplicas;
26  import org.apache.hadoop.hbase.regionserver.StorefileRefresherChore;
27  import org.apache.hadoop.hbase.testclassification.IntegrationTests;
28  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
29  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
30  import org.apache.hadoop.util.ToolRunner;
31  import org.junit.AfterClass;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  /**
37   * An integration test that starts the cluster with three replicas for the meta
38   * It then creates a table, flushes the meta, kills the server holding the primary.
39   * After that a client issues put/get requests on the created table - the other 
40   * replicas of the meta would be used to get the location of the region of the created
41   * table.
42   */
43  @Category(IntegrationTests.class)
44  public class IntegrationTestMetaReplicas {
45    static final Log LOG = LogFactory.getLog(IntegrationTestMetaReplicas.class);
46    /**
47     * Util to get at the cluster.
48     */
49    private static IntegrationTestingUtility util;
50  
51    @BeforeClass
52    public static void setUp() throws Exception {
53      // Set up the integration test util
54      if (util == null) {
55        util = new IntegrationTestingUtility();
56      }
57      util.getConfiguration().setInt(HConstants.META_REPLICAS_NUM, 3);
58      util.getConfiguration().setInt(
59          StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, 1000);
60      // Make sure there are three servers.
61      util.initializeCluster(3);
62      ZooKeeperWatcher zkw = util.getZooKeeperWatcher();
63      Configuration conf = util.getConfiguration();
64      String baseZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT,
65          HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
66      String primaryMetaZnode = ZKUtil.joinZNode(baseZNode,
67          conf.get("zookeeper.znode.metaserver", "meta-region-server"));
68      // check that the data in the znode is parseable (this would also mean the znode exists)
69      byte[] data = ZKUtil.getData(zkw, primaryMetaZnode);
70      ServerName.parseFrom(data);
71      waitUntilZnodeAvailable(1);
72      waitUntilZnodeAvailable(2);
73    }
74  
75    @AfterClass
76    public static void teardown() throws Exception {
77      //Clean everything up.
78      util.restoreCluster();
79      util = null;
80    }
81  
82    private static void waitUntilZnodeAvailable(int replicaId) throws Exception {
83      String znode = util.getZooKeeperWatcher().getZNodeForReplica(replicaId);
84      int i = 0;
85      while (i < 1000) {
86        if (ZKUtil.checkExists(util.getZooKeeperWatcher(), znode) == -1) {
87          Thread.sleep(100);
88          i++;
89        } else break;
90      }
91      if (i == 1000) throw new IOException("znode for meta replica " + replicaId + " not available");
92    }
93  
94    @Test
95    public void testShutdownHandling() throws Exception {
96      // This test creates a table, flushes the meta (with 3 replicas), kills the
97      // server holding the primary meta replica. Then it does a put/get into/from
98      // the test table. The put/get operations would use the replicas to locate the
99      // location of the test table's region
100     TestMetaWithReplicas.shutdownMetaAndDoValidations(util);
101   }
102 
103   public static void main(String[] args) throws Exception {
104     setUp();
105     new IntegrationTestMetaReplicas().testShutdownHandling();
106     teardown();
107   }
108 }