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.regionreplication;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022import static org.mockito.ArgumentMatchers.any;
023import static org.mockito.ArgumentMatchers.anyBoolean;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.times;
026import static org.mockito.Mockito.verify;
027import static org.mockito.Mockito.when;
028
029import java.io.IOException;
030import java.util.Arrays;
031import java.util.Optional;
032import java.util.concurrent.CountDownLatch;
033import org.apache.hadoop.conf.Configuration;
034import org.apache.hadoop.hbase.HBaseClassTestRule;
035import org.apache.hadoop.hbase.HBaseConfiguration;
036import org.apache.hadoop.hbase.TableName;
037import org.apache.hadoop.hbase.client.RegionInfo;
038import org.apache.hadoop.hbase.client.RegionInfoBuilder;
039import org.apache.hadoop.hbase.regionserver.HRegion;
040import org.apache.hadoop.hbase.regionserver.HRegion.FlushResultImpl;
041import org.apache.hadoop.hbase.regionserver.RegionServerServices;
042import org.apache.hadoop.hbase.testclassification.MediumTests;
043import org.apache.hadoop.hbase.testclassification.RegionServerTests;
044import org.junit.After;
045import org.junit.Before;
046import org.junit.ClassRule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049
050@Category({ RegionServerTests.class, MediumTests.class })
051public class TestRegionReplicationBufferManager {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestRegionReplicationBufferManager.class);
056
057  private Configuration conf;
058
059  private RegionServerServices rsServices;
060
061  private RegionReplicationBufferManager manager;
062
063  @Before
064  public void setUp() {
065    conf = HBaseConfiguration.create();
066    rsServices = mock(RegionServerServices.class);
067    when(rsServices.getConfiguration()).thenReturn(conf);
068  }
069
070  @After
071  public void tearDown() {
072    if (manager != null) {
073      manager.stop();
074    }
075  }
076
077  private HRegion mockRegion(RegionInfo regionInfo, long pendingSize) throws IOException {
078    HRegion region = mock(HRegion.class);
079    when(region.getRegionInfo()).thenReturn(regionInfo);
080    if (pendingSize < 0) {
081      when(region.getRegionReplicationSink()).thenReturn(Optional.empty());
082    } else {
083      RegionReplicationSink sink = mock(RegionReplicationSink.class);
084      when(sink.pendingSize()).thenReturn(pendingSize);
085      when(region.getRegionReplicationSink()).thenReturn(Optional.of(sink));
086    }
087    return region;
088  }
089
090  @Test
091  public void testScheduleFlush() throws IOException, InterruptedException {
092    conf.setLong(RegionReplicationBufferManager.MAX_PENDING_SIZE, 1024 * 1024);
093    manager = new RegionReplicationBufferManager(rsServices);
094    RegionInfo info1 = RegionInfoBuilder.newBuilder(TableName.valueOf("info1")).build();
095    RegionInfo info2 = RegionInfoBuilder.newBuilder(TableName.valueOf("info2")).build();
096    HRegion region1 = mockRegion(info1, 1000);
097    HRegion region2 = mockRegion(info2, 10000);
098    when(rsServices.getRegions()).thenReturn(Arrays.asList(region1, region2));
099    CountDownLatch arrive = new CountDownLatch(1);
100    CountDownLatch resume = new CountDownLatch(1);
101    when(region2.flushcache(anyBoolean(), anyBoolean(), any())).then(i -> {
102      arrive.countDown();
103      resume.await();
104      FlushResultImpl result = mock(FlushResultImpl.class);
105      when(result.isFlushSucceeded()).thenReturn(true);
106      return result;
107    });
108    // hit the soft limit, should trigger a flush
109    assertTrue(manager.increase(1000 * 1024));
110    arrive.await();
111
112    // we should have called getRegions once to find the region to flush
113    verify(rsServices, times(1)).getRegions();
114
115    // hit the hard limit, but since the background thread is running as we haven't call the
116    // resume.countDown yet, the schedule of the new background flush task should be discard
117    // silently.
118    assertFalse(manager.increase(100 * 1024));
119    resume.countDown();
120
121    // wait several seconds and then check the getRegions call, we should not call it second time
122    Thread.sleep(2000);
123    verify(rsServices, times(1)).getRegions();
124  }
125}