From a08059878138383fa9f547094545e595fb29084d Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Thu, 1 Jul 2021 01:15:51 -0400 Subject: [PATCH] Avoid superfluous ZK exists calls These cause a major (2x) slowdown in read calls since Zookeeper connections are expensive/slow. Instead, just try the thing and return None if there's no key there. Also wrap the children command in similar error handling since that did not exist and could likely cause some bugs at some point. --- daemon-common/zkhandler.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/daemon-common/zkhandler.py b/daemon-common/zkhandler.py index ded85876..b3c942ee 100644 --- a/daemon-common/zkhandler.py +++ b/daemon-common/zkhandler.py @@ -211,10 +211,10 @@ class ZKHandler(object): """ Read data from a key """ - if self.exists(key): + try: path = self.get_schema_path(key) data = self.zk_conn.get(path)[0].decode(self.encoding) - else: + except NoNodeError: data = None return data @@ -290,8 +290,13 @@ class ZKHandler(object): """ Lists all children of a key """ - path = self.get_schema_path(key) - return self.zk_conn.get_children(path) + try: + path = self.get_schema_path(key) + children = self.zk_conn.get_children(path) + except NoNodeError: + children = None + + return children def rename(self, kkpairs): """