/
OSDMonitor.cc
4721 lines (4230 loc) · 143 KB
/
OSDMonitor.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) 2004-2006 Sage Weil <sage@newdream.net>
*
* 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 <sstream>
#include "OSDMonitor.h"
#include "Monitor.h"
#include "MDSMonitor.h"
#include "PGMonitor.h"
#include "MonitorDBStore.h"
#include "crush/CrushWrapper.h"
#include "crush/CrushTester.h"
#include "messages/MOSDFailure.h"
#include "messages/MOSDMarkMeDown.h"
#include "messages/MOSDMap.h"
#include "messages/MOSDBoot.h"
#include "messages/MOSDAlive.h"
#include "messages/MPoolOp.h"
#include "messages/MPoolOpReply.h"
#include "messages/MOSDPGTemp.h"
#include "messages/MMonCommand.h"
#include "messages/MRemoveSnaps.h"
#include "messages/MOSDScrub.h"
#include "common/Timer.h"
#include "common/ceph_argparse.h"
#include "common/perf_counters.h"
#include "common/strtol.h"
#include "common/config.h"
#include "common/errno.h"
#include "include/compat.h"
#include "include/assert.h"
#include "include/stringify.h"
#include "include/util.h"
#include "common/cmdparse.h"
#include "include/str_list.h"
#include "include/str_map.h"
#define dout_subsys ceph_subsys_mon
#undef dout_prefix
#define dout_prefix _prefix(_dout, mon, osdmap)
static ostream& _prefix(std::ostream *_dout, Monitor *mon, OSDMap& osdmap) {
return *_dout << "mon." << mon->name << "@" << mon->rank
<< "(" << mon->get_state_name()
<< ").osd e" << osdmap.get_epoch() << " ";
}
bool OSDMonitor::_have_pending_crush()
{
return pending_inc.crush.length();
}
CrushWrapper &OSDMonitor::_get_stable_crush()
{
return *osdmap.crush;
}
void OSDMonitor::_get_pending_crush(CrushWrapper& newcrush)
{
bufferlist bl;
if (pending_inc.crush.length())
bl = pending_inc.crush;
else
osdmap.crush->encode(bl);
bufferlist::iterator p = bl.begin();
newcrush.decode(p);
}
void OSDMonitor::create_initial()
{
dout(10) << "create_initial for " << mon->monmap->fsid << dendl;
OSDMap newmap;
bufferlist bl;
mon->store->get("mkfs", "osdmap", bl);
if (bl.length()) {
newmap.decode(bl);
newmap.set_fsid(mon->monmap->fsid);
} else {
newmap.build_simple(g_ceph_context, 0, mon->monmap->fsid, 0,
g_conf->osd_pg_bits, g_conf->osd_pgp_bits);
}
newmap.set_epoch(1);
newmap.created = newmap.modified = ceph_clock_now(g_ceph_context);
// encode into pending incremental
newmap.encode(pending_inc.fullmap);
}
void OSDMonitor::update_from_paxos(bool *need_bootstrap)
{
version_t version = get_last_committed();
if (version == osdmap.epoch)
return;
assert(version >= osdmap.epoch);
dout(15) << "update_from_paxos paxos e " << version
<< ", my e " << osdmap.epoch << dendl;
/*
* We will possibly have a stashed latest that *we* wrote, and we will
* always be sure to have the oldest full map in the first..last range
* due to encode_trim_extra(), which includes the oldest full map in the trim
* transaction.
*
* encode_trim_extra() does not however write the full map's
* version to 'full_latest'. This is only done when we are building the
* full maps from the incremental versions. But don't panic! We make sure
* that the following conditions find whichever full map version is newer.
*/
version_t latest_full = get_version_latest_full();
if (latest_full == 0 && get_first_committed() > 1)
latest_full = get_first_committed();
if (latest_full > 0) {
// make sure we can really believe get_version_latest_full(); see
// 76cd7ac1c2094b34ad36bea89b2246fa90eb2f6d
bufferlist test;
get_version_full(latest_full, test);
if (test.length() == 0) {
dout(10) << __func__ << " ignoring recorded latest_full as it is missing; fallback to search" << dendl;
latest_full = 0;
}
}
if (get_first_committed() > 1 &&
latest_full < get_first_committed()) {
/* a bug introduced in 7fb3804fb860dcd0340dd3f7c39eec4315f8e4b6 would lead
* us to not update the on-disk latest_full key. Upon trim, the actual
* version would cease to exist but we would still point to it. This
* makes sure we get it pointing to a proper version.
*/
version_t lc = get_last_committed();
version_t fc = get_first_committed();
dout(10) << __func__ << " looking for valid full map in interval"
<< " [" << fc << ", " << lc << "]" << dendl;
latest_full = 0;
for (version_t v = lc; v >= fc; v--) {
string full_key = "full_" + stringify(v);
if (mon->store->exists(get_service_name(), full_key)) {
dout(10) << __func__ << " found latest full map v " << v << dendl;
latest_full = v;
break;
}
}
// if we trigger this, then there's something else going with the store
// state, and we shouldn't want to work around it without knowing what
// exactly happened.
assert(latest_full > 0);
MonitorDBStore::Transaction t;
put_version_latest_full(&t, latest_full);
mon->store->apply_transaction(t);
dout(10) << __func__ << " updated the on-disk full map version to "
<< latest_full << dendl;
}
if ((latest_full > 0) && (latest_full > osdmap.epoch)) {
bufferlist latest_bl;
get_version_full(latest_full, latest_bl);
assert(latest_bl.length() != 0);
dout(7) << __func__ << " loading latest full map e" << latest_full << dendl;
osdmap.decode(latest_bl);
}
// walk through incrementals
MonitorDBStore::Transaction *t = NULL;
size_t tx_size = 0;
while (version > osdmap.epoch) {
bufferlist inc_bl;
int err = get_version(osdmap.epoch+1, inc_bl);
assert(err == 0);
assert(inc_bl.length());
dout(7) << "update_from_paxos applying incremental " << osdmap.epoch+1 << dendl;
OSDMap::Incremental inc(inc_bl);
err = osdmap.apply_incremental(inc);
assert(err == 0);
if (t == NULL)
t = new MonitorDBStore::Transaction;
// write out the full map for all past epochs
bufferlist full_bl;
osdmap.encode(full_bl);
tx_size += full_bl.length();
put_version_full(t, osdmap.epoch, full_bl);
put_version_latest_full(t, osdmap.epoch);
// share
dout(1) << osdmap << dendl;
if (osdmap.epoch == 1) {
t->erase("mkfs", "osdmap");
}
if (tx_size > g_conf->mon_sync_max_payload_size*2) {
mon->store->apply_transaction(*t);
delete t;
t = NULL;
tx_size = 0;
}
}
if (t != NULL) {
mon->store->apply_transaction(*t);
delete t;
}
for (int o = 0; o < osdmap.get_max_osd(); o++) {
if (osdmap.is_down(o)) {
// invalidate osd_epoch cache
osd_epoch.erase(o);
// populate down -> out map
if (osdmap.is_in(o) &&
down_pending_out.count(o) == 0) {
dout(10) << " adding osd." << o << " to down_pending_out map" << dendl;
down_pending_out[o] = ceph_clock_now(g_ceph_context);
}
}
}
// blow away any osd_epoch items beyond max_osd
map<int,epoch_t>::iterator p = osd_epoch.upper_bound(osdmap.get_max_osd());
while (p != osd_epoch.end()) {
osd_epoch.erase(p++);
}
if (mon->is_leader()) {
// kick pgmon, make sure it's seen the latest map
mon->pgmon()->check_osd_map(osdmap.epoch);
}
check_subs();
share_map_with_random_osd();
update_logger();
process_failures();
// make sure our feature bits reflect the latest map
update_msgr_features();
}
void OSDMonitor::update_msgr_features()
{
uint64_t mask;
uint64_t features = osdmap.get_features(&mask);
set<int> types;
types.insert((int)entity_name_t::TYPE_OSD);
types.insert((int)entity_name_t::TYPE_CLIENT);
types.insert((int)entity_name_t::TYPE_MDS);
types.insert((int)entity_name_t::TYPE_MON);
for (set<int>::iterator q = types.begin(); q != types.end(); ++q) {
if ((mon->messenger->get_policy(*q).features_required & mask) != features) {
dout(0) << "crush map has features " << features << ", adjusting msgr requires" << dendl;
Messenger::Policy p = mon->messenger->get_policy(*q);
p.features_required = (p.features_required & ~mask) | features;
mon->messenger->set_policy(*q, p);
}
}
}
bool OSDMonitor::thrash()
{
if (!thrash_map)
return false;
thrash_map--;
int o;
// mark a random osd up_thru..
if (rand() % 4 == 0 || thrash_last_up_osd < 0)
o = rand() % osdmap.get_num_osds();
else
o = thrash_last_up_osd;
if (osdmap.is_up(o)) {
dout(5) << "thrash_map osd." << o << " up_thru" << dendl;
pending_inc.new_up_thru[o] = osdmap.get_epoch();
}
// mark a random osd up/down
o = rand() % osdmap.get_num_osds();
if (osdmap.is_up(o)) {
dout(5) << "thrash_map osd." << o << " down" << dendl;
pending_inc.new_state[o] = CEPH_OSD_UP;
} else if (osdmap.exists(o)) {
dout(5) << "thrash_map osd." << o << " up" << dendl;
pending_inc.new_state[o] = CEPH_OSD_UP;
pending_inc.new_up_client[o] = entity_addr_t();
pending_inc.new_up_cluster[o] = entity_addr_t();
pending_inc.new_hb_back_up[o] = entity_addr_t();
pending_inc.new_weight[o] = CEPH_OSD_IN;
thrash_last_up_osd = o;
}
// mark a random osd in
o = rand() % osdmap.get_num_osds();
if (osdmap.exists(o)) {
dout(5) << "thrash_map osd." << o << " in" << dendl;
pending_inc.new_weight[o] = CEPH_OSD_IN;
}
// mark a random osd out
o = rand() % osdmap.get_num_osds();
if (osdmap.exists(o)) {
dout(5) << "thrash_map osd." << o << " out" << dendl;
pending_inc.new_weight[o] = CEPH_OSD_OUT;
}
// generate some pg_temp entries.
// let's assume the hash_map iterates in a random-ish order.
int n = rand() % mon->pgmon()->pg_map.pg_stat.size();
hash_map<pg_t,pg_stat_t>::iterator p = mon->pgmon()->pg_map.pg_stat.begin();
hash_map<pg_t,pg_stat_t>::iterator e = mon->pgmon()->pg_map.pg_stat.end();
while (n--)
++p;
for (int i=0; i<50; i++) {
vector<int> v;
for (int j=0; j<3; j++) {
o = rand() % osdmap.get_num_osds();
if (osdmap.exists(o) && std::find(v.begin(), v.end(), o) == v.end())
v.push_back(o);
}
if (v.size() < 3) {
for (vector<int>::iterator q = p->second.acting.begin(); q != p->second.acting.end(); ++q)
if (std::find(v.begin(), v.end(), *q) == v.end())
v.push_back(*q);
}
if (!v.empty())
pending_inc.new_pg_temp[p->first] = v;
dout(5) << "thrash_map pg " << p->first << " pg_temp remapped to " << v << dendl;
++p;
if (p == e)
p = mon->pgmon()->pg_map.pg_stat.begin();
}
return true;
}
void OSDMonitor::on_active()
{
update_logger();
if (thrash_map) {
if (mon->is_leader()) {
if (thrash())
propose_pending();
} else {
thrash_map = 0;
}
}
if (mon->is_leader())
mon->clog.info() << "osdmap " << osdmap << "\n";
if (!mon->is_leader()) {
list<MOSDFailure*> ls;
take_all_failures(ls);
while (!ls.empty()) {
dispatch(ls.front());
ls.pop_front();
}
}
}
void OSDMonitor::on_shutdown()
{
dout(10) << __func__ << dendl;
// discard failure info, waiters
list<MOSDFailure*> ls;
take_all_failures(ls);
while (!ls.empty()) {
ls.front()->put();
ls.pop_front();
}
}
void OSDMonitor::update_logger()
{
dout(10) << "update_logger" << dendl;
mon->cluster_logger->set(l_cluster_num_osd, osdmap.get_num_osds());
mon->cluster_logger->set(l_cluster_num_osd_up, osdmap.get_num_up_osds());
mon->cluster_logger->set(l_cluster_num_osd_in, osdmap.get_num_in_osds());
mon->cluster_logger->set(l_cluster_osd_epoch, osdmap.get_epoch());
}
void OSDMonitor::remove_redundant_pg_temp()
{
dout(10) << "remove_redundant_pg_temp" << dendl;
for (map<pg_t,vector<int> >::iterator p = osdmap.pg_temp->begin();
p != osdmap.pg_temp->end();
++p) {
if (pending_inc.new_pg_temp.count(p->first) == 0) {
vector<int> raw_up;
osdmap.pg_to_raw_up(p->first, raw_up);
if (raw_up == p->second) {
dout(10) << " removing unnecessary pg_temp " << p->first << " -> " << p->second << dendl;
pending_inc.new_pg_temp[p->first].clear();
}
}
}
}
void OSDMonitor::remove_down_pg_temp()
{
dout(10) << "remove_down_pg_temp" << dendl;
OSDMap tmpmap;
tmpmap.deepish_copy_from(osdmap);
tmpmap.apply_incremental(pending_inc);
for (map<pg_t,vector<int> >::iterator p = tmpmap.pg_temp->begin();
p != tmpmap.pg_temp->end();
++p) {
unsigned num_up = 0;
for (vector<int>::iterator i = p->second.begin();
i != p->second.end();
++i) {
if (!tmpmap.is_down(*i))
++num_up;
}
if (num_up == 0)
pending_inc.new_pg_temp[p->first].clear();
}
}
/* Assign a lower weight to overloaded OSDs.
*
* The osds that will get a lower weight are those with with a utilization
* percentage 'oload' percent greater than the average utilization.
*/
int OSDMonitor::reweight_by_utilization(int oload, std::string& out_str)
{
if (oload <= 100) {
ostringstream oss;
oss << "You must give a percentage higher than 100. "
"The reweighting threshold will be calculated as <average-utilization> "
"times <input-percentage>. For example, an argument of 200 would "
"reweight OSDs which are twice as utilized as the average OSD.\n";
out_str = oss.str();
dout(0) << "reweight_by_utilization: " << out_str << dendl;
return -EINVAL;
}
// Avoid putting a small number (or 0) in the denominator when calculating
// average_util
const PGMap &pgm = mon->pgmon()->pg_map;
if (pgm.osd_sum.kb < 1024) {
ostringstream oss;
oss << "Refusing to reweight: we only have " << pgm.osd_sum << " kb "
"across all osds!\n";
out_str = oss.str();
dout(0) << "reweight_by_utilization: " << out_str << dendl;
return -EDOM;
}
if (pgm.osd_sum.kb_used < 5 * 1024) {
ostringstream oss;
oss << "Refusing to reweight: we only have " << pgm.osd_sum << " kb "
"used across all osds!\n";
out_str = oss.str();
dout(0) << "reweight_by_utilization: " << out_str << dendl;
return -EDOM;
}
float average_util = pgm.osd_sum.kb_used;
average_util /= pgm.osd_sum.kb;
float overload_util = average_util * oload / 100.0;
ostringstream oss;
char buf[128];
snprintf(buf, sizeof(buf), "average_util: %04f, overload_util: %04f. ",
average_util, overload_util);
oss << buf;
std::string sep;
oss << "overloaded osds: ";
bool changed = false;
for (hash_map<int,osd_stat_t>::const_iterator p = pgm.osd_stat.begin();
p != pgm.osd_stat.end();
++p) {
float util = p->second.kb_used;
util /= p->second.kb;
if (util >= overload_util) {
sep = ", ";
// Assign a lower weight to overloaded OSDs. The current weight
// is a factor to take into account the original weights,
// to represent e.g. differing storage capacities
unsigned weight = osdmap.get_weight(p->first);
unsigned new_weight = (unsigned)((average_util / util) * (float)weight);
pending_inc.new_weight[p->first] = new_weight;
char buf[128];
snprintf(buf, sizeof(buf), "%d [%04f -> %04f]", p->first,
(float)weight / (float)0x10000,
(float)new_weight / (float)0x10000);
oss << buf << sep;
changed = true;
}
}
if (sep.empty()) {
oss << "(none)";
}
out_str = oss.str();
dout(0) << "reweight_by_utilization: finished with " << out_str << dendl;
return changed;
}
void OSDMonitor::create_pending()
{
pending_inc = OSDMap::Incremental(osdmap.epoch+1);
pending_inc.fsid = mon->monmap->fsid;
dout(10) << "create_pending e " << pending_inc.epoch << dendl;
// drop any redundant pg_temp entries
remove_redundant_pg_temp();
// drop any pg_temp entries with no up entries
remove_down_pg_temp();
}
/**
* @note receiving a transaction in this function gives a fair amount of
* freedom to the service implementation if it does need it. It shouldn't.
*/
void OSDMonitor::encode_pending(MonitorDBStore::Transaction *t)
{
dout(10) << "encode_pending e " << pending_inc.epoch
<< dendl;
// finalize up pending_inc
pending_inc.modified = ceph_clock_now(g_ceph_context);
int r = pending_inc.propagate_snaps_to_tiers(g_ceph_context, osdmap);
assert(r == 0);
bufferlist bl;
// tell me about it
for (map<int32_t,uint8_t>::iterator i = pending_inc.new_state.begin();
i != pending_inc.new_state.end();
++i) {
int s = i->second ? i->second : CEPH_OSD_UP;
if (s & CEPH_OSD_UP)
dout(2) << " osd." << i->first << " DOWN" << dendl;
if (s & CEPH_OSD_EXISTS)
dout(2) << " osd." << i->first << " DNE" << dendl;
}
for (map<int32_t,entity_addr_t>::iterator i = pending_inc.new_up_client.begin();
i != pending_inc.new_up_client.end();
++i) {
//FIXME: insert cluster addresses too
dout(2) << " osd." << i->first << " UP " << i->second << dendl;
}
for (map<int32_t,uint32_t>::iterator i = pending_inc.new_weight.begin();
i != pending_inc.new_weight.end();
++i) {
if (i->second == CEPH_OSD_OUT) {
dout(2) << " osd." << i->first << " OUT" << dendl;
} else if (i->second == CEPH_OSD_IN) {
dout(2) << " osd." << i->first << " IN" << dendl;
} else {
dout(2) << " osd." << i->first << " WEIGHT " << hex << i->second << dec << dendl;
}
}
// encode
assert(get_last_committed() + 1 == pending_inc.epoch);
::encode(pending_inc, bl, CEPH_FEATURES_ALL);
/* put everything in the transaction */
put_version(t, pending_inc.epoch, bl);
put_last_committed(t, pending_inc.epoch);
// metadata, too!
for (map<int,bufferlist>::iterator p = pending_metadata.begin();
p != pending_metadata.end();
++p)
t->put(OSD_METADATA_PREFIX, stringify(p->first), p->second);
for (set<int>::iterator p = pending_metadata_rm.begin();
p != pending_metadata_rm.end();
++p)
t->erase(OSD_METADATA_PREFIX, stringify(*p));
pending_metadata.clear();
pending_metadata_rm.clear();
}
int OSDMonitor::dump_osd_metadata(int osd, Formatter *f, ostream *err)
{
bufferlist bl;
int r = mon->store->get(OSD_METADATA_PREFIX, stringify(osd), bl);
if (r < 0)
return r;
map<string,string> m;
try {
bufferlist::iterator p = bl.begin();
::decode(m, p);
}
catch (buffer::error& e) {
if (err)
*err << "osd." << osd << " metadata is corrupt";
return -EIO;
}
for (map<string,string>::iterator p = m.begin(); p != m.end(); ++p)
f->dump_string(p->first.c_str(), p->second);
return 0;
}
void OSDMonitor::share_map_with_random_osd()
{
if (osdmap.get_num_up_osds() == 0) {
dout(10) << __func__ << " no up osds, don't share with anyone" << dendl;
return;
}
MonSession *s = mon->session_map.get_random_osd_session(&osdmap);
if (!s) {
dout(10) << __func__ << " no up osd on our session map" << dendl;
return;
}
dout(10) << "committed, telling random " << s->inst << " all about it" << dendl;
// whatev, they'll request more if they need it
MOSDMap *m = build_incremental(osdmap.get_epoch() - 1, osdmap.get_epoch());
mon->messenger->send_message(m, s->inst);
}
version_t OSDMonitor::get_trim_to()
{
if (mon->pgmon()->is_readable() &&
mon->pgmon()->pg_map.creating_pgs.empty()) {
epoch_t floor = mon->pgmon()->pg_map.calc_min_last_epoch_clean();
dout(10) << " min_last_epoch_clean " << floor << dendl;
if (g_conf->mon_osd_force_trim_to > 0 &&
g_conf->mon_osd_force_trim_to < (int)get_last_committed()) {
floor = g_conf->mon_osd_force_trim_to;
dout(10) << " explicit mon_osd_force_trim_to = " << floor << dendl;
}
unsigned min = g_conf->mon_min_osdmap_epochs;
if (floor + min > get_last_committed()) {
if (min < get_last_committed())
floor = get_last_committed() - min;
else
floor = 0;
}
if (floor > get_first_committed())
return floor;
}
return 0;
}
void OSDMonitor::encode_trim_extra(MonitorDBStore::Transaction *tx, version_t first)
{
dout(10) << __func__ << " including full map for e " << first << dendl;
bufferlist bl;
get_version_full(first, bl);
put_version_full(tx, first, bl);
}
// -------------
bool OSDMonitor::preprocess_query(PaxosServiceMessage *m)
{
dout(10) << "preprocess_query " << *m << " from " << m->get_orig_source_inst() << dendl;
switch (m->get_type()) {
// READs
case MSG_MON_COMMAND:
return preprocess_command(static_cast<MMonCommand*>(m));
// damp updates
case MSG_OSD_MARK_ME_DOWN:
return preprocess_mark_me_down(static_cast<MOSDMarkMeDown*>(m));
case MSG_OSD_FAILURE:
return preprocess_failure(static_cast<MOSDFailure*>(m));
case MSG_OSD_BOOT:
return preprocess_boot(static_cast<MOSDBoot*>(m));
case MSG_OSD_ALIVE:
return preprocess_alive(static_cast<MOSDAlive*>(m));
case MSG_OSD_PGTEMP:
return preprocess_pgtemp(static_cast<MOSDPGTemp*>(m));
case CEPH_MSG_POOLOP:
return preprocess_pool_op(static_cast<MPoolOp*>(m));
case MSG_REMOVE_SNAPS:
return preprocess_remove_snaps(static_cast<MRemoveSnaps*>(m));
default:
assert(0);
m->put();
return true;
}
}
bool OSDMonitor::prepare_update(PaxosServiceMessage *m)
{
dout(7) << "prepare_update " << *m << " from " << m->get_orig_source_inst() << dendl;
switch (m->get_type()) {
// damp updates
case MSG_OSD_MARK_ME_DOWN:
return prepare_mark_me_down(static_cast<MOSDMarkMeDown*>(m));
case MSG_OSD_FAILURE:
return prepare_failure(static_cast<MOSDFailure*>(m));
case MSG_OSD_BOOT:
return prepare_boot(static_cast<MOSDBoot*>(m));
case MSG_OSD_ALIVE:
return prepare_alive(static_cast<MOSDAlive*>(m));
case MSG_OSD_PGTEMP:
return prepare_pgtemp(static_cast<MOSDPGTemp*>(m));
case MSG_MON_COMMAND:
return prepare_command(static_cast<MMonCommand*>(m));
case CEPH_MSG_POOLOP:
return prepare_pool_op(static_cast<MPoolOp*>(m));
case MSG_REMOVE_SNAPS:
return prepare_remove_snaps(static_cast<MRemoveSnaps*>(m));
default:
assert(0);
m->put();
}
return false;
}
bool OSDMonitor::should_propose(double& delay)
{
dout(10) << "should_propose" << dendl;
// if full map, propose immediately! any subsequent changes will be clobbered.
if (pending_inc.fullmap.length())
return true;
// adjust osd weights?
if (osd_weight.size() == (unsigned)osdmap.get_max_osd()) {
dout(0) << " adjusting osd weights based on " << osd_weight << dendl;
osdmap.adjust_osd_weights(osd_weight, pending_inc);
delay = 0.0;
osd_weight.clear();
return true;
}
return PaxosService::should_propose(delay);
}
// ---------------------------
// READs
// ---------------------------
// UPDATEs
// failure --
bool OSDMonitor::check_source(PaxosServiceMessage *m, uuid_d fsid) {
// check permissions
MonSession *session = m->get_session();
if (!session)
return true;
if (!session->is_capable("osd", MON_CAP_X)) {
dout(0) << "got MOSDFailure from entity with insufficient caps "
<< session->caps << dendl;
return true;
}
if (fsid != mon->monmap->fsid) {
dout(0) << "check_source: on fsid " << fsid
<< " != " << mon->monmap->fsid << dendl;
return true;
}
return false;
}
bool OSDMonitor::preprocess_failure(MOSDFailure *m)
{
// who is target_osd
int badboy = m->get_target().name.num();
// check permissions
if (check_source(m, m->fsid))
goto didit;
// first, verify the reporting host is valid
if (m->get_orig_source().is_osd()) {
int from = m->get_orig_source().num();
if (!osdmap.exists(from) ||
osdmap.get_addr(from) != m->get_orig_source_inst().addr ||
osdmap.is_down(from)) {
dout(5) << "preprocess_failure from dead osd." << from << ", ignoring" << dendl;
send_incremental(m, m->get_epoch()+1);
goto didit;
}
}
// weird?
if (!osdmap.have_inst(badboy)) {
dout(5) << "preprocess_failure dne(/dup?): " << m->get_target() << ", from " << m->get_orig_source_inst() << dendl;
if (m->get_epoch() < osdmap.get_epoch())
send_incremental(m, m->get_epoch()+1);
goto didit;
}
if (osdmap.get_inst(badboy) != m->get_target()) {
dout(5) << "preprocess_failure wrong osd: report " << m->get_target() << " != map's " << osdmap.get_inst(badboy)
<< ", from " << m->get_orig_source_inst() << dendl;
if (m->get_epoch() < osdmap.get_epoch())
send_incremental(m, m->get_epoch()+1);
goto didit;
}
// already reported?
if (osdmap.is_down(badboy)) {
dout(5) << "preprocess_failure dup: " << m->get_target() << ", from " << m->get_orig_source_inst() << dendl;
if (m->get_epoch() < osdmap.get_epoch())
send_incremental(m, m->get_epoch()+1);
goto didit;
}
if (!can_mark_down(badboy)) {
dout(5) << "preprocess_failure ignoring report of " << m->get_target() << " from " << m->get_orig_source_inst() << dendl;
goto didit;
}
dout(10) << "preprocess_failure new: " << m->get_target() << ", from " << m->get_orig_source_inst() << dendl;
return false;
didit:
m->put();
return true;
}
class C_AckMarkedDown : public Context {
OSDMonitor *osdmon;
MOSDMarkMeDown *m;
public:
C_AckMarkedDown(
OSDMonitor *osdmon,
MOSDMarkMeDown *m)
: osdmon(osdmon), m(m) {}
void finish(int) {
osdmon->mon->send_reply(
m,
new MOSDMarkMeDown(
m->fsid,
m->get_target(),
m->get_epoch(),
m->ack));
}
~C_AckMarkedDown() {
m->put();
}
};
bool OSDMonitor::preprocess_mark_me_down(MOSDMarkMeDown *m)
{
int requesting_down = m->get_target().name.num();
int from = m->get_orig_source().num();
// check permissions
if (check_source(m, m->fsid))
goto reply;
// first, verify the reporting host is valid
if (!m->get_orig_source().is_osd())
goto reply;
if (!osdmap.exists(from) ||
osdmap.is_down(from) ||
osdmap.get_addr(from) != m->get_target().addr) {
dout(5) << "preprocess_mark_me_down from dead osd."
<< from << ", ignoring" << dendl;
send_incremental(m, m->get_epoch()+1);
goto reply;
}
// no down might be set
if (!can_mark_down(requesting_down))
goto reply;
dout(10) << "MOSDMarkMeDown for: " << m->get_target() << dendl;
return false;
reply:
Context *c(new C_AckMarkedDown(this, m));
c->complete(0);
return true;
}
bool OSDMonitor::prepare_mark_me_down(MOSDMarkMeDown *m)
{
int target_osd = m->get_target().name.num();
assert(osdmap.is_up(target_osd));
assert(osdmap.get_addr(target_osd) == m->get_target().addr);
mon->clog.info() << "osd." << target_osd << " marked itself down\n";
pending_inc.new_state[target_osd] = CEPH_OSD_UP;
wait_for_finished_proposal(new C_AckMarkedDown(this, m));
return true;
}
bool OSDMonitor::can_mark_down(int i)
{
if (osdmap.test_flag(CEPH_OSDMAP_NODOWN)) {
dout(5) << "can_mark_down NODOWN flag set, will not mark osd." << i << " down" << dendl;
return false;
}
int up = osdmap.get_num_up_osds() - pending_inc.get_net_marked_down(&osdmap);
float up_ratio = (float)up / (float)osdmap.get_num_osds();
if (up_ratio < g_conf->mon_osd_min_up_ratio) {
dout(5) << "can_mark_down current up_ratio " << up_ratio << " < min "
<< g_conf->mon_osd_min_up_ratio
<< ", will not mark osd." << i << " down" << dendl;
return false;
}
return true;
}
bool OSDMonitor::can_mark_up(int i)
{
if (osdmap.test_flag(CEPH_OSDMAP_NOUP)) {
dout(5) << "can_mark_up NOUP flag set, will not mark osd." << i << " up" << dendl;
return false;
}
return true;
}
/**
* @note the parameter @p i apparently only exists here so we can output the
* osd's id on messages.
*/
bool OSDMonitor::can_mark_out(int i)
{
if (osdmap.test_flag(CEPH_OSDMAP_NOOUT)) {
dout(5) << "can_mark_out NOOUT flag set, will not mark osds out" << dendl;
return false;
}
int in = osdmap.get_num_in_osds() - pending_inc.get_net_marked_out(&osdmap);
float in_ratio = (float)in / (float)osdmap.get_num_osds();
if (in_ratio < g_conf->mon_osd_min_in_ratio) {
if (i >= 0)
dout(5) << "can_mark_down current in_ratio " << in_ratio << " < min "
<< g_conf->mon_osd_min_in_ratio
<< ", will not mark osd." << i << " out" << dendl;
else
dout(5) << "can_mark_down current in_ratio " << in_ratio << " < min "
<< g_conf->mon_osd_min_in_ratio
<< ", will not mark osds out" << dendl;
return false;
}
return true;
}
bool OSDMonitor::can_mark_in(int i)
{
if (osdmap.test_flag(CEPH_OSDMAP_NOIN)) {
dout(5) << "can_mark_in NOIN flag set, will not mark osd." << i << " in" << dendl;
return false;
}
return true;
}
void OSDMonitor::check_failures(utime_t now)
{
for (map<int,failure_info_t>::iterator p = failure_info.begin();
p != failure_info.end();
++p) {