From 78c26186b09b6bbca2e1f00007737adf9ec59014 Mon Sep 17 00:00:00 2001 From: James Kafader Date: Tue, 16 May 2017 11:31:03 -0700 Subject: [PATCH 1/4] standardize the concept of 'now' to ensure that the same view of the service is returned from the read and update queries. --- doublethink/services.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/doublethink/services.py b/doublethink/services.py index 1a22af5..e5ddcb1 100644 --- a/doublethink/services.py +++ b/doublethink/services.py @@ -222,6 +222,8 @@ class ServiceRegistry(object): ''' if candidate is not None: candidate['id'] = role + # use the same concept of 'now' for both queries + now = rr.now().run() if not 'heartbeat_interval' in candidate: raise Exception( @@ -231,8 +233,8 @@ class ServiceRegistry(object): if not (isinstance(val, float) or isinstance(val, int)) or val <= 0: raise Exception('heartbeat_interval must be a number > 0') - candidate['first_heartbeat'] = r.now() - candidate['last_heartbeat'] = r.now() + candidate['first_heartbeat'] = now + candidate['last_heartbeat'] = now if not 'host' in candidate: candidate['host'] = socket.gethostname() if not 'pid' in candidate: @@ -243,7 +245,7 @@ class ServiceRegistry(object): lambda row: r.branch( r.branch( row, - row['last_heartbeat'] > r.now() - row['heartbeat_interval'] * 3, + row['last_heartbeat'] > now - row['heartbeat_interval'] * 3, False), row, candidate), return_changes='always').run() @@ -256,7 +258,7 @@ class ServiceRegistry(object): results = list(self.rr.table( 'services', read_mode='majority').get_all(role).filter( - lambda row: row['last_heartbeat'] > r.now() - row['heartbeat_interval'] * 3).run()) + lambda row: row['last_heartbeat'] > now - row['heartbeat_interval'] * 3).run()) if results: return results[0] else: From 55331083b3cb166467e5451d34d5e14306642482 Mon Sep 17 00:00:00 2001 From: James Kafader Date: Tue, 16 May 2017 11:33:15 -0700 Subject: [PATCH 2/4] correct comment --- doublethink/services.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doublethink/services.py b/doublethink/services.py index e5ddcb1..2f4793c 100644 --- a/doublethink/services.py +++ b/doublethink/services.py @@ -222,7 +222,7 @@ class ServiceRegistry(object): ''' if candidate is not None: candidate['id'] = role - # use the same concept of 'now' for both queries + # use the same concept of 'now' for all queries now = rr.now().run() if not 'heartbeat_interval' in candidate: From a0d17151fe75c6255cab3ea504396be28b2bdd22 Mon Sep 17 00:00:00 2001 From: James Kafader Date: Tue, 16 May 2017 18:16:54 -0700 Subject: [PATCH 3/4] make sure this variable is actually defined --- doublethink/services.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doublethink/services.py b/doublethink/services.py index 2f4793c..43a9e80 100644 --- a/doublethink/services.py +++ b/doublethink/services.py @@ -220,10 +220,10 @@ class ServiceRegistry(object): the unique service, if there is one and it is healthy, otherwise None ''' + # use the same concept of 'now' for all queries + now = self.rr.now().run() if candidate is not None: candidate['id'] = role - # use the same concept of 'now' for all queries - now = rr.now().run() if not 'heartbeat_interval' in candidate: raise Exception( From 158923d88b99a01a2e8d7ec21da106ce5a97d942 Mon Sep 17 00:00:00 2001 From: Noah Levitt Date: Wed, 17 May 2017 12:11:26 -0700 Subject: [PATCH 4/4] avoid database transaction to get current time --- doublethink/services.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doublethink/services.py b/doublethink/services.py index 43a9e80..e59c65e 100644 --- a/doublethink/services.py +++ b/doublethink/services.py @@ -20,6 +20,7 @@ import rethinkdb as r import logging import socket import os +import doublethink class ServiceRegistry(object): ''' @@ -221,7 +222,7 @@ class ServiceRegistry(object): None ''' # use the same concept of 'now' for all queries - now = self.rr.now().run() + now = doublethink.utcnow() if candidate is not None: candidate['id'] = role