Search in sources :

Example 1 with MysqlMutation

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);
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata)

Example 2 with MysqlMutation

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;
}
Also used : MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) ColumnMetadata(com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata) Serializable(java.io.Serializable) MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row)

Example 3 with MysqlMutation

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));
}
Also used : MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) ColumnMetadata(com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata) MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata) Table(com.airbnb.spinaltap.mysql.mutation.schema.Table) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) DataSource(com.airbnb.spinaltap.mysql.DataSource) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) Column(com.airbnb.spinaltap.mysql.mutation.schema.Column) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) Mutation(com.airbnb.jitney.event.spinaltap.v1.Mutation) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row)

Example 4 with MysqlMutation

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());
}
Also used : MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) SourceState(com.airbnb.spinaltap.common.source.SourceState) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Test(org.junit.Test)

Aggregations

MysqlMutation (com.airbnb.spinaltap.mysql.mutation.MysqlMutation)4 MysqlMutationMetadata (com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata)4 MysqlInsertMutation (com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation)3 Row (com.airbnb.spinaltap.mysql.mutation.schema.Row)3 SourceState (com.airbnb.spinaltap.common.source.SourceState)2 MysqlDeleteMutation (com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation)2 MysqlUpdateMutation (com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation)2 ColumnMetadata (com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata)2 Mutation (com.airbnb.jitney.event.spinaltap.v1.Mutation)1 BinlogFilePos (com.airbnb.spinaltap.mysql.BinlogFilePos)1 DataSource (com.airbnb.spinaltap.mysql.DataSource)1 Column (com.airbnb.spinaltap.mysql.mutation.schema.Column)1 Table (com.airbnb.spinaltap.mysql.mutation.schema.Table)1 Serializable (java.io.Serializable)1 Test (org.junit.Test)1