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