-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathstore.go
More file actions
294 lines (255 loc) · 8.58 KB
/
Copy pathstore.go
File metadata and controls
294 lines (255 loc) · 8.58 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 memlog
import (
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/elastic/beats/v7/libbeat/common/transform/typeconv"
"github.com/elastic/beats/v7/libbeat/statestore/backend"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
)
// store implements an actual memlog based store.
// It holds all key value pairs in memory in a memstore struct.
// All changes to the memstore are logged to the diskstore.
// The store execute a checkpoint operation if the checkpoint predicate
// triggers the operation, or if some error in the update log file has been
// detected by the diskstore.
//
// The store allows only one writer, but multiple concurrent readers.
type store struct {
lock sync.RWMutex
disk *diskstore
mem memstore
}
// memstore is the in memory key value store
type memstore struct {
table map[string]entry
}
type entry struct {
value map[string]interface{}
}
// openStore opens a store from the home path.
// The directory and intermediate directories will be created if it does not exist.
// The open routine loads the full key-value store into memory by first reading the data file and finally applying all outstanding updates
// from the update log file.
// If an error in in the log file is detected, the store opening routine continues from the last known valid state and will trigger a checkpoint
// operation on subsequent writes, also truncating the log file.
// Old data files are scheduled for deletion later.
func openStore(log *logp.Logger, home string, mode os.FileMode, bufSz uint, ignoreVersionCheck bool, checkpoint CheckpointPredicate) (*store, error) {
fi, err := os.Stat(home)
if os.IsNotExist(err) {
err = os.MkdirAll(home, os.ModeDir|0o770)
if err != nil {
return nil, err
}
err = writeMetaFile(home, mode)
if err != nil {
return nil, err
}
} else if !fi.Mode().IsDir() {
return nil, fmt.Errorf("'%v' is not a directory", home)
} else {
metaPath := filepath.Join(home, metaFileName)
if _, err := os.Stat(metaPath); os.IsNotExist(err) {
// Directory exists but meta.json is absent — the process likely crashed
// after MkdirAll but before writeMetaFile completed on a prior run.
if err := writeMetaFile(home, mode); err != nil {
return nil, err
}
} else if err != nil {
return nil, fmt.Errorf("failed to stat meta file: %w", err)
} else if err := pathEnsurePermissions(metaPath, mode); err != nil {
return nil, fmt.Errorf("failed to update meta file permissions: %w", err)
}
}
if !ignoreVersionCheck {
meta, err := readMetaFile(home)
if err != nil {
return nil, err
}
if err := checkMeta(meta); err != nil {
return nil, err
}
}
if err := pathEnsurePermissions(filepath.Join(home, activeDataFileName), mode); err != nil {
return nil, fmt.Errorf("failed to update active file permissions: %w", err)
}
dataFiles, err := listDataFiles(home)
if err != nil {
return nil, err
}
for _, df := range dataFiles {
if err := pathEnsurePermissions(df.path, mode); err != nil {
return nil, fmt.Errorf("failed to update data file permissions: %w", err)
}
}
if err := pathEnsurePermissions(filepath.Join(home, logFileName), mode); err != nil {
return nil, fmt.Errorf("failed to update log file permissions: %w", err)
}
tbl := map[string]entry{}
var txid uint64
if L := len(dataFiles); L > 0 {
active := dataFiles[L-1]
txid = active.txid
if err := loadDataFile(active.path, tbl); err != nil {
if errors.Is(err, ErrCorruptStore) {
corruptFilePath := active.path + ".corrupted"
err := os.Rename(active.path, corruptFilePath)
if err != nil {
log.Debugf("Failed to backup corrupt data file '%s': %+v", active.path, err)
}
log.Warnf("Data file is corrupt. It has been renamed to %s. Attempting to restore partial state from log file.", corruptFilePath)
} else {
return nil, err
}
} else {
log.Infof("Loading data file of '%v' succeeded. Active transaction id=%v", home, txid)
}
}
var entries uint
memstore := memstore{tbl}
txid, entries, err = loadLogFile(&memstore, txid, home)
log.Infof("Finished loading transaction log file for '%v'. Active transaction id=%v", home, txid)
if err != nil {
// Error indicates the log file was incomplete or corrupted.
// Anyways, we already have the table in a valid state and will
// continue opening the store from here.
log.Warnf("Incomplete or corrupted log file in %v. Continue with last known complete and consistent state. Reason: %v", home, err)
}
diskstore, err := newDiskStore(log, home, dataFiles, txid, mode, entries, err != nil, bufSz, checkpoint)
if err != nil {
return nil, err
}
return &store{
disk: diskstore,
mem: memstore,
}, nil
}
// Close closes access to the update log file and clears the in memory key
// value store. Access to the store after close can lead to a panic.
func (s *store) Close() error {
s.lock.Lock()
defer s.lock.Unlock()
s.mem = memstore{}
return s.disk.Close()
}
// Has checks if the key is known. The in memory store does not report any
// errors.
func (s *store) Has(key string) (bool, error) {
s.lock.RLock()
defer s.lock.RUnlock()
return s.mem.Has(key), nil
}
// Get retrieves and decodes the key-value pair into to.
func (s *store) Get(key string, to interface{}) error {
s.lock.RLock()
defer s.lock.RUnlock()
dec := s.mem.Get(key)
if dec == nil {
return errKeyUnknown
}
return dec.Decode(to)
}
// Set inserts or overwrites a key-value pair.
// If encoding was successful the in-memory state will be updated and a
// set-operation is logged to the diskstore.
func (s *store) Set(key string, value interface{}) error {
var tmp mapstr.M
if err := typeconv.Convert(&tmp, value); err != nil {
return err
}
s.lock.Lock()
defer s.lock.Unlock()
s.mem.Set(key, tmp)
return s.logOperation(&opSet{K: key, V: tmp})
}
// Remove removes a key from the in memory store and logs a remove operation to
// the diskstore. The operation does not check if the key exists.
func (s *store) Remove(key string) error {
s.lock.Lock()
defer s.lock.Unlock()
s.mem.Remove(key)
return s.logOperation(&opRemove{K: key})
}
// Checkpoint triggers a state checkpoint operation. All state will be written
// to a new transaction data file and fsync'ed. The log file will be reset after
// a successful write.
func (s *store) Checkpoint() error {
s.lock.Lock()
defer s.lock.Unlock()
return s.disk.WriteCheckpoint(s.mem.table)
}
// lopOperation ensures that the diskstore reflects the recent changes to the
// in memory store by either triggering a checkpoint operations or adding the
// operation type to the update log file.
func (s *store) logOperation(op op) error {
if s.disk.mustCheckpoint() {
err := s.disk.WriteCheckpoint(s.mem.table)
if err != nil {
// if writing the new checkpoint file failed we try to fallback to
// appending the log operation.
// idea: make append configurable and retry checkpointing with backoff.
_ = s.disk.LogOperation(op)
}
return err
}
return s.disk.LogOperation(op)
}
// Each iterates over all key-value pairs in the store.
func (s *store) Each(fn func(string, backend.ValueDecoder) (bool, error)) error {
s.lock.RLock()
defer s.lock.RUnlock()
for k, entry := range s.mem.table {
cont, err := fn(k, entry)
if !cont || err != nil {
return err
}
}
return nil
}
func (m *memstore) Has(key string) bool {
_, exists := m.table[key]
return exists
}
func (m *memstore) Get(key string) backend.ValueDecoder {
entry, exists := m.table[key]
if !exists {
return nil
}
return entry
}
func (m *memstore) Set(key string, value mapstr.M) {
m.table[key] = entry{value: value}
}
func (m *memstore) Remove(key string) bool {
_, exists := m.table[key]
if !exists {
return false
}
delete(m.table, key)
return true
}
func (s *store) SetID(_ string) {
// NOOP
}
func (e entry) Decode(to interface{}) error {
return typeconv.Convert(to, e.value)
}