Skip to content

Commit

Permalink
raft: add tests for raft tracing
Browse files Browse the repository at this point in the history
Adds two tests that validate the Raft tracing is correct.

TODO: Actually check the tracing log rather than manual inspection.

Release note: None
  • Loading branch information
andrewbaptist committed Oct 10, 2024
1 parent 3cfe500 commit 84dbc3f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/kv/kvserver/client_raft_log_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/vfs"
"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -132,6 +134,49 @@ func TestRaftLogQueue(t *testing.T) {
}
}

func TestRaftTracerDemo(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

// TODO(baptist): Raft tracing write to the span after it is finished. Need to look at options...
tc := testcluster.StartTestCluster(t, 3, base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
ServerArgs: base.TestServerArgs{
RaftConfig: base.RaftConfig{
RangeLeaseDuration: 24 * time.Hour, // disable lease moves
RaftElectionTimeoutTicks: 1 << 30, // disable elections
},
},
})
defer tc.Stopper().Stop(context.Background())
store := tc.GetFirstStoreFromServer(t, 0)

// Write a single value to ensure we have a leader on n1.
key := tc.ScratchRange(t)
_, pErr := kv.SendWrapped(context.Background(), store.TestSender(), putArgs(key, []byte("value")))
require.NoError(t, pErr.GoError())
require.NoError(t, tc.WaitForSplitAndInitialization(key))
// Set to have 3 voters.
tc.AddVotersOrFatal(t, key, tc.Targets(1, 2)...)
tc.WaitForVotersOrFatal(t, key, tc.Targets(1, 2)...)

for i := 0; i < 100; i++ {
var finish func() tracingpb.Recording
ctx := context.Background()
if i == 50 {
// Trace a random request on a "client" tracer.
ctx, finish = tracing.ContextWithRecordingSpan(ctx, tracing.NewTracerWithOpt(context.Background(), tracing.WithUseAfterFinishOpt(true, true)), "test")
}
_, pErr := kv.SendWrapped(ctx, store.TestSender(), putArgs(key, []byte(fmt.Sprintf("value-%d", i))))
require.NoError(t, pErr.GoError())
// Note that this is the clients span, there may be additional logs created after the span is returned.
if finish != nil {
// TODO: Validate that the log is here and also in the standard logging.
fmt.Println(finish())
}
}
}

// TestCrashWhileTruncatingSideloadedEntries emulates a process crash in the
// middle of applying a raft log truncation command that removes some entries
// from the sideloaded storage. The test expects that storage remains in a
Expand Down
27 changes: 27 additions & 0 deletions pkg/kv/kvserver/replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15049,3 +15049,30 @@ func TestLockAcquisitions1PCInteractions(t *testing.T) {
})
})
}

func TestRaftTracing(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
stopper := stop.NewStopper()
defer stopper.Stop(ctx)

tc := testContext{}

tc.manualClock = timeutil.NewManualTime(timeutil.Unix(0, 123))
cfg := TestStoreConfig(hlc.NewClockForTesting(tc.manualClock))
// testContext tests like to move the manual clock around and assume that they can write at past
// timestamps.
cfg.TestingKnobs.DontCloseTimestamps = true
cfg.TestingKnobs.DisableMergeWaitForReplicasInit = true
cfg.TestingKnobs.TraceAllRaftEvents = true
tc.StartWithStoreConfig(ctx, t, stopper, cfg)

tr := tc.store.cfg.AmbientCtx.Tracer
ctx, finish := tracing.ContextWithRecordingSpan(
ctx, tr, "testTrace",
)

require.NoError(t, tc.store.DB().Put(ctx, "foo", "bar"))
trace := finish()
fmt.Println(trace)
}

0 comments on commit 84dbc3f

Please sign in to comment.