Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

[R4R]supoort hot sync reactor #97

Merged
merged 11 commits into from
Aug 27, 2019
Merged

[R4R]supoort hot sync reactor #97

merged 11 commits into from
Aug 27, 2019

Conversation

unclezoro
Copy link
Collaborator

@unclezoro unclezoro commented Jun 20, 2019

Description

introduce a new block sync protocol to reduce network and CPU resources for full node/witness.

more detail in:
hotsync.pptx

Rationale

SyncPattern is the work pattern of BlockPool.

  1. Mute: will only answer subscribe requests from others, nor will sync from others or from consensus channel.
  2. Hot: handle subscribe requests from other peers as a publisher, also subscribe block from other peers as a subscriber.
  3. Consensus: handle subscribe requests from another peer as a publisher, but subscribe block message from consensus channel.
    The viable transitions are:
                                       Hot --> Consensus
	                                 ^      ^
	                                 |     /
	                                 |   /
	                                 Mute

Example

Under consensus pattern, the hight diff between validator and seed:
WechatIMG3
Under hotSYnc pattern, the hight diff between validator and seed:
WechatIMG4

Changes

Preflight checks

  • build passed (make build)
  • tests passed (make test)
  • integration tests passed (make integration_test)
  • manual transaction test passed (cli invoke)

Already reviewed by

Related issues

... reference related issue #'s here ...

@unclezoro unclezoro changed the title supoort hot reactor [WIP]supoort hot reactor Jun 20, 2019
@unclezoro unclezoro force-pushed the hotreactor branch 13 times, most recently from c8e4ebc to 73072a9 Compare June 21, 2019 09:45
@unclezoro unclezoro changed the title [WIP]supoort hot reactor [R4R]supoort hot reactor Jun 21, 2019
@unclezoro unclezoro requested review from ackratos and darren-liu and removed request for ackratos June 21, 2019 09:45
@unclezoro unclezoro self-assigned this Jun 21, 2019
@unclezoro unclezoro changed the title [R4R]supoort hot reactor [R4R]supoort hot sync reactor Jun 21, 2019
blockchain/hot/pool.go Outdated Show resolved Hide resolved
blockchain/hot/candidate.go Outdated Show resolved Hide resolved
blockchain/hot/candidate.go Outdated Show resolved Hide resolved
switch msg := msg.(type) {
case *blockSubscribeMessage:
hbcR.Logger.Debug("receive blockSubscribeMessage from peer", "peer", src.ID(), "from_height", msg.FromHeight, "to_height", msg.ToHeight)
hbcR.pool.handleSubscribeBlock(msg.FromHeight, msg.ToHeight, src)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the handleSubscribeBlock seems to be a costly operation, it may be an attack point.
also since currently the Receive method runs in sync mode and will block other msg handlers, we need to reconsider this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid memory leak, only the subscribe height is small than currentHeight +maxPublishForesight will be accepted. When the height is small and load two blocks from db takes time, fast_sync have the same concern, only the p2p layer has a send rate limit to defend this.

fix peer lost issue

fix metrics labels

change parameters and log

add more log

try to answer even mute

reduce redundant peers for candidate

remove debug code

do not deliver commit if miss blockId

change sync pattern description

avoid mix use of lock

avoid timer leak

decrease lock use

fix current map read and write

avoid message bust

add more logs

keep blockchain package stay

fix import

change default config

fix review
// the time interval to correct current state.
tryRepairInterval = 1 * time.Second

maxCachedSealedBlock = 100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better make these configurable

Copy link
Collaborator Author

@unclezoro unclezoro Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rethink this, it looks like no impetus to change these configures, these configure can't affect something important. I hope to keep a thin config file.

blockchain/hot/pool.go Outdated Show resolved Hide resolved
blockchain/hot/pool.go Outdated Show resolved Hide resolved

// most of the usage have locked. Only newBlockStateAtHeight do not, but the usage can't
// currently excuted with incCurrentHeight, it is safe.
func (pool *BlockPool) expectingHeight() int64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RWLock ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have lock where called expectingHeight

blockchain/hot/candidate.go Outdated Show resolved Hide resolved
blockchain/hot/candidate.go Outdated Show resolved Hide resolved
blockchain/hot/candidate.go Outdated Show resolved Hide resolved
blockchain/hot/candidate.go Outdated Show resolved Hide resolved
blockchain/hot/candidate.go Outdated Show resolved Hide resolved
hbcR.Logger.Info("hot sync switching to consensus sync")
conR, ok := hbcR.Switch.Reactor("CONSENSUS").(consensusReactor)
if ok {
hbcR.pool.SwitchToConsensusSync(nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not pass in hbcR.pool.state and get rid of stateCopy within SwitchToConsensusSync function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because both fast sync reactor and hot sync reactor are possible to call SwitchToConsensusSync. Because state will be used by consensus reactor, hot sync reactor do not want to hold the same lock, so use copy.

return pool.st
}

func (pool *BlockPool) verifyCommit(blockID types.BlockID, commit *types.Commit) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest add a "Guarded" suffix to all methods in this file who is called with guardance of pool.mtx. For this method, I have to trace his 3 level parents to reason about it is correctly guarded.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add with "Guarded" prefix will decrease the original intention of the function. Even I add "Guarded" suffix, maybe the func is not locked by pool.mtx.

blockchain/hot/pool.go Outdated Show resolved Hide resolved
blockchain/hot/pool.go Outdated Show resolved Hide resolved
blockchain/hot/pool.go Outdated Show resolved Hide resolved
section = section + (1 - float64(m.average)/total)
diceSection = append(diceSection, section)
}
diceValue := rand.Float64() * diceSection[len(diceSection)-1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c.random?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

func newPeerMetrics() peerMetrics {
return peerMetrics{
samples: list.New(),
sampleSequence: rand.Int63n(recalculateInterval),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass c.random in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

@unclezoro unclezoro merged commit f22cda4 into develop Aug 27, 2019
unclezoro added a commit that referenced this pull request Sep 6, 2019
* add index recovery

* fix import order

* add err judge

* fix review sugest

* add default  abciResponses

* change indexHeight to indexHeightKey

* remove debug log

improve performance of IsPersistent

trim p2p metrics

add BroadcastFromNonePersistent config to mempool

remove unnecessary metrics

rename config

fix testcase

* fix review

* change log

* [R4R]supoort hot sync reactor (#97)

* supoort hot reactor

fix peer lost issue

fix metrics labels

change parameters and log

add more log

try to answer even mute

reduce redundant peers for candidate

remove debug code

do not deliver commit if miss blockId

change sync pattern description

avoid mix use of lock

avoid timer leak

decrease lock use

fix current map read and write

avoid message bust

add more logs

keep blockchain package stay

fix import

change default config

fix review

* use a single goroutine for switch

* fix review suggestion

* fix test case

* fix pick error

*  move to decayed once picked freshset

* update peerstate

* fix pool test case

* change issue num of pending log

* use random with seed

* use seed random

* prepare for release  v0.31.5-binance.2 (#112)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants