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.regionserver;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.mockito.Mockito.mock;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.regionserver.compactions.CompactionLifeCycleTracker;
027import org.apache.hadoop.hbase.security.User;
028import org.apache.hadoop.hbase.testclassification.RegionServerTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.jupiter.api.AfterEach;
031import org.junit.jupiter.api.BeforeEach;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034
035@Tag(RegionServerTests.TAG)
036@Tag(SmallTests.TAG)
037public class TestCompactSplitReadOnly {
038
039  private CompactSplit compactSplit;
040  private Configuration conf;
041
042  @BeforeEach
043  public void setUp() {
044    conf = new Configuration();
045    // enable read-only mode
046    conf.setBoolean(HConstants.HBASE_GLOBAL_READONLY_ENABLED_KEY, true);
047    // create CompactSplit with conf-only constructor (available for tests)
048    compactSplit = new CompactSplit(conf);
049  }
050
051  @AfterEach
052  public void tearDown() {
053    // ensure thread pools are shutdown to avoid leakage
054    compactSplit.interruptIfNecessary();
055  }
056
057  @Test
058  public void testRequestSystemCompactionIgnoredWhenReadOnly() throws IOException {
059    // Mock HRegion and HStore minimal behavior
060    HRegion region = mock(HRegion.class);
061    HStore store = mock(HStore.class);
062
063    // Ensure compaction queues start empty
064    assertEquals(0, compactSplit.getCompactionQueueSize());
065
066    // Call requestSystemCompaction for a single store (selectNow = false)
067    compactSplit.requestSystemCompaction(region, store, "test-readonly");
068
069    // Because read-only mode is enabled, no compaction should be queued
070    assertEquals(0, compactSplit.getCompactionQueueSize());
071  }
072
073  @Test
074  public void testSelectCompactionIgnoredWhenReadOnly() throws IOException {
075    HRegion region = mock(HRegion.class);
076    HStore store = mock(HStore.class);
077
078    // Ensure compaction queues start empty
079    assertEquals(0, compactSplit.getCompactionQueueSize());
080
081    // Call requestCompaction which uses selectNow = true and should call selectCompaction
082    compactSplit.requestCompaction(region, store, "test-select-readonly", 0,
083      CompactionLifeCycleTracker.DUMMY, (User) null);
084
085    // Because read-only mode is enabled, selectCompaction should be skipped and no task queued
086    assertEquals(0, compactSplit.getCompactionQueueSize());
087  }
088}