-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
DaemonServer.cc
3068 lines (2871 loc) · 93.9 KB
/
DaemonServer.cc
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2016 John Spray <john.spray@redhat.com>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*/
#include "DaemonServer.h"
#include <boost/algorithm/string.hpp>
#include "mgr/Mgr.h"
#include "include/stringify.h"
#include "include/str_list.h"
#include "auth/RotatingKeyRing.h"
#include "json_spirit/json_spirit_writer.h"
#include "mgr/mgr_commands.h"
#include "mgr/DaemonHealthMetricCollector.h"
#include "mgr/OSDPerfMetricCollector.h"
#include "mgr/MDSPerfMetricCollector.h"
#include "mon/MonCommand.h"
#include "messages/MMgrOpen.h"
#include "messages/MMgrClose.h"
#include "messages/MMgrConfigure.h"
#include "messages/MMonMgrReport.h"
#include "messages/MCommand.h"
#include "messages/MCommandReply.h"
#include "messages/MMgrCommand.h"
#include "messages/MMgrCommandReply.h"
#include "messages/MPGStats.h"
#include "messages/MOSDScrub.h"
#include "messages/MOSDScrub2.h"
#include "messages/MOSDForceRecovery.h"
#include "common/errno.h"
#include "common/pick_address.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_mgr
#undef dout_prefix
#define dout_prefix *_dout << "mgr.server " << __func__ << " "
using namespace TOPNSPC::common;
namespace {
template <typename Map>
bool map_compare(Map const &lhs, Map const &rhs) {
return lhs.size() == rhs.size()
&& std::equal(lhs.begin(), lhs.end(), rhs.begin(),
[] (auto a, auto b) { return a.first == b.first && a.second == b.second; });
}
}
DaemonServer::DaemonServer(MonClient *monc_,
Finisher &finisher_,
DaemonStateIndex &daemon_state_,
ClusterState &cluster_state_,
PyModuleRegistry &py_modules_,
LogChannelRef clog_,
LogChannelRef audit_clog_)
: Dispatcher(g_ceph_context),
client_byte_throttler(new Throttle(g_ceph_context, "mgr_client_bytes",
g_conf().get_val<Option::size_t>("mgr_client_bytes"))),
client_msg_throttler(new Throttle(g_ceph_context, "mgr_client_messages",
g_conf().get_val<uint64_t>("mgr_client_messages"))),
osd_byte_throttler(new Throttle(g_ceph_context, "mgr_osd_bytes",
g_conf().get_val<Option::size_t>("mgr_osd_bytes"))),
osd_msg_throttler(new Throttle(g_ceph_context, "mgr_osd_messsages",
g_conf().get_val<uint64_t>("mgr_osd_messages"))),
mds_byte_throttler(new Throttle(g_ceph_context, "mgr_mds_bytes",
g_conf().get_val<Option::size_t>("mgr_mds_bytes"))),
mds_msg_throttler(new Throttle(g_ceph_context, "mgr_mds_messsages",
g_conf().get_val<uint64_t>("mgr_mds_messages"))),
mon_byte_throttler(new Throttle(g_ceph_context, "mgr_mon_bytes",
g_conf().get_val<Option::size_t>("mgr_mon_bytes"))),
mon_msg_throttler(new Throttle(g_ceph_context, "mgr_mon_messsages",
g_conf().get_val<uint64_t>("mgr_mon_messages"))),
msgr(nullptr),
monc(monc_),
finisher(finisher_),
daemon_state(daemon_state_),
cluster_state(cluster_state_),
py_modules(py_modules_),
clog(clog_),
audit_clog(audit_clog_),
pgmap_ready(false),
timer(g_ceph_context, lock),
shutting_down(false),
tick_event(nullptr),
osd_perf_metric_collector_listener(this),
osd_perf_metric_collector(osd_perf_metric_collector_listener),
mds_perf_metric_collector_listener(this),
mds_perf_metric_collector(mds_perf_metric_collector_listener)
{
g_conf().add_observer(this);
}
DaemonServer::~DaemonServer() {
delete msgr;
g_conf().remove_observer(this);
}
int DaemonServer::init(uint64_t gid, entity_addrvec_t client_addrs)
{
// Initialize Messenger
std::string public_msgr_type = g_conf()->ms_public_type.empty() ?
g_conf().get_val<std::string>("ms_type") : g_conf()->ms_public_type;
msgr = Messenger::create(g_ceph_context, public_msgr_type,
entity_name_t::MGR(gid),
"mgr",
Messenger::get_pid_nonce());
msgr->set_default_policy(Messenger::Policy::stateless_server(0));
msgr->set_auth_client(monc);
// throttle clients
msgr->set_policy_throttlers(entity_name_t::TYPE_CLIENT,
client_byte_throttler.get(),
client_msg_throttler.get());
// servers
msgr->set_policy_throttlers(entity_name_t::TYPE_OSD,
osd_byte_throttler.get(),
osd_msg_throttler.get());
msgr->set_policy_throttlers(entity_name_t::TYPE_MDS,
mds_byte_throttler.get(),
mds_msg_throttler.get());
msgr->set_policy_throttlers(entity_name_t::TYPE_MON,
mon_byte_throttler.get(),
mon_msg_throttler.get());
entity_addrvec_t addrs;
int r = pick_addresses(cct, CEPH_PICK_ADDRESS_PUBLIC, &addrs);
if (r < 0) {
return r;
}
dout(20) << __func__ << " will bind to " << addrs << dendl;
r = msgr->bindv(addrs);
if (r < 0) {
derr << "unable to bind mgr to " << addrs << dendl;
return r;
}
msgr->set_myname(entity_name_t::MGR(gid));
msgr->set_addr_unknowns(client_addrs);
msgr->start();
msgr->add_dispatcher_tail(this);
msgr->set_auth_server(monc);
monc->set_handle_authentication_dispatcher(this);
started_at = ceph_clock_now();
std::lock_guard l(lock);
timer.init();
schedule_tick_locked(
g_conf().get_val<std::chrono::seconds>("mgr_tick_period").count());
return 0;
}
entity_addrvec_t DaemonServer::get_myaddrs() const
{
return msgr->get_myaddrs();
}
int DaemonServer::ms_handle_authentication(Connection *con)
{
auto s = ceph::make_ref<MgrSession>(cct);
con->set_priv(s);
s->inst.addr = con->get_peer_addr();
s->entity_name = con->peer_name;
dout(10) << __func__ << " new session " << s << " con " << con
<< " entity " << con->peer_name
<< " addr " << con->get_peer_addrs()
<< dendl;
AuthCapsInfo &caps_info = con->get_peer_caps_info();
if (caps_info.allow_all) {
dout(10) << " session " << s << " " << s->entity_name
<< " allow_all" << dendl;
s->caps.set_allow_all();
} else if (caps_info.caps.length() > 0) {
auto p = caps_info.caps.cbegin();
string str;
try {
decode(str, p);
}
catch (buffer::error& e) {
dout(10) << " session " << s << " " << s->entity_name
<< " failed to decode caps" << dendl;
return -EACCES;
}
if (!s->caps.parse(str)) {
dout(10) << " session " << s << " " << s->entity_name
<< " failed to parse caps '" << str << "'" << dendl;
return -EACCES;
}
dout(10) << " session " << s << " " << s->entity_name
<< " has caps " << s->caps << " '" << str << "'" << dendl;
}
if (con->get_peer_type() == CEPH_ENTITY_TYPE_OSD) {
std::lock_guard l(lock);
s->osd_id = atoi(s->entity_name.get_id().c_str());
dout(10) << "registering osd." << s->osd_id << " session "
<< s << " con " << con << dendl;
osd_cons[s->osd_id].insert(con);
}
return 1;
}
bool DaemonServer::ms_handle_reset(Connection *con)
{
if (con->get_peer_type() == CEPH_ENTITY_TYPE_OSD) {
auto priv = con->get_priv();
auto session = static_cast<MgrSession*>(priv.get());
if (!session) {
return false;
}
std::lock_guard l(lock);
dout(10) << "unregistering osd." << session->osd_id
<< " session " << session << " con " << con << dendl;
osd_cons[session->osd_id].erase(con);
auto iter = daemon_connections.find(con);
if (iter != daemon_connections.end()) {
daemon_connections.erase(iter);
}
}
return false;
}
bool DaemonServer::ms_handle_refused(Connection *con)
{
// do nothing for now
return false;
}
bool DaemonServer::ms_dispatch2(const ref_t<Message>& m)
{
// Note that we do *not* take ::lock here, in order to avoid
// serializing all message handling. It's up to each handler
// to take whatever locks it needs.
switch (m->get_type()) {
case MSG_PGSTATS:
cluster_state.ingest_pgstats(ref_cast<MPGStats>(m));
maybe_ready(m->get_source().num());
return true;
case MSG_MGR_REPORT:
return handle_report(ref_cast<MMgrReport>(m));
case MSG_MGR_OPEN:
return handle_open(ref_cast<MMgrOpen>(m));
case MSG_MGR_CLOSE:
return handle_close(ref_cast<MMgrClose>(m));
case MSG_COMMAND:
return handle_command(ref_cast<MCommand>(m));
case MSG_MGR_COMMAND:
return handle_command(ref_cast<MMgrCommand>(m));
default:
dout(1) << "Unhandled message type " << m->get_type() << dendl;
return false;
};
}
void DaemonServer::dump_pg_ready(ceph::Formatter *f)
{
f->dump_bool("pg_ready", pgmap_ready.load());
}
void DaemonServer::maybe_ready(int32_t osd_id)
{
if (pgmap_ready.load()) {
// Fast path: we don't need to take lock because pgmap_ready
// is already set
} else {
std::lock_guard l(lock);
if (reported_osds.find(osd_id) == reported_osds.end()) {
dout(4) << "initial report from osd " << osd_id << dendl;
reported_osds.insert(osd_id);
std::set<int32_t> up_osds;
cluster_state.with_osdmap([&](const OSDMap& osdmap) {
osdmap.get_up_osds(up_osds);
});
std::set<int32_t> unreported_osds;
std::set_difference(up_osds.begin(), up_osds.end(),
reported_osds.begin(), reported_osds.end(),
std::inserter(unreported_osds, unreported_osds.begin()));
if (unreported_osds.size() == 0) {
dout(4) << "all osds have reported, sending PG state to mon" << dendl;
pgmap_ready = true;
reported_osds.clear();
// Avoid waiting for next tick
send_report();
} else {
dout(4) << "still waiting for " << unreported_osds.size() << " osds"
" to report in before PGMap is ready" << dendl;
}
}
}
}
void DaemonServer::tick()
{
dout(10) << dendl;
send_report();
adjust_pgs();
schedule_tick_locked(
g_conf().get_val<std::chrono::seconds>("mgr_tick_period").count());
}
// Currently modules do not set health checks in response to events delivered to
// all modules (e.g. notify) so we do not risk a thundering hurd situation here.
// if this pattern emerges in the future, this scheduler could be modified to
// fire after all modules have had a chance to set their health checks.
void DaemonServer::schedule_tick_locked(double delay_sec)
{
ceph_assert(ceph_mutex_is_locked_by_me(lock));
if (tick_event) {
timer.cancel_event(tick_event);
tick_event = nullptr;
}
// on shutdown start rejecting explicit requests to send reports that may
// originate from python land which may still be running.
if (shutting_down)
return;
tick_event = timer.add_event_after(delay_sec,
new LambdaContext([this](int r) {
tick();
}));
}
void DaemonServer::schedule_tick(double delay_sec)
{
std::lock_guard l(lock);
schedule_tick_locked(delay_sec);
}
void DaemonServer::handle_osd_perf_metric_query_updated()
{
dout(10) << dendl;
// Send a fresh MMgrConfigure to all clients, so that they can follow
// the new policy for transmitting stats
finisher.queue(new LambdaContext([this](int r) {
std::lock_guard l(lock);
for (auto &c : daemon_connections) {
if (c->peer_is_osd()) {
_send_configure(c);
}
}
}));
}
void DaemonServer::handle_mds_perf_metric_query_updated()
{
dout(10) << dendl;
// Send a fresh MMgrConfigure to all clients, so that they can follow
// the new policy for transmitting stats
finisher.queue(new LambdaContext([this](int r) {
std::lock_guard l(lock);
for (auto &c : daemon_connections) {
if (c->peer_is_mds()) {
_send_configure(c);
}
}
}));
}
void DaemonServer::shutdown()
{
dout(10) << "begin" << dendl;
msgr->shutdown();
msgr->wait();
cluster_state.shutdown();
dout(10) << "done" << dendl;
std::lock_guard l(lock);
shutting_down = true;
timer.shutdown();
}
static DaemonKey key_from_service(
const std::string& service_name,
int peer_type,
const std::string& daemon_name)
{
if (!service_name.empty()) {
return DaemonKey{service_name, daemon_name};
} else {
return DaemonKey{ceph_entity_type_name(peer_type), daemon_name};
}
}
void DaemonServer::fetch_missing_metadata(const DaemonKey& key,
const entity_addr_t& addr)
{
if (!daemon_state.is_updating(key) &&
(key.type == "osd" || key.type == "mds" || key.type == "mon")) {
std::ostringstream oss;
auto c = new MetadataUpdate(daemon_state, key);
if (key.type == "osd") {
oss << "{\"prefix\": \"osd metadata\", \"id\": "
<< key.name<< "}";
} else if (key.type == "mds") {
c->set_default("addr", stringify(addr));
oss << "{\"prefix\": \"mds metadata\", \"who\": \""
<< key.name << "\"}";
} else if (key.type == "mon") {
oss << "{\"prefix\": \"mon metadata\", \"id\": \""
<< key.name << "\"}";
} else {
ceph_abort();
}
monc->start_mon_command({oss.str()}, {}, &c->outbl, &c->outs, c);
}
}
bool DaemonServer::handle_open(const ref_t<MMgrOpen>& m)
{
std::unique_lock l(lock);
DaemonKey key = key_from_service(m->service_name,
m->get_connection()->get_peer_type(),
m->daemon_name);
auto con = m->get_connection();
dout(10) << "from " << key << " " << con->get_peer_addr() << dendl;
_send_configure(con);
DaemonStatePtr daemon;
if (daemon_state.exists(key)) {
dout(20) << "updating existing DaemonState for " << key << dendl;
daemon = daemon_state.get(key);
}
if (!daemon) {
if (m->service_daemon) {
dout(4) << "constructing new DaemonState for " << key << dendl;
daemon = std::make_shared<DaemonState>(daemon_state.types);
daemon->key = key;
daemon->service_daemon = true;
daemon_state.insert(daemon);
} else {
/* A normal Ceph daemon has connected but we are or should be waiting on
* metadata for it. Close the session so that it tries to reconnect.
*/
dout(2) << "ignoring open from " << key << " " << con->get_peer_addr()
<< "; not ready for session (expect reconnect)" << dendl;
con->mark_down();
l.unlock();
fetch_missing_metadata(key, m->get_source_addr());
return true;
}
}
if (daemon) {
if (m->service_daemon) {
// update the metadata through the daemon state index to
// ensure it's kept up-to-date
daemon_state.update_metadata(daemon, m->daemon_metadata);
}
std::lock_guard l(daemon->lock);
daemon->perf_counters.clear();
daemon->service_daemon = m->service_daemon;
if (m->service_daemon) {
daemon->service_status = m->daemon_status;
utime_t now = ceph_clock_now();
auto [d, added] = pending_service_map.get_daemon(m->service_name,
m->daemon_name);
if (added || d->gid != (uint64_t)m->get_source().num()) {
dout(10) << "registering " << key << " in pending_service_map" << dendl;
d->gid = m->get_source().num();
d->addr = m->get_source_addr();
d->start_epoch = pending_service_map.epoch;
d->start_stamp = now;
d->metadata = m->daemon_metadata;
pending_service_map_dirty = pending_service_map.epoch;
}
}
auto p = m->config_bl.cbegin();
if (p != m->config_bl.end()) {
decode(daemon->config, p);
decode(daemon->ignored_mon_config, p);
dout(20) << " got config " << daemon->config
<< " ignored " << daemon->ignored_mon_config << dendl;
}
daemon->config_defaults_bl = m->config_defaults_bl;
daemon->config_defaults.clear();
dout(20) << " got config_defaults_bl " << daemon->config_defaults_bl.length()
<< " bytes" << dendl;
}
if (con->get_peer_type() != entity_name_t::TYPE_CLIENT &&
m->service_name.empty())
{
// Store in set of the daemon/service connections, i.e. those
// connections that require an update in the event of stats
// configuration changes.
daemon_connections.insert(con);
}
return true;
}
bool DaemonServer::handle_close(const ref_t<MMgrClose>& m)
{
std::lock_guard l(lock);
DaemonKey key = key_from_service(m->service_name,
m->get_connection()->get_peer_type(),
m->daemon_name);
dout(4) << "from " << m->get_connection() << " " << key << dendl;
if (daemon_state.exists(key)) {
DaemonStatePtr daemon = daemon_state.get(key);
daemon_state.rm(key);
{
std::lock_guard l(daemon->lock);
if (daemon->service_daemon) {
pending_service_map.rm_daemon(m->service_name, m->daemon_name);
pending_service_map_dirty = pending_service_map.epoch;
}
}
}
// send same message back as a reply
m->get_connection()->send_message2(m);
return true;
}
void DaemonServer::update_task_status(
DaemonKey key,
const std::map<std::string,std::string>& task_status)
{
dout(10) << "got task status from " << key << dendl;
[[maybe_unused]] auto [daemon, added] =
pending_service_map.get_daemon(key.type, key.name);
if (daemon->task_status != task_status) {
daemon->task_status = task_status;
pending_service_map_dirty = pending_service_map.epoch;
}
}
bool DaemonServer::handle_report(const ref_t<MMgrReport>& m)
{
DaemonKey key;
if (!m->service_name.empty()) {
key.type = m->service_name;
} else {
key.type = ceph_entity_type_name(m->get_connection()->get_peer_type());
}
key.name = m->daemon_name;
dout(10) << "from " << m->get_connection() << " " << key << dendl;
if (m->get_connection()->get_peer_type() == entity_name_t::TYPE_CLIENT &&
m->service_name.empty()) {
// Clients should not be sending us stats unless they are declaring
// themselves to be a daemon for some service.
dout(10) << "rejecting report from non-daemon client " << m->daemon_name
<< dendl;
clog->warn() << "rejecting report from non-daemon client " << m->daemon_name
<< " at " << m->get_connection()->get_peer_addrs();
m->get_connection()->mark_down();
return true;
}
{
std::unique_lock locker(lock);
DaemonStatePtr daemon;
// Look up the DaemonState
if (daemon_state.exists(key)) {
dout(20) << "updating existing DaemonState for " << key << dendl;
daemon = daemon_state.get(key);
} else {
locker.unlock();
// we don't know the hostname at this stage, reject MMgrReport here.
dout(5) << "rejecting report from " << key << ", since we do not have its metadata now."
<< dendl;
// issue metadata request in background
fetch_missing_metadata(key, m->get_source_addr());
locker.lock();
// kill session
auto priv = m->get_connection()->get_priv();
auto session = static_cast<MgrSession*>(priv.get());
if (!session) {
return false;
}
m->get_connection()->mark_down();
dout(10) << "unregistering osd." << session->osd_id
<< " session " << session << " con " << m->get_connection() << dendl;
if (osd_cons.find(session->osd_id) != osd_cons.end()) {
osd_cons[session->osd_id].erase(m->get_connection());
}
auto iter = daemon_connections.find(m->get_connection());
if (iter != daemon_connections.end()) {
daemon_connections.erase(iter);
}
return false;
}
// Update the DaemonState
ceph_assert(daemon != nullptr);
{
std::lock_guard l(daemon->lock);
auto &daemon_counters = daemon->perf_counters;
daemon_counters.update(*m.get());
auto p = m->config_bl.cbegin();
if (p != m->config_bl.end()) {
decode(daemon->config, p);
decode(daemon->ignored_mon_config, p);
dout(20) << " got config " << daemon->config
<< " ignored " << daemon->ignored_mon_config << dendl;
}
utime_t now = ceph_clock_now();
if (daemon->service_daemon) {
if (m->daemon_status) {
daemon->service_status_stamp = now;
daemon->service_status = *m->daemon_status;
}
daemon->last_service_beacon = now;
} else if (m->daemon_status) {
derr << "got status from non-daemon " << key << dendl;
}
// update task status
if (m->task_status) {
update_task_status(key, *m->task_status);
daemon->last_service_beacon = now;
}
if (m->get_connection()->peer_is_osd() || m->get_connection()->peer_is_mon()) {
// only OSD and MON send health_checks to me now
daemon->daemon_health_metrics = std::move(m->daemon_health_metrics);
dout(10) << "daemon_health_metrics " << daemon->daemon_health_metrics
<< dendl;
}
}
}
// if there are any schema updates, notify the python modules
if (!m->declare_types.empty() || !m->undeclare_types.empty()) {
py_modules.notify_all("perf_schema_update", ceph::to_string(key));
}
if (m->get_connection()->peer_is_osd()) {
osd_perf_metric_collector.process_reports(m->osd_perf_metric_reports);
}
if (m->metric_report_message) {
const MetricReportMessage &message = *m->metric_report_message;
boost::apply_visitor(HandlePayloadVisitor(this), message.payload);
}
return true;
}
void DaemonServer::_generate_command_map(
cmdmap_t& cmdmap,
map<string,string> ¶m_str_map)
{
for (auto p = cmdmap.begin();
p != cmdmap.end(); ++p) {
if (p->first == "prefix")
continue;
if (p->first == "caps") {
vector<string> cv;
if (cmd_getval(cmdmap, "caps", cv) &&
cv.size() % 2 == 0) {
for (unsigned i = 0; i < cv.size(); i += 2) {
string k = string("caps_") + cv[i];
param_str_map[k] = cv[i + 1];
}
continue;
}
}
param_str_map[p->first] = cmd_vartype_stringify(p->second);
}
}
const MonCommand *DaemonServer::_get_mgrcommand(
const string &cmd_prefix,
const std::vector<MonCommand> &cmds)
{
const MonCommand *this_cmd = nullptr;
for (const auto &cmd : cmds) {
if (cmd.cmdstring.compare(0, cmd_prefix.size(), cmd_prefix) == 0) {
this_cmd = &cmd;
break;
}
}
return this_cmd;
}
bool DaemonServer::_allowed_command(
MgrSession *s,
const string &service,
const string &module,
const string &prefix,
const cmdmap_t& cmdmap,
const map<string,string>& param_str_map,
const MonCommand *this_cmd) {
if (s->entity_name.is_mon()) {
// mon is all-powerful. even when it is forwarding commands on behalf of
// old clients; we expect the mon is validating commands before proxying!
return true;
}
bool cmd_r = this_cmd->requires_perm('r');
bool cmd_w = this_cmd->requires_perm('w');
bool cmd_x = this_cmd->requires_perm('x');
bool capable = s->caps.is_capable(
g_ceph_context,
s->entity_name,
service, module, prefix, param_str_map,
cmd_r, cmd_w, cmd_x,
s->get_peer_addr());
dout(10) << " " << s->entity_name << " "
<< (capable ? "" : "not ") << "capable" << dendl;
return capable;
}
/**
* The working data for processing an MCommand. This lives in
* a class to enable passing it into other threads for processing
* outside of the thread/locks that called handle_command.
*/
class CommandContext {
public:
ceph::ref_t<MCommand> m_tell;
ceph::ref_t<MMgrCommand> m_mgr;
const std::vector<std::string>& cmd; ///< ref into m_tell or m_mgr
const bufferlist& data; ///< ref into m_tell or m_mgr
bufferlist odata;
cmdmap_t cmdmap;
explicit CommandContext(ceph::ref_t<MCommand> m)
: m_tell{std::move(m)},
cmd(m_tell->cmd),
data(m_tell->get_data()) {
}
explicit CommandContext(ceph::ref_t<MMgrCommand> m)
: m_mgr{std::move(m)},
cmd(m_mgr->cmd),
data(m_mgr->get_data()) {
}
void reply(int r, const std::stringstream &ss) {
reply(r, ss.str());
}
void reply(int r, const std::string &rs) {
// Let the connection drop as soon as we've sent our response
ConnectionRef con = m_tell ? m_tell->get_connection()
: m_mgr->get_connection();
if (con) {
con->mark_disposable();
}
if (r == 0) {
dout(20) << "success" << dendl;
} else {
derr << __func__ << " " << cpp_strerror(r) << " " << rs << dendl;
}
if (con) {
if (m_tell) {
MCommandReply *reply = new MCommandReply(r, rs);
reply->set_tid(m_tell->get_tid());
reply->set_data(odata);
con->send_message(reply);
} else {
MMgrCommandReply *reply = new MMgrCommandReply(r, rs);
reply->set_tid(m_mgr->get_tid());
reply->set_data(odata);
con->send_message(reply);
}
}
}
};
/**
* A context for receiving a bufferlist/error string from a background
* function and then calling back to a CommandContext when it's done
*/
class ReplyOnFinish : public Context {
std::shared_ptr<CommandContext> cmdctx;
public:
bufferlist from_mon;
string outs;
explicit ReplyOnFinish(const std::shared_ptr<CommandContext> &cmdctx_)
: cmdctx(cmdctx_)
{}
void finish(int r) override {
cmdctx->odata.claim_append(from_mon);
cmdctx->reply(r, outs);
}
};
bool DaemonServer::handle_command(const ref_t<MCommand>& m)
{
std::lock_guard l(lock);
auto cmdctx = std::make_shared<CommandContext>(m);
try {
return _handle_command(cmdctx);
} catch (const bad_cmd_get& e) {
cmdctx->reply(-EINVAL, e.what());
return true;
}
}
bool DaemonServer::handle_command(const ref_t<MMgrCommand>& m)
{
std::lock_guard l(lock);
auto cmdctx = std::make_shared<CommandContext>(m);
try {
return _handle_command(cmdctx);
} catch (const bad_cmd_get& e) {
cmdctx->reply(-EINVAL, e.what());
return true;
}
}
void DaemonServer::log_access_denied(
std::shared_ptr<CommandContext>& cmdctx,
MgrSession* session, std::stringstream& ss) {
dout(1) << " access denied" << dendl;
audit_clog->info() << "from='" << session->inst << "' "
<< "entity='" << session->entity_name << "' "
<< "cmd=" << cmdctx->cmd << ": access denied";
ss << "access denied: does your client key have mgr caps? "
"See http://docs.ceph.com/en/latest/mgr/administrator/"
"#client-authentication";
}
void DaemonServer::_check_offlines_pgs(
const set<int>& osds,
const OSDMap& osdmap,
const PGMap& pgmap,
offline_pg_report *report)
{
// reset output
*report = offline_pg_report();
report->osds = osds;
for (const auto& q : pgmap.pg_stat) {
set<int32_t> pg_acting; // net acting sets (with no missing if degraded)
bool found = false;
if (q.second.state == 0) {
report->unknown.insert(q.first);
continue;
}
if (q.second.state & PG_STATE_DEGRADED) {
for (auto& anm : q.second.avail_no_missing) {
if (osds.count(anm.osd)) {
found = true;
continue;
}
if (anm.osd != CRUSH_ITEM_NONE) {
pg_acting.insert(anm.osd);
}
}
} else {
for (auto& a : q.second.acting) {
if (osds.count(a)) {
found = true;
continue;
}
if (a != CRUSH_ITEM_NONE) {
pg_acting.insert(a);
}
}
}
if (!found) {
continue;
}
const pg_pool_t *pi = osdmap.get_pg_pool(q.first.pool());
bool dangerous = false;
if (!pi) {
report->bad_no_pool.insert(q.first); // pool is creating or deleting
dangerous = true;
}
if (!(q.second.state & PG_STATE_ACTIVE)) {
report->bad_already_inactive.insert(q.first);
dangerous = true;
}
if (pg_acting.size() < pi->min_size) {
report->bad_become_inactive.insert(q.first);
dangerous = true;
}
if (dangerous) {
report->not_ok.insert(q.first);
} else {
report->ok.insert(q.first);
if (q.second.state & PG_STATE_DEGRADED) {
report->ok_become_more_degraded.insert(q.first);
} else {
report->ok_become_degraded.insert(q.first);
}
}
}
dout(20) << osds << " -> " << report->ok.size() << " ok, "
<< report->not_ok.size() << " not ok, "
<< report->unknown.size() << " unknown"
<< dendl;
}
void DaemonServer::_maximize_ok_to_stop_set(
const set<int>& orig_osds,
unsigned max,
const OSDMap& osdmap,
const PGMap& pgmap,
offline_pg_report *out_report)
{
dout(20) << "orig_osds " << orig_osds << " max " << max << dendl;
_check_offlines_pgs(orig_osds, osdmap, pgmap, out_report);
if (!out_report->ok_to_stop()) {
return;
}
if (orig_osds.size() >= max) {
// already at max
return;
}
// semi-arbitrarily start with the first osd in the set
offline_pg_report report;
set<int> osds = orig_osds;
int parent = *osds.begin();
set<int> children;
while (true) {
// identify the next parent
int r = osdmap.crush->get_immediate_parent_id(parent, &parent);
if (r < 0) {
return; // just go with what we have so far!
}
// get candidate additions that are beneath this point in the tree
children.clear();
r = osdmap.crush->get_all_children(parent, &children);
if (r < 0) {
return; // just go with what we have so far!
}
dout(20) << " parent " << parent << " children " << children << dendl;
// try adding in more osds
int failed = 0; // how many children we failed to add to our set
for (auto o : children) {
if (o >= 0 && osdmap.is_up(o) && osds.count(o) == 0) {
osds.insert(o);
_check_offlines_pgs(osds, osdmap, pgmap, &report);
if (!report.ok_to_stop()) {
osds.erase(o);
++failed;
continue;
}
*out_report = report;
if (osds.size() == max) {
dout(20) << " hit max" << dendl;
return; // yay, we hit the max
}
}
}
if (failed) {