/// <summary> /// Add a context to the stack. /// </summary> /// <param name="addContextProto"></param> private void AddContext(AddContextProto addContextProto) { lock (_contextStack) { ContextRuntime currentTopContext = _contextStack.Peek(); if (!currentTopContext.Id.Equals(addContextProto.parent_context_id, StringComparison.OrdinalIgnoreCase)) { var e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Trying to instantiate a child context on context with id '{0}' while the current top context id is {1}", addContextProto.parent_context_id, currentTopContext.Id)); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } string contextConfigString = addContextProto.context_configuration; ContextConfiguration contextConfiguration = new ContextConfiguration(contextConfigString); ContextRuntime newTopContext; if (addContextProto.service_configuration != null) { ServiceConfiguration serviceConfiguration = new ServiceConfiguration(addContextProto.service_configuration); newTopContext = currentTopContext.SpawnChildContext(contextConfiguration, serviceConfiguration.TangConfig); } else { newTopContext = currentTopContext.SpawnChildContext(contextConfiguration); } _contextStack.Push(newTopContext); } }
public ContextRuntime GetRootContext() { if (_rootContext == null) { _rootContext = GetRootContext(_rootServiceInjector, _rootContextConfiguration); } return(_rootContext); }
private ContextRuntime GetRootContext( IInjector rootServiceInjector, IConfiguration rootContextConfiguration) { ContextRuntime result; result = new ContextRuntime(rootServiceInjector, rootContextConfiguration); return(result); }
/// <summary> /// Launch an Task. /// </summary> /// <param name="startTaskProto"></param> private void StartTask(StartTaskProto startTaskProto) { lock (_contextStack) { ContextRuntime currentActiveContext = _contextStack.Peek(); string expectedContextId = startTaskProto.context_id; if (!expectedContextId.Equals(currentActiveContext.Id, StringComparison.OrdinalIgnoreCase)) { var e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Task expected context '{0}' but the active context has Id '{1}'", expectedContextId, currentActiveContext.Id)); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } TaskConfiguration taskConfiguration = new TaskConfiguration(startTaskProto.configuration); currentActiveContext.StartTask(taskConfiguration, expectedContextId, _heartBeatManager); } }
/// <summary> /// Shuts down. This forecefully kills the Task if there is one and then shuts down all Contexts on the stack, /// starting at the top. /// </summary> public void Dispose() { lock (_contextStack) { if (_contextStack != null && _contextStack.Any()) { LOGGER.Log(Level.Info, "context stack not empty, forcefully closing context runtime."); ContextRuntime runtime = _contextStack.Last(); if (runtime != null) { runtime.Dispose(); } } } }
/// <summary> /// Spawns a new context. /// The new context will have a serviceInjector that is created by forking the one in this object with the given /// serviceConfiguration. The contextConfiguration is used to fork the contextInjector from that new serviceInjector. /// </summary> /// <param name="contextConfiguration">the new context's context (local) Configuration.</param> /// <param name="serviceConfiguration">the new context's service Configuration.</param> /// <returns>a child context.</returns> public ContextRuntime SpawnChildContext(IConfiguration contextConfiguration, IConfiguration serviceConfiguration) { ContextRuntime childContext = null; lock (_contextLifeCycle) { if (_task.IsPresent()) { 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 childServiceInjector = _serviceInjector.ForkInjector(new IConfiguration[] { serviceConfiguration }); childContext = new ContextRuntime(childServiceInjector, contextConfiguration, Optional <ContextRuntime> .Of(this)); _childContext = Optional <ContextRuntime> .Of(childContext); return(childContext); } catch (Exception e) { Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); Optional <string> parentId = ParentContext.IsPresent() ? Optional <string> .Of(ParentContext.Value.Id) : Optional <string> .Empty(); ContextClientCodeException ex = new ContextClientCodeException(ContextClientCodeException.GetId(contextConfiguration), parentId, "Unable to spawn context", e); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } return(childContext); }
/// <summary> /// Spawns a new context without services of its own. /// The new context will have a serviceInjector that is created by forking the one in this object. The /// contextConfiguration is used to fork the contextInjector from that new serviceInjector. /// </summary> /// <param name="contextConfiguration">the new context's context (local) Configuration.</param> /// <returns> a child context.</returns> public ContextRuntime SpawnChildContext(IConfiguration contextConfiguration) { lock (_contextLifeCycle) { if (_task.IsPresent()) { 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); } IInjector childServiceInjector = _serviceInjector.ForkInjector(); ContextRuntime childContext = new ContextRuntime(childServiceInjector, contextConfiguration, Optional <ContextRuntime> .Of(this)); _childContext = Optional <ContextRuntime> .Of(childContext); return(childContext); } }
/// <summary> /// Start the context manager. This initiates the root context. /// </summary> public void Start() { lock (_contextStack) { ContextRuntime rootContext = _rootContextLauncher.GetRootContext(); LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "Instantiating root context with Id {0}", rootContext.Id)); _contextStack.Push(rootContext); if (_rootContextLauncher.RootTaskConfig.IsPresent()) { LOGGER.Log(Level.Info, "Launching the initial Task"); try { _contextStack.Peek().StartTask(_rootContextLauncher.RootTaskConfig.Value, _rootContextLauncher.RootContextConfig.Id, _heartBeatManager); } catch (TaskClientCodeException e) { Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, "Exception when trying to start a task.", LOGGER); HandleTaskException(e); } } } }