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