Skip to content

Commit 803968f

Browse files
committed
Switched BinaryLogClientMXBean.connect from blocking to non-blocking variant
1 parent ce9242f commit 803968f

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

src/main/java/com/github/shyiko/mysql/binlog/BinaryLogClient.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void run() {
271271
if (isConnected()) {
272272
disconnectChannel();
273273
}
274-
connect(keepAliveConnectTimeout, TimeUnit.MILLISECONDS);
274+
connect(keepAliveConnectTimeout);
275275
} catch (Exception ce) {
276276
if (logger.isLoggable(Level.WARNING)) {
277277
logger.warning("Failed to restore connection to " + hostname + ":" + port +
@@ -306,13 +306,12 @@ protected boolean isKeepAliveThreadRunning() {
306306

307307
/**
308308
* Connect to the replication stream in a separate thread.
309-
* @param timeout connect timeout
310-
* @param timeUnit timeout unit
309+
* @param timeoutInMilliseconds timeout in milliseconds
311310
* @throws AuthenticationException in case of failed authentication
312311
* @throws IOException if anything goes wrong while trying to connect
313312
* @throws TimeoutException if client wasn't able to connect in the requested period of time
314313
*/
315-
public void connect(long timeout, TimeUnit timeUnit) throws IOException, TimeoutException {
314+
public void connect(long timeoutInMilliseconds) throws IOException, TimeoutException {
316315
final CountDownLatch countDownLatch = new CountDownLatch(1);
317316
AbstractLifecycleListener connectListener = new AbstractLifecycleListener() {
318317
@Override
@@ -338,7 +337,7 @@ public void run() {
338337
thread.start();
339338
boolean started = false;
340339
try {
341-
started = countDownLatch.await(timeout, timeUnit);
340+
started = countDownLatch.await(timeoutInMilliseconds, TimeUnit.MILLISECONDS);
342341
} catch (InterruptedException e) {
343342
if (logger.isLoggable(Level.WARNING)) {
344343
logger.log(Level.WARNING, e.getMessage());
@@ -349,7 +348,7 @@ public void run() {
349348
throw exceptionReference.get();
350349
}
351350
if (!started) {
352-
throw new TimeoutException("BinaryLogClient was unable to connect in " + timeUnit.toMillis(timeout) + "ms");
351+
throw new TimeoutException("BinaryLogClient was unable to connect in " + timeoutInMilliseconds + "ms");
353352
}
354353
}
355354

src/main/java/com/github/shyiko/mysql/binlog/jmx/BinaryLogClientMXBean.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.github.shyiko.mysql.binlog.jmx;
1717

1818
import java.io.IOException;
19+
import java.util.concurrent.TimeoutException;
1920

2021
/**
2122
* @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a>
@@ -26,7 +27,7 @@ public interface BinaryLogClientMXBean {
2627
void setBinlogFilename(String binlogFilename);
2728
long getBinlogPosition();
2829
void setBinlogPosition(long binlogPosition);
29-
void connect() throws IOException;
30+
void connect(long timeoutInMilliseconds) throws IOException, TimeoutException;
3031
boolean isConnected();
3132
void disconnect() throws IOException;
3233

src/test/java/com/github/shyiko/mysql/binlog/BinaryLogClientIntegrationTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void setUp() throws Exception {
9393
client.registerEventListener(new TraceEventListener());
9494
client.registerEventListener(eventListener = new CountDownEventListener());
9595
client.registerLifecycleListener(new TraceLifecycleListener());
96-
client.connect(3, TimeUnit.SECONDS);
96+
client.connect(DEFAULT_TIMEOUT);
9797
master.execute(new Callback<Statement>() {
9898
@Override
9999
public void execute(Statement statement) throws SQLException {
@@ -369,7 +369,7 @@ public void execute(Statement statement) throws SQLException {
369369
eventListener.reset();
370370
}
371371
} finally {
372-
client.connect(3, TimeUnit.SECONDS);
372+
client.connect(DEFAULT_TIMEOUT);
373373
}
374374
eventListener.waitFor(WriteRowsEventData.class, 2, DEFAULT_TIMEOUT);
375375
}
@@ -388,7 +388,7 @@ public void execute(Statement statement) throws SQLException {
388388
client.disconnect();
389389
client.setBinlogFilename(binlogFilename);
390390
client.setBinlogPosition(binlogPosition);
391-
client.connect(3, TimeUnit.SECONDS);
391+
client.connect(DEFAULT_TIMEOUT);
392392
eventListener.waitFor(WriteRowsEventData.class, 1, DEFAULT_TIMEOUT);
393393
}
394394

@@ -405,7 +405,7 @@ public void testAutomaticFailover() throws Exception {
405405
clientOverProxy.setKeepAliveConnectTimeout(TimeUnit.SECONDS.toMillis(2));
406406
clientOverProxy.registerEventListener(eventListener);
407407
try {
408-
clientOverProxy.connect(3, TimeUnit.SECONDS);
408+
clientOverProxy.connect(DEFAULT_TIMEOUT);
409409
eventListener.waitFor(EventType.FORMAT_DESCRIPTION, 1, DEFAULT_TIMEOUT);
410410
assertTrue(clientOverProxy.isKeepAliveThreadRunning());
411411
BinaryLogClient.LifecycleListener lifecycleListenerMock =
@@ -431,7 +431,7 @@ public void execute(Statement statement) throws SQLException {
431431
}
432432
assertFalse(clientOverProxy.isKeepAliveThreadRunning());
433433
} finally {
434-
client.connect(3, TimeUnit.SECONDS);
434+
client.connect(DEFAULT_TIMEOUT);
435435
}
436436
} finally {
437437
tcpReverseProxy.unbind();
@@ -474,7 +474,7 @@ public void run() {
474474
@Test(expectedExceptions = AuthenticationException.class)
475475
public void testAuthenticationFailsWhenNonExistingSchemaProvided() throws Exception {
476476
new BinaryLogClient(slave.hostname, slave.port, "mbcj_test_non_existing", slave.username, slave.password).
477-
connect(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
477+
connect(DEFAULT_TIMEOUT);
478478
}
479479

480480
@Test
@@ -494,7 +494,7 @@ public void execute(Statement statement) throws SQLException {
494494
try {
495495
CountDownEventListener isolatedEventListener = new CountDownEventListener();
496496
isolatedClient.registerEventListener(isolatedEventListener);
497-
isolatedClient.connect(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
497+
isolatedClient.connect(DEFAULT_TIMEOUT);
498498
master.execute(new Callback<Statement>() {
499499
@Override
500500
public void execute(Statement statement) throws SQLException {

src/test/java/com/github/shyiko/mysql/binlog/BinaryLogClientTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.github.shyiko.mysql.binlog.jmx.BinaryLogClientStatistics;
1919
import org.testng.annotations.Test;
2020

21-
import java.util.concurrent.TimeUnit;
2221
import java.util.concurrent.TimeoutException;
2322

2423
import static org.testng.Assert.assertEquals;
@@ -62,7 +61,7 @@ public void testLifecycleListenersManagement() {
6261

6362
@Test(expectedExceptions = TimeoutException.class)
6463
public void testConnectionTimeout() throws Exception {
65-
new BinaryLogClient("_localhost_", 3306, "root", "mysql").connect(0, TimeUnit.MILLISECONDS);
64+
new BinaryLogClient("_localhost_", 3306, "root", "mysql").connect(0);
6665
}
6766

6867
@Test(expectedExceptions = IllegalArgumentException.class)

0 commit comments

Comments
 (0)