-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathroute_table_concurrency.cpp
More file actions
248 lines (220 loc) · 10.6 KB
/
Copy pathroute_table_concurrency.cpp
File metadata and controls
248 lines (220 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
// Concurrent registration + lookup stress test for the
// v2 3-tier route table. Spawns N writer threads doing register / unregister
// and M reader threads doing lookup_v2 against disjoint and overlapping
// paths. Without correct lock discipline (route_table_mutex_ as a writer
// lock during register/unregister, shared during lookup; cache mutex
// always taken AFTER table mutex when both are held), this test will
// either deadlock or race.
//
// **TSan gate (wired into per-PR CI):**
// The `build-type: tsan` lane in .github/workflows/verify-build.yml runs
// this binary RTC_ITERATIONS times via `make -C test
// check-route-table-concurrency` (time-boxed to <= 2 min), in addition to
// the single pass the tsan lane's plain `make check` already performs. To
// reproduce locally, rebuild with
// `CXXFLAGS="-fsanitize=thread -g -O1" LDFLAGS="-fsanitize=thread"` and run
// that target. See test/PERFORMANCE.md ("CI wiring — stress gates").
#include <atomic>
#include <chrono>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include "./httpserver.hpp"
#include "./httpserver/detail/webserver_impl.hpp"
#include "./littletest.hpp"
#include "./test_utils.hpp"
namespace ht = httpserver;
class noop_resource : public ht::http_resource {
public:
ht::http_response render_get(const ht::http_request&) override {
return ht::http_response::string("ok");
}
};
LT_BEGIN_SUITE(route_table_concurrency_suite)
void set_up() {}
void tear_down() {}
LT_END_SUITE(route_table_concurrency_suite)
LT_BEGIN_AUTO_TEST(route_table_concurrency_suite,
concurrent_register_and_lookup_no_data_race)
ht::webserver ws{ht::create_webserver(0)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
auto& impl = *ht::webserver_test_access::impl(ws);
// Pre-register a stable set of 32 paths so readers always have
// something to find.
for (int i = 0; i < 32; ++i) {
ws.register_path("/stable/" + std::to_string(i),
std::make_shared<noop_resource>());
}
std::atomic<bool> stop{false};
std::atomic<int> writer_ops{0};
std::atomic<int> reader_ops{0};
const int kWriters = stress_threads(4);
const int kReaders = stress_threads(16);
std::vector<std::thread> threads;
threads.reserve(kWriters + kReaders);
// Writers: register / unregister disjoint paths in their own
// numeric range to avoid duplicate-registration throws.
for (int w = 0; w < kWriters; ++w) {
threads.emplace_back([&, w] {
int counter = 0;
while (!stop.load(std::memory_order_relaxed)) {
std::string p = "/dyn/" + std::to_string(w) + "/"
+ std::to_string(counter % 8);
try {
ws.register_path(p, std::make_shared<noop_resource>());
ws.unregister_path(p);
} catch (...) {
// Race-on-duplicate is permitted under stress; we are
// exercising the lock discipline, not the user-facing
// duplicate-throw contract.
}
++counter;
writer_ops.fetch_add(1, std::memory_order_relaxed);
}
});
}
// Readers: hammer lookup_v2 against the stable + dynamic paths.
for (int r = 0; r < kReaders; ++r) {
threads.emplace_back([&, r] {
int counter = 0;
while (!stop.load(std::memory_order_relaxed)) {
std::string p = "/stable/" + std::to_string(counter % 32);
(void)impl.lookup_v2(ht::http_method::get, p);
std::string p2 = "/dyn/" + std::to_string(r % 4) + "/"
+ std::to_string(counter % 8);
(void)impl.lookup_v2(ht::http_method::get, p2);
++counter;
reader_ops.fetch_add(1, std::memory_order_relaxed);
}
});
}
// Run until enough operations have been observed by both writers and
// readers, or until a wall-clock safety ceiling (30s) to survive
// valgrind/TSan overhead. Using an operation count avoids fixed
// wall-clock sleeps that inflate on loaded CI boxes.
constexpr int kOpThreshold = 10000;
auto deadline = std::chrono::steady_clock::now() + stress_window();
while ((writer_ops.load() < kOpThreshold || reader_ops.load() < kOpThreshold)
&& std::chrono::steady_clock::now() < deadline) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
stop.store(true, std::memory_order_relaxed);
for (auto& t : threads) t.join();
// We don't assert specific counts — the gate is "completed without
// deadlock or crash". TSan-detected races would break the build
// when this same TU is rebuilt under the manual TSan gate.
// Liveness (both roles made progress) can be defeated by valgrind's
// single-core scheduler, which can starve EITHER the writers or the readers
// for the whole window — observed both writer_ops==0 and reader_ops==0
// across runs. The actual race/crash/deadlock concern is enforced by the
// valgrind/sanitizer tools themselves (0 errors) plus the completed join
// above, so only assert liveness off-valgrind.
if (!under_valgrind()) {
LT_CHECK(writer_ops.load() > 0);
LT_CHECK(reader_ops.load() > 0);
}
LT_END_AUTO_TEST(concurrent_register_and_lookup_no_data_race)
// Cycle J: concurrent register/unregister of parameterised paths alongside
// readers. Exercises the segment trie's wildcard_child_ node allocation and
// deallocation paths under contention. Writers use paths
// like /dyn/{id}/wN so the segment trie must allocate and free wildcard nodes
// concurrently with readers calling lookup_v2 on those same paths.
LT_BEGIN_AUTO_TEST(route_table_concurrency_suite,
concurrent_wildcard_node_alloc_and_lookup_no_data_race)
ht::webserver ws{ht::create_webserver(0)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
auto& impl = *ht::webserver_test_access::impl(ws);
// Pre-register a stable set of parameterised paths so readers always
// have something to find regardless of writer timing.
for (int i = 0; i < 16; ++i) {
ws.register_path("/stable/{id}/item/" + std::to_string(i),
std::make_shared<noop_resource>());
}
std::atomic<bool> stop{false};
std::atomic<int> writer_ops{0};
std::atomic<int> reader_ops{0};
const int kWriters = stress_threads(4);
const int kReaders = stress_threads(8);
std::vector<std::thread> threads;
threads.reserve(kWriters + kReaders);
// Writers: concurrently register / unregister parameterised paths with
// different writer indices so each writer owns a disjoint numeric prefix
// for the wildcard segment, exercising the segment trie's wildcard_child_
// allocation and deallocation path concurrently.
for (int w = 0; w < kWriters; ++w) {
threads.emplace_back([&, w] {
int counter = 0;
while (!stop.load(std::memory_order_relaxed)) {
std::string p = "/dyn/{id}/w" + std::to_string(w)
+ "/" + std::to_string(counter % 8);
try {
ws.register_path(p, std::make_shared<noop_resource>());
ws.unregister_path(p);
} catch (...) {
// Duplicate-registration races are tolerated; the
// lock-discipline gate is "no crash or deadlock".
}
++counter;
writer_ops.fetch_add(1, std::memory_order_relaxed);
}
});
}
// Readers: lookup both the stable parameterised paths and the paths
// under concurrent mutation to exercise the shared_lock path on the
// segment trie's wildcard_child_ node.
for (int r = 0; r < kReaders; ++r) {
threads.emplace_back([&, r] {
int counter = 0;
while (!stop.load(std::memory_order_relaxed)) {
// Stable parameterised path — always found.
std::string p = "/stable/42/item/" + std::to_string(counter % 16);
(void)impl.lookup_v2(ht::http_method::get, p);
// Dynamic path — may or may not be registered at this instant.
std::string p2 = "/dyn/99/w" + std::to_string(r % kWriters)
+ "/" + std::to_string(counter % 8);
(void)impl.lookup_v2(ht::http_method::get, p2);
++counter;
reader_ops.fetch_add(1, std::memory_order_relaxed);
}
});
}
// Same baseline + valgrind-tolerant extension as Cycle I above.
auto deadline2 = std::chrono::steady_clock::now() + stress_window();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
while ((writer_ops.load() == 0 || reader_ops.load() == 0)
&& std::chrono::steady_clock::now() < deadline2) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
stop.store(true, std::memory_order_relaxed);
for (auto& t : threads) t.join();
// Liveness (both roles made progress) can be defeated by valgrind's
// single-core scheduler, which can starve EITHER the writers or the readers
// for the whole window — observed both writer_ops==0 and reader_ops==0
// across runs. The actual race/crash/deadlock concern is enforced by the
// valgrind/sanitizer tools themselves (0 errors) plus the completed join
// above, so only assert liveness off-valgrind.
if (!under_valgrind()) {
LT_CHECK(writer_ops.load() > 0);
LT_CHECK(reader_ops.load() > 0);
}
LT_END_AUTO_TEST(concurrent_wildcard_node_alloc_and_lookup_no_data_race)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()