Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feture/fix lease keepalive2 #3

Draft
wants to merge 19 commits into
base: master-for-build
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions DevelopmentSandbox/DevelopmentSandbox.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Deeplay.DotnetNiceGrpcLogs" Version="1.5.1" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\dotnet-etcd\dotnet-etcd.csproj" />
</ItemGroup>

</Project>
96 changes: 96 additions & 0 deletions DevelopmentSandbox/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Net;
using System.Runtime.CompilerServices;
using dotnet_etcd;
using DotnetNiceGrpcLogs;
using Etcdserverpb;
using Grpc.Core;
using Polly;
using Polly.Contrib.WaitAndRetry;
using Polly.Timeout;
using Polly.Utilities;
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;

namespace DevelopmentSandbox // Note: actual namespace depends on the project name.
{
internal class Program
{
static async Task Main(string[] args)
{
var cts = new CancellationTokenSource();
AppDomain.CurrentDomain.ProcessExit += (_, _) => { cts.Cancel(); };
Console.CancelKeyPress += (_, ea) => { cts.Cancel(); };
ILogger logger = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.Console(
theme: SystemConsoleTheme.Literate,
outputTemplate:
"[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj}{NewLine}{Exception}{Properties:j}{NewLine}")
.Enrich.FromLogContext()
.CreateLogger();

string connection_string = Environment.GetEnvironmentVariable("ETCD_CONNECTION_STRING");
EtcdClient client = new EtcdClient(
connection_string,
// handler: handler,
useLegacyRpcExceptionForCancellation: false //,
// interceptors: new GrpcLogsInterceptor(
// logger,
// new LogsInterceptorOptions
// {
// //LoggerName = null,
// IncludeLogData = true
// })
);

Func<Task> doJob = async () =>
{
var leaseId = client.LeaseGrant(new LeaseGrantRequest() { TTL = 5 }).ID;
await client.HighlyReliableLeaseKeepAliveAsync(
leaseId,
5,
retryDurationMs: 1000,
maxRetryBackoffMs: 400,
sleepAfterSuccessMs: 5000 / 3,
cts.Token).ConfigureAwait(false);

// await client.LeaseKeepAlive(
// leaseId,
// CancellationToken.None).ConfigureAwait(false);
};

List<Task> jobs = new List<Task>(1000);

foreach (var i in Enumerable.Range(
0,
20000))
{

await Task.Delay(5); //что бы кипалайвы были в приоритете создания новых тасков
if (cts.Token.IsCancellationRequested)
{
break;
}

var t = Task.Run(
async () =>
{
cts.Token.ThrowIfCancellationRequested();
Console.WriteLine(i);
try
{
await doJob().ConfigureAwait(false);
}
finally
{
cts.Cancel();
}
},
cts.Token);
jobs.Add(t);
}

await await Task.WhenAny(jobs);
logger.Information("endddd");

}
}
};
11 changes: 11 additions & 0 deletions DevelopmentSandbox/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"DevelopmentSandbox": {
"commandName": "Project",
"environmentVariables": {
"ETCD_CONNECTION_STRING": "http://127.0.0.1:23790,http://127.0.0.1:23791,http://127.0.0.1:23792"
}
}
}
}
54 changes: 54 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
version: '3.5'

services:
etcd0:
image: "gcr.io/etcd-development/etcd:v3.5.0-beta.4"
ports:
- "23790:2379"
- "2379:2379"
command:
[
"etcd",
"--name=etcd0",
"--advertise-client-urls=http://etcd0:2379",
"--listen-client-urls=http://0.0.0.0:2379",
"--initial-advertise-peer-urls=http://etcd0:2380",
"--listen-peer-urls=http://0.0.0.0:2380",
"--initial-cluster-token=etcd-cluster-1",
"--initial-cluster=etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380",
"--initial-cluster-state=new"
]

etcd1:
image: "gcr.io/etcd-development/etcd:v3.5.0-beta.4"
ports:
- "23791:2379"
command:
[
"etcd",
"--name=etcd1",
"--advertise-client-urls=http://etcd1:2379",
"--listen-client-urls=http://0.0.0.0:2379",
"--initial-advertise-peer-urls=http://etcd1:2380",
"--listen-peer-urls=http://0.0.0.0:2380",
"--initial-cluster-token=etcd-cluster-1",
"--initial-cluster=etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380",
"--initial-cluster-state=new"
]

etcd2:
image: "gcr.io/etcd-development/etcd:v3.5.0-beta.4"
ports:
- "23792:2379"
command:
[
"etcd",
"--name=etcd2",
"--advertise-client-urls=http://etcd2:2379",
"--listen-client-urls=http://0.0.0.0:2379",
"--initial-advertise-peer-urls=http://etcd2:2380",
"--listen-peer-urls=http://0.0.0.0:2380",
"--initial-cluster-token=etcd-cluster-1",
"--initial-cluster=etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380",
"--initial-cluster-state=new"
]
6 changes: 6 additions & 0 deletions dotnet-etcd.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{8645A28E
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Integration", "tests\Integration\Integration.csproj", "{617202A0-4D3A-4FA7-978E-B5A01A3152BD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevelopmentSandbox", "DevelopmentSandbox\DevelopmentSandbox.csproj", "{41EEF9B2-F4CE-4C28-A2C1-1525AD32729C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -23,6 +25,10 @@ Global
{617202A0-4D3A-4FA7-978E-B5A01A3152BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{617202A0-4D3A-4FA7-978E-B5A01A3152BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{617202A0-4D3A-4FA7-978E-B5A01A3152BD}.Release|Any CPU.Build.0 = Release|Any CPU
{41EEF9B2-F4CE-4C28-A2C1-1525AD32729C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41EEF9B2-F4CE-4C28-A2C1-1525AD32729C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41EEF9B2-F4CE-4C28-A2C1-1525AD32729C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41EEF9B2-F4CE-4C28-A2C1-1525AD32729C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
42 changes: 42 additions & 0 deletions dotnet-etcd/LeaseExpiredOrNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.Serialization;

namespace dotnet_etcd
{
[Serializable]
public class LeaseExpiredOrNotFoundException : Exception
{
private readonly long _leaseId;

public LeaseExpiredOrNotFoundException(long leaseId) : base("LeaseExpiredOrNotFoundException: leaseId=" + leaseId)
{
_leaseId = leaseId;
}

public LeaseExpiredOrNotFoundException(long leaseId, string message) : base(message)
{
_leaseId = leaseId;
}

public LeaseExpiredOrNotFoundException(long leaseId, string message, Exception inner) : base(
message,
inner)
{
_leaseId = leaseId;
}

protected LeaseExpiredOrNotFoundException(
SerializationInfo info,
StreamingContext context) : base(
info,
context)
{
info.AddValue(
name: "leaseId",
value: _leaseId);
}
}
}
3 changes: 3 additions & 0 deletions dotnet-etcd/dotnet-etcd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Advanced uses take advantage of the consistency guarantees to implement database
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />

<Protobuf Include="grpc\proto\etcd\auth.proto" GrpcServices="Client" />
<Protobuf Include="grpc\proto\etcd\election.proto" GrpcServices="Client" />
Expand Down
2 changes: 1 addition & 1 deletion dotnet-etcd/etcdClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public partial class EtcdClient : IDisposable, IEtcdClient
#region Initializers

public EtcdClient(string connectionString, int port = 2379,
HttpClientHandler handler = null, bool ssl = false,
HttpMessageHandler handler = null, bool ssl = false,
bool useLegacyRpcExceptionForCancellation = false, params Interceptor[] interceptors)
{
if (string.IsNullOrWhiteSpace(connectionString))
Expand Down
Loading