feat(presence): every service re-stamps a presence record in the State Store
This commit is contained in:
20 files changed
+1461
-660
No files matched your search
@@ -0,0 +1,19 @@
|
||||
-- One row per running service instance, re-stamped on the restamp interval.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
CREATE TABLE presence (
|
||||
service text NOT NULL,
|
||||
instance_id text NOT NULL,
|
||||
version text NOT NULL,
|
||||
install_type text NOT NULL,
|
||||
started_at timestamptz NOT NULL,
|
||||
reported_at timestamptz NOT NULL,
|
||||
ready boolean NOT NULL,
|
||||
detail text NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (service, instance_id)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DROP TABLE presence;
|
||||
@@ -277,6 +277,17 @@ type PlaybookDispatch struct {
|
||||
DispatchedAt time.Time
|
||||
}
|
||||
|
||||
type Presence struct {
|
||||
Service string
|
||||
InstanceID string
|
||||
Version string
|
||||
InstallType string
|
||||
StartedAt time.Time
|
||||
ReportedAt time.Time
|
||||
Ready bool
|
||||
Detail string
|
||||
}
|
||||
|
||||
type Resource struct {
|
||||
Kind string
|
||||
Value []byte
|
||||
|
||||
@@ -36,6 +36,15 @@ func (q *Queries) DeleteChannelStatusNotIn(ctx context.Context, dollar_1 []strin
|
||||
return err
|
||||
}
|
||||
|
||||
const deletePresenceBefore = `-- name: DeletePresenceBefore :exec
|
||||
DELETE FROM presence WHERE reported_at < $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeletePresenceBefore(ctx context.Context, reportedAt time.Time) error {
|
||||
_, err := q.db.Exec(ctx, deletePresenceBefore, reportedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const listApplyStatus = `-- name: ListApplyStatus :many
|
||||
SELECT process, kind, applied_version, error, reported_at
|
||||
FROM apply_status ORDER BY process, kind
|
||||
@@ -100,6 +109,40 @@ func (q *Queries) ListChannelStatus(ctx context.Context) ([]ChannelStatus, error
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listPresence = `-- name: ListPresence :many
|
||||
SELECT service, instance_id, version, install_type, started_at, reported_at, ready, detail
|
||||
FROM presence ORDER BY service, instance_id
|
||||
`
|
||||
|
||||
func (q *Queries) ListPresence(ctx context.Context) ([]Presence, error) {
|
||||
rows, err := q.db.Query(ctx, listPresence)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Presence
|
||||
for rows.Next() {
|
||||
var i Presence
|
||||
if err := rows.Scan(
|
||||
&i.Service,
|
||||
&i.InstanceID,
|
||||
&i.Version,
|
||||
&i.InstallType,
|
||||
&i.StartedAt,
|
||||
&i.ReportedAt,
|
||||
&i.Ready,
|
||||
&i.Detail,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const upsertApplyStatus = `-- name: UpsertApplyStatus :exec
|
||||
INSERT INTO apply_status (process, kind, applied_version, error, reported_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
@@ -186,3 +229,40 @@ func (q *Queries) UpsertConfigSnapshot(ctx context.Context, arg UpsertConfigSnap
|
||||
_, err := q.db.Exec(ctx, upsertConfigSnapshot, arg.Schema, arg.Document, arg.PublishedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const upsertPresence = `-- name: UpsertPresence :exec
|
||||
INSERT INTO presence (service, instance_id, version, install_type, started_at, reported_at, ready, detail)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (service, instance_id) DO UPDATE SET
|
||||
version = excluded.version,
|
||||
install_type = excluded.install_type,
|
||||
started_at = excluded.started_at,
|
||||
reported_at = excluded.reported_at,
|
||||
ready = excluded.ready,
|
||||
detail = excluded.detail
|
||||
`
|
||||
|
||||
type UpsertPresenceParams struct {
|
||||
Service string
|
||||
InstanceID string
|
||||
Version string
|
||||
InstallType string
|
||||
StartedAt time.Time
|
||||
ReportedAt time.Time
|
||||
Ready bool
|
||||
Detail string
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertPresence(ctx context.Context, arg UpsertPresenceParams) error {
|
||||
_, err := q.db.Exec(ctx, upsertPresence,
|
||||
arg.Service,
|
||||
arg.InstanceID,
|
||||
arg.Version,
|
||||
arg.InstallType,
|
||||
arg.StartedAt,
|
||||
arg.ReportedAt,
|
||||
arg.Ready,
|
||||
arg.Detail,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -42,3 +42,21 @@ ON CONFLICT (process, kind) DO UPDATE SET
|
||||
-- name: ListApplyStatus :many
|
||||
SELECT process, kind, applied_version, error, reported_at
|
||||
FROM apply_status ORDER BY process, kind;
|
||||
|
||||
-- name: UpsertPresence :exec
|
||||
INSERT INTO presence (service, instance_id, version, install_type, started_at, reported_at, ready, detail)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (service, instance_id) DO UPDATE SET
|
||||
version = excluded.version,
|
||||
install_type = excluded.install_type,
|
||||
started_at = excluded.started_at,
|
||||
reported_at = excluded.reported_at,
|
||||
ready = excluded.ready,
|
||||
detail = excluded.detail;
|
||||
|
||||
-- name: DeletePresenceBefore :exec
|
||||
DELETE FROM presence WHERE reported_at < $1;
|
||||
|
||||
-- name: ListPresence :many
|
||||
SELECT service, instance_id, version, install_type, started_at, reported_at, ready, detail
|
||||
FROM presence ORDER BY service, instance_id;
|
||||
@@ -123,3 +123,43 @@ func (s *Store) ListApplyStatus(ctx context.Context) ([]storecontract.ApplyStatu
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PutPresence replaces one instance's record and drops records whose
|
||||
// instance stopped re-stamping long ago, so restarts do not accumulate rows.
|
||||
func (s *Store) PutPresence(ctx context.Context, presence storecontract.Presence) error {
|
||||
reported := presence.ReportedAt
|
||||
if reported.IsZero() {
|
||||
reported = time.Now()
|
||||
}
|
||||
q := s.queries()
|
||||
if err := q.UpsertPresence(ctx, postgresdb.UpsertPresenceParams{
|
||||
Service: presence.Service, InstanceID: presence.InstanceID, Version: presence.Version,
|
||||
InstallType: presence.InstallType, StartedAt: presence.StartedAt.UTC(), ReportedAt: reported.UTC(),
|
||||
Ready: presence.Ready, Detail: presence.Detail,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("store: put presence: %w", err)
|
||||
}
|
||||
if err := q.DeletePresenceBefore(ctx, reported.Add(-presenceRetention).UTC()); err != nil {
|
||||
return fmt.Errorf("store: prune presence: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// presenceRetention is how long a stopped instance stays listed as down.
|
||||
const presenceRetention = 24 * time.Hour
|
||||
|
||||
// ListPresence returns every instance's record, ordered by (service, instance).
|
||||
func (s *Store) ListPresence(ctx context.Context) ([]storecontract.Presence, error) {
|
||||
rows, err := s.queries().ListPresence(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("store: list presence: %w", err)
|
||||
}
|
||||
out := make([]storecontract.Presence, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, storecontract.Presence{
|
||||
Service: r.Service, InstanceID: r.InstanceID, Version: r.Version, InstallType: r.InstallType,
|
||||
StartedAt: r.StartedAt, ReportedAt: r.ReportedAt, Ready: r.Ready, Detail: r.Detail,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -129,6 +129,7 @@ var (
|
||||
_ storecontract.ConfigSnapshotStore = (*Store)(nil)
|
||||
_ storecontract.ChannelStatusStore = (*Store)(nil)
|
||||
_ storecontract.ApplyStatusStore = (*Store)(nil)
|
||||
_ storecontract.PresenceStore = (*Store)(nil)
|
||||
_ task.Caller = (*Store)(nil)
|
||||
_ workflow.Store = (*Store)(nil)
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user