forked from GoogleCloudPlatform/gcsfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
garbage_collect.go
133 lines (111 loc) · 2.92 KB
/
garbage_collect.go
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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcsx
import (
"fmt"
"sync/atomic"
"time"
"golang.org/x/net/context"
"github.com/jacobsa/gcloud/gcs"
"github.com/jacobsa/gcloud/gcs/gcsutil"
"github.com/jacobsa/syncutil"
"github.com/trungtran0689/gcsfuse/internal/logger"
)
func garbageCollectOnce(
ctx context.Context,
tmpObjectPrefix string,
bucket gcs.Bucket) (objectsDeleted uint64, err error) {
const stalenessThreshold = 30 * time.Minute
b := syncutil.NewBundle(ctx)
// List all objects with the temporary prefix.
objects := make(chan *gcs.Object, 100)
b.Add(func(ctx context.Context) (err error) {
defer close(objects)
err = gcsutil.ListPrefix(ctx, bucket, tmpObjectPrefix, objects)
if err != nil {
err = fmt.Errorf("ListPrefix: %w", err)
return
}
return
})
// Filter to the names of objects that are stale.
now := time.Now()
staleNames := make(chan string, 100)
b.Add(func(ctx context.Context) (err error) {
defer close(staleNames)
for o := range objects {
if now.Sub(o.Updated) < stalenessThreshold {
continue
}
select {
case <-ctx.Done():
err = ctx.Err()
return
case staleNames <- o.Name:
}
}
return
})
// Delete those objects.
b.Add(func(ctx context.Context) (err error) {
for name := range staleNames {
err = bucket.DeleteObject(
ctx,
&gcs.DeleteObjectRequest{
Name: name,
})
if err != nil {
err = fmt.Errorf("DeleteObject(%q): %w", name, err)
return
}
atomic.AddUint64(&objectsDeleted, 1)
}
return
})
err = b.Join()
return
}
// Periodically delete stale temporary objects from the supplied bucket until
// the context is cancelled.
func garbageCollect(
ctx context.Context,
tmpObjectPrefix string,
bucket gcs.Bucket) {
const period = 10 * time.Minute
ticker := time.NewTicker(period)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
logger.Info("Starting a garbage collection run.")
startTime := time.Now()
objectsDeleted, err := garbageCollectOnce(ctx, tmpObjectPrefix, bucket)
if err != nil {
logger.Infof(
"Garbage collection failed after deleting %d objects in %v, "+
"with error: %v",
objectsDeleted,
time.Since(startTime),
err)
} else {
logger.Infof(
"Garbage collection succeeded after deleted %d objects in %v.",
objectsDeleted,
time.Since(startTime))
}
}
}