def publish_anticipated_load(session_name=None): iid = Compute.get_instance_id() if session_name is None: nactive = BaseContainer.num_active(BaseContainer.SFX_INT) else: JBoxSessionProps.attach_instance(Compute.get_install_id(), session_name, iid, "Preparing") nactive = BaseContainer.num_active(BaseContainer.SFX_INT) + 1 cont_load_pct = min(100, max(0, nactive * 100 / SessContainer.MAX_CONTAINERS)) self_load = max(Compute.get_instance_stats(iid, 'Load'), cont_load_pct) Compute.publish_stats("Load", "Percent", self_load) accept = Compute.should_accept_session(is_proposed_cluster_leader()) JBoxInstanceProps.set_props(Compute.get_install_id(), iid, load=self_load, accept=accept)
def get_session_status(): ret = {} for c in BaseContainer.session_containers(allcontainers=True): name = c["Names"][0] if (("Names" in c) and (c["Names"] is not None)) else c["Id"][0:12] ret[name] = c["Status"] return ret
def publish_perf_counters(): """ Publish performance counters. Used for status monitoring and auto scaling. """ VolMgr.refresh_disk_use_status() nactive = BaseContainer.num_active(BaseContainer.SFX_INT) stats = [] stats.append(("NumActiveContainers", "Count", nactive)) nactive_api = BaseContainer.num_active(BaseContainer.SFX_API) stats.append(("NumActiveAPIContainers", "Count", nactive_api)) curr_cpu_used_pct = psutil.cpu_percent() last_cpu_used_pct = curr_cpu_used_pct if BaseContainer.LAST_CPU_PCT is None else BaseContainer.LAST_CPU_PCT BaseContainer.LAST_CPU_PCT = curr_cpu_used_pct cpu_used_pct = int((curr_cpu_used_pct + last_cpu_used_pct) / 2) stats.append(("CPUUsed", "Percent", cpu_used_pct)) mem_used_pct = psutil.virtual_memory().percent stats.append(("MemUsed", "Percent", mem_used_pct)) disk_used_pct = 0 for x in psutil.disk_partitions(): if not VolMgr.is_mount_path(x.mountpoint): try: disk_used_pct = max(psutil.disk_usage(x.mountpoint).percent, disk_used_pct) except: pass if BaseContainer.INITIAL_DISK_USED_PCT is None: BaseContainer.INITIAL_DISK_USED_PCT = disk_used_pct disk_used_pct = max(0, (disk_used_pct - BaseContainer.INITIAL_DISK_USED_PCT)) stats.append(("DiskUsed", "Percent", disk_used_pct)) cont_load_pct = min(100, max(0, nactive * 100 / SessContainer.MAX_CONTAINERS)) stats.append(("ContainersUsed", "Percent", cont_load_pct)) api_cont_load_pct = min(100, max(0, nactive_api * 100 / APIContainer.MAX_CONTAINERS)) stats.append(("APIContainersUsed", "Percent", api_cont_load_pct)) stats.append(("DiskIdsUsed", "Percent", VolMgr.used_pct())) overall_load_pct = max( cont_load_pct, api_cont_load_pct, disk_used_pct, mem_used_pct, cpu_used_pct, VolMgr.used_pct() ) stats.append(("Load", "Percent", overall_load_pct)) Compute.publish_stats_multi(stats)
def publish_perf_counters(): """ Publish performance counters. Used for status monitoring and auto scaling. """ VolMgr.refresh_disk_use_status() nactive = BaseContainer.num_active(BaseContainer.SFX_INT) stats = [] stats.append(("NumActiveContainers", "Count", nactive)) nactive_api = BaseContainer.num_active(BaseContainer.SFX_API) stats.append(("NumActiveAPIContainers", "Count", nactive_api)) curr_cpu_used_pct = psutil.cpu_percent() last_cpu_used_pct = curr_cpu_used_pct if BaseContainer.LAST_CPU_PCT is None else BaseContainer.LAST_CPU_PCT BaseContainer.LAST_CPU_PCT = curr_cpu_used_pct cpu_used_pct = int((curr_cpu_used_pct + last_cpu_used_pct)/2) stats.append(("CPUUsed", "Percent", cpu_used_pct)) mem_used_pct = psutil.virtual_memory().percent stats.append(("MemUsed", "Percent", mem_used_pct)) disk_used_pct = 0 for x in psutil.disk_partitions(): if not VolMgr.is_mount_path(x.mountpoint): try: disk_used_pct = max(psutil.disk_usage(x.mountpoint).percent, disk_used_pct) except: pass if BaseContainer.INITIAL_DISK_USED_PCT is None: BaseContainer.INITIAL_DISK_USED_PCT = disk_used_pct disk_used_pct = max(0, (disk_used_pct - BaseContainer.INITIAL_DISK_USED_PCT)) stats.append(("DiskUsed", "Percent", disk_used_pct)) cont_load_pct = min(100, max(0, nactive * 100 / SessContainer.MAX_CONTAINERS)) stats.append(("ContainersUsed", "Percent", cont_load_pct)) api_cont_load_pct = min(100, max(0, nactive_api * 100 / APIContainer.MAX_CONTAINERS)) stats.append(("APIContainersUsed", "Percent", api_cont_load_pct)) stats.append(("DiskIdsUsed", "Percent", VolMgr.used_pct())) overall_load_pct = max(cont_load_pct, api_cont_load_pct, disk_used_pct, mem_used_pct, cpu_used_pct, VolMgr.used_pct()) stats.append(("Load", "Percent", overall_load_pct)) Compute.publish_stats_multi(stats)
def get_api_status(): api_status = dict() for c in BaseContainer.api_containers(allcontainers=True): name = c["Names"][0] if (("Names" in c) and (c["Names"] is not None)) else c["Id"][0:12] api_name = APIContainer.get_api_name_from_container_name(name) if api_name is None: continue cnt = api_status.get(api_name, 0) api_status[api_name] = cnt + 1 self_load = Compute.get_instance_stats(Compute.get_instance_id(), 'Load') accept = Compute.should_accept_session(is_proposed_cluster_leader()) return {'load': self_load, 'accept': accept, 'api_status': api_status}
def publish_instance_state(): iid = Compute.get_instance_id() api_status = dict() for c in BaseContainer.api_containers(allcontainers=True): name = c["Names"][0] if (("Names" in c) and (c["Names"] is not None)) else c["Id"][0:12] api_name = APIContainer.get_api_name_from_container_name(name) if api_name is None: continue cnt = api_status.get(api_name, 0) api_status[api_name] = cnt + 1 self_load = Compute.get_instance_stats(iid, 'Load') accept = Compute.should_accept_session(is_proposed_cluster_leader()) JBoxInstanceProps.set_props(Compute.get_install_id(), iid, load=self_load, accept=accept, api_status=api_status)
def is_terminating(): if not JBoxCfg.get('cloud_host.scale_down'): return False num_active = BaseContainer.num_active() terminate = (num_active == 0) and Compute.can_terminate(is_proposed_cluster_leader()) if terminate: JBoxd.log_warn("terminating to scale down") try: Compute.deregister_instance_dns() except: JBoxd.log_error("Error deregistering instance dns") Compute.terminate_instance() return terminate