Esempio n. 1
0
        public TaskStatusProto ToProto()
        {
            Check();
            TaskStatusProto taskStatusProto = new TaskStatusProto()
            {
                context_id = _contextId,
                task_id    = _taskId,
                state      = GetProtoState(),
            };

            if (_result.IsPresent())
            {
                taskStatusProto.result = ByteUtilities.CopyBytesFrom(_result.Value);
            }
            else if (_lastException.IsPresent())
            {
                //final Encoder<Throwable> codec = new ObjectSerializableCodec<>();
                //final byte[] error = codec.encode(_lastException.get());
                byte[] error = ByteUtilities.StringToByteArrays(_lastException.Value.ToString());
                taskStatusProto.result = ByteUtilities.CopyBytesFrom(error);
            }
            else if (_state == TaskState.Running)
            {
                foreach (TaskMessage message in GetMessages())
                {
                    TaskStatusProto.TaskMessageProto taskMessageProto = new TaskStatusProto.TaskMessageProto()
                    {
                        source_id = message.MessageSourceId,
                        message   = ByteUtilities.CopyBytesFrom(message.Message),
                    };
                    taskStatusProto.task_message.Add(taskMessageProto);
                }
            }
            return(taskStatusProto);
        }
Esempio n. 2
0
        /// <summary>
        /// get state of the running Task
        /// </summary>
        /// <returns> the state of the running Task, if one is running.</returns>
        public Optional <TaskStatusProto> GetTaskStatus()
        {
            lock (_contextLifeCycle)
            {
                if (_task.IsPresent())
                {
                    if (_task.Value.HasEnded())
                    {
                        _task = Optional <TaskRuntime> .Empty();

                        return(Optional <TaskStatusProto> .Empty());
                    }
                    else
                    {
                        TaskStatusProto taskStatusProto = _task.Value.GetStatusProto();
                        if (taskStatusProto.state == State.RUNNING)
                        {
                            // only RUNNING status is allowed to rurn here, all other state pushed out to heartbeat
                            return(Optional <TaskStatusProto> .Of(taskStatusProto));
                        }
                        var e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Task state must be RUNNING, but instead is in {0} state", taskStatusProto.state));
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                        return(Optional <TaskStatusProto> .Empty());
                    }
                }
                else
                {
                    return(Optional <TaskStatusProto> .Empty());
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        ///  THIS ASSUMES THAT IT IS CALLED ON A THREAD HOLDING THE LOCK ON THE HeartBeatManager
        /// </summary>
        /// <param name="e"></param>
        private void HandleTaskInitializationException(TaskClientCodeException e)
        {
            byte[] error;
            try
            {
                error = ByteUtilities.SerializeToBinaryFormat(e);
            }
            catch (SerializationException se)
            {
                error = ByteUtilities.SerializeToBinaryFormat(
                    TaskClientCodeException.CreateWithNonSerializableInnerException(e, se));
            }

            var avroFailedTask = new AvroFailedTask
            {
                identifier = e.TaskId,
                cause      = error,
                data       = ByteUtilities.StringToByteArrays(e.ToString()),
                message    = e.Message
            };

            var taskStatus = new TaskStatusProto
            {
                context_id = e.ContextId,
                task_id    = e.TaskId,
                result     = AvroJsonSerializer <AvroFailedTask> .ToBytes(avroFailedTask),
                state      = State.FAILED
            };

            LOGGER.Log(Level.Error, "Sending Heartbeat for a failed task: {0}", taskStatus);
            _heartBeatManager.OnNext(taskStatus);
        }
Esempio n. 4
0
        public TaskStatusProto ToProto()
        {
            // This is locked because the Task continuation thread which sets the
            // result is potentially different from the HeartBeat thread.
            lock (_heartBeatManager)
            {
                Check();
                TaskStatusProto taskStatusProto = new TaskStatusProto()
                {
                    context_id = ContextId,
                    task_id    = TaskId,
                    state      = GetProtoState()
                };
                if (_result.IsPresent())
                {
                    taskStatusProto.result = ByteUtilities.CopyBytesFrom(_result.Value);
                }
                else if (_lastException.IsPresent())
                {
                    byte[] error;
                    try
                    {
                        error = ByteUtilities.SerializeToBinaryFormat(_lastException.Value);
                    }
                    catch (SerializationException se)
                    {
                        error = ByteUtilities.SerializeToBinaryFormat(
                            NonSerializableTaskException.UnableToSerialize(_lastException.Value, se));
                    }

                    var avroFailedTask = new AvroFailedTask
                    {
                        identifier = _taskId,
                        cause      = error,
                        data       = ByteUtilities.StringToByteArrays(_lastException.Value.ToString()),
                        message    = _lastException.Value.Message
                    };

                    taskStatusProto.result = AvroJsonSerializer <AvroFailedTask> .ToBytes(avroFailedTask);
                }
                else if (_state == TaskState.Running)
                {
                    foreach (TaskMessage message in GetMessages())
                    {
                        TaskStatusProto.TaskMessageProto taskMessageProto = new TaskStatusProto.TaskMessageProto()
                        {
                            source_id = message.MessageSourceId,
                            message   = ByteUtilities.CopyBytesFrom(message.Message),
                        };
                        taskStatusProto.task_message.Add(taskMessageProto);
                    }
                }
                return(taskStatusProto);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Handle task status messages.
        /// </summary>
        /// <param name="taskStatusProto"></param>
        private void Handle(TaskStatusProto taskStatusProto)
        {
            LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "Received task {0} status {1}", taskStatusProto.task_id, taskStatusProto.state));
            string taskId    = taskStatusProto.task_id;
            string contextId = taskStatusProto.context_id;
            State  taskState = taskStatusProto.state;

            if (taskState == State.INIT)
            {
                EvaluatorContext evaluatorContext = GetEvaluatorContext(contextId);
                _runningTask = new RunningTaskImpl(this, taskId, evaluatorContext);
                // this.dispatcher.onNext(RunningTask.class, this.runningTask);
            }
            else if (taskState == State.SUSPEND)
            {
                EvaluatorContext evaluatorContext = GetEvaluatorContext(contextId);
                _runningTask = null;
                byte[] message = taskStatusProto.result != null ? taskStatusProto.result : null;
                LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + evaluatorContext + message.ToString());
                //this.dispatcher.onNext(SuspendedTask.class, new SuspendedTaskImpl(evaluatorContext, message, taskId));
            }
            else if (taskState == State.DONE)
            {
                EvaluatorContext evaluatorContext = GetEvaluatorContext(contextId);
                _runningTask = null;
                byte[] message = taskStatusProto.result != null ? taskStatusProto.result : null;
                LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + evaluatorContext + message.ToString());
                //this.dispatcher.onNext(CompletedTask.class, new CompletedTaskImpl(evaluatorContext, message, taskId));
            }
            else if (taskState == State.FAILED)
            {
                _runningTask = null;
                //EvaluatorContext evaluatorContext = GetEvaluatorContext(contextId);
                //FailedTask failedTask = taskStatusProto.result != null ?
                //    new FailedTask(taskId, ByteUtilities.ByteArrarysToString(taskStatusProto.result), Optional<IActiveContext>.Of(evaluatorContext)) :
                //    new FailedTask(taskId, "Failed task: " + taskState, Optional<IActiveContext>.Of(evaluatorContext));
                //LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + failedTask.ToString());
                //this.dispatcher.onNext(FailedTask.class, taskException);
            }
            else if (taskStatusProto.task_message.Count > 0)
            {
                if (_runningTask != null)
                {
                    var e = new InvalidOperationException("runningTask must be null when there are multiple task messages");
                    Exceptions.Throw(e, LOGGER);
                }
                foreach (TaskStatusProto.TaskMessageProto taskMessageProto in taskStatusProto.task_message)
                {
                    LOGGER.Log(Level.Info, "TODO: REPLACE THIS " + taskMessageProto.ToString());
                    //        this.dispatcher.onNext(TaskMessage.class,
                    //new TaskMessageImpl(taskMessageProto.getMessage().toByteArray(),
                    //    taskId, contextId, taskMessageProto.getSourceId()));
                }
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Called with a specific TaskStatus that must be delivered to the driver
 /// </summary>
 /// <param name="taskStatusProto"></param>
 public void OnNext(TaskStatusProto taskStatusProto)
 {
     LOGGER.Log(Level.Verbose, "Before acquiring lock: HeartbeatManager::OnNext(TaskStatusProto)");
     lock (this)
     {
         LOGGER.Log(Level.Verbose, "HeartbeatManager::OnNext(TaskStatusProto)");
         EvaluatorHeartbeatProto heartbeatProto = GetEvaluatorHeartbeatProto(
             EvaluatorRuntime.GetEvaluatorStatus(),
             ContextManager.GetContextStatusCollection(),
             Optional <TaskStatusProto> .Of(taskStatusProto));
         LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "Triggered a heartbeat: {0}.", heartbeatProto));
         Send(heartbeatProto);
     }
 }
Esempio n. 7
0
        /// <summary>
        ///  THIS ASSUMES THAT IT IS CALLED ON A THREAD HOLDING THE LOCK ON THE HeartBeatManager
        /// </summary>
        /// <param name="e"></param>
        private void HandleTaskException(TaskClientCodeException e)
        {
            LOGGER.Log(Level.Error, "TaskClientCodeException", e);
            byte[]          exception  = ByteUtilities.StringToByteArrays(e.ToString());
            TaskStatusProto taskStatus = new TaskStatusProto()
            {
                context_id = e.ContextId,
                task_id    = e.TaskId,
                result     = exception,
                state      = State.FAILED
            };

            LOGGER.Log(Level.Error, string.Format(CultureInfo.InvariantCulture, "Sending Heartbeatb for a failed task: {0}", taskStatus.ToString()));
            _heartBeatManager.OnNext(taskStatus);
        }
Esempio n. 8
0
 public TaskStatusProto ToProto()
 {
     // This is locked because the Task continuation thread which sets the
     // result is potentially different from the HeartBeat thread.
     lock (_stateLock)
     {
         Check();
         TaskStatusProto taskStatusProto = new TaskStatusProto()
         {
             context_id = ContextId,
             task_id    = TaskId,
             state      = GetProtoState()
         };
         if (_result.IsPresent())
         {
             taskStatusProto.result = ByteUtilities.CopyBytesFrom(_result.Value);
         }
         else if (_lastException.IsPresent())
         {
             byte[] error = ByteUtilities.StringToByteArrays(_lastException.Value.ToString());
             taskStatusProto.result = ByteUtilities.CopyBytesFrom(error);
         }
         else if (_state == TaskState.Running)
         {
             foreach (TaskMessage message in GetMessages())
             {
                 TaskStatusProto.TaskMessageProto taskMessageProto = new TaskStatusProto.TaskMessageProto()
                 {
                     source_id = message.MessageSourceId,
                     message   = ByteUtilities.CopyBytesFrom(message.Message),
                 };
                 taskStatusProto.task_message.Add(taskMessageProto);
             }
         }
         return(taskStatusProto);
     }
 }