Beispiel #1
0
        internal static TaskClientCodeException CreateWithNonSerializableInnerException(
            TaskClientCodeException e, SerializationException serializationException)
        {
            var nonSerializableTaskException = NonSerializableTaskException.UnableToSerialize(e.InnerException, serializationException);

            return(new TaskClientCodeException(
                       e.TaskId,
                       e.ContextId,
                       string.Format("Unable to serialize Task control message. TaskClientCodeException message: {0}", e.Message),
                       nonSerializableTaskException));
        }
        internal static TaskClientCodeException CreateWithNonSerializableInnerException(
            TaskClientCodeException e, SerializationException serializationException)
        {
            var nonSerializableTaskException = NonSerializableTaskException.UnableToSerialize(e.InnerException, serializationException);

            return new TaskClientCodeException(
                e.TaskId, 
                e.ContextId, 
                string.Format("Unable to serialize Task control message. TaskClientCodeException message: {0}", e.Message), 
                nonSerializableTaskException);
        }
Beispiel #3
0
 public void Deliver(byte[] message)
 {
     if (_currentStatus.IsNotRunning())
     {
         Logger.Log(Level.Warning, string.Format(CultureInfo.InvariantCulture, "Trying to send a message to an task that is in {0} state. Ignored.", _currentStatus.State));
         return;
     }
     try
     {
         OnNext(new DriverMessageImpl(message));
     }
     catch (Exception e)
     {
         Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, "Error during message delivery.", Logger);
         _currentStatus.SetException(
             TaskClientCodeException.Create(TaskId, ContextId, "Error during message delivery.", e));
     }
 }
Beispiel #4
0
        public void Suspend(byte[] message)
        {
            Logger.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "Trying to suspend Task {0}", TaskId));

            if (_currentStatus.IsNotRunning())
            {
                Logger.Log(Level.Warning, string.Format(CultureInfo.InvariantCulture, "Trying to suspend an task that is in {0} state. Ignored.", _currentStatus.State));
                return;
            }
            try
            {
                OnNext(new SuspendEventImpl(message));
                _currentStatus.SetSuspendRequested();
            }
            catch (Exception e)
            {
                Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, "Error during Suspend.", Logger);
                _currentStatus.SetException(
                    TaskClientCodeException.Create(TaskId, ContextId, "Error during Suspend().", e));
            }
        }
Beispiel #5
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, "Sending Heartbeat for a failed task: {0}", taskStatus);
     _heartBeatManager.OnNext(taskStatus);
 }
        /// <summary>
        ///  Launches an Task on this context.
        /// </summary>
        /// <param name="taskConfiguration"></param>
        /// <param name="contextId"></param>
        /// <param name="heartBeatManager"></param>
        public void StartTask(TaskConfiguration taskConfiguration, string contextId, HeartBeatManager heartBeatManager)
        {
            lock (_contextLifeCycle)
            {
                bool taskPresent = _task.IsPresent();
                bool taskEnded = taskPresent && _task.Value.HasEnded();

                LOGGER.Log(Level.Info, "ContextRuntime::StartTask(TaskConfiguration)" + "task is present: " + taskPresent + " task has ended: " + taskEnded);
                if (taskPresent)
                {
                    LOGGER.Log(Level.Info, "Task state: " + _task.Value.GetTaskState());
                }

                if (taskEnded)
                {
                    // clean up state
                    _task = Optional<TaskRuntime>.Empty();
                    taskPresent = false;
                }
                if (taskPresent)
                {
                    var e = new InvalidOperationException(
                        string.Format(CultureInfo.InvariantCulture, "Attempting to spawn a child context when an Task with id '{0}' is running", _task.Value.TaskId)); // note: java code is putting thread id here
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
                if (_childContext.IsPresent())
                {
                    var e = new InvalidOperationException("Attempting to instantiate a child context on a context that is not the topmost active context.");
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
                try
                {
                    IInjector taskInjector = _contextInjector.ForkInjector(taskConfiguration.TangConfig);
                    LOGGER.Log(Level.Info, "Trying to inject task with configuration" + taskConfiguration.ToString());
                    TaskRuntime taskRuntime = new TaskRuntime(taskInjector, contextId, taskConfiguration.TaskId, heartBeatManager); // taskInjector.getInstance(TaskRuntime.class);
                    taskRuntime.Initialize();
                    System.Threading.Tasks.Task.Run(new Action(taskRuntime.Start));                    
                    _task = Optional<TaskRuntime>.Of(taskRuntime);
                }
                catch (Exception e)
                {
                    var ex = new TaskClientCodeException(taskConfiguration.TaskId, Id, "Unable to instantiate the new task", e);
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.CaughtAndThrow(ex, Level.Error, "Task start error.", LOGGER);
                }
            }
        }
Beispiel #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 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);
        }
Beispiel #8
0
        private TaskRuntime GetDeprecatedTaskRuntime(
            IInjector taskInjector, string contextId, IConfiguration taskConfiguration, IHeartBeatManager heartBeatManager)
        {
            var taskId = string.Empty;
            try
            {
                taskId = taskInjector.GetNamedInstance<TaskConfigurationOptions.Identifier, string>();
            }
            catch (Exception e)
            {
                var ex = new TaskClientCodeException(string.Empty, Id, "Unable to instantiate the new task", e);
                Utilities.Diagnostics.Exceptions.CaughtAndThrow(ex, Level.Error, "Cannot get instance of Task ID: " + e.StackTrace, LOGGER);
            }

            LOGGER.Log(Level.Info, "Trying to inject task with configuration" + taskConfiguration);

            return new TaskRuntime(taskInjector, contextId, taskId, heartBeatManager);
        }
Beispiel #9
0
        /// <summary>
        ///  Launches an Task on this context.
        /// </summary>
        /// <param name="taskConfiguration"></param>
        /// <param name="contextId"></param>
        /// <param name="heartBeatManager"></param>
        public void StartTask(IConfiguration taskConfiguration, string contextId, IHeartBeatManager heartBeatManager)
        {
            lock (_contextLifeCycle)
            {
                LOGGER.Log(Level.Info, "ContextRuntime::StartTask(TaskConfiguration) task is present: " + _task.IsPresent());

                if (_task.IsPresent())
                {
                    LOGGER.Log(Level.Info, "Task state: " + _task.Value.GetTaskState());
                    LOGGER.Log(Level.Info, "ContextRuntime::StartTask(TaskConfiguration) task has ended: " + _task.Value.HasEnded());

                    if (_task.Value.HasEnded())
                    {
                        // clean up state
                        _task = Optional<TaskRuntime>.Empty();
                    }
                    else
                    {
                        // note: java code is putting thread id here
                        var e = new InvalidOperationException(
                        string.Format(CultureInfo.InvariantCulture, "Attempting to spawn a child context when an Task with id '{0}' is running", _task.Value.TaskId));
                        Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                    }
                }

                if (_childContext.IsPresent())
                {
                    var e = new InvalidOperationException("Attempting to instantiate a child context on a context that is not the topmost active context.");
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }

                var taskInjector = _contextInjector.ForkInjector(taskConfiguration);

                var taskRuntime = _deprecatedTaskStart 
                    ? GetDeprecatedTaskRuntime(taskInjector, contextId, taskConfiguration, heartBeatManager) 
                    : taskInjector.GetInstance<TaskRuntime>();

                try
                {
                    _task = Optional<TaskRuntime>.Of(taskRuntime);
                    taskRuntime.RunTask();
                }
                catch (Exception e)
                {
                    var ex = new TaskClientCodeException(string.Empty, Id, "Unable to run the new task", e);
                    Utilities.Diagnostics.Exceptions.CaughtAndThrow(ex, Level.Error, "Task start error.", LOGGER);
                }
            }
        }