Debian Codegraph @

src/core/lib/experiments/config.cc

raw
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <grpc/support/port_platform.h>
16
17 #include "src/core/lib/experiments/config.h"
18
19 #include <string.h>
20
21 #include <algorithm>
22 #include <atomic>
23 #include <string>
24
25 #include "absl/strings/ascii.h"
26 #include "absl/strings/str_cat.h"
27 #include "absl/strings/str_split.h"
28 #include "absl/strings/string_view.h"
29
30 #include <grpc/support/log.h>
31
32 #include "src/core/lib/experiments/experiments.h"
33 #include "src/core/lib/gprpp/global_config.h"
34 #include "src/core/lib/gprpp/memory.h"
35 #include "src/core/lib/gprpp/no_destruct.h"
36
37 GPR_GLOBAL_CONFIG_DEFINE_STRING(
38 grpc_experiments, "",
39 "List of grpc experiments to enable (or with a '-' prefix to disable).");
40
41 namespace grpc_core {
42
43 namespace {
44 struct Experiments {
45 bool enabled[kNumExperiments];
46 };
47
48 struct ForcedExperiment {
49 bool forced = false;
50 bool value;
51 };
52 ForcedExperiment g_forced_experiments[kNumExperiments];
53
54 std::atomic<bool> g_loaded;
55
56 GPR_ATTRIBUTE_NOINLINE Experiments LoadExperimentsFromConfigVariable() {
57 GPR_ASSERT(g_loaded.exchange(true, std::memory_order_relaxed) == false);
58 // Set defaults from metadata.
59 Experiments experiments;
60 for (size_t i = 0; i < kNumExperiments; i++) {
61 experiments.enabled[i] = g_experiment_metadata[i].default_value;
62 }
63 // Get the global config.
64 auto experiments_str = GPR_GLOBAL_CONFIG_GET(grpc_experiments);
65 // For each comma-separated experiment in the global config:
66 for (auto experiment :
67 absl::StrSplit(absl::string_view(experiments_str.get()), ',')) {
68 // Strip whitespace.
69 experiment = absl::StripAsciiWhitespace(experiment);
70 // Handle ",," without crashing.
71 if (experiment.empty()) continue;
72 // Enable unless prefixed with '-' (=> disable).
73 bool enable = true;
74 if (experiment[0] == '-') {
75 enable = false;
76 experiment.remove_prefix(1);
77 }
78 // See if we can find the experiment in the list in this binary.
79 bool found = false;
80 for (size_t i = 0; i < kNumExperiments; i++) {
81 if (experiment == g_experiment_metadata[i].name) {
82 experiments.enabled[i] = enable;
83 found = true;
84 break;
85 }
86 }
87 // If not found log an error, but don't take any other action.
88 // Allows us an easy path to disabling experiments.
89 if (!found) {
90 gpr_log(GPR_ERROR, "Unknown experiment: %s",
91 std::string(experiment).c_str());
92 }
93 }
94 return experiments;
95 }
96 } // namespace
97
98 bool IsExperimentEnabled(size_t experiment_id) {
99 // One time initialization:
100 static const NoDestruct<Experiments> experiments{
101 LoadExperimentsFromConfigVariable()};
102 // Normal path: just return the value;
103 return experiments->enabled[experiment_id];
104 }
105
106 void PrintExperimentsList() {
107 size_t max_experiment_length = 0;
108 for (size_t i = 0; i < kNumExperiments; i++) {
109 max_experiment_length =
110 std::max(max_experiment_length, strlen(g_experiment_metadata[i].name));
111 }
112 for (size_t i = 0; i < kNumExperiments; i++) {
113 gpr_log(GPR_DEBUG, "%s",
114 absl::StrCat(
115 "gRPC EXPERIMENT ", g_experiment_metadata[i].name,
116 std::string(max_experiment_length -
117 strlen(g_experiment_metadata[i].name) + 1,
118 ' '),
119 IsExperimentEnabled(i) ? "ON " : "OFF", " (default:",
120 g_experiment_metadata[i].default_value ? "ON" : "OFF",
121 g_forced_experiments[i].forced
122 ? absl::StrCat(" force:",
123 g_forced_experiments[i].value ? "ON" : "OFF")
124 : std::string(),
125 ")")
126 .c_str());
127 }
128 }
129
130 void ForceEnableExperiment(absl::string_view experiment, bool enable) {
131 GPR_ASSERT(g_loaded.load(std::memory_order_relaxed) == false);
132 for (size_t i = 0; i < kNumExperiments; i++) {
133 if (g_experiment_metadata[i].name != experiment) continue;
134 if (g_forced_experiments[i].forced) {
135 GPR_ASSERT(g_forced_experiments[i].value == enable);
136 } else {
137 g_forced_experiments[i].forced = true;
138 g_forced_experiments[i].value = enable;
139 }
140 return;
141 }
142 gpr_log(GPR_INFO, "gRPC EXPERIMENT %s not found to force %s",
143 std::string(experiment).c_str(), enable ? "enable" : "disable");
144 }
145
146 } // namespace grpc_core