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 ...@@ -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
......
...@@ -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 = 30000;
private static final String LOCKS = "/locks"; private static final String LOCKS = "/locksM";
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.CheckLock(); locks.initLock();
locks.getLockOrWait(); locks.getLockOrWait();
locks.run(); locks.run();
locks.close(); locks.close();
} }
private void CheckLock() throws KeeperException, InterruptedException { private void initLock() 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.NodeChildrenChanged) { if (watchedEvent.getType() == Event.EventType.NodeDeleted) {
try { try {
getLockOrWait(); getLockOrWait();
} catch (KeeperException e) { } 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