Esempio n. 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }


            var log      = new InMemoryLog();
            var fsm      = new InMemoryStateMachine();
            var settings = new InMemorySettings(1000, 3500, 50, 5000);

            var _peers = new List <IPeer>();
            var peer1  = new NodePeer();

            _peers.Add(new NodePeer()
            {
            });
            var peersProvider = new InMemoryPeersProvider(_peers);
            var node          = new Node(fsm, log, settings, peersProvider, loggerFactory);

            node.Start(new NodeId("gsw" + DateTime.Now.ToString("HHmmssfff")));

            app.UseMvc();
        }
        public async Task CandidateShouldApplyLogsToFsm()
        {
            var currentState  = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string));
            var fsm           = new InMemoryStateMachine();
            var candidate     = new Candidate(currentState, fsm, _peers, _log, _random, _node, _settings, _rules, _loggerFactory.Object);
            var log           = new LogEntry(new FakeCommand("test"), typeof(string), 1);
            var appendEntries = new AppendEntriesBuilder()
                                .WithTerm(1)
                                .WithPreviousLogTerm(1)
                                .WithEntry(log)
                                .WithPreviousLogIndex(1)
                                .WithLeaderCommitIndex(1)
                                .Build();
            await _log.Apply(log);

            var appendEntriesResponse = await candidate.Handle(appendEntries);

            appendEntriesResponse.Success.ShouldBeTrue();
            candidate.CurrentState.CurrentTerm.ShouldBe(1);
            candidate.CurrentState.LastApplied.ShouldBe(1);
            fsm.HandledLogEntries.ShouldBe(1);
            var node = (NothingNode)_node;

            node.BecomeFollowerCount.ShouldBe(1);
        }
Esempio n. 3
0
        private void StartServer(int index)
        {
            var log           = new InMemoryLog();
            var fsm           = new InMemoryStateMachine();
            var settings      = new InMemorySettingsBuilder().WithMinTimeout(1000).WithMaxTimeout(3500).WithHeartbeatTimeout(50).Build();
            var peersProvider = new InMemoryPeersProvider(_peers);
            var node          = new Node(fsm, log, settings, peersProvider);
            var server        = new Server(log, fsm, node);

            _servers.TryAdd(index, server);
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            var log = new InMemoryLog();

            var fsm      = new InMemoryStateMachine();
            var settings = new InMemorySettings(1000, 3500, 50, 5000);

            var _peers        = new List <IPeer>();
            var peersProvider = new InMemoryPeersProvider(_peers);
            var node          = new Node(fsm, log, settings, peersProvider, null);

            node.Start(new NodeId("gsw"));
        }
Esempio n. 5
0
        private void StartServer(int index)
        {
            var log           = new InMemoryLog();
            var fsm           = new InMemoryStateMachine();
            var settings      = new InMemorySettingsBuilder().WithMinTimeout(1000).WithMaxTimeout(3500).WithHeartbeatTimeout(50).Build();
            var peersProvider = new InMemoryPeersProvider(_peers);

            var logger        = new Mock <ILogger>();
            var loggerFactory = new Mock <ILoggerFactory>();

            loggerFactory.Setup(x => x.CreateLogger(It.IsAny <string>())).Returns(logger.Object);

            var node   = new Node(fsm, log, settings, peersProvider, loggerFactory.Object);
            var server = new Server(log, fsm, node);

            _servers.TryAdd(index, server);
        }
Esempio n. 6
0
 private void AddNewServers(int count)
 {
     for (int i = 0; i < count; i++)
     {
         var peer = new NodePeer();
         _peers.Add(peer);
         var log           = new InMemoryLog();
         var fsm           = new InMemoryStateMachine();
         var settings      = new InMemorySettingsBuilder().WithMinTimeout(1000).WithMaxTimeout(3500).WithHeartbeatTimeout(50).Build();
         var peersProvider = new InMemoryPeersProvider(_peers);
         var node          = new Node(fsm, log, settings, peersProvider);
         var server        = new Server(log, fsm, node);
         peer.SetNode(server.Node);
         var nextIndex = _servers.Count;
         _servers.TryAdd(nextIndex, server);
         node.Start(Guid.NewGuid().ToString());
     }
 }
Esempio n. 7
0
        private void AddNewServers(int count)
        {
            for (int i = 0; i < count; i++)
            {
                var peer = new NodePeer();
                _peers.Add(peer);
                var log           = new InMemoryLog();
                var fsm           = new InMemoryStateMachine();
                var settings      = new InMemorySettingsBuilder().WithMinTimeout(1000).WithMaxTimeout(3500).WithHeartbeatTimeout(50).Build();
                var peersProvider = new InMemoryPeersProvider(_peers);

                var logger        = new Mock <ILogger>();
                var loggerFactory = new Mock <ILoggerFactory>();
                loggerFactory.Setup(x => x.CreateLogger(It.IsAny <string>())).Returns(logger.Object);

                var node   = new Node(fsm, log, settings, peersProvider, loggerFactory.Object);
                var server = new Server(log, fsm, node);
                peer.SetNode(server.Node);
                var nextIndex = _servers.Count;
                _servers.TryAdd(nextIndex, server);
                node.Start(new NodeId(Guid.NewGuid().ToString()));
            }
        }
Esempio n. 8
0
 public Server(InMemoryLog log, InMemoryStateMachine fsm, Node node)
 {
     this.Log  = log;
     this.Fsm  = fsm;
     this.Node = node;
 }