Comments on: Leader election using MySQL https://shlomi-noach.github.io/blog/mysql/leader-election-using-mysql Blog by Shlomi Noach Mon, 25 Mar 2019 10:48:45 +0000 hourly 1 https://wordpress.org/?v=5.3.3 By: tbeerbower https://shlomi-noach.github.io/blog/mysql/leader-election-using-mysql/comment-page-1#comment-376690 Thu, 13 Oct 2016 14:11:00 +0000 https://shlomi-noach.github.io/blog/?p=7384#comment-376690 Nice, Thanks!

I think it’s worth mentioning that you can answer ‘Am I the leader?’ by looking at the number of affected rows on the insert/update. Only the leader will change the row values so you can update the row and check leadership in one statement. In JDBC something like …

Properties connectionProperties = new Properties();
connectionProperties.put(“user”, username);
connectionProperties.put(“password”, password);
connectionProperties.put(“useAffectedRows”, “true”);

Connection connection = DriverManager.getConnection(url, connectionProperties);

PreparedStatement statement = connection.prepareStatement(INSERT_ELECTION_STATEMENT);
statement.setString(1, id);
statement.setInt(2, timeout);

boolean leader = statement.executeUpdate() > 0;

Note that you need to set the useAffectedRows (MySQL specific) property since JDBC specifies that the the number of selected rows should be returned.

Also, another benefit of your solution over GET_LOCK is that GET_LOCK doesn’t work with MySQL Cluster since the lock is not visible outside of a single MySQL process.

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/leader-election-using-mysql/comment-page-1#comment-363840 Mon, 20 Jun 2016 12:39:00 +0000 https://shlomi-noach.github.io/blog/?p=7384#comment-363840 @xudifsd I’m not sure I understand the question.

> I don’t want any server who is not leader to alter any data

Which data is that? Any data?

I wouldn’t incorporate an SQL leader logic into every single query in your app. Instead, your app can have a flag, frequently updated, saying “am I the leader”.
If you’re OK that the app will cease writing for a second as the leader changes, you can further require that a new leader only begins writing at least one second after becoming leader, during which time previous leader completes its own writes.

]]>
By: xudifsd https://shlomi-noach.github.io/blog/mysql/leader-election-using-mysql/comment-page-1#comment-357588 Fri, 06 May 2016 03:53:44 +0000 https://shlomi-noach.github.io/blog/?p=7384#comment-357588 Hi, I’m wondering how would you implement fencing with this techniques?
To be more precise, I don’t want any server who is not leader to alter any data. Maybe expressed(not working) as:

IF (SELECT count(*) as is_leader from service_election WHERE anchor=1 AND service_id=’my_service_number_7′) = 1 THEN
do update like insert or update or delete
END IF

In this way, we can prevent malfunctioning master polluting data. But I don’t know if we can express it in SQL

]]>