View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.replication;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.Waiter;
25  import org.apache.hadoop.hbase.regionserver.HRegionServer;
26  import org.apache.hadoop.hbase.replication.regionserver.Replication;
27  import org.apache.hadoop.hbase.replication.regionserver.ReplicationSourceManager;
28  import org.apache.hadoop.hbase.testclassification.MediumTests;
29  import org.apache.hadoop.hbase.util.Threads;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  @Category(MediumTests.class)
34  public class TestReplicationConfigTracker extends TestReplicationBase {
35    private static final Log LOG = LogFactory.getLog(TestReplicationKillRS.class);
36  
37    @Test
38    public void testReplicationConfigTracker() throws Exception {
39      // killing the RS with hbase:meta can result into failed puts until we solve
40      // IO fencing
41      int rsToKill1 = utility1.getHBaseCluster().getServerWithMeta() == 0 ? 1 : 0;
42      int otherRs = rsToKill1 == 0 ? 1 : 0;
43      final HRegionServer regionServer = utility1.getHBaseCluster().getRegionServer(otherRs);
44      final Thread listenerTracker = trackListener(utility1, otherRs);
45      LOG.info("Start loading table");
46      utility1.loadTable(htable1, famName, true);
47      LOG.info("Done loading table");
48      utility1.getHBaseCluster().getRegionServer(rsToKill1).abort("Stopping as part of the test");
49      utility1.getHBaseCluster().waitOnRegionServer(rsToKill1);
50      while (utility1.getHBaseCluster().getMaster().getServerManager().areDeadServersInProgress()) {
51        LOG.info("Waiting on processing of crashed server before proceeding...");
52        Threads.sleep(1000);
53      }
54      Waiter.waitFor(utility1.getConfiguration(), 20000, new Waiter.Predicate<Exception>() {
55        @Override public boolean evaluate() throws Exception {
56          return !listenerTracker.isAlive();
57        }
58      });
59      final ReplicationPeerZKImpl.PeerConfigTracker tracker = getPeerConfigTracker(regionServer);
60      Waiter.waitFor(utility1.getConfiguration(), 20000, new Waiter.Predicate<Exception>() {
61        @Override public boolean evaluate() throws Exception {
62          return tracker.getListeners().size() == 1;
63        }
64      });
65    }
66  
67    private static Thread trackListener(final HBaseTestingUtility utility, final int rs) {
68      Thread trackListener = new Thread() {
69        public void run() {
70          Replication replication = (Replication) utility.getHBaseCluster().getRegionServer(rs)
71              .getReplicationSourceService();
72          ReplicationSourceManager manager = replication.getReplicationManager();
73          ReplicationPeerZKImpl replicationPeerZK =
74              (ReplicationPeerZKImpl) manager.getReplicationPeers().getPeer(PEER_ID);
75          ReplicationPeerZKImpl.PeerConfigTracker peerConfigTracker =
76              replicationPeerZK.getPeerConfigTracker();
77          while (peerConfigTracker.getListeners().size() != 2) {
78            try {
79              Thread.sleep(50);
80            } catch (InterruptedException e) {
81              LOG.error("track config failed", e);
82            }
83          }
84        }
85      };
86      trackListener.setDaemon(true);
87      trackListener.start();
88      return trackListener;
89    }
90  
91    private ReplicationPeerZKImpl.PeerConfigTracker getPeerConfigTracker(HRegionServer rs) {
92      Replication replication = (Replication) rs.getReplicationSourceService();
93      ReplicationSourceManager manager = replication.getReplicationManager();
94      ReplicationPeerZKImpl replicationPeerZK =
95          (ReplicationPeerZKImpl) manager.getReplicationPeers().getPeer(PEER_ID);
96      ReplicationPeerZKImpl.PeerConfigTracker peerConfigTracker =
97          replicationPeerZK.getPeerConfigTracker();
98      return peerConfigTracker;
99    }
100 }