Skip to content

Commit

Permalink
add support for mariadb event types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin Huang committed Jun 12, 2019
1 parent 0a66fbc commit c0b1baa
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
trim_trailing_whitespace = false
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ public long getHeartbeatInterval() {
* <p>
* If set (recommended)
* <ul>
* <li> HEARTBEAT event will be emitted every "heartbeatInterval".
* <li> HEARTBEAT_LOG event will be emitted every "heartbeatInterval".
* <li> if {@link #setKeepAlive(boolean)} is on then keepAlive thread will attempt to reconnect if no
* HEARTBEAT events were received within {@link #setKeepAliveInterval(long)} (instead of trying to send
* HEARTBEAT_LOG events were received within {@link #setKeepAliveInterval(long)} (instead of trying to send
* PING every {@link #setKeepAliveInterval(long)}, which is fundamentally flawed -
* https://github.com/shyiko/mysql-binlog-connector-java/issues/118).
* </ul>
Expand Down Expand Up @@ -574,7 +574,7 @@ public void connect() throws IOException {
ensureEventDataDeserializer(EventType.ROTATE, RotateEventDataDeserializer.class);
synchronized (gtidSetAccessLock) {
if (gtidSet != null) {
ensureEventDataDeserializer(EventType.GTID, GtidEventDataDeserializer.class);
ensureEventDataDeserializer(EventType.GTID_LOG, GtidEventDataDeserializer.class);
ensureEventDataDeserializer(EventType.QUERY, QueryEventDataDeserializer.class);
}
}
Expand Down Expand Up @@ -1035,7 +1035,7 @@ private void updateGtidSet(Event event) {
}
EventHeader eventHeader = event.getHeader();
switch(eventHeader.getEventType()) {
case GTID:
case GTID_LOG:
GtidEventData gtidEventData = (GtidEventData) EventDataWrapper.internal(event.getData());
gtid = gtidEventData.getGtid();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* @author <a href="mailto:[email protected]">Stanley Shyiko</a>
*/
public class DeleteRowsEventData implements EventData {
public class DeleteRowsEventData implements EventData, RowEventData {

private long tableId;
private BitSet includedColumns;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface EventHeader extends Serializable {
long getTimestamp();
EventType getEventType();
long getServerId();
long getEventLength();
long getHeaderLength();
long getDataLength();
long getNextPosition();
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void setServerId(long serverId) {
this.serverId = serverId;
}

@Override
public long getEventLength() {
return eventLength;
}
Expand All @@ -69,6 +70,7 @@ public long getPosition() {
return nextPosition - eventLength;
}

@Override
public long getNextPosition() {
return nextPosition;
}
Expand Down
161 changes: 119 additions & 42 deletions src/main/java/com/github/shyiko/mysql/binlog/event/EventType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.github.shyiko.mysql.binlog.event;

import java.util.HashMap;
import java.util.Map;

/**
* @see <a href="https://dev.mysql.com/doc/internals/en/event-meanings.html">Event Meanings</a> for the original
* documentation.
Expand All @@ -25,90 +28,94 @@ public enum EventType {
/**
* Events of this event type should never occur. Not written to a binary log.
*/
UNKNOWN,
UNKNOWN(0),
/**
* A descriptor event that is written to the beginning of the each binary log file. (In MySQL 4.0 and 4.1,
* this event is written only to the first binary log file that the server creates after startup.) This event is
* used in MySQL 3.23 through 4.1 and superseded in MySQL 5.0 by {@link #FORMAT_DESCRIPTION}.
*/
START_V3,
START_V3(1),
/**
* Written when an updating statement is done.
*/
QUERY,
QUERY(2),
/**
* Written when mysqld stops.
*/
STOP,
STOP(3),
/**
* Written when mysqld switches to a new binary log file. This occurs when someone issues a FLUSH LOGS statement or
* the current binary log file becomes larger than max_binlog_size.
*
* See https://dev.mysql.com/doc/internals/en/rotate-event.html
*/
ROTATE,
ROTATE(4),
/**
* Written every time a statement uses an AUTO_INCREMENT column or the LAST_INSERT_ID() function; precedes other
* events for the statement. This is written only before a {@link #QUERY} and is not used in case of RBR.
*/
INTVAR,
INTVAR(5),
/**
* Used for LOAD DATA INFILE statements in MySQL 3.23.
*/
LOAD,
LOAD(6),
/**
* Not used.
*/
SLAVE,
SLAVE(7),
/**
* Used for LOAD DATA INFILE statements in MySQL 4.0 and 4.1.
*/
CREATE_FILE,
CREATE_FILE(8),
/**
* Used for LOAD DATA INFILE statements as of MySQL 4.0.
*/
APPEND_BLOCK,
APPEND_BLOCK(9),
/**
* Used for LOAD DATA INFILE statements in 4.0 and 4.1.
*/
EXEC_LOAD,
EXEC_LOAD(10),
/**
* Used for LOAD DATA INFILE statements as of MySQL 4.0.
*/
DELETE_FILE,
DELETE_FILE(11),
/**
* Used for LOAD DATA INFILE statements in MySQL 4.0 and 4.1.
*/
NEW_LOAD,
NEW_LOAD(12),
/**
* Written every time a statement uses the RAND() function; precedes other events for the statement. Indicates the
* seed values to use for generating a random number with RAND() in the next statement. This is written only
* before a {@link #QUERY} and is not used in case of RBR.
*/
RAND,
RAND(13),
/**
* Written every time a statement uses a user variable; precedes other events for the statement. Indicates the
* value to use for the user variable in the next statement. This is written only before a {@link #QUERY} and
* is not used in case of RBR.
*/
USER_VAR,
USER_VAR(14),
/**
* A descriptor event that is written to the beginning of the each binary log file.
* This event is used as of MySQL 5.0; it supersedes {@link #START_V3}.
*
* See: https://dev.mysql.com/doc/internals/en/format-description-event.html
*/
FORMAT_DESCRIPTION,
FORMAT_DESCRIPTION(15),
/**
* Generated for a commit of a transaction that modifies one or more tables of an XA-capable storage engine.
* Normal transactions are implemented by sending a {@link #QUERY} containing a BEGIN statement and a {@link #QUERY}
* containing a COMMIT statement (or a ROLLBACK statement if the transaction is rolled back).
*/
XID,
XID(16),
/**
* Used for LOAD DATA INFILE statements as of MySQL 5.0.
*/
BEGIN_LOAD_QUERY,
BEGIN_LOAD_QUERY(17),
/**
* Used for LOAD DATA INFILE statements as of MySQL 5.0.
*/
EXECUTE_LOAD_QUERY,
EXECUTE_LOAD_QUERY(18),
/**
* This event precedes each row operation event. It maps a table definition to a number, where the table definition
* consists of database and table names and column definitions. The purpose of this event is to enable replication
Expand All @@ -117,82 +124,145 @@ public enum EventType {
* of TABLE_MAP events: one per table used by events in the sequence.
* Used in case of RBR.
*/
TABLE_MAP,
TABLE_MAP(19),
/**
* Describes inserted rows (within a single table).
* Used in case of RBR (5.1.0 - 5.1.15).
*/
PRE_GA_WRITE_ROWS,
PRE_GA_WRITE_ROWS(20),
/**
* Describes updated rows (within a single table).
* Used in case of RBR (5.1.0 - 5.1.15).
*/
PRE_GA_UPDATE_ROWS,
PRE_GA_UPDATE_ROWS(21),
/**
* Describes deleted rows (within a single table).
* Used in case of RBR (5.1.0 - 5.1.15).
*/
PRE_GA_DELETE_ROWS,
PRE_GA_DELETE_ROWS(22),
/**
* Describes inserted rows (within a single table).
* Used in case of RBR (5.1.16 - mysql-trunk).
*/
WRITE_ROWS,
WRITE_ROWS_V1(23),
/**
* Describes updated rows (within a single table).
* Used in case of RBR (5.1.16 - mysql-trunk).
*/
UPDATE_ROWS,
UPDATE_ROWS_V1(24),
/**
* Describes deleted rows (within a single table).
* Used in case of RBR (5.1.16 - mysql-trunk).
*/
DELETE_ROWS,
DELETE_ROWS_V1(25),
/**
* Used to log an out of the ordinary event that occurred on the master. It notifies the slave that something
* happened on the master that might cause data to be in an inconsistent state.
*/
INCIDENT,
INCIDENT(26),
/**
* Sent by a master to a slave to let the slave know that the master is still alive. Not written to a binary log.
*/
HEARTBEAT,
HEARTBEAT_LOG(27),
/**
* In some situations, it is necessary to send over ignorable data to the slave: data that a slave can handle in
* case there is code for handling it, but which can be ignored if it is not recognized.
*/
IGNORABLE,
IGNORABLE_LOG(28),
/**
* Introduced to record the original query for rows events in RBR.
*/
ROWS_QUERY,
ROWS_QUERY_LOG(29),
/**
* Describes inserted rows (within a single table).
* Used in case of RBR (5.1.18+).
*/
EXT_WRITE_ROWS,
WRITE_ROWS(30),
/**
* Describes updated rows (within a single table).
* Used in case of RBR (5.1.18+).
*/
EXT_UPDATE_ROWS,
UPDATE_ROWS(31),
/**
* Describes deleted rows (within a single table).
* Used in case of RBR (5.1.18+).
*/
EXT_DELETE_ROWS,
DELETE_ROWS(32),

/**
* Global Transaction Identifier.
*
* MySQL 5.6 GTID events
*/
GTID_LOG(33),
ANONYMOUS_GTID_LOG(34),
PREVIOUS_GTIDS_LOG(35),

/**
* MySQL 5.7 Events
*/
GTID,
ANONYMOUS_GTID,
PREVIOUS_GTIDS,
TRANSACTION_CONTEXT,
VIEW_CHANGE,
TRANSACTION_CONTEXT(36),
VIEW_CHANGE(37),
/**
* Prepared XA transaction terminal event similar to XID except that it is specific to XA transaction.
*/
XA_PREPARE;
XA_PREPARE_LOG(38),

/**
* New Maria event numbers start from here
*/
MARIA_ANNOTATE_ROWS(160),
/**
* Binlog checkpoint event. Used for XA crash recovery on the master, not used
* in replication.
* A binlog checkpoint event specifies a binlog file such that XA crash
* recovery can start from that file - and it is guaranteed to find all XIDs
* that are prepared in storage engines but not yet committed.
*/
MARIA_BINLOG_CHECKPOINT(161),
/**
* Gtid event. For global transaction ID, used to start a new event group,
* instead of the old BEGIN query event, and also to mark stand-alone
* events.
*/
MARIA_GTID(162),
/**
* Gtid list event. Logged at the start of every binlog, to record the
* current replication state. This consists of the last GTID seen for
* each replication domain.
*/
MARIA_GTID_LIST(163),

MARIA_START_ENCRYPTION(164),

/**
* Compressed binlog event.
* Note that the order between WRITE/UPDATE/DELETE events is significant;
* this is so that we can convert from the compressed to the uncompressed
* event type with (type-WRITE_ROWS_COMPRESSED_EVENT + WRITE_ROWS_EVENT)
* and similar for _V1.
*/
MARIA_QUERY_COMPRESSED(165),
MARIA_WRITE_ROWS_COMPRESSED_V1(166),
MARIA_UPDATE_ROWS_COMPRESSED_V1(167),
MARIA_DELETE_ROWS_COMPRESSED_V1(168),
MARIA_WRITE_ROWS_COMPRESSED(169),
MARIA_UPDATE_ROWS_COMPRESSED(170),
MARIA_DELETE_ROWS_COMPRESSED(171);

private int index;

EventType(int index) {
this.index = index;
}

public static final Map<Integer, EventType> EVENT_TYPES = new HashMap<Integer, EventType>();
static {
for (EventType eventType: EventType.values()) {
EVENT_TYPES.put(eventType.index, eventType);
}
}

public static boolean isRowMutation(EventType eventType) {
return EventType.isWrite(eventType) ||
Expand All @@ -202,20 +272,27 @@ public static boolean isRowMutation(EventType eventType) {

public static boolean isWrite(EventType eventType) {
return eventType == PRE_GA_WRITE_ROWS ||
eventType == WRITE_ROWS_V1 ||
eventType == WRITE_ROWS ||
eventType == EXT_WRITE_ROWS;
eventType == MARIA_WRITE_ROWS_COMPRESSED_V1 ||
eventType == MARIA_WRITE_ROWS_COMPRESSED;

}

public static boolean isUpdate(EventType eventType) {
return eventType == PRE_GA_UPDATE_ROWS ||
eventType == UPDATE_ROWS_V1 ||
eventType == UPDATE_ROWS ||
eventType == EXT_UPDATE_ROWS;
eventType == MARIA_UPDATE_ROWS_COMPRESSED_V1 ||
eventType == MARIA_UPDATE_ROWS_COMPRESSED;
}

public static boolean isDelete(EventType eventType) {
return eventType == PRE_GA_DELETE_ROWS ||
eventType == DELETE_ROWS_V1 ||
eventType == DELETE_ROWS ||
eventType == EXT_DELETE_ROWS;
eventType == MARIA_DELETE_ROWS_COMPRESSED_V1 ||
eventType == MARIA_DELETE_ROWS_COMPRESSED;
}

}
Loading

0 comments on commit c0b1baa

Please sign in to comment.