discovery: allow the SyncManager to report whether the graph is synced

This commit is contained in:
Wilmer Paulino
2019-07-26 18:22:37 -07:00
parent 9f0cfe2bee
commit af4234f680
2 changed files with 54 additions and 13 deletions

View File

@@ -3,6 +3,7 @@ package discovery
import (
"errors"
"sync"
"sync/atomic"
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -100,6 +101,16 @@ type SyncManagerCfg struct {
// attempt a historical sync to ensure we have as much of the public channel
// graph as possible.
type SyncManager struct {
// initialHistoricalSyncCompleted serves as a barrier when initializing
// new active GossipSyncers. If 0, the initial historical sync has not
// completed, so we'll defer initializing any active GossipSyncers. If
// 1, then we can transition the GossipSyncer immediately. We set up
// this barrier to ensure we have most of the graph before attempting to
// accept new updates at tip.
//
// NOTE: This must be used atomically.
initialHistoricalSyncCompleted int32
start sync.Once
stop sync.Once
@@ -192,15 +203,6 @@ func (m *SyncManager) syncerHandler() {
defer m.cfg.HistoricalSyncTicker.Stop()
var (
// initialHistoricalSyncCompleted serves as a barrier when
// initializing new active GossipSyncers. If false, the initial
// historical sync has not completed, so we'll defer
// initializing any active GossipSyncers. If true, then we can
// transition the GossipSyncer immediately. We set up this
// barrier to ensure we have most of the graph before attempting
// to accept new updates at tip.
initialHistoricalSyncCompleted = false
// initialHistoricalSyncer is the syncer we are currently
// performing an initial historical sync with.
initialHistoricalSyncer *GossipSyncer
@@ -251,10 +253,10 @@ func (m *SyncManager) syncerHandler() {
fallthrough
// If the initial historical sync has yet to complete,
// then we'll declare is as passive and attempt to
// then we'll declare it as passive and attempt to
// transition it when the initial historical sync
// completes.
case !initialHistoricalSyncCompleted:
case !m.IsGraphSynced():
s.setSyncType(PassiveSync)
m.inactiveSyncers[s.cfg.peerPub] = s
@@ -279,7 +281,7 @@ func (m *SyncManager) syncerHandler() {
if !attemptHistoricalSync {
continue
}
initialHistoricalSyncCompleted = false
m.markGraphSyncing()
log.Debugf("Attempting initial historical sync with "+
"GossipSyncer(%x)", s.cfg.peerPub)
@@ -344,7 +346,7 @@ func (m *SyncManager) syncerHandler() {
case <-initialHistoricalSyncSignal:
initialHistoricalSyncer = nil
initialHistoricalSyncSignal = nil
initialHistoricalSyncCompleted = true
m.markGraphSynced()
log.Debug("Initial historical sync completed")
@@ -667,3 +669,22 @@ func (m *SyncManager) gossipSyncers() map[route.Vertex]*GossipSyncer {
return syncers
}
// markGraphSynced allows us to report that the initial historical sync has
// completed.
func (m *SyncManager) markGraphSynced() {
atomic.StoreInt32(&m.initialHistoricalSyncCompleted, 1)
}
// markGraphSyncing allows us to report that the initial historical sync is
// still undergoing.
func (m *SyncManager) markGraphSyncing() {
atomic.StoreInt32(&m.initialHistoricalSyncCompleted, 0)
}
// IsGraphSynced determines whether we've completed our initial historical sync.
// The initial historical sync is done to ensure we've ingested as much of the
// public graph as possible.
func (m *SyncManager) IsGraphSynced() bool {
return atomic.LoadInt32(&m.initialHistoricalSyncCompleted) == 1
}