]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/Coordinator.cc
4 * DEBUG: section 54 Interprocess Communication
10 #include "base/TextException.h"
11 #include "CacheManager.h"
13 #include "ipc/Coordinator.h"
14 #include "ipc/FdNotes.h"
15 #include "ipc/SharedListen.h"
16 #include "mgr/Inquirer.h"
17 #include "mgr/Request.h"
18 #include "mgr/Response.h"
19 #include "mgr/StoreToCommWriter.h"
22 CBDATA_NAMESPACED_CLASS_INIT(Ipc
, Coordinator
);
23 Ipc::Coordinator
* Ipc::Coordinator::TheInstance
= NULL
;
26 Ipc::Coordinator::Coordinator():
31 void Ipc::Coordinator::start()
36 Ipc::StrandCoord
* Ipc::Coordinator::findStrand(int kidId
)
38 typedef StrandCoords::iterator SI
;
39 for (SI iter
= strands_
.begin(); iter
!= strands_
.end(); ++iter
) {
40 if (iter
->kidId
== kidId
)
46 void Ipc::Coordinator::registerStrand(const StrandCoord
& strand
)
48 debugs(54, 3, HERE
<< "registering kid" << strand
.kidId
<<
50 if (StrandCoord
* found
= findStrand(strand
.kidId
)) {
51 const String oldTag
= found
->tag
;
53 if (oldTag
.size() && !strand
.tag
.size())
54 found
->tag
= oldTag
; // keep more detailed info (XXX?)
56 strands_
.push_back(strand
);
59 // notify searchers waiting for this new strand, if any
60 typedef Searchers::iterator SRI
;
61 for (SRI i
= searchers
.begin(); i
!= searchers
.end();) {
62 if (i
->tag
== strand
.tag
) {
63 notifySearcher(*i
, strand
);
64 i
= searchers
.erase(i
);
71 void Ipc::Coordinator::receive(const TypedMsgHdr
& message
)
73 switch (message
.type()) {
75 debugs(54, 6, HERE
<< "Registration request");
76 handleRegistrationRequest(HereIamMessage(message
));
79 case mtStrandSearchRequest
: {
80 const StrandSearchRequest
sr(message
);
81 debugs(54, 6, HERE
<< "Strand search request: " << sr
.requestorId
<<
83 handleSearchRequest(sr
);
87 case mtSharedListenRequest
:
88 debugs(54, 6, HERE
<< "Shared listen request");
89 handleSharedListenRequest(SharedListenRequest(message
));
92 case mtCacheMgrRequest
:
93 debugs(54, 6, HERE
<< "Cache manager request");
94 handleCacheMgrRequest(Mgr::Request(message
));
97 case mtCacheMgrResponse
:
98 debugs(54, 6, HERE
<< "Cache manager response");
99 handleCacheMgrResponse(Mgr::Response(message
));
103 debugs(54, 1, HERE
<< "Unhandled message type: " << message
.type());
108 void Ipc::Coordinator::handleRegistrationRequest(const HereIamMessage
& msg
)
110 registerStrand(msg
.strand
);
112 // send back an acknowledgement; TODO: remove as not needed?
115 SendMessage(MakeAddr(strandAddrPfx
, msg
.strand
.kidId
), message
);
119 Ipc::Coordinator::handleSharedListenRequest(const SharedListenRequest
& request
)
121 debugs(54, 4, HERE
<< "kid" << request
.requestorId
<<
122 " needs shared listen FD for " << request
.params
.addr
);
123 Listeners::const_iterator i
= listeners
.find(request
.params
);
125 const int sock
= (i
!= listeners
.end()) ?
126 i
->second
: openListenSocket(request
, errNo
);
128 debugs(54, 3, HERE
<< "sending shared listen FD " << sock
<< " for " <<
129 request
.params
.addr
<< " to kid" << request
.requestorId
<<
130 " mapId=" << request
.mapId
);
132 SharedListenResponse
response(sock
, errNo
, request
.mapId
);
134 response
.pack(message
);
135 SendMessage(MakeAddr(strandAddrPfx
, request
.requestorId
), message
);
139 Ipc::Coordinator::handleCacheMgrRequest(const Mgr::Request
& request
)
143 // Let the strand know that we are now responsible for handling the request
144 Mgr::Response
response(request
.requestId
);
146 response
.pack(message
);
147 SendMessage(MakeAddr(strandAddrPfx
, request
.requestorId
), message
);
149 Mgr::Action::Pointer action
=
150 CacheManager::GetInstance()->createRequestedAction(request
.params
);
151 AsyncJob::Start(new Mgr::Inquirer(action
,
152 Mgr::ImportHttpFdIntoComm(request
.fd
), request
, strands_
));
156 Ipc::Coordinator::handleCacheMgrResponse(const Mgr::Response
& response
)
158 Mgr::Inquirer::HandleRemoteAck(response
);
162 Ipc::Coordinator::handleSearchRequest(const Ipc::StrandSearchRequest
&request
)
164 // do we know of a strand with the given search tag?
165 const StrandCoord
*strand
= NULL
;
166 typedef StrandCoords::const_iterator SCCI
;
167 for (SCCI i
= strands_
.begin(); !strand
&& i
!= strands_
.end(); ++i
) {
168 if (i
->tag
== request
.tag
)
173 notifySearcher(request
, *strand
);
177 searchers
.push_back(request
);
178 debugs(54, 3, HERE
<< "cannot yet tell kid" << request
.requestorId
<<
179 " who " << request
.tag
<< " is");
183 Ipc::Coordinator::notifySearcher(const Ipc::StrandSearchRequest
&request
,
184 const StrandCoord
& strand
)
186 debugs(54, 3, HERE
<< "tell kid" << request
.requestorId
<< " that " <<
187 request
.tag
<< " is kid" << strand
.kidId
);
188 const StrandSearchResponse
response(strand
);
190 response
.pack(message
);
191 SendMessage(MakeAddr(strandAddrPfx
, request
.requestorId
), message
);
196 Ipc::Coordinator::openListenSocket(const SharedListenRequest
& request
,
199 const OpenListenerParams
&p
= request
.params
;
201 debugs(54, 6, HERE
<< "opening listen FD at " << p
.addr
<< " for kid" <<
202 request
.requestorId
);
204 Ip::Address addr
= p
.addr
; // comm_open_listener may modify it
207 const int sock
= comm_open_listener(p
.sock_type
, p
.proto
, addr
, p
.flags
,
209 errNo
= (sock
>= 0) ? 0 : errno
;
212 // cache positive results
214 listeners
[request
.params
] = sock
;
219 void Ipc::Coordinator::broadcastSignal(int sig
) const
221 typedef StrandCoords::const_iterator SCI
;
222 for (SCI iter
= strands_
.begin(); iter
!= strands_
.end(); ++iter
) {
223 debugs(54, 5, HERE
<< "signal " << sig
<< " to kid" << iter
->kidId
<<
224 ", PID=" << iter
->pid
);
225 kill(iter
->pid
, sig
);
229 Ipc::Coordinator
* Ipc::Coordinator::Instance()
232 TheInstance
= new Coordinator
;
233 // XXX: if the Coordinator job quits, this pointer will become invalid
234 // we could make Coordinator death fatal, except during exit, but since
235 // Strands do not re-register, even process death would be pointless.
239 const Ipc::StrandCoords
&
240 Ipc::Coordinator::strands() const