|
67 | 67 | */ |
68 | 68 | public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogParser<LogEvent> { |
69 | 69 |
|
| 70 | + public static final String XA_XID = "XA_XID"; |
| 71 | + public static final String XA_TYPE = "XA_TYPE"; |
| 72 | + public static final String XA_START = "XA START"; |
| 73 | + public static final String XA_END = "XA END"; |
| 74 | + public static final String XA_COMMIT = "XA COMMIT"; |
| 75 | + public static final String XA_ROLLBACK = "XA ROLLBACK"; |
70 | 76 | public static final String ISO_8859_1 = "ISO-8859-1"; |
71 | 77 | public static final String UTF_8 = "UTF-8"; |
72 | 78 | public static final int TINYINT_MAX_VALUE = 256; |
@@ -174,7 +180,43 @@ private Entry parseGTIDLogEvent(GtidLogEvent logEvent) { |
174 | 180 |
|
175 | 181 | private Entry parseQueryEvent(QueryLogEvent event, boolean isSeek) { |
176 | 182 | String queryString = event.getQuery(); |
177 | | - if (StringUtils.endsWithIgnoreCase(queryString, BEGIN)) { |
| 183 | + if (StringUtils.startsWithIgnoreCase(queryString, XA_START)) { |
| 184 | + // xa start use TransactionBegin |
| 185 | + TransactionBegin.Builder beginBuilder = TransactionBegin.newBuilder(); |
| 186 | + beginBuilder.setThreadId(event.getSessionId()); |
| 187 | + beginBuilder.addProps(createSpecialPair(XA_TYPE, XA_START)); |
| 188 | + beginBuilder.addProps(createSpecialPair(XA_XID, getXaXid(queryString, XA_START))); |
| 189 | + TransactionBegin transactionBegin = beginBuilder.build(); |
| 190 | + Header header = createHeader(binlogFileName, event.getHeader(), "", "", null); |
| 191 | + return createEntry(header, EntryType.TRANSACTIONBEGIN, transactionBegin.toByteString()); |
| 192 | + } else if (StringUtils.startsWithIgnoreCase(queryString, XA_END)) { |
| 193 | + // xa start use TransactionEnd |
| 194 | + TransactionEnd.Builder endBuilder = TransactionEnd.newBuilder(); |
| 195 | + endBuilder.setTransactionId(String.valueOf(0L)); |
| 196 | + endBuilder.addProps(createSpecialPair(XA_TYPE, XA_END)); |
| 197 | + endBuilder.addProps(createSpecialPair(XA_XID, getXaXid(queryString, XA_END))); |
| 198 | + TransactionEnd transactionEnd = endBuilder.build(); |
| 199 | + Header header = createHeader(binlogFileName, event.getHeader(), "", "", null); |
| 200 | + return createEntry(header, EntryType.TRANSACTIONEND, transactionEnd.toByteString()); |
| 201 | + } else if (StringUtils.startsWithIgnoreCase(queryString, XA_COMMIT)) { |
| 202 | + // xa commit |
| 203 | + Header header = createHeader(binlogFileName, event.getHeader(), "", "", EventType.XACOMMIT); |
| 204 | + RowChange.Builder rowChangeBuider = RowChange.newBuilder(); |
| 205 | + rowChangeBuider.setSql(queryString); |
| 206 | + rowChangeBuider.addProps(createSpecialPair(XA_TYPE, XA_COMMIT)); |
| 207 | + rowChangeBuider.addProps(createSpecialPair(XA_XID, getXaXid(queryString, XA_COMMIT))); |
| 208 | + rowChangeBuider.setEventType(EventType.XACOMMIT); |
| 209 | + return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString()); |
| 210 | + } else if (StringUtils.startsWithIgnoreCase(queryString, XA_ROLLBACK)) { |
| 211 | + // xa rollback |
| 212 | + Header header = createHeader(binlogFileName, event.getHeader(), "", "", EventType.XAROLLBACK); |
| 213 | + RowChange.Builder rowChangeBuider = RowChange.newBuilder(); |
| 214 | + rowChangeBuider.setSql(queryString); |
| 215 | + rowChangeBuider.addProps(createSpecialPair(XA_TYPE, XA_ROLLBACK)); |
| 216 | + rowChangeBuider.addProps(createSpecialPair(XA_XID, getXaXid(queryString, XA_ROLLBACK))); |
| 217 | + rowChangeBuider.setEventType(EventType.XAROLLBACK); |
| 218 | + return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString()); |
| 219 | + } else if (StringUtils.endsWithIgnoreCase(queryString, BEGIN)) { |
178 | 220 | TransactionBegin transactionBegin = createTransactionBegin(event.getSessionId()); |
179 | 221 | Header header = createHeader(binlogFileName, event.getHeader(), "", "", null); |
180 | 222 | return createEntry(header, EntryType.TRANSACTIONBEGIN, transactionBegin.toByteString()); |
@@ -237,6 +279,10 @@ private Entry parseQueryEvent(QueryLogEvent event, boolean isSeek) { |
237 | 279 | } |
238 | 280 | } |
239 | 281 |
|
| 282 | + private String getXaXid(String queryString, String type) { |
| 283 | + return StringUtils.substringAfter(queryString, type); |
| 284 | + } |
| 285 | + |
240 | 286 | private boolean processFilter(String queryString, DdlResult result) { |
241 | 287 | String schemaName = result.getSchemaName(); |
242 | 288 | String tableName = result.getTableName(); |
|
0 commit comments