public void Handle(EvaluatorControlProto message) { lock (_heartBeatManager) { Logger.Log(Level.Info, "Handle Evaluator control message"); if (!message.identifier.Equals(_evaluatorId, StringComparison.OrdinalIgnoreCase)) { OnException(new InvalidOperationException( string.Format(CultureInfo.InvariantCulture, "Identifier mismatch: message for evaluator id[{0}] sent to evaluator id[{1}]", message.identifier, _evaluatorId))); } else if (_state == State.DONE) { if (message.done_evaluator != null) { Logger.Log(Level.Info, "Received ACK from Driver, shutting down Evaluator."); _clock.Dispose(); return; } else { OnException(new InvalidOperationException("Received a control message from Driver after Evaluator is done.")); } } else if (_state != State.RUNNING) { OnException(new InvalidOperationException( string.Format(CultureInfo.InvariantCulture, "Evaluator received a control message but its state is not {0} but rather {1}", State.RUNNING, _state))); } else { if (message.context_control != null) { Logger.Log(Level.Info, "Send task control message to ContextManager"); try { _contextManager.HandleTaskControl(message.context_control); if (_contextManager.ContextStackIsEmpty() && _state == State.RUNNING) { Logger.Log(Level.Info, "Context stack is empty, done"); _state = State.DONE; _heartBeatManager.OnNext(GetEvaluatorStatus()); } } catch (Exception e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, Logger); OnException(e); Utilities.Diagnostics.Exceptions.Throw(new InvalidOperationException(e.ToString(), e), Logger); } } if (message.kill_evaluator != null) { Logger.Log(Level.Info, "Evaluator {0} has been killed by the driver.", _evaluatorId); _state = State.KILLED; _clock.Dispose(); } } } }
private void Heartbeat() { _heartBeatManager.OnNext(ToProto()); }
// TODO: codes here are slightly different from java since the protobuf.net does not generate the HasXXX method, may want to switch to proto-port later /// <summary> /// Processes the given ContextControlProto to launch / close / suspend Tasks and Contexts. /// This also triggers the HeartBeatManager to send a heartbeat with the result of this operation. /// </summary> /// <param name="controlMessage"></param> public void HandleTaskControl(ContextControlProto controlMessage) { try { byte[] message = controlMessage.task_message; if (controlMessage.add_context != null && controlMessage.remove_context != null) { Utilities.Diagnostics.Exceptions.Throw(new InvalidOperationException("Received a message with both add and remove context. This is unsupported."), LOGGER); } if (controlMessage.add_context != null) { LOGGER.Log(Level.Info, "AddContext"); AddContext(controlMessage.add_context); // support submitContextAndTask() if (controlMessage.start_task != null) { LOGGER.Log(Level.Info, "StartTask"); StartTask(controlMessage.start_task); } else { // We need to trigger a heartbeat here. In other cases, the heartbeat will be triggered by the TaskRuntime // Therefore this call can not go into addContext LOGGER.Log(Level.Info, "Trigger Heartbeat"); _heartBeatManager.OnNext(); } } else if (controlMessage.remove_context != null) { LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "RemoveContext with id {0}", controlMessage.remove_context.context_id)); RemoveContext(controlMessage.remove_context.context_id); } else if (controlMessage.start_task != null) { LOGGER.Log(Level.Info, "StartTask only"); StartTask(controlMessage.start_task); } else if (controlMessage.stop_task != null) { LOGGER.Log(Level.Info, "CloseTask"); lock (_contextLock) { _topContext.CloseTask(message); } } else if (controlMessage.suspend_task != null) { LOGGER.Log(Level.Info, "SuspendTask"); lock (_contextLock) { _topContext.SuspendTask(message); } } else if (controlMessage.task_message != null) { LOGGER.Log(Level.Info, "DeliverTaskMessage"); lock (_contextLock) { _topContext.DeliverTaskMessage(message); } } else if (controlMessage.context_message != null) { LOGGER.Log(Level.Info, "Handle context control message"); ContextMessageProto contextMessageProto = controlMessage.context_message; ContextRuntime context = null; lock (_contextLock) { if (_topContext != null) { context = _topContext.GetContextStack().FirstOrDefault(ctx => ctx.Id.Equals(contextMessageProto.context_id)); } } if (context != null) { context.HandleContextMessage(controlMessage.context_message.message); } else { var e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Sent message to unknown context {0}", contextMessageProto.context_id)); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } } else { InvalidOperationException e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unknown task control message: {0}", controlMessage.ToString())); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } } catch (Exception e) { if (e is TaskClientCodeException) { HandleTaskException(e as TaskClientCodeException); } else if (e is ContextClientCodeException) { HandleContextException(e as ContextClientCodeException); } Utilities.Diagnostics.Exceptions.CaughtAndThrow(e, Level.Error, LOGGER); } }
// TODO: codes here are slightly different from java since the protobuf.net does not generate the HasXXX method, may want to switch to proto-port later /// <summary> /// Processes the given ContextControlProto to launch / close / suspend Tasks and Contexts. /// This also triggers the HeartBeatManager to send a heartbeat with the result of this operation. /// </summary> /// <param name="controlMessage"></param> public void HandleTaskControl(ContextControlProto controlMessage) { try { byte[] message = controlMessage.task_message; if (controlMessage.add_context != null && controlMessage.remove_context != null) { Utilities.Diagnostics.Exceptions.Throw( new InvalidOperationException( "Received a message with both add and remove context. This is unsupported."), LOGGER); } if (controlMessage.add_context != null) { LOGGER.Log(Level.Info, "AddContext"); AddContext(controlMessage.add_context); // support submitContextAndTask() if (controlMessage.start_task != null) { LOGGER.Log(Level.Info, "StartTask"); StartTask(controlMessage.start_task); } else { // We need to trigger a heartbeat here. In other cases, the heartbeat will be triggered by the TaskRuntime // Therefore this call can not go into addContext LOGGER.Log(Level.Info, "Trigger Heartbeat"); _heartBeatManager.OnNext(); } } else if (controlMessage.remove_context != null) { LOGGER.Log(Level.Info, "RemoveContext with id {0}", controlMessage.remove_context.context_id); RemoveContext(controlMessage.remove_context.context_id); } else if (controlMessage.start_task != null) { LOGGER.Log(Level.Info, "StartTask only"); StartTask(controlMessage.start_task); } else if (controlMessage.stop_task != null) { LOGGER.Log(Level.Info, "CloseTask"); lock (_contextLock) { _topContext.CloseTask(message); } } else if (controlMessage.suspend_task != null) { LOGGER.Log(Level.Info, "SuspendTask"); lock (_contextLock) { _topContext.SuspendTask(message); } } else if (controlMessage.task_message != null) { LOGGER.Log(Level.Info, "DeliverTaskMessage"); lock (_contextLock) { _topContext.DeliverTaskMessage(message); } } else if (controlMessage.context_message != null) { LOGGER.Log(Level.Info, "Handle context control message"); ContextMessageProto contextMessageProto = controlMessage.context_message; ContextRuntime context = null; lock (_contextLock) { if (_topContext != null) { context = _topContext.GetContextStack() .FirstOrDefault(ctx => ctx.Id.Equals(contextMessageProto.context_id)); } } if (context != null) { context.HandleContextMessage(controlMessage.context_message.message); } else { var e = new InvalidOperationException( string.Format(CultureInfo.InvariantCulture, "Sent message to unknown context {0}", contextMessageProto.context_id)); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } } else { InvalidOperationException e = new InvalidOperationException( string.Format(CultureInfo.InvariantCulture, "Unknown task control message: {0}", controlMessage)); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } } catch (TaskClientCodeException e) { HandleTaskInitializationException(e); } catch (ContextClientCodeException e) { if (!e.ParentId.IsPresent()) { // Crash the Evaluator if an error occurs in the root context. throw; } HandleContextException(e, e.ContextId, e.ParentId.Value); } catch (ContextStartHandlerException e) { if (!e.ParentId.IsPresent()) { // Crash the Evaluator if an error occurs in the root context. ExceptionDispatchInfo.Capture(e.InnerException).Throw(); } // Send back the InnerException to the Driver. HandleContextException(e.InnerException, e.ContextId, e.ParentId.Value); } catch (ContextException e) { if (!e.ParentId.IsPresent()) { // Crash the Evaluator if an error occurs in the root context. ExceptionDispatchInfo.Capture(e.InnerException).Throw(); } else { // Remove the top context. // We do not need to do this for ContextStartHandlerException or ContextClientCodeException // since the child Context has not been spawned those Exceptions were thrown. if (_topContext == null || !_topContext.ParentContext.IsPresent()) { throw new InvalidOperationException("Top context cannot be null if Parent ID is present."); } _topContext = _topContext.ParentContext.Value; } // Send back the InnerException to the Driver. HandleContextException(e.InnerException, e.ContextId, e.ParentId.Value); } }