2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.master
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertFalse
;
23 import java
.io
.IOException
;
24 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
25 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
26 import org
.apache
.hadoop
.hbase
.NamespaceDescriptor
;
27 import org
.apache
.hadoop
.hbase
.StartMiniClusterOption
;
28 import org
.apache
.hadoop
.hbase
.TableName
;
29 import org
.apache
.hadoop
.hbase
.client
.Put
;
30 import org
.apache
.hadoop
.hbase
.client
.Table
;
31 import org
.apache
.hadoop
.hbase
.client
.TableDescriptorBuilder
;
32 import org
.apache
.hadoop
.hbase
.master
.procedure
.AbstractStateMachineNamespaceProcedure
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
34 import org
.apache
.hadoop
.hbase
.testclassification
.MasterTests
;
35 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
36 import org
.apache
.hadoop
.hbase
.util
.JVMClusterUtil
.MasterThread
;
37 import org
.junit
.AfterClass
;
38 import org
.junit
.BeforeClass
;
39 import org
.junit
.ClassRule
;
40 import org
.junit
.Test
;
41 import org
.junit
.experimental
.categories
.Category
;
43 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.ProtobufUtil
;
46 * Testcase for HBASE-21154.
48 @Category({ MasterTests
.class, LargeTests
.class })
49 public class TestMigrateNamespaceTable
{
52 public static final HBaseClassTestRule CLASS_RULE
=
53 HBaseClassTestRule
.forClass(TestMigrateNamespaceTable
.class);
55 private static final HBaseTestingUtility UTIL
= new HBaseTestingUtility();
58 public static void setUp() throws Exception
{
59 StartMiniClusterOption option
= StartMiniClusterOption
.builder().numMasters(1).
60 numAlwaysStandByMasters(1).numRegionServers(1).build();
61 UTIL
.startMiniCluster(option
);
65 public static void tearDown() throws Exception
{
66 UTIL
.shutdownMiniCluster();
70 public void testMigrate() throws IOException
, InterruptedException
{
71 UTIL
.getAdmin().createTable(TableDescriptorBuilder
.NAMESPACE_TABLEDESC
);
72 try (Table table
= UTIL
.getConnection().getTable(TableName
.NAMESPACE_TABLE_NAME
)) {
73 for (int i
= 0; i
< 5; i
++) {
74 NamespaceDescriptor nd
= NamespaceDescriptor
.create("Test-NS-" + i
)
75 .addConfiguration("key-" + i
, "value-" + i
).build();
76 table
.put(new Put(Bytes
.toBytes(nd
.getName())).addColumn(
77 TableDescriptorBuilder
.NAMESPACE_FAMILY_INFO_BYTES
,
78 TableDescriptorBuilder
.NAMESPACE_COL_DESC_BYTES
,
79 ProtobufUtil
.toProtoNamespaceDescriptor(nd
).toByteArray()));
80 AbstractStateMachineNamespaceProcedure
81 .createDirectory(UTIL
.getMiniHBaseCluster().getMaster().getMasterFileSystem(), nd
);
84 MasterThread masterThread
= UTIL
.getMiniHBaseCluster().getMasterThread();
85 masterThread
.getMaster().stop("For testing");
87 UTIL
.getMiniHBaseCluster().startMaster();
89 // 5 + default and system('hbase')
90 assertEquals(7, UTIL
.getAdmin().listNamespaceDescriptors().length
);
91 for (int i
= 0; i
< 5; i
++) {
92 NamespaceDescriptor nd
= UTIL
.getAdmin().getNamespaceDescriptor("Test-NS-" + i
);
93 assertEquals("Test-NS-" + i
, nd
.getName());
94 assertEquals(1, nd
.getConfiguration().size());
95 assertEquals("value-" + i
, nd
.getConfigurationValue("key-" + i
));
97 UTIL
.waitFor(30000, () -> UTIL
.getAdmin().isTableDisabled(TableName
.NAMESPACE_TABLE_NAME
));
99 masterThread
= UTIL
.getMiniHBaseCluster().getMasterThread();
100 masterThread
.getMaster().stop("For testing");
102 UTIL
.getMiniHBaseCluster().startMaster();
104 // make sure that we could still restart the cluster after disabling the namespace table.
105 assertEquals(7, UTIL
.getAdmin().listNamespaceDescriptors().length
);
107 // let's delete the namespace table
108 UTIL
.getAdmin().deleteTable(TableName
.NAMESPACE_TABLE_NAME
);
109 assertFalse(UTIL
.getAdmin().tableExists(TableName
.NAMESPACE_TABLE_NAME
));