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