]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/testmsgr.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / test / testmsgr.cc
CommitLineData
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) 2004-2006 Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
15#include <sys/stat.h>
16#include <iostream>
17#include <string>
18using namespace std;
19
20#include "common/config.h"
21
22#include "mon/MonMap.h"
23#include "mon/MonClient.h"
24#include "msg/Messenger.h"
25#include "messages/MPing.h"
26
27#include "common/Timer.h"
28#include "global/global_init.h"
29#include "common/ceph_argparse.h"
30
31#include <sys/types.h>
32#include <fcntl.h>
33
34#define dout_subsys ceph_subsys_ms
35
36Messenger *messenger = 0;
37
9f95a23c
TL
38ceph::mutex test_lock = ceph::make_mutex("mylock");
39ceph::condition_variable cond;
7c673cae
FG
40
41uint64_t received = 0;
42
43class Admin : public Dispatcher {
44public:
45 Admin()
46 : Dispatcher(g_ceph_context)
47 {
48 }
49private:
50 bool ms_dispatch(Message *m) {
51
52 //cerr << "got ping from " << m->get_source() << std::endl;
53 dout(0) << "got ping from " << m->get_source() << dendl;
9f95a23c 54 test_lock.lock();
7c673cae 55 ++received;
9f95a23c
TL
56 cond.notify_all();
57 test_lock.unlock();
7c673cae
FG
58
59 m->put();
60 return true;
61 }
62
63 bool ms_handle_reset(Connection *con) { return false; }
64 void ms_handle_remote_reset(Connection *con) {}
65 bool ms_handle_refused(Connection *con) { return false; }
66
67} dispatcher;
68
69
70int main(int argc, const char **argv, const char *envp[]) {
71
20effc67 72 auto args = argv_to_vec(argc, argv);
7c673cae
FG
73
74 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
11fdf7f2
TL
75 CODE_ENVIRONMENT_UTILITY,
76 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
7c673cae
FG
77 common_init_finish(g_ceph_context);
78
79 dout(0) << "i am mon " << args[0] << dendl;
80
81 // get monmap
82 MonClient mc(g_ceph_context);
83 if (mc.build_initial_monmap() < 0)
84 return -1;
85
86 // start up network
87 int whoami = mc.monmap.get_rank(args[0]);
11fdf7f2 88 ceph_assert(whoami >= 0);
7c673cae
FG
89 ostringstream ss;
90 ss << mc.monmap.get_addr(whoami);
91 std::string sss(ss.str());
11fdf7f2
TL
92 g_ceph_context->_conf.set_val("public_addr", sss.c_str());
93 g_ceph_context->_conf.apply_changes(nullptr);
94 std::string public_msgr_type = g_conf()->ms_public_type.empty() ? g_conf().get_val<std::string>("ms_type") : g_conf()->ms_public_type;
7c673cae
FG
95 Messenger *rank = Messenger::create(g_ceph_context,
96 public_msgr_type,
97 entity_name_t::MON(whoami), "tester",
98 getpid());
99 int err = rank->bind(g_ceph_context->_conf->public_addr);
100 if (err < 0)
101 return 1;
102
103 // start monitor
104 messenger = rank;
105 messenger->set_default_send_priority(CEPH_MSG_PRIO_HIGH);
106 messenger->add_dispatcher_head(&dispatcher);
107
108 rank->start();
109
110 int isend = 0;
111 if (whoami == 0)
112 isend = 100;
113
9f95a23c 114 std::unique_lock l{test_lock};
7c673cae
FG
115 uint64_t sent = 0;
116 while (1) {
117 while (received + isend <= sent) {
118 //cerr << "wait r " << received << " s " << sent << " is " << isend << std::endl;
119 dout(0) << "wait r " << received << " s " << sent << " is " << isend << dendl;
9f95a23c 120 cond.wait(l);
7c673cae
FG
121 }
122
123 int t = rand() % mc.get_num_mon();
124 if (t == whoami)
125 continue;
126
127 if (rand() % 10 == 0) {
128 //cerr << "mark_down " << t << std::endl;
129 dout(0) << "mark_down " << t << dendl;
11fdf7f2 130 messenger->mark_down_addrs(mc.get_mon_addrs(t));
7c673cae
FG
131 }
132 //cerr << "pinging " << t << std::endl;
133 dout(0) << "pinging " << t << dendl;
11fdf7f2 134 messenger->send_to_mon(new MPing, mc.get_mon_addrs(t));
7c673cae
FG
135 cerr << isend << "\t" << ++sent << "\t" << received << "\r";
136 }
9f95a23c 137 l.unlock();
7c673cae
FG
138
139 // wait for messenger to finish
140 rank->wait();
141
142 return 0;
143}
144