|
| 1 | +/* |
| 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 |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 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. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.security.access; |
| 19 | + |
| 20 | +import static org.apache.hadoop.hbase.HConstants.HBASE_CLIENT_RETRIES_NUMBER; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import org.apache.hadoop.conf.Configuration; |
| 27 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 28 | +import org.apache.hadoop.hbase.HConstants; |
| 29 | +import org.apache.hadoop.hbase.SingleProcessHBaseCluster; |
| 30 | +import org.apache.hadoop.hbase.TableName; |
| 31 | +import org.apache.hadoop.hbase.client.Admin; |
| 32 | +import org.apache.hadoop.hbase.client.Connection; |
| 33 | +import org.apache.hadoop.hbase.client.ConnectionFactory; |
| 34 | +import org.apache.hadoop.hbase.client.Put; |
| 35 | +import org.apache.hadoop.hbase.client.Table; |
| 36 | +import org.apache.hadoop.hbase.master.HMaster; |
| 37 | +import org.apache.hadoop.hbase.regionserver.HRegionServer; |
| 38 | +import org.apache.hadoop.hbase.testclassification.LargeTests; |
| 39 | +import org.apache.hadoop.hbase.testclassification.SecurityTests; |
| 40 | +import org.apache.hadoop.hbase.util.Bytes; |
| 41 | +import org.junit.jupiter.api.AfterEach; |
| 42 | +import org.junit.jupiter.api.BeforeEach; |
| 43 | +import org.junit.jupiter.api.Tag; |
| 44 | +import org.junit.jupiter.api.Test; |
| 45 | +import org.junit.jupiter.api.Timeout; |
| 46 | + |
| 47 | +@Tag(SecurityTests.TAG) |
| 48 | +@Tag(LargeTests.TAG) |
| 49 | +@SuppressWarnings("deprecation") |
| 50 | +public class TestReadOnlyControllerFlush { |
| 51 | + |
| 52 | + private final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); |
| 53 | + private static final TableName TEST_TABLE = TableName.valueOf("read_only_flush_test_table"); |
| 54 | + private static final byte[] TEST_FAMILY = Bytes.toBytes("read_only_flush_col_fam"); |
| 55 | + private static HRegionServer hRegionServer; |
| 56 | + private static HMaster hMaster; |
| 57 | + private static Configuration conf; |
| 58 | + private static Connection connection; |
| 59 | + private static SingleProcessHBaseCluster cluster; |
| 60 | + |
| 61 | + private static Table testTable; |
| 62 | + |
| 63 | + @BeforeEach |
| 64 | + public void beforeClass() throws Exception { |
| 65 | + conf = TEST_UTIL.getConfiguration(); |
| 66 | + |
| 67 | + // Shorten the run time of failed unit tests by limiting retries and the session timeout |
| 68 | + // threshold |
| 69 | + conf.setInt(HBASE_CLIENT_RETRIES_NUMBER, 1); |
| 70 | + conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 1000); |
| 71 | + |
| 72 | + // Set up test class with Read-Only mode disabled so a table can be created |
| 73 | + conf.setBoolean(HConstants.HBASE_GLOBAL_READONLY_ENABLED_KEY, false); |
| 74 | + |
| 75 | + try { |
| 76 | + // Start the test cluster |
| 77 | + cluster = TEST_UTIL.startMiniCluster(1); |
| 78 | + |
| 79 | + hMaster = cluster.getMaster(); |
| 80 | + hRegionServer = cluster.getRegionServerThreads().get(0).getRegionServer(); |
| 81 | + connection = ConnectionFactory.createConnection(conf); |
| 82 | + |
| 83 | + // Create a test table and insert a row so the memstore has data to flush |
| 84 | + testTable = TEST_UTIL.createTable(TEST_TABLE, TEST_FAMILY); |
| 85 | + Put put = new Put(Bytes.toBytes("row1")); |
| 86 | + put.addColumn(TEST_FAMILY, null, Bytes.toBytes("value1")); |
| 87 | + testTable.put(put); |
| 88 | + } catch (Exception e) { |
| 89 | + disableReadOnlyMode(); |
| 90 | + TEST_UTIL.deleteTable(TEST_TABLE); |
| 91 | + if (connection != null) { |
| 92 | + connection.close(); |
| 93 | + } |
| 94 | + TEST_UTIL.shutdownMiniCluster(); |
| 95 | + throw new RuntimeException(e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @AfterEach |
| 100 | + public void afterClass() throws Exception { |
| 101 | + if (connection != null) { |
| 102 | + connection.close(); |
| 103 | + } |
| 104 | + TEST_UTIL.shutdownMiniCluster(); |
| 105 | + } |
| 106 | + |
| 107 | + private static void enableReadOnlyMode() { |
| 108 | + SecureTestUtil.enableReadOnlyMode(conf, hMaster, hRegionServer); |
| 109 | + } |
| 110 | + |
| 111 | + private static void disableReadOnlyMode() { |
| 112 | + SecureTestUtil.disableReadOnlyMode(conf, hMaster, hRegionServer); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testFlushTableWithReadOnlyDisabled() throws IOException { |
| 117 | + disableReadOnlyMode(); |
| 118 | + try (Admin admin = TEST_UTIL.getAdmin()) { |
| 119 | + admin.flush(TEST_TABLE); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + @Timeout(value = 60, unit = TimeUnit.SECONDS) |
| 125 | + public void testCannotFlushTableWithReadOnlyEnabled() throws IOException { |
| 126 | + enableReadOnlyMode(); |
| 127 | + try (Admin admin = TEST_UTIL.getAdmin()) { |
| 128 | + IOException exception = assertThrows(IOException.class, () -> { |
| 129 | + admin.flush(TEST_TABLE); |
| 130 | + }); |
| 131 | + assertTrue(exception.getMessage().contains("Operation not allowed in Read-Only Mode")); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments