Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
L
leader-election-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
mohamadbashar.disoki
leader-election-zookeeper
Commits
2764a3fd
Commit
2764a3fd
authored
Nov 30, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Election process
parent
88582424
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
4 deletions
+33
-4
LeaderElection.java
src/main/java/LeaderElection.java
+33
-4
No files found.
src/main/java/LeaderElection.java
View file @
2764a3fd
import
org.apache.zookeeper.WatchedEvent
;
import
org.apache.zookeeper.Watcher
;
import
org.apache.zookeeper.ZooKeeper
;
import
org.apache.zookeeper.*
;
import
java.io.IOException
;
import
java.util.Collections
;
import
java.util.List
;
public
class
LeaderElection
implements
Watcher
{
private
static
final
String
address
=
"172.29.3.101:2181"
;
private
static
final
int
SESSION_TIMEOUT
=
3000
;
//dead client
private
static
final
String
ELECTION_NAMESPACE
=
"/election"
;
private
String
currentZnodeName
;
private
ZooKeeper
zooKeeper
;
public
static
void
main
(
String
[]
args
)
throws
IOException
,
InterruptedException
{
public
static
void
main
(
String
[]
args
)
throws
IOException
,
InterruptedException
,
KeeperException
{
LeaderElection
leaderElection
=
new
LeaderElection
();
leaderElection
.
connectToZookeeper
();
leaderElection
.
volunteerForLeadership
();
leaderElection
.
electLeader
();
leaderElection
.
run
();
leaderElection
.
close
();
System
.
out
.
println
(
"Successfully Closed"
);
}
public
void
volunteerForLeadership
()
throws
InterruptedException
,
KeeperException
{
String
znodePrefix
=
ELECTION_NAMESPACE
+
"/c_"
;
String
znodeFullPath
=
zooKeeper
.
create
(
znodePrefix
,
new
byte
[]{},
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
EPHEMERAL_SEQUENTIAL
);
System
.
out
.
println
(
znodeFullPath
);
this
.
currentZnodeName
=
znodeFullPath
.
replace
(
ELECTION_NAMESPACE
+
"/"
,
""
);
}
public
void
electLeader
()
throws
InterruptedException
,
KeeperException
{
List
<
String
>
children
=
zooKeeper
.
getChildren
(
ELECTION_NAMESPACE
,
false
);
Collections
.
sort
(
children
);
String
smallestChild
=
children
.
get
(
0
);
//the first element
if
(
smallestChild
.
equals
(
currentZnodeName
))
{
System
.
out
.
println
(
"I'm a leader"
);
}
else
{
System
.
out
.
println
(
"I'm not a leader"
+
smallestChild
+
" is a leader."
);
}
}
private
void
close
()
throws
InterruptedException
{
this
.
zooKeeper
.
close
();
}
...
...
@@ -44,6 +72,7 @@ public class LeaderElection implements Watcher {
}
}
break
;
// case NodeCreated:
// if (watchedEvent.getState() == )
}
...
...
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