mfyuce commented on code in PR #3523:
URL: https://github.com/apache/iggy/pull/3523#discussion_r3655653857


##########
core/connectors/sinks/quickwit_sink/src/lib.rs:
##########
@@ -16,158 +16,325 @@
 // under the License.
 
 use async_trait::async_trait;
+use base64::{Engine as _, engine::general_purpose};
+use iggy_connector_sdk::retry::{
+    ConnectivityConfig, build_retry_client, check_connectivity_with_retry, 
parse_duration,
+};
 use iggy_connector_sdk::{
-    ConsumedMessage, Error, MessagesMetadata, Payload, Sink, TopicMetadata, 
sink_connector,
+    ConsumedMessage, Error, MessagesMetadata, Payload, Schema, Sink, 
TopicMetadata, sink_connector,
 };
+use reqwest::StatusCode;
+use reqwest::Url;
+use reqwest_middleware::ClientWithMiddleware;
 use serde::{Deserialize, Serialize};
-use tracing::{error, info, warn};
+use simd_json::OwnedValue;
+use tracing::{debug, error, info, warn};
 
 sink_connector!(QuickwitSink);
 
+const DEFAULT_MAX_RETRIES: u32 = 3;
+const DEFAULT_RETRY_DELAY: &str = "200ms";
+const DEFAULT_MAX_RETRY_DELAY: &str = "5s";
+const DEFAULT_MAX_OPEN_RETRIES: u32 = 5;
+const DEFAULT_OPEN_RETRY_MAX_DELAY: &str = "30s";
+const DEFAULT_REQUEST_TIMEOUT: &str = "30s";
+
 #[derive(Debug)]
 pub struct QuickwitSink {
     id: u32,
     config: QuickwitSinkConfig,
-    client: reqwest::Client,
+    client: Option<ClientWithMiddleware>,
+    verbose: bool,
     index_id: String,
 }
 
 #[derive(Debug, Serialize, Deserialize)]
 pub struct QuickwitSinkConfig {
-    url: String,
-    index: String,
+    pub url: String,
+    /// Full Quickwit index config YAML, passed to `POST /api/v1/indexes` on 
first open.
+    /// `index_id` is extracted from this YAML to build ingest URLs.
+    pub index: String,
+    pub verbose_logging: Option<bool>,
+    pub max_retries: Option<u32>,
+    pub retry_delay: Option<String>,
+    pub max_retry_delay: Option<String>,
+    pub max_open_retries: Option<u32>,
+    pub open_retry_max_delay: Option<String>,
+    pub request_timeout: Option<String>,
 }
 
-#[derive(Debug, Serialize, Deserialize)]
-struct IndexConfig {
+#[derive(Debug, Deserialize)]
+struct IndexIdExtract {
     index_id: String,
 }
 
 impl QuickwitSink {
     pub fn new(id: u32, config: QuickwitSinkConfig) -> Self {
-        let index_config =
-            
serde_yaml_ng::from_str::<IndexConfig>(&config.index).expect("Invalid index 
config.");
-        QuickwitSink {
+        let verbose = config.verbose_logging.unwrap_or(false);
+        Self {
             id,
             config,
-            index_id: index_config.index_id,
-            client: reqwest::Client::new(),
+            client: None,
+            verbose,
+            index_id: String::new(),
         }
     }
 
+    fn client(&self) -> Result<&ClientWithMiddleware, Error> {
+        self.client
+            .as_ref()
+            .ok_or_else(|| Error::InitError("Quickwit sink client not 
initialized".into()))
+    }
+
     async fn has_index(&self) -> Result<bool, Error> {
+        let client = self.client()?;
         let url = format!("{}/api/v1/indexes/{}", self.config.url, 
self.index_id);
-        let response = self.client.get(&url).send().await.map_err(|error| {
-            error!(
-                "Failed to send HTTP request to check if index with ID: {} 
exists. {error}",
-                self.index_id
-            );
-            Error::HttpRequestFailed(error.to_string())
-        })?;
+        let response = client
+            .get(&url)
+            .send()
+            .await
+            .map_err(|e| Error::HttpRequestFailed(e.to_string()))?;
         let status = response.status();
         if status.is_success() {
             Ok(true)
-        } else if status == reqwest::StatusCode::NOT_FOUND {
+        } else if status == StatusCode::NOT_FOUND {
             Ok(false)
         } else {
             Err(Error::HttpRequestFailed(format!(
-                "Unexpected status code: {status}",
+                "Unexpected status checking Quickwit index: {status}"
             )))
         }
     }
 
     async fn create_index(&self) -> Result<(), Error> {
-        info!("Creating index: {}", self.index_id);
+        info!(
+            "Creating Quickwit index: {} for connector ID: {}",
+            self.index_id, self.id
+        );
+        let client = self.client()?;
         let url = format!("{}/api/v1/indexes", self.config.url);
-        let response = self
-            .client
+        let response = client
             .post(&url)
-            .header("content-type", "application/yaml")
-            .body(self.config.index.to_owned())
+            .header("Content-Type", "application/yaml")
+            .body(self.config.index.clone())
             .send()
             .await
-            .map_err(|error| {
+            .map_err(|e| {
                 error!(
-                    "Failed to send HTTP request to create index: {}. {error}",
-                    self.index_id
+                    "Failed to create Quickwit index: {} for connector ID: {}. 
{e}",
+                    self.index_id, self.id
                 );
-                Error::HttpRequestFailed(error.to_string())
+                Error::HttpRequestFailed(e.to_string())
             })?;
 
-        if !response.status().is_success() {
-            let status = response.status();
+        let status = response.status();
+        if status.is_success() {
+            info!(
+                "Created Quickwit index: {} for connector ID: {}",
+                self.index_id, self.id
+            );
+            Ok(())
+        } else if status == StatusCode::CONFLICT {
+            // Another instance beat us to it; the index exists, which is what 
we want.
+            info!(
+                "Quickwit index already exists (409): {} for connector ID: {}",
+                self.index_id, self.id
+            );
+            Ok(())
+        } else if status.is_client_error() {
             let reason = response.text().await.unwrap_or_default();
             error!(
-                "Received an invalid HTTP response when creating index: {}. 
Status code: {status}, reason: {reason}",
+                "Permanent client error creating Quickwit index: {} for 
connector ID: {}. status: {status}, reason: {reason}",
+                self.index_id, self.id
+            );
+            Err(Error::InitError(format!(
+                "Failed to create index '{0}': {status} {reason}",
                 self.index_id
+            )))
+        } else {
+            let reason = response.text().await.unwrap_or_default();
+            error!(
+                "Server error creating Quickwit index: {} for connector ID: 
{}. status: {status}, reason: {reason}",
+                self.index_id, self.id
             );
-            return Err(Error::InitError(format!(
-                "Failed to create index: {}. {reason}",
+            Err(Error::InitError(format!(
+                "Failed to create index '{0}': {status} {reason}",
                 self.index_id
-            )));
+            )))
         }
-
-        info!("Created index: {}", self.index_id);
-        Ok(())
     }
 
     pub async fn ingest(&self, messages: Vec<simd_json::OwnedValue>) -> 
Result<(), Error> {
+        let client = self.client()?;
+        // At-least-once: Quickwit ingest carries no dedup key, so a retry 
after a
+        // 5xx/timeout that actually committed (commit=auto) double-writes 
those rows.
         let url = format!(
             "{}/api/v1/{}/ingest?commit=auto",
             self.config.url, self.index_id
         );
-        info!("Ingesting messages for index: {}...", self.index_id);
         let messages_count = messages.len();
-        let messages = messages
-            .into_iter()
-            .filter_map(|record| simd_json::to_string(&record).ok())
-            .collect::<Vec<_>>()
-            .join("\n");
-
-        let response = self
-            .client
+        let mut ndjson = String::new();

Review Comment:
   @hubcio 
   
   About `NDJSON Pre-allocation & Memory Footprint`: To optimize the ndjson 
allocation without introducing magic numbers or overhead,  a few approaches:
   
   **Pre-serializing all records to get the exact length**: Cons=> holding all 
individual Strings in a Vec<String> alongside the final joined String doubles 
the peak memory consumption of the batch payload.
   
   **Sample-and-estimate (using the first message length)**: Can lead to 
over/under-allocation in heterogeneous batches.
   
   Current locally:
   
   `let mut ndjson = String::with_capacity(messages.len() * 512);`
   
   This prevents most reallocations without any CPU or memory overhead.
   Does this standard messages.len() * 512 pre-allocation heuristic look good 
to you, or would you prefer a different approach here?
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to