-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathtimestamp.rs
More file actions
237 lines (204 loc) · 7.95 KB
/
timestamp.rs
File metadata and controls
237 lines (204 loc) · 7.95 KB
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
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
//! This module is the low-level mechanisms for getting timestamps from a PD
//! cluster. It should be used via the `get_timestamp` API in `PdClient`.
//!
//! Once a `TimestampOracle` is created, there will be two futures running in a background working
//! thread created automatically. The `get_timestamp` method creates a oneshot channel whose
//! transmitter is served as a `TimestampRequest`. `TimestampRequest`s are sent to the working
//! thread through a bounded multi-producer, single-consumer channel. Every time the first future
//! is polled, it tries to exhaust the channel to get as many requests as possible and sends a
//! single `TsoRequest` to the PD server. The other future receives `TsoResponse`s from the PD
//! server and allocates timestamps for the requests.
use std::collections::VecDeque;
use std::pin::Pin;
use std::sync::Arc;
use futures::pin_mut;
use futures::prelude::*;
use futures::task::AtomicWaker;
use futures::task::Context;
use futures::task::Poll;
use log::debug;
use log::info;
use minitrace::prelude::*;
use pin_project::pin_project;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use tokio::sync::Mutex;
use tonic::transport::Channel;
use crate::internal_err;
use crate::proto::pdpb::pd_client::PdClient;
use crate::proto::pdpb::*;
use crate::Result;
/// It is an empirical value.
const MAX_BATCH_SIZE: usize = 64;
/// TODO: This value should be adjustable.
const MAX_PENDING_COUNT: usize = 1 << 16;
type TimestampRequest = oneshot::Sender<Timestamp>;
/// The timestamp oracle (TSO) which provides monotonically increasing timestamps.
#[derive(Clone)]
pub(crate) struct TimestampOracle {
/// The transmitter of a bounded channel which transports requests of getting a single
/// timestamp to the TSO working thread. A bounded channel is used to prevent using
/// too much memory unexpectedly.
/// In the working thread, the `TimestampRequest`, which is actually a one channel sender,
/// is used to send back the timestamp result.
request_tx: mpsc::Sender<TimestampRequest>,
}
impl TimestampOracle {
pub(crate) fn new(cluster_id: u64, pd_client: &PdClient<Channel>) -> Result<TimestampOracle> {
let pd_client = pd_client.clone();
let (request_tx, request_rx) = mpsc::channel(MAX_BATCH_SIZE);
// Start a background thread to handle TSO requests and responses
tokio::spawn(run_tso(cluster_id, pd_client, request_rx));
Ok(TimestampOracle { request_tx })
}
#[minitrace::trace]
pub(crate) async fn get_timestamp(self) -> Result<Timestamp> {
debug!("getting current timestamp");
let (request, response) = oneshot::channel();
self.request_tx
.send(request)
.await
.map_err(|_| internal_err!("TimestampRequest channel is closed"))?;
Ok(response.await?)
}
}
#[minitrace::trace]
async fn run_tso(
cluster_id: u64,
mut pd_client: PdClient<Channel>,
request_rx: mpsc::Receiver<TimestampRequest>,
) -> Result<()> {
// The `TimestampRequest`s which are waiting for the responses from the PD server
let pending_requests = Arc::new(Mutex::new(VecDeque::with_capacity(MAX_PENDING_COUNT)));
// When there are too many pending requests, the `send_request` future will refuse to fetch
// more requests from the bounded channel. This waker is used to wake up the sending future
// if the queue containing pending requests is no longer full.
let sending_future_waker = Arc::new(AtomicWaker::new());
let request_stream = TsoRequestStream {
cluster_id,
request_rx,
pending_requests: pending_requests.clone(),
self_waker: sending_future_waker.clone(),
};
// let send_requests = rpc_sender.send_all(&mut request_stream);
let mut responses = pd_client.tso(request_stream).await?.into_inner();
while let Some(Ok(resp)) = responses.next().await {
let _span = LocalSpan::enter_with_local_parent("handle_response");
debug!("got response: {:?}", resp);
{
let mut pending_requests = pending_requests.lock().await;
allocate_timestamps(&resp, &mut pending_requests)?;
}
// Wake up the sending future blocked by too many pending requests or locked.
sending_future_waker.wake();
}
// TODO: distinguish between unexpected stream termination and expected end of test
info!("TSO stream terminated");
Ok(())
}
struct RequestGroup {
tso_request: TsoRequest,
requests: Vec<TimestampRequest>,
}
#[pin_project]
struct TsoRequestStream {
cluster_id: u64,
#[pin]
request_rx: mpsc::Receiver<oneshot::Sender<Timestamp>>,
pending_requests: Arc<Mutex<VecDeque<RequestGroup>>>,
self_waker: Arc<AtomicWaker>,
}
impl Stream for TsoRequestStream {
type Item = TsoRequest;
#[minitrace::trace]
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let mut this = self.project();
let pending_requests = this.pending_requests.lock();
pin_mut!(pending_requests);
let mut pending_requests = if let Poll::Ready(pending_requests) = pending_requests.poll(cx)
{
pending_requests
} else {
this.self_waker.register(cx.waker());
return Poll::Pending;
};
let mut requests = Vec::new();
while requests.len() < MAX_BATCH_SIZE && pending_requests.len() < MAX_PENDING_COUNT {
match this.request_rx.poll_recv(cx) {
Poll::Ready(Some(sender)) => {
requests.push(sender);
}
Poll::Ready(None) if requests.is_empty() => return Poll::Ready(None),
_ => break,
}
}
debug!(
"got requests: len {}, pending_requests {}",
requests.len(),
pending_requests.len()
);
if !requests.is_empty() {
let req = TsoRequest {
header: Some(RequestHeader {
cluster_id: *this.cluster_id,
sender_id: 0,
}),
count: requests.len() as u32,
dc_location: String::new(),
};
let request_group = RequestGroup {
tso_request: req.clone(),
requests,
};
pending_requests.push_back(request_group);
debug!(
"sending request to PD: {:?}, pending_requests {}",
req,
pending_requests.len()
);
Poll::Ready(Some(req))
} else {
// Set the waker to the context, then the stream can be waked up after the pending queue
// is no longer full.
this.self_waker.register(cx.waker());
Poll::Pending
}
}
}
fn allocate_timestamps(
resp: &TsoResponse,
pending_requests: &mut VecDeque<RequestGroup>,
) -> Result<()> {
// PD returns the timestamp with the biggest logical value. We can send back timestamps
// whose logical value is from `logical - count + 1` to `logical` using the senders
// in `pending`.
let tail_ts = resp
.timestamp
.as_ref()
.ok_or_else(|| internal_err!("No timestamp in TsoResponse"))?;
let mut offset = resp.count;
if let Some(RequestGroup {
tso_request,
requests,
}) = pending_requests.pop_front()
{
if tso_request.count != offset {
return Err(internal_err!(
"PD gives different number of timestamps than expected"
));
}
for request in requests {
offset -= 1;
let ts = Timestamp {
physical: tail_ts.physical,
logical: tail_ts.logical - offset as i64,
suffix_bits: tail_ts.suffix_bits,
};
let _ = request.send(ts);
}
} else {
return Err(internal_err!("PD gives more TsoResponse than expected"));
};
Ok(())
}