Beispiel #1
0
        public void CompareTo_should_return_the_correct_value(string oidA, string oidB, int result)
        {
            var        subject1 = new ElectionId(ObjectId.Parse(oidA));
            ElectionId subject2 = oidB == null ? (ElectionId)null : new ElectionId(ObjectId.Parse(oidB));

            subject1.CompareTo(subject2).Should().Be(result);
        }
Beispiel #2
0
        public void Equals_should_return_true_if_all_fiels_are_equal()
        {
            var subject1 = new ElectionId(ObjectId.Empty);
            var subject2 = new ElectionId(ObjectId.Empty);

            subject1.Equals(subject2).Should().BeTrue();
            subject1.Equals((object)subject2).Should().BeTrue();
            subject1.GetHashCode().Should().Be(subject2.GetHashCode());
        }
Beispiel #3
0
        public void Equals_should_return_false_if_any_field_is_not_equal()
        {
            var subject1 = new ElectionId(ObjectId.Empty);
            var subject2 = new ElectionId(ObjectId.GenerateNewId());

            subject1.Equals(subject2).Should().BeFalse();
            subject1.Equals((object)subject2).Should().BeFalse();
            subject1.GetHashCode().Should().NotBe(subject2.GetHashCode());
        }
Beispiel #4
0
            public bool IsStale(int setVersion, ElectionId electionId)
            {
                if (_setVersion < setVersion)
                {
                    return(true);
                }
                if (_setVersion > setVersion)
                {
                    return(false);
                }

                if (_electionId == null)
                {
                    return(true);
                }

                return(_electionId.CompareTo(electionId) <= 0);
            }
Beispiel #5
0
            public bool IsStale(int setVersion, ElectionId electionId)
            {
                if (_setVersion < setVersion)
                {
                    return(true);
                }
                if (_setVersion > setVersion)
                {
                    return(false);
                }
                // Now it must be that _setVersion == setVersion
                if (_electionId == null)
                {
                    return(true);
                }

                return(_electionId.CompareTo(electionId) < 0);

                /* above is equivalent to:
                 * return
                 *   _setVersion < setVersion
                 *   || _setVersion == setVersion && (_electionId == null || _electionId.CompareTo(electionId) < 0); */
            }
        private void PublishDescription(EndPoint endPoint, ServerType serverType, IEnumerable <EndPoint> hosts = null, string setName = null, EndPoint primary = null, ElectionId electionId = null, EndPoint canonicalEndPoint = null)
        {
            var current = _serverFactory.GetServerDescription(endPoint);

            var config = new ReplicaSetConfig(
                hosts ?? new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint },
                setName ?? "test",
                primary,
                null);

            var description = current.With(
                averageRoundTripTime: TimeSpan.FromMilliseconds(10),
                replicaSetConfig: serverType.IsReplicaSetMember() ? config : null,
                canonicalEndPoint: canonicalEndPoint,
                electionId: electionId,
                state: ServerState.Connected,
                tags: null,
                type: serverType,
                version: new SemanticVersion(2, 6, 3),
                wireVersionRange: new Range <int>(0, int.MaxValue));

            _serverFactory.PublishDescription(description);
        }
Beispiel #7
0
 public bool IsFresher(int setVersion, ElectionId electionId)
 {
     return
         (_setVersion > setVersion ||
          _setVersion == setVersion && _electionId != null && _electionId.CompareTo(electionId) > 0);
 }
Beispiel #8
0
 public ElectionInfo(int setVersion, ElectionId electionId)
 {
     _setVersion = setVersion;
     _electionId = electionId;
 }
Beispiel #9
0
        private ClusterDescription ProcessReplicaSetChange(ClusterDescription clusterDescription, ServerDescriptionChangedEventArgs args)
        {
            if (!args.NewServerDescription.Type.IsReplicaSetMember())
            {
                return(RemoveServer(clusterDescription, args.NewServerDescription.EndPoint, string.Format("Server is a {0}, not a replica set member.", args.NewServerDescription.Type)));
            }

            if (args.NewServerDescription.Type == ServerType.ReplicaSetGhost)
            {
                return(clusterDescription.WithServerDescription(args.NewServerDescription));
            }

            if (_replicaSetName == null)
            {
                _replicaSetName = args.NewServerDescription.ReplicaSetConfig.Name;
            }

            if (_replicaSetName != args.NewServerDescription.ReplicaSetConfig.Name)
            {
                return(RemoveServer(clusterDescription, args.NewServerDescription.EndPoint, string.Format("Server was a member of the '{0}' replica set, but should be '{1}'.", args.NewServerDescription.ReplicaSetConfig.Name, _replicaSetName)));
            }

            clusterDescription = clusterDescription.WithServerDescription(args.NewServerDescription);
            clusterDescription = EnsureServers(clusterDescription, args.NewServerDescription);

            if (args.NewServerDescription.CanonicalEndPoint != null &&
                !EndPointHelper.Equals(args.NewServerDescription.CanonicalEndPoint, args.NewServerDescription.EndPoint))
            {
                return(RemoveServer(clusterDescription, args.NewServerDescription.EndPoint, "CanonicalEndPoint is different than seed list EndPoint."));
            }

            if (args.NewServerDescription.Type == ServerType.ReplicaSetPrimary)
            {
                if (args.NewServerDescription.ElectionId != null)
                {
                    if (_maxElectionId != null && _maxElectionId.CompareTo(args.NewServerDescription.ElectionId) > 0)
                    {
                        // ignore this change because we've already seen this election id
                        lock (_serversLock)
                        {
                            var server = _servers.SingleOrDefault(x => EndPointHelper.Equals(args.NewServerDescription.EndPoint, x.EndPoint));
                            server.Invalidate();
                            return(clusterDescription.WithServerDescription(
                                       new ServerDescription(server.ServerId, server.EndPoint)));
                        }
                    }

                    _maxElectionId = args.NewServerDescription.ElectionId;
                }

                var currentPrimaryEndPoints = clusterDescription.Servers
                                              .Where(x => x.Type == ServerType.ReplicaSetPrimary)
                                              .Where(x => !EndPointHelper.Equals(x.EndPoint, args.NewServerDescription.EndPoint))
                                              .Select(x => x.EndPoint)
                                              .ToList();

                if (currentPrimaryEndPoints.Count > 0)
                {
                    lock (_serversLock)
                    {
                        var currentPrimaries = _servers.Where(x => EndPointHelper.Contains(currentPrimaryEndPoints, x.EndPoint));
                        foreach (var currentPrimary in currentPrimaries)
                        {
                            // kick off the server to invalidate itself
                            currentPrimary.Invalidate();
                            // set it to disconnected in the cluster
                            clusterDescription = clusterDescription.WithServerDescription(
                                new ServerDescription(currentPrimary.ServerId, currentPrimary.EndPoint));
                        }
                    }
                }
            }

            return(clusterDescription);
        }
Beispiel #10
0
        private void PublishDescription(ICluster cluster, EndPoint endPoint, ServerType serverType, IEnumerable <EndPoint> hosts = null, string setName = null, EndPoint primary = null, ElectionId electionId = null, EndPoint canonicalEndPoint = null, int?setVersion = null)
        {
            var current = _serverFactory.GetServerDescription(endPoint);

            var config = new ReplicaSetConfig(
                hosts ?? new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint },
                setName ?? "test",
                primary,
                setVersion);

            var serverDescription = current.With(
                averageRoundTripTime: TimeSpan.FromMilliseconds(10),
                replicaSetConfig: serverType.IsReplicaSetMember() ? config : null,
                canonicalEndPoint: canonicalEndPoint,
                electionId: electionId,
                state: ServerState.Connected,
                tags: null,
                type: serverType,
                version: new SemanticVersion(2, 6, 3),
                wireVersionRange: new Range <int>(0, int.MaxValue));

            var currentClusterDescription = cluster.Description;

            _serverFactory.PublishDescription(serverDescription);
            SpinWait.SpinUntil(() => !object.ReferenceEquals(cluster.Description, currentClusterDescription), 100); // sometimes returns false and that's OK
        }
Beispiel #11
0
        public void ToString_should_return_string_representation()
        {
            var subject = new ElectionId(ObjectId.Empty);

            subject.ToString().Should().Be(ObjectId.Empty.ToString());
        }