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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
package streamcache
import (
"context"
"crypto/rand"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/internal/dontpanic"
"gitlab.com/gitlab-org/gitaly/internal/git/housekeeping"
)
var (
fileRemoveCounter = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "gitaly_streamcache_filestore_removed_total",
Help: "Number of files removed from streamcache via file walks",
},
[]string{"dir"},
)
diskUsageGauge = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "gitaly_streamcache_filestore_disk_usage_bytes",
Help: "Disk usage per filestore",
},
[]string{"dir"},
)
)
// Filestore creates temporary files in dir. These files get deleted once
// they are older (by mtime) than maxAge via a goroutine that does
// periodic file walks. To make these file walks efficient, we spread the
// temporary files across a balanced directory tree in subdirectories
// aa/my-temp-file where aa is a hexadecimal number. This means
// we eventually create exactly 256 subdirectories.
//
// Each instance of filestore carries a random token which is used as a
// prefix in the tempfiles it creates. This ensures that it is safe to
// have multiple instances of filestore use the same directory on disk;
// their tempfiles will not collide. There is one caveat: if multiple
// filestores share the same directory but have different maximum ages,
// then lowest maximum age becomes the effective maximum age for all of
// them.
type filestore struct {
dir string
maxAge time.Duration
m sync.Mutex
id []byte
counter uint64
stop chan struct{}
}
func newFilestore(dir string, maxAge time.Duration, sleep func(time.Duration), logger logrus.FieldLogger) *filestore {
fs := &filestore{
dir: dir,
maxAge: maxAge,
stop: make(chan struct{}),
}
dontpanic.GoForever(1*time.Minute, func() {
sleepLoop(fs.stop, fs.maxAge, sleep, func() {
diskUsageGauge.WithLabelValues(fs.dir).Set(fs.diskUsage())
if err := fs.cleanWalk(time.Now().Add(-fs.maxAge)); err != nil {
logger.WithError(err).Error("streamcache filestore cleanup")
}
})
})
return fs
}
type namedWriteCloser interface {
Name() string
io.WriteCloser
}
// Create creates a new tempfile. It does not use ioutil.TempFile because
// the documentation of TempFile makes no promises about reusing tempfile
// names after a file has been deleted. By using a very large (uint64)
// counter, Create makes it clear / explicit how unlikely reuse is.
func (fs *filestore) Create() (namedWriteCloser, error) {
if err := fs.ensureCacheID(); err != nil {
return nil, err
}
fileID := fs.nextFileID()
name := fmt.Sprintf("%x-%d",
// fs.id ensures uniqueness among other *filestore instances
fs.id,
// fileID ensures uniqueness (modulo roll-over) among other files
// created by this *filestore instance
fileID,
)
path := filepath.Join(fs.dir, fmt.Sprintf("%02x", uint8(fileID)), name)
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return nil, fmt.Errorf("Create: mkdir: %w", err)
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return nil, fmt.Errorf("Create: %w", err)
}
return f, nil
}
func (fs *filestore) ensureCacheID() error {
fs.m.Lock()
defer fs.m.Unlock()
if len(fs.id) == 0 {
buf := make([]byte, 10)
if _, err := io.ReadFull(rand.Reader, buf); err != nil {
return err
}
fs.id = buf
}
return nil
}
func (fs *filestore) nextFileID() uint64 {
fs.m.Lock()
defer fs.m.Unlock()
fs.counter++
return fs.counter
}
func (fs *filestore) Stop() {
fs.m.Lock()
defer fs.m.Unlock()
select {
case <-fs.stop:
default:
close(fs.stop)
}
}
// cleanWalk removes files but not directories. This is to avoid races
// when a directory looks empty but another goroutine is about to create
// a new file in it with fs.Create(). Because the number of directories
// is bounded by the directory scheme to 256, there is no need to remove
// the directories anyway.
func (fs *filestore) cleanWalk(cutoff time.Time) error {
// If a server reset has left some directories in a bad state, this will
// fix it.
if err := housekeeping.FixDirectoryPermissions(context.Background(), fs.dir); err != nil {
return err
}
return filepath.Walk(fs.dir, func(path string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() && info.ModTime().Before(cutoff) {
err = os.Remove(path)
fileRemoveCounter.WithLabelValues(fs.dir).Inc()
}
if os.IsNotExist(err) {
err = nil
}
return err
})
}
func (fs *filestore) diskUsage() float64 {
var total float64
_ = filepath.Walk(fs.dir, func(_ string, info os.FileInfo, _ error) error {
if info != nil {
total += float64(info.Size())
}
return nil
})
return total
}
|