2 * DEBUG: section 93 eCAP Interface
5 #include "adaptation/ecap/Config.h"
6 #include "adaptation/ecap/Host.h"
7 #include "adaptation/ecap/ServiceRep.h"
8 #include "adaptation/ecap/XactionRep.h"
9 #include "AsyncEngine.h"
10 #include "base/TextException.h"
12 #include "EventLoop.h"
14 #include <libecap/adapter/service.h>
15 #include <libecap/common/options.h>
16 #include <libecap/common/name.h>
17 #include <libecap/common/named_values.h>
21 /// libecap::adapter::services indexed by their URI
22 typedef std::map
<std::string
, Adaptation::Ecap::ServiceRep::AdapterService
> AdapterServices
;
23 /// all loaded services
24 static AdapterServices TheServices
;
25 /// configured services producing async transactions
26 static AdapterServices AsyncServices
;
33 /// wraps Adaptation::Ecap::ServiceConfig to allow eCAP visitors
34 class ConfigRep
: public libecap::Options
37 typedef Adaptation::Ecap::ServiceConfig Master
;
38 typedef libecap::Name Name
;
39 typedef libecap::Area Area
;
41 ConfigRep(const Master
&aMaster
);
43 // libecap::Options API
44 virtual const libecap::Area
option(const libecap::Name
&name
) const;
45 virtual void visitEachOption(libecap::NamedValueVisitor
&visitor
) const;
47 const Master
&master
; ///< the configuration being wrapped
50 /// manages async eCAP transactions
51 class Engine
: public AsyncEngine
55 virtual int checkEvents(int timeout
);
58 void kickAsyncServices(timeval
&timeout
);
62 } // namespace Adaptation
64 Adaptation::Ecap::ConfigRep::ConfigRep(const Master
&aMaster
): master(aMaster
)
69 Adaptation::Ecap::ConfigRep::option(const libecap::Name
&name
) const
71 // we may supply the params we know about, but only when names have host ID
72 if (name
== metaBypassable
)
73 return Area(master
.bypass
? "1" : "0", 1);
75 // TODO: We could build a by-name index, but is it worth it? Good adapters
76 // should use visitEachOption() instead, to check for name typos/errors.
77 typedef Master::Extensions::const_iterator MECI
;
78 for (MECI i
= master
.extensions
.begin(); i
!= master
.extensions
.end(); ++i
) {
80 return Area(i
->second
.data(), i
->second
.size());
87 Adaptation::Ecap::ConfigRep::visitEachOption(libecap::NamedValueVisitor
&visitor
) const
89 // we may supply the params we know about too, but only if we set host ID
90 visitor
.visit(metaBypassable
, Area(master
.bypass
? "1" : "0", 1));
92 // visit adapter-specific options (i.e., those not recognized by Squid)
93 typedef Master::Extensions::const_iterator MECI
;
94 for (MECI i
= master
.extensions
.begin(); i
!= master
.extensions
.end(); ++i
)
95 visitor
.visit(Name(i
->first
), Area::FromTempString(i
->second
));
98 /* Adaptation::Ecap::Engine */
101 Adaptation::Ecap::Engine::checkEvents(int)
103 // Start with the default I/O loop timeout, convert from milliseconds.
104 static const struct timeval maxTimeout
= {
105 EVENT_LOOP_TIMEOUT
/1000, // seconds
106 (EVENT_LOOP_TIMEOUT
% 1000)*1000
108 struct timeval timeout
= maxTimeout
;
110 kickAsyncServices(timeout
);
111 if (timeout
.tv_sec
== maxTimeout
.tv_sec
&& timeout
.tv_usec
== maxTimeout
.tv_usec
)
114 debugs(93, 7, "timeout: " << timeout
.tv_sec
<< "s+" << timeout
.tv_usec
<< "us");
116 // convert back to milliseconds, avoiding int overflows
117 if (timeout
.tv_sec
>= std::numeric_limits
<int>::max()/1000 - 1000)
118 return std::numeric_limits
<int>::max();
120 return timeout
.tv_sec
*1000 + timeout
.tv_usec
/1000;
123 /// resumes async transactions (if any) and returns true if they set a timeout
125 Adaptation::Ecap::Engine::kickAsyncServices(timeval
&timeout
)
127 if (AsyncServices
.empty())
130 debugs(93, 3, "async services: " << AsyncServices
.size());
132 // Activate waiting async transactions, if any.
133 typedef AdapterServices::iterator ASI
;
134 for (ASI s
= AsyncServices
.begin(); s
!= AsyncServices
.end(); ++s
) {
136 s
->second
->resume(); // may call Ecap::Xaction::resume()
139 // Give services a chance to decrease the default timeout.
140 for (ASI s
= AsyncServices
.begin(); s
!= AsyncServices
.end(); ++s
) {
141 s
->second
->suspend(timeout
);
145 /* Adaptation::Ecap::ServiceRep */
147 Adaptation::Ecap::ServiceRep::ServiceRep(const ServiceConfigPointer
&cfg
):
148 /*AsyncJob("Adaptation::Ecap::ServiceRep"),*/ Adaptation::Service(cfg
),
153 Adaptation::Ecap::ServiceRep::~ServiceRep()
157 void Adaptation::Ecap::ServiceRep::noteFailure()
159 assert(false); // XXX: should this be ICAP-specific?
163 Adaptation::Ecap::ServiceRep::finalize()
165 Adaptation::Service::finalize();
166 theService
= FindAdapterService(cfg().uri
);
169 tryConfigureAndStart();
171 } catch (const std::exception
&e
) { // standardized exceptions
172 if (!handleFinalizeFailure(e
.what()))
173 throw; // rethrow for upper layers to handle
174 } catch (...) { // all other exceptions
175 if (!handleFinalizeFailure("unrecognized exception"))
176 throw; // rethrow for upper layers to handle
178 return; // success or handled exception
180 debugs(93,DBG_IMPORTANT
, "WARNING: configured ecap_service was not loaded: " << cfg().uri
);
184 /// attempts to configure and start eCAP service; the caller handles exceptions
186 Adaptation::Ecap::ServiceRep::tryConfigureAndStart()
188 debugs(93,2, HERE
<< "configuring eCAP service: " << theService
->uri());
189 const ConfigRep
cfgRep(dynamic_cast<const ServiceConfig
&>(cfg()));
190 theService
->configure(cfgRep
);
192 debugs(93,DBG_IMPORTANT
, "Starting eCAP service: " << theService
->uri());
195 if (theService
->makesAsyncXactions()) {
196 AsyncServices
[theService
->uri()] = theService
;
197 debugs(93, 5, "asyncs: " << AsyncServices
.size());
201 /// handles failures while configuring or starting an eCAP service;
202 /// returns false if the error must be propagated to higher levels
204 Adaptation::Ecap::ServiceRep::handleFinalizeFailure(const char *error
)
206 const bool salvage
= cfg().bypass
;
207 const int level
= salvage
? DBG_IMPORTANT
:DBG_CRITICAL
;
208 const char *kind
= salvage
? "optional" : "essential";
209 debugs(93, level
, "ERROR: failed to start " << kind
<< " eCAP service: " <<
210 cfg().uri
<< ":\n" << error
);
213 return false; // we cannot handle the problem; the caller may escalate
215 // make up() false, preventing new adaptation requests and enabling bypass
217 debugs(93, level
, "WARNING: " << kind
<< " eCAP service is " <<
218 "down after initialization failure: " << cfg().uri
);
220 return true; // tell the caller to ignore the problem because we handled it
223 bool Adaptation::Ecap::ServiceRep::probed() const
225 return true; // we "probe" the adapter in finalize().
228 bool Adaptation::Ecap::ServiceRep::up() const
230 return theService
!= NULL
;
233 bool Adaptation::Ecap::ServiceRep::wantsUrl(const String
&urlPath
) const
236 return theService
->wantsUrl(urlPath
.termedBuf());
239 Adaptation::Initiate
*
240 Adaptation::Ecap::ServiceRep::makeXactLauncher(HttpMsg
*virgin
,
241 HttpRequest
*cause
, AccessLogEntry::Pointer
&alp
)
245 // register now because (a) we need EventLoop::Running and (b) we do not
246 // want to add more main loop overheads unless an async service is used.
247 static AsyncEngine
*TheEngine
= NULL
;
248 if (AsyncServices
.size() && !TheEngine
&& EventLoop::Running
) {
249 TheEngine
= new Engine
;
250 EventLoop::Running
->registerEngine(TheEngine
);
251 debugs(93, 3, "asyncs: " << AsyncServices
.size() << ' ' << TheEngine
);
254 XactionRep
*rep
= new XactionRep(virgin
, cause
, alp
, Pointer(this));
255 XactionRep::AdapterXaction
x(theService
->makeXaction(rep
));
260 // returns a temporary string depicting service status, for debugging
261 const char *Adaptation::Ecap::ServiceRep::status() const
263 // TODO: move generic stuff from eCAP and ICAP to Adaptation
272 buf
.append("down", 4);
275 buf
.append(",detached", 9);
280 return buf
.content();
283 void Adaptation::Ecap::ServiceRep::detach()
288 bool Adaptation::Ecap::ServiceRep::detached() const
293 Adaptation::Ecap::ServiceRep::AdapterService
294 Adaptation::Ecap::FindAdapterService(const String
& serviceUri
)
296 AdapterServices::const_iterator pos
= TheServices
.find(serviceUri
.termedBuf());
297 if (pos
!= TheServices
.end()) {
301 return ServiceRep::AdapterService();
305 Adaptation::Ecap::RegisterAdapterService(const Adaptation::Ecap::ServiceRep::AdapterService
& adapterService
)
307 TheServices
[adapterService
->uri()] = adapterService
; // may update old one
308 debugs(93, 3, "stored eCAP module service: " << adapterService
->uri());
309 // We do not update AsyncServices here in case they are not configured.
313 Adaptation::Ecap::UnregisterAdapterService(const String
& serviceUri
)
315 if (TheServices
.erase(serviceUri
.termedBuf())) {
316 debugs(93, 3, "unregistered eCAP module service: " << serviceUri
);
317 AsyncServices
.erase(serviceUri
.termedBuf()); // no-op for non-async
320 debugs(93, 3, "failed to unregister eCAP module service: " << serviceUri
);
324 Adaptation::Ecap::CheckUnusedAdapterServices(const Adaptation::Services
& cfgs
)
326 typedef AdapterServices::const_iterator ASCI
;
327 for (ASCI loaded
= TheServices
.begin(); loaded
!= TheServices
.end();
330 for (Services::const_iterator cfged
= cfgs
.begin();
331 cfged
!= cfgs
.end() && !found
; ++cfged
) {
332 found
= (*cfged
)->cfg().uri
== loaded
->second
->uri().c_str();
335 debugs(93, DBG_IMPORTANT
, "Warning: loaded eCAP service has no matching " <<
336 "ecap_service config option: " << loaded
->second
->uri());