Commit 816f8d72 authored by mohammad.salama's avatar mohammad.salama

Herd Effect Solution on Deletion

parent ad3fde9b
<component name="ArtifactManager">
<artifact type="jar" name="ZookeeperProject:jar">
<output-path>$PROJECT_DIR$/out/artifacts/ZookeeperProject_jar</output-path>
<root id="archive" name="ZookeeperProject.jar">
<element id="module-output" name="ZookeeperProject" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/zookeeper-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/slf4j-api-1.7.30.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/commons-io-2.11.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/logback-core-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/zookeeper-jute-3.9.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/logback-classic-1.2.10.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-codec-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-buffer-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-handler-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/audience-annotations-0.12.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-resolver-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-transport-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-tcnative-classes-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-transport-classes-epoll-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-transport-native-unix-common-4.1.94.Final.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-transport-native-epoll-4.1.94.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-osx-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-linux-x86_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-osx-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-linux-aarch_64.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/zookeeper-3.9.1-dependency/netty-tcnative-boringssl-static-2.0.61.Final-windows-x86_64.jar" path-in-jar="/" />
</root>
</artifact>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ArtifactsWorkspaceSettings">
<artifacts-to-build>
<artifact name="ZookeeperProject:jar" />
</artifacts-to-build>
</component>
<component name="ChangeListManager">
<list default="true" id="c96503bf-3b6e-4ec0-9a0f-c9d3e8c6ffc7" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/LeaderElection.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/LeaderElection.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
......@@ -26,7 +32,7 @@
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;D:/HIAST/FIY/FS/Distributed Systems/Lab/5/Session/ZookeeperProject/zookeeper-3.9.1-dependency&quot;,
&quot;project.structure.last.edited&quot;: &quot;Modules&quot;,
&quot;project.structure.last.edited&quot;: &quot;Artifacts&quot;,
&quot;project.structure.proportion&quot;: &quot;0.15&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.2&quot;
}
......
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class LeaderElection implements Watcher {
private static final String address = "192.168.184.10:2181";
public class LeaderElection implements Watcher
{
private static final String myAddress = "192.168.184.10:2181";
private static final String address = "172.29.3.101:2181";
private static final int SESSION_TIMEOUT = 3000;
private String ELECTION_NAMESPACE = "/election";
private ZooKeeper zooKeeper;
private String TARET_ZNODE = "/target_znode" ;
private String currentZnodeName = "";
public static void main(String[] args) throws IOException, InterruptedException
{
private String currentZondeFullName = "";
private String prevZnode = "";
private String prevZnodeFullName = "";
private String leader = "";
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
LeaderElection leaderElection = new LeaderElection();
leaderElection.connectToZookeeper();
leaderElection.volunteerForLeader();
leaderElection.electLeader();
System.out.println(leaderElection.leader);
leaderElection.run();
leaderElection.close();
}
private void close() {
synchronized (zooKeeper) {
public void electLeader() throws InterruptedException, KeeperException
{
List<String> children = zooKeeper.getChildren(ELECTION_NAMESPACE , this);
Collections.sort(children);
this.leader = children.get(0);
if (currentZnodeName.equals(leader))
{
prevZnode = currentZnodeName;
}
else
{
prevZnode = children.get(children.indexOf(currentZnodeName)-1);
}
prevZnodeFullName = ELECTION_NAMESPACE+"/"+prevZnode;
setLeaderInData();
}
private void setLeaderInData() throws InterruptedException, KeeperException
{
Stat temp = zooKeeper.exists(currentZondeFullName , false);
zooKeeper.setData(
currentZondeFullName,
leader.getBytes(),
temp.getVersion()
);
System.out.println("The Leader is Set in Node's Data");
System.out.println("The Leader is : " + leader);
}
private void volunteerForLeader() throws InterruptedException, KeeperException
{
String znodePref = ELECTION_NAMESPACE + "/node_";
String path = zooKeeper.create(znodePref , new byte[] {} , ZooDefs.Ids.OPEN_ACL_UNSAFE , CreateMode.EPHEMERAL_SEQUENTIAL);
currentZondeFullName = path;
this.currentZnodeName = path.replace(ELECTION_NAMESPACE + "/" , "");
}
private void close()
{
synchronized (zooKeeper)
{
try {
zooKeeper.close();
} catch (InterruptedException e) {
......@@ -27,45 +80,94 @@ public class LeaderElection implements Watcher {
}
}
}
public void connectToZookeeper() throws IOException {
this.zooKeeper = new ZooKeeper(address, SESSION_TIMEOUT, this);
public void connectToZookeeper() throws IOException
{
this.zooKeeper = new ZooKeeper(myAddress, SESSION_TIMEOUT, this);
}
public void run() throws InterruptedException {
public void run() throws InterruptedException
{
synchronized (zooKeeper)
{
zooKeeper.wait();
}
}
@Override
public boolean testTargetZnode() throws InterruptedException, KeeperException
{
Stat stat = zooKeeper.exists(TARET_ZNODE , this);
if (stat == null)
{
System.out.println(TARET_ZNODE + " not Exist");
return false;
}
System.out.println(stat.toString());
return true;
}
public void process(WatchedEvent watchedEvent)
{
switch (watchedEvent.getType())
{
case None:
if (watchedEvent.getState() == Event.KeeperState.SyncConnected)
{
case None: {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
System.out.println("Successfully connected to ZooKeeper");
}
else if (watchedEvent.getState() == Event.KeeperState.Closed)
{
} else if (watchedEvent.getState() == Event.KeeperState.Closed) {
System.out.println("Connection Closed");
synchronized (zooKeeper)
{
synchronized (zooKeeper) {
zooKeeper.notifyAll();
}
} else if (watchedEvent.getState() == Event.KeeperState.Disconnected) {
System.out.println("Disconnected");
synchronized (zooKeeper) {
zooKeeper.notifyAll();
}
}
else if (watchedEvent.getState() == Event.KeeperState.Disconnected)
}
case NodeDeleted:
{
try
{
System.out.println("Disconnected");
synchronized (zooKeeper)
Stat prevExist = zooKeeper.exists(prevZnodeFullName , false);
if (prevExist == null && leader==prevZnode)
{
zooKeeper.notifyAll();
this.leader = currentZnodeName;
setLeaderInData();
}
}
break;
catch (KeeperException e)
{
throw new RuntimeException(e);
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
}
case NodeDataChanged:
{
try
{
//System.out.println("ERROR IN Data CHANGED IF HAPPENED");
Stat temp = new Stat();
byte[] newLeader = zooKeeper.getData(prevZnodeFullName , false , temp);
this.leader = newLeader.toString();
setLeaderInData();
}
catch (KeeperException e)
{
throw new RuntimeException(e);
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
}
break;
}
}
}
\ No newline at end of file
Manifest-Version: 1.0
Main-Class: LeaderElection
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