AI-Assisted Coding Lab · Route Tenants With a Hash Ring

45:00left of 45:00

Route Tenants With a Hash Ring

Tenants are spread across database shards by a consistent hash ring that two services, in two languages, must compute identically. The README has the ring's rules and why every routing change is a data migration. Build shards.py with the assistant, and judge each suggestion by what it would do to tenants' data.

shards.py1 of 3

Files

shards.py

Python · Editable
import bisect
import hashlib
def point(key):
"""A position on the ring: the first 8 bytes of SHA-256, big-endian. The Go billing service computes the same."""
raise NotImplementedError
class ShardRing:
"""Maps tenants to database shards with consistent hashing."""
def __init__(self, shards, vnodes=100):
self.vnodes = vnodes
self.points = [] # sorted ring positions
self.owner = {} # position -> shard
self.pins = {} # tenant -> shard, while a tenant's data is being moved
for shard in shards:
self.add_shard(shard)
def add_shard(self, shard):
raise NotImplementedError
def shard_for(self, tenant):
position = point(tenant)
raise NotImplementedError

Test Output

No runs yet.

Assistant

Scripted
  1. Send the first prompt to start.

Next Prompt

Write point. It runs on every request, so make it fast.