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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2018 Red Hat, Inc.
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "include/rados/librados.hpp"
#include "rgw_aio_throttle.h"
namespace rgw {
bool Throttle::waiter_ready() const
{
switch (waiter) {
case Wait::Available: return is_available();
case Wait::Completion: return has_completion();
case Wait::Drained: return is_drained();
default: return false;
}
}
AioResultList BlockingAioThrottle::get(const RGWSI_RADOS::Obj& obj,
OpFunc&& f,
uint64_t cost, uint64_t id)
{
auto p = std::make_unique<Pending>();
p->obj = obj;
p->id = id;
p->cost = cost;
std::unique_lock lock{mutex};
if (cost > window) {
p->result = -EDEADLK; // would never succeed
completed.push_back(*p);
} else {
// wait for the write size to become available
pending_size += p->cost;
if (!is_available()) {
ceph_assert(waiter == Wait::None);
waiter = Wait::Available;
cond.wait(lock, [this] { return is_available(); });
waiter = Wait::None;
}
// register the pending write and attach a completion
p->parent = this;
pending.push_back(*p);
lock.unlock();
std::move(f)(this, *static_cast<AioResult*>(p.get()));
lock.lock();
}
p.release();
return std::move(completed);
}
void BlockingAioThrottle::put(AioResult& r)
{
auto& p = static_cast<Pending&>(r);
std::scoped_lock lock{mutex};
// move from pending to completed
pending.erase(pending.iterator_to(p));
completed.push_back(p);
pending_size -= p.cost;
if (waiter_ready()) {
cond.notify_one();
}
}
AioResultList BlockingAioThrottle::poll()
{
std::unique_lock lock{mutex};
return std::move(completed);
}
AioResultList BlockingAioThrottle::wait()
{
std::unique_lock lock{mutex};
if (completed.empty() && !pending.empty()) {
ceph_assert(waiter == Wait::None);
waiter = Wait::Completion;
cond.wait(lock, [this] { return has_completion(); });
waiter = Wait::None;
}
return std::move(completed);
}
AioResultList BlockingAioThrottle::drain()
{
std::unique_lock lock{mutex};
if (!pending.empty()) {
ceph_assert(waiter == Wait::None);
waiter = Wait::Drained;
cond.wait(lock, [this] { return is_drained(); });
waiter = Wait::None;
}
return std::move(completed);
}
template <typename CompletionToken>
auto YieldingAioThrottle::async_wait(CompletionToken&& token)
{
using boost::asio::async_completion;
using Signature = void(boost::system::error_code);
async_completion<CompletionToken, Signature> init(token);
completion = Completion::create(context.get_executor(),
std::move(init.completion_handler));
return init.result.get();
}
AioResultList YieldingAioThrottle::get(const RGWSI_RADOS::Obj& obj,
OpFunc&& f,
uint64_t cost, uint64_t id)
{
auto p = std::make_unique<Pending>();
p->obj = obj;
p->id = id;
p->cost = cost;
if (cost > window) {
p->result = -EDEADLK; // would never succeed
completed.push_back(*p);
} else {
// wait for the write size to become available
pending_size += p->cost;
if (!is_available()) {
ceph_assert(waiter == Wait::None);
ceph_assert(!completion);
boost::system::error_code ec;
waiter = Wait::Available;
async_wait(yield[ec]);
}
// register the pending write and initiate the operation
pending.push_back(*p);
std::move(f)(this, *static_cast<AioResult*>(p.get()));
}
p.release();
return std::move(completed);
}
void YieldingAioThrottle::put(AioResult& r)
{
auto& p = static_cast<Pending&>(r);
// move from pending to completed
pending.erase(pending.iterator_to(p));
completed.push_back(p);
pending_size -= p.cost;
if (waiter_ready()) {
ceph_assert(completion);
ceph::async::post(std::move(completion), boost::system::error_code{});
waiter = Wait::None;
}
}
AioResultList YieldingAioThrottle::poll()
{
return std::move(completed);
}
AioResultList YieldingAioThrottle::wait()
{
if (!has_completion() && !pending.empty()) {
ceph_assert(waiter == Wait::None);
ceph_assert(!completion);
boost::system::error_code ec;
waiter = Wait::Completion;
async_wait(yield[ec]);
}
return std::move(completed);
}
AioResultList YieldingAioThrottle::drain()
{
if (!is_drained()) {
ceph_assert(waiter == Wait::None);
ceph_assert(!completion);
boost::system::error_code ec;
waiter = Wait::Drained;
async_wait(yield[ec]);
}
return std::move(completed);
}
} // namespace rgw
|