Search in sources :

Example 16 with SourceState

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();
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Test(org.junit.Test)

Example 17 with SourceState

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)));
}
Also used : WriteEvent(com.airbnb.spinaltap.mysql.event.WriteEvent) SourceState(com.airbnb.spinaltap.common.source.SourceState) TableMapEvent(com.airbnb.spinaltap.mysql.event.TableMapEvent) TableCache(com.airbnb.spinaltap.mysql.TableCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) BinlogEvent(com.airbnb.spinaltap.mysql.event.BinlogEvent) QueryEvent(com.airbnb.spinaltap.mysql.event.QueryEvent) DeleteEvent(com.airbnb.spinaltap.mysql.event.DeleteEvent) BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos) StartEvent(com.airbnb.spinaltap.mysql.event.StartEvent) XidEvent(com.airbnb.spinaltap.mysql.event.XidEvent) UpdateEvent(com.airbnb.spinaltap.mysql.event.UpdateEvent) Test(org.junit.Test)

Example 18 with SourceState

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();
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Test(org.junit.Test)

Example 19 with SourceState

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

Example 20 with SourceState

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

Aggregations

SourceState (com.airbnb.spinaltap.common.source.SourceState)21 Test (org.junit.Test)16 MysqlMutationMetadata (com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata)3 MysqlInsertMutation (com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation)2 MysqlMutation (com.airbnb.spinaltap.mysql.mutation.MysqlMutation)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Repository (com.airbnb.spinaltap.common.util.Repository)1 BinlogFilePos (com.airbnb.spinaltap.mysql.BinlogFilePos)1 TableCache (com.airbnb.spinaltap.mysql.TableCache)1 BinlogEvent (com.airbnb.spinaltap.mysql.event.BinlogEvent)1 DeleteEvent (com.airbnb.spinaltap.mysql.event.DeleteEvent)1 QueryEvent (com.airbnb.spinaltap.mysql.event.QueryEvent)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 XidEvent (com.airbnb.spinaltap.mysql.event.XidEvent)1 Row (com.airbnb.spinaltap.mysql.mutation.schema.Row)1 Collection (java.util.Collection)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1