Ejemplo n.º 1
0
 public TenantSliceRange(DocumentStore store, IAggregationRuntime <TDoc, TId> runtime, EventRange range,
                         IReadOnlyList <TenantSliceGroup <TDoc, TId> > groups, CancellationToken projectionCancellation) : base(range, projectionCancellation)
 {
     _store   = store;
     _runtime = runtime;
     Groups   = groups;
 }
Ejemplo n.º 2
0
        private async Task processEventSlices(IShardAgent shardAgent, IAggregationRuntime <TDoc, TId> runtime,
                                              IDocumentStore store, CancellationToken token)
        {
            var beingFetched = new List <EventSlice <TDoc, TId> >();

            foreach (var slice in Slices)
            {
                if (token.IsCancellationRequested)
                {
                    _builder.Complete();
                    break;
                }

                if (runtime.IsNew(slice))
                {
                    _builder.Post(slice);
                }
                else
                {
                    beingFetched.Add(slice);
                }
            }

            if (token.IsCancellationRequested)
            {
                return;
            }

            var ids = beingFetched.Select(x => x.Id).ToArray();

            IReadOnlyList <TDoc> aggregates = null;

            await shardAgent.TryAction(async() =>
            {
                using (var session = (IMartenSession)store.LightweightSession(Tenant.TenantId))
                {
                    aggregates = await runtime.Storage
                                 .LoadManyAsync(ids, session, token).ConfigureAwait(false);
                }
            }, token).ConfigureAwait(false);

            if (token.IsCancellationRequested || aggregates == null)
            {
                return;
            }

            var dict = aggregates.ToDictionary(x => runtime.Storage.Identity(x));

            foreach (var slice in Slices)
            {
                if (dict.TryGetValue(slice.Id, out var aggregate))
                {
                    slice.Aggregate = aggregate;
                }

                _builder.Post(slice);
            }
        }
        internal override IReadOnlyList <AsyncProjectionShard> AsyncProjectionShards(DocumentStore store)
        {
            _runtime = BuildRuntime(store);

            var eventTypes = determineEventTypes();

            var baseFilters = Array.Empty <ISqlFragment>();

            if (!eventTypes.Any(x => x.IsAbstract || x.IsInterface))
            {
                baseFilters = new ISqlFragment[] { new EventTypeFilter(store.Events, eventTypes) };
            }

            return(new List <AsyncProjectionShard> {
                new(this, baseFilters)
            });
Ejemplo n.º 4
0
        internal override IReadOnlyList <AsyncProjectionShard> AsyncProjectionShards(DocumentStore store)
        {
            // TODO -- support sharding

            _runtime = BuildRuntime(store);

            var eventTypes = determineEventTypes();

            var baseFilters = new ISqlFragment[0];

            if (!eventTypes.Any(x => x.IsAbstract || x.IsInterface))
            {
                baseFilters = new ISqlFragment[] { new Marten.Events.Daemon.EventTypeFilter(store.Events, eventTypes) };
            }

            return(new List <AsyncProjectionShard> {
                new(this, baseFilters)
            });
Ejemplo n.º 5
0
        internal async Task Start(IShardAgent shardAgent, ProjectionUpdateBatch updateBatch,
                                  IAggregationRuntime <TDoc, TId> runtime,
                                  DocumentStore store, EventRangeGroup parent)
        {
            _session = new ProjectionDocumentSession(store, updateBatch, new SessionOptions {
                Tracking = DocumentTracking.None, Tenant = Tenant
            });

            _builder = new ActionBlock <EventSlice <TDoc, TId> >(async slice =>
            {
                if (parent.Cancellation.IsCancellationRequested)
                {
                    return;
                }

                await shardAgent.TryAction(async() =>
                {
                    await runtime.ApplyChangesAsync(_session, slice, parent.Cancellation, ProjectionLifecycle.Async).ConfigureAwait(false);
                }, parent.Cancellation, @group: parent, logException: (l, e) =>
                {
                    l.LogError(e, "Failure trying to build a storage operation to update {DocumentType} with {Id}", typeof(TDoc).FullNameInCode(), slice.Id);
                }, actionMode: GroupActionMode.Child).ConfigureAwait(false);
            }, new ExecutionDataflowBlockOptions
            {
                CancellationToken = parent.Cancellation,
            });

            await processEventSlices(shardAgent, runtime, store, parent.Cancellation).ConfigureAwait(false);

            var builder = Volatile.Read(ref _builder);

            if (builder != null)
            {
                builder.Complete();
                await builder.Completion.ConfigureAwait(false);
            }
        }