atharvalade commented on code in PR #3103:
URL: https://github.com/apache/iggy/pull/3103#discussion_r3431557928


##########
core/connectors/sinks/s3_sink/src/lib.rs:
##########
@@ -0,0 +1,478 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::fmt;
+use std::str::FromStr;
+use std::sync::Arc;
+
+use iggy_connector_sdk::{Error, sink_connector};
+use secrecy::SecretString;
+use serde::{Deserialize, Serialize};
+
+mod buffer;
+mod client;
+mod formatter;
+mod path;
+mod sink;
+
+sink_connector!(S3Sink);
+
+const DEFAULT_MAX_ATTEMPTS: u32 = 3;
+const DEFAULT_RETRY_DELAY: &str = "1s";
+const DEFAULT_MAX_FILE_SIZE: &str = "8MiB";
+const DEFAULT_PATH_TEMPLATE: &str = "{stream}/{topic}/{date}/{hour}";
+const DEFAULT_OUTPUT_FORMAT: &str = "json_lines";
+const MAX_S3_SINGLE_PUT_SIZE: u64 = 5 * 1024 * 1024 * 1024; // 5 GiB
+
+#[derive(Clone, Serialize, Deserialize)]
+pub struct S3SinkConfig {
+    pub bucket: String,
+    pub region: String,
+    #[serde(default)]
+    pub prefix: Option<String>,
+    #[serde(default)]
+    pub endpoint: Option<String>,
+    #[serde(
+        default,
+        serialize_with = "iggy_common::serde_secret::serialize_optional_secret"
+    )]
+    pub access_key_id: Option<SecretString>,
+    #[serde(
+        default,
+        serialize_with = "iggy_common::serde_secret::serialize_optional_secret"
+    )]
+    pub secret_access_key: Option<SecretString>,
+    #[serde(default = "default_path_template")]
+    pub path_template: String,
+    #[serde(default = "default_file_rotation")]
+    pub file_rotation: FileRotation,
+    #[serde(default = "default_max_file_size")]
+    pub max_file_size: String,
+    #[serde(default)]
+    pub max_messages_per_file: Option<u64>,
+    #[serde(default = "default_output_format")]
+    pub output_format: String,
+    #[serde(default = "default_true")]
+    pub include_metadata: bool,
+    #[serde(default)]
+    pub include_headers: bool,
+    #[serde(default, alias = "max_retries")]
+    pub max_attempts: Option<u32>,
+    #[serde(default)]
+    pub retry_delay: Option<String>,
+    #[serde(default)]
+    pub path_style: Option<bool>,
+}
+
+impl fmt::Debug for S3SinkConfig {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("S3SinkConfig")
+            .field("bucket", &self.bucket)
+            .field("region", &self.region)
+            .field("prefix", &self.prefix)
+            .field("endpoint", &self.endpoint)
+            .field("access_key_id", &"[REDACTED]")
+            .field("secret_access_key", &"[REDACTED]")
+            .field("path_template", &self.path_template)
+            .field("file_rotation", &self.file_rotation)
+            .field("max_file_size", &self.max_file_size)
+            .field("max_messages_per_file", &self.max_messages_per_file)
+            .field("output_format", &self.output_format)
+            .field("include_metadata", &self.include_metadata)
+            .field("include_headers", &self.include_headers)
+            .field("max_attempts", &self.max_attempts)
+            .field("retry_delay", &self.retry_delay)
+            .field("path_style", &self.path_style)
+            .finish()
+    }
+}
+
+fn default_path_template() -> String {
+    DEFAULT_PATH_TEMPLATE.to_string()
+}
+
+fn default_file_rotation() -> FileRotation {
+    FileRotation::Size
+}
+
+fn default_max_file_size() -> String {
+    DEFAULT_MAX_FILE_SIZE.to_string()
+}
+
+fn default_output_format() -> String {
+    DEFAULT_OUTPUT_FORMAT.to_string()
+}
+
+fn default_true() -> bool {
+    true
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum FileRotation {
+    Size,
+    Messages,
+}
+
+impl fmt::Display for FileRotation {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self {
+            FileRotation::Size => write!(f, "size"),
+            FileRotation::Messages => write!(f, "messages"),
+        }
+    }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub(crate) enum OutputFormat {
+    JsonLines,
+    JsonArray,
+    Raw,
+}
+
+impl TryFrom<&str> for OutputFormat {
+    type Error = Error;
+
+    fn try_from(s: &str) -> Result<Self, Self::Error> {
+        match s.to_lowercase().as_str() {
+            "json_lines" | "jsonl" | "jsonlines" => 
Ok(OutputFormat::JsonLines),
+            "json_array" => Ok(OutputFormat::JsonArray),
+            "raw" => Ok(OutputFormat::Raw),
+            other => Err(Error::InvalidConfigValue(format!(
+                "Unknown output format: '{other}'. Expected: json_lines, 
json_array, or raw"
+            ))),
+        }
+    }
+}
+
+impl OutputFormat {
+    pub fn file_extension(&self) -> &'static str {
+        match self {
+            OutputFormat::JsonLines => "jsonl",
+            OutputFormat::JsonArray => "json",
+            OutputFormat::Raw => "bin",
+        }
+    }
+}
+
+pub struct S3Sink {
+    id: u32,
+    config: S3SinkConfig,
+    bucket: Option<Box<s3::Bucket>>,
+    buffers: DashMap<BufferKey, Arc<tokio::sync::Mutex<buffer::FileBuffer>>>,
+    max_file_size_bytes: u64,
+    max_messages: u64,
+    output_format: OutputFormat,
+    state: tokio::sync::Mutex<SinkState>,
+    retry_delay: std::time::Duration,
+}
+
+impl fmt::Debug for S3Sink {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("S3Sink")
+            .field("id", &self.id)
+            .field("config", &self.config)
+            .field("bucket", &self.bucket.as_ref().map(|b| &b.name))
+            .field("buffers_count", &self.buffers.len())
+            .field("max_file_size_bytes", &self.max_file_size_bytes)
+            .field("max_messages", &self.max_messages)
+            .field("output_format", &self.output_format)
+            .field("retry_delay", &self.retry_delay)
+            .finish()
+    }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub(crate) struct BufferKey {
+    pub stream: String,
+    pub topic: String,
+    pub partition_id: u32,
+}
+
+#[derive(Debug)]
+struct SinkState {
+    messages_received: u64,
+    messages_uploaded: u64,
+    messages_lost: u64,
+}
+
+impl S3Sink {

Review Comment:
   resolved fields (`max_file_size_bytes`, `max_messages`, `output_format`, 
`retry_delay`, `max_attempts`) now live in `ResolvedConfig`, constructed only 
inside `validate_and_parse_config()` called from `open()`; accessing before 
open panics with a clear `BUG` message



-- 
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