sync icon indicating copy to clipboard operation
sync copied to clipboard

Leadership election and Cancellation token in dotnet

Open lawrencegripper opened this issue 7 years ago • 1 comments

I was wondering what your thoughts would be on passing a cancellation token to each of the funcs in the .net implementation. This can then be checked, while the code is running, to understand if leadership has been lost.

I took a look at implementing leadership election in dotnetcore a while back using etcd and used this pattern. Full code here, simple example below.

            var election = new ElectionRunner(
                isNowMaster: (cancellationToken) =>{
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        Console.WriteLine("We're Master!!");
                        Task.Delay(TimeSpan.FromSeconds(3)).Wait();
                    }
                },
                isNowSecondary: (cancellationToken) =>{
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        Console.WriteLine("We're secondary");
                        Task.Delay(TimeSpan.FromSeconds(3)).Wait();                        
                    }
                },
                electionTimeoutSec: 15);

I'm unclear on how feasible this is in the Metaparticle code but if there is agreement that it is an worthwhile addition I can investigate further and hopefully create a PR.

lawrencegripper avatar Jan 04 '18 09:01 lawrencegripper

Under what circumstances would the cancellation be requested? Is this so that as a user that is currently the master, we can request a cancellation of that mastering?

If so, I think that's a great idea, but I think you would pass that to the Election rather than to the isNowMaster function...

I could definitely be missing something here, so please discuss more.

brendandburns avatar Jan 05 '18 20:01 brendandburns