View Javadoc

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.master;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.hbase.HBaseTestingUtility;
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.client.TableState;
27  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
28  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
29  import org.apache.hadoop.hbase.testclassification.LargeTests;
30  import org.apache.hadoop.hbase.testclassification.MasterTests;
31  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
32  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
33  import org.apache.zookeeper.KeeperException;
34  import org.junit.After;
35  import org.junit.Assert;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  /**
40   * Tests the default table lock manager
41   */
42  @Category({ MasterTests.class, LargeTests.class })
43  public class TestTableStateManager {
44  
45    private final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46  
47    @After
48    public void tearDown() throws Exception {
49      TEST_UTIL.shutdownMiniCluster();
50    }
51  
52    @Test(timeout = 60000)
53    public void testUpgradeFromZk() throws Exception {
54      TableName tableName =
55          TableName.valueOf("testUpgradeFromZk");
56      TEST_UTIL.startMiniCluster(2, 1);
57      TEST_UTIL.shutdownMiniHBaseCluster();
58      ZooKeeperWatcher watcher = TEST_UTIL.getZooKeeperWatcher();
59      setTableStateInZK(watcher, tableName, ZooKeeperProtos.DeprecatedTableState.State.DISABLED);
60      TEST_UTIL.restartHBaseCluster(1);
61  
62      HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
63      Assert.assertEquals(
64          master.getTableStateManager().getTableState(tableName),
65          TableState.State.DISABLED);
66    }
67  
68    private void setTableStateInZK(ZooKeeperWatcher watcher, final TableName tableName,
69        final ZooKeeperProtos.DeprecatedTableState.State state)
70        throws KeeperException, IOException {
71      String znode = ZKUtil.joinZNode(watcher.tableZNode, tableName.getNameAsString());
72      if (ZKUtil.checkExists(watcher, znode) == -1) {
73        ZKUtil.createAndFailSilent(watcher, znode);
74      }
75      ZooKeeperProtos.DeprecatedTableState.Builder builder =
76          ZooKeeperProtos.DeprecatedTableState.newBuilder();
77      builder.setState(state);
78      byte[] data = ProtobufUtil.prependPBMagic(builder.build().toByteArray());
79      ZKUtil.setData(watcher, znode, data);
80    }
81  
82  }