catdb

package
v1.0.375 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package catdb is Layer 2 of the two-tier URL categorisation engine — the community category store. It is a self-contained leaf extracted from the flat package main per ADR-0002; it contains the BadgerDB dependency (the proxy's only category-DB use of Badger) behind a narrow API.

Storage: BadgerDB (v4, pure-Go, no CGo).

Key layout: []byte(domain) e.g. "facebook.com" Value layout: []byte(mapped category) e.g. "Social"

Subdomain matching: domain walking — query most-specific label first, then strip the leftmost label and retry, stopping before a bare TLD. e.g. "sub.facebook.com" → "facebook.com" → stop (next would be "com").

Layer 1 (the admin-managed catStore in package main) is always consulted first; this community store is the fallback for entries not in the admin-managed lists.

Index

Constants

View Source
const (
	// TriggerNone means the store opened normally.
	TriggerNone = storeguard.TriggerNone
	// TriggerPoisonMarker means a previous process died inside badger.Open.
	TriggerPoisonMarker = storeguard.TriggerPoisonMarker
	// TriggerOpenError means badger.Open returned an identified corruption error.
	TriggerOpenError = storeguard.TriggerOpenError
	// TriggerRuntimeFault means a previous run contained a memory fault on the
	// store's mapped tables after a healthy open (CHAOS-74).
	TriggerRuntimeFault = storeguard.TriggerRuntimeFault
)

Variables

View Source
var ErrDegraded = errors.New("catdb: community category store is degraded after a memory fault on its mapped tables; Layer 2 is off until restart")

ErrDegraded is returned by a data method of a store that has latched degraded after a contained memory fault. The store is refused WITHOUT touching badger: its mappings are damaged, and every further access is another chance to fault, possibly on a goroutine with no guard at all.

Functions

func OpenResilient added in v1.0.207

func OpenResilient(dir string) (*CommunityDB, Recovery, error)

OpenResilient opens the community store, recovering from a store the previous run could not survive. It never panics on behalf of badger — the panic case is handled by refusing to hand badger a directory a previous process died inside of — and it never destroys data it has not first moved aside.

Returns (db, recovery, nil) on success. On failure the caller MUST degrade (Layer-1-only categorisation) rather than treat it as fatal: this store is a cache, and refusing to boot an in-line gateway over a damaged cache is a self-inflicted outage.

Direct `catdb.Open` remains the entry point for callers that already know the directory is sound.

The configured path is resolved through symlinks ONCE, here, and every recovery step — markers, the quarantine rename, the rebuild — acts on the real target. If it used the configured path instead, an alias such as `/var/lib/catdb -> /mnt/data/catdb` would have the SYMLINK renamed aside. badger would then create a plain directory at the alias, so the rebuilt feed would silently leave the persistent volume while the damaged target stayed where it was. The alias itself is never renamed or replaced.

func QuarantinedCopies added in v1.0.207

func QuarantinedCopies(dir string) []string

QuarantinedCopies returns the `.corrupt.*` siblings of dir, newest last. Exported so the caller can re-surface an unreconciled quarantine from a PRIOR boot: the in-memory record is process-local, so without this a self-healed store looks pristine on the next restart while the evidence — and the disk it occupies — is still sitting there. dir is resolved the same way OpenResilient resolves it, because that is where the copies are.

Types

type CommunityDB

type CommunityDB struct {
	// contains filtered or unexported fields
}

CommunityDB wraps a BadgerDB instance for URL category lookups. All exported methods are safe for concurrent use.

func Open

func Open(dir string) (*CommunityDB, error)

Open opens (or creates) a BadgerDB at the given directory.

It is NOT crash-tolerant on its own, and the comment that used to claim otherwise ("Truncate is enabled so a crashed container can restart without manual intervention") was false in two ways: badger v4 removed the Truncate option entirely, and the worst crash damage does not surface as an error at all — a corrupt `.sst` makes this call PANIC from a goroutine badger spawns, which no caller can recover from. Boot paths must therefore call OpenResilient (resilient.go), which detects and quarantines a store a previous process could not survive. Direct Open is for callers that already know the directory is sound (tests, and OpenResilient itself).

func (*CommunityDB) BulkWrite

func (c *CommunityDB) BulkWrite(entries map[string]string) error

BulkWrite atomically writes a batch of domain→category pairs into BadgerDB. Existing entries for the same domain are overwritten. Uses WriteBatch for high-throughput ingestion without holding a long-lived transaction — safe to call while the DB serves concurrent reads.

A degraded store refuses with ErrDegraded and is never written. The feed syncer charges that as a failed round and backs off. It also checks the latch before downloading, so a dead store does not cost a 50 MB fetch per round. A fault contained here latches the store like one in Lookup. badger's own flush goroutines are outside any guard; see the CHAOS-74 residual-risk notes.

func (*CommunityDB) Close

func (c *CommunityDB) Close() error

Close flushes and closes the underlying BadgerDB. Must be called on graceful shutdown to prevent value-log corruption.

Close is contained but NOT refused on a degraded store. It still has to release badger's resources and its directory lock, and a fault while flushing a damaged store is returned as an error instead of killing a process that is shutting down.

func (*CommunityDB) Lookup

func (c *CommunityDB) Lookup(host string) (string, bool)

Lookup returns the mapped category for host (or any of its parent domains). Uses domain walking: tries host, then strips the leftmost label and retries, stopping when no further parent exists above the TLD. Returns ("", false) when no entry is found.

CHAOS-74: containment is armed ONCE for the whole walk rather than per point lookup. The walk issues up to four or five badger reads for a deep hostname, and this runs on the proxy data path, so a guard per read would pay the defer/recover cost several times per request to protect against a fault that makes all of them equally hopeless. A store that has already latched degraded is refused by access without arming anything at all.

func (*CommunityDB) LookupFaults added in v1.0.264

func (c *CommunityDB) LookupFaults() (faults, memFaults uint64, degraded bool)

LookupFaults returns the number of lookups that failed with a badger error rather than a clean miss, the number of contained memory faults, and whether the store has latched degraded.

A non-zero memFaults count means the data volume damaged the store's mapped tables while it was open and serving. Layer-2 categorisation is off from that point on; Layer 1 remains authoritative.

Ordering contract (invariant: degraded ⇒ memFaults ≥ 1). noteMemFault ADDS to memFaults BEFORE it sets degraded, and this reader loads degraded BEFORE memFaults. Go's atomics are sequentially consistent, so a reader that sees degraded==true is guaranteed to see the Add that preceded it. Loading in the other order could catch memFaults before the Add and degraded after the CAS, so a scrape would report a degraded store with "0 memory fault(s)". The order is pinned by TestLookupFaults_DegradedImpliesACountedFault; no mutex needed.

func (*CommunityDB) Stats

func (c *CommunityDB) Stats() (keys int64)

Stats returns the estimated number of keys stored in the DB.

A degraded store, or a fault contained while reading the table metadata, reports 0 rather than an error. The only production caller is the feed syncer's "is the store empty, sync at once?" check, and a dead store answering 0 there is harmless: the round it triggers refuses on the latch before downloading anything.

type Recovery added in v1.0.207

type Recovery = storeguard.Recovery

Recovery reports what OpenResilient had to do.

type RecoveryTrigger added in v1.0.207

type RecoveryTrigger = storeguard.RecoveryTrigger

RecoveryTrigger names what caused a recovery attempt.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL