Demo code for the ZooKeeper blog - Apache ZooKeeper Meets the Dining Philosophers.
This project demonstrates the application of Apache ZooKeeper in solving the classic Dining Philosophers problem, a common synchronization issue in concurrent programming. By utilizing ZooKeeper's distributed coordination capabilities, the project showcases how to prevent deadlocks and manage resource allocation efficiently in distributed systems. The sample code provides a practical example of implementing distributed locks and ephemeral nodes to synchronize actions across multiple processes.
- Distributed Coordination: Utilizes ZooKeeper to coordinate actions across distributed systems, preventing deadlocks
- Implementation of Distributed Locks: Demonstrates the use of distributed locks (via Curator InterProcessSemaphoreMutex) to manage resource access among multiple processes
- Ephemeral Nodes: Uses ZooKeeper's ephemeral nodes to ensure that locks are automatically released if a process fails
- Resource Allocation Management: Shows how to efficiently allocate resources in a distributed environment
- Comprehensive Metrics: Tracks meals, attempted meals, fork utilization, wait/eat/think time, and leader election frequency
- Clone the repository and navigate to the project directory.
- Build the project using Maven:
mvn clean install
- Start a ZooKeeper instance (local or remote).
- Update the ZooKeeper connection string in
ZookeeperMeetsDiningPhilosophers.javaif not using localhost:// Default: 127.0.0.1:2181 // For Instaclustr: "node1:2181,node2:2181,node3:2181"
- Run the main class:
java -cp target/*.jar ZookeeperMeetsDiningPhilosophers
The program runs 5 philosophers for 10 seconds by default and prints comprehensive statistics at completion, including total meals, fork utilization percentages, and per-philosopher metrics.
- Java 8 or higher
- Apache Maven for building the project
- Apache ZooKeeper (either running locally on
127.0.0.1:2181or a remote instance such as Instaclustr managed ZooKeeper)
This project is a standalone demo. To run against a remote ZooKeeper ensemble:
- Provision a ZooKeeper cluster (e.g., via Instaclustr).
- Update the connection string in the source code with your cluster's node addresses.
- Ensure your machine's IP is allowed in the cluster firewall configuration.
- Build and run as described in Getting Started.
- Paul Brebner - Initial work - NetApp Instaclustr
See also the list of MAINTAINERS who participated in projects in this repository.
This project is licensed under the Apache 2.0 license.