-
Notifications
You must be signed in to change notification settings - Fork 184
/
store.go
163 lines (132 loc) · 3.53 KB
/
store.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
package object
import (
"context"
"errors"
"io"
"io/fs"
"strings"
"sync"
"time"
"go.flipt.io/flipt/internal/containers"
"go.flipt.io/flipt/internal/storage"
storagefs "go.flipt.io/flipt/internal/storage/fs"
"go.uber.org/zap"
gcblob "gocloud.dev/blob"
"gocloud.dev/gcerrors"
)
var _ storagefs.SnapshotStore = (*SnapshotStore)(nil)
type SnapshotStore struct {
*storagefs.Poller
logger *zap.Logger
scheme string
bucket *gcblob.Bucket
prefix string
pollOpts []containers.Option[storagefs.Poller]
mu sync.RWMutex
snap storage.ReadOnlyStore
}
func NewSnapshotStore(ctx context.Context, logger *zap.Logger, scheme string, bucket *gcblob.Bucket, opts ...containers.Option[SnapshotStore]) (*SnapshotStore, error) {
s := &SnapshotStore{
logger: logger,
scheme: scheme,
bucket: bucket,
pollOpts: []containers.Option[storagefs.Poller]{
storagefs.WithInterval(60 * time.Second),
},
}
containers.ApplyAll(s, opts...)
// fetch snapshot at-least once before returning store
// to ensure we have some state to serve
if _, err := s.update(ctx); err != nil {
return nil, err
}
s.Poller = storagefs.NewPoller(s.logger, ctx, s.update, s.pollOpts...)
go s.Poll()
return s, nil
}
// WithPrefix configures the prefix for object store
func WithPrefix(prefix string) containers.Option[SnapshotStore] {
return func(s *SnapshotStore) {
s.prefix = prefix
}
}
// WithPollOptions configures the poller options used when periodically updating snapshot state
func WithPollOptions(opts ...containers.Option[storagefs.Poller]) containers.Option[SnapshotStore] {
return func(s *SnapshotStore) {
s.pollOpts = append(s.pollOpts, opts...)
}
}
// View accepts a function which takes a *StoreSnapshot.
// The SnapshotStore will supply a snapshot which is valid
// for the lifetime of the provided function call.
func (s *SnapshotStore) View(_ context.Context, fn func(storage.ReadOnlyStore) error) error {
s.mu.RLock()
defer s.mu.RUnlock()
return fn(s.snap)
}
func (s *SnapshotStore) String() string {
return s.scheme
}
// Update fetches a new snapshot and swaps it out for the current one.
func (s *SnapshotStore) update(ctx context.Context) (bool, error) {
snap, err := s.build(ctx)
if err != nil {
return false, err
}
s.mu.Lock()
s.snap = snap
s.mu.Unlock()
return true, nil
}
func (s *SnapshotStore) build(ctx context.Context) (*storagefs.Snapshot, error) {
idx, err := s.getIndex(ctx)
if err != nil {
return nil, err
}
iterator := s.bucket.List(&gcblob.ListOptions{
Prefix: s.prefix,
})
var files []fs.File
for {
item, err := iterator.Next(ctx)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, err
}
key := strings.TrimPrefix(item.Key, s.prefix)
if !idx.Match(key) {
continue
}
rd, err := s.bucket.NewReader(ctx, s.prefix+key, &gcblob.ReaderOptions{})
if err != nil {
return nil, err
}
files = append(files, NewFile(
key,
item.Size,
rd,
item.ModTime,
))
}
return storagefs.SnapshotFromFiles(s.logger, files)
}
func (s *SnapshotStore) getIndex(ctx context.Context) (*storagefs.FliptIndex, error) {
rd, err := s.bucket.NewReader(ctx, s.prefix+storagefs.IndexFileName, &gcblob.ReaderOptions{})
if err != nil {
if gcerrors.Code(err) != gcerrors.NotFound {
return nil, err
}
s.logger.Debug("using default index",
zap.String("file", storagefs.IndexFileName),
zap.Error(fs.ErrNotExist))
return storagefs.DefaultFliptIndex()
}
defer rd.Close()
idx, err := storagefs.ParseFliptIndex(rd)
if err != nil {
return nil, err
}
return idx, nil
}