Exemple #1
0
        public EPDeployment Deploy(
            EPCompiled compiled,
            DeploymentOptions options)
        {
            using (_services.Container.EnterContextualReflection()) {
                if (options == null)
                {
                    options = new DeploymentOptions();
                }

                ValidateRuntimeAlive();
                CheckManifest(-1, compiled.Manifest);

                DeploymentInternal deployerResult;

                using (GetDeploymentLock(options)) {
                    var statementIdRecovery = _services.EpServicesHA.StatementIdRecoveryService;
                    var currentStatementId  = statementIdRecovery.CurrentStatementId;
                    if (currentStatementId == null)
                    {
                        currentStatementId = 1;
                    }

                    var deploymentId = DeployerHelperResolver.DetermineDeploymentIdCheckExists(
                        -1,
                        options,
                        _runtime.ServicesContext.DeploymentLifecycleService);
                    deployerResult = Deployer.DeployFresh(
                        deploymentId,
                        currentStatementId.Value,
                        compiled,
                        options.StatementNameRuntime,
                        options.StatementUserObjectRuntime,
                        options.StatementSubstitutionParameter,
                        options.DeploymentClassLoaderOption,
                        _runtime);
                    statementIdRecovery.CurrentStatementId = currentStatementId + deployerResult.Statements.Length;

                    // dispatch event
                    DispatchOnDeploymentEvent(deployerResult, -1);
                }

                return(MakeDeployment(deployerResult));
            }
        }
Exemple #2
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);
                }
            }
        }