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

GetBlock() - Simple optimisations #9541

Merged
merged 32 commits into from
Oct 5, 2021
Merged

GetBlock() - Simple optimisations #9541

merged 32 commits into from
Oct 5, 2021

Conversation

jmozah
Copy link
Contributor

@jmozah jmozah commented Sep 7, 2021

What type of PR is this?
Optimisation

What does this PR do? Why is it needed?
During debugging of #8943, it was found that some of the operations done
during GetBlock() can be optimised further. This PR does the simple optimisations under a flag.

Which issues(s) does this PR fix?
Related to #8943

Other notes for review
The flag is --enable-get-block-optimizations, and it is disabled by default.

@jmozah jmozah requested a review from nisdas September 7, 2021 10:49
@jmozah jmozah self-assigned this Sep 7, 2021
@jmozah jmozah requested a review from a team as a code owner September 7, 2021 10:49
@jmozah jmozah requested review from terencechain and rkapka and removed request for a team September 7, 2021 10:49
@codecov
Copy link

codecov bot commented Sep 7, 2021

Codecov Report

Merging #9541 (84088db) into develop (393549a) will decrease coverage by 0.43%.
The diff coverage is 18.36%.

❗ Current head 84088db differs from pull request most recent head af11581. Consider uploading reports for the commit af11581 to get more accurate results

@@             Coverage Diff             @@
##           develop    #9541      +/-   ##
===========================================
- Coverage    62.34%   61.90%   -0.44%     
===========================================
  Files          572      565       -7     
  Lines        45457    44750     -707     
===========================================
- Hits         28339    27703     -636     
+ Misses       12568    12555      -13     
+ Partials      4550     4492      -58     


eg.Go(func() error {
// Pack ETH1 deposits which have not been included in the beacon chain.
localDeposits, err := vs.deposits(ctx, head, eth1Data)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't you use egctx here?

Suggested change
localDeposits, err := vs.deposits(ctx, head, eth1Data)
localDeposits, err := vs.deposits(egctx, head, eth1Data)


eg.Go(func() error {
// Pack aggregated attestations which have not been included in the beacon chain.
localAtts, err := vs.packAttestations(ctx, head)
Copy link
Member

Choose a reason for hiding this comment

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

Here too

Suggested change
localAtts, err := vs.packAttestations(ctx, head)
localAtts, err := vs.packAttestations(egctx, head)

shared/featureconfig/flags.go Outdated Show resolved Hide resolved
// This is very likely to happen because BlockTimeByHeight returns the last block AT OR BEFORE the specified time.
if lastBlockByEarliestValidTime.Time < earliestValidTime {
lastBlockByEarliestValidTime.Number = big.NewInt(0).Add(lastBlockByEarliestValidTime.Number, big.NewInt(1))
if !featureconfig.Get().EnableGetBlockOptimizations {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this branch omitted when the flag is on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This branch seems to not do anything here.
lastBlockByEarliestValidTime is never used anywhere inside the code.
I need a confirmation if my thinking is right.
WDYT @nisdas

Copy link
Member

Choose a reason for hiding this comment

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

That is a bit more complicated, as that value was used for our inRangeVotes method. This was removed previously when we had the voting bug and defaulted to independent voting compared to voting by majority. Do we ever expect to add the vote by majority feature back ? @prestonvanloon

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My 2 cents: Even if want to add the feature back sometime later. Let's remove unused code for now.

Copy link
Member

Choose a reason for hiding this comment

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

Discussed offline, its better to just remove it now. Performing the extra BlockByTimeStamp call is expensive and can increase block proposal times needlessly.

Comment on lines 23 to 25
dbpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
Copy link
Member

Choose a reason for hiding this comment

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

These are the same imports, please use only one

Copy link
Member

@terencechain terencechain left a comment

Choose a reason for hiding this comment

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

Can we implement benchmarks on before and after? We should know how much this improves

Numbers from tracing instrumentation will be helpful too

@@ -230,16 +232,9 @@ func (vs *Server) buildPhase0BlockData(ctx context.Context, req *ethpb.BlockRequ
return nil, fmt.Errorf("could not get ETH1 data: %v", err)
}

// Pack ETH1 deposits which have not been included in the beacon chain.
deposits, err := vs.deposits(ctx, head, eth1Data)
deposits, atts, err := vs.depositAndPackAttestations(ctx, head, eth1Data)
Copy link
Member

Choose a reason for hiding this comment

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

it is a bit odd to group deposit and attestation packing into one method. But I do understand that it makes it cleaner when applying the flag.

@jmozah
Copy link
Contributor Author

jmozah commented Sep 13, 2021

Can we implement benchmarks on before and after? We should know how much this improves

Numbers from tracing instrumentation will be helpful too

@terencechain yeah we can do that.

@jmozah
Copy link
Contributor Author

jmozah commented Sep 13, 2021

Can we implement benchmarks on before and after? We should know how much this improves
Numbers from tracing instrumentation will be helpful too

@terencechain yeah we can do that.

@terencechain On the second thought, i think it will be nice to run the instrumentation and monitor block proposal times, after we fix the rest of the fixes related to getBlock(). What say?

@terencechain
Copy link
Member

Can we implement benchmarks on before and after? We should know how much this improves
Numbers from tracing instrumentation will be helpful too

@terencechain yeah we can do that.

@terencechain On the second thought, i think it will be nice to run the instrumentation and monitor block proposal times, after we fix the rest of the fixes related to getBlock(). What say?

That's fine with me. Let's wait to merge this until we can monitor block proposal time then

Another method here (easier) is to run getblock benchmark test on both featureflag on/off

@jmozah
Copy link
Contributor Author

jmozah commented Sep 13, 2021

Another method here (easier) is to run getblock benchmark test on both featureflag on/off

Thats a nice idea. I will do this.

@jmozah
Copy link
Contributor Author

jmozah commented Sep 13, 2021

Another method here (easier) is to run getblock benchmark test on both featureflag on/off

Thats a nice idea. I will do this.

Decided offline with @terencechain to keep this PR open until we fix other issues in GetBlock()

@jmozah
Copy link
Contributor Author

jmozah commented Sep 16, 2021

This PR will be closed once all the tracking issues are closed.

}
// Increment the earliest block if the original block's time is before valid time.
// This is very likely to happen because BlockTimeByHeight returns the last block AT OR BEFORE the specified time.
if lastBlockByEarliestValidTime.Time < earliestValidTime {
Copy link
Member

Choose a reason for hiding this comment

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

if this value isnt being used anywhere you can remove it, no need to gate it behind the flag.

Copy link
Contributor Author

@jmozah jmozah Sep 30, 2021

Choose a reason for hiding this comment

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

Makes sense. Done.

@jmozah jmozah changed the title GetBlock() optimizations GetBlock() - Simple optimisations Sep 30, 2021
@jmozah jmozah mentioned this pull request Sep 30, 2021
6 tasks
@jmozah jmozah requested review from nisdas, a team and kasey and removed request for rkapka, terencechain, nisdas, a team and kasey September 30, 2021 15:39
@@ -278,6 +271,70 @@ func (vs *Server) ProposeBeaconBlock(ctx context.Context, req *ethpb.GenericSign
return vs.proposeGenericBeaconBlock(ctx, blk)
}

func (vs *Server) depositAndPackAttestations(ctx context.Context, head state.BeaconState, eth1Data *ethpb.Eth1Data) ([]*ethpb.Deposit, []*ethpb.Attestation, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (vs *Server) depositAndPackAttestations(ctx context.Context, head state.BeaconState, eth1Data *ethpb.Eth1Data) ([]*ethpb.Deposit, []*ethpb.Attestation, error) {
func (vs *Server) packDepositsAndAttestations(ctx context.Context, head state.BeaconState, eth1Data *ethpb.Eth1Data) ([]*ethpb.Deposit, []*ethpb.Attestation, error) {

return deposits, atts, nil
}

func (vs *Server) optimizedDepositAndPackAttestations(ctx context.Context, head state.BeaconState, eth1Data *ethpb.Eth1Data) ([]*ethpb.Deposit, []*ethpb.Attestation, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (vs *Server) optimizedDepositAndPackAttestations(ctx context.Context, head state.BeaconState, eth1Data *ethpb.Eth1Data) ([]*ethpb.Deposit, []*ethpb.Attestation, error) {
func (vs *Server) optimizedPackDepositsAndAttestations(ctx context.Context, head state.BeaconState, eth1Data *ethpb.Eth1Data) ([]*ethpb.Deposit, []*ethpb.Attestation, error) {

if err != nil {
return status.Errorf(codes.Internal, "Could not get ETH1 deposits: %v", err)
}
// if the original context is cancelled, then cancel this routine toobazel run //:gazelle -- fix`
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// if the original context is cancelled, then cancel this routine toobazel run //:gazelle -- fix`
// if the original context is cancelled, then cancel this routine too

@@ -551,7 +605,7 @@ func (vs *Server) deposits(
if vs.MockEth1Votes || !vs.Eth1InfoFetcher.IsConnectedToETH1() {
return []*ethpb.Deposit{}, nil
}
// Need to fetch if the deposits up to the state's latest eth 1 data matches
// Need to fetch if the deposits up to the state's latest ethpb 1 data matches
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Need to fetch if the deposits up to the state's latest ethpb 1 data matches
// Need to fetch if the deposits up to the state's latest eth 1 data matches

@@ -50,6 +50,7 @@ type Flags struct {
EnableOptimizedBalanceUpdate bool // EnableOptimizedBalanceUpdate uses an updated method of performing balance updates.
EnableDoppelGanger bool // EnableDoppelGanger enables doppelganger protection on startup for the validator.
EnableHistoricalSpaceRepresentation bool // EnableHistoricalSpaceRepresentation enables the saving of registry validators in separate buckets to save space
EnableGetBlockOptimizations bool // EnableGetBlockOptimizations optimizes some elements of the GetBlock() function
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
EnableGetBlockOptimizations bool // EnableGetBlockOptimizations optimizes some elements of the GetBlock() function
EnableGetBlockOptimizations bool // EnableGetBlockOptimizations optimizes some elements of the GetBlock() function.

nisdas
nisdas previously approved these changes Oct 1, 2021
Co-authored-by: Nishant Das <nishdas93@gmail.com>
@nisdas nisdas merged commit 362dfa6 into develop Oct 5, 2021
@delete-merged-branch delete-merged-branch bot deleted the getblock_optimization branch October 5, 2021 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants