Пример #1
0
        private static DeploymentInternal Deploy(
            bool recovery,
            string deploymentId,
            int statementIdFirstStatement,
            EPCompiled compiled,
            StatementNameRuntimeOption statementNameResolverRuntime,
            StatementUserObjectRuntimeOption userObjectResolverRuntime,
            StatementSubstitutionParameterOption substitutionParameterResolver,
            DeploymentClassLoaderOption deploymentClassLoaderOption,
            EPRuntimeSPI epRuntime)
        {
            // set variable local version
            epRuntime.ServicesContext.VariableManagementService.SetLocalVersion();

            try {
                return(DeploySafe(
                           recovery,
                           deploymentId,
                           statementIdFirstStatement,
                           compiled,
                           statementNameResolverRuntime,
                           userObjectResolverRuntime,
                           substitutionParameterResolver,
                           deploymentClassLoaderOption,
                           epRuntime));
            }
            catch (EPDeployException) {
                throw;
            }
            catch (Exception ex) {
                throw new EPDeployException(ex.Message, ex, -1);
            }
        }
Пример #2
0
 private static void CompileDeployWSubstitution(
     RegressionEnvironment env,
     string epl,
     IDictionary<string, object> @params)
 {
     var compiled = env.Compile("@Name('s0') " + epl);
     StatementSubstitutionParameterOption resolver = ctx => {
         foreach (var entry in @params) {
             ctx.SetObject(entry.Key, entry.Value);
         }
     };
     env.Deploy(compiled, new DeploymentOptions().WithStatementSubstitutionParameter(resolver));
     env.AddListener("s0");
 }
Пример #3
0
 private static void DeployWithResolver(
     RegressionEnvironment env,
     EPCompiled compiled,
     string statementName,
     StatementSubstitutionParameterOption resolver)
 {
     var options = new DeploymentOptions().WithStatementSubstitutionParameter(resolver);
     options.StatementNameRuntime = context => statementName;
     try {
         env.Deployment.Deploy(compiled, options);
     }
     catch (EPDeployException e) {
         throw new EPRuntimeException(e);
     }
 }
Пример #4
0
 private static void TryInvalidResolver(
     RegressionEnvironment env,
     EPCompiled compiled,
     string expected,
     StatementSubstitutionParameterOption resolver)
 {
     var options = new DeploymentOptions().WithStatementSubstitutionParameter(resolver);
     try {
         env.Deployment.Deploy(compiled, options);
         Assert.Fail();
     }
     catch (EPDeploySubstitutionParameterException e) {
         AssertMessage(e.Message, expected);
     }
     catch (EPDeployException e) {
         throw new EPRuntimeException(e);
     }
 }
Пример #5
0
 public static DeploymentInternal DeployRecover(
     string deploymentId,
     int statementIdFirstStatement,
     EPCompiled compiled,
     StatementNameRuntimeOption statementNameResolverRuntime,
     StatementUserObjectRuntimeOption userObjectResolverRuntime,
     StatementSubstitutionParameterOption substitutionParameterResolver,
     DeploymentClassLoaderOption deploymentClassLoaderOption,
     EPRuntimeSPI epRuntime)
 {
     return(Deploy(
                true,
                deploymentId,
                statementIdFirstStatement,
                compiled,
                statementNameResolverRuntime,
                userObjectResolverRuntime,
                substitutionParameterResolver,
                deploymentClassLoaderOption,
                epRuntime));
 }
Пример #6
0
 /// <summary>
 ///     Sets the callback providing values for substitution parameters.
 /// </summary>
 /// <param name="statementSubstitutionParameter">callback</param>
 /// <returns>itself</returns>
 public DeploymentOptions WithStatementSubstitutionParameter(
     StatementSubstitutionParameterOption statementSubstitutionParameter)
 {
     StatementSubstitutionParameter = statementSubstitutionParameter;
     return this;
 }
Пример #7
0
        private static DeploymentInternal DeploySafe(
            bool recovery,
            string deploymentId,
            int statementIdFirstStatement,
            EPCompiled compiled,
            StatementNameRuntimeOption statementNameResolverRuntime,
            StatementUserObjectRuntimeOption userObjectResolverRuntime,
            StatementSubstitutionParameterOption substitutionParameterResolver,
            DeploymentClassLoaderOption deploymentClassLoaderOption,
            EPRuntimeSPI epRuntime)
        {
            var services = epRuntime.ServicesContext;
            var deploymentClassLoader = DeployerHelperResolver.GetClassLoader(-1, deploymentClassLoaderOption, services);
            var moduleProvider        = ModuleProviderUtil.Analyze(compiled, deploymentClassLoader, services.ClassProvidedPathRegistry);

            if (moduleProvider.ClassLoader is ClassProvidedImportClassLoader)
            {
                ((ClassProvidedImportClassLoader)moduleProvider.ClassLoader).Imported = moduleProvider.ModuleProvider.ModuleDependencies.PathClasses;
            }

            var moduleName = moduleProvider.ModuleProvider.ModuleName;

            // resolve external dependencies
            var moduleDependencies       = moduleProvider.ModuleProvider.ModuleDependencies;
            var deploymentIdDependencies = ResolveDependencies(-1, moduleDependencies, services);

            // initialize EPL objects defined by module
            var moduleEPLObjects = InitializeEPLObjects(moduleProvider, deploymentId, services);

            // determine staged EPL object overlap
            ValidateStagedEPLObjects(moduleEPLObjects, moduleProvider.ModuleProvider.ModuleName, -1, epRuntime.StageService);

            // add EPL objects defined by module to path
            var modulePaths = UpdatePath(-1, moduleEPLObjects, moduleName, deploymentId, services);

            // obtain statement lightweights
            var stmtLightweights = InitializeStatements(
                -1,
                recovery,
                moduleEPLObjects,
                modulePaths,
                moduleName,
                moduleProvider,
                deploymentId,
                statementIdFirstStatement,
                userObjectResolverRuntime,
                statementNameResolverRuntime,
                substitutionParameterResolver,
                services);

            // start statements depending on context association
            var statements = DeployStatements(
                -1, stmtLightweights.Lightweights, recovery, modulePaths, moduleProvider, deploymentId, epRuntime);

            // add dependencies
            AddPathDependencies(deploymentId, moduleDependencies, services);

            // keep statement and deployment
            var deployed = DeploymentInternal.From(
                deploymentId,
                statements,
                deploymentIdDependencies,
                modulePaths,
                moduleEPLObjects,
                moduleProvider);

            services.DeploymentLifecycleService.AddDeployment(deploymentId, deployed);

            // register for recovery
            if (!recovery)
            {
                var recoveryInformation = GetRecoveryInformation(deployed);
                services.DeploymentRecoveryService.Add(
                    deploymentId,
                    statementIdFirstStatement,
                    compiled,
                    recoveryInformation.StatementUserObjectsRuntime,
                    recoveryInformation.StatementNamesWhenProvidedByAPI,
                    stmtLightweights.SubstitutionParameters);
            }

            return(deployed);
        }
Пример #8
0
        public static DeployerModuleStatementLightweights InitializeStatements(
            int rolloutItemNumber,
            bool recovery,
            DeployerModuleEPLObjects moduleEPLObjects,
            DeployerModulePaths modulePaths,
            string moduleName,
            ModuleProviderCLPair moduleProvider,
            string deploymentId,
            int statementIdFirstStatement,
            StatementUserObjectRuntimeOption userObjectResolverRuntime,
            StatementNameRuntimeOption statementNameResolverRuntime,
            StatementSubstitutionParameterOption substitutionParameterResolver,
            EPServicesContext services)
        {
            // get module statements
            IList <StatementProvider> statementResources;

            try {
                statementResources = moduleProvider.ModuleProvider.Statements;
            }
            catch (Exception e) {
                throw new EPException(e);
            }

            // initialize all statements
            IList <StatementLightweight> lightweights = new List <StatementLightweight>();
            IDictionary <int, IDictionary <int, object> > substitutionParameters;
            ISet <string> statementNames    = new HashSet <string>();
            var           moduleIncidentals = moduleEPLObjects.Incidentals;

            try {
                var statementId = statementIdFirstStatement;
                foreach (var statement in statementResources)
                {
                    var lightweight = InitStatement(
                        recovery,
                        moduleName,
                        statement,
                        deploymentId,
                        statementId,
                        moduleEPLObjects.EventTypeResolver,
                        moduleIncidentals,
                        statementNameResolverRuntime,
                        userObjectResolverRuntime,
                        moduleProvider.ClassLoader,
                        services);
                    lightweights.Add(lightweight);
                    statementId++;

                    var statementName = lightweight.StatementContext.StatementName;
                    if (statementNames.Contains(statementName))
                    {
                        throw new EPDeployException(
                                  "Duplicate statement name provide by statement name resolver for statement name '" + statementName + "'",
                                  rolloutItemNumber);
                    }

                    statementNames.Add(statementName);
                }

                // set parameters
                substitutionParameters = SetSubstitutionParameterValues(rolloutItemNumber, deploymentId, lightweights, substitutionParameterResolver);
            }
            catch (Exception) {
                DeployerHelperResolver.ReverseDeployment(deploymentId, modulePaths.DeploymentTypes, lightweights, new EPStatement[0], moduleProvider, services);
                throw;
            }

            return(new DeployerModuleStatementLightweights(statementIdFirstStatement, lightweights, substitutionParameters));
        }
Пример #9
0
        private static IDictionary <int, IDictionary <int, object> > SetSubstitutionParameterValues(
            int rolloutItemNumber,
            string deploymentId,
            IList <StatementLightweight> lightweights,
            StatementSubstitutionParameterOption substitutionParameterResolver)
        {
            if (substitutionParameterResolver == null)
            {
                foreach (var lightweight in lightweights)
                {
                    var required = lightweight.StatementInformationals.SubstitutionParamTypes;
                    if (required != null && required.Length > 0)
                    {
                        throw new EPDeploySubstitutionParameterException(
                                  "Statement '" + lightweight.StatementContext.StatementName + "' has " + required.Length + " substitution parameters",
                                  rolloutItemNumber);
                    }
                }

                return(EmptyDictionary <int, IDictionary <int, object> > .Instance);
            }

            IDictionary <int, IDictionary <int, object> > providedAllStmt = new Dictionary <int, IDictionary <int, object> >();

            foreach (var lightweight in lightweights)
            {
                var substitutionTypes = lightweight.StatementInformationals.SubstitutionParamTypes;
                IDictionary <string, int> paramNames = lightweight.StatementInformationals.SubstitutionParamNames;
                var handler = new DeployerSubstitutionParameterHandler(
                    deploymentId,
                    lightweight,
                    providedAllStmt,
                    substitutionTypes,
                    paramNames);

                try {
                    substitutionParameterResolver.Invoke(handler);
                }
                catch (Exception ex) {
                    throw new EPDeploySubstitutionParameterException(
                              "Failed to set substitution parameter value for statement '" + lightweight.StatementContext.StatementName + "': " + ex.Message,
                              ex,
                              rolloutItemNumber);
                }

                if (substitutionTypes == null || substitutionTypes.Length == 0)
                {
                    continue;
                }

                // check that all values are provided
                var provided     = providedAllStmt.Get(lightweight.StatementContext.StatementId);
                var providedSize = provided == null ? 0 : provided.Count;
                if (providedSize != substitutionTypes.Length)
                {
                    for (var i = 0; i < substitutionTypes.Length; i++)
                    {
                        if (provided == null || !provided.ContainsKey(i + 1))
                        {
                            var name = Convert.ToString(i + 1);
                            if (paramNames != null && !paramNames.IsEmpty())
                            {
                                foreach (var entry in paramNames)
                                {
                                    if (entry.Value == i + 1)
                                    {
                                        name = "'" + entry.Key + "'";
                                    }
                                }
                            }

                            throw new EPDeploySubstitutionParameterException(
                                      "Missing value for substitution parameter " + name + " for statement '" + lightweight.StatementContext.StatementName + "'",
                                      rolloutItemNumber);
                        }
                    }
                }
            }

            return(providedAllStmt);
        }
Пример #10
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);
                }
            }
        }