Пример #1
0
        private static SerdeCompileTimeResolver MakeSerdeResolver(
            ConfigurationCompilerSerde config,
            IDictionary<string, object> transientConfiguration)
        {
            var context = new SerdeProviderFactoryContext();

            IList<SerdeProvider> providers = null;
            if (config.SerdeProviderFactories != null) {
                foreach (var factory in config.SerdeProviderFactories) {
                    try {
                        var instance = TypeHelper.Instantiate<SerdeProviderFactory>(
                            factory,
                            TransientConfigurationResolver.ResolveClassForNameProvider(transientConfiguration));
                        var provider = instance.GetProvider(context);
                        if (provider == null) {
                            throw new ConfigurationException("Binding provider factory '" + factory + "' returned a null value");
                        }

                        if (providers == null) {
                            providers = new List<SerdeProvider>();
                        }

                        providers.Add(provider);
                    }
                    catch (Exception ex) {
                        throw new ConfigurationException("Binding provider factory '" + factory + "' failed to initialize: " + ex.Message, ex);
                    }
                }
            }

            if (providers == null) {
                providers = EmptyList<SerdeProvider>.Instance;
            }

            return new SerdeCompileTimeResolverImpl(
                providers,
                config.IsEnableExtendedBuiltin,
                config.IsEnableSerializable,
                config.IsEnableSerializationFallback);
        }
Пример #2
0
        /// <summary>
        /// Performs the initialization.
        /// </summary>
        /// <param name="startTime">optional start time</param>
        protected void DoInitialize(long?startTime)
        {
            Log.Info("Initializing engine URI '" + _engineURI + "' version " + Version.VERSION);

            // This setting applies to all engines in a given VM
            ExecutionPathDebugLog.IsEnabled           = _configSnapshot.EngineDefaults.Logging.IsEnableExecutionDebug;
            ExecutionPathDebugLog.IsTimerDebugEnabled = _configSnapshot.EngineDefaults.Logging.IsEnableTimerDebug;

            // This setting applies to all engines in a given VM
            MetricReportingPath.IsMetricsEnabled =
                _configSnapshot.EngineDefaults.MetricsReporting.IsEnableMetricsReporting;

            // This setting applies to all engines in a given VM
            AuditPath.AuditPattern = _configSnapshot.EngineDefaults.Logging.AuditPattern;

            // This setting applies to all engines in a given VM
            ThreadingOption.IsThreadingEnabled = (
                ThreadingOption.IsThreadingEnabled ||
                _configSnapshot.EngineDefaults.Threading.IsThreadPoolTimerExec ||
                _configSnapshot.EngineDefaults.Threading.IsThreadPoolInbound ||
                _configSnapshot.EngineDefaults.Threading.IsThreadPoolRouteExec ||
                _configSnapshot.EngineDefaults.Threading.IsThreadPoolOutbound
                );

            if (_engine != null)
            {
                _engine.Services.TimerService.StopInternalClock(false);
                // Give the timer thread a little moment to catch up
                Thread.Sleep(100);

                if (_configSnapshot.EngineDefaults.MetricsReporting.IsEnableMetricsReporting)
                {
                    DestroyEngineMetrics(_engine.Services.EngineURI);
                }

                _engine.Runtime.Initialize();

                _engine.Services.Dispose();
            }

            // Make EP services context factory
            var epServicesContextFactoryClassName = _configSnapshot.EPServicesContextFactoryClassName;
            EPServicesContextFactory epServicesContextFactory;

            if (epServicesContextFactoryClassName == null)
            {
                // Check system properties
                epServicesContextFactoryClassName =
                    Environment.GetEnvironmentVariable("ESPER_EPSERVICE_CONTEXT_FACTORY_CLASS");
            }
            if (epServicesContextFactoryClassName == null)
            {
                epServicesContextFactory = new EPServicesContextFactoryDefault();
            }
            else
            {
                Type clazz;
                try
                {
                    clazz =
                        TransientConfigurationResolver.ResolveClassForNameProvider(
                            _configSnapshot.TransientConfiguration).ClassForName(epServicesContextFactoryClassName);
                }
                catch (TypeLoadException)
                {
                    throw new ConfigurationException(
                              "Type '" + epServicesContextFactoryClassName + "' cannot be loaded");
                }

                Object obj;
                try
                {
                    obj = Activator.CreateInstance(clazz);
                }
                catch (TypeLoadException)
                {
                    throw new ConfigurationException("Class '" + clazz + "' cannot be instantiated");
                }
                catch (MissingMethodException e)
                {
                    throw new ConfigurationException(
                              "Error instantiating class - Default constructor was not found", e);
                }
                catch (MethodAccessException e)
                {
                    throw new ConfigurationException(
                              "Error instantiating class - Caller does not have permission to use constructor", e);
                }
                catch (ArgumentException e)
                {
                    throw new ConfigurationException("Error instantiating class - Type is not a RuntimeType", e);
                }

                epServicesContextFactory = (EPServicesContextFactory)obj;
            }

            var services = epServicesContextFactory.CreateServicesContext(this, _configSnapshot);

            // New runtime
            EPRuntimeSPI           runtimeSPI;
            InternalEventRouteDest routeDest;
            TimerCallback          timerCallback;
            var runtimeClassName = _configSnapshot.EngineDefaults.AlternativeContext.Runtime;

            if (runtimeClassName == null)
            {
                // Check system properties
                runtimeClassName = Environment.GetEnvironmentVariable("ESPER_EPRUNTIME_CLASS");
            }

            if (runtimeClassName == null)
            {
                var runtimeImpl = new EPRuntimeImpl(services);
                runtimeSPI    = runtimeImpl;
                routeDest     = runtimeImpl;
                timerCallback = runtimeImpl.TimerCallback;
            }
            else
            {
                Type clazz;
                try
                {
                    clazz = TypeHelper.ResolveType(runtimeClassName, true);
                }
                catch (TypeLoadException)
                {
                    throw new ConfigurationException("Class '" + runtimeClassName + "' cannot be loaded");
                }

                Object obj;
                try
                {
                    var c = clazz.GetConstructor(new[] { typeof(EPServicesContext) });
                    obj = c.Invoke(new object[] { services });
                }
                catch (TypeLoadException)
                {
                    throw new ConfigurationException("Class '" + clazz + "' cannot be instantiated");
                }
                catch (MissingMethodException)
                {
                    throw new ConfigurationException(
                              "Class '" + clazz + "' cannot be instantiated, constructor accepting services was not found");
                }
                catch (MethodAccessException)
                {
                    throw new ConfigurationException("Illegal access instantiating class '" + clazz + "'");
                }

                runtimeSPI    = (EPRuntimeSPI)obj;
                routeDest     = (InternalEventRouteDest)obj;
                timerCallback = (TimerCallback)obj;
            }

            routeDest.InternalEventRouter         = services.InternalEventRouter;
            services.InternalEventEngineRouteDest = routeDest;

            // set current time, if applicable
            if (startTime != null)
            {
                services.SchedulingService.Time = startTime.Value;
            }

            // Configure services to use the new runtime
            services.TimerService.Callback = timerCallback;

            // Statement lifecycle init
            services.StatementLifecycleSvc.Init();

            // Filter service init
            services.FilterService.Init();

            // Schedule service init
            services.SchedulingService.Init();

            // New admin
            var configOps = new ConfigurationOperationsImpl(
                services.EventAdapterService, services.EventTypeIdGenerator, services.EngineImportService,
                services.VariableService, services.EngineSettingsService, services.ValueAddEventService,
                services.MetricsReportingService, services.StatementEventTypeRefService,
                services.StatementVariableRefService, services.PlugInViews, services.FilterService,
                services.PatternSubexpressionPoolSvc, services.MatchRecognizeStatePoolEngineSvc, services.TableService,
                _configSnapshot.TransientConfiguration);
            var defaultStreamSelector =
                SelectClauseStreamSelectorEnumExtensions.MapFromSODA(
                    _configSnapshot.EngineDefaults.StreamSelection.DefaultStreamSelector);
            EPAdministratorSPI adminSPI;
            var adminClassName = _configSnapshot.EngineDefaults.AlternativeContext.Admin;
            var adminContext   = new EPAdministratorContext(runtimeSPI, services, configOps, defaultStreamSelector);

            if (adminClassName == null)
            {
                // Check system properties
                adminClassName = Environment.GetEnvironmentVariable("ESPER_EPADMIN_CLASS");
            }
            if (adminClassName == null)
            {
                adminSPI = new EPAdministratorImpl(adminContext);
            }
            else
            {
                Type clazz;
                try
                {
                    clazz = TypeHelper.ResolveType(adminClassName, true);
                }
                catch (TypeLoadException)
                {
                    throw new ConfigurationException(
                              "Class '" + epServicesContextFactoryClassName + "' cannot be loaded");
                }

                Object obj;
                try
                {
                    var c = clazz.GetConstructor(new[] { typeof(EPAdministratorContext) });
                    obj = c.Invoke(new[] { adminContext });
                }
                catch (TypeLoadException)
                {
                    throw new ConfigurationException("Class '" + clazz + "' cannot be instantiated");
                }
                catch (MissingMethodException)
                {
                    throw new ConfigurationException(
                              "Class '" + clazz + "' cannot be instantiated, constructor accepting context was not found");
                }
                catch (MethodAccessException)
                {
                    throw new ConfigurationException("Illegal access instantiating class '" + clazz + "'");
                }

                adminSPI = (EPAdministratorSPI)obj;
            }

            // Start clocking
            if (_configSnapshot.EngineDefaults.Threading.IsInternalTimerEnabled)
            {
                if (_configSnapshot.EngineDefaults.TimeSource.TimeUnit != TimeUnit.MILLISECONDS)
                {
                    throw new ConfigurationException("Internal timer requires millisecond time resolution");
                }
                services.TimerService.StartInternalClock();
            }

            // Give the timer thread a little moment to start up
            Thread.Sleep(100);

            // Save engine instance
            _engine = new EPServiceEngine(services, runtimeSPI, adminSPI);

            // Load and initialize adapter loader classes
            LoadAdapters(services);

            // Initialize extension services
            if (services.EngineLevelExtensionServicesContext != null)
            {
                services.EngineLevelExtensionServicesContext.Init(services, runtimeSPI, adminSPI);
            }

            // Start metrics reporting, if any
            if (_configSnapshot.EngineDefaults.MetricsReporting.IsEnableMetricsReporting)
            {
                services.MetricsReportingService.SetContext(runtimeSPI, services);
            }

            // Start engine metrics report
            if (_configSnapshot.EngineDefaults.MetricsReporting.IsEnableMetricsReporting)
            {
                StartEngineMetrics(services, runtimeSPI);
            }

            // register with the statement lifecycle service
            services.StatementLifecycleSvc.LifecycleEvent += HandleLifecycleEvent;

            // call initialize listeners
            try
            {
                if (ServiceInitialized != null)
                {
                    ServiceInitialized(this, new ServiceProviderEventArgs(this));
                }
            }
            catch (Exception ex)
            {
                Log.Error("Runtime exception caught during an ServiceInitialized event:" + ex.Message, ex);
            }
        }
Пример #3
0
        /// <summary>
        ///     Performs the initialization.
        /// </summary>
        /// <param name="startTime">optional start time</param>
        protected void DoInitialize(long? startTime)
        {
            Log.Info("Initializing runtime URI '" + URI + "' version " + RuntimeVersion.RUNTIME_VERSION);

            // Retain config-at-initialization since config-last-provided can be set to new values and "initialize" can be called
            _configAtInitialization = _configLastProvided;

            // Verify settings
            if (_configLastProvided.Runtime.Threading.IsInternalTimerEnabled &&
                _configLastProvided.Common.TimeSource.TimeUnit != TimeUnit.MILLISECONDS) {
                throw new ConfigurationException("Internal timer requires millisecond time resolution");
            }

            // This setting applies to all runtimes in a given VM
            ExecutionPathDebugLog.IsDebugEnabled = _configLastProvided.Runtime.Logging.IsEnableExecutionDebug;
            ExecutionPathDebugLog.IsTimerDebugEnabled = _configLastProvided.Runtime.Logging.IsEnableTimerDebug;

            // This setting applies to all runtimes in a given VM
            AuditPath.AuditPattern = _configLastProvided.Runtime.Logging.AuditPattern;

            if (_runtimeEnvironment != null) {
                if (ServiceStatusProvider != null) {
                    ServiceStatusProvider.Set(false);
                }

                _runtimeEnvironment.Services.TimerService.StopInternalClock(false);

                if (_configLastProvided.Runtime.MetricsReporting.IsRuntimeMetrics) {
                    DestroyEngineMetrics(_runtimeEnvironment.Services.RuntimeURI);
                }

                _runtimeEnvironment.Runtime.Initialize();

                _runtimeEnvironment.Services.Destroy();
            }

            ServiceStatusProvider = new AtomicBoolean(true);
            // Make EP services context factory
            var epServicesContextFactoryClassName = _configLastProvided.Runtime.EPServicesContextFactoryClassName;
            EPServicesContextFactory epServicesContextFactory;
            if (epServicesContextFactoryClassName == null) {
                // Check system properties
                epServicesContextFactoryClassName = Environment.GetEnvironmentVariable("ESPER_EPSERVICE_CONTEXT_FACTORY_CLASS");
            }

            if (epServicesContextFactoryClassName == null) {
                epServicesContextFactory = new EPServicesContextFactoryDefault(Container);
            }
            else {
                Type clazz;
                try {
                    clazz = TransientConfigurationResolver.ResolveClassForNameProvider(_configLastProvided.Common.TransientConfiguration)
                        .ClassForName(epServicesContextFactoryClassName);
                }
                catch (TypeLoadException) {
                    throw new ConfigurationException("Class '" + epServicesContextFactoryClassName + "' cannot be loaded");
                }

                object obj;
                try {
                    obj = TypeHelper.Instantiate(clazz);
                }
                catch (Exception) {
                    throw new ConfigurationException("Class '" + clazz + "' cannot be instantiated");
                }

                epServicesContextFactory = (EPServicesContextFactory) obj;
            }

            EPServicesContext services;
            try {
                services = epServicesContextFactory.CreateServicesContext(this, _configLastProvided);
            }
            catch (EPException ex) {
                throw new ConfigurationException("Failed runtime startup: " + ex.Message, ex);
                //throw;
            }
            catch (Exception ex) {
                throw new ConfigurationException("Failed runtime startup: " + ex.Message, ex);
            }

            // new runtime
            EPEventServiceImpl eventService = epServicesContextFactory.CreateEPRuntime(services, ServiceStatusProvider);

            eventService.InternalEventRouter = services.InternalEventRouter;
            services.InternalEventRouteDest = eventService;

            // set current time, if applicable
            if (startTime != null) {
                services.SchedulingService.Time = startTime.Value;
            }

            // Configure services to use the new runtime
            services.TimerService.Callback = eventService;

            // New services
            EPDeploymentServiceSPI deploymentService = new EPDeploymentServiceImpl(services, this);
            var eventTypeService = new EPEventTypeServiceImpl(services);
            EPContextPartitionService contextPartitionService = new EPContextPartitionServiceImpl(services);
            EPVariableService variableService = new EPVariableServiceImpl(services);
            EPMetricsService metricsService = new EPMetricsServiceImpl(services);
            EPFireAndForgetService fireAndForgetService = new EpFireAndForgetServiceImpl(services, ServiceStatusProvider);
            EPStageServiceSPI stageService = new EPStageServiceImpl(services, ServiceStatusProvider);

            // Build runtime environment
            _runtimeEnvironment = new EPRuntimeEnv(
                services,
                eventService,
                deploymentService,
                eventTypeService,
                contextPartitionService,
                variableService,
                metricsService,
                fireAndForgetService,
                stageService);

            // Stage Recovery
            var stageIterator = services.StageRecoveryService.StagesIterate();
            while (stageIterator.MoveNext()) {
                var entry = stageIterator.Current;

                long currentTimeStage;
                if (services.EpServicesHA.CurrentTimeAsRecovered == null) {
                    currentTimeStage = services.SchedulingService.Time;
                } else if (!services.EpServicesHA.CurrentTimeStageAsRecovered.TryGetValue(entry.Value, out currentTimeStage)) {
                    currentTimeStage = services.SchedulingService.Time;
                }
                
                stageService.RecoverStage(entry.Key, entry.Value, currentTimeStage);
            }

            // Deployment Recovery
            var deploymentIterator = services.DeploymentRecoveryService.Deployments();
            ISet<EventType> protectedVisibleTypes = new LinkedHashSet<EventType>();
            while (deploymentIterator.MoveNext()) {
                var entry = deploymentIterator.Current;
                var deploymentId = entry.Key;

                StatementUserObjectRuntimeOption userObjectResolver = new ProxyStatementUserObjectRuntimeOption {
                    ProcGetUserObject = env => entry.Value.UserObjectsRuntime.Get(env.StatementId)
                };

                StatementNameRuntimeOption statementNameResolver =
                    env => entry.Value.StatementNamesWhenProvidedByAPI.Get(env.StatementId);

                StatementSubstitutionParameterOption substitutionParameterResolver = env => {
                    var param = entry.Value.SubstitutionParameters.Get(env.StatementId);
                    if (param == null) {
                        return;
                    }

                    if (env.SubstitutionParameterNames != null) {
                        foreach (var name in env.SubstitutionParameterNames) {
                            env.SetObject(name.Key, param.Get(name.Value));
                        }
                    }
                    else {
                        for (var i = 0; i < env.SubstitutionParameterTypes.Length; i++) {
                            env.SetObject(i + 1, param.Get(i + 1));
                        }
                    }
                };

                DeploymentInternal deployerResult;
                try {
                    deployerResult = Deployer.DeployRecover(
                        deploymentId,
                        entry.Value.StatementIdFirstStatement,
                        entry.Value.Compiled,
                        statementNameResolver,
                        userObjectResolver,
                        substitutionParameterResolver,
                        null,
                        this);
                }
                catch (EPDeployException ex) {
                    throw new EPException(ex.Message, ex);
                }

                foreach (var eventType in deployerResult.DeploymentTypes.Values) {
                    if (eventType.Metadata.BusModifier == EventTypeBusModifier.BUS ||
                        eventType.Metadata.TypeClass == EventTypeTypeClass.NAMED_WINDOW ||
                        eventType.Metadata.TypeClass == EventTypeTypeClass.STREAM) {
                        protectedVisibleTypes.Add(eventType);
                    }
                }
                
                // handle staged deployments
                var stageUri = services.StageRecoveryService.DeploymentGetStage(deploymentId);
                if (stageUri != null) {
                    stageService.RecoverDeployment(stageUri, deployerResult);
                }
            }

            // Event Handler Recovery
            var eventHandlers = services.ListenerRecoveryService.Listeners;
            while (eventHandlers.MoveNext()) {
                var deployment = eventHandlers.Current;
                var epStatement = services.StatementLifecycleService.GetStatementById(deployment.Key);
                epStatement.RecoveryUpdateEventHandlers(new EPStatementListenerSet(deployment.Value));
            }

            // Filter service init
            ISet<EventType> filterServiceTypes = new LinkedHashSet<EventType>(services.EventTypeRepositoryBus.AllTypes);
            filterServiceTypes.AddAll(protectedVisibleTypes);
            Supplier<ICollection<EventType>> availableTypes = () => filterServiceTypes;
            services.FilterServiceSPI.Init(availableTypes);

            // Schedule service init
            services.SchedulingServiceSPI.Init();
            
            // Stage services init
            stageService.RecoveredStageInitialize(availableTypes);

            // Start clocking
            if (_configLastProvided.Runtime.Threading.IsInternalTimerEnabled) {
                services.TimerService.StartInternalClock();
            }

            // Load and initialize adapter loader classes
            LoadAdapters(services);

            // Initialize extension services
            if (services.RuntimeExtensionServices != null) {
                ((RuntimeExtensionServicesSPI) services.RuntimeExtensionServices).Init(services, eventService, deploymentService, stageService);
            }

            // Start metrics reporting, if any
            if (_configLastProvided.Runtime.MetricsReporting.IsEnableMetricsReporting) {
                services.MetricReportingService.SetContext(services.FilterService, services.SchedulingService, eventService);
            }

            // Start runtimes metrics report
            if (_configLastProvided.Runtime.MetricsReporting.IsRuntimeMetrics) {
                StartEngineMetrics(services, eventService);
            }

            // call initialize listeners
            foreach (var listener in _serviceListeners) {
                try {
                    listener.OnEPRuntimeInitialized(this);
                }
                catch (Exception ex) {
                    Log.Error("Runtime exception caught during an onEPRuntimeInitialized callback:" + ex.Message, ex);
                }
            }
        }
Пример #4
0
 public ClassLoader GetClassLoader()
 {
     return TransientConfigurationResolver
         .ResolveClassLoader(_classLoaderProvider, _transientConfiguration)
         .GetClassLoader();
 }
Пример #5
0
 public ClassLoader GetFastClassClassLoader(Type clazz)
 {
     return TransientConfigurationResolver.ResolveFastClassClassLoaderProvider(_transientConfiguration)
         .Classloader(clazz);
 }
Пример #6
0
 public ClassForNameProvider GetClassForNameProvider()
 {
     return TransientConfigurationResolver.ResolveClassForNameProvider(_transientConfiguration);
 }
Пример #7
0
 public ClassLoader GetClassLoader()
 {
     return(TransientConfigurationResolver.ResolveClassLoader(_transientConfiguration).Classloader());
 }