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;
019
020import static org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.PeerSyncReplicationStateTransitionState.REOPEN_ALL_REGIONS_IN_PEER_VALUE;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022
023import java.io.IOException;
024import java.io.UncheckedIOException;
025import java.util.Optional;
026import java.util.concurrent.CountDownLatch;
027import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
028import org.apache.hadoop.hbase.coprocessor.ObserverContext;
029import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessor;
030import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
031import org.apache.hadoop.hbase.coprocessor.RegionServerObserver;
032import org.apache.hadoop.hbase.master.replication.TransitPeerSyncReplicationStateProcedure;
033import org.apache.hadoop.hbase.testclassification.LargeTests;
034import org.apache.hadoop.hbase.testclassification.ReplicationTests;
035import org.junit.jupiter.api.BeforeAll;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038
039/**
040 * Testcase for HBASE-21441.
041 */
042@Tag(ReplicationTests.TAG)
043@Tag(LargeTests.TAG)
044public class TestSyncReplicationNewRSJoinBetweenRefreshes
045  extends SyncReplicationTestBaseNoBeforeAll {
046
047  private static boolean HALT;
048
049  private static CountDownLatch ARRIVE;
050
051  private static CountDownLatch RESUME;
052
053  public static final class HaltCP implements RegionServerObserver, RegionServerCoprocessor {
054
055    @Override
056    public Optional<RegionServerObserver> getRegionServerObserver() {
057      return Optional.of(this);
058    }
059
060    @Override
061    public void postExecuteProcedures(ObserverContext<RegionServerCoprocessorEnvironment> ctx)
062      throws IOException {
063      synchronized (HaltCP.class) {
064        if (!HALT) {
065          return;
066        }
067        UTIL1.getMiniHBaseCluster().getMaster().getProcedures().stream()
068          .filter(p -> p instanceof TransitPeerSyncReplicationStateProcedure)
069          .filter(p -> !p.isFinished()).map(p -> (TransitPeerSyncReplicationStateProcedure) p)
070          .findFirst().ifPresent(proc -> {
071            // this is the next state of REFRESH_PEER_SYNC_REPLICATION_STATE_ON_RS_BEGIN_VALUE
072            if (proc.getCurrentStateId() == REOPEN_ALL_REGIONS_IN_PEER_VALUE) {
073              // tell the main thread to start a new region server
074              ARRIVE.countDown();
075              try {
076                // wait for the region server to online
077                RESUME.await();
078              } catch (InterruptedException e) {
079                throw new RuntimeException(e);
080              }
081              HALT = false;
082            }
083          });
084      }
085    }
086  }
087
088  @BeforeAll
089  public static void setUp() throws Exception {
090    UTIL1.getConfiguration().setClass(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
091      HaltCP.class, RegionServerObserver.class);
092    SyncReplicationTestBase.setUp();
093  }
094
095  @Test
096  public void test() throws IOException, InterruptedException {
097    UTIL2.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
098      SyncReplicationState.STANDBY);
099    UTIL1.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
100      SyncReplicationState.ACTIVE);
101
102    ARRIVE = new CountDownLatch(1);
103    RESUME = new CountDownLatch(1);
104    HALT = true;
105    Thread t = new Thread(() -> {
106      try {
107        UTIL1.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
108          SyncReplicationState.DOWNGRADE_ACTIVE);
109      } catch (IOException e) {
110        throw new UncheckedIOException(e);
111      }
112    });
113    t.start();
114    ARRIVE.await();
115    UTIL1.getMiniHBaseCluster().startRegionServer();
116    RESUME.countDown();
117    t.join();
118    assertEquals(SyncReplicationState.DOWNGRADE_ACTIVE,
119      UTIL1.getAdmin().getReplicationPeerSyncReplicationState(PEER_ID));
120  }
121}