Exemplo n.º 1
0
        private void ConnectToMaster(VNodeInfo master)
        {
            Debug.Assert(_state == VNodeState.PreReplica);

            var masterEndPoint = GetMasterEndPoint(master, _useSsl);

            if (_connection != null)
            {
                _connection.Stop(string.Format("Reconnecting from old master [{0}] to new master: [{1}].",
                                               _connection.RemoteEndPoint, masterEndPoint));
            }

            _connection = new TcpConnectionManager(_useSsl ? "master-secure" : "master-normal",
                                                   Guid.NewGuid(),
                                                   _tcpDispatcher,
                                                   _publisher,
                                                   masterEndPoint,
                                                   _connector,
                                                   _useSsl,
                                                   _sslTargetHost,
                                                   _sslValidateServer,
                                                   _networkSendQueue,
                                                   _authProvider,
                                                   _heartbeatInterval,
                                                   _heartbeatTimeout,
                                                   OnConnectionEstablished,
                                                   OnConnectionClosed);
            _connection.StartReceiving();
        }
 public ReconnectToMaster(Guid stateCorrelationId, VNodeInfo master)
 {
     Ensure.NotEmptyGuid(stateCorrelationId, "stateCorrelationId");
     Ensure.NotNull(master, "master");
     StateCorrelationId = stateCorrelationId;
     Master             = master;
 }
Exemplo n.º 3
0
        public void Handle(SystemMessage.StateChangeMessage message)
        {
            switch (message.State)
            {
            case VNodeState.PreReplica:
            case VNodeState.CatchingUp:
            case VNodeState.Clone:
            case VNodeState.Follower:
            case VNodeState.PreReadOnlyReplica:
            case VNodeState.ReadOnlyReplica:
                _leaderInfo = ((SystemMessage.ReplicaStateMessage)message).Leader;
                break;

            case VNodeState.Initializing:
            case VNodeState.Unknown:
            case VNodeState.PreLeader:
            case VNodeState.Leader:
            case VNodeState.ResigningLeader:
            case VNodeState.Manager:
            case VNodeState.ShuttingDown:
            case VNodeState.Shutdown:
            case VNodeState.ReadOnlyLeaderless:
                _leaderInfo = null;
                break;

            default:
                throw new Exception(string.Format("Unknown node state: {0}.", message.State));
            }
        }
Exemplo n.º 4
0
 public ReconnectToLeader(Guid stateCorrelationId, VNodeInfo leader)
 {
     Ensure.NotEmptyGuid(stateCorrelationId, "stateCorrelationId");
     Ensure.NotNull(leader, "leader");
     StateCorrelationId = stateCorrelationId;
     Leader             = leader;
 }
Exemplo n.º 5
0
        public ReplicaService(IPublisher publisher,
                              TFChunkDb db,
                              IEpochManager epochManager,
                              IPublisher networkSendQueue,
                              IAuthenticationProvider authProvider,
                              VNodeInfo nodeInfo,
                              bool useSsl,
                              string sslTargetHost,
                              bool sslValidateServer)
        {
            Ensure.NotNull(publisher, "publisher");
            Ensure.NotNull(db, "db");
            Ensure.NotNull(epochManager, "epochManager");
            Ensure.NotNull(networkSendQueue, "networkSendQueue");
            Ensure.NotNull(authProvider, "authProvider");
            Ensure.NotNull(nodeInfo, "nodeInfo");
            if (useSsl)
            {
                Ensure.NotNull(sslTargetHost, "sslTargetHost");
            }

            _publisher        = publisher;
            _db               = db;
            _epochManager     = epochManager;
            _networkSendQueue = networkSendQueue;
            _authProvider     = authProvider;

            _nodeInfo          = nodeInfo;
            _useSsl            = useSsl;
            _sslTargetHost     = sslTargetHost;
            _sslValidateServer = sslValidateServer;

            _connector = new TcpClientConnector();
        }
Exemplo n.º 6
0
 private static IPEndPoint GetMasterEndPoint(VNodeInfo master, bool useSsl)
 {
     Ensure.NotNull(master, "master");
     if (useSsl && master.InternalSecureTcp == null)
     {
         Log.Error("Internal secure connections are required, but no internal secure TCP end point is specified for master [{master}]!", master);
     }
     return(useSsl ? master.InternalSecureTcp ?? master.InternalTcp : master.InternalTcp);
 }
Exemplo n.º 7
0
 private static IPEndPoint GetLeaderEndPoint(VNodeInfo leader, bool useSsl)
 {
     Ensure.NotNull(leader, "leader");
     if (useSsl && leader.InternalSecureTcp == null)
     {
         Log.Error(
             "Internal secure connections are required, but no internal secure TCP end point is specified for leader [{leader}]!",
             leader);
     }
     return(useSsl ? leader.InternalSecureTcp ?? leader.InternalTcp : leader.InternalTcp);
 }
Exemplo n.º 8
0
        public ElectionsService(IPublisher publisher,
                                VNodeInfo nodeInfo,
                                int clusterSize,
                                ICheckpoint writerCheckpoint,
                                ICheckpoint chaserCheckpoint,
                                IEpochManager epochManager,
                                Func <long> getLastCommitPosition,
                                int nodePriority,
                                ITimeProvider timeProvider)
        {
            Ensure.NotNull(publisher, nameof(publisher));
            Ensure.NotNull(nodeInfo, nameof(nodeInfo));
            Ensure.Positive(clusterSize, nameof(clusterSize));
            Ensure.NotNull(writerCheckpoint, nameof(writerCheckpoint));
            Ensure.NotNull(chaserCheckpoint, nameof(chaserCheckpoint));
            Ensure.NotNull(epochManager, nameof(epochManager));
            Ensure.NotNull(getLastCommitPosition, nameof(getLastCommitPosition));
            Ensure.NotNull(timeProvider, nameof(timeProvider));
            if (nodeInfo.IsReadOnlyReplica)
            {
                throw new ArgumentException("Read-only replicas are not allowed to run the Elections service.");
            }

            _publisher             = publisher;
            _nodeInfo              = nodeInfo;
            _publisherEnvelope     = new PublishEnvelope(_publisher);
            _clusterSize           = clusterSize;
            _writerCheckpoint      = writerCheckpoint;
            _chaserCheckpoint      = chaserCheckpoint;
            _epochManager          = epochManager;
            _getLastCommitPosition = getLastCommitPosition;
            _nodePriority          = nodePriority;
            _timeProvider          = timeProvider;

            var ownInfo = GetOwnInfo();

            _servers = new[] {
                MemberInfo.ForVNode(nodeInfo.InstanceId,
                                    _timeProvider.UtcNow,
                                    VNodeState.Initializing,
                                    true,
                                    nodeInfo.InternalTcp, nodeInfo.InternalSecureTcp,
                                    nodeInfo.ExternalTcp, nodeInfo.ExternalSecureTcp,
                                    nodeInfo.InternalHttp, nodeInfo.ExternalHttp,
                                    ownInfo.LastCommitPosition, ownInfo.WriterCheckpoint, ownInfo.ChaserCheckpoint,
                                    ownInfo.EpochPosition, ownInfo.EpochNumber, ownInfo.EpochId, ownInfo.NodePriority,
                                    nodeInfo.IsReadOnlyReplica)
            };
        }
        protected GossipServiceBase(IPublisher bus,
                                    IGossipSeedSource gossipSeedSource,
                                    VNodeInfo nodeInfo,
                                    TimeSpan gossipInterval,
                                    TimeSpan allowedTimeDifference)
        {
            Ensure.NotNull(bus, "bus");
            Ensure.NotNull(gossipSeedSource, "gossipSeedSource");
            Ensure.NotNull(nodeInfo, "nodeInfo");

            _bus                  = bus;
            _publishEnvelope      = new PublishEnvelope(bus);
            _gossipSeedSource     = gossipSeedSource;
            NodeInfo              = nodeInfo;
            GossipInterval        = gossipInterval;
            AllowedTimeDifference = allowedTimeDifference;
            _state                = GossipState.Startup;
        }
Exemplo n.º 10
0
        public ElectionsService(IPublisher publisher,
                                VNodeInfo nodeInfo,
                                int clusterSize,
                                ICheckpoint writerCheckpoint,
                                ICheckpoint chaserCheckpoint,
                                IEpochManager epochManager,
                                Func <long> getLastCommitPosition,
                                int nodePriority)
        {
            Ensure.NotNull(publisher, "publisher");
            Ensure.NotNull(nodeInfo, "nodeInfo");
            Ensure.Positive(clusterSize, "clusterSize");
            Ensure.NotNull(writerCheckpoint, "writerCheckpoint");
            Ensure.NotNull(chaserCheckpoint, "chaserCheckpoint");
            Ensure.NotNull(epochManager, "epochManager");
            Ensure.NotNull(getLastCommitPosition, "getLastCommitPosition");

            _publisher             = publisher;
            _nodeInfo              = nodeInfo;
            _publisherEnvelope     = new PublishEnvelope(_publisher);
            _clusterSize           = clusterSize;
            _writerCheckpoint      = writerCheckpoint;
            _chaserCheckpoint      = chaserCheckpoint;
            _epochManager          = epochManager;
            _getLastCommitPosition = getLastCommitPosition;
            _nodePriority          = nodePriority;

            var ownInfo = GetOwnInfo();

            _servers = new[]
            {
                MemberInfo.ForVNode(nodeInfo.InstanceId,
                                    DateTime.UtcNow,
                                    VNodeState.Initializing,
                                    true,
                                    nodeInfo.InternalTcp, nodeInfo.InternalSecureTcp,
                                    nodeInfo.ExternalTcp, nodeInfo.ExternalSecureTcp,
                                    nodeInfo.InternalHttp, nodeInfo.ExternalHttp,
                                    ownInfo.LastCommitPosition, ownInfo.WriterCheckpoint, ownInfo.ChaserCheckpoint,
                                    ownInfo.EpochPosition, ownInfo.EpochNumber, ownInfo.EpochId, ownInfo.NodePriority)
            };
        }
Exemplo n.º 11
0
        public NodeGossipService(IPublisher bus,
                                 IGossipSeedSource gossipSeedSource,
                                 VNodeInfo nodeInfo,
                                 ICheckpoint writerCheckpoint,
                                 ICheckpoint chaserCheckpoint,
                                 IEpochManager epochManager,
                                 Func <long> getLastCommitPosition,
                                 int nodePriority)
            : base(bus, gossipSeedSource, nodeInfo)
        {
            Ensure.NotNull(writerCheckpoint, "writerCheckpoint");
            Ensure.NotNull(chaserCheckpoint, "chaserCheckpoint");
            Ensure.NotNull(epochManager, "epochManager");
            Ensure.NotNull(getLastCommitPosition, "getLastCommitPosition");

            _writerCheckpoint      = writerCheckpoint;
            _chaserCheckpoint      = chaserCheckpoint;
            _epochManager          = epochManager;
            _getLastCommitPosition = getLastCommitPosition;
            _nodePriority          = nodePriority;
        }
Exemplo n.º 12
0
        public ClusterVNodeController(IPublisher outputBus, VNodeInfo nodeInfo, TFChunkDb db,
                                      ClusterVNodeSettings vnodeSettings, ClusterVNode node,
                                      MessageForwardingProxy forwardingProxy)
        {
            Ensure.NotNull(outputBus, "outputBus");
            Ensure.NotNull(nodeInfo, "nodeInfo");
            Ensure.NotNull(db, "dbConfig");
            Ensure.NotNull(vnodeSettings, "vnodeSettings");
            Ensure.NotNull(node, "node");
            Ensure.NotNull(forwardingProxy, "forwardingProxy");

            _outputBus = outputBus;
            _nodeInfo  = nodeInfo;
            _db        = db;
            _node      = node;

            _forwardingProxy   = forwardingProxy;
            _forwardingTimeout = vnodeSettings.PrepareTimeout + vnodeSettings.CommitTimeout + TimeSpan.FromMilliseconds(300);

            _fsm = CreateFSM();
        }
Exemplo n.º 13
0
        public ReplicaService(IPublisher publisher,
                              TFChunkDb db,
                              IEpochManager epochManager,
                              IPublisher networkSendQueue,
                              IAuthenticationProvider authProvider,
                              AuthorizationGateway authorizationGateway,
                              VNodeInfo nodeInfo,
                              bool useSsl,
                              bool sslValidateServer,
                              X509CertificateCollection sslClientCertificates,
                              TimeSpan heartbeatTimeout,
                              TimeSpan heartbeatInterval,
                              TimeSpan writeTimeout)
        {
            Ensure.NotNull(publisher, "publisher");
            Ensure.NotNull(db, "db");
            Ensure.NotNull(epochManager, "epochManager");
            Ensure.NotNull(networkSendQueue, "networkSendQueue");
            Ensure.NotNull(authProvider, "authProvider");
            Ensure.NotNull(authorizationGateway, "authorizationGateway");
            Ensure.NotNull(nodeInfo, "nodeInfo");

            _publisher            = publisher;
            _db                   = db;
            _epochManager         = epochManager;
            _networkSendQueue     = networkSendQueue;
            _authProvider         = authProvider;
            _authorizationGateway = authorizationGateway;

            _nodeInfo              = nodeInfo;
            _useSsl                = useSsl;
            _sslValidateServer     = sslValidateServer;
            _sslClientCertificates = sslClientCertificates;
            _heartbeatTimeout      = heartbeatTimeout;
            _heartbeatInterval     = heartbeatInterval;

            _connector     = new TcpClientConnector();
            _tcpDispatcher = new InternalTcpDispatcher(writeTimeout);
        }
Exemplo n.º 14
0
        private void ConnectToLeader(VNodeInfo leader)
        {
            Debug.Assert(_state == VNodeState.PreReplica || _state == VNodeState.PreReadOnlyReplica);

            var leaderEndPoint = GetLeaderEndPoint(leader, _useSsl);

            if (leaderEndPoint == null)
            {
                Log.Error("No valid endpoint found to connect to the Leader. Aborting connection operation to Leader.");
                return;
            }

            if (_connection != null)
            {
                _connection.Stop(string.Format("Reconnecting from old leader [{0}] to new leader: [{1}].",
                                               _connection.RemoteEndPoint, leaderEndPoint));
            }

            _connection = new TcpConnectionManager(_useSsl ? "leader-secure" : "leader-normal",
                                                   Guid.NewGuid(),
                                                   _tcpDispatcher,
                                                   _publisher,
                                                   leaderEndPoint,
                                                   _connector,
                                                   _useSsl,
                                                   _sslServerCertValidator,
                                                   _sslClientCertificate == null ? null : new X509CertificateCollection {
                _sslClientCertificate
            },
                                                   _networkSendQueue,
                                                   _authProvider,
                                                   _authorizationGateway,
                                                   _heartbeatInterval,
                                                   _heartbeatTimeout,
                                                   OnConnectionEstablished,
                                                   OnConnectionClosed);
            _connection.StartReceiving();
        }
Exemplo n.º 15
0
        public ClusterVNodeSettings(Guid instanceId, int debugIndex,
                                    IPEndPoint internalTcpEndPoint,
                                    IPEndPoint internalSecureTcpEndPoint,
                                    IPEndPoint externalTcpEndPoint,
                                    IPEndPoint externalSecureTcpEndPoint,
                                    IPEndPoint httpEndPoint,
                                    int nodePriority,
                                    bool readOnlyReplica)
        {
            Ensure.NotEmptyGuid(instanceId, "instanceId");
            Ensure.Equal(false, internalTcpEndPoint == null && internalSecureTcpEndPoint == null, "Both internal TCP endpoints are null");

            Ensure.NotNull(httpEndPoint, nameof(httpEndPoint));

            NodeInfo = new VNodeInfo(instanceId, debugIndex,
                                     internalTcpEndPoint, internalSecureTcpEndPoint,
                                     externalTcpEndPoint, externalSecureTcpEndPoint,
                                     httpEndPoint,
                                     readOnlyReplica);


            NodePriority    = nodePriority;
            ReadOnlyReplica = readOnlyReplica;
        }