forked from grafana/loki
/
fs_object_client.go
227 lines (187 loc) · 6 KB
/
fs_object_client.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package local
import (
"context"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/thanos-io/thanos/pkg/runutil"
"github.com/grafana/loki/pkg/ruler/rulestore/local"
"github.com/grafana/loki/pkg/storage/chunk/client"
"github.com/grafana/loki/pkg/storage/chunk/client/util"
util_log "github.com/grafana/loki/pkg/util/log"
)
// FSConfig is the config for a FSObjectClient.
type FSConfig struct {
Directory string `yaml:"directory"`
}
// RegisterFlags registers flags.
func (cfg *FSConfig) RegisterFlags(f *flag.FlagSet) {
cfg.RegisterFlagsWithPrefix("", f)
}
// RegisterFlags registers flags with prefix.
func (cfg *FSConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
f.StringVar(&cfg.Directory, prefix+"local.chunk-directory", "", "Directory to store chunks in.")
}
func (cfg *FSConfig) ToCortexLocalConfig() local.Config {
return local.Config{
Directory: cfg.Directory,
}
}
// FSObjectClient holds config for filesystem as object store
type FSObjectClient struct {
cfg FSConfig
pathSeparator string
}
// NewFSObjectClient makes a chunk.Client which stores chunks as files in the local filesystem.
func NewFSObjectClient(cfg FSConfig) (*FSObjectClient, error) {
// filepath.Clean cleans up the path by removing unwanted duplicate slashes, dots etc.
// This is needed because DeleteObject works on paths which are already cleaned up and it
// checks whether it is about to delete the configured directory when it becomes empty
cfg.Directory = filepath.Clean(cfg.Directory)
if err := util.EnsureDirectory(cfg.Directory); err != nil {
return nil, err
}
return &FSObjectClient{
cfg: cfg,
pathSeparator: string(os.PathSeparator),
}, nil
}
// Stop implements ObjectClient
func (FSObjectClient) Stop() {}
// GetObject from the store
func (f *FSObjectClient) GetObject(_ context.Context, objectKey string) (io.ReadCloser, int64, error) {
fl, err := os.Open(filepath.Join(f.cfg.Directory, filepath.FromSlash(objectKey)))
if err != nil {
return nil, 0, err
}
stats, err := fl.Stat()
if err != nil {
return nil, 0, err
}
return fl, stats.Size(), nil
}
// PutObject into the store
func (f *FSObjectClient) PutObject(_ context.Context, objectKey string, object io.ReadSeeker) error {
fullPath := filepath.Join(f.cfg.Directory, filepath.FromSlash(objectKey))
err := util.EnsureDirectory(filepath.Dir(fullPath))
if err != nil {
return err
}
fl, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer runutil.CloseWithLogOnErr(util_log.Logger, fl, "fullPath: %s", fullPath)
_, err = io.Copy(fl, object)
if err != nil {
return err
}
err = fl.Sync()
if err != nil {
return err
}
return fl.Close()
}
// List implements chunk.ObjectClient.
// FSObjectClient assumes that prefix is a directory, and only supports "" and "/" delimiters.
func (f *FSObjectClient) List(ctx context.Context, prefix, delimiter string) ([]client.StorageObject, []client.StorageCommonPrefix, error) {
if delimiter != "" && delimiter != "/" {
return nil, nil, fmt.Errorf("unsupported delimiter: %q", delimiter)
}
folderPath := filepath.Join(f.cfg.Directory, filepath.FromSlash(prefix))
info, err := os.Stat(folderPath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil, nil
}
return nil, nil, err
}
if !info.IsDir() {
// When listing single file, return this file only.
return []client.StorageObject{{Key: info.Name(), ModifiedAt: info.ModTime()}}, nil, nil
}
var storageObjects []client.StorageObject
var commonPrefixes []client.StorageCommonPrefix
err = filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore starting folder itself.
if path == folderPath {
return nil
}
relPath, err := filepath.Rel(f.cfg.Directory, path)
if err != nil {
return err
}
relPath = filepath.ToSlash(relPath)
if info.IsDir() {
if delimiter == "" {
// Go into directory
return nil
}
empty, err := isDirEmpty(path)
if err != nil {
return err
}
if !empty {
commonPrefixes = append(commonPrefixes, client.StorageCommonPrefix(relPath+delimiter))
}
return filepath.SkipDir
}
storageObjects = append(storageObjects, client.StorageObject{Key: relPath, ModifiedAt: info.ModTime()})
return nil
})
return storageObjects, commonPrefixes, err
}
func (f *FSObjectClient) DeleteObject(ctx context.Context, objectKey string) error {
// inspired from https://github.com/thanos-io/thanos/blob/55cb8ca38b3539381dc6a781e637df15c694e50a/pkg/objstore/filesystem/filesystem.go#L195
file := filepath.Join(f.cfg.Directory, filepath.FromSlash(objectKey))
for file != f.cfg.Directory {
if err := os.Remove(file); err != nil {
return err
}
file = filepath.Dir(file)
empty, err := isDirEmpty(file)
if err != nil {
return err
}
if !empty {
break
}
}
return nil
}
// DeleteChunksBefore implements BucketClient
func (f *FSObjectClient) DeleteChunksBefore(ctx context.Context, ts time.Time) error {
return filepath.Walk(f.cfg.Directory, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && info.ModTime().Before(ts) {
level.Info(util_log.Logger).Log("msg", "file has exceeded the retention period, removing it", "filepath", info.Name())
if err := os.Remove(path); err != nil {
return err
}
}
return nil
})
}
// IsObjectNotFoundErr returns true if error means that object is not found. Relevant to GetObject and DeleteObject operations.
func (f *FSObjectClient) IsObjectNotFoundErr(err error) bool {
return os.IsNotExist(errors.Cause(err))
}
// copied from https://github.com/thanos-io/thanos/blob/55cb8ca38b3539381dc6a781e637df15c694e50a/pkg/objstore/filesystem/filesystem.go#L181
func isDirEmpty(name string) (ok bool, err error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer runutil.CloseWithErrCapture(&err, f, "dir open")
if _, err = f.Readdir(1); err == io.EOF {
return true, nil
}
return false, err
}