ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m28s
ci / test (pull_request) Successful in 12m39s
ci / build (pull_request) Successful in 16m10s
ci / image (pull_request) Successful in 5m20s
Deleting an organization removed its check results and aggregates in the same transaction as the request. With gigabytes of results that took longer than the request may last, it was cut off and rolled back, so a large organization could not be deleted at all. The organization and everything with a foreign key on it now go in the request; its check data is queued in org_purges. Every API instance purges the queue a chunk at a time, each chunk in a transaction of its own, right after a deletion and every minute. Raw results go first, so the aggregates' refresh does not rebuild what was purged. A lease lets another instance take over a purge whose instance stopped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
120 lines
3.3 KiB
Go
120 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
)
|
|
|
|
// orgPurgeEvery is how often an instance looks for deleted organizations
|
|
// whose check data is left, besides right after a deletion.
|
|
const orgPurgeEvery = time.Minute
|
|
|
|
// purgeOrg deletes an organization with everything in it at once, except
|
|
// its check results and aggregates: they carry no foreign key and can run
|
|
// to gigabytes, more than a request can delete in time, so it queues them
|
|
// for PurgeDeletedOrgs. Nothing shows them once the organization is gone.
|
|
func purgeOrg(ctx context.Context, q *store.Queries, org uuid.UUID) error {
|
|
if err := q.QueueOrgPurge(ctx, org); err != nil {
|
|
return err
|
|
}
|
|
if err := q.DeleteOrgStorage(ctx, org); err != nil {
|
|
return err
|
|
}
|
|
_, err := q.DeleteOrg(ctx, org)
|
|
return err
|
|
}
|
|
|
|
// wakeOrgPurges has PurgeDeletedOrgs run soon after a deletion instead of
|
|
// at the next tick.
|
|
func (s *Server) wakeOrgPurges() {
|
|
select {
|
|
case s.orgPurges <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// runOrgPurges purges the check data of deleted organizations every
|
|
// orgPurgeEvery and when woken, until ctx ends.
|
|
func (s *Server) runOrgPurges(ctx context.Context) {
|
|
tick := time.NewTicker(orgPurgeEvery)
|
|
defer tick.Stop()
|
|
for {
|
|
if err := s.PurgeDeletedOrgs(ctx); err != nil && ctx.Err() == nil {
|
|
s.log.ErrorContext(ctx, "purging deleted organizations failed", "error", err)
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-tick.C:
|
|
case <-s.orgPurges:
|
|
}
|
|
}
|
|
}
|
|
|
|
// PurgeDeletedOrgs deletes the check data of deleted organizations a chunk
|
|
// at a time, each in a transaction of its own, until none is left. An
|
|
// instance leases an organization while it purges it; if it stops, another
|
|
// takes over once the lease runs out and repeats the chunks, which then
|
|
// delete nothing.
|
|
func (s *Server) PurgeDeletedOrgs(ctx context.Context) error {
|
|
for ctx.Err() == nil {
|
|
var org uuid.UUID
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
var err error
|
|
org, err = q.ClaimOrgPurge(ctx)
|
|
return err
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := s.purgeOrgCheckData(ctx, org); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return ctx.Err()
|
|
}
|
|
|
|
func (s *Server) purgeOrgCheckData(ctx context.Context, org uuid.UUID) error {
|
|
start := time.Now()
|
|
var chunks []string
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
var err error
|
|
chunks, err = q.OrgCheckDataChunks(ctx)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var rows int64
|
|
for _, chunk := range chunks {
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
n, err := q.PurgeOrgCheckChunk(ctx, store.PurgeOrgCheckChunkParams{OrgID: org, Chunk: chunk})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rows += n
|
|
return q.ExtendOrgPurge(ctx, org)
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error { return q.FinishOrgPurge(ctx, org) })
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.log.InfoContext(ctx, "purged check data of deleted organization",
|
|
"org", org, "rows", rows, "chunks", len(chunks), "duration", time.Since(start).Round(time.Millisecond))
|
|
return nil
|
|
}
|