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 org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022
023import java.io.IOException;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.NamespaceDescriptor;
027import org.apache.hadoop.hbase.StartTestingClusterOption;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Put;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
032import org.apache.hadoop.hbase.master.procedure.AbstractStateMachineNamespaceProcedure;
033import org.apache.hadoop.hbase.testclassification.LargeTests;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
044
045/**
046 * Testcase for HBASE-21154.
047 */
048@Category({ MasterTests.class, LargeTests.class })
049public class TestMigrateNamespaceTable {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestMigrateNamespaceTable.class);
054
055  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
056
057  @BeforeClass
058  public static void setUp() throws Exception {
059    StartTestingClusterOption option = StartTestingClusterOption.builder().numMasters(1).
060        numAlwaysStandByMasters(1).numRegionServers(1).build();
061    UTIL.startMiniCluster(option);
062  }
063
064  @AfterClass
065  public static void tearDown() throws Exception {
066    UTIL.shutdownMiniCluster();
067  }
068
069  @Test
070  public void testMigrate() throws IOException, InterruptedException {
071    UTIL.getAdmin().createTable(TableDescriptorBuilder.NAMESPACE_TABLEDESC);
072    try (Table table = UTIL.getConnection().getTable(TableName.NAMESPACE_TABLE_NAME)) {
073      for (int i = 0; i < 5; i++) {
074        NamespaceDescriptor nd = NamespaceDescriptor.create("Test-NS-" + i)
075          .addConfiguration("key-" + i, "value-" + i).build();
076        table.put(new Put(Bytes.toBytes(nd.getName())).addColumn(
077          TableDescriptorBuilder.NAMESPACE_FAMILY_INFO_BYTES,
078          TableDescriptorBuilder.NAMESPACE_COL_DESC_BYTES,
079          ProtobufUtil.toProtoNamespaceDescriptor(nd).toByteArray()));
080        AbstractStateMachineNamespaceProcedure
081          .createDirectory(UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem(), nd);
082      }
083    }
084    MasterThread masterThread = UTIL.getMiniHBaseCluster().getMasterThread();
085    masterThread.getMaster().stop("For testing");
086    masterThread.join();
087    UTIL.getMiniHBaseCluster().startMaster();
088
089    // 5 + default and system('hbase')
090    assertEquals(7, UTIL.getAdmin().listNamespaceDescriptors().length);
091    for (int i = 0; i < 5; i++) {
092      NamespaceDescriptor nd = UTIL.getAdmin().getNamespaceDescriptor("Test-NS-" + i);
093      assertEquals("Test-NS-" + i, nd.getName());
094      assertEquals(1, nd.getConfiguration().size());
095      assertEquals("value-" + i, nd.getConfigurationValue("key-" + i));
096    }
097    UTIL.waitFor(30000, () -> UTIL.getAdmin().isTableDisabled(TableName.NAMESPACE_TABLE_NAME));
098
099    masterThread = UTIL.getMiniHBaseCluster().getMasterThread();
100    masterThread.getMaster().stop("For testing");
101    masterThread.join();
102    UTIL.getMiniHBaseCluster().startMaster();
103
104    // make sure that we could still restart the cluster after disabling the namespace table.
105    assertEquals(7, UTIL.getAdmin().listNamespaceDescriptors().length);
106
107    // let's delete the namespace table
108    UTIL.getAdmin().deleteTable(TableName.NAMESPACE_TABLE_NAME);
109    assertFalse(UTIL.getAdmin().tableExists(TableName.NAMESPACE_TABLE_NAME));
110  }
111}