Squid Web Cache master
Loading...
Searching...
No Matches
helper.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 84 Helper process maintenance */
10
11#ifndef SQUID_SRC_HELPER_H
12#define SQUID_SRC_HELPER_H
13
14#include "base/AsyncCall.h"
15#include "base/InstanceId.h"
16#include "base/RefCount.h"
17#include "cbdata.h"
18#include "comm/forward.h"
19#include "dlink.h"
20#include "helper/ChildConfig.h"
21#include "helper/forward.h"
22#include "helper/Reply.h"
23#include "helper/Request.h"
25#include "ip/Address.h"
26#include "sbuf/SBuf.h"
27
28#include <list>
29#include <map>
30#include <queue>
31#include <unordered_map>
32
34class MemBuf;
35class Packable;
36class wordlist;
37
38namespace Helper
39{
41class Xaction {
43public:
44 Xaction(HLPCB *c, void *d, const char *b): request(c, d, b) {}
47};
48
49class SessionBase;
50
66class Client: public RefCountable
67{
68public:
70
73 static Pointer Make(const char *name);
74
75 virtual ~Client();
76
79
81 bool trySubmit(const char *buf, HLPCB * callback, void *data);
82
85 void submitRequest(Xaction *);
86
88 void packStatsInto(Packable *p, const char *label = nullptr) const;
91 bool willOverload() const;
92
96
100 void handleFewerServers(bool madeProgress);
101
105 void dropQueued();
106
108 void callBack(Xaction &);
109
112 virtual void openSessions();
113
114public:
115 wordlist *cmdline = nullptr;
117 std::queue<Xaction *> queue;
118 const char *id_name = nullptr;
120 int ipc_type = 0;
122 unsigned int droppedRequests = 0;
123 time_t overloadStart = 0;
124 time_t last_queue_warn = 0;
125 time_t last_restart = 0;
126 time_t timeout = 0;
127 bool retryTimedOut = false;
128 bool retryBrokenHelper = false;
130 char eom = '\n';
131
132 struct _stats {
133 int requests = 0;
134 int replies = 0;
135 int timedout = 0;
136 int queue_size = 0;
139
140protected:
142 explicit Client(const char * const name): id_name(name) {}
143
144 bool queueFull() const;
145 bool overloaded() const;
146 void syncQueueStats();
147 bool prepSubmit();
148 void submit(const char *buf, HLPCB * callback, void *data);
149};
150
151} // namespace Helper
152
153// TODO: Rename to a *Client.
155{
156public:
158 typedef std::unordered_map<Helper::ReservationId, helper_stateful_server *> Reservations;
159
160 ~statefulhelper() override = default;
161
162 static Pointer Make(const char *name);
163
166
168 void cancelReservation(const Helper::ReservationId reservation);
169
170 /* Helper::Client API */
171 void openSessions() override;
172
173private:
174 friend void helperStatefulSubmit(const statefulhelper::Pointer &, const char *buf, HLPCB *, void *cbData, const Helper::ReservationId &);
175
176 explicit statefulhelper(const char * const name): Helper::Client(name) {}
177
180
181 void submit(const char *buf, HLPCB * callback, void *data, const Helper::ReservationId & reservation);
182 bool trySubmit(const char *buf, HLPCB * callback, void *data, const Helper::ReservationId & reservation);
183
186};
187
188namespace Helper
189{
190
193{
194public:
195 ~SessionBase() override;
196
198 static void HelperServerClosed(SessionBase *);
199
203 void closePipesSafely();
204
210
211 // TODO: Teach each child to report its child-specific state instead.
213 virtual bool reserved() = 0;
214
216 virtual Client &helper() const = 0;
217
219 virtual void dropQueued();
220
221public:
225
226 int pid;
230 void *hIpc;
231
232 char *rbuf;
233 size_t rbuf_sz;
234 size_t roffset;
235
236 struct timeval dispatch_time;
237 struct timeval answer_time;
238
240
246
247 using Requests = std::list<Xaction *>;
249
250 struct {
251 uint64_t uses; //< requests sent to this helper
252 uint64_t replies; //< replies received from this helper
253 uint64_t pending; //< queued lookups waiting to be sent to this helper
254 uint64_t releases; //< times release() has been called on this helper (if stateful)
255 uint64_t timedout; //< requests which timed-out
257 void initStats();
258};
259
262class Session: public SessionBase
263{
265
266public:
268
271
273
279
282
283 // STL says storing std::list iterators is safe when changing the list
284 typedef std::map<uint64_t, Requests::iterator> RequestIndex;
286
287 ~Session() override;
288
293 Xaction *popRequest(int requestId);
294
297 void checkForTimedOutRequests(bool const retry);
298
299 /* SessionBase API */
300 bool reserved() override {return false;}
301 void dropQueued() override;
302 Client &helper() const override { return *parent; }
303
305 static void requestTimeout(const CommTimeoutCbParams &io);
306};
307
308} // namespace Helper
309
310// TODO: Rename to a *Session, matching renamed statefulhelper.
314{
316
317public:
318 ~helper_stateful_server() override;
319 void reserve();
320 void clearReservation();
321
322 /* Helper::SessionBase API */
323 bool reserved() override {return reservationId.reserved();}
324 Helper::Client &helper() const override { return *parent; }
325
327
328 // Reservations temporary lock the server for an exclusive "client" use. The
329 // client keeps the reservation ID as a proof of her reservation. If a
330 // reservation expires, and the server is reserved for another client, then
331 // the reservation ID presented by the late client will not match ours.
334};
335
336void helperSubmit(const Helper::Client::Pointer &, const char *buf, HLPCB *, void *cbData);
337void helperStatefulSubmit(const statefulhelper::Pointer &, const char *buf, HLPCB *, void *cbData, uint64_t reservation);
340
341#endif /* SQUID_SRC_HELPER_H */
342
#define RefCountable
The locking interface for use on Reference-Counted classes.
Definition Lock.h:66
time_t timeout
Requests timeout.
Definition helper.h:126
void handleKilledServer(SessionBase *)
Definition helper.cc:868
bool retryBrokenHelper
Whether the requests must retried on BH replies.
Definition helper.h:128
bool queueFull() const
whether queuing an additional request would overload the helper
Definition helper.cc:489
Ip::Address addr
Definition helper.h:121
dlink_list servers
Definition helper.h:116
time_t last_restart
Definition helper.h:125
Client(const char *const name)
Definition helper.h:142
const char * id_name
Definition helper.h:118
unsigned int droppedRequests
requests not sent during helper overload
Definition helper.h:122
Xaction * nextRequest()
Definition helper.cc:1303
bool willOverload() const
Definition helper.cc:753
virtual ~Client()
Definition helper.cc:855
void packStatsInto(Packable *p, const char *label=nullptr) const
Dump some stats about the helper state to a Packable object.
Definition helper.cc:695
void handleFewerServers(bool madeProgress)
Definition helper.cc:909
char eom
The char which marks the end of (response) message, normally ' '.
Definition helper.h:130
void dropQueued()
Definition helper.cc:888
void submit(const char *buf, HLPCB *callback, void *data)
dispatches or enqueues a helper requests; does not enforce queue limits
Definition helper.cc:565
SBuf onTimedOutResponse
The response to use when helper response timedout.
Definition helper.h:129
bool trySubmit(const char *buf, HLPCB *callback, void *data)
If possible, submit request. Otherwise, either kill Squid or return false.
Definition helper.cc:554
void syncQueueStats()
synchronizes queue-dependent measurements with the current queue state
Definition helper.cc:500
std::queue< Xaction * > queue
Definition helper.h:117
bool prepSubmit()
Definition helper.cc:527
time_t last_queue_warn
Definition helper.h:124
void submitRequest(Xaction *)
Definition helper.cc:456
time_t overloadStart
when the helper became overloaded (zero if it is not)
Definition helper.h:123
ChildConfig childs
Configuration settings for number running.
Definition helper.h:119
bool overloaded() const
Definition helper.cc:494
void callBack(Xaction &)
sends transaction response to the transaction initiator
Definition helper.cc:573
bool retryTimedOut
Whether the timed-out requests must retried.
Definition helper.h:127
struct Helper::Client::_stats stats
wordlist * cmdline
Definition helper.h:115
static Pointer Make(const char *name)
Definition helper.cc:758
virtual void openSessions()
Definition helper.cc:189
a (temporary) lock on a (stateful) helper channel
represents a single helper process
Definition helper.h:193
uint64_t timedout
Definition helper.h:255
uint64_t replies
Definition helper.h:252
struct Helper::SessionBase::_helper_flags flags
virtual void dropQueued()
dequeues and sends an Unknown answer to all queued requests
Definition helper.cc:123
uint64_t pending
Definition helper.h:253
uint64_t releases
Definition helper.h:254
virtual Client & helper() const =0
our creator (parent) object
dlink_node link
Definition helper.h:239
Requests requests
requests in order of submission/expiration
Definition helper.h:248
Ip::Address addr
Definition helper.h:227
const InstanceId< SessionBase > index
Definition helper.h:224
struct timeval dispatch_time
Definition helper.h:236
struct timeval answer_time
Definition helper.h:237
virtual bool reserved()=0
whether the server is locked for exclusive use by a client
void closeWritePipeSafely()
Definition helper.cc:99
~SessionBase() override
Definition helper.cc:135
static void HelperServerClosed(SessionBase *)
close handler to handle exited server processes
Definition helper.cc:929
void closePipesSafely()
Definition helper.cc:73
Comm::ConnectionPointer readPipe
Definition helper.h:228
struct Helper::SessionBase::@53 stats
std::list< Xaction * > Requests
Definition helper.h:247
Comm::ConnectionPointer writePipe
Definition helper.h:229
MemBuf * wqueue
Definition helper.h:269
void dropQueued() override
dequeues and sends an Unknown answer to all queued requests
Definition helper.cc:166
bool reserved() override
whether the server is locked for exclusive use by a client
Definition helper.h:300
uint64_t nextRequestId
Definition helper.h:267
void checkForTimedOutRequests(bool const retry)
Definition helper.cc:1564
MemBuf * writebuf
Definition helper.h:270
Client::Pointer parent
Definition helper.h:272
bool ignoreToEom
Whether to ignore current message, because it is timed-out or other reason.
Definition helper.h:281
Xaction * replyXaction
Definition helper.h:278
CBDATA_CHILD(Session)
Client & helper() const override
our creator (parent) object
Definition helper.h:302
~Session() override
Definition helper.cc:143
Xaction * popRequest(int requestId)
Definition helper.cc:937
RequestIndex requestsIndex
maps request IDs to requests
Definition helper.h:285
std::map< uint64_t, Requests::iterator > RequestIndex
Definition helper.h:284
static void requestTimeout(const CommTimeoutCbParams &io)
Read timeout handler.
Definition helper.cc:1602
Holds the required data to serve a helper request.
Definition helper.h:41
Xaction(HLPCB *c, void *d, const char *b)
Definition helper.h:44
Helper::Reply reply
Definition helper.h:46
MEMPROXY_CLASS(Helper::Xaction)
Helper::Request request
Definition helper.h:45
Definition SBuf.h:94
bool reserved() override
whether the server is locked for exclusive use by a client
Definition helper.h:323
~helper_stateful_server() override
Definition helper.cc:172
statefulhelper::Pointer parent
Definition helper.h:326
Helper::Client & helper() const override
our creator (parent) object
Definition helper.h:324
time_t reservationStart
when the last reservation was made
Definition helper.h:333
CBDATA_CHILD(helper_stateful_server)
Helper::ReservationId reservationId
"confirmation ID" of the last
Definition helper.h:332
void submit(const char *buf, HLPCB *callback, void *data, const Helper::ReservationId &reservation)
Definition helper.cc:665
helper_stateful_server * findServer(const Helper::ReservationId &reservation)
Definition helper.cc:634
~statefulhelper() override=default
std::unordered_map< Helper::ReservationId, helper_stateful_server * > Reservations
Definition helper.h:158
static Pointer Make(const char *name)
Definition helper.cc:764
void reserveServer(helper_stateful_server *srv)
reserve the given server
Definition helper.cc:605
void cancelReservation(const Helper::ReservationId reservation)
undo reserveServer(), clear the reservation and kick the queue
Definition helper.cc:618
void openSessions() override
Definition helper.cc:328
statefulhelper(const char *const name)
Definition helper.h:176
Reservations reservations
Definition helper.h:185
friend void helperStatefulSubmit(const statefulhelper::Pointer &, const char *buf, HLPCB *, void *cbData, const Helper::ReservationId &)
Definition helper.cc:587
bool trySubmit(const char *buf, HLPCB *callback, void *data, const Helper::ReservationId &reservation)
reserved servers indexed by reservation IDs
Definition helper.cc:595
void HLPCB(void *, const Helper::Reply &)
Definition forward.h:33
void helperSubmit(const Helper::Client::Pointer &, const char *buf, HLPCB *, void *cbData)
Definition helper.cc:481
void helperShutdown(const Helper::Client::Pointer &)
Definition helper.cc:770
void helperStatefulSubmit(const statefulhelper::Pointer &, const char *buf, HLPCB *, void *cbData, uint64_t reservation)
void helperStatefulShutdown(const statefulhelper::Pointer &)
Definition helper.cc:809
helper protocol primitives
Definition helper.h:39