Skip to content

Commit c735ab2

Browse files
authored
Merge pull request ceph#57977 from athanatos/sjust/wip-66308-obc-locking
crimson: simplify obc loading by locking excl for load and demoting to needed lock Reviewed-by: Yingxin Cheng <[email protected]> Reviewed-by: Matan Breizman <[email protected]>
2 parents c80f7fd + 161c8b7 commit c735ab2

File tree

7 files changed

+173
-300
lines changed

7 files changed

+173
-300
lines changed

src/crimson/common/tri_mutex.cc

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
SET_SUBSYS(osd);
99
//TODO: SET_SUBSYS(crimson_tri_mutex);
1010

11-
std::optional<seastar::future<>>
12-
read_lock::lock()
11+
seastar::future<> read_lock::lock()
1312
{
1413
return static_cast<tri_mutex*>(this)->lock_for_read();
1514
}
@@ -19,8 +18,7 @@ void read_lock::unlock()
1918
static_cast<tri_mutex*>(this)->unlock_for_read();
2019
}
2120

22-
std::optional<seastar::future<>>
23-
write_lock::lock()
21+
seastar::future<> write_lock::lock()
2422
{
2523
return static_cast<tri_mutex*>(this)->lock_for_write();
2624
}
@@ -30,8 +28,7 @@ void write_lock::unlock()
3028
static_cast<tri_mutex*>(this)->unlock_for_write();
3129
}
3230

33-
std::optional<seastar::future<>>
34-
excl_lock::lock()
31+
seastar::future<> excl_lock::lock()
3532
{
3633
return static_cast<tri_mutex*>(this)->lock_for_excl();
3734
}
@@ -41,44 +38,23 @@ void excl_lock::unlock()
4138
static_cast<tri_mutex*>(this)->unlock_for_excl();
4239
}
4340

44-
void excl_lock_from_read::lock()
45-
{
46-
static_cast<tri_mutex*>(this)->promote_from_read();
47-
}
48-
49-
void excl_lock_from_read::unlock()
50-
{
51-
static_cast<tri_mutex*>(this)->demote_to_read();
52-
}
53-
54-
void excl_lock_from_write::lock()
55-
{
56-
static_cast<tri_mutex*>(this)->promote_from_write();
57-
}
58-
59-
void excl_lock_from_write::unlock()
60-
{
61-
static_cast<tri_mutex*>(this)->demote_to_write();
62-
}
63-
6441
tri_mutex::~tri_mutex()
6542
{
6643
LOG_PREFIX(tri_mutex::~tri_mutex());
6744
DEBUGDPP("", *this);
6845
assert(!is_acquired());
6946
}
7047

71-
std::optional<seastar::future<>>
72-
tri_mutex::lock_for_read()
48+
seastar::future<> tri_mutex::lock_for_read()
7349
{
7450
LOG_PREFIX(tri_mutex::lock_for_read());
7551
DEBUGDPP("", *this);
7652
if (try_lock_for_read()) {
7753
DEBUGDPP("lock_for_read successfully", *this);
78-
return std::nullopt;
54+
return seastar::now();
7955
}
8056
DEBUGDPP("can't lock_for_read, adding to waiters", *this);
81-
waiters.emplace_back(seastar::promise<>(), type_t::read, name);
57+
waiters.emplace_back(seastar::promise<>(), type_t::read);
8258
return waiters.back().pr.get_future();
8359
}
8460

@@ -103,15 +79,6 @@ void tri_mutex::unlock_for_read()
10379
}
10480
}
10581

106-
void tri_mutex::promote_from_read()
107-
{
108-
LOG_PREFIX(tri_mutex::promote_from_read());
109-
DEBUGDPP("", *this);
110-
assert(readers == 1);
111-
--readers;
112-
exclusively_used = true;
113-
}
114-
11582
void tri_mutex::demote_to_read()
11683
{
11784
LOG_PREFIX(tri_mutex::demote_to_read());
@@ -121,17 +88,16 @@ void tri_mutex::demote_to_read()
12188
++readers;
12289
}
12390

124-
std::optional<seastar::future<>>
125-
tri_mutex::lock_for_write()
91+
seastar::future<> tri_mutex::lock_for_write()
12692
{
12793
LOG_PREFIX(tri_mutex::lock_for_write());
12894
DEBUGDPP("", *this);
12995
if (try_lock_for_write()) {
13096
DEBUGDPP("lock_for_write successfully", *this);
131-
return std::nullopt;
97+
return seastar::now();
13298
}
13399
DEBUGDPP("can't lock_for_write, adding to waiters", *this);
134-
waiters.emplace_back(seastar::promise<>(), type_t::write, name);
100+
waiters.emplace_back(seastar::promise<>(), type_t::write);
135101
return waiters.back().pr.get_future();
136102
}
137103

@@ -156,15 +122,6 @@ void tri_mutex::unlock_for_write()
156122
}
157123
}
158124

159-
void tri_mutex::promote_from_write()
160-
{
161-
LOG_PREFIX(tri_mutex::promote_from_write());
162-
DEBUGDPP("", *this);
163-
assert(writers == 1);
164-
--writers;
165-
exclusively_used = true;
166-
}
167-
168125
void tri_mutex::demote_to_write()
169126
{
170127
LOG_PREFIX(tri_mutex::demote_to_write());
@@ -175,17 +132,16 @@ void tri_mutex::demote_to_write()
175132
}
176133

177134
// for exclusive users
178-
std::optional<seastar::future<>>
179-
tri_mutex::lock_for_excl()
135+
seastar::future<> tri_mutex::lock_for_excl()
180136
{
181137
LOG_PREFIX(tri_mutex::lock_for_excl());
182138
DEBUGDPP("", *this);
183139
if (try_lock_for_excl()) {
184140
DEBUGDPP("lock_for_excl, successfully", *this);
185-
return std::nullopt;
141+
return seastar::now();
186142
}
187143
DEBUGDPP("can't lock_for_excl, adding to waiters", *this);
188-
waiters.emplace_back(seastar::promise<>(), type_t::exclusive, name);
144+
waiters.emplace_back(seastar::promise<>(), type_t::exclusive);
189145
return waiters.back().pr.get_future();
190146
}
191147

@@ -254,7 +210,7 @@ void tri_mutex::wake()
254210
default:
255211
assert(0);
256212
}
257-
DEBUGDPP("waking up {}", *this, waiter.waiter_name);
213+
DEBUGDPP("waking up", *this);
258214
waiter.pr.set_value();
259215
waiters.pop_front();
260216
}

src/crimson/common/tri_mutex.h

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,27 @@
33

44
#pragma once
55

6-
#include <optional>
7-
86
#include <seastar/core/future.hh>
97
#include <seastar/core/circular_buffer.hh>
8+
9+
#include "common/hobject.h"
1010
#include "crimson/common/log.h"
1111

1212
class read_lock {
1313
public:
14-
std::optional<seastar::future<>> lock();
14+
seastar::future<> lock();
1515
void unlock();
1616
};
1717

1818
class write_lock {
1919
public:
20-
std::optional<seastar::future<>> lock();
20+
seastar::future<> lock();
2121
void unlock();
2222
};
2323

2424
class excl_lock {
2525
public:
26-
std::optional<seastar::future<>> lock();
27-
void unlock();
28-
};
29-
30-
// promote from read to excl
31-
class excl_lock_from_read {
32-
public:
33-
void lock();
34-
void unlock();
35-
};
36-
37-
// promote from write to excl
38-
class excl_lock_from_write {
39-
public:
40-
void lock();
26+
seastar::future<> lock();
4127
void unlock();
4228
};
4329

@@ -54,21 +40,16 @@ class excl_lock_from_write {
5440
/// - readers
5541
/// - writers
5642
/// - exclusive users
57-
///
58-
/// For lock promotion, a read or a write lock is only allowed to be promoted
59-
/// atomically upon the first locking.
6043
class tri_mutex : private read_lock,
6144
write_lock,
62-
excl_lock,
63-
excl_lock_from_read,
64-
excl_lock_from_write
45+
excl_lock
6546
{
6647
public:
6748
tri_mutex() = default;
6849
#ifdef NDEBUG
69-
tri_mutex(const std::string obj_name) : name() {}
50+
tri_mutex(const hobject_t &obj_name) : name() {}
7051
#else
71-
tri_mutex(const std::string obj_name) : name(obj_name) {}
52+
tri_mutex(const hobject_t &obj_name) : name(obj_name) {}
7253
#endif
7354
~tri_mutex();
7455

@@ -81,35 +62,27 @@ class tri_mutex : private read_lock,
8162
excl_lock& for_excl() {
8263
return *this;
8364
}
84-
excl_lock_from_read& excl_from_read() {
85-
return *this;
86-
}
87-
excl_lock_from_write& excl_from_write() {
88-
return *this;
89-
}
9065

9166
// for shared readers
92-
std::optional<seastar::future<>> lock_for_read();
67+
seastar::future<> lock_for_read();
9368
bool try_lock_for_read() noexcept;
9469
void unlock_for_read();
95-
void promote_from_read();
9670
void demote_to_read();
9771
unsigned get_readers() const {
9872
return readers;
9973
}
10074

10175
// for shared writers
102-
std::optional<seastar::future<>> lock_for_write();
76+
seastar::future<> lock_for_write();
10377
bool try_lock_for_write() noexcept;
10478
void unlock_for_write();
105-
void promote_from_write();
10679
void demote_to_write();
10780
unsigned get_writers() const {
10881
return writers;
10982
}
11083

11184
// for exclusive users
112-
std::optional<seastar::future<>> lock_for_excl();
85+
seastar::future<> lock_for_excl();
11386
bool try_lock_for_excl() noexcept;
11487
void unlock_for_excl();
11588
bool is_excl_acquired() const {
@@ -128,7 +101,7 @@ class tri_mutex : private read_lock,
128101
}
129102
}
130103

131-
std::string_view get_name() const{
104+
const hobject_t &get_name() const{
132105
return name;
133106
}
134107

@@ -144,21 +117,17 @@ class tri_mutex : private read_lock,
144117
none,
145118
};
146119
struct waiter_t {
147-
waiter_t(seastar::promise<>&& pr, type_t type, std::string_view waiter_name)
120+
waiter_t(seastar::promise<>&& pr, type_t type)
148121
: pr(std::move(pr)), type(type)
149122
{}
150123
seastar::promise<> pr;
151124
type_t type;
152-
std::string_view waiter_name;
153125
};
154126
seastar::circular_buffer<waiter_t> waiters;
155-
const std::string name;
127+
const hobject_t name;
156128
friend class read_lock;
157129
friend class write_lock;
158130
friend class excl_lock;
159-
friend class excl_lock_from_read;
160-
friend class excl_lock_from_write;
161-
friend class excl_lock_from_excl;
162131
friend std::ostream& operator<<(std::ostream &lhs, const tri_mutex &rhs);
163132
};
164133

0 commit comments

Comments
 (0)