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 java.io.IOException;
021import java.util.Collections;
022import java.util.List;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.master.HMaster;
030import org.apache.hadoop.hbase.master.MasterServices;
031import org.apache.hadoop.hbase.master.RegionServerList;
032import org.apache.hadoop.hbase.master.ServerManager;
033import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
034import org.apache.hadoop.hbase.master.replication.ClaimReplicationQueuesProcedure;
035import org.apache.hadoop.hbase.procedure2.Procedure;
036import org.apache.hadoop.hbase.testclassification.LargeTests;
037import org.apache.hadoop.hbase.testclassification.ReplicationTests;
038import org.junit.AfterClass;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043
044import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
045
046import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState;
047
048/**
049 * In HBASE-26029, we reimplement the claim queue operation with proc-v2 and make it a step in SCP,
050 * this is a UT to make sure the {@link ClaimReplicationQueuesProcedure} works correctly.
051 */
052@Category({ ReplicationTests.class, LargeTests.class })
053public class TestClaimReplicationQueue extends TestReplicationBase {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestClaimReplicationQueue.class);
058
059  private static final TableName tableName3 = TableName.valueOf("test3");
060
061  private static final String PEER_ID3 = "3";
062
063  private static Table table3;
064
065  private static Table table4;
066
067  private static volatile boolean EMPTY = false;
068
069  public static final class ServerManagerForTest extends ServerManager {
070
071    public ServerManagerForTest(MasterServices master, RegionServerList storage) {
072      super(master, storage);
073    }
074
075    @Override
076    public List<ServerName> getOnlineServersList() {
077      // return no region server to make the procedure hang
078      if (EMPTY) {
079        for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
080          if (e.getClassName().equals(ClaimReplicationQueuesProcedure.class.getName())) {
081            return Collections.emptyList();
082          }
083        }
084      }
085      return super.getOnlineServersList();
086    }
087  }
088
089  public static final class HMasterForTest extends HMaster {
090
091    public HMasterForTest(Configuration conf) throws IOException {
092      super(conf);
093    }
094
095    @Override
096    protected ServerManager createServerManager(MasterServices master, RegionServerList storage)
097      throws IOException {
098      setupClusterConnection();
099      return new ServerManagerForTest(master, storage);
100    }
101  }
102
103  @BeforeClass
104  public static void setUpBeforeClass() throws Exception {
105    CONF1.setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
106    TestReplicationBase.setUpBeforeClass();
107    createTable(tableName3);
108    table3 = connection1.getTable(tableName3);
109    table4 = connection2.getTable(tableName3);
110  }
111
112  @AfterClass
113  public static void tearDownAfterClass() throws Exception {
114    Closeables.close(table3, true);
115    Closeables.close(table4, true);
116    TestReplicationBase.tearDownAfterClass();
117  }
118
119  @Override
120  public void setUpBase() throws Exception {
121    super.setUpBase();
122    // set up two replication peers and only 1 rs to test claim replication queue with multiple
123    // round
124    addPeer(PEER_ID3, tableName3);
125  }
126
127  @Override
128  public void tearDownBase() throws Exception {
129    super.tearDownBase();
130    removePeer(PEER_ID3);
131  }
132
133  @Test
134  public void testClaim() throws Exception {
135    // disable the peers
136    hbaseAdmin.disableReplicationPeer(PEER_ID2);
137    hbaseAdmin.disableReplicationPeer(PEER_ID3);
138
139    // put some data
140    int count1 = UTIL1.loadTable(htable1, famName);
141    int count2 = UTIL1.loadTable(table3, famName);
142
143    EMPTY = true;
144    UTIL1.getMiniHBaseCluster().stopRegionServer(0).join();
145    UTIL1.getMiniHBaseCluster().startRegionServer();
146
147    // since there is no active region server to get the replication queue, the procedure should be
148    // in WAITING_TIMEOUT state for most time to retry
149    HMaster master = UTIL1.getMiniHBaseCluster().getMaster();
150    UTIL1.waitFor(30000,
151      () -> master.getProcedures().stream()
152        .filter(p -> p instanceof ClaimReplicationQueuesProcedure)
153        .anyMatch(p -> p.getState() == ProcedureState.WAITING_TIMEOUT));
154
155    hbaseAdmin.enableReplicationPeer(PEER_ID2);
156    hbaseAdmin.enableReplicationPeer(PEER_ID3);
157
158    EMPTY = false;
159    // wait until the SCP finished, ClaimReplicationQueuesProcedure is a sub procedure of SCP
160    UTIL1.waitFor(30000, () -> master.getProcedures().stream()
161      .filter(p -> p instanceof ServerCrashProcedure).allMatch(Procedure::isSuccess));
162
163    // we should get all the data in the target cluster
164    waitForReplication(htable2, count1, NB_RETRIES);
165    waitForReplication(table4, count2, NB_RETRIES);
166  }
167}