]> git.proxmox.com Git - ceph.git/blame - ceph/src/osd/PG.cc
update dh_systemd restart patch for pacific
[ceph.git] / ceph / src / osd / PG.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#include "PG.h"
7c673cae 16#include "messages/MOSDRepScrub.h"
7c673cae
FG
17
18#include "common/errno.h"
9f95a23c 19#include "common/ceph_releases.h"
7c673cae
FG
20#include "common/config.h"
21#include "OSD.h"
22#include "OpRequest.h"
23#include "ScrubStore.h"
f67539c2 24#include "pg_scrubber.h"
7c673cae 25#include "Session.h"
9f95a23c 26#include "osd/scheduler/OpSchedulerItem.h"
7c673cae
FG
27
28#include "common/Timer.h"
29#include "common/perf_counters.h"
30
31#include "messages/MOSDOp.h"
32#include "messages/MOSDPGNotify.h"
7c673cae 33#include "messages/MOSDPGInfo.h"
7c673cae
FG
34#include "messages/MOSDPGScan.h"
35#include "messages/MOSDPGBackfill.h"
36#include "messages/MOSDPGBackfillRemove.h"
37#include "messages/MBackfillReserve.h"
38#include "messages/MRecoveryReserve.h"
39#include "messages/MOSDPGPush.h"
40#include "messages/MOSDPGPushReply.h"
41#include "messages/MOSDPGPull.h"
42#include "messages/MOSDECSubOpWrite.h"
43#include "messages/MOSDECSubOpWriteReply.h"
44#include "messages/MOSDECSubOpRead.h"
45#include "messages/MOSDECSubOpReadReply.h"
46#include "messages/MOSDPGUpdateLogMissing.h"
47#include "messages/MOSDPGUpdateLogMissingReply.h"
48#include "messages/MOSDBackoff.h"
49#include "messages/MOSDScrubReserve.h"
7c673cae 50#include "messages/MOSDRepOp.h"
7c673cae
FG
51#include "messages/MOSDRepOpReply.h"
52#include "messages/MOSDRepScrubMap.h"
c07f9fc5
FG
53#include "messages/MOSDPGRecoveryDelete.h"
54#include "messages/MOSDPGRecoveryDeleteReply.h"
7c673cae
FG
55
56#include "common/BackTrace.h"
57#include "common/EventTrace.h"
58
59#ifdef WITH_LTTNG
60#define TRACEPOINT_DEFINE
61#define TRACEPOINT_PROBE_DYNAMIC_LINKAGE
62#include "tracing/pg.h"
63#undef TRACEPOINT_PROBE_DYNAMIC_LINKAGE
64#undef TRACEPOINT_DEFINE
65#else
66#define tracepoint(...)
67#endif
68
69#include <sstream>
70
71#define dout_context cct
72#define dout_subsys ceph_subsys_osd
73#undef dout_prefix
74#define dout_prefix _prefix(_dout, this)
75
f67539c2
TL
76using std::list;
77using std::map;
78using std::ostringstream;
79using std::pair;
80using std::set;
81using std::string;
82using std::stringstream;
83using std::unique_ptr;
84using std::vector;
85
86using ceph::bufferlist;
87using ceph::bufferptr;
88using ceph::decode;
89using ceph::encode;
90using ceph::Formatter;
91
9f95a23c 92using namespace ceph::osd::scheduler;
7c673cae
FG
93
94template <class T>
95static ostream& _prefix(std::ostream *_dout, T *t)
96{
11fdf7f2 97 return t->gen_prefix(*_dout);
7c673cae
FG
98}
99
7c673cae
FG
100void PG::get(const char* tag)
101{
11fdf7f2
TL
102 int after = ++ref;
103 lgeneric_subdout(cct, refs, 5) << "PG::get " << this << " "
104 << "tag " << (tag ? tag : "(none") << " "
105 << (after - 1) << " -> " << after << dendl;
7c673cae 106#ifdef PG_DEBUG_REFS
11fdf7f2 107 std::lock_guard l(_ref_id_lock);
7c673cae
FG
108 _tag_counts[tag]++;
109#endif
110}
111
112void PG::put(const char* tag)
113{
114#ifdef PG_DEBUG_REFS
115 {
11fdf7f2 116 std::lock_guard l(_ref_id_lock);
7c673cae 117 auto tag_counts_entry = _tag_counts.find(tag);
11fdf7f2 118 ceph_assert(tag_counts_entry != _tag_counts.end());
7c673cae
FG
119 --tag_counts_entry->second;
120 if (tag_counts_entry->second == 0) {
121 _tag_counts.erase(tag_counts_entry);
122 }
123 }
124#endif
11fdf7f2
TL
125 auto local_cct = cct;
126 int after = --ref;
127 lgeneric_subdout(local_cct, refs, 5) << "PG::put " << this << " "
128 << "tag " << (tag ? tag : "(none") << " "
129 << (after + 1) << " -> " << after
130 << dendl;
131 if (after == 0)
7c673cae
FG
132 delete this;
133}
134
135#ifdef PG_DEBUG_REFS
136uint64_t PG::get_with_id()
137{
138 ref++;
11fdf7f2 139 std::lock_guard l(_ref_id_lock);
7c673cae
FG
140 uint64_t id = ++_ref_id;
141 BackTrace bt(0);
142 stringstream ss;
143 bt.print(ss);
11fdf7f2
TL
144 lgeneric_subdout(cct, refs, 5) << "PG::get " << this << " " << info.pgid
145 << " got id " << id << " "
146 << (ref - 1) << " -> " << ref
147 << dendl;
148 ceph_assert(!_live_ids.count(id));
7c673cae
FG
149 _live_ids.insert(make_pair(id, ss.str()));
150 return id;
151}
152
153void PG::put_with_id(uint64_t id)
154{
11fdf7f2
TL
155 int newref = --ref;
156 lgeneric_subdout(cct, refs, 5) << "PG::put " << this << " " << info.pgid
157 << " put id " << id << " "
158 << (newref + 1) << " -> " << newref
159 << dendl;
7c673cae 160 {
11fdf7f2
TL
161 std::lock_guard l(_ref_id_lock);
162 ceph_assert(_live_ids.count(id));
7c673cae
FG
163 _live_ids.erase(id);
164 }
11fdf7f2 165 if (newref)
7c673cae
FG
166 delete this;
167}
168
169void PG::dump_live_ids()
170{
11fdf7f2 171 std::lock_guard l(_ref_id_lock);
7c673cae
FG
172 dout(0) << "\t" << __func__ << ": " << info.pgid << " live ids:" << dendl;
173 for (map<uint64_t, string>::iterator i = _live_ids.begin();
174 i != _live_ids.end();
175 ++i) {
176 dout(0) << "\t\tid: " << *i << dendl;
177 }
178 dout(0) << "\t" << __func__ << ": " << info.pgid << " live tags:" << dendl;
179 for (map<string, uint64_t>::iterator i = _tag_counts.begin();
180 i != _tag_counts.end();
181 ++i) {
182 dout(0) << "\t\tid: " << *i << dendl;
183 }
184}
185#endif
186
7c673cae
FG
187PG::PG(OSDService *o, OSDMapRef curmap,
188 const PGPool &_pool, spg_t p) :
9f95a23c 189 pg_whoami(o->whoami, p.shard),
11fdf7f2
TL
190 pg_id(p),
191 coll(p),
7c673cae
FG
192 osd(o),
193 cct(o->cct),
194 osdriver(osd->store, coll_t(), OSD::make_snapmapper_oid()),
195 snap_mapper(
196 cct,
197 &osdriver,
198 p.ps(),
11fdf7f2 199 p.get_split_bits(_pool.info.get_pg_num()),
7c673cae
FG
200 _pool.id,
201 p.shard),
7c673cae 202 trace_endpoint("0.0.0.0", 0, "PG"),
7c673cae 203 info_struct_v(0),
7c673cae 204 pgmeta_oid(p.make_pgmeta_oid()),
7c673cae
FG
205 stat_queue_item(this),
206 scrub_queued(false),
207 recovery_queued(false),
208 recovery_ops_active(0),
7c673cae 209 backfill_reserving(false),
7c673cae 210 pg_stats_publish_valid(false),
7c673cae 211 finish_sync_event(NULL),
7c673cae
FG
212 scrub_after_recovery(false),
213 active_pushes(0),
9f95a23c
TL
214 recovery_state(
215 o->cct,
216 pg_whoami,
217 p,
218 _pool,
219 curmap,
220 this,
221 this),
222 pool(recovery_state.get_pool()),
223 info(recovery_state.get_info())
7c673cae
FG
224{
225#ifdef PG_DEBUG_REFS
226 osd->add_pgid(p, this);
227#endif
228#ifdef WITH_BLKIN
229 std::stringstream ss;
230 ss << "PG " << info.pgid;
231 trace_endpoint.copy_name(ss.str());
232#endif
7c673cae
FG
233}
234
235PG::~PG()
236{
7c673cae
FG
237#ifdef PG_DEBUG_REFS
238 osd->remove_pgid(info.pgid, this);
239#endif
240}
241
7c673cae
FG
242void PG::lock(bool no_lockdep) const
243{
9f95a23c
TL
244#ifdef CEPH_DEBUG_MUTEX
245 _lock.lock(no_lockdep);
246#else
247 _lock.lock();
248 locked_by = std::this_thread::get_id();
249#endif
7c673cae 250 // if we have unrecorded dirty state with the lock dropped, there is a bug
9f95a23c 251 ceph_assert(!recovery_state.debug_has_dirty_state());
7c673cae
FG
252
253 dout(30) << "lock" << dendl;
254}
255
9f95a23c
TL
256bool PG::is_locked() const
257{
258 return ceph_mutex_is_locked(_lock);
259}
260
261void PG::unlock() const
262{
263 //generic_dout(0) << this << " " << info.pgid << " unlock" << dendl;
264 ceph_assert(!recovery_state.debug_has_dirty_state());
265#ifndef CEPH_DEBUG_MUTEX
266 locked_by = {};
267#endif
268 _lock.unlock();
269}
270
11fdf7f2 271std::ostream& PG::gen_prefix(std::ostream& out) const
7c673cae 272{
9f95a23c
TL
273 OSDMapRef mapref = recovery_state.get_osdmap();
274#ifdef CEPH_DEBUG_MUTEX
7c673cae 275 if (_lock.is_locked_by_me()) {
9f95a23c
TL
276#else
277 if (locked_by == std::this_thread::get_id()) {
278#endif
7c673cae
FG
279 out << "osd." << osd->whoami
280 << " pg_epoch: " << (mapref ? mapref->get_epoch():0)
281 << " " << *this << " ";
282 } else {
283 out << "osd." << osd->whoami
284 << " pg_epoch: " << (mapref ? mapref->get_epoch():0)
9f95a23c 285 << " pg[" << pg_id.pgid << "(unlocked)] ";
7c673cae 286 }
11fdf7f2 287 return out;
7c673cae 288}
7c673cae 289
9f95a23c
TL
290PerfCounters &PG::get_peering_perf() {
291 return *(osd->recoverystate_perf);
7c673cae
FG
292}
293
9f95a23c
TL
294PerfCounters &PG::get_perf_logger() {
295 return *(osd->logger);
296}
7c673cae 297
9f95a23c
TL
298void PG::log_state_enter(const char *state) {
299 osd->pg_recovery_stats.log_enter(state);
300}
7c673cae 301
9f95a23c
TL
302void PG::log_state_exit(
303 const char *state_name, utime_t enter_time,
304 uint64_t events, utime_t event_dur) {
305 osd->pg_recovery_stats.log_exit(
306 state_name, ceph_clock_now() - enter_time, events, event_dur);
7c673cae 307}
f67539c2 308
9f95a23c 309/********* PG **********/
7c673cae
FG
310
311void PG::remove_snap_mapped_object(
312 ObjectStore::Transaction &t, const hobject_t &soid)
313{
314 t.remove(
315 coll,
316 ghobject_t(soid, ghobject_t::NO_GEN, pg_whoami.shard));
317 clear_object_snap_mapping(&t, soid);
318}
319
320void PG::clear_object_snap_mapping(
321 ObjectStore::Transaction *t, const hobject_t &soid)
322{
323 OSDriver::OSTransaction _t(osdriver.get_transaction(t));
324 if (soid.snap < CEPH_MAXSNAP) {
325 int r = snap_mapper.remove_oid(
326 soid,
327 &_t);
328 if (!(r == 0 || r == -ENOENT)) {
329 derr << __func__ << ": remove_oid returned " << cpp_strerror(r) << dendl;
330 ceph_abort();
331 }
332 }
333}
334
335void PG::update_object_snap_mapping(
336 ObjectStore::Transaction *t, const hobject_t &soid, const set<snapid_t> &snaps)
337{
338 OSDriver::OSTransaction _t(osdriver.get_transaction(t));
11fdf7f2 339 ceph_assert(soid.snap < CEPH_MAXSNAP);
7c673cae
FG
340 int r = snap_mapper.remove_oid(
341 soid,
342 &_t);
343 if (!(r == 0 || r == -ENOENT)) {
344 derr << __func__ << ": remove_oid returned " << cpp_strerror(r) << dendl;
345 ceph_abort();
346 }
347 snap_mapper.add_oid(
348 soid,
349 snaps,
350 &_t);
351}
352
9f95a23c
TL
353/******* PG ***********/
354void PG::clear_primary_state()
7c673cae 355{
f67539c2
TL
356 dout(20) << __func__ << dendl;
357
9f95a23c 358 projected_log = PGLog::IndexedLog();
7c673cae 359
9f95a23c
TL
360 snap_trimq.clear();
361 snap_trimq_repeat.clear();
362 finish_sync_event = 0; // so that _finish_recovery doesn't go off in another thread
363 release_pg_backoffs();
7c673cae 364
f67539c2
TL
365 if (m_scrubber) {
366 m_scrubber->discard_replica_reservations();
367 }
9f95a23c
TL
368 scrub_after_recovery = false;
369
370 agent_clear();
7c673cae
FG
371}
372
91327a77 373
9f95a23c
TL
374bool PG::op_has_sufficient_caps(OpRequestRef& op)
375{
376 // only check MOSDOp
377 if (op->get_req()->get_type() != CEPH_MSG_OSD_OP)
c07f9fc5 378 return true;
9f95a23c
TL
379
380 auto req = op->get_req<MOSDOp>();
381 auto priv = req->get_connection()->get_priv();
382 auto session = static_cast<Session*>(priv.get());
383 if (!session) {
384 dout(0) << "op_has_sufficient_caps: no session for op " << *req << dendl;
c07f9fc5 385 return false;
7c673cae 386 }
9f95a23c
TL
387 OSDCap& caps = session->caps;
388 priv.reset();
7c673cae 389
9f95a23c
TL
390 const string &key = req->get_hobj().get_key().empty() ?
391 req->get_oid().name :
392 req->get_hobj().get_key();
91327a77 393
9f95a23c
TL
394 bool cap = caps.is_capable(pool.name, req->get_hobj().nspace,
395 pool.info.application_metadata,
396 key,
397 op->need_read_cap(),
398 op->need_write_cap(),
399 op->classes(),
400 session->get_peer_socket_addr());
91327a77 401
9f95a23c
TL
402 dout(20) << "op_has_sufficient_caps "
403 << "session=" << session
404 << " pool=" << pool.id << " (" << pool.name
405 << " " << req->get_hobj().nspace
406 << ")"
407 << " pool_app_metadata=" << pool.info.application_metadata
408 << " need_read_cap=" << op->need_read_cap()
409 << " need_write_cap=" << op->need_write_cap()
410 << " classes=" << op->classes()
411 << " -> " << (cap ? "yes" : "NO")
412 << dendl;
413 return cap;
7c673cae
FG
414}
415
9f95a23c 416void PG::queue_recovery()
91327a77 417{
9f95a23c
TL
418 if (!is_primary() || !is_peered()) {
419 dout(10) << "queue_recovery -- not primary or not peered " << dendl;
420 ceph_assert(!recovery_queued);
421 } else if (recovery_queued) {
422 dout(10) << "queue_recovery -- already queued" << dendl;
91327a77 423 } else {
9f95a23c
TL
424 dout(10) << "queue_recovery -- queuing" << dendl;
425 recovery_queued = true;
426 osd->queue_for_recovery(this);
91327a77
AA
427 }
428}
9f95a23c 429
f67539c2 430void PG::queue_scrub_after_repair()
7c673cae 431{
f67539c2 432 dout(10) << __func__ << dendl;
9f95a23c 433 ceph_assert(ceph_mutex_is_locked(_lock));
f67539c2
TL
434
435 m_planned_scrub.must_deep_scrub = true;
436 m_planned_scrub.check_repair = true;
437 m_planned_scrub.must_scrub = true;
438
9f95a23c 439 if (is_scrubbing()) {
f67539c2
TL
440 dout(10) << __func__ << ": scrubbing already" << dendl;
441 return;
9f95a23c 442 }
f67539c2
TL
443 if (scrub_queued) {
444 dout(10) << __func__ << ": already queued" << dendl;
445 return;
446 }
447
448 m_scrubber->set_op_parameters(m_planned_scrub);
449 dout(15) << __func__ << ": queueing" << dendl;
450
451 scrub_queued = true;
452 osd->queue_scrub_after_repair(this, Scrub::scrub_prio_t::high_priority);
9f95a23c 453}
7c673cae 454
9f95a23c
TL
455unsigned PG::get_scrub_priority()
456{
457 // a higher value -> a higher priority
f67539c2
TL
458 int64_t pool_scrub_priority =
459 pool.info.opts.value_or(pool_opts_t::SCRUB_PRIORITY, (int64_t)0);
9f95a23c
TL
460 return pool_scrub_priority > 0 ? pool_scrub_priority : cct->_conf->osd_scrub_priority;
461}
7c673cae 462
9f95a23c
TL
463Context *PG::finish_recovery()
464{
465 dout(10) << "finish_recovery" << dendl;
466 ceph_assert(info.last_complete == info.last_update);
7c673cae 467
9f95a23c 468 clear_recovery_state();
92f5a8d4 469
9f95a23c
TL
470 /*
471 * sync all this before purging strays. but don't block!
472 */
473 finish_sync_event = new C_PG_FinishRecovery(this);
474 return finish_sync_event;
7c673cae
FG
475}
476
f67539c2 477void PG::_finish_recovery(Context* c)
7c673cae 478{
f67539c2
TL
479 dout(15) << __func__ << " finish_sync_event? " << finish_sync_event << " clean? "
480 << is_clean() << dendl;
481
9f95a23c
TL
482 std::scoped_lock locker{*this};
483 if (recovery_state.is_deleting() || !is_clean()) {
484 dout(10) << __func__ << " raced with delete or repair" << dendl;
485 return;
7c673cae 486 }
9f95a23c
TL
487 // When recovery is initiated by a repair, that flag is left on
488 state_clear(PG_STATE_REPAIR);
489 if (c == finish_sync_event) {
f67539c2 490 dout(15) << __func__ << " scrub_after_recovery? " << scrub_after_recovery << dendl;
9f95a23c
TL
491 finish_sync_event = 0;
492 recovery_state.purge_strays();
7c673cae 493
9f95a23c
TL
494 publish_stats_to_osd();
495
496 if (scrub_after_recovery) {
497 dout(10) << "_finish_recovery requeueing for scrub" << dendl;
498 scrub_after_recovery = false;
f67539c2 499 queue_scrub_after_repair();
7c673cae 500 }
9f95a23c
TL
501 } else {
502 dout(10) << "_finish_recovery -- stale" << dendl;
7c673cae 503 }
9f95a23c 504}
7c673cae 505
9f95a23c
TL
506void PG::start_recovery_op(const hobject_t& soid)
507{
508 dout(10) << "start_recovery_op " << soid
509#ifdef DEBUG_RECOVERY_OIDS
510 << " (" << recovering_oids << ")"
511#endif
512 << dendl;
513 ceph_assert(recovery_ops_active >= 0);
514 recovery_ops_active++;
515#ifdef DEBUG_RECOVERY_OIDS
516 recovering_oids.insert(soid);
517#endif
518 osd->start_recovery_op(this, soid);
7c673cae
FG
519}
520
9f95a23c 521void PG::finish_recovery_op(const hobject_t& soid, bool dequeue)
7c673cae 522{
9f95a23c
TL
523 dout(10) << "finish_recovery_op " << soid
524#ifdef DEBUG_RECOVERY_OIDS
f67539c2 525 << " (" << recovering_oids << ")"
9f95a23c
TL
526#endif
527 << dendl;
528 ceph_assert(recovery_ops_active > 0);
529 recovery_ops_active--;
530#ifdef DEBUG_RECOVERY_OIDS
531 ceph_assert(recovering_oids.count(soid));
532 recovering_oids.erase(recovering_oids.find(soid));
533#endif
534 osd->finish_recovery_op(this, soid, dequeue);
7c673cae 535
9f95a23c
TL
536 if (!dequeue) {
537 queue_recovery();
7c673cae 538 }
7c673cae
FG
539}
540
9f95a23c
TL
541void PG::split_into(pg_t child_pgid, PG *child, unsigned split_bits)
542{
543 recovery_state.split_into(child_pgid, &child->recovery_state, split_bits);
7c673cae 544
9f95a23c 545 child->update_snap_mapper_bits(split_bits);
7c673cae 546
9f95a23c
TL
547 child->snap_trimq = snap_trimq;
548 child->snap_trimq_repeat = snap_trimq_repeat;
549
550 _split_into(child_pgid, child, split_bits);
551
552 // release all backoffs for simplicity
553 release_backoffs(hobject_t(), hobject_t::get_max());
7c673cae
FG
554}
555
9f95a23c 556void PG::start_split_stats(const set<spg_t>& childpgs, vector<object_stat_sum_t> *out)
7c673cae 557{
9f95a23c 558 recovery_state.start_split_stats(childpgs, out);
7c673cae
FG
559}
560
9f95a23c
TL
561void PG::finish_split_stats(const object_stat_sum_t& stats, ObjectStore::Transaction &t)
562{
563 recovery_state.finish_split_stats(stats, t);
564}
565
566void PG::merge_from(map<spg_t,PGRef>& sources, PeeringCtx &rctx,
567 unsigned split_bits,
568 const pg_merge_meta_t& last_pg_merge_meta)
569{
570 dout(10) << __func__ << " from " << sources << " split_bits " << split_bits
571 << dendl;
572 map<spg_t, PeeringState*> source_ps;
573 for (auto &&source : sources) {
574 source_ps.emplace(source.first, &source.second->recovery_state);
575 }
576 recovery_state.merge_from(source_ps, rctx, split_bits, last_pg_merge_meta);
577
578 for (auto& i : sources) {
579 auto& source = i.second;
580 // wipe out source's pgmeta
581 rctx.transaction.remove(source->coll, source->pgmeta_oid);
582
583 // merge (and destroy source collection)
584 rctx.transaction.merge_collection(source->coll, coll, split_bits);
7c673cae
FG
585 }
586
9f95a23c
TL
587 // merge_collection does this, but maybe all of our sources were missing.
588 rctx.transaction.collection_set_bits(coll, split_bits);
589
590 snap_mapper.update_bits(split_bits);
7c673cae
FG
591}
592
9f95a23c 593void PG::add_backoff(const ceph::ref_t<Session>& s, const hobject_t& begin, const hobject_t& end)
7c673cae 594{
9f95a23c
TL
595 auto con = s->con;
596 if (!con) // OSD::ms_handle_reset clears s->con without a lock
597 return;
598 auto b = s->have_backoff(info.pgid, begin);
599 if (b) {
600 derr << __func__ << " already have backoff for " << s << " begin " << begin
601 << " " << *b << dendl;
602 ceph_abort();
7c673cae 603 }
9f95a23c
TL
604 std::lock_guard l(backoff_lock);
605 b = ceph::make_ref<Backoff>(info.pgid, this, s, ++s->backoff_seq, begin, end);
606 backoffs[begin].insert(b);
607 s->add_backoff(b);
608 dout(10) << __func__ << " session " << s << " added " << *b << dendl;
609 con->send_message(
610 new MOSDBackoff(
611 info.pgid,
612 get_osdmap_epoch(),
613 CEPH_OSD_BACKOFF_OP_BLOCK,
614 b->id,
615 begin,
616 end));
7c673cae
FG
617}
618
9f95a23c 619void PG::release_backoffs(const hobject_t& begin, const hobject_t& end)
7c673cae 620{
9f95a23c
TL
621 dout(10) << __func__ << " [" << begin << "," << end << ")" << dendl;
622 vector<ceph::ref_t<Backoff>> bv;
623 {
624 std::lock_guard l(backoff_lock);
625 auto p = backoffs.lower_bound(begin);
626 while (p != backoffs.end()) {
627 int r = cmp(p->first, end);
628 dout(20) << __func__ << " ? " << r << " " << p->first
629 << " " << p->second << dendl;
630 // note: must still examine begin=end=p->first case
631 if (r > 0 || (r == 0 && begin < end)) {
632 break;
633 }
634 dout(20) << __func__ << " checking " << p->first
635 << " " << p->second << dendl;
636 auto q = p->second.begin();
637 while (q != p->second.end()) {
638 dout(20) << __func__ << " checking " << *q << dendl;
639 int r = cmp((*q)->begin, begin);
640 if (r == 0 || (r > 0 && (*q)->end < end)) {
641 bv.push_back(*q);
642 q = p->second.erase(q);
643 } else {
644 ++q;
645 }
646 }
647 if (p->second.empty()) {
648 p = backoffs.erase(p);
649 } else {
650 ++p;
651 }
7c673cae
FG
652 }
653 }
9f95a23c
TL
654 for (auto b : bv) {
655 std::lock_guard l(b->lock);
656 dout(10) << __func__ << " " << *b << dendl;
657 if (b->session) {
658 ceph_assert(b->pg == this);
659 ConnectionRef con = b->session->con;
660 if (con) { // OSD::ms_handle_reset clears s->con without a lock
661 con->send_message(
662 new MOSDBackoff(
663 info.pgid,
664 get_osdmap_epoch(),
665 CEPH_OSD_BACKOFF_OP_UNBLOCK,
666 b->id,
667 b->begin,
668 b->end));
7c673cae 669 }
9f95a23c
TL
670 if (b->is_new()) {
671 b->state = Backoff::STATE_DELETING;
7c673cae 672 } else {
9f95a23c
TL
673 b->session->rm_backoff(b);
674 b->session.reset();
7c673cae 675 }
9f95a23c
TL
676 b->pg.reset();
677 }
7c673cae 678 }
7c673cae
FG
679}
680
9f95a23c 681void PG::clear_backoffs()
7c673cae 682{
9f95a23c
TL
683 dout(10) << __func__ << " " << dendl;
684 map<hobject_t,set<ceph::ref_t<Backoff>>> ls;
685 {
686 std::lock_guard l(backoff_lock);
687 ls.swap(backoffs);
688 }
689 for (auto& p : ls) {
690 for (auto& b : p.second) {
691 std::lock_guard l(b->lock);
692 dout(10) << __func__ << " " << *b << dendl;
693 if (b->session) {
694 ceph_assert(b->pg == this);
695 if (b->is_new()) {
696 b->state = Backoff::STATE_DELETING;
697 } else {
698 b->session->rm_backoff(b);
699 b->session.reset();
700 }
701 b->pg.reset();
702 }
703 }
704 }
705}
7c673cae 706
9f95a23c
TL
707// called by Session::clear_backoffs()
708void PG::rm_backoff(const ceph::ref_t<Backoff>& b)
709{
710 dout(10) << __func__ << " " << *b << dendl;
711 std::lock_guard l(backoff_lock);
712 ceph_assert(ceph_mutex_is_locked_by_me(b->lock));
713 ceph_assert(b->pg == this);
714 auto p = backoffs.find(b->begin);
715 // may race with release_backoffs()
716 if (p != backoffs.end()) {
717 auto q = p->second.find(b);
718 if (q != p->second.end()) {
719 p->second.erase(q);
720 if (p->second.empty()) {
721 backoffs.erase(p);
722 }
723 }
724 }
725}
7c673cae 726
f67539c2 727void PG::clear_recovery_state()
9f95a23c
TL
728{
729 dout(10) << "clear_recovery_state" << dendl;
7c673cae 730
9f95a23c 731 finish_sync_event = 0;
7c673cae 732
9f95a23c
TL
733 hobject_t soid;
734 while (recovery_ops_active > 0) {
735#ifdef DEBUG_RECOVERY_OIDS
736 soid = *recovering_oids.begin();
737#endif
738 finish_recovery_op(soid, true);
739 }
7c673cae 740
9f95a23c
TL
741 backfill_info.clear();
742 peer_backfill_info.clear();
743 waiting_on_backfill.clear();
744 _clear_recovery_state(); // pg impl specific hook
745}
7c673cae 746
9f95a23c
TL
747void PG::cancel_recovery()
748{
749 dout(10) << "cancel_recovery" << dendl;
750 clear_recovery_state();
7c673cae
FG
751}
752
9f95a23c
TL
753void PG::set_probe_targets(const set<pg_shard_t> &probe_set)
754{
755 std::lock_guard l(heartbeat_peer_lock);
756 probe_targets.clear();
757 for (set<pg_shard_t>::iterator i = probe_set.begin();
758 i != probe_set.end();
7c673cae 759 ++i) {
9f95a23c 760 probe_targets.insert(i->osd);
7c673cae 761 }
7c673cae
FG
762}
763
9f95a23c 764void PG::send_cluster_message(
f67539c2 765 int target, MessageRef m,
9f95a23c
TL
766 epoch_t epoch, bool share_map_update=false)
767{
768 ConnectionRef con = osd->get_con_osd_cluster(
769 target, get_osdmap_epoch());
770 if (!con) {
11fdf7f2
TL
771 return;
772 }
7c673cae 773
9f95a23c
TL
774 if (share_map_update) {
775 osd->maybe_share_map(con.get(), get_osdmap());
7c673cae 776 }
9f95a23c
TL
777 osd->send_message_osd_cluster(m, con.get());
778}
7c673cae 779
9f95a23c
TL
780void PG::clear_probe_targets()
781{
782 std::lock_guard l(heartbeat_peer_lock);
783 probe_targets.clear();
784}
7c673cae 785
9f95a23c
TL
786void PG::update_heartbeat_peers(set<int> new_peers)
787{
788 bool need_update = false;
789 heartbeat_peer_lock.lock();
790 if (new_peers == heartbeat_peers) {
791 dout(10) << "update_heartbeat_peers " << heartbeat_peers << " unchanged" << dendl;
792 } else {
793 dout(10) << "update_heartbeat_peers " << heartbeat_peers << " -> " << new_peers << dendl;
794 heartbeat_peers.swap(new_peers);
795 need_update = true;
11fdf7f2 796 }
9f95a23c 797 heartbeat_peer_lock.unlock();
11fdf7f2 798
9f95a23c
TL
799 if (need_update)
800 osd->need_heartbeat_peer_update();
801}
11fdf7f2 802
11fdf7f2 803
9f95a23c
TL
804bool PG::check_in_progress_op(
805 const osd_reqid_t &r,
806 eversion_t *version,
807 version_t *user_version,
808 int *return_code,
809 vector<pg_log_op_return_item_t> *op_returns
810 ) const
811{
812 return (
813 projected_log.get_request(r, version, user_version, return_code,
814 op_returns) ||
815 recovery_state.get_pg_log().get_log().get_request(
816 r, version, user_version, return_code, op_returns));
11fdf7f2
TL
817}
818
9f95a23c 819void PG::publish_stats_to_osd()
11fdf7f2 820{
9f95a23c
TL
821 if (!is_primary())
822 return;
11fdf7f2 823
9f95a23c
TL
824 std::lock_guard l{pg_stats_publish_lock};
825 auto stats = recovery_state.prepare_stats_for_publish(
826 pg_stats_publish_valid,
827 pg_stats_publish,
828 unstable_stats);
829 if (stats) {
830 pg_stats_publish = stats.value();
831 pg_stats_publish_valid = true;
11fdf7f2 832 }
11fdf7f2
TL
833}
834
9f95a23c 835unsigned PG::get_target_pg_log_entries() const
11fdf7f2 836{
9f95a23c 837 return osd->get_target_pg_log_entries();
11fdf7f2
TL
838}
839
9f95a23c 840void PG::clear_publish_stats()
11fdf7f2 841{
9f95a23c
TL
842 dout(15) << "clear_stats" << dendl;
843 std::lock_guard l{pg_stats_publish_lock};
844 pg_stats_publish_valid = false;
7c673cae
FG
845}
846
847/**
9f95a23c 848 * initialize a newly instantiated pg
7c673cae 849 *
9f95a23c
TL
850 * Initialize PG state, as when a PG is initially created, or when it
851 * is first instantiated on the current node.
7c673cae 852 *
9f95a23c
TL
853 * @param role our role/rank
854 * @param newup up set
855 * @param newacting acting set
856 * @param history pg history
857 * @param pi past_intervals
858 * @param backfill true if info should be marked as backfill
859 * @param t transaction to write out our new state in
7c673cae 860 */
9f95a23c
TL
861void PG::init(
862 int role,
863 const vector<int>& newup, int new_up_primary,
864 const vector<int>& newacting, int new_acting_primary,
865 const pg_history_t& history,
866 const PastIntervals& pi,
867 bool backfill,
868 ObjectStore::Transaction &t)
869{
870 recovery_state.init(
871 role, newup, new_up_primary, newacting,
872 new_acting_primary, history, pi, backfill, t);
873}
7c673cae 874
9f95a23c
TL
875void PG::shutdown()
876{
877 ch->flush();
878 std::scoped_lock l{*this};
879 recovery_state.shutdown();
880 on_shutdown();
881}
7c673cae 882
9f95a23c
TL
883#pragma GCC diagnostic ignored "-Wpragmas"
884#pragma GCC diagnostic push
885#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
7c673cae 886
9f95a23c
TL
887void PG::upgrade(ObjectStore *store)
888{
889 dout(0) << __func__ << " " << info_struct_v << " -> " << pg_latest_struct_v
890 << dendl;
891 ceph_assert(info_struct_v <= 10);
892 ObjectStore::Transaction t;
7c673cae 893
9f95a23c 894 // <do upgrade steps here>
7c673cae 895
9f95a23c
TL
896 // finished upgrade!
897 ceph_assert(info_struct_v == 10);
7c673cae 898
9f95a23c
TL
899 // update infover_key
900 if (info_struct_v < pg_latest_struct_v) {
901 map<string,bufferlist> v;
902 __u8 ver = pg_latest_struct_v;
903 encode(ver, v[string(infover_key)]);
904 t.omap_setkeys(coll, pgmeta_oid, v);
7c673cae 905 }
7c673cae 906
9f95a23c 907 recovery_state.force_write_state(t);
7c673cae 908
9f95a23c
TL
909 ObjectStore::CollectionHandle ch = store->open_collection(coll);
910 int r = store->queue_transaction(ch, std::move(t));
911 if (r != 0) {
912 derr << __func__ << ": queue_transaction returned "
913 << cpp_strerror(r) << dendl;
914 ceph_abort();
7c673cae 915 }
9f95a23c 916 ceph_assert(r == 0);
7c673cae 917
9f95a23c
TL
918 C_SaferCond waiter;
919 if (!ch->flush_commit(&waiter)) {
920 waiter.wait();
7c673cae 921 }
9f95a23c 922}
7c673cae 923
9f95a23c
TL
924#pragma GCC diagnostic pop
925#pragma GCC diagnostic warning "-Wpragmas"
7c673cae 926
9f95a23c
TL
927void PG::prepare_write(
928 pg_info_t &info,
929 pg_info_t &last_written_info,
930 PastIntervals &past_intervals,
931 PGLog &pglog,
932 bool dirty_info,
933 bool dirty_big_info,
934 bool need_write_epoch,
935 ObjectStore::Transaction &t)
936{
937 info.stats.stats.add(unstable_stats);
938 unstable_stats.clear();
939 map<string,bufferlist> km;
940 string key_to_remove;
941 if (dirty_big_info || dirty_info) {
942 int ret = prepare_info_keymap(
943 cct,
944 &km,
945 &key_to_remove,
11fdf7f2 946 get_osdmap_epoch(),
9f95a23c
TL
947 info,
948 last_written_info,
949 past_intervals,
950 dirty_big_info,
951 need_write_epoch,
952 cct->_conf->osd_fast_info,
953 osd->logger,
954 this);
955 ceph_assert(ret == 0);
7c673cae 956 }
9f95a23c
TL
957 pglog.write_log_and_missing(
958 t, &km, coll, pgmeta_oid, pool.info.require_rollback());
959 if (!km.empty())
960 t.omap_setkeys(coll, pgmeta_oid, km);
961 if (!key_to_remove.empty())
962 t.omap_rmkey(coll, pgmeta_oid, key_to_remove);
963}
7c673cae 964
9f95a23c
TL
965#pragma GCC diagnostic ignored "-Wpragmas"
966#pragma GCC diagnostic push
967#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
7c673cae 968
9f95a23c
TL
969bool PG::_has_removal_flag(ObjectStore *store,
970 spg_t pgid)
971{
972 coll_t coll(pgid);
973 ghobject_t pgmeta_oid(pgid.make_pgmeta_oid());
7c673cae 974
9f95a23c
TL
975 // first try new way
976 set<string> keys;
977 keys.insert("_remove");
978 map<string,bufferlist> values;
979 auto ch = store->open_collection(coll);
980 ceph_assert(ch);
981 if (store->omap_get_values(ch, pgmeta_oid, keys, &values) == 0 &&
982 values.size() == 1)
983 return true;
7c673cae 984
9f95a23c
TL
985 return false;
986}
c07f9fc5 987
9f95a23c
TL
988int PG::peek_map_epoch(ObjectStore *store,
989 spg_t pgid,
990 epoch_t *pepoch)
991{
992 coll_t coll(pgid);
993 ghobject_t legacy_infos_oid(OSD::make_infos_oid());
994 ghobject_t pgmeta_oid(pgid.make_pgmeta_oid());
995 epoch_t cur_epoch = 0;
7c673cae 996
9f95a23c
TL
997 // validate collection name
998 ceph_assert(coll.is_pg());
7c673cae 999
9f95a23c
TL
1000 // try for v8
1001 set<string> keys;
1002 keys.insert(string(infover_key));
1003 keys.insert(string(epoch_key));
1004 map<string,bufferlist> values;
1005 auto ch = store->open_collection(coll);
1006 ceph_assert(ch);
1007 int r = store->omap_get_values(ch, pgmeta_oid, keys, &values);
1008 if (r == 0) {
1009 ceph_assert(values.size() == 2);
7c673cae 1010
9f95a23c
TL
1011 // sanity check version
1012 auto bp = values[string(infover_key)].cbegin();
1013 __u8 struct_v = 0;
1014 decode(struct_v, bp);
1015 ceph_assert(struct_v >= 8);
91327a77 1016
9f95a23c
TL
1017 // get epoch
1018 bp = values[string(epoch_key)].begin();
1019 decode(cur_epoch, bp);
1020 } else {
1021 // probably bug 10617; see OSD::load_pgs()
1022 return -1;
1023 }
7c673cae 1024
9f95a23c
TL
1025 *pepoch = cur_epoch;
1026 return 0;
1027}
7c673cae 1028
9f95a23c
TL
1029#pragma GCC diagnostic pop
1030#pragma GCC diagnostic warning "-Wpragmas"
7c673cae 1031
9f95a23c
TL
1032bool PG::check_log_for_corruption(ObjectStore *store)
1033{
1034 /// TODO: this method needs to work with the omap log
1035 return true;
1036}
7c673cae 1037
9f95a23c
TL
1038//! Get the name we're going to save our corrupt page log as
1039std::string PG::get_corrupt_pg_log_name() const
1040{
1041 const int MAX_BUF = 512;
1042 char buf[MAX_BUF];
1043 struct tm tm_buf;
1044 time_t my_time(time(NULL));
1045 const struct tm *t = localtime_r(&my_time, &tm_buf);
1046 int ret = strftime(buf, sizeof(buf), "corrupt_log_%Y-%m-%d_%k:%M_", t);
1047 if (ret == 0) {
1048 dout(0) << "strftime failed" << dendl;
1049 return "corrupt_log_unknown_time";
7c673cae 1050 }
9f95a23c
TL
1051 string out(buf);
1052 out += stringify(info.pgid);
1053 return out;
7c673cae
FG
1054}
1055
9f95a23c
TL
1056int PG::read_info(
1057 ObjectStore *store, spg_t pgid, const coll_t &coll,
1058 pg_info_t &info, PastIntervals &past_intervals,
1059 __u8 &struct_v)
7c673cae 1060{
9f95a23c
TL
1061 set<string> keys;
1062 keys.insert(string(infover_key));
1063 keys.insert(string(info_key));
1064 keys.insert(string(biginfo_key));
1065 keys.insert(string(fastinfo_key));
1066 ghobject_t pgmeta_oid(pgid.make_pgmeta_oid());
1067 map<string,bufferlist> values;
1068 auto ch = store->open_collection(coll);
1069 ceph_assert(ch);
1070 int r = store->omap_get_values(ch, pgmeta_oid, keys, &values);
1071 ceph_assert(r == 0);
1072 ceph_assert(values.size() == 3 ||
1073 values.size() == 4);
7c673cae 1074
9f95a23c
TL
1075 auto p = values[string(infover_key)].cbegin();
1076 decode(struct_v, p);
1077 ceph_assert(struct_v >= 10);
7c673cae 1078
9f95a23c
TL
1079 p = values[string(info_key)].begin();
1080 decode(info, p);
7c673cae 1081
9f95a23c
TL
1082 p = values[string(biginfo_key)].begin();
1083 decode(past_intervals, p);
1084 decode(info.purged_snaps, p);
7c673cae 1085
9f95a23c
TL
1086 p = values[string(fastinfo_key)].begin();
1087 if (!p.end()) {
1088 pg_fast_info_t fast;
1089 decode(fast, p);
1090 fast.try_apply_to(&info);
1091 }
1092 return 0;
7c673cae
FG
1093}
1094
9f95a23c
TL
1095void PG::read_state(ObjectStore *store)
1096{
1097 PastIntervals past_intervals_from_disk;
1098 pg_info_t info_from_disk;
1099 int r = read_info(
1100 store,
1101 pg_id,
1102 coll,
1103 info_from_disk,
1104 past_intervals_from_disk,
1105 info_struct_v);
1106 ceph_assert(r >= 0);
7c673cae 1107
9f95a23c
TL
1108 if (info_struct_v < pg_compat_struct_v) {
1109 derr << "PG needs upgrade, but on-disk data is too old; upgrade to"
1110 << " an older version first." << dendl;
1111 ceph_abort_msg("PG too old to upgrade");
1112 }
7c673cae 1113
9f95a23c
TL
1114 recovery_state.init_from_disk_state(
1115 std::move(info_from_disk),
1116 std::move(past_intervals_from_disk),
1117 [this, store] (PGLog &pglog) {
1118 ostringstream oss;
1119 pglog.read_log_and_missing(
1120 store,
1121 ch,
1122 pgmeta_oid,
1123 info,
1124 oss,
1125 cct->_conf->osd_ignore_stale_divergent_priors,
1126 cct->_conf->osd_debug_verify_missing_on_start);
1127
1128 if (oss.tellp())
1129 osd->clog->error() << oss.str();
1130 return 0;
1131 });
7c673cae 1132
9f95a23c
TL
1133 if (info_struct_v < pg_latest_struct_v) {
1134 upgrade(store);
7c673cae
FG
1135 }
1136
9f95a23c
TL
1137 // initialize current mapping
1138 {
1139 int primary, up_primary;
1140 vector<int> acting, up;
1141 get_osdmap()->pg_to_up_acting_osds(
1142 pg_id.pgid, &up, &up_primary, &acting, &primary);
1143 recovery_state.init_primary_up_acting(
1144 up,
1145 acting,
1146 up_primary,
1147 primary);
1148 recovery_state.set_role(OSDMap::calc_pg_role(pg_whoami, acting));
1149 }
1150
1151 // init pool options
1152 store->set_collection_opts(ch, pool.info.opts);
7c673cae 1153
9f95a23c
TL
1154 PeeringCtx rctx(ceph_release_t::unknown);
1155 handle_initialize(rctx);
1156 // note: we don't activate here because we know the OSD will advance maps
1157 // during boot.
1158 write_if_dirty(rctx.transaction);
1159 store->queue_transaction(ch, std::move(rctx.transaction));
7c673cae
FG
1160}
1161
9f95a23c
TL
1162void PG::update_snap_map(
1163 const vector<pg_log_entry_t> &log_entries,
1164 ObjectStore::Transaction &t)
7c673cae 1165{
f67539c2 1166 for (auto i = log_entries.cbegin(); i != log_entries.cend(); ++i) {
9f95a23c
TL
1167 OSDriver::OSTransaction _t(osdriver.get_transaction(&t));
1168 if (i->soid.snap < CEPH_MAXSNAP) {
1169 if (i->is_delete()) {
1170 int r = snap_mapper.remove_oid(
1171 i->soid,
1172 &_t);
f67539c2 1173 if (r)
9f95a23c
TL
1174 derr << __func__ << " remove_oid " << i->soid << " failed with " << r << dendl;
1175 // On removal tolerate missing key corruption
1176 ceph_assert(r == 0 || r == -ENOENT);
1177 } else if (i->is_update()) {
1178 ceph_assert(i->snaps.length() > 0);
1179 vector<snapid_t> snaps;
1180 bufferlist snapbl = i->snaps;
1181 auto p = snapbl.cbegin();
1182 try {
1183 decode(snaps, p);
1184 } catch (...) {
1185 derr << __func__ << " decode snaps failure on " << *i << dendl;
1186 snaps.clear();
1187 }
1188 set<snapid_t> _snaps(snaps.begin(), snaps.end());
7c673cae 1189
9f95a23c
TL
1190 if (i->is_clone() || i->is_promote()) {
1191 snap_mapper.add_oid(
1192 i->soid,
1193 _snaps,
1194 &_t);
1195 } else if (i->is_modify()) {
1196 int r = snap_mapper.update_snaps(
1197 i->soid,
1198 _snaps,
1199 0,
1200 &_t);
1201 ceph_assert(r == 0);
11fdf7f2 1202 } else {
9f95a23c 1203 ceph_assert(i->is_clean());
11fdf7f2
TL
1204 }
1205 }
11fdf7f2
TL
1206 }
1207 }
7c673cae
FG
1208}
1209
9f95a23c
TL
1210/**
1211 * filter trimming|trimmed snaps out of snapcontext
1212 */
1213void PG::filter_snapc(vector<snapid_t> &snaps)
7c673cae 1214{
9f95a23c
TL
1215 // nothing needs to trim, we can return immediately
1216 if (snap_trimq.empty() && info.purged_snaps.empty())
1217 return;
1218
1219 bool filtering = false;
1220 vector<snapid_t> newsnaps;
1221 for (vector<snapid_t>::iterator p = snaps.begin();
1222 p != snaps.end();
1223 ++p) {
1224 if (snap_trimq.contains(*p) || info.purged_snaps.contains(*p)) {
1225 if (!filtering) {
1226 // start building a new vector with what we've seen so far
1227 dout(10) << "filter_snapc filtering " << snaps << dendl;
1228 newsnaps.insert(newsnaps.begin(), snaps.begin(), p);
1229 filtering = true;
1230 }
1231 dout(20) << "filter_snapc removing trimq|purged snap " << *p << dendl;
1232 } else {
1233 if (filtering)
1234 newsnaps.push_back(*p); // continue building new vector
d2e6a577 1235 }
c07f9fc5 1236 }
9f95a23c
TL
1237 if (filtering) {
1238 snaps.swap(newsnaps);
1239 dout(10) << "filter_snapc result " << snaps << dendl;
a8e16298 1240 }
a8e16298
TL
1241}
1242
9f95a23c 1243void PG::requeue_object_waiters(map<hobject_t, list<OpRequestRef>>& m)
a8e16298 1244{
f67539c2 1245 for (auto it = m.begin(); it != m.end(); ++it)
9f95a23c
TL
1246 requeue_ops(it->second);
1247 m.clear();
c07f9fc5 1248}
7c673cae 1249
9f95a23c 1250void PG::requeue_op(OpRequestRef op)
c07f9fc5 1251{
9f95a23c
TL
1252 auto p = waiting_for_map.find(op->get_source());
1253 if (p != waiting_for_map.end()) {
1254 dout(20) << __func__ << " " << op << " (waiting_for_map " << p->first << ")"
1255 << dendl;
1256 p->second.push_front(op);
c07f9fc5 1257 } else {
9f95a23c
TL
1258 dout(20) << __func__ << " " << op << dendl;
1259 osd->enqueue_front(
1260 OpSchedulerItem(
1261 unique_ptr<OpSchedulerItem::OpQueueable>(new PGOpItem(info.pgid, op)),
1262 op->get_req()->get_cost(),
1263 op->get_req()->get_priority(),
1264 op->get_req()->get_recv_stamp(),
1265 op->get_req()->get_source().num(),
1266 get_osdmap_epoch()));
7c673cae 1267 }
c07f9fc5 1268}
7c673cae 1269
9f95a23c 1270void PG::requeue_ops(list<OpRequestRef> &ls)
c07f9fc5 1271{
9f95a23c
TL
1272 for (list<OpRequestRef>::reverse_iterator i = ls.rbegin();
1273 i != ls.rend();
1274 ++i) {
1275 requeue_op(*i);
c07f9fc5 1276 }
9f95a23c 1277 ls.clear();
7c673cae
FG
1278}
1279
9f95a23c 1280void PG::requeue_map_waiters()
7c673cae 1281{
9f95a23c
TL
1282 epoch_t epoch = get_osdmap_epoch();
1283 auto p = waiting_for_map.begin();
1284 while (p != waiting_for_map.end()) {
1285 if (epoch < p->second.front()->min_epoch) {
1286 dout(20) << __func__ << " " << p->first << " front op "
1287 << p->second.front() << " must still wait, doing nothing"
1288 << dendl;
1289 ++p;
1290 } else {
1291 dout(20) << __func__ << " " << p->first << " " << p->second << dendl;
1292 for (auto q = p->second.rbegin(); q != p->second.rend(); ++q) {
1293 auto req = *q;
1294 osd->enqueue_front(OpSchedulerItem(
1295 unique_ptr<OpSchedulerItem::OpQueueable>(new PGOpItem(info.pgid, req)),
1296 req->get_req()->get_cost(),
1297 req->get_req()->get_priority(),
1298 req->get_req()->get_recv_stamp(),
1299 req->get_req()->get_source().num(),
1300 epoch));
1301 }
1302 p = waiting_for_map.erase(p);
c07f9fc5 1303 }
9f95a23c
TL
1304 }
1305}
7c673cae 1306
f67539c2
TL
1307bool PG::get_must_scrub() const
1308{
1309 dout(20) << __func__ << " must_scrub? " << (m_planned_scrub.must_scrub ? "true" : "false") << dendl;
1310 return m_planned_scrub.must_scrub;
1311}
1312
1313unsigned int PG::scrub_requeue_priority(Scrub::scrub_prio_t with_priority) const
1314{
1315 return m_scrubber->scrub_requeue_priority(with_priority);
1316}
1317
1318unsigned int PG::scrub_requeue_priority(Scrub::scrub_prio_t with_priority, unsigned int suggested_priority) const
1319{
1320 return m_scrubber->scrub_requeue_priority(with_priority, suggested_priority);
1321}
7c673cae 1322
9f95a23c
TL
1323// ==========================================================================================
1324// SCRUB
7c673cae 1325
9f95a23c 1326/*
f67539c2
TL
1327 * implementation note:
1328 * PG::sched_scrub() is called only once per a specific scrub session.
1329 * That call commits us to the whatever choices are made (deep/shallow, etc').
1330 * Unless failing to start scrubbing, the 'planned scrub' flag-set is 'frozen' into
1331 * PgScrubber's m_flags, then cleared.
9f95a23c 1332 */
9f95a23c 1333bool PG::sched_scrub()
11fdf7f2 1334{
f67539c2
TL
1335 dout(15) << __func__ << " pg(" << info.pgid
1336 << (is_active() ? ") <active>" : ") <not-active>")
1337 << (is_clean() ? " <clean>" : " <not-clean>") << dendl;
9f95a23c
TL
1338 ceph_assert(ceph_mutex_is_locked(_lock));
1339 ceph_assert(!is_scrubbing());
f67539c2
TL
1340
1341 if (!is_primary() || !is_active() || !is_clean()) {
9f95a23c 1342 return false;
11fdf7f2 1343 }
11fdf7f2 1344
f67539c2
TL
1345 if (scrub_queued) {
1346 // only applicable to the very first time a scrub event is queued
1347 // (until handled and posted to the scrub FSM)
1348 dout(10) << __func__ << ": already queued" << dendl;
1349 return false;
1350 }
7c673cae 1351
f67539c2
TL
1352 // analyse the combination of the requested scrub flags, the osd/pool configuration
1353 // and the PG status to determine whether we should scrub now, and what type of scrub
1354 // should that be.
1355 auto updated_flags = verify_scrub_mode();
1356 if (!updated_flags) {
1357 // the stars do not align for starting a scrub for this PG at this time
1358 // (due to configuration or priority issues)
1359 // The reason was already reported by the callee.
1360 dout(10) << __func__ << ": failed to initiate a scrub" << dendl;
1361 return false;
1362 }
7c673cae 1363
f67539c2
TL
1364 // try to reserve the local OSD resources. If failing: no harm. We will
1365 // be retried by the OSD later on.
1366 if (!m_scrubber->reserve_local()) {
1367 dout(10) << __func__ << ": failed to reserve locally" << dendl;
1368 return false;
1369 }
7c673cae 1370
f67539c2
TL
1371 // can commit to the updated flags now, as nothing will stop the scrub
1372 m_planned_scrub = *updated_flags;
9f95a23c 1373
f67539c2
TL
1374 // An interrupted recovery repair could leave this set.
1375 state_clear(PG_STATE_REPAIR);
9f95a23c 1376
f67539c2
TL
1377 // Pass control to the scrubber. It is the scrubber that handles the replicas'
1378 // resources reservations.
1379 m_scrubber->set_op_parameters(m_planned_scrub);
9f95a23c 1380
f67539c2
TL
1381 dout(10) << __func__ << ": queueing" << dendl;
1382
1383 scrub_queued = true;
1384 osd->queue_for_scrub(this, Scrub::scrub_prio_t::low_priority);
1385 return true;
1386}
1387
1388double PG::next_deepscrub_interval() const
1389{
1390 double deep_scrub_interval =
1391 pool.info.opts.value_or(pool_opts_t::DEEP_SCRUB_INTERVAL, 0.0);
1392 if (deep_scrub_interval <= 0.0)
1393 deep_scrub_interval = cct->_conf->osd_deep_scrub_interval;
1394 return info.history.last_deep_scrub_stamp + deep_scrub_interval;
1395}
1396
1397bool PG::is_time_for_deep(bool allow_deep_scrub,
1398 bool allow_scrub,
1399 bool has_deep_errors,
1400 const requested_scrub_t& planned) const
1401{
1402 dout(10) << __func__ << ": need_auto?" << planned.need_auto << " allow_deep_scrub? " << allow_deep_scrub << dendl;
1403
1404 if (!allow_deep_scrub)
1405 return false;
1406
1407 if (planned.need_auto) {
1408 dout(10) << __func__ << ": need repair after scrub errors" << dendl;
1409 return true;
1410 }
1411
1412 if (ceph_clock_now() >= next_deepscrub_interval())
1413 return true;
1414
1415 if (has_deep_errors) {
1416 osd->clog->info() << "osd." << osd->whoami << " pg " << info.pgid
1417 << " Deep scrub errors, upgrading scrub to deep-scrub";
1418 return true;
9f95a23c
TL
1419 }
1420
f67539c2
TL
1421 // we only flip coins if 'allow_scrub' is asserted. Otherwise - as this function is
1422 // called often, we will probably be deep-scrubbing most of the time.
1423 if (allow_scrub) {
1424 bool deep_coin_flip =
1425 (rand() % 100) < cct->_conf->osd_deep_scrub_randomize_ratio * 100;
1426
1427 dout(15) << __func__ << ": time_for_deep=" << planned.time_for_deep
1428 << " deep_coin_flip=" << deep_coin_flip << dendl;
1429
1430 if (deep_coin_flip)
1431 return true;
1432 }
1433
1434 return false;
1435}
1436
1437bool PG::verify_periodic_scrub_mode(bool allow_deep_scrub,
1438 bool try_to_auto_repair,
1439 bool allow_regular_scrub,
1440 bool has_deep_errors,
1441 requested_scrub_t& planned) const
1442
1443{
1444 ceph_assert(!planned.must_deep_scrub && !planned.must_repair);
1445
1446 if (!allow_deep_scrub && has_deep_errors) {
1447 osd->clog->error()
1448 << "osd." << osd->whoami << " pg " << info.pgid
1449 << " Regular scrub skipped due to deep-scrub errors and nodeep-scrub set";
9f95a23c 1450 return false;
f67539c2
TL
1451 }
1452
1453 if (allow_deep_scrub) {
1454 // Initial entry and scheduled scrubs without nodeep_scrub set get here
1455
1456 planned.time_for_deep =
1457 is_time_for_deep(allow_deep_scrub, allow_regular_scrub, has_deep_errors, planned);
1458
1459 if (try_to_auto_repair) {
1460 if (planned.time_for_deep) {
1461 dout(20) << __func__ << ": auto repair with deep scrubbing" << dendl;
1462 planned.auto_repair = true;
1463 } else if (allow_regular_scrub) {
1464 dout(20) << __func__ << ": auto repair with scrubbing, rescrub if errors found"
1465 << dendl;
1466 planned.deep_scrub_on_error = true;
9f95a23c 1467 }
7c673cae 1468 }
7c673cae 1469 }
f67539c2
TL
1470
1471 dout(20) << __func__ << " updated flags: " << planned
1472 << " allow_regular_scrub: " << allow_regular_scrub << dendl;
1473
1474 // NOSCRUB so skip regular scrubs
1475 if (!allow_regular_scrub && !planned.time_for_deep) {
1476 return false;
1477 }
1478
9f95a23c 1479 return true;
7c673cae
FG
1480}
1481
f67539c2 1482std::optional<requested_scrub_t> PG::verify_scrub_mode() const
7c673cae 1483{
f67539c2 1484 dout(10) << __func__ << " processing pg " << info.pgid << dendl;
7c673cae 1485
f67539c2
TL
1486 bool allow_deep_scrub = !(get_osdmap()->test_flag(CEPH_OSDMAP_NODEEP_SCRUB) ||
1487 pool.info.has_flag(pg_pool_t::FLAG_NODEEP_SCRUB));
1488 bool allow_regular_scrub = !(get_osdmap()->test_flag(CEPH_OSDMAP_NOSCRUB) ||
1489 pool.info.has_flag(pg_pool_t::FLAG_NOSCRUB));
1490 bool has_deep_errors = (info.stats.stats.sum.num_deep_scrub_errors > 0);
1491 bool try_to_auto_repair =
1492 (cct->_conf->osd_scrub_auto_repair && get_pgbackend()->auto_repair_supported());
7c673cae 1493
f67539c2
TL
1494 auto upd_flags = m_planned_scrub;
1495
1496 upd_flags.time_for_deep = false;
1497 // Clear these in case user issues the scrub/repair command during
1498 // the scheduling of the scrub/repair (e.g. request reservation)
1499 upd_flags.deep_scrub_on_error = false;
1500 upd_flags.auto_repair = false;
1501
1502 if (upd_flags.must_scrub && !upd_flags.must_deep_scrub && has_deep_errors) {
1503 osd->clog->error() << "osd." << osd->whoami << " pg " << info.pgid
1504 << " Regular scrub request, deep-scrub details will be lost";
1505 }
1506
1507 if (!upd_flags.must_scrub) {
1508 // All periodic scrub handling goes here because must_scrub is
1509 // always set for must_deep_scrub and must_repair.
1510
1511 bool can_start_periodic =
1512 verify_periodic_scrub_mode(allow_deep_scrub, try_to_auto_repair,
1513 allow_regular_scrub, has_deep_errors, upd_flags);
1514 if (!can_start_periodic) {
1515 return std::nullopt;
1516 }
1517 }
1518
1519 // scrubbing while recovering?
1520
1521 bool prevented_by_recovery =
1522 osd->is_recovery_active() && !cct->_conf->osd_scrub_during_recovery &&
1523 (!cct->_conf->osd_repair_during_recovery || !upd_flags.must_repair);
1524
1525 if (prevented_by_recovery) {
1526 dout(20) << __func__ << ": scrubbing prevented during recovery" << dendl;
1527 return std::nullopt;
7c673cae 1528 }
f67539c2
TL
1529
1530 upd_flags.need_auto = false;
1531 return upd_flags;
7c673cae
FG
1532}
1533
f67539c2 1534void PG::reg_next_scrub()
7c673cae 1535{
f67539c2 1536 m_scrubber->reg_next_scrub(m_planned_scrub);
9f95a23c 1537}
7c673cae 1538
9f95a23c
TL
1539void PG::on_info_history_change()
1540{
f67539c2
TL
1541 if (m_scrubber) {
1542 m_scrubber->unreg_next_scrub();
1543 m_scrubber->reg_next_scrub(m_planned_scrub);
1544 }
9f95a23c 1545}
7c673cae 1546
f67539c2 1547void PG::scrub_requested(scrub_level_t scrub_level, scrub_type_t scrub_type)
9f95a23c 1548{
f67539c2 1549 m_scrubber->scrub_requested(scrub_level, scrub_type, m_planned_scrub);
9f95a23c 1550}
7c673cae 1551
9f95a23c
TL
1552void PG::clear_ready_to_merge() {
1553 osd->clear_ready_to_merge(this);
1554}
7c673cae 1555
9f95a23c
TL
1556void PG::queue_want_pg_temp(const vector<int> &wanted) {
1557 osd->queue_want_pg_temp(get_pgid().pgid, wanted);
1558}
7c673cae 1559
9f95a23c
TL
1560void PG::clear_want_pg_temp() {
1561 osd->remove_want_pg_temp(get_pgid().pgid);
1562}
7c673cae 1563
9f95a23c
TL
1564void PG::on_role_change() {
1565 requeue_ops(waiting_for_peered);
1566 plpg_on_role_change();
1567}
7c673cae 1568
9f95a23c 1569void PG::on_new_interval() {
f67539c2 1570 dout(20) << __func__ << " scrub_queued was " << scrub_queued << " flags: " << m_planned_scrub << dendl;
9f95a23c
TL
1571 scrub_queued = false;
1572 projected_last_update = eversion_t();
1573 cancel_recovery();
1574}
7c673cae 1575
9f95a23c
TL
1576epoch_t PG::oldest_stored_osdmap() {
1577 return osd->get_superblock().oldest_map;
1578}
7c673cae 1579
9f95a23c
TL
1580OstreamTemp PG::get_clog_info() {
1581 return osd->clog->info();
1582}
7c673cae 1583
9f95a23c
TL
1584OstreamTemp PG::get_clog_debug() {
1585 return osd->clog->debug();
1586}
7c673cae 1587
9f95a23c
TL
1588OstreamTemp PG::get_clog_error() {
1589 return osd->clog->error();
1590}
7c673cae 1591
9f95a23c
TL
1592void PG::schedule_event_after(
1593 PGPeeringEventRef event,
1594 float delay) {
1595 std::lock_guard lock(osd->recovery_request_lock);
1596 osd->recovery_request_timer.add_event_after(
1597 delay,
1598 new QueuePeeringEvt(
1599 this,
1600 std::move(event)));
1601}
7c673cae 1602
9f95a23c
TL
1603void PG::request_local_background_io_reservation(
1604 unsigned priority,
f67539c2
TL
1605 PGPeeringEventURef on_grant,
1606 PGPeeringEventURef on_preempt) {
9f95a23c
TL
1607 osd->local_reserver.request_reservation(
1608 pg_id,
1609 on_grant ? new QueuePeeringEvt(
f67539c2 1610 this, std::move(on_grant)) : nullptr,
9f95a23c
TL
1611 priority,
1612 on_preempt ? new QueuePeeringEvt(
f67539c2 1613 this, std::move(on_preempt)) : nullptr);
9f95a23c 1614}
7c673cae 1615
9f95a23c
TL
1616void PG::update_local_background_io_priority(
1617 unsigned priority) {
1618 osd->local_reserver.update_priority(
1619 pg_id,
1620 priority);
1621}
7c673cae 1622
9f95a23c
TL
1623void PG::cancel_local_background_io_reservation() {
1624 osd->local_reserver.cancel_reservation(
1625 pg_id);
1626}
7c673cae 1627
9f95a23c
TL
1628void PG::request_remote_recovery_reservation(
1629 unsigned priority,
f67539c2
TL
1630 PGPeeringEventURef on_grant,
1631 PGPeeringEventURef on_preempt) {
9f95a23c
TL
1632 osd->remote_reserver.request_reservation(
1633 pg_id,
1634 on_grant ? new QueuePeeringEvt(
f67539c2 1635 this, std::move(on_grant)) : nullptr,
9f95a23c
TL
1636 priority,
1637 on_preempt ? new QueuePeeringEvt(
f67539c2 1638 this, std::move(on_preempt)) : nullptr);
9f95a23c 1639}
11fdf7f2 1640
9f95a23c
TL
1641void PG::cancel_remote_recovery_reservation() {
1642 osd->remote_reserver.cancel_reservation(
1643 pg_id);
7c673cae
FG
1644}
1645
9f95a23c
TL
1646void PG::schedule_event_on_commit(
1647 ObjectStore::Transaction &t,
1648 PGPeeringEventRef on_commit)
11fdf7f2 1649{
9f95a23c 1650 t.register_on_commit(new QueuePeeringEvt(this, on_commit));
11fdf7f2
TL
1651}
1652
f67539c2
TL
1653void PG::on_activate(interval_set<snapid_t> snaps)
1654{
1655 ceph_assert(!m_scrubber->are_callbacks_pending());
1656 ceph_assert(callbacks_for_degraded_object.empty());
1657 snap_trimq = snaps;
1658 release_pg_backoffs();
1659 projected_last_update = info.last_update;
1660}
1661
9f95a23c 1662void PG::on_active_exit()
11fdf7f2 1663{
9f95a23c
TL
1664 backfill_reserving = false;
1665 agent_stop();
11fdf7f2
TL
1666}
1667
9f95a23c 1668void PG::on_active_advmap(const OSDMapRef &osdmap)
11fdf7f2 1669{
9f95a23c
TL
1670 const auto& new_removed_snaps = osdmap->get_new_removed_snaps();
1671 auto i = new_removed_snaps.find(get_pgid().pool());
1672 if (i != new_removed_snaps.end()) {
1673 bool bad = false;
1674 for (auto j : i->second) {
1675 if (snap_trimq.intersects(j.first, j.second)) {
1676 decltype(snap_trimq) added, overlap;
1677 added.insert(j.first, j.second);
1678 overlap.intersection_of(snap_trimq, added);
1679 derr << __func__ << " removed_snaps already contains "
1680 << overlap << dendl;
1681 bad = true;
1682 snap_trimq.union_of(added);
1683 } else {
1684 snap_trimq.insert(j.first, j.second);
1685 }
1686 }
1687 dout(10) << __func__ << " new removed_snaps " << i->second
1688 << ", snap_trimq now " << snap_trimq << dendl;
1689 ceph_assert(!bad || !cct->_conf->osd_debug_verify_cached_snaps);
1690 }
1691
1692 const auto& new_purged_snaps = osdmap->get_new_purged_snaps();
1693 auto j = new_purged_snaps.find(get_pgid().pgid.pool());
1694 if (j != new_purged_snaps.end()) {
1695 bool bad = false;
1696 for (auto k : j->second) {
1697 if (!recovery_state.get_info().purged_snaps.contains(k.first, k.second)) {
1698 interval_set<snapid_t> rm, overlap;
1699 rm.insert(k.first, k.second);
1700 overlap.intersection_of(recovery_state.get_info().purged_snaps, rm);
1701 derr << __func__ << " purged_snaps does not contain "
1702 << rm << ", only " << overlap << dendl;
1703 recovery_state.adjust_purged_snaps(
1704 [&overlap](auto &purged_snaps) {
1705 purged_snaps.subtract(overlap);
1706 });
1707 // This can currently happen in the normal (if unlikely) course of
1708 // events. Because adding snaps to purged_snaps does not increase
1709 // the pg version or add a pg log entry, we don't reliably propagate
1710 // purged_snaps additions to other OSDs.
1711 // One example:
1712 // - purge S
1713 // - primary and replicas update purged_snaps
1714 // - no object updates
1715 // - pg mapping changes, new primary on different node
1716 // - new primary pg version == eversion_t(), so info is not
1717 // propagated.
1718 //bad = true;
1719 } else {
1720 recovery_state.adjust_purged_snaps(
1721 [&k](auto &purged_snaps) {
1722 purged_snaps.erase(k.first, k.second);
1723 });
11fdf7f2
TL
1724 }
1725 }
9f95a23c
TL
1726 dout(10) << __func__ << " new purged_snaps " << j->second
1727 << ", now " << recovery_state.get_info().purged_snaps << dendl;
1728 ceph_assert(!bad || !cct->_conf->osd_debug_verify_cached_snaps);
11fdf7f2 1729 }
11fdf7f2
TL
1730}
1731
9f95a23c 1732void PG::queue_snap_retrim(snapid_t snap)
7c673cae 1733{
9f95a23c
TL
1734 if (!is_active() ||
1735 !is_primary()) {
1736 dout(10) << __func__ << " snap " << snap << " - not active and primary"
1737 << dendl;
7c673cae 1738 return;
7c673cae 1739 }
9f95a23c
TL
1740 if (!snap_trimq.contains(snap)) {
1741 snap_trimq.insert(snap);
1742 snap_trimq_repeat.insert(snap);
1743 dout(20) << __func__ << " snap " << snap
1744 << ", trimq now " << snap_trimq
1745 << ", repeat " << snap_trimq_repeat << dendl;
1746 kick_snap_trim();
1747 } else {
1748 dout(20) << __func__ << " snap " << snap
1749 << " already in trimq " << snap_trimq << dendl;
7c673cae 1750 }
7c673cae
FG
1751}
1752
9f95a23c 1753void PG::on_active_actmap()
7c673cae 1754{
9f95a23c
TL
1755 if (cct->_conf->osd_check_for_log_corruption)
1756 check_log_for_corruption(osd->store);
1757
1758
1759 if (recovery_state.is_active()) {
1760 dout(10) << "Active: kicking snap trim" << dendl;
1761 kick_snap_trim();
7c673cae 1762 }
9f95a23c
TL
1763
1764 if (recovery_state.is_peered() &&
1765 !recovery_state.is_clean() &&
1766 !recovery_state.get_osdmap()->test_flag(CEPH_OSDMAP_NOBACKFILL) &&
1767 (!recovery_state.get_osdmap()->test_flag(CEPH_OSDMAP_NOREBALANCE) ||
1768 recovery_state.is_degraded())) {
1769 queue_recovery();
7c673cae
FG
1770 }
1771}
1772
9f95a23c 1773void PG::on_backfill_reserved()
7c673cae 1774{
9f95a23c
TL
1775 backfill_reserving = false;
1776 queue_recovery();
7c673cae
FG
1777}
1778
9f95a23c 1779void PG::on_backfill_canceled()
7c673cae 1780{
9f95a23c
TL
1781 if (!waiting_on_backfill.empty()) {
1782 waiting_on_backfill.clear();
1783 finish_recovery_op(hobject_t::get_max());
7c673cae
FG
1784 }
1785}
1786
9f95a23c 1787void PG::on_recovery_reserved()
7c673cae 1788{
9f95a23c 1789 queue_recovery();
7c673cae
FG
1790}
1791
9f95a23c 1792void PG::set_not_ready_to_merge_target(pg_t pgid, pg_t src)
7c673cae 1793{
9f95a23c 1794 osd->set_not_ready_to_merge_target(pgid, src);
7c673cae
FG
1795}
1796
9f95a23c 1797void PG::set_not_ready_to_merge_source(pg_t pgid)
7c673cae 1798{
9f95a23c 1799 osd->set_not_ready_to_merge_source(pgid);
7c673cae
FG
1800}
1801
9f95a23c 1802void PG::set_ready_to_merge_target(eversion_t lu, epoch_t les, epoch_t lec)
7c673cae 1803{
9f95a23c 1804 osd->set_ready_to_merge_target(this, lu, les, lec);
7c673cae
FG
1805}
1806
9f95a23c 1807void PG::set_ready_to_merge_source(eversion_t lu)
7c673cae 1808{
9f95a23c 1809 osd->set_ready_to_merge_source(this, lu);
7c673cae
FG
1810}
1811
9f95a23c 1812void PG::send_pg_created(pg_t pgid)
7c673cae 1813{
9f95a23c
TL
1814 osd->send_pg_created(pgid);
1815}
7c673cae 1816
9f95a23c
TL
1817ceph::signedspan PG::get_mnow()
1818{
1819 return osd->get_mnow();
1820}
7c673cae 1821
9f95a23c
TL
1822HeartbeatStampsRef PG::get_hb_stamps(int peer)
1823{
1824 return osd->get_hb_stamps(peer);
7c673cae
FG
1825}
1826
9f95a23c
TL
1827void PG::schedule_renew_lease(epoch_t lpr, ceph::timespan delay)
1828{
1829 auto spgid = info.pgid;
1830 auto o = osd;
1831 osd->mono_timer.add_event(
1832 delay,
1833 [o, lpr, spgid]() {
1834 o->queue_renew_lease(lpr, spgid);
1835 });
1836}
7c673cae 1837
9f95a23c 1838void PG::queue_check_readable(epoch_t lpr, ceph::timespan delay)
7c673cae 1839{
9f95a23c 1840 osd->queue_check_readable(info.pgid, lpr, delay);
7c673cae
FG
1841}
1842
9f95a23c 1843void PG::rebuild_missing_set_with_deletes(PGLog &pglog)
91327a77 1844{
9f95a23c
TL
1845 pglog.rebuild_missing_set_with_deletes(
1846 osd->store,
1847 ch,
1848 recovery_state.get_info());
91327a77
AA
1849}
1850
9f95a23c 1851void PG::on_activate_committed()
91327a77 1852{
9f95a23c
TL
1853 if (!is_primary()) {
1854 // waiters
1855 if (recovery_state.needs_flush() == 0) {
1856 requeue_ops(waiting_for_peered);
1857 } else if (!waiting_for_peered.empty()) {
1858 dout(10) << __func__ << " flushes in progress, moving "
1859 << waiting_for_peered.size() << " items to waiting_for_flush"
1860 << dendl;
1861 ceph_assert(waiting_for_flush.empty());
1862 waiting_for_flush.swap(waiting_for_peered);
91327a77 1863 }
9f95a23c
TL
1864 }
1865}
91327a77 1866
9f95a23c
TL
1867// Compute pending backfill data
1868static int64_t pending_backfill(CephContext *cct, int64_t bf_bytes, int64_t local_bytes)
11fdf7f2 1869{
9f95a23c
TL
1870 lgeneric_dout(cct, 20) << __func__ << " Adjust local usage "
1871 << (local_bytes >> 10) << "KiB"
1872 << " primary usage " << (bf_bytes >> 10)
1873 << "KiB" << dendl;
11fdf7f2 1874
9f95a23c
TL
1875 return std::max((int64_t)0, bf_bytes - local_bytes);
1876}
7c673cae 1877
7c673cae 1878
9f95a23c
TL
1879// We can zero the value of primary num_bytes as just an atomic.
1880// However, setting above zero reserves space for backfill and requires
1881// the OSDService::stat_lock which protects all OSD usage
1882bool PG::try_reserve_recovery_space(
1883 int64_t primary_bytes, int64_t local_bytes) {
1884 // Use tentative_bacfill_full() to make sure enough
1885 // space is available to handle target bytes from primary.
7c673cae 1886
9f95a23c
TL
1887 // TODO: If we passed num_objects from primary we could account for
1888 // an estimate of the metadata overhead.
7c673cae 1889
9f95a23c
TL
1890 // TODO: If we had compressed_allocated and compressed_original from primary
1891 // we could compute compression ratio and adjust accordingly.
7c673cae 1892
9f95a23c
TL
1893 // XXX: There is no way to get omap overhead and this would only apply
1894 // to whatever possibly different partition that is storing the database.
7c673cae 1895
9f95a23c
TL
1896 // update_osd_stat() from heartbeat will do this on a new
1897 // statfs using ps->primary_bytes.
1898 uint64_t pending_adjustment = 0;
1899 if (primary_bytes) {
1900 // For erasure coded pool overestimate by a full stripe per object
1901 // because we don't know how each objected rounded to the nearest stripe
1902 if (pool.info.is_erasure()) {
1903 primary_bytes /= (int)get_pgbackend()->get_ec_data_chunk_count();
1904 primary_bytes += get_pgbackend()->get_ec_stripe_chunk_size() *
1905 info.stats.stats.sum.num_objects;
1906 local_bytes /= (int)get_pgbackend()->get_ec_data_chunk_count();
1907 local_bytes += get_pgbackend()->get_ec_stripe_chunk_size() *
1908 info.stats.stats.sum.num_objects;
1909 }
1910 pending_adjustment = pending_backfill(
1911 cct,
1912 primary_bytes,
1913 local_bytes);
1914 dout(10) << __func__ << " primary_bytes " << (primary_bytes >> 10)
1915 << "KiB"
1916 << " local " << (local_bytes >> 10) << "KiB"
1917 << " pending_adjustments " << (pending_adjustment >> 10) << "KiB"
1918 << dendl;
7c673cae 1919 }
7c673cae 1920
9f95a23c
TL
1921 // This lock protects not only the stats OSDService but also setting the
1922 // pg primary_bytes. That's why we don't immediately unlock
1923 std::lock_guard l{osd->stat_lock};
1924 osd_stat_t cur_stat = osd->osd_stat;
1925 if (cct->_conf->osd_debug_reject_backfill_probability > 0 &&
1926 (rand()%1000 < (cct->_conf->osd_debug_reject_backfill_probability*1000.0))) {
1927 dout(10) << "backfill reservation rejected: failure injection"
1928 << dendl;
1929 return false;
1930 } else if (!cct->_conf->osd_debug_skip_full_check_in_backfill_reservation &&
1931 osd->tentative_backfill_full(this, pending_adjustment, cur_stat)) {
1932 dout(10) << "backfill reservation rejected: backfill full"
1933 << dendl;
1934 return false;
1935 } else {
1936 // Don't reserve space if skipped reservation check, this is used
1937 // to test the other backfill full check AND in case a corruption
1938 // of num_bytes requires ignoring that value and trying the
1939 // backfill anyway.
1940 if (primary_bytes &&
1941 !cct->_conf->osd_debug_skip_full_check_in_backfill_reservation) {
1942 primary_num_bytes.store(primary_bytes);
1943 local_num_bytes.store(local_bytes);
1944 } else {
1945 unreserve_recovery_space();
1946 }
1947 return true;
7c673cae
FG
1948 }
1949}
1950
9f95a23c
TL
1951void PG::unreserve_recovery_space() {
1952 primary_num_bytes.store(0);
1953 local_num_bytes.store(0);
7c673cae
FG
1954}
1955
9f95a23c 1956void PG::_scan_rollback_obs(const vector<ghobject_t> &rollback_obs)
7c673cae 1957{
9f95a23c
TL
1958 ObjectStore::Transaction t;
1959 eversion_t trimmed_to = recovery_state.get_last_rollback_info_trimmed_to_applied();
1960 for (vector<ghobject_t>::const_iterator i = rollback_obs.begin();
1961 i != rollback_obs.end();
1962 ++i) {
1963 if (i->generation < trimmed_to.version) {
1964 dout(10) << __func__ << "osd." << osd->whoami
1965 << " pg " << info.pgid
1966 << " found obsolete rollback obj "
1967 << *i << " generation < trimmed_to "
1968 << trimmed_to
1969 << "...repaired" << dendl;
1970 t.remove(coll, *i);
1971 }
1972 }
1973 if (!t.empty()) {
1974 derr << __func__ << ": queueing trans to clean up obsolete rollback objs"
1975 << dendl;
1976 osd->store->queue_transaction(ch, std::move(t), NULL);
1977 }
7c673cae
FG
1978}
1979
7c673cae 1980
9f95a23c 1981void PG::_repair_oinfo_oid(ScrubMap &smap)
7c673cae 1982{
9f95a23c
TL
1983 for (map<hobject_t, ScrubMap::object>::reverse_iterator i = smap.objects.rbegin();
1984 i != smap.objects.rend();
1985 ++i) {
1986 const hobject_t &hoid = i->first;
1987 ScrubMap::object &o = i->second;
7c673cae 1988
9f95a23c
TL
1989 bufferlist bl;
1990 if (o.attrs.find(OI_ATTR) == o.attrs.end()) {
1991 continue;
1992 }
1993 bl.push_back(o.attrs[OI_ATTR]);
1994 object_info_t oi;
1995 try {
1996 oi.decode(bl);
1997 } catch(...) {
1998 continue;
1999 }
2000 if (oi.soid != hoid) {
2001 ObjectStore::Transaction t;
2002 OSDriver::OSTransaction _t(osdriver.get_transaction(&t));
2003 osd->clog->error() << "osd." << osd->whoami
2004 << " found object info error on pg "
2005 << info.pgid
2006 << " oid " << hoid << " oid in object info: "
2007 << oi.soid
2008 << "...repaired";
2009 // Fix object info
2010 oi.soid = hoid;
2011 bl.clear();
2012 encode(oi, bl, get_osdmap()->get_features(CEPH_ENTITY_TYPE_OSD, nullptr));
7c673cae 2013
9f95a23c
TL
2014 bufferptr bp(bl.c_str(), bl.length());
2015 o.attrs[OI_ATTR] = bp;
7c673cae 2016
9f95a23c
TL
2017 t.setattr(coll, ghobject_t(hoid), OI_ATTR, bl);
2018 int r = osd->store->queue_transaction(ch, std::move(t));
2019 if (r != 0) {
2020 derr << __func__ << ": queue_transaction got " << cpp_strerror(r)
2021 << dendl;
2022 }
7c673cae
FG
2023 }
2024 }
7c673cae 2025}
7c673cae 2026
9f95a23c
TL
2027void PG::repair_object(
2028 const hobject_t &soid,
2029 const list<pair<ScrubMap::object, pg_shard_t> > &ok_peers,
2030 const set<pg_shard_t> &bad_peers)
2031{
2032 set<pg_shard_t> ok_shards;
2033 for (auto &&peer: ok_peers) ok_shards.insert(peer.second);
d2e6a577 2034
9f95a23c
TL
2035 dout(10) << "repair_object " << soid
2036 << " bad_peers osd.{" << bad_peers << "},"
2037 << " ok_peers osd.{" << ok_shards << "}" << dendl;
11fdf7f2 2038
9f95a23c
TL
2039 const ScrubMap::object &po = ok_peers.back().first;
2040 eversion_t v;
2041 object_info_t oi;
2042 try {
2043 bufferlist bv;
2044 if (po.attrs.count(OI_ATTR)) {
2045 bv.push_back(po.attrs.find(OI_ATTR)->second);
2046 }
2047 auto bliter = bv.cbegin();
2048 decode(oi, bliter);
2049 } catch (...) {
2050 dout(0) << __func__ << ": Need version of replica, bad object_info_t: "
2051 << soid << dendl;
2052 ceph_abort();
11fdf7f2
TL
2053 }
2054
9f95a23c
TL
2055 if (bad_peers.count(get_primary())) {
2056 // We should only be scrubbing if the PG is clean.
2057 ceph_assert(waiting_for_unreadable_object.empty());
2058 dout(10) << __func__ << ": primary = " << get_primary() << dendl;
11fdf7f2
TL
2059 }
2060
9f95a23c
TL
2061 /* No need to pass ok_peers, they must not be missing the object, so
2062 * force_object_missing will add them to missing_loc anyway */
2063 recovery_state.force_object_missing(bad_peers, soid, oi.version);
7c673cae
FG
2064}
2065
f67539c2 2066void PG::forward_scrub_event(ScrubAPI fn, epoch_t epoch_queued)
7c673cae 2067{
f67539c2
TL
2068 dout(20) << __func__ << " queued at: " << epoch_queued << dendl;
2069 if (is_active() && m_scrubber) {
2070 ((*m_scrubber).*fn)(epoch_queued);
7c673cae 2071 } else {
f67539c2
TL
2072 // pg might be in the process of being deleted
2073 dout(5) << __func__ << " refusing to forward. " << (is_clean() ? "(clean) " : "(not clean) ") <<
2074 (is_active() ? "(active) " : "(not active) ") << dendl;
7c673cae 2075 }
f67539c2 2076}
7c673cae 2077
f67539c2
TL
2078void PG::replica_scrub(OpRequestRef op, ThreadPool::TPHandle& handle)
2079{
2080 dout(10) << __func__ << " (op)" << dendl;
2081 if (m_scrubber)
2082 m_scrubber->replica_scrub_op(op);
7c673cae
FG
2083}
2084
f67539c2
TL
2085void PG::scrub(epoch_t epoch_queued, ThreadPool::TPHandle& handle)
2086{
2087 dout(10) << __func__ << " queued at: " << epoch_queued << dendl;
2088 // a new scrub
9f95a23c 2089 scrub_queued = false;
f67539c2 2090 forward_scrub_event(&ScrubPgIF::initiate_regular_scrub, epoch_queued);
7c673cae
FG
2091}
2092
f67539c2
TL
2093// note: no need to secure OSD resources for a recovery scrub
2094void PG::recovery_scrub(epoch_t epoch_queued,
2095 [[maybe_unused]] ThreadPool::TPHandle& handle)
f6b5b4d7 2096{
f67539c2
TL
2097 dout(10) << __func__ << " queued at: " << epoch_queued << dendl;
2098 // a new scrub
2099 scrub_queued = false;
2100 forward_scrub_event(&ScrubPgIF::initiate_scrub_after_repair, epoch_queued);
f6b5b4d7
TL
2101}
2102
f67539c2
TL
2103void PG::replica_scrub(epoch_t epoch_queued,
2104 [[maybe_unused]] ThreadPool::TPHandle& handle)
2105{
2106 dout(10) << __func__ << " queued at: " << epoch_queued
2107 << (is_primary() ? " (primary)" : " (replica)") << dendl;
2108 scrub_queued = false;
2109 forward_scrub_event(&ScrubPgIF::send_start_replica, epoch_queued);
7c673cae
FG
2110}
2111
f67539c2
TL
2112void PG::scrub_send_scrub_resched(epoch_t epoch_queued,
2113 [[maybe_unused]] ThreadPool::TPHandle& handle)
7c673cae 2114{
f67539c2
TL
2115 dout(10) << __func__ << " queued at: " << epoch_queued << dendl;
2116 scrub_queued = false;
2117 forward_scrub_event(&ScrubPgIF::send_scrub_resched, epoch_queued);
7c673cae
FG
2118}
2119
f67539c2
TL
2120void PG::scrub_send_resources_granted(epoch_t epoch_queued,
2121 [[maybe_unused]] ThreadPool::TPHandle& handle)
7c673cae 2122{
f67539c2
TL
2123 dout(10) << __func__ << " queued at: " << epoch_queued << dendl;
2124 forward_scrub_event(&ScrubPgIF::send_remotes_reserved, epoch_queued);
7c673cae
FG
2125}
2126
f67539c2
TL
2127void PG::scrub_send_resources_denied(epoch_t epoch_queued,
2128 [[maybe_unused]] ThreadPool::TPHandle& handle)
7c673cae 2129{
f67539c2
TL
2130 dout(10) << __func__ << " queued at: " << epoch_queued << dendl;
2131 forward_scrub_event(&ScrubPgIF::send_reservation_failure, epoch_queued);
7c673cae
FG
2132}
2133
f67539c2
TL
2134void PG::replica_scrub_resched(epoch_t epoch_queued,
2135 [[maybe_unused]] ThreadPool::TPHandle& handle)
7c673cae 2136{
f67539c2
TL
2137 dout(10) << __func__ << " queued at: " << epoch_queued << dendl;
2138 scrub_queued = false;
2139 forward_scrub_event(&ScrubPgIF::send_sched_replica, epoch_queued);
11fdf7f2
TL
2140}
2141
f67539c2
TL
2142void PG::scrub_send_pushes_update(epoch_t epoch_queued,
2143 [[maybe_unused]] ThreadPool::TPHandle& handle)
2144{
2145 dout(10) << __func__ << " queued at: " << epoch_queued << dendl;
2146 forward_scrub_event(&ScrubPgIF::active_pushes_notification, epoch_queued);
7c673cae
FG
2147}
2148
f67539c2
TL
2149void PG::scrub_send_replica_pushes(epoch_t epoch_queued,
2150 [[maybe_unused]] ThreadPool::TPHandle& handle)
2151{
2152 dout(15) << __func__ << " queued at: " << epoch_queued << dendl;
2153 forward_scrub_event(&ScrubPgIF::send_replica_pushes_upd, epoch_queued);
7c673cae
FG
2154}
2155
f67539c2
TL
2156void PG::scrub_send_applied_update(epoch_t epoch_queued,
2157 [[maybe_unused]] ThreadPool::TPHandle& handle)
7c673cae 2158{
f67539c2
TL
2159 dout(15) << __func__ << " queued at: " << epoch_queued << dendl;
2160 forward_scrub_event(&ScrubPgIF::update_applied_notification, epoch_queued);
2161}
7c673cae 2162
f67539c2
TL
2163void PG::scrub_send_unblocking(epoch_t epoch_queued,
2164 [[maybe_unused]] ThreadPool::TPHandle& handle)
2165{
2166 dout(15) << __func__ << " queued at: " << epoch_queued << dendl;
2167 forward_scrub_event(&ScrubPgIF::send_scrub_unblock, epoch_queued);
2168}
7c673cae 2169
f67539c2
TL
2170void PG::scrub_send_digest_update(epoch_t epoch_queued,
2171 [[maybe_unused]] ThreadPool::TPHandle& handle)
2172{
2173 dout(15) << __func__ << " queued at: " << epoch_queued << dendl;
2174 forward_scrub_event(&ScrubPgIF::digest_update_notification, epoch_queued);
2175}
7c673cae 2176
f67539c2
TL
2177void PG::scrub_send_replmaps_ready(epoch_t epoch_queued,
2178 [[maybe_unused]] ThreadPool::TPHandle& handle)
2179{
2180 dout(15) << __func__ << " queued at: " << epoch_queued << dendl;
2181 forward_scrub_event(&ScrubPgIF::send_replica_maps_ready, epoch_queued);
2182}
7c673cae 2183
f67539c2
TL
2184bool PG::ops_blocked_by_scrub() const
2185{
2186 return !waiting_for_scrub.empty();
2187}
11fdf7f2 2188
f67539c2
TL
2189Scrub::scrub_prio_t PG::is_scrub_blocking_ops() const
2190{
2191 return waiting_for_scrub.empty() ? Scrub::scrub_prio_t::low_priority
2192 : Scrub::scrub_prio_t::high_priority;
7c673cae
FG
2193}
2194
9f95a23c 2195bool PG::old_peering_msg(epoch_t reply_epoch, epoch_t query_epoch)
7c673cae 2196{
f67539c2
TL
2197 if (auto last_reset = get_last_peering_reset();
2198 last_reset > reply_epoch || last_reset > query_epoch) {
2199 dout(10) << "old_peering_msg reply_epoch " << reply_epoch << " query_epoch "
2200 << query_epoch << " last_peering_reset " << last_reset << dendl;
9f95a23c
TL
2201 return true;
2202 }
2203 return false;
7c673cae
FG
2204}
2205
9f95a23c
TL
2206struct FlushState {
2207 PGRef pg;
2208 epoch_t epoch;
2209 FlushState(PG *pg, epoch_t epoch) : pg(pg), epoch(epoch) {}
2210 ~FlushState() {
2211 std::scoped_lock l{*pg};
2212 if (!pg->pg_has_reset_since(epoch)) {
2213 pg->recovery_state.complete_flush();
2214 }
7c673cae 2215 }
9f95a23c
TL
2216};
2217typedef std::shared_ptr<FlushState> FlushStateRef;
7c673cae 2218
9f95a23c 2219void PG::start_flush_on_transaction(ObjectStore::Transaction &t)
7c673cae 2220{
9f95a23c
TL
2221 // flush in progress ops
2222 FlushStateRef flush_trigger (std::make_shared<FlushState>(
2223 this, get_osdmap_epoch()));
2224 t.register_on_applied(new ContainerContext<FlushStateRef>(flush_trigger));
2225 t.register_on_commit(new ContainerContext<FlushStateRef>(flush_trigger));
7c673cae
FG
2226}
2227
9f95a23c 2228bool PG::try_flush_or_schedule_async()
11fdf7f2 2229{
9f95a23c
TL
2230 Context *c = new QueuePeeringEvt(
2231 this, get_osdmap_epoch(), PeeringState::IntervalFlush());
2232 if (!ch->flush_commit(c)) {
2233 return false;
2234 } else {
2235 delete c;
2236 return true;
2237 }
11fdf7f2
TL
2238}
2239
9f95a23c 2240ostream& operator<<(ostream& out, const PG& pg)
11fdf7f2 2241{
9f95a23c 2242 out << pg.recovery_state;
f67539c2
TL
2243
2244 // listing all scrub-related flags - both current and "planned next scrub"
2245 if (pg.is_scrubbing()) {
2246 out << *pg.m_scrubber;
2247 }
2248 out << pg.m_planned_scrub;
11fdf7f2 2249
9f95a23c
TL
2250 if (pg.recovery_ops_active)
2251 out << " rops=" << pg.recovery_ops_active;
11fdf7f2 2252
9f95a23c
TL
2253 //out << " (" << pg.pg_log.get_tail() << "," << pg.pg_log.get_head() << "]";
2254 if (pg.recovery_state.have_missing()) {
2255 out << " m=" << pg.recovery_state.get_num_missing();
2256 if (pg.is_primary()) {
2257 uint64_t unfound = pg.recovery_state.get_num_unfound();
2258 if (unfound)
2259 out << " u=" << unfound;
2260 }
2261 }
2262 if (!pg.is_clean()) {
2263 out << " mbc=" << pg.recovery_state.get_missing_by_count();
2264 }
2265 if (!pg.snap_trimq.empty()) {
2266 out << " trimq=";
2267 // only show a count if the set is large
2268 if (pg.snap_trimq.num_intervals() > 16) {
2269 out << pg.snap_trimq.size();
2270 if (!pg.snap_trimq_repeat.empty()) {
2271 out << "(" << pg.snap_trimq_repeat.size() << ")";
2272 }
2273 } else {
2274 out << pg.snap_trimq;
2275 if (!pg.snap_trimq_repeat.empty()) {
2276 out << "(" << pg.snap_trimq_repeat << ")";
2277 }
2278 }
2279 }
2280 if (!pg.recovery_state.get_info().purged_snaps.empty()) {
2281 out << " ps="; // snap trim queue / purged snaps
2282 if (pg.recovery_state.get_info().purged_snaps.num_intervals() > 16) {
2283 out << pg.recovery_state.get_info().purged_snaps.size();
2284 } else {
2285 out << pg.recovery_state.get_info().purged_snaps;
2286 }
11fdf7f2 2287 }
11fdf7f2 2288
9f95a23c 2289 out << "]";
9f95a23c 2290 return out;
11fdf7f2
TL
2291}
2292
9f95a23c 2293bool PG::can_discard_op(OpRequestRef& op)
7c673cae 2294{
9f95a23c
TL
2295 auto m = op->get_req<MOSDOp>();
2296 if (cct->_conf->osd_discard_disconnected_ops && OSD::op_is_discardable(m)) {
2297 dout(20) << " discard " << *m << dendl;
2298 return true;
2299 }
7c673cae 2300
9f95a23c
TL
2301 if (m->get_map_epoch() < info.history.same_primary_since) {
2302 dout(7) << " changed after " << m->get_map_epoch()
2303 << ", dropping " << *m << dendl;
2304 return true;
2305 }
7c673cae 2306
9f95a23c
TL
2307 if ((m->get_flags() & (CEPH_OSD_FLAG_BALANCE_READS |
2308 CEPH_OSD_FLAG_LOCALIZE_READS)) &&
2309 !is_primary() &&
2310 m->get_map_epoch() < info.history.same_interval_since) {
2311 // Note: the Objecter will resend on interval change without the primary
2312 // changing if it actually sent to a replica. If the primary hasn't
2313 // changed since the send epoch, we got it, and we're primary, it won't
2314 // have resent even if the interval did change as it sent it to the primary
2315 // (us).
2316 return true;
7c673cae 2317 }
7c673cae 2318
7c673cae 2319
9f95a23c
TL
2320 if (m->get_connection()->has_feature(CEPH_FEATURE_RESEND_ON_SPLIT)) {
2321 // >= luminous client
2322 if (m->get_connection()->has_feature(CEPH_FEATURE_SERVER_NAUTILUS)) {
2323 // >= nautilus client
2324 if (m->get_map_epoch() < pool.info.get_last_force_op_resend()) {
2325 dout(7) << __func__ << " sent before last_force_op_resend "
2326 << pool.info.last_force_op_resend
2327 << ", dropping" << *m << dendl;
2328 return true;
2329 }
2330 } else {
2331 // == < nautilus client (luminous or mimic)
2332 if (m->get_map_epoch() < pool.info.get_last_force_op_resend_prenautilus()) {
2333 dout(7) << __func__ << " sent before last_force_op_resend_prenautilus "
2334 << pool.info.last_force_op_resend_prenautilus
2335 << ", dropping" << *m << dendl;
2336 return true;
2337 }
7c673cae 2338 }
9f95a23c
TL
2339 if (m->get_map_epoch() < info.history.last_epoch_split) {
2340 dout(7) << __func__ << " pg split in "
2341 << info.history.last_epoch_split << ", dropping" << dendl;
2342 return true;
7c673cae 2343 }
9f95a23c
TL
2344 } else if (m->get_connection()->has_feature(CEPH_FEATURE_OSD_POOLRESEND)) {
2345 // < luminous client
2346 if (m->get_map_epoch() < pool.info.get_last_force_op_resend_preluminous()) {
2347 dout(7) << __func__ << " sent before last_force_op_resend_preluminous "
2348 << pool.info.last_force_op_resend_preluminous
2349 << ", dropping" << *m << dendl;
2350 return true;
7c673cae
FG
2351 }
2352 }
2353
9f95a23c 2354 return false;
7c673cae
FG
2355}
2356
9f95a23c
TL
2357template<typename T, int MSGTYPE>
2358bool PG::can_discard_replica_op(OpRequestRef& op)
7c673cae 2359{
9f95a23c
TL
2360 auto m = op->get_req<T>();
2361 ceph_assert(m->get_type() == MSGTYPE);
7c673cae 2362
9f95a23c 2363 int from = m->get_source().num();
7c673cae 2364
9f95a23c
TL
2365 // if a repop is replied after a replica goes down in a new osdmap, and
2366 // before the pg advances to this new osdmap, the repop replies before this
2367 // repop can be discarded by that replica OSD, because the primary resets the
2368 // connection to it when handling the new osdmap marking it down, and also
2369 // resets the messenger sesssion when the replica reconnects. to avoid the
2370 // out-of-order replies, the messages from that replica should be discarded.
2371 OSDMapRef next_map = osd->get_next_osdmap();
f67539c2
TL
2372 if (next_map->is_down(from)) {
2373 dout(20) << " " << __func__ << " dead for nextmap is down " << from << dendl;
9f95a23c 2374 return true;
f67539c2 2375 }
9f95a23c
TL
2376 /* Mostly, this overlaps with the old_peering_msg
2377 * condition. An important exception is pushes
2378 * sent by replicas not in the acting set, since
2379 * if such a replica goes down it does not cause
2380 * a new interval. */
f67539c2
TL
2381 if (next_map->get_down_at(from) >= m->map_epoch) {
2382 dout(20) << " " << __func__ << " dead for 'get_down_at' " << from << dendl;
9f95a23c 2383 return true;
f67539c2 2384 }
7c673cae 2385
9f95a23c
TL
2386 // same pg?
2387 // if pg changes _at all_, we reset and repeer!
2388 if (old_peering_msg(m->map_epoch, m->map_epoch)) {
2389 dout(10) << "can_discard_replica_op pg changed " << info.history
2390 << " after " << m->map_epoch
2391 << ", dropping" << dendl;
2392 return true;
7c673cae 2393 }
9f95a23c 2394 return false;
7c673cae
FG
2395}
2396
9f95a23c 2397bool PG::can_discard_scan(OpRequestRef op)
7c673cae 2398{
9f95a23c
TL
2399 auto m = op->get_req<MOSDPGScan>();
2400 ceph_assert(m->get_type() == MSG_OSD_PG_SCAN);
7c673cae 2401
9f95a23c
TL
2402 if (old_peering_msg(m->map_epoch, m->query_epoch)) {
2403 dout(10) << " got old scan, ignoring" << dendl;
2404 return true;
7c673cae 2405 }
9f95a23c 2406 return false;
7c673cae
FG
2407}
2408
9f95a23c 2409bool PG::can_discard_backfill(OpRequestRef op)
7c673cae 2410{
9f95a23c
TL
2411 auto m = op->get_req<MOSDPGBackfill>();
2412 ceph_assert(m->get_type() == MSG_OSD_PG_BACKFILL);
7c673cae 2413
9f95a23c
TL
2414 if (old_peering_msg(m->map_epoch, m->query_epoch)) {
2415 dout(10) << " got old backfill, ignoring" << dendl;
2416 return true;
7c673cae
FG
2417 }
2418
9f95a23c 2419 return false;
7c673cae 2420
7c673cae
FG
2421}
2422
9f95a23c 2423bool PG::can_discard_request(OpRequestRef& op)
7c673cae 2424{
9f95a23c
TL
2425 switch (op->get_req()->get_type()) {
2426 case CEPH_MSG_OSD_OP:
2427 return can_discard_op(op);
2428 case CEPH_MSG_OSD_BACKOFF:
2429 return false; // never discard
2430 case MSG_OSD_REPOP:
2431 return can_discard_replica_op<MOSDRepOp, MSG_OSD_REPOP>(op);
2432 case MSG_OSD_PG_PUSH:
2433 return can_discard_replica_op<MOSDPGPush, MSG_OSD_PG_PUSH>(op);
2434 case MSG_OSD_PG_PULL:
2435 return can_discard_replica_op<MOSDPGPull, MSG_OSD_PG_PULL>(op);
2436 case MSG_OSD_PG_PUSH_REPLY:
2437 return can_discard_replica_op<MOSDPGPushReply, MSG_OSD_PG_PUSH_REPLY>(op);
2438 case MSG_OSD_REPOPREPLY:
2439 return can_discard_replica_op<MOSDRepOpReply, MSG_OSD_REPOPREPLY>(op);
2440 case MSG_OSD_PG_RECOVERY_DELETE:
2441 return can_discard_replica_op<MOSDPGRecoveryDelete, MSG_OSD_PG_RECOVERY_DELETE>(op);
7c673cae 2442
9f95a23c
TL
2443 case MSG_OSD_PG_RECOVERY_DELETE_REPLY:
2444 return can_discard_replica_op<MOSDPGRecoveryDeleteReply, MSG_OSD_PG_RECOVERY_DELETE_REPLY>(op);
7c673cae 2445
9f95a23c
TL
2446 case MSG_OSD_EC_WRITE:
2447 return can_discard_replica_op<MOSDECSubOpWrite, MSG_OSD_EC_WRITE>(op);
2448 case MSG_OSD_EC_WRITE_REPLY:
2449 return can_discard_replica_op<MOSDECSubOpWriteReply, MSG_OSD_EC_WRITE_REPLY>(op);
2450 case MSG_OSD_EC_READ:
2451 return can_discard_replica_op<MOSDECSubOpRead, MSG_OSD_EC_READ>(op);
2452 case MSG_OSD_EC_READ_REPLY:
2453 return can_discard_replica_op<MOSDECSubOpReadReply, MSG_OSD_EC_READ_REPLY>(op);
2454 case MSG_OSD_REP_SCRUB:
2455 return can_discard_replica_op<MOSDRepScrub, MSG_OSD_REP_SCRUB>(op);
2456 case MSG_OSD_SCRUB_RESERVE:
2457 return can_discard_replica_op<MOSDScrubReserve, MSG_OSD_SCRUB_RESERVE>(op);
2458 case MSG_OSD_REP_SCRUBMAP:
2459 return can_discard_replica_op<MOSDRepScrubMap, MSG_OSD_REP_SCRUBMAP>(op);
2460 case MSG_OSD_PG_UPDATE_LOG_MISSING:
2461 return can_discard_replica_op<
2462 MOSDPGUpdateLogMissing, MSG_OSD_PG_UPDATE_LOG_MISSING>(op);
2463 case MSG_OSD_PG_UPDATE_LOG_MISSING_REPLY:
2464 return can_discard_replica_op<
2465 MOSDPGUpdateLogMissingReply, MSG_OSD_PG_UPDATE_LOG_MISSING_REPLY>(op);
2466
2467 case MSG_OSD_PG_SCAN:
2468 return can_discard_scan(op);
2469 case MSG_OSD_PG_BACKFILL:
2470 return can_discard_backfill(op);
2471 case MSG_OSD_PG_BACKFILL_REMOVE:
2472 return can_discard_replica_op<MOSDPGBackfillRemove,
2473 MSG_OSD_PG_BACKFILL_REMOVE>(op);
7c673cae 2474 }
9f95a23c 2475 return true;
7c673cae
FG
2476}
2477
9f95a23c 2478void PG::do_peering_event(PGPeeringEventRef evt, PeeringCtx &rctx)
7c673cae 2479{
9f95a23c
TL
2480 dout(10) << __func__ << ": " << evt->get_desc() << dendl;
2481 ceph_assert(have_same_or_newer_map(evt->get_epoch_sent()));
2482 if (old_peering_evt(evt)) {
2483 dout(10) << "discard old " << evt->get_desc() << dendl;
2484 } else {
2485 recovery_state.handle_event(evt, &rctx);
7c673cae 2486 }
9f95a23c
TL
2487 // write_if_dirty regardless of path above to ensure we capture any work
2488 // done by OSD::advance_pg().
2489 write_if_dirty(rctx.transaction);
7c673cae
FG
2490}
2491
9f95a23c 2492void PG::queue_peering_event(PGPeeringEventRef evt)
7c673cae 2493{
9f95a23c
TL
2494 if (old_peering_evt(evt))
2495 return;
2496 osd->osd->enqueue_peering_evt(info.pgid, evt);
7c673cae
FG
2497}
2498
9f95a23c
TL
2499void PG::queue_null(epoch_t msg_epoch,
2500 epoch_t query_epoch)
7c673cae 2501{
9f95a23c
TL
2502 dout(10) << "null" << dendl;
2503 queue_peering_event(
2504 PGPeeringEventRef(std::make_shared<PGPeeringEvent>(msg_epoch, query_epoch,
2505 NullEvt())));
7c673cae
FG
2506}
2507
9f95a23c 2508void PG::find_unfound(epoch_t queued, PeeringCtx &rctx)
7c673cae 2509{
9f95a23c
TL
2510 /*
2511 * if we couldn't start any recovery ops and things are still
2512 * unfound, see if we can discover more missing object locations.
2513 * It may be that our initial locations were bad and we errored
2514 * out while trying to pull.
2515 */
2516 if (!recovery_state.discover_all_missing(rctx)) {
2517 string action;
2518 if (state_test(PG_STATE_BACKFILLING)) {
2519 auto evt = PGPeeringEventRef(
2520 new PGPeeringEvent(
2521 queued,
2522 queued,
2523 PeeringState::UnfoundBackfill()));
2524 queue_peering_event(evt);
2525 action = "in backfill";
2526 } else if (state_test(PG_STATE_RECOVERING)) {
2527 auto evt = PGPeeringEventRef(
2528 new PGPeeringEvent(
2529 queued,
2530 queued,
2531 PeeringState::UnfoundRecovery()));
2532 queue_peering_event(evt);
2533 action = "in recovery";
2534 } else {
2535 action = "already out of recovery/backfill";
7c673cae 2536 }
9f95a23c
TL
2537 dout(10) << __func__ << ": no luck, giving up on this pg for now (" << action << ")" << dendl;
2538 } else {
2539 dout(10) << __func__ << ": no luck, giving up on this pg for now (queue_recovery)" << dendl;
2540 queue_recovery();
7c673cae 2541 }
7c673cae
FG
2542}
2543
9f95a23c
TL
2544void PG::handle_advance_map(
2545 OSDMapRef osdmap, OSDMapRef lastmap,
2546 vector<int>& newup, int up_primary,
2547 vector<int>& newacting, int acting_primary,
2548 PeeringCtx &rctx)
7c673cae 2549{
9f95a23c
TL
2550 dout(10) << __func__ << ": " << osdmap->get_epoch() << dendl;
2551 osd_shard->update_pg_epoch(pg_slot, osdmap->get_epoch());
2552 recovery_state.advance_map(
2553 osdmap,
2554 lastmap,
2555 newup,
2556 up_primary,
2557 newacting,
2558 acting_primary,
2559 rctx);
7c673cae
FG
2560}
2561
9f95a23c 2562void PG::handle_activate_map(PeeringCtx &rctx)
7c673cae 2563{
9f95a23c
TL
2564 dout(10) << __func__ << ": " << get_osdmap()->get_epoch()
2565 << dendl;
2566 recovery_state.activate_map(rctx);
7c673cae 2567
9f95a23c 2568 requeue_map_waiters();
7c673cae
FG
2569}
2570
9f95a23c 2571void PG::handle_initialize(PeeringCtx &rctx)
7c673cae 2572{
9f95a23c
TL
2573 dout(10) << __func__ << dendl;
2574 PeeringState::Initialize evt;
2575 recovery_state.handle_event(evt, &rctx);
7c673cae
FG
2576}
2577
f67539c2 2578
9f95a23c 2579void PG::handle_query_state(Formatter *f)
7c673cae 2580{
9f95a23c
TL
2581 dout(10) << "handle_query_state" << dendl;
2582 PeeringState::QueryState q(f);
2583 recovery_state.handle_event(q, 0);
7c673cae 2584
f67539c2
TL
2585 // This code has moved to after the close of recovery_state array.
2586 // I don't think that scrub is a recovery state
2587 if (is_primary() && is_active() && m_scrubber && m_scrubber->is_scrub_active()) {
2588 m_scrubber->handle_query_state(f);
9f95a23c 2589 }
7c673cae
FG
2590}
2591
9f95a23c 2592void PG::init_collection_pool_opts()
11fdf7f2 2593{
9f95a23c
TL
2594 auto r = osd->store->set_collection_opts(ch, pool.info.opts);
2595 if (r < 0 && r != -EOPNOTSUPP) {
2596 derr << __func__ << " set_collection_opts returns error:" << r << dendl;
11fdf7f2 2597 }
11fdf7f2
TL
2598}
2599
9f95a23c 2600void PG::on_pool_change()
7c673cae 2601{
9f95a23c
TL
2602 init_collection_pool_opts();
2603 plpg_on_pool_change();
7c673cae
FG
2604}
2605
9f95a23c
TL
2606void PG::C_DeleteMore::complete(int r) {
2607 ceph_assert(r == 0);
2608 pg->lock();
2609 if (!pg->pg_has_reset_since(epoch)) {
2610 pg->osd->queue_for_pg_delete(pg->get_pgid(), epoch);
7c673cae 2611 }
9f95a23c
TL
2612 pg->unlock();
2613 delete this;
7c673cae
FG
2614}
2615
f67539c2
TL
2616std::pair<ghobject_t, bool> PG::do_delete_work(
2617 ObjectStore::Transaction &t,
2618 ghobject_t _next)
7c673cae 2619{
9f95a23c 2620 dout(10) << __func__ << dendl;
7c673cae 2621
9f95a23c
TL
2622 {
2623 float osd_delete_sleep = osd->osd->get_osd_delete_sleep();
2624 if (osd_delete_sleep > 0 && delete_needs_sleep) {
2625 epoch_t e = get_osdmap()->get_epoch();
2626 PGRef pgref(this);
2627 auto delete_requeue_callback = new LambdaContext([this, pgref, e](int r) {
2628 dout(20) << __func__ << " wake up at "
2629 << ceph_clock_now()
2630 << ", re-queuing delete" << dendl;
2631 std::scoped_lock locker{*this};
2632 delete_needs_sleep = false;
2633 if (!pg_has_reset_since(e)) {
2634 osd->queue_for_pg_delete(get_pgid(), e);
2635 }
2636 });
7c673cae 2637
9f95a23c
TL
2638 auto delete_schedule_time = ceph::real_clock::now();
2639 delete_schedule_time += ceph::make_timespan(osd_delete_sleep);
2640 std::lock_guard l{osd->sleep_lock};
2641 osd->sleep_timer.add_event_at(delete_schedule_time,
2642 delete_requeue_callback);
2643 dout(20) << __func__ << " Delete scheduled at " << delete_schedule_time << dendl;
f67539c2 2644 return std::make_pair(_next, true);
9f95a23c
TL
2645 }
2646 }
7c673cae 2647
9f95a23c 2648 delete_needs_sleep = true;
7c673cae 2649
adb31ebb
TL
2650 ghobject_t next;
2651
9f95a23c
TL
2652 vector<ghobject_t> olist;
2653 int max = std::min(osd->store->get_ideal_list_max(),
2654 (int)cct->_conf->osd_target_transaction_size);
adb31ebb 2655
9f95a23c
TL
2656 osd->store->collection_list(
2657 ch,
adb31ebb 2658 _next,
9f95a23c
TL
2659 ghobject_t::get_max(),
2660 max,
2661 &olist,
2662 &next);
2663 dout(20) << __func__ << " " << olist << dendl;
7c673cae 2664
adb31ebb
TL
2665 // make sure we've removed everything
2666 // by one more listing from the beginning
2667 if (_next != ghobject_t() && olist.empty()) {
2668 next = ghobject_t();
2669 osd->store->collection_list(
2670 ch,
2671 next,
2672 ghobject_t::get_max(),
2673 max,
2674 &olist,
2675 &next);
2676 if (!olist.empty()) {
2677 dout(0) << __func__ << " additional unexpected onode list"
2678 <<" (new onodes has appeared since PG removal started"
2679 << olist << dendl;
2680 }
2681 }
2682
9f95a23c
TL
2683 OSDriver::OSTransaction _t(osdriver.get_transaction(&t));
2684 int64_t num = 0;
2685 for (auto& oid : olist) {
2686 if (oid == pgmeta_oid) {
7c673cae
FG
2687 continue;
2688 }
9f95a23c
TL
2689 if (oid.is_pgmeta()) {
2690 osd->clog->warn() << info.pgid << " found stray pgmeta-like " << oid
2691 << " during PG removal";
7c673cae 2692 }
9f95a23c
TL
2693 int r = snap_mapper.remove_oid(oid.hobj, &_t);
2694 if (r != 0 && r != -ENOENT) {
2695 ceph_abort();
7c673cae 2696 }
9f95a23c
TL
2697 t.remove(coll, oid);
2698 ++num;
7c673cae 2699 }
f67539c2 2700 bool running = true;
9f95a23c
TL
2701 if (num) {
2702 dout(20) << __func__ << " deleting " << num << " objects" << dendl;
2703 Context *fin = new C_DeleteMore(this, get_osdmap_epoch());
2704 t.register_on_commit(fin);
7c673cae 2705 } else {
9f95a23c
TL
2706 if (cct->_conf->osd_inject_failure_on_pg_removal) {
2707 _exit(1);
7c673cae 2708 }
7c673cae 2709
9f95a23c
TL
2710 // final flush here to ensure completions drop refs. Of particular concern
2711 // are the SnapMapper ContainerContexts.
2712 {
2713 PGRef pgref(this);
2714 PGLog::clear_info_log(info.pgid, &t);
2715 t.remove_collection(coll);
2716 t.register_on_commit(new ContainerContext<PGRef>(pgref));
2717 t.register_on_applied(new ContainerContext<PGRef>(pgref));
2718 osd->store->queue_transaction(ch, std::move(t));
7c673cae 2719 }
9f95a23c 2720 ch->flush();
7c673cae 2721
9f95a23c
TL
2722 if (!osd->try_finish_pg_delete(this, pool.info.get_pg_num())) {
2723 dout(1) << __func__ << " raced with merge, reinstantiating" << dendl;
2724 ch = osd->store->create_new_collection(coll);
2725 create_pg_collection(t,
2726 info.pgid,
2727 info.pgid.get_split_bits(pool.info.get_pg_num()));
2728 init_pg_ondisk(t, info.pgid, &pool.info);
2729 recovery_state.reset_last_persisted();
2730 } else {
2731 recovery_state.set_delete_complete();
7c673cae 2732
9f95a23c
TL
2733 // cancel reserver here, since the PG is about to get deleted and the
2734 // exit() methods don't run when that happens.
2735 osd->local_reserver.cancel_reservation(info.pgid);
7c673cae 2736
f67539c2 2737 running = false;
9f95a23c 2738 }
7c673cae 2739 }
f67539c2 2740 return {next, running};
7c673cae
FG
2741}
2742
9f95a23c 2743int PG::pg_stat_adjust(osd_stat_t *ns)
7c673cae 2744{
9f95a23c
TL
2745 osd_stat_t &new_stat = *ns;
2746 if (is_primary()) {
2747 return 0;
7c673cae 2748 }
9f95a23c
TL
2749 // Adjust the kb_used by adding pending backfill data
2750 uint64_t reserved_num_bytes = get_reserved_num_bytes();
7c673cae 2751
9f95a23c
TL
2752 // For now we don't consider projected space gains here
2753 // I suggest we have an optional 2 pass backfill that frees up
2754 // space in a first pass. This could be triggered when at nearfull
2755 // or near to backfillfull.
2756 if (reserved_num_bytes > 0) {
2757 // TODO: Handle compression by adjusting by the PGs average
2758 // compression precentage.
2759 dout(20) << __func__ << " reserved_num_bytes " << (reserved_num_bytes >> 10) << "KiB"
2760 << " Before kb_used " << new_stat.statfs.kb_used() << "KiB" << dendl;
2761 if (new_stat.statfs.available > reserved_num_bytes)
2762 new_stat.statfs.available -= reserved_num_bytes;
2763 else
2764 new_stat.statfs.available = 0;
2765 dout(20) << __func__ << " After kb_used " << new_stat.statfs.kb_used() << "KiB" << dendl;
2766 return 1;
7c673cae 2767 }
9f95a23c 2768 return 0;
7c673cae
FG
2769}
2770
11fdf7f2
TL
2771void PG::dump_pgstate_history(Formatter *f)
2772{
9f95a23c
TL
2773 std::scoped_lock l{*this};
2774 recovery_state.dump_history(f);
11fdf7f2 2775}
7c673cae 2776
11fdf7f2
TL
2777void PG::dump_missing(Formatter *f)
2778{
9f95a23c 2779 for (auto& i : recovery_state.get_pg_log().get_missing().get_items()) {
11fdf7f2
TL
2780 f->open_object_section("object");
2781 f->dump_object("oid", i.first);
2782 f->dump_object("missing_info", i.second);
9f95a23c
TL
2783 if (recovery_state.get_missing_loc().needs_recovery(i.first)) {
2784 f->dump_bool(
2785 "unfound",
2786 recovery_state.get_missing_loc().is_unfound(i.first));
11fdf7f2 2787 f->open_array_section("locations");
9f95a23c 2788 for (auto l : recovery_state.get_missing_loc().get_locations(i.first)) {
11fdf7f2
TL
2789 f->dump_object("shard", l);
2790 }
2791 f->close_section();
2792 }
2793 f->close_section();
2794 }
2795}
2796
2797void PG::get_pg_stats(std::function<void(const pg_stat_t&, epoch_t lec)> f)
2798{
9f95a23c 2799 std::lock_guard l{pg_stats_publish_lock};
11fdf7f2
TL
2800 if (pg_stats_publish_valid) {
2801 f(pg_stats_publish, pg_stats_publish.get_effective_last_epoch_clean());
2802 }
11fdf7f2
TL
2803}
2804
2805void PG::with_heartbeat_peers(std::function<void(int)> f)
2806{
9f95a23c 2807 std::lock_guard l{heartbeat_peer_lock};
11fdf7f2
TL
2808 for (auto p : heartbeat_peers) {
2809 f(p);
2810 }
2811 for (auto p : probe_targets) {
2812 f(p);
2813 }
9f95a23c
TL
2814}
2815
2816uint64_t PG::get_min_alloc_size() const {
2817 return osd->store->get_min_alloc_size();
11fdf7f2 2818}