def RunJob(self, request, context): """ Performs a run of the given job. """ job_compin = self._knowledge.actual_state.get_job_compin( request.job_id) if job_compin is None or job_compin.phase != CompinPhase.READY: run_status = { 'config': "", 'instanceId': request.job_id, 'runId': request.run_id, 'startTime': datetime.now().isoformat(), 'endTime': datetime.now().isoformat(), 'output': "", 'error': "This instance is not deployed yet.", 'returnCode': -1, } logging.info(f"Cannot run the probe. {run_status['error']}") headers = { "Content-Type": "application/json", "access-token": self._knowledge.api_endpoint_access_token } requests.post( f"http://{API_ENDPOINT_IP}:{API_ENDPOINT_PORT}/ccapi/on-fail", headers=headers, json=run_status) return RunJobAck() logging.info(f"Running job {job_compin.id}") params = RunParameters(instance_id=request.job_id, probe_id=request.job_id, run_id=request.run_id, state=request.state) job_agent: MiddlewareAgentStub = connect_to_grpc_server( MiddlewareAgentStub, job_compin.ip, AGENT_PORT) return job_agent.RunProbe(params)
def __init__(self, knowledge: Knowledge): super().__init__(knowledge) self.client_controller: ClientControllerInternalStub = connect_to_grpc_server( ClientControllerInternalStub, CLIENT_CONTROLLER_HOST, CLIENT_CONTROLLER_PORT )
def get_time(application, component, probe, percentile=None): requirements = arch_pb.ApplicationTimingRequirements() requirements.name = f"{application}_{component}_{probe}".upper() if percentile: percentile = int(percentile) contract = requirements.contracts.add() contract.percentile = percentile stub: PredictorStub = connect_to_grpc_server(PredictorStub, PREDICTOR_HOST, PREDICTOR_PORT) response = stub.ReportPercentiles(requirements) if response.mean < 0: print( f"The specified probe is not registered with the performance data aggregator." ) elif percentile: print( f"Probe {probe}: response time at {percentile} percentile is {response.contracts[0].time}" ) else: print(f"Probe {probe}: mean response time is {response.mean}")
def submit_app(architecture_grpc: arch_pb.Architecture) -> None: """ Uploads an app for the pre-assessment. Once pre-assessment is completed, the app will be sent for production :param architecture_grpc: protobuf representation of the application descriptor """ stub: DeployControllerStub = connect_to_grpc_server( DeployControllerStub, CTL_HOST, CTL_PORT) # Send the app for pre-assessment response = stub.SubmitArchitecture(architecture_grpc) # Check results if response.rc == deploy_pb.RC_OK: print("Application %s submitted successfully for pre-assessment" % architecture_grpc.name) elif response.rc == deploy_pb.RC_NAME_NOT_AVAILABLE: print( f"Name {architecture_grpc.name} is already taken by another application/namespace", file=sys.stderr) else: print(f"An unknown error happened during submission", file=sys.stderr)
def submit_requirements(architecture_grpc: arch_pb.Architecture) -> None: """ Uploads requirements for an app for the application review. :param architecture_grpc: protobuf representation of the application requirements """ stub: DeployControllerStub = connect_to_grpc_server( DeployControllerStub, CTL_HOST, CTL_PORT) # Send the app for pre-assessment response = stub.SubmitRequirements(architecture_grpc) # Check results if response.rc == deploy_pb.RC_OK: print("Application requirements submitted successfully") elif response.rc == deploy_pb.RC_NAME_NOT_AVAILABLE: print(f"Application {architecture_grpc.name} is not available", file=sys.stderr) elif response.rc == deploy_pb.RC_APP_ALREADY_ACCEPTED: print( f"Application {architecture_grpc.name} has already been deployed", file=sys.stderr) else: print(f"An unknown error happened during submission", file=sys.stderr)
def measure_workload(self, compin: ManagedCompin, probe: str, cycles: int) -> List[str]: stub: MiddlewareAgentStub = connect_to_grpc_server(MiddlewareAgentStub, compin.ip, AGENT_PORT, block=True, production=True) measure_msg = middleware_pb2.ProbeMeasurement( probe=middleware_pb2.ProbeDescriptor(name=probe), warmUpCycles=0, measuredCycles=cycles) result = stub.MeasureProbe(measure_msg) if result.result != middleware_pb2.ProbeCallResult.Result.Value("OK"): # TODO: propagate this exception to the highest level raise Exception("Error in measurements") data: List[str] = [] for row in stub.CollectProbeResults(measure_msg.probe): if row.WhichOneof("resultType") == "header": assert row.header.strip() == self.MEASUREMENT_HEADER elif row.WhichOneof("resultType") == "row": data.append(row.row) return data
def update_instance_phase(instance: ManagedCompin) -> None: stub = connect_to_grpc_server(MiddlewareAgentStub, instance.ip, AGENT_PORT) instance.phase = ExecutionContext.ping_compin(stub)
def ping_instance_ip(ip: str) -> CompinPhase: return ExecutionContext.ping_compin(connect_to_grpc_server(MiddlewareAgentStub, ip, AGENT_PORT))
def __init__(self): self._predictor_service = connect_to_grpc_server( PredictorStub, PREDICTOR_HOST, PREDICTOR_PORT)
def _run(self) -> None: client_controller = connect_to_grpc_server( ClientControllerExternalStub, CLIENT_CONTROLLER_EXTERNAL_HOST, CLIENT_CONTROLLER_EXTERNAL_PORT) request = mw_protocols.ClientConnectRequest( application=self._application, clientType=self._type) request.establishedConnection = False while True: try: if self._stop: return if self._id is not None: request.establishedConnection = True request.id = self._id for command in client_controller.Connect(request): command_type = command.WhichOneof("commandType") if command_type == 'ERROR': self._connected = False logging.info( "Received an error from Client Controller") if command.ERROR == mw_protocols.ClientResponseCode.Value( "IP_NOT_KNOWN"): logging.info( "This UE was not authorized to use this application" ) elif command.ERROR == mw_protocols.ClientResponseCode.Value( "NO_APP_SUBSCRIPTION"): logging.info( "You are not subscribed to this application") elif command.ERROR == mw_protocols.ClientResponseCode.Value( "ID_NOT_FOUND"): logging.info( f"ID {self._id} is not recognized by CC. Resetting ID." ) request.establishedConnection = False self._id = None break elif command_type == 'SET_DEPENDENCY': logging.info( "Received new address for %s dependency. Value = %s" % (command.SET_DEPENDENCY.name, command.SET_DEPENDENCY.ip)) with self._dependency_lock: self._dependencies[ command.SET_DEPENDENCY. name] = command.SET_DEPENDENCY.ip if self._update_call is not None: self._update_call(command.SET_DEPENDENCY.name, command.SET_DEPENDENCY.ip) self._connected = True elif command_type == 'SET_ID': logging.info( f"Received ID {command.SET_ID} from Client Controller" ) self._id = command.SET_ID self._connected = True elif command_type == 'WAIT': logging.info("WAIT received") self._connected = True if self._stop: self._connected = False return time.sleep(1) except Exception as e: self._connected = False logging.exception("") time.sleep(1)
def set_statefulness_key(self, ip) -> None: agent: MiddlewareAgentStub = connect_to_grpc_server( MiddlewareAgentStub, ip, AGENT_PORT) agent.SetStatefulnessKey( MongoParameters(shardKey=int(self.persistent_id)))
# MAPE-K wrapper mapek_wrapper: Optional[AdaptationLoopWrapper] = None if not args.fake_mapek: mapek_wrapper = AdaptationLoopWrapper() knowledge = mapek_wrapper.get_knowledge() else: # Adds some static nodes FAKE_HW_CONFIG = "hw0" knowledge = Knowledge() knowledge.nodes["node0"] = Node("node0", FAKE_HW_CONFIG, "", []) knowledge.nodes["node1"] = Node("node1", FAKE_HW_CONFIG, "", []) # Planner scenario_pln: ScenarioPlanner if not args.fake_planner: _predictor_stub: PredictorStub = connect_to_grpc_server(PredictorStub, PREDICTOR_HOST, PREDICTOR_PORT) while True: try: _predictor_stub.Predict(Assignment()) break except grpc.RpcError as e: logging.info(f"Predictor connection unsuccessful") time.sleep(1) logging.info(f"Successfully connected to predictor at {PREDICTOR_HOST}:{PREDICTOR_PORT}") scenario_pln = PredictorScenarioPlanner(knowledge, _predictor_stub) else: scenario_pln = FakeScenarioPlanner() dependency_solver = MasterSlaveSolver() app_judge = AppJudge(app_db, scenario_pln)
@app.route("/ccapi/on-fail", methods=['POST']) def on_fail(): run_status = request.json log_run_status(run_status) return "200 OK" if __name__ == "__main__": setup_logging() ivis_core_thread = Thread(target=app.run, args=(IVIS_INTERFACE_HOST, IVIS_PORT), daemon=True) ivis_core_thread.start() if not testing_job_agent: ivis_interface: IvisInterfaceStub = connect_to_grpc_server( IvisInterfaceStub, IVIS_INTERFACE_HOST, IVIS_INTERFACE_PORT) # Submit the job ivis_interface.SubmitJob( IvisJobDescription(job_id="ivisjob", code=SAMPLE_JOB_CODE, config=json.dumps( json.loads(SAMPLE_JOB_CONFIG)), docker_container="d3srepo/qoscloud-default")) logging.info("Job has been submitted") # Check the job status until DEPLOYED status = JobStatus() while status.status != JobAdmissionStatus.Value("DEPLOYED"): status = ivis_interface.GetJobStatus(JobID(job_id="ivisjob")) logging.info( f"Current job admission status: {JobAdmissionStatus.Name(status.status)}" )
import time from cloud_controller.middleware import AGENT_PORT from cloud_controller.middleware.helpers import connect_to_grpc_server from cloud_controller.middleware.middleware_pb2 import RunParameters from cloud_controller.middleware.middleware_pb2_grpc import MiddlewareAgentStub from cloud_controller.middleware.user_agents import ClientAgent if __name__ == "__main__": agent = ClientAgent("facerecognition3", "client") agent.start() ip = None while ip is None: ip = agent.get_dependency_address("recognizer") mwa: MiddlewareAgentStub = connect_to_grpc_server(MiddlewareAgentStub, ip, AGENT_PORT) for i in range(5): print(mwa.RunProbe(RunParameters( instance_id="recognizer", run_id=str(i), probe_id="recognize" ))) print(f"Executed run {i}") time.sleep(2)
def __init__(self, knowledge: Knowledge): self._deploy_controller: DeployControllerStub = connect_to_grpc_server( DeployControllerStub, CTL_HOST, CTL_PORT) self._knowledge: Knowledge = knowledge self._templates: List[str] = [] self._jobs: Dict[str, Architecture] = {}
def get_instance_agent(self, app_name: str, component_name: str, instance_id: str) -> Optional[MiddlewareAgentStub]: ip = self.get_instance_ip(app_name, component_name, instance_id) return connect_to_grpc_server(MiddlewareAgentStub, ip, AGENT_PORT)
from cloud_controller.middleware import AGENT_HOST, AGENT_PORT from cloud_controller.middleware.helpers import connect_to_grpc_server from cloud_controller.middleware.middleware_pb2 import InstanceConfig, CODE, RunParameters from cloud_controller.middleware.middleware_pb2_grpc import MiddlewareAgentStub mwa = connect_to_grpc_server(MiddlewareAgentStub, AGENT_HOST, AGENT_PORT) cfg = InstanceConfig(instance_id="id", api_endpoint_ip="0.0.0.0", api_endpoint_port=8282, production=False) probe = cfg.probes.add() probe.name = "recognize" mwa.InitializeInstance(cfg) mwa.RunProbe(RunParameters(instance_id="id", run_id="0", probe_id="recognize"))
def __init__(self, knowledge: Knowledge): super().__init__(knowledge) self.publisher = connect_to_grpc_server(DeployPublisherStub, PUBLISHER_HOST, PUBLISHER_PORT) self.postponed_apps = []