Ejemplo n.º 1
0
        private static async Task UseConfiguration(RaftCluster.NodeConfiguration config, string?persistentStorage)
        {
            config.Members.Add(new IPEndPoint(IPAddress.Loopback, 3262));
            config.Members.Add(new IPEndPoint(IPAddress.Loopback, 3263));
            config.Members.Add(new IPEndPoint(IPAddress.Loopback, 3264));
            var loggerFactory = new LoggerFactory();
            var loggerOptions = new ConsoleLoggerOptions
            {
                LogToStandardErrorThreshold = LogLevel.Warning
            };

            loggerFactory.AddProvider(new ConsoleLoggerProvider(new FakeOptionsMonitor <ConsoleLoggerOptions>(loggerOptions)));
            config.LoggerFactory = loggerFactory;

            using var cluster      = new RaftCluster(config);
            cluster.LeaderChanged += ClusterConfigurator.LeaderChanged;
            var modifier = default(DataModifier?);

            if (!string.IsNullOrEmpty(persistentStorage))
            {
                var state = new SimplePersistentState(persistentStorage);
                cluster.AuditTrail = state;
                modifier           = new DataModifier(cluster, state);
            }
            await cluster.StartAsync(CancellationToken.None);

            await(modifier?.StartAsync(CancellationToken.None) ?? Task.CompletedTask);
            using var handler       = new CancelKeyPressHandler();
            Console.CancelKeyPress += handler.Handler;
            await handler.WaitAsync();

            Console.CancelKeyPress -= handler.Handler;
            await(modifier?.StopAsync(CancellationToken.None) ?? Task.CompletedTask);
            await cluster.StopAsync(CancellationToken.None);
        }
Ejemplo n.º 2
0
        // Creates and returns a configured array of raftnodes using the test cluster
        static internal RaftNode[] ConfigureRaftCluster(int numberOfNodes, SM sm)
        {
            RaftNode[] nodes = new RaftNode[numberOfNodes];

            // Create nodes
            for (uint i = 0; i < numberOfNodes; i++)
            {
                if (sm == SM.Numeral)
                {
                    nodes[i] = new RaftNode(i, new NumeralStateMachine());
                }
                else
                {
                    nodes[i] = new RaftNode(i, new DictionaryStateMachine());
                }
            }

            // Adding them to a cluster and configuring them
            foreach (RaftNode node in nodes)
            {
                var c = new RaftCluster();
                Array.ForEach(nodes, x => c.AddNode(new ObjectRaftConnector(x.NodeId, x)));
                node.Configure(c);
            }

            return(nodes);
        }
Ejemplo n.º 3
0
        public ClusterService()
        {
            var numberOfNodes = 5;
            var _nodes        = new List <RaftNode>();

            // Create nodes
            for (uint i = 0; i < numberOfNodes; i++)
            {
                var node = new RaftNode(i, new NumeralStateMachine());
                _nodes.Add(node);
            }

            // Adding them to a cluster and configuring them
            foreach (RaftNode node in _nodes)
            {
                var c = new RaftCluster();
                _nodes.ForEach(x => c.AddNode(new ObjectRaftConnector(x.NodeId, x)));
                node.Configure(c);
            }

            this.cluster = _nodes[0].Cluster;

            _nodes.ForEach(node => node.Run());

            this.nodes = _nodes;
        }
Ejemplo n.º 4
0
        // Creates a single-node cluster with a numeral state machine and returns the node
        static internal RaftNode CreateNode()
        {
            RaftNode node = new RaftNode(1, new NumeralStateMachine());
            var      c    = new RaftCluster();

            c.AddNode(new ObjectRaftConnector(node.NodeId, node));
            node.Configure(c);
            return(node);
        }
Ejemplo n.º 5
0
 public RaftService(IClient client) : base(client)
 {
     AddLearner    = new RaftAddLearner(client);
     AddPeer       = new RaftAddPeer(client);
     AddCluster    = new RaftCluster(client);
     Leader        = new RaftLeader(client);
     PromoteToPeer = new RaftPromoteToPeer(client);
     RemovePeer    = new RaftRemovePeer(client);
     Role          = new RaftRole(client);
 }
Ejemplo n.º 6
0
        private static RaftNode StartNode(Configuration conf)
        {
            var id   = conf.CurrentNode.Id;
            var node = new RaftNode(id, new RaftCore.StateMachine.Implementations.NumeralStateMachine());
            var clusterConfiguration = new RaftCluster();

            foreach (var anotherNode in conf.Nodes)
            {
                clusterConfiguration.AddNode(new APIRaftConnector(anotherNode.Id, anotherNode.BaseUrl + "/RaftApi"));
            }
            node.Configure(clusterConfiguration);
            node.Run();
            return(node);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Configures the node: adds the cluster object, calculates the election timeout and sets the initial state to Follower.
 /// </summary>
 /// <param name="cluster"><see cref="RaftCluster"/> instance containing all the nodes in the cluster</param>
 public void Configure(RaftCluster cluster)
 {
     this.Cluster           = cluster;
     this.ElectionTimeoutMS = Cluster.CalculateElectionTimeoutMS();
     this.NodeState         = NodeState.Follower;
 }