|
21 | 21 | # Ray modules |
22 | 22 | import ray |
23 | 23 | import ray._private.ray_constants as ray_constants |
24 | | -from ray._common.network_utils import build_address, parse_address |
| 24 | +from ray._common.network_utils import ( |
| 25 | + build_address, |
| 26 | + get_localhost_ip, |
| 27 | + is_ipv6, |
| 28 | + node_ip_address_from_perspective, |
| 29 | + parse_address, |
| 30 | +) |
25 | 31 | from ray._private.ray_constants import RAY_NODE_IP_FILENAME |
26 | 32 | from ray._private.resource_isolation_config import ResourceIsolationConfig |
27 | 33 | from ray._raylet import GcsClient, GcsClientOptions |
@@ -618,52 +624,21 @@ def resolve_ip_for_localhost(host: str): |
618 | 624 | return host |
619 | 625 |
|
620 | 626 |
|
621 | | -def node_ip_address_from_perspective(address: str): |
622 | | - """IP address by which the local node can be reached *from* the `address`. |
623 | | -
|
624 | | - Args: |
625 | | - address: The IP address and port of any known live service on the |
626 | | - network you care about. |
627 | | -
|
628 | | - Returns: |
629 | | - The IP address by which the local node can be reached from the address. |
630 | | - """ |
631 | | - ip_address, port = parse_address(address) |
632 | | - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
633 | | - try: |
634 | | - # This command will raise an exception if there is no internet |
635 | | - # connection. |
636 | | - s.connect((ip_address, int(port))) |
637 | | - node_ip_address = s.getsockname()[0] |
638 | | - except OSError as e: |
639 | | - node_ip_address = "127.0.0.1" |
640 | | - # [Errno 101] Network is unreachable |
641 | | - if e.errno == errno.ENETUNREACH: |
642 | | - try: |
643 | | - # try get node ip address from host name |
644 | | - host_name = socket.getfqdn(socket.gethostname()) |
645 | | - node_ip_address = socket.gethostbyname(host_name) |
646 | | - except Exception: |
647 | | - pass |
648 | | - finally: |
649 | | - s.close() |
650 | | - |
651 | | - return node_ip_address |
652 | | - |
653 | | - |
654 | 627 | # NOTE: This API should not be used when you obtain the |
655 | 628 | # IP address when ray.init is not called because |
656 | 629 | # it cannot find the IP address if it is specified by |
657 | 630 | # ray start --node-ip-address. You should instead use |
658 | 631 | # get_cached_node_ip_address. |
659 | | -def get_node_ip_address(address="8.8.8.8:53"): |
| 632 | +def get_node_ip_address(address=None): |
660 | 633 | if ray._private.worker._global_node is not None: |
661 | 634 | return ray._private.worker._global_node.node_ip_address |
| 635 | + |
662 | 636 | if not ray_constants.ENABLE_RAY_CLUSTER: |
663 | 637 | # Use loopback IP as the local IP address to prevent bothersome |
664 | 638 | # firewall popups on OSX and Windows. |
665 | 639 | # https://github.com/ray-project/ray/issues/18730. |
666 | | - return "127.0.0.1" |
| 640 | + return get_localhost_ip() |
| 641 | + |
667 | 642 | return node_ip_address_from_perspective(address) |
668 | 643 |
|
669 | 644 |
|
@@ -1225,7 +1200,10 @@ def start_api_server( |
1225 | 1200 | port = ray_constants.DEFAULT_DASHBOARD_PORT |
1226 | 1201 | else: |
1227 | 1202 | port_retries = 0 |
1228 | | - port_test_socket = socket.socket() |
| 1203 | + port_test_socket = socket.socket( |
| 1204 | + socket.AF_INET6 if is_ipv6(host) else socket.AF_INET, |
| 1205 | + socket.SOCK_STREAM, |
| 1206 | + ) |
1229 | 1207 | port_test_socket.setsockopt( |
1230 | 1208 | socket.SOL_SOCKET, |
1231 | 1209 | socket.SO_REUSEADDR, |
|
0 commit comments