Leader election with lifetime term
A processing device receives request from a process of a plurality of processes of a clusterized service, to attempt to create, in a file system accessible by the process, a file associated with a leader state of the plurality of processes. Responsive to successfully creating the file, the processing device to enable the process to enter the leader state. The processing device is further to enable the process to stay in the leader state for a lifetime of the process.
1. A method comprising:
attempting, by a process of a plurality of processes of a clusterized service, to create, in a file system accessible by the process, a file associated with a leader state of the plurality of processes;
responsive to successfully creating the file, entering, by the process, the leader state; and
staying, by the process, in the leader state for a lifetime of the process.
2. The method of claim 1 further comprises:
responsive to failing to create the file associated with the leader state, repeating an attempt to create the file upon expiration of a timeout.
3. The method of claim 1 further comprises:
responsive to determining that the lifetime of the process has ended, deleting, by an operating system process, the file associated with the leader state from the file system.
4. The method of claim 3 , whereas the operating system process is a garbage collector process.
5. The method claim 1 , wherein the file associated with the leader state has a predetermined name and is created at a predetermined location in the file system.
6. The method of claim 1 further comprising:
responsive to entering, by the process, the leader state, denying, by an operating system process, a request from a second process of the plurality of processes to enter the leader state.
7. The method claim 1 , wherein responsive to successfully creating the file, acquiring, by the process, exclusive access to the file.
8. A system comprising:
a memory; and
a processing device operatively coupled to the memory, wherein the processing device comprises a plurality of processes of a clusterized service, and wherein a process of the plurality of processes to:
attempt to create, in a file system accessible by the process, a file associated with a leader state of the plurality of processes;
responsive to successfully creating the file, enter the leader state; and
stay in the leader state for a lifetime of the process.
9. The system of claim 8 , wherein the process is further to:
responsive to failing to create the file associated with the leader state, repeat an attempt to create the file upon expiration of a timeout.
10. The system of claim 8 , wherein the process is further to:
responsive to determining that the lifetime of the process has ended, delete, by an operating system process, the file associated with the leader state from the file system.
11. The system of claim 10 , whereas the operating system process is a garbage collector process.
12. The system of claim 8 , wherein the file associated with the leader state has a predetermined name and is created at a predetermined location in the file system.
13. The system of claim 8 , wherein the process is further to:
responsive to entering the leader state, deny, by an operating system process, a request from a second process of the plurality of processes to enter the leader state.
14. The system claim 8 , wherein the process is further to:
responsive to successfully creating the file, acquire exclusive access to the file.
15. A computer-readable non-transitory storage medium comprising executable instructions that, when executed by a processing device, cause a process of a plurality of processes running on the processing device to:
attempt to create, in a file system accessible by the process, a file associated with a leader state of the plurality of processes;
responsive to successfully creating the file, enter the leader state; and
stay in the leader state for a lifetime of the process.
16. The computer-readable non-transitory storage medium of claim 15 , wherein the process is further to:
responsive to failing to create the file associated with the leader state, repeat an attempt to create the file upon expiration of a timeout.
17. The computer-readable non-transitory storage medium of claim 15 , wherein the process is further to:
responsive to determining that the lifetime of the process has ended, delete, by an operating system process, the file associated with the leader state from the file system.
18. The computer-readable non-transitory storage medium of claim 17 , whereas the operating system process is a garbage collector process.
19. The computer-readable non-transitory storage medium of claim 15 , wherein the file associated with the leader state has a predetermined name and is created at a predetermined location in the file system.
20. The computer-readable non-transitory storage medium of claim 15 , wherein the process is further to:
responsive to entering the leader state, deny, by an operating system process, a request from a second process of the plurality of processes to enter the leader state.