Skip to content

Instantly share code, notes, and snippets.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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)
def update_server(
self,
server_id: ServerId,
token_count: int,
) -> None:
...
def alloc(
ch: ConsistentHashingWithTokens,
) -> pd.Series:
return pd.Series(
ch.key_lookup(url)
for url in urls
).value_counts(normalize=True)
ch = ConsistentHashingWithTokens({'a': 100, 'b': 400, 'c': 500})
alloc(ch=ch)
def experiment_capacity(a_tokens: int) -> float:
assert a_tokens >= 2 and a_tokens % 2 == 0
a = random_str()
b = random_str()
ch = ConsistentHashingWithTokens(
server_tokens={a: a_tokens, b: a_tokens // 2},
)
return sum(
ch.key_lookup(key=url) == a
for url in urls
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._allocate_servers()
class ConsistentHashingWithTokens:
def __init__(
self,
server_tokens: Dict[ServerId, int],
seed: int = SEED,
):
...
@dataclass(frozen=True)
class ServerToken:
server_id: ServerId
token_idx: int
_seed: int = SEED
@property
@functools.lru_cache()
def key(self) -> str:
return f"{self.node_id}-{self.token_idx}"
from collections import defaultdict
def compute_slices(
n: int,
k: int,
seed: Optional[int] = None,
) -> List[float]:
np.random.seed(seed)
tokens = np.random.rand(n * k)