Beispiel #1
0
        public void TestMultiClusterGatewaySelection()
        {
            var candidates = new SiloAddress[] {
                SiloAddress.New(new IPEndPoint(0, 0), 0),
                SiloAddress.New(new IPEndPoint(0, 0), 1),
                SiloAddress.New(new IPEndPoint(0, 1), 0),
                SiloAddress.New(new IPEndPoint(0, 1), 1),
                SiloAddress.New(new IPEndPoint(0, 234), 1),
                SiloAddress.New(new IPEndPoint(1, 0), 0),
                SiloAddress.New(new IPEndPoint(1, 0), 1),
                SiloAddress.New(new IPEndPoint(1, 1), 1),
                SiloAddress.New(new IPEndPoint(1, 234), 1),
                SiloAddress.New(new IPEndPoint(2, 234), 1),
                SiloAddress.New(new IPEndPoint(3, 234), 1),
                SiloAddress.New(new IPEndPoint(4, 234), 1),
            };

            Func <SiloAddress, UpdateFaultCombo> group = (SiloAddress a) => new UpdateFaultCombo(a.Endpoint.Port, a.Generation);

            // randomize order
            var r          = new Random();
            var randomized = new SortedList <int, SiloAddress>();

            foreach (var c in candidates)
            {
                randomized.Add(r.Next(), c);
            }

            var x = MembershipHelper.DeterministicBalancedChoice(randomized.Values, 10, group);

            for (int i = 0; i < 10; i++)
            {
                Assert.Equal(candidates[i], x[i]);
            }
        }
Beispiel #2
0
        public List <SiloAddress> GetApproximateMultiClusterGateways()
        {
            if (ReferenceEquals(this.cachedSnapshot, this.tableManager.MembershipTableSnapshot))
            {
                return(this.gatewayCache);
            }

            lock (this.gatewayCacheUpdateLock)
            {
                var currentMembership = this.tableManager.MembershipTableSnapshot;
                if (ReferenceEquals(this.cachedSnapshot, currentMembership))
                {
                    return(this.gatewayCache);
                }

                var activeSilos   = new List <SiloAddress>();
                var activeEntries = new List <MembershipEntry>();
                foreach (var entry in currentMembership.Entries)
                {
                    var silo   = entry.Key;
                    var status = entry.Value.Status;
                    if (status == SiloStatus.Active)
                    {
                        activeEntries.Add(entry.Value);
                        activeSilos.Add(entry.Key);
                    }
                }

                List <SiloAddress> result;

                // take all the active silos if their count does not exceed the desired number of gateways
                if (activeEntries.Count <= this.maxMultiClusterGateways)
                {
                    result = activeSilos;
                }
                else
                {
                    result = MembershipHelper.DeterministicBalancedChoice(
                        activeSilos,
                        this.maxMultiClusterGateways,
                        (SiloAddress a) => new UpdateFaultCombo(currentMembership.Entries[a]),
                        logger);
                }

                if (logger.IsEnabled(LogLevel.Debug))
                {
                    var gateways = string.Join(", ", result.Select(silo => silo.ToString()));
                    logger.Debug($"-DetermineMultiClusterGateways {gateways}");
                }

                Interlocked.Exchange(ref this.cachedSnapshot, currentMembership);
                this.gatewayCache = result;
                return(result);
            }
        }