1use chrono::Utc;
2use parking_lot::RwLock;
3use std::collections::HashMap;
4use std::sync::Arc;
5
6use crate::data_store_interface::DataStoreTrait;
7use crate::evaluation::evaluator::SpecType;
8use crate::global_configs::GlobalConfigs;
9use crate::id_lists_adapter::{IdList, IdListsUpdateListener};
10use crate::interned_string::InternedString;
11use crate::networking::ResponseData;
12use crate::observability::observability_client_adapter::{MetricType, ObservabilityEvent};
13use crate::observability::ops_stats::{OpsStatsForInstance, OPS_STATS};
14use crate::observability::sdk_errors_observer::ErrorBoundaryEvent;
15use crate::sdk_event_emitter::{SdkEvent, SdkEventEmitter};
16use crate::specs_response::proto_specs::deserialize_protobuf;
17use crate::specs_response::spec_types::{SpecsResponseFull, SpecsResponseNoUpdates};
18use crate::utils::try_release_unused_heap_memory;
19use crate::{
20 log_d, log_e, log_error_to_statsig_and_console, log_w, read_lock_or_else, write_lock_or_else,
21 SpecsFormat, SpecsInfo, SpecsSource, SpecsUpdate, SpecsUpdateListener, StatsigErr,
22 StatsigOptions, StatsigRuntime,
23};
24
25pub struct SpecStoreData {
26 pub source: SpecsSource,
27 pub source_api: Option<String>,
28 pub time_received_at: Option<u64>,
29 pub values: SpecsResponseFull,
30 pub id_lists: HashMap<String, IdList>,
31}
32
33const TAG: &str = stringify!(SpecStore);
34
35pub struct SpecStore {
36 pub data: Arc<RwLock<SpecStoreData>>,
37
38 data_store_key: String,
39 data_store: Option<Arc<dyn DataStoreTrait>>,
40 statsig_runtime: Arc<StatsigRuntime>,
41 ops_stats: Arc<OpsStatsForInstance>,
42 global_configs: Arc<GlobalConfigs>,
43 event_emitter: Arc<SdkEventEmitter>,
44}
45
46impl SpecStore {
47 #[must_use]
48 pub fn new(
49 sdk_key: &str,
50 data_store_key: String,
51 statsig_runtime: Arc<StatsigRuntime>,
52 event_emitter: Arc<SdkEventEmitter>,
53 options: Option<&StatsigOptions>,
54 ) -> SpecStore {
55 let mut data_store = None;
56 if let Some(options) = options {
57 data_store = options.data_store.clone();
58 }
59
60 SpecStore {
61 data_store_key,
62 data: Arc::new(RwLock::new(SpecStoreData {
63 values: SpecsResponseFull::default(),
64 time_received_at: None,
65 source: SpecsSource::Uninitialized,
66 source_api: None,
67 id_lists: HashMap::new(),
68 })),
69 event_emitter,
70 data_store,
71 statsig_runtime,
72 ops_stats: OPS_STATS.get_for_instance(sdk_key),
73 global_configs: GlobalConfigs::get_instance(sdk_key),
74 }
75 }
76
77 pub fn set_source(&self, source: SpecsSource) {
78 let mut locked_data = write_lock_or_else!(self.data, {
79 log_e!(TAG, "Failed to acquire write lock: Failed to lock data");
80 return;
81 });
82
83 locked_data.source = source;
84 log_d!(TAG, "Source Changed ({:?})", locked_data.source);
85 }
86
87 pub fn get_current_values(&self) -> Option<SpecsResponseFull> {
88 let data = read_lock_or_else!(self.data, {
89 log_e!(TAG, "Failed to acquire read lock: Failed to lock data");
90 return None;
91 });
92
93 let json = serde_json::to_string(&data.values).ok()?;
94 serde_json::from_str::<SpecsResponseFull>(&json).ok()
95 }
96
97 pub fn get_fields_used_for_entity(
98 &self,
99 entity_name: &str,
100 entity_type: SpecType,
101 ) -> Vec<String> {
102 let data = read_lock_or_else!(self.data, {
103 log_error_to_statsig_and_console!(
104 &self.ops_stats,
105 TAG,
106 StatsigErr::LockFailure(
107 "Failed to acquire read lock for spec store data".to_string()
108 )
109 );
110 return vec![];
111 });
112
113 let entities = match entity_type {
114 SpecType::Gate => &data.values.feature_gates,
115 SpecType::DynamicConfig | SpecType::Experiment => &data.values.dynamic_configs,
116 SpecType::Layer => &data.values.layer_configs,
117 SpecType::ParameterStore => return vec![],
118 };
119
120 let entity_name = InternedString::from_str_ref(entity_name);
121 let entity = entities.get(&entity_name);
122
123 match entity {
124 Some(entity) => match &entity.as_spec_ref().fields_used {
125 Some(fields) => fields.iter().map(|f| f.unperformant_to_string()).collect(),
126 None => vec![],
127 },
128 None => vec![],
129 }
130 }
131
132 pub fn unperformant_keys_entity_filter(
133 &self,
134 top_level_key: &str,
135 entity_type: &str,
136 ) -> Vec<String> {
137 let data = read_lock_or_else!(self.data, {
138 log_error_to_statsig_and_console!(
139 &self.ops_stats,
140 TAG,
141 StatsigErr::LockFailure(
142 "Failed to acquire read lock for spec store data".to_string()
143 )
144 );
145 return vec![];
146 });
147
148 if top_level_key == "param_stores" {
149 match &data.values.param_stores {
150 Some(param_stores) => {
151 return param_stores
152 .keys()
153 .map(|k| k.unperformant_to_string())
154 .collect()
155 }
156 None => return vec![],
157 }
158 }
159
160 let values = match top_level_key {
161 "feature_gates" => &data.values.feature_gates,
162 "dynamic_configs" => &data.values.dynamic_configs,
163 "layer_configs" => &data.values.layer_configs,
164 _ => {
165 log_e!(TAG, "Invalid top level key: {}", top_level_key);
166 return vec![];
167 }
168 };
169
170 if entity_type == "*" {
171 return values.keys().map(|k| k.unperformant_to_string()).collect();
172 }
173
174 values
175 .iter()
176 .filter(|(_, v)| v.as_spec_ref().entity == entity_type)
177 .map(|(k, _)| k.unperformant_to_string())
178 .collect()
179 }
180
181 pub fn set_values(&self, mut specs_update: SpecsUpdate) -> Result<(), StatsigErr> {
182 let prep_result = self.specs_update_prep(&mut specs_update).map_err(|e| {
190 log_error_to_statsig_and_console!(self.ops_stats, TAG, e);
191 e
192 })?;
193
194 let (next_values, response_format) = match prep_result {
195 PrepResult::HasUpdates(next_values, response_format) => (next_values, response_format),
196 PrepResult::CurrentValuesNewer => return Ok(()),
197 PrepResult::NoUpdates => {
198 self.ops_stats_log_no_update(specs_update.source, specs_update.source_api);
199 return Ok(());
200 }
201 };
202
203 let apply_result = self
206 .specs_update_apply(next_values, &specs_update)
207 .map_err(|e| {
208 log_error_to_statsig_and_console!(self.ops_stats, TAG, e);
209 e
210 })?;
211
212 try_release_unused_heap_memory();
213
214 self.specs_update_notify(response_format, specs_update, apply_result)
217 .map_err(|e| {
218 log_error_to_statsig_and_console!(self.ops_stats, TAG, e);
219 e
220 })?;
221
222 Ok(())
223 }
224}
225
226enum PrepResult {
229 HasUpdates(Box<SpecsResponseFull>, SpecsFormat),
230 NoUpdates,
231 CurrentValuesNewer,
232}
233
234struct ApplyResult {
235 prev_source: SpecsSource,
236 prev_lcut: u64,
237 time_received_at: u64,
238}
239
240impl SpecStore {
241 fn specs_update_prep(&self, specs_update: &mut SpecsUpdate) -> Result<PrepResult, StatsigErr> {
242 let response_format = self.get_spec_response_format(specs_update);
243
244 let read_data = read_lock_or_else!(self.data, {
245 let msg = "Failed to acquire read lock for extract_response_from_update";
246 log_e!(TAG, "{}", msg);
247 return Err(StatsigErr::LockFailure(msg.to_string()));
248 });
249
250 let current_values = &read_data.values;
251
252 let first_deserialize_result =
254 self.deserialize_specs_data(current_values, &response_format, &mut specs_update.data);
255
256 let first_deserialize_error = match first_deserialize_result {
257 Ok(next_values) => {
258 if self.are_current_values_newer(&read_data, &next_values) {
259 return Ok(PrepResult::CurrentValuesNewer);
260 }
261
262 if next_values.has_updates {
263 return Ok(PrepResult::HasUpdates(
264 Box::new(next_values),
265 response_format,
266 ));
267 }
268
269 None
270 }
271 Err(e) => Some(e),
272 };
273
274 let second_deserialize_result = specs_update
276 .data
277 .deserialize_into::<SpecsResponseNoUpdates>();
278
279 let second_deserialize_error = match second_deserialize_result {
280 Ok(result) => {
281 if !result.has_updates {
282 return Ok(PrepResult::NoUpdates);
283 }
284
285 None
286 }
287 Err(e) => Some(e),
288 };
289
290 let error = first_deserialize_error
291 .or(second_deserialize_error)
292 .unwrap_or_else(|| {
293 StatsigErr::JsonParseError("SpecsResponse".to_string(), "Unknown error".to_string())
294 });
295
296 Err(error)
297 }
298
299 fn specs_update_apply(
300 &self,
301 next_values: Box<SpecsResponseFull>,
302 specs_update: &SpecsUpdate,
303 ) -> Result<ApplyResult, StatsigErr> {
304 self.try_update_global_configs(&next_values);
306
307 let mut data = write_lock_or_else!(self.data, {
308 let msg = "Failed to acquire write lock for swap_current_with_next";
309 log_e!(TAG, "{}", msg);
310 return Err(StatsigErr::LockFailure(msg.to_string()));
311 });
312
313 let prev_source = std::mem::replace(&mut data.source, specs_update.source.clone());
314 let prev_lcut = data.values.time;
315 let time_received_at = Utc::now().timestamp_millis() as u64;
316
317 data.values = *next_values;
318 data.time_received_at = Some(time_received_at);
319 data.source_api = specs_update.source_api.clone();
320
321 Ok(ApplyResult {
322 prev_source,
323 prev_lcut,
324 time_received_at,
325 })
326 }
327
328 fn specs_update_notify(
329 &self,
330 response_format: SpecsFormat,
331 specs_update: SpecsUpdate,
332 apply_result: ApplyResult,
333 ) -> Result<(), StatsigErr> {
334 let SpecsUpdate {
335 data,
336 source,
337 source_api,
338 ..
339 } = specs_update;
340
341 let current_lcut = {
342 let read_lock = read_lock_or_else!(self.data, {
343 let msg = "Failed to acquire read lock for set_values";
344 log_e!(TAG, "{}", msg);
345 return Err(StatsigErr::LockFailure(msg.to_string()));
346 });
347
348 self.emit_specs_updated_sdk_event(
349 &read_lock.source,
350 &read_lock.source_api,
351 &read_lock.values,
352 );
353
354 read_lock.values.time
355 };
356
357 self.try_update_data_store(&source, data, apply_result.time_received_at);
359
360 self.ops_stats_log_config_propagation_diff(
361 current_lcut,
362 apply_result.prev_lcut,
363 &source,
364 &apply_result.prev_source,
365 source_api,
366 response_format,
367 );
368
369 Ok(())
370 }
371
372 fn deserialize_specs_data(
373 &self,
374 current_values: &SpecsResponseFull,
375 response_format: &SpecsFormat,
376 response_data: &mut ResponseData,
377 ) -> Result<SpecsResponseFull, StatsigErr> {
378 let mut next_values = SpecsResponseFull::default();
379
380 let parse_result = match response_format {
381 SpecsFormat::Protobuf => deserialize_protobuf(
382 &self.ops_stats,
383 current_values,
384 &mut next_values,
385 response_data,
386 ),
387 SpecsFormat::Json => response_data.deserialize_in_place(&mut next_values),
388 };
389
390 match parse_result {
391 Ok(()) => Ok(next_values),
392 Err(e) => Err(e),
393 }
394 }
395
396 fn emit_specs_updated_sdk_event(
397 &self,
398 source: &SpecsSource,
399 source_api: &Option<String>,
400 values: &SpecsResponseFull,
401 ) {
402 self.event_emitter.emit(SdkEvent::SpecsUpdated {
403 source,
404 source_api,
405 values,
406 });
407 }
408
409 fn get_spec_response_format(&self, update: &SpecsUpdate) -> SpecsFormat {
410 let content_type = update.data.get_header_ref("content-type");
411 if content_type.map(|s| s.as_str().contains("application/octet-stream")) != Some(true) {
412 return SpecsFormat::Json;
413 }
414
415 let content_encoding = update.data.get_header_ref("content-encoding");
416 if content_encoding.map(|s| s.as_str().contains("statsig-br")) != Some(true) {
417 return SpecsFormat::Json;
418 }
419
420 SpecsFormat::Protobuf
421 }
422
423 fn try_update_global_configs(&self, dcs: &SpecsResponseFull) {
424 if let Some(diagnostics) = &dcs.diagnostics {
425 self.global_configs
426 .set_diagnostics_sampling_rates(diagnostics.clone());
427 }
428
429 if let Some(sdk_configs) = &dcs.sdk_configs {
430 self.global_configs.set_sdk_configs(sdk_configs.clone());
431 }
432
433 if let Some(sdk_flags) = &dcs.sdk_flags {
434 self.global_configs.set_sdk_flags(sdk_flags.clone());
435 }
436 }
437
438 fn try_update_data_store(&self, source: &SpecsSource, mut data: ResponseData, now: u64) {
439 if source != &SpecsSource::Network {
440 return;
441 }
442
443 if data.get_header_ref("x-deltas-used").is_some() {
444 log_d!(
445 TAG,
446 "Skipping data store write for delta response identified by x-deltas-used header"
447 );
448 return;
449 }
450
451 let data_store = match &self.data_store {
452 Some(data_store) => data_store.clone(),
453 None => return,
454 };
455
456 let data_store_key = self.data_store_key.clone();
457 let supports_bytes = data_store.supports_bytes();
458
459 let spawn_result = self.statsig_runtime.spawn(
460 "spec_store_update_data_store",
461 move |_shutdown_notif| async move {
462 if supports_bytes {
463 let data_bytes = match data.read_to_bytes() {
464 Ok(bytes) => bytes,
465 Err(e) => {
466 log_e!(TAG, "Failed to read data as bytes: {}", e);
467 return;
468 }
469 };
470
471 let _ = data_store
472 .set_bytes(&data_store_key, &data_bytes, Some(now))
473 .await;
474 return;
475 }
476
477 let data_string = match data.read_to_string() {
478 Ok(s) => s,
479 Err(e) => {
480 log_w!(
481 TAG,
482 "Skipping data store write because payload is not valid UTF-8 and data store does not support bytes: {}",
483 e
484 );
485 return;
486 }
487 };
488
489 let _ = data_store
490 .set(&data_store_key, &data_string, Some(now))
491 .await;
492 },
493 );
494
495 if let Err(e) = spawn_result {
496 log_e!(
497 TAG,
498 "Failed to spawn spec store update data store task: {e}"
499 );
500 }
501 }
502
503 fn are_current_values_newer(
504 &self,
505 data: &SpecStoreData,
506 next_values: &SpecsResponseFull,
507 ) -> bool {
508 let curr_values = &data.values;
509 let curr_checksum = curr_values.checksum.as_deref().unwrap_or_default();
510 let new_checksum = next_values.checksum.as_deref().unwrap_or_default();
511
512 let cached_time_is_newer = curr_values.time > 0 && curr_values.time > next_values.time;
513 let checksums_match = !curr_checksum.is_empty() && curr_checksum == new_checksum;
514
515 if cached_time_is_newer || checksums_match {
516 log_d!(
517 TAG,
518 "Received values for [time: {}, checksum: {}], but currently has values for [time: {}, checksum: {}]. Ignoring values.",
519 next_values.time,
520 new_checksum,
521 curr_values.time,
522 curr_checksum,
523 );
524 return true;
525 }
526
527 false
528 }
529}
530
531impl SpecStore {
534 fn ops_stats_log_no_update(&self, source: SpecsSource, source_api: Option<String>) {
535 log_d!(TAG, "No Updates");
536 self.ops_stats.log(ObservabilityEvent::new_event(
537 MetricType::Increment,
538 "config_no_update".to_string(),
539 1.0,
540 Some(HashMap::from([
541 ("source".to_string(), source.to_string()),
542 ("source_api".to_string(), source_api.unwrap_or_default()),
543 ])),
544 ));
545 }
546
547 #[allow(clippy::too_many_arguments)]
548 fn ops_stats_log_config_propagation_diff(
549 &self,
550 lcut: u64,
551 prev_lcut: u64,
552 source: &SpecsSource,
553 prev_source: &SpecsSource,
554 source_api: Option<String>,
555 response_format: SpecsFormat,
556 ) {
557 let delay = (Utc::now().timestamp_millis() as u64).saturating_sub(lcut);
558 log_d!(TAG, "Updated ({:?})", source);
559
560 if *prev_source == SpecsSource::Uninitialized || *prev_source == SpecsSource::Loading {
561 return;
562 }
563
564 self.ops_stats.log(ObservabilityEvent::new_event(
565 MetricType::Dist,
566 "config_propagation_diff".to_string(),
567 delay as f64,
568 Some(HashMap::from([
569 ("source".to_string(), source.to_string()),
570 ("lcut".to_string(), lcut.to_string()),
571 ("prev_lcut".to_string(), prev_lcut.to_string()),
572 ("source_api".to_string(), source_api.unwrap_or_default()),
573 (
574 "response_format".to_string(),
575 Into::<&str>::into(&response_format).to_string(),
576 ),
577 ])),
578 ));
579 }
580}
581
582impl SpecsUpdateListener for SpecStore {
585 fn did_receive_specs_update(&self, update: SpecsUpdate) -> Result<(), StatsigErr> {
586 self.set_values(update)
587 }
588
589 fn get_current_specs_info(&self) -> SpecsInfo {
590 let data = read_lock_or_else!(self.data, {
591 log_e!(
592 TAG,
593 "Failed to acquire read lock for get_current_specs_info"
594 );
595 return SpecsInfo {
596 lcut: None,
597 checksum: None,
598 source: SpecsSource::Error,
599 source_api: None,
600 };
601 });
602
603 SpecsInfo {
604 lcut: Some(data.values.time),
605 checksum: data.values.checksum.clone(),
606 source: data.source.clone(),
607 source_api: data.source_api.clone(),
608 }
609 }
610}
611
612impl IdListsUpdateListener for SpecStore {
615 fn get_current_id_list_metadata(
616 &self,
617 ) -> HashMap<String, crate::id_lists_adapter::IdListMetadata> {
618 let data = read_lock_or_else!(self.data, {
619 let err = StatsigErr::LockFailure(
620 "Failed to acquire read lock for id list metadata".to_string(),
621 );
622 log_error_to_statsig_and_console!(self.ops_stats, TAG, err);
623 return HashMap::new();
624 });
625
626 data.id_lists
627 .iter()
628 .map(|(key, list)| (key.clone(), list.metadata.clone()))
629 .collect()
630 }
631
632 fn did_receive_id_list_updates(
633 &self,
634 updates: HashMap<String, crate::id_lists_adapter::IdListUpdate>,
635 ) {
636 let mut data = write_lock_or_else!(self.data, {
637 let err = StatsigErr::LockFailure(
638 "Failed to acquire write lock for did_receive_id_list_updates".to_string(),
639 );
640 log_error_to_statsig_and_console!(self.ops_stats, TAG, err);
641
642 return;
643 });
644
645 data.id_lists.retain(|name, _| updates.contains_key(name));
647
648 for (list_name, update) in updates {
649 if let Some(entry) = data.id_lists.get_mut(&list_name) {
650 entry.apply_update(update);
652 } else {
653 let mut list = IdList::new(update.new_metadata.clone());
655 list.apply_update(update);
656 data.id_lists.insert(list_name, list);
657 }
658 }
659 }
660}