File tree Expand file tree Collapse file tree 5 files changed +61
-5
lines changed Expand file tree Collapse file tree 5 files changed +61
-5
lines changed Original file line number Diff line number Diff line change @@ -321,6 +321,9 @@ type GCConfig struct {
321
321
322
322
// TTL is the ttl for job.
323
323
TTL time.Duration `yaml:"ttl" mapstructure:"ttl"`
324
+
325
+ // BatchSize is the batch size when operating gorm database.
326
+ BatchSize int `yaml:"batchSize" mapstructure:"batchSize"`
324
327
}
325
328
326
329
type PreheatConfig struct {
@@ -442,8 +445,9 @@ func New() *Config {
442
445
},
443
446
Job : JobConfig {
444
447
GC : GCConfig {
445
- Interval : DefaultJobGCInterval ,
446
- TTL : DefaultJobGCTTL ,
448
+ Interval : DefaultJobGCInterval ,
449
+ TTL : DefaultJobGCTTL ,
450
+ BatchSize : DefaultJobGCBatchSize ,
447
451
},
448
452
Preheat : PreheatConfig {
449
453
RegistryTimeout : DefaultJobPreheatRegistryTimeout ,
@@ -629,6 +633,10 @@ func (cfg *Config) Validate() error {
629
633
return errors .New ("gc requires parameter ttl" )
630
634
}
631
635
636
+ if cfg .Job .GC .BatchSize == 0 {
637
+ return errors .New ("gc requires parameter batchSize" )
638
+ }
639
+
632
640
if cfg .Job .Preheat .RegistryTimeout == 0 {
633
641
return errors .New ("preheat requires parameter registryTimeout" )
634
642
}
Original file line number Diff line number Diff line change @@ -178,8 +178,9 @@ func TestConfig_Load(t *testing.T) {
178
178
},
179
179
Job : JobConfig {
180
180
GC : GCConfig {
181
- Interval : 1 * time .Second ,
182
- TTL : 1 * time .Second ,
181
+ Interval : 1 * time .Second ,
182
+ TTL : 1 * time .Second ,
183
+ BatchSize : 100 ,
183
184
},
184
185
Preheat : PreheatConfig {
185
186
RegistryTimeout : DefaultJobPreheatRegistryTimeout ,
@@ -765,6 +766,21 @@ func TestConfig_Validate(t *testing.T) {
765
766
assert .EqualError (err , "gc requires parameter ttl" )
766
767
},
767
768
},
769
+ {
770
+ name : "gc requires parameter batchSize" ,
771
+ config : New (),
772
+ mock : func (cfg * Config ) {
773
+ cfg .Auth .JWT = mockJWTConfig
774
+ cfg .Database .Type = DatabaseTypeMysql
775
+ cfg .Database .Mysql = mockMysqlConfig
776
+ cfg .Database .Redis = mockRedisConfig
777
+ cfg .Job .GC .BatchSize = 0
778
+ },
779
+ expect : func (t * testing.T , err error ) {
780
+ assert := assert .New (t )
781
+ assert .EqualError (err , "gc requires parameter batchSize" )
782
+ },
783
+ },
768
784
{
769
785
name : "preheat requires parameter registryTimeout" ,
770
786
config : New (),
Original file line number Diff line number Diff line change @@ -93,6 +93,9 @@ const (
93
93
// DefaultJobGCTTL is the default ttl for job.
94
94
DefaultJobGCTTL = 12 * time .Hour
95
95
96
+ // DefaultJobGCBatchSize is the default batch size for operating on the database in gc job.
97
+ DefaultJobGCBatchSize = 5000
98
+
96
99
// DefaultJobPreheatRegistryTimeout is the default timeout for requesting registry to get token and manifest.
97
100
DefaultJobPreheatRegistryTimeout = 1 * time .Minute
98
101
Original file line number Diff line number Diff line change 72
72
gc :
73
73
interval : 1s
74
74
ttl : 1s
75
+ batchSize : 100
75
76
preheat :
76
77
registryTimeout : 1m
77
78
tls :
Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ func (gc *gc) Serve() {
61
61
select {
62
62
case <- tick .C :
63
63
logger .Infof ("gc job started" )
64
- if err := gc .db . WithContext (context .Background ()). Where ( "created_at < ?" , time . Now (). Add ( - gc . config . Job . GC . TTL )). Unscoped (). Delete ( & models. Job {}). Error ; err != nil {
64
+ if err := gc .deleteInBatches (context .Background ()); err != nil {
65
65
logger .Errorf ("gc job failed: %v" , err )
66
66
}
67
67
case <- gc .done :
@@ -74,3 +74,31 @@ func (gc *gc) Serve() {
74
74
func (gc * gc ) Stop () {
75
75
close (gc .done )
76
76
}
77
+
78
+ // deleteInBatches deletes jobs in batches.
79
+ func (gc * gc ) deleteInBatches (ctx context.Context ) error {
80
+ for {
81
+ jobs := make ([]models.Job , 0 , gc .config .Job .GC .BatchSize )
82
+ if err := gc .db .WithContext (ctx ).Where ("created_at < ?" , time .Now ().Add (- gc .config .Job .GC .TTL )).Limit (gc .config .Job .GC .BatchSize ).Find (& jobs ).Error ; err != nil {
83
+ return err
84
+ }
85
+
86
+ if len (jobs ) == 0 {
87
+ logger .Infof ("gc job finished" )
88
+ break
89
+ }
90
+
91
+ var ids []uint
92
+ for _ , job := range jobs {
93
+ ids = append (ids , job .ID )
94
+ }
95
+
96
+ if err := gc .db .WithContext (ctx ).Where ("id IN ?" , ids ).Unscoped ().Delete (& models.Job {}).Error ; err != nil {
97
+ return err
98
+ }
99
+
100
+ logger .Infof ("deleted %d jobs" , len (jobs ))
101
+ }
102
+
103
+ return nil
104
+ }
You can’t perform that action at this time.
0 commit comments