Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
L
leader reelection with zookeeper
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tammam.alsoleman
leader reelection with zookeeper
Commits
f23e1511
Commit
f23e1511
authored
Nov 30, 2025
by
tammam.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improved Leader Election Implementation - Avoiding Herd Effect with Smart Watching
parent
a76b64e4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
14 deletions
+24
-14
LeaderElection.java
src/main/java/LeaderElection.java
+24
-14
No files found.
src/main/java/LeaderElection.java
View file @
f23e1511
...
...
@@ -12,13 +12,14 @@ public class LeaderElection implements Watcher {
private
String
currentZnodeName
;
private
ZooKeeper
zooKeeper
;
private
boolean
isLeader
=
false
;
public
static
void
main
(
String
[]
args
)
throws
IOException
,
InterruptedException
,
KeeperException
{
LeaderElection
leaderElection
=
new
LeaderElection
();
leaderElection
.
connectToZookeeper
();
leaderElection
.
volunteerForLeadership
();
leaderElection
.
electLeader
();
leaderElection
.
electLeader
WithSmartWatching
();
leaderElection
.
run
();
leaderElection
.
close
();
System
.
out
.
println
(
"Successfully Closed"
);
...
...
@@ -32,18 +33,28 @@ public class LeaderElection implements Watcher {
this
.
currentZnodeName
=
znodeFullPath
.
replace
(
ELECTION_NAMESPACE
+
"/"
,
""
);
}
public
void
electLeader
()
throws
InterruptedException
,
KeeperException
{
List
<
String
>
children
=
zooKeeper
.
getChildren
(
ELECTION_NAMESPACE
,
this
);
public
void
electLeaderWithSmartWatching
()
throws
InterruptedException
,
KeeperException
{
List
<
String
>
children
=
zooKeeper
.
getChildren
(
ELECTION_NAMESPACE
,
false
);
Collections
.
sort
(
children
);
String
smallestChild
=
children
.
get
(
0
);
//the first element
String
smallestChild
=
children
.
get
(
0
);
if
(
smallestChild
.
equals
(
currentZnodeName
))
{
System
.
out
.
println
(
"I'm a leader"
);
isLeader
=
true
;
System
.
out
.
println
(
"I'm the LEADER: "
+
currentZnodeName
);
}
else
{
System
.
out
.
println
(
"I'm not a leader"
+
smallestChild
+
" is a leader."
);
isLeader
=
false
;
int
currentIndex
=
children
.
indexOf
(
currentZnodeName
);
String
previousNode
=
children
.
get
(
currentIndex
-
1
);
System
.
out
.
println
(
"I'm a FOLLOWER. Watching previous node: "
+
previousNode
);
watchPreviousNode
(
previousNode
);
}
}
private
void
watchPreviousNode
(
String
previousNode
)
throws
KeeperException
,
InterruptedException
{
String
previousNodePath
=
ELECTION_NAMESPACE
+
"/"
+
previousNode
;
zooKeeper
.
exists
(
previousNodePath
,
this
);
}
private
void
close
()
throws
InterruptedException
{
...
...
@@ -74,15 +85,14 @@ public class LeaderElection implements Watcher {
}
else
if
(
watchedEvent
.
getState
()
==
Event
.
KeeperState
.
Closed
)
{
System
.
out
.
println
(
"Closed Successfully"
);
}
case
NodeChildrenChanged:
case
NodeDeleted:
System
.
out
.
println
(
"Previous node deleted - re-electing leader"
);
try
{
electLeader
();
System
.
out
.
println
(
ELECTION_NAMESPACE
+
" NodeChildrenChanged"
);
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
KeeperException
e
)
{
throw
new
RuntimeException
(
e
);
electLeaderWithSmartWatching
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
break
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment