Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
D
Distributed Lock
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
mohamadbashar.disoki
Distributed Lock
Commits
2bf3bf1e
Commit
2bf3bf1e
authored
Nov 28, 2024
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix Bug
parent
ed0a986a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
28 deletions
+27
-28
Readme.md
Readme.md
+3
-4
Locks.java
src/main/java/org/ds/Locks.java
+24
-24
No files found.
Readme.md
View file @
2bf3bf1e
...
...
@@ -11,8 +11,7 @@ This project demonstrates a simple implementation of distributed locking using A
-
[
Usage
](
#usage
)
-
[
Dependencies
](
#dependencies
)
-
[
Running the Project
](
#running-the-project
)
-
[
Contributing
](
#contributing
)
-
[
License
](
#license
)
## Overview
...
...
@@ -76,7 +75,7 @@ To use this distributed locking mechanism in your project, follow these steps:
mvn clean
install
```
3.
**
Run the Application
**
:
3.
**
Run the Application
three
times
to simulate three services trying to get the lock
**
:
```
bash
java
-cp
target/DLock-1.0-SNAPSHOT.jar org.ds.Locks
```
...
...
src/main/java/org/ds/Locks.java
View file @
2bf3bf1e
...
...
@@ -2,7 +2,6 @@ package org.ds;
import
org.apache.zookeeper.*
;
import
org.apache.zookeeper.data.Stat
;
import
java.io.IOException
;
import
java.util.Collections
;
...
...
@@ -11,8 +10,8 @@ import java.util.List;
public
class
Locks
implements
Watcher
{
private
static
final
String
ADDRESS
=
"172.29.3.101:2181"
;
private
static
final
int
SESSION_TIMEOUT
=
3000
;
private
static
final
String
LOCKS
=
"/locks"
;
private
static
final
int
SESSION_TIMEOUT
=
3000
0
;
private
static
final
String
LOCKS
=
"/locks
M
"
;
private
String
myCandidate
;
private
ZooKeeper
zooKeeper
;
...
...
@@ -21,42 +20,43 @@ public class Locks implements Watcher {
Locks
locks
=
new
Locks
();
locks
.
connectToZookeeper
();
locks
.
Check
Lock
();
locks
.
init
Lock
();
locks
.
getLockOrWait
();
locks
.
run
();
locks
.
close
();
}
private
void
Check
Lock
()
throws
KeeperException
,
InterruptedException
{
private
void
init
Lock
()
throws
KeeperException
,
InterruptedException
{
if
(
zooKeeper
.
exists
(
LOCKS
,
false
)
==
null
)
{
zooKeeper
.
create
(
LOCKS
,
new
byte
[
0
],
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
PERSISTENT
);
}
String
path
=
zooKeeper
.
create
(
LOCKS
+
"/"
+
"lock-"
,
new
byte
[]{},
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
EPHEMERAL_SEQUENTIAL
);
myCandidate
=
path
.
replace
(
LOCKS
+
"/"
,
""
);
System
.
out
.
println
(
"myCandidate "
+
myCandidate
);
}
private
void
getLockOrWait
()
throws
KeeperException
,
InterruptedException
{
CheckLock
();
Stat
stat
=
null
;
String
predecessor
=
""
;
while
(
stat
==
null
)
{
List
<
String
>
children
=
zooKeeper
.
getChildren
(
LOCKS
,
false
);
// System.out.println(
children);
System
.
out
.
println
(
"List of children "
+
children
);
Collections
.
sort
(
children
);
if
(
children
.
get
(
0
).
equals
(
myCandidate
))
{
System
.
out
.
println
(
"I acquired a lock :). will leave it in 10 seconds"
);
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
System
.
out
.
println
(
"leaving in "
+
i
+
"seconds"
);
System
.
out
.
println
(
"leaving in "
+
(
10
-
i
)
+
"seconds"
);
Thread
.
sleep
(
1000
);
}
zooKeeper
.
delete
(
LOCKS
+
"/"
+
children
.
get
(
0
)
,
-
1
);
zooKeeper
.
delete
(
LOCKS
+
"/"
+
myCandidate
,
-
1
);
}
else
{
System
.
out
.
println
(
"i could not acquire a lock. So will wait"
);
int
predecessorIndex
=
Collections
.
binarySearch
(
children
,
myCandidate
)
-
1
;
predecessor
=
children
.
get
(
predecessorIndex
);
stat
=
zooKeeper
.
exists
(
LOCKS
+
"/"
+
predecessor
,
this
);
//Check Race Condition
if
(
zooKeeper
.
exists
(
LOCKS
+
"/"
+
predecessor
,
this
)
==
null
)
{
getLockOrWait
();
}
}
}
...
...
@@ -78,7 +78,7 @@ public class Locks implements Watcher {
@Override
public
void
process
(
WatchedEvent
watchedEvent
)
{
if
(
watchedEvent
.
getType
()
==
Event
.
EventType
.
Node
ChildrenChang
ed
)
{
if
(
watchedEvent
.
getType
()
==
Event
.
EventType
.
Node
Delet
ed
)
{
try
{
getLockOrWait
();
}
catch
(
KeeperException
e
)
{
...
...
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