|
| 1 | +# This file is derived from the Kazoo project |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | + |
| 14 | +from kazoo.exceptions import ( |
| 15 | + EXCEPTIONS, |
| 16 | + NoNodeError, |
| 17 | +) |
| 18 | +from kazoo.loggingsupport import BLATHER |
| 19 | +from kazoo.protocol.connection import ( |
| 20 | + ConnectionHandler, |
| 21 | + CREATED_EVENT, |
| 22 | + DELETED_EVENT, |
| 23 | + CHANGED_EVENT, |
| 24 | + CHILD_EVENT, |
| 25 | + CLOSE_RESPONSE, |
| 26 | +) |
| 27 | +from kazoo.protocol.serialization import ( |
| 28 | + Close, |
| 29 | + Exists, |
| 30 | + Transaction, |
| 31 | + GetChildren, |
| 32 | + GetChildren2, |
| 33 | + Watch, |
| 34 | +) |
| 35 | +from kazoo.protocol.states import ( |
| 36 | + Callback, |
| 37 | + WatchedEvent, |
| 38 | + EVENT_TYPE_MAP, |
| 39 | +) |
| 40 | + |
| 41 | +from nodepool.zk.vendor.serialization import ( |
| 42 | + AddWatch, |
| 43 | + RemoveWatches, |
| 44 | +) |
| 45 | +from nodepool.zk.vendor.states import ( |
| 46 | + AddWatchMode, |
| 47 | + WatcherType, |
| 48 | +) |
| 49 | + |
| 50 | + |
| 51 | +class ZuulConnectionHandler(ConnectionHandler): |
| 52 | + def _find_persistent_recursive_watchers(self, path): |
| 53 | + parts = path.split("/") |
| 54 | + watchers = [] |
| 55 | + for count in range(len(parts)): |
| 56 | + candidate = "/".join(parts[: count + 1]) |
| 57 | + if not candidate: |
| 58 | + candidate = '/' |
| 59 | + watchers.extend( |
| 60 | + self.client._persistent_recursive_watchers.get(candidate, []) |
| 61 | + ) |
| 62 | + return watchers |
| 63 | + |
| 64 | + def _read_watch_event(self, buffer, offset): |
| 65 | + client = self.client |
| 66 | + watch, offset = Watch.deserialize(buffer, offset) |
| 67 | + path = watch.path |
| 68 | + |
| 69 | + self.logger.debug("Received EVENT: %s", watch) |
| 70 | + |
| 71 | + watchers = [] |
| 72 | + |
| 73 | + if watch.type in (CREATED_EVENT, CHANGED_EVENT): |
| 74 | + watchers.extend(client._data_watchers.pop(path, [])) |
| 75 | + watchers.extend(client._persistent_watchers.get(path, [])) |
| 76 | + watchers.extend(self._find_persistent_recursive_watchers(path)) |
| 77 | + elif watch.type == DELETED_EVENT: |
| 78 | + watchers.extend(client._data_watchers.pop(path, [])) |
| 79 | + watchers.extend(client._child_watchers.pop(path, [])) |
| 80 | + watchers.extend(client._persistent_watchers.get(path, [])) |
| 81 | + watchers.extend(self._find_persistent_recursive_watchers(path)) |
| 82 | + elif watch.type == CHILD_EVENT: |
| 83 | + watchers.extend(client._child_watchers.pop(path, [])) |
| 84 | + else: |
| 85 | + self.logger.warn("Received unknown event %r", watch.type) |
| 86 | + return |
| 87 | + |
| 88 | + # Strip the chroot if needed |
| 89 | + path = client.unchroot(path) |
| 90 | + ev = WatchedEvent(EVENT_TYPE_MAP[watch.type], client._state, path) |
| 91 | + |
| 92 | + # Last check to ignore watches if we've been stopped |
| 93 | + if client._stopped.is_set(): |
| 94 | + return |
| 95 | + |
| 96 | + # Dump the watchers to the watch thread |
| 97 | + for watch in watchers: |
| 98 | + client.handler.dispatch_callback(Callback("watch", watch, (ev,))) |
| 99 | + |
| 100 | + def _read_response(self, header, buffer, offset): |
| 101 | + client = self.client |
| 102 | + request, async_object, xid = client._pending.popleft() |
| 103 | + if header.zxid and header.zxid > 0: |
| 104 | + client.last_zxid = header.zxid |
| 105 | + if header.xid != xid: |
| 106 | + exc = RuntimeError( |
| 107 | + "xids do not match, expected %r " "received %r", |
| 108 | + xid, |
| 109 | + header.xid, |
| 110 | + ) |
| 111 | + async_object.set_exception(exc) |
| 112 | + raise exc |
| 113 | + |
| 114 | + # Determine if its an exists request and a no node error |
| 115 | + exists_error = ( |
| 116 | + header.err == NoNodeError.code and request.type == Exists.type |
| 117 | + ) |
| 118 | + |
| 119 | + # Set the exception if its not an exists error |
| 120 | + if header.err and not exists_error: |
| 121 | + callback_exception = EXCEPTIONS[header.err]() |
| 122 | + self.logger.debug( |
| 123 | + "Received error(xid=%s) %r", xid, callback_exception |
| 124 | + ) |
| 125 | + if async_object: |
| 126 | + async_object.set_exception(callback_exception) |
| 127 | + elif request and async_object: |
| 128 | + if exists_error: |
| 129 | + # It's a NoNodeError, which is fine for an exists |
| 130 | + # request |
| 131 | + async_object.set(None) |
| 132 | + else: |
| 133 | + try: |
| 134 | + response = request.deserialize(buffer, offset) |
| 135 | + except Exception as exc: |
| 136 | + self.logger.exception( |
| 137 | + "Exception raised during deserialization " |
| 138 | + "of request: %s", |
| 139 | + request, |
| 140 | + ) |
| 141 | + async_object.set_exception(exc) |
| 142 | + return |
| 143 | + self.logger.debug( |
| 144 | + "Received response(xid=%s): %r", xid, response |
| 145 | + ) |
| 146 | + |
| 147 | + # We special case a Transaction as we have to unchroot things |
| 148 | + if request.type == Transaction.type: |
| 149 | + response = Transaction.unchroot(client, response) |
| 150 | + |
| 151 | + async_object.set(response) |
| 152 | + |
| 153 | + # Determine if watchers should be registered or unregistered |
| 154 | + if not client._stopped.is_set(): |
| 155 | + watcher = getattr(request, "watcher", None) |
| 156 | + if watcher: |
| 157 | + if isinstance(request, AddWatch): |
| 158 | + if request.mode == AddWatchMode.PERSISTENT: |
| 159 | + client._persistent_watchers[request.path].add( |
| 160 | + watcher |
| 161 | + ) |
| 162 | + elif request.mode == AddWatchMode.PERSISTENT_RECURSIVE: |
| 163 | + client._persistent_recursive_watchers[ |
| 164 | + request.path |
| 165 | + ].add(watcher) |
| 166 | + elif isinstance(request, (GetChildren, GetChildren2)): |
| 167 | + client._child_watchers[request.path].add(watcher) |
| 168 | + else: |
| 169 | + client._data_watchers[request.path].add(watcher) |
| 170 | + if isinstance(request, RemoveWatches): |
| 171 | + if request.watcher_type == WatcherType.CHILDREN: |
| 172 | + client._child_watchers.pop(request.path, None) |
| 173 | + elif request.watcher_type == WatcherType.DATA: |
| 174 | + client._data_watchers.pop(request.path, None) |
| 175 | + elif request.watcher_type == WatcherType.ANY: |
| 176 | + client._child_watchers.pop(request.path, None) |
| 177 | + client._data_watchers.pop(request.path, None) |
| 178 | + client._persistent_watchers.pop(request.path, None) |
| 179 | + client._persistent_recursive_watchers.pop( |
| 180 | + request.path, None |
| 181 | + ) |
| 182 | + |
| 183 | + if isinstance(request, Close): |
| 184 | + self.logger.log(BLATHER, "Read close response") |
| 185 | + return CLOSE_RESPONSE |
0 commit comments