private AggregateStatistics <int> GetAndResetInt() { long sum = 0; int min = int.MaxValue; int max = 0; int count = 0; T val; bool noElements = true; while (queue.TryDequeue(out val)) { var value = (int)(object)val; sum += value; min = Math.Min(min, value); max = Math.Max(max, value); count++; noElements = false; } int average = count == 0 ? 0 : (int)(sum / count); if (noElements) { return(null); } return(new AggregateStatistics <int> { Sum = (int)sum, Min = min, Max = max, Count = count, Average = average }); }
void IEventsProcessor.ProcessEvents(string origin, SessionMode mode, IEnumerable <IEvent> events) { if (origin == _store.Id) { return; } ProcessInfo pi; pi.Events = events; pi.Origin = origin; pi.Mode = mode; _processes.Enqueue(pi); if (!Monitor.TryEnter(_gate, 10)) { return; } try { while (!_processes.IsEmpty) { ProcessInfo info; if (_processes.TryDequeue(out info)) { ProcessEvents(info); } } } finally { Monitor.Exit(_gate); } }
public void ProcessPendingQueues(Action <UnacknowledgedReliableMessageContext> unsentMessageProcessor) { UnacknowledgedReliableMessageContext context; while (pendingQueue.TryDequeue(out context)) { EnqueueMessageForResend(context); } var selectedQueueIndex = resendQueueIndex % resendQueues.Length; var currentQueue = resendQueues[selectedQueueIndex]; while (currentQueue.Count > 0) { var messageContext = currentQueue.Dequeue(); if (messageContext.Acknowledged) { messageContextPool.ReturnObject(messageContext); } else { unsentMessageProcessor(messageContext); EnqueueMessageForResend(messageContext); } } resendQueueIndex++; }
public void Run() { if (Queue.IsEmpty) { return; } IRunnable task = default(IRunnable); if (Queue.TryDequeue(out task)) { while (task != null) { task.Run(); Queue.TryDequeue(out task); } } }
public Parallelize(int numberOfThreads, IConcurrentQueue <ScheduledTask> messages) { _messages = messages; _waitForWork = new List <Wait_for_work <ScheduledTask> >(); for (var i = 0; i < numberOfThreads; i++) { var wfw = new Wait_for_work <ScheduledTask>(_messages, () => { ScheduledTask result; var success = _messages.TryDequeue(out result); return(new Tuple <bool, ScheduledTask>(success, result)); }); wfw.Dequeued += _ => _.ContinueWith(_.Message); _waitForWork.Add(wfw); } }
public Parallelize(int numberOfThreads, IConcurrentQueue<ScheduledTask> messages) { _messages = messages; _waitForWork = new List<Wait_for_work<ScheduledTask>>(); for (var i = 0; i < numberOfThreads; i++) { var wfw = new Wait_for_work<ScheduledTask>(_messages, () => { ScheduledTask result; var success = _messages.TryDequeue(out result); return new Tuple<bool, ScheduledTask>(success, result); }); wfw.Dequeued += _ => _.ContinueWith(_.Message); _waitForWork.Add(wfw); } }
/// <summary> /// Execution du vaccum. /// Le vaccum consiste à purger les tuples inutiles (qui sont liés à des transactions qui ne sont plus valides). Ce /// mécanisme est nécessaire quand on utilise MVCC car les tuples ne sont /// jamais supprimés, seules les transactions changent de status. /// Lors du vaccum, la liste des transactions est aussi purgé ainsi que les indexs. /// </summary> private void Vacuum() { if (_involvedSlots.IsEmpty) { return; } // Abort is store is disposing if (!_disposed && _valuesLock.TryEnterWriteLock(2)) { var sw = Stopwatch.StartNew(); try { if (_disposed) { _statVaccumSkipped.Incr(); // ExitWriteLock in finally return; } // Purge des transactions _transactionManager.Vacuum(); // Select strategy : if vacuum evictions greater than x -> replace values dictionary var vaccumStrategy = _vaccumCounter <= 0 ? VacuumStrategy.ResetContainer : VacuumStrategy.RemoveSlots; while (!_involvedSlots.IsEmpty) { SlotList slots; if (_involvedSlots.TryDequeue(out slots)) { // Evict elements deleted since a while ( = slot marked as deleted by a committed transaction) foreach (var slot in slots) { if (slot.XMax != null) { var tx = _transactionManager.GetTransaction(slot.XMax.Value); // Remove slot if (tx == null) { slot.Id = 0; if (slot is IDisposable) { ((IDisposable)slot).Dispose(); } } } } if (vaccumStrategy == VacuumStrategy.RemoveSlots && slots.Length == 0) { slots.Dispose(); _values.Remove(slots.Id); _vaccumCounter--; } } } if (vaccumStrategy == VacuumStrategy.ResetContainer) { _vaccumCounter = VACUUM_EVICTIONS_THROTTLE; // RESET // Création d'un dictionnaire temporaire dans lequel on va copier les valeurs à conserver. // Comme un dictionnaire ne permet pas de récupérer la place qui n'est plus utilisé, on va ainsi optimiser // la mémoire. // On peut se permettre de le faire ici car le vaccum bloque toutes les transactions. var data = new Dictionary <Identity, SlotList>((int)(_values.Count * 0.5)); // On parcourt toutes les valeurs foreach (var slots in _values) { if (_disposed) { break; } // If slot list is empty, it is not adding to the new dictionary if (slots.Value.Length == 0) { slots.Value.Dispose(); } else { data.Add(slots.Key, new SlotList(slots.Value)); } } _values = data; } } finally { sw.Stop(); _valuesLock.ExitWriteLock(); _statVaccumCount.Incr(); _statVaccumAverage.IncrBy(sw.ElapsedMilliseconds); } } else { _statVaccumSkipped.Incr(); } }