-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmasterproxy.cpp
More file actions
290 lines (231 loc) · 8.21 KB
/
Copy pathmasterproxy.cpp
File metadata and controls
290 lines (231 loc) · 8.21 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
Socle Library Ecosystem
Copyright (c) 2014, Ales Stibal <astib@mag0.net>, All rights reserved.
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 3.0 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.
*/
#include <vector>
#include "masterproxy.hpp"
#include "baseproxy.hpp"
#include "log/logger.hpp"
int MasterProxy::prepare_sockets(baseCom* xcom)
{
int r = 0;
r += baseProxy::prepare_sockets(xcom);
auto lc_ = std::scoped_lock(proxies_lock_);
for(auto& p: proxies()) {
if(p && not p->state().dead()) {
r += p->prepare_sockets(xcom); // fill my fd_sets!
}
}
return r;
}
bool MasterProxy::run_timers()
{
if(baseProxy::run_timers()) {
{
auto l_ = std::scoped_lock(proxies_lock_);
for(auto i = proxies().begin(); i != proxies().end(); ) {
auto const& p = *i;
if(not p) {
_inf("null sub-proxy!!");
i = proxies().erase(i);
continue;
}
if(not p->state().dead()) {
auto lcx = logan_context(p->to_string(iNOT));
p->run_timers();
}
else {
defer_proxy_ul(std::move(*i));
i = proxies().erase(i);
continue;
}
++i;
}
}
return true;
}
return false;
}
void MasterProxy::defer_proxy_ul(proxy_entry&& entry) {
{
auto l_ = std::scoped_lock(deferred_lock_);
deferred().push_back({std::move(entry), std::chrono::steady_clock::now()});
_deb("MasterProxy::defer_proxy_ul: queued, deferred=%zd", deferred().size());
}
}
MasterProxy::pressure_level MasterProxy::get_pressure_level() const {
auto lock = std::scoped_lock(deferred_lock_);
return deferred().size() >= deferred_pressure_threshold
? pressure_level::elevated
: pressure_level::nominal;
}
void MasterProxy::reap_deferred(std::size_t count_budget,
std::optional<std::chrono::milliseconds> time_budget,
std::size_t target_size) {
auto const started_at = std::chrono::steady_clock::now();
std::size_t reaped = 0;
std::size_t deferred_left = 0;
while(reaped < count_budget) {
proxy_entry entry;
{
auto lock = std::scoped_lock(deferred_lock_);
if(deferred().empty()) break;
if(deferred().size() <= target_size) break;
if(deferred().front().deferred_at + deferred_grace > std::chrono::steady_clock::now()) break;
entry = std::move(deferred().front().proxy);
deferred().pop_front();
deferred_left = deferred().size();
}
// HostCX and Com teardown has worker-thread affinity. Keep destruction
// on the MasterProxy worker, but outside both queue locks.
entry.reset();
++reaped;
if(time_budget and std::chrono::steady_clock::now() - started_at >= *time_budget) break;
}
if(reaped > 0) {
_deb("MasterProxy::reap_deferred: reaped=%zd, deferred=%zd", reaped, deferred_left);
}
}
int MasterProxy::handle_sockets_once(baseCom* xcom) {
switch(get_pressure_level()) {
case pressure_level::nominal:
if(++deferred_reap_tick_ >= deferred_reap_every) {
deferred_reap_tick_ = 0;
reap_deferred(1, std::nullopt);
}
break;
case pressure_level::elevated:
deferred_reap_tick_ = 0;
reap_deferred(deferred_pressure_batch, deferred_reap_time_budget);
break;
case pressure_level::critical:
deferred_reap_tick_ = 0;
reap_deferred(deferred_critical_batch, deferred_critical_time_budget);
break;
case pressure_level::emergency:
deferred_reap_tick_ = 0;
reap_deferred(deferred_emergency_batch, deferred_emergency_time_budget,
deferred_emergency_low_watermark);
break;
}
int my_handle_returned = 0;
try {
my_handle_returned = baseProxy::handle_sockets_once(xcom);
_ext("handling own sockets: returned %d", my_handle_returned);
}
catch(socle::com_error const& e) {
_err("master proxy exception: %s", e.what());
return 0;
}
int r = 0;
int proxies_handled = 0;
int proxies_shutdown = 0;
int proxies_deleted = 0;
auto l_ = std::scoped_lock(proxies_lock_);
if(proxies().empty()) return 0;
for(auto& proxy : proxies()) {
if(not proxy) continue;
if(state().dead()) {
proxy->state().dead(true);
}
if (not proxy->state().dead()) {
r++;
proxies_handled++;
auto const pref = logan_lite::context();
{
auto lcx = logan_context(proxy->to_string(iNOT));
try {
// Sub-proxies share their worker's poll cycle. Keeping the
// whole lifecycle on that worker avoids cross-thread access
// to HostCX buffers, readiness state and connection teardown.
proxy->handle_sockets_once(xcom);
}
catch (socle::com_error const &e) {
_err("slave proxy exception: %s", e.what());
proxy->state().dead(true);
}
catch (std::exception const &e) {
_err("slave proxy exception: %s", e.what());
proxy->state().dead(true);
}
}
logan_lite::context(pref);
}
}
for(auto i = proxies().begin(); i != proxies().end(); ) {
auto const& proxy = *i;
if(not proxy) {
i = proxies().erase(i);
proxies_deleted++;
continue;
}
if (proxy->state().dead()) {
auto lcx = logan_context(proxy->to_string(iNOT));
defer_proxy_ul(std::move(*i));
i = proxies().erase(i);
proxies_shutdown++;
proxies_deleted++;
continue;
}
++i;
}
_ext("MasterProxy::handle_sockets_once: returning %d, sub-proxies: handled=%d, shutdown=%d, deleted=%d",r,proxies_handled,proxies_shutdown,proxies_deleted);
return r;
}
void MasterProxy::shutdown() {
_inf("MasterProxy::shutdown");
int i = 0;
vector_type<proxy_entry> shutdown_entries;
{
// anyone getting proxies from list would get a valid pointer until it is
// atomically moved to the local shutdown list.
auto l_ = std::scoped_lock(proxies_lock_);
shutdown_entries.reserve(proxies().size());
for(auto& entry : proxies()) shutdown_entries.emplace_back(std::move(entry));
proxies().clear();
}
{
auto l_ = std::scoped_lock(deferred_lock_);
shutdown_entries.reserve(shutdown_entries.size() + deferred().size());
while(not deferred().empty()) {
shutdown_entries.emplace_back(std::move(deferred().front().proxy));
deferred().pop_front();
}
}
for(auto& proxy : shutdown_entries) {
_inf("MasterProxy::shutdown: slave[%d]",i);
if(proxy) {
proxy->shutdown();
}
i++;
}
}
std::string MasterProxy::hr() {
std::stringstream ss;
ss << "Masterproxy:\n";
ss << baseProxy::hr();
auto lc_ = std::scoped_lock(proxies_lock_);
if(not proxies().empty()) {
ss << "Slaves:\n";
int i = 0;
for(auto const& proxy: proxies()) {
ss << "slave-" + std::to_string(i) + ":\n";
ss << proxy->hr();
ss << "\n";
}
}
else {
ss << "Slaves: <empty>";
}
return ss.str();
}