arrow-zerobus-sdk-wrapper 0.8.1

Cross-platform Rust SDK wrapper for Databricks Zerobus with Python bindings
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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Integration test for debug file output
//!
//! Verifies that Arrow and Protobuf debug files are written correctly when enabled.

use arrow_zerobus_sdk_wrapper::{WrapperConfiguration, ZerobusWrapper};
use arrow::array::{Int64Array, StringArray};
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use std::sync::Arc;
use std::path::PathBuf;
use tempfile::TempDir;
use tokio::time::sleep;
use std::time::Duration;

#[tokio::test]
#[ignore] // Requires real SDK, but tests the debug file writing logic
async fn test_debug_files_written() {
    // Create temporary directory for debug output
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_credentials("client_id".to_string(), "client_secret".to_string())
    .with_unity_catalog("https://unity-catalog-url".to_string())
    .with_debug_output(debug_output_dir.clone())
    .with_debug_flush_interval_secs(1); // Short interval for testing

    // Initialize wrapper with debug enabled
    let wrapper_result = ZerobusWrapper::new(config).await;
    
    // May fail without real SDK, but tests the debug initialization
    if let Ok(wrapper) = wrapper_result {
        // Create test batch
        let schema = Schema::new(vec![
            Field::new("id", DataType::Int64, false),
            Field::new("name", DataType::Utf8, false),
        ]);
        let id_array = Int64Array::from(vec![1, 2, 3]);
        let name_array = StringArray::from(vec!["Alice", "Bob", "Charlie"]);
        let batch = RecordBatch::try_new(
            Arc::new(schema),
            vec![Arc::new(id_array), Arc::new(name_array)],
        )
        .unwrap();

        // Send batch (this should write debug files)
        let _result = wrapper.send_batch(batch).await;
        
        // Flush debug files
        wrapper.flush().await.unwrap();
        
        // Wait a bit for file writes to complete
        sleep(Duration::from_millis(500)).await;
        
        // Verify Arrow file was created
        let arrow_file = debug_output_dir.join("zerobus/arrow/table.arrow");
        if arrow_file.exists() {
            let metadata = std::fs::metadata(&arrow_file).unwrap();
            assert!(metadata.len() > 0, "Arrow file should not be empty");
        }
        
        // Verify Protobuf file was created
        let proto_file = debug_output_dir.join("zerobus/proto/table.proto");
        if proto_file.exists() {
            let metadata = std::fs::metadata(&proto_file).unwrap();
            assert!(metadata.len() > 0, "Protobuf file should not be empty");
        }
    }
}

#[tokio::test]
async fn test_debug_files_disabled() {
    // Test that debug files are not created when disabled
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_credentials("client_id".to_string(), "client_secret".to_string())
    .with_unity_catalog("https://unity-catalog-url".to_string());
    // Debug not enabled

    // Initialize wrapper without debug
    let wrapper_result = ZerobusWrapper::new(config).await;
    
    // Should work without debug (may fail without real SDK, but tests the flow)
    if wrapper_result.is_ok() {
        // Debug directories should not be created
        let arrow_dir = debug_output_dir.join("zerobus/arrow");
        let proto_dir = debug_output_dir.join("zerobus/proto");
        assert!(!arrow_dir.exists());
        assert!(!proto_dir.exists());
    }
}

#[tokio::test]
#[ignore] // Requires real SDK
async fn test_debug_file_rotation() {
    // Test that files are rotated when max size is reached
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_credentials("client_id".to_string(), "client_secret".to_string())
    .with_unity_catalog("https://unity-catalog-url".to_string())
    .with_debug_output(debug_output_dir.clone())
    .with_debug_max_file_size(Some(1024)); // Small max size for testing

    // Initialize wrapper with debug and rotation
    let wrapper_result = ZerobusWrapper::new(config).await;
    
    if let Ok(wrapper) = wrapper_result {
        // Create large batch to trigger rotation
        let schema = Schema::new(vec![
            Field::new("data", DataType::Utf8, false),
        ]);
        
        // Create a batch with enough data to exceed max file size
        let large_data: Vec<String> = (0..1000)
            .map(|i| format!("data_{}", i))
            .collect();
        let data_array = StringArray::from(large_data);
        let batch = RecordBatch::try_new(
            Arc::new(schema),
            vec![Arc::new(data_array)],
        )
        .unwrap();

        // Send multiple batches to trigger rotation
        for _ in 0..10 {
            let _result = wrapper.send_batch(batch.clone()).await;
        }
        
        // Flush and check for rotated files
        wrapper.flush().await.unwrap();
        sleep(Duration::from_millis(500)).await;
        
        // Check if rotated files exist (with timestamp suffix)
        let arrow_dir = debug_output_dir.join("zerobus/arrow");
        if arrow_dir.exists() {
            let files: Vec<_> = std::fs::read_dir(&arrow_dir)
                .unwrap()
                .filter_map(|e| e.ok())
                .collect();
            // Should have at least the main file, possibly rotated files
            assert!(!files.is_empty());
        }
    }
}

#[tokio::test]
async fn test_debug_files_written_when_writer_disabled() {
    // Test that debug files are written even when writer is disabled
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_debug_output(debug_output_dir.clone())
    .with_zerobus_writer_disabled(true);
    // Note: No credentials required when writer is disabled

    // Initialize wrapper with writer disabled mode
    let wrapper_result = ZerobusWrapper::new(config).await;
    
    // Should succeed without credentials
    assert!(wrapper_result.is_ok(), "Wrapper should initialize without credentials when writer disabled");
    
    let wrapper = wrapper_result.unwrap();
    
    // Create test batch
    let schema = Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, false),
    ]);
    let id_array = Int64Array::from(vec![1, 2, 3]);
    let name_array = StringArray::from(vec!["Alice", "Bob", "Charlie"]);
    let batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(id_array), Arc::new(name_array)],
    )
    .unwrap();

    // Send batch - should write debug files but skip SDK calls
    let result = wrapper.send_batch(batch).await;
    
    // Should succeed (conversion succeeded, no network calls made)
    assert!(result.is_ok(), "send_batch should succeed when writer disabled");
    let transmission_result = result.unwrap();
    assert!(transmission_result.success, "Transmission result should indicate success");
    
    // Flush debug files
    wrapper.flush().await.unwrap();
    
    // Wait a bit for file writes to complete
    sleep(Duration::from_millis(500)).await;
    
    // Verify Arrow file was created
    let sanitized_table_name = "test_table".replace(['.', '/'], "_");
    let arrow_file = debug_output_dir.join(format!("zerobus/arrow/{}.arrow", sanitized_table_name));
    assert!(arrow_file.exists(), "Arrow file should be created when writer disabled");
    let metadata = std::fs::metadata(&arrow_file).unwrap();
    assert!(metadata.len() > 0, "Arrow file should not be empty");
    
    // Verify Protobuf file was created
    let proto_file = debug_output_dir.join(format!("zerobus/proto/{}.proto", sanitized_table_name));
    assert!(proto_file.exists(), "Protobuf file should be created when writer disabled");
    let metadata = std::fs::metadata(&proto_file).unwrap();
    assert!(metadata.len() > 0, "Protobuf file should not be empty");
}

#[tokio::test]
async fn test_arrow_only_debug_output() {
    // Test that only Arrow files are created when arrow_enabled=true, protobuf_enabled=false
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_debug_arrow_enabled(true)
    .with_debug_protobuf_enabled(false)
    .with_debug_output(debug_output_dir.clone())
    .with_zerobus_writer_disabled(true);

    let wrapper = ZerobusWrapper::new(config).await.unwrap();
    
    // Create test batch
    let schema = Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, false),
    ]);
    let id_array = Int64Array::from(vec![1, 2, 3]);
    let name_array = StringArray::from(vec!["Alice", "Bob", "Charlie"]);
    let batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(id_array), Arc::new(name_array)],
    )
    .unwrap();

    // Send batch
    wrapper.send_batch(batch).await.unwrap();
    wrapper.flush().await.unwrap();
    sleep(Duration::from_millis(500)).await;
    
    // Verify Arrow file was created
    let sanitized_table_name = "test_table".replace(['.', '/'], "_");
    let arrow_file = debug_output_dir.join(format!("zerobus/arrow/{}.arrows", sanitized_table_name));
    assert!(arrow_file.exists(), "Arrow file should be created when arrow_enabled=true");
    
    // Verify Protobuf file was NOT created
    let proto_file = debug_output_dir.join(format!("zerobus/proto/{}.proto", sanitized_table_name));
    assert!(!proto_file.exists(), "Protobuf file should NOT be created when protobuf_enabled=false");
}

#[tokio::test]
async fn test_protobuf_only_debug_output() {
    // Test that only Protobuf files are created when protobuf_enabled=true, arrow_enabled=false
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_debug_arrow_enabled(false)
    .with_debug_protobuf_enabled(true)
    .with_debug_output(debug_output_dir.clone())
    .with_zerobus_writer_disabled(true);

    let wrapper = ZerobusWrapper::new(config).await.unwrap();
    
    // Create test batch
    let schema = Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, false),
    ]);
    let id_array = Int64Array::from(vec![1, 2, 3]);
    let name_array = StringArray::from(vec!["Alice", "Bob", "Charlie"]);
    let batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(id_array), Arc::new(name_array)],
    )
    .unwrap();

    // Send batch
    wrapper.send_batch(batch).await.unwrap();
    wrapper.flush().await.unwrap();
    sleep(Duration::from_millis(500)).await;
    
    // Verify Protobuf file was created
    let sanitized_table_name = "test_table".replace(['.', '/'], "_");
    let proto_file = debug_output_dir.join(format!("zerobus/proto/{}.proto", sanitized_table_name));
    assert!(proto_file.exists(), "Protobuf file should be created when protobuf_enabled=true");
    
    // Verify Arrow file was NOT created
    let arrow_file = debug_output_dir.join(format!("zerobus/arrow/{}.arrows", sanitized_table_name));
    assert!(!arrow_file.exists(), "Arrow file should NOT be created when arrow_enabled=false");
}

#[tokio::test]
async fn test_both_formats_enabled() {
    // Test that both Arrow and Protobuf files are created when both flags are true
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_debug_arrow_enabled(true)
    .with_debug_protobuf_enabled(true)
    .with_debug_output(debug_output_dir.clone())
    .with_zerobus_writer_disabled(true);

    let wrapper = ZerobusWrapper::new(config).await.unwrap();
    
    // Create test batch
    let schema = Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, false),
    ]);
    let id_array = Int64Array::from(vec![1, 2, 3]);
    let name_array = StringArray::from(vec!["Alice", "Bob", "Charlie"]);
    let batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(id_array), Arc::new(name_array)],
    )
    .unwrap();

    // Send batch
    wrapper.send_batch(batch).await.unwrap();
    wrapper.flush().await.unwrap();
    sleep(Duration::from_millis(500)).await;
    
    // Verify both files were created
    let sanitized_table_name = "test_table".replace(['.', '/'], "_");
    let arrow_file = debug_output_dir.join(format!("zerobus/arrow/{}.arrows", sanitized_table_name));
    let proto_file = debug_output_dir.join(format!("zerobus/proto/{}.proto", sanitized_table_name));
    
    assert!(arrow_file.exists(), "Arrow file should be created when arrow_enabled=true");
    assert!(proto_file.exists(), "Protobuf file should be created when protobuf_enabled=true");
}

#[tokio::test]
async fn test_both_formats_disabled() {
    // Test that no debug files are created when both flags are false
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_debug_arrow_enabled(false)
    .with_debug_protobuf_enabled(false)
    .with_debug_output(debug_output_dir.clone())
    .with_zerobus_writer_disabled(true);

    let wrapper = ZerobusWrapper::new(config).await.unwrap();
    
    // Create test batch
    let schema = Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, false),
    ]);
    let id_array = Int64Array::from(vec![1, 2, 3]);
    let name_array = StringArray::from(vec!["Alice", "Bob", "Charlie"]);
    let batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(id_array), Arc::new(name_array)],
    )
    .unwrap();

    // Send batch
    wrapper.send_batch(batch).await.unwrap();
    wrapper.flush().await.unwrap();
    sleep(Duration::from_millis(500)).await;
    
    // Verify no files were created
    let sanitized_table_name = "test_table".replace(['.', '/'], "_");
    let arrow_file = debug_output_dir.join(format!("zerobus/arrow/{}.arrows", sanitized_table_name));
    let proto_file = debug_output_dir.join(format!("zerobus/proto/{}.proto", sanitized_table_name));
    
    assert!(!arrow_file.exists(), "Arrow file should NOT be created when arrow_enabled=false");
    assert!(!proto_file.exists(), "Protobuf file should NOT be created when protobuf_enabled=false");
}

#[tokio::test]
async fn test_rotation_no_recursive_timestamps() {
    // Test that file rotation doesn't create recursive timestamps
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_debug_arrow_enabled(true)
    .with_debug_output(debug_output_dir.clone())
    .with_zerobus_writer_disabled(true);

    let wrapper = ZerobusWrapper::new(config).await.unwrap();
    
    // Create batches to trigger multiple rotations
    let schema = Schema::new(vec![Field::new("id", DataType::Int64, false)]);
    let batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(Int64Array::from(vec![1; 1001]))], // Exceed rotation threshold
    )
    .unwrap();
    
    // Trigger multiple rotations
    for _ in 0..5 {
        wrapper.send_batch(batch.clone()).await.unwrap();
    }
    
    wrapper.flush().await.unwrap();
    sleep(Duration::from_millis(500)).await;
    
    // Check rotated files for recursive timestamps
    let arrow_dir = debug_output_dir.join("zerobus/arrow");
    if arrow_dir.exists() {
        let entries: Vec<_> = std::fs::read_dir(&arrow_dir)
            .unwrap()
            .map(|e| e.unwrap().file_name().to_string_lossy().to_string())
            .collect();
        
        for entry in entries {
            // Count timestamp patterns - should be at most 1 per filename
            let timestamp_patterns: Vec<_> = entry.match_indices("_20")
                .filter(|(_, s)| {
                    // Check if followed by date pattern (YYYYMMDD_HHMMSS)
                    let rest = &entry[s.len()..];
                    rest.len() >= 14 && rest.chars().take(14).all(|c| c.is_ascii_digit() || c == '_')
                })
                .collect();
            
            assert!(timestamp_patterns.len() <= 1, 
                "Filename should have at most one timestamp pattern: {} (found {})", 
                entry, timestamp_patterns.len());
        }
    }
}

#[tokio::test]
async fn test_file_retention_cleanup() {
    // Test that old files are cleaned up when retention limit is exceeded
    use std::fs;
    
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    let arrow_dir = debug_output_dir.join("zerobus/arrow");
    fs::create_dir_all(&arrow_dir).unwrap();
    
    // Pre-create 12 rotated files (more than limit of 10)
    let sanitized_table_name = "test_table".replace(['.', '/'], "_");
    for i in 0..12 {
        let timestamp = format!("20250101_{:06}", i * 100);
        let file_path = arrow_dir.join(format!("{}_{}.arrows", sanitized_table_name, timestamp));
        fs::File::create(&file_path).unwrap();
    }
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_debug_arrow_enabled(true)
    .with_debug_output(debug_output_dir.clone())
    .with_debug_max_files_retained(Some(10))
    .with_zerobus_writer_disabled(true);

    let wrapper = ZerobusWrapper::new(config).await.unwrap();
    
    // Trigger rotation which should cleanup old files
    let schema = Schema::new(vec![Field::new("id", DataType::Int64, false)]);
    let batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(Int64Array::from(vec![1; 1001]))],
    )
    .unwrap();
    
    wrapper.send_batch(batch).await.unwrap();
    wrapper.flush().await.unwrap();
    sleep(Duration::from_millis(500)).await;
    
    // Count files (should be <= 11: 10 old + 1 new active, or 10 if cleanup worked)
    let entries: Vec<_> = fs::read_dir(&arrow_dir)
        .unwrap()
        .map(|e| e.unwrap().file_name().to_string_lossy().to_string())
        .collect();
    
    assert!(entries.len() <= 11, 
        "Should have at most 11 files after cleanup (10 retained + 1 active), got {}", 
        entries.len());
}

#[tokio::test]
async fn test_file_retention_unlimited() {
    // Test that files are not cleaned up when retention is unlimited (None)
    use std::fs;
    
    let temp_dir = TempDir::new().unwrap();
    let debug_output_dir = temp_dir.path().to_path_buf();
    let arrow_dir = debug_output_dir.join("zerobus/arrow");
    fs::create_dir_all(&arrow_dir).unwrap();
    
    // Pre-create some rotated files
    let sanitized_table_name = "test_table".replace(['.', '/'], "_");
    for i in 0..5 {
        let timestamp = format!("20250101_{:06}", i * 100);
        let file_path = arrow_dir.join(format!("{}_{}.arrows", sanitized_table_name, timestamp));
        fs::File::create(&file_path).unwrap();
    }
    
    let initial_count = fs::read_dir(&arrow_dir).unwrap().count();
    
    let config = WrapperConfiguration::new(
        "https://test.cloud.databricks.com".to_string(),
        "test_table".to_string(),
    )
    .with_debug_arrow_enabled(true)
    .with_debug_output(debug_output_dir.clone())
    .with_debug_max_files_retained(None) // Unlimited
    .with_zerobus_writer_disabled(true);

    let wrapper = ZerobusWrapper::new(config).await.unwrap();
    
    // Trigger rotation
    let schema = Schema::new(vec![Field::new("id", DataType::Int64, false)]);
    let batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(Int64Array::from(vec![1; 1001]))],
    )
    .unwrap();
    
    wrapper.send_batch(batch).await.unwrap();
    wrapper.flush().await.unwrap();
    sleep(Duration::from_millis(500)).await;
    
    // All files should still exist (no cleanup)
    let final_count = fs::read_dir(&arrow_dir).unwrap().count();
    assert!(final_count >= initial_count + 1, 
        "Should retain all files with unlimited retention (initial: {}, final: {})", 
        initial_count, final_count);
}