Skip to content

Commit

Permalink
chore: misc bug fixes supporting sqlite migration (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge authored Oct 14, 2024
1 parent 28c3172 commit 0daa74f
Show file tree
Hide file tree
Showing 31 changed files with 504 additions and 259 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ backrest-*
dist
__debug_bin
cmd/backrest/backrest
*.exe
*.exe
27 changes: 24 additions & 3 deletions cmd/backrest/backrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func main() {
// Create / load the operation log
oplogFile := path.Join(env.DataDir(), "oplog.sqlite")
opstore, err := sqlitestore.NewSqliteStore(oplogFile)
if err != nil {
if errors.Is(err, sqlitestore.ErrLocked) {
zap.S().Fatalf("oplog is locked by another instance of backrest that is using the same data directory %q, kill that instance before starting another one.", env.DataDir())
} else if err != nil {
zap.S().Warnf("operation log may be corrupted, if errors recur delete the file %q and restart. Your backups stored in your repos are safe.", oplogFile)
zap.S().Fatalf("error creating oplog: %v", err)
}
Expand All @@ -87,6 +89,23 @@ func main() {
zap.S().Fatalf("error creating task log store: %v", err)
}
logstore.MigrateTarLogsInDir(logStore, filepath.Join(env.DataDir(), "rotatinglogs"))
deleteLogsForOp := func(ops []*v1.Operation, event oplog.OperationEvent) {
if event != oplog.OPERATION_DELETED {
return
}
for _, op := range ops {
if err := logStore.DeleteWithParent(op.Id); err != nil {
zap.S().Warnf("error deleting logs for operation %q: %v", op.Id, err)
}
}
}
log.Subscribe(oplog.Query{}, &deleteLogsForOp)
defer func() {
if err := logStore.Close(); err != nil {
zap.S().Warnf("error closing log store: %v", err)
}
log.Unsubscribe(&deleteLogsForOp)
}()

// Create orchestrator and start task loop.
orchestrator, err := orchestrator.NewOrchestrator(resticPath, cfg, log, logStore)
Expand Down Expand Up @@ -226,6 +245,9 @@ func installLoggers() {
zap.S().Infof("writing logs to: %v", logsDir)
}

// migrateBboltOplog migrates the old bbolt oplog to the new sqlite oplog.
// It is careful to ensure that all migrations are applied before copying
// operations directly to the sqlite logstore.
func migrateBboltOplog(logstore oplog.OpStore) {
oldBboltOplogFile := path.Join(env.DataDir(), "oplog.boltdb")
if _, err := os.Stat(oldBboltOplogFile); err == nil {
Expand All @@ -243,7 +265,6 @@ func migrateBboltOplog(logstore oplog.OpStore) {
batch := make([]*v1.Operation, 0, 32)

var errs []error

if err := oldOplog.Query(oplog.Query{}, func(op *v1.Operation) error {
batch = append(batch, op)
if len(batch) == 256 {
Expand Down Expand Up @@ -278,7 +299,7 @@ func migrateBboltOplog(logstore oplog.OpStore) {
if err := oldOpstore.Close(); err != nil {
zap.S().Warnf("error closing old bbolt oplog: %v", err)
}
if err := os.Remove(oldBboltOplogFile); err != nil {
if err := os.Rename(oldBboltOplogFile, oldBboltOplogFile+".deprecated"); err != nil {
zap.S().Warnf("error removing old bbolt oplog: %v", err)
}

Expand Down
113 changes: 62 additions & 51 deletions gen/go/v1/operations.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ go 1.22.0
toolchain go1.23.1

require (
al.essio.dev/pkg/shellescape v1.5.0
al.essio.dev/pkg/shellescape v1.5.1
connectrpc.com/connect v1.17.0
github.com/containrrr/shoutrrr v0.8.0
github.com/djherbis/buffer v1.2.0
github.com/djherbis/nio/v3 v3.0.1
github.com/getlantern/systray v1.2.2
github.com/gitploy-io/cronexpr v0.2.2
github.com/gofrs/flock v0.12.1
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/go-cmp v0.6.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/hashicorp/go-multierror v1.1.1
github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb
Expand All @@ -23,12 +23,12 @@ require (
github.com/prometheus/client_golang v1.20.4
go.etcd.io/bbolt v1.3.11
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.27.0
golang.org/x/net v0.29.0
golang.org/x/crypto v0.28.0
golang.org/x/net v0.30.0
golang.org/x/sync v0.8.0
google.golang.org/genproto/googleapis/api v0.0.0-20240924160255-9d4c2d233b61
google.golang.org/grpc v1.67.0
google.golang.org/protobuf v1.34.2
google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.35.1
gopkg.in/natefinch/lumberjack.v2 v2.2.1
zombiezen.com/go/sqlite v1.4.0
)
Expand All @@ -49,28 +49,29 @@ require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/josephspurrier/goversioninfo v1.4.1 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/common v0.60.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/image v0.20.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/image v0.21.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect
modernc.org/libc v1.61.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
Expand Down
Loading

0 comments on commit 0daa74f

Please sign in to comment.