1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.zookeeper;
21  
22  
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.*;
30  import org.apache.hadoop.hbase.zookeeper.ZKTableReadOnly;
31  import org.junit.AfterClass;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  @Category(MediumTests.class)
37  public class TestZKTableReadOnly {
38    private static final Log LOG = LogFactory.getLog(TestZooKeeperNodeTracker.class);
39    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
40  
41    @BeforeClass
42    public static void setUpBeforeClass() throws Exception {
43      TEST_UTIL.startMiniZKCluster();
44    }
45  
46    @AfterClass
47    public static void tearDownAfterClass() throws Exception {
48      TEST_UTIL.shutdownMiniZKCluster();
49    }
50  
51    Abortable abortable = new Abortable() {
52      @Override
53      public void abort(String why, Throwable e) {
54        LOG.info(why, e);
55      }
56  
57      @Override
58      public boolean isAborted() {
59        return false;
60      }
61    };
62  
63    private boolean enableAndCheckEnabled(ZooKeeperWatcher zkw, String tableName) throws Exception {
64      // set the table to enabled, as that is the only state that differs
65      // between the two formats
66      ZKTable zkt = new ZKTable(zkw);
67      zkt.setEnabledTable(tableName);
68      return ZKTableReadOnly.isEnabledTable(zkw, tableName);
69    }
70  
71    private void runClientCompatiblityWith92ZNodeTest(String tableName, Configuration conf)
72    throws Exception {
73      ZooKeeperWatcher zkw = new ZooKeeperWatcher(conf,
74        tableName, abortable, true);
75      assertTrue(enableAndCheckEnabled(zkw, tableName));
76    }
77    /**
78     * Test that client ZK reader can handle the 0.92 table format znode.
79     */
80    @Test
81    public void testClientCompatibilityWith92ZNode() throws Exception {
82      // test without useMulti
83      String tableName = "testClientCompatibilityWith92ZNode";
84      // Set the client to read from the 0.92 table znode format
85      Configuration conf = HBaseConfiguration.create(TEST_UTIL.getConfiguration());
86      String znode92 = conf.get("zookeeper.znode.masterTableEnableDisable92", "table92");
87      conf.set("zookeeper.znode.clientTableEnableDisable", znode92);
88      runClientCompatiblityWith92ZNodeTest(tableName, conf);
89  
90      // test with useMulti
91      tableName = "testClientCompatibilityWith92ZNodeUseMulti";
92      conf.setBoolean(HConstants.ZOOKEEPER_USEMULTI, true);
93      runClientCompatiblityWith92ZNodeTest(tableName, conf);
94    }
95  
96    private void runClientCompatibilityWith94ZNodeTest(String tableName, Configuration conf)
97    throws Exception {
98      ZooKeeperWatcher zkw = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(),
99        tableName, abortable, true);
100     assertTrue(enableAndCheckEnabled(zkw, tableName));
101   }
102 
103   /**
104    * Test that client ZK reader can handle the current (0.94) table format znode.
105    */
106   @Test
107   public void testClientCompatibilityWith94ZNode() throws Exception {
108     String tableName = "testClientCompatibilityWith94ZNode";
109 
110     // without useMulti
111     runClientCompatibilityWith94ZNodeTest(tableName, TEST_UTIL.getConfiguration());
112 
113     // with useMulti
114     tableName = "testClientCompatiblityWith94ZNodeUseMulti";
115     Configuration conf = HBaseConfiguration.create(TEST_UTIL.getConfiguration());
116     conf.setBoolean(HConstants.ZOOKEEPER_USEMULTI, true);
117     runClientCompatibilityWith94ZNodeTest(tableName, conf);
118   }
119 
120   @org.junit.Rule
121   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
122     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
123 }