question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Support for Leader Election

See original GitHub issue

I noticed a pattern we use occasionally at Netflix (Leader Election) wasn’t represented in this framework so I thought I’d raise an issue and outline some of the nice features that a good Spring wrapper around the Curator recipe could offer.

  • Enabling the feature
    • Suggest something like @EnableLeaderElection(zkPath=“/some/zookeeper/path”)
    • Extra nice if we can make this Repeatable or or take an array argument for zkPath to multiple leadership for multiple topics
  • Discovery/Eureka integration
    • If an application is marked out of service or unhealthy in Eureka (or just unhealthy in terms of Spring Boot HealthIndicators possibly), the application should give up its LeaderLatch so that another instance can take over. This helps to support Red/Black pushes.
    • Implementation note, when coming back into service or healthy again, we would want the application to again request leadership. This may require re-creating the Curator LeaderLatch, so that bean may not be a good fit for Singleton scope and instead may need some proxy.
  • Event Integration
    • A LeadershipEvent should be fired whenever status changes. This would be used to support batch processes.
    • Common patterns that may be useful to expose via Config are to shut down framework components such as MessagingContainers (JMS, SQS, RabbitMQ) doing queue consumption, Spring Integration channel adapters, Spring Batch jobs, @Scheduled pollers, etc. Obviously users can subscribe to the events themselves to manage each of these frameworks, but I’m considering a config pattern as follows
spring.leadership.manage.messaging=aContainerName //some specific container bean name
spring.leadership.manage.messaging=* //all the containers
spring.leadership.manage.integration=aChannel,anotherChannel,*Channel //bean names that support start/stop
spring.leadership.manage.batch=aJob, anotherJob
  • Actuator
    • An actuator for leadership that can indicate if the current instance is a leader (Helpful if status code also reflects this, 2xx for yes, something else for no)
    • Actuator should indicate what beans/channels are being governed by Leadership as in the above point
    • Actuator should accept POST requests to force an instance to relinquish leadership (a handy testing util)

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
jkschneidercommented, Sep 30, 2016

WDYT about @LeaderOnly for things like @Scheduled to use instead of a prop where possible?

2reactions
daniellavoiecommented, Feb 10, 2017

I went a little further with an implementation and realized that using Spring EventListener to react to a LeaderElection composed with a @Scheduled is something really dangerous for race conditions. The task that we wish to be executed by a single instance needs to run in a context where the lock has the highest guarantees of being active.

This is why I am proposing something like this :

  @Autowired
  private LockClient lockClient;

  @Scheduled(fixedDelay=60000)
  public void  executeSomethingImportant(){
    // Will executes the Runnable if lock is active.
    lockClient.executeIfOwned("lock-key", () -> importantService.doStuff());

    // This variant uses spring.application.name as the lock key.
    lockClient.executeIfOwned(() -> importantService.doStuff());
  }

With a LockClient providing a “safe” context to execute our tasks, we could then support an extension of @Scheduled like @LeaderScheduled. But this is tricky since it would represents rewriting a ScheduledAnnotationBeanPostProcessor specially for the @LeaderScheduled. I am not a fan. I think a simple wrap with lockClient.executeIfOwned by end users is less messy than having to fork the processor.

Edit : A working POC is available here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Leader election in distributed systems - Amazon AWS
Leader election is the simple idea of giving one thing (a process, host, thread, object, or human) in a distributed system some special...
Read more >
Leader Election pattern - Azure Architecture Center
Learn how use the Leader Election pattern to coordinate the actions performed by a collection of collaborating task instances in a distributed application....
Read more >
Simple leader election with Kubernetes and Docker
In this post we'll see how you can use Kubernetes to easily perform leader election in your distributed application.
Read more >
Project 1: Leader Election using the Raft Consensus Protocol
Once a leader gets elected, it must issue AppendEntries requests to preserve its leadership. If the current leader crashes or fails to send...
Read more >
Configuring leader election | OpenShift Container Platform 4.6
The leader pod periodically renews the leader lease and gives up leadership when it cannot renew the lease. This implementation allows for a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found