Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

progresslog: Make block progress log internal. #2499

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions blockdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/decred/dcrd/blockchain/v4"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/database/v2"
"github.com/decred/dcrd/internal/progresslog"
)

const (
Expand Down Expand Up @@ -194,7 +195,7 @@ func dumpBlockChain(params *chaincfg.Params, b *blockchain.BlockChain) error {
dcrdLog.Infof("Writing the blockchain to flat file %q. This might take a "+
"while...", cfg.DumpBlockchain)

progressLogger := newBlockProgressLogger("Wrote", dcrdLog)
progressLogger := progresslog.New("Wrote", dcrdLog)

file, err := os.Create(cfg.DumpBlockchain)
if err != nil {
Expand Down Expand Up @@ -240,7 +241,7 @@ func dumpBlockChain(params *chaincfg.Params, b *blockchain.BlockChain) error {
return err
}

progressLogger.logBlockHeight(bl, tipHeight)
progressLogger.LogBlockHeight(bl.MsgBlock(), tipHeight)
}

bmgrLog.Infof("Successfully dumped the blockchain (%v blocks) to %v.",
Expand Down
7 changes: 4 additions & 3 deletions blockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/decred/dcrd/internal/fees"
"github.com/decred/dcrd/internal/mempool"
"github.com/decred/dcrd/internal/mining"
"github.com/decred/dcrd/internal/progresslog"
"github.com/decred/dcrd/internal/rpcserver"
peerpkg "github.com/decred/dcrd/peer/v2"
"github.com/decred/dcrd/txscript/v3"
Expand Down Expand Up @@ -306,7 +307,7 @@ type blockManager struct {
rejectedTxns map[chainhash.Hash]struct{}
requestedTxns map[chainhash.Hash]struct{}
requestedBlocks map[chainhash.Hash]struct{}
progressLogger *blockProgressLogger
progressLogger *progresslog.BlockLogger
syncPeer *peerpkg.Peer
msgChan chan interface{}
wg sync.WaitGroup
Expand Down Expand Up @@ -1115,7 +1116,7 @@ func (b *blockManager) handleBlockMsg(bmsg *blockMsg) {
} else {
// When the block is not an orphan, log information about it and
// update the chain state.
b.progressLogger.logBlockHeight(bmsg.block, b.SyncHeight())
b.progressLogger.LogBlockHeight(bmsg.block.MsgBlock(), b.SyncHeight())

if onMainChain {
// Notify stake difficulty subscribers and prune invalidated
Expand Down Expand Up @@ -2462,7 +2463,7 @@ func newBlockManager(config *blockManagerConfig) (*blockManager, error) {
requestedTxns: make(map[chainhash.Hash]struct{}),
requestedBlocks: make(map[chainhash.Hash]struct{}),
peerStates: make(map[*peerpkg.Peer]*peerSyncState),
progressLogger: newBlockProgressLogger("Processed", bmgrLog),
progressLogger: progresslog.New("Processed", bmgrLog),
msgChan: make(chan interface{}, config.MaxPeers*3),
headerList: list.New(),
quit: make(chan struct{}),
Expand Down
26 changes: 26 additions & 0 deletions internal/progresslog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
progresslog
===========

[![Build Status](https://github.com/decred/dcrd/workflows/Build%20and%20Test/badge.svg)](https://github.com/decred/dcrd/actions)
[![ISC License](https://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
[![Doc](https://img.shields.io/badge/doc-reference-blue.svg)](https://pkg.go.dev/github.com/decred/dcrd/internal/progresslog)

Package progresslog provides periodic logging for block processing.

Tests are included to ensure proper functionality.

## Feature Overview

- Maintains cumulative totals about blocks between each logging interval
- Total number of blocks
- Total number of transactions
- Total number of votes
- Total number of tickets
- Total number of revocations
- Logs all cumulative data every 10 seconds
- Immediately logs any outstanding data when the provided sync height is reached

## License

Package progresslog is licensed under the [copyfree](http://copyfree.org) ISC
License.
67 changes: 37 additions & 30 deletions blocklogger.go → internal/progresslog/blocklogger.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
// Copyright (c) 2015-2019 The Decred developers
// Copyright (c) 2015-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package main
package progresslog

import (
"sync"
"time"

"github.com/decred/dcrd/dcrutil/v3"
"github.com/decred/dcrd/wire"
"github.com/decred/slog"
)

// blockProgressLogger provides periodic logging for other services in order
// to show users progress of certain "actions" involving some or all current
// blocks. Ex: syncing to best chain, indexing all blocks, etc.
type blockProgressLogger struct {
// BlockLogger provides periodic logging for other services in order to show
// users progress of certain "actions" involving some or all current blocks.
// For example, syncing to best chain, indexing all blocks, etc.
type BlockLogger struct {
sync.Mutex
subsystemLogger slog.Logger
progressAction string

receivedLogBlocks int64
receivedLogTx int64
receivedLogVotes int64
receivedLogRevocations int64
receivedLogTickets int64
lastBlockLogTime time.Time

subsystemLogger slog.Logger
progressAction string
sync.Mutex
}

// newBlockProgressLogger returns a new block progress logger.
// New returns a new block progress logger.
//
// The progress message is templated as follows:
// {progressAction} {numProcessed} {blocks|block} in the last {timePeriod}
// ({numTxs}, height {lastBlockHeight}, {lastBlockTimeStamp})
func newBlockProgressLogger(progressMessage string, logger slog.Logger) *blockProgressLogger {
return &blockProgressLogger{
// ({numTxs} {transactions|transaction}, {numTickets} {tickets|ticket},
// {numVotes} {votes|vote}, {numRevocations} {revocations|revocation},
// height {lastBlockHeight}, {lastBlockTimeStamp})
func New(progressMessage string, logger slog.Logger) *BlockLogger {
return &BlockLogger{
lastBlockLogTime: time.Now(),
progressAction: progressMessage,
subsystemLogger: logger,
}
}

// logBlockHeight logs a new block height as an information message to show
// progress to the user. In order to prevent spam, it limits logging to one
// LogBlockHeight logs a new block height 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 (b *blockProgressLogger) logBlockHeight(block *dcrutil.Block, syncHeight int64) {
func (b *BlockLogger) LogBlockHeight(block *wire.MsgBlock, syncHeight int64) {
b.Lock()
defer b.Unlock()

header := &block.Header
b.receivedLogBlocks++
b.receivedLogTx += int64(len(block.MsgBlock().Transactions))
b.receivedLogVotes += int64(block.MsgBlock().Header.Voters)
b.receivedLogRevocations += int64(block.MsgBlock().Header.Revocations)
b.receivedLogTickets += int64(block.MsgBlock().Header.FreshStake)
b.receivedLogTx += int64(len(block.Transactions))
b.receivedLogVotes += int64(header.Voters)
b.receivedLogRevocations += int64(header.Revocations)
b.receivedLogTickets += int64(header.FreshStake)
now := time.Now()
duration := now.Sub(b.lastBlockLogTime)
if block.Height() < syncHeight && duration < time.Second*10 {
if int64(header.Height) < syncHeight && duration < time.Second*10 {
return
}

Expand Down Expand Up @@ -82,12 +87,11 @@ func (b *blockProgressLogger) logBlockHeight(block *dcrutil.Block, syncHeight in
if b.receivedLogVotes == 1 {
voteStr = "vote"
}
b.subsystemLogger.Infof("%s %d %s in the last %s (%d %s, %d %s, %d %s, %d %s, height "+
"%d, %s)",
b.progressAction, b.receivedLogBlocks, blockStr, tDuration,
b.receivedLogTx, txStr, b.receivedLogTickets, ticketStr,
b.receivedLogVotes, voteStr, b.receivedLogRevocations,
revocationStr, block.Height(), block.MsgBlock().Header.Timestamp)
b.subsystemLogger.Infof("%s %d %s in the last %s (%d %s, %d %s, %d %s, "+
"%d %s, height %d, %s)", b.progressAction, b.receivedLogBlocks,
blockStr, tDuration, b.receivedLogTx, txStr, b.receivedLogTickets,
ticketStr, b.receivedLogVotes, voteStr, b.receivedLogRevocations,
revocationStr, header.Height, header.Timestamp)

b.receivedLogBlocks = 0
b.receivedLogTx = 0
Expand All @@ -97,6 +101,9 @@ func (b *blockProgressLogger) logBlockHeight(block *dcrutil.Block, syncHeight in
b.lastBlockLogTime = now
}

func (b *blockProgressLogger) SetLastLogTime(time time.Time) {
// SetLastLogTime updates the last time data was logged to the provided time.
func (b *BlockLogger) SetLastLogTime(time time.Time) {
b.Lock()
b.lastBlockLogTime = time
b.Unlock()
}
Loading