Commit e7046cc9 authored by mohammad.salama's avatar mohammad.salama

Herd Effect in zookeeper solution

parent 816f8d72
......@@ -42,6 +42,34 @@
<recent name="D:\HIAST\FIY\FS\Distributed Systems\Lab\5\Session\ZookeeperProject" />
</key>
</component>
<component name="RunManager" selected="Application.C3">
<configuration name="C1" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="LeaderElection" />
<module name="ZookeeperProject" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="C2" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="LeaderElection" />
<module name="ZookeeperProject" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="C3" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="LeaderElection" />
<module name="ZookeeperProject" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="Application.C1" />
<item itemvalue="Application.C2" />
<item itemvalue="Application.C3" />
</list>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
......
No preview for this file type
......@@ -2,6 +2,7 @@ import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
......@@ -27,38 +28,42 @@ public class LeaderElection implements Watcher
leaderElection.connectToZookeeper();
leaderElection.volunteerForLeader();
leaderElection.electLeader();
System.out.println(leaderElection.leader);
leaderElection.run();
leaderElection.close();
}
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
synchronized (zooKeeper)
{
prevZnode = children.get(children.indexOf(currentZnodeName)-1);
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;
Stat prevExist = zooKeeper.exists(prevZnodeFullName , this);
}
System.out.println("prevZnode from ElectLeader : " + prevZnode);
setLeaderInData();
}
prevZnodeFullName = ELECTION_NAMESPACE+"/"+prevZnode;
setLeaderInData();
}
private void setLeaderInData() throws InterruptedException, KeeperException
{
Stat temp = zooKeeper.exists(currentZondeFullName , false);
synchronized (zooKeeper){
zooKeeper.setData(
currentZondeFullName,
leader.getBytes(),
temp.getVersion()
);
-1);
System.out.println("The Leader is Set in Node's Data");
System.out.println("The Leader is : " + leader);
System.out.println("The Leader is : " + leader);}
}
private void volunteerForLeader() throws InterruptedException, KeeperException
......@@ -67,6 +72,7 @@ public class LeaderElection implements Watcher
String path = zooKeeper.create(znodePref , new byte[] {} , ZooDefs.Ids.OPEN_ACL_UNSAFE , CreateMode.EPHEMERAL_SEQUENTIAL);
currentZondeFullName = path;
this.currentZnodeName = path.replace(ELECTION_NAMESPACE + "/" , "");
System.out.println("I am Node " + currentZnodeName);
}
private void close()
......@@ -93,22 +99,12 @@ public class LeaderElection implements Watcher
}
}
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: {
case None:
{
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
System.out.println("Successfully connected to ZooKeeper");
} else if (watchedEvent.getState() == Event.KeeperState.Closed) {
......@@ -123,6 +119,7 @@ public class LeaderElection implements Watcher
zooKeeper.notifyAll();
}
}
break;
}
......@@ -130,44 +127,48 @@ public class LeaderElection implements Watcher
{
try
{
Stat prevExist = zooKeeper.exists(prevZnodeFullName , false);
if (prevExist == null && leader==prevZnode)
System.out.println("Detecting Node Deleted ");
System.out.println("prev full name is : " + prevZnodeFullName);
///Stat prevExist = zooKeeper.exists(prevZnodeFullName, this);
if (leader.equals(prevZnode))
{
this.leader = currentZnodeName;
prevZnode = currentZnodeName;
prevZnodeFullName = currentZondeFullName;
setLeaderInData();
}
}
catch (KeeperException e)
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
catch (InterruptedException e)
{
catch (KeeperException e) {
throw new RuntimeException(e);
}
break;
}
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();
System.out.println("Detecting Node Data Changed");
byte[] newLeader = zooKeeper.getData(prevZnodeFullName, this, new Stat());
this.leader = new String(newLeader, StandardCharsets.UTF_8);
setLeaderInData();
}
catch (KeeperException e)
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
catch (InterruptedException e)
catch (KeeperException e)
{
throw new RuntimeException(e);
}
break;
}
break;
default:break;
}
}
}
\ No newline at end of file
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