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
816f8d72
Commit
816f8d72
authored
Dec 03, 2023
by
mohammad.salama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Herd Effect Solution on Deletion
parent
ad3fde9b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
169 additions
and
27 deletions
+169
-27
ZookeeperProject_jar.xml
.idea/artifacts/ZookeeperProject_jar.xml
+31
-0
workspace.xml
.idea/workspace.xml
+7
-1
ZookeeperProject.jar
ZookeeperProject.jar
+0
-0
LeaderElection.java
src/LeaderElection.java
+128
-26
MANIFEST.MF
src/META-INF/MANIFEST.MF
+3
-0
No files found.
.idea/artifacts/ZookeeperProject_jar.xml
0 → 100644
View file @
816f8d72
<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
.idea/workspace.xml
View file @
816f8d72
<?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 @@
"
RunOnceActivity.OpenProjectViewOnStart
"
:
"
true
"
,
"
RunOnceActivity.ShowReadmeOnStart
"
:
"
true
"
,
"
last_opened_file_path
"
:
"
D:/HIAST/FIY/FS/Distributed Systems/Lab/5/Session/ZookeeperProject/zookeeper-3.9.1-dependency
"
,
"
project.structure.last.edited
"
:
"
Module
s
"
,
"
project.structure.last.edited
"
:
"
Artifact
s
"
,
"
project.structure.proportion
"
:
"
0.15
"
,
"
project.structure.side.proportion
"
:
"
0.2
"
}
...
...
ZookeeperProject.jar
0 → 100644
View file @
816f8d72
File added
src/LeaderElection.java
View file @
816f8d72
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
{
S
ystem
.
out
.
println
(
"Disconnected"
);
synchronized
(
zooKeeper
)
S
tat
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
src/META-INF/MANIFEST.MF
0 → 100644
View file @
816f8d72
Manifest-Version: 1.0
Main-Class: LeaderElection
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