tags:

views:

115

answers:

2

I have clustered applications that requires one of the nodes to be designated as the master. The cluster nodes are tracked in a table with nodeID, isMaster, lastTimestamp columns.

Each node in the cluster will try to become a master every X seconds. Node can only become a master if either

  • there is no other master nodes
  • the lastTimestamp on current master node is older by 2*X

When one of the above conditions is satisfied

  • the current master node's isMaster should be cleared
  • the new master node's isMaster should be set
  • the new master node's lastTimestamp should be set to 'now' timestamp.

What is the single (portable) SQL statement to achieve the above without the possibility of two or more nodes becoming the master?

+1  A: 

I can imagine a solution for an Oracle database but I'm not sure it'd be portable. Why does this need to be a single portable SQL statement? Most database allow table locking and transactions, which allow you to do this sort of thing in multiple statements.

Jamie Love
I can do it in multiple statements. I was just curious if it's possible to do it in a single statement. What would Oracle solution look like?
Artur...
+1  A: 

Isn't this sort of coordination normally handled by the DBMS itself, rather than by applications running on the DBMS? I too can envisage ways to do it in the DBMS I'm familiar with, but without knowing more about your system (it presumably is using shared disks, so all the nodes see the same data; there are presumably locking protocols that prevent concurrent access to the data; is it a user process on the master node that periodically updates the lastTimestamp), it is going to be hard to help much. And, as Jamie Love pointed out, the DBMS should allow multiple processes to coordinate access to the relevant records - the main relevant record being the current master record.

[Edited: Maybe I was reading too much into it.

The single UPDATE statement has to do differential updates on two rows of the table, and must fail if only one of the two updates is possible. That is, it must both change the current master to be non-master and also change its own record so it is the master. One issue is how does the DBMS enforce the 'only one row may be the master' constraint. Let's assume that works and the statement as a whole will fail if there's an issue - as it should. Why do people so often omit the table name, even when they provide the column names? Oh well, the table name is hereinafter ClusterControl. Each node must know its own NodeID somehow; I've used {MyNodeID} to indicate where that appears in the SQL.

You need a separate heartbeat update:

 UPDATE ClusterControl
     SET lastTimestamp = CURRENT_TIMESTAMP
     WHERE NodeID = {MyNodeID};

The "seize master status" update might be:

UPDATE ClusterControl
    SET lastTimestamp = (CASE
                         WHEN NodeID = {MyNodeID} THEN CURRENT_TIMESTAMP
                         ELSE lastTimestamp END),
        isMaster      = (CASE
                         WHEN NodeID = {MyNodeId} THEN 'Y'
                         ELSE 'N' END)
    WHERE (NodeID  = {MyNodeID} AND isMaster = 'N') OR
          (NodeID != {MyNodeID} AND
           lastTimestamp < CURRENT_TIMESTAMP - INTERVAL '120' SECOND AND
           isMaster = 'Y'
          );

The theory behind the 'seize master status' update is (SET clause):

  • the lastTimestamp field for the new master is set to the current timestamp, but the old master is unchanged.
  • the isMaster field is changed to 'Y' for the new master and to 'N' for the old master.

The theory behind the WHERE clause is:

  • Only change the record for the current node if it is not the current master or the record for the current master node when this node is not the current node and the timestamp is more than 120 seconds ("2 * X" in the question) old.

Since there is a (possibly mythical) constraint in place to ensure that only one row has the 'Y' flag, this should fail as required when the master is up to date.

Untested SQL!

]

Jonathan Leffler
I think you reading too deep into this. I think my question has enough information in it for either "yes, it can be done and here is how" or "no, it cannot be done with a single statement".
Artur...
You may be right that I'm reading to much into it.
Jonathan Leffler