feat(outbox): relay committed outbox events to Kafka
This commit is contained in:
1 parent
78f1651f6b
commit
6c3b34f73e
42 files changed
+3731
-45
No files matched your search
@@ -11,6 +11,38 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const getOutboxBacklog = `-- name: GetOutboxBacklog :one
|
||||
SELECT
|
||||
(
|
||||
SELECT count(*)
|
||||
FROM outbox_events AS pending
|
||||
WHERE pending.published_at IS NULL
|
||||
)::bigint AS pending_events,
|
||||
coalesce((
|
||||
SELECT extract(EPOCH FROM clock_timestamp() - oldest.occurred_at)
|
||||
FROM outbox_events AS oldest
|
||||
WHERE oldest.published_at IS NULL
|
||||
ORDER BY oldest.position
|
||||
LIMIT 1
|
||||
), 0)::float8 AS oldest_pending_age_seconds
|
||||
`
|
||||
|
||||
type GetOutboxBacklogRow struct {
|
||||
PendingEvents int64
|
||||
OldestPendingAgeSeconds float64
|
||||
}
|
||||
|
||||
// Reads the relay backlog for metrics without locking. Both subqueries use
|
||||
// the partial index on unpublished positions, so the cost follows the backlog
|
||||
// size, not the table size. The age comes from the database clock, the same
|
||||
// clock that set occurred_at.
|
||||
func (q *Queries) GetOutboxBacklog(ctx context.Context) (GetOutboxBacklogRow, error) {
|
||||
row := q.db.QueryRow(ctx, getOutboxBacklog)
|
||||
var i GetOutboxBacklogRow
|
||||
err := row.Scan(&i.PendingEvents, &i.OldestPendingAgeSeconds)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertOutboxEvent = `-- name: InsertOutboxEvent :exec
|
||||
INSERT INTO outbox_events (
|
||||
event_id,
|
||||
@@ -53,3 +85,124 @@ func (q *Queries) InsertOutboxEvent(ctx context.Context, arg InsertOutboxEventPa
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const lockPendingOutboxEvents = `-- name: LockPendingOutboxEvents :many
|
||||
SELECT
|
||||
pending.event_id,
|
||||
pending.event_type,
|
||||
pending.schema_version,
|
||||
pending.aggregate_type,
|
||||
pending.aggregate_id,
|
||||
pending.aggregate_version,
|
||||
pending.payload,
|
||||
pending.occurred_at
|
||||
FROM outbox_events AS pending
|
||||
WHERE
|
||||
pending.published_at IS NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM outbox_events AS earlier
|
||||
WHERE
|
||||
earlier.aggregate_type = pending.aggregate_type
|
||||
AND earlier.aggregate_id = pending.aggregate_id
|
||||
AND earlier.published_at IS NULL
|
||||
AND (earlier.aggregate_version, earlier.position)
|
||||
< (pending.aggregate_version, pending.position)
|
||||
)
|
||||
ORDER BY pending.position
|
||||
LIMIT $1
|
||||
FOR UPDATE OF pending SKIP LOCKED
|
||||
`
|
||||
|
||||
type LockPendingOutboxEventsRow struct {
|
||||
EventID pgtype.UUID
|
||||
EventType string
|
||||
SchemaVersion int32
|
||||
AggregateType string
|
||||
AggregateID pgtype.UUID
|
||||
AggregateVersion int64
|
||||
Payload []byte
|
||||
OccurredAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Locks the next relay batch. For each aggregate only its earliest unpublished
|
||||
// event (lowest aggregate_version, then position) qualifies, so a relay never
|
||||
// holds a later version while an earlier one is still pending, whether that
|
||||
// earlier row is locked by another relay or was just rolled back. Rows locked
|
||||
// by another relay are skipped without waiting. The statement snapshot can
|
||||
// only see a later version after the earlier version's transaction committed,
|
||||
// because aggregate writers check the expected version, so scanning without a
|
||||
// position watermark cannot overtake an earlier version.
|
||||
func (q *Queries) LockPendingOutboxEvents(ctx context.Context, batchLimit int32) ([]LockPendingOutboxEventsRow, error) {
|
||||
rows, err := q.db.Query(ctx, lockPendingOutboxEvents, batchLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []LockPendingOutboxEventsRow
|
||||
for rows.Next() {
|
||||
var i LockPendingOutboxEventsRow
|
||||
if err := rows.Scan(
|
||||
&i.EventID,
|
||||
&i.EventType,
|
||||
&i.SchemaVersion,
|
||||
&i.AggregateType,
|
||||
&i.AggregateID,
|
||||
&i.AggregateVersion,
|
||||
&i.Payload,
|
||||
&i.OccurredAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markOutboxEventsPublished = `-- name: MarkOutboxEventsPublished :execrows
|
||||
UPDATE outbox_events
|
||||
SET published_at = clock_timestamp()
|
||||
WHERE
|
||||
event_id = ANY($1::uuid [])
|
||||
AND published_at IS NULL
|
||||
`
|
||||
|
||||
// Marks the events that the broker acknowledged. The relay passes only IDs
|
||||
// whose rows its own transaction locked, so the statement never waits and the
|
||||
// count equals the number of distinct IDs.
|
||||
func (q *Queries) MarkOutboxEventsPublished(ctx context.Context, eventIds []pgtype.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, markOutboxEventsPublished, eventIds)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const setRelayTransactionTimeouts = `-- name: SetRelayTransactionTimeouts :exec
|
||||
SELECT
|
||||
set_config('statement_timeout', $1::text, true),
|
||||
set_config(
|
||||
'idle_in_transaction_session_timeout',
|
||||
$2::text,
|
||||
true
|
||||
)
|
||||
`
|
||||
|
||||
type SetRelayTransactionTimeoutsParams struct {
|
||||
StatementTimeoutMs string
|
||||
IdleTimeoutMs string
|
||||
}
|
||||
|
||||
// Bounds the claim transaction on the server. statement_timeout stops a
|
||||
// statement that runs too long; idle_in_transaction_session_timeout ends the
|
||||
// session, and with it the transaction and its row locks, when the client
|
||||
// stays silent longer than a publish may take, for example after the process
|
||||
// froze or lost the network without closing the connection. Both settings
|
||||
// are local to the transaction.
|
||||
func (q *Queries) SetRelayTransactionTimeouts(ctx context.Context, arg SetRelayTransactionTimeoutsParams) error {
|
||||
_, err := q.db.Exec(ctx, setRelayTransactionTimeouts, arg.StatementTimeoutMs, arg.IdleTimeoutMs)
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user