1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "DirectMessenger.h"
16 #include "msg/DispatchStrategy.h"
19 class DirectConnection : public Connection {
20 /// sent messages are dispatched here
21 DispatchStrategy *const dispatchers;
23 /// the connection that will be attached to outgoing messages, so that replies
24 /// can be dispatched back to the sender. the pointer is atomic for
25 /// thread-safety between mark_down() and send_message(). no reference is held
26 /// on this Connection to avoid cyclical refs. we don't need a reference
27 /// because its owning DirectMessenger will mark both connections down (and
28 /// clear this pointer) before dropping its own reference
29 std::atomic<Connection*> reply_connection{nullptr};
32 DirectConnection(CephContext *cct, DirectMessenger *m,
33 DispatchStrategy *dispatchers)
35 dispatchers(dispatchers)
38 /// sets the Connection that will receive replies to outgoing messages
39 void set_direct_reply_connection(ConnectionRef conn);
41 /// return true if a peer connection exists
42 bool is_connected() override;
44 /// pass the given message directly to our dispatchers
45 int send_message(Message *m) override;
47 /// release our pointer to the peer connection. later calls to is_connected()
48 /// will return false, and send_message() will fail with -ENOTCONN
49 void mark_down() override;
51 /// noop - keepalive messages are not needed within a process
52 void send_keepalive() override {}
54 /// noop - reconnect/recovery semantics are not needed within a process
55 void mark_disposable() override {}
58 void DirectConnection::set_direct_reply_connection(ConnectionRef conn)
60 reply_connection.store(conn.get());
63 bool DirectConnection::is_connected()
65 // true between calls to set_direct_reply_connection() and mark_down()
66 return reply_connection.load() != nullptr;
69 int DirectConnection::send_message(Message *m)
71 // read reply_connection atomically and take a reference
72 ConnectionRef conn = reply_connection.load();
77 // attach reply_connection to the Message, so that calls to
78 // m->get_connection()->send_message() can be dispatched back to the sender
79 m->set_connection(conn);
81 dispatchers->ds_dispatch(m);
85 void DirectConnection::mark_down()
87 Connection *conn = reply_connection.load();
89 return; // already marked down
91 if (!reply_connection.compare_exchange_weak(conn, nullptr)) {
92 return; // lost the race to mark down
94 // called only once to avoid loops
99 static ConnectionRef create_loopback(DirectMessenger *m,
101 DispatchStrategy *dispatchers)
103 auto loopback = boost::intrusive_ptr<DirectConnection>(
104 new DirectConnection(m->cct, m, dispatchers));
105 // loopback replies go to itself
106 loopback->set_direct_reply_connection(loopback);
107 loopback->set_peer_type(name.type());
108 loopback->set_features(CEPH_FEATURES_ALL);
112 DirectMessenger::DirectMessenger(CephContext *cct, entity_name_t name,
113 string mname, uint64_t nonce,
114 DispatchStrategy *dispatchers)
115 : SimplePolicyMessenger(cct, name, mname, nonce),
116 dispatchers(dispatchers),
117 loopback_connection(create_loopback(this, name, dispatchers))
119 dispatchers->set_messenger(this);
122 DirectMessenger::~DirectMessenger()
126 int DirectMessenger::set_direct_peer(DirectMessenger *peer)
128 if (get_myinst() == peer->get_myinst()) {
129 return -EADDRINUSE; // must have a different entity instance
131 peer_inst = peer->get_myinst();
133 // allocate a Connection that dispatches to the peer messenger
134 auto direct_connection = boost::intrusive_ptr<DirectConnection>(
135 new DirectConnection(cct, peer, peer->dispatchers.get()));
137 direct_connection->set_peer_addr(peer_inst.addr);
138 direct_connection->set_peer_type(peer_inst.name.type());
139 direct_connection->set_features(CEPH_FEATURES_ALL);
141 // if set_direct_peer() was already called on the peer messenger, we can
142 // finish by attaching their connections. if not, the later call to
143 // peer->set_direct_peer() will attach their connection to ours
144 auto connection = peer->get_connection(get_myinst());
146 auto p = static_cast<DirectConnection*>(connection.get());
148 p->set_direct_reply_connection(direct_connection);
149 direct_connection->set_direct_reply_connection(p);
152 peer_connection = std::move(direct_connection);
156 int DirectMessenger::bind(const entity_addr_t &bind_addr)
158 if (peer_connection) {
159 return -EINVAL; // can't change address after sharing it with the peer
161 set_myaddr(bind_addr);
162 loopback_connection->set_peer_addr(bind_addr);
166 int DirectMessenger::client_bind(const entity_addr_t &bind_addr)
169 return bind(bind_addr);
172 int DirectMessenger::start()
174 if (!peer_connection) {
175 return -EINVAL; // did not connect to a peer
178 return -EINVAL; // already started
181 dispatchers->start();
182 return SimplePolicyMessenger::start();
185 int DirectMessenger::shutdown()
188 return -EINVAL; // not started
192 peer_connection.reset();
193 loopback_connection.reset();
195 dispatchers->shutdown();
196 SimplePolicyMessenger::shutdown();
197 sem.Put(); // signal wait()
201 void DirectMessenger::wait()
203 sem.Get(); // wait on signal from shutdown()
207 ConnectionRef DirectMessenger::get_connection(const entity_inst_t& dst)
209 if (dst == peer_inst) {
210 return peer_connection;
212 if (dst == get_myinst()) {
213 return loopback_connection;
218 ConnectionRef DirectMessenger::get_loopback_connection()
220 return loopback_connection;
223 int DirectMessenger::send_message(Message *m, const entity_inst_t& dst)
225 auto conn = get_connection(dst);
230 return conn->send_message(m);
233 void DirectMessenger::mark_down(const entity_addr_t& addr)
236 if (addr == peer_inst.addr) {
237 conn = peer_connection;
238 } else if (addr == get_myaddr()) {
239 conn = loopback_connection;
246 void DirectMessenger::mark_down_all()
248 if (peer_connection) {
249 peer_connection->mark_down();
251 loopback_connection->mark_down();