-
Notifications
You must be signed in to change notification settings - Fork 33
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
[P2P] chore: concurrent bootstrapping #694
Draft
bryanchriswhite
wants to merge
15
commits into
main
Choose a base branch
from
chore/concurrent-p2p-bootstrap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2bea8a2
chore: concurrent p2p bootstrapping
bryanchriswhite af6189a
chore: update changelog
bryanchriswhite bc6a38e
Merge remote-tracking branch 'pokt/main' into HEAD
bryanchriswhite ebd7231
chore: fix "boostrap" typo
bryanchriswhite ad695f9
fix: invalid zerolog #Fields() arg type`
bryanchriswhite 96fac17
chore: fix comment typo
bryanchriswhite f9ca955
refactor: update import alias
bryanchriswhite 778dfc5
chore: copy limiter from storj/common
bryanchriswhite 28b49a8
fixup: rename bootstrapFromRPC
bryanchriswhite 8dba296
fixup: use limiter instead of waitgroup
bryanchriswhite a6b424a
fixup: always bootstrap
bryanchriswhite c2a4049
chore: add `MaxBootstrapConcurrency`
bryanchriswhite 0eee237
chore: update changelog
bryanchriswhite b41153e
fix: runtime manager test
bryanchriswhite f4fa61e
fix: linter
bryanchriswhite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,10 +10,10 @@ import ( | |||||
"strings" | ||||||
|
||||||
rpcCHP "github.com/pokt-network/pocket/p2p/providers/current_height_provider/rpc" | ||||||
rpcABP "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc" | ||||||
typesP2P "github.com/pokt-network/pocket/p2p/types" | ||||||
rpcPSP "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc" | ||||||
"github.com/pokt-network/pocket/rpc" | ||||||
"github.com/pokt-network/pocket/runtime/defaults" | ||||||
"github.com/pokt-network/pocket/shared/utils" | ||||||
) | ||||||
|
||||||
// configureBootstrapNodes parses the bootstrap nodes from the config and validates them | ||||||
|
@@ -43,37 +43,54 @@ func (m *p2pModule) configureBootstrapNodes() error { | |||||
|
||||||
// bootstrap attempts to bootstrap from a bootstrap node | ||||||
func (m *p2pModule) bootstrap() error { | ||||||
var pstore typesP2P.Peerstore | ||||||
limiter := utils.NewLimiter(int(m.cfg.MaxBootstrapConcurrency)) | ||||||
|
||||||
for _, bootstrapNode := range m.bootstrapNodes { | ||||||
m.logger.Info().Str("endpoint", bootstrapNode).Msg("Attempting to bootstrap from bootstrap node") | ||||||
for _, serviceURL := range m.bootstrapNodes { | ||||||
// concurrent bootstrapping | ||||||
// TECHDEBT(#595): add ctx to interface methods and propagate down. | ||||||
limiter.Go(context.TODO(), func() { | ||||||
m.bootstrapFromRPC(strings.Clone(serviceURL)) | ||||||
}) | ||||||
} | ||||||
|
||||||
client, err := rpc.NewClientWithResponses(bootstrapNode) | ||||||
if err != nil { | ||||||
continue | ||||||
} | ||||||
healthCheck, err := client.GetV1Health(context.TODO()) | ||||||
if err != nil || healthCheck == nil || healthCheck.StatusCode != http.StatusOK { | ||||||
m.logger.Warn().Str("bootstrapNode", bootstrapNode).Msg("Error getting a green health check from bootstrap node") | ||||||
continue | ||||||
} | ||||||
limiter.Close() | ||||||
|
||||||
pstoreProvider := rpcABP.NewRPCPeerstoreProvider( | ||||||
rpcABP.WithP2PConfig( | ||||||
m.GetBus().GetRuntimeMgr().GetConfig().P2P, | ||||||
), | ||||||
rpcABP.WithCustomRPCURL(bootstrapNode), | ||||||
) | ||||||
return nil | ||||||
} | ||||||
|
||||||
currentHeightProvider := rpcCHP.NewRPCCurrentHeightProvider(rpcCHP.WithCustomRPCURL(bootstrapNode)) | ||||||
// bootstrapFromRPC fetches the peerstore of the peer at `serviceURL` via RPC | ||||||
// and adds it to this host's peerstore after performing a health check. | ||||||
// TECHDEBT(SOLID): refactor; this method has more than one reason to change | ||||||
func (m *p2pModule) bootstrapFromRPC(serviceURL string) { | ||||||
m.logger.Info().Str("endpoint", serviceURL).Msg("Attempting to bootstrap from bootstrap node") | ||||||
|
||||||
pstore, err = pstoreProvider.GetStakedPeerstoreAtHeight(currentHeightProvider.CurrentHeight()) | ||||||
if err != nil { | ||||||
m.logger.Warn().Err(err).Str("endpoint", bootstrapNode).Msg("Error getting address book from bootstrap node") | ||||||
continue | ||||||
} | ||||||
client, err := rpc.NewClientWithResponses(serviceURL) | ||||||
if err != nil { | ||||||
return | ||||||
} | ||||||
healthCheck, err := client.GetV1Health(context.TODO()) | ||||||
if err != nil || healthCheck == nil || healthCheck.StatusCode != http.StatusOK { | ||||||
m.logger.Warn().Str("serviceURL", serviceURL).Msg("Error getting a green health check from bootstrap node") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return | ||||||
} | ||||||
|
||||||
// fetch `serviceURL`'s peerstore | ||||||
pstoreProvider := rpcPSP.NewRPCPeerstoreProvider( | ||||||
rpcPSP.WithP2PConfig( | ||||||
m.GetBus().GetRuntimeMgr().GetConfig().P2P, | ||||||
), | ||||||
rpcPSP.WithCustomRPCURL(serviceURL), | ||||||
) | ||||||
|
||||||
currentHeightProvider := rpcCHP.NewRPCCurrentHeightProvider(rpcCHP.WithCustomRPCURL(serviceURL)) | ||||||
|
||||||
pstore, err := pstoreProvider.GetStakedPeerstoreAtHeight(currentHeightProvider.CurrentHeight()) | ||||||
if err != nil { | ||||||
m.logger.Warn().Err(err).Str("endpoint", serviceURL).Msg("Error getting address book from bootstrap node") | ||||||
return | ||||||
} | ||||||
|
||||||
// add `serviceURL`'s peers to this node's peerstore | ||||||
for _, peer := range pstore.GetPeerList() { | ||||||
m.logger.Debug().Str("address", peer.GetAddress().String()).Msg("Adding peer to network") | ||||||
if err := m.network.AddPeer(peer); err != nil { | ||||||
|
@@ -82,11 +99,6 @@ func (m *p2pModule) bootstrap() error { | |||||
Msg("adding peer") | ||||||
} | ||||||
} | ||||||
|
||||||
if m.network.GetPeerstore().Size() == 0 { | ||||||
return fmt.Errorf("bootstrap failed") | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func isValidHostnamePort(str string) bool { | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/*Copyright (c) 2020 Storj Labs, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package utils | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// Limiter implements concurrent goroutine limiting. | ||
// | ||
// After calling Wait or Close, no new goroutines are allowed | ||
// to start. | ||
type Limiter struct { | ||
noCopy noCopy //nolint:structcheck // see noCopy definition | ||
|
||
limit chan struct{} | ||
close sync.Once | ||
closed chan struct{} | ||
} | ||
|
||
// NewLimiter creates a new limiter with limit set to n. | ||
func NewLimiter(n int) *Limiter { | ||
return &Limiter{ | ||
limit: make(chan struct{}, n), | ||
closed: make(chan struct{}), | ||
} | ||
} | ||
|
||
// Go tries to start fn as a goroutine. | ||
// When the limit is reached it will wait until it can run it | ||
// or the context is canceled. | ||
func (limiter *Limiter) Go(ctx context.Context, fn func()) bool { | ||
if ctx.Err() != nil { | ||
return false | ||
} | ||
|
||
select { | ||
case limiter.limit <- struct{}{}: | ||
case <-limiter.closed: | ||
return false | ||
case <-ctx.Done(): | ||
return false | ||
} | ||
|
||
go func() { | ||
defer func() { <-limiter.limit }() | ||
fn() | ||
}() | ||
|
||
return true | ||
} | ||
|
||
// Wait waits for all running goroutines to finish and | ||
// disallows new goroutines to start. | ||
func (limiter *Limiter) Wait() { limiter.Close() } | ||
|
||
// Close waits for all running goroutines to finish and | ||
// disallows new goroutines to start. | ||
func (limiter *Limiter) Close() { | ||
limiter.close.Do(func() { | ||
close(limiter.closed) | ||
// ensure all goroutines are finished | ||
for i := 0; i < cap(limiter.limit); i++ { | ||
limiter.limit <- struct{}{} | ||
} | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀