Distributed Systems Course, implementation of Chord DHT in node.js
A simple Distributed Hash Table made in NodeJS
This is just an experiment as a coursework for Distributed Systems class on National Technical University of Athens. The goal is to implement a simple DHT using some defined rules:
- There must always be a ring of nodes, which every node has a pointer to the next one and another pointer to the previous one
- If a node is alone then it points to no one
- If there are only two nodes, then them both point to each other
- File retrieval must be routed through the ring until it finds the specified file and returns it to the sender, this differs from the original DHT implementation since it does not guarantee a O(nlogn) search time
- To join the network, the node must know at least one other node which has already joined the network, otherwise it will create a new DHT.
- We are not assuming failures, all nodes work to perfection and grecefuly exits the network at all times.
You need to have Node.js installed (newest edition, version 14 and after), see Node's official website.
After that, run npm install
inside the folder
The main entrypoint is the index.js
file. It accepts a single argument from the command line, the known hostlist, this will tell the node to connect to an existing network of other nodes. This list can be comma separated and the program will try to connect to each one of them, the first one to answer will be the entrypoint to the network.
If no nodes respond, a new network will be created.
- Running as first node:
node index.js replication X type Y
X can take any number from 1 to n and Y can take two values: either "chain-replication" or "eventual consistency" - Try to connect to an existing node:
node index.js localhost:<port>
ornode index.js <ip_address>:<port>
Every node must contain 3 base informations:
- The next node
- The previous node
- The node ID
Previous and next nodes are objects with 3 properties: ip
, port
and id
. The IP and port are String and Int respectively, the ID is a sha1
hash computed on hashFactory using a fixed password and ip:port
as base data.
{
port: Number,
ip: String,
id: String
}
The folder structure looks like the following:
- src: Contains all the source files
- config: Contains the message strings to all commands
- consoleCommands: That is the client side implementation of all the interactive commands the user can issue at the terminal, in short, the files here are what should happen if you issue a valid command
- messages: Contains the implementation of all messages from the receiver perspective (aKa, what should happen if I receive such message)
- utils: Utility wrappers
- node.js: Main node file, contains all definitions of a node
When the node connects you can type help
into the terminal to get a list of all available commands along with their params.
Inserts key value pair inside Chord
Queries key inside Chord
Gracefully departs node
Deletes key value pair from the Chord
Gives an overview of the Chord network
Explains all these commands