/
OSD.cc
8745 lines (7672 loc) · 252 KB
/
OSD.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 "acconfig.h"
#include <fstream>
#include <iostream>
#include <errno.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <signal.h>
#include <ctype.h>
#include <boost/scoped_ptr.hpp>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#include "osd/PG.h"
#include "include/types.h"
#include "include/compat.h"
#include "OSD.h"
#include "OSDMap.h"
#include "Watch.h"
#include "osdc/Objecter.h"
#include "common/ceph_argparse.h"
#include "common/version.h"
#include "common/io_priority.h"
#include "os/ObjectStore.h"
#include "ReplicatedPG.h"
#include "Ager.h"
#include "msg/Messenger.h"
#include "msg/Message.h"
#include "mon/MonClient.h"
#include "messages/MLog.h"
#include "messages/MGenericMessage.h"
#include "messages/MPing.h"
#include "messages/MOSDPing.h"
#include "messages/MOSDFailure.h"
#include "messages/MOSDMarkMeDown.h"
#include "messages/MOSDOp.h"
#include "messages/MOSDOpReply.h"
#include "messages/MOSDRepOp.h"
#include "messages/MOSDRepOpReply.h"
#include "messages/MOSDSubOp.h"
#include "messages/MOSDSubOpReply.h"
#include "messages/MOSDBoot.h"
#include "messages/MOSDPGTemp.h"
#include "messages/MOSDMap.h"
#include "messages/MMonGetOSDMap.h"
#include "messages/MOSDPGNotify.h"
#include "messages/MOSDPGQuery.h"
#include "messages/MOSDPGLog.h"
#include "messages/MOSDPGRemove.h"
#include "messages/MOSDPGInfo.h"
#include "messages/MOSDPGCreate.h"
#include "messages/MOSDPGTrim.h"
#include "messages/MOSDPGScan.h"
#include "messages/MOSDPGBackfill.h"
#include "messages/MOSDPGMissing.h"
#include "messages/MBackfillReserve.h"
#include "messages/MRecoveryReserve.h"
#include "messages/MOSDECSubOpWrite.h"
#include "messages/MOSDECSubOpWriteReply.h"
#include "messages/MOSDECSubOpRead.h"
#include "messages/MOSDECSubOpReadReply.h"
#include "messages/MOSDAlive.h"
#include "messages/MOSDScrub.h"
#include "messages/MOSDRepScrub.h"
#include "messages/MMonCommand.h"
#include "messages/MCommand.h"
#include "messages/MCommandReply.h"
#include "messages/MPGStats.h"
#include "messages/MPGStatsAck.h"
#include "messages/MWatchNotify.h"
#include "messages/MOSDPGPush.h"
#include "messages/MOSDPGPushReply.h"
#include "messages/MOSDPGPull.h"
#include "common/perf_counters.h"
#include "common/Timer.h"
#include "common/LogClient.h"
#include "common/HeartbeatMap.h"
#include "common/admin_socket.h"
#include "global/signal_handler.h"
#include "global/pidfile.h"
#include "include/color.h"
#include "perfglue/cpu_profiler.h"
#include "perfglue/heap_profiler.h"
#include "osd/ClassHandler.h"
#include "osd/OpRequest.h"
#include "auth/AuthAuthorizeHandler.h"
#include "common/errno.h"
#include "objclass/objclass.h"
#include "common/cmdparse.h"
#include "include/str_list.h"
#include "include/assert.h"
#include "common/config.h"
#ifdef WITH_LTTNG
#include "tracing/osd.h"
#else
#define tracepoint(...)
#endif
static coll_t META_COLL("meta");
#define dout_subsys ceph_subsys_osd
#undef dout_prefix
#define dout_prefix _prefix(_dout, whoami, get_osdmap_epoch())
static ostream& _prefix(std::ostream* _dout, int whoami, epoch_t epoch) {
return *_dout << "osd." << whoami << " " << epoch << " ";
}
//Initial features in new superblock.
//Features here are also automatically upgraded
CompatSet OSD::get_osd_initial_compat_set() {
CompatSet::FeatureSet ceph_osd_feature_compat;
CompatSet::FeatureSet ceph_osd_feature_ro_compat;
CompatSet::FeatureSet ceph_osd_feature_incompat;
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_BASE);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_PGINFO);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_OLOC);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_LEC);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_CATEGORIES);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_HOBJECTPOOL);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_BIGINFO);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBINFO);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_LEVELDBLOG);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_SNAPMAPPER);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_HINTS);
ceph_osd_feature_incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_PGMETA);
return CompatSet(ceph_osd_feature_compat, ceph_osd_feature_ro_compat,
ceph_osd_feature_incompat);
}
//Features are added here that this OSD supports.
CompatSet OSD::get_osd_compat_set() {
CompatSet compat = get_osd_initial_compat_set();
//Any features here can be set in code, but not in initial superblock
compat.incompat.insert(CEPH_OSD_FEATURE_INCOMPAT_SHARDS);
return compat;
}
OSDService::OSDService(OSD *osd) :
osd(osd),
cct(osd->cct),
whoami(osd->whoami), store(osd->store),
log_client(osd->log_client), clog(osd->clog),
pg_recovery_stats(osd->pg_recovery_stats),
cluster_messenger(osd->cluster_messenger),
client_messenger(osd->client_messenger),
logger(osd->logger),
recoverystate_perf(osd->recoverystate_perf),
monc(osd->monc),
op_wq(osd->op_shardedwq),
peering_wq(osd->peering_wq),
recovery_wq(osd->recovery_wq),
snap_trim_wq(osd->snap_trim_wq),
scrub_wq(osd->scrub_wq),
rep_scrub_wq(osd->rep_scrub_wq),
recovery_gen_wq("recovery_gen_wq", cct->_conf->osd_recovery_thread_timeout,
&osd->recovery_tp),
op_gen_wq("op_gen_wq", cct->_conf->osd_recovery_thread_timeout, &osd->osd_tp),
class_handler(osd->class_handler),
pg_epoch_lock("OSDService::pg_epoch_lock"),
publish_lock("OSDService::publish_lock"),
pre_publish_lock("OSDService::pre_publish_lock"),
peer_map_epoch_lock("OSDService::peer_map_epoch_lock"),
sched_scrub_lock("OSDService::sched_scrub_lock"), scrubs_pending(0),
scrubs_active(0),
agent_lock("OSD::agent_lock"),
agent_valid_iterator(false),
agent_ops(0),
agent_active(true),
agent_thread(this),
agent_stop_flag(false),
agent_timer_lock("OSD::agent_timer_lock"),
agent_timer(osd->client_messenger->cct, agent_timer_lock),
objecter(new Objecter(osd->client_messenger->cct, osd->objecter_messenger, osd->monc, NULL, 0, 0)),
objecter_finisher(osd->client_messenger->cct),
watch_lock("OSD::watch_lock"),
watch_timer(osd->client_messenger->cct, watch_lock),
next_notif_id(0),
backfill_request_lock("OSD::backfill_request_lock"),
backfill_request_timer(cct, backfill_request_lock, false),
last_tid(0),
tid_lock("OSDService::tid_lock"),
reserver_finisher(cct),
local_reserver(&reserver_finisher, cct->_conf->osd_max_backfills,
cct->_conf->osd_min_recovery_priority),
remote_reserver(&reserver_finisher, cct->_conf->osd_max_backfills,
cct->_conf->osd_min_recovery_priority),
pg_temp_lock("OSDService::pg_temp_lock"),
map_cache_lock("OSDService::map_lock"),
map_cache(cct, cct->_conf->osd_map_cache_size),
map_bl_cache(cct->_conf->osd_map_cache_size),
map_bl_inc_cache(cct->_conf->osd_map_cache_size),
in_progress_split_lock("OSDService::in_progress_split_lock"),
stat_lock("OSD::stat_lock"),
full_status_lock("OSDService::full_status_lock"),
cur_state(NONE),
last_msg(0),
cur_ratio(0),
epoch_lock("OSDService::epoch_lock"),
boot_epoch(0), up_epoch(0), bind_epoch(0),
is_stopping_lock("OSDService::is_stopping_lock"),
state(NOT_STOPPING)
#ifdef PG_DEBUG_REFS
, pgid_lock("OSDService::pgid_lock")
#endif
{
objecter->init();
}
OSDService::~OSDService()
{
delete objecter;
}
void OSDService::_start_split(spg_t parent, const set<spg_t> &children)
{
for (set<spg_t>::const_iterator i = children.begin();
i != children.end();
++i) {
dout(10) << __func__ << ": Starting split on pg " << *i
<< ", parent=" << parent << dendl;
assert(!pending_splits.count(*i));
assert(!in_progress_splits.count(*i));
pending_splits.insert(make_pair(*i, parent));
assert(!rev_pending_splits[parent].count(*i));
rev_pending_splits[parent].insert(*i);
}
}
void OSDService::mark_split_in_progress(spg_t parent, const set<spg_t> &children)
{
Mutex::Locker l(in_progress_split_lock);
map<spg_t, set<spg_t> >::iterator piter = rev_pending_splits.find(parent);
assert(piter != rev_pending_splits.end());
for (set<spg_t>::const_iterator i = children.begin();
i != children.end();
++i) {
assert(piter->second.count(*i));
assert(pending_splits.count(*i));
assert(!in_progress_splits.count(*i));
assert(pending_splits[*i] == parent);
pending_splits.erase(*i);
piter->second.erase(*i);
in_progress_splits.insert(*i);
}
if (piter->second.empty())
rev_pending_splits.erase(piter);
}
void OSDService::cancel_pending_splits_for_parent(spg_t parent)
{
Mutex::Locker l(in_progress_split_lock);
return _cancel_pending_splits_for_parent(parent);
}
void OSDService::_cancel_pending_splits_for_parent(spg_t parent)
{
map<spg_t, set<spg_t> >::iterator piter = rev_pending_splits.find(parent);
if (piter == rev_pending_splits.end())
return;
for (set<spg_t>::iterator i = piter->second.begin();
i != piter->second.end();
++i) {
assert(pending_splits.count(*i));
assert(!in_progress_splits.count(*i));
pending_splits.erase(*i);
dout(10) << __func__ << ": Completing split on pg " << *i
<< " for parent: " << parent << dendl;
_cancel_pending_splits_for_parent(*i);
}
rev_pending_splits.erase(piter);
}
void OSDService::_maybe_split_pgid(OSDMapRef old_map,
OSDMapRef new_map,
spg_t pgid)
{
assert(old_map->have_pg_pool(pgid.pool()));
if (pgid.ps() < static_cast<unsigned>(old_map->get_pg_num(pgid.pool()))) {
set<spg_t> children;
pgid.is_split(old_map->get_pg_num(pgid.pool()),
new_map->get_pg_num(pgid.pool()), &children);
_start_split(pgid, children);
} else {
assert(pgid.ps() < static_cast<unsigned>(new_map->get_pg_num(pgid.pool())));
}
}
void OSDService::init_splits_between(spg_t pgid,
OSDMapRef frommap,
OSDMapRef tomap)
{
// First, check whether we can avoid this potentially expensive check
if (tomap->have_pg_pool(pgid.pool()) &&
pgid.is_split(
frommap->get_pg_num(pgid.pool()),
tomap->get_pg_num(pgid.pool()),
NULL)) {
// Ok, a split happened, so we need to walk the osdmaps
set<spg_t> new_pgs; // pgs to scan on each map
new_pgs.insert(pgid);
OSDMapRef curmap(get_map(frommap->get_epoch()));
for (epoch_t e = frommap->get_epoch() + 1;
e <= tomap->get_epoch();
++e) {
OSDMapRef nextmap(try_get_map(e));
if (!nextmap)
continue;
set<spg_t> even_newer_pgs; // pgs added in this loop
for (set<spg_t>::iterator i = new_pgs.begin(); i != new_pgs.end(); ++i) {
set<spg_t> split_pgs;
if (i->is_split(curmap->get_pg_num(i->pool()),
nextmap->get_pg_num(i->pool()),
&split_pgs)) {
start_split(*i, split_pgs);
even_newer_pgs.insert(split_pgs.begin(), split_pgs.end());
}
}
new_pgs.insert(even_newer_pgs.begin(), even_newer_pgs.end());
curmap = nextmap;
}
assert(curmap == tomap); // we must have had both frommap and tomap
}
}
void OSDService::expand_pg_num(OSDMapRef old_map,
OSDMapRef new_map)
{
Mutex::Locker l(in_progress_split_lock);
for (set<spg_t>::iterator i = in_progress_splits.begin();
i != in_progress_splits.end();
) {
if (!new_map->have_pg_pool(i->pool())) {
in_progress_splits.erase(i++);
} else {
_maybe_split_pgid(old_map, new_map, *i);
++i;
}
}
for (map<spg_t, spg_t>::iterator i = pending_splits.begin();
i != pending_splits.end();
) {
if (!new_map->have_pg_pool(i->first.pool())) {
rev_pending_splits.erase(i->second);
pending_splits.erase(i++);
} else {
_maybe_split_pgid(old_map, new_map, i->first);
++i;
}
}
}
bool OSDService::splitting(spg_t pgid)
{
Mutex::Locker l(in_progress_split_lock);
return in_progress_splits.count(pgid) ||
pending_splits.count(pgid);
}
void OSDService::complete_split(const set<spg_t> &pgs)
{
Mutex::Locker l(in_progress_split_lock);
for (set<spg_t>::const_iterator i = pgs.begin();
i != pgs.end();
++i) {
dout(10) << __func__ << ": Completing split on pg " << *i << dendl;
assert(!pending_splits.count(*i));
assert(in_progress_splits.count(*i));
in_progress_splits.erase(*i);
}
}
void OSDService::need_heartbeat_peer_update()
{
osd->need_heartbeat_peer_update();
}
void OSDService::pg_stat_queue_enqueue(PG *pg)
{
osd->pg_stat_queue_enqueue(pg);
}
void OSDService::pg_stat_queue_dequeue(PG *pg)
{
osd->pg_stat_queue_dequeue(pg);
}
void OSDService::start_shutdown()
{
{
Mutex::Locker l(agent_timer_lock);
agent_timer.cancel_all_events();
agent_timer.shutdown();
}
}
void OSDService::shutdown()
{
reserver_finisher.stop();
{
Mutex::Locker l(watch_lock);
watch_timer.shutdown();
}
objecter->shutdown();
objecter_finisher.stop();
{
Mutex::Locker l(backfill_request_lock);
backfill_request_timer.shutdown();
}
osdmap = OSDMapRef();
next_osdmap = OSDMapRef();
}
void OSDService::init()
{
reserver_finisher.start();
objecter_finisher.start();
objecter->set_client_incarnation(0);
objecter->start();
watch_timer.init();
agent_timer.init();
agent_thread.create();
}
void OSDService::activate_map()
{
// wake/unwake the tiering agent
agent_lock.Lock();
agent_active =
!osdmap->test_flag(CEPH_OSDMAP_NOTIERAGENT) &&
osd->is_active();
agent_cond.Signal();
agent_lock.Unlock();
}
class AgentTimeoutCB : public Context {
PGRef pg;
public:
AgentTimeoutCB(PGRef _pg) : pg(_pg) {}
void finish(int) {
pg->agent_choose_mode_restart();
}
};
void OSDService::agent_entry()
{
dout(10) << __func__ << " start" << dendl;
agent_lock.Lock();
while (!agent_stop_flag) {
if (agent_queue.empty()) {
dout(20) << __func__ << " empty queue" << dendl;
agent_cond.Wait(agent_lock);
continue;
}
uint64_t level = agent_queue.rbegin()->first;
set<PGRef>& top = agent_queue.rbegin()->second;
dout(10) << __func__
<< " tiers " << agent_queue.size()
<< ", top is " << level
<< " with pgs " << top.size()
<< ", ops " << agent_ops << "/"
<< g_conf->osd_agent_max_ops
<< (agent_active ? " active" : " NOT ACTIVE")
<< dendl;
dout(20) << __func__ << " oids " << agent_oids << dendl;
if (agent_ops >= g_conf->osd_agent_max_ops || top.empty() ||
!agent_active) {
agent_cond.Wait(agent_lock);
continue;
}
if (!agent_valid_iterator || agent_queue_pos == top.end()) {
agent_queue_pos = top.begin();
agent_valid_iterator = true;
}
PGRef pg = *agent_queue_pos;
int max = g_conf->osd_agent_max_ops - agent_ops;
agent_lock.Unlock();
if (!pg->agent_work(max)) {
dout(10) << __func__ << " " << *pg
<< " no agent_work, delay for " << g_conf->osd_agent_delay_time
<< " seconds" << dendl;
osd->logger->inc(l_osd_tier_delay);
// Queue a timer to call agent_choose_mode for this pg in 5 seconds
agent_timer_lock.Lock();
Context *cb = new AgentTimeoutCB(pg);
agent_timer.add_event_after(g_conf->osd_agent_delay_time, cb);
agent_timer_lock.Unlock();
}
agent_lock.Lock();
}
agent_lock.Unlock();
dout(10) << __func__ << " finish" << dendl;
}
void OSDService::agent_stop()
{
{
Mutex::Locker l(agent_lock);
// By this time all ops should be cancelled
assert(agent_ops == 0);
// By this time all PGs are shutdown and dequeued
if (!agent_queue.empty()) {
set<PGRef>& top = agent_queue.rbegin()->second;
derr << "agent queue not empty, for example " << (*top.begin())->info.pgid << dendl;
assert(0 == "agent queue not empty");
}
agent_stop_flag = true;
agent_cond.Signal();
}
agent_thread.join();
}
// -------------------------------------
float OSDService::get_full_ratio()
{
float full_ratio = cct->_conf->osd_failsafe_full_ratio;
if (full_ratio > 1.0) full_ratio /= 100.0;
return full_ratio;
}
float OSDService::get_nearfull_ratio()
{
float nearfull_ratio = cct->_conf->osd_failsafe_nearfull_ratio;
if (nearfull_ratio > 1.0) nearfull_ratio /= 100.0;
return nearfull_ratio;
}
void OSDService::check_nearfull_warning(const osd_stat_t &osd_stat)
{
Mutex::Locker l(full_status_lock);
enum s_names new_state;
time_t now = ceph_clock_gettime(NULL);
// We base ratio on kb_avail rather than kb_used because they can
// differ significantly e.g. on btrfs volumes with a large number of
// chunks reserved for metadata, and for our purposes (avoiding
// completely filling the disk) it's far more important to know how
// much space is available to use than how much we've already used.
float ratio = ((float)(osd_stat.kb - osd_stat.kb_avail)) / ((float)osd_stat.kb);
float nearfull_ratio = get_nearfull_ratio();
float full_ratio = get_full_ratio();
cur_ratio = ratio;
if (full_ratio > 0 && ratio > full_ratio) {
new_state = FULL;
} else if (nearfull_ratio > 0 && ratio > nearfull_ratio) {
new_state = NEAR;
} else {
cur_state = NONE;
return;
}
if (cur_state != new_state) {
cur_state = new_state;
} else if (now - last_msg < cct->_conf->osd_op_complaint_time) {
return;
}
last_msg = now;
if (cur_state == FULL)
clog->error() << "OSD full dropping all updates " << (int)(ratio * 100) << "% full";
else
clog->warn() << "OSD near full (" << (int)(ratio * 100) << "%)";
}
bool OSDService::check_failsafe_full()
{
Mutex::Locker l(full_status_lock);
if (cur_state == FULL)
return true;
return false;
}
bool OSDService::too_full_for_backfill(double *_ratio, double *_max_ratio)
{
Mutex::Locker l(full_status_lock);
double max_ratio;
max_ratio = cct->_conf->osd_backfill_full_ratio;
if (_ratio)
*_ratio = cur_ratio;
if (_max_ratio)
*_max_ratio = max_ratio;
return cur_ratio >= max_ratio;
}
void OSDService::update_osd_stat(vector<int>& hb_peers)
{
Mutex::Locker lock(stat_lock);
// fill in osd stats too
struct statfs stbuf;
osd->store->statfs(&stbuf);
uint64_t bytes = stbuf.f_blocks * stbuf.f_bsize;
uint64_t used = (stbuf.f_blocks - stbuf.f_bfree) * stbuf.f_bsize;
uint64_t avail = stbuf.f_bavail * stbuf.f_bsize;
osd_stat.kb = bytes >> 10;
osd_stat.kb_used = used >> 10;
osd_stat.kb_avail = avail >> 10;
osd->logger->set(l_osd_stat_bytes, bytes);
osd->logger->set(l_osd_stat_bytes_used, used);
osd->logger->set(l_osd_stat_bytes_avail, avail);
osd_stat.hb_in.swap(hb_peers);
osd_stat.hb_out.clear();
check_nearfull_warning(osd_stat);
osd->op_tracker.get_age_ms_histogram(&osd_stat.op_queue_age_hist);
dout(20) << "update_osd_stat " << osd_stat << dendl;
}
void OSDService::send_message_osd_cluster(int peer, Message *m, epoch_t from_epoch)
{
OSDMapRef next_map = get_nextmap_reserved();
// service map is always newer/newest
assert(from_epoch <= next_map->get_epoch());
if (next_map->is_down(peer) ||
next_map->get_info(peer).up_from > from_epoch) {
m->put();
release_map(next_map);
return;
}
const entity_inst_t& peer_inst = next_map->get_cluster_inst(peer);
Connection *peer_con = osd->cluster_messenger->get_connection(peer_inst).get();
share_map_peer(peer, peer_con, next_map);
peer_con->send_message(m);
release_map(next_map);
}
ConnectionRef OSDService::get_con_osd_cluster(int peer, epoch_t from_epoch)
{
OSDMapRef next_map = get_nextmap_reserved();
// service map is always newer/newest
assert(from_epoch <= next_map->get_epoch());
if (next_map->is_down(peer) ||
next_map->get_info(peer).up_from > from_epoch) {
release_map(next_map);
return NULL;
}
ConnectionRef con = osd->cluster_messenger->get_connection(next_map->get_cluster_inst(peer));
release_map(next_map);
return con;
}
pair<ConnectionRef,ConnectionRef> OSDService::get_con_osd_hb(int peer, epoch_t from_epoch)
{
OSDMapRef next_map = get_nextmap_reserved();
// service map is always newer/newest
assert(from_epoch <= next_map->get_epoch());
pair<ConnectionRef,ConnectionRef> ret;
if (next_map->is_down(peer) ||
next_map->get_info(peer).up_from > from_epoch) {
release_map(next_map);
return ret;
}
ret.first = osd->hbclient_messenger->get_connection(next_map->get_hb_back_inst(peer));
if (next_map->get_hb_front_addr(peer) != entity_addr_t())
ret.second = osd->hbclient_messenger->get_connection(next_map->get_hb_front_inst(peer));
release_map(next_map);
return ret;
}
void OSDService::queue_want_pg_temp(pg_t pgid, vector<int>& want)
{
Mutex::Locker l(pg_temp_lock);
pg_temp_wanted[pgid] = want;
}
void OSDService::send_pg_temp()
{
Mutex::Locker l(pg_temp_lock);
if (pg_temp_wanted.empty())
return;
dout(10) << "send_pg_temp " << pg_temp_wanted << dendl;
MOSDPGTemp *m = new MOSDPGTemp(osdmap->get_epoch());
m->pg_temp = pg_temp_wanted;
monc->send_mon_message(m);
}
// --------------------------------------
// dispatch
epoch_t OSDService::get_peer_epoch(int peer)
{
Mutex::Locker l(peer_map_epoch_lock);
map<int,epoch_t>::iterator p = peer_map_epoch.find(peer);
if (p == peer_map_epoch.end())
return 0;
return p->second;
}
epoch_t OSDService::note_peer_epoch(int peer, epoch_t e)
{
Mutex::Locker l(peer_map_epoch_lock);
map<int,epoch_t>::iterator p = peer_map_epoch.find(peer);
if (p != peer_map_epoch.end()) {
if (p->second < e) {
dout(10) << "note_peer_epoch osd." << peer << " has " << e << dendl;
p->second = e;
} else {
dout(30) << "note_peer_epoch osd." << peer << " has " << p->second << " >= " << e << dendl;
}
return p->second;
} else {
dout(10) << "note_peer_epoch osd." << peer << " now has " << e << dendl;
peer_map_epoch[peer] = e;
return e;
}
}
void OSDService::forget_peer_epoch(int peer, epoch_t as_of)
{
Mutex::Locker l(peer_map_epoch_lock);
map<int,epoch_t>::iterator p = peer_map_epoch.find(peer);
if (p != peer_map_epoch.end()) {
if (p->second <= as_of) {
dout(10) << "forget_peer_epoch osd." << peer << " as_of " << as_of
<< " had " << p->second << dendl;
peer_map_epoch.erase(p);
} else {
dout(10) << "forget_peer_epoch osd." << peer << " as_of " << as_of
<< " has " << p->second << " - not forgetting" << dendl;
}
}
}
bool OSDService::should_share_map(entity_name_t name, Connection *con,
epoch_t epoch, OSDMapRef& osdmap,
const epoch_t *sent_epoch_p)
{
bool should_send = false;
dout(20) << "should_share_map "
<< name << " " << con->get_peer_addr()
<< " " << epoch << dendl;
// does client have old map?
if (name.is_client()) {
bool message_sendmap = epoch < osdmap->get_epoch();
if (message_sendmap && sent_epoch_p) {
dout(20) << "client session last_sent_epoch: "
<< *sent_epoch_p
<< " versus osdmap epoch " << osdmap->get_epoch() << dendl;
if (*sent_epoch_p < osdmap->get_epoch()) {
should_send = true;
} // else we don't need to send it out again
}
}
if (con->get_messenger() == osd->cluster_messenger &&
con != osd->cluster_messenger->get_loopback_connection() &&
osdmap->is_up(name.num()) &&
(osdmap->get_cluster_addr(name.num()) == con->get_peer_addr() ||
osdmap->get_hb_back_addr(name.num()) == con->get_peer_addr())) {
// remember
epoch_t has = MAX(get_peer_epoch(name.num()), epoch);
// share?
if (has < osdmap->get_epoch()) {
dout(10) << name << " " << con->get_peer_addr()
<< " has old map " << epoch << " < "
<< osdmap->get_epoch() << dendl;
should_send = true;
}
}
return should_send;
}
void OSDService::share_map(
entity_name_t name,
Connection *con,
epoch_t epoch,
OSDMapRef& osdmap,
epoch_t *sent_epoch_p)
{
dout(20) << "share_map "
<< name << " " << con->get_peer_addr()
<< " " << epoch << dendl;
if ((!osd->is_active()) && (!osd->is_stopping())) {
/*It is safe not to proceed as OSD is not in healthy state*/
return;
}
bool want_shared = should_share_map(name, con, epoch,
osdmap, sent_epoch_p);
if (want_shared){
if (name.is_client()) {
dout(10) << name << " has old map " << epoch
<< " < " << osdmap->get_epoch() << dendl;
// we know the Session is valid or we wouldn't be sending
if (sent_epoch_p) {
*sent_epoch_p = osdmap->get_epoch();
}
send_incremental_map(epoch, con, osdmap);
} else if (con->get_messenger() == osd->cluster_messenger &&
osdmap->is_up(name.num()) &&
(osdmap->get_cluster_addr(name.num()) == con->get_peer_addr() ||
osdmap->get_hb_back_addr(name.num()) == con->get_peer_addr())) {
dout(10) << name << " " << con->get_peer_addr()
<< " has old map " << epoch << " < "
<< osdmap->get_epoch() << dendl;
note_peer_epoch(name.num(), osdmap->get_epoch());
send_incremental_map(epoch, con, osdmap);
}
}
}
void OSDService::share_map_peer(int peer, Connection *con, OSDMapRef map)
{
if (!map)
map = get_osdmap();
// send map?
epoch_t pe = get_peer_epoch(peer);
if (pe) {
if (pe < map->get_epoch()) {
send_incremental_map(pe, con, map);
note_peer_epoch(peer, map->get_epoch());
} else
dout(20) << "share_map_peer " << con << " already has epoch " << pe << dendl;
} else {
dout(20) << "share_map_peer " << con << " don't know epoch, doing nothing" << dendl;
// no idea about peer's epoch.
// ??? send recent ???
// do nothing.
}
}
bool OSDService::inc_scrubs_pending()
{
bool result = false;
sched_scrub_lock.Lock();
if (scrubs_pending + scrubs_active < cct->_conf->osd_max_scrubs) {
dout(20) << "inc_scrubs_pending " << scrubs_pending << " -> " << (scrubs_pending+1)
<< " (max " << cct->_conf->osd_max_scrubs << ", active " << scrubs_active << ")" << dendl;
result = true;
++scrubs_pending;
} else {
dout(20) << "inc_scrubs_pending " << scrubs_pending << " + " << scrubs_active << " active >= max " << cct->_conf->osd_max_scrubs << dendl;
}
sched_scrub_lock.Unlock();
return result;
}
void OSDService::dec_scrubs_pending()
{
sched_scrub_lock.Lock();
dout(20) << "dec_scrubs_pending " << scrubs_pending << " -> " << (scrubs_pending-1)
<< " (max " << cct->_conf->osd_max_scrubs << ", active " << scrubs_active << ")" << dendl;
--scrubs_pending;
assert(scrubs_pending >= 0);
sched_scrub_lock.Unlock();
}
void OSDService::inc_scrubs_active(bool reserved)
{
sched_scrub_lock.Lock();
++(scrubs_active);
if (reserved) {
--(scrubs_pending);
dout(20) << "inc_scrubs_active " << (scrubs_active-1) << " -> " << scrubs_active
<< " (max " << cct->_conf->osd_max_scrubs
<< ", pending " << (scrubs_pending+1) << " -> " << scrubs_pending << ")" << dendl;
assert(scrubs_pending >= 0);
} else {
dout(20) << "inc_scrubs_active " << (scrubs_active-1) << " -> " << scrubs_active
<< " (max " << cct->_conf->osd_max_scrubs
<< ", pending " << scrubs_pending << ")" << dendl;
}
sched_scrub_lock.Unlock();
}
void OSDService::dec_scrubs_active()
{
sched_scrub_lock.Lock();
dout(20) << "dec_scrubs_active " << scrubs_active << " -> " << (scrubs_active-1)
<< " (max " << cct->_conf->osd_max_scrubs << ", pending " << scrubs_pending << ")" << dendl;
--scrubs_active;
sched_scrub_lock.Unlock();
}
void OSDService::retrieve_epochs(epoch_t *_boot_epoch, epoch_t *_up_epoch,
epoch_t *_bind_epoch) const
{
Mutex::Locker l(epoch_lock);
if (_boot_epoch)
*_boot_epoch = boot_epoch;
if (_up_epoch)
*_up_epoch = up_epoch;
if (_bind_epoch)
*_bind_epoch = bind_epoch;
}
void OSDService::set_epochs(const epoch_t *_boot_epoch, const epoch_t *_up_epoch,
const epoch_t *_bind_epoch)
{
Mutex::Locker l(epoch_lock);
if (_boot_epoch) {
assert(*_boot_epoch == 0 || *_boot_epoch >= boot_epoch);
boot_epoch = *_boot_epoch;
}
if (_up_epoch) {
assert(*_up_epoch == 0 || *_up_epoch >= up_epoch);
up_epoch = *_up_epoch;
}
if (_bind_epoch) {
assert(*_bind_epoch == 0 || *_bind_epoch >= bind_epoch);
bind_epoch = *_bind_epoch;
}
}
bool OSDService::prepare_to_stop()
{
Mutex::Locker l(is_stopping_lock);
if (state != NOT_STOPPING)
return false;
OSDMapRef osdmap = get_osdmap();
if (osdmap && osdmap->is_up(whoami)) {
dout(0) << __func__ << " telling mon we are shutting down" << dendl;
state = PREPARING_TO_STOP;
monc->send_mon_message(new MOSDMarkMeDown(monc->get_fsid(),
osdmap->get_inst(whoami),
osdmap->get_epoch(),
true // request ack
));
utime_t now = ceph_clock_now(cct);