]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/Iterator.cc
2 * Copyright (C) 1996-2014 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 93 Adaptation */
12 #include "adaptation/Answer.h"
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"
18 #include "base/TextException.h"
20 #include "HttpReply.h"
21 #include "HttpRequest.h"
23 Adaptation::Iterator::Iterator(
24 HttpMsg
*aMsg
, HttpRequest
*aCause
,
25 AccessLogEntry::Pointer
&alp
,
26 const ServiceGroupPointer
&aGroup
):
28 Adaptation::Initiate("Iterator"),
38 HTTPMSGLOCK(theCause
);
44 Adaptation::Iterator::~Iterator()
47 HTTPMSGUNLOCK(theMsg
);
48 HTTPMSGUNLOCK(theCause
);
51 void Adaptation::Iterator::start()
53 Adaptation::Initiate::start();
55 thePlan
= ServicePlan(theGroup
, filter());
57 // Add adaptation group name once and now, before
58 // dynamic groups change it at step() time.
59 if (Adaptation::Config::needHistory
&& !thePlan
.exhausted() && (dynamic_cast<ServiceSet
*>(theGroup
.getRaw()) || dynamic_cast<ServiceChain
*>(theGroup
.getRaw()))) {
60 HttpRequest
*request
= dynamic_cast<HttpRequest
*>(theMsg
);
64 Adaptation::History::Pointer ah
= request
->adaptHistory(true);
65 SBuf
gid(theGroup
->id
);
66 ah
->recordAdaptationService(gid
);
72 void Adaptation::Iterator::step()
75 debugs(93,5, HERE
<< '#' << iterations
<< " plan: " << thePlan
);
79 if (thePlan
.exhausted()) { // nothing more to do
80 sendAnswer(Answer::Forward(theMsg
));
85 HttpRequest
*request
= dynamic_cast<HttpRequest
*>(theMsg
);
89 request
->clearError();
91 if (iterations
> Adaptation::Config::service_iteration_limit
) {
92 debugs(93,DBG_CRITICAL
, "Adaptation iterations limit (" <<
93 Adaptation::Config::service_iteration_limit
<< ") exceeded:\n" <<
94 "\tPossible service loop with " <<
95 theGroup
->kind
<< " " << theGroup
->id
<< ", plan=" << thePlan
);
96 throw TexcHere("too many adaptations");
99 ServicePointer service
= thePlan
.current();
100 Must(service
!= NULL
);
101 debugs(93,5, HERE
<< "using adaptation service: " << service
->cfg().key
);
103 if (Adaptation::Config::needHistory
) {
104 Adaptation::History::Pointer ah
= request
->adaptHistory(true);
105 SBuf
uid(thePlan
.current()->cfg().key
);
106 ah
->recordAdaptationService(uid
);
109 theLauncher
= initiateAdaptation(
110 service
->makeXactLauncher(theMsg
, theCause
, al
));
111 Must(initiated(theLauncher
));
116 Adaptation::Iterator::noteAdaptationAnswer(const Answer
&answer
)
118 switch (answer
.kind
) {
119 case Answer::akForward
:
120 handleAdaptedHeader(const_cast<HttpMsg
*>(answer
.message
.getRaw()));
123 case Answer::akBlock
:
124 handleAdaptationBlock(answer
);
127 case Answer::akError
:
128 handleAdaptationError(answer
.final
);
134 Adaptation::Iterator::handleAdaptedHeader(HttpMsg
*aMsg
)
136 // set theCause if we switched to request satisfaction mode
137 if (!theCause
) { // probably sent a request message
138 if (dynamic_cast<HttpReply
*>(aMsg
)) { // we got a response message
139 if (HttpRequest
*cause
= dynamic_cast<HttpRequest
*>(theMsg
)) {
140 // definately sent request, now use it as the cause
141 theCause
= cause
; // moving the lock
143 debugs(93,3, HERE
<< "in request satisfaction mode");
149 HTTPMSGUNLOCK(theMsg
);
154 clearAdaptation(theLauncher
);
155 if (!updatePlan(true)) // do not immediatelly advance the new plan
156 thePlan
.next(filter());
160 void Adaptation::Iterator::noteInitiatorAborted()
162 announceInitiatorAbort(theLauncher
); // propogate to the transaction
164 mustStop("initiator gone");
167 void Adaptation::Iterator::handleAdaptationBlock(const Answer
&answer
)
169 debugs(93,5, HERE
<< "blocked by " << answer
);
170 clearAdaptation(theLauncher
);
176 void Adaptation::Iterator::handleAdaptationError(bool final
)
178 debugs(93,5, HERE
<< "final: " << final
<< " plan: " << thePlan
);
179 clearAdaptation(theLauncher
);
182 // can we replace the failed service (group-level bypass)?
183 const bool srcIntact
= !theMsg
->body_pipe
||
184 !theMsg
->body_pipe
->consumedSize();
185 // can we ignore the failure (compute while thePlan is not exhausted)?
186 Must(!thePlan
.exhausted());
187 const bool canIgnore
= thePlan
.current()->cfg().bypass
;
188 debugs(85,5, HERE
<< "flags: " << srcIntact
<< canIgnore
<< adapted
);
191 if (thePlan
.replacement(filter()) != NULL
) {
192 debugs(93,3, HERE
<< "trying a replacement service");
198 if (canIgnore
&& srcIntact
&& adapted
) {
199 debugs(85,3, HERE
<< "responding with older adapted msg");
200 sendAnswer(Answer::Forward(theMsg
));
201 mustStop("sent older adapted msg");
205 // caller may recover if we can ignore the error and virgin msg is intact
206 const bool useVirgin
= canIgnore
&& !adapted
&& srcIntact
;
207 tellQueryAborted(!useVirgin
);
208 mustStop("group failure");
211 bool Adaptation::Iterator::doneAll() const
213 return Adaptation::Initiate::doneAll() && thePlan
.exhausted();
216 void Adaptation::Iterator::swanSong()
218 if (theInitiator
.set())
219 tellQueryAborted(true); // abnormal condition that should not happen
221 if (initiated(theLauncher
))
222 clearAdaptation(theLauncher
);
224 Adaptation::Initiate::swanSong();
227 bool Adaptation::Iterator::updatePlan(bool adopt
)
229 HttpRequest
*r
= theCause
? theCause
: dynamic_cast<HttpRequest
*>(theMsg
);
232 Adaptation::History::Pointer ah
= r
->adaptHistory();
234 debugs(85,9, HERE
<< "no history to store a service-proposed plan");
235 return false; // the feature is not enabled or is not triggered
239 if (!ah
->extractNextServices(services
)) { // clears history
240 debugs(85,9, HERE
<< "no service-proposed plan received");
241 return false; // the service did not provide a new plan
245 debugs(85,3, HERE
<< "rejecting service-proposed plan");
249 debugs(85,3, HERE
<< "retiring old plan: " << thePlan
);
251 Adaptation::ServiceFilter f
= this->filter();
252 DynamicGroupCfg current
, future
;
253 DynamicServiceChain::Split(f
, services
, current
, future
);
255 if (!future
.empty()) {
256 ah
->setFutureServices(future
);
257 debugs(85,3, HERE
<< "noted future service-proposed plan: " << future
);
260 // use the current config even if it is empty; we must replace the old plan
261 theGroup
= new DynamicServiceChain(current
, f
); // refcounted
262 thePlan
= ServicePlan(theGroup
, f
);
263 debugs(85,3, HERE
<< "adopted service-proposed plan: " << thePlan
);
267 Adaptation::ServiceFilter
Adaptation::Iterator::filter() const
269 // the method may differ from theGroup->method due to request satisfaction
270 Method method
= methodNone
;
271 // temporary variables, no locking needed
272 HttpRequest
*req
= NULL
;
273 HttpReply
*rep
= NULL
;
275 if (HttpRequest
*r
= dynamic_cast<HttpRequest
*>(theMsg
)) {
276 method
= methodReqmod
;
279 } else if (HttpReply
*theReply
= dynamic_cast<HttpReply
*>(theMsg
)) {
280 method
= methodRespmod
;
284 Must(false); // should not happen
287 return ServiceFilter(method
, theGroup
->point
, req
, rep
, al
);
290 CBDATA_NAMESPACED_CLASS_INIT(Adaptation
, Iterator
);