-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathobjectstore.rs
More file actions
524 lines (456 loc) · 16.5 KB
/
objectstore.rs
File metadata and controls
524 lines (456 loc) · 16.5 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Objectstore service for uploading attachments.
use std::array::TryFromSliceError;
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use bytes::Bytes;
use futures::StreamExt;
use objectstore_client::{Client, ExpirationPolicy, PutBuilder, Session, Usecase};
use relay_config::ObjectstoreServiceConfig;
use relay_system::{
Addr, AsyncResponse, FromMessage, Interface, LoadShed, NoResponse, Sender, SimpleService,
};
use sentry_protos::snuba::v1::TraceItem;
use uuid::Uuid;
use crate::constants::DEFAULT_ATTACHMENT_RETENTION;
use crate::envelope::ItemType;
use crate::managed::{
Counted, Managed, ManagedEnvelope, ManagedResult, OutcomeError, Quantities, Rejected,
};
use crate::processing::utils::store::item_id_to_uuid;
use crate::services::outcome::DiscardReason;
use crate::services::store::{Store, StoreAttachment, StoreEnvelope, StoreTraceItem};
use crate::services::upload;
use crate::statsd::{RelayCounters, RelayTimers};
use super::outcome::Outcome;
/// Messages that the objectstore service can handle.
pub enum Objectstore {
Envelope(StoreEnvelope),
TraceAttachment(Managed<StoreTraceAttachment>),
EventAttachment(Managed<StoreAttachment>),
Stream {
message: upload::Stream,
sender: Sender<Result<ObjectstoreKey, Error>>,
},
}
impl Objectstore {
fn ty(&self) -> &str {
match self {
Objectstore::Envelope(_) => "envelope",
Objectstore::TraceAttachment(_) => "attachment_v2",
Objectstore::EventAttachment(_) => "attachment",
Objectstore::Stream { .. } => "stream",
}
}
fn attachment_count(&self) -> usize {
match self {
Self::Envelope(StoreEnvelope { envelope }) => envelope
.envelope()
.items()
.filter(|item| *item.ty() == ItemType::Attachment)
.count(),
Self::TraceAttachment(_) => 1,
Self::EventAttachment(_) => 1,
Self::Stream { .. } => 1,
}
}
}
impl Interface for Objectstore {}
impl FromMessage<StoreEnvelope> for Objectstore {
type Response = NoResponse;
fn from_message(message: StoreEnvelope, _sender: ()) -> Self {
Self::Envelope(message)
}
}
impl FromMessage<Managed<StoreTraceAttachment>> for Objectstore {
type Response = NoResponse;
fn from_message(message: Managed<StoreTraceAttachment>, _sender: ()) -> Self {
Self::TraceAttachment(message)
}
}
impl FromMessage<Managed<StoreAttachment>> for Objectstore {
type Response = NoResponse;
fn from_message(message: Managed<StoreAttachment>, _sender: ()) -> Self {
Self::EventAttachment(message)
}
}
impl FromMessage<upload::Stream> for Objectstore {
type Response = AsyncResponse<Result<ObjectstoreKey, Error>>;
fn from_message(
message: upload::Stream,
sender: Sender<Result<ObjectstoreKey, Error>>,
) -> Self {
Self::Stream { message, sender }
}
}
/// An attachment that is ready for upload / EAP storage.
pub struct StoreTraceAttachment {
/// The body to be uploaded to objectstore.
pub body: Bytes,
/// The trace item to be published via Kafka.
pub trace_item: TraceItem,
}
impl Counted for StoreTraceAttachment {
fn quantities(&self) -> Quantities {
self.trace_item.quantities()
}
}
/// Errors that can occur when trying to upload an attachment.
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("load shed")]
LoadShed,
#[error("upload failed: {0}")]
UploadFailed(#[from] objectstore_client::Error),
#[error("UUID conversion failed: {0}")]
Uuid(#[from] TryFromSliceError),
}
impl Error {
fn as_str(&self) -> &'static str {
match self {
Error::LoadShed => "load-shed",
Error::UploadFailed(_) => "upload_failed",
Error::Uuid(_) => "uuid",
}
}
}
impl OutcomeError for Error {
type Error = Self;
fn consume(self) -> (Option<Outcome>, Self::Error) {
(Some(Outcome::Invalid(DiscardReason::Internal)), self)
}
}
/// The objectstore key that identifies a successful upload.
#[derive(Debug, PartialEq)]
pub struct ObjectstoreKey(String);
impl ObjectstoreKey {
/// Returns the underlying [`String`].
pub fn into_inner(self) -> String {
self.0
}
}
impl fmt::Display for ObjectstoreKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
/// The objectstore service that uploads attachments.
///
/// Accepts upload requests and maintains a list of concurrent uploads.
#[derive(Clone)]
pub struct ObjectstoreService {
inner: Arc<ObjectstoreServiceInner>,
}
impl ObjectstoreService {
pub fn new(
config: &ObjectstoreServiceConfig,
store: Option<Addr<Store>>,
) -> anyhow::Result<Option<Self>> {
let Some(store) = store else { return Ok(None) };
let ObjectstoreServiceConfig {
objectstore_url,
max_concurrent_requests: _,
max_backlog: _,
timeout,
} = config;
let Some(objectstore_url) = objectstore_url else {
return Ok(None);
};
let objectstore_client = Client::builder(objectstore_url)
.configure_reqwest(|builder| builder.timeout(Duration::from_secs(*timeout)))
.build()?;
let event_attachments = Usecase::new("attachments")
.with_expiration_policy(ExpirationPolicy::TimeToLive(DEFAULT_ATTACHMENT_RETENTION));
let trace_attachments = Usecase::new("trace_attachments")
.with_expiration_policy(ExpirationPolicy::TimeToLive(DEFAULT_ATTACHMENT_RETENTION));
let inner = ObjectstoreServiceInner {
store,
objectstore_client,
event_attachments,
trace_attachments,
};
Ok(Some(Self {
inner: Arc::new(inner),
}))
}
}
impl SimpleService for ObjectstoreService {
type Interface = Objectstore;
async fn handle_message(&self, message: Self::Interface) {
self.inner.handle_message(message).await
}
}
impl LoadShed<Objectstore> for ObjectstoreService {
fn handle_loadshed(&self, message: Objectstore) {
relay_statsd::metric!(
counter(RelayCounters::AttachmentUpload) += message.attachment_count() as u64,
result = "load_shed",
type = message.ty(),
);
match message {
Objectstore::Envelope(envelope) => {
// Event attachments can still go the old route.
self.inner.store.send(envelope);
}
Objectstore::EventAttachment(message) => {
// Event attachments can still go the old route.
self.inner.store.send(message);
}
Objectstore::TraceAttachment(managed) => {
let _ = managed.reject_err(Error::LoadShed);
}
Objectstore::Stream { message: _, sender } => {
sender.send(Err(Error::LoadShed));
}
}
}
}
struct ObjectstoreServiceInner {
store: Addr<Store>,
objectstore_client: Client,
event_attachments: Usecase,
trace_attachments: Usecase,
}
impl ObjectstoreServiceInner {
async fn handle_message(&self, message: Objectstore) {
match message {
Objectstore::Envelope(StoreEnvelope { envelope }) => {
self.handle_envelope(envelope).await;
}
Objectstore::TraceAttachment(attachment) => {
self.handle_trace_attachment(attachment).await
}
Objectstore::EventAttachment(attachment) => {
self.handle_event_attachment(attachment).await
}
Objectstore::Stream {
message: managed,
sender,
} => self.handle_stream(managed, sender).await,
}
}
/// Uploads all attachments belonging to the given envelope.
///
/// This mutates the attachment items in-place, setting their `stored_key` field to the key
/// in objectstore.
async fn handle_envelope(&self, mut envelope: ManagedEnvelope) -> () {
let scoping = envelope.scoping();
let session = self
.event_attachments
.for_project(scoping.organization_id.value(), scoping.project_id.value())
.session(&self.objectstore_client);
let attachments = envelope
.envelope_mut()
.items_mut()
.filter(|item| *item.ty() == ItemType::Attachment);
match session {
Err(error) => {
relay_log::error!(error = &error as &dyn std::error::Error, "session error");
relay_statsd::metric!(
counter(RelayCounters::AttachmentUpload) += attachments.count() as u64,
result = error.to_string().as_str(),
type = "envelope",
);
}
Ok(session) => {
for attachment in attachments {
// we are not storing zero-size attachments in objectstore
if attachment.is_empty() {
continue;
}
let result = self
.upload_bytes("envelope", &session, attachment.payload(), None)
.await;
relay_statsd::metric!(
counter(RelayCounters::AttachmentUpload) += 1,
result = match &result {
Ok(_) => "success",
Err(e) => e.as_str(),
},
type = "envelope",
);
if let Ok(stored_key) = result {
attachment.set_stored_key(stored_key.into_inner());
}
}
}
}
// last but not least, forward the envelope to the store endpoint
self.store.send(StoreEnvelope { envelope });
}
/// Uploads the attachment.
///
/// This mutates the attachment item in-place, setting the `stored_key` field to the key in the
/// objectstore.
async fn handle_event_attachment(&self, mut attachment: Managed<StoreAttachment>) {
// we are not storing zero-size attachments in objectstore
if attachment.attachment.is_empty() {
self.store.send(attachment);
return;
}
let scoping = attachment.scoping();
let session = self
.event_attachments
.for_project(scoping.organization_id.value(), scoping.project_id.value())
.session(&self.objectstore_client);
match session {
Err(error) => {
relay_log::error!(error = &error as &dyn std::error::Error, "session error");
relay_statsd::metric!(
counter(RelayCounters::AttachmentUpload) += 1,
result = error.to_string().as_str(),
type = "attachment",
);
}
Ok(session) => {
let result = self
.upload_bytes(
"attachment",
&session,
attachment.attachment.payload(),
None,
)
.await;
relay_statsd::metric!(
counter(RelayCounters::AttachmentUpload) += 1,
result = match &result {
Ok(_) => "success",
Err(e) => e.as_str(),
},
type = "attachment",
);
if let Ok(stored_key) = result {
attachment.modify(|attachment, _| {
attachment
.attachment
.set_stored_key(stored_key.into_inner());
});
}
}
}
self.store.send(attachment)
}
async fn handle_trace_attachment(&self, managed: Managed<StoreTraceAttachment>) {
let result = self.do_handle_store_attachment(managed).await;
relay_statsd::metric!(
counter(RelayCounters::AttachmentUpload) += 1,
result = match result {
Ok(()) => "success",
Err(e) => e.into_inner().as_str(),
},
type = "attachment_v2",
);
}
async fn do_handle_store_attachment(
&self,
managed: Managed<StoreTraceAttachment>,
) -> Result<(), Rejected<Error>> {
let scoping = managed.scoping();
let session = self
.trace_attachments
.for_project(scoping.organization_id.value(), scoping.project_id.value())
.session(&self.objectstore_client)
.map_err(Error::UploadFailed)
.reject(&managed)?;
let body = Bytes::clone(&managed.body);
// Make sure that the attachment can be converted into a trace item:
let trace_item = managed.try_map(|attachment, _record_keeper| {
let StoreTraceAttachment {
trace_item,
body: _,
} = attachment;
Ok::<_, Error>(StoreTraceItem { trace_item })
})?;
// Upload the attachment:
if !body.is_empty() {
relay_log::trace!("Starting attachment upload");
let key = item_id_to_uuid(&trace_item.trace_item.item_id)
.map_err(Error::from)
.reject(&trace_item)?
.as_simple()
.to_string();
#[cfg(debug_assertions)]
let original_key = key.clone();
let _stored_key = self
.upload_bytes("attachment_v2", &session, body, Some(key))
.await
.reject(&trace_item)?;
#[cfg(debug_assertions)]
debug_assert_eq!(_stored_key.into_inner(), original_key);
}
// Only after successful upload forward the attachment to the store.
self.store.send(trace_item);
Ok(())
}
async fn handle_stream(
&self,
upload::Stream { scoping, stream }: upload::Stream,
sender: Sender<Result<ObjectstoreKey, Error>>,
) {
let session = match self
.event_attachments
.for_project(scoping.organization_id.value(), scoping.project_id.value())
.session(&self.objectstore_client)
{
Ok(session) => session,
Err(error) => {
relay_statsd::metric!(
counter(RelayCounters::AttachmentUpload) += 1,
result = error.to_string().as_str(),
type = "stream",
);
sender.send(Err(Error::UploadFailed(error)));
return;
}
};
let request = session
.put_stream(stream.boxed())
// generate ID here to drop the hyphens and be consistent with other attachment uploads.
.key(Uuid::now_v7().as_simple().to_string());
let result = self.upload("stream", request).await;
relay_statsd::metric!(
counter(RelayCounters::AttachmentUpload) += 1,
result = match &result {
Ok(_) => "success",
Err(e) => e.as_str(),
},
type = "stream",
);
sender.send(result);
}
async fn upload_bytes(
&self,
ty: &str,
session: &Session,
payload: Bytes,
key: Option<String>,
) -> Result<ObjectstoreKey, Error> {
let mut request = session.put(payload);
if let Some(key) = key {
request = request.key(key);
}
self.upload(ty, request).await
}
async fn upload(&self, ty: &str, request: PutBuilder) -> Result<ObjectstoreKey, Error> {
self.upload_inner(ty, request).await.inspect_err(|e| {
relay_log::error!(
error = e as &dyn std::error::Error,
"objectstore upload failed"
)
})
}
async fn upload_inner(&self, ty: &str, request: PutBuilder) -> Result<ObjectstoreKey, Error> {
relay_log::trace!("Starting attachment upload");
let response = relay_statsd::metric!(
timer(RelayTimers::AttachmentUploadDuration),
type = ty,
{
request.send()
.await
.map_err(Error::UploadFailed)?
}
);
relay_log::trace!("Finished attachment upload");
Ok(ObjectstoreKey(response.key))
}
}