sift_stream 0.10.0

A robust Sift telemetry streaming library
Documentation
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
use super::IngestionConfigForm;
use crate::stream::flow::{add_new_flows, validate_flows};
use sift_connect::SiftChannel;
use sift_error::prelude::*;
use sift_rs::{
    assets::v1::Asset,
    ingestion_configs::v2::{FlowConfig, IngestionConfig},
    retry::{RetryConfig, RetryExt},
    wrappers::{
        assets::{AssetServiceWrapper, new_asset_service},
        ingestion_configs::{IngestionConfigServiceWrapper, new_ingestion_config_service},
    },
};

/// Loads or creates an ingestion config and returns the config, flows, and asset.
/// This is shared logic used by both IngestionConfigMode and FileBackupMode builders.
pub async fn load_ingestion_config(
    grpc_channel: SiftChannel,
    ingestion_config: IngestionConfigForm,
) -> Result<(IngestionConfig, Vec<FlowConfig>, Asset)> {
    #[cfg(feature = "tracing")]
    tracing::info_span!("load_ingestion_config");

    let IngestionConfigForm {
        asset_name,
        client_key,
        flows,
    } = ingestion_config;

    let ingestion_config_service_wrapper = new_ingestion_config_service(grpc_channel.clone());
    let retrying_ingestion_config =
        ingestion_config_service_wrapper.retrying(RetryConfig::default());

    let ingestion_config_create_res = {
        let asset_name = asset_name.clone();
        let client_key = client_key.clone();
        let flows = flows.clone();
        retrying_ingestion_config
            .call(|mut w| {
                let asset_name = asset_name.clone();
                let client_key = client_key.clone();
                let flows = flows.clone();
                async move {
                    w.try_create_ingestion_config(&asset_name, &client_key, &flows)
                        .await
                }
            })
            .await
    };

    let (ingestion_config, new_config) = match ingestion_config_create_res {
        Ok(ingestion_config) => (ingestion_config, true),
        Err(e) if e.kind() == ErrorKind::AlreadyExistsError => {
            let client_key = client_key.clone();
            let ingestion_config = retrying_ingestion_config
                .call(|mut w| {
                    let client_key = client_key.clone();
                    async move { w.try_get_ingestion_config_by_client_key(&client_key).await }
                })
                .await?;

            #[cfg(feature = "tracing")]
            tracing::info!(
                ingestion_config_id = ingestion_config.ingestion_config_id,
                "an existing ingestion config was found with the provided client-key"
            );
            (ingestion_config, false)
        }
        Err(e) => return Err(e),
    };

    // Get the asset corresponding to the ingestion config.
    let asset_service_wrapper = new_asset_service(grpc_channel.clone());
    let retrying_asset = asset_service_wrapper.retrying(RetryConfig::default());
    let asset = {
        let asset_id = ingestion_config.asset_id.clone();
        retrying_asset
            .call(|mut w| {
                let asset_id = asset_id.clone();
                async move { w.try_get_asset_by_id(&asset_id).await }
            })
            .await
            .context("failed to retrieve asset specified by ingestion config")?
    };

    // Sanity check the Sift asset matches the expected asset-name.
    if asset.name != asset_name {
        return Err(Error::new_msg(
            ErrorKind::IncompatibleIngestionConfigChange,
            format!(
                "local ingestion config references asset '{asset_name}' but this existing config in Sift refers to asset '{}'",
                asset.name
            ),
        ));
    }

    // If the ingestion config was created new, the create call provided all the known ingestion
    // config flows, so no further API calls are required.
    if new_config {
        #[cfg(feature = "tracing")]
        {
            let flow_names = flows
                .iter()
                .map(|f| f.name.as_str())
                .collect::<Vec<&str>>()
                .join(",");
            tracing::info!(
                ingestion_config_id = ingestion_config.ingestion_config_id,
                flow_names = flow_names,
                "created new ingestion config"
            );
        }
        return Ok((ingestion_config, flows, asset));
    }

    // Fetch existing flow configs that match the names of the provided flow configs
    // so we can validate what we are trying to use vs. what Sift expects.
    let flow_names = flows
        .iter()
        .map(|f| format!("'{}'", f.name))
        .collect::<Vec<String>>()
        .join(",");

    let filter = flow_names
        .is_empty()
        .then(String::new)
        .unwrap_or_else(|| format!("flow_name in [{flow_names}]"));

    let existing_flows = {
        let ingestion_config_id = ingestion_config.ingestion_config_id.clone();
        let filter = filter.clone();
        retrying_ingestion_config
            .call(|mut w| {
                let ingestion_config_id = ingestion_config_id.clone();
                let filter = filter.clone();
                async move { w.try_filter_flows(&ingestion_config_id, &filter).await }
            })
            .await?
    };

    // If no flows were provided, use the existing flows in Sift to populate the local flow cache.
    if flows.is_empty() {
        #[cfg(feature = "tracing")]
        tracing::info!(
            ingestion_config_id = ingestion_config.ingestion_config_id,
            "no flows provided, using existing flows in Sift to populate the local flow cache"
        );
        return Ok((ingestion_config, existing_flows, asset));
    }

    // Find the local flows that Sift does not yet know about so they can be created.
    let mut flows_to_create: Vec<FlowConfig> = Vec::new();
    for flow in &flows {
        let mut flow_exists = false;

        for existing_flow in existing_flows.iter().filter(|ef| ef == &flow) {
            flow_exists = flow
                .channels
                .iter()
                .zip(existing_flow.channels.iter())
                .all(|(lhs, rhs)| lhs == rhs);

            if flow_exists {
                break;
            }
        }

        // If the flow isn't known to Sift yet, it will need to be created.
        if !flow_exists {
            flows_to_create.push(flow.clone());
        }
    }

    if !flows_to_create.is_empty() {
        // Capture names before moving flows_to_create into add_new_flows.
        #[cfg(feature = "tracing")]
        let flow_names: Vec<String> = flows_to_create.iter().map(|f| f.name.clone()).collect();

        let results = add_new_flows(
            grpc_channel.clone(),
            &ingestion_config.ingestion_config_id,
            flows_to_create,
        )
        .await;

        let mut flow_create_error = false;
        for (idx, result) in results.into_iter().enumerate() {
            match result {
                Ok(Ok(())) => {}
                // A concurrent SiftStream instance may have won the race to create this flow.
                Ok(Err(e)) if e.kind() == ErrorKind::AlreadyExistsError => {}
                Ok(Err(e)) => {
                    #[cfg(feature = "tracing")]
                    tracing::error!("failed to create flow {}: {e}", flow_names[idx]);
                    flow_create_error = true;
                }
                Err(e) => {
                    #[cfg(feature = "tracing")]
                    tracing::error!("failed to create flow {}: {e}", flow_names[idx]);
                    flow_create_error = true;
                }
            }
        }

        if flow_create_error {
            return Err(Error::new_msg(
                ErrorKind::CreateFlowError,
                "Ingestion config flow creation failed. See logs for details.",
            ));
        }

        #[cfg(feature = "tracing")]
        tracing::info!(
            ingestion_config_id = ingestion_config.ingestion_config_id,
            new_flows = flow_names.join(","),
            "created new flows for ingestion config"
        );

        // Fetch all flows Sift knows about with the specified names for validation.
        let sift_flows = {
            let ingestion_config_id = ingestion_config.ingestion_config_id.clone();
            let filter = filter.clone();
            retrying_ingestion_config
                .call(|mut w| {
                    let ingestion_config_id = ingestion_config_id.clone();
                    let filter = filter.clone();
                    async move { w.try_filter_flows(&ingestion_config_id, &filter).await }
                })
                .await?
        };

        validate_flows(&flows, &sift_flows)?;

        Ok((ingestion_config, sift_flows, asset))
    } else {
        Ok((ingestion_config, existing_flows, asset))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::{
        MockIngestionConfigService, create_mock_grpc_channel_with_ingestion_service,
        create_mock_grpc_channel_with_service,
    };
    use sift_rs::common::r#type::v1::ChannelDataType;
    use sift_rs::ingestion_configs::v2::ChannelConfig;
    use uuid::Uuid;

    fn create_test_flow_config(name: &str) -> FlowConfig {
        FlowConfig {
            name: name.to_string(),
            channels: vec![
                ChannelConfig {
                    name: "channel1".to_string(),
                    data_type: ChannelDataType::Double.into(),
                    ..Default::default()
                },
                ChannelConfig {
                    name: "channel2".to_string(),
                    data_type: ChannelDataType::Int32.into(),
                    ..Default::default()
                },
            ],
        }
    }

    fn create_test_ingestion_config_form(
        asset_name: &str,
        client_key: &str,
        flows: Vec<FlowConfig>,
    ) -> IngestionConfigForm {
        IngestionConfigForm {
            asset_name: asset_name.to_string(),
            client_key: client_key.to_string(),
            flows,
        }
    }

    #[tokio::test]
    async fn test_load_ingestion_config_creates_new_config() {
        let (grpc_channel, _) = create_mock_grpc_channel_with_service().await;
        let asset_name = "test_asset";
        let client_key = format!("new_client_key_{}", Uuid::new_v4());
        let flows = vec![
            create_test_flow_config("test_flow_1"),
            create_test_flow_config("test_flow_2"),
        ];

        let form = create_test_ingestion_config_form(asset_name, &client_key, flows.clone());

        let (ingestion_config, returned_flows, asset) =
            load_ingestion_config(grpc_channel, form).await.unwrap();

        assert_eq!(ingestion_config.client_key, client_key);
        assert_eq!(asset.name, asset_name);
        assert_eq!(returned_flows.len(), flows.len());
    }

    #[tokio::test]
    async fn test_load_ingestion_config_creates_new_config_with_empty_flows() {
        let (grpc_channel, _) = create_mock_grpc_channel_with_service().await;
        let asset_name = "test_asset";
        let client_key = format!("new_client_key_{}", Uuid::new_v4());

        let form = create_test_ingestion_config_form(asset_name, &client_key, vec![]);

        let (ingestion_config, returned_flows, asset) =
            load_ingestion_config(grpc_channel, form).await.unwrap();

        assert_eq!(ingestion_config.client_key, client_key);
        assert_eq!(asset.name, asset_name);
        assert_eq!(returned_flows.len(), 0);
    }

    #[tokio::test]
    async fn test_load_ingestion_config_with_existing_config_and_empty_flows() {
        let (grpc_channel, _) = create_mock_grpc_channel_with_service().await;
        let client_key = "already_exists_client_key";
        let asset_name = "already_exists_asset";

        let form = create_test_ingestion_config_form(asset_name, client_key, vec![]);

        let (ingestion_config, returned_flows, _) =
            load_ingestion_config(grpc_channel, form).await.unwrap();

        assert_eq!(ingestion_config.client_key, client_key);
        // Should use existing flows from Sift when no flows provided
        assert!(!returned_flows.is_empty());
    }

    #[tokio::test]
    async fn test_load_ingestion_config_with_existing_config_and_new_flows() {
        let (grpc_channel, _) = create_mock_grpc_channel_with_service().await;
        let client_key = "already_exists_client_key";
        let asset_name = "already_exists_asset";
        let flows = vec![
            create_test_flow_config("new_flow_1"),
            create_test_flow_config("new_flow_2"),
        ];

        let form = create_test_ingestion_config_form(asset_name, client_key, flows.clone());

        let (ingestion_config, returned_flows, _) =
            load_ingestion_config(grpc_channel, form).await.unwrap();

        assert_eq!(ingestion_config.client_key, client_key);
        // Should return flows (either existing or newly created)
        assert!(!returned_flows.is_empty());
    }

    #[tokio::test]
    async fn test_load_ingestion_config_asset_name_mismatch() {
        let (grpc_channel, _) = create_mock_grpc_channel_with_service().await;
        let client_key = "already_exists_client_key";
        // Use a different asset name than what's in the mock service
        let asset_name = "different_asset_name";
        let flows = vec![];

        let form = create_test_ingestion_config_form(asset_name, client_key, flows);

        let result = load_ingestion_config(grpc_channel, form).await;

        // Should fail with IncompatibleIngestionConfigChange error
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.kind(), ErrorKind::IncompatibleIngestionConfigChange);
    }

    #[tokio::test]
    async fn test_load_ingestion_config_handles_existing_flows() {
        let (grpc_channel, _) = create_mock_grpc_channel_with_service().await;
        let client_key = "already_exists_client_key";
        let asset_name = "already_exists_asset";

        // Use the same flow name as the default mock service
        let existing_flows = vec![FlowConfig {
            name: "already_exists_flow".to_string(),
            channels: vec![ChannelConfig {
                name: "channel1".to_string(),
                data_type: ChannelDataType::Double.into(),
                ..Default::default()
            }],
        }];
        let form = create_test_ingestion_config_form(asset_name, client_key, existing_flows);

        let (ingestion_config, returned_flows, _) =
            load_ingestion_config(grpc_channel, form).await.unwrap();

        assert_eq!(ingestion_config.client_key, client_key);
        // Should return existing flows
        assert!(!returned_flows.is_empty());
    }

    // Covers the Err(e) => return Err(e) branch at the ingestion-config create match.
    // An empty asset name triggers ArgumentValidationError from try_create_ingestion_config,
    // which is not AlreadyExistsError and must propagate rather than be swallowed.
    #[tokio::test]
    async fn test_load_ingestion_config_propagates_non_already_exists_create_error() {
        let (grpc_channel, _) = create_mock_grpc_channel_with_service().await;
        let form = create_test_ingestion_config_form("", "some_client_key", vec![]);

        let result = load_ingestion_config(grpc_channel, form).await;

        assert!(result.is_err());
        assert_ne!(result.unwrap_err().kind(), ErrorKind::AlreadyExistsError);
    }

    // Covers the flow_create_error path where add_new_flows returns a non-AlreadyExists
    // error, which should surface as CreateFlowError.
    #[tokio::test]
    async fn test_load_ingestion_config_flow_creation_failure_returns_error() {
        let service = MockIngestionConfigService::with_flow_create_error(
            sift_error::Error::new_msg(sift_error::ErrorKind::IoError, "io error"),
        );
        let (grpc_channel, _) = create_mock_grpc_channel_with_ingestion_service(service).await;

        // brand_new_flow is not in the mock's existing flows, so it is queued for creation.
        let flows = vec![FlowConfig {
            name: "brand_new_flow".to_string(),
            channels: vec![ChannelConfig {
                name: "ch".to_string(),
                data_type: ChannelDataType::Double.into(),
                ..Default::default()
            }],
        }];
        let form = create_test_ingestion_config_form(
            "already_exists_asset",
            "already_exists_client_key",
            flows,
        );

        let result = load_ingestion_config(grpc_channel, form).await;

        assert!(result.is_err());
        assert_eq!(result.unwrap_err().kind(), ErrorKind::CreateFlowError);
    }

    // Covers the AlreadyExistsError arm inside add_new_flows processing (the race
    // condition where a concurrent SiftStream instance created the flow first).
    // The function must succeed and return the flows created by the winning instance.
    #[tokio::test]
    async fn test_load_ingestion_config_race_condition_on_flow_creation_succeeds() {
        let race_flow = FlowConfig {
            name: "race_flow".to_string(),
            channels: vec![ChannelConfig {
                name: "ch".to_string(),
                data_type: ChannelDataType::Double.into(),
                ..Default::default()
            }],
        };
        let service = MockIngestionConfigService::with_race_condition(vec![race_flow.clone()]);
        let (grpc_channel, _) = create_mock_grpc_channel_with_ingestion_service(service).await;

        let form = create_test_ingestion_config_form(
            "already_exists_asset",
            "already_exists_client_key",
            vec![race_flow],
        );

        let (ingestion_config, returned_flows, asset) =
            load_ingestion_config(grpc_channel, form).await.unwrap();

        assert_eq!(ingestion_config.client_key, "already_exists_client_key");
        assert_eq!(asset.name, "already_exists_asset");
        assert_eq!(returned_flows.len(), 1);
        assert_eq!(returned_flows[0].name, "race_flow");
    }
}