anmolnar commented on code in PR #8044: URL: https://github.com/apache/hbase/pull/8044#discussion_r3087477718
########## hbase-server/src/test/java/org/apache/hadoop/hbase/security/access/TestReadOnlyController.java: ########## @@ -0,0 +1,217 @@ +/* + * 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.security.access; + +import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_RETRIES_NUMBER; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtil; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.SingleProcessHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.ConnectionFactory; +import org.apache.hadoop.hbase.client.Delete; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Row; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.master.HMaster; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.SecurityTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.ExpectedException; +import org.junit.rules.TestName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Category({ SecurityTests.class, LargeTests.class }) +@SuppressWarnings("deprecation") +public class TestReadOnlyController { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestReadOnlyController.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestReadOnlyController.class); + private final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); + private static final TableName TEST_TABLE = TableName.valueOf("read_only_test_table"); + private static final byte[] TEST_FAMILY = Bytes.toBytes("read_only_table_col_fam"); + private static HRegionServer hRegionServer; + private static HMaster hMaster; + private static Configuration conf; + private static Connection connection; + private static SingleProcessHBaseCluster cluster; + + private static Table testTable; + @Rule + public TestName name = new TestName(); + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Before + public void beforeClass() throws Exception { + conf = TEST_UTIL.getConfiguration(); + + // Shorten the run time of failed unit tests by limiting retries and the session timeout + // threshold + conf.setInt(HBASE_CLIENT_RETRIES_NUMBER, 1); + conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 1000); + + // Set up test class with Read-Only mode disabled so a table can be created + conf.setBoolean(HConstants.HBASE_GLOBAL_READONLY_ENABLED_KEY, false); + + try { + // Start the test cluster + cluster = TEST_UTIL.startMiniCluster(1); + + hMaster = cluster.getMaster(); + hRegionServer = cluster.getRegionServerThreads().get(0).getRegionServer(); + connection = ConnectionFactory.createConnection(conf); + + // Create a test table + testTable = TEST_UTIL.createTable(TEST_TABLE, TEST_FAMILY); + } catch (Exception e) { + // Delete the created table, and clean up the connection and cluster before throwing an + // exception + disableReadOnlyMode(); + TEST_UTIL.deleteTable(TEST_TABLE); + connection.close(); + TEST_UTIL.shutdownMiniCluster(); + throw new RuntimeException(e); + } + } + + @After + public void afterClass() throws Exception { + if (connection != null) { + connection.close(); + } + TEST_UTIL.shutdownMiniCluster(); + } + + private static void enableReadOnlyMode() { + // Dynamically enable Read-Only mode if it is not active + if (!isReadOnlyModeEnabled()) { + LOG.info("Dynamically enabling Read-Only mode by setting {} to true", + HConstants.HBASE_GLOBAL_READONLY_ENABLED_DEFAULT); + conf.setBoolean(HConstants.HBASE_GLOBAL_READONLY_ENABLED_KEY, true); + notifyObservers(); + } + } + + private static void disableReadOnlyMode() { + // Dynamically disable Read-Only mode if it is active + if (isReadOnlyModeEnabled()) { + LOG.info("Dynamically disabling Read-Only mode by setting {} to false", + HConstants.HBASE_GLOBAL_READONLY_ENABLED_DEFAULT); + conf.setBoolean(HConstants.HBASE_GLOBAL_READONLY_ENABLED_KEY, false); + notifyObservers(); + } + } + + private static boolean isReadOnlyModeEnabled() { + return conf.getBoolean(HConstants.HBASE_GLOBAL_READONLY_ENABLED_KEY, + HConstants.HBASE_GLOBAL_READONLY_ENABLED_DEFAULT); + } Review Comment: Why do you want to remove this? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
