Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't trigger heartbeats from idle backtracking #630

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ import org.slf4j.Logger
startTimestamp = Instant.EPOCH,
startWallClock = Instant.EPOCH,
currentQueryWallClock = Instant.EPOCH,
previousQueryWallClock = Instant.EPOCH)
previousQueryWallClock = Instant.EPOCH,
idleCountBeforeHeartbeat = 0)
}

final case class QueryState(
Expand All @@ -69,7 +70,8 @@ import org.slf4j.Logger
startTimestamp: Instant,
startWallClock: Instant,
currentQueryWallClock: Instant,
previousQueryWallClock: Instant) {
previousQueryWallClock: Instant,
idleCountBeforeHeartbeat: Long) {

def backtracking: Boolean = backtrackingCount > 0

Expand Down Expand Up @@ -453,6 +455,10 @@ import org.slf4j.Logger

def nextQuery(state: QueryState): (QueryState, Option[Source[Envelope, NotUsed]]) = {
val newIdleCount = if (state.rowCount == 0) state.idleCount + 1 else 0
val newIdleCountBeforeHeartbeat =
if (state.backtracking) state.idleCountBeforeHeartbeat
else if (state.rowCount == 0) state.idleCountBeforeHeartbeat + 1
else 0
// only start tracking query wall clock (for heartbeats) after initial backtracking query
val newQueryWallClock = if (state.latestBacktracking != TimestampOffset.Zero) clock.instant() else Instant.EPOCH
val newState =
Expand All @@ -473,7 +479,8 @@ import org.slf4j.Logger
latestBacktracking = fromOffset,
backtrackingExpectFiltered = state.latestBacktrackingSeenCount,
currentQueryWallClock = newQueryWallClock,
previousQueryWallClock = state.currentQueryWallClock)
previousQueryWallClock = state.currentQueryWallClock,
idleCountBeforeHeartbeat = newIdleCountBeforeHeartbeat)
} else if (switchFromBacktracking(state)) {
// switching from backtracking
state.copy(
Expand All @@ -483,7 +490,8 @@ import org.slf4j.Logger
idleCount = newIdleCount,
backtrackingCount = 0,
currentQueryWallClock = newQueryWallClock,
previousQueryWallClock = state.currentQueryWallClock)
previousQueryWallClock = state.currentQueryWallClock,
idleCountBeforeHeartbeat = newIdleCountBeforeHeartbeat)
} else {
// continue
val newBacktrackingCount = if (state.backtracking) state.backtrackingCount + 1 else 0
Expand All @@ -495,7 +503,8 @@ import org.slf4j.Logger
backtrackingCount = newBacktrackingCount,
backtrackingExpectFiltered = state.latestBacktrackingSeenCount,
currentQueryWallClock = newQueryWallClock,
previousQueryWallClock = state.currentQueryWallClock)
previousQueryWallClock = state.currentQueryWallClock,
idleCountBeforeHeartbeat = newIdleCountBeforeHeartbeat)
}

val behindCurrentTime =
Expand Down Expand Up @@ -553,7 +562,7 @@ import org.slf4j.Logger
}

def heartbeat(state: QueryState): Option[Envelope] = {
if (state.idleCount >= 1 && state.previousQueryWallClock != Instant.EPOCH) {
if (state.idleCountBeforeHeartbeat >= 2 && state.previousQueryWallClock != Instant.EPOCH) {
// using wall clock to measure duration since the start time (database timestamp) up to idle backtracking limit
val timestamp = state.startTimestamp.plus(
JDuration.between(state.startWallClock, state.previousQueryWallClock.minus(backtrackingBehindCurrentTime)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,16 @@ final class R2dbcReadJournal(system: ExtendedActorSystem, config: Config, cfgPat
env.sequenceNr)
Nil
} else {
if (log.isDebugEnabled()) {
if (latestBacktracking.isAfter(t.timestamp))
log.debug(
"Event from query for persistenceId [{}] seqNr [{}] timestamp [{}]" +
" was before latest timestamp from backtracking or heartbeat [{}].",
env.persistenceId,
env.sequenceNr,
t.timestamp,
latestBacktracking)
}
env :: Nil
}
case _ =>
Expand Down