Beispiel #1
0
        public void Modify(Uri replica, IList <Uri> allReplicas, IReplicaStorageProvider storageProvider, Request request, ref double weight)
        {
            DateTime lastGrayTimestamp;

            var storage = storageProvider.Obtain <DateTime>(storageKey);

            if (!storage.TryGetValue(replica, out lastGrayTimestamp))
            {
                return;
            }

            var currentTime = timeProvider.GetCurrentTime();
            var grayPeriod  = grayPeriodProvider.GetGrayPeriod();

            if (lastGrayTimestamp + grayPeriod >= currentTime)
            {
                weight = 0.0;
            }
            else
            {
                if (storage.Remove(replica, lastGrayTimestamp))
                {
                    LogReplicaIsNoLongerGray(replica);
                }
            }
        }
        /// <inheritdoc />
        public void Learn(ReplicaResult result, IReplicaStorageProvider storageProvider)
        {
            if (result.Verdict != ResponseVerdict.Reject)
            {
                return;
            }

            if (result.Response.Code == ResponseCode.StreamReuseFailure ||
                result.Response.Code == ResponseCode.StreamInputFailure ||
                result.Response.Code == ResponseCode.ContentInputFailure ||
                result.Response.Code == ResponseCode.ContentReuseFailure)
            {
                return;
            }

            var storage = storageProvider.Obtain <DateTime>(StorageKey);
            var wasGray = storage.ContainsKey(result.Replica);

            storage[result.Replica] = getCurrentTime();

            if (!wasGray)
            {
                LogReplicaIsGrayNow(result.Replica);
            }
        }
Beispiel #3
0
        /// <inheritdoc />
        public void Learn(ReplicaResult result, IReplicaStorageProvider storageProvider)
        {
            var storage = storageProvider.Obtain <bool>(StorageKey);

            var wasLeader = IsLeader(result.Replica, storage, out var hadStoredStatus);
            var isLeader  = resultDetector.IsLeaderResult(result);

            if (isLeader == wasLeader)
            {
                return;
            }

            var updatedStatus = hadStoredStatus
                ? storage.TryUpdate(result.Replica, isLeader, wasLeader)
                : storage.TryAdd(result.Replica, isLeader);

            if (updatedStatus)
            {
                if (isLeader)
                {
                    LogLeaderDetected(result.Replica);
                }
                else
                {
                    LogLeaderFailed(result.Replica);
                }
            }
        }
Beispiel #4
0
 /// <inheritdoc />
 public void Modify(Uri replica, IList <Uri> allReplicas, IReplicaStorageProvider storageProvider, Request request, RequestParameters parameters, ref double weight)
 {
     if (!IsLeader(replica, storageProvider.Obtain <bool>(StorageKey)))
     {
         weight = 0.0;
     }
 }
Beispiel #5
0
        public void Modify(Uri replica, IList <Uri> allReplicas, IReplicaStorageProvider storageProvider, Request request, ref double weight)
        {
            THealth currentHealth;

            if (storageProvider.Obtain <THealth>(storageKey).TryGetValue(replica, out currentHealth))
            {
                implementation.ModifyWeight(currentHealth, ref weight);
            }
        }
Beispiel #6
0
        public void SetUp()
        {
            replica1 = new Uri("http://replica1");
            replica2 = new Uri("http://replica2");
            replicas = new List <Uri> {
                replica1, replica2
            };
            request = Request.Get("foo/bar");

            storageProvider = Substitute.For <IReplicaStorageProvider>();
            storageProvider.Obtain <int>(Arg.Any <string>()).Returns(storage = new ConcurrentDictionary <Uri, int>());

            implementation = Substitute.For <IAdaptiveHealthImplementation <int> >();
            tuningPolicy   = Substitute.For <IAdaptiveHealthTuningPolicy>();
            modifier       = new AdaptiveHealthModifier <int>(implementation, tuningPolicy, log = new ConsoleLog());
        }
Beispiel #7
0
        public void Learn(ReplicaResult result, IReplicaStorageProvider storageProvider)
        {
            var storage = storageProvider.Obtain <THealth>(storageKey);

            while (true)
            {
                THealth currentHealth;
                THealth newHealth;
                bool    foundHealth;

                if (!(foundHealth = storage.TryGetValue(result.Replica, out currentHealth)))
                {
                    currentHealth = implementation.CreateDefaultHealth();
                }

                switch (tuningPolicy.SelectAction(result))
                {
                case AdaptiveHealthAction.Increase:
                    newHealth = implementation.IncreaseHealth(currentHealth);
                    break;

                case AdaptiveHealthAction.Decrease:
                    newHealth = implementation.DecreaseHealth(currentHealth);
                    break;

                default:
                    newHealth = currentHealth;
                    break;
                }

                if (implementation.AreEqual(currentHealth, newHealth))
                {
                    break;
                }

                var updatedHealth = foundHealth
                    ? storage.TryUpdate(result.Replica, newHealth, currentHealth)
                    : storage.TryAdd(result.Replica, newHealth);

                if (updatedHealth)
                {
                    implementation.LogHealthChange(result.Replica, currentHealth, newHealth, log);
                    break;
                }
            }
        }
Beispiel #8
0
        public void Learn(ReplicaResult result, IReplicaStorageProvider storageProvider)
        {
            if (result.Verdict != ResponseVerdict.Reject)
            {
                return;
            }

            var storage = storageProvider.Obtain <DateTime>(storageKey);
            var wasGray = storage.ContainsKey(result.Replica);

            storage[result.Replica] = timeProvider.GetCurrentTime();

            if (!wasGray)
            {
                LogReplicaIsGrayNow(result.Replica);
            }
        }
        public void SetUp()
        {
            replica  = new Uri("http://replica");
            replicas = new List <Uri> {
                replica
            };
            request  = Request.Get("foo/bar");
            response = new Response(ResponseCode.Ok);
            result   = new ReplicaResult(replica, response, ResponseVerdict.Accept, TimeSpan.Zero);
            weight   = 1.0;

            resultDetector  = Substitute.For <ILeaderResultDetector>();
            storageProvider = Substitute.For <IReplicaStorageProvider>();
            storageProvider.Obtain <bool>(Arg.Any <string>()).Returns(storage = new ConcurrentDictionary <Uri, bool>());

            modifier = new LeadershipWeightModifier(resultDetector, new ConsoleLog());
        }
        public void SetUp()
        {
            weight   = 1.0;
            request  = Request.Get("foo/bar");
            replica1 = new Uri("http://replica1");
            replica2 = new Uri("http://replica2");
            replicas = new List <Uri> {
                replica1, replica2
            };
            storage = new ConcurrentDictionary <Uri, DateTime>();

            storageProvider = Substitute.For <IReplicaStorageProvider>();
            storageProvider.Obtain <DateTime>(Arg.Any <string>()).Returns(storage);

            periodProvider = Substitute.For <IGrayPeriodProvider>();
            periodProvider.GetGrayPeriod().Returns(5.Minutes());

            timeProvider = Substitute.For <ITimeProvider>();
            timeProvider.GetCurrentTime().Returns(currentTime = DateTime.UtcNow);

            modifier = new GrayListModifier(periodProvider, timeProvider, new ConsoleLog());
        }