This project is a simulation of consistent hashing using Zig and Raylib. The application visualizes how a hash ring distributes nodes and routes requests to these nodes based on hash values. It demonstrates the addition and removal of nodes in the ring and shows how requests are routed.
chsim.mp4
To build and run this project, you'll need:
- Zig: The Zig programming language. You can download it from the official Zig website.
- Raylib: A C library for creating games and multimedia applications. You need to have Raylib installed and properly set up for Zig. You can find the library here.
-
Clone the Repository:
git clone https://github.com/basilysf1709/chsim.git cd https://github.com/basilysf1709/chsim.git
-
Install Raylib:
Follow the instructions on the Raylib installation page. Ensure that
raylib.h
is accessible for your Zig build. -
Build & Run the Project:
The project uses Zig’s build system. To build the application, run:
zig build run
Once the application is running, you can interact with the simulation using the following controls:
- Press 'A': Add a new virtual node to the hash ring.
- Press 'D': Remove a virtual node from the hash ring.
- Press SPACE: Generate a request with a random key and visualize its routing to the appropriate node.
VirtualNode
: Represents a virtual node in the hash ring with properties likeid
,parent_id
,position
,name
, andip
.HashRing
: Manages the collection of virtual nodes and provides functionality to add, remove, and find nodes. It also sorts the nodes by their position on the ring.
HashRing.init
: Initializes the hash ring with an allocator and a specified number of virtual nodes per node.HashRing.addNode
: Adds a new node to the hash ring and sorts the nodes.HashRing.removeNode
: Removes nodes from the hash ring if the number exceeds the specified limit.HashRing.findNode
: Finds the appropriate node for a given key based on its hash value.HashRing.hash
: Computes a hash value for a given key.
- Nodes: Displayed as purple circles on a ring. Their positions on the ring are determined by their hash values.
- Requests: Represented as red circles on the ring. The path from the request to the target node is visualized with a red arc.
Feel free to contribute to this project by submitting issues, improvements, or pull requests. Your feedback and suggestions are welcome! Any kinds of contributions are welcome
This project is licensed under the MIT License. See the LICENSE file for details.