Skip to content

Commit 09474fc

Browse files
authored
Avoid retrieving all the roots all the time in remote config (#1069)
Signed-off-by: Bob Weinand <[email protected]>
1 parent b361ec9 commit 09474fc

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

datadog-remote-config/src/fetch/fetcher.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::targets::TargetsList;
4+
use crate::targets::{Root, TargetsList};
55
use crate::{
66
RemoteConfigCapabilities, RemoteConfigPath, RemoteConfigPathRef, RemoteConfigPathType,
77
RemoteConfigProduct, Target,
@@ -189,16 +189,29 @@ pub struct ConfigFetcher<S: FileStorage> {
189189
state: Arc<ConfigFetcherState<S::StoredFile>>,
190190
}
191191

192-
#[derive(Default)]
193192
pub struct ConfigClientState {
194193
opaque_backend_state: Vec<u8>,
195194
last_configs: Vec<String>,
196195
// 'static because it actually depends on last_configs, and rust doesn't like self-referencing
197196
last_config_paths: HashSet<RemoteConfigPathRef<'static>>,
198197
targets_version: u64,
198+
root_version: u64,
199199
last_error: Option<String>,
200200
}
201201

202+
impl Default for ConfigClientState {
203+
fn default() -> Self {
204+
ConfigClientState {
205+
opaque_backend_state: vec![],
206+
last_configs: vec![],
207+
last_config_paths: Default::default(),
208+
targets_version: 0,
209+
root_version: 1,
210+
last_error: None,
211+
}
212+
}
213+
}
214+
202215
impl<S: FileStorage> ConfigFetcher<S> {
203216
pub fn new(file_storage: S, state: Arc<ConfigFetcherState<S::StoredFile>>) -> Self {
204217
ConfigFetcher {
@@ -265,7 +278,7 @@ impl<S: FileStorage> ConfigFetcher<S> {
265278
let config_req = ClientGetConfigsRequest {
266279
client: Some(datadog_trace_protobuf::remoteconfig::Client {
267280
state: Some(ClientState {
268-
root_version: 1,
281+
root_version: opaque_state.root_version,
269282
targets_version: opaque_state.targets_version,
270283
config_states,
271284
has_error: opaque_state.last_error.is_some(),
@@ -351,6 +364,21 @@ impl<S: FileStorage> ConfigFetcher<S> {
351364
))
352365
})?;
353366

367+
opaque_state.root_version = response.roots.iter().try_fold(
368+
opaque_state.root_version,
369+
|max, cur| -> anyhow::Result<_> {
370+
let decoded_root =
371+
base64::engine::general_purpose::STANDARD.decode(cur.as_slice())?;
372+
let root = Root::try_parse(decoded_root.as_slice()).map_err(|e| {
373+
anyhow::Error::msg(e).context(format!(
374+
"Decoded roots reply: {}",
375+
String::from_utf8_lossy(decoded_root.as_slice())
376+
))
377+
})?;
378+
Ok(std::cmp::max(max, root.signed.version))
379+
},
380+
)?;
381+
354382
opaque_state.opaque_backend_state = targets_list
355383
.signed
356384
.custom

datadog-remote-config/src/targets.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,19 @@ impl TargetData<'_> {
6363
.and_then(|v| u64::from_str(v.get()).ok())
6464
}
6565
}
66+
67+
#[derive(Deserialize)]
68+
pub struct RootSigned {
69+
pub version: u64,
70+
}
71+
72+
#[derive(Deserialize)]
73+
pub struct Root {
74+
pub signed: RootSigned,
75+
}
76+
77+
impl Root {
78+
pub fn try_parse(data: &[u8]) -> serde_json::error::Result<Self> {
79+
serde_json::from_slice(data)
80+
}
81+
}

0 commit comments

Comments
 (0)