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