Skip to content

Commit 86e4d3b

Browse files
committed
Added BinaryLogClient::gtidSetFallbackToPurged
1 parent 2f109e0 commit 86e4d3b

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public X509Certificate[] getAcceptedIssuers() {
132132

133133
private GtidSet gtidSet;
134134
private final Object gtidSetAccessLock = new Object();
135+
private boolean gtidSetFallbackToPurged;
135136

136137
private EventDeserializer eventDeserializer = new EventDeserializer();
137138

@@ -298,12 +299,15 @@ public String getGtidSet() {
298299

299300
/**
300301
* @param gtidSet GTID set (can be an empty string).
301-
* <p>NOTE #1: Any value but null will switch BinaryLogClient into a GTID mode (in which case GTID set will be
302-
* updated with each incoming GTID event) as well as set binlogFilename to "" (empty string) (meaning
303-
* BinaryLogClient will request events "outside of the set" <u>starting from the oldest known binlog</u>).
302+
* <p>NOTE #1: Any value but null will switch BinaryLogClient into a GTID mode (this will also set binlogFilename
303+
* to "" (provided it's null) forcing MySQL to send events starting from the oldest known binlog (keep in mind
304+
* that connection will fail if gtid_purged is anything but empty (unless
305+
* {@link #setGtidSetFallbackToPurged(boolean)} is set to true))).
304306
* <p>NOTE #2: {@link #setBinlogFilename(String)} and {@link #setBinlogPosition(long)} can be used to specify the
305307
* exact position from which MySQL server should start streaming events (taking into account GTID set).
308+
* <p>NOTE #3: GTID set is automatically updated with each incoming GTID event (provided GTID mode is on).
306309
* @see #getGtidSet()
310+
* @see #setGtidSetFallbackToPurged(boolean)
307311
*/
308312
public void setGtidSet(String gtidSet) {
309313
if (gtidSet != null && this.binlogFilename == null) {
@@ -314,6 +318,21 @@ public void setGtidSet(String gtidSet) {
314318
}
315319
}
316320

321+
/**
322+
* @see #setGtidSetFallbackToPurged(boolean)
323+
*/
324+
public boolean isGtidSetFallbackToPurged() {
325+
return gtidSetFallbackToPurged;
326+
}
327+
328+
/**
329+
* @param gtidSetFallbackToPurged true if gtid_purged should be used as a fallback when gtidSet is set to "" and
330+
* MySQL server has purged some of the binary logs, false otherwise (default).
331+
*/
332+
public void setGtidSetFallbackToPurged(boolean gtidSetFallbackToPurged) {
333+
this.gtidSetFallbackToPurged = gtidSetFallbackToPurged;
334+
}
335+
317336
/**
318337
* @return true if "keep alive" thread should be automatically started (default), false otherwise.
319338
* @see #setKeepAlive(boolean)
@@ -474,6 +493,13 @@ public void connect() throws IOException {
474493
GreetingPacket greetingPacket = receiveGreeting();
475494
authenticate(greetingPacket);
476495
connectionId = greetingPacket.getThreadId();
496+
if ("".equals(binlogFilename)) {
497+
synchronized (gtidSetAccessLock) {
498+
if (gtidSet != null && "".equals(gtidSet.toString()) && gtidSetFallbackToPurged) {
499+
gtidSet = new GtidSet(fetchGtidPurged());
500+
}
501+
}
502+
}
477503
if (binlogFilename == null) {
478504
fetchBinlogFilenameAndPosition();
479505
}
@@ -800,6 +826,15 @@ public boolean isConnected() {
800826
return connected;
801827
}
802828

829+
private String fetchGtidPurged() throws IOException {
830+
channel.write(new QueryCommand("show global variables like 'gtid_purged'"));
831+
ResultSetRowPacket[] resultSet = readResultSet();
832+
if (resultSet.length != 0) {
833+
return resultSet[0].getValue(1).toUpperCase();
834+
}
835+
return "";
836+
}
837+
803838
private void fetchBinlogFilenameAndPosition() throws IOException {
804839
ResultSetRowPacket[] resultSet;
805840
channel.write(new QueryCommand("show master status"));

0 commit comments

Comments
 (0)