Ejemplo n.º 1
0
        public Candidate(ServerIdentifier serverIdentifier, 
            IElectionTimer electionTimer,
            IObserver<RequestVoteMessage> requestVote,
            IObservable<RequestVoteResultMessage> voteReply, 
            IVotingSystem votingSystem,
            IElection election,
            ILogReplication logReplication,
            Nodes nodes,
            ILoggerFactory loggerFactory,            
            RaftOptions options)
        {
            isDispose = false;
            _electionTimer = electionTimer;
            _requestVote = requestVote;
            _voteReply = voteReply;
            _votingSystem = votingSystem;
            _serverIdentifier = serverIdentifier;        
            _election = election;
            _options = options;
            _logger = loggerFactory.CreateLogger(nameof(Candidate) + " " + serverIdentifier);
            _election.CurrentTerm++;
            _election.VotedFor = _serverIdentifier.Id;
            _logReplication = logReplication;
            _nodes = nodes;

            if (_options.UseLogging)
                _logger.LogInformation($"{nameof(ServerStateType.Candidate)}");

            RequestElection();
        }
Ejemplo n.º 2
0
        public bool SetNewCurrentElection(IElection election)
        {
            if (CurrentElection.Equals(election))
            {
                return(false);
            }

            CurrentElection = election;

            return(true);
        }
Ejemplo n.º 3
0
        public bool RemoveElection(IElection election)
        {
            lock (electionsLock)
            {
                if (elections.Contains(election))
                {
                    return(false);
                }

                elections.Remove(election);
            }

            return(true);
        }
Ejemplo n.º 4
0
        public bool AddElection(IElection election)
        {
            lock (electionsLock)
            {
                if (elections.Contains(election))
                {
                    return(false);
                }

                elections.Add(election);
            }

            return(true);
        }
Ejemplo n.º 5
0
        private void StartElectionService()
        {
            Console.WriteLine("Starting local election service...");

            Uri baseAddress = DiscoveryHelper.AvailableTcpBaseAddress;

            _election = new Election();

            _election.Elected += ElectionElected;
            _election.ProcessLost += ElectionProcessLost;

            _election.FindConstituents();

            _electionHost = new ServiceHost(typeof(Election), baseAddress);

            _electionHost.AddDefaultEndpoints();

            _electionHost.Open();

            _election.ElectionIdentity = _electionHost.Description.Endpoints[1].Address;

            Console.WriteLine("Election Service Started at: {0}", _electionHost.Description.Endpoints[1].Address.ToString());
            Console.WriteLine();

            IntroduceElection();
        }
Ejemplo n.º 6
-1
        public Leader(IElection election, 
            IHartbeatTimer hartbeat,
            IObservable<AppendEntryResultMessage> reply,
            IObserver<AppendEntryMessage> append,
            IObserver<ClientResultMessage> clientReply,
            IObservable<ClientMessage> client,
            ILogReplication logReplication, 
            Nodes nodes,
            ILoggerFactory loggerFactory,
            RaftOptions options, 
            ServerIdentifier serverIdentifier)
        {
            _isDispose = false;
            _hartbeat = hartbeat;
            _append = append;
            _clientReply = clientReply;
            _client = client;
            _logReplication = logReplication;
            _nodes = nodes;
            _options = options;
            _serverIdentifier = serverIdentifier;
            _election = election;
            _logger = loggerFactory.CreateLogger(nameof(Leader) + " " + serverIdentifier);

            if (_options.UseLogging)
                _logger.LogInformation($"{nameof(ServerStateType.Leader)}");

            // Reinitialized after election
            NextIndex = new ConcurrentDictionary<ServerIdentifier, int>();
            MatchIndex = new ConcurrentDictionary<ServerIdentifier, int>();
            _hartbeat.Leader(SendHartbeat);
            _replyDispose = reply.Subscribe(EntryReplyMessageRecived);
            _clientReplyDispose = client.Subscribe(ClientMessageRecived);
        }