Skip to content

Instantly share code, notes, and snippets.

def consistent_hashing_same_allocations(
n: int,
urls: List[str],
ch_cls: ConsistentHashing,
) -> float:
ch = ch_cls(server_ids={random_str() for _ in range(n)})
allocs = {url: ch.key_lookup(key=url) for url in urls}
ch.add_server(server_id=random_str())
return sum(
allocs[url] == ch.key_lookup(key=url)
def update_server(
self,
server_id: ServerId,
token_count: int,
) -> None:
if token_count < 1:
raise ValueError(
f"make sure that token_count >= 1"
)
self._remove_server_tokens(server_id=server_id)
class ConsistentHashingWithTokens:
def __init__(
self,
server_tokens: Dict[ServerId, int],
seed: int = SEED,
):
assert all(tokens >= 1 for tokens in server_tokens.values())
self._seed = seed
self._server_tokens = server_tokens
self._update_token_hashes()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.