Skip to content

Commit

Permalink
f r's
Browse files Browse the repository at this point in the history
  • Loading branch information
jrconlin committed Dec 31, 2019
1 parent ab3fa7e commit 828ff95
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 39 deletions.
22 changes: 2 additions & 20 deletions tokenserver/assignment/memorynode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import hashlib

from zope.interface import implements

from tokenserver.assignment import INodeAssignment
Expand All @@ -16,19 +14,11 @@ class MemoryNodeAssignmentBackend(object):
implements(INodeAssignment)

def __init__(self, service_entry=None, **kw):
self._service_entry = service_entry
self.service_entry = service_entry
self._users = {}
self._next_uid = 1
self.settings = kw or {}

@property
def service_entry(self):
"""Implement this as a property to have the context when looking for
the value of the setting"""
if self._service_entry is None:
self._service_entry = self.settings.get('service_entry')
return self._service_entry

def clear(self):
self._users.clear()
self._next_uid = 1
Expand All @@ -39,14 +29,6 @@ def get_user(self, service, email):
except KeyError:
return None

def should_allocate_to_spanner(self, email):
"""use a simple, reproducable hashing mechanism to determine if
a user should be provisioned to spanner. Does not need to be
secure, just a selectable percentage."""
return ord(hashlib.sha1(email.encode()).digest()[0]) < (
256 * (self.settings.get(
'migrate_new_user_percentage', 0) * .01))

def allocate_user(self, service, email, generation=0, client_state='',
keys_changed_at=0, node=None):
if (service, email) in self._users:
Expand All @@ -56,7 +38,7 @@ def allocate_user(self, service, email, generation=0, client_state='',
user = {
'email': email,
'uid': self._next_uid,
'node': self._service_entry,
'node': self.service_entry,
'generation': generation,
'keys_changed_at': keys_changed_at,
'client_state': client_state,
Expand Down
21 changes: 10 additions & 11 deletions tokenserver/assignment/sqlnode/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ def __init__(self, sqluri, create_tables=False, pool_size=100,
self.services = get_cls('services', _Base)
self.nodes = get_cls('nodes', _Base)
self.users = get_cls('users', _Base)
self.settings = kw or {}

for table in (self.services, self.nodes, self.users):
table.metadata.bind = self._engine
Expand Down Expand Up @@ -691,17 +690,17 @@ def get_best_node(self, service, email):
nodeid = row.id
node = str(row.node)

# Update the node to reflect the new assignment.
# This is a little racy with concurrent assignments, but no big
# deal.
where = [nodes.c.service == service, nodes.c.node == node]
where = and_(*where)
fields = {'current_load': nodes.c.current_load + 1}
if not send_to_spanner:
# Update the node to reflect the new assignment.
# This is a little racy with concurrent assignments, but no big
# deal.
where = [nodes.c.service == service, nodes.c.node == node]
where = and_(*where)
fields = {'available': self._sqlfunc_max(nodes.c.available - 1, 0),
'current_load': nodes.c.current_load + 1}
query = update(nodes, where, fields)
con = self._safe_execute(query, close=True)
con.close()
fields['available'] = self._sqlfunc_max(nodes.c.available - 1, 0)
query = update(nodes, where, fields)
con = self._safe_execute(query, close=True)
con.close()

return nodeid, node

Expand Down
2 changes: 0 additions & 2 deletions tokenserver/tests/assignment/test_sqlnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def setUp(self):
self.backend.add_service('sync-1.5', '{node}/1.5/{uid}')
self.backend.add_service('queuey-1.0', '{node}/{service}/{uid}')
self.backend.add_node('sync-1.0', 'https://phx12', 100)
self.backend.add_node('sync-1.5', 'https://phx11', 100)
self.backend.migrate_new_user_percentage = 0

def test_node_allocation(self):
user = self.backend.get_user("sync-1.0", "test1@example.com")
Expand Down
3 changes: 0 additions & 3 deletions tokenserver/tests/test_memorynode.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ node = https://example.com
token_duration = 3600
node_type_patterns =
example:*example*
spanner_entry = https://spanner.example.com
spanner_node_id = 800
migrate_new_user_percentage=0

[endpoints]
sync-1.1 = {node}/1.1/{uid}
Expand Down
4 changes: 2 additions & 2 deletions tokenserver/tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ def setUp(self):
# Ensure we have a node with enough capacity to run the tests.
self.backend.add_node('sync-1.1', self.mysql_node, 100)
self.backend.add_node('sync-1.5', self.mysql_node, 100)
# Ensure we have a spanner node, but give it not capacity
# Ensure we have a spanner node, but give it no capacity
# so users are not assigned to it except under special
# circumstances.
self.backend.add_node('sync-1.5', self.spanner_node, 0, nodeid=800)
Expand All @@ -827,7 +827,7 @@ def tearDown(self):

def test_assign_new_users_to_spanner(self):
self.backend.migrate_new_user_percentage = 1
# These emails are carefully selected os that the first is assigned
# These emails are carefully selected so that the first is assigned
# to spanner, but the second will not be.
EMAIL0 = "abO-test@example.com"
EMAIL1 = "abT-test@example.com"
Expand Down
1 change: 0 additions & 1 deletion tokenserver/tests/test_sql.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ secrets.master_secrets = "abcdef"
"123456"
node_type_patterns =
example:*example.com
spanner_entry = https://spanner.example.com
spanner_node_id = 800
migrate_new_user_percentage=0

Expand Down

0 comments on commit 828ff95

Please sign in to comment.