]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/Coordinator.cc
IpcIoFile atomic queue v2:
[thirdparty/squid.git] / src / ipc / Coordinator.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 54 Interprocess Communication
5 *
6 */
7
8
9 #include "config.h"
10 #include "base/TextException.h"
11 #include "CacheManager.h"
12 #include "comm.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"
20
21
22 CBDATA_NAMESPACED_CLASS_INIT(Ipc, Coordinator);
23 Ipc::Coordinator* Ipc::Coordinator::TheInstance = NULL;
24
25
26 Ipc::Coordinator::Coordinator():
27 Port(coordinatorAddr)
28 {
29 }
30
31 void Ipc::Coordinator::start()
32 {
33 Port::start();
34 }
35
36 Ipc::StrandCoord* Ipc::Coordinator::findStrand(int kidId)
37 {
38 typedef StrandCoords::iterator SI;
39 for (SI iter = strands_.begin(); iter != strands_.end(); ++iter) {
40 if (iter->kidId == kidId)
41 return &(*iter);
42 }
43 return NULL;
44 }
45
46 void Ipc::Coordinator::registerStrand(const StrandCoord& strand)
47 {
48 debugs(54, 3, HERE << "registering kid" << strand.kidId <<
49 ' ' << strand.tag);
50 if (StrandCoord* found = findStrand(strand.kidId)) {
51 const String oldTag = found->tag;
52 *found = strand;
53 if (oldTag.size() && !strand.tag.size())
54 found->tag = oldTag; // keep more detailed info (XXX?)
55 } else {
56 strands_.push_back(strand);
57 }
58
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);
65 } else {
66 ++i;
67 }
68 }
69 }
70
71 void Ipc::Coordinator::receive(const TypedMsgHdr& message)
72 {
73 switch (message.type()) {
74 case mtRegistration:
75 debugs(54, 6, HERE << "Registration request");
76 handleRegistrationRequest(HereIamMessage(message));
77 break;
78
79 case mtStrandSearchRequest: {
80 const StrandSearchRequest sr(message);
81 debugs(54, 6, HERE << "Strand search request: " << sr.requestorId <<
82 " tag: " << sr.tag);
83 handleSearchRequest(sr);
84 break;
85 }
86
87 case mtSharedListenRequest:
88 debugs(54, 6, HERE << "Shared listen request");
89 handleSharedListenRequest(SharedListenRequest(message));
90 break;
91
92 case mtCacheMgrRequest:
93 debugs(54, 6, HERE << "Cache manager request");
94 handleCacheMgrRequest(Mgr::Request(message));
95 break;
96
97 case mtCacheMgrResponse:
98 debugs(54, 6, HERE << "Cache manager response");
99 handleCacheMgrResponse(Mgr::Response(message));
100 break;
101
102 default:
103 debugs(54, 1, HERE << "Unhandled message type: " << message.type());
104 break;
105 }
106 }
107
108 void Ipc::Coordinator::handleRegistrationRequest(const HereIamMessage& msg)
109 {
110 registerStrand(msg.strand);
111
112 // send back an acknowledgement; TODO: remove as not needed?
113 TypedMsgHdr message;
114 msg.pack(message);
115 SendMessage(MakeAddr(strandAddrPfx, msg.strand.kidId), message);
116 }
117
118 void
119 Ipc::Coordinator::handleSharedListenRequest(const SharedListenRequest& request)
120 {
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);
124 int errNo = 0;
125 const int sock = (i != listeners.end()) ?
126 i->second : openListenSocket(request, errNo);
127
128 debugs(54, 3, HERE << "sending shared listen FD " << sock << " for " <<
129 request.params.addr << " to kid" << request.requestorId <<
130 " mapId=" << request.mapId);
131
132 SharedListenResponse response(sock, errNo, request.mapId);
133 TypedMsgHdr message;
134 response.pack(message);
135 SendMessage(MakeAddr(strandAddrPfx, request.requestorId), message);
136 }
137
138 void
139 Ipc::Coordinator::handleCacheMgrRequest(const Mgr::Request& request)
140 {
141 debugs(54, 4, HERE);
142
143 // Let the strand know that we are now responsible for handling the request
144 Mgr::Response response(request.requestId);
145 TypedMsgHdr message;
146 response.pack(message);
147 SendMessage(MakeAddr(strandAddrPfx, request.requestorId), message);
148
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_));
153 }
154
155 void
156 Ipc::Coordinator::handleCacheMgrResponse(const Mgr::Response& response)
157 {
158 Mgr::Inquirer::HandleRemoteAck(response);
159 }
160
161 void
162 Ipc::Coordinator::handleSearchRequest(const Ipc::StrandSearchRequest &request)
163 {
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)
169 strand = &(*i);
170 }
171
172 if (strand) {
173 notifySearcher(request, *strand);
174 return;
175 }
176
177 searchers.push_back(request);
178 debugs(54, 3, HERE << "cannot yet tell kid" << request.requestorId <<
179 " who " << request.tag << " is");
180 }
181
182 void
183 Ipc::Coordinator::notifySearcher(const Ipc::StrandSearchRequest &request,
184 const StrandCoord& strand)
185 {
186 debugs(54, 3, HERE << "tell kid" << request.requestorId << " that " <<
187 request.tag << " is kid" << strand.kidId);
188 const StrandSearchResponse response(strand);
189 TypedMsgHdr message;
190 response.pack(message);
191 SendMessage(MakeAddr(strandAddrPfx, request.requestorId), message);
192 }
193
194
195 int
196 Ipc::Coordinator::openListenSocket(const SharedListenRequest& request,
197 int &errNo)
198 {
199 const OpenListenerParams &p = request.params;
200
201 debugs(54, 6, HERE << "opening listen FD at " << p.addr << " for kid" <<
202 request.requestorId);
203
204 Ip::Address addr = p.addr; // comm_open_listener may modify it
205
206 enter_suid();
207 const int sock = comm_open_listener(p.sock_type, p.proto, addr, p.flags,
208 FdNote(p.fdNote));
209 errNo = (sock >= 0) ? 0 : errno;
210 leave_suid();
211
212 // cache positive results
213 if (sock >= 0)
214 listeners[request.params] = sock;
215
216 return sock;
217 }
218
219 void Ipc::Coordinator::broadcastSignal(int sig) const
220 {
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);
226 }
227 }
228
229 Ipc::Coordinator* Ipc::Coordinator::Instance()
230 {
231 if (!TheInstance)
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.
236 return TheInstance;
237 }
238
239 const Ipc::StrandCoords&
240 Ipc::Coordinator::strands() const
241 {
242 return strands_;
243 }