Commit 2bf3bf1e authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

Fix Bug

parent ed0a986a
......@@ -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
```
......
......@@ -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 = 30000;
private static final String LOCKS = "/locksM";
private String myCandidate;
private ZooKeeper zooKeeper;
......@@ -21,42 +20,43 @@ public class Locks implements Watcher {
Locks locks = new Locks();
locks.connectToZookeeper();
locks.CheckLock();
locks.initLock();
locks.getLockOrWait();
locks.run();
locks.close();
}
private void CheckLock() throws KeeperException, InterruptedException {
private void initLock() 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.NodeChildrenChanged) {
if (watchedEvent.getType() == Event.EventType.NodeDeleted) {
try {
getLockOrWait();
} catch (KeeperException e) {
......
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