For backpressure detection we maintain a histogram of 10 buckets. Every buckets records how many items are in the cache in that bucket and how many cursors are pointing to an item in that bucket. In the TimedCache the bucket also takes note what is the newest and oldest members timespan because buckets also has a maximum timespan they can hold (this is done for better bucket distribution) We update the NumCurrentItems, NewestMemberTimespan and OldestMemberTimespan (when it's a new bucket) when we add and remove cache item (potentially opening or removing a bucket) We update NumCurrentCursors every time we move a cursor If the first (most outdated bucket) has at least one cursor pointing to it, we say we are under back pressure (in a full cache).
Пример #1
0
        private TimedQueueCacheBucket GetOrCreateBucket()
        {
            TimedQueueCacheBucket cacheBucket;

            if (_cacheCursorHistogram.Count == 0)
            {
                Log(_logger, "TimedQueueCache for QueueId:{0}, Add: No buckets, creating the first bucket", Id.ToString());
                cacheBucket = new TimedQueueCacheBucket();
                _cacheCursorHistogram.Add(cacheBucket);
            }
            else
            {
                cacheBucket = _cacheCursorHistogram.Last(); // last one
            }

            // if last bucket is full or containing all the TimeSpan, open a new one
            if (cacheBucket.NumCurrentItems == _cacheHistogramMaxBucketSize ||
                (cacheBucket.NewestMemberTimestamp - cacheBucket.OldestMemberTimestamp) > _bucketTimeSpan)
            {
                Log(_logger, "TimedQueueCache for QueueId:{0}, Add: Last bucket exceeded size ", Id.ToString());
                cacheBucket = new TimedQueueCacheBucket();
                _cacheCursorHistogram.Add(cacheBucket);
            }

            return(cacheBucket);
        }
        private TimedQueueCacheBucket GetOrCreateBucket()
        {
            TimedQueueCacheBucket cacheBucket;
            if (_cacheCursorHistogram.Count == 0)
            {
                Log(_logger, "TimedQueueCache for QueueId:{0}, Add: No buckets, creating the first bucket", Id.ToString());
                cacheBucket = new TimedQueueCacheBucket();
                _cacheCursorHistogram.Add(cacheBucket);
            }
            else
            {
                cacheBucket = _cacheCursorHistogram.Last(); // last one
            }

            // if last bucket is full or containing all the TimeSpan, open a new one
            if (cacheBucket.NumCurrentItems == _cacheHistogramMaxBucketSize ||
                (cacheBucket.NewestMemberTimestamp - cacheBucket.OldestMemberTimestamp) > _bucketTimeSpan)
            {
                Log(_logger, "TimedQueueCache for QueueId:{0}, Add: Last bucket exceeded size ", Id.ToString());
                cacheBucket = new TimedQueueCacheBucket();
                _cacheCursorHistogram.Add(cacheBucket);
            }

            return cacheBucket;
        }