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.master.region; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022import static org.mockito.ArgumentMatchers.anyBoolean; 023import static org.mockito.Mockito.mock; 024import static org.mockito.Mockito.when; 025 026import java.io.IOException; 027import java.util.Collections; 028import java.util.concurrent.TimeUnit; 029import java.util.concurrent.atomic.AtomicInteger; 030import java.util.concurrent.atomic.AtomicLong; 031import org.apache.hadoop.conf.Configuration; 032import org.apache.hadoop.fs.Path; 033import org.apache.hadoop.hbase.Abortable; 034import org.apache.hadoop.hbase.HBaseClassTestRule; 035import org.apache.hadoop.hbase.HBaseConfiguration; 036import org.apache.hadoop.hbase.TableName; 037import org.apache.hadoop.hbase.Waiter; 038import org.apache.hadoop.hbase.client.RegionInfoBuilder; 039import org.apache.hadoop.hbase.regionserver.HRegion; 040import org.apache.hadoop.hbase.regionserver.HStore; 041import org.apache.hadoop.hbase.testclassification.MasterTests; 042import org.apache.hadoop.hbase.testclassification.MediumTests; 043import org.junit.After; 044import org.junit.Before; 045import org.junit.ClassRule; 046import org.junit.Test; 047import org.junit.experimental.categories.Category; 048 049@Category({ MasterTests.class, MediumTests.class }) 050public class TestMasterRegionFlush { 051 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestMasterRegionFlush.class); 055 056 private Configuration conf; 057 058 private HRegion region; 059 060 private MasterRegionFlusherAndCompactor flusher; 061 062 private AtomicInteger flushCalled; 063 064 private AtomicLong memstoreHeapSize; 065 066 private AtomicLong memstoreOffHeapSize; 067 068 @Before 069 public void setUp() throws IOException { 070 conf = HBaseConfiguration.create(); 071 region = mock(HRegion.class); 072 HStore store = mock(HStore.class); 073 when(store.getStorefilesCount()).thenReturn(1); 074 when(region.getStores()).thenReturn(Collections.singletonList(store)); 075 when(region.getRegionInfo()) 076 .thenReturn(RegionInfoBuilder.newBuilder(TableName.valueOf("hbase:local")).build()); 077 flushCalled = new AtomicInteger(0); 078 memstoreHeapSize = new AtomicLong(0); 079 memstoreOffHeapSize = new AtomicLong(0); 080 when(region.getMemStoreHeapSize()).thenAnswer(invocation -> memstoreHeapSize.get()); 081 when(region.getMemStoreOffHeapSize()).thenAnswer(invocation -> memstoreOffHeapSize.get()); 082 when(region.flush(anyBoolean())).thenAnswer(invocation -> { 083 assertTrue(invocation.getArgument(0)); 084 memstoreHeapSize.set(0); 085 memstoreOffHeapSize.set(0); 086 flushCalled.incrementAndGet(); 087 return null; 088 }); 089 } 090 091 @After 092 public void tearDown() { 093 if (flusher != null) { 094 flusher.close(); 095 flusher = null; 096 } 097 } 098 099 private void initFlusher(long flushSize, long flushPerChanges, long flushIntervalMs) { 100 flusher = new MasterRegionFlusherAndCompactor(conf, new Abortable() { 101 102 @Override 103 public boolean isAborted() { 104 return false; 105 } 106 107 @Override 108 public void abort(String why, Throwable e) { 109 } 110 }, region, flushSize, flushPerChanges, flushIntervalMs, 4, new Path("/tmp"), ""); 111 } 112 113 @Test 114 public void testTriggerFlushBySize() throws IOException, InterruptedException { 115 initFlusher(1024 * 1024, 1_000_000, TimeUnit.MINUTES.toMillis(15)); 116 memstoreHeapSize.set(1000 * 1024); 117 flusher.onUpdate(); 118 Thread.sleep(1000); 119 assertEquals(0, flushCalled.get()); 120 memstoreOffHeapSize.set(1000 * 1024); 121 flusher.onUpdate(); 122 Waiter.waitFor(conf, 2000, () -> flushCalled.get() == 1); 123 } 124 125 private void assertTriggerFlushByChanges(int changes) throws InterruptedException { 126 int currentFlushCalled = flushCalled.get(); 127 for (int i = 0; i < changes; i++) { 128 flusher.onUpdate(); 129 } 130 Thread.sleep(1000); 131 assertEquals(currentFlushCalled, flushCalled.get()); 132 flusher.onUpdate(); 133 Waiter.waitFor(conf, 5000, () -> flushCalled.get() == currentFlushCalled + 1); 134 } 135 136 @Test 137 public void testTriggerFlushByChanges() throws InterruptedException { 138 initFlusher(128 * 1024 * 1024, 10, TimeUnit.MINUTES.toMillis(15)); 139 assertTriggerFlushByChanges(10); 140 assertTriggerFlushByChanges(10); 141 } 142 143 @Test 144 public void testPeriodicalFlush() throws InterruptedException { 145 initFlusher(128 * 1024 * 1024, 1_000_000, TimeUnit.SECONDS.toMillis(1)); 146 assertEquals(0, flushCalled.get()); 147 Thread.sleep(1500); 148 assertEquals(1, flushCalled.get()); 149 Thread.sleep(1000); 150 assertEquals(2, flushCalled.get()); 151 152 } 153}