Skip to content

Commit

Permalink
chore: separate gc and convert
Browse files Browse the repository at this point in the history
- GC doesn't return any error, change to the error log.
- Change gc call to ```goroutine```.

Signed-off-by: Yadong Ding <[email protected]>
  • Loading branch information
Desiki-high committed Sep 6, 2023
1 parent a7aa579 commit 664b67d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
10 changes: 3 additions & 7 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ func NewLocalAdapter(cfg *config.Config) (*LocalAdapter, error) {
func startScheduledGC(content *content.Content) {
ticker := time.NewTicker(time.Hour)
for range ticker.C {
if err := content.GC(namespaces.WithNamespace(context.Background(), "acceleration-service"), content.Threshold/2); err != nil {
logrus.Error(errors.Wrap(err, "scheduled gc task"))
}
content.GC(namespaces.WithNamespace(context.Background(), "acceleration-service"), content.Threshold/2)
}
}

Expand All @@ -126,14 +124,12 @@ func (adp *LocalAdapter) Convert(ctx context.Context, source string) error {
return err
}
adp.content.GcMutex.RLock()
defer adp.content.GcMutex.RUnlock()
if _, err = adp.cvt.Convert(ctx, source, target, cacheRef); err != nil {
adp.content.GcMutex.RUnlock()
return err
}
adp.content.GcMutex.RUnlock()
if err := adp.content.GC(ctx, adp.content.Threshold); err != nil {
return err
}
go adp.content.GC(ctx, adp.content.Threshold)
return nil
}

Expand Down
20 changes: 11 additions & 9 deletions pkg/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,27 +135,29 @@ func (content *Content) Size() (int64, error) {
}

// GC clean the local caches by cfg.Provider.GCPolicy configuration
func (content *Content) GC(ctx context.Context, threshold int64) error {
func (content *Content) GC(ctx context.Context, threshold int64) {
size, err := content.Size()
if err != nil {
return err
logrus.Error(errors.Wrap(err, "gc get content size"))
return
}
// if the local content size over eighty percent of threshold, gc start
if size > (threshold*gcPercent)/100 {
if _, err, _ := content.gcSingleflight.Do(accelerationServiceNamespace, func() (interface{}, error) {
content.gcSingleflight.Do(accelerationServiceNamespace, func() (interface{}, error) {
content.GcMutex.Lock()
defer content.GcMutex.Unlock()
// recalculate the local cache size
size, err := content.Size()
if err != nil {
return nil, err
logrus.Error(errors.Wrap(err, "gc get content size"))
return nil, nil
}
return nil, content.garbageCollect(ctx, size-(threshold*gcPercent)/100)
}); err != nil {
return err
}
if err := content.garbageCollect(ctx, size-(threshold*gcPercent)/100); err != nil {
logrus.Error(errors.Wrap(err, "gc"))
}
return nil, nil
})
}
return nil
}

// garbageCollect clean the local caches by lease
Expand Down

0 comments on commit 664b67d

Please sign in to comment.