Skip to content

Commit bf45be9

Browse files
committedMar 12, 2023
Delay reporting of blockchain not in sync to avoid false monitoring alerts
1 parent bf5f6f9 commit bf45be9

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed
 

‎api/worker.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -1625,12 +1625,12 @@ func (w *Worker) GetBalanceHistory(address string, fromTimestamp, toTimestamp in
16251625

16261626
func (w *Worker) waitForBackendSync() {
16271627
// wait a short time if blockbook is synchronizing with backend
1628-
inSync, _, _ := w.is.GetSyncState()
1628+
inSync, _, _, _ := w.is.GetSyncState()
16291629
count := 30
16301630
for !inSync && count > 0 {
16311631
time.Sleep(time.Millisecond * 100)
16321632
count--
1633-
inSync, _, _ = w.is.GetSyncState()
1633+
inSync, _, _, _ = w.is.GetSyncState()
16341634
}
16351635
}
16361636

@@ -2262,9 +2262,15 @@ func nonZeroTime(t time.Time) *time.Time {
22622262

22632263
// GetSystemInfo returns information about system
22642264
func (w *Worker) GetSystemInfo(internal bool) (*SystemInfo, error) {
2265-
start := time.Now()
2265+
start := time.Now().UTC()
22662266
vi := common.GetVersionInfo()
2267-
inSync, bestHeight, lastBlockTime := w.is.GetSyncState()
2267+
inSync, bestHeight, lastBlockTime, startSync := w.is.GetSyncState()
2268+
if !inSync && !w.is.InitialSync {
2269+
// if less than 5 seconds into syncing, return inSync=true to avoid short time not in sync reports that confuse monitoring
2270+
if startSync.Add(5 * time.Second).After(start) {
2271+
inSync = true
2272+
}
2273+
}
22682274
inSyncMempool, lastMempoolTime, mempoolSize := w.is.GetMempoolSyncState()
22692275
ci, err := w.chain.GetChainInfo()
22702276
var backendError string

‎common/internalstate.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type InternalState struct {
6969
InitialSync bool `json:"initialSync"`
7070
IsSynchronized bool `json:"isSynchronized"`
7171
BestHeight uint32 `json:"bestHeight"`
72+
StartSync time.Time `json:"-"`
7273
LastSync time.Time `json:"lastSync"`
7374
BlockTimes []uint32 `json:"-"`
7475
AvgBlockPeriod uint32 `json:"-"`
@@ -96,6 +97,7 @@ type InternalState struct {
9697
func (is *InternalState) StartedSync() {
9798
is.mux.Lock()
9899
defer is.mux.Unlock()
100+
is.StartSync = time.Now().UTC()
99101
is.IsSynchronized = false
100102
}
101103

@@ -105,15 +107,15 @@ func (is *InternalState) FinishedSync(bestHeight uint32) {
105107
defer is.mux.Unlock()
106108
is.IsSynchronized = true
107109
is.BestHeight = bestHeight
108-
is.LastSync = time.Now()
110+
is.LastSync = time.Now().UTC()
109111
}
110112

111113
// UpdateBestHeight sets new best height, without changing IsSynchronized flag
112114
func (is *InternalState) UpdateBestHeight(bestHeight uint32) {
113115
is.mux.Lock()
114116
defer is.mux.Unlock()
115117
is.BestHeight = bestHeight
116-
is.LastSync = time.Now()
118+
is.LastSync = time.Now().UTC()
117119
}
118120

119121
// FinishedSyncNoChange marks end of synchronization in case no index update was necessary, it does not update lastSync time
@@ -124,10 +126,10 @@ func (is *InternalState) FinishedSyncNoChange() {
124126
}
125127

126128
// GetSyncState gets the state of synchronization
127-
func (is *InternalState) GetSyncState() (bool, uint32, time.Time) {
129+
func (is *InternalState) GetSyncState() (bool, uint32, time.Time, time.Time) {
128130
is.mux.Lock()
129131
defer is.mux.Unlock()
130-
return is.IsSynchronized, is.BestHeight, is.LastSync
132+
return is.IsSynchronized, is.BestHeight, is.LastSync, is.StartSync
131133
}
132134

133135
// StartedMempoolSync signals start of mempool synchronization

‎db/txcache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (c *TxCache) GetTransaction(txid string) (*bchain.Tx, int, error) {
4646
}
4747
if tx != nil {
4848
// number of confirmations is not stored in cache, they change all the time
49-
_, bestheight, _ := c.is.GetSyncState()
49+
_, bestheight, _, _ := c.is.GetSyncState()
5050
tx.Confirmations = bestheight - h + 1
5151
c.metrics.TxCacheEfficiency.With(common.Labels{"status": "hit"}).Inc()
5252
return tx, int(h), nil

‎fiat/fiat_rates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (rd *RatesDownloader) Run() error {
135135
} else {
136136
glog.Info("FiatRatesDownloader: UpdateHistoricalTokenTickers finished")
137137
if is != nil {
138-
is.HistoricalTokenFiatRatesTime = time.Now()
138+
is.HistoricalTokenFiatRatesTime = time.Now().UTC()
139139
}
140140
}
141141
}()

‎server/socketio.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ type resultGetInfo struct {
584584
}
585585

586586
func (s *SocketIoServer) getInfo() (res resultGetInfo, err error) {
587-
_, height, _ := s.is.GetSyncState()
587+
_, height, _, _ := s.is.GetSyncState()
588588
res.Result.Blocks = int(height)
589589
res.Result.Testnet = s.chain.IsTestnet()
590590
res.Result.Network = s.chain.GetNetworkName()

0 commit comments

Comments
 (0)