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.replication;
019
020import static org.apache.hadoop.hbase.master.procedure.ReopenTableRegionsProcedure.PROGRESSIVE_BATCH_BACKOFF_MILLIS_KEY;
021import static org.apache.hadoop.hbase.master.procedure.ReopenTableRegionsProcedure.PROGRESSIVE_BATCH_SIZE_MAX_KEY;
022import static org.junit.jupiter.api.Assertions.assertEquals;
023
024import java.util.Collections;
025import java.util.Optional;
026import java.util.concurrent.Future;
027import java.util.concurrent.TimeUnit;
028import java.util.concurrent.atomic.AtomicReference;
029import org.apache.hadoop.hbase.HBaseTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
034import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
035import org.apache.hadoop.hbase.master.procedure.ReopenTableRegionsProcedure;
036import org.apache.hadoop.hbase.replication.DummyReplicationEndpoint;
037import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
038import org.apache.hadoop.hbase.testclassification.MasterTests;
039import org.apache.hadoop.hbase.testclassification.MediumTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.junit.jupiter.api.AfterAll;
042import org.junit.jupiter.api.BeforeAll;
043import org.junit.jupiter.api.Tag;
044import org.junit.jupiter.api.Test;
045
046@Tag(MasterTests.TAG)
047@Tag(MediumTests.TAG)
048public class TestSerialPeerReopenThrottling {
049
050  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
051  private static final TableName TABLE_NAME = TableName.valueOf("serialPeerReopen");
052  private static final byte[] CF = Bytes.toBytes("cf");
053  private static final int REGION_COUNT = 5;
054
055  @BeforeAll
056  public static void setUp() throws Exception {
057    UTIL.getConfiguration().setInt(PROGRESSIVE_BATCH_SIZE_MAX_KEY, 1);
058    UTIL.getConfiguration().setLong(PROGRESSIVE_BATCH_BACKOFF_MILLIS_KEY, 500);
059    UTIL.startMiniCluster(1);
060    UTIL.getAdmin().createTable(
061      TableDescriptorBuilder.newBuilder(TABLE_NAME)
062        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF)
063          .setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build())
064        .build(),
065      Bytes.toBytes("a"), Bytes.toBytes("z"), REGION_COUNT);
066    UTIL.waitTableAvailable(TABLE_NAME);
067  }
068
069  @AfterAll
070  public static void tearDown() throws Exception {
071    UTIL.shutdownMiniCluster();
072  }
073
074  @Test
075  public void testChangeToSerialReopensRegionsInBatches() throws Exception {
076    Admin admin = UTIL.getAdmin();
077    ReplicationPeerConfig peerConfig = peerConfig(false);
078    admin.addReplicationPeer("changeToSerial", peerConfig, true);
079
080    assertReopenedInBatches(admin.updateReplicationPeerConfigAsync("changeToSerial",
081      ReplicationPeerConfig.newBuilder(peerConfig).setSerial(true).build()));
082  }
083
084  @Test
085  public void testAddSerialPeerReopensRegionsInBatches() throws Exception {
086    assertReopenedInBatches(
087      UTIL.getAdmin().addReplicationPeerAsync("addSerial", peerConfig(true), true));
088  }
089
090  private static ReplicationPeerConfig peerConfig(boolean serial) {
091    return ReplicationPeerConfig.newBuilder()
092      .setReplicationEndpointImpl(DummyReplicationEndpoint.class.getName())
093      .setReplicateAllUserTables(false)
094      .setTableCFsMap(Collections.singletonMap(TABLE_NAME, Collections.emptyList()))
095      .setSerial(serial).build();
096  }
097
098  private static void assertReopenedInBatches(Future<Void> peerChange) throws Exception {
099    // Completed child procedures are not retained, so capture the reopen while it runs.
100    AtomicReference<ReopenTableRegionsProcedure> reopen = new AtomicReference<>();
101    UTIL.waitFor(30_000, 10, () -> {
102      findReopen().ifPresent(reopen::set);
103      return reopen.get() != null;
104    });
105    peerChange.get(60, TimeUnit.SECONDS);
106
107    assertEquals(REGION_COUNT, reopen.get().getRegionsReopened());
108    assertEquals(REGION_COUNT, reopen.get().getBatchesProcessed());
109  }
110
111  private static Optional<ReopenTableRegionsProcedure> findReopen() {
112    return UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getProcedures()
113      .stream().filter(ReopenTableRegionsProcedure.class::isInstance)
114      .map(ReopenTableRegionsProcedure.class::cast).filter(p -> p.getTableName().equals(TABLE_NAME))
115      .findFirst();
116  }
117}