]>
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) 2015 Haomai Wang | |
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 | #include <stdlib.h> | |
18 | #include <stdint.h> | |
19 | #include <string> | |
20 | #include <unistd.h> | |
21 | #include <iostream> | |
22 | ||
23 | using namespace std; | |
24 | ||
7c673cae FG |
25 | #include "common/ceph_argparse.h" |
26 | #include "common/debug.h" | |
27 | #include "global/global_init.h" | |
28 | #include "msg/Messenger.h" | |
29 | #include "messages/MOSDOp.h" | |
30 | #include "messages/MOSDOpReply.h" | |
31 | ||
32 | class ServerDispatcher : public Dispatcher { | |
33 | uint64_t think_time; | |
34 | ThreadPool op_tp; | |
35 | class OpWQ : public ThreadPool::WorkQueue<Message> { | |
36 | list<Message*> messages; | |
37 | ||
38 | public: | |
39 | OpWQ(time_t timeout, time_t suicide_timeout, ThreadPool *tp) | |
40 | : ThreadPool::WorkQueue<Message>("ServerDispatcher::OpWQ", timeout, suicide_timeout, tp) {} | |
41 | ||
42 | bool _enqueue(Message *m) override { | |
43 | messages.push_back(m); | |
44 | return true; | |
45 | } | |
46 | void _dequeue(Message *m) override { | |
47 | ceph_abort(); | |
48 | } | |
49 | bool _empty() override { | |
50 | return messages.empty(); | |
51 | } | |
52 | Message *_dequeue() override { | |
53 | if (messages.empty()) | |
54 | return NULL; | |
55 | Message *m = messages.front(); | |
56 | messages.pop_front(); | |
57 | return m; | |
58 | } | |
59 | void _process(Message *m, ThreadPool::TPHandle &handle) override { | |
60 | MOSDOp *osd_op = static_cast<MOSDOp*>(m); | |
61 | MOSDOpReply *reply = new MOSDOpReply(osd_op, 0, 0, 0, false); | |
62 | m->get_connection()->send_message(reply); | |
63 | m->put(); | |
64 | } | |
65 | void _process_finish(Message *m) override { } | |
66 | void _clear() override { | |
67 | assert(messages.empty()); | |
68 | } | |
69 | } op_wq; | |
70 | ||
71 | public: | |
72 | ServerDispatcher(int threads, uint64_t delay): Dispatcher(g_ceph_context), think_time(delay), | |
73 | op_tp(g_ceph_context, "ServerDispatcher::op_tp", "tp_serv_disp", threads, "serverdispatcher_op_threads"), | |
74 | op_wq(30, 30, &op_tp) { | |
75 | op_tp.start(); | |
76 | } | |
77 | ~ServerDispatcher() override { | |
78 | op_tp.stop(); | |
79 | } | |
80 | bool ms_can_fast_dispatch_any() const override { return true; } | |
81 | bool ms_can_fast_dispatch(const Message *m) const override { | |
82 | switch (m->get_type()) { | |
83 | case CEPH_MSG_OSD_OP: | |
84 | return true; | |
85 | default: | |
86 | return false; | |
87 | } | |
88 | } | |
89 | ||
90 | void ms_handle_fast_connect(Connection *con) override {} | |
91 | void ms_handle_fast_accept(Connection *con) override {} | |
92 | bool ms_dispatch(Message *m) override { return true; } | |
93 | bool ms_handle_reset(Connection *con) override { return true; } | |
94 | void ms_handle_remote_reset(Connection *con) override {} | |
95 | bool ms_handle_refused(Connection *con) override { return false; } | |
96 | void ms_fast_dispatch(Message *m) override { | |
97 | usleep(think_time); | |
98 | //cerr << __func__ << " reply message=" << m << std::endl; | |
99 | op_wq.queue(m); | |
100 | } | |
101 | bool ms_verify_authorizer(Connection *con, int peer_type, int protocol, | |
102 | bufferlist& authorizer, bufferlist& authorizer_reply, | |
103 | bool& isvalid, CryptoKey& session_key) override { | |
104 | isvalid = true; | |
105 | return true; | |
106 | } | |
107 | }; | |
108 | ||
109 | class MessengerServer { | |
110 | Messenger *msgr; | |
111 | string type; | |
112 | string bindaddr; | |
113 | ServerDispatcher dispatcher; | |
114 | ||
115 | public: | |
116 | MessengerServer(string t, string addr, int threads, int delay): | |
117 | msgr(NULL), type(t), bindaddr(addr), dispatcher(threads, delay) { | |
118 | msgr = Messenger::create(g_ceph_context, type, entity_name_t::OSD(0), "server", 0, 0); | |
119 | msgr->set_default_policy(Messenger::Policy::stateless_server(0)); | |
120 | } | |
121 | ~MessengerServer() { | |
122 | msgr->shutdown(); | |
123 | msgr->wait(); | |
124 | } | |
125 | void start() { | |
126 | entity_addr_t addr; | |
127 | addr.parse(bindaddr.c_str()); | |
128 | msgr->bind(addr); | |
129 | msgr->add_dispatcher_head(&dispatcher); | |
130 | msgr->start(); | |
131 | msgr->wait(); | |
132 | } | |
133 | }; | |
134 | ||
135 | void usage(const string &name) { | |
136 | cerr << "Usage: " << name << " [bind ip:port] [server worker threads] [thinktime us]" << std::endl; | |
137 | cerr << " [bind ip:port]: The ip:port pair to bind, client need to specify this pair to connect" << std::endl; | |
138 | cerr << " [server worker threads]: threads will process incoming messages and reply(matching pg threads)" << std::endl; | |
139 | cerr << " [thinktime]: sleep time when do dispatching(match fast dispatch logic in OSD.cc)" << std::endl; | |
140 | } | |
141 | ||
142 | int main(int argc, char **argv) | |
143 | { | |
144 | vector<const char*> args; | |
145 | argv_to_vec(argc, (const char **)argv, args); | |
146 | ||
147 | auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, | |
148 | CODE_ENVIRONMENT_UTILITY, 0); | |
149 | common_init_finish(g_ceph_context); | |
150 | g_ceph_context->_conf->apply_changes(NULL); | |
151 | ||
152 | if (args.size() < 3) { | |
153 | usage(argv[0]); | |
154 | return 1; | |
155 | } | |
156 | ||
157 | int worker_threads = atoi(args[1]); | |
158 | int think_time = atoi(args[2]); | |
159 | std::string public_msgr_type = g_ceph_context->_conf->ms_public_type.empty() ? g_ceph_context->_conf->get_val<std::string>("ms_type") : g_ceph_context->_conf->ms_public_type; | |
160 | ||
161 | cerr << " This tool won't handle connection error alike things, " << std::endl; | |
162 | cerr << "please ensure the proper network environment to test." << std::endl; | |
163 | cerr << " Or ctrl+c when meeting error and restart tests" << std::endl; | |
164 | cerr << " using ms-public-type " << public_msgr_type << std::endl; | |
165 | cerr << " bind ip:port " << args[0] << std::endl; | |
166 | cerr << " worker threads " << worker_threads << std::endl; | |
167 | cerr << " thinktime(us) " << think_time << std::endl; | |
168 | ||
169 | MessengerServer server(public_msgr_type, args[0], worker_threads, think_time); | |
170 | server.start(); | |
171 | ||
172 | return 0; | |
173 | } |