|
@@ -180,17 +180,40 @@ func InitLogDB(batchSize int) error {
|
|
|
|
|
|
|
|
log.Info("log database migration started")
|
|
log.Info("log database migration started")
|
|
|
|
|
|
|
|
- err := migrateLOGDB(batchSize)
|
|
|
|
|
|
|
+ err := migrateLogDB(batchSize)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return fmt.Errorf("failed to migrate log database: %w", err)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // ignore migrate log error when use double database
|
|
|
|
|
+ if LogDB == DB {
|
|
|
|
|
+ return fmt.Errorf("failed to migrate log database: %w", err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.Errorf("failed to migrate log database: %v", err)
|
|
|
|
|
+ log.Warn("log database migration with backend started")
|
|
|
|
|
|
|
|
- log.Info("log database migrated")
|
|
|
|
|
|
|
+ go migrateLogDBBackend(batchSize)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.Info("log database migrated")
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func migrateLOGDB(batchSize int) error {
|
|
|
|
|
|
|
+func migrateLogDBBackend(batchSize int) {
|
|
|
|
|
+ ticker := time.NewTicker(time.Minute)
|
|
|
|
|
+ defer ticker.Stop()
|
|
|
|
|
+
|
|
|
|
|
+ for range ticker.C {
|
|
|
|
|
+ err := migrateLogDB(batchSize)
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.Errorf("failed to migrate log database: %v", err)
|
|
|
|
|
+ ticker.Reset(time.Minute)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func migrateLogDB(batchSize int) error {
|
|
|
// Pre-migration cleanup to remove expired data
|
|
// Pre-migration cleanup to remove expired data
|
|
|
err := preMigrationCleanup(batchSize)
|
|
err := preMigrationCleanup(batchSize)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -325,6 +348,15 @@ func preMigrationCleanup(batchSize int) error {
|
|
|
return fmt.Errorf("failed to cleanup logs: %w", err)
|
|
return fmt.Errorf("failed to cleanup logs: %w", err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Clean up retry logs
|
|
|
|
|
+ err = preMigrationCleanupRetryLogs(batchSize)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ if ignoreNoSuchTable(err) {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ return fmt.Errorf("failed to cleanup retry logs: %w", err)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Clean up request details
|
|
// Clean up request details
|
|
|
err = preMigrationCleanupRequestDetails(batchSize)
|
|
err = preMigrationCleanupRequestDetails(batchSize)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -342,7 +374,7 @@ func preMigrationCleanup(batchSize int) error {
|
|
|
// preMigrationCleanupLogs cleans up expired logs using ID-based batch deletion
|
|
// preMigrationCleanupLogs cleans up expired logs using ID-based batch deletion
|
|
|
func preMigrationCleanupLogs(batchSize int) error {
|
|
func preMigrationCleanupLogs(batchSize int) error {
|
|
|
logStorageHours := config.GetLogStorageHours()
|
|
logStorageHours := config.GetLogStorageHours()
|
|
|
- if logStorageHours <= 0 {
|
|
|
|
|
|
|
+ if logStorageHours == 0 {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -391,10 +423,70 @@ func preMigrationCleanupLogs(batchSize int) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// preMigrationCleanupRetryLogs cleans up expired logs using ID-based batch deletion
|
|
|
|
|
+func preMigrationCleanupRetryLogs(batchSize int) error {
|
|
|
|
|
+ logStorageHours := config.GetRetryLogStorageHours()
|
|
|
|
|
+ if logStorageHours == 0 {
|
|
|
|
|
+ logStorageHours = config.GetLogStorageHours()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if logStorageHours == 0 {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if batchSize <= 0 {
|
|
|
|
|
+ batchSize = defaultCleanLogBatchSize
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ cutoffTime := time.Now().Add(-time.Duration(logStorageHours) * time.Hour)
|
|
|
|
|
+
|
|
|
|
|
+ // First, get the IDs to delete
|
|
|
|
|
+ ids := make([]int, 0, batchSize)
|
|
|
|
|
+
|
|
|
|
|
+ for {
|
|
|
|
|
+ ids = ids[:0]
|
|
|
|
|
+
|
|
|
|
|
+ err := LogDB.Model(&RetryLog{}).
|
|
|
|
|
+ Select("id").
|
|
|
|
|
+ Where("created_at < ?", cutoffTime).
|
|
|
|
|
+ Limit(batchSize).
|
|
|
|
|
+ Find(&ids).Error
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // If no IDs found, we're done
|
|
|
|
|
+ if len(ids) == 0 {
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Delete by IDs
|
|
|
|
|
+ err = LogDB.Where("id IN (?)", ids).
|
|
|
|
|
+ Session(&gorm.Session{SkipDefaultTransaction: true}).
|
|
|
|
|
+ Delete(&Log{}).Error
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.Infof("deleted %d expired retry log records", len(ids))
|
|
|
|
|
+
|
|
|
|
|
+ // If we got less than batchSize, we're done
|
|
|
|
|
+ if len(ids) < batchSize {
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// preMigrationCleanupRequestDetails cleans up expired request details using ID-based batch deletion
|
|
// preMigrationCleanupRequestDetails cleans up expired request details using ID-based batch deletion
|
|
|
func preMigrationCleanupRequestDetails(batchSize int) error {
|
|
func preMigrationCleanupRequestDetails(batchSize int) error {
|
|
|
detailStorageHours := config.GetLogDetailStorageHours()
|
|
detailStorageHours := config.GetLogDetailStorageHours()
|
|
|
- if detailStorageHours <= 0 {
|
|
|
|
|
|
|
+ if detailStorageHours == 0 {
|
|
|
|
|
+ detailStorageHours = config.GetLogStorageHours()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if detailStorageHours == 0 {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|