Fix race in layoutstate

When we create a new layoutstate for a tenant, we can create a
zero-byte entry which can result in a json-decode error from
another component watching the state.

Update it to atomically write the state.

Change-Id: Idc62dbdc2299405b6c0e9948dc7ccc50830750a8
This commit is contained in:
James E. Blair 2021-11-16 07:15:00 -08:00
parent 7bee0b5861
commit 92553f4273
1 changed files with 7 additions and 2 deletions

View File

@ -118,6 +118,9 @@ class LayoutStateStore(ZooKeeperBase, MutableMapping):
except NoNodeError:
raise KeyError(tenant_name)
if not data:
raise KeyError(tenant_name)
return LayoutState.fromDict({
"ltime": zstat.last_modified_transaction_id,
**json.loads(data)
@ -125,9 +128,11 @@ class LayoutStateStore(ZooKeeperBase, MutableMapping):
def __setitem__(self, tenant_name, state):
path = f"{self.layout_root}/{tenant_name}"
self.kazoo_client.ensure_path(path)
data = json.dumps(state.toDict(), sort_keys=True).encode("utf-8")
zstat = self.kazoo_client.set(path, data)
if self.kazoo_client.exists(path):
zstat = self.kazoo_client.set(path, data)
else:
_, zstat = self.kazoo_client.create(path, data, include_data=True)
# Set correct ltime of the layout in Zookeeper
state.ltime = zstat.last_modified_transaction_id