Exemplo n.º 1
0
        // 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);
            }
        }
Exemplo n.º 2
0
        // 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);
            }
        }