summaryrefslogtreecommitdiffstats
path: root/src/test/crimson/test_alien_echo.cc
blob: 1dbe81335641b77a864ccfcea0c9dca665008794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*-

#include "auth/Auth.h"
#include "messages/MPing.h"
#include "crimson/net/Connection.h"
#include "crimson/net/Dispatcher.h"
#include "crimson/net/Messenger.h"
#include "crimson/net/Config.h"
#include "crimson/thread/Condition.h"
#include "crimson/thread/Throttle.h"

#include <seastar/core/alien.hh>
#include <seastar/core/app-template.hh>
#include <seastar/core/future-util.hh>
#include <seastar/core/reactor.hh>


enum class echo_role {
  as_server,
  as_client,
};

namespace seastar_pingpong {
struct DummyAuthAuthorizer : public AuthAuthorizer {
  DummyAuthAuthorizer()
    : AuthAuthorizer(CEPH_AUTH_CEPHX)
  {}
  bool verify_reply(bufferlist::const_iterator&,
                    std::string *connection_secret) override {
    return true;
  }
  bool add_challenge(CephContext*, const bufferlist&) override {
    return true;
  }
};

struct Server {
  ceph::thread::Throttle byte_throttler;
  ceph::net::Messenger& msgr;
  struct ServerDispatcher : ceph::net::Dispatcher {
    unsigned count = 0;
    seastar::condition_variable on_reply;
    seastar::future<> ms_dispatch(ceph::net::ConnectionRef c,
                                  MessageRef m) override {
      std::cout << "server got ping " << *m << std::endl;
      // reply with a pong
      return c->send(MessageRef{new MPing(), false}).then([this] {
        ++count;
        on_reply.signal();
      });
    }
    seastar::future<ceph::net::msgr_tag_t, bufferlist>
    ms_verify_authorizer(peer_type_t peer_type,
                         auth_proto_t protocol,
                         bufferlist& auth) override {
      return seastar::make_ready_future<ceph::net::msgr_tag_t, bufferlist>(
          0, bufferlist{});
    }
    seastar::future<std::unique_ptr<AuthAuthorizer>>
    ms_get_authorizer(peer_type_t) override {
      return seastar::make_ready_future<std::unique_ptr<AuthAuthorizer>>(
          new DummyAuthAuthorizer{});
    }
  } dispatcher;
  Server(ceph::net::Messenger& msgr)
    : byte_throttler(ceph::net::conf.osd_client_message_size_cap),
      msgr{msgr}
  {
    msgr.set_crc_header();
    msgr.set_crc_data();
  }
};

struct Client {
  ceph::thread::Throttle byte_throttler;
  ceph::net::Messenger& msgr;
  struct ClientDispatcher : ceph::net::Dispatcher {
    unsigned count = 0;
    seastar::condition_variable on_reply;
    seastar::future<> ms_dispatch(ceph::net::ConnectionRef c,
                                  MessageRef m) override {
      std::cout << "client got pong " << *m << std::endl;
      ++count;
      on_reply.signal();
      return seastar::now();
    }
  } dispatcher;
  Client(ceph::net::Messenger& msgr)
    : byte_throttler(ceph::net::conf.osd_client_message_size_cap),
      msgr{msgr}
  {
    msgr.set_crc_header();
    msgr.set_crc_data();
  }
};
} // namespace seastar_pingpong

class SeastarContext {
  seastar::file_desc begin_fd;
  ceph::thread::Condition on_end;

public:
  SeastarContext()
    : begin_fd{seastar::file_desc::eventfd(0, 0)}
  {}

  template<class Func>
  std::thread with_seastar(Func&& func) {
    return std::thread{[this, func = std::forward<Func>(func)] {
        // alien: are you ready?
        wait_for_seastar();
        // alien: could you help me apply(func)?
        func();
        // alien: i've sent my request. have you replied it?
        // wait_for_seastar();
        // alien: you are free to go!
        on_end.notify();
      }};
  }

  void run(seastar::app_template& app, int argc, char** argv) {
    app.run(argc, argv, [this] {
      return seastar::now().then([this] {
        return set_seastar_ready();
      }).then([this] {
        // seastar: let me know once i am free to leave.
        return on_end.wait();
      }).handle_exception([](auto ep) {
        std::cerr << "Error: " << ep << std::endl;
      }).finally([] {
        seastar::engine().exit(0);
      });
    });
  }

  seastar::future<> set_seastar_ready() {
    // seastar: i am ready to serve!
    ::eventfd_write(begin_fd.get(), 1);
    return seastar::now();
  }

private:
  void wait_for_seastar() {
    eventfd_t result = 0;
    if (int r = ::eventfd_read(begin_fd.get(), &result); r < 0) {
      std::cerr << "unable to eventfd_read():" << errno << std::endl;
    }
  }
};

static seastar::future<>
seastar_echo(const entity_addr_t addr, echo_role role, unsigned count)
{
  std::cout << "seastar/";
  if (role == echo_role::as_server) {
    return ceph::net::Messenger::create(entity_name_t::OSD(0), "server",
                                        addr.get_nonce(), 0)
      .then([addr, count] (auto msgr) {
        return seastar::do_with(seastar_pingpong::Server{*msgr},
          [addr, count](auto& server) mutable {
            std::cout << "server listening at " << addr << std::endl;
            // bind the server
            server.msgr.set_policy_throttler(entity_name_t::TYPE_OSD,
                                             &server.byte_throttler);
            return server.msgr.bind(entity_addrvec_t{addr})
              .then([&server] {
                return server.msgr.start(&server.dispatcher);
              }).then([&dispatcher=server.dispatcher, count] {
                return dispatcher.on_reply.wait([&dispatcher, count] {
                  return dispatcher.count >= count;
                });
              }).finally([&server] {
                std::cout << "server shutting down" << std::endl;
                return server.msgr.shutdown();
              });
          });
      });
  } else {
    return ceph::net::Messenger::create(entity_name_t::OSD(1), "client",
                                        addr.get_nonce(), 0)
      .then([addr, count] (auto msgr) {
        return seastar::do_with(seastar_pingpong::Client{*msgr},
          [addr, count](auto& client) {
            std::cout << "client sending to " << addr << std::endl;
            client.msgr.set_policy_throttler(entity_name_t::TYPE_OSD,
                                             &client.byte_throttler);
            return client.msgr.start(&client.dispatcher)
              .then([addr, &client] {
                return client.msgr.connect(addr, entity_name_t::TYPE_OSD);
              }).then([&disp=client.dispatcher, count](ceph::net::ConnectionXRef conn) {
                return seastar::do_until(
                  [&disp,count] { return disp.count >= count; },
                  [&disp,conn] { return (*conn)->send(MessageRef{new MPing(), false})
                                   .then([&] { return disp.on_reply.wait(); });
                });
              }).finally([&client] {
                std::cout << "client shutting down" << std::endl;
                return client.msgr.shutdown();
              });
          });
      });
  }
}

int main(int argc, char** argv)
{
  namespace po = boost::program_options;
  po::options_description desc{"Allowed options"};
  desc.add_options()
    ("help,h", "show help message")
    ("role", po::value<std::string>()->default_value("pong"),
     "role to play (ping | pong)")
    ("port", po::value<uint16_t>()->default_value(9010),
     "port #")
    ("nonce", po::value<uint32_t>()->default_value(42),
     "a unique number to identify the pong server")
    ("count", po::value<unsigned>()->default_value(10),
     "stop after sending/echoing <count> MPing messages")
    ("v2", po::value<bool>()->default_value(false),
     "using msgr v2 protocol");
  po::variables_map vm;
  std::vector<std::string> unrecognized_options;
  try {
    auto parsed = po::command_line_parser(argc, argv)
      .options(desc)
      .allow_unregistered()
      .run();
    po::store(parsed, vm);
    if (vm.count("help")) {
      std::cout << desc << std::endl;
      return 0;
    }
    po::notify(vm);
    unrecognized_options = po::collect_unrecognized(parsed.options, po::include_positional);
  } catch(const po::error& e) {
    std::cerr << "error: " << e.what() << std::endl;
    return 1;
  }

  entity_addr_t addr;
  if (vm["v2"].as<bool>()) {
    addr.set_type(entity_addr_t::TYPE_MSGR2);
  } else {
    addr.set_type(entity_addr_t::TYPE_LEGACY);
  }
  addr.set_family(AF_INET);
  addr.set_port(vm["port"].as<std::uint16_t>());
  addr.set_nonce(vm["nonce"].as<std::uint32_t>());

  echo_role role = echo_role::as_server;
  if (vm["role"].as<std::string>() == "ping") {
    role = echo_role::as_client;
  }

  auto count = vm["count"].as<unsigned>();
  seastar::app_template app;
  SeastarContext sc;
  auto job = sc.with_seastar([&] {
    auto fut = seastar::alien::submit_to(0, [addr, role, count] {
      return seastar_echo(addr, role, count);
    });
    fut.wait();
  });
  std::vector<char*> av{argv[0]};
  std::transform(begin(unrecognized_options),
                 end(unrecognized_options),
                 std::back_inserter(av),
                 [](auto& s) {
                   return const_cast<char*>(s.c_str());
                 });
  sc.run(app, av.size(), av.data());
  job.join();
}

/*
 * Local Variables:
 * compile-command: "make -j4 \
 * -C ../../../build \
 * unittest_seastar_echo"
 * End:
 */