]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
osd/scrub: introduce OsdService::get_locked_pg()
authorRonen Friedman <rfriedma@redhat.com>
Sat, 16 Sep 2023 16:53:35 +0000 (11:53 -0500)
committerRonen Friedman <rfriedma@redhat.com>
Wed, 20 Sep 2023 06:39:10 +0000 (01:39 -0500)
which returns an RAII-wrapper around a locked PG.

Signed-off-by: Ronen Friedman <rfriedma@redhat.com>
src/osd/OSD.cc
src/osd/OSD.h
src/osd/PG.cc
src/osd/PG.h
src/osd/scrubber/osd_scrub_sched.h
src/test/osd/test_scrub_sched.cc

index 255976aba0ec12a8c0bcad6b8e139816ba1808d0..4b7ecfc00cbe0b8476fd692d41a35385e4d21edd 100644 (file)
@@ -7595,6 +7595,17 @@ void OSD::handle_fast_scrub(MOSDScrub2 *m)
   m->put();
 }
 
+std::optional<PGLockWrapper> OSDService::get_locked_pg(spg_t pgid)
+{
+  auto pg = osd->lookup_lock_pg(pgid);
+  if (pg) {
+    return PGLockWrapper{std::move(pg)};
+  } else {
+    return std::nullopt;
+  }
+}
+
+
 MPGStats* OSD::collect_pg_stats()
 {
   dout(15) << __func__ << dendl;
index 338b390dde1846cc6b1a299c500f472c9a8d1d0c..efde588d939ebb06a059f3d40e86530382661f74 100644 (file)
@@ -260,6 +260,12 @@ public:
     spg_t pgid,
     bool allow_requested_repair_only) final;
 
+  /**
+   * locks the named PG, returning an RAII wrapper that unlocks upon
+   * destruction.
+   * returns nullopt if failing to lock.
+   */
+  std::optional<PGLockWrapper> get_locked_pg(spg_t pgid) final;
 
  private:
   // -- agent shared state --
index 79758e22c3af084316f40c9e7f225992bfa2b9b2..25a8ac7197845b1bc1559f102f7d926a5a08c7da 100644 (file)
@@ -2836,3 +2836,11 @@ void PG::with_heartbeat_peers(std::function<void(int)>&& f)
 uint64_t PG::get_min_alloc_size() const {
   return osd->store->get_min_alloc_size();
 }
+
+PGLockWrapper::~PGLockWrapper()
+{
+  if (m_pg) {
+    // otherwise - we were 'moved from'
+    m_pg->unlock();
+  }
+}
index 3cb22b73e534d18721ce79f890f0565c119b4cdb..56ac9a65bf1a04ce6e3c3382bfbb2b42d7bce71a 100644 (file)
@@ -1450,4 +1450,22 @@ public:
  }
 };
 
+/**
+ * Initialized with a locked PG. That PG is unlocked in the
+ * destructor.
+ * Used by OsdScrub when initiating a scrub.
+ */
+class PGLockWrapper {
+ public:
+  explicit PGLockWrapper(PGRef locked_pg) : m_pg{locked_pg} {}
+  PGRef pg() { return m_pg; }
+  ~PGLockWrapper();
+  PGLockWrapper(PGLockWrapper&& rhs) : m_pg(std::move(rhs.m_pg)) {
+    rhs.m_pg = nullptr;
+  }
+  PGLockWrapper(const PGLockWrapper& rhs) = delete;
+ private:
+  PGRef m_pg;
+};
+
 #endif
index 94cca0f38471bdda5395d70d96351a9355845c76..2ec36bbc43031c3992b7c095b4c0117610015de0 100644 (file)
@@ -109,8 +109,8 @@ ScrubQueue interfaces (main functions):
 
 #include <optional>
 #include "utime.h"
-#include "scrub_job.h"
-class PG;
+#include "osd/scrubber/scrub_job.h"
+#include "osd/PG.h"
 
 namespace Scrub {
 
@@ -132,6 +132,13 @@ class ScrubSchedListener {
  public:
   virtual int get_nodeid() const = 0;  // returns the OSD number ('whoami')
 
+  /**
+   * locks the named PG, returning an RAII wrapper that unlocks upon
+   * destruction.
+   * returns nullopt if failing to lock.
+   */
+  virtual std::optional<PGLockWrapper> get_locked_pg(spg_t pgid) = 0;
+
   /**
    * A callback used by the ScrubQueue object to initiate a scrub on a specific
    * PG.
index a3f1a348584aa2432006ca481290bebf1cd47adf..e1847619db7257164782eccf028478c636185d01 100644 (file)
@@ -114,6 +114,12 @@ class FakeOsd : public Scrub::ScrubSchedListener {
     m_next_response[pgid] = result;
   }
 
+  std::optional<PGLockWrapper> get_locked_pg(spg_t pgid)
+  {
+    std::ignore = pgid;
+    return std::nullopt;
+  }
+
  private:
   int m_osd_num;
   std::map<spg_t, schedule_result_t> m_next_response;