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();
}
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);
}
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);
}
}
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)));
}
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));
}
Aggregations