Skip to content

This project implements a Chord Distributed Hash Table (DHT) with additional features such as leader election and networking with broadcast/gossip mechanism.

onggiahuy97/chord

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chord DHT Project

About

This project implements a Chord Distributed Hash Table (DHT) with additional features such as leader election and networking with broadcast/gossip mechanism.

Project Structure

.
├── .gitignore
├── Dockerfile
├── Makefile
├── README.md
├── cleanup_port.py
├── main.py
├── requirements.txt
├── setup.py
├── chord.egg-info/
├── src/
│   ├── chord.py
│   ├── connector.py
├── test/
│   ├── playground.py
│   ├── test_failure_detector.py
│   ├── test_bully_leader_election.py
│   ├── test_chord.py
│   ├── test_network_broadcast.py
│   ├── test_gossiping.py
└── __pycache__/

Prerequisites

  • Python 3.12 or higher
  • pip (Python package installer)

Setup Instructions

1. Clone the Repository

git clone https://github.com/onggiahuy97/chord.git
cd chord

2. Create a Virtual Environment (Optional)

python -m venv venv
source venv/bin/activate  # On Windows use `venv\Scripts\activate`

3. Install Dependencies

pip install -r requirements.txt

4. Install the Package

pip install -e .

Running the Project

1. Start the Chord DHT Node

python main.py

The server will start on http://localhost:5000 by default.

1.1 Run Simulator of Chord DHT (Optional)

git clone https://github.com/onggiahuy97/chord_ui_swift

You will need macOS (macbook) to run the application.

API Endpoints

List All Nodes

GET /nodes

Response Example:

{
    "nodes": [
        {
            "id": 8,
            "successor": 15,
            "messagesCount": 1
        }
    ]
}

Join Node

POST /join

Response Example:

{
    "message": "Node joined",
    "node_id": 8
}

Error Response (400):

{
    "error": "The Chord ring is full"
}

Get Hash

GET /hash?key=<key>

Response Example:

{
    "hash": 23,
    "successor": 8
}

Insert Key-Value

POST /insert
Content-Type: application/json

Request Body:

{
    "key": "test",
    "value": "success"
}

Response Example:

{
    "message": "Key 'test' with value 'success' has been inserted",
    "key": "test",
    "value": "success",
    "hash_id": 27,
    "stored_at_node": 8
}

Get Value

GET /get/<key>

Response Example:

{
    "key": "test",
    "value": "success",
    "hash_id": 27,
    "stored_at_node": 8
}

Error Response (404):

{
    "message": "Key not found"
}

Node Leave

POST /leave/<id>

Response Example:

{
    "message": "Node '25' successfully left the ring."
}

Node Info

GET /info/<id>

Response Example:

{
    "id": 8,
    "successor": 15,
    "messages": [
        {
            "hash_id": 7,
            "key": "genre1",
            "value": "Rock"
        }
    ]
}

Running Tests

Run all tests:

make test-all

Run individual tests:

make test-chord
make test-broadcast
make test-leader-election
make test-failure-detector
make test-gossiping

Cleaning Up

Clean up processes on ports 5000-5010:

python cleanup_port.py

Docker Support

docker build -t chord .
docker run -p 5000:5000 chord

Project Components

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

About

This project implements a Chord Distributed Hash Table (DHT) with additional features such as leader election and networking with broadcast/gossip mechanism.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages