-
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.
98308: kvserver,kvflowcontrol: integrate flow control r=irfansharif a=irfansharif Part of #95563. See individual commits. Release note: None Co-authored-by: irfan sharif <[email protected]>
- Loading branch information
Showing
162 changed files
with
10,794 additions
and
820 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data") | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "inspectz", | ||
srcs = [ | ||
"inspectz.go", | ||
"unsupported.go", | ||
], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/inspectz", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/inspectz/inspectzpb", | ||
"//pkg/kv/kvserver/kvflowcontrol", | ||
"//pkg/kv/kvserver/kvflowcontrol/kvflowinspectpb", | ||
"//pkg/roachpb", | ||
"//pkg/util/errorutil", | ||
"//pkg/util/log", | ||
], | ||
) | ||
|
||
get_x_data(name = "get_x_data") |
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,151 @@ | ||
// Copyright 2023 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 inspectz | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/inspectz/inspectzpb" | ||
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvflowcontrol" | ||
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvflowcontrol/kvflowinspectpb" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
) | ||
|
||
// URLPrefix is the prefix for all inspectz endpoints hosted by the server. | ||
const URLPrefix = "/inspectz/" | ||
|
||
// Server is a concrete implementation of the InspectzServer interface, | ||
// organizing everything under /inspectz/*. It's the top-level component that | ||
// houses parsing logic for common inspectz URL parameters and maintains routing | ||
// logic. | ||
type Server struct { | ||
log.AmbientContext | ||
|
||
mux *http.ServeMux | ||
handles kvflowcontrol.Handles | ||
kvflowController kvflowcontrol.Controller | ||
} | ||
|
||
var _ inspectzpb.InspectzServer = &Server{} | ||
|
||
// NewServer sets up an inspectz server. | ||
func NewServer( | ||
ambient log.AmbientContext, | ||
handles kvflowcontrol.Handles, | ||
kvflowController kvflowcontrol.Controller, | ||
) *Server { | ||
mux := http.NewServeMux() | ||
server := &Server{ | ||
AmbientContext: ambient, | ||
|
||
mux: mux, | ||
handles: handles, | ||
kvflowController: kvflowController, | ||
} | ||
mux.Handle("/inspectz/kvflowhandles", http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
ctx := server.AnnotateCtx(context.Background()) | ||
|
||
req := &kvflowinspectpb.HandlesRequest{} | ||
if rangeIDs, ok := parseRangeIDs(r.URL.Query().Get("ranges"), w); ok { | ||
req.RangeIDs = rangeIDs | ||
} | ||
resp, err := server.KVFlowHandles(ctx, req) | ||
if err != nil { | ||
log.ErrorfDepth(ctx, 1, "%s", err) | ||
http.Error(w, "internal error: check logs for details", http.StatusInternalServerError) | ||
return | ||
} | ||
respond(ctx, w, http.StatusOK, resp) | ||
}, | ||
)) | ||
mux.Handle("/inspectz/kvflowcontroller", http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
ctx := server.AnnotateCtx(context.Background()) | ||
|
||
req := &kvflowinspectpb.ControllerRequest{} | ||
resp, err := server.KVFlowController(ctx, req) | ||
if err != nil { | ||
log.ErrorfDepth(ctx, 1, "%s", err) | ||
http.Error(w, "internal error: check logs for details", http.StatusInternalServerError) | ||
return | ||
} | ||
respond(ctx, w, http.StatusOK, resp) | ||
}, | ||
)) | ||
|
||
return server | ||
} | ||
|
||
// KVFlowController implements the InspectzServer interface. | ||
func (s *Server) KVFlowController( | ||
ctx context.Context, request *kvflowinspectpb.ControllerRequest, | ||
) (*kvflowinspectpb.ControllerResponse, error) { | ||
return &kvflowinspectpb.ControllerResponse{ | ||
Streams: s.kvflowController.Inspect(ctx), | ||
}, nil | ||
} | ||
|
||
// KVFlowHandles implements the InspectzServer interface. | ||
func (s *Server) KVFlowHandles( | ||
ctx context.Context, request *kvflowinspectpb.HandlesRequest, | ||
) (*kvflowinspectpb.HandlesResponse, error) { | ||
resp := &kvflowinspectpb.HandlesResponse{} | ||
if len(request.RangeIDs) == 0 { | ||
request.RangeIDs = s.handles.Inspect() | ||
} | ||
for _, rangeID := range request.RangeIDs { | ||
handle, found := s.handles.Lookup(rangeID) | ||
if !found { | ||
continue // nothing to do | ||
} | ||
resp.Handles = append(resp.Handles, handle.Inspect(ctx)) | ||
} | ||
return resp, nil | ||
} | ||
|
||
// ServeHTTP serves various tools under the /debug endpoint. | ||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
s.mux.ServeHTTP(w, r) | ||
} | ||
|
||
func respond(ctx context.Context, w http.ResponseWriter, code int, payload interface{}) { | ||
res, err := json.Marshal(payload) | ||
if err != nil { | ||
log.ErrorfDepth(ctx, 1, "%s", err) | ||
http.Error(w, "internal error: check logs for details", http.StatusInternalServerError) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(code) | ||
_, _ = w.Write(res) | ||
} | ||
|
||
func parseRangeIDs(input string, w http.ResponseWriter) (ranges []roachpb.RangeID, ok bool) { | ||
if len(input) == 0 { | ||
return nil, true | ||
} | ||
for _, part := range strings.Split(input, ",") { | ||
rangeID, err := strconv.ParseInt(part, 10, 64) | ||
if err != nil { | ||
http.Error(w, "invalid range id", http.StatusBadRequest) | ||
return nil, false | ||
} | ||
|
||
ranges = append(ranges, roachpb.RangeID(rangeID)) | ||
} | ||
return ranges, true | ||
} |
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,36 @@ | ||
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data") | ||
load("@rules_proto//proto:defs.bzl", "proto_library") | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") | ||
|
||
proto_library( | ||
name = "inspectzpb_proto", | ||
srcs = ["inspectz.proto"], | ||
strip_import_prefix = "/pkg", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/kv/kvserver/kvflowcontrol/kvflowinspectpb:kvflowinspectpb_proto", | ||
"@go_googleapis//google/api:annotations_proto", | ||
], | ||
) | ||
|
||
go_proto_library( | ||
name = "inspectzpb_go_proto", | ||
compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_grpc_compiler"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/inspectz/inspectzpb", | ||
proto = ":inspectzpb_proto", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/kv/kvserver/kvflowcontrol/kvflowinspectpb", | ||
"@org_golang_google_genproto//googleapis/api/annotations:go_default_library", | ||
], | ||
) | ||
|
||
go_library( | ||
name = "inspectzpb", | ||
embed = [":inspectzpb_go_proto"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/inspectz/inspectzpb", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
get_x_data(name = "get_x_data") |
Oops, something went wrong.