Skip to content

Commit

Permalink
server: return authoritative span statistics for db details endpoint
Browse files Browse the repository at this point in the history
Resolves: cockroachdb#96163

This change makes the admin API endpoint getting database statistics
scan KV for span statistics instead of using the range descriptor cache.
This provides authoritative output, helping deflake
`TestMultiRegionDatabaseStats`.

Release note (sql change): admin API database details endpoint now
returns authoritative range statistics.
  • Loading branch information
Thomas Hardy committed Aug 2, 2023
1 parent f6c4d9d commit 26db608
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions pkg/server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/quotapool"
"github.com/cockroachdb/cockroach/pkg/util/rangedesc"
"github.com/cockroachdb/cockroach/pkg/util/safesql"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
Expand Down Expand Up @@ -1336,7 +1337,7 @@ func (s *adminServer) statsForSpan(
// Get a list of nodeIDs, range counts, and replica counts per node
// for the specified span.
nodeIDs, rangeCount, replCounts, err := getNodeIDsRangeCountReplCountForSpan(
ctx, s.distSender, rSpan,
ctx, s.db, rSpan, s.st,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1451,23 +1452,29 @@ func (s *adminServer) statsForSpan(
// Returns the list of node ids, range count,
// and replica count for the specified span.
func getNodeIDsRangeCountReplCountForSpan(
ctx context.Context, ds *kvcoord.DistSender, rSpan roachpb.RSpan,
ctx context.Context, db *kv.DB, rSpan roachpb.RSpan, st *cluster.Settings,
) (nodeIDList []roachpb.NodeID, rangeCount int64, replCounts map[roachpb.NodeID]int64, _ error) {
nodeIDs := make(map[roachpb.NodeID]struct{})
replCountForNodeID := make(map[roachpb.NodeID]int64)
ri := kvcoord.MakeRangeIterator(ds)
ri.Seek(ctx, rSpan.Key, kvcoord.Ascending)
for ; ri.Valid(); ri.Next(ctx) {
rangeCount++
for _, repl := range ri.Desc().Replicas().Descriptors() {
replCountForNodeID[repl.NodeID]++
nodeIDs[repl.NodeID] = struct{}{}
}
if !ri.NeedAnother(rSpan) {
break
scanner := rangedesc.NewScanner(db)
pageSize := int(roachpb.RangeDescPageSize.Get(&st.SV))
err := scanner.Scan(ctx, pageSize, func() {
// If the underlying txn fails and needs to be retried,
// clear the nodeIDs we've collected so far.
nodeIDs = map[roachpb.NodeID]struct{}{}
replCountForNodeID = map[roachpb.NodeID]int64{}
rangeCount = 0
}, rSpan.AsRawSpanWithNoLocals(), func(scanned ...roachpb.RangeDescriptor) error {
for _, desc := range scanned {
rangeCount++
for _, repl := range desc.Replicas().Descriptors() {
nodeIDs[repl.NodeID] = struct{}{}
replCountForNodeID[repl.NodeID]++
}
}
}
if err := ri.Error(); err != nil {
return nil
})
if err != nil {
return nil, 0, nil, err
}

Expand Down

0 comments on commit 26db608

Please sign in to comment.