def add_endpoint(self, endpoint): self.__add_endpoint(endpoint) def local_lock(fn): def wrapped_fn(*args, **kwargs): try: self.cond.acquire() return fn(*args, **kwargs) finally: self.cond.release() return wrapped_fn @local_lock def update_state(value): endpoint.update_state(value) if self.endpoint_owned(endpoint): endpoint.update() @local_lock def update_config(value): endpoint.update_config(value) if self.endpoint_owned(endpoint): endpoint.update() @local_lock def update_confirmed(ips): if ips: self.confirmed[endpoint.name] = ips elif endpoint.name in self.confirmed: del self.confirmed[endpoint.name] # Kick off a loadbalancer update. self.update_loadbalancer(endpoint) # Watch the config for this endpoint. logging.info("Watching endpoint %s." % (endpoint.name)) update_state( self.zk_conn.watch_contents(paths.endpoint_state(endpoint.name), update_state, '', clean=True)) update_config( self.zk_conn.watch_contents(paths.endpoint(endpoint.name), update_config, '', clean=True)) # Select the manager for this endpoint. self.manager_select(endpoint) # Update the loadbalancer for this endpoint. update_confirmed( self.zk_conn.watch_children(paths.confirmed_ips(endpoint.name), update_confirmed, clean=True))
def remove_endpoint(self, endpoint_name, unmanage=False): """ This removes / unmanages the endpoint. """ logging.info("Removing endpoint %s from manager %s" % (endpoint_name, self.uuid)) endpoint = self.endpoints.get(endpoint_name, None) if endpoint: self.zk_conn.clear_watch_path(paths.endpoint_state(endpoint.name)) self.zk_conn.clear_watch_path(paths.endpoint(endpoint.name)) self.zk_conn.clear_watch_path(paths.confirmed_ips(endpoint.name)) # Update the loadbalancer for this endpoint. self.update_loadbalancer(endpoint, remove=True) self.__remove_endpoint(endpoint, unmanage) self.manager_remove(endpoint)
def set_endpoint_state(self, endpoint_name, state): self.zk_conn.write(paths.endpoint_state(endpoint_name), state)
def get_endpoint_state(self, endpoint_name): return self.zk_conn.read(paths.endpoint_state(endpoint_name))
def update_state(self, state): if state != State.running: # Always make sure that the latest action reflects our state. self.scale_manager.zk_conn.write(paths.endpoint_state("api"), State.running)