~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zlay

atproto relay in zig · mirror of https://tangled.org/zat.dev/zlay

0
0
153 lines · 5.3 KB Raw History
//! collection index cleaner — removes entries that should not be in the index
//!
//! two scopes, each triggered via admin API: inactive accounts (queries
//! postgres for accounts with non-active status and removes their entries)
//! and invalid collections (removes every pair filed under a name that is
//! not an NSID).
//! modeled on backfill.zig's Backfiller pattern.

const std = @import("std");
const Io = std.Io;
const pg = @import("pg");
const collection_index_mod = @import("index.zig");
const event_log_mod = @import("../event_log.zig");

const Allocator = std.mem.Allocator;
const log = std.log.scoped(.cleaner);

pub const Scope = enum { inactive_accounts, invalid_collections };

pub const Cleaner = struct {
    scope: Scope = .inactive_accounts,
    allocator: Allocator,
    io: Io,
    collection_index: *collection_index_mod.CollectionIndex,
    persist: *event_log_mod.DiskPersist,
    running: std.atomic.Value(bool),
    thread: ?std.Thread,
    scanned: std.atomic.Value(u64),
    removed: std.atomic.Value(u64),
    shutdown: *std.atomic.Value(bool),

    fn db(self: *Cleaner) *pg.Pool {
        return self.persist.db;
    }

    pub fn init(
        allocator: Allocator,
        io: Io,
        collection_index: *collection_index_mod.CollectionIndex,
        persist: *event_log_mod.DiskPersist,
        shutdown: *std.atomic.Value(bool),
    ) Cleaner {
        return .{
            .allocator = allocator,
            .io = io,
            .collection_index = collection_index,
            .persist = persist,
            .running = .{ .raw = false },
            .thread = null,
            .scanned = .{ .raw = 0 },
            .removed = .{ .raw = 0 },
            .shutdown = shutdown,
        };
    }

    pub fn isRunning(self: *Cleaner) bool {
        return self.running.load(.acquire);
    }

    /// block until any in-progress cleanup thread completes.
    /// must be called before tearing down DiskPersist or CollectionIndex.
    pub fn waitForCompletion(self: *Cleaner) void {
        if (self.thread) |t| {
            t.join();
            self.thread = null;
        }
    }

    /// start cleanup. returns error if already running.
    pub fn start(self: *Cleaner, scope: Scope) !void {
        if (self.running.cmpxchgStrong(false, true, .acq_rel, .acquire) != null) {
            return error.AlreadyRunning;
        }
        errdefer self.running.store(false, .release);

        self.scanned.store(0, .release);
        self.removed.store(0, .release);
        self.scope = scope;
        self.thread = std.Thread.spawn(.{}, run, .{self}) catch return error.SpawnFailed;
    }

    fn run(self: *Cleaner) void {
        defer {
            // note: do NOT clear self.thread here — waitForCompletion() needs
            // the handle to join this thread before dp/ci teardown.
            self.running.store(false, .release);
        }

        if (self.scope == .invalid_collections) {
            log.info("invalid-collection purge started", .{});
            self.collection_index.purgeInvalidCollections(self.allocator, self.shutdown, &self.removed) catch |err| {
                log.err("invalid-collection purge failed: {s}", .{@errorName(err)});
            };
            log.info("invalid-collection purge finished: removed {d} pairs", .{self.removed.load(.monotonic)});
            return;
        }

        log.info("cleanup started", .{});

        const pool = self.db();

        // page through inactive accounts by uid
        var last_uid: i64 = 0;
        while (!self.shutdown.load(.acquire)) {
            var batch_count: u64 = 0;
            {
                var result = pool.query(
                    "SELECT uid, did FROM account WHERE (status != 'active' OR upstream_status != 'active') AND uid > $1 ORDER BY uid LIMIT 500",
                    .{last_uid},
                ) catch |err| {
                    log.err("cleanup query failed: {s}", .{@errorName(err)});
                    break;
                };
                defer result.deinit();

                while (result.nextUnsafe() catch null) |row| {
                    const uid = row.get(i64, 0);
                    const did = row.get([]const u8, 1);

                    self.collection_index.removeAll(did) catch |err| {
                        log.debug("removeAll failed for {s}: {s}", .{ did, @errorName(err) });
                    };

                    last_uid = uid;
                    batch_count += 1;
                    _ = self.scanned.fetchAdd(1, .monotonic);
                    _ = self.removed.fetchAdd(1, .monotonic);
                }
            }

            if (batch_count == 0) break;

            log.info("cleanup progress: scanned {d}, last_uid={d}", .{
                self.scanned.load(.monotonic),
                last_uid,
            });

            // brief pause between batches to avoid hammering postgres
            self.io.sleep(Io.Duration.fromMilliseconds(50), .awake) catch {};
        }

        const total = self.scanned.load(.monotonic);
        log.info("cleanup complete: removed {d} inactive accounts from collection index", .{total});
    }

    pub fn getStatus(self: *Cleaner) struct { running: bool, scanned: u64, removed: u64 } {
        return .{
            .running = self.isRunning(),
            .scanned = self.scanned.load(.monotonic),
            .removed = self.removed.load(.monotonic),
        };
    }
};