Пример #1
0
        internal void QueueRebalance(RebalancingMode mode)
        {
            if (Interlocked.CompareExchange(ref _rebalancingQueued, Queued, NotQueued) == NotQueued)
            {
                Channel.SendMessageAboutOneResource(Guid.Empty, Actions.RebalanceQueued);

                _queueRebalance(() =>
                {
                    Channel.SendMessageAboutOneResource(Guid.Empty, Actions.RebalanceStarted);
                    var time = Stopwatch.StartNew();

                    bool lockTaken = false;
                    try
                    {
                        _writeLock.Enter(ref lockTaken);
                        var rebalancer = CreateRebalancer(mode);
                        _expectedGet   = rebalancer.Rebalance(_token);
                        _root          = rebalancer.ConstructNewTreeAfterCalculation();
                        _currentPath   = rebalancer.OutPath;
                        rebalancer.Dispose();
                    }
                    finally
                    {
                        time.Stop();
                        Channel.SendMessageAboutOneResource(Guid.Empty, Actions.RebalanceEnded,
                                                            time.Elapsed);
                        _rebalancingQueued = NotQueued;
                        if (lockTaken)
                        {
                            _writeLock.Exit();
                        }
                    }
                });
            }
        }
Пример #2
0
        internal void WriteResourceRegardlessofTransaction(Guid key, object resource)
        {
            var t = Stopwatch.StartNew();

            WriteResourceInt(key, resource);
            t.Stop();
            if (_statChannel != null)
            {
                _statChannel.SendMessageAboutOneResource(key, Actions.Posted, t.Elapsed);
            }
        }
Пример #3
0
        private ConcurrentDictionary <T, TElementType> GetFirstLevelCache()
        {
            Contract.Ensures(Contract.Result <ConcurrentDictionary <T, TElementType> >() != null);
            Contract.Invariant(_firstLevelCache != null);
            ConcurrentDictionary <T, TElementType> flc = null;

            try
            {
                if (_firstLevelCache != null)
                {
                    flc = _firstLevelCache.Target as ConcurrentDictionary <T, TElementType>;
                }
            }
            catch (NullReferenceException)//даже проверка на null не дает гарантии, что после этой проверки _firstLevelCache не обнулится. Проверка исключает часть выбрасываемых исключений
            {
            }
            while (flc == null)//в данной ситуации гарантирована рабочая ссылка по первому проходу цикла.
            //Тем не менее, в целях безопасности (если алгоритм изменится), цикл обеспечивает 100% инициализацию в любых ситуациях и не дороже обычного условного перехода
            {
                flc = new ConcurrentDictionary <T, TElementType>();
                var wr    = new WeakReference(flc);
                var oldWr = Interlocked.CompareExchange(ref _firstLevelCache, wr, _firstLevelCache);
                //вернется та ссыль, которая была до обмена, может там уже и будет словарь, тогда возьмем его. Если его нет - возьмем свой словарь.
                //wr.Target точно будет не null, т.к. мы ее держим через сильную ссылку flc, можно было бы, в принципе вместе wr.Target присвоить flc
                flc = (oldWr.Target ?? wr.Target) as ConcurrentDictionary <T, TElementType>;

                _lowItemsCountFixing = flc;
                if (_channel != null)
                {
                    _channel.SendMessageAboutOneResource(Guid.Empty, Actions.FirstLevelCacheRecreated);
                }
            }
            return(flc);
        }
Пример #4
0
        internal object GetItem(Guid id)
        {
            var t    = Stopwatch.StartNew();
            var item = GetItemInt(id);

            t.Stop();
            StatChannel.SendMessageAboutOneResource(id, Actions.ExternalGet, t.Elapsed);
            return(item);
        }
Пример #5
0
        internal SecurityToken GetTokenFor(Guid id, ISecurityContext context)
        {
            Contract.Requires(context != null);
            Contract.Ensures(Contract.Result <SecurityToken>() != null);
            var t     = Stopwatch.StartNew();
            var token = GetTokenForInt(id, context);

            t.Stop();
            _channel.SendMessageAboutOneResource(id, Actions.ExternalGet, t.Elapsed);
            return(token);
        }