Beispiel #1
0
        public async Task <ISet <PubSubSubscriptionState> > RegisterProducer(StreamId streamId, IStreamProducerExtension streamProducer)
        {
            counterProducersAdded.Increment();
            if (!IsActiveProducer(streamProducer))
            {
                throw new ArgumentException($"Trying to register non active IStreamProducerExtension: {streamProducer}", "streamProducer");
            }

            try
            {
                int producersRemoved = RemoveDeadProducers();

                var publisherState = new PubSubPublisherState(streamId, streamProducer);
                State.Producers.Add(publisherState);
                LogPubSubCounts("RegisterProducer {0}", streamProducer);
                await WriteStateAsync();

                counterProducersTotal.DecrementBy(producersRemoved);
                counterProducersTotal.Increment();
            }
            catch (Exception exc)
            {
                logger.Error(ErrorCode.Stream_RegisterProducerFailed, $"Failed to register a stream producer.  Stream: {streamId}, Producer: {streamProducer}", exc);
                // Corrupted state, deactivate grain.
                DeactivateOnIdle();
                throw;
            }
            return(State.Consumers.Where(c => !c.IsFaulted).ToSet());
        }
Beispiel #2
0
 private int RemoveDeadProducers()
 {
     // Remove only those we know for sure are Dead.
     int numRemoved = 0;
     if (State.Producers != null && State.Producers.Count > 0)
         numRemoved = State.Producers.RemoveWhere(producerState => IsDeadProducer(producerState.Producer));
     
     if (numRemoved > 0)
     {
         LogPubSubCounts("RemoveDeadProducers: removed {0} outdated producers", numRemoved);
         counterProducersRemoved.Increment();
         counterProducersTotal.DecrementBy(numRemoved);
     }
     return numRemoved;
 }
        public async Task UnregisterProducer(StreamId streamId, IStreamProducerExtension streamProducer)
        {
            counterProducersRemoved.Increment();
            try
            {
                int numRemoved = State.Producers.RemoveWhere(s => s.Equals(streamId, streamProducer));
                LogPubSubCounts("UnregisterProducer {0} NumRemoved={1}", streamProducer, numRemoved);

                if (numRemoved > 0)
                {
                    Task updateStorageTask = State.Producers.Count == 0 && State.Consumers.Count == 0
                        ? ClearStateAsync() //State contains no producers or consumers, remove it from storage
                        : WriteStateAsync();
                    await updateStorageTask;
                }
                counterProducersTotal.DecrementBy(numRemoved);
            }
            catch (Exception exc)
            {
                logger.Error(ErrorCode.Stream_UnegisterProducerFailed,
                             $"Failed to unregister a stream producer.  Stream: {streamId}, Producer: {streamProducer}", exc);
                // Corrupted state, deactivate grain.
                DeactivateOnIdle();
                throw;
            }
            if (State.Producers.Count == 0 && State.Consumers.Count == 0)
            {
                DeactivateOnIdle(); // No producers or consumers left now, so flag ourselves to expedite Deactivation
            }
        }
Beispiel #4
0
        public async Task UnregisterConsumer(StreamId streamId, IStreamConsumerExtension streamConsumer)
        {
            int numRemoved = State.Consumers.RemoveWhere(c => c.Equals(streamId, streamConsumer));
            counterConsumersRemoved.Increment();
            counterConsumersTotal.DecrementBy(numRemoved);

            LogPubSubCounts("UnregisterConsumer {0} NumRemoved={1}", streamConsumer, numRemoved);
            await State.WriteStateAsync();

            int numProducers = State.Producers.Count;
            if (numProducers > 0)
            {
                if (logger.IsVerbose) logger.Verbose("Notifying {0} existing producers about unregistered consumer.", numProducers);
                
                // Notify producers about unregistered consumer.
                var tasks = new List<Task>();
                foreach (var producerState in State.Producers
                    .Where(producerState => IsActiveProducer(producerState.Producer)))
                        tasks.Add(producerState.Producer.RemoveSubscriber(streamId, streamConsumer));
                
                await Task.WhenAll(tasks);
            }
            else if (State.Consumers.Count == 0) // + we already know that numProducers == 0 from previous if-clause
            {
                // No producers or consumers left now, so flag ourselves to expedite Deactivation
                DeactivateOnIdle();
            }
        }
 public void Counter_DecrementBy()
 {
     StatisticName name = new StatisticName(CounterName);
     int startValue = 10;
     int newValue = startValue - 1;
     CounterStatistic ctr = CounterStatistic.FindOrCreate(name);
     ctr.IncrementBy(startValue);
     Assert.Equal(startValue, ctr.GetCurrentValue());
     ctr.DecrementBy(1);
     Assert.Equal(newValue, ctr.GetCurrentValue());
 }
Beispiel #6
0
        public async Task UnregisterConsumer(GuidId subscriptionId, StreamId streamId)
        {
            if (State.Consumers.Any(c => c.IsFaulted && c.Equals(subscriptionId)))
            {
                throw new FaultedSubscriptionException(subscriptionId, streamId);
            }

            int numRemoved = State.Consumers.RemoveWhere(c => c.Equals(subscriptionId));

            counterConsumersRemoved.Increment();
            counterConsumersTotal.DecrementBy(numRemoved);

            LogPubSubCounts("UnregisterSubscription {0} NumRemoved={1}", subscriptionId, numRemoved);

            await WriteStateAsync();
            await NotifyProducersOfRemovedSubscription(subscriptionId, streamId);
            await ClearStateWhenEmpty();
        }
        public async Task RegisterConsumer(
            GuidId subscriptionId,
            StreamId streamId,
            IStreamConsumerExtension streamConsumer,
            IStreamFilterPredicateWrapper filter)
        {
            counterConsumersAdded.Increment();
            PubSubSubscriptionState pubSubState = State.Consumers.FirstOrDefault(s => s.Equals(subscriptionId));

            if (pubSubState != null && pubSubState.IsFaulted)
            {
                throw new FaultedSubscriptionException(subscriptionId, streamId);
            }
            try
            {
                if (pubSubState == null)
                {
                    pubSubState = new PubSubSubscriptionState(subscriptionId, streamId, streamConsumer);
                    State.Consumers.Add(pubSubState);
                }

                if (filter != null)
                {
                    pubSubState.AddFilter(filter);
                }

                LogPubSubCounts("RegisterConsumer {0}", streamConsumer);
                await WriteStateAsync();

                counterConsumersTotal.Increment();
            }
            catch (Exception exc)
            {
                logger.Error(ErrorCode.Stream_RegisterConsumerFailed,
                             $"Failed to register a stream consumer.  Stream: {streamId}, SubscriptionId {subscriptionId}, Consumer: {streamConsumer}", exc);
                // Corrupted state, deactivate grain.
                DeactivateOnIdle();
                throw;
            }

            int numProducers = State.Producers.Count;

            if (numProducers <= 0)
            {
                return;
            }

            if (logger.IsEnabled(LogLevel.Debug))
            {
                logger.Debug("Notifying {0} existing producer(s) about new consumer {1}. Producers={2}",
                             numProducers, streamConsumer, Utils.EnumerableToString(State.Producers));
            }

            // Notify producers about a new streamConsumer.
            var tasks                = new List <Task>();
            var producers            = State.Producers.ToList();
            int initialProducerCount = producers.Count;

            try
            {
                foreach (PubSubPublisherState producerState in producers)
                {
                    tasks.Add(ExecuteProducerTask(producerState, producerState.Producer.AddSubscriber(subscriptionId, streamId, streamConsumer, filter)));
                }

                Exception exception = null;
                try
                {
                    await Task.WhenAll(tasks);
                }
                catch (Exception exc)
                {
                    exception = exc;
                }

                // if the number of producers has been changed, resave state.
                if (State.Producers.Count != initialProducerCount)
                {
                    await WriteStateAsync();

                    counterConsumersTotal.DecrementBy(initialProducerCount - State.Producers.Count);
                }

                if (exception != null)
                {
                    throw exception;
                }
            }
            catch (Exception exc)
            {
                logger.Error(ErrorCode.Stream_RegisterConsumerFailed,
                             $"Failed to update producers while register a stream consumer.  Stream: {streamId}, SubscriptionId {subscriptionId}, Consumer: {streamConsumer}", exc);
                // Corrupted state, deactivate grain.
                DeactivateOnIdle();
                throw;
            }
        }
 internal static void OnTransactionCommitted()
 {
     CommitTransactionQueueLength.DecrementBy(1);
     CommitTransactionCompleted.Increment();
 }
 internal static void OnTransactionStarted()
 {
     StartTransactionQueueLength.DecrementBy(1);
     StartTransactionCompleted.Increment();
 }