Redo Apply Lag
EasyStreams · Oracle
A primary database generates redo log sequences and a standby applies them. You get two monitoring streams, each a list of [time, sequence] rows sorted by time with sequence never decreasing:
generated: attime, the primary had generated up tosequence.applied: attime, the standby had applied up tosequence.
The lag at time t is G - A, where G is the sequence of the last generated row with time <= t and A is the sequence of the last applied row with time <= t; either is 0 if no such row exists. The standby never applies a sequence before it has been generated, so the lag is never negative.
queries is a list of times in any order. Return the lag at each query time, in the same order as queries.
Example 1
- Input:
- generated = [[1, 100], [4, 130], [9, 150]], applied = [[3, 90], [6, 125], [10, 150]], queries = [5, 0, 9, 12]
- Output:
- [40, 0, 25, 0]
- Explanation
- At time 5 the latest generated row is
[4, 130]and the latest applied row is[3, 90], so the lag is 40. Before time 1 neither stream has a row, so the lag is 0. At 9 it is150 - 125 = 25, and by 12 the standby has caught up.
Example 2
- Input:
- generated = [[2, 7]], applied = [[5, 7]], queries = [2, 5]
- Output:
- [7, 0]
- Explanation
- At time 2 the primary is at 7 but the standby has no row yet, so its sequence counts as 0 and the lag is 7. At 5 both are at 7.
Example 3
- Input:
- generated = [[1, 3], [1, 8]], applied = [[1, 2]], queries = [1]
- Output:
- [6]
- Explanation
- Two generated rows share time 1; the last one, 8, is the current value. The lag is
8 - 2 = 6.
Constraints
- 0 ≤ generated.length, applied.length, queries.length ≤ 105
- 0 ≤ time, query ≤ 109
- 0 ≤ sequence ≤ 1012
- Both streams are sorted by time; several rows may share a time, and the last one wins.