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