-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sub-transaction positioning #2035
Open
osheroff
wants to merge
5
commits into
master
Choose a base branch
from
ben/sub_tx_positioning
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ public class BinlogConnectorReplicator extends RunLoopProcess implements Replica | |
|
||
private Position lastHeartbeatPosition; | ||
private final HeartbeatNotifier heartbeatNotifier; | ||
private Position startPosition; | ||
private Long stopAtHeartbeat; | ||
private Filter filter; | ||
private Boolean ignoreMissingSchema; | ||
|
@@ -86,6 +87,11 @@ public class BinlogConnectorReplicator extends RunLoopProcess implements Replica | |
|
||
private boolean isConnected = false; | ||
|
||
@Override | ||
public StopPriority getStopPriority() { | ||
return StopPriority.BINLOG; | ||
} | ||
|
||
private class ClientReconnectedException extends Exception {} | ||
|
||
public BinlogConnectorReplicator( | ||
|
@@ -182,6 +188,7 @@ public BinlogConnectorReplicator( | |
this.client.setUseSendAnnotateRowsEvent(true); | ||
|
||
|
||
this.startPosition = start; | ||
BinlogPosition startBinlog = start.getBinlogPosition(); | ||
if (startBinlog.getGtidSetStr() != null) { | ||
String gtidStr = startBinlog.getGtidSetStr(); | ||
|
@@ -408,8 +415,8 @@ private void processQueryEvent(BinlogConnectorEvent event) throws Exception { | |
data.getDatabase(), | ||
data.getSql(), | ||
this.schemaStore, | ||
Position.valueOf(event.getPosition(), getLastHeartbeatRead()), | ||
Position.valueOf(event.getNextPosition(), getLastHeartbeatRead()), | ||
Position.valueOf(event.getPosition(), getLastHeartbeatRead(), 0L), | ||
Position.valueOf(event.getNextPosition(), getLastHeartbeatRead(), 0L), | ||
event.getEvent().getHeader().getTimestamp() | ||
); | ||
} | ||
|
@@ -534,11 +541,13 @@ private void tryReconnect() throws TimeoutException { | |
* @return A RowMapBuffer of rows; either in-memory or on disk. | ||
*/ | ||
|
||
private RowMapBuffer getTransactionRows(BinlogConnectorEvent beginEvent) throws Exception { | ||
private RowMapBuffer getTransactionRows(BinlogConnectorEvent beginEvent, long startTXOffset) throws Exception { | ||
BinlogConnectorEvent event; | ||
BinlogPosition lastTableMapPosition = null; | ||
RowMapBuffer buffer = new RowMapBuffer(MAX_TX_ELEMENTS, this.bufferMemoryUsage); | ||
|
||
String currentQuery = null; | ||
long txOffset = 0; | ||
|
||
while ( true ) { | ||
event = pollEvent(); | ||
|
@@ -551,7 +560,9 @@ private RowMapBuffer getTransactionRows(BinlogConnectorEvent beginEvent) throws | |
EventType eventType = event.getEvent().getHeader().getEventType(); | ||
if (event.isCommitEvent()) { | ||
if (!buffer.isEmpty()) { | ||
buffer.getLast().setTXCommit(); | ||
RowMap lastRow = buffer.getLast(); | ||
lastRow.setTXCommit(); | ||
lastRow.updateNextPosition(event.getNextPosition()); | ||
long timeSpent = buffer.getLast().getTimestampMillis() - beginEvent.getEvent().getHeader().getTimestamp(); | ||
transactionExecutionTime.update(timeSpent); | ||
transactionRowCount.update(buffer.size()); | ||
|
@@ -574,22 +585,27 @@ private RowMapBuffer getTransactionRows(BinlogConnectorEvent beginEvent) throws | |
if ( table != null && shouldOutputEvent(table.getDatabase(), table.getName(), filter, table.getColumnNames()) ) { | ||
List<RowMap> rows; | ||
try { | ||
rows = event.jsonMaps(table, getLastHeartbeatRead(), currentQuery); | ||
rows = event.jsonMaps(table, getLastHeartbeatRead(), currentQuery, lastTableMapPosition); | ||
} catch ( ColumnDefCastException e ) { | ||
logColumnDefCastException(table, e); | ||
|
||
throw(e); | ||
} | ||
|
||
for ( RowMap r : rows ) | ||
if (shouldOutputRowMap(table.getDatabase(), table.getName(), r, filter)) { | ||
buffer.add(r); | ||
for ( RowMap r : rows ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should prevent this logic from impacting rows having different positions, not applying any tx_offset logic:
|
||
r.setXoffset(txOffset++); | ||
if ( shouldOutputRowMap(table.getDatabase(), table.getName(), r, filter) ) { | ||
if ( startTXOffset <= r.getXoffset() ) { | ||
buffer.add(r); | ||
} | ||
} | ||
} | ||
} | ||
break; | ||
case TABLE_MAP: | ||
TableMapEventData data = event.tableMapData(); | ||
tableCache.processEvent(getSchema(), this.filter, this.ignoreMissingSchema, data.getTableId(), data.getDatabase(), data.getTable()); | ||
lastTableMapPosition = event.getPosition(); | ||
break; | ||
case ROWS_QUERY: | ||
RowsQueryEventData rqed = event.getEvent().getData(); | ||
|
@@ -663,6 +679,7 @@ private void logColumnDefCastException(Table table, ColumnDefCastException e) { | |
* @return either a RowMap or null | ||
*/ | ||
public RowMap getRow() throws Exception { | ||
long txOffset; | ||
BinlogConnectorEvent event; | ||
|
||
if ( stopOnEOF && hitEOF ) | ||
|
@@ -699,29 +716,46 @@ public RowMap getRow() throws Exception { | |
} | ||
} | ||
|
||
|
||
switch (event.getType()) { | ||
case WRITE_ROWS: | ||
case EXT_WRITE_ROWS: | ||
case UPDATE_ROWS: | ||
case EXT_UPDATE_ROWS: | ||
case DELETE_ROWS: | ||
case EXT_DELETE_ROWS: | ||
LOGGER.warn("Started replication stream inside a transaction. This shouldn't normally happen."); | ||
LOGGER.warn("Assuming new transaction at unexpected event:" + event); | ||
LOGGER.warn("Started replication stream inside a transaction, probably recovering from stopping inside transaction."); | ||
|
||
queue.offerFirst(event); | ||
rowBuffer = getTransactionRows(event); | ||
rowBuffer = getTransactionRows(event, 0); | ||
break; | ||
case TABLE_MAP: | ||
TableMapEventData data = event.tableMapData(); | ||
tableCache.processEvent(getSchema(), this.filter,this.ignoreMissingSchema, data.getTableId(), data.getDatabase(), data.getTable()); | ||
|
||
if ( startPosition != null && startPosition.getTXOffset() > 0 ) { | ||
LOGGER.info("Restarting maxwell inside transaction: {}", startPosition); | ||
queue.offerFirst(event); // put table map back on top of queue | ||
rowBuffer = getTransactionRows(event, startPosition.getTXOffset()); | ||
} | ||
|
||
startPosition = null; | ||
break; | ||
case QUERY: | ||
txOffset = 0; | ||
QueryEventData qe = event.queryData(); | ||
String sql = qe.getSql(); | ||
|
||
if ( startPosition != null && startPosition.getTXOffset() > 0 ) { | ||
LOGGER.info("Restarting maxwell inside transaction: {}", startPosition); | ||
txOffset = startPosition.getTXOffset();; | ||
} | ||
|
||
startPosition = null; | ||
|
||
if (BinlogConnectorEvent.BEGIN.equals(sql)) { | ||
try { | ||
rowBuffer = getTransactionRows(event); | ||
rowBuffer = getTransactionRows(event, txOffset); | ||
} catch ( ClientReconnectedException e ) { | ||
// rowBuffer should already be empty by the time we get to this switch | ||
// statement, but we null it for clarity | ||
|
@@ -739,8 +773,17 @@ public RowMap getRow() throws Exception { | |
// in mariaDB the GTID event supplants the normal BEGIN | ||
MariadbGtidEventData g = event.mariaGtidData(); | ||
if ( (g.getFlags() & MariadbGtidEventData.FL_STANDALONE) == 0 ) { | ||
txOffset = 0; | ||
|
||
if ( startPosition != null && startPosition.getTXOffset() > 0 ) { | ||
LOGGER.info("Restarting maxwell inside transaction: {}", startPosition); | ||
txOffset = startPosition.getTXOffset();; | ||
} | ||
|
||
startPosition = null; | ||
|
||
try { | ||
rowBuffer = getTransactionRows(event); | ||
rowBuffer = getTransactionRows(event, txOffset); | ||
} catch ( ClientReconnectedException e ) { | ||
// rowBuffer should already be empty by the time we get to this switch | ||
// statement, but we null it for clarity | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we fix like this, prevent it from some nullpoint exception
Position nextPosition = null;
if(lastTableMapPosition != null){
nextPosition = Position.valueOf(lastTableMapPosition, lastHeartbeatRead, 0L);
}else{
nextPosition = Position.valueOf(this.nextPosition, lastHeartbeatRead, 0L);
}