View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.zookeeper;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.apache.hadoop.hbase.Abortable;
27  import org.apache.hadoop.hbase.CoordinatedStateException;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.testclassification.MediumTests;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.TableStateManager;
32  import org.apache.zookeeper.KeeperException;
33  import org.junit.AfterClass;
34  import org.junit.BeforeClass;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  import static org.junit.Assert.assertFalse;
39  import static org.junit.Assert.assertTrue;
40  import static org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos.Table;
41  
42  @Category(MediumTests.class)
43  public class TestZKTableStateManager {
44    private static final Log LOG = LogFactory.getLog(TestZKTableStateManager.class);
45    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46  
47    @BeforeClass
48    public static void setUpBeforeClass() throws Exception {
49      TEST_UTIL.startMiniZKCluster();
50    }
51  
52    @AfterClass
53    public static void tearDownAfterClass() throws Exception {
54      TEST_UTIL.shutdownMiniZKCluster();
55    }
56  
57    @Test
58    public void testTableStates()
59        throws CoordinatedStateException, IOException, KeeperException, InterruptedException {
60      final TableName name =
61          TableName.valueOf("testDisabled");
62      Abortable abortable = new Abortable() {
63        @Override
64        public void abort(String why, Throwable e) {
65          LOG.info(why, e);
66        }
67  
68        @Override
69        public boolean isAborted() {
70          return false;
71        }
72  
73      };
74      ZooKeeperWatcher zkw = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(),
75        name.getNameAsString(), abortable, true);
76      TableStateManager zkt = new ZKTableStateManager(zkw);
77      assertFalse(zkt.isTableState(name, Table.State.ENABLED));
78      assertFalse(zkt.isTableState(name, Table.State.DISABLING));
79      assertFalse(zkt.isTableState(name, Table.State.DISABLED));
80      assertFalse(zkt.isTableState(name, Table.State.ENABLING));
81      assertFalse(zkt.isTableState(name, Table.State.DISABLED, Table.State.DISABLING));
82      assertFalse(zkt.isTableState(name, Table.State.DISABLED, Table.State.ENABLING));
83      assertFalse(zkt.isTablePresent(name));
84      zkt.setTableState(name, Table.State.DISABLING);
85      assertTrue(zkt.isTableState(name, Table.State.DISABLING));
86      assertTrue(zkt.isTableState(name, Table.State.DISABLED, Table.State.DISABLING));
87      assertFalse(zkt.getTablesInStates(Table.State.DISABLED).contains(name));
88      assertTrue(zkt.isTablePresent(name));
89      zkt.setTableState(name, Table.State.DISABLED);
90      assertTrue(zkt.isTableState(name, Table.State.DISABLED));
91      assertTrue(zkt.isTableState(name, Table.State.DISABLED, Table.State.DISABLING));
92      assertFalse(zkt.isTableState(name, Table.State.DISABLING));
93      assertTrue(zkt.getTablesInStates(Table.State.DISABLED).contains(name));
94      assertTrue(zkt.isTablePresent(name));
95      zkt.setTableState(name, Table.State.ENABLING);
96      assertTrue(zkt.isTableState(name, Table.State.ENABLING));
97      assertTrue(zkt.isTableState(name, Table.State.DISABLED, Table.State.ENABLING));
98      assertFalse(zkt.isTableState(name, Table.State.DISABLED));
99      assertFalse(zkt.getTablesInStates(Table.State.DISABLED).contains(name));
100     assertTrue(zkt.isTablePresent(name));
101     zkt.setTableState(name, Table.State.ENABLED);
102     assertTrue(zkt.isTableState(name, Table.State.ENABLED));
103     assertFalse(zkt.isTableState(name, Table.State.ENABLING));
104     assertTrue(zkt.isTablePresent(name));
105     zkt.setDeletedTable(name);
106     assertFalse(zkt.isTableState(name, Table.State.ENABLED));
107     assertFalse(zkt.isTableState(name, Table.State.DISABLING));
108     assertFalse(zkt.isTableState(name, Table.State.DISABLED));
109     assertFalse(zkt.isTableState(name, Table.State.ENABLING));
110     assertFalse(zkt.isTableState(name, Table.State.DISABLED, Table.State.DISABLING));
111     assertFalse(zkt.isTableState(name, Table.State.DISABLED, Table.State.ENABLING));
112     assertFalse(zkt.isTablePresent(name));
113   }
114 }