Bug #39611 ReplicationDriver not selecting last host specified in JDBC config
Submitted: 23 Sep 2008 17:36 Modified: 21 Nov 2008 13:34
Reporter: Nu Deal Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.1.6 OS:Any
Assigned to:
Tags: Contribution, replication driver
Triage: D3 (Medium)

[23 Sep 2008 17:36] Nu Deal
Description:
When configuring the Java Replication Driver we find that the last slave specified is never used.

We're using com.mysql.jdbc.ReplicationDriver in our datasource configuration and the following datasource URL:

jdbc:mysql://masterHost:3306,slaveHost1:3306,slaveHost2:3306/database?autoReconnect=true&useServerPrepStmts=false&cachePrepStmts=true&jdbcCompliantTruncation=false&useUnicode=true&roundRobinLoadBalance=true

How to repeat:
Set up a Master and 1 Slave.  Perform some reads.  The slave is never used.

Set up 1 Master and 2 Slaves. Perform some reads.  The second slave is never used.

Suggested fix:
The key issue was with the way Java will effectively Math.floor the random number when converting to the int value.  Therefore, index can never equal indexRange.

Here's the diff I applied to com.mysql.jdbc.ConnectionImpl.java within the getNextRoundRobinHostIndex() method.

315c315
< 		int index = (int)(Math.random() * indexRange);
---
> 		int index = (int)(Math.random() * (indexRange+1));

This maintains an even distribution selection across the slaves 0..n
[24 Sep 2008 16:08] Juan Uys
patch

Attachment: patch (application/octet-stream, text), 142 bytes.

[30 Sep 2008 12:09] Tonci Grgin
Hi and thanks for your report.

Verified as described by looking into code.
[18 Nov 2008 0:09] Todd Farmer
This was resolved in 5.1.7 by patch #739:

-		int indexRange = hostList.size() - 1;
+		int indexRange = hostList.size();

Comment from merge:

Fixed BUG#39611 - ReplicationConnection never sends queries to last host in slave list.
[18 Nov 2008 16:42] Todd Farmer
Workaround is to list the final slave twice in pre-5.1.7 versions.
[21 Nov 2008 13:34] Tony Bedford
An entry was added to the 5.1.7 changelog:

When configuring the Java Replication Driver the last slave specified was never used.
[21 Nov 2008 13:36] Tony Bedford
An entry was added to the 5.1.7 changelog:

When configuring the Java Replication Driver the last slave specified was never used.