Search in sources :

Example 6 with SourceState

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

the class AbstractMysqlSourceTest method testGetState.

@Test
public void testGetState() throws Exception {
    TestSource source = new TestSource();
    SourceState savedState = mock(SourceState.class);
    when(stateRepository.read()).thenReturn(savedState);
    source.initialize();
    SourceState state = source.getSavedState();
    assertEquals(savedState, state);
    when(stateRepository.read()).thenReturn(null);
    state = source.getSavedState();
    assertEquals(0L, state.getLastOffset());
    assertEquals(0L, state.getLastTimestamp());
    assertEquals(AbstractMysqlSource.LATEST_BINLOG_POS, state.getLastPosition());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) Test(org.junit.Test)

Example 7 with SourceState

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

the class AbstractMysqlSource method initialize.

protected void initialize() {
    tableCache.clear();
    SourceState state = getSavedState();
    lastSavedState.set(state);
    lastTransaction.set(new Transaction(state.getLastTimestamp(), state.getLastOffset(), state.getLastPosition()));
    setPosition(state.getLastPosition());
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState)

Example 8 with SourceState

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

the class StateHistory method removeLast.

public SourceState removeLast(int count) {
    Preconditions.checkArgument(count > 0, "Count should be greater than 0");
    Preconditions.checkState(!stateHistory.isEmpty(), "The state history is empty");
    Preconditions.checkState(stateHistory.size() >= count, "Count is larger than history size");
    SourceState state = stateHistory.removeLast();
    for (int i = 1; i < count; i++) {
        state = stateHistory.removeLast();
    }
    save();
    return state;
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState)

Example 9 with SourceState

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

the class AbstractMysqlSource method commitCheckpoint.

public void commitCheckpoint(Mutation<?> mutation) {
    SourceState savedState = lastSavedState.get();
    if (mutation == null || savedState == null) {
        return;
    }
    Preconditions.checkState(mutation instanceof MysqlMutation);
    MysqlMutationMetadata metadata = ((MysqlMutation) mutation).getMetadata();
    // Make sure we are saving at a higher watermark
    if (savedState.getLastOffset() >= metadata.getId()) {
        return;
    }
    SourceState newState = new SourceState(metadata.getTimestamp(), metadata.getId(), currentLeaderEpoch.get(), metadata.getLastTransaction().getPosition());
    saveState(newState);
    stateHistory.add(newState);
    stateRollbackCount.set(1);
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata)

Example 10 with SourceState

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

the class AbstractMysqlSource method resetToLastValidState.

/**
 * Resets the source to the last valid state
 */
void resetToLastValidState() {
    if (stateHistory.size() >= stateRollbackCount.get()) {
        SourceState newState = stateHistory.removeLast(stateRollbackCount.get());
        saveState(newState);
        metrics.resetSourcePosition();
        log.info("Reset source {} position to {}.", name, newState.getLastPosition());
        stateRollbackCount.accumulateAndGet(STATE_ROLLBACK_BACKOFF_RATE, (value, rate) -> value * rate);
    } else {
        stateHistory.clear();
        saveState(getEarliestState());
        metrics.resetEarliestPosition();
        log.info("Reset source {} position to earliest.", name);
    }
}
Also used : SourceState(com.airbnb.spinaltap.common.source.SourceState)

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