Ejemplo n.º 1
0
        public void OnNext(byte[] message)
        {
            LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "MESSAGE: TaskRuntime id [{0}] on evaluator id [{1}]", _id, _evaluatorManager.Id));
            ContextControlProto contextControlProto = new ContextControlProto();

            contextControlProto.task_message = message;
            _evaluatorManager.Handle(contextControlProto);
        }
Ejemplo n.º 2
0
        public void Dispose()
        {
            LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "DISPOSE: TaskRuntime id [{0}] on evaluator id [{1}]", _id, _evaluatorManager.Id));
            ContextControlProto contextControlProto = new ContextControlProto();

            contextControlProto.stop_task = new StopTaskProto();
            _evaluatorManager.Handle(contextControlProto);
        }
Ejemplo n.º 3
0
        public void Suspend()
        {
            LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "SUSPEND: TaskRuntime id [{0}] on evaluator id [{1}]", _id, _evaluatorManager.Id));
            ContextControlProto contextControlProto = new ContextControlProto();

            contextControlProto.suspend_task = new SuspendTaskProto();
            _evaluatorManager.Handle(contextControlProto);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Packages the TaskControlProto in an EvaluatorControlProto and forward it to the EvaluatorRuntime
        /// </summary>
        /// <param name="contextControlProto"></param>
        public void Handle(ContextControlProto contextControlProto)
        {
            lock (_evaluatorDescriptor)
            {
                LOGGER.Log(Level.Info, "Task control message from " + _evaluatorId);
                EvaluatorControlProto evaluatorControlProto = new EvaluatorControlProto();
                evaluatorControlProto.timestamp       = DateTime.Now.Ticks;
                evaluatorControlProto.identifier      = Id;
                evaluatorControlProto.context_control = contextControlProto;

                Handle(evaluatorControlProto);
            }
        }
Ejemplo n.º 5
0
        public void Dispose()
        {
            if (_disposed)
            {
                var e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Active context [{0}] already closed", _identifier));
                Exceptions.Throw(e, LOGGER);
            }
            LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "Submit close context: RunningEvaluator id [{0}] for context id [{1}]", EvaluatorId, Id));
            RemoveContextProto removeContextProto = new RemoveContextProto();

            removeContextProto.context_id = Id;
            ContextControlProto contextControlProto = new ContextControlProto();

            contextControlProto.remove_context = removeContextProto;
            _evaluatorManager.Handle(contextControlProto);
            _disposed = true;
        }
Ejemplo n.º 6
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)
                {
                    Org.Apache.REEF.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");
                    _contextStack.Peek().CloseTask(message);
                }
                else if (controlMessage.suspend_task != null)
                {
                    LOGGER.Log(Level.Info, "SuspendTask");
                    _contextStack.Peek().SuspendTask(message);
                }
                else if (controlMessage.task_message != null)
                {
                    LOGGER.Log(Level.Info, "DeliverTaskMessage");
                    _contextStack.Peek().DeliverTaskMessage(message);
                }
                else if (controlMessage.context_message != null)
                {
                    LOGGER.Log(Level.Info, "Handle context contol message");
                    ContextMessageProto contextMessageProto = controlMessage.context_message;
                    bool deliveredMessage = false;
                    foreach (ContextRuntime context in _contextStack)
                    {
                        if (context.Id.Equals(contextMessageProto.context_id))
                        {
                            LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "Handle context message {0}", controlMessage.context_message.message));
                            context.HandleContextMessaage(controlMessage.context_message.message);
                            deliveredMessage = true;
                            break;
                        }
                    }
                    if (!deliveredMessage)
                    {
                        InvalidOperationException e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Sent message to unknown context {0}", contextMessageProto.context_id));
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                    }
                }
                else
                {
                    InvalidOperationException e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unknown task control message: {0}", controlMessage.ToString()));
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
            }
            catch (Exception e)
            {
                if (e is TaskClientCodeException)
                {
                    HandleTaskException((TaskClientCodeException)e);
                }
                else if (e is ContextClientCodeException)
                {
                    HandlContextException((ContextClientCodeException)e);
                }
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.CaughtAndThrow(e, Level.Error, LOGGER);
            }
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
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 (TaskClientCodeException e)
            {
                HandleTaskException(e);
            }
            catch (ContextClientCodeException e)
            {
                HandleContextException(e);
            }
        }