Exemple #1
0
        internal void AddHandler <TActualMessage>(VNodeState state, Action <VNodeState, Message> handler)
            where TActualMessage : Message
        {
            var stateNum = (int)state;

            Dictionary <Type, Action <VNodeState, Message> > stateHandlers = _handlers[stateNum];

            if (stateHandlers == null)
            {
                stateHandlers = _handlers[stateNum] = new Dictionary <Type, Action <VNodeState, Message> >();
            }

            //var existingHandler = stateHandlers[typeof (TActualMessage)];
            //stateHandlers[typeof (TActualMessage)] = existingHandler == null
            //                                            ? handler
            //                                            : (s, m) => { existingHandler(s, m); handler(s, m); };

            if (stateHandlers.ContainsKey(typeof(TActualMessage)))
            {
                throw new InvalidOperationException(
                          string.Format("Handler already defined for state {0} and message {1}",
                                        state,
                                        typeof(TActualMessage).FullName));
            }
            stateHandlers[typeof(TActualMessage)] = handler;
        }
Exemple #2
0
        public void Start()
        {
            StartingTime.Start();

            StartedEvent = new ManualResetEvent(false);
            Node.MainBus.Subscribe(
                new AdHocHandler <SystemMessage.StateChangeMessage>(m =>
            {
                NodeState = VNodeState.Unknown;
            }));
            Node.MainBus.Subscribe(
                new AdHocHandler <SystemMessage.BecomeMaster>(m =>
            {
                NodeState = VNodeState.Master;
                StartedEvent.Set();
            }));
            Node.MainBus.Subscribe(
                new AdHocHandler <SystemMessage.BecomeSlave>(m =>
            {
                NodeState = VNodeState.Slave;
                StartedEvent.Set();
            }));

            Node.Start();
        }
Exemple #3
0
 public static MemberInfo Initial(Guid instanceId,
                                  DateTime timeStamp,
                                  VNodeState state,
                                  bool isAlive,
                                  EndPoint internalTcpEndPoint,
                                  EndPoint internalSecureTcpEndPoint,
                                  EndPoint externalTcpEndPoint,
                                  EndPoint externalSecureTcpEndPoint,
                                  EndPoint httpEndPoint,
                                  string advertiseHostToClientAs,
                                  int advertiseHttpPortToClientAs,
                                  int advertiseTcpPortToClientAs,
                                  int nodePriority,
                                  bool isReadOnlyReplica)
 {
     if (state == VNodeState.Manager)
     {
         throw new ArgumentException(string.Format("Wrong State for VNode: {0}", state), "state");
     }
     return(new MemberInfo(instanceId, timeStamp, state, isAlive,
                           internalTcpEndPoint, internalSecureTcpEndPoint,
                           externalTcpEndPoint, externalSecureTcpEndPoint,
                           httpEndPoint, advertiseHostToClientAs, advertiseHttpPortToClientAs, advertiseTcpPortToClientAs,
                           -1, -1, -1, -1, -1, Guid.Empty, nodePriority, isReadOnlyReplica));
 }
Exemple #4
0
 internal MemberInfo(MemberInfoDto dto)
 {
     InstanceId                = dto.InstanceId;
     TimeStamp                 = dto.TimeStamp;
     State                     = dto.State;
     IsAlive                   = dto.IsAlive;
     InternalTcpEndPoint       = new DnsEndPoint(dto.InternalTcpIp, dto.InternalTcpPort);
     InternalSecureTcpEndPoint = dto.InternalSecureTcpPort > 0
                         ? new DnsEndPoint(dto.InternalTcpIp, dto.InternalSecureTcpPort)
                         : null;
     ExternalTcpEndPoint       = dto.ExternalTcpIp != null ? new DnsEndPoint(dto.ExternalTcpIp, dto.ExternalTcpPort) : null;
     ExternalSecureTcpEndPoint = dto.ExternalTcpIp != null && dto.ExternalSecureTcpPort > 0
                         ? new DnsEndPoint(dto.ExternalTcpIp, dto.ExternalSecureTcpPort)
                         : null;
     HttpEndPoint                = new DnsEndPoint(dto.HttpEndPointIp, dto.HttpEndPointPort);
     AdvertiseHostToClientAs     = dto.AdvertiseHostToClientAs;
     AdvertiseHttpPortToClientAs = dto.AdvertiseHttpPortToClientAs;
     AdvertiseTcpPortToClientAs  = dto.AdvertiseTcpPortToClientAs;
     LastCommitPosition          = dto.LastCommitPosition;
     WriterCheckpoint            = dto.WriterCheckpoint;
     ChaserCheckpoint            = dto.ChaserCheckpoint;
     EpochPosition               = dto.EpochPosition;
     EpochNumber       = dto.EpochNumber;
     EpochId           = dto.EpochId;
     NodePriority      = dto.NodePriority;
     IsReadOnlyReplica = dto.IsReadOnlyReplica;
 }
Exemple #5
0
 public static MemberInfo ForVNode(Guid instanceId,
                                   DateTime timeStamp,
                                   VNodeState state,
                                   bool isAlive,
                                   EndPoint internalTcpEndPoint,
                                   EndPoint internalSecureTcpEndPoint,
                                   EndPoint externalTcpEndPoint,
                                   EndPoint externalSecureTcpEndPoint,
                                   EndPoint httpEndPoint,
                                   string advertiseHostToClientAs,
                                   int advertiseHttpPortToClientAs,
                                   int advertiseTcpPortToClientAs,
                                   long lastCommitPosition,
                                   long writerCheckpoint,
                                   long chaserCheckpoint,
                                   long epochPosition,
                                   int epochNumber,
                                   Guid epochId,
                                   int nodePriority,
                                   bool isReadOnlyReplica)
 {
     if (state == VNodeState.Manager)
     {
         throw new ArgumentException(string.Format("Wrong State for VNode: {0}", state), "state");
     }
     return(new MemberInfo(instanceId, timeStamp, state, isAlive,
                           internalTcpEndPoint, internalSecureTcpEndPoint,
                           externalTcpEndPoint, externalSecureTcpEndPoint,
                           httpEndPoint, advertiseHostToClientAs, advertiseHttpPortToClientAs, advertiseTcpPortToClientAs,
                           lastCommitPosition, writerCheckpoint, chaserCheckpoint,
                           epochPosition, epochNumber, epochId, nodePriority, isReadOnlyReplica));
 }
Exemple #6
0
        public void Start()
        {
            StartingTime.Start();

            Node.MainBus.Subscribe(
                new AdHocHandler <SystemMessage.StateChangeMessage>(m => {
                NodeState = _isReadOnlyReplica ?
                            VNodeState.ReadOnlyMasterless : VNodeState.Unknown;
            }));
            if (!_isReadOnlyReplica)
            {
                Node.MainBus.Subscribe(
                    new AdHocHandler <SystemMessage.BecomeMaster>(m => {
                    NodeState = VNodeState.Master;
                    _started.TrySetResult(true);
                }));
                Node.MainBus.Subscribe(
                    new AdHocHandler <SystemMessage.BecomeSlave>(m => {
                    NodeState = VNodeState.Slave;
                    _started.TrySetResult(true);
                }));
            }
            else
            {
                Node.MainBus.Subscribe(
                    new AdHocHandler <SystemMessage.BecomeReadOnlyReplica>(m => {
                    NodeState = VNodeState.ReadOnlyReplica;
                    _started.TrySetResult(true);
                }));
            }
            _host.Start();
            Node.Start();
        }
Exemple #7
0
 public static bool IsReplica(this VNodeState state)
 {
     return(state == VNodeState.CatchingUp ||
            state == VNodeState.Clone ||
            state == VNodeState.Follower ||
            state == VNodeState.ReadOnlyReplica);
 }
Exemple #8
0
        internal MemberInfo(Guid instanceId, DateTime timeStamp, VNodeState state, bool isAlive,
                            EndPoint internalTcpEndPoint, EndPoint internalSecureTcpEndPoint,
                            EndPoint externalTcpEndPoint, EndPoint externalSecureTcpEndPoint,
                            EndPoint httpEndPoint,
                            long lastCommitPosition, long writerCheckpoint, long chaserCheckpoint,
                            long epochPosition, int epochNumber, Guid epochId, int nodePriority, bool isReadOnlyReplica)
        {
            Ensure.Equal(false, internalTcpEndPoint == null && internalSecureTcpEndPoint == null, "Both internal TCP endpoints are null");
            Ensure.NotNull(httpEndPoint, nameof(httpEndPoint));

            InstanceId = instanceId;

            TimeStamp = timeStamp;
            State     = state;
            IsAlive   = isAlive;

            InternalTcpEndPoint       = internalTcpEndPoint;
            InternalSecureTcpEndPoint = internalSecureTcpEndPoint;
            ExternalTcpEndPoint       = externalTcpEndPoint;
            ExternalSecureTcpEndPoint = externalSecureTcpEndPoint;
            HttpEndPoint = httpEndPoint;

            LastCommitPosition = lastCommitPosition;
            WriterCheckpoint   = writerCheckpoint;
            ChaserCheckpoint   = chaserCheckpoint;

            EpochPosition = epochPosition;
            EpochNumber   = epochNumber;
            EpochId       = epochId;

            NodePriority      = nodePriority;
            IsReadOnlyReplica = isReadOnlyReplica;
        }
Exemple #9
0
        private MemberInfo(Guid instanceId, DateTime timeStamp, VNodeState state, bool isAlive,
                           IPEndPoint internalTcpEndPoint, IPEndPoint internalSecureTcpEndPoint,
                           IPEndPoint externalTcpEndPoint, IPEndPoint externalSecureTcpEndPoint,
                           IPEndPoint internalHttpEndPoint, IPEndPoint externalHttpEndPoint,
                           long lastCommitPosition, long writerCheckpoint, long chaserCheckpoint,
                           long epochPosition, int epochNumber, Guid epochId, int nodePriority)
        {
            Ensure.NotNull(internalTcpEndPoint, "internalTcpEndPoint");
            Ensure.NotNull(externalTcpEndPoint, "externalTcpEndPoint");
            Ensure.NotNull(internalHttpEndPoint, "internalHttpEndPoint");
            Ensure.NotNull(externalHttpEndPoint, "externalHttpEndPoint");

            InstanceId = instanceId;

            TimeStamp = timeStamp;
            State     = state;
            IsAlive   = isAlive;

            InternalTcpEndPoint       = internalTcpEndPoint;
            InternalSecureTcpEndPoint = internalSecureTcpEndPoint;
            ExternalTcpEndPoint       = externalTcpEndPoint;
            ExternalSecureTcpEndPoint = externalSecureTcpEndPoint;
            InternalHttpEndPoint      = internalHttpEndPoint;
            ExternalHttpEndPoint      = externalHttpEndPoint;

            LastCommitPosition = lastCommitPosition;
            WriterCheckpoint   = writerCheckpoint;
            ChaserCheckpoint   = chaserCheckpoint;

            EpochPosition = epochPosition;
            EpochNumber   = epochNumber;
            EpochId       = epochId;

            NodePriority = nodePriority;
        }
Exemple #10
0
        internal MemberInfo(MemberInfoDto dto)
        {
            InstanceId = dto.InstanceId;
            TimeStamp  = dto.TimeStamp;
            State      = dto.State;
            IsAlive    = dto.IsAlive;
            var internalTcpIp = IPAddress.Parse(dto.InternalTcpIp);
            var externalTcpIp = dto.ExternalTcpIp != null?IPAddress.Parse(dto.ExternalTcpIp) : null;

            InternalTcpEndPoint       = new IPEndPoint(internalTcpIp, dto.InternalTcpPort);
            InternalSecureTcpEndPoint = dto.InternalSecureTcpPort > 0
                                ? new IPEndPoint(internalTcpIp, dto.InternalSecureTcpPort)
                                : null;
            ExternalTcpEndPoint       = externalTcpIp != null ? new IPEndPoint(externalTcpIp, dto.ExternalTcpPort) : null;
            ExternalSecureTcpEndPoint = externalTcpIp != null && dto.ExternalSecureTcpPort > 0
                                ? new IPEndPoint(externalTcpIp, dto.ExternalSecureTcpPort)
                                : null;
            InternalHttpEndPoint = new IPEndPoint(IPAddress.Parse(dto.InternalHttpIp), dto.InternalHttpPort);
            ExternalHttpEndPoint = new IPEndPoint(IPAddress.Parse(dto.ExternalHttpIp), dto.ExternalHttpPort);
            LastCommitPosition   = dto.LastCommitPosition;
            WriterCheckpoint     = dto.WriterCheckpoint;
            ChaserCheckpoint     = dto.ChaserCheckpoint;
            EpochPosition        = dto.EpochPosition;
            EpochNumber          = dto.EpochNumber;
            EpochId           = dto.EpochId;
            NodePriority      = dto.NodePriority;
            IsReadOnlyReplica = dto.IsReadOnlyReplica;
        }
Exemple #11
0
 public static MemberInfo ForVNode(Guid instanceId,
                                   DateTime timeStamp,
                                   VNodeState state,
                                   bool isAlive,
                                   IPEndPoint internalTcpEndPoint,
                                   IPEndPoint internalSecureTcpEndPoint,
                                   IPEndPoint externalTcpEndPoint,
                                   IPEndPoint externalSecureTcpEndPoint,
                                   IPEndPoint internalHttpEndPoint,
                                   IPEndPoint externalHttpEndPoint,
                                   long lastCommitPosition,
                                   long writerCheckpoint,
                                   long chaserCheckpoint,
                                   long epochPosition,
                                   int epochNumber,
                                   Guid epochId,
                                   int nodePriority)
 {
     if (state == VNodeState.Manager)
     {
         throw new ArgumentException(string.Format("Wrong State for VNode: {0}", state), "state");
     }
     return(new MemberInfo(instanceId, timeStamp, state, isAlive,
                           internalTcpEndPoint, internalSecureTcpEndPoint,
                           externalTcpEndPoint, externalSecureTcpEndPoint,
                           internalHttpEndPoint, externalHttpEndPoint,
                           lastCommitPosition, writerCheckpoint, chaserCheckpoint,
                           epochPosition, epochNumber, epochId, nodePriority));
 }
        public void Handle(SystemMessage.StateChangeMessage message)
        {
            _state = message.State;

            switch (message.State)
            {
            case VNodeState.Initializing:
            case VNodeState.Unknown:
            case VNodeState.PreMaster:
            case VNodeState.Master:
            case VNodeState.ShuttingDown:
            case VNodeState.Shutdown: {
                Disconnect();
                break;
            }

            case VNodeState.PreReplica: {
                var m = (SystemMessage.BecomePreReplica)message;
                ConnectToMaster(m.Master);
                break;
            }

            case VNodeState.CatchingUp:
            case VNodeState.Clone:
            case VNodeState.Slave: {
                // nothing changed, essentially
                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
 private void Handle(SystemMessage.BecomeShutdown message)
 {
     Log.Info("========== [{0}] IS SHUT DOWN.", _nodeInfo.InternalHttp);
     _state = VNodeState.Shutdown;
     try
     {
         _outputBus.Publish(message);
     }
     catch (Exception exc)
     {
         Log.ErrorException(exc, "Error when publishing {0}.", message);
     }
     if (_exitProcessOnShutdown)
     {
         try
         {
             _node.WorkersHandler.Stop();
             _mainQueue.RequestStop();
         }
         catch (Exception exc)
         {
             Log.ErrorException(exc, "Error when stopping workers/main queue.");
         }
         Application.Exit(ExitCode.Success, "Shutdown and exit from process was requested.");
     }
 }
Exemple #14
0
 private void Handle(SystemMessage.BecomePreMaster message)
 {
     Log.Info("========== [{0}] PRE-MASTER STATE, WAITING FOR CHASER TO CATCH UP...", _httpEndPoint);
     _state = VNodeState.PreMaster;
     _mainQueue.Publish(new SystemMessage.WaitForChaserToCatchUp(Guid.NewGuid(), TimeSpan.Zero));
     _outputBus.Publish(message);
 }
        private void Handle(SystemMessage.BecomeWorking message)
        {
            Log.Info("[{0}] IS WORKING!!! SPARTA!!!111", _httpEndPoint);

            _state = VNodeState.Master;

            _outputBus.Publish(message);
        }
        private void Handle(SystemMessage.BecomeShutdown message)
        {
            Log.Info("[{0}] IS SHUT DOWN!!! SWEET DREAMS!!!111", _httpEndPoint);

            _state = VNodeState.Shutdown;

            _outputBus.Publish(message);
        }
Exemple #17
0
 public void Handle(SystemMessage.StateChangeMessage msg)
 {
     //switching to master from non-Master
     if (_state != msg.State)
     {
         _replicaLogPositions.Clear();
     }
     _state = msg.State;
 }
Exemple #18
0
        public void Handle(SystemMessage.StateChangeMessage message)
        {
            _state = message.State;

            if (message.State == VNodeState.ShuttingDown)
            {
                _stop = true;
            }
        }
        private void Handle(SystemMessage.BecomeUnknown message)
        {
            Log.Info("========== [{0}] IS UNKNOWN...", _nodeInfo.InternalHttp);

            _state  = VNodeState.Unknown;
            _master = null;
            _outputBus.Publish(message);
            _mainQueue.Publish(new ElectionMessage.StartElections());
        }
		public void Handle(SystemMessage.StateChangeMessage message) {
			_state = message.State;

			if (message.State == VNodeState.Leader)
				_noQuorumTimestamp = TimeSpan.Zero;

			if (message.State == VNodeState.ShuttingDown)
				_stop = true;
		}
Exemple #21
0
        private void HandleBecomeOtherNodeState(SystemMessage.StateChangeMessage received)
        {
            if (_managerState != ManagerState.Initial)
            {
                Received(received, "Inauguration process is now stopped.");
                _managerState = ManagerState.Initial;
            }

            _nodeState = received.State;
        }
Exemple #22
0
 private void Handle(SystemMessage.BecomeShutdown message)
 {
     Log.Info("========== [{0}] IS SHUT DOWN!!! SWEET DREAMS!!!", _httpEndPoint);
     _state = VNodeState.Shutdown;
     _outputBus.Publish(message);
     if (_exitProcessOnShutdown)
     {
         Application.Exit(ExitCode.Success, "Shutdown with exiting from process was requested.");
     }
 }
Exemple #23
0
 private void HandleBecomePreLeader(SystemMessage.BecomePreLeader received)
 {
     Received(
         received,
         "Starting inauguration process with correlation id {currentCorrelationId}. Waiting for chaser.",
         received.CorrelationId);
     _nodeState          = received.State;
     _managerState       = ManagerState.WaitingForChaser;
     _stateCorrelationId = received.CorrelationId;
 }
        public void Handle(SystemMessage.StateChangeMessage message)
        {
            _currentState = message.State;
            if (_currentState != VNodeState.Master)
            {
                _ready = false;
            }

            StartWhenConditionsAreMet();
        }
Exemple #25
0
        private static bool TryHandle(VNodeState state, Action <VNodeState, Message>[] handlers, Message message, int msgTypeId)
        {
            Action <VNodeState, Message> handler = handlers[msgTypeId];

            if (handler != null)
            {
                handler(state, message);
                return(true);
            }
            return(false);
        }
Exemple #26
0
        public void Handle(SystemMessage.StateChangeMessage message)
        {
            CurrentRole = message.State;
            var replicaState = message as SystemMessage.ReplicaStateMessage;

            CurrentLeader = replicaState == null ? null : replicaState.Leader;
            _cluster      = UpdateCluster(_cluster, x => x.InstanceId == NodeInfo.InstanceId ? GetUpdatedMe(x) : x,
                                          _timeProvider, DeadMemberRemovalPeriod);

            _bus.Publish(new GossipMessage.GossipUpdated(_cluster));
        }
        private void Handle(SystemMessage.BecomeShuttingDown message)
        {
            Log.Info("[{0}] IS SHUTTING DOWN!!! FAREWELL, WORLD...", _httpEndPoint);

            _state = VNodeState.ShuttingDown;

            _mainQueue.Publish(TimerMessage.Schedule.Create(ShutdownTimeout,
                                                            new PublishEnvelope(_mainQueue),
                                                            new SystemMessage.ShutdownTimeout()));
            _outputBus.Publish(message);
        }
Exemple #28
0
        public void Handle(SystemMessage.StateChangeMessage message)
        {
            CurrentRole = message.State;
            var replicaState = message as SystemMessage.ReplicaStateMessage;

            CurrentMaster = replicaState == null ? null : replicaState.Master;
            _cluster      = UpdateCluster(_cluster, x => x.InstanceId == NodeInfo.InstanceId ? GetUpdatedMe(x) : x);

            //if (_cluster.HasChangedSince(oldCluster))
            //LogClusterChange(oldCluster, _cluster, _nodeInfo.InternalHttp);
            _bus.Publish(new GossipMessage.GossipUpdated(_cluster));
        }
        public void Handle(SystemMessage.StateChangeMessage message)
        {
            _state = message.State;

            if (message.State == VNodeState.Master)
            {
                return;
            }
            Log.Debug("Persistent subscriptions received state change to {0}. Stopping listening", _state);
            ShutdownSubscriptions();
            Stop();
        }
 public void Handle(SystemMessage.StateChangeMessage msg)
 {
     if (_state == VNodeState.Master && msg.State != VNodeState.Master)
     {
         var commits = _commitAcks.GetAllCommitAcks();
         foreach (var commit in commits)
         {
             CommitReplicated(commit.CommitAcks[0]);
         }
         _commitAcks.ClearCommitAcks();
     }
     _state = msg.State;
 }
 private static bool IsRunning(VNodeState vNodeState)
 {
     switch (vNodeState)
     {
         case VNodeState.PreReplica:
         case VNodeState.CatchingUp:
         case VNodeState.Clone:
         case VNodeState.Slave:
         case VNodeState.PreMaster:
         case VNodeState.Master:
         case VNodeState.Manager:
             return true;
         case VNodeState.Initializing:
         case VNodeState.Unknown:
         case VNodeState.ShuttingDown:
         case VNodeState.Shutdown:
             return false;
         default:
             throw new ArgumentOutOfRangeException("vNodeState");
     }
 }
 protected StateChangeMessage(Guid correlationId, VNodeState state)
 {
     Ensure.NotEmptyGuid(correlationId, "correlationId");
     CorrelationId = correlationId;
     State = state;
 }
 protected ReplicaStateMessage(Guid correlationId, VNodeState state, VNodeInfo master)
     : base(correlationId, state)
 {
     Ensure.NotNull(master, "master");
     Master = master;
 }
 public VNodeStatusChangeArgs(VNodeState newVNodeState)
 {
     NewVNodeState = newVNodeState;
 }
Exemple #35
0
 protected StateChangeMessage(VNodeState state)
 {
     State = state;
 }