Search code examples
c#algorithmmaster-slave

An algorithm for setting one server as the master among several?


Wondering if anyone out there has any advice or examples on a good algorithm for guaranteeing one and only one server be set as the master among several that start up at the same time?

What I have come up with is the following, but the snag is with the last question below:

  1. On startup a small service runs that gets host name, and writes it to a file on a shared network drive.
  2. The machine that comes up last will be the the winner and therefore be set as the master.
  3. If any machine goes down and comes up again, it will be the master.
  4. But what happens if the master goes down and doesn't come back up?

Or is this all bad to begin with? Is there a better way to go about it?


Solution

  • There are various solutions to this problem. If you are assuming that a shared file system is always available, then that simplifies the problem.

    • Each time a computer comes up, it checks the "master" file on the shared system.
    • If none is there, it creates the master file and writes its name to it.
    • If one is there, it checks to see if the master hostname is up. If the master hostname is up, it then does nothing.
    • If the master hostname is not up, it writes its name to the master file and it is now the master.
    • Before any machine does any "master" operation, it checks the shared hostname to be sure it is the master.

    This is not a fool-proof solution, because it relies on the file system to control access to the file (two computers coming up my both try to write to the file at the same time). However, because the master file should only have one hostname in it, the final step should guarantee that only one computer really thinks its the master.

    The problem is a bit harder without the assumption of an up-all-the-time shared file system.