Пример #1
0
        /// <summary>
        /// Create a new ContextRuntime.
        /// </summary>
        /// <param name="serviceInjector"></param>
        /// <param name="contextConfiguration">the Configuration for this context.</param>
        /// <param name="parentContext"></param>
        public ContextRuntime(
            IInjector serviceInjector,
            IConfiguration contextConfiguration,
            Optional <ContextRuntime> parentContext)
        {
            ContextConfiguration config = contextConfiguration as ContextConfiguration;

            if (config == null)
            {
                var e = new ArgumentException("contextConfiguration is not of type ContextConfiguration");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
            }
            _contextLifeCycle = new ContextLifeCycle(config.Id);
            _serviceInjector  = serviceInjector;
            _parentContext    = parentContext;
            try
            {
                _contextInjector = serviceInjector.ForkInjector();
            }
            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);
            }
            // Trigger the context start events on contextInjector.
            _contextLifeCycle.Start();
        }
Пример #2
0
        /// <summary>
        /// Create a new ContextRuntime.
        /// </summary>
        /// <param name="serviceInjector"></param>
        /// <param name="contextConfiguration">the Configuration for this context.</param>
        /// <param name="parentContext"></param>
        public ContextRuntime(
                IInjector serviceInjector,
                IConfiguration contextConfiguration,
                Optional<ContextRuntime> parentContext)
        {
            ContextConfiguration config = contextConfiguration as ContextConfiguration;
            if (config == null)
            {
                var e = new ArgumentException("contextConfiguration is not of type ContextConfiguration");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
            }
            _contextLifeCycle = new ContextLifeCycle(config.Id);
            _serviceInjector = serviceInjector;
            _parentContext = parentContext;
            try
            {
                _contextInjector = serviceInjector.ForkInjector();
            }
            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);
            }
            // Trigger the context start events on contextInjector.
            _contextLifeCycle.Start();
        }
Пример #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 HandlContextException(ContextClientCodeException e)
        {
            LOGGER.Log(Level.Error, "ContextClientCodeException", e);
            byte[]             exception          = ByteUtilities.StringToByteArrays(e.ToString());
            ContextStatusProto contextStatusProto = new ContextStatusProto()
            {
                context_id    = e.ContextId,
                context_state = ContextStatusProto.State.FAIL,
                error         = exception
            };

            if (e.ParentId.IsPresent())
            {
                contextStatusProto.parent_id = e.ParentId.Value;
            }
            LOGGER.Log(Level.Error, string.Format(CultureInfo.InvariantCulture, "Sending Heartbeat for a failed context: {0}", contextStatusProto.ToString()));
            _heartBeatManager.OnNext(contextStatusProto);
        }
Пример #4
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="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);
        }
Пример #5
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);
        }
Пример #6
0
 /// <summary>
 /// THIS ASSUMES THAT IT IS CALLED ON A THREAD HOLDING THE LOCK ON THE HeartBeatManager
 /// </summary>
 /// <param name="e"></param>
 private void HandleContextException(ContextClientCodeException e)
 {
     LOGGER.Log(Level.Error, "ContextClientCodeException", e);
     byte[] exception = ByteUtilities.StringToByteArrays(e.ToString());
     ContextStatusProto contextStatusProto = new ContextStatusProto()
     {
         context_id = e.ContextId,
         context_state = ContextStatusProto.State.FAIL,
         error = exception
     };
     if (e.ParentId.IsPresent())
     {
         contextStatusProto.parent_id = e.ParentId.Value;
     }
     LOGGER.Log(Level.Error, "Sending Heartbeat for a failed context: {0}", contextStatusProto);
     _heartBeatManager.OnNext(contextStatusProto);
 }
Пример #7
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="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;
        }
Пример #8
0
        public ContextRuntime(
            string id,
            IInjector serviceInjector,
            IConfiguration contextConfiguration)
        {
            // This should only be used at the root context to support backward compatibility.
            LOGGER.Log(Level.Info, "Instantiating root context");
            _contextLifeCycle = new ContextLifeCycle(id);
            _serviceInjector = serviceInjector;
            _parentContext = Optional<ContextRuntime>.Empty();
            try
            {
                _contextInjector = serviceInjector.ForkInjector();
            }
            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(contextConfiguration), parentId, "Unable to spawn context", e);

                Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }

            // Trigger the context start events on contextInjector.
            _contextLifeCycle.Start();
        }