/// <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); }
/// <summary> /// Launches an Task on this context. /// </summary> /// <param name="taskConfiguration"></param> public Thread StartTaskOnNewThread(IConfiguration taskConfiguration) { 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); } } AssertChildContextNotPresent("Attempting to instantiate a child context on a context that is not the topmost active context."); var taskInjector = _contextInjector.ForkInjector(taskConfiguration); try { var taskRuntime = taskInjector.GetInstance <TaskRuntime>(); _task = Optional <TaskRuntime> .Of(taskRuntime); return(taskRuntime.StartTaskOnNewThread()); } catch (InjectionException e) { var taskId = string.Empty; try { taskId = taskInjector.GetNamedInstance <TaskConfigurationOptions.Identifier, string>(); } catch (Exception) { LOGGER.Log(Level.Error, "Unable to get Task ID from TaskConfiguration."); } var ex = TaskClientCodeException.Create(taskId, Id, "Unable to run the new task", e); Utilities.Diagnostics.Exceptions.CaughtAndThrow(ex, Level.Error, "Task start error.", LOGGER); return(null); } catch (Exception e) { throw new SystemException("System error in starting Task.", e); } } }
/// <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); }
/// <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); } } }