def connect(self, timeout = None): '''Connects to the zookeeper server''' # if no timeout provided, thake the configured one if timeout is None: timeout = self._timeout # check, if we're already connected if self._handle is not None and not self.is_connected(): raise RuntimeError('Already connected') return condition = threading.Condition() def connection_watch(handle, type, state, path): condition.acquire() condition.notify() condition.release() # try to connect condition.acquire() self._handle = zookeeper.init( ','.join(self._servers), connection_watch, self._timeout * 1000) condition.wait(self._timeout) condition.release() if zookeeper.state(self._handle) != zookeeper.CONNECTED_STATE: zookeeper.close(self._handle) raise RuntimeError( 'unable to connect to %s ' % (' or '.join(self._servers))) zookeeper.set_watcher(self._handle, self.__global_watch)
def _register_watcher(self, handler, type, state, path): LOGGER.info("register_handler state is: %d" % state) zookeeper.set_watcher(self._register_handler, self._register_watcher) # 连接或重连成功 if state == zookeeper.CONNECTED_STATE: LOGGER.info("register_handler: connect or reconnect successfully") self._real_register()
def _discovery_watcher(self, handler, type, state, path): LOGGER.info("discovery_handler state is: %d" % state) zookeeper.set_watcher(self._discovery_handler, self._discovery_watcher) # 连接或重连成功 if state == zookeeper.CONNECTED_STATE: LOGGER.info("discovery_handler: connect or reconnect successfully") self._real_discovery(handler, type, state, self._base_znode)
def set_global(self): """ Set our dispatch function as the ZooKeeper global watcher. Return: None Exceptions: None """ zookeeper.set_watcher(self._zk, self.dispatch) return
def connection_init_watcher(self, h, type, state, path): self.handle = h self.conn_cv.acquire() self.connected = True self.conn_cv.notifyAll() self.conn_cv.release() if self.connect_watcher != None: zookeeper.set_watcher(h, self.connection_watcher)
def _connection_state_watcher(self, handler, type_, state, path): LOGGER.info("connection state is: %s" % state) zookeeper.set_watcher(handler, self._connection_state_watcher) # 连接或重连成功 if state == zookeeper.CONNECTED_STATE: LOGGER.info("connect or reconnect successfully, " "handler is: %s" % handler) # 拉取配置 self._real_start(handler, type_, state, self._base_znode)
def set_connection_watcher(self, watcher): """ Sets a permanent global watcher on the connection. This will get notice of changes to the connection state. @param: watcher function """ if not callable(watcher): raise SyntaxError("Invalid Watcher %r" % (watcher)) watcher = self._wrap_watcher(watcher, None, None) zookeeper.set_watcher(self.handle, watcher)
def connection_watcher(self, h, type, state, path): type = TYPE_NAME_MAPPING.get(type, type) state = STATE_NAME_MAPPING.get(state, state) if self.connection_watcher != None: import traceback try: func = self.connect_watcher func(type, state) except: print 'watch connection error' print traceback.format_exc() zookeeper.set_watcher(h, self.connection_watcher)
def watcher(zk, type, state, nodepath): log('watcher', { 'type': type, 'state': state, 'nodepath': nodepath }, 'DEBUG') if type == zookeeper.SESSION_EVENT: zookeeper.set_watcher(zk, watcher) if state == zookeeper.CONNECTED_STATE: for k in G.project: if zookeeper.exists(zk, k, watcher): # 启动时马上就通知一次,防止在断线过程中出现了数据更改,而服务又不知道 on_zookeeper_node_change(zookeeper.get(zk, k), k) if zookeeper.exists(zk, ROOT_NODE, watcher): config = zookeeper.get(zk, ROOT_NODE)[0] if config != '': G.config = json.loads(config) if not isinstance(G.config, dict): raise TypeError() else: G.config = {} log('config', G.config, 'DEBUG') elif type == zookeeper.CREATED_EVENT or type == zookeeper.CHANGED_EVENT: nodevalue = zookeeper.get(zk, nodepath, watcher) if nodepath == ROOT_NODE: if nodevalue[0] != '': G.config = json.loads(nodevalue[0]) if not isinstance(G.config, dict): raise TypeError() else: G.config = {} log('config', G.config, 'DEBUG') # restart process if G.config.has_key('restart') and G.config['restart'] in [ True, G.name ]: G.queue.put((0, {'type': 'restart'})) else: on_zookeeper_node_change(nodevalue, nodepath) elif type == zookeeper.DELETED_EVENT: zookeeper.exists(zk, nodepath, watcher) # 期待再次创建节点
def watcher(zk, type, state, nodepath): if type == zookeeper.SESSION_EVENT: zookeeper.set_watcher(zk, watcher)