perf(store-and-forward): rank sweep lanes over a covering narrow index, join payloads for the page only (SF-3, WP6 task 6.3)
The fair-paging sweep computed ROW_NUMBER() over every column of every due
row, so each sweep sorted the whole due backlog with payloads into temp
storage. The ranking now projects only id + lane keys over the new covering
idx_sf_messages_lane (status, category, target, created_at,
last_attempt_at_ms, retry_interval_ms, id), takes the page of ids, and joins
back by primary key. The index is created in place by StoreAndForwardSchema
(idempotent, local only — not part of CDC capture). idx_sf_messages_category
is kept (still used by parked-count filters).
EXPLAIN QUERY PLAN + timing, sqlite3 3.54, 200,000 due rows x ~1 KB payload,
20 targets x 3 categories, LIMIT 500:
before: SEARCH idx_sf_messages_status_created (status=?) + USE TEMP B-TREE
FOR ORDER BY (window over full rows) + outer temp b-tree; 1.29 s
after: SEARCH USING COVERING INDEX idx_sf_messages_lane (no window sort),
outer temp b-tree over (id, rank, created_at) only, PK join of 500
rows; 0.15 s
This commit is contained in:
@@ -129,7 +129,7 @@ The telemetry-emitting `ICachedCallLifecycleObserver` hook is dispatched through
|
|||||||
|
|
||||||
### Retry Sweep Indexing
|
### Retry Sweep Indexing
|
||||||
|
|
||||||
`GetMessagesForRetryAsync` orders candidates `ORDER BY created_at ASC` within the due `status`. The existing `idx_sf_messages_status_due (status, last_attempt_at_ms)` matches the status filter and due-time predicate but not this ordering, forcing a sort/scan on a large sweep. The sweep page is ranked **per `(category, target)` lane** (`ROW_NUMBER() OVER (PARTITION BY category, target ORDER BY created_at)`, ordered by lane rank then `created_at`, limited to `SweepBatchLimit`), so a dead target with hundreds of due rows contributes one row per round like every other lane instead of owning the whole page and starving healthy targets (review 2026-10-21 02/C2); per-target FIFO is preserved. A second covering index, `idx_sf_messages_status_created (status, created_at)`, lets the sweep walk matching rows already in `created_at` order and stop at the batch limit without re-sorting or touching non-pending rows (arch-review WP1.4). Both indexes are retained — `idx_sf_messages_status_due` still backs status+due-time lookups that don't order by `created_at`.
|
`GetMessagesForRetryAsync` orders candidates `ORDER BY created_at ASC` within the due `status`. The existing `idx_sf_messages_status_due (status, last_attempt_at_ms)` matches the status filter and due-time predicate but not this ordering, forcing a sort/scan on a large sweep. The sweep page is ranked **per `(category, target)` lane** (`ROW_NUMBER() OVER (PARTITION BY category, target ORDER BY created_at)`, ordered by lane rank then `created_at`, limited to `SweepBatchLimit`), so a dead target with hundreds of due rows contributes one row per round like every other lane instead of owning the whole page and starving healthy targets (review 2026-10-21 02/C2); per-target FIFO is preserved. A second covering index, `idx_sf_messages_status_created (status, created_at)`, lets the sweep walk matching rows already in `created_at` order and stop at the batch limit without re-sorting or touching non-pending rows (arch-review WP1.4). Both indexes are retained — `idx_sf_messages_status_due` still backs status+due-time lookups that don't order by `created_at`. **SF-3 (2026-12-03):** the lane ranking runs over a narrow projection (`id` + lane keys) backed by the covering `idx_sf_messages_lane (status, category, target, created_at, last_attempt_at_ms, retry_interval_ms, id)`, so the window needs no sort and never reads a payload; only the chosen page of ids is joined back for full rows (200 000 due 1 KB rows: 1.29 s → 0.15 s per sweep). The index is created in place by `StoreAndForwardSchema.Apply`.
|
||||||
|
|
||||||
## Parked Message Management
|
## Parked Message Management
|
||||||
|
|
||||||
|
|||||||
@@ -111,6 +111,19 @@ public static class StoreAndForwardSchema
|
|||||||
"CREATE INDEX IF NOT EXISTS idx_sf_messages_status_created ON sf_messages(status, created_at)";
|
"CREATE INDEX IF NOT EXISTS idx_sf_messages_status_created ON sf_messages(status, created_at)";
|
||||||
orderIndex.ExecuteNonQuery();
|
orderIndex.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SF-3: covering index for the sweep's fair lane ranking. Leading (status, category,
|
||||||
|
// target, created_at) matches ROW_NUMBER() OVER (PARTITION BY category, target ORDER BY
|
||||||
|
// created_at) so the window reads rows already in lane order with no sort, and the
|
||||||
|
// trailing due-predicate columns + id make it covering, so the ranking never reads a
|
||||||
|
// payload. Applied in place on every boot (idempotent); local only — indexes are not
|
||||||
|
// part of LocalDb's CDC capture.
|
||||||
|
using (var laneIndex = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
laneIndex.CommandText =
|
||||||
|
"CREATE INDEX IF NOT EXISTS idx_sf_messages_lane ON sf_messages(status, category, target, created_at, last_attempt_at_ms, retry_interval_ms, id)";
|
||||||
|
laneIndex.ExecuteNonQuery();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -211,16 +211,28 @@ public class StoreAndForwardStorage
|
|||||||
// hundreds of due rows contributes one row per "round" like every other
|
// hundreds of due rows contributes one row per "round" like every other
|
||||||
// lane instead of owning the whole page. Within a lane rank order == created_at
|
// lane instead of owning the whole page. Within a lane rank order == created_at
|
||||||
// order, so per-target FIFO is preserved for the service's GroupBy.
|
// order, so per-target FIFO is preserved for the service's GroupBy.
|
||||||
|
//
|
||||||
|
// SF-3: the ranking runs over a NARROW projection (id + lane keys) that
|
||||||
|
// idx_sf_messages_lane covers in (category, target, created_at) order, so the window
|
||||||
|
// needs no sort and never touches a payload; only the chosen page of ids is joined
|
||||||
|
// back for the full rows. Ranking over every column used to sort the whole due
|
||||||
|
// backlog — payloads included — on every sweep.
|
||||||
cmd.CommandText = $@"
|
cmd.CommandText = $@"
|
||||||
SELECT {cols}
|
WITH page AS (
|
||||||
FROM (
|
SELECT id, lane_rank, created_at
|
||||||
SELECT {cols},
|
FROM (
|
||||||
ROW_NUMBER() OVER (PARTITION BY category, target ORDER BY created_at ASC) AS lane_rank
|
SELECT id, created_at,
|
||||||
FROM sf_messages
|
ROW_NUMBER() OVER (PARTITION BY category, target ORDER BY created_at ASC) AS lane_rank
|
||||||
WHERE {due}
|
FROM sf_messages
|
||||||
|
WHERE {due}
|
||||||
|
)
|
||||||
|
ORDER BY lane_rank ASC, created_at ASC
|
||||||
|
LIMIT @limit
|
||||||
)
|
)
|
||||||
ORDER BY lane_rank ASC, created_at ASC
|
SELECT {SweepColumnsM}
|
||||||
LIMIT @limit";
|
FROM page p
|
||||||
|
JOIN sf_messages m ON m.id = p.id
|
||||||
|
ORDER BY p.lane_rank ASC, p.created_at ASC";
|
||||||
cmd.Parameters.AddWithValue("@limit", limit);
|
cmd.Parameters.AddWithValue("@limit", limit);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -238,6 +250,15 @@ public class StoreAndForwardStorage
|
|||||||
return await ReadMessagesAsync(cmd);
|
return await ReadMessagesAsync(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>The sweep's column list qualified with the <c>m</c> alias (SF-3 join-back).</summary>
|
||||||
|
private static readonly string SweepColumnsM =
|
||||||
|
string.Join(", ", new[]
|
||||||
|
{
|
||||||
|
"id", "category", "target", "payload_json", "retry_count", "max_retries",
|
||||||
|
"retry_interval_ms", "created_at", "last_attempt_at", "status", "last_error", "origin_instance",
|
||||||
|
"execution_id", "source_script", "parent_execution_id",
|
||||||
|
}.Select(c => "m." + c));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates a message after a delivery attempt.
|
/// Updates a message after a delivery attempt.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -853,6 +853,31 @@ public class StoreAndForwardStorageTests : IAsyncLifetime, IDisposable
|
|||||||
Assert.Equal("a000", dead[0]);
|
Assert.Equal("a000", dead[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SF-3 (WP6 6.3): the sweep ranks lanes over the covering lane index (no payload read, no
|
||||||
|
/// sort for the window) and joins back only the chosen page by primary key.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||||
|
[Fact]
|
||||||
|
public async Task GetMessagesForRetry_RanksOverTheCoveringLaneIndex()
|
||||||
|
{
|
||||||
|
await using var connection = _localDb.Db.CreateConnection();
|
||||||
|
await using var cmd = connection.CreateCommand();
|
||||||
|
cmd.CommandText = @"EXPLAIN QUERY PLAN
|
||||||
|
SELECT id, created_at,
|
||||||
|
ROW_NUMBER() OVER (PARTITION BY category, target ORDER BY created_at ASC) AS lane_rank
|
||||||
|
FROM sf_messages
|
||||||
|
WHERE status = 0
|
||||||
|
AND (last_attempt_at_ms IS NULL OR retry_interval_ms = 0 OR (1 - last_attempt_at_ms) >= retry_interval_ms)";
|
||||||
|
var plan = new List<string>();
|
||||||
|
await using (var reader = await cmd.ExecuteReaderAsync())
|
||||||
|
while (await reader.ReadAsync())
|
||||||
|
plan.Add(reader.GetString(3));
|
||||||
|
|
||||||
|
Assert.Contains(plan, p => p.Contains("COVERING INDEX idx_sf_messages_lane"));
|
||||||
|
Assert.DoesNotContain(plan, p => p.Contains("TEMP B-TREE"));
|
||||||
|
}
|
||||||
|
|
||||||
// ReplaceAll_SwapsTheEntireBuffer_Atomically was DELETED with ReplaceAllAsync in
|
// ReplaceAll_SwapsTheEntireBuffer_Atomically was DELETED with ReplaceAllAsync in
|
||||||
// LocalDb Phase 2, and deliberately not replaced. It asserted a destructive
|
// LocalDb Phase 2, and deliberately not replaced. It asserted a destructive
|
||||||
// delete-all-then-insert-all, which was the standby's anti-entropy apply: a resync
|
// delete-all-then-insert-all, which was the standby's anti-entropy apply: a resync
|
||||||
|
|||||||
Reference in New Issue
Block a user