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
Hide 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
...
@@ -11,8 +11,7 @@ This project demonstrates a simple implementation of distributed locking using A
-
[
Usage
](
#usage
)
-
[
Usage
](
#usage
)
-
[
Dependencies
](
#dependencies
)
-
[
Dependencies
](
#dependencies
)
-
[
Running the Project
](
#running-the-project
)
-
[
Running the Project
](
#running-the-project
)
-
[
Contributing
](
#contributing
)
-
[
License
](
#license
)
## Overview
## Overview
...
@@ -76,9 +75,9 @@ To use this distributed locking mechanism in your project, follow these steps:
...
@@ -76,9 +75,9 @@ To use this distributed locking mechanism in your project, follow these steps:
mvn clean
install
mvn clean
install
```
```
3.
**
Run the Application
**
:
3.
**
Run the Application
three
times
to simulate three services trying to get the lock
**
:
```
bash
```
bash
java
-cp
target/DLock-1.0-SNAPSHOT.jar org.ds.Locks
java
-cp
target/DLock-1.0-SNAPSHOT.jar org.ds.Locks
```
```
## Dependencies
## Dependencies
...
...
src/main/java/org/ds/Locks.java
View file @
2bf3bf1e
...
@@ -2,7 +2,6 @@ package org.ds;
...
@@ -2,7 +2,6 @@ package org.ds;
import
org.apache.zookeeper.*
;
import
org.apache.zookeeper.*
;
import
org.apache.zookeeper.data.Stat
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.Collections
;
import
java.util.Collections
;
...
@@ -11,8 +10,8 @@ import java.util.List;
...
@@ -11,8 +10,8 @@ import java.util.List;
public
class
Locks
implements
Watcher
{
public
class
Locks
implements
Watcher
{
private
static
final
String
ADDRESS
=
"172.29.3.101:2181"
;
private
static
final
String
ADDRESS
=
"172.29.3.101:2181"
;
private
static
final
int
SESSION_TIMEOUT
=
3000
;
private
static
final
int
SESSION_TIMEOUT
=
3000
0
;
private
static
final
String
LOCKS
=
"/locks"
;
private
static
final
String
LOCKS
=
"/locks
M
"
;
private
String
myCandidate
;
private
String
myCandidate
;
private
ZooKeeper
zooKeeper
;
private
ZooKeeper
zooKeeper
;
...
@@ -21,42 +20,43 @@ public class Locks implements Watcher {
...
@@ -21,42 +20,43 @@ public class Locks implements Watcher {
Locks
locks
=
new
Locks
();
Locks
locks
=
new
Locks
();
locks
.
connectToZookeeper
();
locks
.
connectToZookeeper
();
locks
.
Check
Lock
();
locks
.
init
Lock
();
locks
.
getLockOrWait
();
locks
.
getLockOrWait
();
locks
.
run
();
locks
.
run
();
locks
.
close
();
locks
.
close
();
}
}
private
void
Check
Lock
()
throws
KeeperException
,
InterruptedException
{
private
void
init
Lock
()
throws
KeeperException
,
InterruptedException
{
if
(
zooKeeper
.
exists
(
LOCKS
,
false
)
==
null
)
{
if
(
zooKeeper
.
exists
(
LOCKS
,
false
)
==
null
)
{
zooKeeper
.
create
(
LOCKS
,
new
byte
[
0
],
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
PERSISTENT
);
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
);
String
path
=
zooKeeper
.
create
(
LOCKS
+
"/"
+
"lock-"
,
new
byte
[]{},
ZooDefs
.
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
EPHEMERAL_SEQUENTIAL
);
myCandidate
=
path
.
replace
(
LOCKS
+
"/"
,
""
);
myCandidate
=
path
.
replace
(
LOCKS
+
"/"
,
""
);
System
.
out
.
println
(
"myCandidate "
+
myCandidate
);
}
}
private
void
getLockOrWait
()
throws
KeeperException
,
InterruptedException
{
private
void
getLockOrWait
()
throws
KeeperException
,
InterruptedException
{
CheckLock
();
Stat
stat
=
null
;
String
predecessor
=
""
;
String
predecessor
=
""
;
while
(
stat
==
null
)
{
List
<
String
>
children
=
zooKeeper
.
getChildren
(
LOCKS
,
false
);
List
<
String
>
children
=
zooKeeper
.
getChildren
(
LOCKS
,
false
);
System
.
out
.
println
(
"List of children "
+
children
);
// System.out.println(children);
Collections
.
sort
(
children
);
Collections
.
sort
(
children
);
if
(
children
.
get
(
0
).
equals
(
myCandidate
))
{
if
(
children
.
get
(
0
).
equals
(
myCandidate
))
{
System
.
out
.
println
(
"I acquired a lock :). will leave it in 10 seconds"
);
System
.
out
.
println
(
"I acquired a lock :). will leave it in 10 seconds"
);
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
System
.
out
.
println
(
"leaving in "
+
(
10
-
i
)
+
"seconds"
);
System
.
out
.
println
(
"leaving in "
+
i
+
"seconds"
);
Thread
.
sleep
(
1000
);
Thread
.
sleep
(
1000
);
}
}
zooKeeper
.
delete
(
LOCKS
+
"/"
+
myCandidate
,
-
1
);
zooKeeper
.
delete
(
LOCKS
+
"/"
+
children
.
get
(
0
),
-
1
);
}
else
{
}
else
{
System
.
out
.
println
(
"i could not acquire a lock. So will wait"
);
System
.
out
.
println
(
"i could not acquire a lock. So will wait"
);
int
predecessorIndex
=
Collections
.
binarySearch
(
children
,
myCandidate
)
-
1
;
int
predecessorIndex
=
Collections
.
binarySearch
(
children
,
myCandidate
)
-
1
;
predecessor
=
children
.
get
(
predecessorIndex
);
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 {
...
@@ -78,7 +78,7 @@ public class Locks implements Watcher {
@Override
@Override
public
void
process
(
WatchedEvent
watchedEvent
)
{
public
void
process
(
WatchedEvent
watchedEvent
)
{
if
(
watchedEvent
.
getType
()
==
Event
.
EventType
.
Node
ChildrenChang
ed
)
{
if
(
watchedEvent
.
getType
()
==
Event
.
EventType
.
Node
Delet
ed
)
{
try
{
try
{
getLockOrWait
();
getLockOrWait
();
}
catch
(
KeeperException
e
)
{
}
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