def test_assign_free_threads(self): cpu = get_cpu() workload = get_test_workload("a", 2, STATIC) workloads = {workload.get_id(): workload} cpu_allocator = GreedyCpuAllocator() self.__set_cpu_allocator(cpu_allocator) # Assign threads log.info("Assign threads") cpu_in_0 = copy.deepcopy(cpu) request = AllocateThreadsRequest(cpu_in_0, workload.get_id(), workloads, {}, DEFAULT_TEST_REQUEST_METADATA) cpu_out_0 = cpu_allocator.assign_threads(request).get_cpu() cpu_in_1 = copy.deepcopy(cpu) body = AllocateThreadsRequest(cpu_in_1, workload.get_id(), workloads, {}, DEFAULT_TEST_REQUEST_METADATA).to_dict() cpu_out_1 = self.client.put("/assign_threads", data=json.dumps(body), content_type='application/json') cpu_out_1 = deserialize_response(cpu_out_1.headers, cpu_out_1.json).get_cpu() log.info("cpu_out_0: {}".format(cpu_out_0)) log.info("cpu_out_1: {}".format(cpu_out_1)) self.assertEqual(cpu_out_0.to_dict(), cpu_out_1.to_dict()) # Free threads log.info("Free threads") cpu_in_0 = copy.deepcopy(cpu_out_0) request = AllocateThreadsRequest(cpu_in_0, workload.get_id(), workloads, {}, DEFAULT_TEST_REQUEST_METADATA) cpu_out_0 = cpu_allocator.free_threads(request).get_cpu() cpu_in_1 = copy.deepcopy(cpu_out_1) body = AllocateThreadsRequest(cpu_in_1, workload.get_id(), workloads, {}, DEFAULT_TEST_REQUEST_METADATA).to_dict() cpu_out_1 = self.client.put("/free_threads", data=json.dumps(body), content_type='application/json') cpu_out_1 = deserialize_response(cpu_out_1.headers, cpu_out_1.json).get_cpu() log.info("cpu_out_0: {}".format(cpu_out_0)) log.info("cpu_out_1: {}".format(cpu_out_1)) self.assertEqual(cpu_out_0.to_dict(), cpu_out_1.to_dict())
def free_threads(self, request: AllocateThreadsRequest) -> AllocateResponse: url = "{}/free_threads".format(self.__url) body = request.to_dict() try: log.info("freeing threads remotely for workload: %s", request.get_workload_id()) response = requests.put(url, json=body, headers=self.__headers, timeout=self.__timeout) except requests.exceptions.Timeout as e: log.error("freeing threads remotely for workload: %s timed out", request.get_workload_id()) raise e if response.status_code == 200: log.info( "freed threads remotely with response code: %s for workload: %s", response.status_code, request.get_workload_id()) return deserialize_response(response.headers, response.json()) log.error( "failed to free threads remotely for workload: %s with status code: %d", request.get_workload_id(), response.status_code) raise CpuAllocationException("Failed to free threads: {}".format( response.text))
def test_assign_free_threads(self): cpu = get_cpu() workload = get_test_workload("a", 2, STATIC) cpu_allocator = GreedyCpuAllocator() self.__set_cpu_allocator(cpu_allocator) # Assign threads log.info("Assign threads") cpu_in_0 = copy.deepcopy(cpu) request = get_no_usage_threads_request(cpu_in_0, [workload]) cpu_out_0 = cpu_allocator.assign_threads(request).get_cpu() cpu_in_1 = copy.deepcopy(cpu) request = get_no_usage_threads_request(cpu_in_1, [workload]) cpu_out_1 = self.client.put( "/assign_threads", data=json.dumps(request.to_dict()), content_type='application/json') cpu_out_1 = deserialize_response(cpu_out_1.headers, cpu_out_1.json).get_cpu() log.info("cpu_out_0: {}".format(cpu_out_0)) log.info("cpu_out_1: {}".format(cpu_out_1)) self.assertEqual(cpu_out_0.to_dict(), cpu_out_1.to_dict()) # Free threads log.info("Free threads") cpu_in_0 = copy.deepcopy(cpu_out_0) request = get_no_usage_threads_request(cpu_in_0, [workload]) cpu_out_0 = cpu_allocator.free_threads(request).get_cpu() cpu_in_1 = copy.deepcopy(cpu_out_1) request = get_no_usage_threads_request(cpu_in_1, [workload]) cpu_out_1 = self.client.put( "/free_threads", data=json.dumps(request.to_dict()), content_type='application/json') cpu_out_1 = deserialize_response(cpu_out_1.headers, cpu_out_1.json).get_cpu() log.info("cpu_out_0: {}".format(cpu_out_0)) log.info("cpu_out_1: {}".format(cpu_out_1)) self.assertEqual(cpu_out_0.to_dict(), cpu_out_1.to_dict())
def rebalance(self, request: AllocateRequest) -> AllocateResponse: url = "{}/rebalance".format(self.__url) body = request.to_dict() log.debug("url: {}, body: {}".format(url, body)) response = requests.put(url, json=body, headers=self.__headers, timeout=self.__timeout) log.debug("rebalance response code: {}".format(response.status_code)) if response.status_code == 200: return deserialize_response(response.headers, response.json()) raise CpuAllocationException("Failed to rebalance threads: {}".format(response.text))
def free_threads(self, request: AllocateThreadsRequest) -> AllocateResponse: url = "{}/free_threads".format(self.__url) body = request.to_dict() log.debug("url: {}, body: {}".format(url, body)) response = requests.put(url, json=body, headers=self.__headers, timeout=self.__solver_max_runtime_secs) log.debug("free_threads response code: {}".format( response.status_code)) if response.status_code == 200: return deserialize_response(response.headers, response.json()) raise CpuAllocationException("Failed to free threads: {}".format( response.text))
def rebalance(self, request: AllocateRequest) -> AllocateResponse: url = "{}/rebalance".format(self.__url) body = request.to_dict() try: log.info("rebalancing threads remotely") response = requests.put(url, json=body, headers=self.__headers, timeout=self.__timeout) except requests.exceptions.Timeout as e: log.info("rebalancing threads remotely timed out") raise e if response.status_code == 200: log.info("rebalanced threads remotely") return deserialize_response(response.headers, response.json()) log.error("failed to rebalance threads remotely with status code: %d", response.status_code) raise CpuAllocationException("Failed to rebalance threads: {}".format( response.text))