public static IApplicationBuilder UseRafty(this IApplicationBuilder builder,
                                                   Uri baseUri,
                                                   IMessageSender messageSender,
                                                   IMessageBus messageBus,
                                                   IStateMachine stateMachine,
                                                   IServiceRegistry serviceRegistry,
                                                   ILoggerFactory loggerFactory,
                                                   IServersInCluster serversInCluster,
                                                   string raftyBasePath = null)
        {
            builder.UseRaftyForTesting(baseUri, messageSender, messageBus, stateMachine, serviceRegistry,
                                       loggerFactory, serversInCluster, raftyBasePath);

            return(builder);
        }
示例#2
0
文件: Server.cs 项目: falps/Rafty
 public Server(IMessageBus messageBus,
               IServersInCluster serversInCluster,
               IStateMachine stateMachine,
               ILoggerFactory loggerFactory)
 {
     _stateMachine = stateMachine;
     _logger       = loggerFactory.CreateLogger <Server>();
     _messageBus   = messageBus;
     _serversInClusterInCluster = serversInCluster;
     Id         = Guid.NewGuid();
     Log        = new List <Log>();
     NextIndex  = new List <Next>();
     MatchIndex = new List <Match>();
     State      = new Follower();
     SendElectionTimeoutMessage(1);
 }
示例#3
0
 public RequestVoteTests()
 {
     _serversInCluster = new InMemoryServersInCluster();
 }
示例#4
0
 public AppendEntriesTests()
 {
     _serversInCluster = new InMemoryServersInCluster();
 }
示例#5
0
 public AllServersTests()
 {
     _messageBus       = new Mock <IMessageBus>();
     _serversInCluster = new InMemoryServersInCluster();
 }
示例#6
0
 public FollowerTests()
 {
     _messageBusMock   = new Mock <IMessageBus>();
     _serversInCluster = new InMemoryServersInCluster();
 }
示例#7
0
 public AcceptanceTestsSteps()
 {
     _serversInCluster = new InMemoryServersInCluster();
     _serviceRegistry  = new ServiceRegistry();
     _servers          = new List <ServerContainer>();
 }
        public static (IApplicationBuilder builder, Server server, ServerInCluster serverInCluster) UseRaftyForTesting(this IApplicationBuilder builder,
                                                                                                                       Uri baseUri,
                                                                                                                       IMessageSender messageSender,
                                                                                                                       IMessageBus messageBus,
                                                                                                                       IStateMachine stateMachine,
                                                                                                                       IServiceRegistry serviceRegistry,
                                                                                                                       ILoggerFactory loggerFactory,
                                                                                                                       IServersInCluster serversInCluster,
                                                                                                                       string raftyBasePath = null)
        {
            var urlConfig = RaftyUrlConfig.Get(raftyBasePath);

            var server = new Server(messageBus, serversInCluster, stateMachine, loggerFactory);
            var logger = loggerFactory.CreateLogger <IApplicationBuilder>();

            serviceRegistry.Register(new RegisterService(RaftyServiceDiscoveryName.Get(), server.Id, baseUri));

            messageSender.SetServer(server);

            var serverInCluster = new ServerInCluster(server.Id);

            serversInCluster.Add(serverInCluster);

            builder.Map(urlConfig.appendEntriesUrl, app =>
            {
                app.Run(async context =>
                {
                    try
                    {
                        var reader        = new StreamReader(context.Request.Body);
                        var content       = reader.ReadToEnd();
                        var appendEntries = JsonConvert.DeserializeObject <AppendEntries>(content, new JsonSerializerSettings
                        {
                            TypeNameHandling = TypeNameHandling.All
                        });
                        var appendEntriesResponse = await server.Receive(appendEntries);
                        await context.Response.WriteAsync(JsonConvert.SerializeObject(appendEntriesResponse));
                    }
                    catch (Exception exception)
                    {
                        logger.LogError(new EventId(1), exception, $"There was an error handling {urlConfig.appendEntriesUrl}");
                    }
                });
            });

            builder.Map(urlConfig.requestVoteUrl, app =>
            {
                app.Run(async context =>
                {
                    try
                    {
                        var reader      = new StreamReader(context.Request.Body);
                        var content     = reader.ReadToEnd();
                        var requestVote = JsonConvert.DeserializeObject <RequestVote>(content, new JsonSerializerSettings
                        {
                            TypeNameHandling = TypeNameHandling.All
                        });
                        var requestVoteResponse = server.Receive(requestVote);
                        await context.Response.WriteAsync(JsonConvert.SerializeObject(requestVoteResponse));
                    }
                    catch (Exception exception)
                    {
                        logger.LogError(new EventId(1), exception, $"There was an error handling {urlConfig.requestVoteUrl}");
                    }
                });
            });

            builder.Map(urlConfig.commandUrl, app =>
            {
                app.Run(async context =>
                {
                    try
                    {
                        var reader  = new StreamReader(context.Request.Body);
                        var content = reader.ReadToEnd();
                        var command = JsonConvert.DeserializeObject <Command>(content, new JsonSerializerSettings
                        {
                            TypeNameHandling = TypeNameHandling.All
                        });
                        var sendCommandToLeaderResponse = await server.Receive(command);
                        await context.Response.WriteAsync(JsonConvert.SerializeObject(sendCommandToLeaderResponse));
                    }
                    catch (Exception exception)
                    {
                        logger.LogError(new EventId(1), exception, $"There was an error handling {urlConfig.commandUrl}");
                    }
                });
            });

            var applicationLifetime = builder.ApplicationServices.GetRequiredService <IApplicationLifetime>();

            applicationLifetime.ApplicationStopping.Register(() => OnStopping(builder.ApplicationServices));

            applicationLifetime.ApplicationStopped.Register(() => OnStopped(builder.ApplicationServices));

            return(builder, server, serverInCluster);
        }
 public InitialServerStateTests()
 {
     _serversInCluster = new InMemoryServersInCluster();
 }