Ejemplo n.º 1
0
            private async Task FinilizeQueueWork(int NumOfDeqeuedMsgs, string qName, IReliableQueue <Wi> q)
            {
                long LastCheckedVal;

                if (null == q)
                {
                    // this executer tried to get q and failed.
                    // typically this happens when # of executers > q
                    // check for decrease workers
                    this.workManager.m_DeferedTaskExec.AddWork(this.workManager.TryDecreaseExecuters);
                    return;
                }

                bool bMoreMessages = NumOfDeqeuedMsgs > this.workManager.YieldQueueAfter;
                bool bEmptyQ       = true;

                using (ITransaction tx = this.workManager.StateManager.CreateTransaction())
                {
                    bEmptyQ = !(await q.GetCountAsync(tx) > 0);
                }

                if (bMoreMessages || !bEmptyQ) // did we find messages in the queue
                {
                    this.workManager.m_TraceWriter.TraceMessage(string.Format("queue:{0} pushed back to queues, queue still have more work", qName));

                    this.leaveQ(qName, true);
                }
                else
                {
                    long now = DateTime.UtcNow.Ticks;
                    // was queue previously empty?
                    bool bCheckedBefore = this.workManager.m_QueueManager.m_SuspectedEmptyQueues.TryGetValue(qName, out LastCheckedVal);

                    // Q was in suspected empty queue and has expired
                    if (bCheckedBefore && ((now - LastCheckedVal) > this.workManager.m_RemoveEmptyQueueAfterTicks))
                    {
                        this.workManager.m_TraceWriter.TraceMessage(string.Format("queue:{0} confirmed empty, and will be removed", qName));

                        // remove it from suspected queue
                        this.workManager.m_QueueManager.m_SuspectedEmptyQueues.TryRemove(qName, out LastCheckedVal);

                        // remove from the queue list
                        await this.workManager.m_QueueManager.RemoveQueueAsync(qName);

                        // remove asscioated handler
                        this.workManager.m_DeferedTaskExec.AddWork(() => this.workManager.RemoveHandlerForQueue(qName));

                        // modify executers to reflect the current state
                        this.workManager.m_DeferedTaskExec.AddWork(this.workManager.TryDecreaseExecuters);
                    }
                    else
                    {
                        this.workManager.m_TraceWriter.TraceMessage(
                            string.Format("queue:{0} pushed back to queues and flagged as an empty queue suspect ", qName));
                        // the queue was not a suspect before, or has not expired
                        this.workManager.m_QueueManager.m_SuspectedEmptyQueues.AddOrUpdate(qName, now, (k, v) => { return(v); });
                        this.leaveQ(qName, false);
                    }
                }
            }
        protected override async Task <int> GetMessageCountAsync(IReliableState reliableState, ServiceFabricTransaction transaction)
        {
            IReliableQueue <byte[]> collection = (IReliableQueue <byte[]>)reliableState;

            long count = await collection.GetCountAsync(transaction.Tx).ConfigureAwait(false);

            return((int)count);
        }
        /// <summary>
        /// See interface
        /// </summary>
        public async Task <int> GetMessageCountAsync()
        {
            using (var tx = new ServiceFabricTransaction(_stateManager, null))
            {
                IReliableQueue <byte[]> collection = await GetCollectionAsync();

                long count = await collection.GetCountAsync(tx.Tx);

                return((int)count);
            }
        }
        public async Task <IActionResult> GetQueueLengthAsync()
        {
            IReliableQueue <DeviceEventSeries> queue =
                await this.stateManager.GetOrAddAsync <IReliableQueue <DeviceEventSeries> >(DataService.EventQueueName);

            using (ITransaction tx = this.stateManager.CreateTransaction())
            {
                long count = await queue.GetCountAsync(tx);

                return(this.Ok(count));
            }
        }
Ejemplo n.º 5
0
        private async Task LoadNumOfBufferedItems()
        {
            long buffered = 0;


            foreach (string qName in this.m_QueueManager.QueueNames)
            {
                IReliableQueue <Wi> q = await this.m_QueueManager.GetOrAddQueueAsync(qName);

                buffered += await q.GetCountAsync();
            }
            this.m_NumOfBufferedWorkItems = buffered;
        }
        private async Task LoadNumOfBufferedItems()
        {
            long buffered = 0;

            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                foreach (string qName in this.m_QueueManager.QueueNames)
                {
                    IReliableQueue <Wi> q = await this.m_QueueManager.GetOrAddQueueAsync(qName);

                    buffered += await q.GetCountAsync(tx);
                }
            }
            this.m_NumOfBufferedWorkItems = buffered;
        }
        public async Task AddMessageAsync(string roomName, ChatMessage chatMessage, CancellationToken cancellationToken)
        {
            IReliableQueue <ChatMessage> room = await GetRoomQueue(roomName);

            using (var tx = stateManager.CreateTransaction())
            {
                long msgCount = await room.GetCountAsync(tx);

                if (msgCount == 0)
                {
                    // new room, increment counter
                    await IncrementRoomCountAsync(1, cancellationToken, tx);
                }

                await room.EnqueueAsync(tx, chatMessage);

                await tx.CommitAsync();
            }
        }
Ejemplo n.º 8
0
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            ServiceEventSource.Current.RunAsyncInvoked(ServiceEventSourceName);


            IReliableQueue <string> inputQueue = await this.StateManager.GetOrAddAsync <IReliableQueue <string> >("inputQueue");

            IReliableDictionary <string, long> wordCountDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >("wordCountDictionary");

            IReliableDictionary <string, long> statsDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >("statsDictionary");


            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    using (ITransaction tx = this.StateManager.CreateTransaction())
                    {
                        ConditionalValue <string> dequeuReply = await inputQueue.TryDequeueAsync(tx);

                        if (dequeuReply.HasValue)
                        {
                            string word = dequeuReply.Value;

                            long count = await wordCountDictionary.AddOrUpdateAsync(
                                tx,
                                word, /* key */
                                1,    /* value to add if key is absent */
                                (key, oldValue) => oldValue + 1);

                            long numberOfProcessedWords = await statsDictionary.AddOrUpdateAsync(
                                tx,
                                "Number of Words Processed",
                                1,
                                (key, oldValue) => oldValue + 1);

                            long queueLength = await inputQueue.GetCountAsync(tx);

                            await tx.CommitAsync();

                            ServiceEventSource.Current.RunAsyncStatus(
                                this.Partition.PartitionInfo.Id,
                                numberOfProcessedWords,
                                queueLength,
                                word,
                                count);
                        }
                    }

                    await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
                }
                catch (TimeoutException)
                {
                    //Service Fabric uses timeouts on collection operations to prevent deadlocks.
                    //If this exception is thrown, it means that this transaction was waiting the default
                    //amount of time (4 seconds) but was unable to acquire the lock. In this case we simply
                    //retry after a random backoff interval. You can also control the timeout via a parameter
                    //on the collection operation.
                    Thread.Sleep(TimeSpan.FromSeconds(new Random().Next(100, 300)));

                    continue;
                }
                catch (Exception exception)
                {
                    //For sample code only: simply trace the exception.
                    ServiceEventSource.Current.MessageEvent(exception.ToString());
                }
            }
        }
Ejemplo n.º 9
0
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            IReliableQueue <DeviceEventSeries> queue = await this.StateManager.GetOrAddAsync <IReliableQueue <DeviceEventSeries> >(EventQueueName);

            int iteration = 0;

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    using (ITransaction tx = this.StateManager.CreateTransaction())
                    {
                        // When the number of items in the queue reaches a certain size..
                        int count = (int)await queue.GetCountAsync(tx);

                        ServiceEventSource.Current.ServiceMessage(
                            this.Context,
                            "Current queue size: {0} in iteration {1}",
                            count, iteration);

                        // if the queue size reaches the batch size, start draining the queue
                        // always drain the queue every nth iteration so that nothing sits in the queue indefinitely
                        if (count >= OffloadBatchSize || iteration == DrainIteration)
                        {
                            ServiceEventSource.Current.ServiceMessage(
                                this.Context,
                                "Starting batch offload..");

                            // Dequeue the items into a batch
                            List <DeviceEventSeries> batch = new List <DeviceEventSeries>(count);

                            for (int i = 0; i < count; ++i)
                            {
                                cancellationToken.ThrowIfCancellationRequested();

                                ConditionalValue <DeviceEventSeries> result = await queue.TryDequeueAsync(tx);

                                if (result.HasValue)
                                {
                                    batch.Add(result.Value);
                                }
                            }

                            //TODO: Process the data or send to a storage location.

                            // Commit the dequeue operations
                            await tx.CommitAsync();

                            ServiceEventSource.Current.ServiceMessage(
                                this.Context,
                                "Batch offloaded {0} events.",
                                count);

                            // skip the delay and move on to the next batch.
                            iteration = 0;
                            continue;
                        }
                        else if (count > 0)
                        {
                            iteration++;
                        }
                    }
                }
                catch (TimeoutException)
                {
                    // transient error. Retry.
                    ServiceEventSource.Current.ServiceMessage(this.Context, "TimeoutException in RunAsync.");
                }
                catch (FabricTransientException fte)
                {
                    // transient error. Retry.
                    ServiceEventSource.Current.ServiceMessage(this.Context, "FabricTransientException in RunAsync: {0}", fte.Message);
                }
                catch (FabricNotPrimaryException)
                {
                    // not primary any more, time to quit.
                    return;
                }

                await Task.Delay(this.OffloadBatchInterval, cancellationToken);
            }
        }
 private async Task <int> GetCountDiff(ITransaction tx)
 {
     return((int)await queue.GetCountAsync(tx).ConfigureAwait(false) - signal.CurrentCount);
 }
 public Task <long> GetCountAsync(ITransaction tx)
 {
     return(_queue.GetCountAsync(tx));
 }