Skip to content

Commit

Permalink
Merge "Merge "Republish periodically in lieu of heartbeat"" into R2.20
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Oct 19, 2015
2 parents 57a32ab + 94bec18 commit bdf4223
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
42 changes: 5 additions & 37 deletions src/discovery/client.py
Expand Up @@ -289,47 +289,15 @@ def _publish_int(self, service, data):
# publisher - send periodic heartbeat
def heartbeat(self):
while True:
# send heartbeat for each published object seperately
# Republish each published object seperately
# dictionary can change size during iteration
pub_list = self.pubdata.copy()
for cookie in pub_list:
gevent.sleep(0)
payload = {'cookie': cookie}
self.syslog('Sending cookie %s in heartbeat' % cookie)
try:
r = requests.post(
self.hburl, data=json.dumps(payload), headers=self._headers)
except requests.exceptions.ConnectionError:
service, data = pub_list[cookie]
ConnectionState.update(conn_type = ConnectionType.DISCOVERY,
name = service, status = ConnectionStatus.DOWN,
server_addrs = ['%s:%s' % (self._server_ip, \
self._server_port)],
message = 'HeartBeat - Connection Error')
self.syslog('Connection Error')
continue

# if DS lost track of our data, republish
if r.status_code == 404:
# forget cached cookie and object; will re-learn
self.syslog('Server lost track of token %s' % (cookie))
service, data = self.pubdata[cookie]
del self.pubdata[cookie]
self._publish_int(service, data)
elif r.status_code != 200:
service, data = pub_list[cookie]
ConnectionState.update(conn_type = ConnectionType.DISCOVERY,
name = service, status = ConnectionStatus.DOWN,
server_addrs = ['%s:%s' % (self._server_ip, \
self._server_port)],
message = 'HeartBeat Error: %d' % r.status_code)
else:
service, data = pub_list[cookie]
ConnectionState.update(conn_type = ConnectionType.DISCOVERY,
name = service, status = ConnectionStatus.UP,
server_addrs = ['%s:%s' % (self._server_ip, \
self._server_port)],
message = 'HeartBeat OK')
self.syslog('Republish %s' % (cookie))
service, data = self.pubdata[cookie]
del self.pubdata[cookie]
self._publish_int(service, data)
gevent.sleep(HC_INTERVAL)
# end client

Expand Down
26 changes: 26 additions & 0 deletions src/discovery/tests/test_publish.py
Expand Up @@ -301,3 +301,29 @@ def test_publish_admin_state(self):
entry = response['services'][0]
self.assertEqual(entry['admin_state'], 'down')
self.assertEqual(entry['oper_state'], 'up')

# Use python client library to publish a service
# ensure service is up after multiple hearbeat intervals
def test_publish_client_lib(self):
disc = client.DiscoveryClient(self._disc_server_ip,
self._disc_server_port, "test-publish")

service_type = 'foobar'
service_data = {
"ip-addr" : "1.1.1.1",
"port" : "1234",
}
tasks = []
hbtask = disc.publish(service_type, service_data)
tasks.append(hbtask)

# wait for multiple heartbeat intervals
time.sleep(60)

(code, msg) = self._http_get('/services.json')
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response['services']), 1)

entry = response['services'][0]
self.assertEqual(entry['status'], 'up')

0 comments on commit bdf4223

Please sign in to comment.