Commit 6715a099 authored by drnull03's avatar drnull03

damn that was fun finished this project

parents
/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
<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>
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();
}
}
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) {
}
}
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();
}
}
}
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
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 );
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment