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