use new index "role" in service registry

This commit is contained in:
Noah Levitt 2017-09-06 17:25:35 -07:00
parent 43cbcdf644
commit 03e641549e
2 changed files with 7 additions and 7 deletions

View File

@ -110,7 +110,7 @@ class ServiceRegistry(object):
self.rr.table_create( self.rr.table_create(
'services', shards=1, 'services', shards=1,
replicas=min(3, len(self.rr.servers))).run() replicas=min(3, len(self.rr.servers))).run()
# self.rr.table('services').index_create...? self.rr.table('services').index_create('role').run()
def heartbeat(self, status_info): def heartbeat(self, status_info):
''' '''
@ -218,7 +218,7 @@ class ServiceRegistry(object):
the unique service, if there is one and it is healthy, otherwise the unique service, if there is one and it is healthy, otherwise
None None
''' '''
# use the same concept of 'now' for all queries # use the same concept of 'now' for all queries
now = doublethink.utcnow() now = doublethink.utcnow()
if candidate is not None: if candidate is not None:
candidate['id'] = role candidate['id'] = role
@ -275,9 +275,9 @@ class ServiceRegistry(object):
value of 'load' value of 'load'
''' '''
try: try:
result = self.rr.table('services').filter({"role":role}).filter( result = self.rr.table('services').get_all(role, index='role').filter(
lambda svc: r.now().sub(svc["last_heartbeat"]) < svc["ttl"] lambda svc: r.now().sub(svc["last_heartbeat"]) < svc["ttl"]
).order_by("load")[0].run() ).order_by("load")[0].run()
return result return result
except r.ReqlNonExistenceError: except r.ReqlNonExistenceError:
return None return None
@ -300,7 +300,7 @@ class ServiceRegistry(object):
try: try:
query = self.rr.table('services') query = self.rr.table('services')
if role: if role:
query = query.filter({"role":role}) query = query.get_all(role, index='role')
query = query.filter( query = query.filter(
lambda svc: r.now().sub(svc["last_heartbeat"]) < svc["ttl"] #.default(20.0) lambda svc: r.now().sub(svc["last_heartbeat"]) < svc["ttl"] #.default(20.0)
).order_by("load") ).order_by("load")

View File

@ -3,7 +3,7 @@ import codecs
setuptools.setup( setuptools.setup(
name='doublethink', name='doublethink',
version='0.2.0.dev83', version='0.2.0.dev84',
packages=['doublethink'], packages=['doublethink'],
classifiers=[ classifiers=[
'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 2.7',