This project demonstrates a simple implementation of distributed locking using Apache ZooKeeper. The `Locks` class provides a mechanism to acquire and release locks in a distributed environment.
## Table of Contents
-[Overview](#overview)
-[Prerequisites](#prerequisites)
-[Project Structure](#project-structure)
-[How It Works](#how-it-works)
-[Usage](#usage)
-[Dependencies](#dependencies)
-[Running the Project](#running-the-project)
-[Contributing](#contributing)
-[License](#license)
## Overview
Distributed locking is a critical mechanism in distributed systems to ensure that only one process can access a resource at a time. This project uses Apache ZooKeeper, a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.
## Prerequisites
- Java 17
- Apache Maven
- Apache ZooKeeper (running on `172.29.3.101:2181`)
## Project Structure
```
/src
/main
/java
/org
/ds
Locks.java
/pom.xml
README.md
```
-**Locks.java**: The main class that implements the distributed locking mechanism.
-**pom.xml**: Maven configuration file that includes the necessary dependencies.
## How It Works
### Locks.java
The `Locks` class implements the distributed locking mechanism using ZooKeeper. Here's a breakdown of the key methods:
-**connectToZookeeper()**: Connects to the ZooKeeper instance.
-**CheckLock()**: Checks if the `/locks` node exists in ZooKeeper. If not, it creates the node. It then creates an ephemeral sequential node under `/locks`.
-**getLockOrWait()**: Attempts to acquire the lock. If the current node is the smallest in the sequence, it acquires the lock. Otherwise, it waits for the predecessor node to be deleted.
-**run()**: Keeps the ZooKeeper connection alive by waiting on the ZooKeeper instance.
-**close()**: Closes the ZooKeeper connection.
-**process(WatchedEvent watchedEvent)**: Handles events from ZooKeeper, such as node deletion, to retry acquiring the lock.
### Main Execution Flow
1.**Connection**: The `main` method initializes the `Locks` instance and connects to ZooKeeper.
2.**Lock Acquisition**: The `CheckLock` method is called to create the necessary nodes. The `getLockOrWait` method is then called to attempt to acquire the lock.
3.**Lock Holding**: If the lock is acquired, the program holds the lock for 10 seconds and then releases it.
4.**Lock Waiting**: If the lock is not acquired, the program waits for the predecessor node to be deleted.
5.**Cleanup**: The ZooKeeper connection is closed.
## Usage
To use this distributed locking mechanism in your project, follow these steps: