]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2014 UnitedStack <haomai@unitedstack.com> | |
7 | * | |
8 | * Author: Haomai Wang <haomaiwang@gmail.com> | |
9 | * | |
10 | * This is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU Lesser General Public | |
12 | * License version 2.1, as published by the Free Software | |
13 | * Foundation. See file COPYING. | |
14 | * | |
15 | */ | |
16 | ||
17 | #ifndef CEPH_ASYNCMESSENGER_H | |
18 | #define CEPH_ASYNCMESSENGER_H | |
19 | ||
20 | #include "include/types.h" | |
21 | #include "include/xlist.h" | |
22 | ||
23 | #include <map> | |
24 | using namespace std; | |
25 | #include "include/unordered_map.h" | |
26 | #include "include/unordered_set.h" | |
27 | ||
28 | #include "common/Mutex.h" | |
29 | #include "include/atomic.h" | |
30 | #include "common/Cond.h" | |
31 | #include "common/Thread.h" | |
32 | ||
33 | #include "msg/SimplePolicyMessenger.h" | |
34 | #include "msg/DispatchQueue.h" | |
35 | #include "include/assert.h" | |
36 | #include "AsyncConnection.h" | |
37 | #include "Event.h" | |
38 | ||
39 | ||
40 | class AsyncMessenger; | |
41 | ||
42 | /** | |
43 | * If the Messenger binds to a specific address, the Processor runs | |
44 | * and listens for incoming connections. | |
45 | */ | |
46 | class Processor { | |
47 | AsyncMessenger *msgr; | |
48 | NetHandler net; | |
49 | Worker *worker; | |
50 | ServerSocket listen_socket; | |
51 | EventCallbackRef listen_handler; | |
52 | ||
53 | class C_processor_accept; | |
54 | ||
55 | public: | |
56 | Processor(AsyncMessenger *r, Worker *w, CephContext *c); | |
57 | ~Processor() { delete listen_handler; }; | |
58 | ||
59 | void stop(); | |
60 | int bind(const entity_addr_t &bind_addr, | |
61 | const set<int>& avoid_ports, | |
62 | entity_addr_t* bound_addr); | |
63 | void start(); | |
64 | void accept(); | |
65 | }; | |
66 | ||
67 | /* | |
68 | * AsyncMessenger is represented for maintaining a set of asynchronous connections, | |
69 | * it may own a bind address and the accepted connections will be managed by | |
70 | * AsyncMessenger. | |
71 | * | |
72 | */ | |
73 | ||
74 | class AsyncMessenger : public SimplePolicyMessenger { | |
75 | // First we have the public Messenger interface implementation... | |
76 | public: | |
77 | /** | |
78 | * Initialize the AsyncMessenger! | |
79 | * | |
80 | * @param cct The CephContext to use | |
81 | * @param name The name to assign ourselves | |
82 | * _nonce A unique ID to use for this AsyncMessenger. It should not | |
83 | * be a value that will be repeated if the daemon restarts. | |
84 | */ | |
85 | AsyncMessenger(CephContext *cct, entity_name_t name, const std::string &type, | |
86 | string mname, uint64_t _nonce); | |
87 | ||
88 | /** | |
89 | * Destroy the AsyncMessenger. Pretty simple since all the work is done | |
90 | * elsewhere. | |
91 | */ | |
92 | ~AsyncMessenger() override; | |
93 | ||
94 | /** @defgroup Accessors | |
95 | * @{ | |
96 | */ | |
97 | void set_addr_unknowns(const entity_addr_t &addr) override; | |
98 | ||
99 | int get_dispatch_queue_len() override { | |
100 | return dispatch_queue.get_queue_len(); | |
101 | } | |
102 | ||
103 | double get_dispatch_queue_max_age(utime_t now) override { | |
104 | return dispatch_queue.get_max_age(now); | |
105 | } | |
106 | /** @} Accessors */ | |
107 | ||
108 | /** | |
109 | * @defgroup Configuration functions | |
110 | * @{ | |
111 | */ | |
112 | void set_cluster_protocol(int p) override { | |
113 | assert(!started && !did_bind); | |
114 | cluster_protocol = p; | |
115 | } | |
116 | ||
117 | int bind(const entity_addr_t& bind_addr) override; | |
118 | int rebind(const set<int>& avoid_ports) override; | |
119 | int client_bind(const entity_addr_t& bind_addr) override; | |
120 | ||
121 | /** @} Configuration functions */ | |
122 | ||
123 | /** | |
124 | * @defgroup Startup/Shutdown | |
125 | * @{ | |
126 | */ | |
127 | int start() override; | |
128 | void wait() override; | |
129 | int shutdown() override; | |
130 | ||
131 | /** @} // Startup/Shutdown */ | |
132 | ||
133 | /** | |
134 | * @defgroup Messaging | |
135 | * @{ | |
136 | */ | |
137 | int send_message(Message *m, const entity_inst_t& dest) override { | |
138 | Mutex::Locker l(lock); | |
139 | ||
140 | return _send_message(m, dest); | |
141 | } | |
142 | ||
143 | /** @} // Messaging */ | |
144 | ||
145 | /** | |
146 | * @defgroup Connection Management | |
147 | * @{ | |
148 | */ | |
149 | ConnectionRef get_connection(const entity_inst_t& dest) override; | |
150 | ConnectionRef get_loopback_connection() override; | |
151 | void mark_down(const entity_addr_t& addr) override; | |
152 | void mark_down_all() override { | |
153 | shutdown_connections(true); | |
154 | } | |
155 | /** @} // Connection Management */ | |
156 | ||
157 | /** | |
158 | * @defgroup Inner classes | |
159 | * @{ | |
160 | */ | |
161 | ||
162 | /** | |
163 | * @} // Inner classes | |
164 | */ | |
165 | ||
166 | protected: | |
167 | /** | |
168 | * @defgroup Messenger Interfaces | |
169 | * @{ | |
170 | */ | |
171 | /** | |
172 | * Start up the DispatchQueue thread once we have somebody to dispatch to. | |
173 | */ | |
174 | void ready() override; | |
175 | /** @} // Messenger Interfaces */ | |
176 | ||
177 | private: | |
178 | ||
179 | /** | |
180 | * @defgroup Utility functions | |
181 | * @{ | |
182 | */ | |
183 | ||
184 | /** | |
185 | * Create a connection associated with the given entity (of the given type). | |
186 | * Initiate the connection. (This function returning does not guarantee | |
187 | * connection success.) | |
188 | * | |
189 | * @param addr The address of the entity to connect to. | |
190 | * @param type The peer type of the entity at the address. | |
191 | * | |
192 | * @return a pointer to the newly-created connection. Caller does not own a | |
193 | * reference; take one if you need it. | |
194 | */ | |
195 | AsyncConnectionRef create_connect(const entity_addr_t& addr, int type); | |
196 | ||
197 | /** | |
198 | * Queue up a Message for delivery to the entity specified | |
199 | * by addr and dest_type. | |
200 | * submit_message() is responsible for creating | |
201 | * new AsyncConnection (and closing old ones) as necessary. | |
202 | * | |
203 | * @param m The Message to queue up. This function eats a reference. | |
204 | * @param con The existing Connection to use, or NULL if you don't know of one. | |
205 | * @param dest_addr The address to send the Message to. | |
206 | * @param dest_type The peer type of the address we're sending to | |
207 | * just drop silently under failure. | |
208 | */ | |
209 | void submit_message(Message *m, AsyncConnectionRef con, | |
210 | const entity_addr_t& dest_addr, int dest_type); | |
211 | ||
212 | int _send_message(Message *m, const entity_inst_t& dest); | |
213 | void _finish_bind(const entity_addr_t& bind_addr, | |
214 | const entity_addr_t& listen_addr); | |
215 | ||
216 | private: | |
217 | static const uint64_t ReapDeadConnectionThreshold = 5; | |
218 | ||
219 | NetworkStack *stack; | |
220 | std::vector<Processor*> processors; | |
221 | friend class Processor; | |
222 | DispatchQueue dispatch_queue; | |
223 | ||
224 | // the worker run messenger's cron jobs | |
225 | Worker *local_worker; | |
226 | ||
227 | std::string ms_type; | |
228 | ||
229 | /// overall lock used for AsyncMessenger data structures | |
230 | Mutex lock; | |
231 | // AsyncMessenger stuff | |
232 | /// approximately unique ID set by the Constructor for use in entity_addr_t | |
233 | uint64_t nonce; | |
234 | ||
235 | /// true, specifying we haven't learned our addr; set false when we find it. | |
236 | // maybe this should be protected by the lock? | |
237 | bool need_addr; | |
238 | ||
239 | /** | |
240 | * set to bind address if bind was called before NetworkStack was ready to | |
241 | * bind | |
242 | */ | |
243 | entity_addr_t pending_bind_addr; | |
244 | ||
245 | /** | |
246 | * false; set to true if a pending bind exists | |
247 | */ | |
248 | bool pending_bind = false; | |
249 | ||
250 | /** | |
251 | * The following aren't lock-protected since you shouldn't be able to race | |
252 | * the only writers. | |
253 | */ | |
254 | ||
255 | /** | |
256 | * false; set to true if the AsyncMessenger bound to a specific address; | |
257 | * and set false again by Accepter::stop(). | |
258 | */ | |
259 | bool did_bind; | |
260 | /// counter for the global seq our connection protocol uses | |
261 | __u32 global_seq; | |
262 | /// lock to protect the global_seq | |
263 | ceph_spinlock_t global_seq_lock; | |
264 | ||
265 | /** | |
266 | * hash map of addresses to Asyncconnection | |
267 | * | |
268 | * NOTE: a Asyncconnection* with state CLOSED may still be in the map but is considered | |
269 | * invalid and can be replaced by anyone holding the msgr lock | |
270 | */ | |
271 | ceph::unordered_map<entity_addr_t, AsyncConnectionRef> conns; | |
272 | ||
273 | /** | |
274 | * list of connection are in teh process of accepting | |
275 | * | |
276 | * These are not yet in the conns map. | |
277 | */ | |
278 | set<AsyncConnectionRef> accepting_conns; | |
279 | ||
280 | /** | |
281 | * list of connection are closed which need to be clean up | |
282 | * | |
283 | * Because AsyncMessenger and AsyncConnection follow a lock rule that | |
284 | * we can lock AsyncMesenger::lock firstly then lock AsyncConnection::lock | |
285 | * but can't reversed. This rule is aimed to avoid dead lock. | |
286 | * So if AsyncConnection want to unregister itself from AsyncMessenger, | |
287 | * we pick up this idea that just queue itself to this set and do lazy | |
288 | * deleted for AsyncConnection. "_lookup_conn" must ensure not return a | |
289 | * AsyncConnection in this set. | |
290 | */ | |
291 | Mutex deleted_lock; | |
292 | set<AsyncConnectionRef> deleted_conns; | |
293 | ||
294 | EventCallbackRef reap_handler; | |
295 | ||
296 | /// internal cluster protocol version, if any, for talking to entities of the same type. | |
297 | int cluster_protocol; | |
298 | ||
299 | Cond stop_cond; | |
300 | bool stopped; | |
301 | ||
302 | AsyncConnectionRef _lookup_conn(const entity_addr_t& k) { | |
303 | assert(lock.is_locked()); | |
304 | ceph::unordered_map<entity_addr_t, AsyncConnectionRef>::iterator p = conns.find(k); | |
305 | if (p == conns.end()) | |
306 | return NULL; | |
307 | ||
308 | // lazy delete, see "deleted_conns" | |
309 | Mutex::Locker l(deleted_lock); | |
310 | if (deleted_conns.erase(p->second)) { | |
311 | p->second->get_perf_counter()->dec(l_msgr_active_connections); | |
312 | conns.erase(p); | |
313 | return NULL; | |
314 | } | |
315 | ||
316 | return p->second; | |
317 | } | |
318 | ||
319 | void _init_local_connection() { | |
320 | assert(lock.is_locked()); | |
321 | local_connection->peer_addr = my_inst.addr; | |
322 | local_connection->peer_type = my_inst.name.type(); | |
323 | local_connection->set_features(CEPH_FEATURES_ALL); | |
324 | ms_deliver_handle_fast_connect(local_connection.get()); | |
325 | } | |
326 | ||
327 | void shutdown_connections(bool queue_reset); | |
328 | ||
329 | public: | |
330 | ||
331 | /// con used for sending messages to ourselves | |
332 | ConnectionRef local_connection; | |
333 | ||
334 | /** | |
335 | * @defgroup AsyncMessenger internals | |
336 | * @{ | |
337 | */ | |
338 | /** | |
339 | * This wraps _lookup_conn. | |
340 | */ | |
341 | AsyncConnectionRef lookup_conn(const entity_addr_t& k) { | |
342 | Mutex::Locker l(lock); | |
343 | return _lookup_conn(k); | |
344 | } | |
345 | ||
346 | int accept_conn(AsyncConnectionRef conn) { | |
347 | Mutex::Locker l(lock); | |
348 | auto it = conns.find(conn->peer_addr); | |
349 | if (it != conns.end()) { | |
350 | AsyncConnectionRef existing = it->second; | |
351 | ||
352 | // lazy delete, see "deleted_conns" | |
353 | // If conn already in, we will return 0 | |
354 | Mutex::Locker l(deleted_lock); | |
355 | if (deleted_conns.erase(existing)) { | |
356 | } else if (conn != existing) { | |
357 | return -1; | |
358 | } | |
359 | } | |
360 | conns[conn->peer_addr] = conn; | |
361 | conn->get_perf_counter()->inc(l_msgr_active_connections); | |
362 | accepting_conns.erase(conn); | |
363 | return 0; | |
364 | } | |
365 | ||
366 | void learned_addr(const entity_addr_t &peer_addr_for_me); | |
367 | void add_accept(Worker *w, ConnectedSocket cli_socket, entity_addr_t &addr); | |
368 | NetworkStack *get_stack() { | |
369 | return stack; | |
370 | } | |
371 | ||
372 | /** | |
373 | * This wraps ms_deliver_get_authorizer. We use it for AsyncConnection. | |
374 | */ | |
375 | AuthAuthorizer *get_authorizer(int peer_type, bool force_new) { | |
376 | return ms_deliver_get_authorizer(peer_type, force_new); | |
377 | } | |
378 | ||
379 | /** | |
380 | * This wraps ms_deliver_verify_authorizer; we use it for AsyncConnection. | |
381 | */ | |
382 | bool verify_authorizer(Connection *con, int peer_type, int protocol, bufferlist& auth, bufferlist& auth_reply, | |
383 | bool& isvalid, CryptoKey& session_key) { | |
384 | return ms_deliver_verify_authorizer(con, peer_type, protocol, auth, | |
385 | auth_reply, isvalid, session_key); | |
386 | } | |
387 | /** | |
388 | * Increment the global sequence for this AsyncMessenger and return it. | |
389 | * This is for the connect protocol, although it doesn't hurt if somebody | |
390 | * else calls it. | |
391 | * | |
392 | * @return a global sequence ID that nobody else has seen. | |
393 | */ | |
394 | __u32 get_global_seq(__u32 old=0) { | |
395 | ceph_spin_lock(&global_seq_lock); | |
396 | if (old > global_seq) | |
397 | global_seq = old; | |
398 | __u32 ret = ++global_seq; | |
399 | ceph_spin_unlock(&global_seq_lock); | |
400 | return ret; | |
401 | } | |
402 | /** | |
403 | * Get the protocol version we support for the given peer type: either | |
404 | * a peer protocol (if it matches our own), the protocol version for the | |
405 | * peer (if we're connecting), or our protocol version (if we're accepting). | |
406 | */ | |
407 | int get_proto_version(int peer_type, bool connect) const; | |
408 | ||
409 | /** | |
410 | * Fill in the address and peer type for the local connection, which | |
411 | * is used for delivering messages back to ourself. | |
412 | */ | |
413 | void init_local_connection() { | |
414 | Mutex::Locker l(lock); | |
415 | _init_local_connection(); | |
416 | } | |
417 | ||
418 | /** | |
419 | * Unregister connection from `conns` | |
420 | * | |
421 | * See "deleted_conns" | |
422 | */ | |
423 | void unregister_conn(AsyncConnectionRef conn) { | |
424 | Mutex::Locker l(deleted_lock); | |
425 | deleted_conns.insert(conn); | |
426 | ||
427 | if (deleted_conns.size() >= ReapDeadConnectionThreshold) { | |
428 | local_worker->center.dispatch_event_external(reap_handler); | |
429 | } | |
430 | } | |
431 | ||
432 | /** | |
433 | * Reap dead connection from `deleted_conns` | |
434 | * | |
435 | * @return the number of dead connections | |
436 | * | |
437 | * See "deleted_conns" | |
438 | */ | |
439 | int reap_dead(); | |
440 | ||
441 | /** | |
442 | * @} // AsyncMessenger Internals | |
443 | */ | |
444 | } ; | |
445 | ||
446 | #endif /* CEPH_ASYNCMESSENGER_H */ |