/
sequence.go
96 lines (81 loc) · 3.24 KB
/
sequence.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
package accounthist
import (
"context"
"encoding/hex"
"fmt"
pbaccounthist "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/accounthist/v1"
"github.com/golang/protobuf/proto"
"github.com/dfuse-io/kvdb/store"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type SequenceData struct {
CurrentOrdinal uint64 // while in memory, this value is the last written shard ordinal number that was assisgned
LastGlobalSeq uint64 // taken from the top-most action stored in this shard, defines by the chain
LastDeletedOrdinal uint64 // taken from the top-most action stored in this shard
MaxEntries uint64 // initialized with the process' max entries per account, but can be reduced if some more recent shards covered this account
}
func (sqd *SequenceData) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddUint64("current_ordinal", sqd.CurrentOrdinal)
encoder.AddUint64("last_global_seq", sqd.LastGlobalSeq)
return nil
}
// TODO: this should be under injector
func LatestShardSeqDataPerFacet(ctx context.Context, kvStore store.KVStore, key Facet, shardNum byte, decoder RowKeyDecoderFunc, unmarshalAction bool) (SequenceData, byte, error) {
startKey, endKey := FacetRangeLowerBound(key, shardNum)
zlog.Debug("scanning sequence data for action key",
zap.Stringer("key", key),
zap.Int("current_shard_num", int(shardNum)),
zap.String("start_key", hex.EncodeToString(startKey)),
zap.String("end_key", hex.EncodeToString(endKey)),
)
return scanShardSeqDataPerFacet(ctx, kvStore, startKey, endKey, decoder, unmarshalAction)
}
// TODO: this should be under injector
func ShardSeqDataPerFacet(ctx context.Context, kvStore store.KVStore, key Facet, shardNum byte, decoder RowKeyDecoderFunc, unmarshalAction bool) (SequenceData, error) {
startKey, endKey := FacetShardRange(key, shardNum)
zlog.Debug("reading last sequence data for shard",
zap.Stringer("key", key),
zap.Int("current_shard_num", int(shardNum)),
zap.String("start_key", hex.EncodeToString(startKey)),
zap.String("end_key", hex.EncodeToString(endKey)),
)
seqData, _, err := scanShardSeqDataPerFacet(ctx, kvStore, startKey, endKey, decoder, unmarshalAction)
return seqData, err
}
func scanShardSeqDataPerFacet(
ctx context.Context,
kvStore store.KVStore,
startKey RowKey,
endKey RowKey,
decoder RowKeyDecoderFunc,
unmarshalAction bool,
) (SequenceData, byte, error) {
ctx, cancel := context.WithTimeout(ctx, DatabaseTimeout)
defer cancel()
//t0 := time.Now()
//defer func() {
// i.currentBatchMetrics.totalReadSeqDuration += time.Since(t0)
// i.currentBatchMetrics.readSeqCallCount++
//}()
it := kvStore.Scan(ctx, startKey, endKey, 1)
hasNext := it.Next()
if !hasNext && it.Err() != nil {
return SequenceData{}, 0, fmt.Errorf("scan last action: %w", it.Err())
}
if !hasNext {
return SequenceData{}, 0, store.ErrNotFound
}
seqData := SequenceData{}
_, shardNum, currentOrdinal := decoder(it.Item().Key)
seqData.CurrentOrdinal = currentOrdinal
if unmarshalAction {
newact := &pbaccounthist.ActionRow{}
if err := proto.Unmarshal(it.Item().Value, newact); err != nil {
return SequenceData{}, 0, err
}
seqData.LastGlobalSeq = newact.ActionTrace.Receipt.GlobalSequence
seqData.LastDeletedOrdinal = newact.LastDeletedSeq
}
return seqData, shardNum, nil
}