Something went wrong. Try again.
atproto relay in zig zlay.waow.tech
relay zig atproto
Something went wrong. Try again.
Zig
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139//! collection index cleaner — removes stale entries for inactive accounts//!//! queries postgres for accounts with non-active status and removes their//! entries from the RocksDB collection index. triggered via admin API.//! modeled on backfill.zig's Backfiller pattern.
const std = @import("std");const Io = std.Io;const pg = @import("pg");const collection_index_mod = @import("collection_index.zig");const event_log_mod = @import("event_log.zig");
const Allocator = std.mem.Allocator;const log = std.log.scoped(.cleaner);
pub const Cleaner = struct { 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) !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.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); }
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), }; }};