15
15
*/
16
16
package com .github .shyiko .mysql .binlog ;
17
17
18
- import com .github .shyiko .mysql .binlog .event .*;
19
- import com .github .shyiko .mysql .binlog .event .deserialization .*;
18
+ import com .github .shyiko .mysql .binlog .event .AnnotateRowsEventData ;
19
+ import com .github .shyiko .mysql .binlog .event .Event ;
20
+ import com .github .shyiko .mysql .binlog .event .EventHeader ;
21
+ import com .github .shyiko .mysql .binlog .event .EventHeaderV4 ;
22
+ import com .github .shyiko .mysql .binlog .event .EventType ;
23
+ import com .github .shyiko .mysql .binlog .event .GtidEventData ;
24
+ import com .github .shyiko .mysql .binlog .event .MariadbGtidEventData ;
25
+ import com .github .shyiko .mysql .binlog .event .MariadbGtidListEventData ;
26
+ import com .github .shyiko .mysql .binlog .event .QueryEventData ;
27
+ import com .github .shyiko .mysql .binlog .event .RotateEventData ;
28
+ import com .github .shyiko .mysql .binlog .event .deserialization .ChecksumType ;
29
+ import com .github .shyiko .mysql .binlog .event .deserialization .EventDataDeserializationException ;
30
+ import com .github .shyiko .mysql .binlog .event .deserialization .EventDataDeserializer ;
31
+ import com .github .shyiko .mysql .binlog .event .deserialization .EventDeserializer ;
20
32
import com .github .shyiko .mysql .binlog .event .deserialization .EventDeserializer .EventDataWrapper ;
33
+ import com .github .shyiko .mysql .binlog .event .deserialization .GtidEventDataDeserializer ;
34
+ import com .github .shyiko .mysql .binlog .event .deserialization .QueryEventDataDeserializer ;
35
+ import com .github .shyiko .mysql .binlog .event .deserialization .RotateEventDataDeserializer ;
21
36
import com .github .shyiko .mysql .binlog .io .ByteArrayInputStream ;
22
37
import com .github .shyiko .mysql .binlog .jmx .BinaryLogClientMXBean ;
23
38
import com .github .shyiko .mysql .binlog .network .AuthenticationException ;
34
49
import com .github .shyiko .mysql .binlog .network .protocol .Packet ;
35
50
import com .github .shyiko .mysql .binlog .network .protocol .PacketChannel ;
36
51
import com .github .shyiko .mysql .binlog .network .protocol .ResultSetRowPacket ;
37
- import com .github .shyiko .mysql .binlog .network .protocol .command .*;
52
+ import com .github .shyiko .mysql .binlog .network .protocol .command .Command ;
53
+ import com .github .shyiko .mysql .binlog .network .protocol .command .DumpBinaryLogCommand ;
54
+ import com .github .shyiko .mysql .binlog .network .protocol .command .DumpBinaryLogGtidCommand ;
55
+ import com .github .shyiko .mysql .binlog .network .protocol .command .PingCommand ;
56
+ import com .github .shyiko .mysql .binlog .network .protocol .command .QueryCommand ;
57
+ import com .github .shyiko .mysql .binlog .network .protocol .command .SSLRequestCommand ;
38
58
39
59
import javax .net .ssl .SSLContext ;
40
60
import javax .net .ssl .TrustManager ;
@@ -115,14 +135,12 @@ public X509Certificate[] getAcceptedIssuers() {
115
135
private volatile long connectionId ;
116
136
private SSLMode sslMode = SSLMode .DISABLED ;
117
137
118
- private GtidSet gtidSet ;
119
- private final Object gtidSetAccessLock = new Object ();
138
+ protected GtidSet gtidSet ;
139
+ protected final Object gtidSetAccessLock = new Object ();
120
140
private boolean gtidSetFallbackToPurged ;
121
141
private boolean useBinlogFilenamePositionInGtidMode ;
122
142
private String gtid ;
123
143
private boolean tx ;
124
- private boolean isMariadb = false ;
125
- private boolean mariadbSendAnnotateRowsEvent = false ;
126
144
127
145
private EventDeserializer eventDeserializer = new EventDeserializer ();
128
146
@@ -132,7 +150,7 @@ public X509Certificate[] getAcceptedIssuers() {
132
150
private SocketFactory socketFactory ;
133
151
private SSLSocketFactory sslSocketFactory ;
134
152
135
- private volatile PacketChannel channel ;
153
+ protected volatile PacketChannel channel ;
136
154
private volatile boolean connected ;
137
155
private volatile long masterServerId = -1 ;
138
156
@@ -317,15 +335,14 @@ public void setGtidSet(String gtidSet) {
317
335
this .binlogFilename = "" ;
318
336
}
319
337
synchronized (gtidSetAccessLock ) {
320
- // mariadb GtidSet format will be domainId-serverId-sequence
321
- if (gtidSet != null && !gtidSet .contains (":" )) {
322
- this .gtidSet = new MariadbGtidSet (gtidSet );
323
- } else {
324
- this .gtidSet = gtidSet != null ? new GtidSet (gtidSet ) : null ;
325
- }
338
+ this .gtidSet = gtidSet != null ? buildGtidSet (gtidSet ) : null ;
326
339
}
327
340
}
328
341
342
+ protected GtidSet buildGtidSet (String gtidSet ) {
343
+ return new GtidSet (gtidSet );
344
+ }
345
+
329
346
/**
330
347
* @see #setGtidSetFallbackToPurged(boolean)
331
348
* @return whether gtid_purged is used as a fallback
@@ -488,19 +505,6 @@ public void setThreadFactory(ThreadFactory threadFactory) {
488
505
this .threadFactory = threadFactory ;
489
506
}
490
507
491
- public boolean isMariadbSendAnnotateRowsEvent () {
492
- return mariadbSendAnnotateRowsEvent ;
493
- }
494
-
495
- /**
496
- * Only in Mariadb, if set true, the Slave server connects with the BINLOG_SEND_ANNOTATE_ROWS_EVENT flag (value is 2)
497
- * in the COM_BINLOG_DUMP Slave Registration phase
498
- * @param mariadbSendAnnotateRowsEvent
499
- */
500
- public void setMariadbSendAnnotateRowsEvent (boolean mariadbSendAnnotateRowsEvent ) {
501
- this .mariadbSendAnnotateRowsEvent = mariadbSendAnnotateRowsEvent ;
502
- }
503
-
504
508
/**
505
509
* Connect to the replication stream. Note that this method blocks until disconnected.
506
510
* @throws AuthenticationException if authentication fails
@@ -538,17 +542,8 @@ public void connect() throws IOException, IllegalStateException {
538
542
channel .authenticationComplete ();
539
543
540
544
connectionId = greetingPacket .getThreadId ();
541
- isMariadb = greetingPacket .getServerVersion ().toLowerCase ().contains ("mariadb" );
542
545
if ("" .equals (binlogFilename )) {
543
- synchronized (gtidSetAccessLock ) {
544
- if (gtidSet != null && "" .equals (gtidSet .toString ()) && gtidSetFallbackToPurged ) {
545
- if (isMariadb ) {
546
- gtidSet = new MariadbGtidSet (fetchGtidPurged ());
547
- } else {
548
- gtidSet = new GtidSet (fetchGtidPurged ());
549
- }
550
- }
551
- }
546
+ setupGtidSet ();
552
547
}
553
548
if (binlogFilename == null ) {
554
549
fetchBinlogFilenameAndPosition ();
@@ -597,13 +592,7 @@ public void connect() throws IOException, IllegalStateException {
597
592
ensureEventDataDeserializer (EventType .ROTATE , RotateEventDataDeserializer .class );
598
593
synchronized (gtidSetAccessLock ) {
599
594
if (gtidSet != null ) {
600
- ensureEventDataDeserializer (EventType .GTID , GtidEventDataDeserializer .class );
601
- ensureEventDataDeserializer (EventType .QUERY , QueryEventDataDeserializer .class );
602
- if (isMariadb ) {
603
- ensureEventDataDeserializer (EventType .ANNOTATE_ROWS , AnnotateRowsEventDataDeserializer .class );
604
- ensureEventDataDeserializer (EventType .MARIADB_GTID , MariadbGtidEventDataDeserializer .class );
605
- ensureEventDataDeserializer (EventType .MARIADB_GTID_LIST , MariadbGtidListEventDataDeserializer .class );
606
- }
595
+ ensureGtidEventDataDeserializer ();
607
596
}
608
597
}
609
598
listenForEventPackets ();
@@ -676,7 +665,7 @@ public Object call() throws Exception {
676
665
};
677
666
}
678
667
679
- private void checkError (byte [] packet ) throws IOException {
668
+ protected void checkError (byte [] packet ) throws IOException {
680
669
if (packet [0 ] == (byte ) 0xFF /* error */ ) {
681
670
byte [] bytes = Arrays .copyOfRange (packet , 1 , packet .length );
682
671
ErrorPacket errorPacket = new ErrorPacket (bytes );
@@ -720,7 +709,6 @@ private boolean tryUpgradeToSSL(GreetingPacket greetingPacket) throws IOExceptio
720
709
return false ;
721
710
}
722
711
723
-
724
712
private void enableHeartbeat () throws IOException {
725
713
channel .write (new QueryCommand ("set @master_heartbeat_period=" + heartbeatInterval * 1000000 ));
726
714
byte [] statementResult = channel .read ();
@@ -735,35 +723,23 @@ private void setMasterServerId() throws IOException {
735
723
}
736
724
}
737
725
738
- private void requestBinaryLogStream () throws IOException {
726
+ protected void requestBinaryLogStream () throws IOException {
739
727
long serverId = blocking ? this .serverId : 0 ; // http://bugs.mysql.com/bug.php?id=71178
740
728
Command dumpBinaryLogCommand ;
741
729
synchronized (gtidSetAccessLock ) {
742
730
if (gtidSet != null ) {
743
- if (isMariadb ) {
744
- channel .write (new QueryCommand ("SET @mariadb_slave_capability=4" ));
745
- checkError (channel .read ());
746
- channel .write (new QueryCommand ("SET @slave_connect_state = '" + gtidSet .toString () + "'" ));
747
- checkError (channel .read ());
748
- channel .write (new QueryCommand ("SET @slave_gtid_strict_mode = 0" ));
749
- checkError (channel .read ());
750
- channel .write (new QueryCommand ("SET @slave_gtid_ignore_duplicates = 0" ));
751
- checkError (channel .read ());
752
- dumpBinaryLogCommand = new DumpBinaryLogCommand (serverId , "" , 0L , isMariadbSendAnnotateRowsEvent ());
753
- } else {
754
- dumpBinaryLogCommand = new DumpBinaryLogGtidCommand (serverId ,
755
- useBinlogFilenamePositionInGtidMode ? binlogFilename : "" ,
756
- useBinlogFilenamePositionInGtidMode ? binlogPosition : 4 ,
757
- gtidSet );
758
- }
731
+ dumpBinaryLogCommand = new DumpBinaryLogGtidCommand (serverId ,
732
+ useBinlogFilenamePositionInGtidMode ? binlogFilename : "" ,
733
+ useBinlogFilenamePositionInGtidMode ? binlogPosition : 4 ,
734
+ gtidSet );
759
735
} else {
760
736
dumpBinaryLogCommand = new DumpBinaryLogCommand (serverId , binlogFilename , binlogPosition );
761
737
}
762
738
}
763
739
channel .write (dumpBinaryLogCommand );
764
740
}
765
741
766
- private void ensureEventDataDeserializer (EventType eventType ,
742
+ protected void ensureEventDataDeserializer (EventType eventType ,
767
743
Class <? extends EventDataDeserializer > eventDataDeserializerClass ) {
768
744
EventDataDeserializer eventDataDeserializer = eventDeserializer .getEventDataDeserializer (eventType );
769
745
if (eventDataDeserializer .getClass () != eventDataDeserializerClass &&
@@ -780,6 +756,10 @@ private void ensureEventDataDeserializer(EventType eventType,
780
756
}
781
757
}
782
758
759
+ protected void ensureGtidEventDataDeserializer () {
760
+ ensureEventDataDeserializer (EventType .GTID , GtidEventDataDeserializer .class );
761
+ ensureEventDataDeserializer (EventType .QUERY , QueryEventDataDeserializer .class );
762
+ }
783
763
784
764
private void spawnKeepAliveThread () {
785
765
final ExecutorService threadExecutor =
@@ -924,6 +904,14 @@ private String fetchGtidPurged() throws IOException {
924
904
return "" ;
925
905
}
926
906
907
+ protected void setupGtidSet () throws IOException {
908
+ synchronized (gtidSetAccessLock ) {
909
+ if (gtidSet != null && "" .equals (gtidSet .toString ()) && gtidSetFallbackToPurged ) {
910
+ gtidSet = new GtidSet (fetchGtidPurged ());
911
+ }
912
+ }
913
+ }
914
+
927
915
private void fetchBinlogFilenameAndPosition () throws IOException {
928
916
ResultSetRowPacket [] resultSet ;
929
917
channel .write (new QueryCommand ("show master status" ));
@@ -1025,7 +1013,7 @@ private byte[] readPacketSplitInChunks(ByteArrayInputStream inputStream, int pac
1025
1013
return result ;
1026
1014
}
1027
1015
1028
- private void updateClientBinlogFilenameAndPosition (Event event ) {
1016
+ protected void updateClientBinlogFilenameAndPosition (Event event ) {
1029
1017
EventHeader eventHeader = event .getHeader ();
1030
1018
EventType eventType = eventHeader .getEventType ();
1031
1019
if (eventType == EventType .ROTATE ) {
@@ -1044,7 +1032,7 @@ private void updateClientBinlogFilenameAndPosition(Event event) {
1044
1032
}
1045
1033
}
1046
1034
1047
- private void updateGtidSet (Event event ) {
1035
+ protected void updateGtidSet (Event event ) {
1048
1036
synchronized (gtidSetAccessLock ) {
1049
1037
if (gtidSet == null ) {
1050
1038
return ;
@@ -1070,34 +1058,39 @@ private void updateGtidSet(Event event) {
1070
1058
tx = false ;
1071
1059
break ;
1072
1060
case QUERY :
1073
- case ANNOTATE_ROWS :
1074
- String sql ;
1075
- if (eventHeader .getEventType () == EventType .QUERY ) {
1076
- QueryEventData queryEventData = (QueryEventData ) EventDataWrapper .internal (event .getData ());
1077
- sql = queryEventData .getSql ();
1078
- } else {
1079
- AnnotateRowsEventData annotateRowsEventData = (AnnotateRowsEventData ) EventDataWrapper .internal (event .getData ());
1080
- sql = annotateRowsEventData .getRowsQuery ();
1081
- }
1082
-
1061
+ QueryEventData queryEventData = (QueryEventData ) EventDataWrapper .internal (event .getData ());
1062
+ String sql = queryEventData .getSql ();
1083
1063
if (sql == null ) {
1084
1064
break ;
1085
1065
}
1086
- if ("BEGIN" .equals (sql )) {
1087
- tx = true ;
1088
- } else
1089
- if ("COMMIT" .equals (sql ) || "ROLLBACK" .equals (sql )) {
1090
- commitGtid ();
1091
- tx = false ;
1092
- } else
1093
- if (!tx ) {
1094
- // auto-commit query, likely DDL
1095
- commitGtid ();
1066
+ commitGtid (sql );
1067
+ break ;
1068
+ case ANNOTATE_ROWS :
1069
+ AnnotateRowsEventData annotateRowsEventData = (AnnotateRowsEventData ) EventDeserializer .EventDataWrapper .internal (event .getData ());
1070
+ sql = annotateRowsEventData .getRowsQuery ();
1071
+ if (sql == null ) {
1072
+ break ;
1096
1073
}
1074
+ commitGtid (sql );
1075
+ break ;
1097
1076
default :
1098
1077
}
1099
1078
}
1100
1079
1080
+ protected void commitGtid (String sql ) {
1081
+ if ("BEGIN" .equals (sql )) {
1082
+ tx = true ;
1083
+ } else
1084
+ if ("COMMIT" .equals (sql ) || "ROLLBACK" .equals (sql )) {
1085
+ commitGtid ();
1086
+ tx = false ;
1087
+ } else
1088
+ if (!tx ) {
1089
+ // auto-commit query, likely DDL
1090
+ commitGtid ();
1091
+ }
1092
+ }
1093
+
1101
1094
private void commitGtid () {
1102
1095
if (gtid != null ) {
1103
1096
synchronized (gtidSetAccessLock ) {
@@ -1308,7 +1301,7 @@ public interface LifecycleListener {
1308
1301
/**
1309
1302
* Default (no-op) implementation of {@link LifecycleListener}.
1310
1303
*/
1311
- public static abstract class AbstractLifecycleListener implements LifecycleListener {
1304
+ public static abstract class AbstractLifecycleListener implements LifecycleListener {
1312
1305
1313
1306
public void onConnect (BinaryLogClient client ) { }
1314
1307
0 commit comments