Fix a data race in the block logger (#1533)

This commit is contained in:
Elichai Turkel 2021-02-17 17:05:25 +02:00 committed by GitHub
parent 2908a46441
commit 00a023620d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 17 deletions

View File

@ -11,27 +11,38 @@ import (
"github.com/kaspanet/kaspad/util/mstime"
)
var (
// BlockLogger is a type tracking the amount of blocks/headers/transactions to log the time it took to receive them
type BlockLogger struct {
receivedLogBlocks int64
receivedLogHeaders int64
receivedLogTransactions int64
lastBlockLogTime time.Time
)
}
// NewBlockLogger creates a new instance with zeroed blocks/headers/transactions/time counters.
func NewBlockLogger() *BlockLogger {
return &BlockLogger{
receivedLogBlocks: 0,
receivedLogHeaders: 0,
receivedLogTransactions: 0,
lastBlockLogTime: time.Now(),
}
}
// LogBlock logs a new block blue score as an information message
// to show progress to the user. In order to prevent spam, it limits logging to
// one message every 10 seconds with duration and totals included.
func LogBlock(block *externalapi.DomainBlock) {
func (bl *BlockLogger) LogBlock(block *externalapi.DomainBlock) {
if len(block.Transactions) == 0 {
receivedLogHeaders++
bl.receivedLogHeaders++
} else {
receivedLogBlocks++
bl.receivedLogBlocks++
}
receivedLogTransactions += int64(len(block.Transactions))
bl.receivedLogTransactions += int64(len(block.Transactions))
now := time.Now()
duration := now.Sub(lastBlockLogTime)
duration := now.Sub(bl.lastBlockLogTime)
if duration < time.Second*10 {
return
}
@ -41,26 +52,26 @@ func LogBlock(block *externalapi.DomainBlock) {
// Log information about new block blue score.
blockStr := "blocks"
if receivedLogBlocks == 1 {
if bl.receivedLogBlocks == 1 {
blockStr = "block"
}
txStr := "transactions"
if receivedLogTransactions == 1 {
if bl.receivedLogTransactions == 1 {
txStr = "transaction"
}
headerStr := "headers"
if receivedLogBlocks == 1 {
if bl.receivedLogBlocks == 1 {
headerStr = "header"
}
log.Infof("Processed %d %s and %d %s in the last %s (%d %s, %s)",
receivedLogBlocks, blockStr, receivedLogHeaders, headerStr, truncatedDuration, receivedLogTransactions,
bl.receivedLogBlocks, blockStr, bl.receivedLogHeaders, headerStr, truncatedDuration, bl.receivedLogTransactions,
txStr, mstime.UnixMilliseconds(block.Header.TimeInMilliseconds()))
receivedLogBlocks = 0
receivedLogHeaders = 0
receivedLogTransactions = 0
lastBlockLogTime = now
bl.receivedLogBlocks = 0
bl.receivedLogHeaders = 0
bl.receivedLogTransactions = 0
bl.lastBlockLogTime = now
}

View File

@ -3,6 +3,7 @@ package blockprocessor
import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/processes/blockprocessor/blocklogger"
"github.com/kaspanet/kaspad/infrastructure/logger"
"time"
)
@ -13,6 +14,7 @@ type blockProcessor struct {
genesisHash *externalapi.DomainHash
targetTimePerBlock time.Duration
databaseContext model.DBManager
blockLogger *blocklogger.BlockLogger
consensusStateManager model.ConsensusStateManager
pruningManager model.PruningManager
@ -49,6 +51,7 @@ func New(
genesisHash *externalapi.DomainHash,
targetTimePerBlock time.Duration,
databaseContext model.DBManager,
consensusStateManager model.ConsensusStateManager,
pruningManager model.PruningManager,
blockValidator model.BlockValidator,
@ -81,6 +84,7 @@ func New(
genesisHash: genesisHash,
targetTimePerBlock: targetTimePerBlock,
databaseContext: databaseContext,
blockLogger: blocklogger.NewBlockLogger(),
pruningManager: pruningManager,
blockValidator: blockValidator,
dagTopologyManager: dagTopologyManager,

View File

@ -2,7 +2,6 @@ package blockprocessor
import (
"fmt"
"github.com/kaspanet/kaspad/domain/consensus/processes/blockprocessor/blocklogger"
"github.com/kaspanet/kaspad/util/difficulty"
"github.com/kaspanet/kaspad/domain/consensus/model"
@ -144,7 +143,7 @@ func (bp *blockProcessor) validateAndInsertBlock(block *externalapi.DomainBlock,
return nil, logClosureErr
}
blocklogger.LogBlock(block)
bp.blockLogger.LogBlock(block)
return &externalapi.BlockInsertionResult{
VirtualSelectedParentChainChanges: selectedParentChainChanges,