Create a distributed hash table implementing the Chord protocol lookup logic. Define the finger table structure for a node and implement find_successor that runs in O(log N) by jumping through finger table entries closest to the target ID.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Implementing Chord finger table and logarithmic lookup
Provided Python code defines Node and FingerEntry classes modeling a Chord ring with finger tables, including an interval check that handles ring wraparound, and implements find_successor using closest_preceding_finger to achieve O(log N) lookups by jumping through fingers. A demonstration sets up a 3-bit ring with nodes at 1,3,5,7 and queries the successor of key 6.
Below is a minimal, self‑contained Python sketch of the Chord lookup logic. It shows a FingerEntry structure, a Node with a finger table of size m (the number of identifier bits), and the standard find_successor/closest_preceding_finger methods that achieve (O(\log N)) hops by consulting the finger table.
python
import bisect
class FingerEntry:
"""
One row of a finger table.
start: (n + 2^k) mod 2^m
node: first node that succeeds 'start' on the ring
"""
def __init__(self, start, node):
self.start = start
self.node = node
class Node:
def __init__(self, node_id, m):
self.id = node_id
self.m = m
self.ring_size = 2 ** m
self.successor = self # updated when the node joins a ring
self.predecessor = None
self.fingers = [] # list of FingerEntry
def _in_interval(self, x, a, b, inclusive_left=False, inclusive_right=True):
"""
Return True if x is in (a, b] on a modulo ring of size ring_size.
Inclusivity of endpoints is configurable.
"""
if a == b: # full circle
return True
if a < b:
if inclusive_left and inclusive_right:
return a <= x <= b
elif inclusive_left:
return a <= x < b
elif inclusive_right:
return a < x <= b
else:
return a < x < b
else:
# interval wraps around 0
if inclusive_left and inclusive_right:
return x >= a or x <= b
elif inclusive_left:
return x >= a or x < b
elif inclusive_right:
return x > a or x <= b
else:
return x > a or x < b
def closest_preceding_finger(self, target_id):
"""
Return the highest‑order finger whose node ID lies strictly between
this node and target_id on the ring.
"""
for i in reversed(range(self.m)):
finger_node = self.fingers[i].node
if finger_node is not None and \
self._in_interval(finger_node.id, self.id, target_id, False, False):
return finger_node
return self
def find_successor(self, id_):
"""
Find the node responsible for id_ by walking the finger table.
Runs in O(log N) expected hops.
"""
# trivial one‑node ring
if self.successor == self:
return self
# if id_ is in (self.id, self.successor.id], then successor is responsible
if self._in_interval(id_, self.id, self.successor.id, False, True):
return self.successor
# otherwise, forward the query to the closest preceding finger
n0 = self.closest_preceding_finger(id_)
if n0 == self:
# no better finger; ask our immediate successor
return self.successor.find_successor(id_)
return n0.find_successor(id_)
def build_finger_table(self, all_nodes):
"""
Populate this node's finger table given a sorted list of all nodes in the ring.
"""
self.fingers = []
node_ids = [n.id for n in all_nodes]
for k in range(self.m):
start = (self.id + (1 << k)) % self.ring_size
idx = bisect.bisect_left(node_ids, start)
succ = all_nodes[idx % len(all_nodes)]
self.fingers.append(FingerEntry(start, succ))
# first finger points to immediate successor
self.successor = self.fingers[0].node
# --- Example usage ---
# Build a small ring with m=3 (IDs in [0,7])
m = 3
nodes = [Node(1, m), Node(3, m), Node(5, m), Node(7, m)]
nodes.sort(key=lambda n: n.id)
# Set predecessor/successor pointers
for i, n in enumerate(nodes):
n.successor = nodes[(i + 1) % len(nodes)]
n.predecessor = nodes[(i - 1) % len(nodes)]
# Build finger tables for each node
for n in nodes:
n.build_finger_table(nodes)
# Look up the successor of some key
key = 6
start = nodes[0] # arbitrary start node
responsible = start.find_successor(key)
print(f"Successor of {key} is node {responsible.id}")
The find_successor method will traverse at most (O(\log N)) remote nodes because it always forwards the lookup to the largest finger that precedes the target ID. In a real distributed deployment the recursive calls would be RPCs to remote nodes; this example simulates them with direct method calls.