arkflow-plugin 0.1.0

High-performance Rust flow processing engine
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! HTTP output component
//!
//! Send the processed data to the HTTP endpoint

use arkflow_core::output::{register_output_builder, Output, OutputBuilder};
use arkflow_core::{Error, MessageBatch};
use async_trait::async_trait;
use reqwest::{header, Client};
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::sync::Mutex;

/// HTTP output configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpOutputConfig {
    /// Destination URL
    pub url: String,
    /// HTTP method
    pub method: String,
    /// Timeout Period (ms)
    pub timeout_ms: u64,
    /// Number of retries
    pub retry_count: u32,
    /// Request header
    pub headers: Option<std::collections::HashMap<String, String>>,
}

/// HTTP output component
pub struct HttpOutput {
    config: HttpOutputConfig,
    client: Arc<Mutex<Option<Client>>>,
    connected: AtomicBool,
}

impl HttpOutput {
    /// Create a new HTTP output component
    pub fn new(config: HttpOutputConfig) -> Result<Self, Error> {
        Ok(Self {
            config,
            client: Arc::new(Mutex::new(None)),
            connected: AtomicBool::new(false),
        })
    }
}

#[async_trait]
impl Output for HttpOutput {
    async fn connect(&self) -> Result<(), Error> {
        // Create an HTTP client
        let client_builder =
            Client::builder().timeout(std::time::Duration::from_millis(self.config.timeout_ms));
        let client_arc = self.client.clone();
        client_arc.lock().await.replace(
            client_builder.build().map_err(|e| {
                Error::Connection(format!("Unable to create an HTTP client: {}", e))
            })?,
        );

        self.connected.store(true, Ordering::SeqCst);
        Ok(())
    }

    async fn write(&self, msg: &MessageBatch) -> Result<(), Error> {
        let client_arc = self.client.clone();
        let client_arc_guard = client_arc.lock().await;
        if !self.connected.load(Ordering::SeqCst) || client_arc_guard.is_none() {
            return Err(Error::Connection("The output is not connected".to_string()));
        }

        let client = client_arc_guard.as_ref().unwrap();
        let content = msg.as_string()?;
        if content.is_empty() {
            return Ok(());
        }
        let body;
        if content.len() == 1 {
            body = content[0].clone();
        } else {
            body = serde_json::to_string(&content)
                .map_err(|_| Error::Process("Unable to serialize message".to_string()))?;
        }

        // Build the request
        let mut request_builder = match self.config.method.to_uppercase().as_str() {
            "GET" => client.get(&self.config.url),
            "POST" => client.post(&self.config.url).body(body), // Content-Type由统一逻辑添加
            "PUT" => client.put(&self.config.url).body(body),
            "DELETE" => client.delete(&self.config.url),
            "PATCH" => client.patch(&self.config.url).body(body),
            _ => {
                return Err(Error::Config(format!(
                    "HTTP methods that are not supported: {}",
                    self.config.method
                )))
            }
        };

        // Add request headers
        if let Some(headers) = &self.config.headers {
            for (key, value) in headers {
                request_builder = request_builder.header(key, value);
            }
        }

        // Add content type header (if not specified)
        // 始终添加Content-Type头(如果未指定)
        if let Some(headers) = &self.config.headers {
            if !headers.contains_key("Content-Type") {
                request_builder = request_builder.header(header::CONTENT_TYPE, "application/json");
            }
        } else {
            request_builder = request_builder.header(header::CONTENT_TYPE, "application/json");
        }

        // Send a request
        let mut retry_count = 0;
        let mut last_error = None;

        while retry_count <= self.config.retry_count {
            match request_builder.try_clone().unwrap().send().await {
                Ok(response) => {
                    if response.status().is_success() {
                        return Ok(());
                    } else {
                        let status = response.status();
                        let body = response
                            .text()
                            .await
                            .unwrap_or_else(|_| "<Unable to read response body>".to_string());
                        last_error = Some(Error::Process(format!(
                            "HTTP Request Failed: Status code {}, response: {}",
                            status, body
                        )));
                    }
                }
                Err(e) => {
                    last_error = Some(Error::Connection(format!("HTTP request error: {}", e)));
                }
            }

            retry_count += 1;
            if retry_count <= self.config.retry_count {
                // Index backoff retry
                tokio::time::sleep(std::time::Duration::from_millis(
                    100 * 2u64.pow(retry_count - 1),
                ))
                .await;
            }
        }

        Err(last_error.unwrap_or_else(|| Error::Unknown("Unknown HTTP error".to_string())))
    }

    async fn close(&self) -> Result<(), Error> {
        self.connected.store(false, Ordering::SeqCst);
        let mut guard = self.client.lock().await;
        *guard = None;
        Ok(())
    }
}

pub(crate) struct HttpOutputBuilder;
impl OutputBuilder for HttpOutputBuilder {
    fn build(&self, config: &Option<serde_json::Value>) -> Result<Arc<dyn Output>, Error> {
        if config.is_none() {
            return Err(Error::Config(
                "HTTP output configuration is missing".to_string(),
            ));
        }
        let config: HttpOutputConfig = serde_json::from_value(config.clone().unwrap())?;

        Ok(Arc::new(HttpOutput::new(config)?))
    }
}

pub fn init() {
    register_output_builder("http", Arc::new(HttpOutputBuilder));
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{
        extract::Json,
        http::StatusCode,
        response::IntoResponse,
        routing::{delete, get, patch, post, put},
        Router,
    };
    use serde_json::json;
    use std::net::SocketAddr;

    /// Test creating a new HTTP output component
    #[tokio::test]
    async fn test_http_output_new() {
        // Create a basic configuration
        let config = HttpOutputConfig {
            url: "http://example.com".to_string(),
            method: "POST".to_string(),
            timeout_ms: 5000,
            retry_count: 3,
            headers: None,
        };

        // Create a new HTTP output component
        let output = HttpOutput::new(config);
        assert!(output.is_ok(), "Failed to create HTTP output component");
    }

    /// Test connecting to the HTTP output
    #[tokio::test]
    async fn test_http_output_connect() {
        // Create a basic configuration
        let config = HttpOutputConfig {
            url: "http://example.com".to_string(),
            method: "POST".to_string(),
            timeout_ms: 5000,
            retry_count: 3,
            headers: None,
        };

        // Create and connect the HTTP output
        let output = HttpOutput::new(config).unwrap();
        let result = output.connect().await;
        assert!(result.is_ok(), "Failed to connect HTTP output");
        assert!(
            output.connected.load(Ordering::SeqCst),
            "Connected flag not set"
        );

        // Verify client is initialized
        let client_guard = output.client.lock().await;
        assert!(client_guard.is_some(), "HTTP client not initialized");
    }

    /// Test closing the HTTP output
    #[tokio::test]
    async fn test_http_output_close() {
        // Create a basic configuration
        let config = HttpOutputConfig {
            url: "http://example.com".to_string(),
            method: "POST".to_string(),
            timeout_ms: 5000,
            retry_count: 3,
            headers: None,
        };

        // Create, connect, and close the HTTP output
        let output = HttpOutput::new(config).unwrap();
        output.connect().await.unwrap();
        let result = output.close().await;

        assert!(result.is_ok(), "Failed to close HTTP output");
        assert!(
            !output.connected.load(Ordering::SeqCst),
            "Connected flag not reset"
        );

        // Verify client is cleared
        let client_guard = output.client.lock().await;
        assert!(client_guard.is_none(), "HTTP client not cleared");
    }

    /// Test writing to HTTP output without connecting first
    #[tokio::test]
    async fn test_http_output_write_without_connect() {
        // Create a basic configuration
        let config = HttpOutputConfig {
            url: "http://example.com".to_string(),
            method: "POST".to_string(),
            timeout_ms: 5000,
            retry_count: 3,
            headers: None,
        };

        // Create HTTP output without connecting
        let output = HttpOutput::new(config).unwrap();
        let msg = MessageBatch::from_string("test message");
        let result = output.write(&msg).await;

        // Should return connection error
        assert!(result.is_err(), "Write should fail when not connected");
        match result {
            Err(Error::Connection(_)) => {} // Expected error
            _ => panic!("Expected Connection error"),
        }
    }

    /// Test writing with empty message
    #[tokio::test]
    async fn test_http_output_write_empty_message() {
        // Create a basic configuration
        let config = HttpOutputConfig {
            url: "http://example.com".to_string(),
            method: "POST".to_string(),
            timeout_ms: 5000,
            retry_count: 3,
            headers: None,
        };

        // Create and connect HTTP output
        let output = HttpOutput::new(config).unwrap();
        output.connect().await.unwrap();

        // Create empty message
        let msg = MessageBatch::new_binary(vec![]);
        let result = output.write(&msg).await;

        // Should succeed with empty message
        assert!(result.is_ok(), "Write should succeed with empty message");
    }

    /// Test HTTP output with custom headers
    #[tokio::test]
    async fn test_http_output_with_custom_headers() {
        // Create a configuration with custom headers
        let mut headers = std::collections::HashMap::new();
        headers.insert("X-Custom-Header".to_string(), "test-value".to_string());
        headers.insert("Content-Type".to_string(), "application/xml".to_string());

        let config = HttpOutputConfig {
            url: "http://example.com".to_string(),
            method: "POST".to_string(),
            timeout_ms: 5000,
            retry_count: 3,
            headers: Some(headers),
        };

        // Create and connect HTTP output
        let output = HttpOutput::new(config).unwrap();
        output.connect().await.unwrap();

        // We can't easily test the actual headers sent without a mock server,
        // but we can verify the component initializes correctly
        assert!(output.connected.load(Ordering::SeqCst));
    }

    /// Test HTTP output with unsupported method
    #[tokio::test]
    async fn test_http_output_unsupported_method() {
        // Create a configuration with unsupported method
        let config = HttpOutputConfig {
            url: "http://example.com".to_string(),
            method: "CONNECT".to_string(), // Unsupported method
            timeout_ms: 5000,
            retry_count: 3,
            headers: None,
        };

        // Create and connect HTTP output
        let output = HttpOutput::new(config).unwrap();
        output.connect().await.unwrap();

        // Try to write a message
        let msg = MessageBatch::from_string("test message");
        let result = output.write(&msg).await;

        // Should return config error
        assert!(result.is_err(), "Write should fail with unsupported method");
        match result {
            Err(Error::Config(_)) => {} // Expected error
            _ => panic!("Expected Config error, got {:?}", result),
        }
    }

    /// Helper function to create a test HTTP server
    async fn setup_test_server() -> (String, tokio::task::JoinHandle<()>) {
        // Create a router with endpoints for different HTTP methods
        let app = Router::new()
            .route("/get", get(|| async { StatusCode::OK }))
            .route(
                "/post",
                post(|_: Json<serde_json::Value>| async { StatusCode::CREATED }),
            )
            .route(
                "/put",
                put(|_: Json<serde_json::Value>| async { StatusCode::OK }),
            )
            .route("/delete", delete(|| async { StatusCode::NO_CONTENT }))
            .route(
                "/patch",
                patch(|_: Json<serde_json::Value>| async { StatusCode::OK }),
            )
            .route(
                "/error",
                get(|| async { StatusCode::INTERNAL_SERVER_ERROR.into_response() }),
            );

        // Find an available port
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let port = addr.port();
        drop(listener); // Release the port for the server to use

        // Start the server in a separate task
        let server_handle = tokio::spawn(async move {
            let addr = SocketAddr::from(([127, 0, 0, 1], port));
            axum::Server::bind(&addr)
                .serve(app.into_make_service())
                .await
                .unwrap();
        });

        // Give the server a moment to start up
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

        // Return the base URL and server handle
        (format!("http://127.0.0.1:{}", port), server_handle)
    }

    /// Test HTTP output with successful GET request
    #[tokio::test]
    async fn test_http_output_get_request() {
        // Start a test server
        let (base_url, server_handle) = setup_test_server().await;
        let url = format!("{}/get", base_url);

        // Create configuration for GET request
        let config = HttpOutputConfig {
            url,
            method: "GET".to_string(),
            timeout_ms: 5000,
            retry_count: 3,
            headers: None,
        };

        // Create, connect, and write to HTTP output
        let output = HttpOutput::new(config).unwrap();
        output.connect().await.unwrap();
        let msg = MessageBatch::from_string("test message");
        let result = output.write(&msg).await;

        // Should succeed with 200 OK response
        assert!(result.is_ok(), "Write should succeed with 200 OK response");

        // Clean up
        output.close().await.unwrap();
        server_handle.abort();
    }

    /// Test HTTP output with successful POST request
    #[tokio::test]
    async fn test_http_output_post_request() {
        // Start a test server
        let (base_url, server_handle) = setup_test_server().await;
        let url = format!("{}/post", base_url);

        // Create configuration for POST request
        let config = HttpOutputConfig {
            url,
            method: "POST".to_string(),
            timeout_ms: 5000,
            retry_count: 3,
            headers: None,
        };

        // Create, connect, and write to HTTP output
        let output = HttpOutput::new(config).unwrap();
        output.connect().await.unwrap();

        // Use a valid JSON string for the message
        let msg = MessageBatch::from_string("{\"test\": \"message\"}");

        // Add a small delay to ensure server is ready
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        let result = output.write(&msg).await;

        // Should succeed with 201 Created response
        assert!(
            result.is_ok(),
            "Write should succeed with 201 Created response"
        );

        // Clean up
        output.close().await.unwrap();
        server_handle.abort();
    }

    /// Test HTTP output with error response
    #[tokio::test]
    async fn test_http_output_error_response() {
        // Start a test server
        let (base_url, server_handle) = setup_test_server().await;
        let url = format!("{}/error", base_url);

        // Create configuration with no retries
        let config = HttpOutputConfig {
            url,
            method: "GET".to_string(),
            timeout_ms: 5000,
            retry_count: 0, // No retries
            headers: None,
        };

        // Create, connect, and write to HTTP output
        let output = HttpOutput::new(config).unwrap();
        output.connect().await.unwrap();
        let msg = MessageBatch::from_string("test message");
        let result = output.write(&msg).await;

        // Should fail with either Processing or Connection error
        assert!(result.is_err(), "Write should fail with error response");
        match result {
            Err(Error::Process(_)) => {}    // Expected error type 1
            Err(Error::Connection(_)) => {} // Also acceptable error type
            _ => panic!("Expected Processing or Connection error, got {:?}", result),
        }

        // Clean up
        output.close().await.unwrap();
        server_handle.abort();
    }

    /// Test HTTP output retry mechanism
    #[tokio::test]
    async fn test_http_output_retry() {
        // Start a test server
        let (base_url, server_handle) = setup_test_server().await;

        // Use a non-existent endpoint to force connection errors
        let url = format!("{}/nonexistent", base_url);

        // Create configuration with retries
        let config = HttpOutputConfig {
            url,
            method: "GET".to_string(),
            timeout_ms: 1000,
            retry_count: 2, // 2 retries
            headers: None,
        };

        // Create, connect, and write to HTTP output
        let output = HttpOutput::new(config).unwrap();
        output.connect().await.unwrap();
        let msg = MessageBatch::from_string("test message");

        // Measure time to verify retry delay
        let start = std::time::Instant::now();
        let result = output.write(&msg).await;
        let elapsed = start.elapsed();

        // Should fail after retries
        assert!(result.is_err(), "Write should fail after retries");

        // Verify that some time has passed for retries (at least 300ms for 2 retries)
        // First retry: 100ms, Second retry: 200ms
        assert!(
            elapsed.as_millis() >= 300,
            "Retry mechanism not working properly"
        );

        // Clean up
        output.close().await.unwrap();
        server_handle.abort();
    }

    /// Test HTTP output builder
    #[tokio::test]
    async fn test_http_output_builder() {
        // Create a valid configuration
        let config = json!({
            "url": "http://example.com",
            "method": "POST",
            "timeout_ms": 5000,
            "retry_count": 3
        });

        // Create builder and build output
        let builder = HttpOutputBuilder;
        let result = builder.build(&Some(config));
        assert!(
            result.is_ok(),
            "Builder should create output with valid config"
        );

        // Test with missing configuration
        let result = builder.build(&None);
        assert!(result.is_err(), "Builder should fail with missing config");

        // Test with invalid configuration
        let invalid_config = json!({"invalid": "config"});
        let result = builder.build(&Some(invalid_config));
        assert!(result.is_err(), "Builder should fail with invalid config");
    }
}