Пример #1
0
 public ResolveWeaklyConflictResolver(IStoreSnapshots snapstore, IStoreEvents eventstore, IDelayedChannel delay, StreamIdGenerator streamGen)
 {
     _snapstore  = snapstore;
     _eventstore = eventstore;
     _delay      = delay;
     _streamGen  = streamGen;
 }
Пример #2
0
 public ResolveWeaklyConflictResolver(IStoreStreams eventstore, IDelayedChannel delay, StreamIdGenerator streamGen, int maxPulledDelayed)
 {
     _store            = eventstore;
     _delay            = delay;
     _streamGen        = streamGen;
     _maxPulledDelayed = maxPulledDelayed;
 }
Пример #3
0
        private async Task InvokeDelayedChannel(IDelayedChannel channel, string channelKey, string specificKey, DelayedAttribute attr, MessageHandler handler, IInvokeHandlerContext context)
        {
            var msgs = await channel.Pull(channelKey, key : specificKey, max : attr.Count).ConfigureAwait(false);

            if (!msgs.Any())
            {
                Logger.Write(LogLevel.Debug, () => $"No delayed events found on channel [{channelKey}] specific key [{specificKey}]");
                return;
            }

            var idx   = 0;
            var count = msgs.Count();

            Invokes.Mark();
            InvokeSize.Update(msgs.Count());
            Logger.Write(LogLevel.Info, () => $"Starting invoke handle {count} times channel key [{channelKey}] specific key [{specificKey}]");
            using (var ctx = InvokeTime.NewContext())
            {
                foreach (var msg in msgs.Cast <DelayedMessage>())
                {
                    idx++;
                    Logger.Write(LogLevel.Debug,
                                 () => $"Invoking handle {idx}/{count} times channel key [{channelKey}] specific key [{specificKey}]");
                    await handler.Invoke(msg.Message, context).ConfigureAwait(false);
                }
                if (ctx.Elapsed > TimeSpan.FromSeconds(5))
                {
                    SlowLogger.Write(LogLevel.Warn, () => $"Bulk invoking {count} times on channel key [{channelKey}] specific key [{specificKey}] took {ctx.Elapsed.TotalSeconds} seconds!");
                }
                Logger.Write(LogLevel.Info, () => $"Bulk invoking {count} times on channel key [{channelKey}] specific key [{specificKey}] took {ctx.Elapsed.TotalMilliseconds} ms!");
            }
            Logger.Write(LogLevel.Info, () => $"Finished invoke handle {count} times channel key [{channelKey}] specific key [{specificKey}]");
        }
Пример #4
0
        private async Task InvokeDelayedChannel(IDelayedChannel channel, string channelKey, string specificKey, DelayedAttribute attr, MessageHandler handler, IInvokeHandlerContext context)
        {
            var msgs = await channel.Pull(channelKey, key : specificKey, max : attr.Count).ConfigureAwait(false);

            var messages = msgs as IDelayedMessage[] ?? msgs.ToArray();

            if (!messages.Any())
            {
                Logger.Write(LogLevel.Debug, () => $"No delayed events found on channel [{channelKey}] specific key [{specificKey}]");
                return;
            }

            var count = messages.Length;

            Logger.Write(LogLevel.Debug, () => $"Starting invoke handle {count} times channel key [{channelKey}] specific key [{specificKey}]");
            using (var ctx = _metrics.Begin("Bulk Messages Time"))
            {
                foreach (var idx in Enumerable.Range(0, messages.Length))
                {
                    Logger.Write(LogLevel.Debug,
                                 () => $"Invoking handle {idx}/{count} times channel key [{channelKey}] specific key [{specificKey}]");
                    await handler.Invoke(messages[idx].Message, context).ConfigureAwait(false);
                }

                if (ctx.Elapsed > TimeSpan.FromSeconds(5))
                {
                    SlowLogger.Write(LogLevel.Warn, () => $"Bulk invoking {count} times on channel key [{channelKey}] specific key [{specificKey}] took {ctx.Elapsed.TotalSeconds} seconds!");
                }
                Logger.Write(LogLevel.Info, () => $"Bulk invoking {count} times on channel key [{channelKey}] specific key [{specificKey}] took {ctx.Elapsed.TotalMilliseconds} ms!");
            }
        }
Пример #5
0
        private async Task InvokeDelayedChannel(IDelayedChannel channel, string channelKey, string specificKey, DelayedAttribute attr, MessageHandler handler, IInvokeHandlerContext context)
        {
            var msgs = await channel.Pull(channelKey, key : specificKey, max : attr.Count).ConfigureAwait(false);

            var messages = msgs as IDelayedMessage[] ?? msgs.ToArray();

            var count = messages.Length;

            using (var ctx = _metrics.Begin("Bulk Messages Time"))
            {
                switch (attr.Mode)
                {
                case DeliveryMode.Single:
                    foreach (var idx in Enumerable.Range(0, messages.Length))
                    {
                        await handler.Invoke(messages[idx].Message, context).ConfigureAwait(false);
                    }
                    break;

                case DeliveryMode.First:
                    await handler.Invoke(messages[0].Message, context).ConfigureAwait(false);

                    break;

                case DeliveryMode.Last:
                    await handler.Invoke(messages[messages.Length - 1].Message, context).ConfigureAwait(false);

                    break;

                case DeliveryMode.FirstAndLast:
                    await handler.Invoke(messages[0].Message, context).ConfigureAwait(false);

                    await handler.Invoke(messages[messages.Length - 1].Message, context).ConfigureAwait(false);

                    break;
                }

                if (ctx.Elapsed > TimeSpan.FromSeconds(5))
                {
                    SlowLogger.InfoEvent("Invoked", "{Count} messages channel [{Channel:l}] key [{Key:l}] took {Milliseconds} ms", count, channelKey, specificKey, ctx.Elapsed.TotalMilliseconds);
                }
                Logger.DebugEvent("Invoked", "{Count} messages channel [{Channel:l}] key [{Key:l}] took {Milliseconds} ms", count, channelKey, specificKey, ctx.Elapsed.TotalMilliseconds);
            }
        }
Пример #6
0
 public ResolveWeaklyConflictResolver(IStoreStreams eventstore, IDelayedChannel delay)
 {
     _store = eventstore;
     _delay = delay;
 }
        protected override async Task Terminate(IInvokeHandlerContext context)
        {
            IDelayedChannel channel = null;

            try
            {
                channel = context.Builder.Build <IDelayedChannel>();
            }
            // Catch in case IDelayedChannel isn't registered which shouldn't happen unless a user registered Consumer without GetEventStore
            catch (Exception) { }

            var msgType = context.MessageBeingHandled.GetType();

            if (!msgType.IsInterface)
            {
                msgType = _mapper.GetMappedTypeFor(msgType) ?? msgType;
            }

            context.Extensions.Set(new State
            {
                ScopeWasPresent = Transaction.Current != null
            });

            var messageHandler = context.MessageHandler;
            var channelKey     = $"{messageHandler.HandlerType.FullName}:{msgType.FullName}";

            bool contains = false;

            lock (Lock) contains = IsNotDelayed.Contains(channelKey);

            var contextChannelKey = "";

            if (context.Headers.ContainsKey(Defaults.ChannelKey))
            {
                contextChannelKey = context.Headers[Defaults.ChannelKey];
            }

            // Special case for when we are bulk processing messages from DelayedSubscriber, simply process it and return dont check for more bulk
            if (channel == null || contains || contextChannelKey == channelKey)
            {
                Logger.Write(LogLevel.Debug, () => $"Invoking handle for message {msgType.FullName} on handler {messageHandler.HandlerType.FullName}");
                await messageHandler.Invoke(context.MessageBeingHandled, context).ConfigureAwait(false);

                return;
            }
            // If we are bulk processing and the above check fails it means the current message handler shouldn't be called
            if (!string.IsNullOrEmpty(contextChannelKey))
            {
                return;
            }

            if (IsDelayed.ContainsKey(channelKey))
            {
                DelayedAttribute delayed;
                IsDelayed.TryGetValue(channelKey, out delayed);

                var msgPkg = new DelayedMessage
                {
                    MessageId  = context.MessageId,
                    Headers    = context.Headers,
                    Message    = context.MessageBeingHandled,
                    Received   = DateTime.UtcNow,
                    ChannelKey = channelKey
                };

                InvokesDelayed.Mark();

                var specificKey = "";
                if (delayed.KeyPropertyFunc != null)
                {
                    try
                    {
                        specificKey = delayed.KeyPropertyFunc(context.MessageBeingHandled);
                    }
                    catch
                    {
                        Logger.Warn($"Failed to get key properties from message {msgType.FullName}");
                    }
                }

                await channel.AddToQueue(channelKey, msgPkg, key : specificKey).ConfigureAwait(false);

                bool bulkInvoked;
                if (context.Extensions.TryGet <bool>("BulkInvoked", out bulkInvoked) && bulkInvoked)
                {
                    // Prevents a single message from triggering a dozen different bulk invokes
                    Logger.Write(LogLevel.Debug, () => $"Limiting bulk processing for a single message to a single invoke");
                    return;
                }



                int?     size = null;
                TimeSpan?age  = null;

                if (delayed.Delay.HasValue)
                {
                    age = await channel.Age(channelKey, key : specificKey).ConfigureAwait(false);
                }
                if (delayed.Count.HasValue)
                {
                    size = await channel.Size(channelKey, key : specificKey).ConfigureAwait(false);
                }


                if (!ShouldExecute(delayed, size, age))
                {
                    Logger.Write(LogLevel.Debug, () => $"Threshold Count [{delayed.Count}] DelayMs [{delayed.Delay}] Size [{size}] Age [{age?.TotalMilliseconds}] - delaying processing channel [{channelKey}] specific [{specificKey}]");

                    return;
                }

                lock (RecentLock)
                {
                    if (RecentlyInvoked.ContainsKey($"{channelKey}.{specificKey}"))
                    {
                        Logger.Write(LogLevel.Debug, () => $"Channel [{channelKey}] specific [{specificKey}] was checked by this instance recently - leaving it alone");
                        return;
                    }
                    RecentlyInvoked.Add($"{channelKey}.{specificKey}", DateTime.UtcNow.AddSeconds(10));
                }
                context.Extensions.Set <bool>("BulkInvoked", true);

                Logger.Write(LogLevel.Info, () => $"Threshold Count [{delayed.Count}] DelayMs [{delayed.Delay}] Size [{size}] Age [{age?.TotalMilliseconds}] - bulk processing channel [{channelKey}] specific [{specificKey}]");

                await InvokeDelayedChannel(channel, channelKey, specificKey, delayed, messageHandler, context).ConfigureAwait(false);

                return;
            }

            var attrs =
                Attribute.GetCustomAttributes(messageHandler.HandlerType, typeof(DelayedAttribute))
                .Cast <DelayedAttribute>();
            var single = attrs.SingleOrDefault(x => x.Type == msgType);

            if (single == null)
            {
                lock (Lock) IsNotDelayed.Add(channelKey);
            }
            else
            {
                Logger.Write(LogLevel.Debug,
                             () => $"Found delayed handler {messageHandler.HandlerType.FullName} for message {msgType.FullName}");
                IsDelayed.TryAdd(channelKey, single);
            }
            await Terminate(context).ConfigureAwait(false);
        }
Пример #8
0
        protected override async Task Terminate(IInvokeHandlerContext context)
        {
            if (context.Extensions.TryGet(out ActiveSagaInstance saga) && saga.NotFound && ((SagaMetadata)saga.GetType().GetProperty("Metadata", BindingFlags.NonPublic).GetValue(saga)).SagaType == context.MessageHandler.Instance.GetType())
            {
                return;
            }

            IDelayedChannel channel = null;

            try
            {
                IContainer container;
                if (context.Extensions.TryGet <IContainer>(out container))
                {
                    channel = container.Resolve <IDelayedChannel>();
                }
            }
            catch (Exception)
            {
                // Catch in case IDelayedChannel isn't registered which shouldn't happen unless a user registered Consumer without GetEventStore
            }

            var msgType = context.MessageBeingHandled.GetType();

            if (!msgType.IsInterface)
            {
                msgType = _mapper.GetMappedTypeFor(msgType) ?? msgType;
            }

            context.Extensions.Set(new State
            {
                ScopeWasPresent = Transaction.Current != null
            });

            var messageHandler = context.MessageHandler;
            var channelKey     = $"{messageHandler.HandlerType.FullName}:{msgType.FullName}";

            bool contains = false;

            lock (Lock) contains = IsNotDelayed.Contains(channelKey);

            context.Extensions.TryGet(Defaults.ChannelKey, out string contextChannelKey);

            // Special case for when we are bulk processing messages from DelayedSubscriber or BulkMessage, simply process it and return dont check for more bulk
            if (channel == null || contains || contextChannelKey == channelKey || context.Headers.ContainsKey(Defaults.BulkHeader))
            {
                await messageHandler.Invoke(context.MessageBeingHandled, context).ConfigureAwait(false);

                return;
            }
            // If we are bulk processing and the above check fails it means the current message handler shouldn't be called
            // because if contextChannelKey is set we are using delayed bulk SendLocal
            // which should only execute the message on the handlers that were marked delayed, the other handlers were already invoked when the message
            // originally came in
            if (!string.IsNullOrEmpty(contextChannelKey))
            {
                return;
            }


            if (IsDelayed.ContainsKey(channelKey))
            {
                DelayedAttribute delayed;
                IsDelayed.TryGetValue(channelKey, out delayed);


                var specificKey = "";
                if (delayed.KeyPropertyFunc != null)
                {
                    try
                    {
                        specificKey = delayed.KeyPropertyFunc(context.MessageBeingHandled);
                    }
                    catch
                    {
                        Logger.Warn($"Failed to get key properties from message {msgType.FullName}");
                    }
                }

                var msgPkg = new DelayedMessage
                {
                    MessageId   = context.MessageId,
                    Headers     = context.Headers,
                    Message     = context.MessageBeingHandled as IMessage,
                    Received    = DateTime.UtcNow,
                    ChannelKey  = channelKey,
                    SpecificKey = specificKey
                };

                await channel.AddToQueue(channelKey, msgPkg, key : specificKey).ConfigureAwait(false);

                Logger.DebugEvent("QueueAdd", "Message added to delayed queue [{Channel:l}] key [{Key:l}]", channelKey, specificKey);

                bool bulkInvoked;
                if (context.Extensions.TryGet <bool>("BulkInvoked", out bulkInvoked) && bulkInvoked)
                {
                    // Prevents a single message from triggering a dozen different bulk invokes
                    return;
                }

                int?     size = null;
                TimeSpan?age  = null;

                if (delayed.Delay.HasValue)
                {
                    age = await channel.Age(channelKey, key : specificKey).ConfigureAwait(false);
                }
                if (delayed.Count.HasValue)
                {
                    size = await channel.Size(channelKey, key : specificKey).ConfigureAwait(false);
                }

                if (!ShouldExecute(delayed, size, age))
                {
                    return;
                }

                context.Extensions.Set <bool>("BulkInvoked", true);

                Logger.DebugEvent("Threshold", "Count [{Count}] DelayMs [{DelayMs}] bulk procesing Size [{Size}] Age [{Age}] - [{Channel:l}] key [{Key:l}]", delayed.Count, delayed.Delay, size, age?.TotalMilliseconds, channelKey, specificKey);

                await InvokeDelayedChannel(channel, channelKey, specificKey, delayed, messageHandler, context).ConfigureAwait(false);

                return;
            }

            var attrs =
                Attribute.GetCustomAttributes(messageHandler.HandlerType, typeof(DelayedAttribute))
                .Cast <DelayedAttribute>();
            var single = attrs.SingleOrDefault(x => x.Type == msgType);

            if (single == null)
            {
                lock (Lock) IsNotDelayed.Add(channelKey);
            }
            else
            {
                IsDelayed.TryAdd(channelKey, single);
            }

            await Terminate(context).ConfigureAwait(false);
        }