]> git.ipfire.org Git - thirdparty/squid.git/blame - src/adaptation/Iterator.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / adaptation / Iterator.cc
CommitLineData
a22e6cd3 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
bbc27441
AJ
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.
a22e6cd3
AR
7 */
8
bbc27441
AJ
9/* DEBUG: section 93 Adaptation */
10
582c2af2 11#include "squid.h"
1adcebc3 12#include "adaptation/Answer.h"
a22e6cd3
AR
13#include "adaptation/Config.h"
14#include "adaptation/Iterator.h"
15#include "adaptation/Service.h"
16#include "adaptation/ServiceFilter.h"
17#include "adaptation/ServiceGroups.h"
3d93a84d 18#include "base/TextException.h"
602d9612 19#include "HttpReply.h"
ac38abee 20#include "sbuf/StringConvert.h"
a22e6cd3 21
4299f876 22Adaptation::Iterator::Iterator(
63df1d28 23 Http::Message *aMsg, HttpRequest *aCause,
af0ded40 24 AccessLogEntry::Pointer &alp,
4cb2536f 25 const ServiceGroupPointer &aGroup):
f53969cc
SM
26 AsyncJob("Iterator"),
27 Adaptation::Initiate("Iterator"),
28 theGroup(aGroup),
29 theMsg(aMsg),
30 theCause(aCause),
31 al(alp),
32 theLauncher(0),
33 iterations(0),
34 adapted(false)
a22e6cd3 35{
b248c2a3
AJ
36 if (theCause != NULL)
37 HTTPMSGLOCK(theCause);
38
39 if (theMsg != NULL)
40 HTTPMSGLOCK(theMsg);
a22e6cd3
AR
41}
42
43Adaptation::Iterator::~Iterator()
44{
45 assert(!theLauncher);
46 HTTPMSGUNLOCK(theMsg);
47 HTTPMSGUNLOCK(theCause);
48}
49
50void Adaptation::Iterator::start()
51{
52 Adaptation::Initiate::start();
53
54 thePlan = ServicePlan(theGroup, filter());
c302ddb5
CT
55
56 // Add adaptation group name once and now, before
57 // dynamic groups change it at step() time.
58 if (Adaptation::Config::needHistory && !thePlan.exhausted() && (dynamic_cast<ServiceSet *>(theGroup.getRaw()) || dynamic_cast<ServiceChain *>(theGroup.getRaw()))) {
59 HttpRequest *request = dynamic_cast<HttpRequest*>(theMsg);
60 if (!request)
61 request = theCause;
62 Must(request);
63 Adaptation::History::Pointer ah = request->adaptHistory(true);
a32d75e7 64 auto gid = StringToSBuf(theGroup->id);
c302ddb5
CT
65 ah->recordAdaptationService(gid);
66 }
67
a22e6cd3
AR
68 step();
69}
70
71void Adaptation::Iterator::step()
72{
73 ++iterations;
74 debugs(93,5, HERE << '#' << iterations << " plan: " << thePlan);
75
76 Must(!theLauncher);
77
78 if (thePlan.exhausted()) { // nothing more to do
3af10ac0 79 sendAnswer(Answer::Forward(theMsg));
a22e6cd3
AR
80 Must(done());
81 return;
82 }
83
129fe2a1
CT
84 HttpRequest *request = dynamic_cast<HttpRequest*>(theMsg);
85 if (!request)
86 request = theCause;
87 assert(request);
88 request->clearError();
89
a22e6cd3
AR
90 if (iterations > Adaptation::Config::service_iteration_limit) {
91 debugs(93,DBG_CRITICAL, "Adaptation iterations limit (" <<
e1381638
AJ
92 Adaptation::Config::service_iteration_limit << ") exceeded:\n" <<
93 "\tPossible service loop with " <<
94 theGroup->kind << " " << theGroup->id << ", plan=" << thePlan);
a22e6cd3
AR
95 throw TexcHere("too many adaptations");
96 }
97
98 ServicePointer service = thePlan.current();
99 Must(service != NULL);
100 debugs(93,5, HERE << "using adaptation service: " << service->cfg().key);
101
c302ddb5
CT
102 if (Adaptation::Config::needHistory) {
103 Adaptation::History::Pointer ah = request->adaptHistory(true);
a32d75e7 104 auto uid = StringToSBuf(thePlan.current()->cfg().key);
c302ddb5
CT
105 ah->recordAdaptationService(uid);
106 }
107
a22e6cd3 108 theLauncher = initiateAdaptation(
af0ded40 109 service->makeXactLauncher(theMsg, theCause, al));
4299f876 110 Must(initiated(theLauncher));
a22e6cd3
AR
111 Must(!done());
112}
113
3af10ac0
AR
114void
115Adaptation::Iterator::noteAdaptationAnswer(const Answer &answer)
116{
117 switch (answer.kind) {
118 case Answer::akForward:
63df1d28 119 handleAdaptedHeader(const_cast<Http::Message*>(answer.message.getRaw()));
3af10ac0
AR
120 break;
121
122 case Answer::akBlock:
123 handleAdaptationBlock(answer);
124 break;
125
126 case Answer::akError:
127 handleAdaptationError(answer.final);
128 break;
129 }
130}
131
132void
63df1d28 133Adaptation::Iterator::handleAdaptedHeader(Http::Message *aMsg)
a22e6cd3
AR
134{
135 // set theCause if we switched to request satisfaction mode
136 if (!theCause) { // probably sent a request message
137 if (dynamic_cast<HttpReply*>(aMsg)) { // we got a response message
138 if (HttpRequest *cause = dynamic_cast<HttpRequest*>(theMsg)) {
139 // definately sent request, now use it as the cause
140 theCause = cause; // moving the lock
141 theMsg = 0;
142 debugs(93,3, HERE << "in request satisfaction mode");
143 }
144 }
145 }
146
147 Must(aMsg);
148 HTTPMSGUNLOCK(theMsg);
b248c2a3
AJ
149 theMsg = aMsg;
150 HTTPMSGLOCK(theMsg);
a22e6cd3
AR
151 adapted = true;
152
153 clearAdaptation(theLauncher);
154 if (!updatePlan(true)) // do not immediatelly advance the new plan
155 thePlan.next(filter());
156 step();
157}
158
159void Adaptation::Iterator::noteInitiatorAborted()
160{
161 announceInitiatorAbort(theLauncher); // propogate to the transaction
162 clearInitiator();
163 mustStop("initiator gone");
164}
165
3af10ac0
AR
166void Adaptation::Iterator::handleAdaptationBlock(const Answer &answer)
167{
168 debugs(93,5, HERE << "blocked by " << answer);
169 clearAdaptation(theLauncher);
170 updatePlan(false);
171 sendAnswer(answer);
172 mustStop("blocked");
173}
174
175void Adaptation::Iterator::handleAdaptationError(bool final)
a22e6cd3
AR
176{
177 debugs(93,5, HERE << "final: " << final << " plan: " << thePlan);
178 clearAdaptation(theLauncher);
179 updatePlan(false);
180
181 // can we replace the failed service (group-level bypass)?
e1381638
AJ
182 const bool srcIntact = !theMsg->body_pipe ||
183 !theMsg->body_pipe->consumedSize();
a22e6cd3
AR
184 // can we ignore the failure (compute while thePlan is not exhausted)?
185 Must(!thePlan.exhausted());
186 const bool canIgnore = thePlan.current()->cfg().bypass;
187 debugs(85,5, HERE << "flags: " << srcIntact << canIgnore << adapted);
188
189 if (srcIntact) {
190 if (thePlan.replacement(filter()) != NULL) {
191 debugs(93,3, HERE << "trying a replacement service");
192 step();
193 return;
194 }
195 }
196
197 if (canIgnore && srcIntact && adapted) {
198 debugs(85,3, HERE << "responding with older adapted msg");
3af10ac0 199 sendAnswer(Answer::Forward(theMsg));
a22e6cd3
AR
200 mustStop("sent older adapted msg");
201 return;
202 }
203
204 // caller may recover if we can ignore the error and virgin msg is intact
205 const bool useVirgin = canIgnore && !adapted && srcIntact;
206 tellQueryAborted(!useVirgin);
207 mustStop("group failure");
208}
209
210bool Adaptation::Iterator::doneAll() const
211{
212 return Adaptation::Initiate::doneAll() && thePlan.exhausted();
213}
214
215void Adaptation::Iterator::swanSong()
216{
4299f876 217 if (theInitiator.set())
a22e6cd3
AR
218 tellQueryAborted(true); // abnormal condition that should not happen
219
4299f876 220 if (initiated(theLauncher))
a22e6cd3
AR
221 clearAdaptation(theLauncher);
222
223 Adaptation::Initiate::swanSong();
224}
225
226bool Adaptation::Iterator::updatePlan(bool adopt)
227{
228 HttpRequest *r = theCause ? theCause : dynamic_cast<HttpRequest*>(theMsg);
229 Must(r);
230
231 Adaptation::History::Pointer ah = r->adaptHistory();
aaf0559d
AR
232 if (!ah) {
233 debugs(85,9, HERE << "no history to store a service-proposed plan");
a22e6cd3 234 return false; // the feature is not enabled or is not triggered
aaf0559d 235 }
a22e6cd3
AR
236
237 String services;
238 if (!ah->extractNextServices(services)) { // clears history
239 debugs(85,9, HERE << "no service-proposed plan received");
240 return false; // the service did not provide a new plan
241 }
242
243 if (!adopt) {
244 debugs(85,3, HERE << "rejecting service-proposed plan");
245 return false;
246 }
e1381638 247
a22e6cd3 248 debugs(85,3, HERE << "retiring old plan: " << thePlan);
53340485 249
eb898410 250 Adaptation::ServiceFilter f = this->filter();
53340485 251 DynamicGroupCfg current, future;
eb898410 252 DynamicServiceChain::Split(f, services, current, future);
53340485
AR
253
254 if (!future.empty()) {
255 ah->setFutureServices(future);
256 debugs(85,3, HERE << "noted future service-proposed plan: " << future);
257 }
258
259 // use the current config even if it is empty; we must replace the old plan
eb898410
AJ
260 theGroup = new DynamicServiceChain(current, f); // refcounted
261 thePlan = ServicePlan(theGroup, f);
a22e6cd3
AR
262 debugs(85,3, HERE << "adopted service-proposed plan: " << thePlan);
263 return true;
264}
265
266Adaptation::ServiceFilter Adaptation::Iterator::filter() const
267{
268 // the method may differ from theGroup->method due to request satisfaction
269 Method method = methodNone;
270 // temporary variables, no locking needed
271 HttpRequest *req = NULL;
272 HttpReply *rep = NULL;
273
274 if (HttpRequest *r = dynamic_cast<HttpRequest*>(theMsg)) {
275 method = methodReqmod;
276 req = r;
277 rep = NULL;
b0365bd9 278 } else if (HttpReply *theReply = dynamic_cast<HttpReply*>(theMsg)) {
a22e6cd3
AR
279 method = methodRespmod;
280 req = theCause;
b0365bd9 281 rep = theReply;
a22e6cd3
AR
282 } else {
283 Must(false); // should not happen
284 }
285
af0ded40 286 return ServiceFilter(method, theGroup->point, req, rep, al);
a22e6cd3
AR
287}
288
289CBDATA_NAMESPACED_CLASS_INIT(Adaptation, Iterator);
f53969cc 290