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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use futures::Future;
use tokio::sync::oneshot::Sender;
use tokio::sync::{mpsc, oneshot};
use tokio::time::Instant;
use super::{
Commit, CommitPolicy, CommitPolicyNoDefault, DefaultCommitPolicy, InnerCommitPolicy,
ServiceHandleMessage, ShardStats,
};
use crate::{ServiceData, ShardError, ShardShutdownStats};
pub(super) struct ServiceHandleShardSender<Key, Data> {
sender: mpsc::Sender<ServiceHandleMessage<Key, Data>>,
}
impl<Key, Data> ServiceHandleShardSender<Key, Data> {
pub(super) fn from_sender(sender: mpsc::Sender<ServiceHandleMessage<Key, Data>>) -> Self {
Self { sender }
}
}
pub(super) struct ShutdownHandleShardSender<Key: Send + 'static, Data: ServiceData> {
sender: mpsc::Sender<ServiceHandleMessage<Key, Data>>,
}
impl<Key: Send + 'static, Data: ServiceData> ShutdownHandleShardSender<Key, Data> {
pub(super) fn from_sender(sender: mpsc::Sender<ServiceHandleMessage<Key, Data>>) -> Self {
Self { sender }
}
pub(super) async fn shutdown(self) -> Result<ShardShutdownStats, ShardError> {
let (result_tx, result_rx) = oneshot::channel();
self.sender
.send(ServiceHandleMessage::Shutdown(result_tx))
.await?;
Ok(result_rx.await?)
}
}
impl<Key: Send + 'static, Data: ServiceData> Clone for ShutdownHandleShardSender<Key, Data> {
fn clone(&self) -> Self {
Self {
sender: self.sender.clone(),
}
}
}
impl<Key, Data> Clone for ServiceHandleShardSender<Key, Data> {
fn clone(&self) -> Self {
Self {
sender: self.sender.clone(),
}
}
}
impl<Key, Data> ServiceHandleShardSender<Key, Data> {
pub(super) async fn execute<F, T>(&mut self, key: Key, func: F) -> Result<T, ShardError>
where
F: FnOnce(&Data) -> T + Send + 'static,
T: Send + 'static,
{
let (result_tx, result_rx) = oneshot::channel();
self.sender
.send(ServiceHandleMessage::Execute(
key,
Box::new(move |data: Result<&Data, ShardError>| {
// If the result_tx was closed, that means the `execute` future was dropped,
// which means no one is going to get the result of `func` anyways, so don't
// bother running it. In the event that the shard is overloaded, and causing
// timeouts, this serves as a load-shedding mechanism.
if result_tx.is_closed() {
return;
}
let result = match data {
Ok(data) => {
let result = (func)(data);
Ok(result)
}
Err(err) => Err(err),
};
result_tx.send(result).ok();
}),
))
.await?;
result_rx.await?
}
pub(super) async fn execute_if_cached<F, T>(
&mut self,
key: Key,
func: F,
) -> Result<Option<T>, ShardError>
where
F: FnOnce(&Data) -> T + Send + 'static,
T: Send + 'static,
{
let (result_tx, result_rx) = oneshot::channel();
self.sender
.send(ServiceHandleMessage::ExecuteIfCached(
key,
Box::new(move |data: Option<&Data>| {
// If the result_tx was closed, that means the `execute` future was dropped,
// which means no one is going to get the result of `func` anyways, so don't
// bother running it. In the event that the shard is overloaded, and causing
// timeouts, this serves as a load-shedding mechanism.
if result_tx.is_closed() {
return;
}
let result = match data {
Some(data) => {
let result = (func)(data);
Ok(Some(result))
}
None => Ok(None),
};
result_tx.send(result).ok();
}),
))
.await?;
result_rx.await?
}
pub(super) async fn execute_mut<F, T>(&mut self, key: Key, func: F) -> Result<T, ShardError>
where
F: FnOnce(&mut Data) -> Commit<T> + Send + 'static,
T: Send + 'static,
{
let (result_tx, result_rx) = oneshot::channel();
self.sender
.send(ServiceHandleMessage::ExecuteMut(
key,
Box::new(
move |data: Result<&mut Data, ShardError>,
default_commit_policy: DefaultCommitPolicy| {
// If the `result_tx` was closed, this means that the future awaiting the execution
// of the mutation was dropped. We can treat this as a "write timeout", where we
// simply give up mutating the data. This is a means of back-pressure, to ensure that
// if the client has given up trying to service this request, so does the shard. In
// the event that the shard is behind, this back-pressure and fast-failing can ensure
// that the shard will catch up to real-time.
if result_tx.is_closed() {
return InnerCommitPolicy::Noop;
}
match data {
Ok(data) => {
let result = (func)(data);
let (commit_policy, result) = result.into_inner();
let result = Ok(result);
// We need to convert the commit policy to the inner commit policy,
// the only difference here is that the "Immediate" variant holds a callback
// which will resolving the future for the caller until the upstream layer
// has acknowledged the write.
// The code dupe around `result_tx.send(...)` kinda sucks, but unfortunately
// is unavoidable.
let commit_policy =
commit_policy.apply_default(default_commit_policy);
match commit_policy {
CommitPolicyNoDefault::Within(duration) => {
result_tx.send(result).ok();
InnerCommitPolicy::Within(duration)
}
CommitPolicyNoDefault::Noop => {
result_tx.send(result).ok();
InnerCommitPolicy::Noop
}
CommitPolicyNoDefault::Immediate => {
result_tx.send(result).ok();
// todo: move `result_tx.send(result)` back in here once the data persist stuff is
// fully done.
InnerCommitPolicy::Immediate(Box::new(move |result| {
// result_tx.send(result).ok();
}))
}
}
}
Err(err) => {
result_tx.send(Err(err)).ok();
InnerCommitPolicy::Noop
}
}
},
),
))
.await?;
result_rx.await?
}
pub(super) async fn take_data(
&mut self,
key: Key,
) -> Result<Option<TakenData<Key, Data>>, ShardError> {
let (result_tx, result_rx) = oneshot::channel();
self.sender
.send(ServiceHandleMessage::TakeData(key, result_tx))
.await?;
Ok(result_rx.await?)
}
pub(super) async fn get_shard_stats(&mut self) -> Result<ShardStats, ShardError> {
let (result_tx, result_rx) = oneshot::channel();
self.sender
.send(ServiceHandleMessage::GetStats(result_tx))
.await?;
Ok(result_rx.await?)
}
}
pub struct TakenData<Key, Data> {
pub key: Key,
pub data: Data,
pub was_enqueued_at: Option<Instant>,
}