Skip to content

Commit

Permalink
rac2: safe format send stream stats
Browse files Browse the repository at this point in the history
Add `String()` and `SafeFormat()` methods for `RangeSendStreamStats` and
`ReplicaSendStreamStats`.

Part of: #123509
Release note: None
  • Loading branch information
kvoli committed Oct 14, 2024
1 parent 3bf40c2 commit aabc0b2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/kv/kvserver/kvflowcontrol/rac2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_library(
"//pkg/util/admission/admissionpb",
"//pkg/util/buildutil",
"//pkg/util/hlc",
"//pkg/util/humanizeutil",
"//pkg/util/log",
"//pkg/util/metric",
"//pkg/util/queue",
Expand Down
37 changes: 36 additions & 1 deletion pkg/kv/kvserver/kvflowcontrol/rac2/range_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
Expand Down Expand Up @@ -219,6 +220,22 @@ type RangeSendStreamStats struct {
internal []ReplicaSendStreamStats
}

func (s *RangeSendStreamStats) String() string {
return redact.StringWithoutMarkers(s)
}

// SafeFormat implements the redact.SafeFormatter interface.
func (s *RangeSendStreamStats) SafeFormat(w redact.SafePrinter, _ rune) {
w.Printf("[")
for i := range s.internal {
if i > 0 {
w.Printf(", ")
}
w.Printf("r%v=(%v)", s.internal[i].ReplicaID, s.internal[i])
}
w.Printf("]")
}

// Clear clears the stats for all replica send streams so that the underlying
// memory can be reused.
func (s *RangeSendStreamStats) Clear() {
Expand Down Expand Up @@ -323,6 +340,15 @@ type ReplicaSendStreamStats struct {
ReplicaSendQueueStats
}

func (rsss ReplicaSendStreamStats) String() string {
return redact.StringWithoutMarkers(rsss)
}

func (rsss ReplicaSendStreamStats) SafeFormat(w redact.SafePrinter, _ rune) {
w.Printf("is_state_replicate=%v has_send_queue=%v %v",
rsss.IsStateReplicate, rsss.HasSendQueue, rsss.ReplicaSendQueueStats)
}

// ReplicaSendQueueStats contains the size and count of the send stream queue
// for a replica.
type ReplicaSendQueueStats struct {
Expand All @@ -333,6 +359,16 @@ type ReplicaSendQueueStats struct {
SendQueueCount int64
}

func (rsqs ReplicaSendQueueStats) String() string {
return redact.StringWithoutMarkers(rsqs)
}

// SafeFormat implements the redact.SafeFormatter interface.
func (rsqs ReplicaSendQueueStats) SafeFormat(w redact.SafePrinter, _ rune) {
w.Printf("send_queue_size=%v / %v entries",
humanizeutil.IBytes(rsqs.SendQueueBytes), rsqs.SendQueueCount)
}

// RaftEvent carries a RACv2-relevant subset of raft state sent to storage.
type RaftEvent struct {
// MsgAppMode is the current mode. This is only relevant on the leader.
Expand Down Expand Up @@ -1480,7 +1516,6 @@ func (rc *rangeController) SendStreamStats(statsToSet *RangeSendStreamStats) {
if len(statsToSet.internal) != 0 {
panic(errors.AssertionFailedf("statsToSet is non-empty %v", statsToSet.internal))
}
statsToSet.Clear()
rc.mu.RLock()
defer rc.mu.RUnlock()

Expand Down
33 changes: 33 additions & 0 deletions pkg/kv/kvserver/kvflowcontrol/rac2/range_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2305,3 +2305,36 @@ func TestConstructRaftEventForReplica(t *testing.T) {
})
}
}

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

stats := RangeSendStreamStats{
internal: []ReplicaSendStreamStats{
{
IsStateReplicate: false,
HasSendQueue: true,
ReplicaSendQueueStats: ReplicaSendQueueStats{
ReplicaID: 1,
SendQueueCount: 10,
SendQueueBytes: 100,
},
},
{
IsStateReplicate: true,
HasSendQueue: false,
ReplicaSendQueueStats: ReplicaSendQueueStats{
ReplicaID: 2,
SendQueueCount: 0,
SendQueueBytes: 0,
},
},
},
}

require.Equal(t,
"[r1=(is_state_replicate=false has_send_queue=true send_queue_size=100 B / 10 entries), "+
"r2=(is_state_replicate=true has_send_queue=false send_queue_size=0 B / 0 entries)]",
stats.String())
}

0 comments on commit aabc0b2

Please sign in to comment.