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 org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.client.RegionInfoBuilder;
025import org.apache.hadoop.hbase.regionserver.HRegionServer;
026import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL;
027import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
028import org.apache.hadoop.hbase.replication.SyncReplicationState;
029import org.apache.hadoop.hbase.replication.SyncReplicationTestBase;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.hbase.testclassification.ReplicationTests;
032import org.apache.hadoop.hbase.wal.AbstractFSWALProvider;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037/**
038 * Testcase for HBASE-20456.
039 */
040@Category({ ReplicationTests.class, LargeTests.class })
041public class TestSyncReplicationShipperQuit extends SyncReplicationTestBase {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestSyncReplicationShipperQuit.class);
046
047  @Test
048  public void testShipperQuitWhenDA() throws Exception {
049    // set to serial replication
050    UTIL1.getAdmin().updateReplicationPeerConfig(PEER_ID, ReplicationPeerConfig
051      .newBuilder(UTIL1.getAdmin().getReplicationPeerConfig(PEER_ID)).setSerial(true).build());
052    UTIL2.getAdmin().updateReplicationPeerConfig(PEER_ID, ReplicationPeerConfig
053      .newBuilder(UTIL2.getAdmin().getReplicationPeerConfig(PEER_ID)).setSerial(true).build());
054    UTIL2.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
055      SyncReplicationState.STANDBY);
056    UTIL1.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
057      SyncReplicationState.ACTIVE);
058
059    writeAndVerifyReplication(UTIL1, UTIL2, 0, 100);
060    HRegionServer rs = UTIL1.getRSForFirstRegionInTable(TABLE_NAME);
061    AbstractFSWAL<?> wal =
062      (AbstractFSWAL<?>) rs.getWAL(RegionInfoBuilder.newBuilder(TABLE_NAME).build());
063    String walGroupId =
064      AbstractFSWALProvider.getWALPrefixFromWALName(wal.getCurrentFileName().getName());
065    ReplicationSourceShipper shipper =
066      ((ReplicationSource) ((Replication) rs.getReplicationSourceService()).getReplicationManager()
067        .getSource(PEER_ID)).workerThreads.get(walGroupId);
068    assertFalse(shipper.isFinished());
069
070    UTIL1.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
071      SyncReplicationState.DOWNGRADE_ACTIVE);
072    writeAndVerifyReplication(UTIL1, UTIL2, 100, 200);
073
074    ReplicationSource source = (ReplicationSource) ((Replication) rs.getReplicationSourceService())
075      .getReplicationManager().getSource(PEER_ID);
076    // the peer is serial so here we can make sure that the previous wals have already been
077    // replicated, and finally the shipper should be removed from the worker pool
078    UTIL1.waitFor(10000, () -> !source.workerThreads.containsKey(walGroupId));
079    assertTrue(shipper.isFinished());
080  }
081}