/
stateful_syncer.go
318 lines (276 loc) · 9.14 KB
/
stateful_syncer.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
// Copyright 2020 Coinbase, Inc.
//
// 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 statefulsyncer
import (
"context"
"errors"
"fmt"
"log"
"runtime"
"time"
"golang.org/x/sync/semaphore"
"github.com/klaytn/rosetta-sdk-go-klaytn/fetcher"
storageErrs "github.com/klaytn/rosetta-sdk-go-klaytn/storage/errors"
"github.com/klaytn/rosetta-sdk-go-klaytn/storage/modules"
"github.com/klaytn/rosetta-sdk-go-klaytn/syncer"
"github.com/klaytn/rosetta-sdk-go-klaytn/types"
"github.com/klaytn/rosetta-sdk-go-klaytn/utils"
)
var _ syncer.Handler = (*StatefulSyncer)(nil)
var _ syncer.Helper = (*StatefulSyncer)(nil)
const (
// DefaultPruneSleepTime is how long we sleep between
// pruning attempts.
DefaultPruneSleepTime = 10 * time.Minute
// pruneBuffer is the cushion we apply to pastBlockLimit
// when pruning.
pruneBuffer = 2
// semaphoreWeight is the weight of each semaphore request.
semaphoreWeight = int64(1)
)
// StatefulSyncer is an abstraction layer over
// the stateless syncer package. This layer
// handles sync restarts and provides
// fully populated blocks during reorgs (not
// provided by stateless syncer).
type StatefulSyncer struct {
network *types.NetworkIdentifier
fetcher *fetcher.Fetcher
cancel context.CancelFunc
blockStorage *modules.BlockStorage
counterStorage *modules.CounterStorage
logger Logger
workers []modules.BlockWorker
cacheSize int
maxConcurrency int64
pastBlockLimit int
adjustmentWindow int64
pruneSleepTime time.Duration
// SeenSemaphore limits how many executions of
// BlockSeen occur concurrently.
seenSemaphore *semaphore.Weighted
seenSemaphoreSize int64
}
// Logger is used by the statefulsyncer to
// log the addition and removal of blocks.
type Logger interface {
AddBlockStream(context.Context, *types.Block) error
RemoveBlockStream(context.Context, *types.BlockIdentifier) error
}
// PruneHelper is used by the stateful syncer
// to determine the safe pruneable index. This is
// a helper instead of a static argument because the
// pruneable index is often a function of the state
// of some number of structs.
type PruneHelper interface {
// PruneableIndex is the largest block
// index that is considered safe to prune.
PruneableIndex(ctx context.Context, headIndex int64) (int64, error)
}
// New returns a new *StatefulSyncer.
func New(
ctx context.Context,
network *types.NetworkIdentifier,
fetcher *fetcher.Fetcher,
blockStorage *modules.BlockStorage,
counterStorage *modules.CounterStorage,
logger Logger,
cancel context.CancelFunc,
workers []modules.BlockWorker,
options ...Option,
) *StatefulSyncer {
s := &StatefulSyncer{
network: network,
fetcher: fetcher,
cancel: cancel,
blockStorage: blockStorage,
counterStorage: counterStorage,
workers: workers,
logger: logger,
// Optional args
cacheSize: syncer.DefaultCacheSize,
maxConcurrency: syncer.DefaultMaxConcurrency,
pastBlockLimit: syncer.DefaultPastBlockLimit,
adjustmentWindow: syncer.DefaultAdjustmentWindow,
pruneSleepTime: DefaultPruneSleepTime,
seenSemaphoreSize: int64(runtime.NumCPU()),
}
// Override defaults with any provided options
for _, opt := range options {
opt(s)
}
// We set this after options because the caller
// has the ability to set the max concurrency
// of seen invocations.
s.seenSemaphore = semaphore.NewWeighted(s.seenSemaphoreSize)
return s
}
// Sync starts a new sync run after properly initializing blockStorage.
func (s *StatefulSyncer) Sync(ctx context.Context, startIndex int64, endIndex int64) error {
s.blockStorage.Initialize(s.workers)
// Ensure storage is in correct state for starting at index
if startIndex != -1 { // attempt to remove blocks from storage (without handling)
if err := s.blockStorage.SetNewStartIndex(ctx, startIndex); err != nil {
return fmt.Errorf("%w: unable to set new start index", err)
}
} else { // attempt to load last processed index
head, err := s.blockStorage.GetHeadBlockIdentifier(ctx)
if err == nil {
startIndex = head.Index + 1
}
}
// Load in previous blocks into syncer cache to handle reorgs.
// If previously processed blocks exist in storage, they are fetched.
// Otherwise, none are provided to the cache (the syncer will not attempt
// a reorg if the cache is empty).
pastBlocks := s.blockStorage.CreateBlockCache(ctx, s.pastBlockLimit)
syncer := syncer.New(
s.network,
s,
s,
s.cancel,
syncer.WithPastBlocks(pastBlocks),
syncer.WithCacheSize(s.cacheSize),
syncer.WithMaxConcurrency(s.maxConcurrency),
syncer.WithAdjustmentWindow(s.adjustmentWindow),
)
return syncer.Sync(ctx, startIndex, endIndex)
}
// Prune will repeatedly attempt to prune BlockStorage until
// the context is canceled or an error is encountered.
//
// PruneHelper is provided as an argument here instead of
// in the initializer because the caller may wish to change
// pruning strategies during syncing.
func (s *StatefulSyncer) Prune(ctx context.Context, helper PruneHelper) error {
for ctx.Err() == nil {
// We don't use a timer pattern because s.pruneSleepTime is defined
// as the time between pruning runs. Using a timer would only guarantee
// that the difference between starts of each pruning run are s.pruneSleepTime.
if err := utils.ContextSleep(ctx, s.pruneSleepTime); err != nil {
return err
}
headBlock, err := s.blockStorage.GetHeadBlockIdentifier(ctx)
if headBlock == nil && errors.Is(err, storageErrs.ErrHeadBlockNotFound) {
// this will occur when we are waiting for the first block to be synced
continue
}
if err != nil {
return err
}
oldestIndex, err := s.blockStorage.GetOldestBlockIndex(ctx)
if oldestIndex == -1 && errors.Is(err, storageErrs.ErrOldestIndexMissing) {
// this will occur when we have yet to store the oldest index
continue
}
if err != nil {
return err
}
pruneableIndex, err := helper.PruneableIndex(ctx, headBlock.Index)
if err != nil {
return fmt.Errorf("%w: could not determine pruneable index", err)
}
if pruneableIndex < oldestIndex {
continue
}
firstPruned, lastPruned, err := s.blockStorage.Prune(
ctx,
pruneableIndex,
int64(s.pastBlockLimit)*pruneBuffer, // we should be very cautious about pruning
)
if err != nil {
return err
}
// firstPruned and lastPruned are -1 if there is nothing to prune
if firstPruned != -1 && lastPruned != -1 {
pruneMessage := fmt.Sprintf("pruned blocks %d-%d", firstPruned, lastPruned)
if firstPruned == lastPruned {
pruneMessage = fmt.Sprintf("pruned block %d", firstPruned)
}
log.Println(pruneMessage)
}
}
return ctx.Err()
}
// BlockSeen is called by the syncer when a block is seen.
func (s *StatefulSyncer) BlockSeen(ctx context.Context, block *types.Block) error {
if err := s.seenSemaphore.Acquire(ctx, semaphoreWeight); err != nil {
return err
}
defer s.seenSemaphore.Release(semaphoreWeight)
if err := s.blockStorage.SeeBlock(ctx, block); err != nil {
return fmt.Errorf(
"%w: unable to pre-store block %s:%d",
err,
block.BlockIdentifier.Hash,
block.BlockIdentifier.Index,
)
}
return nil
}
// BlockAdded is called by the syncer when a block is added.
func (s *StatefulSyncer) BlockAdded(ctx context.Context, block *types.Block) error {
err := s.blockStorage.AddBlock(ctx, block)
if err != nil {
return fmt.Errorf(
"%w: unable to add block to storage %s:%d",
err,
block.BlockIdentifier.Hash,
block.BlockIdentifier.Index,
)
}
_ = s.logger.AddBlockStream(ctx, block)
return nil
}
// BlockRemoved is called by the syncer when a block is removed.
func (s *StatefulSyncer) BlockRemoved(
ctx context.Context,
blockIdentifier *types.BlockIdentifier,
) error {
err := s.blockStorage.RemoveBlock(ctx, blockIdentifier)
if err != nil {
return fmt.Errorf(
"%w: unable to remove block from storage %s:%d",
err,
blockIdentifier.Hash,
blockIdentifier.Index,
)
}
_ = s.logger.RemoveBlockStream(ctx, blockIdentifier)
return nil
}
// NetworkStatus is called by the syncer to get the current
// network status.
func (s *StatefulSyncer) NetworkStatus(
ctx context.Context,
network *types.NetworkIdentifier,
) (*types.NetworkStatusResponse, error) {
networkStatus, fetchErr := s.fetcher.NetworkStatusRetry(ctx, network, nil)
if fetchErr != nil {
return nil, fetchErr.Err
}
return networkStatus, nil
}
// Block is called by the syncer to fetch a block.
func (s *StatefulSyncer) Block(
ctx context.Context,
network *types.NetworkIdentifier,
block *types.PartialBlockIdentifier,
) (*types.Block, error) {
blockResponse, fetchErr := s.fetcher.BlockRetry(ctx, network, block)
if fetchErr != nil {
return nil, fetchErr.Err
}
return blockResponse, nil
}