def pretty_print_smartstack_backends_for_locations(service_instance, tasks, locations, expected_count, verbose, synapse_port, synapse_haproxy_url_format): """ Pretty prints the status of smartstack backends of a specified service and instance in the specified locations """ rows = [(" Name", "LastCheck", "LastChange", "Status")] if verbose else [] expected_count_per_location = int(expected_count / len(locations)) for location in sorted(locations): hosts = locations[location] # arbitrarily choose the first host with a given attribute to query for replication stats synapse_host = hosts[0] sorted_backends = sorted( get_backends( service_instance, synapse_host=synapse_host, synapse_port=synapse_port, synapse_haproxy_url_format=synapse_haproxy_url_format, ), key=lambda backend: backend['status'], reverse=True, # Specify reverse so that backends in 'UP' are placed above 'MAINT' ) matched_tasks = match_backends_and_tasks(sorted_backends, tasks) running_count = sum(1 for backend, task in matched_tasks if backend and backend_is_up(backend)) rows.append(" %s - %s" % (location, haproxy_backend_report(expected_count_per_location, running_count))) # If verbose mode is specified, show status of individual backends if verbose: for backend, task in matched_tasks: if backend is not None: rows.append(format_haproxy_backend_row(backend, task is not None)) return format_table(rows)
def is_healthy_in_haproxy(local_port, backends): local_ip = gethostbyname(gethostname()) for backend in backends: ip, port, _ = ip_port_hostname_from_svname(backend['svname']) if ip == local_ip and port == local_port: if backend_is_up(backend): log.debug("Found a healthy local backend: %s" % backend) return True else: log.debug("Found a unhealthy local backend: %s" % backend) return False log.debug("Couldn't find any haproxy backend listening on %s" % local_port) return False
def build_smartstack_location_dict( location: str, matched_backends_and_tasks: List[Tuple[Optional[HaproxyBackend], Optional[MarathonTask]]], should_return_individual_backends: bool = False, ) -> MutableMapping[str, Any]: running_backends_count = 0 backends = [] for backend, task in matched_backends_and_tasks: if backend is None: continue if backend_is_up(backend): running_backends_count += 1 if should_return_individual_backends: backends.append(build_smartstack_backend_dict(backend, task)) return { "name": location, "running_backends_count": running_backends_count, "backends": backends, }
def test_backend_is_up(): assert True is backend_is_up({"status": "UP"}) assert True is backend_is_up({"status": "UP 1/2"}) assert False is backend_is_up({"status": "DOWN"}) assert False is backend_is_up({"status": "DOWN 1/2"}) assert False is backend_is_up({"status": "MAINT"})