예제 #1
0
        public IProjectionGetResult <T> Get <T>(IBlobId projectionId) where T : IProjectionDefinition
        {
            if (ReferenceEquals(null, projectionId))
            {
                throw new ArgumentNullException(nameof(projectionId));
            }

            Type   projectionType = typeof(T);
            string contractId     = projectionType.GetContractId();

            try
            {
                ProjectionVersion liveVersion = GetProjectionVersions(contractId).GetLive();
                if (ReferenceEquals(null, liveVersion))
                {
                    log.Warn(() => $"Unable to find `live` version for projection with contract id {contractId} and name {projectionType.Name}");
                    return(new ProjectionGetResult <T>(default(T)));
                }

                ISnapshot                snapshot    = snapshotStore.Load(contractId, projectionId, liveVersion);
                ProjectionStream         stream      = LoadProjectionStream(projectionType, liveVersion, projectionId, snapshot);
                IProjectionGetResult <T> queryResult = stream.RestoreFromHistory <T>();

                return(queryResult);
            }
            catch (Exception ex)
            {
                log.ErrorException($"Unable to load projection. ProjectionId:{projectionId} ProjectionContractId:{contractId} ProjectionName:{projectionType.Name}", ex);
                return(new ProjectionGetResult <T>(default(T)));
            }
        }
예제 #2
0
        ProjectionStream LoadProjectionStream(Type projectionType, ProjectionVersion projectionVersion, IBlobId projectionId, ISnapshot snapshot)
        {
            if (ReferenceEquals(null, projectionVersion))
            {
                throw new ArgumentNullException(nameof(projectionVersion));
            }
            if (ReferenceEquals(null, projectionId))
            {
                throw new ArgumentNullException(nameof(projectionId));
            }
            if (ReferenceEquals(null, snapshot))
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            ISnapshot currentSnapshot = snapshot;
            string    contractId      = projectionVersion.ProjectionName;

            List <ProjectionCommit> projectionCommits = new List <ProjectionCommit>();
            int snapshotMarker = snapshot.Revision;

            while (true)
            {
                snapshotMarker++;
                var loadedCommits = projectionStore.Load(projectionVersion, projectionId, snapshotMarker).ToList();
                projectionCommits.AddRange(loadedCommits);
                bool isSnapshotable = typeof(IAmNotSnapshotable).IsAssignableFrom(projectionType) == false;

                if (isSnapshotable && snapshotStrategy.ShouldCreateSnapshot(projectionCommits, snapshot.Revision))
                {
                    ProjectionStream checkpointStream = new ProjectionStream(projectionId, projectionCommits, currentSnapshot);
                    var       projectionState         = checkpointStream.RestoreFromHistory(projectionType).Projection.State;
                    ISnapshot newSnapshot             = new Snapshot(projectionId, contractId, projectionState, snapshot.Revision + 1);
                    snapshotStore.Save(newSnapshot, projectionVersion);
                    currentSnapshot = newSnapshot;

                    projectionCommits.Clear();

                    log.Debug(() => $"Snapshot created for projection `{contractId}` with id={projectionId} where ({loadedCommits.Count}) were zipped. Snapshot: `{snapshot.GetType().Name}`");
                }
                else if (loadedCommits.Count() < snapshotStrategy.EventsInSnapshot)
                {
                    break;
                }
                else
                {
                    log.Warn($"Potential memory leak. The system will be down fairly soon. The projection `{contractId}` with id={projectionId} loads a lot of projection commits ({loadedCommits.Count}) and snapshot `{snapshot.GetType().Name}` which puts a lot of CPU and RAM pressure. You can resolve this by configuring the snapshot settings`.");
                }
            }

            ProjectionStream stream = new ProjectionStream(projectionId, projectionCommits, currentSnapshot);

            return(stream);
        }
예제 #3
0
        IProjectionGetResult <PersistentProjectionVersionHandler> GetProjectionVersionsFromStore(string contractId)
        {
            var versionId                   = new ProjectionVersionManagerId(contractId);
            var persistentVersionType       = typeof(PersistentProjectionVersionHandler);
            var persistentVersionContractId = persistentVersionType.GetContractId();
            var persistentVersion           = new ProjectionVersion(persistentVersionContractId, ProjectionStatus.Live, 1, persistentVersionType.GetProjectionHash());
            ProjectionStream stream         = LoadProjectionStream(persistentVersionType, persistentVersion, versionId, new NoSnapshot(versionId, contractId));
            var queryResult                 = stream.RestoreFromHistory <PersistentProjectionVersionHandler>();

            return(queryResult);
        }
예제 #4
0
        public IProjectionGetResult <IProjectionDefinition> Get(IBlobId projectionId, Type projectionType)
        {
            if (ReferenceEquals(null, projectionId))
            {
                throw new ArgumentNullException(nameof(projectionId));
            }

            ProjectionStream stream = LoadProjectionStream(projectionType, projectionId);

            return(stream.RestoreFromHistory(projectionType));
        }
예제 #5
0
        public IProjectionGetResult <T> Get <T>(IBlobId projectionId) where T : IProjectionDefinition
        {
            if (ReferenceEquals(null, projectionId))
            {
                throw new ArgumentNullException(nameof(projectionId));
            }

            Type projectionType = typeof(T);

            ProjectionStream stream = LoadProjectionStream(projectionType, projectionId);

            return(stream.RestoreFromHistory <T>());
        }
예제 #6
0
        ProjectionStream LoadProjectionStream(Type projectionType, ProjectionVersion version, IBlobId projectionId, SnapshotMeta snapshotMeta)
        {
            Func <ISnapshot> loadSnapshot = () => snapshotStore.Load(version.ProjectionName, projectionId, version);

            List <ProjectionCommit> projectionCommits = new List <ProjectionCommit>();
            int snapshotMarker = snapshotMeta.Revision;

            while (true)
            {
                snapshotMarker++;
                var loadedCommits = projectionStore.Load(version, projectionId, snapshotMarker).ToList();
                projectionCommits.AddRange(loadedCommits);

                if (projectionType.IsSnapshotable())
                {
                    if (snapshotStrategy.ShouldCreateSnapshot(projectionCommits, snapshotMeta.Revision))
                    {
                        ProjectionStream checkpointStream = new ProjectionStream(projectionId, projectionCommits, loadSnapshot);
                        var       projectionState         = checkpointStream.RestoreFromHistory(projectionType).State;
                        ISnapshot newSnapshot             = new Snapshot(projectionId, version.ProjectionName, projectionState, snapshotMeta.Revision + 1);
                        snapshotStore.Save(newSnapshot, version);
                        loadSnapshot = () => newSnapshot;

                        projectionCommits.Clear();

                        log.Debug(() => $"Snapshot created for projection `{version.ProjectionName}` with id={projectionId} where ({loadedCommits.Count}) were zipped. Snapshot: `{newSnapshot.GetType().Name}`");
                    }
                }
                else
                {
                    loadSnapshot = () => new NoSnapshot(projectionId, version.ProjectionName);
                }

                if (loadedCommits.Count < snapshotStrategy.EventsInSnapshot)
                {
                    break;
                }

                if (loadedCommits.Count > snapshotStrategy.EventsInSnapshot * 1.5)
                {
                    log.Warn(() => $"Potential memory leak. The system will be down fairly soon. The projection `{version.ProjectionName}` with id={projectionId} loads a lot of projection commits ({loadedCommits.Count}) which puts a lot of CPU and RAM pressure. You can resolve this by configuring the snapshot settings`.");
                }
            }

            ProjectionStream stream = new ProjectionStream(projectionId, projectionCommits, loadSnapshot);

            return(stream);
        }
예제 #7
0
        public ReadResult <IProjectionDefinition> Get(IBlobId projectionId, Type projectionType)
        {
            if (ReferenceEquals(null, projectionId))
            {
                throw new ArgumentNullException(nameof(projectionId));
            }

            try
            {
                ProjectionStream stream = LoadProjectionStream(projectionType, projectionId);
                return(new ReadResult <IProjectionDefinition>(stream.RestoreFromHistory(projectionType)));
            }
            catch (Exception ex)
            {
                log.ErrorException($"Unable to load projection. {projectionType.Name}({projectionId})", ex);
                return(ReadResult <IProjectionDefinition> .WithError(ex));
            }
        }
예제 #8
0
        ReadResult <ProjectionVersionsHandler> GetProjectionVersionsFromStore(string projectionName)
        {
            try
            {
                var persistentVersionType             = typeof(ProjectionVersionsHandler);
                var projectionVersions_ProjectionName = persistentVersionType.GetContractId();

                var versionId           = new ProjectionVersionManagerId(projectionName, context.Tenant);
                var persistentVersion   = new ProjectionVersion(projectionVersions_ProjectionName, ProjectionStatus.Live, 1, projectionHasher.CalculateHash(persistentVersionType));
                ProjectionStream stream = LoadProjectionStream(persistentVersionType, persistentVersion, versionId, new NoSnapshot(versionId, projectionVersions_ProjectionName).GetMeta());
                var queryResult         = stream.RestoreFromHistory <ProjectionVersionsHandler>();

                return(new ReadResult <ProjectionVersionsHandler>(queryResult));
            }
            catch (Exception ex)
            {
                return(ReadResult <ProjectionVersionsHandler> .WithNotFoundHint(ex.Message));
            }
        }
예제 #9
0
        public ReadResult <T> Get <T>(IBlobId projectionId) where T : IProjectionDefinition
        {
            try
            {
                if (ReferenceEquals(null, projectionId))
                {
                    throw new ArgumentNullException(nameof(projectionId));
                }

                Type projectionType = typeof(T);

                ProjectionStream stream = LoadProjectionStream(projectionType, projectionId);
                return(new ReadResult <T>(stream.RestoreFromHistory <T>()));
            }
            catch (Exception ex)
            {
                log.ErrorException($"Unable to load projection. {typeof(T).Name}({projectionId})", ex);
                return(ReadResult <T> .WithError(ex));
            }
        }
        public async Task <ReadResult <T> > GetAsync <T>(IBlobId projectionId) where T : IProjectionDefinition
        {
            if (ReferenceEquals(null, projectionId))
            {
                throw new ArgumentNullException(nameof(projectionId));
            }

            try
            {
                Type projectionType = typeof(T);

                ProjectionStream stream = await LoadProjectionStreamAsync(projectionType, projectionId);

                return(new ReadResult <T>(stream.RestoreFromHistory <T>()));
            }
            catch (Exception ex)
            {
                log.ErrorException(ex, () => $"Unable to load projection. {typeof(T).Name}({projectionId})");
                return(ReadResult <T> .WithError(ex));
            }
        }