-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
index_usage_stats.go
134 lines (113 loc) · 4.06 KB
/
index_usage_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package server
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/sql/idxusage"
"github.com/cockroachdb/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// IndexUsageStatistics is the GRPC handler for serving index usage statistics.
// If the NodeID in the request payload is left empty, the handler will issue
// a cluster-wide RPC fanout to aggregate all index usage statistics from all
// the nodes. If the NodeID is specified, then the handler will handle the
// request either locally (if the NodeID matches the current node's NodeID) or
// forward it to the correct node.
func (s *statusServer) IndexUsageStatistics(
ctx context.Context, req *serverpb.IndexUsageStatisticsRequest,
) (*serverpb.IndexUsageStatisticsResponse, error) {
ctx = propagateGatewayMetadata(ctx)
ctx = s.AnnotateCtx(ctx)
if _, err := s.privilegeChecker.requireViewActivityPermission(ctx); err != nil {
return nil, err
}
localReq := &serverpb.IndexUsageStatisticsRequest{
NodeID: "local",
OrderedTableID: req.OrderedTableID,
OrderedIndexID: req.OrderedIndexID,
}
if len(req.NodeID) > 0 {
requestedNodeID, local, err := s.parseNodeID(req.NodeID)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
if local {
statsReader :=
s.sqlServer.pgServer.SQLServer.GetLocalIndexStatisticsReader()
return serializeIndexUsageStats(req, statsReader)
}
statusServer, err := s.dialNode(ctx, requestedNodeID)
if err != nil {
return nil, err
}
return statusServer.IndexUsageStatistics(ctx, localReq)
}
// Creating a sink to aggregate all the information.
aggStats := s.sqlServer.pgServer.SQLServer.GetClusterIndexStatisticsProvider()
aggStats.Clear()
dialFn := func(ctx context.Context, nodeID roachpb.NodeID) (interface{}, error) {
client, err := s.dialNode(ctx, nodeID)
return client, err
}
fetchIndexUsageStats := func(ctx context.Context, client interface{}, _ roachpb.NodeID) (interface{}, error) {
statusClient := client.(serverpb.StatusClient)
return statusClient.IndexUsageStatistics(ctx, localReq)
}
aggFn := func(_ roachpb.NodeID, resp interface{}) {
stats := resp.(*serverpb.IndexUsageStatisticsResponse)
aggStats.BatchInsert(stats.Statistics)
}
var combinedError error
errFn := func(_ roachpb.NodeID, nodeFnError error) {
if nodeFnError != nil {
combinedError = errors.CombineErrors(combinedError, nodeFnError)
}
}
// It's unfortunate that we cannot use paginatedIterateNodes here because we
// need to aggregate all stats before returning. Returning a partial result
// yields an incorrect result.
if err := s.iterateNodes(ctx,
fmt.Sprintf("requesting index usage stats for node %s", req.NodeID),
dialFn, fetchIndexUsageStats, aggFn, errFn); err != nil {
return nil, err
}
return serializeIndexUsageStats(req, aggStats)
}
func serializeIndexUsageStats(
req *serverpb.IndexUsageStatisticsRequest, reader idxusage.Reader,
) (*serverpb.IndexUsageStatisticsResponse, error) {
indexStats := make([]roachpb.CollectedIndexUsageStatistics, 0)
var max *uint64
if req.GetMax() != nil {
maxLimit := req.GetMaxLimit()
max = &maxLimit
}
err := reader.ForEach(idxusage.IteratorOptions{
SortedTableID: req.OrderedTableID,
SortedIndexID: req.OrderedIndexID,
Max: max,
}, func(key *roachpb.IndexUsageKey, value *roachpb.IndexUsageStatistics) error {
indexStats = append(indexStats, roachpb.CollectedIndexUsageStatistics{
Key: *key,
Stats: *value,
})
return nil
})
if err != nil {
return nil, err
}
return &serverpb.IndexUsageStatisticsResponse{
Statistics: indexStats,
}, nil
}