Skip to content

Commit

Permalink
db: log additional WAL details during replay
Browse files Browse the repository at this point in the history
When we finish replaying a WAL, log the logical log which will encode all the
replayed segment files (if WAL failover resulted in multiple physical files).

```
[JOB 1] WAL 000002: {(data,000)} stopped reading at offset: (data/000002.log: 0); replayed 0 keys in 0 batches
```

Additionally, implement the SafeFormatter interface to ensure that relevant
details are not redacted.

Informs #4162.
  • Loading branch information
jbowens committed Nov 13, 2024
1 parent 5f81af5 commit b03b494
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion open.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ func (d *DB) replayWAL(
}

d.opts.Logger.Infof("[JOB %d] WAL %s stopped reading at offset: %s; replayed %d keys in %d batches",
jobID, base.DiskFileNum(ll.Num).String(), offset, keysReplayed, batchesReplayed)
jobID, errors.Safe(ll.String()), offset, keysReplayed, batchesReplayed)
if !d.opts.ReadOnly {
flushMem()
}
Expand Down
26 changes: 16 additions & 10 deletions wal/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ package wal
import (
"bytes"
"cmp"
"fmt"
"io"
"slices"
"strings"

"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/batchrepr"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/record"
"github.com/cockroachdb/pebble/vfs"
"github.com/cockroachdb/redact"
)

// A LogicalLog identifies a logical WAL and its consituent segment files.
Expand All @@ -38,7 +37,12 @@ type segment struct {

// String implements fmt.Stringer.
func (s segment) String() string {
return fmt.Sprintf("(%s,%s)", s.dir.Dirname, s.logNameIndex)
return redact.StringWithoutMarkers(s)
}

// SafeFormat implements redact.SafeFormatter.
func (s segment) SafeFormat(w redact.SafePrinter, _ rune) {
w.Printf("(%s,%s)", errors.Safe(s.dir.Dirname), s.logNameIndex)
}

// NumSegments returns the number of constituent physical log files that make up
Expand Down Expand Up @@ -73,17 +77,19 @@ func (ll LogicalLog) OpenForRead() Reader {

// String implements fmt.Stringer.
func (ll LogicalLog) String() string {
var sb strings.Builder
sb.WriteString(base.DiskFileNum(ll.Num).String())
sb.WriteString(": {")
return redact.StringWithoutMarkers(ll)
}

// SafeFormat implements redact.SafeFormatter.
func (ll LogicalLog) SafeFormat(w redact.SafePrinter, _ rune) {
w.Printf("%s: {", base.DiskFileNum(ll.Num).String())
for i := range ll.segments {
if i > 0 {
sb.WriteString(", ")
w.SafeString(", ")
}
sb.WriteString(ll.segments[i].String())
w.Print(ll.segments[i])
}
sb.WriteString("}")
return sb.String()
w.SafeString("}")
}

// appendDeletableLogs appends all of the LogicalLog's constituent physical
Expand Down
12 changes: 10 additions & 2 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/record"
"github.com/cockroachdb/pebble/vfs"
"github.com/cockroachdb/redact"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -434,8 +435,15 @@ type Offset struct {
// String implements fmt.Stringer, returning a string representation of the
// offset.
func (o Offset) String() string {
return redact.StringWithoutMarkers(o)
}

// SafeFormat implements redact.SafeFormatter.
func (o Offset) SafeFormat(w redact.SafePrinter, _ rune) {
if o.PreviousFilesBytes > 0 {
return fmt.Sprintf("(%s: %d), %d from previous files", o.PhysicalFile, o.Physical, o.PreviousFilesBytes)
w.Printf("(%s: %d), %d from previous files", o.PhysicalFile, o.Physical, o.PreviousFilesBytes)
return
}
return fmt.Sprintf("(%s: %d)", o.PhysicalFile, o.Physical)
w.Printf("(%s: %d)", o.PhysicalFile, o.Physical)

}

0 comments on commit b03b494

Please sign in to comment.