-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CURATOR-724. Fix LeaderLatch recover on reconnected and missing leaderPath #515
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -509,7 +509,7 @@ public void processResult(CuratorFramework client, CuratorEvent event) throws Ex | |
getChildren(); | ||
} | ||
} else { | ||
log.error("getChildren() failed. rc = {}", event.getResultCode()); | ||
log.error("[id={}] creatingParentContainersIfNeeded() failed (rc = {})", id, event.getResultCode()); | ||
} | ||
} | ||
}; | ||
|
@@ -528,7 +528,7 @@ private synchronized void internalStart() { | |
reset(); | ||
} catch (Exception e) { | ||
ThreadUtils.checkInterrupted(e); | ||
log.error("An error occurred checking resetting leadership.", e); | ||
log.error("[id={}] failed to check resetting leadership.", id, e); | ||
} | ||
} | ||
} | ||
|
@@ -545,10 +545,10 @@ private void checkLeadership(List<String> children) throws Exception { | |
List<String> sortedChildren = LockInternals.getSortedChildren(LOCK_NAME, sorter, children); | ||
int ourIndex = (localOurPath != null) ? sortedChildren.indexOf(ZKPaths.getNodeFromPath(localOurPath)) : -1; | ||
|
||
log.debug("checkLeadership with id: {}, ourPath: {}, children: {}", id, localOurPath, sortedChildren); | ||
log.debug("[id={}] checkLeadership with ourPath: {}, children: {}", id, localOurPath, sortedChildren); | ||
|
||
if (ourIndex < 0) { | ||
log.error("Can't find our node. Resetting. Index: {}", ourIndex); | ||
log.error("[id={}] failed to find our node; resetting (index: {})", id, ourIndex); | ||
reset(); | ||
return; | ||
} | ||
|
@@ -582,7 +582,7 @@ public void process(WatchedEvent event) { | |
getChildren(); | ||
} catch (Exception ex) { | ||
ThreadUtils.checkInterrupted(ex); | ||
log.error("An error occurred checking the leadership.", ex); | ||
log.error("[id={}] failed to check the leadership.", id, ex); | ||
} | ||
} | ||
} | ||
|
@@ -592,7 +592,7 @@ public void process(WatchedEvent event) { | |
@Override | ||
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { | ||
if (event.getResultCode() == KeeperException.Code.NONODE.intValue()) { | ||
// previous node is gone - retry getChildren | ||
log.debug("[id={}] previous node is gone; retry getChildren", id); | ||
getChildren(); | ||
} | ||
} | ||
|
@@ -607,29 +607,37 @@ private void getChildren() throws Exception { | |
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { | ||
if (event.getResultCode() == KeeperException.Code.OK.intValue()) { | ||
checkLeadership(event.getChildren()); | ||
} else if (event.getResultCode() == KeeperException.Code.NONODE.intValue()) { | ||
log.debug("[id={}] latchPath has gone; reset", id); | ||
// This is possible when RECONNECTED during: | ||
// (1) Scale the zk cluster to 0 nodes. | ||
// (2) Scale it back. | ||
// | ||
// See also https://issues.apache.org/jira/browse/CURATOR-724 | ||
reset(); | ||
} else { | ||
log.error("[id={}] getChildren() failed (rc = {})", id, event.getResultCode()); | ||
} | ||
} | ||
}; | ||
client.getChildren().inBackground(callback).forPath(ZKPaths.makePath(latchPath, null)); | ||
} | ||
|
||
@VisibleForTesting | ||
volatile CountDownLatch debugHandleReconnectedLatch = null; | ||
|
||
@VisibleForTesting | ||
protected void handleStateChange(ConnectionState newState) { | ||
switch (newState) { | ||
default: { | ||
// NOP | ||
break; | ||
} | ||
|
||
case RECONNECTED: { | ||
try { | ||
if (client.getConnectionStateErrorPolicy().isErrorState(ConnectionState.SUSPENDED) | ||
|| !hasLeadership.get()) { | ||
getChildren(); | ||
if (debugHandleReconnectedLatch != null) { | ||
debugHandleReconnectedLatch.await(); | ||
} | ||
getChildren(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also it doesn't hurt we always call |
||
} catch (Exception e) { | ||
ThreadUtils.checkInterrupted(e); | ||
log.error("Could not reset leader latch", e); | ||
log.error("[id={}] failed to recheck leadership on reconnected", id, e); | ||
setLeadership(false); | ||
} | ||
break; | ||
|
@@ -646,6 +654,11 @@ protected void handleStateChange(ConnectionState newState) { | |
setLeadership(false); | ||
break; | ||
} | ||
|
||
default: { | ||
// NOP | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -662,7 +675,7 @@ private synchronized void setLeadership(boolean newValue) { | |
|
||
private void setNode(String newValue) throws Exception { | ||
String oldPath = ourPath.getAndSet(newValue); | ||
log.debug("setNode with id: {}, oldPath: {}, newValue: {}", id, oldPath, newValue); | ||
log.debug("[id={}] setNode with oldPath: {}, newValue: {}", id, oldPath, newValue); | ||
if (oldPath != null) { | ||
client.delete().guaranteed().inBackground().forPath(oldPath); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for sure, so, in case of
RECONNECTED
, we have no idea what happened to the cluster andgetChildren
is a conservative option comparing toreset
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Updated add a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also ensure that the test failed without this fix.