Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: separate gc and convert #180

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Desiki-high marked this conversation as resolved.
Show resolved Hide resolved
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