Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
Z
Zookeeper-DS05
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
mohammad.salama
Zookeeper-DS05
Commits
e7046cc9
You need to sign in or sign up before continuing.
Commit
e7046cc9
authored
Dec 04, 2023
by
mohammad.salama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Herd Effect in zookeeper solution
parent
816f8d72
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
41 deletions
+70
-41
workspace.xml
.idea/workspace.xml
+28
-0
ZookeeperProject.jar
ZookeeperProject.jar
+0
-0
LeaderElection.java
src/LeaderElection.java
+42
-41
No files found.
.idea/workspace.xml
View file @
e7046cc9
...
...
@@ -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"
>
...
...
ZookeeperProject.jar
View file @
e7046cc9
No preview for this file type
src/LeaderElection.java
View file @
e7046cc9
...
...
@@ -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
(
Keeper
Exception
e
)
catch
(
Interrupted
Exception
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
(
Keeper
Exception
e
)
catch
(
Interrupted
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
Interrupted
Exception
e
)
catch
(
Keeper
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
break
;
}
break
;
default
:
break
;
}
}
}
\ No newline at end of file
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