forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filestore.go
424 lines (360 loc) · 11.7 KB
/
filestore.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package quickfix
import (
"fmt"
"io"
"os"
"path"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/quickfixgo/quickfix/config"
)
type msgDef struct {
offset int64
size int
}
type fileStoreFactory struct {
settings *Settings
backupFactory *backupStoreFactory
}
type fileStore struct {
sessionID SessionID
cache *memoryStore
offsets map[int]msgDef
bodyFname string
headerFname string
sessionFname string
senderSeqNumsFname string
targetSeqNumsFname string
bodyFile *os.File
headerFile *os.File
sessionFile *os.File
senderSeqNumsFile *os.File
targetSeqNumsFile *os.File
fileSync bool
backupStore *backupStore
}
// NewFileStoreFactory returns a file-based implementation of MessageStoreFactory
func NewFileStoreFactory(settings *Settings, backupFactory *backupStoreFactory) MessageStoreFactory {
sfs := &fileStoreFactory{
settings: settings,
backupFactory: backupFactory,
}
return sfs
}
// Create creates a new FileStore implementation of the MessageStore interface
func (f fileStoreFactory) Create(sessionID SessionID) (msgStore MessageStore, err error) {
sessionSettings, ok := f.settings.SessionSettings()[sessionID]
if !ok {
return nil, fmt.Errorf("unknown session: %v", sessionID)
}
dirname, err := sessionSettings.Setting(config.FileStorePath)
if err != nil {
return nil, err
}
var fsync bool
if sessionSettings.HasSetting(config.FileStoreSync) {
fsync, err = sessionSettings.BoolSetting(config.FileStoreSync)
if err != nil {
return nil, err
}
} else {
fsync = true //existing behavior is to fsync writes
}
backupStore, err := f.backupFactory.Create(sessionID)
if err != nil {
fmt.Printf("file store: failed to init backup store, err: %v\n", err)
}
return newFileStore(sessionID, dirname, fsync, backupStore)
}
func newFileStore(sessionID SessionID, dirname string, fileSync bool, backupStore *backupStore) (*fileStore, error) {
if err := os.MkdirAll(dirname, os.ModePerm); err != nil {
return nil, err
}
sessionPrefix := SessionIDFilenamePrefix(sessionID)
store := &fileStore{
sessionID: sessionID,
cache: &memoryStore{},
offsets: make(map[int]msgDef),
bodyFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "body")),
headerFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "header")),
sessionFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "session")),
senderSeqNumsFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "senderseqnums")),
targetSeqNumsFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "targetseqnums")),
fileSync: fileSync,
backupStore: backupStore,
}
if err := store.Refresh(); err != nil {
return nil, err
}
return store, nil
}
// Reset deletes the store files and sets the seqnums back to 1
func (store *fileStore) Reset() error {
if err := store.cache.Reset(); err != nil {
return errors.Wrap(err, "cache reset")
}
if err := store.Close(); err != nil {
return errors.Wrap(err, "close")
}
if err := removeFile(store.bodyFname); err != nil {
return err
}
if err := removeFile(store.headerFname); err != nil {
return err
}
if err := removeFile(store.sessionFname); err != nil {
return err
}
if err := removeFile(store.senderSeqNumsFname); err != nil {
return err
}
if err := removeFile(store.targetSeqNumsFname); err != nil {
return err
}
store.backupStore.Reset()
if err := store.Refresh(); err != nil {
return err
}
return nil
}
// Refresh closes the store files and then reloads from them
func (store *fileStore) Refresh() (err error) {
if err = store.cache.Reset(); err != nil {
err = errors.Wrap(err, "cache reset")
return
}
if err = store.Close(); err != nil {
return err
}
creationTimePopulated, err := store.populateCache()
if err != nil {
return err
}
if store.bodyFile, err = openOrCreateFile(store.bodyFname, 0660); err != nil {
return err
}
if store.headerFile, err = openOrCreateFile(store.headerFname, 0660); err != nil {
return err
}
if store.sessionFile, err = openOrCreateFile(store.sessionFname, 0660); err != nil {
return err
}
if store.senderSeqNumsFile, err = openOrCreateFile(store.senderSeqNumsFname, 0660); err != nil {
return err
}
if store.targetSeqNumsFile, err = openOrCreateFile(store.targetSeqNumsFname, 0660); err != nil {
return err
}
if !creationTimePopulated {
if err := store.setSession(); err != nil {
return err
}
}
if err := store.SetNextSenderMsgSeqNum(store.NextSenderMsgSeqNum()); err != nil {
return errors.Wrap(err, "set next sender")
}
if err := store.SetNextTargetMsgSeqNum(store.NextTargetMsgSeqNum()); err != nil {
return errors.Wrap(err, "set next target")
}
return nil
}
func (store *fileStore) populateCache() (creationTimePopulated bool, err error) {
if tmpHeaderFile, err := os.Open(store.headerFname); err == nil {
defer tmpHeaderFile.Close()
for {
var seqNum, size int
var offset int64
if cnt, err := fmt.Fscanf(tmpHeaderFile, "%d,%d,%d\n", &seqNum, &offset, &size); err != nil || cnt != 3 {
break
}
store.offsets[seqNum] = msgDef{offset: offset, size: size}
}
}
if timeBytes, err := os.ReadFile(store.sessionFname); err == nil {
var ctime time.Time
if err := ctime.UnmarshalText(timeBytes); err == nil {
store.cache.creationTime = ctime
creationTimePopulated = true
}
}
if senderSeqNumBytes, err := os.ReadFile(store.senderSeqNumsFname); err == nil {
if senderSeqNum, err := strconv.Atoi(string(senderSeqNumBytes)); err == nil {
if err = store.cache.SetNextSenderMsgSeqNum(senderSeqNum); err != nil {
return creationTimePopulated, errors.Wrap(err, "cache set next sender")
}
}
}
if targetSeqNumBytes, err := os.ReadFile(store.targetSeqNumsFname); err == nil {
if targetSeqNum, err := strconv.Atoi(string(targetSeqNumBytes)); err == nil {
if err = store.cache.SetNextTargetMsgSeqNum(targetSeqNum); err != nil {
return creationTimePopulated, errors.Wrap(err, "cache set next target")
}
}
}
return creationTimePopulated, nil
}
func (store *fileStore) setSession() error {
if _, err := store.sessionFile.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("unable to rewind file: %s: %s", store.sessionFname, err.Error())
}
data, err := store.cache.CreationTime().MarshalText()
if err != nil {
return fmt.Errorf("unable to marshal session time to file: %s: %s", store.sessionFname, err.Error())
}
if _, err := store.sessionFile.Write(data); err != nil {
return fmt.Errorf("unable to write to file: %s: %s", store.sessionFname, err.Error())
}
if store.fileSync {
if err := store.sessionFile.Sync(); err != nil {
return fmt.Errorf("unable to flush file: %s: %s", store.sessionFname, err.Error())
}
}
return nil
}
func (store *fileStore) setSeqNum(f *os.File, seqNum int) error {
if _, err := f.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("unable to rewind file: %s: %s", f.Name(), err.Error())
}
if _, err := fmt.Fprintf(f, "%019d", seqNum); err != nil {
return fmt.Errorf("unable to write to file: %s: %s", f.Name(), err.Error())
}
if store.fileSync {
if err := f.Sync(); err != nil {
return fmt.Errorf("unable to flush file: %s: %s", f.Name(), err.Error())
}
}
return nil
}
// NextSenderMsgSeqNum returns the next MsgSeqNum that will be sent
func (store *fileStore) NextSenderMsgSeqNum() int {
return store.cache.NextSenderMsgSeqNum()
}
// NextTargetMsgSeqNum returns the next MsgSeqNum that should be received
func (store *fileStore) NextTargetMsgSeqNum() int {
return store.cache.NextTargetMsgSeqNum()
}
// SetNextSenderMsgSeqNum sets the next MsgSeqNum that will be sent
func (store *fileStore) SetNextSenderMsgSeqNum(next int) error {
if err := store.cache.SetNextSenderMsgSeqNum(next); err != nil {
return errors.Wrap(err, "cache")
}
if err := store.setSeqNum(store.senderSeqNumsFile, next); err != nil {
return err
}
store.backupStore.SetNextSenderMsgSeqNum(next)
return nil
}
// SetNextTargetMsgSeqNum sets the next MsgSeqNum that should be received
func (store *fileStore) SetNextTargetMsgSeqNum(next int) error {
if err := store.cache.SetNextTargetMsgSeqNum(next); err != nil {
return errors.Wrap(err, "cache")
}
if err := store.setSeqNum(store.targetSeqNumsFile, next); err != nil {
return err
}
store.backupStore.SetNextTargetMsgSeqNum(next)
return nil
}
// IncrNextSenderMsgSeqNum increments the next MsgSeqNum that will be sent
func (store *fileStore) IncrNextSenderMsgSeqNum() error {
if err := store.cache.IncrNextSenderMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache")
}
seqNum := store.cache.NextSenderMsgSeqNum()
if err := store.setSeqNum(store.senderSeqNumsFile, seqNum); err != nil {
return err
}
store.backupStore.SetNextSenderMsgSeqNum(seqNum)
return nil
}
// IncrNextTargetMsgSeqNum increments the next MsgSeqNum that should be received
func (store *fileStore) IncrNextTargetMsgSeqNum() error {
if err := store.cache.IncrNextTargetMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache")
}
seqNum := store.cache.NextTargetMsgSeqNum()
if err := store.setSeqNum(store.targetSeqNumsFile, seqNum); err != nil {
return err
}
store.backupStore.SetNextTargetMsgSeqNum(seqNum)
return nil
}
// CreationTime returns the creation time of the store
func (store *fileStore) CreationTime() time.Time {
return store.cache.CreationTime()
}
func (store *fileStore) SaveMessage(seqNum int, msg []byte) error {
offset, err := store.bodyFile.Seek(0, io.SeekEnd)
if err != nil {
return fmt.Errorf("unable to seek to end of file: %s: %s", store.bodyFname, err.Error())
}
if _, err := store.headerFile.Seek(0, io.SeekEnd); err != nil {
return fmt.Errorf("unable to seek to end of file: %s: %s", store.headerFname, err.Error())
}
if _, err := fmt.Fprintf(store.headerFile, "%d,%d,%d\n", seqNum, offset, len(msg)); err != nil {
return fmt.Errorf("unable to write to file: %s: %s", store.headerFname, err.Error())
}
store.offsets[seqNum] = msgDef{offset: offset, size: len(msg)}
if _, err := store.bodyFile.Write(msg); err != nil {
return fmt.Errorf("unable to write to file: %s: %s", store.bodyFname, err.Error())
}
if store.fileSync {
if err := store.bodyFile.Sync(); err != nil {
return fmt.Errorf("unable to flush file: %s: %s", store.bodyFname, err.Error())
}
if err := store.headerFile.Sync(); err != nil {
return fmt.Errorf("unable to flush file: %s: %s", store.headerFname, err.Error())
}
}
store.backupStore.SaveMessage(seqNum, msg)
return nil
}
func (store *fileStore) getMessage(seqNum int) (msg []byte, found bool, err error) {
msgInfo, found := store.offsets[seqNum]
if !found {
return
}
msg = make([]byte, msgInfo.size)
if _, err = store.bodyFile.ReadAt(msg, msgInfo.offset); err != nil {
return nil, true, fmt.Errorf("unable to read from file: %s: %s", store.bodyFname, err.Error())
}
return msg, true, nil
}
func (store *fileStore) GetMessages(beginSeqNum, endSeqNum int) ([][]byte, error) {
var msgs [][]byte
for seqNum := beginSeqNum; seqNum <= endSeqNum; seqNum++ {
m, found, err := store.getMessage(seqNum)
if err != nil {
return nil, err
}
if found {
msgs = append(msgs, m)
}
}
return msgs, nil
}
// Close closes the store's files
func (store *fileStore) Close() error {
if err := closeFile(store.bodyFile); err != nil {
return err
}
if err := closeFile(store.headerFile); err != nil {
return err
}
if err := closeFile(store.sessionFile); err != nil {
return err
}
if err := closeFile(store.senderSeqNumsFile); err != nil {
return err
}
if err := closeFile(store.targetSeqNumsFile); err != nil {
return err
}
store.bodyFile = nil
store.headerFile = nil
store.sessionFile = nil
store.senderSeqNumsFile = nil
store.targetSeqNumsFile = nil
return nil
}