Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit aa05f85

Browse files
committed
Add initial commit.
MIT licensed. Documentation. Some tests. Chord distributed hash table implementation.
0 parents  commit aa05f85

File tree

5 files changed

+675
-0
lines changed

5 files changed

+675
-0
lines changed

LICENSE.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
=====================
3+
4+
Copyright (c) 2013 Optimizely, Inc.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

README.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
Chord - Distributed Hash Table
2+
==============================
3+
4+
[Chord] [1] is a self-organizing distributed hash table. This is an implementation of the Chord
5+
algorithm in Node.js. It provides the ability to construct a Chord cluster and to
6+
route application layer messages to a process responsible
7+
8+
It supports virtual nodes and uses UDP as its out-of-process transport layer.
9+
10+
# API
11+
12+
## chord.hash(string)
13+
14+
Occasionally, you may wish to hash a value the same way that Chord does. It currently
15+
uses Murmurhash3-128, but `hash` will always expose the current hash algorithm.
16+
17+
## chord.Chord(listen_port, virtual_node_count, node_to_join, on_message)
18+
## chord.Chord(listen_port, virtual_node_count, on_message)
19+
20+
The primary entry point for starting a Chord server. The available arguments are:
21+
22+
* `listen_port`: The UDP port on which to listen.
23+
* `virtual_node_count`: The number of virtual nodes to start.
24+
* `node_to_join`: The address of a node to join (optional).
25+
* `on_message`: A callback function that is notified when a message addressed to a
26+
local virtual node is received.
27+
28+
The returned value is a `send_message` function. The `send_message` function also has a
29+
`close` property, which is a function to shut down the local virtual nodes.
30+
31+
var server = chord.Chord(...); // start the server
32+
server(...) // send a message (see below)
33+
server.close() // stop the server
34+
35+
## send_message(to, id, message, reply_to)
36+
37+
This is the function for sending a new application level message to another node in the
38+
Chord ring. It takes the following parameters:
39+
40+
* `to`: Optional (must be null if not used). The address of the node to which to send.
41+
* `id`: The key of the DHT to which to address the message. The node which is currently
42+
responsible for that point in the hash ring will receive the message.
43+
* `message`: A JSON-able object, which will be send to the recipient.
44+
* `reply_to`: Optional. The address of the node for the recipient to reply to. This may be
45+
used as a way to share the identity of a node with another node. If the recipient replies,
46+
their message will be sent to the `reply_to` node. If no `reply_to` is specified, replies
47+
return to the original sender.
48+
49+
50+
51+
## chord.Client(on_message, listen_port)
52+
53+
The client is intended to provide an easy way to have non-members of the Chord ring
54+
communicate with members of the Chord ring. This is useful if, for example, members of the
55+
Chord ring provide a data storage service, and clients of this data storage service need
56+
to make requests of it without themselves storing data.
57+
58+
A client is really just a completely local Chord ring that never joins any other node. As such,
59+
it has all of the machinery necessary to speak the Chord wire protocol to another Chord ring.
60+
61+
* `on_message`: The callback that is notified when a message is received.
62+
* `listen_port`: The port on which to listen for reply messages.
63+
64+
## on_message(from, id, message, reply)
65+
66+
Whenever a client or server receives a message, its `on_message` callback is triggered. The callback
67+
receives a few parameters:
68+
69+
* `from`: The address of the sender. There is a `from.id` property, which is the point in the hash
70+
ring of the sender's identity.
71+
* `id`: The key to which the message was sent.
72+
* `message`: The application layer message that was received.
73+
* `reply`: A callback for sending a reply message (see below).
74+
75+
## reply(message, to, reply_to)
76+
77+
Send a message to source (or `reply_to`) of a received message.
78+
79+
* `message`: The message to send back.
80+
* `to`: Optional. The address of the node to which to send the reply.
81+
* `reply_to`: Optional. The address to which the recipient will reply. If not specified,
82+
any reply will return to the originator of the message. One useful pattern is to
83+
pass `from` as the `reply_to`, which will cause the next reply to also go to the originator
84+
of the request. This can permit multi-stage operations without having to route the final reply
85+
through intermediate nodes.
86+
87+
Setting up a cluster
88+
--------------------
89+
When you set up a cluster, you will first create an initial node. The only thing that
90+
distinguishes your initial node from any other node is that it does not join any other
91+
node. Subsequent nodes join any existing node. The Chord protocol will distribute the
92+
existence of new nodes around the ring automatically.
93+
94+
To create a new node:
95+
96+
var chord = require('chord');
97+
chord.Chord(1234, // Listen on port 1234
98+
10, // Start 10 virtual nodes
99+
on_message); // Call the function 'on_message' when we receive something
100+
101+
To connect a subsequent node:
102+
103+
chord.Chord(1235,
104+
10,
105+
{address:'127.0.0.1', port:1234}, // Connect to the existing server on port 1234
106+
on_message_2); // Provide a callback for receiving.
107+
108+
All 20 virtual nodes will talk amongst themselves to arrange themselves in a ring. Subsequent
109+
messages will be delivered the node that owns a particular key at that time. Note that due to
110+
nodes leaving an joining, the node that owns a particular key may change over time. Your application
111+
should be designed to expect this.
112+
113+
[1] http://pdos.csail.mit.edu/papers/chord:sigcomm01/chord_sigcomm.pdf "Chord: A Scalable Peer-to-peer Lookup Service for Internet Applications"

0 commit comments

Comments
 (0)