Esempio n. 1
0
        public override async Task StartAsync(CancellationToken cancellationToken)
        {
            Directory.CreateDirectory(options.DataDir);
            this.node = DqliteNode.Create(options.Id, options.Address, options.DataDir, options.NodeOptions);
            this.node.Start();

            using (var client = new DqliteClient(this.options.ConnectionOptions, true))
            {
                await client.ConnectAsync(cancellationToken);

                var nodes = await client.GetNodesAsync(cancellationToken);

                var node = nodes.FirstOrDefault(x => x.Id == this.options.Id);
                if (node == null)
                {
                    await client.AddNodeAsync(this.options.Id, this.options.Address, cancellationToken);

                    await client.PromoteNodeAsync(this.options.Id, cancellationToken);
                }
                else if (node.Address != this.options.Address)
                {
                    throw new InvalidOperationException("Node with same id already exist with different address");
                }
            }
            await this.ExecuteAsync(x => x.StartAsync(cancellationToken));

            await base.StartAsync(cancellationToken);
        }
Esempio n. 2
0
 public DqliteClientTests()
 {
     this.dataDir = Directory.CreateDirectory(
         Path.Combine(Path.GetTempPath(), "dqlite_tests_" + Guid.NewGuid())).FullName;
     this.node = DqliteNode.Create(1, "127.0.0.1:6543", this.dataDir);
     this.node.Start();
 }
Esempio n. 3
0
        public static async Task Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.Error.WriteLine("Incorrect number of arguments");
                Environment.Exit(-1);
            }

            var id      = ulong.Parse(args[0]);
            var address = args[1];
            var dataDir = args[2];

            if (!Directory.Exists(dataDir))
            {
                Directory.CreateDirectory(dataDir);
            }

            var tcs = new TaskCompletionSource <bool>();

            Console.CancelKeyPress += (sender, args2) =>
            {
                args2.Cancel = true;
                tcs.TrySetResult(true);
            };

            using (var node = DqliteNode.Create(id, address, dataDir))
            {
                node.Start();
                await tcs.Task;
            }
        }
Esempio n. 4
0
        public async Task FailOverTest()
        {
            var dataDir = Directory.CreateDirectory(
                Path.Combine(Path.GetTempPath(), "dqlite_tests_" + Guid.NewGuid()));

            try
            {
                var builder = new DqliteConnectionStringBuilder();
                builder.Nodes = new string[] { "127.0.0.1:5001", "127.0.0.1:5002", "127.0.0.1:5003" };
                using (var node02 = DqliteNode.Create(2, "127.0.0.1:5002", Path.Combine(dataDir.FullName, "2")))
                    using (var node03 = DqliteNode.Create(3, "127.0.0.1:5003", Path.Combine(dataDir.FullName, "3")))
                    {
                        node02.Start();
                        node03.Start();
                        using (var node01 = DqliteNode.Create(1, "127.0.0.1:5001", Path.Combine(dataDir.FullName, "1")))
                        {
                            node01.Start();
                            using (var client = new DqliteClient(builder, true))
                            {
                                await client.ConnectAsync();

                                await client.AddNodeAsync(2, "127.0.0.1:5002", DqliteNodeRoles.Voter);

                                await client.AddNodeAsync(3, "127.0.0.1:5003", DqliteNodeRoles.Voter);

                                var leader = await client.GetLeaderAsync();

                                var nodes = await client.GetNodesAsync();

                                Assert.Equal(1UL, leader.Id);
                                Assert.Equal(3, nodes.Length);
                            }
                        }

                        using (var cts = new CancellationTokenSource())
                            using (var client = new DqliteClient(builder, true))
                            {
                                cts.CancelAfter(TimeSpan.FromMinutes(1));
                                await client.ConnectAsync(cts.Token);

                                var leader = await client.GetLeaderAsync();

                                var nodes = await client.GetNodesAsync();

                                Assert.NotEqual(1UL, leader.Id);
                                Assert.Equal(3, nodes.Count());
                            }
                    }
            }
            finally
            {
                dataDir.Delete(true);
            }
        }