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.HBaseClassTestRule;
021import org.apache.hadoop.hbase.HBaseTestingUtility;
022import org.apache.hadoop.hbase.HConstants;
023import org.apache.hadoop.hbase.MetaTableAccessor;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.TableState;
026import org.apache.hadoop.hbase.testclassification.LargeTests;
027import org.apache.hadoop.hbase.testclassification.MasterTests;
028import org.apache.hadoop.hbase.util.JVMClusterUtil;
029import org.apache.hadoop.hbase.util.Threads;
030import org.junit.After;
031import org.junit.Before;
032import org.junit.ClassRule;
033import org.junit.Rule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.junit.rules.TestName;
037
038
039import static junit.framework.TestCase.assertTrue;
040
041/**
042 * Tests the default table lock manager
043 */
044@Category({ MasterTests.class, LargeTests.class })
045public class TestTableStateManager {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestTableStateManager.class);
050
051  private final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
052
053  @Rule
054  public TestName name = new TestName();
055
056  @Before
057  public void before() throws Exception {
058    TEST_UTIL.startMiniCluster();
059  }
060
061  @After
062  public void after() throws Exception {
063    TEST_UTIL.shutdownMiniCluster();
064  }
065
066  @Test
067  public void testMigration() throws Exception {
068    final TableName tableName = TableName.valueOf(name.getMethodName());
069    TEST_UTIL.createTable(tableName, HConstants.CATALOG_FAMILY_STR);
070    TEST_UTIL.getAdmin().disableTable(tableName);
071    // Table is disabled. Now remove the DISABLED column from the hbase:meta for this table's
072    // region. We want to see if Master will read the DISABLED from zk and make use of it as
073    // though it were reading the zk table state written by a hbase-1.x cluster.
074    TableState state = MetaTableAccessor.getTableState(TEST_UTIL.getConnection(), tableName);
075    assertTrue("State=" + state, state.getState().equals(TableState.State.DISABLED));
076    MetaTableAccessor.deleteTableState(TEST_UTIL.getConnection(), tableName);
077    assertTrue(MetaTableAccessor.getTableState(TEST_UTIL.getConnection(), tableName) == null);
078    // Now kill Master so a new one can come up and run through the zk migration.
079    HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
080    master.stop("Restarting");
081    while (!master.isStopped()) {
082      Threads.sleep(1);
083    }
084    assertTrue(master.isStopped());
085    JVMClusterUtil.MasterThread newMasterThread = TEST_UTIL.getMiniHBaseCluster().startMaster();
086    master = newMasterThread.getMaster();
087    while (!master.isInitialized()) {
088      Threads.sleep(1);
089    }
090    assertTrue(MetaTableAccessor.getTableState(TEST_UTIL.getConnection(),
091        tableName).getState().equals(TableState.State.DISABLED));
092  }
093}