use of com.airbnb.spinaltap.mysql.mutation.MysqlMutation in project SpinalTap by airbnb.
the class AbstractMysqlSource method commitCheckpoint.
public void commitCheckpoint(Mutation<?> mutation) {
SourceState savedState = lastSavedState.get();
if (mutation == null || savedState == null) {
return;
}
Preconditions.checkState(mutation instanceof MysqlMutation);
MysqlMutationMetadata metadata = ((MysqlMutation) mutation).getMetadata();
// Make sure we are saving at a higher watermark
if (savedState.getLastOffset() >= metadata.getId()) {
return;
}
SourceState newState = new SourceState(metadata.getTimestamp(), metadata.getId(), currentLeaderEpoch.get(), metadata.getLastTransaction().getPosition());
saveState(newState);
stateHistory.add(newState);
stateRollbackCount.set(1);
}
use of com.airbnb.spinaltap.mysql.mutation.MysqlMutation in project SpinalTap by airbnb.
the class UpdateMutationMapper method mapEvent.
@Override
protected List<MysqlMutation> mapEvent(Table table, UpdateEvent event) {
List<MysqlMutation> mutations = Lists.newArrayList();
Collection<ColumnMetadata> cols = table.getColumns().values();
List<Map.Entry<Serializable[], Serializable[]>> rows = event.getRows();
for (int position = 0; position < rows.size(); position++) {
MysqlMutationMetadata metadata = createMetadata(table, event, position);
Row previousRow = new Row(table, zip(rows.get(position).getKey(), cols));
Row newRow = new Row(table, zip(rows.get(position).getValue(), cols));
// to retain invariant that a mutation captures changes to a single PK
if (table.getPrimaryKey().isPresent() && !previousRow.getPrimaryKeyValue().equals(newRow.getPrimaryKeyValue())) {
mutations.add(new MysqlDeleteMutation(metadata, previousRow));
mutations.add(new MysqlInsertMutation(metadata, newRow));
} else {
mutations.add(new MysqlUpdateMutation(metadata, previousRow, newRow));
}
}
;
return mutations;
}
use of com.airbnb.spinaltap.mysql.mutation.MysqlMutation in project SpinalTap by airbnb.
the class KafkaDestinationTest method createMutation.
private Mutation createMutation(MutationType type) {
Mapper<com.airbnb.spinaltap.Mutation<?>, ? extends TBase<?, ?>> thriftMutationMapper = ThriftMutationMapper.create("spinaltap");
Table table = new Table(0L, TABLE, DATABASE, ImmutableList.of(new ColumnMetadata("id", ColumnDataType.LONGLONG, true, 0)), ImmutableList.of("id"));
MysqlMutationMetadata metadata = new MysqlMutationMetadata(new DataSource(HOSTNAME, 0, "service"), new BinlogFilePos(), table, 0L, 0L, 0L, null, null, 0L, 0);
Row row = new Row(table, ImmutableMap.of("id", new Column(new ColumnMetadata("id", ColumnDataType.LONGLONG, true, 0), 1L)));
MysqlMutation mutation;
switch(type) {
case INSERT:
mutation = new MysqlInsertMutation(metadata, row);
break;
case UPDATE:
mutation = new MysqlUpdateMutation(metadata, row, row);
break;
case DELETE:
mutation = new MysqlDeleteMutation(metadata, row);
break;
default:
mutation = null;
}
return (Mutation) (thriftMutationMapper.map(mutation));
}
use of com.airbnb.spinaltap.mysql.mutation.MysqlMutation in project SpinalTap by airbnb.
the class AbstractMysqlSourceTest method testCommitCheckpoint.
@Test
public void testCommitCheckpoint() throws Exception {
StateHistory stateHistory = new TestStateHistory();
TestSource source = new TestSource(stateHistory);
Row row = new Row(null, ImmutableMap.of());
BinlogFilePos filePos = new BinlogFilePos("test.txt", 18, 156);
Transaction lastTransaction = new Transaction(0L, 0L, filePos);
MysqlMutationMetadata metadata = new MysqlMutationMetadata(null, filePos, null, 0L, 0L, 0L, null, lastTransaction, 0, 0);
MysqlMutation mutation = new MysqlInsertMutation(metadata, row);
SourceState savedState = new SourceState(SAVED_TIMESTAMP, SAVED_OFFSET, 0L, BINLOG_FILE_POS);
when(stateRepository.read()).thenReturn(savedState);
source.initialize();
source.checkpoint(mutation);
assertEquals(savedState, source.getLastSavedState().get());
source.checkpoint(null);
assertEquals(savedState, source.getLastSavedState().get());
long newOffset = SAVED_OFFSET + 1;
metadata = new MysqlMutationMetadata(null, filePos, null, 0L, newOffset, 23L, null, lastTransaction, 0, 0);
mutation = new MysqlInsertMutation(metadata, row);
source.checkpoint(mutation);
assertEquals(new SourceState(23L, newOffset, 0L, filePos), source.getLastSavedState().get());
assertEquals(stateHistory.removeLast(), source.getLastSavedState().get());
}
Aggregations