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.master;
019
020import org.apache.hadoop.hbase.client.TableState;
021import org.apache.hadoop.hbase.exceptions.DeserializationException;
022import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
023import org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos;
024import org.apache.hadoop.hbase.testclassification.LargeTests;
025import org.apache.hadoop.hbase.testclassification.MasterTests;
026import org.apache.hadoop.hbase.zookeeper.ZKUtil;
027import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
028import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtility;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.zookeeper.KeeperException;
034import org.junit.After;
035import org.junit.Assert;
036import org.junit.Before;
037import org.junit.ClassRule;
038import org.junit.Rule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.junit.rules.TestName;
042
043import java.io.IOException;
044
045import static junit.framework.TestCase.assertTrue;
046
047/**
048 * Tests that table state is mirrored out to zookeeper for hbase-1.x clients.
049 * Also tests that table state gets migrated from zookeeper on master start.
050 */
051@Category({ MasterTests.class, LargeTests.class })
052public class TestMirroringTableStateManager {
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055      HBaseClassTestRule.forClass(TestMirroringTableStateManager.class);
056  @Rule
057  public TestName name = new TestName();
058
059  private final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
060
061  @Before
062  public void before() throws Exception {
063    TEST_UTIL.startMiniCluster();
064  }
065
066  @After
067  public void after() throws Exception {
068    TEST_UTIL.shutdownMiniCluster();
069  }
070
071  @Test
072  public void testMirroring() throws Exception {
073    final TableName tableName = TableName.valueOf(name.getMethodName());
074    TEST_UTIL.createTable(tableName, HConstants.CATALOG_FAMILY_STR);
075    ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
076    assertTrue(TableState.State.ENABLED.equals(getTableStateInZK(zkw, tableName)));
077    TEST_UTIL.getAdmin().disableTable(tableName);
078    assertTrue(TableState.State.DISABLED.equals(getTableStateInZK(zkw, tableName)));
079    TEST_UTIL.getAdmin().deleteTable(tableName);
080    assertTrue(getTableStateInZK(zkw, tableName) == null);
081  }
082
083  private TableState.State getTableStateInZK(ZKWatcher watcher, final TableName tableName)
084      throws KeeperException, IOException, InterruptedException {
085    String znode = ZNodePaths.joinZNode(watcher.getZNodePaths().tableZNode,
086            tableName.getNameAsString());
087    byte [] data = ZKUtil.getData(watcher, znode);
088    if (data == null || data.length <= 0) {
089      return null;
090    }
091    try {
092      ProtobufUtil.expectPBMagicPrefix(data);
093      ZooKeeperProtos.DeprecatedTableState.Builder builder =
094          ZooKeeperProtos.DeprecatedTableState.newBuilder();
095      int magicLen = ProtobufUtil.lengthOfPBMagic();
096      ProtobufUtil.mergeFrom(builder, data, magicLen, data.length - magicLen);
097      return TableState.State.valueOf(builder.getState().toString());
098    } catch (IOException e) {
099      KeeperException ke = new KeeperException.DataInconsistencyException();
100      ke.initCause(e);
101      throw ke;
102    } catch (DeserializationException e) {
103      throw ZKUtil.convert(e);
104    }
105  }
106}