forked from alpacahq/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongostore.go
424 lines (370 loc) · 13.6 KB
/
mongostore.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
// Copyright (c) quickfixengine.org All rights reserved.
//
// This file may be distributed under the terms of the quickfixengine.org
// license as defined by quickfixengine.org and appearing in the file
// LICENSE included in the packaging of this file.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
//
// See http://www.quickfixengine.org/LICENSE for licensing information.
//
// Contact ask@quickfixengine.org if any conditions of this licensing
// are not clear to you.
package quickfix
import (
"context"
"fmt"
"time"
"github.com/cryptogarageinc/quickfix-go/config"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type mongoStoreFactory struct {
settings *Settings
messagesCollection string
sessionsCollection string
}
type mongoStore struct {
sessionID SessionID
cache *memoryStore
mongoURL string
mongoDatabase string
db *mongo.Client
messagesCollection string
sessionsCollection string
allowTransactions bool
*messageBuilder
}
// NewMongoStoreFactory returns a mongo-based implementation of MessageStoreFactory.
func NewMongoStoreFactory(settings *Settings) MessageStoreFactory {
return NewMongoStoreFactoryPrefixed(settings, "")
}
// NewMongoStoreFactoryPrefixed returns a mongo-based implementation of MessageStoreFactory, with prefix on collections.
func NewMongoStoreFactoryPrefixed(settings *Settings, collectionsPrefix string) MessageStoreFactory {
return mongoStoreFactory{
settings: settings,
messagesCollection: collectionsPrefix + "messages",
sessionsCollection: collectionsPrefix + "sessions",
}
}
// Create creates a new MongoStore implementation of the MessageStore interface.
func (f mongoStoreFactory) Create(sessionID SessionID) (msgStore MessageStore, err error) {
var mongoConnectionURL string
var mongoDatabase string
var mongoReplicaSet string
settings := make([]*SessionSettings, 1, 2)
settings[0] = f.settings.GlobalSettings()
if sessionSettings, ok := f.settings.SessionSettings()[sessionID]; ok {
settings = append(settings, sessionSettings)
}
for _, sessionSettings := range settings {
if sessionSettings.HasSetting(config.MongoStoreConnection) {
mongoConnectionURL, err = sessionSettings.Setting(config.MongoStoreConnection)
if err != nil {
return nil, err
}
}
if sessionSettings.HasSetting(config.MongoStoreDatabase) {
mongoDatabase, err = sessionSettings.Setting(config.MongoStoreDatabase)
if err != nil {
return nil, err
}
}
// Optional.
if sessionSettings.HasSetting(config.MongoStoreReplicaSet) {
mongoReplicaSet, err = sessionSettings.Setting(config.MongoStoreReplicaSet)
if err != nil {
return nil, err
}
}
}
if len(mongoConnectionURL) == 0 {
return nil, fmt.Errorf("MongoStoreConnection configuration is not found. session: %v", sessionID)
} else if len(mongoDatabase) == 0 {
return nil, fmt.Errorf("MongoStoreDatabase configuration is not found. session: %v", sessionID)
}
return newMongoStore(sessionID, mongoConnectionURL, mongoDatabase, mongoReplicaSet, f.messagesCollection, f.sessionsCollection)
}
func newMongoStore(sessionID SessionID, mongoURL, mongoDatabase, mongoReplicaSet, messagesCollection, sessionsCollection string) (store *mongoStore, err error) {
allowTransactions := len(mongoReplicaSet) > 0
store = &mongoStore{
sessionID: sessionID,
cache: &memoryStore{},
mongoURL: mongoURL,
mongoDatabase: mongoDatabase,
messagesCollection: messagesCollection,
sessionsCollection: sessionsCollection,
allowTransactions: allowTransactions,
}
store.messageBuilder = newMessageBuilder(store)
if err = store.cache.Reset(); err != nil {
err = errors.Wrap(err, "cache reset")
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
store.db, err = mongo.Connect(ctx, options.Client().ApplyURI(mongoURL).SetDirect(len(mongoReplicaSet) == 0).SetReplicaSet(mongoReplicaSet))
if err != nil {
return
}
err = store.populateCache()
return
}
func generateMessageFilter(s *SessionID) (messageFilter *mongoQuickFixEntryData) {
messageFilter = &mongoQuickFixEntryData{
BeginString: s.BeginString,
SessionQualifier: s.Qualifier,
SenderCompID: s.SenderCompID,
SenderSubID: s.SenderSubID,
SenderLocID: s.SenderLocationID,
TargetCompID: s.TargetCompID,
TargetSubID: s.TargetSubID,
TargetLocID: s.TargetLocationID,
}
return
}
type mongoQuickFixEntryData struct {
// Message specific data.
Msgseq int `bson:"msgseq,omitempty"`
Message []byte `bson:"message,omitempty"`
// Session specific data.
CreationTime time.Time `bson:"creation_time,omitempty"`
IncomingSeqNum int `bson:"incoming_seq_num,omitempty"`
OutgoingSeqNum int `bson:"outgoing_seq_num,omitempty"`
// Indexed data.
BeginString string `bson:"begin_string"`
SessionQualifier string `bson:"session_qualifier"`
SenderCompID string `bson:"sender_comp_id"`
SenderSubID string `bson:"sender_sub_id"`
SenderLocID string `bson:"sender_loc_id"`
TargetCompID string `bson:"target_comp_id"`
TargetSubID string `bson:"target_sub_id"`
TargetLocID string `bson:"target_loc_id"`
}
// Reset deletes the store records and sets the seqnums back to 1.
func (store *mongoStore) Reset() error {
if store.db == nil {
return ErrAccessToClosedStore
}
msgFilter := generateMessageFilter(&store.sessionID)
_, err := store.db.Database(store.mongoDatabase).Collection(store.messagesCollection).DeleteMany(context.Background(), msgFilter)
if err != nil {
return err
}
if err = store.cache.Reset(); err != nil {
return err
}
sessionUpdate := generateMessageFilter(&store.sessionID)
sessionUpdate.CreationTime = store.cache.CreationTime()
sessionUpdate.IncomingSeqNum = store.cache.NextTargetMsgSeqNum()
sessionUpdate.OutgoingSeqNum = store.cache.NextSenderMsgSeqNum()
_, err = store.db.Database(store.mongoDatabase).Collection(store.sessionsCollection).UpdateOne(context.Background(), msgFilter, bson.M{"$set": sessionUpdate})
return err
}
// Refresh reloads the store from the database.
func (store *mongoStore) Refresh() error {
if err := store.cache.Reset(); err != nil {
return err
}
return store.populateCache()
}
func (store *mongoStore) populateCache() error {
if store.db == nil {
return ErrAccessToClosedStore
}
msgFilter := generateMessageFilter(&store.sessionID)
res := store.db.Database(store.mongoDatabase).Collection(store.sessionsCollection).FindOne(context.Background(), msgFilter)
if res.Err() != nil && res.Err() != mongo.ErrNoDocuments {
return errors.Wrap(res.Err(), "query")
}
if res.Err() != mongo.ErrNoDocuments {
// session record found, load it
sessionData := &mongoQuickFixEntryData{}
if err := res.Decode(&sessionData); err != nil {
return errors.Wrap(err, "decode")
}
store.cache.creationTime = sessionData.CreationTime
if err := store.cache.SetNextTargetMsgSeqNum(sessionData.IncomingSeqNum); err != nil {
return errors.Wrap(err, "cache set next target")
}
if err := store.cache.SetNextSenderMsgSeqNum(sessionData.OutgoingSeqNum); err != nil {
return errors.Wrap(err, "cache set next sender")
}
return nil
}
// session record not found, create it
msgFilter.CreationTime = store.cache.creationTime
msgFilter.IncomingSeqNum = store.cache.NextTargetMsgSeqNum()
msgFilter.OutgoingSeqNum = store.cache.NextSenderMsgSeqNum()
if _, err := store.db.Database(store.mongoDatabase).Collection(store.sessionsCollection).InsertOne(context.Background(), msgFilter); err != nil {
return errors.Wrap(err, "insert")
}
return nil
}
// NextSenderMsgSeqNum returns the next MsgSeqNum that will be sent.
func (store *mongoStore) NextSenderMsgSeqNum() int {
return store.cache.NextSenderMsgSeqNum()
}
// NextTargetMsgSeqNum returns the next MsgSeqNum that should be received.
func (store *mongoStore) NextTargetMsgSeqNum() int {
return store.cache.NextTargetMsgSeqNum()
}
// SetNextSenderMsgSeqNum sets the next MsgSeqNum that will be sent.
func (store *mongoStore) SetNextSenderMsgSeqNum(next int) error {
if store.db == nil {
return ErrAccessToClosedStore
}
msgFilter := generateMessageFilter(&store.sessionID)
sessionUpdate := generateMessageFilter(&store.sessionID)
sessionUpdate.IncomingSeqNum = store.cache.NextTargetMsgSeqNum()
sessionUpdate.OutgoingSeqNum = next
sessionUpdate.CreationTime = store.cache.CreationTime()
if _, err := store.db.Database(store.mongoDatabase).Collection(store.sessionsCollection).UpdateOne(context.Background(), msgFilter, bson.M{"$set": sessionUpdate}); err != nil {
return err
}
return store.cache.SetNextSenderMsgSeqNum(next)
}
// SetNextTargetMsgSeqNum sets the next MsgSeqNum that should be received.
func (store *mongoStore) SetNextTargetMsgSeqNum(next int) error {
if store.db == nil {
return ErrAccessToClosedStore
}
msgFilter := generateMessageFilter(&store.sessionID)
sessionUpdate := generateMessageFilter(&store.sessionID)
sessionUpdate.IncomingSeqNum = next
sessionUpdate.OutgoingSeqNum = store.cache.NextSenderMsgSeqNum()
sessionUpdate.CreationTime = store.cache.CreationTime()
if _, err := store.db.Database(store.mongoDatabase).Collection(store.sessionsCollection).UpdateOne(context.Background(), msgFilter, bson.M{"$set": sessionUpdate}); err != nil {
return err
}
return store.cache.SetNextTargetMsgSeqNum(next)
}
// IncrNextSenderMsgSeqNum increments the next MsgSeqNum that will be sent.
func (store *mongoStore) IncrNextSenderMsgSeqNum() error {
if err := store.cache.IncrNextSenderMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache incr")
}
return store.SetNextSenderMsgSeqNum(store.cache.NextSenderMsgSeqNum())
}
// IncrNextTargetMsgSeqNum increments the next MsgSeqNum that should be received.
func (store *mongoStore) IncrNextTargetMsgSeqNum() error {
if err := store.cache.IncrNextTargetMsgSeqNum(); err != nil {
return errors.Wrap(err, "cache incr")
}
return store.SetNextTargetMsgSeqNum(store.cache.NextTargetMsgSeqNum())
}
// CreationTime returns the creation time of the store.
func (store *mongoStore) CreationTime() time.Time {
return store.cache.CreationTime()
}
func (store *mongoStore) SaveMessage(seqNum int, msg []byte) (err error) {
if store.db == nil {
err = ErrAccessToClosedStore
return
}
msgFilter := generateMessageFilter(&store.sessionID)
msgFilter.Msgseq = seqNum
msgFilter.Message = msg
_, err = store.db.Database(store.mongoDatabase).Collection(store.messagesCollection).InsertOne(context.Background(), msgFilter)
return
}
func (store *mongoStore) SaveMessageAndIncrNextSenderMsgSeqNum(seqNum int, msg []byte) error {
if !store.allowTransactions {
err := store.SaveMessage(seqNum, msg)
if err != nil {
return err
}
return store.IncrNextSenderMsgSeqNum()
}
// If the mongodb supports replicasets, perform this operation as a transaction instead-
var next int
err := store.db.UseSession(context.Background(), func(sessionCtx mongo.SessionContext) error {
if err := sessionCtx.StartTransaction(); err != nil {
return err
}
msgFilter := generateMessageFilter(&store.sessionID)
msgFilter.Msgseq = seqNum
msgFilter.Message = msg
_, err := store.db.Database(store.mongoDatabase).Collection(store.messagesCollection).InsertOne(sessionCtx, msgFilter)
if err != nil {
return err
}
next = store.cache.NextSenderMsgSeqNum() + 1
msgFilter = generateMessageFilter(&store.sessionID)
sessionUpdate := generateMessageFilter(&store.sessionID)
sessionUpdate.IncomingSeqNum = store.cache.NextTargetMsgSeqNum()
sessionUpdate.OutgoingSeqNum = next
sessionUpdate.CreationTime = store.cache.CreationTime()
_, err = store.db.Database(store.mongoDatabase).Collection(store.sessionsCollection).UpdateOne(sessionCtx, msgFilter, bson.M{"$set": sessionUpdate})
if err != nil {
return err
}
return sessionCtx.CommitTransaction(context.Background())
})
if err != nil {
return err
}
return store.cache.SetNextSenderMsgSeqNum(next)
}
func (store *mongoStore) GetMessages(beginSeqNum, endSeqNum int) (msgs [][]byte, err error) {
if store.db == nil {
err = ErrAccessToClosedStore
return
}
msgFilter := generateMessageFilter(&store.sessionID)
// Marshal into database form.
msgFilterBytes, err := bson.Marshal(msgFilter)
if err != nil {
return
}
seqFilter := bson.M{}
err = bson.Unmarshal(msgFilterBytes, &seqFilter)
if err != nil {
return
}
// Modify the query to use a range for the sequence filter.
seqFilter["msgseq"] = bson.M{
"$gte": beginSeqNum,
"$lte": endSeqNum,
}
sortOpt := options.Find().SetSort(bson.D{{Key: "msgseq", Value: 1}})
cursor, err := store.db.Database(store.mongoDatabase).Collection(store.messagesCollection).Find(context.Background(), seqFilter, sortOpt)
if err != nil {
return
}
for cursor.Next(context.Background()) {
if err = cursor.Decode(&msgFilter); err != nil {
return
}
msgs = append(msgs, msgFilter.Message)
}
err = cursor.Close(context.Background())
return
}
func (store *mongoStore) SaveMessageWithTx(messageBuildData *BuildMessageInput) (output *BuildMessageOutput, err error) {
output, err = store.BuildMessage(messageBuildData)
if err != nil {
return
}
err = store.IncrNextSenderMsgSeqNum()
if err != nil {
return
}
err = store.SaveMessage(output.SeqNum, output.MsgBytes)
return
}
// Close closes the store's database connection.
func (store *mongoStore) Close() error {
if store.db != nil {
err := store.db.Disconnect(context.Background())
if err != nil {
return errors.Wrap(err, "error disconnecting from database")
}
store.db = nil
}
return nil
}