reader_concurrency_semaphore: add state to permits

Instead of a simple boolean, designating whether the permit was already
admitted or not, add a proper state field with a value for all the
different states the permit can be in. Currently there are three such
states:
* registered - the permit was created and started accounting resource
  consumption.
* waiting - the permit was queued to wait for admission.
* admitted - the permit was successfully admitted.

The state will be used for debugging purposes, both during coredump
debugging as well as for dumping diagnostics data about permits.
This commit is contained in:
Botond Dénes
2020-10-08 08:45:01 +03:00
parent ff623e70b3
commit 70fa543c31
2 changed files with 24 additions and 4 deletions

View File

@@ -74,7 +74,7 @@ class reader_permit::impl {
sstring _op_name;
std::string_view _op_name_view;
reader_resources _resources;
bool _admitted = false;
reader_permit::state _state = reader_permit::state::registered;
public:
struct value_tag {};
@@ -113,21 +113,29 @@ public:
return _op_name_view;
}
reader_permit::state get_state() const {
return _state;
}
void on_waiting() {
_state = reader_permit::state::waiting;
}
void on_admission() {
_admitted = true;
_state = reader_permit::state::admitted;
_semaphore.consume(_resources);
}
void consume(reader_resources res) {
_resources += res;
if (_admitted) {
if (_state == reader_permit::state::admitted) {
_semaphore.consume(res);
}
}
void signal(reader_resources res) {
_resources -= res;
if (_admitted) {
if (_state == reader_permit::state::admitted) {
_semaphore.signal(res);
}
}
@@ -147,6 +155,10 @@ reader_permit::reader_permit(reader_concurrency_semaphore& semaphore, const sche
{
}
void reader_permit::on_waiting() {
_impl->on_waiting();
}
void reader_permit::on_admission() {
_impl->on_admission();
}
@@ -287,6 +299,7 @@ future<reader_permit::resource_units> reader_concurrency_semaphore::do_wait_admi
}
promise<reader_permit::resource_units> pr;
auto fut = pr.get_future();
permit.on_waiting();
_wait_list.push_back(entry(std::move(pr), std::move(permit), r), timeout);
return fut;
}

View File

@@ -90,6 +90,12 @@ class reader_permit {
public:
class resource_units;
enum class state {
registered, // read is registered, but didn't attempt admission yet
waiting, // waiting for admission
admitted,
};
private:
class impl;
shared_ptr<impl> _impl;
@@ -98,6 +104,7 @@ private:
explicit reader_permit(reader_concurrency_semaphore& semaphore, const schema* const schema, std::string_view op_name);
explicit reader_permit(reader_concurrency_semaphore& semaphore, const schema* const schema, sstring&& op_name);
void on_waiting();
void on_admission();
public: