fix(routing): 修正 Worker 心跳时间查询

生产 shadow 发布证明 PostgreSQL 将未显式定型的时间参数推断为 interval,导致 Worker 列表和容量查询返回 SQLSTATE 42883。

改为在 Go 中计算心跳截止时间并以 timestamptz 参数查询,补充真实 PostgreSQL 集成回归。
This commit is contained in:
2026-08-05 22:57:33 +08:00
parent 7786692d32
commit 21f72da7a8
2 changed files with 27 additions and 5 deletions
+6 -4
View File
@@ -101,14 +101,15 @@ func (s *Store) ListWorkers(ctx context.Context, now time.Time) ([]executionpool
if now.IsZero() { if now.IsZero() {
now = time.Now() now = time.Now()
} }
cutoff := now.Add(-workerHeartbeatStaleAfter)
rows, err := s.pool.Query(ctx, ` rows, err := s.pool.Query(ctx, `
SELECT worker_id, instance_id, pool_id, endpoint, protocol_version, revision, SELECT worker_id, instance_id, pool_id, endpoint, protocol_version, revision,
capabilities, allocated_capacity, safe_capacity, heavy_capacity, capabilities, allocated_capacity, safe_capacity, heavy_capacity,
active_tasks, pressure_state, heartbeat_at, load_sampled_at active_tasks, pressure_state, heartbeat_at, load_sampled_at
FROM gateway_worker_instances FROM gateway_worker_instances
WHERE status = 'active' WHERE status = 'active'
AND heartbeat_at > $1 - $2::interval AND heartbeat_at > $1::timestamptz
ORDER BY pool_id, instance_id`, now, workerHeartbeatStaleAfter.String()) ORDER BY pool_id, instance_id`, cutoff)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -346,6 +347,7 @@ func (s *Store) ListCapacity(ctx context.Context, now time.Time) ([]executionpoo
if now.IsZero() { if now.IsZero() {
now = time.Now() now = time.Now()
} }
cutoff := now.Add(-workerHeartbeatStaleAfter)
rows, err := s.pool.Query(ctx, ` rows, err := s.pool.Query(ctx, `
SELECT pool.pool_id, SELECT pool.pool_id,
COUNT(worker.instance_id)::int, COUNT(worker.instance_id)::int,
@@ -360,10 +362,10 @@ FROM gateway_execution_pools pool
LEFT JOIN gateway_worker_instances worker LEFT JOIN gateway_worker_instances worker
ON worker.pool_id = pool.pool_id ON worker.pool_id = pool.pool_id
AND worker.status = 'active' AND worker.status = 'active'
AND worker.heartbeat_at > $1 - $2::interval AND worker.heartbeat_at > $2::timestamptz
WHERE pool.state = 'active' WHERE pool.state = 'active'
GROUP BY pool.pool_id GROUP BY pool.pool_id
ORDER BY pool.pool_id`, now, workerHeartbeatStaleAfter.String()) ORDER BY pool.pool_id`, now, cutoff)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -0,0 +1,20 @@
package store
import (
"context"
"testing"
"time"
)
func TestExecutionPoolQueriesUseTimestampCutoff(t *testing.T) {
db := billingV2IntegrationStore(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if _, err := db.ListWorkers(ctx, time.Now()); err != nil {
t.Fatalf("list workers with timestamp cutoff: %v", err)
}
if _, err := db.ListCapacity(ctx, time.Now()); err != nil {
t.Fatalf("list capacity with timestamp cutoff: %v", err)
}
}