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.replication.regionserver;
019
020import static org.junit.jupiter.api.Assertions.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.util.Optional;
024import java.util.stream.Stream;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.client.TableDescriptor;
031import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
032import org.apache.hadoop.hbase.regionserver.HRegionServer;
033import org.apache.hadoop.hbase.replication.TestReplicationBaseNoBeforeAll;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.testclassification.ReplicationTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.hadoop.hbase.util.JVMClusterUtil;
038import org.junit.jupiter.api.BeforeAll;
039import org.junit.jupiter.api.Tag;
040import org.junit.jupiter.api.Test;
041import org.junit.jupiter.api.TestInfo;
042
043@Tag(ReplicationTests.TAG)
044@Tag(MediumTests.TAG)
045public class TestReplicationSourceManagerJoin extends TestReplicationBaseNoBeforeAll {
046
047  @BeforeAll
048  public static void setUpBeforeClass() throws Exception {
049    // NUM_SLAVES1 is presumed 2 in below.
050    NUM_SLAVES1 = 2;
051    configureClusters(UTIL1, UTIL2);
052    startClusters();
053  }
054
055  @Test
056  public void testReplicationSourcesTerminate(TestInfo testInfo) throws Exception {
057    String testName = testInfo.getTestMethod().get().getName();
058    // Create table in source cluster only, let TableNotFoundException block peer to avoid
059    // recovered source end.
060    TableName tableName = TableName.valueOf(testName);
061    TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName)
062      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(famName)
063        .setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build())
064      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(noRepfamName)).build();
065    hbaseAdmin.createTable(td);
066    assertFalse(UTIL2.getAdmin().tableExists(tableName));
067    Table table = UTIL1.getConnection().getTable(tableName);
068    // load data
069    for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
070      table.put(new Put(Bytes.toBytes(i)).addColumn(famName, row, row));
071    }
072    // Kill rs holding table region. There are only TWO servers. We depend on it.
073    Optional<HRegionServer> server = UTIL1.getMiniHBaseCluster().getLiveRegionServerThreads()
074      .stream().map(JVMClusterUtil.RegionServerThread::getRegionServer)
075      .filter(rs -> !rs.getRegions(tableName).isEmpty()).findAny();
076    assertTrue(server.isPresent());
077    server.get().abort("stopping for test");
078
079    UTIL1.waitFor(60000, () -> 1 == UTIL1.getMiniHBaseCluster().getNumLiveRegionServers());
080    UTIL1.waitTableAvailable(tableName);
081    // Wait for recovered source running
082    HRegionServer rs =
083      UTIL1.getMiniHBaseCluster().getLiveRegionServerThreads().get(0).getRegionServer();
084    ReplicationSourceManager manager = rs.getReplicationSourceService().getReplicationManager();
085    UTIL1.waitFor(60000, () -> !manager.getOldSources().isEmpty());
086
087    assertFalse(manager.getSources().isEmpty());
088    assertFalse(manager.getOldSources().isEmpty());
089
090    // Check all sources running before manager.join(), terminated after manager.join().
091    Stream.concat(manager.getSources().stream(), manager.getOldSources().stream())
092      .filter(src -> src instanceof ReplicationSource)
093      .forEach(src -> assertTrue(((ReplicationSource) src).sourceRunning));
094    manager.join();
095    Stream.concat(manager.getSources().stream(), manager.getOldSources().stream())
096      .filter(src -> src instanceof ReplicationSource)
097      .forEach(src -> assertFalse(((ReplicationSource) src).sourceRunning));
098  }
099
100}