Files
nmon/internal/backup/restore_verify.go

77 lines
2.3 KiB
Go

package backup
import (
"context"
"fmt"
"sort"
"time"
"github.com/v10networks/nmon/internal/config"
"github.com/v10networks/nmon/internal/store"
"github.com/v10networks/nmon/internal/vmsup"
)
// SeriesVerifier proves the restored metric stores serve the series the backup fingerprinted.
type SeriesVerifier interface {
// VerifySeries brings the stores in dataDir up, checks every fingerprint up to boundary, and stops them.
VerifySeries(ctx context.Context, dataDir string, boundary time.Time, stores []StoreManifest) error
}
// verify loads or opens the restored inventory and compares its row counts with the manifest, then has the
// verifier replay the sampled series. Both must pass before Restore reports done.
func verify(ctx context.Context, o *RestoreOptions, m *Manifest, res *RestoreResult) error {
counts, err := restoredCounts(ctx, o, m)
if err != nil {
return err
}
tables := make([]string, 0, len(m.Rows))
for t := range m.Rows {
tables = append(tables, t)
}
sort.Strings(tables)
for _, t := range tables {
if counts[t] != m.Rows[t] {
return fmt.Errorf("table %s has %d rows, backup had %d", t, counts[t], m.Rows[t])
}
res.RowsVerified += counts[t]
}
var withSeries []StoreManifest
for _, s := range m.Stores {
if len(s.Series) > 0 {
withSeries = append(withSeries, s)
res.SeriesVerified += len(s.Series)
}
}
if len(withSeries) == 0 {
return nil
}
v := o.Verifier
if v == nil {
v = NewVMVerifier(vmsup.Options{})
}
return v.VerifySeries(ctx, o.DataDir, m.Boundary, withSeries)
}
func restoredCounts(ctx context.Context, o *RestoreOptions, m *Manifest) (map[string]int64, error) {
if m.Inventory == inventoryDB {
st, err := store.Open(ctx, store.DriverSQLite, config.InventoryDB(o.DataDir), nil, o.Clock)
if err != nil {
return nil, fmt.Errorf("open restored inventory: %w", err)
}
defer func() { _ = st.Close() }() // read-only use
return st.RowCounts(ctx)
}
if o.Store == nil {
return nil, fmt.Errorf("archive holds a %s export: RestoreOptions.Store (an open, migrated, empty database) is required", m.Dialect)
}
f, err := openStaged(o.DataDir, inventoryJSONL)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }() // read-only handle
if _, err := o.Store.ImportJSONL(ctx, f); err != nil {
return nil, err
}
return o.Store.RowCounts(ctx)
}