Search in sources :

Example 1 with BinlogFilePos

use of com.airbnb.spinaltap.mysql.BinlogFilePos in project SpinalTap by airbnb.

the class MysqlMutationMapperTest method testXid.

@Test
public void testXid() throws Exception {
    XidEvent xidEvent = new XidEvent(SERVER_ID, 15l, new BinlogFilePos("test.200", 18, 130), 0l);
    List<? extends Mutation> mutations = eventMapper.map(xidEvent);
    assertTrue(mutations.isEmpty());
    assertEquals(15L, lastTransaction.get().getTimestamp());
    verify(metrics, times(1)).transactionReceived();
}
Also used : BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos) XidEvent(com.airbnb.spinaltap.mysql.event.XidEvent) Test(org.junit.Test)

Example 2 with BinlogFilePos

use of com.airbnb.spinaltap.mysql.BinlogFilePos in project SpinalTap by airbnb.

the class CachedMysqlSchemaStore method put.

@Override
public void put(@NotNull final MysqlTableSchema schema) {
    String database = schema.getDatabase();
    String table = schema.getTable();
    BinlogFilePos binlogFilePos = schema.getBinlogFilePos();
    schemaStore.put(schema);
    if (!schemaVersionTable.contains(database, table)) {
        schemaVersionTable.put(database, table, Maps.newTreeMap());
        schemaBinlogPositionTable.put(database, table, Maps.newTreeMap());
    }
    TreeMap<Integer, MysqlTableSchema> schemaVersionMap = schemaVersionTable.get(database, table);
    TreeMap<BinlogFilePos, MysqlTableSchema> schemaTreeMap = schemaBinlogPositionTable.get(database, table);
    schemaVersionMap.put(schema.getVersion(), schema);
    schemaTreeMap.put(binlogFilePos, schema);
    invalidate(schemaVersionMap);
    invalidate(schemaTreeMap);
}
Also used : BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos)

Example 3 with BinlogFilePos

use of com.airbnb.spinaltap.mysql.BinlogFilePos in project SpinalTap by airbnb.

the class CachedMysqlSchemaStore method query.

@Override
public MysqlTableSchema query(@NotNull final String database, @NotNull final String table, @NotNull final BinlogFilePos binlogFilePos) {
    try {
        TreeMap<BinlogFilePos, MysqlTableSchema> tableSchemaTreeMap = Preconditions.checkNotNull(schemaBinlogPositionTable.get(database, table), String.format("No schema found for database: %s table: %s", database, table));
        BinlogFilePos key = Preconditions.checkNotNull(tableSchemaTreeMap.floorKey(binlogFilePos), String.format("No schema found for database: %s table: %s at binlog_pos: %s", database, table, binlogFilePos));
        metrics.schemaStoreGetSuccess(database, table);
        return tableSchemaTreeMap.get(key);
    } catch (Exception ex) {
        metrics.schemaStoreGetFailure(database, table, ex);
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
}
Also used : BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos)

Example 4 with BinlogFilePos

use of com.airbnb.spinaltap.mysql.BinlogFilePos in project SpinalTap by airbnb.

the class MysqlSchemaTracker method processDDLStatement.

public void processDDLStatement(@NotNull final QueryEvent event) {
    BinlogFilePos binlogFilePos = event.getBinlogFilePos();
    String ddl = event.getSql();
    if (schemaStore.get(binlogFilePos) != null) {
        log.info(String.format("DDL Statement (%s) has already been processed. (BinlogFilePos: %s)", ddl, binlogFilePos));
        return;
    }
    // It could be a new database which has not been created in schema store database, so don't
    // switch to any database before applying database DDL.
    schemaDatabase.applyDDLStatement(DATABASE_DDL_SQL_PATTERN.matcher(ddl).find() ? "" : event.getDatabase(), ddl);
    // Get schemas for active tables in schema store
    Table<String, String, MysqlTableSchema> activeTableSchemasInStore = Tables.newCustomTable(Maps.newHashMap(), Maps::newHashMap);
    schemaStore.getAll().values().stream().map(treeMap -> treeMap.lastEntry().getValue()).filter(schema -> !schema.getColumnInfo().isEmpty()).forEach(schema -> activeTableSchemasInStore.put(schema.getDatabase(), schema.getTable(), schema));
    Set<String> activeDatabasesInStore = activeTableSchemasInStore.rowKeySet();
    Set<String> databasesInSchemaDatabase = schemaDatabase.listDatabases();
    // Handle new databases
    Sets.difference(databasesInSchemaDatabase, activeDatabasesInStore).forEach(newDatabase -> updateSchemaStore(newDatabase, event, Maps.newHashMap(), schemaDatabase.fetchTableSchema(newDatabase)));
    // Handle existing databases
    activeDatabasesInStore.forEach(database -> updateSchemaStore(database, event, activeTableSchemasInStore.row(database), schemaDatabase.fetchTableSchema(database)));
}
Also used : QueryEvent(com.airbnb.spinaltap.mysql.event.QueryEvent) BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Set(java.util.Set) NotNull(javax.validation.constraints.NotNull) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) Tables(com.google.common.collect.Tables) Slf4j(lombok.extern.slf4j.Slf4j) Lists(com.google.common.collect.Lists) Map(java.util.Map) Pattern(java.util.regex.Pattern) Table(com.google.common.collect.Table) Maps(com.google.common.collect.Maps) BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos)

Example 5 with BinlogFilePos

use of com.airbnb.spinaltap.mysql.BinlogFilePos 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)

Aggregations

BinlogFilePos (com.airbnb.spinaltap.mysql.BinlogFilePos)7 QueryEvent (com.airbnb.spinaltap.mysql.event.QueryEvent)2 XidEvent (com.airbnb.spinaltap.mysql.event.XidEvent)2 Test (org.junit.Test)2 Mutation (com.airbnb.jitney.event.spinaltap.v1.Mutation)1 SourceState (com.airbnb.spinaltap.common.source.SourceState)1 DataSource (com.airbnb.spinaltap.mysql.DataSource)1 TableCache (com.airbnb.spinaltap.mysql.TableCache)1 Transaction (com.airbnb.spinaltap.mysql.Transaction)1 BinlogEvent (com.airbnb.spinaltap.mysql.event.BinlogEvent)1 DeleteEvent (com.airbnb.spinaltap.mysql.event.DeleteEvent)1 StartEvent (com.airbnb.spinaltap.mysql.event.StartEvent)1 TableMapEvent (com.airbnb.spinaltap.mysql.event.TableMapEvent)1 UpdateEvent (com.airbnb.spinaltap.mysql.event.UpdateEvent)1 WriteEvent (com.airbnb.spinaltap.mysql.event.WriteEvent)1 MysqlDeleteMutation (com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation)1 MysqlInsertMutation (com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation)1 MysqlMutation (com.airbnb.spinaltap.mysql.mutation.MysqlMutation)1 MysqlMutationMetadata (com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata)1 MysqlUpdateMutation (com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation)1