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.
16 #ifdef HAVE_SYS_MOUNT_H
17 #include <sys/mount.h>
20 #ifdef HAVE_SYS_PARAM_H
21 #include <sys/param.h>
32 #include <boost/scoped_ptr.hpp>
33 #include <boost/random/mersenne_twister.hpp>
34 #include <boost/random/uniform_int.hpp>
37 #include "osd/osd_types.h"
38 #include "osdc/Objecter.h"
39 #include "mon/MonClient.h"
40 #include "msg/Dispatcher.h"
41 #include "msg/Messenger.h"
42 #include "common/Timer.h"
43 #include "common/ceph_argparse.h"
44 #include "global/global_init.h"
45 #include "global/signal_handler.h"
46 #include "common/config.h"
47 #include "common/debug.h"
48 #include "common/errno.h"
49 #include "common/ceph_mutex.h"
50 #include "common/strtol.h"
51 #include "common/LogEntry.h"
52 #include "auth/KeyRing.h"
53 #include "auth/AuthAuthorizeHandler.h"
54 #include "include/uuid.h"
55 #include "include/ceph_assert.h"
57 #include "messages/MOSDBoot.h"
58 #include "messages/MOSDAlive.h"
59 #include "messages/MOSDPGCreate.h"
60 #include "messages/MOSDPGRemove.h"
61 #include "messages/MOSDMap.h"
62 #include "messages/MPGStats.h"
63 #include "messages/MLog.h"
64 #include "messages/MOSDPGTemp.h"
68 #define dout_context g_ceph_context
69 #define dout_subsys ceph_subsys_
71 #define dout_prefix _prefix(_dout, get_name())
72 static ostream
& _prefix(std::ostream
*_dout
, const string
&n
) {
73 return *_dout
<< " stub(" << n
<< ") ";
77 typedef boost::mt11213b rngen_t
;
78 typedef boost::scoped_ptr
<Messenger
> MessengerRef
;
79 typedef boost::scoped_ptr
<Objecter
> ObjecterRef
;
81 class TestStub
: public Dispatcher
84 MessengerRef messenger
;
88 ceph::condition_variable cond
;
94 struct C_Tick
: public Context
{
96 explicit C_Tick(TestStub
*stub
) : s(stub
) {}
97 void finish(int r
) override
{
98 generic_dout(20) << "C_Tick::" << __func__
<< dendl
;
99 if (r
== -ECANCELED
) {
100 generic_dout(20) << "C_Tick::" << __func__
101 << " shutdown" << dendl
;
108 bool ms_dispatch(Message
*m
) override
= 0;
109 void ms_handle_connect(Connection
*con
) override
= 0;
110 void ms_handle_remote_reset(Connection
*con
) override
= 0;
111 virtual int _shutdown() = 0;
112 // courtesy method to be implemented by the stubs at their
114 virtual void _tick() { }
115 // different stubs may have different needs; if a stub needs
116 // to tick, then it must call this function.
117 void start_ticking(double t
=1.0) {
123 dout(20) << __func__
<< " adding tick timer" << dendl
;
124 timer
.add_event_after(tick_seconds
, new C_Tick(this));
126 // If we have a function to start ticking that the stubs can
127 // use at their own discretion, then we should also have a
128 // function to disable said ticking to be used the same way.
130 // For simplicity's sake, we don't cancel the tick right off
131 // the bat; instead, we wait for the next tick to kick in and
133 void stop_ticking() {
134 dout(20) << __func__
<< " disable tick" << dendl
;
140 std::cout
<< __func__
<< std::endl
;
141 if (do_shutdown
|| (tick_seconds
<= 0)) {
142 std::cout
<< __func__
<< " "
143 << (do_shutdown
? "shutdown" : "stop ticking")
148 timer
.add_event_after(tick_seconds
, new C_Tick(this));
151 virtual const string
get_name() = 0;
152 virtual int init() = 0;
154 virtual int shutdown() {
155 std::lock_guard l
{lock
};
159 dout(10) << __func__
<< " error shutting down: "
160 << cpp_strerror(-r
) << dendl
;
165 messenger
->shutdown();
169 virtual void print(ostream
&out
) {
170 out
<< "stub(" << get_name() << ")";
174 if (messenger
!= NULL
)
178 TestStub(CephContext
*cct
, string who
)
181 lock(ceph::make_mutex(who
.append("::lock"))),
184 tick_seconds(0.0) { }
187 class ClientStub
: public TestStub
189 ObjecterRef objecter
;
193 bool ms_dispatch(Message
*m
) override
{
194 std::lock_guard l
{lock
};
195 dout(1) << "client::" << __func__
<< " " << *m
<< dendl
;
196 switch (m
->get_type()) {
197 case CEPH_MSG_OSD_MAP
:
198 objecter
->handle_osd_map((MOSDMap
*)m
);
205 void ms_handle_connect(Connection
*con
) override
{
206 dout(1) << "client::" << __func__
<< " " << con
<< dendl
;
207 std::lock_guard l
{lock
};
208 objecter
->ms_handle_connect(con
);
211 void ms_handle_remote_reset(Connection
*con
) override
{
212 dout(1) << "client::" << __func__
<< " " << con
<< dendl
;
213 std::lock_guard l
{lock
};
214 objecter
->ms_handle_remote_reset(con
);
217 bool ms_handle_reset(Connection
*con
) override
{
218 dout(1) << "client::" << __func__
<< dendl
;
219 std::lock_guard l
{lock
};
220 objecter
->ms_handle_reset(con
);
224 bool ms_handle_refused(Connection
*con
) override
{
228 const string
get_name() override
{
232 int _shutdown() override
{
234 objecter
->shutdown();
240 explicit ClientStub(CephContext
*cct
)
241 : TestStub(cct
, "client"),
242 gen((int) time(NULL
))
245 int init() override
{
247 err
= monc
.build_initial_monmap();
249 derr
<< "ClientStub::" << __func__
<< " ERROR: build initial monmap: "
250 << cpp_strerror(err
) << dendl
;
254 messenger
.reset(Messenger::create_client_messenger(cct
, "stubclient"));
255 ceph_assert(messenger
.get() != NULL
);
257 messenger
->set_default_policy(
258 Messenger::Policy::lossy_client(CEPH_FEATURE_OSDREPLYMUX
));
259 dout(10) << "ClientStub::" << __func__
<< " starting messenger at "
260 << messenger
->get_myaddrs() << dendl
;
262 objecter
.reset(new Objecter(cct
, messenger
.get(), &monc
, NULL
, 0, 0));
263 ceph_assert(objecter
.get() != NULL
);
264 objecter
->set_balanced_budget();
266 monc
.set_messenger(messenger
.get());
268 messenger
->add_dispatcher_head(this);
270 monc
.set_want_keys(CEPH_ENTITY_TYPE_MON
|CEPH_ENTITY_TYPE_OSD
);
274 derr
<< "ClientStub::" << __func__
<< " monc init error: "
275 << cpp_strerror(-err
) << dendl
;
279 err
= monc
.authenticate();
281 derr
<< "ClientStub::" << __func__
<< " monc authenticate error: "
282 << cpp_strerror(-err
) << dendl
;
286 monc
.wait_auth_rotating(30.0);
288 objecter
->set_client_incarnation(0);
297 objecter
->wait_for_osd_map();
299 dout(10) << "ClientStub::" << __func__
<< " done" << dendl
;
304 class OSDStub
: public TestStub
311 map
<pg_t
,pg_stat_t
> pgs
;
312 set
<pg_t
> pgs_changes
;
315 boost::uniform_int
<> mon_osd_rng
;
317 utime_t last_boot_attempt
;
318 static const double STUB_BOOT_INTERVAL
;
324 STUB_MON_OSD_ALIVE
= 1,
325 STUB_MON_OSD_PGTEMP
= 2,
326 STUB_MON_OSD_FAILURE
= 3,
327 STUB_MON_OSD_PGSTATS
= 4,
330 STUB_MON_OSD_FIRST
= STUB_MON_OSD_ALIVE
,
331 STUB_MON_OSD_LAST
= STUB_MON_LOG
,
334 struct C_CreatePGs
: public Context
{
336 explicit C_CreatePGs(OSDStub
*stub
) : s(stub
) {}
337 void finish(int r
) override
{
338 if (r
== -ECANCELED
) {
339 generic_dout(20) << "C_CreatePGs::" << __func__
340 << " shutdown" << dendl
;
343 generic_dout(20) << "C_CreatePGs::" << __func__
<< dendl
;
344 s
->auto_create_pgs();
349 OSDStub(int _whoami
, CephContext
*cct
)
350 : TestStub(cct
, "osd"),
353 mon_osd_rng(STUB_MON_OSD_FIRST
, STUB_MON_OSD_LAST
)
355 dout(20) << __func__
<< " auth supported: "
356 << cct
->_conf
->auth_supported
<< dendl
;
358 ss
<< "client-osd" << whoami
;
359 std::string public_msgr_type
= cct
->_conf
->ms_public_type
.empty() ? cct
->_conf
.get_val
<std::string
>("ms_type") : cct
->_conf
->ms_public_type
;
360 messenger
.reset(Messenger::create(cct
, public_msgr_type
, entity_name_t::OSD(whoami
),
361 ss
.str().c_str(), getpid(), 0));
363 Throttle
throttler(g_ceph_context
, "osd_client_bytes",
364 g_conf()->osd_client_message_size_cap
);
366 messenger
->set_default_policy(
367 Messenger::Policy::stateless_server(0));
368 messenger
->set_policy_throttlers(entity_name_t::TYPE_CLIENT
,
370 messenger
->set_policy(entity_name_t::TYPE_MON
,
371 Messenger::Policy::lossy_client(
373 CEPH_FEATURE_PGID64
|
374 CEPH_FEATURE_OSDENC
));
375 messenger
->set_policy(entity_name_t::TYPE_OSD
,
376 Messenger::Policy::stateless_server(0));
378 dout(10) << __func__
<< " public addr " << g_conf()->public_addr
<< dendl
;
379 int err
= messenger
->bind(g_conf()->public_addr
);
383 if (monc
.build_initial_monmap() < 0)
387 monc
.set_messenger(messenger
.get());
390 int init() override
{
391 dout(10) << __func__
<< dendl
;
392 std::lock_guard l
{lock
};
394 dout(1) << __func__
<< " fsid " << monc
.monmap
.fsid
395 << " osd_fsid " << g_conf()->osd_uuid
<< dendl
;
396 dout(1) << __func__
<< " name " << g_conf()->name
<< dendl
;
399 messenger
->add_dispatcher_head(this);
400 monc
.set_want_keys(CEPH_ENTITY_TYPE_MON
| CEPH_ENTITY_TYPE_OSD
);
402 int err
= monc
.init();
404 derr
<< __func__
<< " monc init error: "
405 << cpp_strerror(-err
) << dendl
;
409 err
= monc
.authenticate();
411 derr
<< __func__
<< " monc authenticate error: "
412 << cpp_strerror(-err
) << dendl
;
416 ceph_assert(!monc
.get_fsid().is_zero());
418 monc
.wait_auth_rotating(30.0);
421 dout(10) << __func__
<< " creating osd superblock" << dendl
;
422 sb
.cluster_fsid
= monc
.monmap
.fsid
;
423 sb
.osd_fsid
.generate_random();
425 sb
.compat_features
= CompatSet();
426 dout(20) << __func__
<< " " << sb
<< dendl
;
427 dout(20) << __func__
<< " osdmap " << osdmap
<< dendl
;
432 // give a chance to the mons to inform us of what PGs we should create
433 timer
.add_event_after(30.0, new C_CreatePGs(this));
438 int _shutdown() override
{
444 dout(1) << __func__
<< " boot?" << dendl
;
446 utime_t now
= ceph_clock_now();
447 if ((last_boot_attempt
> 0.0)
448 && ((now
- last_boot_attempt
)) <= STUB_BOOT_INTERVAL
) {
449 dout(1) << __func__
<< " backoff and try again later." << dendl
;
453 dout(1) << __func__
<< " boot!" << dendl
;
454 MOSDBoot
*mboot
= new MOSDBoot
;
456 last_boot_attempt
= now
;
457 monc
.send_mon_message(mboot
);
460 void add_pg(pg_t pgid
, epoch_t epoch
, pg_t parent
) {
462 utime_t now
= ceph_clock_now();
466 s
.last_epoch_clean
= epoch
;
468 s
.state
|= PG_STATE_CLEAN
| PG_STATE_ACTIVE
;
473 s
.last_unstale
= now
;
476 pgs_changes
.insert(pgid
);
479 void auto_create_pgs() {
480 bool has_pgs
= !pgs
.empty();
482 << ": " << (has_pgs
? "has pgs; ignore" : "create pgs") << dendl
;
486 if (!osdmap
.get_epoch()) {
488 << " still don't have osdmap; reschedule pg creation" << dendl
;
489 timer
.add_event_after(10.0, new C_CreatePGs(this));
493 auto& osdmap_pools
= osdmap
.get_pools();
494 for (auto pit
= osdmap_pools
.begin(); pit
!= osdmap_pools
.end(); ++pit
) {
495 const int64_t pool_id
= pit
->first
;
496 const pg_pool_t
&pool
= pit
->second
;
497 int ruleno
= pool
.get_crush_rule();
499 if (!osdmap
.crush
->rule_exists(ruleno
)) {
501 << " no crush rule for pool id " << pool_id
502 << " rule no " << ruleno
<< dendl
;
506 epoch_t pool_epoch
= pool
.get_last_change();
508 << " pool num pgs " << pool
.get_pg_num()
509 << " epoch " << pool_epoch
<< dendl
;
511 for (ps_t ps
= 0; ps
< pool
.get_pg_num(); ++ps
) {
512 pg_t
pgid(ps
, pool_id
);
515 << " pgid " << pgid
<< " parent " << parent
<< dendl
;
516 add_pg(pgid
, pool_epoch
, parent
);
521 void update_osd_stat() {
523 int ret
= statfs(".", &stbuf
);
527 << " cannot statfs ." << cpp_strerror(ret
) << dendl
;
531 osd_stat
.statfs
.total
= stbuf
.f_blocks
* stbuf
.f_bsize
;
532 osd_stat
.statfs
.available
= stbuf
.f_bavail
* stbuf
.f_bsize
;
533 osd_stat
.statfs
.internally_reserved
= 0;
536 void send_pg_stats() {
538 << " pgs " << pgs
.size() << " osdmap " << osdmap
<< dendl
;
539 MPGStats
*mstats
= new MPGStats(monc
.get_fsid(), osdmap
.get_epoch());
542 mstats
->osd_stat
= osd_stat
;
544 set
<pg_t
>::iterator it
;
545 for (it
= pgs_changes
.begin(); it
!= pgs_changes
.end(); ++it
) {
547 if (pgs
.count(pgid
) == 0) {
549 << " pgid " << pgid
<< " not on our map" << dendl
;
550 ceph_abort_msg("pgid not on our map");
552 pg_stat_t
&s
= pgs
[pgid
];
553 mstats
->pg_stat
[pgid
] = s
;
555 JSONFormatter
f(true);
558 << " pg " << pgid
<< " stats:\n";
563 dout(10) << __func__
<< " send " << *mstats
<< dendl
;
564 monc
.send_mon_message(mstats
);
567 void modify_pg(pg_t pgid
) {
568 dout(10) << __func__
<< " pg " << pgid
<< dendl
;
569 ceph_assert(pgs
.count(pgid
) > 0);
571 pg_stat_t
&s
= pgs
[pgid
];
572 utime_t now
= ceph_clock_now();
574 if (now
- s
.last_change
< 10.0) {
576 << " pg " << pgid
<< " changed in the last 10s" << dendl
;
580 s
.state
^= PG_STATE_CLEAN
;
581 if (s
.state
& PG_STATE_CLEAN
)
586 pgs_changes
.insert(pgid
);
590 dout(10) << __func__
<< dendl
;
594 << " no pgs available! don't attempt to modify." << dendl
;
598 boost::uniform_int
<> pg_rng(0, pgs
.size()-1);
601 int num_pgs
= pg_rng(gen
);
602 while ((int)pgs_pos
.size() < num_pgs
)
603 pgs_pos
.insert(pg_rng(gen
));
605 map
<pg_t
,pg_stat_t
>::iterator it
= pgs
.begin();
606 set
<int>::iterator pos_it
= pgs_pos
.begin();
609 while (pos_it
!= pgs_pos
.end()) {
611 dout(20) << __func__
<< " pg at pos " << at
<< dendl
;
612 while ((pgs_at
!= at
) && (it
!= pgs
.end())) {
616 ceph_assert(it
!= pgs
.end());
618 << " pg at pos " << at
<< ": " << it
->first
<< dendl
;
619 modify_pg(it
->first
);
625 dout(10) << __func__
<< dendl
;
626 if (!osdmap
.exists(whoami
)) {
627 dout(0) << __func__
<< " I'm not in the osdmap!!\n";
628 JSONFormatter
f(true);
633 if (osdmap
.get_epoch() == 0) {
634 dout(1) << __func__
<< " wait for osdmap" << dendl
;
637 epoch_t up_thru
= osdmap
.get_up_thru(whoami
);
638 dout(10) << __func__
<< "up_thru: " << osdmap
.get_up_thru(whoami
) << dendl
;
640 monc
.send_mon_message(new MOSDAlive(osdmap
.get_epoch(), up_thru
));
644 if (osdmap
.get_epoch() == 0) {
645 dout(1) << __func__
<< " wait for osdmap" << dendl
;
648 dout(10) << __func__
<< dendl
;
649 MOSDPGTemp
*m
= new MOSDPGTemp(osdmap
.get_epoch());
650 monc
.send_mon_message(m
);
654 dout(10) << __func__
<< dendl
;
658 dout(10) << __func__
<< dendl
;
661 if (!pgs_changes
.empty())
663 monc
.sub_want("osd_pg_creates", 0, CEPH_SUBSCRIBE_ONETIME
);
666 dout(20) << __func__
<< " pg pools:\n";
668 JSONFormatter
f(true);
669 f
.open_array_section("pools");
670 auto& osdmap_pools
= osdmap
.get_pools();
671 for (auto pit
= osdmap_pools
.begin(); pit
!= osdmap_pools
.end(); ++pit
) {
672 const int64_t pool_id
= pit
->first
;
673 const pg_pool_t
&pool
= pit
->second
;
674 f
.open_object_section("pool");
675 f
.dump_int("pool_id", pool_id
);
676 f
.open_object_section("pool_dump");
687 dout(10) << __func__
<< dendl
;
689 MLog
*m
= new MLog(monc
.get_fsid());
691 boost::uniform_int
<> log_rng(1, 10);
692 size_t num_entries
= log_rng(gen
);
694 << " send " << num_entries
<< " log messages" << dendl
;
696 utime_t now
= ceph_clock_now();
698 for (; num_entries
> 0; --num_entries
) {
700 e
.rank
= messenger
->get_myname();
701 e
.addrs
= messenger
->get_myaddrs();
705 e
.msg
= "OSDStub::op_log";
706 m
->entries
.push_back(e
);
709 monc
.send_mon_message(m
);
712 void _tick() override
{
713 if (!osdmap
.exists(whoami
)) {
714 std::cout
<< __func__
<< " not in the cluster; boot!" << std::endl
;
721 boost::uniform_int
<> op_rng(STUB_MON_OSD_FIRST
, STUB_MON_OSD_LAST
);
722 int op
= op_rng(gen
);
724 case STUB_MON_OSD_ALIVE
:
727 case STUB_MON_OSD_PGTEMP
:
730 case STUB_MON_OSD_FAILURE
:
733 case STUB_MON_OSD_PGSTATS
:
742 void handle_pg_create(MOSDPGCreate
*m
) {
743 ceph_assert(m
!= NULL
);
744 if (m
->epoch
< osdmap
.get_epoch()) {
745 std::cout
<< __func__
<< " epoch " << m
->epoch
<< " < "
746 << osdmap
.get_epoch() << "; dropping" << std::endl
;
751 for (map
<pg_t
,pg_create_t
>::iterator it
= m
->mkpg
.begin();
752 it
!= m
->mkpg
.end(); ++it
) {
753 pg_create_t
&c
= it
->second
;
754 std::cout
<< __func__
<< " pg " << it
->first
755 << " created " << c
.created
756 << " parent " << c
.parent
<< std::endl
;
757 if (pgs
.count(it
->first
)) {
758 std::cout
<< __func__
<< " pg " << it
->first
759 << " exists; skipping" << std::endl
;
763 pg_t pgid
= it
->first
;
764 add_pg(pgid
, c
.created
, c
.parent
);
769 void handle_osd_map(MOSDMap
*m
) {
770 dout(1) << __func__
<< dendl
;
771 if (m
->fsid
!= monc
.get_fsid()) {
773 << " message fsid " << m
->fsid
<< " != " << monc
.get_fsid()
775 dout(0) << __func__
<< " " << m
776 << " from " << m
->get_source_inst()
778 dout(0) << monc
.get_monmap() << dendl
;
780 ceph_assert(m
->fsid
== monc
.get_fsid());
782 epoch_t first
= m
->get_first();
783 epoch_t last
= m
->get_last();
785 << " epochs [" << first
<< "," << last
<< "]"
786 << " current " << osdmap
.get_epoch() << dendl
;
788 if (last
<= osdmap
.get_epoch()) {
789 dout(5) << __func__
<< " no new maps here; dropping" << dendl
;
794 if (first
> osdmap
.get_epoch() + 1) {
796 << osdmap
.get_epoch() + 1 << ".." << (first
-1) << dendl
;
797 if ((m
->oldest_map
< first
&& osdmap
.get_epoch() == 0) ||
798 m
->oldest_map
<= osdmap
.get_epoch()) {
799 monc
.sub_want("osdmap", osdmap
.get_epoch()+1,
800 CEPH_SUBSCRIBE_ONETIME
);
807 epoch_t start_full
= std::max(osdmap
.get_epoch() + 1, first
);
809 if (m
->maps
.size() > 0) {
810 map
<epoch_t
,bufferlist
>::reverse_iterator rit
;
811 rit
= m
->maps
.rbegin();
812 if (start_full
<= rit
->first
) {
813 start_full
= rit
->first
;
815 << " full epoch " << start_full
<< dendl
;
816 bufferlist
&bl
= rit
->second
;
817 auto p
= bl
.cbegin();
822 for (epoch_t e
= start_full
; e
<= last
; e
++) {
823 map
<epoch_t
,bufferlist
>::iterator it
;
824 it
= m
->incremental_maps
.find(e
);
825 if (it
== m
->incremental_maps
.end())
829 << " incremental epoch " << e
830 << " on full epoch " << start_full
<< dendl
;
831 OSDMap::Incremental inc
;
832 bufferlist
&bl
= it
->second
;
833 auto p
= bl
.cbegin();
836 int err
= osdmap
.apply_incremental(inc
);
838 derr
<< "osd." << whoami
<< "::" << __func__
839 << "** ERROR: applying incremental: "
840 << cpp_strerror(err
) << dendl
;
841 ceph_abort_msg("error applying incremental");
844 dout(30) << __func__
<< "\nosdmap:\n";
845 JSONFormatter
f(true);
850 if (osdmap
.is_up(whoami
) &&
851 osdmap
.get_addrs(whoami
) == messenger
->get_myaddrs()) {
853 << " got into the osdmap and we're up!" << dendl
;
856 if (m
->newest_map
&& m
->newest_map
> last
) {
858 << " they have more maps; requesting them!" << dendl
;
859 monc
.sub_want("osdmap", osdmap
.get_epoch()+1, CEPH_SUBSCRIBE_ONETIME
);
863 dout(10) << __func__
<< " done" << dendl
;
867 bool ms_dispatch(Message
*m
) override
{
868 dout(1) << __func__
<< " " << *m
<< dendl
;
870 switch (m
->get_type()) {
871 case MSG_OSD_PG_CREATE
:
872 handle_pg_create((MOSDPGCreate
*)m
);
874 case CEPH_MSG_OSD_MAP
:
875 handle_osd_map((MOSDMap
*)m
);
884 void ms_handle_connect(Connection
*con
) override
{
885 dout(1) << __func__
<< " " << con
<< dendl
;
886 if (con
->get_peer_type() == CEPH_ENTITY_TYPE_MON
) {
887 dout(10) << __func__
<< " on mon" << dendl
;
891 void ms_handle_remote_reset(Connection
*con
) override
{}
893 bool ms_handle_reset(Connection
*con
) override
{
894 dout(1) << __func__
<< dendl
;
895 return con
->get_priv().get();
898 bool ms_handle_refused(Connection
*con
) override
{
902 const string
get_name() override
{
904 ss
<< "osd." << whoami
;
909 double const OSDStub::STUB_BOOT_INTERVAL
= 10.0;
912 #define dout_prefix *_dout << "main "
914 const char *our_name
= NULL
;
915 vector
<TestStub
*> stubs
;
916 ceph::mutex shutdown_lock
= ceph::make_mutex("main::shutdown_lock");
917 ceph::condition_variable shutdown_cond
;
918 Context
*shutdown_cb
= NULL
;
919 SafeTimer
*shutdown_timer
= NULL
;
921 struct C_Shutdown
: public Context
923 void finish(int r
) override
{
924 generic_dout(10) << "main::shutdown time has ran out" << dendl
;
925 shutdown_cond
.notify_all();
929 void handle_test_signal(int signum
)
931 if ((signum
!= SIGINT
) && (signum
!= SIGTERM
))
934 std::cerr
<< "*** Got signal " << sig_str(signum
) << " ***" << std::endl
;
935 std::lock_guard l
{shutdown_lock
};
936 if (shutdown_timer
) {
937 shutdown_timer
->cancel_all_events();
938 shutdown_cond
.notify_all();
943 ceph_assert(our_name
!= NULL
);
945 std::cout
<< "usage: " << our_name
946 << " <--stub-id ID> [--stub-id ID...]"
950 -c FILE Read configuration from FILE\n\
951 --keyring FILE Read keyring from FILE\n\
952 --help This message\n\
954 Test-specific Options:\n\
955 --stub-id ID1..ID2 Interval of OSD ids for multiple stubs to mimic.\n\
956 --stub-id ID OSD id a stub will mimic to be\n\
957 (same as --stub-id ID..ID)\n\
961 int get_id_interval(int &first
, int &last
, string
&str
)
963 size_t found
= str
.find("..");
964 string first_str
, last_str
;
965 if (found
== string::npos
) {
966 first_str
= last_str
= str
;
968 first_str
= str
.substr(0, found
);
969 last_str
= str
.substr(found
+2);
973 first
= strict_strtol(first_str
.c_str(), 10, &err
);
974 if ((first
== 0) && (!err
.empty())) {
975 std::cerr
<< err
<< std::endl
;
979 last
= strict_strtol(last_str
.c_str(), 10, &err
);
980 if ((last
== 0) && (!err
.empty())) {
981 std::cerr
<< err
<< std::endl
;
987 int main(int argc
, const char *argv
[])
989 vector
<const char*> args
;
991 argv_to_vec(argc
, argv
, args
);
993 auto cct
= global_init(NULL
, args
,
994 CEPH_ENTITY_TYPE_OSD
, CODE_ENVIRONMENT_UTILITY
,
995 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE
);
997 common_init_finish(g_ceph_context
);
998 g_ceph_context
->_conf
.apply_changes(nullptr);
1001 double duration
= 300.0;
1003 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end();) {
1006 if (ceph_argparse_double_dash(args
, i
)) {
1008 } else if (ceph_argparse_witharg(args
, i
, &val
,
1009 "--stub-id", (char*) NULL
)) {
1010 int first
= -1, last
= -1;
1011 if (get_id_interval(first
, last
, val
) < 0) {
1012 std::cerr
<< "** error parsing stub id '" << val
<< "'" << std::endl
;
1016 for (; first
<= last
; ++first
)
1017 stub_ids
.insert(first
);
1018 } else if (ceph_argparse_witharg(args
, i
, &val
,
1019 "--duration", (char*) NULL
)) {
1021 duration
= (double) strict_strtol(val
.c_str(), 10, &err
);
1022 if ((duration
== 0) && (!err
.empty())) {
1023 std::cerr
<< "** error parsing '--duration " << val
<< "': '"
1024 << err
<< std::endl
;
1027 } else if (ceph_argparse_flag(args
, i
, "--help", (char*) NULL
)) {
1031 std::cerr
<< "unknown argument '" << *i
<< "'" << std::endl
;
1036 if (stub_ids
.empty()) {
1037 std::cerr
<< "** error: must specify at least one '--stub-id <ID>'"
1043 for (set
<int>::iterator i
= stub_ids
.begin(); i
!= stub_ids
.end(); ++i
) {
1046 std::cout
<< __func__
<< " starting stub." << whoami
<< std::endl
;
1047 OSDStub
*stub
= new OSDStub(whoami
, g_ceph_context
);
1048 int err
= stub
->init();
1050 std::cerr
<< "** osd stub error: " << cpp_strerror(-err
) << std::endl
;
1053 stubs
.push_back(stub
);
1056 std::cout
<< __func__
<< " starting client stub" << std::endl
;
1057 ClientStub
*cstub
= new ClientStub(g_ceph_context
);
1058 int err
= cstub
->init();
1060 std::cerr
<< "** client stub error: " << cpp_strerror(-err
) << std::endl
;
1063 stubs
.push_back(cstub
);
1065 init_async_signal_handler();
1066 register_async_signal_handler_oneshot(SIGINT
, handle_test_signal
);
1067 register_async_signal_handler_oneshot(SIGTERM
, handle_test_signal
);
1070 unique_lock locker
{shutdown_lock
};
1071 shutdown_timer
= new SafeTimer(g_ceph_context
, shutdown_lock
);
1072 shutdown_timer
->init();
1073 if (duration
!= 0) {
1074 std::cout
<< __func__
1075 << " run test for " << duration
<< " seconds" << std::endl
;
1076 shutdown_timer
->add_event_after((double) duration
, new C_Shutdown
);
1078 shutdown_cond
.wait(locker
);
1079 shutdown_timer
->shutdown();
1080 delete shutdown_timer
;
1081 shutdown_timer
= NULL
;
1083 unregister_async_signal_handler(SIGINT
, handle_test_signal
);
1084 unregister_async_signal_handler(SIGTERM
, handle_test_signal
);
1086 std::cout
<< __func__
<< " waiting for stubs to finish" << std::endl
;
1087 vector
<TestStub
*>::iterator it
;
1089 for (i
= 0, it
= stubs
.begin(); it
!= stubs
.end(); ++it
, ++i
) {
1093 std::cout
<< __func__
<< " finished " << (*it
)->get_name() << std::endl
;