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 eCAP Interface */
12 #include "adaptation/ecap/Config.h"
13 #include "adaptation/ecap/Host.h"
14 #include "adaptation/ecap/ServiceRep.h"
15 #include "adaptation/ecap/XactionRep.h"
16 #include "AsyncEngine.h"
17 #include "base/TextException.h"
19 #include "EventLoop.h"
21 #include <libecap/adapter/service.h>
22 #include <libecap/common/options.h>
23 #include <libecap/common/name.h>
24 #include <libecap/common/named_values.h>
28 /// libecap::adapter::services indexed by their URI
29 typedef std::map
<std::string
, Adaptation::Ecap::ServiceRep::AdapterService
> AdapterServices
;
30 /// all loaded services
31 static AdapterServices TheServices
;
32 /// configured services producing async transactions
33 static AdapterServices AsyncServices
;
40 /// wraps Adaptation::Ecap::ServiceConfig to allow eCAP visitors
41 class ConfigRep
: public libecap::Options
44 typedef Adaptation::Ecap::ServiceConfig Master
;
45 typedef libecap::Name Name
;
46 typedef libecap::Area Area
;
48 ConfigRep(const Master
&aMaster
);
50 // libecap::Options API
51 virtual const libecap::Area
option(const libecap::Name
&name
) const;
52 virtual void visitEachOption(libecap::NamedValueVisitor
&visitor
) const;
54 const Master
&master
; ///< the configuration being wrapped
57 /// manages async eCAP transactions
58 class Engine
: public AsyncEngine
62 virtual int checkEvents(int timeout
);
65 void kickAsyncServices(timeval
&timeout
);
69 } // namespace Adaptation
71 Adaptation::Ecap::ConfigRep::ConfigRep(const Master
&aMaster
): master(aMaster
)
76 Adaptation::Ecap::ConfigRep::option(const libecap::Name
&name
) const
78 // we may supply the params we know about, but only when names have host ID
79 if (name
== metaBypassable
)
80 return Area(master
.bypass
? "1" : "0", 1);
82 // TODO: We could build a by-name index, but is it worth it? Good adapters
83 // should use visitEachOption() instead, to check for name typos/errors.
84 typedef Master::Extensions::const_iterator MECI
;
85 for (MECI i
= master
.extensions
.begin(); i
!= master
.extensions
.end(); ++i
) {
87 return Area(i
->second
.data(), i
->second
.size());
94 Adaptation::Ecap::ConfigRep::visitEachOption(libecap::NamedValueVisitor
&visitor
) const
96 // we may supply the params we know about too, but only if we set host ID
97 visitor
.visit(metaBypassable
, Area(master
.bypass
? "1" : "0", 1));
99 // visit adapter-specific options (i.e., those not recognized by Squid)
100 typedef Master::Extensions::const_iterator MECI
;
101 for (MECI i
= master
.extensions
.begin(); i
!= master
.extensions
.end(); ++i
)
102 visitor
.visit(Name(i
->first
), Area::FromTempString(i
->second
));
105 /* Adaptation::Ecap::Engine */
108 Adaptation::Ecap::Engine::checkEvents(int)
110 // Start with the default I/O loop timeout, convert from milliseconds.
111 static const struct timeval maxTimeout
= {
112 EVENT_LOOP_TIMEOUT
/1000, // seconds
113 (EVENT_LOOP_TIMEOUT
% 1000)*1000
115 struct timeval timeout
= maxTimeout
;
117 kickAsyncServices(timeout
);
118 if (timeout
.tv_sec
== maxTimeout
.tv_sec
&& timeout
.tv_usec
== maxTimeout
.tv_usec
)
121 debugs(93, 7, "timeout: " << timeout
.tv_sec
<< "s+" << timeout
.tv_usec
<< "us");
123 // convert back to milliseconds, avoiding int overflows
124 if (timeout
.tv_sec
>= std::numeric_limits
<int>::max()/1000 - 1000)
125 return std::numeric_limits
<int>::max();
127 return timeout
.tv_sec
*1000 + timeout
.tv_usec
/1000;
130 /// resumes async transactions (if any) and returns true if they set a timeout
132 Adaptation::Ecap::Engine::kickAsyncServices(timeval
&timeout
)
134 if (AsyncServices
.empty())
137 debugs(93, 3, "async services: " << AsyncServices
.size());
139 // Activate waiting async transactions, if any.
140 typedef AdapterServices::iterator ASI
;
141 for (ASI s
= AsyncServices
.begin(); s
!= AsyncServices
.end(); ++s
) {
143 s
->second
->resume(); // may call Ecap::Xaction::resume()
146 // Give services a chance to decrease the default timeout.
147 for (ASI s
= AsyncServices
.begin(); s
!= AsyncServices
.end(); ++s
) {
148 s
->second
->suspend(timeout
);
152 /* Adaptation::Ecap::ServiceRep */
154 Adaptation::Ecap::ServiceRep::ServiceRep(const ServiceConfigPointer
&cfg
):
155 /*AsyncJob("Adaptation::Ecap::ServiceRep"),*/ Adaptation::Service(cfg
),
160 Adaptation::Ecap::ServiceRep::~ServiceRep()
164 void Adaptation::Ecap::ServiceRep::noteFailure()
166 assert(false); // XXX: should this be ICAP-specific?
170 Adaptation::Ecap::ServiceRep::finalize()
172 Adaptation::Service::finalize();
173 theService
= FindAdapterService(cfg().uri
);
176 tryConfigureAndStart();
178 } catch (const std::exception
&e
) { // standardized exceptions
179 if (!handleFinalizeFailure(e
.what()))
180 throw; // rethrow for upper layers to handle
181 } catch (...) { // all other exceptions
182 if (!handleFinalizeFailure("unrecognized exception"))
183 throw; // rethrow for upper layers to handle
185 return; // success or handled exception
187 debugs(93,DBG_IMPORTANT
, "WARNING: configured ecap_service was not loaded: " << cfg().uri
);
191 /// attempts to configure and start eCAP service; the caller handles exceptions
193 Adaptation::Ecap::ServiceRep::tryConfigureAndStart()
195 debugs(93,2, HERE
<< "configuring eCAP service: " << theService
->uri());
196 const ConfigRep
cfgRep(dynamic_cast<const ServiceConfig
&>(cfg()));
197 theService
->configure(cfgRep
);
199 debugs(93,DBG_IMPORTANT
, "Starting eCAP service: " << theService
->uri());
202 if (theService
->makesAsyncXactions()) {
203 AsyncServices
[theService
->uri()] = theService
;
204 debugs(93, 5, "asyncs: " << AsyncServices
.size());
208 /// handles failures while configuring or starting an eCAP service;
209 /// returns false if the error must be propagated to higher levels
211 Adaptation::Ecap::ServiceRep::handleFinalizeFailure(const char *error
)
213 const bool salvage
= cfg().bypass
;
214 const int level
= salvage
? DBG_IMPORTANT
:DBG_CRITICAL
;
215 const char *kind
= salvage
? "optional" : "essential";
216 debugs(93, level
, "ERROR: failed to start " << kind
<< " eCAP service: " <<
217 cfg().uri
<< ":\n" << error
);
220 return false; // we cannot handle the problem; the caller may escalate
222 // make up() false, preventing new adaptation requests and enabling bypass
224 debugs(93, level
, "WARNING: " << kind
<< " eCAP service is " <<
225 "down after initialization failure: " << cfg().uri
);
227 return true; // tell the caller to ignore the problem because we handled it
230 bool Adaptation::Ecap::ServiceRep::probed() const
232 return true; // we "probe" the adapter in finalize().
235 bool Adaptation::Ecap::ServiceRep::up() const
237 return theService
!= NULL
;
240 bool Adaptation::Ecap::ServiceRep::wantsUrl(const String
&urlPath
) const
243 return theService
->wantsUrl(urlPath
.termedBuf());
246 Adaptation::Initiate
*
247 Adaptation::Ecap::ServiceRep::makeXactLauncher(HttpMsg
*virgin
,
248 HttpRequest
*cause
, AccessLogEntry::Pointer
&alp
)
252 // register now because (a) we need EventLoop::Running and (b) we do not
253 // want to add more main loop overheads unless an async service is used.
254 static AsyncEngine
*TheEngine
= NULL
;
255 if (AsyncServices
.size() && !TheEngine
&& EventLoop::Running
) {
256 TheEngine
= new Engine
;
257 EventLoop::Running
->registerEngine(TheEngine
);
258 debugs(93, 3, "asyncs: " << AsyncServices
.size() << ' ' << TheEngine
);
261 XactionRep
*rep
= new XactionRep(virgin
, cause
, alp
, Pointer(this));
262 XactionRep::AdapterXaction
x(theService
->makeXaction(rep
));
267 // returns a temporary string depicting service status, for debugging
268 const char *Adaptation::Ecap::ServiceRep::status() const
270 // TODO: move generic stuff from eCAP and ICAP to Adaptation
279 buf
.append("down", 4);
282 buf
.append(",detached", 9);
287 return buf
.content();
290 void Adaptation::Ecap::ServiceRep::detach()
295 bool Adaptation::Ecap::ServiceRep::detached() const
300 Adaptation::Ecap::ServiceRep::AdapterService
301 Adaptation::Ecap::FindAdapterService(const String
& serviceUri
)
303 AdapterServices::const_iterator pos
= TheServices
.find(serviceUri
.termedBuf());
304 if (pos
!= TheServices
.end()) {
308 return ServiceRep::AdapterService();
312 Adaptation::Ecap::RegisterAdapterService(const Adaptation::Ecap::ServiceRep::AdapterService
& adapterService
)
314 TheServices
[adapterService
->uri()] = adapterService
; // may update old one
315 debugs(93, 3, "stored eCAP module service: " << adapterService
->uri());
316 // We do not update AsyncServices here in case they are not configured.
320 Adaptation::Ecap::UnregisterAdapterService(const String
& serviceUri
)
322 if (TheServices
.erase(serviceUri
.termedBuf())) {
323 debugs(93, 3, "unregistered eCAP module service: " << serviceUri
);
324 AsyncServices
.erase(serviceUri
.termedBuf()); // no-op for non-async
327 debugs(93, 3, "failed to unregister eCAP module service: " << serviceUri
);
331 Adaptation::Ecap::CheckUnusedAdapterServices(const Adaptation::Services
& cfgs
)
333 typedef AdapterServices::const_iterator ASCI
;
334 for (ASCI loaded
= TheServices
.begin(); loaded
!= TheServices
.end();
337 for (Services::const_iterator cfged
= cfgs
.begin();
338 cfged
!= cfgs
.end() && !found
; ++cfged
) {
339 found
= (*cfged
)->cfg().uri
== loaded
->second
->uri().c_str();
342 debugs(93, DBG_IMPORTANT
, "Warning: loaded eCAP service has no matching " <<
343 "ecap_service config option: " << loaded
->second
->uri());