/
store.go
217 lines (185 loc) · 5.91 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
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
// Copyright 2023 Arista Networks, Inc. All rights reserved.
//
// Use of this source code is governed by the MIT license that can be found
// in the LICENSE file.
//
package store
import (
"context"
"errors"
"io"
"os"
)
var ErrRetry = errors.New("the operation needs to be retried")
type Decoder interface {
Decode(v any) error
}
type Encoder interface {
Encode(v any) error
}
// A Store represents a way to marshal and unmarshal values of type T atomically
// from and to the file system.
//
// Basic usage is:
//
// st := store.New[Type](json.NewEncoder, json.NewDecoder)
//
// err := st.LoadAndStore(context.Background(), "/path/to/state.json", 0666, func(val *Type) error {
// // Use and/or modify val; it will get re-marshaled to the file
// return nil
// })
// if err != nil {
// log.Fatal(err)
// }
type Store[T any] struct {
newEncoder func(io.Writer) Encoder
newDecoder func(io.Reader) Decoder
}
func New[T any, E Encoder, D Decoder](newEncoder func(io.Writer) E, newDecoder func(io.Reader) D) *Store[T] {
return &Store[T]{
newEncoder: func(w io.Writer) Encoder { return newEncoder(w) },
newDecoder: func(r io.Reader) Decoder { return newDecoder(r) },
}
}
// Load reads the contents of the file at path and unmarshals it into v.
//
// Load may block if another store is in the process of writing to the file.
func (store *Store[T]) Load(ctx context.Context, path string, v *T) (canary any, err error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
rdf, err := openShared(path, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
defer rdf.Close()
if err := RLock(ctx, rdf); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if err := store.newDecoder(rdf).Decode(v); err != nil {
return nil, err
}
newCanary, err := lstatIno(rdf, "")
if err != nil {
return nil, err
}
return newCanary, nil
}
// Store marshals v and writes the result into the specified path, overwriting
// its contents. This write is atomic: either all of the data has been written,
// or none of it, in which case the destination remains untouched.
// This prevents all situations where a crashing process leaves the file
// half-written and corrupt.
//
// Store may block if another store is in the process of reading the file.
func (store *Store[T]) Store(ctx context.Context, path string, mode os.FileMode, v *T, canary any) (err error) {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// Write the updated contents to an alternate file, then atomically
// swap it with the original. This avoid corrupting the store should
// the process terminate mid-write.
wf, err := openShared(path+".lock", os.O_WRONLY|os.O_CREATE, mode&^os.ModeType)
if err != nil {
return err
}
defer wf.Close()
if err := Lock(ctx, wf); err != nil {
return err
}
oldCanary, _ := canary.(uint64)
newCanary, err := lstatIno(nil, path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
// Compare canaries -- we use inodes as canaries, so an inode of 0 means
// the file was missing.
if newCanary != oldCanary {
// The destination changed while we were waiting for the lock. This
// means that another concurrent store completed, and we need
// to retry.
return ErrRetry
}
if ko, err := deleted(wf); ko {
if err == nil {
// Another process pulled the rug from under us; we managed to acquire an
// exclusive lock, but that lock is held on the final file, not the
// temporary .lock file.
//
// In other words, we only acquired the lock after another call to Store
// finished atomically swapping the result.
//
// There's nothing we can do except return ErrRetry.
err = ErrRetry
}
return err
}
if err := wf.Truncate(0); err != nil {
return err
}
if err := store.newEncoder(wf).Encode(v); err != nil {
return err
}
return rename(wf, path)
}
// LoadAndStoreFunc is the signature of the user callback called by LoadAndStore.
//
// LoadAndStore calls the function with val set to a non-nil pointer to the
// value that was unmarshaled from the content of the specified file.
//
// If the value fails to load (commonly, because the file does not exist, or
// less commonly, because the file fails to unmarshal), the function is still
// called with val set to a pointer to the zero value of T, and err is set to
// the error that occured during loading.
type LoadAndStoreFunc[T any] func(ctx context.Context, val *T, err error) error
func (store *Store[T]) tryLoadAndStore(ctx context.Context, path string, mode os.FileMode, fn LoadAndStoreFunc[T]) error {
var value T
canary, err := store.Load(ctx, path, &value)
if err := fn(ctx, &value, err); err != nil {
return err
}
return store.Store(ctx, path, mode, &value, canary)
}
// LoadAndStore loads the file at path and calls the specified function with the
// result of that load, as if store.Load(ctx, path, &v) was called.
//
// The user function is then free to modify that value. If it returns without
// an error, LoadAndStore attempts to store the value back into the file.
//
// If the underlying file did not change since it first loaded, the store succeeds.
// Otherwise, it is aborted, and the process is retried, reloading the file and
// calling the user function for re-modification.
//
// In effect, LoadAndStore has Compare-and-Swap semantics; the function is preferred
// over Load and Store when the caller needs to update partially the contents of
// the file.
func (store *Store[T]) LoadAndStore(ctx context.Context, path string, mode os.FileMode, fn LoadAndStoreFunc[T]) error {
err := ErrRetry
for err == ErrRetry {
err = store.tryLoadAndStore(ctx, path, mode, fn)
}
return err
}
func deleted(f *os.File) (ok bool, e error) {
fino, err := lstatIno(f, "")
if err != nil {
return true, err
}
pino, err := lstatIno(nil, f.Name())
switch {
case errors.Is(err, os.ErrNotExist):
return true, nil
case err != nil:
return true, err
}
return fino != pino, nil
}