-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
cas.rs
247 lines (216 loc) · 7.07 KB
/
cas.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
// Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Duration;
use bytes::Bytes;
use futures::FutureExt;
use grpc_util::hyper::AddrIncomingWithStream;
use hashing::Fingerprint;
use parking_lot::Mutex;
use protos::gen::build::bazel::remote::execution::v2 as remexec;
use protos::gen::google::bytestream::byte_stream_server::ByteStreamServer;
use remexec::action_cache_server::ActionCacheServer;
use remexec::capabilities_server::CapabilitiesServer;
use remexec::content_addressable_storage_server::ContentAddressableStorageServer;
use testutil::data::{TestData, TestDirectory, TestTree};
use tonic::transport::Server;
use crate::action_cache_service::{ActionCacheHandle, ActionCacheResponder};
use crate::cas_service::StubCASResponder;
///
/// Implements the:
/// * ContentAddressableStorage
/// * ActionCache
/// * Capabilities
/// ...gRPC APIs.
///
/// NB: You might expect that these services could be generically composed, but the
/// `tonic::{Server, Router}` builder pattern changes its type with each call to `add_service`,
/// making it very challenging to wrap. Instead, we statically compose them.
///
pub struct StubCAS {
// CAS fields.
// TODO: These are inlined (rather than namespaced) for backwards compatibility.
read_request_count: Arc<Mutex<usize>>,
pub write_message_sizes: Arc<Mutex<Vec<usize>>>,
pub blobs: Arc<Mutex<HashMap<Fingerprint, Bytes>>>,
// AC fields.
pub action_cache: ActionCacheHandle,
// Generic server fields.
local_addr: SocketAddr,
shutdown_sender: Option<tokio::sync::oneshot::Sender<()>>,
}
impl Drop for StubCAS {
fn drop(&mut self) {
if let Some(s) = self.shutdown_sender.take() {
let _ = s.send(());
}
}
}
pub struct StubCASBuilder {
ac_always_errors: bool,
cas_always_errors: bool,
chunk_size_bytes: Option<usize>,
content: HashMap<Fingerprint, Bytes>,
port: Option<u16>,
instance_name: Option<String>,
required_auth_token: Option<String>,
ac_read_delay: Duration,
ac_write_delay: Duration,
}
impl StubCASBuilder {
pub fn new() -> Self {
StubCASBuilder {
ac_always_errors: false,
cas_always_errors: false,
chunk_size_bytes: None,
content: HashMap::new(),
port: None,
instance_name: None,
required_auth_token: None,
ac_read_delay: Duration::from_millis(0),
ac_write_delay: Duration::from_millis(0),
}
}
}
impl StubCASBuilder {
pub fn chunk_size_bytes(mut self, chunk_size_bytes: usize) -> Self {
if self.chunk_size_bytes.is_some() {
panic!("Can't set chunk_size_bytes twice");
}
self.chunk_size_bytes = Some(chunk_size_bytes);
self
}
pub fn port(mut self, port: u16) -> Self {
if self.port.is_some() {
panic!("Can't set port twice");
}
self.port = Some(port);
self
}
pub fn file(mut self, file: &TestData) -> Self {
self.content.insert(file.fingerprint(), file.bytes());
self
}
pub fn directory(mut self, directory: &TestDirectory) -> Self {
self
.content
.insert(directory.fingerprint(), directory.bytes());
self
}
pub fn tree(mut self, tree: &TestTree) -> Self {
self.content.insert(tree.fingerprint(), tree.bytes());
self
}
pub fn unverified_content(mut self, fingerprint: Fingerprint, content: Bytes) -> Self {
self.content.insert(fingerprint, content);
self
}
pub fn ac_always_errors(mut self) -> Self {
self.ac_always_errors = true;
self
}
pub fn cas_always_errors(mut self) -> Self {
self.cas_always_errors = true;
self
}
pub fn ac_read_delay(mut self, duration: Duration) -> Self {
self.ac_read_delay = duration;
self
}
pub fn ac_write_delay(mut self, duration: Duration) -> Self {
self.ac_write_delay = duration;
self
}
pub fn instance_name(mut self, instance_name: String) -> Self {
if self.instance_name.is_some() {
panic!("Can't set instance_name twice");
}
self.instance_name = Some(instance_name);
self
}
pub fn required_auth_token(mut self, required_auth_token: String) -> Self {
if self.required_auth_token.is_some() {
panic!("Can't set required_auth_token twice");
}
self.required_auth_token = Some(required_auth_token);
self
}
pub fn build(self) -> StubCAS {
let read_request_count = Arc::new(Mutex::new(0));
let write_message_sizes = Arc::new(Mutex::new(Vec::new()));
let blobs = Arc::new(Mutex::new(self.content));
let cas_responder = StubCASResponder {
chunk_size_bytes: self.chunk_size_bytes.unwrap_or(1024),
instance_name: self.instance_name,
blobs: blobs.clone(),
always_errors: self.cas_always_errors,
read_request_count: read_request_count.clone(),
write_message_sizes: write_message_sizes.clone(),
required_auth_header: self.required_auth_token.map(|t| format!("Bearer {}", t)),
};
let action_map = Arc::new(Mutex::new(HashMap::new()));
let ac_always_errors = Arc::new(AtomicBool::new(self.ac_always_errors));
let ac_responder = ActionCacheResponder {
action_map: action_map.clone(),
always_errors: ac_always_errors.clone(),
read_delay: self.ac_read_delay,
write_delay: self.ac_write_delay,
};
let addr = format!("127.0.0.1:{}", self.port.unwrap_or(0))
.parse()
.expect("failed to parse IP address");
let incoming = hyper::server::conn::AddrIncoming::bind(&addr).expect("failed to bind port");
let local_addr = incoming.local_addr();
let incoming = AddrIncomingWithStream(incoming);
let (shutdown_sender, shutdown_receiver) = tokio::sync::oneshot::channel();
tokio::spawn(async move {
let mut server = Server::builder();
let router = server
.add_service(ActionCacheServer::new(ac_responder.clone()))
.add_service(ByteStreamServer::new(cas_responder.clone()))
.add_service(ContentAddressableStorageServer::new(cas_responder.clone()))
.add_service(CapabilitiesServer::new(cas_responder));
router
.serve_with_incoming_shutdown(incoming, shutdown_receiver.map(drop))
.await
.unwrap();
});
StubCAS {
read_request_count,
write_message_sizes,
blobs,
action_cache: ActionCacheHandle {
action_map,
always_errors: ac_always_errors,
},
local_addr,
shutdown_sender: Some(shutdown_sender),
}
}
}
impl StubCAS {
pub fn builder() -> StubCASBuilder {
StubCASBuilder::new()
}
pub fn empty() -> StubCAS {
StubCAS::builder().build()
}
pub fn cas_always_errors() -> StubCAS {
StubCAS::builder().cas_always_errors().build()
}
///
/// The address on which this server is listening over insecure HTTP transport.
///
pub fn address(&self) -> String {
format!("http://{}", self.local_addr)
}
pub fn read_request_count(&self) -> usize {
*self.read_request_count.lock()
}
pub fn remove(&self, fingerprint: Fingerprint) -> bool {
self.blobs.lock().remove(&fingerprint).is_some()
}
}