-
Notifications
You must be signed in to change notification settings - Fork 498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add service checking direct reachability from peers #195
Merged
+254
−10
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1f30fd7
reachability script
justheuristic 9ed72d9
move to server
justheuristic a97e6ac
black-isort
justheuristic b446718
duh
justheuristic 9536422
duh
justheuristic ff63744
black-isort
justheuristic 7aad991
Merge branch 'main' into check_reachability
justheuristic 7ba646e
Merge branch 'main' into check_reachability
justheuristic 58d71df
black-isort
justheuristic 9ffc4e2
Merge branch 'main' into check_reachability
justheuristic 9894855
Fix minor issues
borzunov c575ae9
Fix minor issues (2)
borzunov c087ea1
Implement graceful shutdown
borzunov 17dcbbb
Add comment
borzunov 378b78b
Query random key to collect more DHT neighbors
borzunov 6f30759
Ignore exceptions when creating reachability service
borzunov 02b7196
Refactor, add run_dht.py
borzunov 344c664
Update comment and defaults in run_dht.py
borzunov 5ac3b99
Remove debug things
borzunov d5440cc
Use startup_timeout=60 for the stripped probe
borzunov 974c643
Bump loglevels for some messages
borzunov 5fa285c
Don't log requests triggered by ourselves
borzunov 1f1e409
Merge branch 'main' into check_reachability
borzunov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
""" | ||
A copy of run_dht.py from hivemind with the ReachabilityProtocol added: | ||
https://github.com/learning-at-home/hivemind/blob/master/hivemind/hivemind_cli/run_dht.py | ||
|
||
This script may be used for launching lightweight CPU machines serving as bootstrap nodes to a Petals swarm. | ||
|
||
This may be eventually merged to the hivemind upstream. | ||
""" | ||
|
||
import time | ||
from argparse import ArgumentParser | ||
from secrets import token_hex | ||
|
||
from hivemind.dht import DHT, DHTNode | ||
from hivemind.utils.logging import get_logger, use_hivemind_log_handler | ||
from hivemind.utils.networking import log_visible_maddrs | ||
|
||
from petals.server.reachability import ReachabilityProtocol | ||
|
||
use_hivemind_log_handler("in_root_logger") | ||
logger = get_logger(__name__) | ||
|
||
|
||
async def report_status(dht: DHT, node: DHTNode): | ||
logger.info( | ||
f"{len(node.protocol.routing_table.uid_to_peer_id) + 1} DHT nodes (including this one) " | ||
f"are in the local routing table " | ||
) | ||
logger.debug(f"Routing table contents: {node.protocol.routing_table}") | ||
logger.info(f"Local storage contains {len(node.protocol.storage)} keys") | ||
logger.debug(f"Local storage contents: {node.protocol.storage}") | ||
|
||
# Contact peers and keep the routing table healthy (remove stale PeerIDs) | ||
await node.get(f"heartbeat_{token_hex(16)}", latest=True) | ||
|
||
|
||
def main(): | ||
parser = ArgumentParser() | ||
parser.add_argument( | ||
"--initial_peers", | ||
nargs="*", | ||
help="Multiaddrs of the peers that will welcome you into the existing DHT. " | ||
"Example: /ip4/203.0.113.1/tcp/31337/p2p/XXXX /ip4/203.0.113.2/tcp/7777/p2p/YYYY", | ||
) | ||
parser.add_argument( | ||
"--host_maddrs", | ||
nargs="*", | ||
default=["/ip4/0.0.0.0/tcp/0", "/ip6/::/tcp/0"], | ||
help="Multiaddrs to listen for external connections from other DHT instances. " | ||
"Defaults to all IPv4 interfaces and the TCP protocol: /ip4/0.0.0.0/tcp/0", | ||
) | ||
parser.add_argument( | ||
"--announce_maddrs", | ||
nargs="*", | ||
help="Visible multiaddrs the host announces for external connections from other DHT instances", | ||
) | ||
parser.add_argument( | ||
"--use_ipfs", | ||
action="store_true", | ||
help='Use IPFS to find initial_peers. If enabled, you only need to provide the "/p2p/XXXX" ' | ||
"part of the multiaddrs for the initial_peers " | ||
"(no need to specify a particular IPv4/IPv6 host and port)", | ||
) | ||
parser.add_argument( | ||
"--identity_path", | ||
help="Path to a private key file. If defined, makes the peer ID deterministic. " | ||
"If the file does not exist, writes a new private key to this file.", | ||
) | ||
parser.add_argument( | ||
"--no_relay", | ||
action="store_false", | ||
dest="use_relay", | ||
help="Disable circuit relay functionality in libp2p (see https://docs.libp2p.io/concepts/nat/circuit-relay/)", | ||
) | ||
parser.add_argument( | ||
"--use_auto_relay", action="store_true", help="Look for libp2p relays to reach peers behind NATs/firewalls" | ||
) | ||
parser.add_argument( | ||
"--refresh_period", type=int, default=30, help="Period (in seconds) for fetching the keys from DHT" | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
dht = DHT( | ||
start=True, | ||
initial_peers=args.initial_peers, | ||
host_maddrs=args.host_maddrs, | ||
announce_maddrs=args.announce_maddrs, | ||
use_ipfs=args.use_ipfs, | ||
identity_path=args.identity_path, | ||
use_relay=args.use_relay, | ||
use_auto_relay=args.use_auto_relay, | ||
) | ||
log_visible_maddrs(dht.get_visible_maddrs(), only_p2p=args.use_ipfs) | ||
|
||
reachability_protocol = ReachabilityProtocol.attach_to_dht(dht, await_ready=True) | ||
|
||
while True: | ||
dht.run_coroutine(report_status, return_future=False) | ||
time.sleep(args.refresh_period) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed the env variable here since this check currently works only for the public swarm, and the env var were making an impression that you could enable it for the private swarm too.
We can update the logic for this in future PR: e.g., make the server run the check if the swarm is public or custom API URL is provided.