-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmysql.go
562 lines (488 loc) · 18.1 KB
/
mysql.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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Copyright 2024 The Tessera authors. All Rights Reserved.
//
// Licensed 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 mysql contains a MySQL-based storage implementation for Tessera.
package mysql
import (
"bytes"
"context"
"crypto/sha256"
"database/sql"
"errors"
"fmt"
"io/fs"
"os"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/transparency-dev/merkle/rfc6962"
tessera "github.com/transparency-dev/trillian-tessera"
"github.com/transparency-dev/trillian-tessera/api"
"github.com/transparency-dev/trillian-tessera/api/layout"
options "github.com/transparency-dev/trillian-tessera/internal/options"
storage "github.com/transparency-dev/trillian-tessera/storage/internal"
"k8s.io/klog/v2"
)
const (
selectCompatibilityVersionSQL = "SELECT `compatibilityVersion` FROM `Tessera` WHERE `id` = 0"
selectCheckpointByIDSQL = "SELECT `note`, `published_at` FROM `Checkpoint` WHERE `id` = ?"
selectCheckpointByIDForUpdateSQL = selectCheckpointByIDSQL + " FOR UPDATE"
replaceCheckpointSQL = "REPLACE INTO `Checkpoint` (`id`, `note`, `published_at`) VALUES (?, ?, ?)"
selectTreeStateByIDSQL = "SELECT `size`, `root` FROM `TreeState` WHERE `id` = ?"
selectTreeStateByIDForUpdateSQL = selectTreeStateByIDSQL + " FOR UPDATE"
replaceTreeStateSQL = "REPLACE INTO `TreeState` (`id`, `size`, `root`) VALUES (?, ?, ?)"
selectSubtreeByLevelAndIndexSQL = "SELECT `nodes` FROM `Subtree` WHERE `level` = ? AND `index` = ?"
replaceSubtreeSQL = "REPLACE INTO `Subtree` (`level`, `index`, `nodes`) VALUES (?, ?, ?)"
selectTiledLeavesSQL = "SELECT `size`, `data` FROM `TiledLeaves` WHERE `tile_index` = ?"
replaceTiledLeavesSQL = "REPLACE INTO `TiledLeaves` (`tile_index`, `size`, `data`) VALUES (?, ?, ?)"
checkpointID = 0
treeStateID = 0
schemaCompatibilityVersion = 1
minCheckpointInterval = time.Second
)
// Storage is a MySQL-based storage implementation for Tessera.
type Storage struct {
db *sql.DB
queue *storage.Queue
newCheckpoint options.NewCPFunc
cpUpdated chan struct{}
}
// New creates a new instance of the MySQL-based Storage.
// Note that `tessera.WithCheckpointSigner()` is mandatory in the `opts` argument.
func New(ctx context.Context, db *sql.DB, opts ...func(*options.StorageOptions)) (tessera.Driver, error) {
opt := storage.ResolveStorageOptions(opts...)
if opt.CheckpointInterval < minCheckpointInterval {
return nil, fmt.Errorf("requested CheckpointInterval too low - %v < %v", opt.CheckpointInterval, minCheckpointInterval)
}
s := &Storage{
db: db,
newCheckpoint: opt.NewCP,
cpUpdated: make(chan struct{}, 1),
}
if err := s.db.Ping(); err != nil {
klog.Errorf("Failed to ping database: %v", err)
return nil, err
}
if s.newCheckpoint == nil {
return nil, errors.New("tessera.WithCheckpointSigner must be provided in New()")
}
if err := s.ensureVersion(ctx, schemaCompatibilityVersion); err != nil {
return nil, fmt.Errorf("incompatible schema version: %v", err)
}
s.queue = storage.NewQueue(ctx, opt.BatchMaxAge, opt.BatchMaxSize, s.sequenceBatch)
if err := s.maybeInitTree(ctx); err != nil {
return nil, fmt.Errorf("maybeInitTree: %v", err)
}
go func(ctx context.Context, i time.Duration) {
t := time.NewTicker(i)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-s.cpUpdated:
case <-t.C:
}
if err := s.publishCheckpoint(ctx, i); err != nil {
klog.Warningf("publishCheckpoint: %v", err)
}
}
}(ctx, opt.CheckpointInterval)
return s, nil
}
func (s *Storage) ensureVersion(ctx context.Context, wantVersion uint8) error {
row := s.db.QueryRowContext(ctx, selectCompatibilityVersionSQL)
if row.Err() != nil {
return row.Err()
}
var gotVersion uint8
if err := row.Scan(&gotVersion); err != nil {
return fmt.Errorf("failed to read Tessera version from DB: %v", err)
}
if gotVersion != wantVersion {
return fmt.Errorf("DB has Tessera compatibility version of %d, but version %d required", gotVersion, wantVersion)
}
return nil
}
// maybeInitTree will insert an initial "empty tree" row into the
// TreeState table iff no row already exists.
//
// This method doesn't also publish this new empty tree as a Checkpoint,
// rather, such a checkpoint will be published asynchronously by the
// same mechanism used to publish future checkpoints. Although in _this_
// case it would be expected to happen in very short order given that it's
// likely that no row currently exists in the Checkpoints table either.
func (s *Storage) maybeInitTree(ctx context.Context) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("being tx init tree state: %v", err)
}
defer func() {
if err := tx.Rollback(); err != nil && err != sql.ErrTxDone {
klog.Errorf("Failed to rollback in write initial tree state: %v", err)
}
}()
treeState, err := s.readTreeState(ctx, tx)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
klog.Errorf("Failed to read tree state: %v", err)
return err
}
if treeState == nil {
klog.Infof("Initializing tree state")
if err := s.writeTreeState(ctx, tx, 0, rfc6962.DefaultHasher.EmptyRoot()); err != nil {
klog.Errorf("Failed to write initial tree state: %v", err)
return err
}
// Only need to commit if we've actually initialised the tree state, otherwise we'll
// rely on the defer'd rollback to tidy up.
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit init tree state: %v", err)
}
s.cpUpdated <- struct{}{}
}
return nil
}
// ReadCheckpoint returns the latest stored checkpoint.
// If the checkpoint is not found, it returns os.ErrNotExist.
func (s *Storage) ReadCheckpoint(ctx context.Context) ([]byte, error) {
row := s.db.QueryRowContext(ctx, selectCheckpointByIDSQL, checkpointID)
if err := row.Err(); err != nil {
return nil, err
}
var checkpoint []byte
var at int64
if err := row.Scan(&checkpoint, &at); err != nil {
if err == sql.ErrNoRows {
return nil, os.ErrNotExist
}
return nil, fmt.Errorf("scan checkpoint: %v", err)
}
return checkpoint, nil
}
// publishCheckpoint creates a new checkpoint for the given size and root hash, and stores it in the
// Checkpoint table.
func (s *Storage) publishCheckpoint(ctx context.Context, interval time.Duration) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin tx: %v", err)
}
defer func() {
if err := tx.Rollback(); err != nil && err != sql.ErrTxDone {
klog.Warningf("publishCheckpoint rollback failed: %v", err)
}
}()
var note string
var at int64
if err := tx.QueryRowContext(ctx, selectCheckpointByIDForUpdateSQL, checkpointID).Scan(¬e, &at); err != nil && !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("scan checkpoint: %v", err)
}
if time.Since(time.UnixMilli(at)) < interval {
// Too soon, try again later.
klog.V(1).Info("skipping publish - too soon")
return nil
}
treeState, err := s.readTreeState(ctx, tx)
if err != nil {
return fmt.Errorf("readTreeState: %v", err)
}
rawCheckpoint, err := s.newCheckpoint(treeState.size, treeState.root)
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, replaceCheckpointSQL, checkpointID, rawCheckpoint, time.Now().UnixMilli()); err != nil {
return err
}
return tx.Commit()
}
type treeState struct {
size uint64
root []byte
}
// readTreeState returns the currently stored tree state information.
// If there is no stored tree state, it returns os.ErrNotExist.
func (s *Storage) readTreeState(ctx context.Context, tx *sql.Tx) (*treeState, error) {
row := tx.QueryRowContext(ctx, selectTreeStateByIDForUpdateSQL, treeStateID)
if err := row.Err(); err != nil {
return nil, err
}
r := &treeState{}
if err := row.Scan(&r.size, &r.root); err != nil {
if err == sql.ErrNoRows {
return nil, os.ErrNotExist
}
return nil, fmt.Errorf("scan tree state: %v", err)
}
return r, nil
}
// writeTreeState updates the TreeState table with the new tree state information.
func (s *Storage) writeTreeState(ctx context.Context, tx *sql.Tx, size uint64, rootHash []byte) error {
if _, err := tx.ExecContext(ctx, replaceTreeStateSQL, treeStateID, size, rootHash); err != nil {
klog.Errorf("Failed to execute replaceTreeStateSQL: %v", err)
return err
}
return nil
}
// ReadTile returns a full tile or a partial tile at the given level, index and treeSize.
// If the tile is not found, it returns os.ErrNotExist.
//
// Note that if a partial tile is requested, but a larger tile is available, this
// will return the largest tile available. This could be trimmed to return only the
// number of entries specifically requested if this behaviour becomes problematic.
func (s *Storage) ReadTile(ctx context.Context, level, index uint64, p uint8) ([]byte, error) {
row := s.db.QueryRowContext(ctx, selectSubtreeByLevelAndIndexSQL, level, index)
if err := row.Err(); err != nil {
return nil, err
}
var tile []byte
if err := row.Scan(&tile); err != nil {
if err == sql.ErrNoRows {
return nil, os.ErrNotExist
}
return nil, fmt.Errorf("scan tile: %v", err)
}
numEntries := uint64(len(tile) / sha256.Size)
requestedEntries := uint64(p)
if requestedEntries == 0 {
requestedEntries = layout.TileWidth
}
if requestedEntries > numEntries {
// If the user has requested a size larger than we have, they can't have it
return nil, os.ErrNotExist
}
return tile, nil
}
// writeTile replaces the tile nodes at the given level and index.
func (s *Storage) writeTile(ctx context.Context, tx *sql.Tx, level, index uint64, nodes []byte) error {
if _, err := tx.ExecContext(ctx, replaceSubtreeSQL, level, index, nodes); err != nil {
klog.Errorf("Failed to execute replaceSubtreeSQL: %v", err)
return err
}
return nil
}
// ReadEntryBundle returns the log entries at the given index.
// If the entry bundle is not found, it returns os.ErrNotExist.
//
// Note that if a partial tile is requested, but a larger tile is available, this
// will return the largest tile available. This could be trimmed to return only the
// number of entries specifically requested if this behaviour becomes problematic.
func (s *Storage) ReadEntryBundle(ctx context.Context, index uint64, p uint8) ([]byte, error) {
row := s.db.QueryRowContext(ctx, selectTiledLeavesSQL, index)
if err := row.Err(); err != nil {
return nil, err
}
var size uint32
var entryBundle []byte
if err := row.Scan(&size, &entryBundle); err != nil {
if err == sql.ErrNoRows {
return nil, os.ErrNotExist
}
return nil, fmt.Errorf("scan entry bundle: %v", err)
}
requestedSize := uint32(p)
if requestedSize == 0 {
requestedSize = layout.EntryBundleWidth
}
if requestedSize > size {
return nil, fmt.Errorf("bundle with %d entries requested, but only %d available: %w", requestedSize, size, os.ErrNotExist)
}
return entryBundle, nil
}
func (s *Storage) writeEntryBundle(ctx context.Context, tx *sql.Tx, index uint64, size uint32, entryBundle []byte) error {
if _, err := tx.ExecContext(ctx, replaceTiledLeavesSQL, index, size, entryBundle); err != nil {
klog.Errorf("Failed to execute replaceTiledLeavesSQL: %v", err)
return err
}
return nil
}
// Add is the entrypoint for adding entries to a sequencing log.
func (s *Storage) Add(ctx context.Context, entry *tessera.Entry) tessera.IndexFuture {
return s.queue.Add(ctx, entry)
}
// sequenceBatch writes the entries from the provided batch into the entry bundle files of the log.
//
// This func starts filling entries bundles at the next available slot in the log, ensuring that the
// sequenced entries are contiguous from the zeroth entry (i.e left-hand dense).
// We try to minimise the number of partially complete entry bundles by writing entries in chunks rather
// than one-by-one.
//
// TODO(#21): Separate sequencing and integration for better performance.
func (s *Storage) sequenceBatch(ctx context.Context, entries []*tessera.Entry) error {
// Return when there is no entry to sequence.
if len(entries) == 0 {
return nil
}
// Get a Tx for making transaction requests.
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin tx: %v", err)
}
// Defer a rollback in case anything fails.
defer func() {
if err := tx.Rollback(); err != nil && err != sql.ErrTxDone {
klog.Errorf("Failed to rollback in sequenceBatch: %v", err)
}
}()
// Get tree size. Note that "SELECT ... FOR UPDATE" is used for row-level locking.
row := tx.QueryRowContext(ctx, selectTreeStateByIDForUpdateSQL, treeStateID)
if err := row.Err(); err != nil {
return fmt.Errorf("select tree state: %v", err)
}
state := treeState{}
if err := row.Scan(&state.size, &state.root); err != nil {
return fmt.Errorf("failed to read tree state: %w", err)
}
// Integrate the new entries into the entry bundle (TiledLeaves table) and tile (Subtree table).
if err := s.appendEntries(ctx, tx, state.size, entries); err != nil {
return fmt.Errorf("failed to integrate: %w", err)
}
// Commit the transaction.
err = tx.Commit()
select {
case s.cpUpdated <- struct{}{}:
default:
}
return err
}
// appendEntries incorporates the provided entries into the log starting at fromSeq.
func (s *Storage) appendEntries(ctx context.Context, tx *sql.Tx, fromSeq uint64, entries []*tessera.Entry) error {
sequencedEntries := make([]storage.SequencedEntry, len(entries))
// Assign provisional sequence numbers to entries.
// We need to do this here in order to support serialisations which include the log position.
for i, e := range entries {
sequencedEntries[i] = storage.SequencedEntry{
BundleData: e.MarshalBundleData(fromSeq + uint64(i)),
LeafHash: e.LeafHash(),
}
}
// Add sequenced entries to entry bundles.
bundleIndex, entriesInBundle := fromSeq/layout.EntryBundleWidth, fromSeq%layout.EntryBundleWidth
bundleWriter := &bytes.Buffer{}
// If the latest bundle is partial, we need to read the data it contains in for our newer, larger, bundle.
if entriesInBundle > 0 {
row := tx.QueryRowContext(ctx, selectTiledLeavesSQL, bundleIndex)
if err := row.Err(); err != nil {
return fmt.Errorf("query tiled leaves: %v", err)
}
var size uint32
var partialEntryBundle []byte
if err := row.Scan(&size, &partialEntryBundle); err != nil {
return fmt.Errorf("scan partial entry bundle: %w", err)
}
if size != uint32(entriesInBundle) {
return fmt.Errorf("expected %d entries in storage but found %d", entriesInBundle, size)
}
if _, err := bundleWriter.Write(partialEntryBundle); err != nil {
return fmt.Errorf("write partial entry bundle: %w", err)
}
}
// Add new entries to the bundle.
for _, e := range sequencedEntries {
if _, err := bundleWriter.Write(e.BundleData); err != nil {
return fmt.Errorf("write bundle data: %w", err)
}
entriesInBundle++
// This bundle is full, so we need to write it out.
if entriesInBundle == layout.EntryBundleWidth {
if err := s.writeEntryBundle(ctx, tx, bundleIndex, uint32(entriesInBundle), bundleWriter.Bytes()); err != nil {
return fmt.Errorf("writeEntryBundle: %w", err)
}
// Prepare the next entry bundle for any remaining entries in the batch.
bundleIndex++
entriesInBundle = 0
bundleWriter = &bytes.Buffer{}
}
}
// If we have a partial bundle remaining once we've added all the entries from the batch,
// this needs writing out too.
if entriesInBundle > 0 {
if err := s.writeEntryBundle(ctx, tx, bundleIndex, uint32(entriesInBundle), bundleWriter.Bytes()); err != nil {
return fmt.Errorf("writeEntryBundle: %w", err)
}
}
lh := make([][]byte, len(sequencedEntries))
for i, e := range sequencedEntries {
lh[i] = e.LeafHash
}
newSize, newRoot, err := s.integrate(ctx, tx, fromSeq, lh)
if err != nil {
return fmt.Errorf("integrate: %v", err)
}
// Write new tree state.
if err := s.writeTreeState(ctx, tx, newSize, newRoot); err != nil {
return fmt.Errorf("writeCheckpoint: %w", err)
}
klog.Infof("New tree: %d, %x", newSize, newRoot)
return nil
}
// integrate adds the provided leaf hashes to the merkle tree, starting at the provided location.
func (s *Storage) integrate(ctx context.Context, tx *sql.Tx, fromSeq uint64, lh [][]byte) (uint64, []byte, error) {
getTiles := func(ctx context.Context, tileIDs []storage.TileID, treeSize uint64) ([]*api.HashTile, error) {
hashTiles := make([]*api.HashTile, len(tileIDs))
if len(tileIDs) == 0 {
return hashTiles, nil
}
// Build the SQL and args to fetch the hash tiles.
var sql strings.Builder
args := make([]any, 0, len(tileIDs)*2)
for i, id := range tileIDs {
if i != 0 {
sql.WriteString(" UNION ALL ")
}
_, err := sql.WriteString(selectSubtreeByLevelAndIndexSQL)
if err != nil {
return nil, err
}
args = append(args, id.Level, id.Index)
}
rows, err := tx.QueryContext(ctx, sql.String(), args...)
if err != nil {
return nil, fmt.Errorf("failed to query the hash tiles with SQL (%s): %w", sql.String(), err)
}
defer func() {
if err := rows.Close(); err != nil {
klog.Warningf("Failed to close the rows: %v", err)
}
}()
i := 0
for rows.Next() {
var tile []byte
if err := rows.Scan(&tile); err != nil {
return nil, fmt.Errorf("scan subtree tile: %w", err)
}
t := &api.HashTile{}
if err := t.UnmarshalText(tile); err != nil {
return nil, fmt.Errorf("unmarshal tile: %w", err)
}
hashTiles[i] = t
i++
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("rows error while fetching subtrees: %w", err)
}
return hashTiles, nil
}
newSize, newRoot, tiles, err := storage.Integrate(ctx, getTiles, fromSeq, lh)
if err != nil {
return 0, nil, fmt.Errorf("storage.Integrate: %v", err)
}
for k, v := range tiles {
nodes, err := v.MarshalText()
if err != nil {
return 0, nil, err
}
if err := s.writeTile(ctx, tx, uint64(k.Level), k.Index, nodes); err != nil {
return 0, nil, fmt.Errorf("failed to set tile(%v): %w", k, err)
}
}
return newSize, newRoot, nil
}