Skip to content

Commit

Permalink
Fix dask ip resolution. (#6475)
Browse files Browse the repository at this point in the history
This adopts the solution used in dask/dask-xgboost#40 which employs the get_host_ip from dmlc-core tracker.
  • Loading branch information
trivialfis committed Dec 8, 2020
1 parent 47b8618 commit 0ffaf0f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions python-package/xgboost/dask.py
Expand Up @@ -33,7 +33,7 @@
from .core import DMatrix, DeviceQuantileDMatrix, Booster, _expect, DataIter
from .core import _deprecate_positional_args
from .training import train as worker_train
from .tracker import RabitTracker
from .tracker import RabitTracker, get_host_ip
from .sklearn import XGBModel, XGBRegressorBase, XGBClassifierBase
from .sklearn import xgboost_model_doc

Expand Down Expand Up @@ -70,8 +70,7 @@
def _start_tracker(n_workers):
"""Start Rabit tracker """
env = {'DMLC_NUM_WORKER': n_workers}
import socket
host = socket.gethostbyname(socket.gethostname())
host = get_host_ip('auto')
rabit_context = RabitTracker(hostIP=host, nslave=n_workers)
env.update(rabit_context.slave_envs())

Expand Down
22 changes: 22 additions & 0 deletions python-package/xgboost/tracker.py
Expand Up @@ -52,6 +52,28 @@ def get_some_ip(host):
return socket.getaddrinfo(host, None)[0][4][0]


def get_host_ip(hostIP=None):
if hostIP is None or hostIP == 'auto':
hostIP = 'ip'

if hostIP == 'dns':
hostIP = socket.getfqdn()
elif hostIP == 'ip':
from socket import gaierror
try:
hostIP = socket.gethostbyname(socket.getfqdn())
except gaierror:
logging.warning(
'gethostbyname(socket.getfqdn()) failed... trying on hostname()')
hostIP = socket.gethostbyname(socket.gethostname())
if hostIP.startswith("127."):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# doesn't have to be reachable
s.connect(('10.255.255.255', 1))
hostIP = s.getsockname()[0]
return hostIP


def get_family(addr):
return socket.getaddrinfo(addr, None)[0][0]

Expand Down

0 comments on commit 0ffaf0f

Please sign in to comment.