public DqliteConnector(DqliteConnectionStringBuilder settings, bool connectLeader) { this.Settings = settings; this.connectLeader = connectLeader; this.attempts = 1000; this.delay = 250; }
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(); var connectionOptions = new DqliteConnectionStringBuilder() { Nodes = this.store.Get() }; using (var client = new DqliteClient(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, DqliteNodeRoles.Voter, 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); }
public void Test(string connectionString, string[] nodes, string dataSource, bool?tcpKeepAlive, int?socketReceiveBufferSize, int?socketSendBufferSize) { var builder = new DqliteConnectionStringBuilder(); if (nodes != null) { builder.Nodes = nodes; } if (dataSource != null) { builder.DataSource = dataSource; } if (tcpKeepAlive != null) { builder.TcpKeepAlive = tcpKeepAlive.Value; } if (socketReceiveBufferSize != null) { builder.SocketReceiveBufferSize = socketReceiveBufferSize.Value; } if (socketSendBufferSize != null) { builder.SocketSendBufferSize = socketSendBufferSize.Value; } Assert.Equal(connectionString, builder.ToString()); }
public async Task Test1Async(int rows) { var builder = new DqliteConnectionStringBuilder() { Nodes = new [] { "127.0.0.1:6543" }, DataSource = "main" }; using (var connection = new DqliteConnection(builder.ToString())) { await connection.OpenAsync(); using (var command = new DqliteCommand() { Connection = connection }) { command.CommandText = "CREATE TABLE IF NOT EXISTS Sample(n INT);"; await command.ExecuteNonQueryAsync(); command.CommandText = "DELETE FROM Sample;"; await command.ExecuteNonQueryAsync(); command.CommandText = "INSERT INTO Sample(n) VALUES(@value);"; await command.PrepareAsync(); for (int i = 0; i < rows; ++i) { command.Parameters.AddWithValue("@value", i); await command.ExecuteNonQueryAsync(); command.Parameters.Clear(); } command.CommandText = "SELECT count(0) FROM Sample;"; await using (var reader = await command.ExecuteReaderAsync()) { Assert.True(await reader.ReadAsync()); Assert.Equal(1, reader.FieldCount); Assert.Equal(rows, reader.GetInt64(0)); Assert.False(await reader.ReadAsync()); } command.CommandText = "SELECT n FROM Sample;"; await using (var reader = await command.ExecuteReaderAsync()) { for (int i = 0; i < rows; ++i) { Assert.True(await reader.ReadAsync()); Assert.Equal(i, reader.GetInt64(0)); } Assert.False(await reader.ReadAsync()); } } } }
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); } }
public DqliteClient(DqliteConnectionStringBuilder settings, bool ConnectLeader = false) { this.connector = new DqliteConnector(settings, ConnectLeader); }
public async Task ClusterTest() { var dataDir = Directory.CreateDirectory( Path.Combine(Path.GetTempPath(), "dqlite_tests_" + Guid.NewGuid())); try { DqliteConnectionStringBuilder builder = new DqliteConnectionStringBuilder(); builder.Nodes = new string[] { "127.0.0.1:5001", "127.0.0.1:5002", "127.0.0.1:5003" }; using (var node01 = new NodeProcess(1, "127.0.0.1:5001", Path.Combine(dataDir.FullName, "1"))) using (var node02 = new NodeProcess(2, "127.0.0.1:5002", Path.Combine(dataDir.FullName, "2"))) using (var node03 = new NodeProcess(3, "127.0.0.1:5003", Path.Combine(dataDir.FullName, "3"))) { Assert.False(node01.HasExited); Assert.False(node02.HasExited); Assert.False(node03.HasExited); await Task.Delay(10000); using (var client = new DqliteClient(builder)) { await client.ConnectAsync(); await client.AddNodeAsync(2, "127.0.0.1:5002"); Assert.Equal(1ul, (await client.GetLeaderAsync()).Id); await client.PromoteNodeAsync(2); await client.AddNodeAsync(3, "127.0.0.1:5003"); await client.PromoteNodeAsync(3); var nodes = await client.GetNodesAsync(); Assert.Equal(3, nodes.Count()); } using (var client = new DqliteClient(builder)) { await client.ConnectAsync(); var leader = await client.GetLeaderAsync(); var nodes = await client.GetNodesAsync(); Assert.Equal(3, nodes.Count()); } using (var client = new DqliteClient(builder)) { await client.ConnectAsync(); var nodes = await client.GetNodesAsync(); Assert.Equal(3, nodes.Count()); } node01.Kill(); using (var cts = new CancellationTokenSource()) { cts.CancelAfter(30 * 1000); using (var client = new DqliteClient(builder)) { var nodes = await client.GetNodesAsync(); Assert.Equal(3, nodes.Count()); } } using (var cts = new CancellationTokenSource()) { cts.CancelAfter(30 * 1000); using (var client = new DqliteClient(builder)) { var nodes = await client.GetNodesAsync(); Assert.Equal(3, nodes.Count()); } } using (var cts = new CancellationTokenSource()) { cts.CancelAfter(30 * 1000); using (var newNode01 = new NodeProcess(1, "127.0.0.1:5004", Path.Combine(dataDir.FullName, "4"))) using (var client = new DqliteClient(builder)) { Console.WriteLine("Started Removing node"); await client.RemoveNodeAsync(1); Console.WriteLine("Finished Removing node"); await client.AddNodeAsync(1, "127.0.0.1:5004"); await client.PromoteNodeAsync(1); var nodes = await client.GetNodesAsync(); Assert.Equal(3, nodes.Count()); } } } } finally { dataDir.Delete(true); } }
public DqliteConnector(DqliteConnectionStringBuilder settings, bool connectLeader) { this.Settings = settings; this.connectLeader = connectLeader; }