/
OSDMonitor.cc
7048 lines (6392 loc) · 218 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>
* Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
* Copyright (C) 2014 Red Hat <contact@redhat.com>
*
* Author: Loic Dachary <loic@dachary.org>
*
* 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 "crush/CrushTreeDumper.h"
#include "messages/MOSDFailure.h"
#include "messages/MOSDMarkMeDown.h"
#include "messages/MOSDMap.h"
#include "messages/MMonGetOSDMap.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/TextTable.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 "erasure-code/ErasureCodePlugin.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, mon->quorum_features | CEPH_FEATURE_RESERVED);
pending_inc.full_crc = newmap.get_crc();
dout(20) << " full crc " << pending_inc.full_crc << dendl;
}
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::TransactionRef t(new MonitorDBStore::Transaction);
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::TransactionRef t;
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)
t.reset(new MonitorDBStore::Transaction);
// Write out the full map for all past epochs. Encode the full
// map with the same features as the incremental. If we don't
// know, use the quorum features. If we don't know those either,
// encode with all features.
uint64_t f = inc.encode_features;
if (!f)
f = mon->quorum_features;
if (!f)
f = -1;
bufferlist full_bl;
osdmap.encode(full_bl, f | CEPH_FEATURE_RESERVED);
tx_size += full_bl.length();
bufferlist orig_full_bl;
get_version_full(osdmap.epoch, orig_full_bl);
if (orig_full_bl.length()) {
// the primary provided the full map
assert(inc.have_crc);
if (inc.full_crc != osdmap.crc) {
// This will happen if the mons were running mixed versions in
// the past or some other circumstance made the full encoded
// maps divergent. Reloading here will bring us back into
// sync with the primary for this and all future maps. OSDs
// will also be brought back into sync when they discover the
// crc mismatch and request a full map from a mon.
derr << __func__ << " full map CRC mismatch, resetting to canonical"
<< dendl;
osdmap = OSDMap();
osdmap.decode(orig_full_bl);
}
} else {
assert(!inc.have_crc);
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);
t = MonitorDBStore::TransactionRef();
tx_size = 0;
}
}
if (t) {
mon->store->apply_transaction(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++);
}
/** we don't have any of the feature bit infrastructure in place for
* supporting primary_temp mappings without breaking old clients/OSDs.*/
assert(g_conf->mon_osd_allow_primary_temp || osdmap.primary_temp->empty());
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()
{
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) {
uint64_t mask;
uint64_t features = osdmap.get_features(*q, &mask);
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 ceph::unordered_map iterates in a random-ish order.
int n = rand() % mon->pgmon()->pg_map.pg_stat.size();
ceph::unordered_map<pg_t,pg_stat_t>::iterator p = mon->pgmon()->pg_map.pg_stat.begin();
ceph::unordered_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++) {
unsigned size = osdmap.get_pg_size(p->first);
vector<int> v;
bool have_real_osd = false;
for (int j=0; j < (int)size; j++) {
o = rand() % osdmap.get_num_osds();
if (osdmap.exists(o) && std::find(v.begin(), v.end(), o) == v.end()) {
have_real_osd = true;
v.push_back(o);
}
}
for (vector<int>::iterator q = p->second.acting.begin();
q != p->second.acting.end() && v.size() < size;
++q) {
if (std::find(v.begin(), v.end(), *q) == v.end()) {
if (*q != CRUSH_ITEM_NONE)
have_real_osd = true;
v.push_back(*q);
}
}
if (osdmap.pg_is_ec(p->first)) {
while (v.size() < size)
v.push_back(CRUSH_ITEM_NONE);
}
if (!v.empty() && have_real_osd)
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());
}
/* 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,
bool by_pg, const set<int64_t> *pools)
{
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();
return -EINVAL;
}
const PGMap &pgm = mon->pgmon()->pg_map;
vector<int> pgs_by_osd(osdmap.get_max_osd());
// Avoid putting a small number (or 0) in the denominator when calculating
// average_util
double average_util;
if (by_pg) {
// by pg mapping
double weight_sum = 0.0; // sum up the crush weights
unsigned num_pg_copies = 0;
int num_osds = 0;
for (ceph::unordered_map<pg_t,pg_stat_t>::const_iterator p =
pgm.pg_stat.begin();
p != pgm.pg_stat.end();
++p) {
if (pools && pools->count(p->first.pool()) == 0)
continue;
for (vector<int>::const_iterator q = p->second.acting.begin();
q != p->second.acting.end();
++q) {
if (*q >= (int)pgs_by_osd.size())
pgs_by_osd.resize(*q);
if (pgs_by_osd[*q] == 0) {
weight_sum += osdmap.crush->get_item_weightf(*q);
++num_osds;
}
++pgs_by_osd[*q];
++num_pg_copies;
}
}
if (!num_osds || (num_pg_copies / num_osds < g_conf->mon_reweight_min_pgs_per_osd)) {
ostringstream oss;
oss << "Refusing to reweight: we only have " << num_pg_copies
<< " PGs across " << num_osds << " osds!\n";
out_str = oss.str();
return -EDOM;
}
average_util = (double)num_pg_copies / weight_sum;
} else {
// by osd utilization
int num_osd = MIN(1, pgm.osd_stat.size());
if ((uint64_t)pgm.osd_sum.kb * 1024 / num_osd
< g_conf->mon_reweight_min_bytes_per_osd) {
ostringstream oss;
oss << "Refusing to reweight: we only have " << pgm.osd_sum.kb
<< " kb across all osds!\n";
out_str = oss.str();
return -EDOM;
}
if ((uint64_t)pgm.osd_sum.kb_used * 1024 / num_osd
< g_conf->mon_reweight_min_bytes_per_osd) {
ostringstream oss;
oss << "Refusing to reweight: we only have " << pgm.osd_sum.kb_used
<< " kb used across all osds!\n";
out_str = oss.str();
return -EDOM;
}
average_util = (double)pgm.osd_sum.kb_used / (double)pgm.osd_sum.kb;
}
// adjust down only if we are above the threshold
double overload_util = average_util * (double)oload / 100.0;
// but aggressively adjust weights up whenever possible.
double underload_util = average_util;
ostringstream oss;
char buf[128];
snprintf(buf, sizeof(buf), "average %04f, overload %04f. ",
average_util, overload_util);
oss << buf;
std::string sep;
oss << "reweighted: ";
bool changed = false;
for (ceph::unordered_map<int,osd_stat_t>::const_iterator p =
pgm.osd_stat.begin();
p != pgm.osd_stat.end();
++p) {
float util;
if (by_pg) {
util = pgs_by_osd[p->first] / osdmap.crush->get_item_weightf(p->first);
} else {
util = (double)p->second.kb_used / (double)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), "osd.%d [%04f -> %04f]", p->first,
(float)weight / (float)0x10000,
(float)new_weight / (float)0x10000);
oss << buf << sep;
changed = true;
}
if (util <= underload_util) {
// assign a higher weight.. if we can.
unsigned weight = osdmap.get_weight(p->first);
unsigned new_weight = (unsigned)((average_util / util) * (float)weight);
if (new_weight > 0x10000)
new_weight = 0x10000;
if (new_weight > weight) {
sep = ", ";
pending_inc.new_weight[p->first] = new_weight;
char buf[128];
snprintf(buf, sizeof(buf), "osd.%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(10) << "reweight_by_utilization: finished with " << out_str << dendl;
return changed;
}
template <typename F>
class OSDUtilizationDumper : public CrushTreeDumper::Dumper<F> {
public:
typedef CrushTreeDumper::Dumper<F> Parent;
OSDUtilizationDumper(const CrushWrapper *crush, const OSDMap *osdmap_,
const PGMap *pgm_, bool tree_) :
Parent(crush),
osdmap(osdmap_),
pgm(pgm_),
tree(tree_),
average_util(0),
min_var(-1),
max_var(-1),
stddev(0),
sum(0) {
if (pgm->osd_sum.kb)
average_util = 100.0 * (double)pgm->osd_sum.kb_used / (double)pgm->osd_sum.kb;
}
protected:
void dump_stray(F *f) {
for (int i = 0; i <= osdmap->get_max_osd(); i++) {
if (osdmap->exists(i) && !this->is_touched(i))
dump_item(CrushTreeDumper::Item(i, 0, 0), f);
}
}
virtual void dump_item(const CrushTreeDumper::Item &qi, F *f) {
if (!tree && qi.is_bucket())
return;
float reweight = qi.is_bucket() ? -1 : osdmap->get_weightf(qi.id);
int64_t kb = 0, kb_used = 0, kb_avail = 0;
double util = 0;
if (get_bucket_utilization(qi.id, kb, kb_used, kb_avail) && kb > 0)
util = 100.0 * (double)kb_used / (double)kb;
double var = 1.0;
if (average_util)
var = util / average_util;
dump_item(qi, reweight, kb, kb_used, kb_avail, util, var, f);
if (!qi.is_bucket()) {
if (min_var < 0 || var < min_var) min_var = var;
if (max_var < 0 || var > max_var) max_var = var;
double dev = util - average_util;
dev *= dev;
stddev += reweight * dev;
sum += reweight;
}
}
virtual void dump_item(const CrushTreeDumper::Item &qi, float &reweight,
int64_t kb, int64_t kb_used, int64_t kb_avail,
double& util, double& var, F *f) = 0;
double dev() {
return sum > 0 ? sqrt(stddev / sum) : 0;
}
bool get_bucket_utilization(int id, int64_t& kb, int64_t& kb_used,
int64_t& kb_avail) const {
if (id >= 0) {
typedef ceph::unordered_map<int32_t,osd_stat_t> OsdStat;
OsdStat::const_iterator p = pgm->osd_stat.find(id);
if (p == pgm->osd_stat.end())
return false;
kb = p->second.kb;
kb_used = p->second.kb_used;
kb_avail = p->second.kb_avail;
return kb > 0;
}
kb = 0;
kb_used = 0;
kb_avail = 0;
for (int k = osdmap->crush->get_bucket_size(id) - 1; k >= 0; k--) {
int item = osdmap->crush->get_bucket_item(id, k);
int64_t kb_i = 0, kb_used_i = 0, kb_avail_i;
if (!get_bucket_utilization(item, kb_i, kb_used_i, kb_avail_i))
return false;
kb += kb_i;
kb_used += kb_used_i;
kb_avail += kb_avail_i;
}
return kb > 0;
}
protected:
const OSDMap *osdmap;
const PGMap *pgm;
bool tree;
double average_util;
double min_var;
double max_var;
double stddev;
double sum;
};
class OSDUtilizationPlainDumper : public OSDUtilizationDumper<TextTable> {
public:
typedef OSDUtilizationDumper<TextTable> Parent;
OSDUtilizationPlainDumper(const CrushWrapper *crush, const OSDMap *osdmap,
const PGMap *pgm, bool tree) :
Parent(crush, osdmap, pgm, tree) {}
void dump(TextTable *tbl) {
tbl->define_column("ID", TextTable::LEFT, TextTable::RIGHT);
tbl->define_column("WEIGHT", TextTable::LEFT, TextTable::RIGHT);
tbl->define_column("REWEIGHT", TextTable::LEFT, TextTable::RIGHT);
tbl->define_column("SIZE", TextTable::LEFT, TextTable::RIGHT);
tbl->define_column("USE", TextTable::LEFT, TextTable::RIGHT);
tbl->define_column("AVAIL", TextTable::LEFT, TextTable::RIGHT);
tbl->define_column("%USE", TextTable::LEFT, TextTable::RIGHT);
tbl->define_column("VAR", TextTable::LEFT, TextTable::RIGHT);
if (tree)
tbl->define_column("TYPE NAME", TextTable::LEFT, TextTable::LEFT);
Parent::dump(tbl);
dump_stray(tbl);
*tbl << "" << "" << "TOTAL"
<< si_t(pgm->osd_sum.kb << 10)
<< si_t(pgm->osd_sum.kb_used << 10)
<< si_t(pgm->osd_sum.kb_avail << 10)
<< lowprecision_t(average_util)
<< ""
<< TextTable::endrow;
}
protected:
struct lowprecision_t {
float v;
lowprecision_t(float _v) : v(_v) {}
};
friend std::ostream &operator<<(ostream& out, const lowprecision_t& v);
virtual void dump_item(const CrushTreeDumper::Item &qi, float &reweight,
int64_t kb, int64_t kb_used, int64_t kb_avail,
double& util, double& var, TextTable *tbl) {
*tbl << qi.id
<< weightf_t(qi.weight)
<< weightf_t(reweight)
<< si_t(kb << 10)
<< si_t(kb_used << 10)
<< si_t(kb_avail << 10)
<< lowprecision_t(util)
<< lowprecision_t(var);
if (tree) {
ostringstream name;
for (int k = 0; k < qi.depth; k++)
name << " ";
if (qi.is_bucket()) {
int type = crush->get_bucket_type(qi.id);
name << crush->get_type_name(type) << " "
<< crush->get_item_name(qi.id);
} else {
name << "osd." << qi.id;
}
*tbl << name.str();
}
*tbl << TextTable::endrow;
}
public:
string summary() {
ostringstream out;
out << "MIN/MAX VAR: " << lowprecision_t(min_var)
<< "/" << lowprecision_t(max_var) << " "
<< "STDDEV: " << lowprecision_t(dev());
return out.str();
}
};
ostream& operator<<(ostream& out,
const OSDUtilizationPlainDumper::lowprecision_t& v)
{
if (v.v < -0.01) {
return out << "-";
} else if (v.v < 0.001) {
return out << "0";
} else {
std::streamsize p = out.precision();
return out << std::fixed << std::setprecision(2) << v.v << std::setprecision(p);
}
}
class OSDUtilizationFormatDumper : public OSDUtilizationDumper<Formatter> {
public:
typedef OSDUtilizationDumper<Formatter> Parent;
OSDUtilizationFormatDumper(const CrushWrapper *crush, const OSDMap *osdmap,
const PGMap *pgm, bool tree) :
Parent(crush, osdmap, pgm, tree) {}
void dump(Formatter *f) {
f->open_array_section("nodes");
Parent::dump(f);
f->close_section();
f->open_array_section("stray");
dump_stray(f);
f->close_section();
}
protected:
virtual void dump_item(const CrushTreeDumper::Item &qi, float &reweight,
int64_t kb, int64_t kb_used, int64_t kb_avail,
double& util, double& var, Formatter *f) {
f->open_object_section("item");
CrushTreeDumper::dump_item_fields(crush, qi, f);
f->dump_float("reweight", reweight);
f->dump_int("kb", kb);
f->dump_int("kb_used", kb_used);
f->dump_int("kb_avail", kb_avail);
f->dump_float("utilization", util);
f->dump_float("var", var);
CrushTreeDumper::dump_bucket_children(crush, qi, f);
f->close_section();
}
public:
void summary(Formatter *f) {
f->open_object_section("summary");
f->dump_int("total_kb", pgm->osd_sum.kb);
f->dump_int("total_kb_used", pgm->osd_sum.kb_used);
f->dump_int("total_kb_avail", pgm->osd_sum.kb_avail);
f->dump_float("average_utilization", average_util);
f->dump_float("min_var", min_var);
f->dump_float("max_var", max_var);
f->dump_float("dev", dev());
f->close_section();
}
};
void OSDMonitor::print_utilization(ostream &out, Formatter *f, bool tree) const
{
const PGMap *pgm = &mon->pgmon()->pg_map;
const CrushWrapper *crush = osdmap.crush.get();
if (f) {
f->open_object_section("df");
OSDUtilizationFormatDumper d(crush, &osdmap, pgm, tree);
d.dump(f);
d.summary(f);
f->close_section();
f->flush(out);
} else {
OSDUtilizationPlainDumper d(crush, &osdmap, pgm, tree);
TextTable tbl;
d.dump(&tbl);
out << tbl
<< d.summary() << "\n";
}
}
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
OSDMap::remove_redundant_temporaries(g_ceph_context, osdmap, &pending_inc);
// drop any pg or primary_temp entries with no up entries
OSDMap::remove_down_temps(g_ceph_context, osdmap, &pending_inc);
}
/**
* @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::TransactionRef 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 full map and determine its crc
OSDMap tmp;
{
tmp.deepish_copy_from(osdmap);
tmp.apply_incremental(pending_inc);
bufferlist fullbl;
::encode(tmp, fullbl, mon->quorum_features | CEPH_FEATURE_RESERVED);
pending_inc.full_crc = tmp.get_crc();
// include full map in the txn. note that old monitors will
// overwrite this. new ones will now skip the local full map
// encode and reload from this.
put_version_full(t, pending_inc.epoch, fullbl);
}
// encode
assert(get_last_committed() + 1 == pending_inc.epoch);
::encode(pending_inc, bl, mon->quorum_features | CEPH_FEATURE_RESERVED);
dout(20) << " full_crc " << tmp.get_crc()
<< " inc_crc " << pending_inc.inc_crc << dendl;
/* 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;