-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: add crdb_internal.reset_sql_statistics() builtin
Previously, there was no mechanism to immediately clear SQL statistics. Users would have to wait until the reset interval expires. This commit creates a builtin to immediately clears SQL stats. Release note (sql change): SQL stats can now be cleared using crdb_internal.reset_sql_statistics()
- Loading branch information
Showing
15 changed files
with
1,209 additions
and
507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func (s *statusServer) SQLStatisticsReset( | ||
ctx context.Context, req *serverpb.SQLStatisticsResetRequest, | ||
) (*serverpb.SQLStatisticsResetResponse, error) { | ||
ctx = propagateGatewayMetadata(ctx) | ||
ctx = s.AnnotateCtx(ctx) | ||
|
||
if _, err := s.privilegeChecker.requireAdminUser(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
response := &serverpb.SQLStatisticsResetResponse{} | ||
|
||
localReq := &serverpb.SQLStatisticsResetRequest{ | ||
NodeID: "local", | ||
} | ||
|
||
if len(req.NodeID) > 0 { | ||
requestedNodeID, local, err := s.parseNodeID(req.NodeID) | ||
if err != nil { | ||
return nil, status.Errorf(codes.InvalidArgument, err.Error()) | ||
} | ||
if local { | ||
s.admin.server.sqlServer.pgServer.SQLServer.ResetSQLStats(ctx) | ||
response.NumOfNodesResetted = 1 | ||
return response, nil | ||
} | ||
status, err := s.dialNode(ctx, requestedNodeID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return status.SQLStatisticsReset(ctx, localReq) | ||
} | ||
|
||
dialFn := func(ctx context.Context, nodeID roachpb.NodeID) (interface{}, error) { | ||
client, err := s.dialNode(ctx, nodeID) | ||
return client, err | ||
} | ||
|
||
resetSQLStats := func(ctx context.Context, client interface{}, _ roachpb.NodeID) (interface{}, error) { | ||
status := client.(serverpb.StatusClient) | ||
return status.SQLStatisticsReset(ctx, localReq) | ||
} | ||
|
||
if err := s.iterateNodes(ctx, fmt.Sprintf("reset SQL statistics for node %s", req.NodeID), | ||
dialFn, | ||
resetSQLStats, | ||
func(nodeID roachpb.NodeID, resp interface{}) { | ||
response.NumOfNodesResetted++ | ||
}, | ||
func(nodeID roachpb.NodeID, err error) { | ||
}, | ||
); err != nil { | ||
return nil, err | ||
} | ||
return response, nil | ||
} |
Oops, something went wrong.