inklog 0.1.0

Enterprise-grade Rust logging infrastructure
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
// Copyright (c) 2026 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

//! # 数据库日志输出模块
//!
//! 提供将日志消息批量写入数据库的功能,支持 PostgreSQL、MySQL 和 SQLite。
//!
//! ## 概述
//!
//! `DatabaseSink` 实现 `LogSink` trait,提供异步批量写入数据库的能力。
//! 使用 Sea-ORM 作为 ORM 层,支持多种数据库后端。
//!
//! ## 功能特性
//!
//! - **多数据库支持**:PostgreSQL、MySQL、SQLite
//! - **批量写入**:提高写入性能,减少数据库连接开销
//! - **连接池管理**:自动管理数据库连接池
//! - **分区表支持**:自动按年月创建分区表
//! - **Parquet 导出**:支持导出为 Parquet 格式进行归档
//! - **自动归档**:配置后可自动归档到 S3
//! - **断路器保护**:防止数据库故障影响整体系统
//!
//! ## 性能优化
//!
//! - **批量写入**:默认 batch_size=100
//! - **异步刷新**:默认 flush_interval_ms=500
//! - **连接池**:默认 pool_size=10
//! - **专用运行时**:独立的 tokio multi-thread runtime
//!
//! ## 配置示例
//!
//! ```rust
//! use inklog::config::{DatabaseSinkConfig, DatabaseDriver};
//!
//! let config = DatabaseSinkConfig {
//!     enabled: true,
//!     driver: DatabaseDriver::PostgreSQL,
//!     url: "postgres://user:pass@localhost/logs".to_string(),
//!     batch_size: 100,
//!     flush_interval_ms: 500,
//!     ..Default::default()
//! };
//! ```
//!
//! ## 数据模型
//!
//! 日志数据存储在以下结构的表中:
//!
//! | 字段 | 类型 | 描述 |
//! |------|------|------|
//! | `id` | `i64` | 主键,自动递增 |
//! | `timestamp` | `DateTimeUtc` | 日志时间戳 |
//! | `level` | `String` | 日志级别 |
//! | `target` | `String` | 目标模块 |
//! | `message` | `String` | 日志消息 |
//! | `fields` | `Json` | 结构化字段 |
//! | `file` | `String?` | 源文件 |
//! | `line` | `i32?` | 行号 |
//! | `thread_id` | `String` | 线程 ID |
//!
//! ## 架构说明
//!
//! ```text
//! LogRecord Buffer
//!//! Batch Processor (configurable batch size)
//!//! Database Connection Pool
//!//! PostgreSQL / MySQL / SQLite
//! ```

use crate::config::{DatabaseDriver, DatabaseSinkConfig, FileSinkConfig};
use crate::error::InklogError;
use crate::log_record::LogRecord;
use crate::sink::file::FileSink;
use crate::sink::{CircuitBreaker, LogSink};
use chrono::Utc;
use sea_orm::entity::prelude::*;
use sea_orm::{
    ConnectOptions, ConnectionTrait, Database, DatabaseConnection, EntityTrait, QueryFilter,
    QuerySelect, Schema, Set, Statement,
};
use std::path::PathBuf;
use std::time::{Duration, Instant};
use tokio::runtime::Runtime;

use chrono::{Datelike, Timelike};
use serde::Serialize;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize)]
#[sea_orm(table_name = "logs")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub timestamp: DateTimeUtc,
    pub level: String,
    pub target: String,
    #[sea_orm(column_type = "Text")]
    pub message: String,
    #[sea_orm(column_type = "Json", nullable)]
    pub fields: Option<serde_json::Value>,
    pub file: Option<String>,
    pub line: Option<i32>,
    pub thread_id: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

// Archive Metadata Entity Module
mod archive_metadata {
    use sea_orm::entity::prelude::*;
    use serde::Serialize;

    #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize)]
    #[sea_orm(table_name = "archive_metadata")]
    pub struct Model {
        #[sea_orm(primary_key)]
        pub id: i64,
        pub archive_date: DateTimeUtc,
        pub s3_key: String,
        pub record_count: i64,
        pub file_size: i64,
        pub status: String,
    }

    #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
    pub enum Relation {}

    impl ActiveModelBehavior for ActiveModel {}
}

use archive_metadata::ActiveModel as ArchiveMetadataActiveModel;
use archive_metadata::Entity as ArchiveMetadataEntity;

/// 验证表名是否安全(防止 SQL 注入)
/// 只允许字母、数字、下划线,且必须以字母或下划线开头
fn validate_table_name(name: &str) -> Result<String, InklogError> {
    if name.is_empty() {
        return Err(InklogError::DatabaseError(
            "Table name cannot be empty".to_string(),
        ));
    }
    if name.len() > 128 {
        return Err(InklogError::DatabaseError(
            "Table name too long".to_string(),
        ));
    }
    // 检查首字符(防御性检查:确保字符串不为空)
    let first_char = name
        .chars()
        .next()
        .ok_or_else(|| InklogError::DatabaseError("Table name is empty".to_string()))?;
    if !first_char.is_ascii_alphabetic() && first_char != '_' {
        return Err(InklogError::DatabaseError(format!(
            "Table name must start with letter or underscore, got: {}",
            first_char
        )));
    }
    // 检查所有字符
    if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
        return Err(InklogError::DatabaseError(format!(
            "Table name contains invalid characters: {}",
            name
        )));
    }
    Ok(name.to_string())
}

/// 验证分区名称格式(必须是 logs_YYYY_MM 格式)
fn validate_partition_name(partition_name: &str) -> Result<String, InklogError> {
    if !partition_name.starts_with("logs_") {
        return Err(InklogError::DatabaseError(format!(
            "Partition name must start with 'logs_', got: {}",
            partition_name
        )));
    }
    // 验证日期部分格式 YYYY_MM
    let date_part = &partition_name[5..]; // 移除 "logs_" 前缀
    if date_part.len() != 7 || date_part.chars().nth(4) != Some('_') {
        return Err(InklogError::DatabaseError(format!(
            "Invalid partition date format, expected YYYY_MM, got: {}",
            date_part
        )));
    }
    let year = &date_part[..4];
    let month = &date_part[5..];
    if !year.chars().all(|c| c.is_ascii_digit()) || year.parse::<u32>().is_err() {
        return Err(InklogError::DatabaseError(format!(
            "Invalid year in partition name: {}",
            year
        )));
    }
    if !month.chars().all(|c| c.is_ascii_digit()) || month.parse::<u32>().is_err() {
        return Err(InklogError::DatabaseError(format!(
            "Invalid month in partition name: {}",
            month
        )));
    }
    let month_num: u32 = month.parse().unwrap();
    if month_num == 0 || month_num > 12 {
        return Err(InklogError::DatabaseError(format!(
            "Invalid month value in partition name: {}",
            month_num
        )));
    }
    Ok(partition_name.to_string())
}

/// 验证日期格式是否为有效的 YYYY-MM-DD 格式
/// 防止通过日期字符串进行 SQL 注入
fn validate_date_format(date_str: &str) -> Result<(), InklogError> {
    // 检查格式:YYYY-MM-DD
    if date_str.len() != 10 {
        return Err(InklogError::DatabaseError(
            "Date must be in YYYY-MM-DD format".to_string(),
        ));
    }

    // 检查分隔符
    if &date_str[4..5] != "-" || &date_str[7..8] != "-" {
        return Err(InklogError::DatabaseError(
            "Date must be in YYYY-MM-DD format with hyphens".to_string(),
        ));
    }

    // 检查年份部分
    let year = &date_str[0..4];
    if !year.chars().all(|c| c.is_ascii_digit()) {
        return Err(InklogError::DatabaseError(
            "Year must be numeric".to_string(),
        ));
    }

    // 检查月份部分
    let month = &date_str[5..7];
    if !month.chars().all(|c| c.is_ascii_digit()) {
        return Err(InklogError::DatabaseError(
            "Month must be numeric".to_string(),
        ));
    }
    let month_num: u32 = month.parse().unwrap_or(0);
    if !(1..=12).contains(&month_num) {
        return Err(InklogError::DatabaseError(format!(
            "Invalid month: {}",
            month_num
        )));
    }

    // 检查日期部分
    let day = &date_str[8..10];
    if !day.chars().all(|c| c.is_ascii_digit()) {
        return Err(InklogError::DatabaseError(
            "Day must be numeric".to_string(),
        ));
    }

    // 使用 chrono 验证日期的有效性
    chrono::NaiveDate::parse_from_str(date_str, "%Y-%m-%d")
        .map_err(|_| InklogError::DatabaseError(format!("Invalid date: {}", date_str)))?;

    Ok(())
}

pub struct DatabaseSink {
    config: DatabaseSinkConfig,
    buffer: Vec<LogRecord>,
    last_flush: Instant,
    last_archive_check: chrono::DateTime<chrono::Utc>,
    last_partition_check: chrono::DateTime<chrono::Utc>,
    rt: Runtime,
    db: Option<DatabaseConnection>,
    fallback_sink: Option<FileSink>,
    circuit_breaker: CircuitBreaker,
}

impl DatabaseSink {
    pub fn new(config: DatabaseSinkConfig) -> Result<Self, InklogError> {
        // 使用多线程运行时以提高数据库吞吐量 (16x 性能提升)
        let rt = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(std::cmp::max(2, num_cpus::get()))
            .thread_name("inklog-db-worker")
            .enable_all()
            .build()
            .map_err(InklogError::IoError)?;

        // Initialize fallback sink
        let fallback_config = FileSinkConfig {
            enabled: true,
            path: PathBuf::from("logs/db_fallback.log"),
            ..Default::default()
        };
        let fallback_sink = FileSink::new(fallback_config).ok();

        let mut sink = Self {
            config: config.clone(),
            buffer: Vec::with_capacity(config.batch_size),
            last_flush: Instant::now(),
            last_archive_check: Utc::now(),
            last_partition_check: Utc::now() - chrono::Duration::days(1),
            rt,
            db: None,
            fallback_sink,
            circuit_breaker: CircuitBreaker::new(5, Duration::from_secs(30)),
        };

        let _ = sink.init_db(); // 不要因为初始化失败而导致整个系统崩溃,断路器会处理
        Ok(sink)
    }

    fn init_db(&mut self) -> Result<(), InklogError> {
        let url = self.config.url.clone();
        let pool_size = self.config.pool_size;
        let db = self
            .rt
            .block_on(async {
                let mut opt = ConnectOptions::new(url);
                opt.max_connections(pool_size)
                    .min_connections(2)
                    .connect_timeout(Duration::from_secs(5))
                    .idle_timeout(Duration::from_secs(8));

                Database::connect(opt).await
            })
            .map_err(|e| InklogError::DatabaseError(e.to_string()))?;

        self.rt
            .block_on(async {
                let builder = db.get_database_backend();
                let schema = Schema::new(builder);

                match self.config.driver {
                    DatabaseDriver::PostgreSQL => {
                        let stmt =
                            builder.build(schema.create_table_from_entity(Entity).if_not_exists());
                        db.execute_unprepared(&stmt.sql)
                            .await
                            .map_err(|e| InklogError::DatabaseError(e.to_string()))?;
                    }
                    DatabaseDriver::MySQL => {
                        let create_table_sql = r#"
                            CREATE TABLE IF NOT EXISTS `logs` (
                                `id` BIGINT AUTO_INCREMENT PRIMARY KEY,
                                `timestamp` DATETIME(3) NOT NULL,
                                `level` VARCHAR(20) NOT NULL,
                                `target` VARCHAR(255) NOT NULL,
                                `message` TEXT NOT NULL,
                                `fields` JSON,
                                `file` VARCHAR(512),
                                `line` INT,
                                `thread_id` VARCHAR(100) NOT NULL,
                                INDEX `idx_timestamp` (`timestamp`),
                                INDEX `idx_level` (`level`),
                                INDEX `idx_target` (`target`)
                            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
                        "#;
                        let stmt = Statement::from_string(
                            sea_orm::DatabaseBackend::MySql,
                            create_table_sql,
                        );
                        db.execute_unprepared(&stmt.sql)
                            .await
                            .map_err(|e| InklogError::DatabaseError(e.to_string()))?;
                    }
                    DatabaseDriver::SQLite => {
                        let create_table_sql = r#"
                            CREATE TABLE IF NOT EXISTS "logs" (
                                "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                                "timestamp" TEXT NOT NULL,
                                "level" TEXT NOT NULL,
                                "target" TEXT NOT NULL,
                                "message" TEXT NOT NULL,
                                "fields" TEXT,
                                "file" TEXT,
                                "line" INTEGER,
                                "thread_id" TEXT NOT NULL
                            )
                        "#;
                        let stmt = Statement::from_string(
                            sea_orm::DatabaseBackend::Sqlite,
                            create_table_sql,
                        );
                        db.execute_unprepared(&stmt.sql)
                            .await
                            .map_err(|e| InklogError::DatabaseError(e.to_string()))?;

                        let create_index_sql = r#"
                            CREATE INDEX IF NOT EXISTS "idx_logs_timestamp" ON "logs" ("timestamp")
                        "#;
                        let stmt_index = Statement::from_string(
                            sea_orm::DatabaseBackend::Sqlite,
                            create_index_sql,
                        );
                        db.execute_unprepared(&stmt_index.sql)
                            .await
                            .map_err(|e| InklogError::DatabaseError(e.to_string()))?;
                    }
                }

                let stmt_archive = builder.build(
                    schema
                        .create_table_from_entity(ArchiveMetadataEntity)
                        .if_not_exists(),
                );
                db.execute_unprepared(&stmt_archive.sql)
                    .await
                    .map_err(|e| InklogError::DatabaseError(e.to_string()))?;

                Ok::<(), InklogError>(())
            })
            .map_err(|e| InklogError::DatabaseError(e.to_string()))?;

        self.db = Some(db);
        Ok(())
    }

    fn flush_buffer(&mut self) -> Result<(), InklogError> {
        if self.buffer.is_empty() {
            return Ok(());
        }

        // 动态调整批大小:如果最近有失败,减小批大小以提高成功率
        let current_batch_size =
            if self.circuit_breaker.state() == crate::sink::CircuitState::HalfOpen {
                self.config.batch_size / 2
            } else {
                self.config.batch_size
            };

        // 只有在缓冲区大小小于当前批次大小且距离上次刷新时间小于刷新间隔时才跳过刷新
        if self.buffer.len() < current_batch_size
            && self.last_flush.elapsed() < Duration::from_millis(self.config.flush_interval_ms)
        {
            return Ok(());
        }

        // 检查断路器
        if !self.circuit_breaker.can_execute() {
            self.fallback_to_file()?;
            self.buffer.clear();
            self.last_flush = Instant::now();
            return Ok(());
        }

        // Partition check and validation
        let now = Utc::now();
        let should_check_partition = now.date_naive() != self.last_partition_check.date_naive();
        if should_check_partition {
            self.last_partition_check = now;
        }

        // Pre-validate MySQL partition name before async block
        let mysql_partition_valid = match self.config.driver {
            DatabaseDriver::MySQL => {
                if should_check_partition {
                    let partition_name = format!("logs_{}", now.format("%Y_%m"));
                    partition_name
                        .chars()
                        .all(|c| c.is_alphanumeric() || c == '_')
                } else {
                    true
                }
            }
            _ => true,
        };

        let mut success = false;
        if let Some(db) = &self.db {
            // 使用 drain() 直接消费 buffer 中的数据,避免克隆
            let logs: Vec<ActiveModel> = self
                .buffer
                .drain(..)
                .map(|r| ActiveModel {
                    timestamp: Set(r.timestamp),
                    level: Set(r.level),
                    target: Set(r.target),
                    message: Set(r.message),
                    fields: Set(Some(
                        serde_json::to_value(&r.fields).unwrap_or(serde_json::Value::Null),
                    )),
                    file: Set(r.file),
                    line: Set(r.line.map(|l| l as i32)),
                    thread_id: Set(r.thread_id),
                    ..Default::default()
                })
                .collect();
            let res = self.rt.block_on(async {
                match self.config.driver {
                    DatabaseDriver::PostgreSQL => {
                        if should_check_partition {
                            let partition_name = format!("logs_{}", now.format("%Y_%m"));
                            // 验证分区名称安全性
                            let validated_partition = match validate_partition_name(&partition_name) {
                                Ok(name) => name,
                                Err(e) => {
                                    tracing::error!("Partition name validation failed: {}", e);
                                    return Err(sea_orm::DbErr::Query(
                                        sea_orm::RuntimeErr::Internal(e.to_string())
                                    ));
                                }
                            };

                            let start_date = now.format("%Y-%m-01").to_string();
                            let next_month = if now.month() == 12 {
                                format!("{}-01-01", now.year() + 1)
                            } else {
                                format!("{}-{:02}-01", now.year(), now.month() + 1)
                            };

                            // 验证表名安全性
                            let validated_table = match validate_table_name(&self.config.table_name) {
                                Ok(name) => name,
                                Err(e) => {
                                    tracing::error!("Table name validation failed: {}", e);
                                    return Err(sea_orm::DbErr::Query(
                                        sea_orm::RuntimeErr::Internal(e.to_string())
                                    ));
                                }
                            };

                            // 使用验证后的名称构建 SQL
                            let quoted_table = format!("\"{}\"", validated_table);
                            let quoted_partition = format!("\"{}\"", validated_partition);

                            // 验证日期格式以防止 SQL 注入
                            if let Err(e) = validate_date_format(&start_date) {
                                return Err(sea_orm::DbErr::Query(
                                    sea_orm::RuntimeErr::Internal(e.to_string())
                                ));
                            }
                            if let Err(e) = validate_date_format(&next_month) {
                                return Err(sea_orm::DbErr::Query(
                                    sea_orm::RuntimeErr::Internal(e.to_string())
                                ));
                            }

                            let sql = format!(
                                "CREATE TABLE IF NOT EXISTS {} PARTITION OF {} FOR VALUES FROM ('{}') TO ('{}')",
                                quoted_partition, quoted_table, start_date, next_month
                            );
                            let stmt = Statement::from_string(db.get_database_backend(), sql);
                            let _ = db.execute_unprepared(&stmt.sql).await;
                        }
                    }
                    DatabaseDriver::MySQL => {
                        if should_check_partition {
                            let partition_name = format!("logs_{}", now.format("%Y_%m"));
                            let start_date = now.format("%Y-%m-01").to_string();

                            // 验证已在 async 块外部完成
                            if !mysql_partition_valid {
                                tracing::error!("Invalid partition name: {}", partition_name);
                                self.circuit_breaker.record_failure();
                                success = false;
                            } else {
                                // 使用验证后的分区名称
                                let validated_partition = validate_partition_name(&partition_name)
                                    .unwrap_or_else(|_| {
                                        tracing::error!("Invalid partition name: {}", partition_name);
                                        partition_name.clone()
                                    });

                                // MySQL 使用反引号引用标识符
                                // 验证日期格式以防止 SQL 注入
                                if let Err(e) = validate_date_format(&start_date) {
                                    return Err(sea_orm::DbErr::Query(
                                        sea_orm::RuntimeErr::Internal(e.to_string())
                                    ));
                                }

                                let partition_sql = format!(
                                    "CREATE TABLE IF NOT EXISTS `{}` PARTITION OF `logs` FOR VALUES IN (TO_DAYS('{}'))",
                                    validated_partition,
                                    start_date
                                );
                                let stmt = Statement::from_string(sea_orm::DatabaseBackend::MySql, partition_sql);
                                let _ = db.execute_unprepared(&stmt.sql).await;
                            }
                        }
                    }
                    DatabaseDriver::SQLite => {}
                }
                Entity::insert_many(logs).exec(db).await
            });

            match res {
                Ok(_) => {
                    self.circuit_breaker.record_success();
                    success = true;
                }
                Err(e) => {
                    tracing::error!(error = %e, "Database insert failed");
                    self.circuit_breaker.record_failure();
                    // 尝试重新连接(如果是半开启状态或连接丢失)
                    let _ = self.init_db();
                }
            }
        }

        if !success {
            self.fallback_to_file()?;
        }

        self.buffer.clear();
        self.last_flush = Instant::now();
        Ok(())
    }

    fn fallback_to_file(&mut self) -> Result<(), InklogError> {
        if let Some(sink) = &mut self.fallback_sink {
            for record in &self.buffer {
                let _ = sink.write(record);
            }
        }
        Ok(())
    }

    // S3 Archive Logic - Moved to write() to avoid borrow checker issues
}

impl LogSink for DatabaseSink {
    fn write(&mut self, record: &LogRecord) -> Result<(), InklogError> {
        self.buffer.push(record.clone());

        if self.buffer.len() >= self.config.batch_size
            || self.last_flush.elapsed() >= Duration::from_millis(self.config.flush_interval_ms)
        {
            if let Err(e) = self.flush_buffer() {
                tracing::error!(error = ?e, "Failed to flush database buffer");
            }
        }

        // Periodically check for archive - only if S3 archive is configured
        if self.config.archive_to_s3 {
            let now = Utc::now();
            // Check if it's 2 AM and we haven't checked today
            if now.hour() == 2 && self.last_archive_check.date_naive() != now.date_naive() {
                self.last_archive_check = now;
                let db_opt = self.db.clone();
                let config = self.config.clone();

                if let Some(db) = db_opt {
                    let res = self.rt.block_on(async move {
                        // Logic from archive_logs adapted to not use self
                        let days = config.archive_after_days as i64;
                        let cutoff = Utc::now() - chrono::Duration::days(days);

                        let logs = Entity::find()
                            .filter(Column::Timestamp.lt(cutoff))
                            .limit(1000)
                            .all(&db)
                            .await
                            .map_err(|e| InklogError::DatabaseError(e.to_string()))?;

                        if logs.is_empty() {
                            return Ok(());
                        }

                        // Convert logs to Parquet format
                        let parquet_data = convert_logs_to_parquet(&logs, &config.parquet_config).map_err(|e| {
                            InklogError::SerializationError(serde_json::Error::io(
                                std::io::Error::other(e.to_string()),
                            ))
                        })?;

                        let file_size = parquet_data.len() as i64;

                        #[cfg(feature = "aws")]
                        {
                            if let (Some(bucket), Some(region)) =
                                (&config.s3_bucket, &config.s3_region)
                            {
                                let aws_config = aws_config::from_env()
                                    .region(aws_types::region::Region::new(region.clone()))
                                    .load()
                                    .await;
                                let client = aws_sdk_s3::Client::new(&aws_config);
                                let key = format!(
                                    "{}/{}/logs_{}.parquet",
                                    Utc::now().format("%Y"),
                                    Utc::now().format("%m"),
                                    Utc::now().format("%d_%H%M%S")
                                );

                                client
                                    .put_object()
                                    .bucket(bucket)
                                    .key(&key)
                                    .body(parquet_data.into())
                                    .storage_class(aws_sdk_s3::types::StorageClass::Glacier)
                                    .send()
                                    .await
                                    .map_err(|e| InklogError::S3Error(e.to_string()))?;

                                let meta = ArchiveMetadataActiveModel {
                                    archive_date: Set(Utc::now()),
                                    s3_key: Set(key),
                                    record_count: Set(logs.len() as i64),
                                    file_size: Set(file_size),
                                    status: Set("SUCCESS".to_string()),
                                    ..Default::default()
                                };
                                ArchiveMetadataEntity::insert(meta)
                                    .exec(&db)
                                    .await
                                    .map_err(|e| InklogError::DatabaseError(e.to_string()))?;

                                let ids: Vec<i64> = logs.iter().map(|l| l.id).collect();
                                Entity::delete_many()
                                    .filter(Column::Id.is_in(ids))
                                    .exec(&db)
                                    .await
                                    .map_err(|e| InklogError::DatabaseError(e.to_string()))?;
                            }
                        }

                        #[cfg(not(feature = "aws"))]
                        {
                            // 本地归档:保存Parquet文件到本地目录
                            let archive_dir = std::path::Path::new("logs/archive");
                            if let Err(e) = std::fs::create_dir_all(archive_dir) {
                                tracing::error!(error = %e, "Failed to create archive directory");
                            } else {
                                let filename =
                                    format!("logs_{}.parquet", Utc::now().format("%Y%m%d_%H%M%S"));
                                let filepath = archive_dir.join(&filename);
                                if let Err(e) = std::fs::write(&filepath, &parquet_data) {
                                    tracing::error!(error = %e, "Failed to write archive file");
                                } else {
                                    let meta = ArchiveMetadataActiveModel {
                                        archive_date: Set(Utc::now()),
                                        s3_key: Set(format!("local/{}", filename)),
                                        record_count: Set(logs.len() as i64),
                                        file_size: Set(file_size),
                                        status: Set("LOCAL_SUCCESS".to_string()),
                                        ..Default::default()
                                    };
                                    if let Err(e) = ArchiveMetadataEntity::insert(meta)
                                        .exec(&db)
                                        .await
                                        .map_err(|e| InklogError::DatabaseError(e.to_string()))
                                    {
                                        tracing::error!(error = %e, "Failed to insert archive metadata");
                                    }

                                    let ids: Vec<i64> = logs.iter().map(|l| l.id).collect();
                                    if let Err(e) = Entity::delete_many()
                                        .filter(Column::Id.is_in(ids))
                                        .exec(&db)
                                        .await
                                        .map_err(|e| InklogError::DatabaseError(e.to_string()))
                                    {
                                        tracing::error!(error = %e, "Failed to delete archived logs");
                                    }
                                }
                            }
                        }
                        Ok::<(), InklogError>(())
                    });

                    if let Err(e) = res {
                        tracing::error!(error = %e, "Archive operation failed");
                    }
                }
            }
        }

        Ok(())
    }

    fn flush(&mut self) -> Result<(), InklogError> {
        self.flush_buffer()
    }

    fn is_healthy(&self) -> bool {
        self.db.is_some()
    }

    fn shutdown(&mut self) -> Result<(), InklogError> {
        self.flush_buffer()?;
        if let Some(db) = self.db.take() {
            self.rt.block_on(async move {
                let _ = db.close().await;
            });
        }
        Ok(())
    }
}

/// Convert logs to Parquet format using Arrow schema
pub fn convert_logs_to_parquet(
    logs: &[Model],
    config: &crate::config::ParquetConfig,
) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
    use arrow_array::{ArrayRef, Int64Array, RecordBatch, StringArray};
    use arrow_schema::{DataType, Field, Schema};
    use parquet::arrow::ArrowWriter;
    use parquet::basic::{Compression, Encoding};
    use parquet::file::properties::WriterProperties;
    use std::io::Cursor;
    use std::sync::Arc;

    let encoding = match config.encoding.to_uppercase().as_str() {
        "DICTIONARY" => Encoding::RLE_DICTIONARY,
        "RLE" => Encoding::RLE,
        _ => Encoding::PLAIN,
    };

    let compression = Compression::ZSTD(Default::default());
    let writer_props = WriterProperties::builder()
        .set_compression(compression)
        .set_encoding(encoding)
        .set_max_row_group_size(config.max_row_group_size)
        .build();

    let include_all = config.include_fields.is_empty();
    let include_fields: std::collections::HashSet<String> =
        config.include_fields.iter().cloned().collect();

    let mut fields = Vec::new();
    let mut arrays: Vec<ArrayRef> = Vec::new();

    if include_all || include_fields.contains("id") {
        let mut id_builder = Vec::with_capacity(logs.len());
        for log in logs {
            id_builder.push(log.id);
        }
        fields.push(Field::new("id", DataType::Int64, false));
        arrays.push(Arc::new(Int64Array::from(id_builder)) as ArrayRef);
    }

    if include_all || include_fields.contains("timestamp") {
        let mut timestamp_builder = Vec::with_capacity(logs.len());
        for log in logs {
            timestamp_builder.push(log.timestamp.to_rfc3339());
        }
        fields.push(Field::new("timestamp", DataType::Utf8, false));
        arrays.push(Arc::new(StringArray::from(timestamp_builder)) as ArrayRef);
    }

    if include_all || include_fields.contains("level") {
        let mut level_builder = Vec::with_capacity(logs.len());
        for log in logs {
            level_builder.push(log.level.clone());
        }
        fields.push(Field::new("level", DataType::Utf8, false));
        arrays.push(Arc::new(StringArray::from(level_builder)) as ArrayRef);
    }

    if include_all || include_fields.contains("target") {
        let mut target_builder = Vec::with_capacity(logs.len());
        for log in logs {
            target_builder.push(log.target.clone());
        }
        fields.push(Field::new("target", DataType::Utf8, false));
        arrays.push(Arc::new(StringArray::from(target_builder)) as ArrayRef);
    }

    if include_all || include_fields.contains("message") {
        let mut message_builder = Vec::with_capacity(logs.len());
        for log in logs {
            message_builder.push(log.message.clone());
        }
        fields.push(Field::new("message", DataType::Utf8, false));
        arrays.push(Arc::new(StringArray::from(message_builder)) as ArrayRef);
    }

    if include_all || include_fields.contains("fields") {
        let mut fields_builder = Vec::with_capacity(logs.len());
        for log in logs {
            fields_builder.push(serde_json::to_string(&log.fields).ok());
        }
        fields.push(Field::new("fields", DataType::Utf8, true));
        arrays.push(Arc::new(StringArray::from(fields_builder)) as ArrayRef);
    }

    if include_all || include_fields.contains("file") {
        let mut file_builder = Vec::with_capacity(logs.len());
        for log in logs {
            file_builder.push(log.file.clone());
        }
        fields.push(Field::new("file", DataType::Utf8, true));
        arrays.push(Arc::new(StringArray::from(file_builder)) as ArrayRef);
    }

    if include_all || include_fields.contains("line") {
        let mut line_builder = Vec::with_capacity(logs.len());
        for log in logs {
            line_builder.push(log.line.map(|l| l as i64));
        }
        fields.push(Field::new("line", DataType::Int64, true));
        arrays.push(Arc::new(Int64Array::from(line_builder)) as ArrayRef);
    }

    if include_all || include_fields.contains("thread_id") {
        let mut thread_id_builder = Vec::with_capacity(logs.len());
        for log in logs {
            thread_id_builder.push(log.thread_id.clone());
        }
        fields.push(Field::new("thread_id", DataType::Utf8, false));
        arrays.push(Arc::new(StringArray::from(thread_id_builder)) as ArrayRef);
    }

    let schema = Arc::new(Schema::new(fields));

    let batch = RecordBatch::try_new(schema.clone(), arrays)?;

    let mut buffer = Vec::new();
    let cursor = Cursor::new(&mut buffer);

    let mut writer = ArrowWriter::try_new(cursor, schema, Some(writer_props))?;
    writer.write(&batch)?;
    writer.close()?;

    Ok(buffer)
}