Explorar o código

fix(dns): reset the query log cursor when the AdGuard instance changes

Switching from one AdGuard host to another kept the previous cursor, which mixed two histories and declared false gaps. The host is stored with the cursor and a change restarts from the last hour.

Refs #2078
gnacho hai 2 semanas
pai
achega
e720d6262b

+ 12 - 0
server-go/internal/dnsactivity/ingest.go

@@ -26,6 +26,11 @@ const (
 	kvState  = "adguard_querylog_state"
 	kvStateT = "adguard_querylog_state_ts"
 	kvGap    = "adguard_querylog_gap"
+	// kvHost guarda la instancia cuyo log se está siguiendo: el cursor de una
+	// instancia no vale para otra (hosts distintos, historiales distintos).
+	kvHost = "adguard_querylog_host"
+	// kvConfigHost es la clave de configuración de la integración.
+	kvConfigHost = "adguard_host"
 )
 
 // Source es la lectura del log: la implementan el cliente estándar, el GL y
@@ -153,6 +158,13 @@ func (i *Ingestor) ingest(ctx context.Context) (TickResult, error) {
 	}
 
 	now := i.now()
+	// Un cambio de instancia invalida el cursor: seguir con el de la anterior
+	// mezclaría dos historiales y declararía huecos falsos.
+	cfgHost := i.kvGet(ctx, kvConfigHost)
+	if prev := i.kvGet(ctx, kvHost); prev != cfgHost {
+		_ = i.kvSet(ctx, kvCursor, "")
+		_ = i.kvSet(ctx, kvHost, cfgHost)
+	}
 	cursor, firstRun := i.cursor(ctx, now)
 
 	macs, err := resolveMACs(ctx, i.db)

+ 41 - 0
server-go/internal/dnsactivity/ingest_test.go

@@ -263,3 +263,44 @@ func TestIngestErrorKeepsState(t *testing.T) {
 		t.Errorf("state = %q", res.State)
 	}
 }
+
+func TestIngestResetsCursorWhenInstanceChanges(t *testing.T) {
+	now := time.Date(2026, 11, 16, 11, 0, 30, 0, time.UTC)
+	src := &fakeSource{seq: []*adapters.QueryLogPage{
+		{Oldest: time.Date(2026, 11, 16, 10, 30, 0, 0, time.UTC), Queries: []adapters.DNSQuery{
+			q("2026-11-16T10:59:00Z", "192.168.1.42", "a.example", false),
+		}},
+		{Oldest: time.Date(2026, 11, 16, 10, 45, 0, 0, time.UTC), Queries: []adapters.DNSQuery{
+			q("2026-11-16T10:58:00Z", "192.168.1.42", "a.example", false),
+		}},
+	}}
+	ing, d := newTestIngestor(t, src, now)
+	if _, err := d.Exec(`INSERT INTO kv (key, value) VALUES (?, ?)`, kvConfigHost, "adguard.lan"); err != nil {
+		t.Fatal(err)
+	}
+	if _, err := ing.Tick(context.Background()); err != nil {
+		t.Fatal(err)
+	}
+
+	// Otra instancia: cursor a cero, primera lectura de una hora hacia atrás.
+	// La consulta de 10:58 es más antigua que el cursor de la instancia previa
+	// (10:59), así que solo entra si el cursor se reseteó.
+	if _, err := d.Exec(`UPDATE kv SET value = 'otra.lan' WHERE key = ?`, kvConfigHost); err != nil {
+		t.Fatal(err)
+	}
+	if _, err := ing.Tick(context.Background()); err != nil {
+		t.Fatal(err)
+	}
+	var host string
+	_ = d.QueryRow(`SELECT value FROM kv WHERE key = ?`, kvHost).Scan(&host)
+	if host != "otra.lan" {
+		t.Errorf("host = %q, want otra.lan", host)
+	}
+	// Gate del reset: sin él, la entrada de 10:58 quedaría por debajo del
+	// cursor anterior y no se ingeriría.
+	var total int
+	_ = d.QueryRow(`SELECT COALESCE(SUM(queries),0) FROM dns_activity WHERE domain = 'a.example'`).Scan(&total)
+	if total != 2 {
+		t.Errorf("queries = %d, want 2 (la segunda instancia reingiere su hora)", total)
+	}
+}