Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/test/lqvacuumer/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max-retry = 0 # avoid waiting for retries to speed up test
58 changes: 58 additions & 0 deletions e2e/test/lqvacuumer/lq_vacuumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package nxdomain

import (
_ "embed"
"os"
"strings"
"sync"
"testing"
"time"

"github.com/internetarchive/Zeno/e2e"
"github.com/internetarchive/Zeno/internal/pkg/source/lq"
)

type recordMatcher struct {
vacuumedSuccess bool
unexpectedError bool
}

func (rm *recordMatcher) Match(record map[string]string) {
if record["level"] == "INFO" && record["msg"] == "vacuuming complete" {
rm.vacuumedSuccess = true
}
if record["level"] == "ERROR" {
if strings.Contains(record["err"], "failed to resolve DNS") {
} else {
rm.unexpectedError = true
}
}
}

func (rm *recordMatcher) Assert(t *testing.T) {
if rm.unexpectedError {
t.Error("An unexpected error was logged during the test")
}
}

func (rm *recordMatcher) ShouldStop() bool {
return rm.vacuumedSuccess || rm.unexpectedError
}

func Test_LQ_vacuumer(t *testing.T) {
os.RemoveAll("jobs")

rm := &recordMatcher{}
wg := &sync.WaitGroup{}
shouldStopCh := make(chan struct{})
wg.Add(2)

lq.VacuumInterval = 1 * time.Second

go e2e.StartHandleLogRecord(t, wg, rm, shouldStopCh)
go e2e.ExecuteCmdZenoGetURL(t, wg, []string{"http://nxdomain.nxtld/"})

e2e.WaitForGoroutines(t, wg, shouldStopCh)
rm.Assert(t)

}
2 changes: 0 additions & 2 deletions internal/pkg/source/lq/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ func (s *LQ) consumer() {
// Close the urlBuffer to signal consumerSenders to finish
close(urlBuffer)

s.wg.Done()

logger.Debug("closed")
return
}
Expand Down
2 changes: 0 additions & 2 deletions internal/pkg/source/lq/finisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ func (s *LQ) finisher() {
// Close the batch channel to signal the dispatcher to finish.
close(batchCh)

s.wg.Done()

logger.Debug("closed")
return
}
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/source/lq/lq.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func (s *LQ) Start(finishChan, produceChan chan *models.Item) error {
s.produceCh = produceChan
s.client = LQclient

s.wg.Add(3)
go s.consumer()
go s.producer()
go s.finisher()
s.wg.Go(s.consumer)
s.wg.Go(s.producer)
s.wg.Go(s.finisher)
s.wg.Go(s.vacuumer)

logger.Info("started")

Expand Down
2 changes: 0 additions & 2 deletions internal/pkg/source/lq/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ func (s *LQ) producer() {
// Close the batch channel to signal the dispatcher to finish.
close(batchCh)

s.wg.Done()

logger.Debug("closed")
return
}
Expand Down
39 changes: 39 additions & 0 deletions internal/pkg/source/lq/vacuumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lq

import (
"context"
"time"

"github.com/internetarchive/Zeno/internal/pkg/log"
)

var VacuumInterval = 10 * time.Minute

func (s *LQ) vacuumer() {
logger := log.NewFieldedLogger(&log.Fields{
"component": "lq.vacuumer",
})

// Create a context to manage goroutines
ctx, cancel := context.WithCancel(s.ctx)
defer cancel()

ticker := time.NewTicker(VacuumInterval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
logger.Debug("context cancelled, exiting")
return
case <-ticker.C:
logger.Info("vacuuming")
_, err := s.client.dbWrite.Exec("VACUUM;")
if err != nil {
logger.Error("vacuuming failed", err)
}
logger.Info("vacuuming complete")
Copy link
Preview

Copilot AI Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The VACUUM operation can be resource-intensive and may block other database operations. Consider using 'PRAGMA incremental_vacuum;' for less disruptive maintenance or implementing a check to avoid vacuuming when the database is under heavy load.

Suggested change
logger.Info("vacuuming complete")
logger.Info("incremental vacuuming")
_, err := s.client.dbWrite.Exec("PRAGMA incremental_vacuum;")
if err != nil {
logger.Error("incremental vacuuming failed", err)
}
logger.Info("incremental vacuuming complete")

Copilot uses AI. Check for mistakes.

}
}

}