|
1 | | -use super::{Command, Event, Shared, Watch}; |
2 | | -use crate::{ |
3 | | - stats::{self, Unsent}, |
4 | | - ToProto, WatchRequest, |
5 | | -}; |
6 | | -use console_api as proto; |
7 | | -use proto::resources::resource; |
8 | | -use tokio::sync::{mpsc, Notify}; |
9 | | - |
10 | 1 | use std::{ |
11 | 2 | sync::{ |
12 | 3 | atomic::{AtomicBool, Ordering::*}, |
13 | 4 | Arc, |
14 | 5 | }, |
15 | 6 | time::{Duration, Instant}, |
16 | 7 | }; |
| 8 | + |
| 9 | +use console_api as proto; |
| 10 | +use prost::Message; |
| 11 | +use proto::resources::resource; |
| 12 | +use tokio::sync::{mpsc, Notify}; |
17 | 13 | use tracing_core::{span::Id, Metadata}; |
18 | 14 |
|
| 15 | +use super::{Command, Event, Shared, Watch}; |
| 16 | +use crate::{ |
| 17 | + stats::{self, Unsent}, |
| 18 | + ToProto, WatchRequest, |
| 19 | +}; |
| 20 | + |
19 | 21 | mod id_data; |
20 | 22 | mod shrink; |
21 | 23 | use self::id_data::{IdData, Include}; |
22 | 24 | use self::shrink::{ShrinkMap, ShrinkVec}; |
23 | 25 |
|
| 26 | +/// Should match tonic's (private) codec::DEFAULT_MAX_RECV_MESSAGE_SIZE |
| 27 | +const MAX_MESSAGE_SIZE: usize = 4 * 1024 * 1024; |
| 28 | + |
| 29 | +/// The smallest amount we will shrink retention to in an attempt to fit data in MAX_MESSAGE_SIZE |
| 30 | +const MIN_RETENTION: Duration = Duration::from_secs(1); |
| 31 | + |
24 | 32 | /// Aggregates instrumentation traces and prepares state for the instrument |
25 | 33 | /// server. |
26 | 34 | /// |
@@ -274,24 +282,48 @@ impl Aggregator { |
274 | 282 | /// Add the task subscription to the watchers after sending the first update |
275 | 283 | fn add_instrument_subscription(&mut self, subscription: Watch<proto::instrument::Update>) { |
276 | 284 | tracing::debug!("new instrument subscription"); |
277 | | - |
278 | | - let task_update = Some(self.task_update(Include::All)); |
279 | | - let resource_update = Some(self.resource_update(Include::All)); |
280 | | - let async_op_update = Some(self.async_op_update(Include::All)); |
281 | 285 | let now = Instant::now(); |
282 | 286 |
|
283 | | - let update = &proto::instrument::Update { |
284 | | - task_update, |
285 | | - resource_update, |
286 | | - async_op_update, |
287 | | - now: Some(self.base_time.to_timestamp(now)), |
288 | | - new_metadata: Some(proto::RegisterMetadata { |
289 | | - metadata: (*self.all_metadata).clone(), |
290 | | - }), |
| 287 | + let update = loop { |
| 288 | + let update = proto::instrument::Update { |
| 289 | + task_update: Some(self.task_update(Include::All)), |
| 290 | + resource_update: Some(self.resource_update(Include::All)), |
| 291 | + async_op_update: Some(self.async_op_update(Include::All)), |
| 292 | + now: Some(self.base_time.to_timestamp(now)), |
| 293 | + new_metadata: Some(proto::RegisterMetadata { |
| 294 | + metadata: (*self.all_metadata).clone(), |
| 295 | + }), |
| 296 | + }; |
| 297 | + let el = update.encoded_len(); |
| 298 | + if el < MAX_MESSAGE_SIZE { |
| 299 | + // normal case |
| 300 | + break Some(update); |
| 301 | + } |
| 302 | + // If the grpc message is bigger than tokio-console will accept throw away the oldest |
| 303 | + // inactive data and try again |
| 304 | + self.retention /= 2; |
| 305 | + self.cleanup_closed(); |
| 306 | + tracing::debug!( |
| 307 | + retention = ?self.retention, |
| 308 | + message_size = el, |
| 309 | + max_message_size = MAX_MESSAGE_SIZE, |
| 310 | + "Message too big, reduced retention", |
| 311 | + ); |
| 312 | + |
| 313 | + if self.retention <= MIN_RETENTION { |
| 314 | + self.retention = MIN_RETENTION; |
| 315 | + break None; |
| 316 | + } |
291 | 317 | }; |
| 318 | + if update.is_none() { |
| 319 | + tracing::error!(min_retention = ?MIN_RETENTION, "Message too big. Start with smaller retention."); |
| 320 | + // User will only get updates |
| 321 | + self.watchers.push(subscription); |
| 322 | + return; |
| 323 | + } |
292 | 324 |
|
293 | 325 | // Send the initial state --- if this fails, the subscription is already dead |
294 | | - if subscription.update(update) { |
| 326 | + if subscription.update(&update.unwrap()) { |
295 | 327 | self.watchers.push(subscription) |
296 | 328 | } |
297 | 329 | } |
|
0 commit comments