Skip to content

Commit

Permalink
Expose metadata limits and annotation info to filter stack
Browse files Browse the repository at this point in the history
  • Loading branch information
ananda1066 committed Feb 22, 2024
1 parent 84b0bb3 commit 4af5679
Show file tree
Hide file tree
Showing 21 changed files with 190 additions and 84 deletions.
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4245,6 +4245,7 @@ grpc_cc_library(
"//src/core:hpack_constants",
"//src/core:match",
"//src/core:metadata_batch",
"//src/core:metadata_info",
"//src/core:parsed_metadata",
"//src/core:random_early_detection",
"//src/core:slice",
Expand Down Expand Up @@ -4422,6 +4423,7 @@ grpc_cc_library(
"//src/core:max_concurrent_streams_policy",
"//src/core:memory_quota",
"//src/core:metadata_batch",
"//src/core:metadata_info",
"//src/core:ping_abuse_policy",
"//src/core:ping_callbacks",
"//src/core:ping_rate_policy",
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Package.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions build_autogenerated.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config.m4

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config.w32

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gRPC-C++.podspec

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions gRPC-Core.podspec

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions grpc.gemspec

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions grpc.gyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ grpc_cc_library(
deps = ["//:gpr_platform"],
)

grpc_cc_library(
name = "metadata_info",
srcs = ["lib/transport/metadata_info.cc"],
hdrs = ["lib/transport/metadata_info.h"],
deps = [
"channel_args",
"hpack_constants",
"metadata_batch",
"slice",
"//:gpr_platform",
"//:grpc_base",
],
)

grpc_cc_library(
name = "experiments",
srcs = [
Expand Down
38 changes: 5 additions & 33 deletions src/core/ext/transport/chttp2/transport/chttp2_transport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
#include "src/core/lib/transport/error_utils.h"
#include "src/core/lib/transport/http2_errors.h"
#include "src/core/lib/transport/metadata_batch.h"
#include "src/core/lib/transport/metadata_info.h"
#include "src/core/lib/transport/status_conversion.h"
#include "src/core/lib/transport/transport.h"

Expand All @@ -116,8 +117,6 @@
#define DEFAULT_CONNECTION_WINDOW_TARGET (1024 * 1024)
#define MAX_WINDOW 0x7fffffffu
#define MAX_WRITE_BUFFER_SIZE (64 * 1024 * 1024)
#define DEFAULT_MAX_HEADER_LIST_SIZE (16 * 1024)
#define DEFAULT_MAX_HEADER_LIST_SIZE_SOFT_LIMIT (8 * 1024)

#define KEEPALIVE_TIME_BACKOFF_MULTIPLIER 2

Expand Down Expand Up @@ -525,21 +524,8 @@ static void read_channel_args(grpc_chttp2_transport* t,
.GetDurationFromIntMillis(GRPC_ARG_HTTP_TARPIT_MAX_DURATION_MS)
.value_or(grpc_core::Duration::Seconds(1))
.millis();

const int soft_limit =
channel_args.GetInt(GRPC_ARG_MAX_METADATA_SIZE).value_or(-1);
if (soft_limit < 0) {
// Set soft limit to 0.8 * hard limit if this is larger than
// `DEFAULT_MAX_HEADER_LIST_SIZE_SOFT_LIMIT` and
// `GRPC_ARG_MAX_METADATA_SIZE` is not set.
t->max_header_list_size_soft_limit = std::max(
DEFAULT_MAX_HEADER_LIST_SIZE_SOFT_LIMIT,
static_cast<int>(
0.8 * channel_args.GetInt(GRPC_ARG_ABSOLUTE_MAX_METADATA_SIZE)
.value_or(-1)));
} else {
t->max_header_list_size_soft_limit = soft_limit;
}
t->max_header_list_size_soft_limit =
grpc_core::GetSoftLimitFromChannelArgs(channel_args);

int value;
if (!is_client) {
Expand All @@ -557,22 +543,8 @@ static void read_channel_args(grpc_chttp2_transport* t,
if (value >= 0) {
t->settings.mutable_local().SetHeaderTableSize(value);
}
value = channel_args.GetInt(GRPC_ARG_ABSOLUTE_MAX_METADATA_SIZE).value_or(-1);
if (value >= 0) {
t->settings.mutable_local().SetMaxHeaderListSize(value);
} else {
// Set value to 1.25 * soft limit if this is larger than
// `DEFAULT_MAX_HEADER_LIST_SIZE` and
// `GRPC_ARG_ABSOLUTE_MAX_METADATA_SIZE` is not set.
const int soft_limit =
channel_args.GetInt(GRPC_ARG_MAX_METADATA_SIZE).value_or(-1);
const int value = (soft_limit >= 0 && soft_limit < (INT_MAX / 1.25))
? static_cast<int>(soft_limit * 1.25)
: soft_limit;
if (value > DEFAULT_MAX_HEADER_LIST_SIZE) {
t->settings.mutable_local().SetMaxHeaderListSize(value);
}
}
t->settings.mutable_local().SetMaxHeaderListSize(
grpc_core::GetHardLimitFromChannelArgs(channel_args));
value = channel_args.GetInt(GRPC_ARG_HTTP2_MAX_FRAME_SIZE).value_or(-1);
if (value >= 0) {
t->settings.mutable_local().SetMaxFrameSize(value);
Expand Down
50 changes: 1 addition & 49 deletions src/core/ext/transport/chttp2/transport/hpack_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "src/core/lib/slice/slice.h"
#include "src/core/lib/slice/slice_refcount.h"
#include "src/core/lib/surface/validate_metadata.h"
#include "src/core/lib/transport/metadata_info.h"
#include "src/core/lib/transport/parsed_metadata.h"

// IWYU pragma: no_include <type_traits>
Expand Down Expand Up @@ -1076,55 +1077,6 @@ Slice HPackParser::String::Take() {
GPR_UNREACHABLE_CODE(return Slice());
}

class HPackParser::MetadataSizeEncoder {
public:
explicit MetadataSizeEncoder(std::string& summary) : summary_(summary) {}

void Encode(const Slice& key, const Slice& value) {
AddToSummary(key.as_string_view(), value.size());
}

template <typename Key, typename Value>
void Encode(Key, const Value& value) {
AddToSummary(Key::key(), EncodedSizeOfKey(Key(), value));
}

private:
void AddToSummary(absl::string_view key,
size_t value_length) GPR_ATTRIBUTE_NOINLINE {
absl::StrAppend(&summary_, key, ":",
hpack_constants::SizeForEntry(key.size(), value_length),
",");
}
std::string& summary_;
};

class HPackParser::MetadataSizesAnnotation
: public CallTracerAnnotationInterface::Annotation {
public:
MetadataSizesAnnotation(grpc_metadata_batch* metadata_buffer,
uint64_t soft_limit, uint64_t hard_limit)
: CallTracerAnnotationInterface::Annotation(
CallTracerAnnotationInterface::AnnotationType::kMetadataSizes),
metadata_buffer_(metadata_buffer),
soft_limit_(soft_limit),
hard_limit_(hard_limit) {}

std::string ToString() const override {
std::string metadata_annotation =
absl::StrCat("gRPC metadata soft_limit:", soft_limit_,
",hard_limit:", hard_limit_, ",");
MetadataSizeEncoder encoder(metadata_annotation);
metadata_buffer_->Encode(&encoder);
return metadata_annotation;
}

private:
grpc_metadata_batch* metadata_buffer_;
uint64_t soft_limit_;
uint64_t hard_limit_;
};

// PUBLIC INTERFACE

HPackParser::HPackParser() = default;
Expand Down
2 changes: 0 additions & 2 deletions src/core/ext/transport/chttp2/transport/hpack_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ class HPackParser {
// Helper classes: see implementation
class Parser;
class Input;
class MetadataSizeEncoder;
class MetadataSizesAnnotation;

// Helper to parse a string and turn it into a slice with appropriate memory
// management characteristics
Expand Down
53 changes: 53 additions & 0 deletions src/core/lib/transport/metadata_info.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2023 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/core/lib/transport/metadata_info.h"

#include "src/core/lib/slice/slice.h"

namespace grpc_core {

class MetadataSizesAnnotation::MetadataSizeEncoder {
public:
explicit MetadataSizeEncoder(std::string& summary) : summary_(summary) {}

void Encode(const Slice& key, const Slice& value) {
AddToSummary(key.as_string_view(), value.size());
}

template <typename Key, typename Value>
void Encode(Key, const Value& value) {
AddToSummary(Key::key(), EncodedSizeOfKey(Key(), value));
}

private:
void AddToSummary(absl::string_view key,
size_t value_length) GPR_ATTRIBUTE_NOINLINE {
absl::StrAppend(&summary_, key, ":",
hpack_constants::SizeForEntry(key.size(), value_length),
",");
}
std::string& summary_;
};

std::string MetadataSizesAnnotation::ToString() const {
std::string metadata_annotation =
absl::StrCat("gRPC metadata soft_limit:", soft_limit_,
",hard_limit:", hard_limit_, ",");
MetadataSizeEncoder encoder(metadata_annotation);
metadata_buffer_->Encode(&encoder);
return metadata_annotation;
}

} // namespace grpc_core
Loading

0 comments on commit 4af5679

Please sign in to comment.