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>
23 /// libecap::adapter::services indexed by their URI
24 typedef std::map
<std::string
, Adaptation::Ecap::ServiceRep::AdapterService
> AdapterServices
;
25 /// all loaded services
26 static AdapterServices TheServices
;
27 /// configured services producing async transactions
28 static AdapterServices AsyncServices
;
35 /// wraps Adaptation::Ecap::ServiceConfig to allow eCAP visitors
36 class ConfigRep
: public libecap::Options
39 typedef Adaptation::Ecap::ServiceConfig Master
;
40 typedef libecap::Name Name
;
41 typedef libecap::Area Area
;
43 ConfigRep(const Master
&aMaster
);
45 // libecap::Options API
46 virtual const libecap::Area
option(const libecap::Name
&name
) const;
47 virtual void visitEachOption(libecap::NamedValueVisitor
&visitor
) const;
49 const Master
&master
; ///< the configuration being wrapped
52 /// manages async eCAP transactions
53 class Engine
: public AsyncEngine
57 virtual int checkEvents(int timeout
);
60 void kickAsyncServices(timeval
&timeout
);
64 } // namespace Adaptation
66 Adaptation::Ecap::ConfigRep::ConfigRep(const Master
&aMaster
): master(aMaster
)
71 Adaptation::Ecap::ConfigRep::option(const libecap::Name
&name
) const
73 // we may supply the params we know about, but only when names have host ID
74 if (name
== metaBypassable
)
75 return Area(master
.bypass
? "1" : "0", 1);
77 // TODO: We could build a by-name index, but is it worth it? Good adapters
78 // should use visitEachOption() instead, to check for name typos/errors.
79 typedef Master::Extensions::const_iterator MECI
;
80 for (MECI i
= master
.extensions
.begin(); i
!= master
.extensions
.end(); ++i
) {
82 return Area(i
->second
.data(), i
->second
.size());
89 Adaptation::Ecap::ConfigRep::visitEachOption(libecap::NamedValueVisitor
&visitor
) const
91 // we may supply the params we know about too, but only if we set host ID
92 visitor
.visit(metaBypassable
, Area(master
.bypass
? "1" : "0", 1));
94 // visit adapter-specific options (i.e., those not recognized by Squid)
95 typedef Master::Extensions::const_iterator MECI
;
96 for (MECI i
= master
.extensions
.begin(); i
!= master
.extensions
.end(); ++i
)
97 visitor
.visit(Name(i
->first
), Area::FromTempString(i
->second
));
100 /* Adaptation::Ecap::Engine */
103 Adaptation::Ecap::Engine::checkEvents(int)
105 // Start with the default I/O loop timeout, convert from milliseconds.
106 static const struct timeval maxTimeout
{
107 EVENT_LOOP_TIMEOUT
/1000, // seconds
108 (EVENT_LOOP_TIMEOUT
% 1000)*1000
110 struct timeval timeout
= maxTimeout
;
112 kickAsyncServices(timeout
);
113 if (timeout
.tv_sec
== maxTimeout
.tv_sec
&& timeout
.tv_usec
== maxTimeout
.tv_usec
)
116 debugs(93, 7, "timeout: " << timeout
.tv_sec
<< "s+" << timeout
.tv_usec
<< "us");
118 // convert back to milliseconds, avoiding int overflows
119 if (timeout
.tv_sec
>= std::numeric_limits
<int>::max()/1000 - 1000)
120 return std::numeric_limits
<int>::max();
122 return timeout
.tv_sec
*1000 + timeout
.tv_usec
/1000;
125 /// resumes async transactions (if any) and returns true if they set a timeout
127 Adaptation::Ecap::Engine::kickAsyncServices(timeval
&timeout
)
129 if (AsyncServices
.empty())
132 debugs(93, 3, "async services: " << AsyncServices
.size());
134 // Activate waiting async transactions, if any.
135 typedef AdapterServices::iterator ASI
;
136 for (ASI s
= AsyncServices
.begin(); s
!= AsyncServices
.end(); ++s
) {
138 s
->second
->resume(); // may call Ecap::Xaction::resume()
141 // Give services a chance to decrease the default timeout.
142 for (ASI s
= AsyncServices
.begin(); s
!= AsyncServices
.end(); ++s
) {
143 s
->second
->suspend(timeout
);
147 /* Adaptation::Ecap::ServiceRep */
149 Adaptation::Ecap::ServiceRep::ServiceRep(const ServiceConfigPointer
&cfg
):
150 /*AsyncJob("Adaptation::Ecap::ServiceRep"),*/ Adaptation::Service(cfg
),
155 Adaptation::Ecap::ServiceRep::~ServiceRep()
159 void Adaptation::Ecap::ServiceRep::noteFailure()
161 assert(false); // XXX: should this be ICAP-specific?
165 Adaptation::Ecap::ServiceRep::finalize()
167 Adaptation::Service::finalize();
168 theService
= FindAdapterService(cfg().uri
);
171 tryConfigureAndStart();
173 } catch (const std::exception
&e
) { // standardized exceptions
174 if (!handleFinalizeFailure(e
.what()))
175 throw; // rethrow for upper layers to handle
176 } catch (...) { // all other exceptions
177 if (!handleFinalizeFailure("unrecognized exception"))
178 throw; // rethrow for upper layers to handle
180 return; // success or handled exception
182 debugs(93,DBG_IMPORTANT
, "WARNING: configured ecap_service was not loaded: " << cfg().uri
);
186 /// attempts to configure and start eCAP service; the caller handles exceptions
188 Adaptation::Ecap::ServiceRep::tryConfigureAndStart()
190 debugs(93,2, HERE
<< "configuring eCAP service: " << theService
->uri());
191 const ConfigRep
cfgRep(dynamic_cast<const ServiceConfig
&>(cfg()));
192 theService
->configure(cfgRep
);
194 debugs(93,DBG_IMPORTANT
, "Starting eCAP service: " << theService
->uri());
197 if (theService
->makesAsyncXactions()) {
198 AsyncServices
[theService
->uri()] = theService
;
199 debugs(93, 5, "asyncs: " << AsyncServices
.size());
203 /// handles failures while configuring or starting an eCAP service;
204 /// returns false if the error must be propagated to higher levels
206 Adaptation::Ecap::ServiceRep::handleFinalizeFailure(const char *error
)
208 const bool salvage
= cfg().bypass
;
209 const int level
= salvage
? DBG_IMPORTANT
:DBG_CRITICAL
;
210 const char *kind
= salvage
? "optional" : "essential";
211 debugs(93, level
, "ERROR: failed to start " << kind
<< " eCAP service: " <<
212 cfg().uri
<< ":\n" << error
);
215 return false; // we cannot handle the problem; the caller may escalate
217 // make up() false, preventing new adaptation requests and enabling bypass
219 debugs(93, level
, "WARNING: " << kind
<< " eCAP service is " <<
220 "down after initialization failure: " << cfg().uri
);
222 return true; // tell the caller to ignore the problem because we handled it
225 bool Adaptation::Ecap::ServiceRep::probed() const
227 return true; // we "probe" the adapter in finalize().
230 bool Adaptation::Ecap::ServiceRep::up() const
232 return theService
!= NULL
;
235 bool Adaptation::Ecap::ServiceRep::wantsUrl(const String
&urlPath
) const
238 return theService
->wantsUrl(urlPath
.termedBuf());
241 Adaptation::Initiate
*
242 Adaptation::Ecap::ServiceRep::makeXactLauncher(HttpMsg
*virgin
,
243 HttpRequest
*cause
, AccessLogEntry::Pointer
&alp
)
247 // register now because (a) we need EventLoop::Running and (b) we do not
248 // want to add more main loop overheads unless an async service is used.
249 static AsyncEngine
*TheEngine
= NULL
;
250 if (AsyncServices
.size() && !TheEngine
&& EventLoop::Running
) {
251 TheEngine
= new Engine
;
252 EventLoop::Running
->registerEngine(TheEngine
);
253 debugs(93, 3, "asyncs: " << AsyncServices
.size() << ' ' << TheEngine
);
256 XactionRep
*rep
= new XactionRep(virgin
, cause
, alp
, Pointer(this));
257 XactionRep::AdapterXaction
x(theService
->makeXaction(rep
));
262 // returns a temporary string depicting service status, for debugging
263 const char *Adaptation::Ecap::ServiceRep::status() const
265 // TODO: move generic stuff from eCAP and ICAP to Adaptation
274 buf
.append("down", 4);
277 buf
.append(",detached", 9);
282 return buf
.content();
285 void Adaptation::Ecap::ServiceRep::detach()
290 bool Adaptation::Ecap::ServiceRep::detached() const
295 Adaptation::Ecap::ServiceRep::AdapterService
296 Adaptation::Ecap::FindAdapterService(const String
& serviceUri
)
298 AdapterServices::const_iterator pos
= TheServices
.find(serviceUri
.termedBuf());
299 if (pos
!= TheServices
.end()) {
303 return ServiceRep::AdapterService();
307 Adaptation::Ecap::RegisterAdapterService(const Adaptation::Ecap::ServiceRep::AdapterService
& adapterService
)
309 TheServices
[adapterService
->uri()] = adapterService
; // may update old one
310 debugs(93, 3, "stored eCAP module service: " << adapterService
->uri());
311 // We do not update AsyncServices here in case they are not configured.
315 Adaptation::Ecap::UnregisterAdapterService(const String
& serviceUri
)
317 if (TheServices
.erase(serviceUri
.termedBuf())) {
318 debugs(93, 3, "unregistered eCAP module service: " << serviceUri
);
319 AsyncServices
.erase(serviceUri
.termedBuf()); // no-op for non-async
322 debugs(93, 3, "failed to unregister eCAP module service: " << serviceUri
);
326 Adaptation::Ecap::CheckUnusedAdapterServices(const Adaptation::Services
& cfgs
)
328 typedef AdapterServices::const_iterator ASCI
;
329 for (ASCI loaded
= TheServices
.begin(); loaded
!= TheServices
.end();
332 for (Services::const_iterator cfged
= cfgs
.begin();
333 cfged
!= cfgs
.end() && !found
; ++cfged
) {
334 found
= (*cfged
)->cfg().uri
== loaded
->second
->uri().c_str();
337 debugs(93, DBG_IMPORTANT
, "Warning: loaded eCAP service has no matching " <<
338 "ecap_service config option: " << loaded
->second
->uri());