Skip to content

Commit

Permalink
Get host ip.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Dec 7, 2020
1 parent a10da4e commit 55faba5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
9 changes: 4 additions & 5 deletions python-package/xgboost/dask.py
Original file line number Diff line number Diff line change
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 @@ -67,9 +67,10 @@
LOGGER = logging.getLogger('[xgboost.dask]')


def _start_tracker(host, n_workers):
def _start_tracker(n_workers):
"""Start Rabit tracker """
env = {'DMLC_NUM_WORKER': n_workers}
host = get_host_ip('auto')
rabit_context = RabitTracker(hostIP=host, nslave=n_workers)
env.update(rabit_context.slave_envs())

Expand Down Expand Up @@ -601,9 +602,7 @@ def _dmatrix_from_list_of_parts(is_quantile, **kwargs):

async def _get_rabit_args(n_workers: int, client):
'''Get rabit context arguments from data distribution in DaskDMatrix.'''
host = distributed.comm.get_address_host(client.scheduler.address)
env = await client.run_on_scheduler(
_start_tracker, host.strip('/:'), n_workers)
env = await client.run_on_scheduler(_start_tracker, n_workers)
rabit_args = [('%s=%s' % item).encode() for item in env.items()]
return rabit_args

Expand Down
21 changes: 21 additions & 0 deletions python-package/xgboost/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ 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.warn('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 55faba5

Please sign in to comment.