Skip to content

Commit

Permalink
Fix pinger cache bug and add test.
Browse files Browse the repository at this point in the history
Testing Done:
Added a unit test that shows pinger will return UNREACHABLE on a netloc that should pass if the netloc has been used previously with a faster timeout.

Changed global_pinger_memo to take into account the timeout and number of tries, and the global_pinger_memo test now passes.

CI is running: https://travis-ci.org/pantsbuild/pants/builds/85163916

Bugs closed: 1766

Reviewed at https://rbcommons.com/s/twitter/r/2948/
  • Loading branch information
TansyArron authored and ericzundel committed Oct 13, 2015
1 parent 24aa7ae commit b148ee4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/python/pants/cache/pinger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pants.util.contextutil import Timer


_global_pinger_memo = {} # netloc -> rt time in secs.
_global_pinger_memo = {} # (netloc, timeout, tries) -> rt time in secs.


class Pinger(object):
Expand All @@ -28,12 +28,18 @@ def __init__(self, timeout, tries):
def ping(self, netloc):
"""Time a single roundtrip to the netloc.
:param netloc: string of "host:port"
:returns: the fastest ping time for a given netloc and number of tries.
or Pinger.UNREACHABLE if ping times out.
:rtype: float
Note that we don't use actual ICMP pings, because cmd-line ping is
inflexible and platform-dependent, so shelling out to it is annoying,
and the ICMP python lib can only be called by the superuser.
"""
if netloc in _global_pinger_memo:
return _global_pinger_memo[netloc]
netloc_info = (netloc, self._timeout, self._tries)
if netloc_info in _global_pinger_memo:
return _global_pinger_memo[netloc_info]

host, colon, portstr = netloc.partition(':')
port = int(portstr) if portstr else None
Expand All @@ -48,7 +54,7 @@ def ping(self, netloc):
except Exception:
new_rt_secs = Pinger.UNREACHABLE
rt_secs = min(rt_secs, new_rt_secs)
_global_pinger_memo[netloc] = rt_secs
_global_pinger_memo[netloc_info] = rt_secs
return rt_secs

def pings(self, netlocs):
Expand Down
6 changes: 6 additions & 0 deletions tests/python/pants_test/cache/test_pinger.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def test_pinger_timeout_config(self):
self.assertLess(ping_results[self.fast_netloc], 1)
self.assertEqual(ping_results[self.slow_netloc], Pinger.UNREACHABLE)

def test_global_pinger_memo(self):
fast_pinger = Pinger(timeout=self.slow_seconds, tries=2)
slow_pinger = Pinger(timeout=self.timeout_seconds, tries=2)
self.assertEqual(fast_pinger.pings([self.slow_netloc])[0][1], Pinger.UNREACHABLE)
self.assertNotEqual(slow_pinger.pings([self.slow_netloc])[0][1], Pinger.UNREACHABLE)

def tearDown(self):
for server in self.servers:
server.shutdown()

0 comments on commit b148ee4

Please sign in to comment.