Beispiel #1
0
        /// <summary>
        /// Remove the context with the given ID from the stack.
        /// </summary>
        /// <param name="contextId"> context id</param>
        private void RemoveContext(string contextId)
        {
            lock (_contextLock)
            {
                string currentTopContextId = _topContext.Id;
                if (!contextId.Equals(_topContext.Id, StringComparison.OrdinalIgnoreCase))
                {
                    var e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Trying to close context with id '{0}' while the top context id is {1}", contextId, currentTopContextId));
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
                var hasParentContext = _topContext.ParentContext.IsPresent();
                _topContext.Dispose();
                if (hasParentContext)
                {
                    // We did not close the root context. Therefore, we need to inform the
                    // driver explicitly that this context is closed. The root context notification
                    // is implicit in the Evaluator close/done notification.
                    _heartBeatManager.OnNext(); // Ensure Driver gets notified of context DONE state
                }

                // does not matter if null.
                _topContext = _topContext.ParentContext.Value;
            }
            //// System.gc(); // TODO: garbage collect?
        }
Beispiel #2
0
        /// <summary>
        /// Add a context to the stack.
        /// </summary>
        /// <param name="addContextProto"></param>
        private void AddContext(AddContextProto addContextProto)
        {
            lock (_contextLock)
            {
                var currentTopContext = _topContext;
                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));
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }

                var contextConfiguration = _serializer.FromString(addContextProto.context_configuration);

                ContextRuntime newTopContext;
                if (!string.IsNullOrWhiteSpace(addContextProto.service_configuration))
                {
                    var serviceConfiguration = _serializer.FromString(addContextProto.service_configuration);
                    newTopContext = currentTopContext.SpawnChildContext(contextConfiguration, serviceConfiguration);
                }
                else
                {
                    newTopContext = currentTopContext.SpawnChildContext(contextConfiguration);
                }
                _topContext = newTopContext;
            }
        }
Beispiel #3
0
 /// <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);
     }
 }
Beispiel #4
0
 internal ContextRuntime GetRootContext()
 {
     if (_rootContext == null)
     {
         _rootContext = GetRootContext(_rootServiceInjector, _rootContextConfiguration);
     }
     return(_rootContext);
 }
 public ContextRuntime GetRootContext()
 {
     if (_rootContext == null)
     {
         _rootContext = GetRootContext(_rootServiceInjector, _rootContextConfiguration);
     }
     return _rootContext;
 }
Beispiel #6
0
        internal ContextRuntime GetRootContext()
        {
            if (_rootContext == null)
            {
                _rootContext = new ContextRuntime(_rootServiceInjector, _rootContextConfiguration, Optional <ContextRuntime> .Empty());
            }

            return(_rootContext);
        }
Beispiel #7
0
        private ContextRuntime GetRootContext(
            IInjector rootServiceInjector,
            IConfiguration rootContextConfiguration)
        {
            ContextRuntime result;

            result = new ContextRuntime(rootServiceInjector, rootContextConfiguration);
            return(result);
        }
Beispiel #8
0
 /// <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 (_contextLock)
     {
         if (_topContext != null)
         {
             LOGGER.Log(Level.Info, "context stack not empty, forcefully closing context runtime.");
             _topContext.Dispose();
             _topContext = null;
         }
     }
 }
Beispiel #9
0
 /// <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);
     }
 }
Beispiel #10
0
 /// <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();
             }
         }
     }
 }
Beispiel #11
0
        /// <summary>
        /// Launch an Task.
        /// </summary>
        /// <param name="startTaskProto"></param>
        private void StartTask(StartTaskProto startTaskProto)
        {
            lock (_contextLock)
            {
                ContextRuntime currentActiveContext = _topContext;
                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));
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }

                var configuration = _serializer.FromString(startTaskProto.configuration);
                currentActiveContext.StartTask(configuration);
            }
        }
Beispiel #12
0
 internal ContextRuntime GetRootContext()
 {
     if (_rootContext == null)
     {
         // TODO[JIRA REEF-1167]: Remove use of deprecated ContextRuntime constructor and deprecatedContextConfiguration
         var deprecatedContextConfiguration = _rootContextConfiguration as ContextConfiguration;
         if (deprecatedContextConfiguration != null)
         {
             LOGGER.Log(Level.Info, "Using deprecated ContextConfiguration.");
             _rootContext = new ContextRuntime(Id, _rootServiceInjector, _rootContextConfiguration);
         }
         else
         {
             _rootContext = new ContextRuntime(_rootServiceInjector, _rootContextConfiguration, Optional<ContextRuntime>.Empty());
         }
     }
     return _rootContext;
 }
Beispiel #13
0
        /// <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="childContextConfiguration">the new context's context (local) Configuration.</param>
        /// <param name="childServiceConfiguration">the new context's service Configuration.</param>
        /// <returns>a child context.</returns>
        public ContextRuntime SpawnChildContext(IConfiguration childContextConfiguration, IConfiguration childServiceConfiguration)
        {
            lock (_contextLifeCycle)
            {
                if (_task.IsPresent())
                {
                    var message =
                        string.Format(CultureInfo.InvariantCulture, "Attempting to spawn a child context when an Task with id '{0}' is running", _task.Value.TaskId);

                    var e = new InvalidOperationException(message);
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }

                AssertChildContextNotPresent("Attempting to instantiate a child context on a context that is not the topmost active context.");

                try
                {
                    var childServiceInjector = _serviceInjector.ForkInjector(childServiceConfiguration);
                    var childContext         = new ContextRuntime(childServiceInjector, childContextConfiguration, Optional <ContextRuntime> .Of(this));

                    _childContext = Optional <ContextRuntime> .Of(childContext);

                    return(childContext);
                }
                catch (Exception e)
                {
                    Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);

                    var childContextId = string.Empty;
                    try
                    {
                        var injector = TangFactory.GetTang().NewInjector(childContextConfiguration);
                        childContextId = injector.GetNamedInstance <ContextConfigurationOptions.ContextIdentifier, string>();
                    }
                    catch (InjectionException)
                    {
                        Utilities.Diagnostics.Exceptions.Caught(
                            e, Level.Error, "Unable to get Context ID from child ContextConfiguration. Using empty string.", LOGGER);
                    }

                    throw new ContextClientCodeException(childContextId, Optional <string> .Of(Id), "Unable to spawn context", e);
                }
            }
        }
        /// <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(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);
        }
Beispiel #15
0
        /// <summary>
        /// Start the context manager. This initiates the root context.
        /// </summary>
        public void Start()
        {
            lock (_contextLock)
            {
                _topContext = _rootContextLauncher.GetRootContext();
                LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "Instantiating root context with Id {0}", _topContext.Id));

                if (_rootContextLauncher.RootTaskConfig.IsPresent())
                {
                    LOGGER.Log(Level.Info, "Launching the initial Task");
                    try
                    {
                        _topContext.StartTask(_rootContextLauncher.RootTaskConfig.Value);
                    }
                    catch (TaskClientCodeException e)
                    {
                        Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, "Exception when trying to start a task.", LOGGER);
                        HandleTaskException(e);
                    }
                }
            }
        }
        /// <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);
            }
        }
Beispiel #17
0
        /// <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="childContextConfiguration">the new context's context (local) Configuration.</param>
        /// <param name="childServiceConfiguration">the new context's service Configuration.</param>
        /// <returns>a child context.</returns>
        public ContextRuntime SpawnChildContext(IConfiguration childContextConfiguration, IConfiguration childServiceConfiguration)
        {
            lock (_contextLifeCycle)
            {
                if (_task.IsPresent())
                {
                    var message =
                        string.Format(CultureInfo.InvariantCulture, "Attempting to spawn a child context when an Task with id '{0}' is running", _task.Value.TaskId);

                    var e = new InvalidOperationException(message);
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }

                AssertChildContextNotPresent("Attempting to instantiate a child context on a context that is not the topmost active context.");

                try
                {
                    var childServiceInjector = _serviceInjector.ForkInjector(childServiceConfiguration);
                    var childContext         = new ContextRuntime(childServiceInjector, childContextConfiguration, Optional <ContextRuntime> .Of(this));

                    _childContext = Optional <ContextRuntime> .Of(childContext);

                    return(childContext);
                }
                catch (Exception e)
                {
                    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(childContextConfiguration), parentId, "Unable to spawn context", e);

                    Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                }
            }
            return(null);
        }
Beispiel #18
0
        /// <summary>
        /// Start the context manager. This initiates the root context.
        /// </summary>
        public void Start()
        {
            lock (_contextLock)
            {
                _topContext = _rootContextLauncher.GetRootContext();
                LOGGER.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "Instantiating root context with Id {0}", _topContext.Id));

                if (_rootContextLauncher.RootTaskConfig.IsPresent())
                {
                    LOGGER.Log(Level.Info, "Launching the initial Task");
                    try
                    {
                        _topContext.StartTask(_rootContextLauncher.RootTaskConfig.Value, _rootContextLauncher.RootContextConfig.Id, _heartBeatManager);
                    }
                    catch (TaskClientCodeException e)
                    {
                        Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, "Exception when trying to start a task.", LOGGER);
                        HandleTaskException(e);
                    }
                }
            }
        }
Beispiel #19
0
        /// <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);
                    }
                }
            }
        }
Beispiel #20
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);
            }
        }
Beispiel #21
0
        /// <summary>
        /// Remove the context with the given ID from the stack.
        /// </summary>
        /// <param name="contextId"> context id</param>
        private void RemoveContext(string contextId)
        {
            lock (_contextLock)
            {
                string currentTopContextId = _topContext.Id;
                if (!contextId.Equals(_topContext.Id, StringComparison.OrdinalIgnoreCase))
                {
                    var e = new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Trying to close context with id '{0}' while the top context id is {1}", contextId, currentTopContextId));
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
                var hasParentContext = _topContext.ParentContext.IsPresent();
                _topContext.Dispose();
                if (hasParentContext)
                {
                    // We did not close the root context. Therefore, we need to inform the
                    // driver explicitly that this context is closed. The root context notification
                    // is implicit in the Evaluator close/done notification.
                    _heartBeatManager.OnNext(); // Ensure Driver gets notified of context DONE state
                }

                // does not matter if null.
                _topContext = _topContext.ParentContext.Value;
            }
            // System.gc(); // TODO: garbage collect?
        }
Beispiel #22
0
 /// <summary>
 /// Add a context to the stack.
 /// </summary>
 /// <param name="addContextProto"></param>
 private void AddContext(AddContextProto addContextProto)
 {
     lock (_contextLock)
     {
         var currentTopContext = _topContext;
         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));
             Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
         }
         string contextConfigString = addContextProto.context_configuration;
         var contextConfiguration = new ContextConfiguration(contextConfigString);
         ContextRuntime newTopContext;
         if (addContextProto.service_configuration != null)
         {
             var serviceConfiguration = new ServiceConfiguration(addContextProto.service_configuration);
             newTopContext = currentTopContext.SpawnChildContext(contextConfiguration, serviceConfiguration.TangConfig);
         }
         else
         {
             newTopContext = currentTopContext.SpawnChildContext(contextConfiguration);
         }
         _topContext = newTopContext;
     }
 }
Beispiel #23
0
 /// <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 (_contextLock)
     {
         if (_topContext != null)
         {
             LOGGER.Log(Level.Info, "context stack not empty, forcefully closing context runtime.");
             _topContext.Dispose();
             _topContext = null;
         }
     }
 }
        // 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);
            }
        }
Beispiel #25
0
        internal ContextRuntime GetRootContext()
        {
            if (_rootContext == null)
            {
                _rootContext = new ContextRuntime(_rootServiceInjector, _rootContextConfiguration, Optional<ContextRuntime>.Empty());
            }

            return _rootContext;
        }
 /// <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>
        ///  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(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;
        }
Beispiel #28
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 (Exception e)
            {
                if (e is TaskClientCodeException)
                {
                    HandleTaskException(e as TaskClientCodeException);
                }
                else if (e is ContextClientCodeException)
                {
                    HandleContextException(e as ContextClientCodeException);
                }
                Utilities.Diagnostics.Exceptions.CaughtAndThrow(e, Level.Error, LOGGER);
            }
        }
 private ContextRuntime GetRootContext( 
     IInjector rootServiceInjector,
     IConfiguration rootContextConfiguration)
 {
     ContextRuntime result;
     result = new ContextRuntime(rootServiceInjector, rootContextConfiguration);
     return result;
 }