-
Notifications
You must be signed in to change notification settings - Fork 187
/
replay.rs
302 lines (283 loc) · 10.8 KB
/
replay.rs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
use crate::dvr::{Action, ConnectionId, Direction, Event};
use aws_smithy_http::body::SdkBody;
use aws_smithy_http::result::ConnectorError;
use bytes::{Bytes, BytesMut};
use http::{Request, Version};
use http_body::Body;
use std::collections::{HashMap, VecDeque};
use std::error::Error;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use tokio::task::JoinHandle;
/// Wrapper type to enable optionally waiting for a future to complete
#[derive(Debug)]
enum Waitable<T> {
Loading(JoinHandle<T>),
Value(T),
}
impl<T> Waitable<T> {
/// Consumes the future and returns the value
async fn take(self) -> T {
match self {
Waitable::Loading(f) => f.await.expect("join failed"),
Waitable::Value(value) => value,
}
}
/// Waits for the future to be ready
async fn wait(&mut self) {
match self {
Waitable::Loading(f) => *self = Waitable::Value(f.await.expect("join failed")),
Waitable::Value(_) => {}
}
}
}
/// Replay traffic recorded by a [`RecordingConnection`](super::RecordingConnection)
#[derive(Clone, Debug)]
pub struct ReplayingConnection {
live_events: Arc<Mutex<HashMap<ConnectionId, VecDeque<Event>>>>,
verifiable_events: Arc<HashMap<ConnectionId, Request<Bytes>>>,
num_events: Arc<AtomicUsize>,
recorded_requests: Arc<Mutex<HashMap<ConnectionId, Waitable<http::Request<Bytes>>>>>,
}
impl ReplayingConnection {
fn next_id(&self) -> ConnectionId {
ConnectionId(self.num_events.fetch_add(1, Ordering::Relaxed))
}
/// Validate actual requests against expected requests
pub async fn validate(
self,
checked_headers: &[&str],
body_comparer: impl Fn(&[u8], &[u8]) -> Result<(), Box<dyn Error>>,
) -> Result<(), Box<dyn Error>> {
let mut actual_requests = self.recorded_requests.lock().unwrap();
for conn_id in 0..self.verifiable_events.len() {
let conn_id = ConnectionId(conn_id);
let expected = self.verifiable_events.get(&conn_id).unwrap();
let actual = actual_requests
.remove(&conn_id)
.ok_or(format!(
"expected connection {:?} but request was never sent",
conn_id
))?
.take()
.await;
aws_smithy_protocol_test::assert_uris_match(actual.uri(), expected.uri());
body_comparer(expected.body().as_ref(), actual.body().as_ref())?;
let expected_headers = checked_headers
.iter()
.flat_map(|key| {
let _ = expected.headers().get(*key)?;
Some((
*key,
expected
.headers()
.get_all(*key)
.iter()
.map(|h| h.to_str().unwrap())
.collect::<Vec<_>>()
.join(", "),
))
})
.collect::<Vec<_>>();
aws_smithy_protocol_test::validate_headers(actual.headers(), expected_headers)?;
}
Ok(())
}
/// Return all the recorded requests for further analysis
pub async fn take_requests(self) -> Vec<http::Request<Bytes>> {
let mut recorded_requests = self.recorded_requests.lock().unwrap();
let mut out = Vec::with_capacity(recorded_requests.len());
for conn_id in 0..recorded_requests.len() {
out.push(
recorded_requests
.remove(&ConnectionId(conn_id))
.expect("should exist")
.take()
.await,
)
}
out
}
/// Build a replay connection from a sequence of events
pub fn new(events: Vec<Event>) -> Self {
let mut event_map: HashMap<_, VecDeque<_>> = HashMap::new();
for event in events {
let event_buffer = event_map.entry(event.connection_id).or_default();
event_buffer.push_back(event);
}
let verifiable_events = event_map
.iter()
.map(|(id, events)| {
let mut body = BytesMut::new();
for event in events {
if let Action::Data {
direction: Direction::Request,
data,
} = &event.action
{
body.extend_from_slice(&data.copy_to_vec());
}
}
let initial_request = events.iter().next().expect("must have one event");
let request = match &initial_request.action {
Action::Request { request } => {
http::Request::from(request).map(|_| Bytes::from(body))
}
_ => panic!("invalid first event"),
};
(*id, request)
})
.collect();
let verifiable_events = Arc::new(verifiable_events);
ReplayingConnection {
live_events: Arc::new(Mutex::new(event_map)),
num_events: Arc::new(AtomicUsize::new(0)),
recorded_requests: Default::default(),
verifiable_events,
}
}
}
async fn replay_body(events: VecDeque<Event>, mut sender: hyper::body::Sender) {
for event in events {
match event.action {
Action::Request { .. } => panic!(),
Action::Response { .. } => panic!(),
Action::Data {
data,
direction: Direction::Response,
} => {
sender
.send_data(Bytes::from(data.into_bytes()))
.await
.expect("this is in memory traffic that should not fail to send");
}
Action::Data {
data: _data,
direction: Direction::Request,
} => {}
Action::Eof {
direction: Direction::Request,
..
} => {}
Action::Eof {
direction: Direction::Response,
ok: true,
..
} => {
drop(sender);
break;
}
Action::Eof {
direction: Direction::Response,
ok: false,
..
} => {
sender.abort();
break;
}
}
}
}
fn convert_version(version: &str) -> Version {
match version {
"HTTP/1.1" => Version::HTTP_11,
"HTTP/2.0" => Version::HTTP_2,
_ => panic!("unsupported: {}", version),
}
}
impl tower::Service<http::Request<SdkBody>> for ReplayingConnection {
type Response = http::Response<SdkBody>;
type Error = ConnectorError;
#[allow(clippy::type_complexity)]
type Future = std::pin::Pin<
Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>,
>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, mut req: Request<SdkBody>) -> Self::Future {
let event_id = self.next_id();
let mut events = match self.live_events.lock().unwrap().remove(&event_id) {
Some(traffic) => traffic,
None => {
return Box::pin(std::future::ready(Err(ConnectorError::other(
format!("no data for event {}. req: {:?}", event_id.0, req).into(),
None,
))))
}
};
let _initial_request = events.pop_front().unwrap();
let (sender, response_body) = hyper::Body::channel();
let body = SdkBody::from(response_body);
let recording = self.recorded_requests.clone();
let recorded_request = tokio::spawn(async move {
let mut data_read = vec![];
while let Some(data) = req.body_mut().data().await {
data_read
.extend_from_slice(data.expect("in memory request should not fail").as_ref())
}
req.map(|_| Bytes::from(data_read))
});
let mut recorded_request = Waitable::Loading(recorded_request);
let fut = async move {
let resp = loop {
let event = events
.pop_front()
.expect("no events, needed a response event");
match event.action {
// to ensure deterministic behavior if the request EOF happens first in the log,
// wait for the request body to be done before returning a response.
Action::Eof {
direction: Direction::Request,
..
} => {
recorded_request.wait().await;
}
Action::Request { .. } => panic!("invalid"),
Action::Response {
response: Err(error),
} => break Err(ConnectorError::other(error.0.into(), None)),
Action::Response {
response: Ok(response),
} => {
let mut builder = http::Response::builder()
.status(response.status)
.version(convert_version(&response.version));
for (name, values) in response.headers {
for value in values {
builder = builder.header(&name, &value);
}
}
tokio::spawn(async move {
replay_body(events, sender).await;
// insert the finalized body into
});
break Ok(builder.body(body).expect("valid builder"));
}
Action::Data {
direction: Direction::Request,
data: _data,
} => {
tracing::info!("get request data");
}
Action::Eof {
direction: Direction::Response,
..
} => panic!("got eof before response"),
Action::Data {
data: _,
direction: Direction::Response,
} => panic!("got response data before response"),
}
};
recording.lock().unwrap().insert(event_id, recorded_request);
resp
};
Box::pin(fut)
}
}