Fair routine list reads: the most overdue first
One routine read an hour went to the first channel due in the round's fixed order, so channels early in it held the slot: six channels never re-read on the new server, most others not for ten days. Now the most overdue list goes first (never-read most of all), a failed read steps aside for six hours, small reads for waiting approvals don't use the slot, and a channel the round no longer sees can't hold the queue. Budgets and backoffs unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,38 @@ internal fun poolJitterMs(number: Int): Long = (number.toLong() * 7_919L % 48L)
|
||||
/** Whether a cached list is old enough for its routine refresh. */
|
||||
internal fun poolRefreshDue(fetchedAtMs: Long, number: Int, nowMs: Long) = nowMs - fetchedAtMs >= POOL_REFRESH_MS + poolJitterMs(number)
|
||||
|
||||
/** How long a channel whose routine read failed steps aside, so it can't hold the routine slot from the others. */
|
||||
internal const val FAILED_ROUTINE_RETRY_MS = 6 * 60 * 60_000L
|
||||
/** A channel not seen by the background round for this long (switched off, or gone) is left out of the queue. */
|
||||
internal const val ROUTINE_SEEN_MS = 3 * 60 * 60_000L
|
||||
|
||||
/**
|
||||
* Who gets the routine refresh (Paul, 23 November 2026). The background round goes through the channels in a fixed order and
|
||||
* there's one routine read an hour, so the first channel due took it, and a channel early in the order that kept falling due
|
||||
* again (a partial read retried every six hours) could hold it: six channels were never read on the new server, and most
|
||||
* others not for ten days. Now the most overdue list goes first, a list never read here counting as most overdue of all; a
|
||||
* channel whose read failed steps aside for [FAILED_ROUTINE_RETRY_MS]; and only channels the round has seen recently count, so
|
||||
* one switched off can't hold the queue. The hourly budget and the backoffs are unchanged: this only decides the order.
|
||||
*/
|
||||
internal class RoutineQueue {
|
||||
private class Seen(val fetchedAtMs: Long, val atMs: Long)
|
||||
private val seen = HashMap<Int, Seen>()
|
||||
private val failedAt = HashMap<Int, Long>()
|
||||
|
||||
private fun overdue(number: Int, fetchedAtMs: Long, nowMs: Long) = nowMs - fetchedAtMs - POOL_REFRESH_MS - poolJitterMs(number)
|
||||
|
||||
@Synchronized fun note(number: Int, fetchedAtMs: Long, nowMs: Long) { seen[number] = Seen(fetchedAtMs, nowMs) }
|
||||
@Synchronized fun failed(number: Int, nowMs: Long) { failedAt[number] = nowMs }
|
||||
|
||||
/** True when [number] is the channel whose routine refresh should go next. */
|
||||
@Synchronized fun isNext(number: Int, nowMs: Long): Boolean {
|
||||
val candidates = seen.filter { (n, s) -> nowMs - s.atMs <= ROUTINE_SEEN_MS && poolRefreshDue(s.fetchedAtMs, n, nowMs) &&
|
||||
failedAt[n]?.let { nowMs - it >= FAILED_ROUTINE_RETRY_MS } != false }
|
||||
val next = candidates.entries.sortedWith(compareByDescending<Map.Entry<Int, Seen>> { overdue(it.key, it.value.fetchedAtMs, nowMs) }.thenBy { it.key }).firstOrNull()
|
||||
return next?.key == number
|
||||
}
|
||||
}
|
||||
|
||||
/** Only counts and times, so it's safe to log. Never titles, addresses or the account. */
|
||||
internal class SchedulerReport(val programmes: Int, val episodes: Int, val shows: List<String>, val requests: Int,
|
||||
val fetchMs: Long, val planMs: Long, val bytes: Long, val fromCache: Boolean, val endMs: Long,
|
||||
@@ -50,6 +82,8 @@ internal class ChannelScheduler(private val store: ScheduleStore,
|
||||
}
|
||||
/** When this TV last made a routine (weekly) list refresh; at most one per [POOL_ROUTINE_GAP_MS]. */
|
||||
@Volatile internal var lastRoutineRefreshMs = 0L
|
||||
/** The order routine refreshes go in: see [RoutineQueue]. */
|
||||
internal val routineQueue = RoutineQueue()
|
||||
/** Disk only and quick, so the guide and player agree from the first frame after a restart. */
|
||||
fun load(userId: String, loop: SampleChannel): ScheduledChannel? {
|
||||
if (store.loadProgress(userId, loop.number)?.enabled != true) return null
|
||||
@@ -376,17 +410,22 @@ internal class ChannelScheduler(private val store: ScheduleStore,
|
||||
val waiting = cached != null && now - cached.fetchedAtMs >= PARTIAL_POOL_RETRY_MS && choices.accepted.any { a ->
|
||||
a.id.startsWith(MOVED_PREFIX) && a.ref.isNotEmpty() && cached.series.none { it.id == a.id || seriesKey(it.title) == seriesKey(a.title) } &&
|
||||
!IdRepairs.coolingDown(a.type, a.ref, now) }
|
||||
val due = cached == null || cached.signature != signature || waiting ||
|
||||
(poolRefreshDue(cached.fetchedAtMs, number, now) && now - lastRoutineRefreshMs >= POOL_ROUTINE_GAP_MS)
|
||||
// The routine refresh goes to the most overdue list, not the first in the round's order ([RoutineQueue]).
|
||||
if (cached != null) routineQueue.note(number, cached.fetchedAtMs, now)
|
||||
val routineDue = cached != null && poolRefreshDue(cached.fetchedAtMs, number, now) && now - lastRoutineRefreshMs >= POOL_ROUTINE_GAP_MS &&
|
||||
routineQueue.isNext(number, now)
|
||||
val due = cached == null || cached.signature != signature || waiting || routineDue
|
||||
if (due) {
|
||||
if (routine) lastRoutineRefreshMs = now
|
||||
// Due only for approvals still waiting: those alone are read, and the rest of the pool stays as it is. A read like
|
||||
// that is small, so it doesn't use up the routine slot.
|
||||
val onlyWaiting = waiting && cached != null && cached.signature == signature && !routineDue
|
||||
if (routine && !onlyWaiting) lastRoutineRefreshMs = now
|
||||
val started = android.os.SystemClock.elapsedRealtime()
|
||||
try {
|
||||
// Due only for approvals still waiting: those alone are read, and the rest of the pool stays as it is.
|
||||
val onlyWaiting = waiting && cached != null && cached.signature == signature && !poolRefreshDue(cached.fetchedAtMs, number, now)
|
||||
val read = session.readChannelPool(programming, loop, choices, checkActive, previous = cached?.series, waitingOnly = onlyWaiting)
|
||||
requests = read.requests
|
||||
fetchMs = android.os.SystemClock.elapsedRealtime() - started
|
||||
if (read.series.isEmpty() && routineDue) routineQueue.failed(number, now)
|
||||
if (read.series.isNotEmpty()) {
|
||||
// A refresh that had to keep a few previous copies is tried again in a few hours rather than a week.
|
||||
// A read only for waiting approvals leaves the list's own refresh where it was.
|
||||
@@ -395,6 +434,7 @@ internal class ChannelScheduler(private val store: ScheduleStore,
|
||||
val signed = keepRefound(userId, programming, read)
|
||||
pool = ScheduleStore.SavedPool(read.series, fetched, signed)
|
||||
store.savePool(userId, number, pool)
|
||||
routineQueue.note(number, fetched, now)
|
||||
// Stage C, report only: episodes a show here didn't have at the previous read. Never touches the plan.
|
||||
if (cached != null) runCatching {
|
||||
val found = newEpisodes(number, cached.series, read.series, now)
|
||||
@@ -403,7 +443,12 @@ internal class ChannelScheduler(private val store: ScheduleStore,
|
||||
}
|
||||
} catch (failure: EmbyCheckException) {
|
||||
Log.w("OrbitSchedule", "channel=$number poolReadFailed status=${failure.httpStatus} retryable=${failure.retryable} keptCache=${cached != null}")
|
||||
if (routineDue) routineQueue.failed(number, now)
|
||||
if (cached == null) throw failure
|
||||
} catch (other: Exception) {
|
||||
// Paced out, or anything else: this channel steps aside for a while, so it can't hold the routine slot.
|
||||
if (routineDue && other !is kotlinx.coroutines.CancellationException) routineQueue.failed(number, now)
|
||||
throw other
|
||||
}
|
||||
}
|
||||
val started = android.os.SystemClock.elapsedRealtime()
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package tv.orbit.classic
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
/** Fair routine refreshes (Paul, 23 November 2026): the most overdue list first, and nobody holds the slot. */
|
||||
class RoutineQueueTest {
|
||||
private val hour = 60 * 60_000L
|
||||
private val day = 24 * hour
|
||||
private val now = 1_795_000_000_000L
|
||||
|
||||
@Test fun aNeverReadListGoesFirstThenTheMostOverdue() {
|
||||
val q = RoutineQueue()
|
||||
q.note(305, now - 8 * day - 3 * day, now) // After Dark, long overdue
|
||||
q.note(102, 0L, now) // Orbit 2, never read here
|
||||
q.note(101, now - 10 * day, now)
|
||||
q.note(106, now - day, now) // not due
|
||||
assertTrue(q.isNext(102, now))
|
||||
assertFalse(q.isNext(305, now)); assertFalse(q.isNext(101, now)); assertFalse(q.isNext(106, now))
|
||||
q.note(102, now, now) // read
|
||||
val next = listOf(305, 101).single { q.isNext(it, now) }
|
||||
val other = listOf(305, 101).single { it != next }
|
||||
q.note(next, now, now)
|
||||
assertTrue(q.isNext(other, now))
|
||||
}
|
||||
|
||||
@Test fun aChannelWhoseReadFailsStepsAside() {
|
||||
val q = RoutineQueue()
|
||||
q.note(305, 0L, now); q.note(101, now - 20 * day, now)
|
||||
assertTrue(q.isNext(305, now))
|
||||
q.failed(305, now)
|
||||
assertTrue(q.isNext(101, now))
|
||||
assertFalse(q.isNext(305, now + hour))
|
||||
q.note(101, now + hour, now + FAILED_ROUTINE_RETRY_MS); q.note(305, 0L, now + FAILED_ROUTINE_RETRY_MS)
|
||||
assertTrue(q.isNext(305, now + FAILED_ROUTINE_RETRY_MS))
|
||||
}
|
||||
|
||||
@Test fun aChannelTheRoundNoLongerSeesCantHoldTheQueue() {
|
||||
val q = RoutineQueue()
|
||||
q.note(999, 0L, now) // switched off after this
|
||||
q.note(101, now - 20 * day, now + 4 * hour)
|
||||
assertTrue(q.isNext(101, now + 4 * hour))
|
||||
assertEquals(false, q.isNext(999, now + 4 * hour))
|
||||
}
|
||||
|
||||
@Test fun nothingDueMeansNobody() {
|
||||
val q = RoutineQueue()
|
||||
q.note(101, now, now)
|
||||
assertFalse(q.isNext(101, now))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user