Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_server( | |
self, | |
server_id: ServerId, | |
token_count: int, | |
) -> None: | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConsistentHashingWithTokens: | |
def __init__( | |
self, | |
server_tokens: Dict[ServerId, int], | |
seed: int = SEED, | |
): | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder