-
Notifications
You must be signed in to change notification settings - Fork 374
/
log_storage.go
888 lines (780 loc) · 30.4 KB
/
log_storage.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
// Copyright 2016 Trillian Authors
//
// 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 crdb
import (
"bytes"
"context"
"database/sql"
"errors"
"fmt"
"sort"
"strconv"
"sync"
"time"
"github.com/transparency-dev/merkle/compact"
"github.com/transparency-dev/merkle/rfc6962"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"k8s.io/klog/v2"
"github.com/google/trillian"
"github.com/google/trillian/monitoring"
"github.com/google/trillian/storage"
"github.com/google/trillian/storage/cache"
"github.com/google/trillian/storage/tree"
"github.com/google/trillian/types"
)
const (
valuesPlaceholder5 = "($1,$2,$3,$4,$5)"
insertLeafDataSQL = "INSERT INTO LeafData(TreeId,LeafIdentityHash,LeafValue,ExtraData,QueueTimestampNanos) VALUES" + valuesPlaceholder5
insertSequencedLeafSQL = "INSERT INTO SequencedLeafData(TreeId,LeafIdentityHash,MerkleLeafHash,SequenceNumber,IntegrateTimestampNanos) VALUES"
selectNonDeletedTreeIDByTypeAndStateSQL = `
SELECT TreeId FROM Trees
WHERE TreeType IN($1,$2)
AND TreeState IN($3,$4)
AND (Deleted IS NULL OR Deleted = 'false')`
selectLatestSignedLogRootSQL = `SELECT TreeHeadTimestamp,TreeSize,RootHash,TreeRevision,RootSignature
FROM TreeHead WHERE TreeId=$1
ORDER BY TreeHeadTimestamp DESC LIMIT 1`
selectLeavesByRangeSQL = `SELECT s.MerkleLeafHash,l.LeafIdentityHash,l.LeafValue,s.SequenceNumber,l.ExtraData,l.QueueTimestampNanos,s.IntegrateTimestampNanos
FROM LeafData l,SequencedLeafData s
WHERE l.LeafIdentityHash = s.LeafIdentityHash
AND s.SequenceNumber >= $1 AND s.SequenceNumber < $2 AND l.TreeId = $3 AND s.TreeId = l.TreeId` + orderBySequenceNumberSQL
// These statements need to be expanded to provide the correct number of parameter placeholders.
// Note that this uses the MySQL-specific marker syntax here, but is eventually replaced with
// the postgres syntax in getStmt.
selectLeavesByMerkleHashSQL = `SELECT s.MerkleLeafHash,l.LeafIdentityHash,l.LeafValue,s.SequenceNumber,l.ExtraData,l.QueueTimestampNanos,s.IntegrateTimestampNanos
FROM LeafData l,SequencedLeafData s
WHERE l.LeafIdentityHash = s.LeafIdentityHash
AND s.MerkleLeafHash IN (` + placeholderSQL + `) AND l.TreeId = ? AND s.TreeId = l.TreeId`
// TODO(#1548): rework the code so the dummy hash isn't needed (e.g. this assumes hash size is 32)
dummyMerkleLeafHash = "00000000000000000000000000000000"
// This statement returns a dummy Merkle leaf hash value (which must be
// of the right size) so that its signature matches that of the other
// leaf-selection statements.
// Note that this uses the MySQL-specific marker syntax here, but is eventually replaced with
// the postgres syntax in getStmt.
selectLeavesByLeafIdentityHashSQL = `SELECT '` + dummyMerkleLeafHash + `',l.LeafIdentityHash,l.LeafValue,-1,l.ExtraData,l.QueueTimestampNanos,s.IntegrateTimestampNanos
FROM LeafData l LEFT JOIN SequencedLeafData s ON (l.LeafIdentityHash = s.LeafIdentityHash AND l.TreeID = s.TreeID)
WHERE l.LeafIdentityHash IN (` + placeholderSQL + `) AND l.TreeId = ?`
// Same as above except with leaves ordered by sequence so we only incur this cost when necessary
orderBySequenceNumberSQL = " ORDER BY s.SequenceNumber"
selectLeavesByMerkleHashOrderedBySequenceSQL = selectLeavesByMerkleHashSQL + orderBySequenceNumberSQL
logIDLabel = "logid"
)
var (
once sync.Once
queuedCounter monitoring.Counter
queuedDupCounter monitoring.Counter
dequeuedCounter monitoring.Counter
queueLatency monitoring.Histogram
queueInsertLatency monitoring.Histogram
queueReadLatency monitoring.Histogram
queueInsertLeafLatency monitoring.Histogram
queueInsertEntryLatency monitoring.Histogram
dequeueLatency monitoring.Histogram
dequeueSelectLatency monitoring.Histogram
dequeueRemoveLatency monitoring.Histogram
)
func createMetrics(mf monitoring.MetricFactory) {
queuedCounter = mf.NewCounter("crdb_queued_leaves", "Number of leaves queued", logIDLabel)
queuedDupCounter = mf.NewCounter("crdb_queued_dup_leaves", "Number of duplicate leaves queued", logIDLabel)
dequeuedCounter = mf.NewCounter("crdb_dequeued_leaves", "Number of leaves dequeued", logIDLabel)
queueLatency = mf.NewHistogram("crdb_queue_leaves_latency", "Latency of queue leaves operation in seconds", logIDLabel)
queueInsertLatency = mf.NewHistogram("crdb_queue_leaves_latency_insert", "Latency of insertion part of queue leaves operation in seconds", logIDLabel)
queueReadLatency = mf.NewHistogram("crdb_queue_leaves_latency_read_dups", "Latency of read-duplicates part of queue leaves operation in seconds", logIDLabel)
queueInsertLeafLatency = mf.NewHistogram("crdb_queue_leaf_latency_leaf", "Latency of insert-leaf part of queue (single) leaf operation in seconds", logIDLabel)
queueInsertEntryLatency = mf.NewHistogram("crdb_queue_leaf_latency_entry", "Latency of insert-entry part of queue (single) leaf operation in seconds", logIDLabel)
dequeueLatency = mf.NewHistogram("crdb_dequeue_leaves_latency", "Latency of dequeue leaves operation in seconds", logIDLabel)
dequeueSelectLatency = mf.NewHistogram("crdb_dequeue_leaves_latency_select", "Latency of selection part of dequeue leaves operation in seconds", logIDLabel)
dequeueRemoveLatency = mf.NewHistogram("crdb_dequeue_leaves_latency_remove", "Latency of removal part of dequeue leaves operation in seconds", logIDLabel)
}
func labelForTX(t *logTreeTX) string {
return strconv.FormatInt(t.treeID, 10)
}
func observe(hist monitoring.Histogram, duration time.Duration, label string) {
hist.Observe(duration.Seconds(), label)
}
type crdbLogStorage struct {
*crdbTreeStorage
admin storage.AdminStorage
metricFactory monitoring.MetricFactory
}
// NewLogStorage creates a storage.LogStorage instance for the specified CockroachDB URL.
// It assumes storage.AdminStorage is backed by the same CockroachDB database as well.
func NewLogStorage(db *sql.DB, mf monitoring.MetricFactory) storage.LogStorage {
if mf == nil {
mf = monitoring.InertMetricFactory{}
}
return &crdbLogStorage{
admin: NewSQLAdminStorage(db),
crdbTreeStorage: newTreeStorage(db),
metricFactory: mf,
}
}
func (m *crdbLogStorage) CheckDatabaseAccessible(ctx context.Context) error {
return m.db.PingContext(ctx)
}
func (m *crdbLogStorage) getLeavesByMerkleHashStmt(ctx context.Context, num int, orderBySequence bool) (*sql.Stmt, error) {
if orderBySequence {
return m.getStmt(ctx, selectLeavesByMerkleHashOrderedBySequenceSQL, num, "?", "?")
}
return m.getStmt(ctx, selectLeavesByMerkleHashSQL, num, "?", "?")
}
func (m *crdbLogStorage) getLeavesByLeafIdentityHashStmt(ctx context.Context, num int) (*sql.Stmt, error) {
return m.getStmt(ctx, selectLeavesByLeafIdentityHashSQL, num, "?", "?")
}
func (m *crdbLogStorage) GetActiveLogIDs(ctx context.Context) ([]int64, error) {
// Include logs that are DRAINING in the active list as we're still
// integrating leaves into them.
rows, err := m.db.QueryContext(
ctx, selectNonDeletedTreeIDByTypeAndStateSQL,
trillian.TreeType_LOG.String(), trillian.TreeType_PREORDERED_LOG.String(),
trillian.TreeState_ACTIVE.String(), trillian.TreeState_DRAINING.String())
if err != nil {
return nil, err
}
defer func() {
if err := rows.Close(); err != nil {
klog.Errorf("rows.Close(): %v", err)
}
}()
ids := []int64{}
for rows.Next() {
var treeID int64
if err := rows.Scan(&treeID); err != nil {
return nil, err
}
ids = append(ids, treeID)
}
return ids, rows.Err()
}
func (m *crdbLogStorage) beginInternal(ctx context.Context, tree *trillian.Tree) (*logTreeTX, error) {
once.Do(func() {
createMetrics(m.metricFactory)
})
stCache := cache.NewLogSubtreeCache(rfc6962.DefaultHasher)
ttx, err := m.beginTreeTx(ctx, tree, rfc6962.DefaultHasher.Size(), stCache)
if err != nil && err != storage.ErrTreeNeedsInit {
return nil, err
}
ltx := &logTreeTX{
treeTX: ttx,
ls: m,
dequeued: make(map[string]dequeuedLeaf),
}
ltx.slr, ltx.readRev, err = ltx.fetchLatestRoot(ctx)
if err == storage.ErrTreeNeedsInit {
ltx.treeTX.writeRevision = 0
return ltx, err
} else if err != nil {
if err := ttx.Close(); err != nil {
klog.Errorf("ttx.Close(): %v", err)
}
return nil, err
}
if err := ltx.root.UnmarshalBinary(ltx.slr.LogRoot); err != nil {
if err := ttx.Close(); err != nil {
klog.Errorf("ttx.Close(): %v", err)
}
return nil, err
}
ltx.treeTX.writeRevision = ltx.readRev + 1
return ltx, nil
}
// TODO(pavelkalinnikov): This and many other methods of this storage
// implementation can leak a specific sql.ErrTxDone all the way to the client,
// if the transaction is rolled back as a result of a canceled context. It must
// return "generic" errors, and only log the specific ones for debugging.
func (m *crdbLogStorage) ReadWriteTransaction(ctx context.Context, tree *trillian.Tree, f storage.LogTXFunc) error {
tx, err := m.beginInternal(ctx, tree)
if err != nil && err != storage.ErrTreeNeedsInit {
return err
}
defer func() {
if err := tx.Close(); err != nil {
klog.Errorf("tx.Close(): %v", err)
}
}()
if err := f(ctx, tx); err != nil {
return err
}
return tx.Commit(ctx)
}
func (m *crdbLogStorage) AddSequencedLeaves(ctx context.Context, tree *trillian.Tree, leaves []*trillian.LogLeaf, timestamp time.Time) ([]*trillian.QueuedLogLeaf, error) {
tx, err := m.beginInternal(ctx, tree)
if tx != nil {
// Ensure we don't leak the transaction. For example if we get an
// ErrTreeNeedsInit from beginInternal() or if AddSequencedLeaves fails
// below.
defer func() {
if err := tx.Close(); err != nil {
klog.Errorf("tx.Close(): %v", err)
}
}()
}
if err != nil {
return nil, err
}
res, err := tx.AddSequencedLeaves(ctx, leaves, timestamp)
if err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return nil, err
}
return res, nil
}
func (m *crdbLogStorage) SnapshotForTree(ctx context.Context, tree *trillian.Tree) (storage.ReadOnlyLogTreeTX, error) {
tx, err := m.beginInternal(ctx, tree)
if err != nil && err != storage.ErrTreeNeedsInit {
return nil, err
}
return tx, err
}
func (m *crdbLogStorage) QueueLeaves(ctx context.Context, tree *trillian.Tree, leaves []*trillian.LogLeaf, queueTimestamp time.Time) ([]*trillian.QueuedLogLeaf, error) {
tx, err := m.beginInternal(ctx, tree)
if tx != nil {
// Ensure we don't leak the transaction. For example if we get an
// ErrTreeNeedsInit from beginInternal() or if QueueLeaves fails
// below.
defer func() {
if err := tx.Close(); err != nil {
klog.Errorf("tx.Close(): %v", err)
}
}()
}
if err != nil {
return nil, err
}
existing, err := tx.QueueLeaves(ctx, leaves, queueTimestamp)
if err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return nil, err
}
ret := make([]*trillian.QueuedLogLeaf, len(leaves))
for i, e := range existing {
if e != nil {
ret[i] = &trillian.QueuedLogLeaf{
Leaf: e,
Status: status.Newf(codes.AlreadyExists, "leaf already exists: %v", e.LeafIdentityHash).Proto(),
}
continue
}
ret[i] = &trillian.QueuedLogLeaf{Leaf: leaves[i]}
}
return ret, nil
}
type logTreeTX struct {
treeTX
ls *crdbLogStorage
root types.LogRootV1
readRev int64
slr *trillian.SignedLogRoot
dequeued map[string]dequeuedLeaf
}
// GetMerkleNodes returns the requested nodes at the read revision.
func (t *logTreeTX) GetMerkleNodes(ctx context.Context, ids []compact.NodeID) ([]tree.Node, error) {
t.treeTX.mu.Lock()
defer t.treeTX.mu.Unlock()
return t.subtreeCache.GetNodes(ids, t.getSubtreesAtRev(ctx, t.readRev))
}
func (t *logTreeTX) DequeueLeaves(ctx context.Context, limit int, cutoffTime time.Time) ([]*trillian.LogLeaf, error) {
t.treeTX.mu.Lock()
defer t.treeTX.mu.Unlock()
if t.treeType == trillian.TreeType_PREORDERED_LOG {
// TODO(pavelkalinnikov): Optimize this by fetching only the required
// fields of LogLeaf. We can avoid joining with LeafData table here.
return t.getLeavesByRangeInternal(ctx, int64(t.root.TreeSize), int64(limit))
}
start := time.Now()
stx, err := t.tx.PrepareContext(ctx, selectQueuedLeavesSQL)
if err != nil {
klog.Warningf("Failed to prepare dequeue select: %s", err)
return nil, err
}
defer func() {
if err := stx.Close(); err != nil {
klog.Errorf("stx.Close(): %v", err)
}
}()
leaves := make([]*trillian.LogLeaf, 0, limit)
rows, err := stx.QueryContext(ctx, t.treeID, cutoffTime.UnixNano(), limit)
if err != nil {
klog.Warningf("Failed to select rows for work: %s", err)
return nil, err
}
defer func() {
if err := rows.Close(); err != nil {
klog.Errorf("rows.Close(): %v", err)
}
}()
for rows.Next() {
leaf, dqInfo, err := t.dequeueLeaf(rows)
if err != nil {
klog.Warningf("Error dequeuing leaf: %v", err)
return nil, err
}
if len(leaf.LeafIdentityHash) != t.hashSizeBytes {
return nil, errors.New("dequeued a leaf with incorrect hash size")
}
k := string(leaf.LeafIdentityHash)
if _, ok := t.dequeued[k]; ok {
// dupe, user probably called DequeueLeaves more than once.
continue
}
t.dequeued[k] = dqInfo
leaves = append(leaves, leaf)
}
if rows.Err() != nil {
return nil, rows.Err()
}
label := labelForTX(t)
observe(dequeueSelectLatency, time.Since(start), label)
observe(dequeueLatency, time.Since(start), label)
dequeuedCounter.Add(float64(len(leaves)), label)
return leaves, nil
}
// sortLeavesForInsert returns a slice containing the passed in leaves sorted
// by LeafIdentityHash, and paired with their original positions.
// QueueLeaves and AddSequencedLeaves use this to make the order that LeafData
// row locks are acquired deterministic and reduce the chance of deadlocks.
func sortLeavesForInsert(leaves []*trillian.LogLeaf) []leafAndPosition {
ordLeaves := make([]leafAndPosition, len(leaves))
for i, leaf := range leaves {
ordLeaves[i] = leafAndPosition{leaf: leaf, idx: i}
}
sort.Sort(byLeafIdentityHashWithPosition(ordLeaves))
return ordLeaves
}
func (t *logTreeTX) QueueLeaves(ctx context.Context, leaves []*trillian.LogLeaf, queueTimestamp time.Time) ([]*trillian.LogLeaf, error) {
t.treeTX.mu.Lock()
defer t.treeTX.mu.Unlock()
// Don't accept batches if any of the leaves are invalid.
for _, leaf := range leaves {
if len(leaf.LeafIdentityHash) != t.hashSizeBytes {
return nil, fmt.Errorf("queued leaf must have a leaf ID hash of length %d", t.hashSizeBytes)
}
leaf.QueueTimestamp = timestamppb.New(queueTimestamp)
if err := leaf.QueueTimestamp.CheckValid(); err != nil {
return nil, fmt.Errorf("got invalid queue timestamp: %w", err)
}
}
start := time.Now()
label := labelForTX(t)
ordLeaves := sortLeavesForInsert(leaves)
existingCount := 0
existingLeaves := make([]*trillian.LogLeaf, len(leaves))
// CockroachDB/Postgres will cancel a transaction if an insert
// statement is run with a duplicate key. This is not ideal for
// QueueLeaves, as we want to detect these errors in-code
// and return them as AlreadyExists errors and add metrics.
// Thus, we use a SAVEPOINT and rollback on duplicates.
const savepoint = "SAVEPOINT QueueLeaves"
if _, err := t.tx.ExecContext(ctx, savepoint); err != nil {
klog.Errorf("Error adding savepoint: %s", err)
return nil, crdbToGRPC(err)
}
for _, ol := range ordLeaves {
i, leaf := ol.idx, ol.leaf
leafStart := time.Now()
if err := leaf.QueueTimestamp.CheckValid(); err != nil {
return nil, fmt.Errorf("got invalid queue timestamp: %w", err)
}
qTimestamp := leaf.QueueTimestamp.AsTime()
if _, err := t.tx.ExecContext(ctx, savepoint); err != nil {
klog.Errorf("Error updating savepoint: %s", err)
return nil, crdbToGRPC(err)
}
_, err := t.tx.ExecContext(ctx, insertLeafDataSQL, t.treeID, leaf.LeafIdentityHash, leaf.LeafValue, leaf.ExtraData, qTimestamp.UnixNano())
insertDuration := time.Since(leafStart)
observe(queueInsertLeafLatency, insertDuration, label)
if isDuplicateErr(err) {
// Remember the duplicate leaf, using the requested leaf for now.
existingLeaves[i] = leaf
existingCount++
queuedDupCounter.Inc(label)
// Note: one must roll back since there are side-effects in the transaction
// in crdb/postgres
if _, err := t.tx.ExecContext(ctx, "ROLLBACK TO "+savepoint); err != nil {
klog.Errorf("Error rolling back to savepoint: %s", err)
return nil, crdbToGRPC(err)
}
continue
}
if err != nil {
klog.Warningf("Error inserting %d into LeafData: %s", i, err)
return nil, crdbToGRPC(err)
}
// Create the work queue entry
args := []interface{}{
t.treeID,
leaf.LeafIdentityHash,
leaf.MerkleLeafHash,
}
args = append(args, queueArgs(t.treeID, leaf.LeafIdentityHash, qTimestamp)...)
_, err = t.tx.ExecContext(
ctx,
insertUnsequencedEntrySQL,
args...,
)
if err != nil {
klog.Warningf("Error inserting into Unsequenced: %s", err)
return nil, crdbToGRPC(err)
}
leafDuration := time.Since(leafStart)
observe(queueInsertEntryLatency, (leafDuration - insertDuration), label)
}
insertDuration := time.Since(start)
observe(queueInsertLatency, insertDuration, label)
queuedCounter.Add(float64(len(leaves)), label)
if _, err := t.tx.ExecContext(ctx, "RELEASE "+savepoint); err != nil {
klog.Errorf("Error releasing savepoint: %s", err)
return nil, crdbToGRPC(err)
}
if existingCount == 0 {
return existingLeaves, nil
}
// For existing leaves, we need to retrieve the contents. First collate the desired LeafIdentityHash values.
var toRetrieve [][]byte
for _, existing := range existingLeaves {
if existing != nil {
toRetrieve = append(toRetrieve, existing.LeafIdentityHash)
}
}
results, err := t.getLeafDataByIdentityHash(ctx, toRetrieve)
if err != nil {
return nil, fmt.Errorf("failed to retrieve existing leaves: %v", err)
}
if len(results) != len(toRetrieve) {
return nil, fmt.Errorf("failed to retrieve all existing leaves: got %d, want %d", len(results), len(toRetrieve))
}
// Replace the requested leaves with the actual leaves.
for i, requested := range existingLeaves {
if requested == nil {
continue
}
found := false
for _, result := range results {
if bytes.Equal(result.LeafIdentityHash, requested.LeafIdentityHash) {
existingLeaves[i] = result
found = true
break
}
}
if !found {
return nil, fmt.Errorf("failed to find existing leaf for hash %x", requested.LeafIdentityHash)
}
}
totalDuration := time.Since(start)
readDuration := totalDuration - insertDuration
observe(queueReadLatency, readDuration, label)
observe(queueLatency, totalDuration, label)
return existingLeaves, nil
}
func (t *logTreeTX) AddSequencedLeaves(ctx context.Context, leaves []*trillian.LogLeaf, timestamp time.Time) ([]*trillian.QueuedLogLeaf, error) {
t.treeTX.mu.Lock()
defer t.treeTX.mu.Unlock()
res := make([]*trillian.QueuedLogLeaf, len(leaves))
ok := status.New(codes.OK, "OK").Proto()
// Leaves in this transaction are inserted in two tables. For each leaf, if
// one of the two inserts fails, we remove the side effect by rolling back to
// a savepoint installed before the first insert of the two.
const savepoint = "SAVEPOINT AddSequencedLeaves"
if _, err := t.tx.ExecContext(ctx, savepoint); err != nil {
klog.Errorf("Error adding savepoint: %s", err)
return nil, crdbToGRPC(err)
}
// TODO(pavelkalinnikov): Consider performance implication of executing this
// extra SAVEPOINT, especially for 1-entry batches. Optimize if necessary.
// Note: LeafData inserts are presumably protected from deadlocks due to
// sorting, but the order of the corresponding SequencedLeafData inserts
// becomes indeterministic. However, in a typical case when leaves are
// supplied in contiguous non-intersecting batches, the chance of having
// circular dependencies between transactions is significantly lower.
ordLeaves := sortLeavesForInsert(leaves)
for _, ol := range ordLeaves {
i, leaf := ol.idx, ol.leaf
// This should fail on insert, but catch it early.
if got, want := len(leaf.LeafIdentityHash), t.hashSizeBytes; got != want {
return nil, status.Errorf(codes.FailedPrecondition, "leaves[%d] has incorrect hash size %d, want %d", i, got, want)
}
if _, err := t.tx.ExecContext(ctx, savepoint); err != nil {
klog.Errorf("Error updating savepoint: %s", err)
return nil, crdbToGRPC(err)
}
res[i] = &trillian.QueuedLogLeaf{Status: ok}
// TODO(pavelkalinnikov): Measure latencies.
_, err := t.tx.ExecContext(ctx, insertLeafDataSQL,
t.treeID, leaf.LeafIdentityHash, leaf.LeafValue, leaf.ExtraData, timestamp.UnixNano())
// TODO(pavelkalinnikov): Detach PREORDERED_LOG integration latency metric.
// TODO(pavelkalinnikov): Support opting out from duplicates detection.
if isDuplicateErr(err) {
res[i].Status = status.New(codes.FailedPrecondition, "conflicting LeafIdentityHash").Proto()
// Note: one must roll back since there are side-effects in the transaction
// in crdb/postgres
if _, err := t.tx.ExecContext(ctx, "ROLLBACK TO "+savepoint); err != nil {
klog.Errorf("Error rolling back to savepoint: %s", err)
return nil, crdbToGRPC(err)
}
continue
} else if err != nil {
klog.Errorf("Error inserting leaves[%d] into LeafData: %s", i, err)
return nil, crdbToGRPC(err)
}
_, err = t.tx.ExecContext(ctx, insertSequencedLeafSQL+valuesPlaceholder5,
t.treeID, leaf.LeafIdentityHash, leaf.MerkleLeafHash, leaf.LeafIndex, 0)
// TODO(pavelkalinnikov): Update IntegrateTimestamp on integrating the leaf.
if isDuplicateErr(err) {
res[i].Status = status.New(codes.FailedPrecondition, "conflicting LeafIndex").Proto()
if _, err := t.tx.ExecContext(ctx, "ROLLBACK TO "+savepoint); err != nil {
klog.Errorf("Error rolling back to savepoint: %s", err)
return nil, crdbToGRPC(err)
}
} else if err != nil {
klog.Errorf("Error inserting leaves[%d] into SequencedLeafData: %s", i, err)
return nil, crdbToGRPC(err)
}
// TODO(pavelkalinnikov): Load LeafData for conflicting entries.
}
if _, err := t.tx.ExecContext(ctx, "RELEASE "+savepoint); err != nil {
klog.Errorf("Error releasing savepoint: %s", err)
return nil, crdbToGRPC(err)
}
return res, nil
}
func (t *logTreeTX) GetLeavesByRange(ctx context.Context, start, count int64) ([]*trillian.LogLeaf, error) {
t.treeTX.mu.Lock()
defer t.treeTX.mu.Unlock()
return t.getLeavesByRangeInternal(ctx, start, count)
}
func (t *logTreeTX) getLeavesByRangeInternal(ctx context.Context, start, count int64) ([]*trillian.LogLeaf, error) {
if count <= 0 {
return nil, status.Errorf(codes.InvalidArgument, "invalid count %d, want > 0", count)
}
if start < 0 {
return nil, status.Errorf(codes.InvalidArgument, "invalid start %d, want >= 0", start)
}
if t.treeType == trillian.TreeType_LOG {
treeSize := int64(t.root.TreeSize)
if treeSize <= 0 {
return nil, status.Errorf(codes.OutOfRange, "empty tree")
} else if start >= treeSize {
return nil, status.Errorf(codes.OutOfRange, "invalid start %d, want < TreeSize(%d)", start, treeSize)
}
// Ensure no entries queried/returned beyond the tree.
if maxCount := treeSize - start; count > maxCount {
count = maxCount
}
}
// TODO(pavelkalinnikov): Further clip `count` to a safe upper bound like 64k.
args := []interface{}{start, start + count, t.treeID}
rows, err := t.tx.QueryContext(ctx, selectLeavesByRangeSQL, args...)
if err != nil {
klog.Warningf("Failed to get leaves by range: %s", err)
return nil, err
}
defer func() {
if err := rows.Close(); err != nil {
klog.Errorf("rows.Close(): %v", err)
}
}()
ret := make([]*trillian.LogLeaf, 0, count)
for wantIndex := start; rows.Next(); wantIndex++ {
leaf := &trillian.LogLeaf{}
var qTimestamp, iTimestamp int64
if err := rows.Scan(
&leaf.MerkleLeafHash,
&leaf.LeafIdentityHash,
&leaf.LeafValue,
&leaf.LeafIndex,
&leaf.ExtraData,
&qTimestamp,
&iTimestamp); err != nil {
klog.Warningf("Failed to scan merkle leaves: %s", err)
return nil, err
}
if leaf.LeafIndex != wantIndex {
if wantIndex < int64(t.root.TreeSize) {
return nil, fmt.Errorf("got unexpected index %d, want %d", leaf.LeafIndex, wantIndex)
}
break
}
leaf.QueueTimestamp = timestamppb.New(time.Unix(0, qTimestamp))
if err := leaf.QueueTimestamp.CheckValid(); err != nil {
return nil, fmt.Errorf("got invalid queue timestamp: %w", err)
}
leaf.IntegrateTimestamp = timestamppb.New(time.Unix(0, iTimestamp))
if err := leaf.IntegrateTimestamp.CheckValid(); err != nil {
return nil, fmt.Errorf("got invalid integrate timestamp: %w", err)
}
ret = append(ret, leaf)
}
if err := rows.Err(); err != nil {
klog.Warningf("Failed to read returned leaves: %s", err)
return nil, err
}
return ret, nil
}
func (t *logTreeTX) GetLeavesByHash(ctx context.Context, leafHashes [][]byte, orderBySequence bool) ([]*trillian.LogLeaf, error) {
t.treeTX.mu.Lock()
defer t.treeTX.mu.Unlock()
tmpl, err := t.ls.getLeavesByMerkleHashStmt(ctx, len(leafHashes), orderBySequence)
if err != nil {
return nil, err
}
return t.getLeavesByHashInternal(ctx, leafHashes, tmpl, "merkle")
}
// getLeafDataByIdentityHash retrieves leaf data by LeafIdentityHash, returned
// as a slice of LogLeaf objects for convenience. However, note that the
// returned LogLeaf objects will not have a valid MerkleLeafHash, LeafIndex, or IntegrateTimestamp.
func (t *logTreeTX) getLeafDataByIdentityHash(ctx context.Context, leafHashes [][]byte) ([]*trillian.LogLeaf, error) {
tmpl, err := t.ls.getLeavesByLeafIdentityHashStmt(ctx, len(leafHashes))
if err != nil {
return nil, err
}
return t.getLeavesByHashInternal(ctx, leafHashes, tmpl, "leaf-identity")
}
func (t *logTreeTX) LatestSignedLogRoot(ctx context.Context) (*trillian.SignedLogRoot, error) {
t.treeTX.mu.Lock()
defer t.treeTX.mu.Unlock()
if t.slr == nil {
return nil, storage.ErrTreeNeedsInit
}
return t.slr, nil
}
// fetchLatestRoot reads the latest root and the revision from the DB.
func (t *logTreeTX) fetchLatestRoot(ctx context.Context) (*trillian.SignedLogRoot, int64, error) {
var timestamp, treeSize, treeRevision int64
var rootHash, rootSignatureBytes []byte
if err := t.tx.QueryRowContext(
ctx, selectLatestSignedLogRootSQL, t.treeID).Scan(
×tamp, &treeSize, &rootHash, &treeRevision, &rootSignatureBytes,
); err == sql.ErrNoRows {
// It's possible there are no roots for this tree yet
return nil, 0, storage.ErrTreeNeedsInit
}
// Put logRoot back together. Fortunately LogRoot has a deterministic serialization.
logRoot, err := (&types.LogRootV1{
RootHash: rootHash,
TimestampNanos: uint64(timestamp),
TreeSize: uint64(treeSize),
}).MarshalBinary()
if err != nil {
return nil, 0, err
}
return &trillian.SignedLogRoot{LogRoot: logRoot}, treeRevision, nil
}
func (t *logTreeTX) StoreSignedLogRoot(ctx context.Context, root *trillian.SignedLogRoot) error {
t.treeTX.mu.Lock()
defer t.treeTX.mu.Unlock()
var logRoot types.LogRootV1
if err := logRoot.UnmarshalBinary(root.LogRoot); err != nil {
klog.Warningf("Failed to parse log root: %x %v", root.LogRoot, err)
return err
}
if len(logRoot.Metadata) != 0 {
return fmt.Errorf("unimplemented: crdb storage does not support log root metadata")
}
res, err := t.tx.ExecContext(
ctx,
insertTreeHeadSQL,
t.treeID,
logRoot.TimestampNanos,
logRoot.TreeSize,
logRoot.RootHash,
t.treeTX.writeRevision,
[]byte{})
if err != nil {
klog.Warningf("Failed to store signed root: %s", err)
}
return checkResultOkAndRowCountIs(res, err, 1)
}
func (t *logTreeTX) getLeavesByHashInternal(ctx context.Context, leafHashes [][]byte, tmpl *sql.Stmt, desc string) ([]*trillian.LogLeaf, error) {
stx := t.tx.StmtContext(ctx, tmpl)
defer func() {
if err := stx.Close(); err != nil {
klog.Errorf("stx.Close(): %v", err)
}
}()
var args []interface{}
for _, hash := range leafHashes {
args = append(args, []byte(hash))
}
args = append(args, t.treeID)
rows, err := stx.QueryContext(ctx, args...)
if err != nil {
klog.Warningf("Query() %s hash = %v", desc, err)
return nil, err
}
defer func() {
if err := rows.Close(); err != nil {
klog.Errorf("rows.Close(): %v", err)
}
}()
// The tree could include duplicates so we don't know how many results will be returned
var ret []*trillian.LogLeaf
for rows.Next() {
leaf := &trillian.LogLeaf{}
// We might be using a LEFT JOIN in our statement, so leaves which are
// queued but not yet integrated will have a NULL IntegrateTimestamp
// when there's no corresponding entry in SequencedLeafData, even though
// the table definition forbids that, so we use a nullable type here and
// check its validity below.
var integrateTS sql.NullInt64
var queueTS int64
if err := rows.Scan(&leaf.MerkleLeafHash, &leaf.LeafIdentityHash, &leaf.LeafValue, &leaf.LeafIndex, &leaf.ExtraData, &queueTS, &integrateTS); err != nil {
klog.Warningf("LogID: %d Scan() %s = %s", t.treeID, desc, err)
return nil, err
}
leaf.QueueTimestamp = timestamppb.New(time.Unix(0, queueTS))
if err := leaf.QueueTimestamp.CheckValid(); err != nil {
return nil, fmt.Errorf("got invalid queue timestamp: %w", err)
}
if integrateTS.Valid {
leaf.IntegrateTimestamp = timestamppb.New(time.Unix(0, integrateTS.Int64))
if err := leaf.IntegrateTimestamp.CheckValid(); err != nil {
return nil, fmt.Errorf("got invalid integrate timestamp: %w", err)
}
}
if got, want := len(leaf.MerkleLeafHash), t.hashSizeBytes; got != want {
return nil, fmt.Errorf("LogID: %d Scanned leaf %s does not have hash length %d, got %d", t.treeID, desc, want, got)
}
ret = append(ret, leaf)
}
if err := rows.Err(); err != nil {
klog.Warningf("Failed to read returned leaves: %s", err)
return nil, err
}
return ret, nil
}
// leafAndPosition records original position before sort.
type leafAndPosition struct {
leaf *trillian.LogLeaf
idx int
}
// byLeafIdentityHashWithPosition allows sorting (as above), but where we need
// to remember the original position
type byLeafIdentityHashWithPosition []leafAndPosition
func (l byLeafIdentityHashWithPosition) Len() int {
return len(l)
}
func (l byLeafIdentityHashWithPosition) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
func (l byLeafIdentityHashWithPosition) Less(i, j int) bool {
return bytes.Compare(l[i].leaf.LeafIdentityHash, l[j].leaf.LeafIdentityHash) == -1
}