summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/dualdriver/dualdriver.go
blob: c7763dacef3ceb3fe8f6721586f4e0460b6e16c7 (plain)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package dualdriver

import (
	"context"
	"errors"
	"fmt"
	"reflect"
	"time"

	"github.com/algorand/go-algorand/ledger/store/trackerdb"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/util/db"
	"github.com/google/go-cmp/cmp"
)

// ErrInconsistentResult is returned when the two stores return different results.
var ErrInconsistentResult = errors.New("inconsistent results between store engines")

var allowAllUnexported = cmp.Exporter(func(f reflect.Type) bool { return true })

type trackerStore struct {
	primary   trackerdb.Store
	secondary trackerdb.Store
	trackerdb.Reader
	trackerdb.Writer
	trackerdb.Catchpoint
}

// MakeStore creates a dual tracker store that verifies that both stores return the same results.
func MakeStore(primary trackerdb.Store, secondary trackerdb.Store) trackerdb.Store {
	return &trackerStore{primary, secondary, &reader{primary, secondary}, &writer{primary, secondary}, &catchpoint{primary, secondary}}
}

func (s *trackerStore) SetSynchronousMode(ctx context.Context, mode db.SynchronousMode, fullfsync bool) (err error) {
	errP := s.primary.SetSynchronousMode(ctx, mode, fullfsync)
	errS := s.secondary.SetSynchronousMode(ctx, mode, fullfsync)
	return coalesceErrors(errP, errS)
}

func (s *trackerStore) IsSharedCacheConnection() bool {
	// Note: this is not something to check for being equal but rather keep the most conservative answer.
	return s.primary.IsSharedCacheConnection() || s.secondary.IsSharedCacheConnection()
}

func (s *trackerStore) Batch(fn trackerdb.BatchFn) (err error) {
	return s.BatchContext(context.Background(), fn)
}

func (s *trackerStore) BatchContext(ctx context.Context, fn trackerdb.BatchFn) (err error) {
	handle, err := s.BeginBatch(ctx)
	if err != nil {
		return err
	}
	defer handle.Close()

	err = fn(ctx, handle)
	if err != nil {
		return err
	}

	return handle.Commit()
}

func (s *trackerStore) BeginBatch(ctx context.Context) (trackerdb.Batch, error) {
	primary, err := s.primary.BeginBatch(ctx)
	if err != nil {
		return nil, err
	}
	secondary, err := s.secondary.BeginBatch(ctx)
	if err != nil {
		return nil, err
	}
	return &batch{primary, secondary, &writer{primary, secondary}}, nil
}

func (s *trackerStore) Snapshot(fn trackerdb.SnapshotFn) (err error) {
	return s.SnapshotContext(context.Background(), fn)
}

func (s *trackerStore) SnapshotContext(ctx context.Context, fn trackerdb.SnapshotFn) (err error) {
	handle, err := s.BeginSnapshot(ctx)
	if err != nil {
		return err
	}
	defer handle.Close()

	err = fn(ctx, handle)
	if err != nil {
		return err
	}
	return nil
}

func (s *trackerStore) BeginSnapshot(ctx context.Context) (trackerdb.Snapshot, error) {
	primary, err := s.primary.BeginSnapshot(ctx)
	if err != nil {
		return nil, err
	}
	secondary, err := s.secondary.BeginSnapshot(ctx)
	if err != nil {
		return nil, err
	}
	return &snapshot{primary, secondary, &reader{primary, secondary}}, nil
}

func (s *trackerStore) Transaction(fn trackerdb.TransactionFn) (err error) {
	return s.TransactionContext(context.Background(), fn)
}

func (s *trackerStore) TransactionContext(ctx context.Context, fn trackerdb.TransactionFn) error {
	handle, err := s.BeginTransaction(ctx)
	if err != nil {
		return err
	}
	defer handle.Close()

	err = fn(ctx, handle)
	if err != nil {
		return err
	}

	return handle.Commit()
}

func (s *trackerStore) BeginTransaction(ctx context.Context) (trackerdb.Transaction, error) {
	primary, err := s.primary.BeginTransaction(ctx)
	if err != nil {
		return nil, err
	}
	secondary, err := s.secondary.BeginTransaction(ctx)
	if err != nil {
		return nil, err
	}
	return &transaction{primary, secondary, &reader{primary, secondary}, &writer{primary, secondary}, &catchpoint{primary, secondary}}, nil
}

// RunMigrations implements trackerdb.Transaction
func (s *trackerStore) RunMigrations(ctx context.Context, params trackerdb.Params, log logging.Logger, targetVersion int32) (mgr trackerdb.InitParams, err error) {
	paramsP, errP := s.primary.RunMigrations(ctx, params, log, targetVersion)
	paramsS, errS := s.secondary.RunMigrations(ctx, params, log, targetVersion)
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results
	if paramsP != paramsS {
		err = ErrInconsistentResult
		return
	}
	// return primary result
	return paramsP, nil
}

func (s *trackerStore) Vacuum(ctx context.Context) (stats db.VacuumStats, err error) {
	// ignore the stats
	// Note: this is a SQL specific operation, so the are unlikely to match
	stats, errP := s.primary.Vacuum(ctx)
	_, errS := s.secondary.Vacuum(ctx)
	err = coalesceErrors(errP, errS)
	return
}

func (s *trackerStore) ResetToV6Test(ctx context.Context) error {
	// TODO
	return nil
}

func (s *trackerStore) Close() {
	s.primary.Close()
	s.secondary.Close()
}

type reader struct {
	primary   trackerdb.Reader
	secondary trackerdb.Reader
}

// MakeAccountsOptimizedReader implements trackerdb.Reader
func (r *reader) MakeAccountsOptimizedReader() (trackerdb.AccountsReader, error) {
	primary, errP := r.primary.MakeAccountsOptimizedReader()
	secondary, errS := r.secondary.MakeAccountsOptimizedReader()
	err := coalesceErrors(errP, errS)
	if err != nil {
		return nil, err
	}
	return &accountsReader{primary, secondary}, nil
}

// MakeAccountsReader implements trackerdb.Reader
func (r *reader) MakeAccountsReader() (trackerdb.AccountsReaderExt, error) {
	primary, errP := r.primary.MakeAccountsReader()
	secondary, errS := r.secondary.MakeAccountsReader()
	err := coalesceErrors(errP, errS)
	if err != nil {
		return nil, err
	}
	return &accountsReaderExt{primary, secondary}, nil
}

// MakeOnlineAccountsOptimizedReader implements trackerdb.Reader
func (r *reader) MakeOnlineAccountsOptimizedReader() (trackerdb.OnlineAccountsReader, error) {
	primary, errP := r.primary.MakeOnlineAccountsOptimizedReader()
	secondary, errS := r.secondary.MakeOnlineAccountsOptimizedReader()
	err := coalesceErrors(errP, errS)
	if err != nil {
		return nil, err
	}
	return &onlineAccountsReader{primary, secondary}, nil
}

// MakeSpVerificationCtxReader implements trackerdb.Reader
func (r *reader) MakeSpVerificationCtxReader() trackerdb.SpVerificationCtxReader {
	primary := r.primary.MakeSpVerificationCtxReader()
	secondary := r.secondary.MakeSpVerificationCtxReader()
	return &stateproofReader{primary, secondary}
}

// MakeCatchpointPendingHashesIterator implements trackerdb.Reader
func (*reader) MakeCatchpointPendingHashesIterator(hashCount int) trackerdb.CatchpointPendingHashesIter {
	// TODO: catchpoint
	return nil
}

// MakeCatchpointReader implements trackerdb.Reader
func (*reader) MakeCatchpointReader() (trackerdb.CatchpointReader, error) {
	// TODO: catchpoint
	return nil, nil
}

// MakeEncodedAccoutsBatchIter implements trackerdb.Reader
func (*reader) MakeEncodedAccoutsBatchIter() trackerdb.EncodedAccountsBatchIter {
	// TODO: catchpoint
	return nil
}

// MakeKVsIter implements trackerdb.Reader
func (*reader) MakeKVsIter(ctx context.Context) (trackerdb.KVsIter, error) {
	// TODO: catchpoint
	return nil, nil
}

type writer struct {
	primary   trackerdb.Writer
	secondary trackerdb.Writer
}

// MakeAccountsOptimizedWriter implements trackerdb.Writer
func (w *writer) MakeAccountsOptimizedWriter(hasAccounts, hasResources, hasKvPairs, hasCreatables bool) (trackerdb.AccountsWriter, error) {
	primary, errP := w.primary.MakeAccountsOptimizedWriter(hasAccounts, hasResources, hasKvPairs, hasCreatables)
	secondary, errS := w.secondary.MakeAccountsOptimizedWriter(hasAccounts, hasResources, hasKvPairs, hasCreatables)
	err := coalesceErrors(errP, errS)
	if err != nil {
		return nil, err
	}
	return &accountsWriter{primary, secondary}, nil
}

// MakeAccountsWriter implements trackerdb.Writer
func (w *writer) MakeAccountsWriter() (trackerdb.AccountsWriterExt, error) {
	primary, errP := w.primary.MakeAccountsWriter()
	secondary, errS := w.secondary.MakeAccountsWriter()
	err := coalesceErrors(errP, errS)
	if err != nil {
		return nil, err
	}
	return &accountsWriterExt{primary, secondary}, nil
}

// MakeOnlineAccountsOptimizedWriter implements trackerdb.Writer
func (w *writer) MakeOnlineAccountsOptimizedWriter(hasAccounts bool) (trackerdb.OnlineAccountsWriter, error) {
	primary, errP := w.primary.MakeOnlineAccountsOptimizedWriter(hasAccounts)
	secondary, errS := w.secondary.MakeOnlineAccountsOptimizedWriter(hasAccounts)
	err := coalesceErrors(errP, errS)
	if err != nil {
		return nil, err
	}
	return &onlineAccountsWriter{primary, secondary}, nil
}

// MakeSpVerificationCtxWriter implements trackerdb.Writer
func (w *writer) MakeSpVerificationCtxWriter() trackerdb.SpVerificationCtxWriter {
	primary := w.primary.MakeSpVerificationCtxWriter()
	secondary := w.secondary.MakeSpVerificationCtxWriter()
	return &stateproofWriter{primary, secondary}
}

// Testing implements trackerdb.Writer
func (w *writer) Testing() trackerdb.WriterTestExt {
	primary := w.primary.Testing()
	secondary := w.secondary.Testing()
	return &writerForTesting{primary, secondary}
}

type catchpoint struct {
	primary   trackerdb.Catchpoint
	secondary trackerdb.Catchpoint
}

// MakeCatchpointReaderWriter implements trackerdb.Catchpoint
func (*catchpoint) MakeCatchpointReaderWriter() (trackerdb.CatchpointReaderWriter, error) {
	// TODO: catchpoint
	return nil, nil
}

// MakeCatchpointWriter implements trackerdb.Catchpoint
func (*catchpoint) MakeCatchpointWriter() (trackerdb.CatchpointWriter, error) {
	// TODO: catchpoint
	return nil, nil
}

// MakeMerkleCommitter implements trackerdb.Catchpoint
func (*catchpoint) MakeMerkleCommitter(staging bool) (trackerdb.MerkleCommitter, error) {
	// TODO: catchpoint
	return nil, nil
}

// MakeOrderedAccountsIter implements trackerdb.Catchpoint
func (*catchpoint) MakeOrderedAccountsIter(accountCount int) trackerdb.OrderedAccountsIter {
	// TODO: catchpoint
	return nil
}

type batch struct {
	primary   trackerdb.Batch
	secondary trackerdb.Batch
	trackerdb.Writer
}

// ResetTransactionWarnDeadline implements trackerdb.Batch
func (b *batch) ResetTransactionWarnDeadline(ctx context.Context, deadline time.Time) (prevDeadline time.Time, err error) {
	_, _ = b.primary.ResetTransactionWarnDeadline(ctx, deadline)
	_, _ = b.secondary.ResetTransactionWarnDeadline(ctx, deadline)
	// ignore results, this is very engine specific
	return
}

// Commit implements trackerdb.Batch
func (b *batch) Commit() error {
	errP := b.primary.Commit()
	errS := b.secondary.Commit()
	// errors are unlikely to match between engines
	return coalesceErrors(errP, errS)
}

// Close implements trackerdb.Batch
func (b *batch) Close() error {
	errP := b.primary.Close()
	errS := b.secondary.Close()
	// errors are unlikely to match between engines
	return coalesceErrors(errP, errS)
}

type transaction struct {
	primary   trackerdb.Transaction
	secondary trackerdb.Transaction
	trackerdb.Reader
	trackerdb.Writer
	trackerdb.Catchpoint
}

// ResetTransactionWarnDeadline implements trackerdb.Transaction
func (tx *transaction) ResetTransactionWarnDeadline(ctx context.Context, deadline time.Time) (prevDeadline time.Time, err error) {
	_, _ = tx.primary.ResetTransactionWarnDeadline(ctx, deadline)
	_, _ = tx.secondary.ResetTransactionWarnDeadline(ctx, deadline)
	// ignore results, this is very engine specific
	return
}

// RunMigrations implements trackerdb.Transaction
func (tx *transaction) RunMigrations(ctx context.Context, params trackerdb.Params, log logging.Logger, targetVersion int32) (mgr trackerdb.InitParams, err error) {
	paramsP, errP := tx.primary.RunMigrations(ctx, params, log, targetVersion)
	paramsS, errS := tx.secondary.RunMigrations(ctx, params, log, targetVersion)
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results
	if paramsP != paramsS {
		err = ErrInconsistentResult
		return
	}
	// return primary result
	return paramsP, nil
}

// Commit implements trackerdb.Transaction
func (tx *transaction) Commit() error {
	errP := tx.primary.Commit()
	errS := tx.secondary.Commit()
	// errors are unlikely to match between engines
	return coalesceErrors(errP, errS)
}

// Close implements trackerdb.Transaction
func (tx *transaction) Close() error {
	errP := tx.primary.Close()
	errS := tx.secondary.Close()
	// errors are unlikely to match between engines
	return coalesceErrors(errP, errS)
}

type snapshot struct {
	primary   trackerdb.Snapshot
	secondary trackerdb.Snapshot
	trackerdb.Reader
}

// ResetTransactionWarnDeadline implements trackerdb.Snapshot
func (s *snapshot) ResetTransactionWarnDeadline(ctx context.Context, deadline time.Time) (prevDeadline time.Time, err error) {
	_, _ = s.primary.ResetTransactionWarnDeadline(ctx, deadline)
	_, _ = s.secondary.ResetTransactionWarnDeadline(ctx, deadline)
	// ignore results, this is very engine specific
	return
}

// Close implements trackerdb.Snapshot
func (s *snapshot) Close() error {
	errP := s.primary.Close()
	errS := s.secondary.Close()
	// errors are unlikely to match between engines
	return coalesceErrors(errP, errS)
}

//
// refs
//

type accountRef struct {
	primary   trackerdb.AccountRef
	secondary trackerdb.AccountRef
}

// AccountRefMarker implements trackerdb.AccountRef
func (accountRef) AccountRefMarker() {}
func (ref accountRef) String() string {
	return fmt.Sprintf("accountRef{primary: %s, secondary: %s}", ref.primary.String(), ref.secondary.String())
}

func coalesceAccountRefs(primary, secondary trackerdb.AccountRef) (trackerdb.AccountRef, error) {
	if primary != nil && secondary != nil {
		return accountRef{primary, secondary}, nil
	} else if primary == nil && secondary == nil {
		// all good, ref is nil
		return nil, nil
	} else {
		// ref mismatch
		return nil, ErrInconsistentResult
	}
}

type onlineAccountRef struct {
	primary   trackerdb.OnlineAccountRef
	secondary trackerdb.OnlineAccountRef
}

// OnlineAccountRefMarker implements trackerdb.OnlineAccountRef
func (onlineAccountRef) OnlineAccountRefMarker() {}

func coalesceOnlineAccountRefs(primary, secondary trackerdb.OnlineAccountRef) (trackerdb.OnlineAccountRef, error) {
	if primary != nil && secondary != nil {
		return onlineAccountRef{primary, secondary}, nil
	} else if primary == nil && secondary == nil {
		// all good, ref is nil
		return nil, nil
	} else {
		// ref mismatch
		return nil, ErrInconsistentResult
	}
}

type resourceRef struct {
	primary   trackerdb.ResourceRef
	secondary trackerdb.ResourceRef
}

// ResourceRefMarker implements trackerdb.ResourceRef
func (resourceRef) ResourceRefMarker() {}

type creatableRef struct {
	primary   trackerdb.CreatableRef
	secondary trackerdb.CreatableRef
}

// CreatableRefMarker implements trackerdb.CreatableRef
func (creatableRef) CreatableRefMarker() {}

//
// helpers
//

func coalesceErrors(errP error, errS error) error {
	if errP == nil && errS != nil {
		logging.Base().Error("secondary engine error, ", errS)
		return ErrInconsistentResult
	}
	if errP != nil && errS == nil {
		logging.Base().Error("primary engine error, ", errP)
		return ErrInconsistentResult
	}
	// happy case (no errors)
	if errP == nil && errS == nil {
		return nil
	}
	// happy case (both errored)
	return errP
}