use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.
the class StateRepositoryTest method testRead.
@Test
public void testRead() throws Exception {
SourceState state = mock(SourceState.class);
when(repository.get()).thenReturn(state);
when(repository.exists()).thenReturn(false);
assertNull(stateRepository.read());
when(repository.exists()).thenReturn(true);
Assert.assertEquals(state, stateRepository.read());
verify(metrics, times(2)).stateRead();
}
use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.
the class MysqlEventFilterTest method testEventFilter.
@Test
public void testEventFilter() throws Exception {
TableCache tableCache = mock(TableCache.class);
BinlogEvent lastEvent = new XidEvent(0l, 0l, BINLOG_FILE_POS, 0l);
BinlogFilePos nextPosition = new BinlogFilePos("test.123", 15, 100);
SourceState state = new SourceState(0l, lastEvent.getOffset(), 0l, BINLOG_FILE_POS);
Filter<BinlogEvent> filter = MysqlEventFilter.create(tableCache, TABLE_NAMES, new AtomicReference(state));
when(tableCache.contains(TABLE_ID)).thenReturn(true);
assertTrue(filter.apply(new TableMapEvent(TABLE_ID, 0l, 0l, nextPosition, DATABASE_NAME, TABLE_NAME, new byte[1])));
assertTrue(filter.apply(new WriteEvent(TABLE_ID, 0l, 0l, nextPosition, Collections.emptyList())));
assertTrue(filter.apply(new DeleteEvent(TABLE_ID, 0l, 0l, nextPosition, Collections.emptyList())));
assertTrue(filter.apply(new UpdateEvent(TABLE_ID, 0l, 0l, nextPosition, Collections.emptyList())));
assertTrue(filter.apply(new XidEvent(0l, 0l, BINLOG_FILE_POS, 12l)));
assertTrue(filter.apply(new QueryEvent(0l, 0l, BINLOG_FILE_POS, DATABASE_NAME, "")));
assertTrue(filter.apply(new StartEvent(0l, 0l, BINLOG_FILE_POS)));
assertFalse(filter.apply(new TableMapEvent(TABLE_ID, 0l, 0l, BINLOG_FILE_POS, "", "", new byte[1])));
assertFalse(filter.apply(new WriteEvent(0l, 0l, 0l, BINLOG_FILE_POS, Collections.emptyList())));
assertFalse(filter.apply(new WriteEvent(TABLE_ID, 0l, 0l, BINLOG_FILE_POS, Collections.emptyList())));
assertFalse(filter.apply(mock(BinlogEvent.class)));
}
use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.
the class AbstractMysqlSourceTest method testOpenClose.
@Test
public void testOpenClose() throws Exception {
TestSource source = new TestSource();
SourceState savedState = new SourceState(SAVED_TIMESTAMP, SAVED_OFFSET, 0L, BINLOG_FILE_POS);
when(stateRepository.read()).thenReturn(savedState);
source.open();
Transaction lastTransaction = new Transaction(savedState.getLastTimestamp(), savedState.getLastOffset(), savedState.getLastPosition());
assertEquals(savedState, source.getLastSavedState().get());
assertEquals(lastTransaction, source.getLastTransaction().get());
assertEquals(BINLOG_FILE_POS, source.getPosition());
verify(tableCache, times(1)).clear();
}
use of com.airbnb.spinaltap.common.source.SourceState 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());
}
use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.
the class AbstractMysqlSourceTest method testResetToLastValidState.
@Test
public void testResetToLastValidState() throws Exception {
StateHistory stateHistory = new TestStateHistory();
TestSource source = new TestSource(stateHistory);
SourceState savedState = mock(SourceState.class);
SourceState earliestState = new SourceState(0L, 0L, 0L, AbstractMysqlSource.EARLIEST_BINLOG_POS);
when(stateRepository.read()).thenReturn(savedState);
SourceState firstState = mock(SourceState.class);
SourceState secondState = mock(SourceState.class);
SourceState thirdState = mock(SourceState.class);
SourceState fourthState = mock(SourceState.class);
stateHistory.add(firstState);
stateHistory.add(secondState);
stateHistory.add(thirdState);
source.initialize();
source.resetToLastValidState();
assertEquals(thirdState, source.getLastSavedState().get());
source.resetToLastValidState();
assertEquals(firstState, source.getLastSavedState().get());
assertTrue(stateHistory.isEmpty());
source.resetToLastValidState();
assertEquals(earliestState, source.getLastSavedState().get());
stateHistory.add(firstState);
stateHistory.add(secondState);
stateHistory.add(thirdState);
stateHistory.add(fourthState);
source.resetToLastValidState();
assertEquals(firstState, source.getLastSavedState().get());
stateHistory.add(firstState);
stateHistory.add(secondState);
source.resetToLastValidState();
assertEquals(earliestState, source.getLastSavedState().get());
assertTrue(stateHistory.isEmpty());
BinlogFilePos filePos = new BinlogFilePos("test.txt", 18, 156);
Transaction lastTransaction = new Transaction(0L, 0L, filePos);
MysqlMutationMetadata metadata = new MysqlMutationMetadata(null, null, null, 0L, 1L, 23L, null, lastTransaction, 0L, 0);
source.checkpoint(new MysqlInsertMutation(metadata, null));
assertFalse(stateHistory.isEmpty());
source.resetToLastValidState();
assertEquals(new SourceState(23L, 1L, 0L, filePos), source.getLastSavedState().get());
}
Aggregations