| 1 |
import random |
| 2 |
from typing import Callable, Dict, List, Tuple |
| 3 |
import time |
| 4 |
|
| 5 |
from azure.kusto.data.exceptions import KustoClientError |
| 6 |
from azure.kusto.ingest._ranked_storage_account import _RankedStorageAccount |
| 7 |
|
| 8 |
|
| 9 |
class _RankedStorageAccountSet: |
| 10 |
DEFAULT_NUMBER_OF_BUCKETS: int = 6 |
| 11 |
DEFAULT_BUCKET_DURATION_IN_SECONDS: int = 10 |
| 12 |
DEFAULT_TIERS: Tuple[int, int, int, int] = (90, 70, 30, 0) |
| 13 |
DEFAULT_TIME_PROVIDER_IN_SECONDS: Callable[[], float] = time.time |
| 14 |
|
| 15 |
def __init__( |
| 16 |
self, |
| 17 |
number_of_buckets: int = DEFAULT_NUMBER_OF_BUCKETS, |
| 18 |
bucket_duration: float = DEFAULT_BUCKET_DURATION_IN_SECONDS, |
| 19 |
tiers: Tuple[int, int, int, int] = DEFAULT_TIERS, |
| 20 |
time_provider: Callable[[], float] = DEFAULT_TIME_PROVIDER_IN_SECONDS, |
| 21 |
): |
| 22 |
self.accounts: Dict[str, _RankedStorageAccount] = dict() |
| 23 |
self.number_of_buckets = number_of_buckets |
| 24 |
self.bucket_duration = bucket_duration |
| 25 |
self.tiers = tiers |
| 26 |
self.time_provider = time_provider |
| 27 |
|
| 28 |
def add_account_result(self, account_name: str, success: bool): |
| 29 |
if self.accounts.get(account_name) is None: |
| 30 |
raise KustoClientError(f"Account {account_name} does not exist in the set") |
| 31 |
self.accounts[account_name].log_result(success) |
| 32 |
|
| 33 |
def add_storage_account(self, account_name: str): |
| 34 |
if self.accounts.get(account_name) is None: |
| 35 |
self.accounts[account_name] = _RankedStorageAccount(account_name, self.number_of_buckets, self.bucket_duration, self.time_provider) |
| 36 |
|
| 37 |
def get_storage_account(self, account_name: str) -> _RankedStorageAccount: |
| 38 |
return self.accounts.get(account_name) |
| 39 |
|
| 40 |
def get_ranked_shuffled_accounts(self) -> List[_RankedStorageAccount]: |
| 41 |
accounts_by_tier: List[List[_RankedStorageAccount]] = [[] for _ in range(len(self.tiers))] |
| 42 |
|
| 43 |
for account in self.accounts.values(): |
| 44 |
rank_percentage = account.get_rank() * 100.0 |
| 45 |
for i in range(len(self.tiers)): |
| 46 |
if rank_percentage >= self.tiers[i]: |
| 47 |
accounts_by_tier[i].append(account) |
| 48 |
break |
| 49 |
|
| 50 |
# Shuffle accounts in each tier |
| 51 |
for tier in accounts_by_tier: |
| 52 |
random.shuffle(tier) |
| 53 |
|
| 54 |
# Flatten the list |
| 55 |
return [item for sublist in accounts_by_tier for item in sublist] |