forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialized_action.hh
126 lines (110 loc) · 3.99 KB
/
serialized_action.hh
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
/*
* Copyright (C) 2017-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <functional>
#include <seastar/core/semaphore.hh>
#include <seastar/core/future.hh>
#include <seastar/core/shared_future.hh>
#include <seastar/util/later.hh>
#include <seastar/core/abort_source.hh>
// An async action wrapper which ensures that at most one action
// is running at any time.
class serialized_action {
public:
template <typename... T>
using future = seastar::future<T...>;
private:
std::function<future<>()> _func;
seastar::shared_future<> _pending;
seastar::semaphore _sem;
private:
future<> do_trigger() {
_pending = {};
return futurize_invoke(_func);
}
public:
serialized_action(std::function<future<>()> func)
: _func(std::move(func))
, _sem(1)
{ }
serialized_action(serialized_action&&) = delete;
serialized_action(const serialized_action&) = delete;
// Makes sure that a new action will be started after this call and
// returns a future which resolves when that action completes.
// At most one action can run at any given moment.
// A single action is started on behalf of all earlier triggers.
//
// When action is not currently running, it is started immediately if !later or
// at some point in time soon after current fiber defers when later is true.
future<> trigger(bool later = false, seastar::abort_source* as = nullptr) {
if (_pending.valid()) {
return as ? _pending.get_future(*as) : _pending.get_future();
}
seastar::shared_promise<> pr;
_pending = pr.get_shared_future();
future<> ret = _pending;
std::optional<future<>> abortable;
if (as) {
abortable = _pending.get_future(*as);
}
// run in background, synchronize using `ret`
(void)_sem.wait().then([this, later] () mutable {
if (later) {
return seastar::yield().then([this] () mutable {
return do_trigger();
});
}
return do_trigger();
}).then_wrapped([pr = std::move(pr)] (auto&& f) mutable {
if (f.failed()) {
pr.set_exception(f.get_exception());
} else {
pr.set_value();
}
});
ret = ret.finally([this] {
_sem.signal();
});
if (abortable) {
// exception will be delivered to each individual future as well
(void)std::move(ret).handle_exception([] (std::exception_ptr ep) {});
return std::move(*abortable);
}
return ret;
}
// Like trigger() but can be aborted
future<> trigger(seastar::abort_source& as) {
return trigger(false, &as);
}
// Like trigger(), but defers invocation of the action to allow for batching
// more requests.
future<> trigger_later() {
return trigger(true);
}
// Waits for all invocations initiated in the past.
future<> join() {
return get_units(_sem, 1).discard_result();
}
// The adaptor is to be used as an argument to utils::observable.observe()
// When the notification happens the adaptor just triggers the action
// Note, that all arguments provided by the notification callback are lost,
// its up to the action to get the needed values
// Also, the future<> returned from .trigger() is ignored, the action code
// runs in the background. The user should .join() the action if it needs
// to wait for it to finish on stop/drain/shutdown
class observing_adaptor {
friend class serialized_action;
serialized_action& _action;
observing_adaptor(serialized_action& act) noexcept : _action(act) {}
public:
template <typename... Args>
void operator()(Args&&...) { (void)_action.trigger(); };
};
observing_adaptor make_observer() noexcept {
return observing_adaptor(*this);
}
};