From 8909ec6e008dab5ce70c5165ac76923adab2562d Mon Sep 17 00:00:00 2001 From: zhangduo Date: Wed, 29 Jun 2016 22:11:16 +0800 Subject: [PATCH] HBASE-16135 PeerClusterZnode under rs of removed peer may never be deleted --- .../hbase/replication/ReplicationQueuesZKImpl.java | 8 ++- .../regionserver/ReplicationSourceManager.java | 75 ++++++++++++++-------- .../regionserver/TestReplicationSourceManager.java | 45 +++++++++++-- 3 files changed, 93 insertions(+), 35 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/replication/ReplicationQueuesZKImpl.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/replication/ReplicationQueuesZKImpl.java index 949efd5..ab5afcd 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/replication/ReplicationQueuesZKImpl.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/replication/ReplicationQueuesZKImpl.java @@ -299,9 +299,11 @@ public class ReplicationQueuesZKImpl extends ReplicationStateZKBase implements R for (String peerId : peerIdsToProcess) { ReplicationQueueInfo replicationQueueInfo = new ReplicationQueueInfo(peerId); if (!peerExists(replicationQueueInfo.getPeerId())) { - LOG.warn("Peer " + peerId + " didn't exist, skipping the replay"); - // Protection against moving orphaned queues - continue; + // the orphaned queues must be moved, otherwise the delete op of dead rs will fail, + // this will cause the whole multi op fail. + // NodeFailoverWorker will skip the orphaned queues. + LOG.warn("Peer " + peerId + + " didn't exist, will move its queue to avoid the failure of multi op"); } String newPeerId = peerId + "-" + znode; String newPeerZnode = ZKUtil.joinZNode(this.myQueuesZnode, newPeerId); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationSourceManager.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationSourceManager.java index 95f7635..22b436a 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationSourceManager.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationSourceManager.java @@ -19,6 +19,9 @@ package org.apache.hadoop.hbase.replication.regionserver; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.util.concurrent.ThreadFactoryBuilder; + import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -56,8 +59,6 @@ import org.apache.hadoop.hbase.replication.ReplicationQueueInfo; import org.apache.hadoop.hbase.replication.ReplicationQueues; import org.apache.hadoop.hbase.replication.ReplicationTracker; -import com.google.common.util.concurrent.ThreadFactoryBuilder; - /** * This class is responsible to manage all the replication * sources. There are two classes of sources: @@ -326,6 +327,11 @@ public class ReplicationSourceManager implements ReplicationListener { return this.oldsources; } + @VisibleForTesting + List getAllQueues() { + return replicationQueues.getAllQueues(); + } + void preLogRoll(Path newLog) throws IOException { synchronized (this.hlogsById) { String name = newLog.getName(); @@ -465,34 +471,42 @@ public class ReplicationSourceManager implements ReplicationListener { + sources.size() + " and another " + oldsources.size() + " that were recovered"); String terminateMessage = "Replication stream was removed by a user"; - ReplicationSourceInterface srcToRemove = null; List oldSourcesToDelete = new ArrayList(); - // First close all the recovered sources for this peer - for (ReplicationSourceInterface src : oldsources) { - if (id.equals(src.getPeerClusterId())) { - oldSourcesToDelete.add(src); + // synchronized on oldsources to avoid adding recovered source for the to-be-removed peer + // see NodeFailoverWorker.run + synchronized (oldsources) { + // First close all the recovered sources for this peer + for (ReplicationSourceInterface src : oldsources) { + if (id.equals(src.getPeerClusterId())) { + oldSourcesToDelete.add(src); + } } } for (ReplicationSourceInterface src : oldSourcesToDelete) { src.terminate(terminateMessage); - closeRecoveredQueue((src)); + closeRecoveredQueue(src); } LOG.info("Number of deleted recovered sources for " + id + ": " + oldSourcesToDelete.size()); // Now look for the one on this cluster - for (ReplicationSourceInterface src : this.sources) { - if (id.equals(src.getPeerClusterId())) { - srcToRemove = src; - break; + List srcToRemove = new ArrayList(); + synchronized (this.replicationPeers) {// synchronize on replicationPeers to avoid adding source + // for the to-be-removed peer + for (ReplicationSourceInterface src : this.sources) { + if (id.equals(src.getPeerClusterId())) { + srcToRemove.add(src); + } } } - if (srcToRemove == null) { + if (srcToRemove.size() == 0) { LOG.error("The queue we wanted to close is missing " + id); return; } - srcToRemove.terminate(terminateMessage); - this.sources.remove(srcToRemove); + for (ReplicationSourceInterface toRemove : srcToRemove) { + toRemove.terminate(terminateMessage); + this.sources.remove(toRemove); + } deleteSource(id, true); } @@ -532,9 +546,12 @@ public class ReplicationSourceManager implements ReplicationListener { private final UUID clusterId; /** - * * @param rsZnode */ + public NodeFailoverWorker(String rsZnode) { + this(rsZnode, replicationQueues, replicationPeers, ReplicationSourceManager.this.clusterId); + } + public NodeFailoverWorker(String rsZnode, final ReplicationQueues replicationQueues, final ReplicationPeers replicationPeers, final UUID clusterId) { super("Failover-for-"+rsZnode); @@ -575,6 +592,7 @@ public class ReplicationSourceManager implements ReplicationListener { for (Map.Entry> entry : newQueues.entrySet()) { String peerId = entry.getKey(); + SortedSet walsSet = entry.getValue(); try { // there is not an actual peer defined corresponding to peerId for the failover. ReplicationQueueInfo replicationQueueInfo = new ReplicationQueueInfo(peerId); @@ -589,23 +607,28 @@ public class ReplicationSourceManager implements ReplicationListener { } if (peer == null || peerConfig == null) { LOG.warn("Skipping failover for peer:" + actualPeerId + " of node" + rsZnode); + replicationQueues.removeQueue(peerId); continue; } ReplicationSourceInterface src = getReplicationSource(conf, fs, ReplicationSourceManager.this, this.rq, this.rp, server, peerId, this.clusterId, peerConfig, peer); - if (!this.rp.getPeerIds().contains((src.getPeerClusterId()))) { - src.terminate("Recovered queue doesn't belong to any current peer"); - break; - } - oldsources.add(src); - SortedSet hlogsSet = entry.getValue(); - for (String hlog : hlogsSet) { - src.enqueueLog(new Path(oldLogDir, hlog)); + // synchronized on oldsources to avoid adding recovered source for the to-be-removed peer + // see removePeer + synchronized (oldsources) { + if (!this.rp.getPeerIds().contains(src.getPeerClusterId())) { + src.terminate("Recovered queue doesn't belong to any current peer"); + closeRecoveredQueue(src); + continue; + } + oldsources.add(src); + for (String wal : walsSet) { + src.enqueueLog(new Path(oldLogDir, wal)); + } + src.startup(); } - src.startup(); - hlogsByIdRecoveredQueues.put(peerId, hlogsSet); + hlogsByIdRecoveredQueues.put(peerId, walsSet); } catch (IOException e) { // TODO manage it LOG.error("Failed creating a source", e); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/replication/regionserver/TestReplicationSourceManager.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/replication/regionserver/TestReplicationSourceManager.java index 2d86cd8..c9f5e6b 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/replication/regionserver/TestReplicationSourceManager.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/replication/regionserver/TestReplicationSourceManager.java @@ -21,6 +21,9 @@ package org.apache.hadoop.hbase.replication.regionserver; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.google.common.collect.Sets; + +import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collection; @@ -71,10 +74,10 @@ import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; - -import com.google.common.collect.Sets; +import org.junit.rules.TestName; @Category(MediumTests.class) public class TestReplicationSourceManager { @@ -172,15 +175,24 @@ public class TestReplicationSourceManager { utility.shutdownMiniCluster(); } - @Before - public void setUp() throws Exception { + @Rule + public TestName testName = new TestName(); + + private void cleanLogDir() throws IOException { fs.delete(logDir, true); fs.delete(oldLogDir, true); } + @Before + public void setUp() throws Exception { + LOG.info("Start " + testName.getMethodName()); + cleanLogDir(); + } + @After public void tearDown() throws Exception { - setUp(); + LOG.info("End " + testName.getMethodName()); + cleanLogDir(); } @Test @@ -239,7 +251,6 @@ public class TestReplicationSourceManager { @Test public void testClaimQueues() throws Exception { - LOG.debug("testNodeFailoverWorkerCopyQueuesFromRSUsingMulti"); conf.setBoolean(HConstants.ZOOKEEPER_USEMULTI, true); final Server server = new DummyServer("hostname0.example.org"); ReplicationQueues rq = @@ -393,6 +404,28 @@ public class TestReplicationSourceManager { s0.abort("", null); } + @Test + public void testCleanupUnknownPeerZNode() throws Exception { + final Server server = new DummyServer("hostname1.example.org"); + ReplicationQueues rq = + ReplicationFactory.getReplicationQueues(server.getZooKeeper(), server.getConfiguration(), + server); + rq.init(server.getServerName().toString()); + // populate some znodes in the peer znode + // add log to an unknown peer + String group = "testgroup"; + rq.addLog("2", group + ".log1"); + rq.addLog("2", group + ".log2"); + + NodeFailoverWorker w1 = manager.new NodeFailoverWorker(server.getServerName().getServerName()); + w1.run(); + + // The log of the unknown peer should be removed from zk + for (String peer : manager.getAllQueues()) { + assertTrue(peer.startsWith("1")); + } + } + static class DummyNodeFailoverWorker extends Thread { private SortedMap> logZnodesMap; Server server; -- 1.9.1