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

Herd Effect in zookeeper solution

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