Skip to content

Commit 2e493f4

Browse files
craig[bot]miraradeva
andcommitted
157762: apply: improve Asserter logging and error handling r=pav-kv a=miraradeva Previously, the `Asserter` would panic if it found any discrepancy. This usually works as expected and the panic message in the test failure (e.g. kvnemesis). However, it's possible that the goroutine that invoked the `Asserter` is in a part of the code that doesn't check/recover from panics (e.g. the `defer` logic in `processRaftSnapshotRequest`). This commit changes the panics to `log.Fatalf`s to have a better chance of logging these. It also improves one of the log messages in the `Asserter` to print out the log state in case of a discrepancy. Closes: cockroachdb#157253 Release note: None Co-authored-by: Mira Radeva <[email protected]>
2 parents d4a53c9 + d6bcb67 commit 2e493f4

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

pkg/kv/kvserver/apply/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ go_library(
1717
"//pkg/raft/raftpb",
1818
"//pkg/roachpb",
1919
"//pkg/util/hlc",
20+
"//pkg/util/log",
2021
"//pkg/util/syncutil",
2122
"@com_github_cockroachdb_errors//:errors",
2223
],

pkg/kv/kvserver/apply/asserter.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package apply
77

88
import (
9+
"context"
910
fmt "fmt"
1011

1112
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
@@ -14,6 +15,7 @@ import (
1415
"github.com/cockroachdb/cockroach/pkg/raft/raftpb"
1516
"github.com/cockroachdb/cockroach/pkg/roachpb"
1617
"github.com/cockroachdb/cockroach/pkg/util/hlc"
18+
"github.com/cockroachdb/cockroach/pkg/util/log"
1719
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
1820
)
1921

@@ -113,7 +115,8 @@ func (a *Asserter) forRange(rangeID roachpb.RangeID) *rangeAsserter {
113115
// In the common case, this only needs to look at the previous entry.
114116
func (r *rangeAsserter) stateAt(index kvpb.RaftIndex) applyState {
115117
if int(index) >= len(r.log) {
116-
panic(fmt.Sprintf("index %d is beyond end of log at %d", index, len(r.log)-1))
118+
log.KvExec.Fatalf(context.Background(),
119+
"index %d is beyond end of log at %d", index, len(r.log)-1)
117120
}
118121
// All entries (except gaps) have cmdID, raftIndex, and raftTerm, so we're
119122
// done once we also find a LAI and closed timestamp.
@@ -157,8 +160,8 @@ func (r *rangeAsserter) propose(
157160
req *kvpb.BatchRequest,
158161
) {
159162
fail := func(msg string, args ...interface{}) {
160-
panic(fmt.Sprintf("r%d/%d cmd %s: %s (%s)",
161-
r.rangeID, replicaID, cmdID, fmt.Sprintf(msg, args...), req))
163+
log.KvExec.Fatalf(context.Background(), "r%d/%d cmd %s: %s (%s)",
164+
r.rangeID, replicaID, cmdID, fmt.Sprintf(msg, args...), req)
162165
}
163166

164167
// INVARIANT: all proposals have a command ID.
@@ -242,8 +245,8 @@ func (r *rangeAsserter) apply(
242245
}
243246

244247
fail := func(msg string, args ...interface{}) {
245-
panic(fmt.Sprintf("r%d/%d: %s (%s)\ndata: %x",
246-
r.rangeID, replicaID, fmt.Sprintf(msg, args...), entry, raftEntry.Data))
248+
log.KvExec.Fatalf(context.Background(), "r%d/%d: %s (%s)\ndata: %x",
249+
r.rangeID, replicaID, fmt.Sprintf(msg, args...), entry, raftEntry.Data)
247250
}
248251

249252
// INVARIANT: all commands have a command ID. etcd/raft may commit noop
@@ -368,8 +371,9 @@ func (r *rangeAsserter) applySnapshot(
368371
}
369372

370373
fail := func(msg string, args ...interface{}) {
371-
panic(fmt.Sprintf("r%d/%d snapshot from %d at index %d: %s (%s)",
372-
r.rangeID, replicaID, sender, index, fmt.Sprintf(msg, args...), state))
374+
log.KvExec.Fatalf(context.Background(),
375+
"r%d/%d snapshot from %d at index %d and state (%s): %s",
376+
r.rangeID, replicaID, sender, index, state, fmt.Sprintf(msg, args...))
373377
}
374378

375379
r.Lock()
@@ -410,6 +414,6 @@ func (r *rangeAsserter) applySnapshot(
410414
state.raftTerm = logState.raftTerm
411415
}
412416
if state != logState {
413-
fail("state differs from log state: %s", state)
417+
fail("state differs from log state: (%s)", logState)
414418
}
415419
}

0 commit comments

Comments
 (0)