-
Notifications
You must be signed in to change notification settings - Fork 499
/
buffered_storage_backend.go
265 lines (207 loc) · 7.6 KB
/
buffered_storage_backend.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
// BufferedStorageBackend is a ledger backend that provides buffered access over a given DataStore.
// The DataStore must contain files generated from a LedgerExporter.
package ledgerbackend
import (
"context"
"sync"
"time"
"github.com/pkg/errors"
"github.com/stellar/go/support/datastore"
"github.com/stellar/go/xdr"
)
// Ensure BufferedStorageBackend implements LedgerBackend
var _ LedgerBackend = (*BufferedStorageBackend)(nil)
type BufferedStorageBackendConfig struct {
BufferSize uint32 `toml:"buffer_size"`
NumWorkers uint32 `toml:"num_workers"`
RetryLimit uint32 `toml:"retry_limit"`
RetryWait time.Duration `toml:"retry_wait"`
}
// BufferedStorageBackend is a ledger backend that reads from a storage service.
// The storage service contains files generated from the ledgerExporter.
type BufferedStorageBackend struct {
config BufferedStorageBackendConfig
bsBackendLock sync.RWMutex
// ledgerBuffer is the buffer for LedgerCloseMeta data read in parallel.
ledgerBuffer *ledgerBuffer
dataStore datastore.DataStore
prepared *Range // Non-nil if any range is prepared
closed bool // False until the core is closed
lcmBatch xdr.LedgerCloseMetaBatch
nextLedger uint32
lastLedger uint32
}
// NewBufferedStorageBackend returns a new BufferedStorageBackend instance.
func NewBufferedStorageBackend(config BufferedStorageBackendConfig, dataStore datastore.DataStore) (*BufferedStorageBackend, error) {
if config.BufferSize == 0 {
return nil, errors.New("buffer size must be > 0")
}
if config.NumWorkers > config.BufferSize {
return nil, errors.New("number of workers must be <= BufferSize")
}
if dataStore.GetSchema().LedgersPerFile <= 0 {
return nil, errors.New("ledgersPerFile must be > 0")
}
bsBackend := &BufferedStorageBackend{
config: config,
dataStore: dataStore,
}
return bsBackend, nil
}
// GetLatestLedgerSequence returns the most recent ledger sequence number available in the buffer.
func (bsb *BufferedStorageBackend) GetLatestLedgerSequence(ctx context.Context) (uint32, error) {
bsb.bsBackendLock.RLock()
defer bsb.bsBackendLock.RUnlock()
if bsb.closed {
return 0, errors.New("BufferedStorageBackend is closed; cannot GetLatestLedgerSequence")
}
if bsb.prepared == nil {
return 0, errors.New("BufferedStorageBackend must be prepared, call PrepareRange first")
}
latestSeq, err := bsb.ledgerBuffer.getLatestLedgerSequence()
if err != nil {
return 0, err
}
return latestSeq, nil
}
// getBatchForSequence checks if the requested sequence is in the cached batch.
// Otherwise will continuously load in the next LedgerCloseMetaBatch until found.
func (bsb *BufferedStorageBackend) getBatchForSequence(ctx context.Context, sequence uint32) error {
// Sequence inside the current cached LedgerCloseMetaBatch
if sequence >= uint32(bsb.lcmBatch.StartSequence) && sequence <= uint32(bsb.lcmBatch.EndSequence) {
return nil
}
// Sequence is before the current LedgerCloseMetaBatch
// Does not support retrieving LedgerCloseMeta before the current cached batch
if sequence < uint32(bsb.lcmBatch.StartSequence) {
return errors.New("requested sequence precedes current LedgerCloseMetaBatch")
}
// Sequence is beyond the current LedgerCloseMetaBatch
var err error
bsb.lcmBatch, err = bsb.ledgerBuffer.getFromLedgerQueue(ctx)
if err != nil {
return errors.Wrap(err, "failed getting next ledger batch from queue")
}
return nil
}
// nextExpectedSequence returns nextLedger (if currently set) or start of
// prepared range. Otherwise it returns 0.
// This is done because `nextLedger` is 0 between the moment Stellar-Core is
// started and streaming the first ledger (in such case we return first ledger
// in requested range).
func (bsb *BufferedStorageBackend) nextExpectedSequence() uint32 {
if bsb.nextLedger == 0 && bsb.prepared != nil {
return bsb.prepared.from
}
return bsb.nextLedger
}
// GetLedger returns the LedgerCloseMeta for the specified ledger sequence number
func (bsb *BufferedStorageBackend) GetLedger(ctx context.Context, sequence uint32) (xdr.LedgerCloseMeta, error) {
bsb.bsBackendLock.RLock()
defer bsb.bsBackendLock.RUnlock()
if bsb.closed {
return xdr.LedgerCloseMeta{}, errors.New("BufferedStorageBackend is closed; cannot GetLedger")
}
if bsb.prepared == nil {
return xdr.LedgerCloseMeta{}, errors.New("session is not prepared, call PrepareRange first")
}
if sequence < bsb.ledgerBuffer.ledgerRange.from {
return xdr.LedgerCloseMeta{}, errors.New("requested sequence preceeds current LedgerRange")
}
if bsb.ledgerBuffer.ledgerRange.bounded {
if sequence > bsb.ledgerBuffer.ledgerRange.to {
return xdr.LedgerCloseMeta{}, errors.New("requested sequence beyond current LedgerRange")
}
}
if sequence < bsb.lastLedger {
return xdr.LedgerCloseMeta{}, errors.New("requested sequence preceeds the lastLedger")
}
if sequence > bsb.nextExpectedSequence() {
return xdr.LedgerCloseMeta{}, errors.New("requested sequence is not the lastLedger nor the next available ledger")
}
err := bsb.getBatchForSequence(ctx, sequence)
if err != nil {
return xdr.LedgerCloseMeta{}, err
}
ledgerCloseMeta, err := bsb.lcmBatch.GetLedger(sequence)
if err != nil {
return xdr.LedgerCloseMeta{}, err
}
bsb.lastLedger = bsb.nextLedger
bsb.nextLedger++
return ledgerCloseMeta, nil
}
// PrepareRange checks if the starting and ending (if bounded) ledgers exist.
func (bsb *BufferedStorageBackend) PrepareRange(ctx context.Context, ledgerRange Range) error {
bsb.bsBackendLock.Lock()
defer bsb.bsBackendLock.Unlock()
if bsb.closed {
return errors.New("BufferedStorageBackend is closed; cannot PrepareRange")
}
if alreadyPrepared, err := bsb.startPreparingRange(ledgerRange); err != nil {
return errors.Wrap(err, "error starting prepare range")
} else if alreadyPrepared {
return nil
}
bsb.prepared = &ledgerRange
return nil
}
// IsPrepared returns true if a given ledgerRange is prepared.
func (bsb *BufferedStorageBackend) IsPrepared(ctx context.Context, ledgerRange Range) (bool, error) {
bsb.bsBackendLock.RLock()
defer bsb.bsBackendLock.RUnlock()
if bsb.closed {
return false, errors.New("BufferedStorageBackend is closed; cannot IsPrepared")
}
return bsb.isPrepared(ledgerRange), nil
}
func (bsb *BufferedStorageBackend) isPrepared(ledgerRange Range) bool {
if bsb.closed {
return false
}
if bsb.prepared == nil {
return false
}
if bsb.ledgerBuffer.ledgerRange.from > ledgerRange.from {
return false
}
if bsb.ledgerBuffer.ledgerRange.bounded && !ledgerRange.bounded {
return false
}
if !bsb.ledgerBuffer.ledgerRange.bounded && !ledgerRange.bounded {
return true
}
if !bsb.ledgerBuffer.ledgerRange.bounded && ledgerRange.bounded {
return true
}
if bsb.ledgerBuffer.ledgerRange.to >= ledgerRange.to {
return true
}
return false
}
// Close closes existing BufferedStorageBackend processes.
// Note, once a BufferedStorageBackend instance is closed it can no longer be used and
// all subsequent calls to PrepareRange(), GetLedger(), etc will fail.
// Close is thread-safe and can be called from another go routine.
func (bsb *BufferedStorageBackend) Close() error {
bsb.bsBackendLock.RLock()
defer bsb.bsBackendLock.RUnlock()
if bsb.ledgerBuffer != nil {
bsb.ledgerBuffer.close()
}
bsb.closed = true
return nil
}
// startPreparingRange prepares the ledger range by setting the range in the ledgerBuffer
func (bsb *BufferedStorageBackend) startPreparingRange(ledgerRange Range) (bool, error) {
if bsb.isPrepared(ledgerRange) {
return true, nil
}
var err error
bsb.ledgerBuffer, err = bsb.newLedgerBuffer(ledgerRange)
if err != nil {
return false, err
}
bsb.nextLedger = ledgerRange.from
return false, nil
}