Search in sources :

Example 11 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.

the class StateRepository method read.

/**
 * Reads the source state from the repository. Returns a new state if one does not exist
 */
public SourceState read() {
    SourceState state = null;
    try {
        if (repository.exists()) {
            state = repository.get();
        } else {
            log.info("State does not exist for source {}", sourceName);
        }
    } catch (Exception ex) {
        log.error("Failed to read state for source " + sourceName, ex);
        metrics.stateReadFailure(ex);
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
    log.debug("Read state for source {}. state={}", sourceName, state);
    metrics.stateRead();
    return state;
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState)

Example 12 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.

the class StateHistoryTest method test.

@Test
public void test() throws Exception {
    SourceState firstState = mock(SourceState.class);
    SourceState secondState = mock(SourceState.class);
    SourceState thirdState = mock(SourceState.class);
    SourceState fourthState = mock(SourceState.class);
    TestRepository repository = new TestRepository(firstState);
    StateHistory history = new StateHistory(SOURCE_NAME, 2, repository, metrics);
    history.add(secondState);
    assertEquals(Arrays.asList(firstState, secondState), repository.get());
    history.add(thirdState);
    history.add(fourthState);
    assertEquals(Arrays.asList(thirdState, fourthState), repository.get());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Test(org.junit.Test)

Example 13 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.

the class StateHistoryTest method testRemoveMoreElementsThanInHistory.

@Test(expected = IllegalStateException.class)
public void testRemoveMoreElementsThanInHistory() throws Exception {
    SourceState firstState = mock(SourceState.class);
    SourceState secondState = mock(SourceState.class);
    TestRepository repository = new TestRepository(firstState, secondState);
    StateHistory history = new StateHistory(SOURCE_NAME, 2, repository, metrics);
    history.removeLast(3);
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Test(org.junit.Test)

Example 14 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.

the class StateHistoryTest method testEmptyHistory.

@Test
public void testEmptyHistory() throws Exception {
    SourceState state = mock(SourceState.class);
    TestRepository repository = new TestRepository();
    StateHistory history = new StateHistory(SOURCE_NAME, 2, repository, metrics);
    assertTrue(history.isEmpty());
    repository = new TestRepository(state);
    history = new StateHistory(SOURCE_NAME, 2, repository, metrics);
    assertFalse(history.isEmpty());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Test(org.junit.Test)

Example 15 with SourceState

use of com.airbnb.spinaltap.common.source.SourceState in project SpinalTap by airbnb.

the class StateHistoryTest method testRemoveLastFromHistory.

@Test
public void testRemoveLastFromHistory() throws Exception {
    SourceState firstState = mock(SourceState.class);
    SourceState secondState = mock(SourceState.class);
    SourceState thirdState = mock(SourceState.class);
    TestRepository repository = new TestRepository(firstState, secondState, thirdState);
    StateHistory history = new StateHistory(SOURCE_NAME, 3, repository, metrics);
    assertEquals(thirdState, history.removeLast());
    assertEquals(secondState, history.removeLast());
    assertEquals(firstState, history.removeLast());
    assertTrue(history.isEmpty());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) 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