helper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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"
24 #include "helper/ReservationId.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 
34 class MemBuf;
35 class Packable;
36 class wordlist;
37 
38 namespace Helper
39 {
41 class Xaction {
43 public:
44  Xaction(HLPCB *c, void *d, const char *b): request(c, d, b) {}
47 };
48 
49 class SessionBase;
50 
66 class Client: public RefCountable
67 {
68 public:
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 
114 public:
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;
137  int avg_svc_time = 0;
138  } stats;
139 
140 protected:
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 {
156 public:
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 
173 private:
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 
188 namespace Helper
189 {
190 
193 {
194 public:
195  ~SessionBase() override;
196 
198  static void HelperServerClosed(SessionBase *);
199 
203  void closePipesSafely();
204 
209  void closeWritePipeSafely();
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 
221 public:
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 
241  struct _helper_flags {
242  bool writing;
243  bool closing;
244  bool shutdown;
245  } flags;
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
256  } stats;
257  void initStats();
258 };
259 
262 class Session: public SessionBase
263 {
265 
266 public:
267  uint64_t nextRequestId;
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 
317 public:
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 
336 void helperSubmit(const Helper::Client::Pointer &, const char *buf, HLPCB *, void *cbData);
337 void helperStatefulSubmit(const statefulhelper::Pointer &, const char *buf, HLPCB *, void *cbData, uint64_t reservation);
340 
341 #endif /* SQUID_SRC_HELPER_H */
342 
uint64_t replies
Definition: helper.h:252
unsigned int droppedRequests
requests not sent during helper overload
Definition: helper.h:122
time_t reservationStart
when the last reservation was made
Definition: helper.h:333
Xaction * nextRequest()
Definition: helper.cc:1302
void openSessions() override
Definition: helper.cc:327
char eom
The char which marks the end of (response) message, normally ' '.
Definition: helper.h:130
Definition: Client.h:29
Ip::Address addr
Definition: helper.h:121
struct timeval dispatch_time
Definition: helper.h:236
bool reserved() const
Definition: ReservationId.h:22
helper_stateful_server * findServer(const Helper::ReservationId &reservation)
Definition: helper.cc:633
uint64_t nextRequestId
Definition: helper.h:267
int ipc_type
Definition: helper.h:120
uint64_t uses
Definition: helper.h:251
static Pointer Make(const char *name)
Definition: helper.cc:763
~SessionBase() override
Definition: helper.cc:134
Xaction * replyXaction
Definition: helper.h:278
virtual Client & helper() const =0
our creator (parent) object
void handleFewerServers(bool madeProgress)
Definition: helper.cc:908
struct Helper::SessionBase::_helper_flags flags
uint64_t releases
Definition: helper.h:254
Definition: SBuf.h:93
void submitRequest(Xaction *)
Definition: helper.cc:455
Comm::ConnectionPointer writePipe
Definition: helper.h:229
~helper_stateful_server() override
Definition: helper.cc:171
void packStatsInto(Packable *p, const char *label=nullptr) const
Dump some stats about the helper state to a Packable object.
Definition: helper.cc:694
Client::Pointer parent
Definition: helper.h:272
time_t last_queue_warn
Definition: helper.h:124
Helper::Client & helper() const override
our creator (parent) object
Definition: helper.h:324
Helper::Reply reply
Definition: helper.h:46
bool trySubmit(const char *buf, HLPCB *callback, void *data)
If possible, submit request. Otherwise, either kill Squid or return false.
Definition: helper.cc:553
time_t last_restart
Definition: helper.h:125
void callBack(Xaction &)
sends transaction response to the transaction initiator
Definition: helper.cc:572
dlink_node link
Definition: helper.h:239
static void requestTimeout(const CommTimeoutCbParams &io)
Read timeout handler.
Definition: helper.cc:1601
CBDATA_CHILD(helper_stateful_server)
std::list< Xaction * > Requests
Definition: helper.h:247
Xaction(HLPCB *c, void *d, const char *b)
Definition: helper.h:44
std::unordered_map< Helper::ReservationId, helper_stateful_server * > Reservations
Definition: helper.h:158
static Pointer Make(const char *name)
Definition: helper.cc:757
void submit(const char *buf, HLPCB *callback, void *data, const Helper::ReservationId &reservation)
Definition: helper.cc:664
void cancelReservation(const Helper::ReservationId reservation)
undo reserveServer(), clear the reservation and kick the queue
Definition: helper.cc:617
void reserveServer(helper_stateful_server *srv)
reserve the given server
Definition: helper.cc:604
void closeWritePipeSafely()
Definition: helper.cc:98
SBuf onTimedOutResponse
The response to use when helper response timedout.
Definition: helper.h:129
void helperStatefulShutdown(const statefulhelper::Pointer &)
Definition: helper.cc:808
Comm::ConnectionPointer readPipe
Definition: helper.h:228
MemBuf * writebuf
Definition: helper.h:270
MemBuf * wqueue
Definition: helper.h:269
Client & helper() const override
our creator (parent) object
Definition: helper.h:302
bool reserved() override
whether the server is locked for exclusive use by a client
Definition: helper.h:300
void checkForTimedOutRequests(bool const retry)
Definition: helper.cc:1563
bool ignoreToEom
Whether to ignore current message, because it is timed-out or other reason.
Definition: helper.h:281
void handleKilledServer(SessionBase *)
Definition: helper.cc:867
bool reserved() override
whether the server is locked for exclusive use by a client
Definition: helper.h:323
struct timeval answer_time
Definition: helper.h:237
virtual ~Client()
Definition: helper.cc:854
Definition: MemBuf.h:23
static void HelperServerClosed(SessionBase *)
close handler to handle exited server processes
Definition: helper.cc:928
Requests requests
requests in order of submission/expiration
Definition: helper.h:248
struct Helper::Client::_stats stats
bool overloaded() const
Definition: helper.cc:493
struct Helper::SessionBase::@60 stats
bool willOverload() const
Definition: helper.cc:752
ChildConfig childs
Configuration settings for number running.
Definition: helper.h:119
const InstanceId< SessionBase > index
Definition: helper.h:224
Ip::Address addr
Definition: helper.h:227
void submit(const char *buf, HLPCB *callback, void *data)
dispatches or enqueues a helper requests; does not enforce queue limits
Definition: helper.cc:564
CBDATA_CHILD(Session)
void helperSubmit(const Helper::Client::Pointer &, const char *buf, HLPCB *, void *cbData)
Definition: helper.cc:480
a (temporary) lock on a (stateful) helper channel
Definition: ReservationId.h:17
helper protocol primitives
Definition: ChildConfig.h:12
std::queue< Xaction * > queue
Definition: helper.h:117
wordlist * cmdline
Definition: helper.h:115
const char * id_name
Definition: helper.h:118
Xaction * popRequest(int requestId)
Definition: helper.cc:936
void initStats()
Definition: helper.cc:62
bool retryTimedOut
Whether the timed-out requests must retried.
Definition: helper.h:127
bool prepSubmit()
Definition: helper.cc:526
time_t timeout
Requests timeout.
Definition: helper.h:126
time_t overloadStart
when the helper became overloaded (zero if it is not)
Definition: helper.h:123
virtual void dropQueued()
dequeues and sends an Unknown answer to all queued requests
Definition: helper.cc:122
bool trySubmit(const char *buf, HLPCB *callback, void *data, const Helper::ReservationId &reservation)
reserved servers indexed by reservation IDs
Definition: helper.cc:594
std::map< uint64_t, Requests::iterator > RequestIndex
Definition: helper.h:284
statefulhelper::Pointer parent
Definition: helper.h:326
void closePipesSafely()
Definition: helper.cc:72
void dropQueued()
Definition: helper.cc:887
~Session() override
Definition: helper.cc:142
virtual bool reserved()=0
whether the server is locked for exclusive use by a client
uint64_t pending
Definition: helper.h:253
void dropQueued() override
dequeues and sends an Unknown answer to all queued requests
Definition: helper.cc:165
statefulhelper(const char *const name)
Definition: helper.h:176
friend void helperStatefulSubmit(const statefulhelper::Pointer &, const char *buf, HLPCB *, void *cbData, const Helper::ReservationId &)
Definition: helper.cc:586
bool retryBrokenHelper
Whether the requests must retried on BH replies.
Definition: helper.h:128
Client(const char *const name)
Definition: helper.h:142
Helper::Request request
Definition: helper.h:45
void helperShutdown(const Helper::Client::Pointer &)
Definition: helper.cc:769
void HLPCB(void *, const Helper::Reply &)
Definition: forward.h:33
MEMPROXY_CLASS(Helper::Xaction)
virtual void openSessions()
Definition: helper.cc:188
void helperStatefulSubmit(const statefulhelper::Pointer &, const char *buf, HLPCB *, void *cbData, uint64_t reservation)
RequestIndex requestsIndex
maps request IDs to requests
Definition: helper.h:285
~statefulhelper() override=default
void syncQueueStats()
synchronizes queue-dependent measurements with the current queue state
Definition: helper.cc:499
uint64_t timedout
Definition: helper.h:255
dlink_list servers
Definition: helper.h:116
represents a single helper process
Definition: helper.h:192
Helper::ReservationId reservationId
"confirmation ID" of the last
Definition: helper.h:332
Reservations reservations
Definition: helper.h:185
Holds the required data to serve a helper request.
Definition: helper.h:41
bool queueFull() const
whether queuing an additional request would overload the helper
Definition: helper.cc:488

 

Introduction

Documentation

Support

Miscellaneous