From b0ea67e2106c1782fc43dd4b1d3a4ba55cf0a0fa Mon Sep 17 00:00:00 2001 From: muXxer Date: Wed, 27 Mar 2024 10:56:54 +0100 Subject: [PATCH 1/2] Add ticker to update the sync status even if latest commitment doesn't change --- .../trivialsyncmanager/syncmanager.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/protocol/engine/syncmanager/trivialsyncmanager/syncmanager.go b/pkg/protocol/engine/syncmanager/trivialsyncmanager/syncmanager.go index fef30bf12..79f382214 100644 --- a/pkg/protocol/engine/syncmanager/trivialsyncmanager/syncmanager.go +++ b/pkg/protocol/engine/syncmanager/trivialsyncmanager/syncmanager.go @@ -1,12 +1,14 @@ package trivialsyncmanager import ( + "context" "time" "github.com/iotaledger/hive.go/runtime/event" "github.com/iotaledger/hive.go/runtime/module" "github.com/iotaledger/hive.go/runtime/options" "github.com/iotaledger/hive.go/runtime/syncutils" + "github.com/iotaledger/hive.go/runtime/timeutil" "github.com/iotaledger/hive.go/runtime/workerpool" "github.com/iotaledger/iota-core/pkg/model" "github.com/iotaledger/iota-core/pkg/protocol/engine" @@ -50,8 +52,9 @@ type SyncManager struct { isBootstrappedLock syncutils.RWMutex isBootstrapped bool - isSyncedLock syncutils.RWMutex - isSynced bool + isSyncedLock syncutils.RWMutex + isSynced bool + isSyncedTicker *timeutil.Ticker isFinalizationDelayedLock syncutils.RWMutex isFinalizationDelayed bool @@ -137,6 +140,7 @@ func New(subModule module.Module, e *engine.Engine, latestCommitment *model.Comm isBootstrapped: false, isSynced: false, + isSyncedTicker: nil, isFinalizationDelayed: true, lastAcceptedBlockSlot: latestCommitment.Slot(), lastConfirmedBlockSlot: latestCommitment.Slot(), @@ -145,6 +149,17 @@ func New(subModule module.Module, e *engine.Engine, latestCommitment *model.Comm lastPrunedEpoch: 0, hasPruned: false, }, opts, func(s *SyncManager) { + ctx, cancel := context.WithCancel(context.Background()) + e.ShutdownEvent().OnTrigger(func() { + // stop the ticker when the engine is shutting down + cancel() + }) + + // start the sync status update ticker + s.isSyncedTicker = timeutil.NewTicker(func() { + s.updateSyncStatus() + }, 1*time.Second, ctx) + s.updatePrunedEpoch(s.engine.Storage.LastPrunedEpoch()) // set the default bootstrapped function From e2a8a8cbe2addeaf06fa7d3084d674b8757101f2 Mon Sep 17 00:00:00 2001 From: muXxer Date: Wed, 27 Mar 2024 13:37:33 +0100 Subject: [PATCH 2/2] Properly shutdown the syncmanager --- .../trivialsyncmanager/syncmanager.go | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pkg/protocol/engine/syncmanager/trivialsyncmanager/syncmanager.go b/pkg/protocol/engine/syncmanager/trivialsyncmanager/syncmanager.go index 79f382214..d0ccdb7c1 100644 --- a/pkg/protocol/engine/syncmanager/trivialsyncmanager/syncmanager.go +++ b/pkg/protocol/engine/syncmanager/trivialsyncmanager/syncmanager.go @@ -129,6 +129,8 @@ func NewProvider(opts ...options.Option[SyncManager]) module.Provider[*engine.En } func New(subModule module.Module, e *engine.Engine, latestCommitment *model.Commitment, finalizedSlot iotago.SlotIndex, opts ...options.Option[SyncManager]) *SyncManager { + ctxUpdateSyncStatusTicker, ctxCancelUpdateSyncStatusTicker := context.WithCancel(context.Background()) + return module.InitSimpleLifecycle(options.Apply(&SyncManager{ Module: subModule, events: syncmanager.NewEvents(), @@ -149,16 +151,10 @@ func New(subModule module.Module, e *engine.Engine, latestCommitment *model.Comm lastPrunedEpoch: 0, hasPruned: false, }, opts, func(s *SyncManager) { - ctx, cancel := context.WithCancel(context.Background()) - e.ShutdownEvent().OnTrigger(func() { - // stop the ticker when the engine is shutting down - cancel() - }) - - // start the sync status update ticker + // start the sync status update ticker, tick every half slot duration s.isSyncedTicker = timeutil.NewTicker(func() { s.updateSyncStatus() - }, 1*time.Second, ctx) + }, time.Duration(e.CommittedAPI().ProtocolParameters().SlotDurationInSeconds())*time.Second/2, ctxUpdateSyncStatusTicker) s.updatePrunedEpoch(s.engine.Storage.LastPrunedEpoch()) @@ -168,7 +164,15 @@ func New(subModule module.Module, e *engine.Engine, latestCommitment *model.Comm return time.Since(e.Clock.Accepted().RelativeTime()) < s.optsBootstrappedThreshold && e.Notarization.IsBootstrapped() } } - })) + }), func(syncManager *SyncManager) { + // stop the ticker when the engine is shutting down + ctxCancelUpdateSyncStatusTicker() + + // wait for the ticker to gracefully shut down + syncManager.isSyncedTicker.WaitForGracefulShutdown() + + syncManager.Module.StoppedEvent().Trigger() + }) } func (s *SyncManager) SyncStatus() *syncmanager.SyncStatus {