Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
L
LeaderElection
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
diaa.hanna
LeaderElection
Commits
6715a099
Commit
6715a099
authored
Dec 03, 2025
by
drnull03
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
damn that was fun finished this project
parents
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
219 additions
and
0 deletions
+219
-0
.gitignore
.gitignore
+1
-0
launch.sh
launch.sh
+3
-0
pom.xml
pom.xml
+45
-0
App.java
src/main/java/com/ds/LeaderElection/App.java
+25
-0
LeaderElection.java
src/main/java/com/ds/LeaderElection/LeaderElection.java
+75
-0
ZKConnection.java
src/main/java/com/ds/LeaderElection/ZKConnection.java
+22
-0
log4j.properties
src/main/resources/log4j.properties
+10
-0
AppTest.java
src/test/java/com/ds/LeaderElection/AppTest.java
+38
-0
No files found.
.gitignore
0 → 100644
View file @
6715a099
target/
launch.sh
0 → 100755
View file @
6715a099
/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo1.cfg
/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo2.cfg
/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo3.cfg
pom.xml
0 → 100644
View file @
6715a099
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.ds.LeaderElection
</groupId>
<artifactId>
LeaderElection
</artifactId>
<packaging>
jar
</packaging>
<version>
1.0-SNAPSHOT
</version>
<name>
LeaderElection
</name>
<url>
http://maven.apache.org
</url>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.1
</version>
<scope>
test
</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>
org.apache.zookeeper
</groupId>
<artifactId>
zookeeper
</artifactId>
<version>
3.8.5
</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.10.1
</version>
<configuration>
<source>
17
</source>
<target>
17
</target>
</configuration>
</plugin>
<plugin>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
exec-maven-plugin
</artifactId>
<version>
3.1.0
</version>
<configuration>
<mainClass>
com.ds.LeaderElection.App
</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/java/com/ds/LeaderElection/App.java
0 → 100644
View file @
6715a099
package
com
.
ds
.
LeaderElection
;
public
class
App
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
ZKConnection
zkConnection
=
new
ZKConnection
();
var
zk
=
zkConnection
.
connect
(
"localhost:2183"
);
//zoo3.cfg
// Create a leader election participant
LeaderElection
election
=
new
LeaderElection
(
zk
);
// Volunteer for leadership (creates ephemeral sequential node)
election
.
volunteerForLeadership
();
// Begin election process
election
.
electLeader
();
// might figure out something better
zkConnection
.
close
();
}
}
src/main/java/com/ds/LeaderElection/LeaderElection.java
0 → 100644
View file @
6715a099
package
com
.
ds
.
LeaderElection
;
import
org.apache.zookeeper.*
;
import
org.apache.zookeeper.data.Stat
;
import
java.util.List
;
public
class
LeaderElection
implements
Watcher
{
private
ZooKeeper
zooKeeper
;
private
String
currentNode
;
private
static
final
String
ELECTION_NODE
=
"/election"
;
public
LeaderElection
(
ZooKeeper
zooKeeper
)
{
this
.
zooKeeper
=
zooKeeper
;
}
public
void
volunteerForLeadership
()
throws
Exception
{
// first time check
Stat
stat
=
zooKeeper
.
exists
(
ELECTION_NODE
,
false
);
if
(
stat
==
null
)
{
zooKeeper
.
create
(
ELECTION_NODE
,
new
byte
[]
{},
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
PERSISTENT
);
// this
// is
// presistent
}
// Create ephemeral sequential node
currentNode
=
zooKeeper
.
create
(
ELECTION_NODE
+
"/node_"
,
new
byte
[]
{},
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
EPHEMERAL_SEQUENTIAL
);
System
.
out
.
println
(
"Node created: "
+
currentNode
);
}
public
void
electLeader
()
throws
Exception
{
while
(
true
)
{
List
<
String
>
children
=
zooKeeper
.
getChildren
(
ELECTION_NODE
,
false
);
children
.
sort
(
String:
:
compareTo
);
String
smallestNode
=
children
.
get
(
0
);
String
currentNodeName
=
currentNode
.
substring
(
currentNode
.
lastIndexOf
(
"/"
)
+
1
);
if
(
currentNodeName
.
equals
(
smallestNode
))
{
System
.
out
.
println
(
currentNode
+
" is the leader THIS IS MEEEE :) "
);
// Keep leader alive (e.g. 60 seconds)
synchronized
(
this
)
{
wait
();
}
}
else
{
System
.
out
.
println
(
currentNode
+
"I am not the leader so sad :("
);
// Find the node immediately before current node
int
index
=
children
.
indexOf
(
currentNodeName
)
-
1
;
String
watchNode
=
children
.
get
(
index
);
final
Object
lock
=
new
Object
();
// Watch the previous node
zooKeeper
.
exists
(
ELECTION_NODE
+
"/"
+
watchNode
,
event
->
{
if
(
event
.
getType
()
==
Event
.
EventType
.
NodeDeleted
)
{
synchronized
(
lock
)
{
lock
.
notifyAll
();
}
}
});
// Wait until the previous node is deleted
synchronized
(
lock
)
{
lock
.
wait
();
}
}
}
}
@Override
public
void
process
(
WatchedEvent
event
)
{
}
}
src/main/java/com/ds/LeaderElection/ZKConnection.java
0 → 100644
View file @
6715a099
package
com
.
ds
.
LeaderElection
;
import
org.apache.zookeeper.ZooKeeper
;
import
java.io.IOException
;
public
class
ZKConnection
{
private
ZooKeeper
zoo
;
//host contains 2181
//this code is a simple wrapper it just connect to our backend
public
ZooKeeper
connect
(
String
host
)
throws
IOException
{
zoo
=
new
ZooKeeper
(
host
,
3000
,
event
->
{
});
return
zoo
;
}
// this function just close the tcp connections
public
void
close
()
throws
InterruptedException
{
if
(
zoo
!=
null
)
{
zoo
.
close
();
}
}
}
src/main/resources/log4j.properties
0 → 100644
View file @
6715a099
log4j.rootLogger
=
ERROR, stdout
log4j.appender.stdout
=
org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout
=
org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern
=
%m%n
# Silence the Zookeeper client
log4j.logger.org.apache.zookeeper
=
ERROR
log4j.logger.org.apache.zookeeper.ClientCnxn
=
ERROR
log4j.logger.org.apache.zookeeper.ZooKeeper
=
ERROR
src/test/java/com/ds/LeaderElection/AppTest.java
0 → 100644
View file @
6715a099
package
com
.
ds
.
LeaderElection
;
import
junit.framework.Test
;
import
junit.framework.TestCase
;
import
junit.framework.TestSuite
;
/**
* Unit test for simple App.
*/
public
class
AppTest
extends
TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public
AppTest
(
String
testName
)
{
super
(
testName
);
}
/**
* @return the suite of tests being tested
*/
public
static
Test
suite
()
{
return
new
TestSuite
(
AppTest
.
class
);
}
/**
* Rigourous Test :-)
*/
public
void
testApp
()
{
assertTrue
(
true
);
}
}
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