Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated fix for refs/heads/experiments-update #272

Open
wants to merge 27 commits into
base: experiments-update
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5cb2b62
[experiments] Add a grpc_generate_one_off_internal_targets hook in th…
Vignesh2208 May 31, 2023
242d9ef
sanity
Vignesh2208 May 31, 2023
4851981
sanity
Vignesh2208 Jun 1, 2023
089b1e7
[experiments] Fix experiment expiry
Vignesh2208 Jun 2, 2023
4e6a437
remove expiry checking
Vignesh2208 Jun 2, 2023
a34698c
Merge branch 'master' into experiments-update
Vignesh2208 Jun 2, 2023
cce7927
[experiments] Fix default value for debug builds
Vignesh2208 Jun 2, 2023
b1092f9
Merge branch 'master' into experiments-update
Vignesh2208 Jun 2, 2023
402767e
add a generated test
Vignesh2208 Jun 3, 2023
99b46e9
format
Vignesh2208 Jun 3, 2023
a4613d6
sanity
Vignesh2208 Jun 3, 2023
97aaa22
update
Vignesh2208 Jun 5, 2023
33db085
Merge branch 'master' into experiments-update
Vignesh2208 Jun 6, 2023
17f51f3
sanity
Vignesh2208 Jun 6, 2023
cc5c265
merge
Vignesh2208 Jun 21, 2023
0ac3583
Adding a hermetic experiments test
Vignesh2208 Jun 21, 2023
3673795
regenerate projects
Vignesh2208 Jun 21, 2023
025a944
regenerate projects
Vignesh2208 Jun 21, 2023
b8a63ca
Merge branch 'master' into experiments-update
Vignesh2208 Jun 21, 2023
43293dd
sanity
Vignesh2208 Jun 21, 2023
fb7206d
review comments
Vignesh2208 Jun 21, 2023
1e86d91
regenerate projects
Vignesh2208 Jun 21, 2023
783eb59
sanity
Vignesh2208 Jun 21, 2023
8a74743
review comments
Vignesh2208 Jun 23, 2023
79d1983
sanity
Vignesh2208 Jun 23, 2023
cee2525
review comments
Vignesh2208 Jun 26, 2023
5434a0b
Automated change: Fix sanity tests
Vignesh2208 Jun 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions CMakeLists.txt

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

12 changes: 12 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 src/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ grpc_cc_library(
"absl/strings",
],
language = "c++",
tags = ["nofixdeps"],
deps = [
"no_destruct",
"//:config_vars",
Expand Down
36 changes: 35 additions & 1 deletion src/core/lib/experiments/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ std::atomic<bool> g_loaded(false);
absl::AnyInvocable<bool(struct ExperimentMetadata)>* g_check_constraints_cb =
nullptr;

class TestExperiments {
public:
TestExperiments(const ExperimentMetadata* experiment_metadata,
size_t num_experiments) {
enabled_ = new bool[num_experiments];
for (size_t i = 0; i < num_experiments; i++) {
if (g_check_constraints_cb != nullptr) {
enabled_[i] = (*g_check_constraints_cb)(experiment_metadata[i]);
} else {
enabled_[i] = experiment_metadata[i].default_value;
}
}
}

// Overloading [] operator to access elements in array style
bool operator[](int index) { return enabled_[index]; }

~TestExperiments() { delete enabled_; }

private:
bool* enabled_;
};

TestExperiments* g_test_experiments = nullptr;

GPR_ATTRIBUTE_NOINLINE Experiments LoadExperimentsFromConfigVariable() {
g_loaded.store(true, std::memory_order_relaxed);
// Set defaults from metadata.
Expand Down Expand Up @@ -111,11 +136,20 @@ void TestOnlyReloadExperimentsFromConfigVariables() {
PrintExperimentsList();
}

void LoadTestOnlyExperimentsFromMetadata(
const ExperimentMetadata* experiment_metadata, size_t num_experiments) {
g_test_experiments =
new TestExperiments(experiment_metadata, num_experiments);
}

bool IsExperimentEnabled(size_t experiment_id) {
// Normal path: just return the value;
return ExperimentsSingleton().enabled[experiment_id];
}

bool IsTestExperimentEnabled(size_t experiment_id) {
return (*g_test_experiments)[experiment_id];
}

void PrintExperimentsList() {
size_t max_experiment_length = 0;
for (size_t i = 0; i < kNumExperiments; i++) {
Expand Down
28 changes: 20 additions & 8 deletions src/core/lib/experiments/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,37 @@

namespace grpc_core {

struct ExperimentMetadata {
const char* name;
const char* description;
const char* additional_constaints;
bool default_value;
bool allow_in_fuzzing_config;
};

#ifndef GRPC_EXPERIMENTS_ARE_FINAL
// Return true if experiment \a experiment_id is enabled.
// Experiments are numbered by their order in the g_experiment_metadata array
// declared in experiments.h.
bool IsExperimentEnabled(size_t experiment_id);

// Given a test experiment id, returns true if the test experiment is enabled.
// Test experiments can be loaded using the LoadTestOnlyExperimentsFromMetadata
// method.
bool IsTestExperimentEnabled(size_t experiment_id);

// Reload experiment state from config variables.
// Does not change ForceEnableExperiment state.
// Expects the caller to handle global thread safety - so really only
// appropriate for carefully written tests.
void TestOnlyReloadExperimentsFromConfigVariables();

// Reload experiment state from passed metadata.
// Does not change ForceEnableExperiment state.
// Expects the caller to handle global thread safety - so really only
// appropriate for carefully written tests.
void LoadTestOnlyExperimentsFromMetadata(
const ExperimentMetadata* experiment_metadata, size_t num_experiments);
#endif

// Print out a list of all experiments that are built into this binary.
Expand All @@ -49,14 +69,6 @@ void PrintExperimentsList();
// If this is called twice for the same experiment, both calls must agree.
void ForceEnableExperiment(absl::string_view experiment_name, bool enable);

struct ExperimentMetadata {
const char* name;
const char* description;
const char* additional_constaints;
bool default_value;
bool allow_in_fuzzing_config;
};

// Register a function to be called to validate the value an experiment can
// take subject to additional constraints.
// The function will take the ExperimentMetadata as its argument. It will return
Expand Down
45 changes: 45 additions & 0 deletions test/core/experiments/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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.

load("//bazel:grpc_build_system.bzl", "grpc_cc_library", "grpc_cc_test")

grpc_cc_library(
name = "experiments_lib",
srcs = [
"fixtures/experiments.cc",
],
hdrs = [
"fixtures/experiments.h",
],
language = "c++",
deps = [
"//:config_vars",
"//:gpr",
"//src/core:experiments",
"//src/core:no_destruct",
],
)

grpc_cc_test(
name = "experiments_test",
srcs = ["experiments_test.cc"],
external_deps = ["gtest"],
language = "C++",
uses_event_engine = False,
uses_polling = False,
deps = [
":experiments_lib",
"//test/core/util:grpc_test_util",
],
)
62 changes: 62 additions & 0 deletions test/core/experiments/experiments_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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.

// Auto generated by tools/codegen/core/gen_experiments.py

#include <grpc/support/port_platform.h>

#include "test/core/experiments/fixtures/experiments.h"

#include "gtest/gtest.h"

#include "src/core/lib/experiments/config.h"

#ifndef GRPC_EXPERIMENTS_ARE_FINAL

bool GetExperimentTestExperiment1ExpectedValue() { return false; }

bool GetExperimentTestExperiment2ExpectedValue() { return false; }

bool GetExperimentTestExperiment3ExpectedValue() {
#ifdef NDEBUG
return false;
#else
return true;
#endif
}

bool GetExperimentTestExperiment4ExpectedValue() { return true; }

TEST(ExperimentsTest, CheckExperimentValuesTest) {
ASSERT_EQ(grpc_core::IsTestExperiment1Enabled(),
GetExperimentTestExperiment1ExpectedValue());

ASSERT_EQ(grpc_core::IsTestExperiment2Enabled(),
GetExperimentTestExperiment2ExpectedValue());

ASSERT_EQ(grpc_core::IsTestExperiment3Enabled(),
GetExperimentTestExperiment3ExpectedValue());

ASSERT_EQ(grpc_core::IsTestExperiment4Enabled(),
GetExperimentTestExperiment4ExpectedValue());
}

#endif // GRPC_EXPERIMENTS_ARE_FINAL

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
grpc_core::LoadTestOnlyExperimentsFromMetadata(
grpc_core::g_test_experiment_metadata, grpc_core::kNumTestExperiments);
return RUN_ALL_TESTS();
}
52 changes: 52 additions & 0 deletions test/core/experiments/fixtures/experiments.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.

// Auto generated by tools/codegen/core/gen_experiments.py

#include <grpc/support/port_platform.h>

#include "test/core/experiments/fixtures/experiments.h"

#ifndef GRPC_EXPERIMENTS_ARE_FINAL
namespace {
const char* const description_test_experiment_1 = "Test Experiment 1";
const char* const additional_constraints_test_experiment_1 = "{}";
const char* const description_test_experiment_2 = "Test Experiment 2";
const char* const additional_constraints_test_experiment_2 = "{}";
const char* const description_test_experiment_3 = "Test Experiment 3";
const char* const additional_constraints_test_experiment_3 = "{}";
const char* const description_test_experiment_4 = "Test Experiment 4";
const char* const additional_constraints_test_experiment_4 = "{}";
#ifdef NDEBUG
const bool kDefaultForDebugOnly = false;
#else
const bool kDefaultForDebugOnly = true;
#endif
} // namespace

namespace grpc_core {

const ExperimentMetadata g_test_experiment_metadata[] = {
{"test_experiment_1", description_test_experiment_1,
additional_constraints_test_experiment_1, false, true},
{"test_experiment_2", description_test_experiment_2,
additional_constraints_test_experiment_2, false, true},
{"test_experiment_3", description_test_experiment_3,
additional_constraints_test_experiment_3, kDefaultForDebugOnly, true},
{"test_experiment_4", description_test_experiment_4,
additional_constraints_test_experiment_4, true, true},
};

} // namespace grpc_core
#endif
Loading