summaryrefslogtreecommitdiffstats
path: root/src/osd/scrubber/scrub_queue_entry.h
blob: 03d959769b20651f444c392f606dd0d07a14bb07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#pragma once

#include <compare>
#include <string_view>

#include "include/utime.h"
#include "osd/osd_types.h"
#include "osd/scrubber_common.h"

namespace Scrub {

/**
 * Possible urgency levels for a specific scheduling target (shallow or deep):
 *
 * (note: the 'urgency' attribute conveys both the relative priority for
 * scheduling and the behavior of the scrub). The urgency levels are:
 *                    ^^^^^^^^^^^^^^^^^^^^^
 *
 * periodic scrubs:
 * ---------------
 *
 * 'periodic_regular' - the "standard" shallow/deep scrub performed
 *      periodically on each PG.
 *
 *
 * priority scrubs (termed 'required' or 'must' in the legacy code):
 * ---------------------------------------------------------------
 * In order of ascending priority:
 *
 * 'must_scrub' - the PG info is not valid (i.e. we do not have a valid
 *     'last-scrub' stamp). A high-priority shallow scrub is required.
 *
 * 'after_repair' - triggered immediately after a recovery process
 *   ('m_after_repair_scrub_required' was set).
 *   This type of scrub is always deep.
 *   (note: this urgency level is not implemented in this commit)
 *
 * 'repairing' - the target is currently being deep-scrubbed with the repair
 *   flag set. Triggered by a previous shallow scrub that ended with errors.
 *
 * 'operator_requested' - the target was manually requested for scrubbing by
 *   an administrator.
 *
 * 'must_repair' - the target is required to be deep-scrubbed with the
 *   repair flag set, initiated by a message specifying 'do_repair'.
 */
enum class urgency_t {
  periodic_regular,
  must_scrub,
  after_repair,
  repairing,
  operator_requested,
  must_repair,
};

/**
 * SchedEntry holds the scheduling details for scrubbing a specific PG at
 * a specific scrub level. Namely - it identifies the [pg,level] combination,
 * the 'urgency' attribute of the scheduled scrub (which determines most of
 * its behavior and scheduling decisions) and the actual time attributes
 * for scheduling (target, deadline, not_before).
 *
 * In this commit - the 'urgency' attribute is not fully used yet, and some
 * of the scrub behavior is still controlled by the 'planned scrub' flags.
 */
struct SchedEntry {
  constexpr SchedEntry(spg_t pgid, scrub_level_t level)
      : pgid{pgid}
      , level{level}
  {}

  SchedEntry(const SchedEntry&) = default;
  SchedEntry(SchedEntry&&) = default;
  SchedEntry& operator=(const SchedEntry&) = default;
  SchedEntry& operator=(SchedEntry&&) = default;

  spg_t pgid;
  scrub_level_t level;

  urgency_t urgency{urgency_t::periodic_regular};

  /// scheduled_at, not-before & the deadline times
  Scrub::scrub_schedule_t schedule;

  /// either 'none', or the reason for the latest failure/delay (for
  /// logging/reporting purposes)
  delay_cause_t last_issue{delay_cause_t::none};
};


static inline std::weak_ordering cmp_ripe_entries(
    const Scrub::SchedEntry& l,
    const Scrub::SchedEntry& r) noexcept
{
  // for 'higher is better' sub elements - the 'r.' is on the left
  if (auto cmp = r.urgency <=> l.urgency; cmp != 0) {
    return cmp;
  }
  // if we are comparing the two targets of the same PG, once both are
  // ripe - the 'deep' scrub is considered 'higher' than the 'shallow' one.
  if (l.pgid == r.pgid && r.level < l.level) {
    return std::weak_ordering::less;
  }
  // the 'utime_t' operator<=> is 'partial_ordering', it seems.
  if (auto cmp = std::weak_order(
	  double(l.schedule.scheduled_at), double(r.schedule.scheduled_at));
      cmp != 0) {
    return cmp;
  }
  if (r.level < l.level) {
    return std::weak_ordering::less;
  }
  if (auto cmp = std::weak_order(
	  double(l.schedule.not_before), double(r.schedule.not_before));
      cmp != 0) {
    return cmp;
  }
  return std::weak_ordering::greater;
}

static inline std::weak_ordering cmp_future_entries(
    const Scrub::SchedEntry& l,
    const Scrub::SchedEntry& r) noexcept
{
  if (auto cmp = std::weak_order(
	  double(l.schedule.not_before), double(r.schedule.not_before));
      cmp != 0) {
    return cmp;
  }
  // for 'higher is better' sub elements - the 'r.' is on the left
  if (auto cmp = r.urgency <=> l.urgency; cmp != 0) {
    return cmp;
  }
  if (auto cmp = std::weak_order(
	  double(l.schedule.scheduled_at), double(r.schedule.scheduled_at));
      cmp != 0) {
    return cmp;
  }
  if (r.level < l.level) {
    return std::weak_ordering::less;
  }
  return std::weak_ordering::greater;
}

static inline std::weak_ordering cmp_entries(
    utime_t t,
    const Scrub::SchedEntry& l,
    const Scrub::SchedEntry& r) noexcept
{
  bool l_ripe = l.schedule.not_before <= t;
  bool r_ripe = r.schedule.not_before <= t;
  if (l_ripe) {
    if (r_ripe) {
      return cmp_ripe_entries(l, r);
    }
    return std::weak_ordering::less;
  }
  if (r_ripe) {
    return std::weak_ordering::greater;
  }
  return cmp_future_entries(l, r);
}

// ---  the interface required by 'not_before_queue_t':

static inline const utime_t& project_not_before(const Scrub::SchedEntry& e)
{
  return e.schedule.not_before;
}

static inline const spg_t& project_removal_class(const Scrub::SchedEntry& e)
{
  return e.pgid;
}


/// 'not_before_queue_t' requires a '<' operator, to be used for
/// eligible entries:
static inline bool operator<(
    const Scrub::SchedEntry& lhs,
    const Scrub::SchedEntry& rhs)
{
  return cmp_ripe_entries(lhs, rhs) == std::weak_ordering::less;
}

}  // namespace Scrub


namespace fmt {

// clang-format off
template <>
struct formatter<Scrub::urgency_t> : formatter<std::string_view> {
  template <typename FormatContext>
  auto format(Scrub::urgency_t urg, FormatContext& ctx) const
  {
    using enum Scrub::urgency_t;
    std::string_view desc;
    switch (urg) {
      case periodic_regular:    desc = "periodic-regular"; break;
      case must_scrub:          desc = "must-scrub"; break;
      case after_repair:        desc = "after-repair"; break;
      case repairing:           desc = "repairing"; break;
      case operator_requested:  desc = "operator-requested"; break;
      case must_repair:         desc = "must-repair"; break;
      // better to not have a default case, so that the compiler will warn
    }
    return formatter<string_view>::format(desc, ctx);
  }
};
// clang-format on

template <>
struct formatter<Scrub::SchedEntry> {
  constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
  template <typename FormatContext>
  auto format(const Scrub::SchedEntry& st, FormatContext& ctx) const
  {
    return fmt::format_to(
	ctx.out(), "{}/{},nb:{:s},({},tr:{:s},dl:{:s})", st.pgid,
	(st.level == scrub_level_t::deep ? "dp" : "sh"), st.schedule.not_before,
	st.urgency, st.schedule.scheduled_at, st.schedule.deadline);
  }
};
}  // namespace fmt