Esempio n. 1
0
        public EPServicesContext CreateServicesContext(EPServiceProvider epServiceProvider, ConfigurationInformation configSnapshot)
        {
            // Directory for binding resources
            var resourceDirectory = new SimpleServiceDirectory();

            EventTypeIdGenerator eventTypeIdGenerator;

            if (configSnapshot.EngineDefaults.AlternativeContextConfig == null || configSnapshot.EngineDefaults.AlternativeContextConfig.EventTypeIdGeneratorFactory == null)
            {
                eventTypeIdGenerator = new EventTypeIdGeneratorImpl();
            }
            else
            {
                var eventTypeIdGeneratorFactory = TypeHelper.Instantiate <EventTypeIdGeneratorFactory>(
                    configSnapshot.EngineDefaults.AlternativeContextConfig.EventTypeIdGeneratorFactory);
                eventTypeIdGenerator = eventTypeIdGeneratorFactory.Create(new EventTypeIdGeneratorContext(epServiceProvider.URI));
            }

            // Make services that depend on snapshot config entries
            var eventAdapterService = new EventAdapterServiceImpl(eventTypeIdGenerator, configSnapshot.EngineDefaults.EventMetaConfig.AnonymousCacheSize);

            Init(eventAdapterService, configSnapshot);

            // New read-write lock for concurrent event processing
            var eventProcessingRwLock = ReaderWriterLockManager.CreateLock(
                MethodBase.GetCurrentMethod().DeclaringType);

            var timeSourceService     = MakeTimeSource(configSnapshot);
            var schedulingService     = SchedulingServiceProvider.NewService(timeSourceService);
            var schedulingMgmtService = new SchedulingMgmtServiceImpl();
            var engineImportService   = MakeEngineImportService(configSnapshot);
            var engineSettingsService = new EngineSettingsService(configSnapshot.EngineDefaults, configSnapshot.PlugInEventTypeResolutionURIs);
            var databaseConfigService = MakeDatabaseRefService(configSnapshot, schedulingService, schedulingMgmtService);

            var plugInViews = new PluggableObjectCollection();

            plugInViews.AddViews(configSnapshot.PlugInViews, configSnapshot.PlugInVirtualDataWindows);
            var plugInPatternObj = new PluggableObjectCollection();

            plugInPatternObj.AddPatternObjects(configSnapshot.PlugInPatternObjects);

            // exception handling
            ExceptionHandlingService exceptionHandlingService = InitExceptionHandling(
                epServiceProvider.URI,
                configSnapshot.EngineDefaults.ExceptionHandlingConfig,
                configSnapshot.EngineDefaults.ConditionHandlingConfig);

            // Statement context factory
            Type systemVirtualDWViewFactory = null;

            if (configSnapshot.EngineDefaults.AlternativeContextConfig.VirtualDataWindowViewFactory != null)
            {
                try {
                    systemVirtualDWViewFactory = TypeHelper.ResolveType(configSnapshot.EngineDefaults.AlternativeContextConfig.VirtualDataWindowViewFactory);
                    if (!systemVirtualDWViewFactory.IsImplementsInterface(typeof(VirtualDataWindowFactory)))
                    {
                        throw new ConfigurationException("Class " + systemVirtualDWViewFactory.Name + " does not implement the interface " + typeof(VirtualDataWindowFactory).FullName);
                    }
                }
                catch (TypeLoadException e) {
                    throw new ConfigurationException("Failed to look up class " + systemVirtualDWViewFactory);
                }
            }
            StatementContextFactory statementContextFactory = new StatementContextFactoryDefault(plugInViews, plugInPatternObj, systemVirtualDWViewFactory);

            long msecTimerResolution = configSnapshot.EngineDefaults.ThreadingConfig.InternalTimerMsecResolution;

            if (msecTimerResolution <= 0)
            {
                throw new ConfigurationException("Timer resolution configuration not set to a valid value, expecting a non-zero value");
            }
            var timerService = new TimerServiceImpl(epServiceProvider.URI, msecTimerResolution);

            var variableService = new VariableServiceImpl(configSnapshot.EngineDefaults.VariablesConfig.MsecVersionRelease, schedulingService, eventAdapterService, null);

            InitVariables(variableService, configSnapshot.Variables, engineImportService);

            var tableService = new TableServiceImpl();

            var statementLockFactory = new StatementLockFactoryImpl(configSnapshot.EngineDefaults.ExecutionConfig.IsFairlock, configSnapshot.EngineDefaults.ExecutionConfig.IsDisableLocking);
            var streamFactoryService = StreamFactoryServiceProvider.NewService(
                epServiceProvider.URI,
                configSnapshot.EngineDefaults.ViewResourcesConfig.IsShareViews);

            var filterService = FilterServiceProvider.NewService(
                configSnapshot.EngineDefaults.ExecutionConfig.FilterServiceProfile,
                configSnapshot.EngineDefaults.ExecutionConfig.IsAllowIsolatedService);

            var metricsReporting   = new MetricReportingServiceImpl(configSnapshot.EngineDefaults.MetricsReportingConfig, epServiceProvider.URI);
            var namedWindowService = new NamedWindowServiceImpl(
                schedulingService,
                variableService,
                tableService,
                engineSettingsService.EngineSettings.ExecutionConfig.IsPrioritized,
                eventProcessingRwLock,
                exceptionHandlingService,
                configSnapshot.EngineDefaults.LoggingConfig.IsEnableQueryPlan,
                metricsReporting);

            var valueAddEventService = new ValueAddEventServiceImpl();

            valueAddEventService.Init(configSnapshot.RevisionEventTypes, configSnapshot.VariantStreams, eventAdapterService, eventTypeIdGenerator);

            var statementEventTypeRef = new StatementEventTypeRefImpl();
            var statementVariableRef  = new StatementVariableRefImpl(variableService, tableService);

            var threadingService = new ThreadingServiceImpl(
                configSnapshot.EngineDefaults.ThreadingConfig);

            var internalEventRouterImpl = new InternalEventRouterImpl();

            var statementIsolationService = new StatementIsolationServiceImpl();

            DeploymentStateService deploymentStateService = new DeploymentStateServiceImpl();

            StatementMetadataFactory stmtMetadataFactory;

            if (configSnapshot.EngineDefaults.AlternativeContextConfig.StatementMetadataFactory == null)
            {
                stmtMetadataFactory = new StatementMetadataFactoryDefault();
            }
            else
            {
                stmtMetadataFactory = TypeHelper.Instantiate <StatementMetadataFactory>(configSnapshot.EngineDefaults.AlternativeContextConfig.StatementMetadataFactory);
            }

            ContextManagementService contextManagementService = new ContextManagementServiceImpl();

            SchedulableAgentInstanceDirectory schedulableAgentInstanceDirectory = null;     // not required for Non-HA.

            PatternSubexpressionPoolEngineSvc patternSubexpressionPoolSvc = null;

            if (configSnapshot.EngineDefaults.PatternsConfig.MaxSubexpressions != null)
            {
                patternSubexpressionPoolSvc = new PatternSubexpressionPoolEngineSvc(
                    configSnapshot.EngineDefaults.PatternsConfig.MaxSubexpressions.GetValueOrDefault(),
                    configSnapshot.EngineDefaults.PatternsConfig.IsMaxSubexpressionPreventStart);
            }

            MatchRecognizeStatePoolEngineSvc matchRecognizeStatePoolEngineSvc = null;

            if (configSnapshot.EngineDefaults.MatchRecognizeConfig.MaxStates != null)
            {
                matchRecognizeStatePoolEngineSvc = new MatchRecognizeStatePoolEngineSvc(
                    configSnapshot.EngineDefaults.MatchRecognizeConfig.MaxStates.Value,
                    configSnapshot.EngineDefaults.MatchRecognizeConfig.IsMaxStatesPreventStart);
            }

            var scriptingService = new ScriptingServiceImpl();

            scriptingService.DiscoverEngines();

            // New services context
            EPServicesContext services = new EPServicesContext(
                epServiceProvider.URI, schedulingService,
                eventAdapterService, engineImportService, engineSettingsService, databaseConfigService, plugInViews,
                statementLockFactory, eventProcessingRwLock, null, resourceDirectory, statementContextFactory,
                plugInPatternObj, timerService, filterService, streamFactoryService,
                namedWindowService, variableService, tableService, timeSourceService, valueAddEventService, metricsReporting, statementEventTypeRef,
                statementVariableRef, configSnapshot, threadingService, internalEventRouterImpl, statementIsolationService, schedulingMgmtService,
                deploymentStateService, exceptionHandlingService, new PatternNodeFactoryImpl(), eventTypeIdGenerator, stmtMetadataFactory,
                contextManagementService, schedulableAgentInstanceDirectory, patternSubexpressionPoolSvc, matchRecognizeStatePoolEngineSvc,
                new DataFlowServiceImpl(epServiceProvider, new DataFlowConfigurationStateServiceImpl()),
                new ExprDeclaredServiceImpl(),
                new ContextControllerFactoryFactorySvcImpl(),
                new ContextManagerFactoryServiceImpl(),
                new EPStatementFactoryDefault(),
                new RegexHandlerFactoryDefault(),
                new ViewableActivatorFactoryDefault(),
                scriptingService
                );

            // Engine services subset available to statements
            statementContextFactory.StmtEngineServices = services;

            // Circular dependency
            var statementLifecycleSvc = new StatementLifecycleSvcImpl(epServiceProvider, services);

            services.StatementLifecycleSvc = statementLifecycleSvc;

            // Observers to statement events
            statementLifecycleSvc.LifecycleEvent += (s, theEvent) => metricsReporting.Observe(theEvent);

            // Circular dependency
            statementIsolationService.ServicesContext = services;

            return(services);
        }
        public EPServicesContext CreateServicesContext(
            IContainer container,
            EPServiceProvider epServiceProvider,
            ConfigurationInformation configSnapshot)
        {
            var lockManager        = container.LockManager();
            var rwLockManager      = container.RWLockManager();
            var threadLocalManager = container.ThreadLocalManager();
            var resourceManager    = container.ResourceManager();

            // Directory for binding resources
            var resourceDirectory = container.Resolve <Directory>();

            // Engine import service
            var engineImportService = MakeEngineImportService(
                container.Resolve <ClassLoaderProvider>(),
                configSnapshot,
                AggregationFactoryFactoryDefault.INSTANCE,
                epServiceProvider.URI);

            // Event Type Id Generation
            EventTypeIdGenerator eventTypeIdGenerator;

            if (configSnapshot.EngineDefaults.AlternativeContext == null ||
                configSnapshot.EngineDefaults.AlternativeContext.EventTypeIdGeneratorFactory == null)
            {
                eventTypeIdGenerator = new EventTypeIdGeneratorImpl();
            }
            else
            {
                var eventTypeIdGeneratorFactory = TypeHelper.Instantiate <EventTypeIdGeneratorFactory>(
                    configSnapshot.EngineDefaults.AlternativeContext.EventTypeIdGeneratorFactory,
                    engineImportService.GetClassForNameProvider());
                eventTypeIdGenerator = eventTypeIdGeneratorFactory.Create(
                    new EventTypeIdGeneratorContext(epServiceProvider.URI));
            }

            // Make services that depend on snapshot config entries
            EventAdapterAvroHandler avroHandler = EventAdapterAvroHandlerUnsupported.INSTANCE;

            if (configSnapshot.EngineDefaults.EventMeta.AvroSettings.IsEnableAvro)
            {
                try
                {
                    avroHandler = TypeHelper.Instantiate <EventAdapterAvroHandler>(
                        EventAdapterAvroHandlerConstants.HANDLER_IMPL, engineImportService.GetClassForNameProvider());
                }
                catch (Exception e)
                {
                    Log.Debug(
                        "Avro provider {0} not instantiated, not enabling Avro support: {1}",
                        EventAdapterAvroHandlerConstants.HANDLER_IMPL, e.Message);
                }
                try
                {
                    avroHandler.Init(configSnapshot.EngineDefaults.EventMeta.AvroSettings, engineImportService);
                }
                catch (Exception e)
                {
                    throw new ConfigurationException("Failed to initialize Esper-Avro: " + e.Message, e);
                }
            }

            var eventAdapterService = new EventAdapterServiceImpl(
                container,
                eventTypeIdGenerator,
                configSnapshot.EngineDefaults.EventMeta.AnonymousCacheSize,
                avroHandler,
                engineImportService);

            Init(eventAdapterService, configSnapshot, engineImportService, resourceManager);

            // New read-write lock for concurrent event processing
            var eventProcessingRwLock = rwLockManager.CreateLock(GetType());

            var timeSourceService     = MakeTimeSource(configSnapshot);
            var schedulingService     = SchedulingServiceProvider.NewService(timeSourceService, lockManager);
            var schedulingMgmtService = new SchedulingMgmtServiceImpl();
            var engineSettingsService = new EngineSettingsService(
                configSnapshot.EngineDefaults, configSnapshot.PlugInEventTypeResolutionURIs);
            var databaseConfigService = MakeDatabaseRefService(
                configSnapshot, schedulingService, schedulingMgmtService, engineImportService);

            var plugInViews = new PluggableObjectCollection();

            plugInViews.AddViews(
                configSnapshot.PlugInViews, configSnapshot.PlugInVirtualDataWindows, engineImportService);
            var plugInPatternObj = new PluggableObjectCollection();

            plugInPatternObj.AddPatternObjects(configSnapshot.PlugInPatternObjects, engineImportService);

            // exception handling
            var exceptionHandlingService = InitExceptionHandling(
                epServiceProvider.URI, configSnapshot.EngineDefaults.ExceptionHandling,
                configSnapshot.EngineDefaults.ConditionHandling, engineImportService);

            // Statement context factory
            Type systemVirtualDWViewFactory = null;

            if (configSnapshot.EngineDefaults.AlternativeContext.VirtualDataWindowViewFactory != null)
            {
                try
                {
                    systemVirtualDWViewFactory =
                        engineImportService.GetClassForNameProvider()
                        .ClassForName(configSnapshot.EngineDefaults.AlternativeContext.VirtualDataWindowViewFactory);
                    if (!TypeHelper.IsImplementsInterface(systemVirtualDWViewFactory, typeof(VirtualDataWindowFactory)))
                    {
                        throw new ConfigurationException(
                                  "Type " + systemVirtualDWViewFactory.Name + " does not implement the interface " +
                                  typeof(VirtualDataWindowFactory).Name);
                    }
                }
                catch (TypeLoadException)
                {
                    throw new ConfigurationException("Failed to look up class " + systemVirtualDWViewFactory);
                }
            }
            var statementContextFactory = new StatementContextFactoryDefault(
                plugInViews, plugInPatternObj, systemVirtualDWViewFactory);

            var msecTimerResolution = configSnapshot.EngineDefaults.Threading.InternalTimerMsecResolution;

            if (msecTimerResolution <= 0)
            {
                throw new ConfigurationException(
                          "Timer resolution configuration not set to a valid value, expecting a non-zero value");
            }
            var timerService = new TimerServiceImpl(epServiceProvider.URI, msecTimerResolution);

            var variableService = new VariableServiceImpl(
                container,
                configSnapshot.EngineDefaults.Variables.MsecVersionRelease,
                schedulingService,
                eventAdapterService,
                null);

            InitVariables(variableService, configSnapshot.Variables, engineImportService);

            var tableService = new TableServiceImpl(container);

            var statementLockFactory = new StatementLockFactoryImpl(
                rwLockManager,
                configSnapshot.EngineDefaults.Execution.IsFairlock,
                configSnapshot.EngineDefaults.Execution.IsDisableLocking
                );

            var streamFactoryService = StreamFactoryServiceProvider.NewService(
                epServiceProvider.URI, configSnapshot.EngineDefaults.ViewResources.IsShareViews);
            var filterService = FilterServiceProvider.NewService(
                lockManager,
                rwLockManager,
                configSnapshot.EngineDefaults.Execution.FilterServiceProfile,
                configSnapshot.EngineDefaults.Execution.IsAllowIsolatedService);
            var metricsReporting = new MetricReportingServiceImpl(
                configSnapshot.EngineDefaults.MetricsReporting,
                epServiceProvider.URI,
                rwLockManager);
            var namedWindowMgmtService = new NamedWindowMgmtServiceImpl(
                configSnapshot.EngineDefaults.Logging.IsEnableQueryPlan,
                metricsReporting);
            var namedWindowDispatchService = new NamedWindowDispatchServiceImpl(
                schedulingService, variableService, tableService,
                engineSettingsService.EngineSettings.Execution.IsPrioritized, eventProcessingRwLock,
                exceptionHandlingService, metricsReporting, threadLocalManager);

            var valueAddEventService = new ValueAddEventServiceImpl(lockManager);

            valueAddEventService.Init(
                configSnapshot.RevisionEventTypes, configSnapshot.VariantStreams, eventAdapterService,
                eventTypeIdGenerator);

            var statementEventTypeRef = new StatementEventTypeRefImpl(rwLockManager);
            var statementVariableRef  = new StatementVariableRefImpl(
                variableService, tableService, namedWindowMgmtService, rwLockManager);

            var threadingService = new ThreadingServiceImpl(configSnapshot.EngineDefaults.Threading);

            var internalEventRouterImpl = new InternalEventRouterImpl(epServiceProvider.URI);

            var statementIsolationService = new StatementIsolationServiceImpl();

            var deploymentStateService = new DeploymentStateServiceImpl(lockManager);

            StatementMetadataFactory stmtMetadataFactory;

            if (configSnapshot.EngineDefaults.AlternativeContext.StatementMetadataFactory == null)
            {
                stmtMetadataFactory = new StatementMetadataFactoryDefault();
            }
            else
            {
                stmtMetadataFactory = TypeHelper.Instantiate <StatementMetadataFactory>(
                    configSnapshot.EngineDefaults.AlternativeContext.StatementMetadataFactory,
                    engineImportService.GetClassForNameProvider());
            }

            var contextManagementService = new ContextManagementServiceImpl();

            PatternSubexpressionPoolEngineSvc patternSubexpressionPoolSvc = null;

            if (configSnapshot.EngineDefaults.Patterns.MaxSubexpressions != null)
            {
                patternSubexpressionPoolSvc =
                    new PatternSubexpressionPoolEngineSvc(
                        configSnapshot.EngineDefaults.Patterns.MaxSubexpressions.Value,
                        configSnapshot.EngineDefaults.Patterns.IsMaxSubexpressionPreventStart);
            }

            MatchRecognizeStatePoolEngineSvc matchRecognizeStatePoolEngineSvc = null;

            if (configSnapshot.EngineDefaults.MatchRecognize.MaxStates != null)
            {
                matchRecognizeStatePoolEngineSvc =
                    new MatchRecognizeStatePoolEngineSvc(
                        configSnapshot.EngineDefaults.MatchRecognize.MaxStates.Value,
                        configSnapshot.EngineDefaults.MatchRecognize.IsMaxStatesPreventStart);
            }

            var scriptingService = new ScriptingServiceImpl();

            scriptingService.DiscoverEngines();

            // New services context

            var services = new EPServicesContext(
                container, epServiceProvider.URI,
                schedulingService,
                eventAdapterService,
                engineImportService,
                engineSettingsService,
                databaseConfigService,
                plugInViews,
                statementLockFactory,
                eventProcessingRwLock,
                null,
                resourceDirectory,
                statementContextFactory,
                plugInPatternObj,
                timerService,
                filterService,
                streamFactoryService,
                namedWindowMgmtService,
                namedWindowDispatchService,
                variableService,
                tableService,
                timeSourceService,
                valueAddEventService,
                metricsReporting,
                statementEventTypeRef,
                statementVariableRef,
                configSnapshot,
                threadingService,
                internalEventRouterImpl,
                statementIsolationService,
                schedulingMgmtService,
                deploymentStateService,
                exceptionHandlingService,
                new PatternNodeFactoryImpl(),
                eventTypeIdGenerator,
                stmtMetadataFactory,
                contextManagementService,
                patternSubexpressionPoolSvc,
                matchRecognizeStatePoolEngineSvc,
                new DataFlowServiceImpl(epServiceProvider, new DataFlowConfigurationStateServiceImpl(), lockManager),
                new ExprDeclaredServiceImpl(lockManager),
                new ContextControllerFactoryFactorySvcImpl(),
                new ContextManagerFactoryServiceImpl(),
                new EPStatementFactoryDefault(),
                new RegexHandlerFactoryDefault(container),
                new ViewableActivatorFactoryDefault(),
                new FilterNonPropertyRegisteryServiceImpl(),
                new ResultSetProcessorHelperFactoryImpl(),
                new ViewServicePreviousFactoryImpl(),
                new EventTableIndexServiceImpl(),
                new EPRuntimeIsolatedFactoryImpl(),
                new FilterBooleanExpressionFactoryImpl(),
                new DataCacheFactory(),
                new MultiMatchHandlerFactoryImpl(),
                NamedWindowConsumerMgmtServiceImpl.INSTANCE,
                AggregationFactoryFactoryDefault.INSTANCE,
                scriptingService
                );

            // Engine services subset available to statements
            statementContextFactory.StmtEngineServices = services;

            // Circular dependency
            var statementLifecycleSvc = new StatementLifecycleSvcImpl(epServiceProvider, services);

            services.StatementLifecycleSvc = statementLifecycleSvc;

            // Observers to statement events
            statementLifecycleSvc.LifecycleEvent += (s, theEvent) => metricsReporting.Observe(theEvent);

            // Circular dependency
            statementIsolationService.SetEpServicesContext(services);

            return(services);
        }