|
@@ -1,15 +1,13 @@
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
-import dataclasses
|
|
|
|
import threading
|
|
import threading
|
|
-from functools import partial
|
|
|
|
from typing import List, NamedTuple, Optional, Sequence, Tuple
|
|
from typing import List, NamedTuple, Optional, Sequence, Tuple
|
|
|
|
|
|
from hivemind import DHT, PeerID
|
|
from hivemind import DHT, PeerID
|
|
from hivemind.utils.logging import get_logger, use_hivemind_log_handler
|
|
from hivemind.utils.logging import get_logger, use_hivemind_log_handler
|
|
|
|
|
|
-from src.data_structures import ModuleUID, RemoteModuleInfo
|
|
|
|
-from src.dht_utils import _get_remote_module_infos
|
|
|
|
|
|
+from src.data_structures import ModuleUID, RemoteModuleInfo, ServerState
|
|
|
|
+from src.dht_utils import get_remote_module_infos
|
|
|
|
|
|
use_hivemind_log_handler("in_root_logger")
|
|
use_hivemind_log_handler("in_root_logger")
|
|
logger = get_logger(__file__)
|
|
logger = get_logger(__file__)
|
|
@@ -18,21 +16,20 @@ logger = get_logger(__file__)
|
|
Span = NamedTuple("Span", [("start", int), ("end", Optional[int]), ("peer_id", PeerID)])
|
|
Span = NamedTuple("Span", [("start", int), ("end", Optional[int]), ("peer_id", PeerID)])
|
|
|
|
|
|
|
|
|
|
-@dataclasses.dataclass(frozen=False, init=False) # TODO[borzunov@] eto ne dataclass
|
|
|
|
class RemoteSequenceInfo:
|
|
class RemoteSequenceInfo:
|
|
"""Keeps and updates the meta-information about which peers host which blocks"""
|
|
"""Keeps and updates the meta-information about which peers host which blocks"""
|
|
|
|
|
|
dht: DHT
|
|
dht: DHT
|
|
- block_uids: List[ModuleUID, ...]
|
|
|
|
- block_infos: List[Optional[RemoteModuleInfo], ...]
|
|
|
|
|
|
+ block_uids: List[ModuleUID]
|
|
|
|
+ block_infos: List[Optional[RemoteModuleInfo]]
|
|
spans_by_priority: List[Span] # sorted from best to worst
|
|
spans_by_priority: List[Span] # sorted from best to worst
|
|
- spans_containing_block: Tuple[List[Span], ...]
|
|
|
|
|
|
+ spans_containing_block: Tuple[List[Span]]
|
|
lock_changes: threading.Lock
|
|
lock_changes: threading.Lock
|
|
|
|
|
|
def __init__(self, dht: DHT, block_uids: Sequence[ModuleUID]):
|
|
def __init__(self, dht: DHT, block_uids: Sequence[ModuleUID]):
|
|
self.dht = dht
|
|
self.dht = dht
|
|
self.block_uids = list(block_uids)
|
|
self.block_uids = list(block_uids)
|
|
- self.block_infos: List[Optional[RemoteModuleInfo], ...] = [None] * len(self.block_uids)
|
|
|
|
|
|
+ self.block_infos = [None] * len(self.block_uids)
|
|
self.spans_by_priority = []
|
|
self.spans_by_priority = []
|
|
self.spans_containing_block = tuple(list() for _ in range(len(self.block_uids)))
|
|
self.spans_containing_block = tuple(list() for _ in range(len(self.block_uids)))
|
|
self.lock_changes = threading.Lock()
|
|
self.lock_changes = threading.Lock()
|
|
@@ -48,21 +45,17 @@ class RemoteSequenceInfo:
|
|
self.spans_by_priority, self.spans_containing_block = self.compute_spans(self.block_infos)
|
|
self.spans_by_priority, self.spans_containing_block = self.compute_spans(self.block_infos)
|
|
|
|
|
|
def update_block_infos_(self):
|
|
def update_block_infos_(self):
|
|
- new_block_infos: Sequence[RemoteModuleInfo] = self.dht.run_coroutine(
|
|
|
|
- partial(_get_remote_module_infos, uids=self.block_uids, expiration_time=float("inf")), return_future=False
|
|
|
|
- )
|
|
|
|
|
|
+ new_block_infos = get_remote_module_infos(self.dht, self.block_uids, expiration_time=float("inf"))
|
|
assert len(new_block_infos) == len(self.block_uids)
|
|
assert len(new_block_infos) == len(self.block_uids)
|
|
for block_index, (uid, info) in enumerate(zip(self.block_uids, new_block_infos)):
|
|
for block_index, (uid, info) in enumerate(zip(self.block_uids, new_block_infos)):
|
|
if info is None:
|
|
if info is None:
|
|
logger.warning(f"Found no block info for block {uid}")
|
|
logger.warning(f"Found no block info for block {uid}")
|
|
if not isinstance(info, RemoteModuleInfo):
|
|
if not isinstance(info, RemoteModuleInfo):
|
|
logger.warning(f"Unexpected dht entry type for {uid}: {info}")
|
|
logger.warning(f"Unexpected dht entry type for {uid}: {info}")
|
|
- if not info.peer_ids:
|
|
|
|
|
|
+ if not info.servers:
|
|
logger.warning(f"Found no active peers for block {uid}")
|
|
logger.warning(f"Found no active peers for block {uid}")
|
|
if info.uid != uid:
|
|
if info.uid != uid:
|
|
logger.warning(f"The DHT entry for {uid} actually points to {info.uid}")
|
|
logger.warning(f"The DHT entry for {uid} actually points to {info.uid}")
|
|
- if not isinstance(info.peer_ids, set):
|
|
|
|
- logger.warning(f"Expected peer_ids for {uid} to be a set, got {type(info.peer_ids)}")
|
|
|
|
self.block_infos[block_index] = info
|
|
self.block_infos[block_index] = info
|
|
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
@@ -70,14 +63,20 @@ class RemoteSequenceInfo:
|
|
closed_spans = []
|
|
closed_spans = []
|
|
active_spans = {}
|
|
active_spans = {}
|
|
for block_index, info in enumerate(block_infos):
|
|
for block_index, info in enumerate(block_infos):
|
|
- for peer_id in info.peer_ids:
|
|
|
|
|
|
+ for peer_id, server in info.servers.items():
|
|
|
|
+ if server.state != ServerState.ONLINE:
|
|
|
|
+ continue
|
|
if peer_id not in active_spans:
|
|
if peer_id not in active_spans:
|
|
active_spans[peer_id] = Span(start=block_index, end=block_index + 1, peer_id=peer_id)
|
|
active_spans[peer_id] = Span(start=block_index, end=block_index + 1, peer_id=peer_id)
|
|
else: # peer_id in active_spans
|
|
else: # peer_id in active_spans
|
|
active_spans[peer_id] = active_spans[peer_id]._replace(end=block_index + 1)
|
|
active_spans[peer_id] = active_spans[peer_id]._replace(end=block_index + 1)
|
|
|
|
|
|
for peer_id in list(active_spans.keys()):
|
|
for peer_id in list(active_spans.keys()):
|
|
- if peer_id not in info.peer_ids or block_index == len(block_infos) - 1:
|
|
|
|
|
|
+ if (
|
|
|
|
+ peer_id not in info.servers or
|
|
|
|
+ info.servers[peer_id].state != ServerState.ONLINE or
|
|
|
|
+ block_index == len(block_infos) - 1
|
|
|
|
+ ):
|
|
closed_spans.append(active_spans.pop(peer_id))
|
|
closed_spans.append(active_spans.pop(peer_id))
|
|
assert not active_spans
|
|
assert not active_spans
|
|
|
|
|