From 7b929329203240f4942b5818db486229e38776d6 Mon Sep 17 00:00:00 2001 From: ChandraSekhar K Date: Sun, 21 Apr 2024 19:57:29 +0530 Subject: [PATCH] HBASE-28239 Auto create configured namespaces --- .../master/procedure/InitMetaProcedure.java | 16 ++++ .../TestAdditionalNameSpaceCreation.java | 81 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/TestAdditionalNameSpaceCreation.java diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/InitMetaProcedure.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/InitMetaProcedure.java index a23f11a2e875..095578af709a 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/InitMetaProcedure.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/InitMetaProcedure.java @@ -28,6 +28,7 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.RegionInfoBuilder; import org.apache.hadoop.hbase.client.TableDescriptor; @@ -57,6 +58,8 @@ public class InitMetaProcedure extends AbstractStateMachineTableProcedure 0) { + Arrays.stream(customNamespaces).forEach(ns -> { + try { + insertNamespaceToMeta(env.getMasterServices().getConnection(), + NamespaceDescriptor.create(ns).build()); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } return Flow.NO_MORE_STATE; default: diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestAdditionalNameSpaceCreation.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestAdditionalNameSpaceCreation.java new file mode 100644 index 000000000000..7f05cbce559a --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestAdditionalNameSpaceCreation.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase; + +import static org.apache.hadoop.hbase.master.procedure.InitMetaProcedure.ADDITIONAL_NAMESPACES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.master.HMaster; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TestName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Category({ MiscTests.class, MediumTests.class }) +public class TestAdditionalNameSpaceCreation { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestAdditionalNameSpaceCreation.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestAdditionalNameSpaceCreation.class); + private static HMaster master; + protected final static int NUM_SLAVES_BASE = 4; + private static HBaseTestingUtil TEST_UTIL; + protected static Admin admin; + protected static HBaseClusterInterface cluster; + + @Rule + public TestName name = new TestName(); + + @BeforeClass + public static void setUp() throws Exception { + TEST_UTIL = new HBaseTestingUtil(); + TEST_UTIL.getConfiguration().setStrings(ADDITIONAL_NAMESPACES, "ns1", "ns2"); + TEST_UTIL.startMiniCluster(NUM_SLAVES_BASE); + admin = TEST_UTIL.getAdmin(); + cluster = TEST_UTIL.getHBaseCluster(); + master = ((SingleProcessHBaseCluster) cluster).getMaster(); + LOG.info("Done initializing cluster"); + } + + @AfterClass + public static void tearDown() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Test + public void testCustomNameSpaceCreationDuringStartup() throws IOException { + List namespaces = Arrays.asList(admin.listNamespaces()); + assertEquals(4, namespaces.size()); + assertTrue(namespaces.contains("ns1")); + assertTrue(namespaces.contains("ns2")); + } +}