protected AppendEntryBaseTest()
        {
            // Arrange  
            var mocker = new AutoMocker();

            ServerIdentifier = new ServerIdentifier();

            mocker.Use(ServerIdentifier);

            mocker.Setup<ISubject<AppendEntryMessage>>(p => p.Subscribe(It.IsAny<IObserver<AppendEntryMessage>>()))
                .Callback<IObserver<AppendEntryMessage>>((o) =>
                {
                    AppendEntryCallback = o;
                });
            AppendEntryCallback = mocker.Get<ISubject<AppendEntryMessage>>();

            AppendEntryResult = new Subject<AppendEntryResultMessage>();
            Election = new Election();
            LogReplication = new LogReplication();
            
            AppendEntry = new Rafting.AppendEntry(AppendEntryResult,
                mocker.Get<ISubject<AppendEntryMessage>>(),
                mocker.Get<IHartbeatTimer>(),
                LogReplication,
                Election,
                mocker.Get<ILoggerFactory>(),
                new RaftOptions(),
                mocker.Get<ServerIdentifier>(),
                null);
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
0
 public KvPropertyService(
     IRepository <KvProperty> kvPropertyRepository,
     IKvPropertyValidator kvPropertyValidator,
     ILogReplication logReplication,
     IConsensusContext consensusContext)
 {
     _kvPropertyRepository = kvPropertyRepository;
     _kvPropertyValidator  = kvPropertyValidator;
     _logReplication       = logReplication;
     _consensusContext     = consensusContext;
 }
Exemplo n.º 4
0
 public ServicesService(
     IRepository <Service> serviceRepository,
     IServiceValidator serviceValidator,
     IMemoryCache memoryCache,
     IServiceHealthWorker serviceHealthWorker,
     ILogReplication logReplication,
     IConsensusContext consensusContext)
 {
     _serviceRepository   = serviceRepository;
     _serviceValidator    = serviceValidator;
     _memoryCache         = memoryCache;
     _serviceHealthWorker = serviceHealthWorker;
     _logReplication      = logReplication;
     _consensusContext    = consensusContext;
 }
Exemplo n.º 5
0
 public VoteReceived(IObserver<RequestVoteResultMessage> voteReply,
     ServerIdentifier serverIdentifier,
     IObservable<RequestVoteMessage> requestVote, 
     Election election,
     ILoggerFactory loggerFactory,
     Nodes nodes,
     RaftOptions options, 
     ILogReplication logReplication)
 {
     _voteReply = voteReply;
     _election = election;
     _nodes = nodes;
     _options = options;
     _logReplication = logReplication;
     _serverIdentifier = serverIdentifier;
     _voteReceivedDisposable = requestVote.Subscribe(VoteRequestReceived);
     _logger = loggerFactory.CreateLogger(nameof(VoteReceived) + " " + serverIdentifier);
 }
Exemplo n.º 6
0
 public AppendEntry(IObserver<AppendEntryResultMessage> entryReply,
    IObservable<AppendEntryMessage> receivedAppendEntryMessage,
    IHartbeatTimer hartbeat,
    ILogReplication logReplication, 
    Election election,
    ILoggerFactory loggerFactory,
    RaftOptions options, 
    ServerIdentifier serverIdentifier,
    IEnumerable<IRaftService> services)
 {
     _entryReply = entryReply;
     _hartbeat = hartbeat;
     _receivedDisposable =  receivedAppendEntryMessage.Subscribe(RaciveHartbeat);
     _logReplication = logReplication;
     _election = election;
     _options = options;
     _serverIdentifier = serverIdentifier;
     _services = services;
     _logger = loggerFactory.CreateLogger(nameof(AppendEntry) + " " + serverIdentifier);
 }
Exemplo n.º 7
0
        public ServiceHealthWorker(
            IRepository <Service> serviceRepository,
            IRepository <ServiceHealth> serviceHealthRepository,
            ILogger <ServiceHealthWorker> logger,
            IMemoryCache memoryCache,
            IApplicationLifetime applicationLifetime,
            HttpClient httpClient,
            ILogReplication logReplication)
        {
            _serviceRepository       = serviceRepository;
            _serviceHealthRepository = serviceHealthRepository;
            _logger              = logger;
            _memoryCache         = memoryCache;
            _applicationLifetime = applicationLifetime;
            _httpClient          = httpClient;
            _logReplication      = logReplication;

            _applicationLifetime.ApplicationStopping.Register(DisposeResources);
            _tokenSources = new ConcurrentDictionary <string, CancellationTokenSource>();
        }
Exemplo n.º 8
0
        public Node(IVoteReceived voteReceived, 
            IAppendEntry appendEntry, 
            IHartbeatTimer hartbeat, 
            ILoggerFactory loggerFactory, 
            IObservableProvider observableProvider,
            ILogReplication logReplication,
            ServerIdentifier serverIdentifier)
        {
            _logReplication = logReplication;
            _serverIdentifier = serverIdentifier;
            _hartbeatTimer = hartbeat;

            _stateMachine = new Machine<ServerStateType, ServerStateType>(ServerStateType.Candidate,
                loggerFactory, observableProvider);

            _stateMachine.On(ServerStateType.Follower)
                .OnEntry(() =>
                {
                    hartbeat.Subscribe(_stateMachine);
                })
                .OnEntrySubscribe<Follower>()
                .Permit(ServerStateType.Candidate, ServerStateType.Candidate);

            _stateMachine.On(ServerStateType.Candidate)
                .OnEntrySubscribe<Candidate>()
                .OnEntry(hartbeat.Reset)
                .Permit(ServerStateType.Follower, ServerStateType.Follower)
                .Permit(ServerStateType.Leader, ServerStateType.Leader)
                .Permit(ServerStateType.Candidate, ServerStateType.Candidate);

            _stateMachine.On(ServerStateType.Leader)
                .OnEntrySubscribe<Leader>()
                .Permit(ServerStateType.Follower, ServerStateType.Follower);

            _voteReceivedeDispose = voteReceived.Subscribe(_stateMachine);
            _appendEntryDispose = appendEntry.Subscribe(_stateMachine);
            _stateMachine.OnNext(ServerStateType.Follower);
        }
Exemplo n.º 9
-1
        public LeaderBaseTests()
        {
            var mocker = new AutoMocker();

            ServerIdentifier = new ServerIdentifier();
            mocker.Use(ServerIdentifier);

            Election = new Election();
            AppendEntryResult = new Subject<AppendEntryResultMessage>();
            AppendEntry = new Subject<AppendEntryMessage>();
            LogReplication = new LogReplication();
            Nodes = new Nodes();

            Leader = new Rafting.States.Leader(
                Election,
                mocker.Get<IHartbeatTimer>(),
                AppendEntryResult,
                AppendEntry,
                mocker.Get<IObserver<ClientResultMessage>>(),
                mocker.Get<IObservable<ClientMessage>>(),
                LogReplication,
                Nodes,
                mocker.Get<ILoggerFactory>(),
                new RaftOptions(),
                mocker.Get<ServerIdentifier>());
        }
Exemplo n.º 10
-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);
        }