Remove unneeded network roundtrip when retrieving components

When querying the component registry we currently ensure the root path
zuul/components/<kind> exists before iterating over the children. This
is unneeded since we can just catch the NoNodeError for the case there
is no such path yet.

Change-Id: I7be370a298f3a4f443e53443ab28abc552c2fba6
This commit is contained in:
Tobias Henkel
2021-03-13 13:15:49 +01:00
parent 22935c1177
commit 8626890e1f

View File

@@ -17,6 +17,7 @@ import threading
from typing import Any, Dict, List, Optional
from kazoo.client import KazooClient
from kazoo.exceptions import NoNodeError
from zuul.zk import NoClientException, ZooKeeperBase, ZooKeeperClient
@@ -153,12 +154,15 @@ class ZooKeeperComponentRegistry(ZooKeeperBase):
result = []
path = "{}/{}".format(self.ROOT, kind)
self.kazoo_client.ensure_path(path)
for node in self.kazoo_client.get_children(path):
path = "{}/{}/{}".format(self.ROOT, kind, node)
data, _ = self.kazoo_client.get(path)
content = json.loads(data.decode("UTF-8"))
result.append(ZooKeeperComponentReadOnly(self.client, content))
try:
for node in self.kazoo_client.get_children(path):
path = "{}/{}/{}".format(self.ROOT, kind, node)
data, _ = self.kazoo_client.get(path)
content = json.loads(data.decode("UTF-8"))
result.append(ZooKeeperComponentReadOnly(self.client, content))
except NoNodeError:
# If the node doesn't exist there is no component registered.
pass
return result
def register(self, kind: str, hostname: str) -> ZooKeeperComponent: