near / nearcore

Reference client for NEAR Protocol
https://near.org
GNU General Public License v3.0
2.32k stars 622 forks source link

Implement state sync source selection #10213

Open saketh-are opened 11 months ago

saketh-are commented 11 months ago

After #10099, nodes are aware of the snapshots hosted by peers in the network. A node seeking to obtain a shard part can request it from any node which hosts a snapshot including the shard. We expect several hosts to be available for each shard.

We would like to implement a source selection strategy which distributes requests effectively across the network. Considering that state parts are expensive to generate but can be cached in memory, we generally aim to direct requests by different nodes for the same part to the same host.

We will implement selection based on rendezvous hashing. A priority P(v,i) = hash(v ++ i) will be defined for each source v and each part i. A node interested in obtaining a part will attempt to request it from the possible sources in order of their priority. By doing so we enable network nodes to reach a rough consensus on which nodes should serve which parts, without a need for perfect synchronization.

The SnapshotHostsCache should expose an API select_host(sync_hash: CryptoHash, sid: ShardId, pid: PartId) -> Option<PeerId>. The arguments specify a specific part and the function should select a peer from which to request the part. In case the same arguments are passed multiple times (likely due to failed attempts to obtain the part from the previously selected hosts), a different host should be selected on each call, going down the list of possibilities in their priority order.

Implementation details to consider include:

marcelo-gonzalez commented 9 months ago

@VanBarbascu I guess this could be considered more or less closed, but I will leave it open until it's actually hooked up to decentralized state sync