2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 84 Helper process maintenance */
11 #ifndef SQUID_HELPER_H
12 #define SQUID_HELPER_H
14 #include "base/AsyncCall.h"
15 #include "base/InstanceId.h"
17 #include "comm/forward.h"
19 #include "helper/ChildConfig.h"
20 #include "helper/forward.h"
21 #include "helper/Reply.h"
22 #include "helper/Request.h"
23 #include "ip/Address.h"
24 #include "sbuf/SBuf.h"
35 /// Holds the required data to serve a helper request.
37 MEMPROXY_CLASS(Helper::Xaction
);
39 Xaction(HLPCB
*c
, void *d
, const char *b
): request(c
, d
, b
) {}
40 Helper::Request request
;
46 * Managers a set of individual helper processes with a common queue of requests.
48 * With respect to load, a helper goes through these states (roughly):
49 * idle: no processes are working on requests (and no requests are queued);
50 * normal: some, but not all processes are working (and no requests are queued);
51 * busy: all processes are working (and some requests are possibly queued);
52 * overloaded: a busy helper with more than queue-size requests in the queue.
54 * A busy helper queues new requests and issues a WARNING every 10 minutes or so.
55 * An overloaded helper either drops new requests or keeps queuing them, depending on
56 * whether the caller can handle dropped requests (trySubmit vs helperSubmit APIs).
57 * If an overloaded helper has been overloaded for 3+ minutes, an attempt to use
58 * it results in on-persistent-overload action, which may kill worker.
65 inline helper(const char *name
) :
75 retryBrokenHelper(false),
77 memset(&stats
, 0, sizeof(stats
));
81 /// \returns next request in the queue, or nil.
82 Helper::Xaction
*nextRequest();
84 /// If possible, submit request. Otherwise, either kill Squid or return false.
85 bool trySubmit(const char *buf
, HLPCB
* callback
, void *data
);
87 /// Submits a request to the helper or add it to the queue if none of
88 /// the servers is available.
89 void submitRequest(Helper::Xaction
*r
);
91 /// Dump some stats about the helper state to a Packable object
92 void packStatsInto(Packable
*p
, const char *label
= NULL
) const;
93 /// whether the helper will be in "overloaded" state after one more request
94 /// already overloaded helpers return true
95 bool willOverload() const;
100 std::queue
<Helper::Xaction
*> queue
;
102 Helper::ChildConfig childs
; ///< Configuration settings for number running.
105 unsigned int droppedRequests
; ///< requests not sent during helper overload
106 time_t overloadStart
; ///< when the helper became overloaded (zero if it is not)
107 time_t last_queue_warn
;
109 time_t timeout
; ///< Requests timeout
110 bool retryTimedOut
; ///< Whether the timed-out requests must retried
111 bool retryBrokenHelper
; ///< Whether the requests must retried on BH replies
112 SBuf onTimedOutResponse
; ///< The response to use when helper response timedout
113 char eom
; ///< The char which marks the end of (response) message, normally '\n'
124 friend void helperSubmit(helper
* hlp
, const char *buf
, HLPCB
* callback
, void *data
);
125 bool queueFull() const;
126 bool overloaded() const;
127 void syncQueueStats();
129 void submit(const char *buf
, HLPCB
* callback
, void *data
);
132 class statefulhelper
: public helper
134 CBDATA_CLASS(statefulhelper
);
137 inline statefulhelper(const char *name
) : helper(name
), datapool(NULL
) {}
138 inline ~statefulhelper() {}
141 MemAllocator
*datapool
;
144 friend void helperStatefulSubmit(statefulhelper
* hlp
, const char *buf
, HLPCB
* callback
, void *data
, helper_stateful_server
* lastserver
);
145 void submit(const char *buf
, HLPCB
* callback
, void *data
, helper_stateful_server
*lastserver
);
146 bool trySubmit(const char *buf
, HLPCB
* callback
, void *data
, helper_stateful_server
*lastserver
);
150 * Fields shared between stateless and stateful helper servers.
152 class HelperServerBase
155 /** Closes pipes to the helper safely.
156 * Handles the case where the read and write pipes are the same FD.
158 * \param name displayed for the helper being shutdown if logging an error
160 void closePipesSafely(const char *name
);
162 /** Closes the reading pipe.
163 * If the read and write sockets are the same the write pipe will
164 * also be closed. Otherwise its left open for later handling.
166 * \param name displayed for the helper being shutdown if logging an error
168 void closeWritePipeSafely(const char *name
);
171 /// Helper program identifier; does not change when contents do,
172 /// including during assignment
173 const InstanceId
<HelperServerBase
> index
;
176 Comm::ConnectionPointer readPipe
;
177 Comm::ConnectionPointer writePipe
;
184 struct timeval dispatch_time
;
185 struct timeval answer_time
;
189 struct _helper_flags
{
196 typedef std::list
<Helper::Xaction
*> Requests
;
197 Requests requests
; ///< requests in order of submission/expiration
200 uint64_t uses
; //< requests sent to this helper
201 uint64_t replies
; //< replies received from this helper
202 uint64_t pending
; //< queued lookups waiting to be sent to this helper
203 uint64_t releases
; //< times release() has been called on this helper (if stateful)
204 uint64_t timedout
; //< requests which timed-out
210 class CommTimeoutCbParams
;
212 class helper_server
: public HelperServerBase
214 CBDATA_CLASS(helper_server
);
217 uint64_t nextRequestId
;
224 /// The helper request Xaction object for the current reply .
225 /// A helper reply may be distributed to more than one of the retrieved
226 /// packets from helper. This member stores the Xaction object as long as
227 /// the end-of-message for current reply is not retrieved.
228 Helper::Xaction
*replyXaction
;
230 /// Whether to ignore current message, because it is timed-out or other reason
233 // STL says storing std::list iterators is safe when changing the list
234 typedef std::map
<uint64_t, Requests::iterator
> RequestIndex
;
235 RequestIndex requestsIndex
; ///< maps request IDs to requests
237 /// Search in queue for the request with requestId, return the related
238 /// Xaction object and remove it from queue.
239 /// If concurrency is disabled then the requestId is ignored and the
240 /// Xaction of the next request in queue is retrieved.
241 Helper::Xaction
*popRequest(int requestId
);
243 /// Run over the active requests lists and forces a retry, or timedout reply
244 /// or the configured "on timeout response" for timedout requests.
245 void checkForTimedOutRequests(bool const retry
);
247 /// Read timeout handler
248 static void requestTimeout(const CommTimeoutCbParams
&io
);
251 class helper_stateful_server
: public HelperServerBase
253 CBDATA_CLASS(helper_stateful_server
);
257 /* MemBuf writebuf; */
259 statefulhelper
*parent
;
261 void *data
; /* State data used by the calling routines */
265 void helperOpenServers(helper
* hlp
);
266 void helperStatefulOpenServers(statefulhelper
* hlp
);
267 void helperSubmit(helper
* hlp
, const char *buf
, HLPCB
* callback
, void *data
);
268 void helperStatefulSubmit(statefulhelper
* hlp
, const char *buf
, HLPCB
* callback
, void *data
, helper_stateful_server
* lastserver
);
269 void helperShutdown(helper
* hlp
);
270 void helperStatefulShutdown(statefulhelper
* hlp
);
271 void helperStatefulReleaseServer(helper_stateful_server
* srv
);
272 void *helperStatefulServerGetData(helper_stateful_server
* srv
);
274 #endif /* SQUID_HELPER_H */