-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathfailover.go
More file actions
416 lines (373 loc) · 12 KB
/
failover.go
File metadata and controls
416 lines (373 loc) · 12 KB
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
package node
import (
"context"
"errors"
"fmt"
"net/http"
"sync/atomic"
"time"
"github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
"github.com/evstack/ev-node/block"
coreexecutor "github.com/evstack/ev-node/core/execution"
coresequencer "github.com/evstack/ev-node/core/sequencer"
"github.com/evstack/ev-node/pkg/config"
genesispkg "github.com/evstack/ev-node/pkg/genesis"
"github.com/evstack/ev-node/pkg/p2p"
"github.com/evstack/ev-node/pkg/raft"
rpcserver "github.com/evstack/ev-node/pkg/rpc/server"
"github.com/evstack/ev-node/pkg/signer"
"github.com/evstack/ev-node/pkg/store"
evsync "github.com/evstack/ev-node/pkg/sync"
)
// failoverState collect the components to reset when switching modes.
type failoverState struct {
logger zerolog.Logger
p2pClient *p2p.Client
headerSyncService *evsync.HeaderSyncService
dataSyncService *evsync.DataSyncService
rpcServer *http.Server
bc *block.Components
raftNode *raft.Node
isAggregator bool
// catchup fields — used when the aggregator needs to sync before producing
catchupEnabled bool
catchupTimeout time.Duration
daBlockTime time.Duration
store store.Store
}
func newSyncMode(
nodeConfig config.Config,
genesis genesispkg.Genesis,
exec coreexecutor.Executor,
da block.DAClient,
logger zerolog.Logger,
rktStore store.Store,
blockMetrics *block.Metrics,
nodeOpts NodeOptions,
raftNode *raft.Node,
p2pClient *p2p.Client,
) (*failoverState, error) {
blockComponentsFn := func(headerSyncService *evsync.HeaderSyncService, dataSyncService *evsync.DataSyncService) (*block.Components, error) {
return block.NewSyncComponents(
nodeConfig,
genesis,
rktStore,
exec,
da,
headerSyncService.Store(),
dataSyncService.Store(),
headerSyncService,
dataSyncService,
logger,
blockMetrics,
nodeOpts.BlockOptions,
raftNode,
)
}
return setupFailoverState(nodeConfig, genesis, logger, rktStore, blockComponentsFn, raftNode, p2pClient, false)
}
func newAggregatorMode(
nodeConfig config.Config,
signer signer.Signer,
genesis genesispkg.Genesis,
exec coreexecutor.Executor,
sequencer coresequencer.Sequencer,
da block.DAClient,
logger zerolog.Logger,
rktStore store.Store,
blockMetrics *block.Metrics,
nodeOpts NodeOptions,
raftNode *raft.Node,
p2pClient *p2p.Client,
) (*failoverState, error) {
blockComponentsFn := func(headerSyncService *evsync.HeaderSyncService, dataSyncService *evsync.DataSyncService) (*block.Components, error) {
return block.NewAggregatorWithCatchupComponents(
nodeConfig,
genesis,
rktStore,
exec,
sequencer,
da,
signer,
headerSyncService,
dataSyncService,
logger,
blockMetrics,
nodeOpts.BlockOptions,
raftNode,
)
}
return setupFailoverState(nodeConfig, genesis, logger, rktStore, blockComponentsFn, raftNode, p2pClient, true)
}
func setupFailoverState(
nodeConfig config.Config,
genesis genesispkg.Genesis,
logger zerolog.Logger,
rktStore store.Store,
buildComponentsFn func(headerSyncService *evsync.HeaderSyncService, dataSyncService *evsync.DataSyncService) (*block.Components, error),
raftNode *raft.Node,
p2pClient *p2p.Client,
isAggregator bool,
) (*failoverState, error) {
headerSyncService, err := evsync.NewHeaderSyncService(rktStore, nodeConfig, genesis, p2pClient, logger.With().Str("component", "HeaderSyncService").Logger())
if err != nil {
return nil, fmt.Errorf("error while initializing HeaderSyncService: %w", err)
}
dataSyncService, err := evsync.NewDataSyncService(rktStore, nodeConfig, genesis, p2pClient, logger.With().Str("component", "DataSyncService").Logger())
if err != nil {
return nil, fmt.Errorf("error while initializing DataSyncService: %w", err)
}
bestKnownHeightProvider := func() uint64 {
hHeight := headerSyncService.Store().Height()
dHeight := dataSyncService.Store().Height()
return min(hHeight, dHeight)
}
handler, err := rpcserver.NewServiceHandler(
rktStore,
headerSyncService.Store(),
dataSyncService.Store(),
p2pClient,
genesis.ProposerAddress,
logger,
nodeConfig,
bestKnownHeightProvider,
raftNode,
)
if err != nil {
return nil, fmt.Errorf("error creating RPC handler: %w", err)
}
rpcServer := &http.Server{
Addr: nodeConfig.RPC.Address,
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
bc, err := buildComponentsFn(headerSyncService, dataSyncService)
if err != nil {
return nil, fmt.Errorf("build follower components: %w", err)
}
// Catchup only applies to aggregator nodes that need to sync before
catchupEnabled := isAggregator && nodeConfig.Node.CatchupTimeout.Duration > 0
if isAggregator && !catchupEnabled {
bc.Syncer = nil
}
return &failoverState{
logger: logger,
p2pClient: p2pClient,
headerSyncService: headerSyncService,
dataSyncService: dataSyncService,
rpcServer: rpcServer,
bc: bc,
raftNode: raftNode,
isAggregator: isAggregator,
store: rktStore,
catchupEnabled: catchupEnabled,
catchupTimeout: nodeConfig.Node.CatchupTimeout.Duration,
daBlockTime: nodeConfig.DA.BlockTime.Duration,
}, nil
}
func (f *failoverState) shouldStartSyncInPublisherMode(ctx context.Context) bool {
if !f.isAggregator || f.raftNode == nil || !f.raftNode.IsLeader() {
return false
}
height, err := f.store.Height(ctx)
if err != nil {
f.logger.Warn().Err(err).Msg("cannot determine local height; keeping blocking sync startup")
return false
}
if height > 0 {
return false
}
f.logger.Info().
Msg("raft leader with empty store: starting sync services in publisher mode")
return true
}
func (f *failoverState) Run(pCtx context.Context) (multiErr error) {
stopService := func(stoppable func(context.Context) error, name string) { //nolint:contextcheck // shutdown uses context.Background intentionally
// parent context is cancelled already, so we need to create a new one
shutdownCtx, done := context.WithTimeout(context.Background(), 3*time.Second) //nolint:contextcheck // intentional: need fresh context for graceful shutdown after cancellation
defer done()
if err := stoppable(shutdownCtx); err != nil && !errors.Is(err, context.Canceled) {
multiErr = errors.Join(multiErr, fmt.Errorf("stopping %s: %w", name, err))
}
}
cCtx, cancel := context.WithCancel(pCtx)
defer cancel()
wg, ctx := errgroup.WithContext(cCtx)
wg.Go(func() (rerr error) { //nolint:contextcheck // block components stop API does not accept context
defer func() {
if err := f.bc.Stop(); err != nil && !errors.Is(err, context.Canceled) {
rerr = errors.Join(rerr, fmt.Errorf("stopping block components: %w", err))
}
}()
f.logger.Info().Str("addr", f.rpcServer.Addr).Msg("Started RPC server")
if err := f.rpcServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
})
// start header and data sync services concurrently to avoid cumulative startup delay.
startSyncInPublisherMode := f.shouldStartSyncInPublisherMode(ctx)
syncWg, syncCtx := errgroup.WithContext(ctx)
syncWg.Go(func() error {
var err error
if startSyncInPublisherMode {
err = f.headerSyncService.StartForPublishing(syncCtx)
} else {
err = f.headerSyncService.Start(syncCtx)
}
if err != nil {
return fmt.Errorf("header sync service: %w", err)
}
return nil
})
syncWg.Go(func() error {
var err error
if startSyncInPublisherMode {
err = f.dataSyncService.StartForPublishing(syncCtx)
} else {
err = f.dataSyncService.Start(syncCtx)
}
if err != nil {
return fmt.Errorf("data sync service: %w", err)
}
return nil
})
if err := syncWg.Wait(); err != nil {
return err
}
defer stopService(f.headerSyncService.Stop, "header sync")
defer stopService(f.dataSyncService.Stop, "data sync")
if f.catchupEnabled && f.bc.Syncer != nil {
if err := f.runCatchupPhase(ctx); err != nil {
return err
}
}
wg.Go(func() error {
defer func() { //nolint:contextcheck // shutdown uses context.Background intentionally
shutdownCtx, done := context.WithTimeout(context.Background(), 3*time.Second)
defer done()
_ = f.rpcServer.Shutdown(shutdownCtx)
}()
if err := f.bc.Start(ctx); err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("components started with error: %w", err)
}
return nil
})
return wg.Wait()
}
// runCatchupPhase starts the catchup syncer, waits until DA head is reached and P2P
// is caught up, then stops the syncer so the executor can take over.
func (f *failoverState) runCatchupPhase(ctx context.Context) error {
f.logger.Info().Msg("catchup: syncing from DA and P2P before producing blocks")
if err := f.bc.Syncer.Start(ctx); err != nil {
return fmt.Errorf("catchup syncer start: %w", err)
}
defer f.bc.Syncer.Stop(context.Background()) // nolint:errcheck,contextcheck // not critical
caughtUp, err := f.waitForCatchup(ctx)
if err != nil {
return err
}
if !caughtUp {
return ctx.Err()
}
f.logger.Info().Msg("catchup: fully caught up, stopping syncer and starting block production")
f.bc.Syncer = nil
return nil
}
// waitForCatchup polls DA and P2P catchup status until both sources indicate the node is caught up.
func (f *failoverState) waitForCatchup(ctx context.Context) (bool, error) {
pollInterval := f.daBlockTime
if pollInterval <= 0 {
pollInterval = time.Second / 10
}
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
var timeoutCh <-chan time.Time
if f.catchupTimeout > 0 {
f.logger.Debug().Dur("p2p_timeout", f.catchupTimeout).Msg("P2P catchup timeout configured")
timeoutCh = time.After(f.catchupTimeout)
} else {
f.logger.Debug().Msg("P2P catchup timeout disabled, relying on DA only")
}
ignoreP2P := false
for {
select {
case <-ctx.Done():
return false, nil
case <-timeoutCh:
f.logger.Info().Msg("catchup: P2P timeout reached, ignoring P2P status")
ignoreP2P = true
timeoutCh = nil
case <-ticker.C:
daCaughtUp := f.bc.Syncer != nil && f.bc.Syncer.HasReachedDAHead()
storeHeight, err := f.store.Height(ctx)
if err != nil {
f.logger.Warn().Err(err).Msg("failed to get store height during catchup")
continue
}
maxP2PHeight := max(
f.headerSyncService.Store().Height(),
f.dataSyncService.Store().Height(),
)
p2pCaughtUp := ignoreP2P || (maxP2PHeight > 0 && storeHeight >= maxP2PHeight)
if !ignoreP2P && f.catchupTimeout == 0 && maxP2PHeight == 0 {
p2pCaughtUp = true
}
pipelineDrained := f.bc.Syncer == nil || f.bc.Syncer.PendingCount() == 0
if daCaughtUp && p2pCaughtUp && pipelineDrained {
f.logger.Info().
Uint64("store_height", storeHeight).
Uint64("max_p2p_height", maxP2PHeight).
Msg("catchup: fully caught up")
return true, nil
}
}
}
}
func (f *failoverState) IsSynced(s *raft.RaftBlockState) (int, error) {
if f.bc.Syncer != nil {
return f.bc.Syncer.IsSyncedWithRaft(s)
}
if f.bc.Executor != nil {
return f.bc.Executor.IsSyncedWithRaft(s)
}
return 0, errors.New("sync check not supported in this mode")
}
func (f *failoverState) Recover(ctx context.Context, state *raft.RaftBlockState) error {
if f.bc.Syncer != nil {
return f.bc.Syncer.RecoverFromRaft(ctx, state)
}
return errors.New("recovery not supported in this mode")
}
var _ leaderElection = &singleRoleElector{}
var _ testSupportElection = &singleRoleElector{}
// singleRoleElector implements leaderElection but with a static role. No switchover.
type singleRoleElector struct {
running atomic.Bool
runnable raft.Runnable
}
func newSingleRoleElector(factory func() (raft.Runnable, error)) (*singleRoleElector, error) {
r, err := factory()
if err != nil {
return nil, err
}
return &singleRoleElector{runnable: r}, nil
}
func (a *singleRoleElector) Run(ctx context.Context) error {
a.running.Store(true)
defer a.running.Store(false)
return a.runnable.Run(ctx)
}
func (a *singleRoleElector) IsRunning() bool {
return a.running.Load()
}
// for testing purposes only
func (a *singleRoleElector) state() *failoverState {
if v, ok := a.runnable.(*failoverState); ok {
return v
}
return nil
}