-
Notifications
You must be signed in to change notification settings - Fork 233
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
feat(bootstrap): take autobootstrap to completion #403
Changes from 2 commits
e2842f0
7cce5bd
da6edaf
632f3c5
98cf914
2fdad28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,8 @@ type IpfsDHT struct { | |
|
||
bootstrapCfg opts.BootstrapConfig | ||
|
||
triggerBootstrap chan struct{} | ||
triggerAutoBootstrap bool | ||
triggerBootstrap chan *bootstrapReq | ||
} | ||
|
||
// Assert that IPFS assumptions about interfaces aren't broken. These aren't a | ||
|
@@ -103,12 +104,14 @@ func New(ctx context.Context, h host.Host, options ...opts.Option) (*IpfsDHT, er | |
|
||
dht.proc.AddChild(dht.providers.Process()) | ||
dht.Validator = cfg.Validator | ||
dht.triggerAutoBootstrap = cfg.TriggerAutoBootstrap | ||
|
||
if !cfg.Client { | ||
for _, p := range cfg.Protocols { | ||
h.SetStreamHandler(p, dht.handleNewStream) | ||
} | ||
} | ||
dht.startBootstrapping() | ||
return dht, nil | ||
} | ||
|
||
|
@@ -159,7 +162,7 @@ func makeDHT(ctx context.Context, h host.Host, dstore ds.Batching, protocols []p | |
routingTable: rt, | ||
protocols: protocols, | ||
bucketSize: bucketSize, | ||
triggerBootstrap: make(chan struct{}), | ||
triggerBootstrap: make(chan *bootstrapReq), | ||
} | ||
|
||
dht.ctx = dht.newContextWithLocalTags(ctx) | ||
|
@@ -171,10 +174,10 @@ func makeDHT(ctx context.Context, h host.Host, dstore ds.Batching, protocols []p | |
// come up with an alternative solution. | ||
// issue is being tracked at https://github.com/libp2p/go-libp2p-kad-dht/issues/387 | ||
/*func (dht *IpfsDHT) rtRecovery(proc goprocess.Process) { | ||
writeResp := func(errorChan chan error, err error) { | ||
writeResp := func(errorChan chan error, errChan error) { | ||
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. sed? |
||
select { | ||
case <-proc.Closing(): | ||
case errorChan <- err: | ||
case errorChan <- errChan: | ||
} | ||
close(errorChan) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import ( | |
"sync" | ||
"time" | ||
|
||
process "github.com/jbenet/goprocess" | ||
processctx "github.com/jbenet/goprocess/context" | ||
"github.com/libp2p/go-libp2p-core/routing" | ||
"github.com/multiformats/go-multiaddr" | ||
_ "github.com/multiformats/go-multiaddr-dns" | ||
|
@@ -41,52 +43,71 @@ func init() { | |
} | ||
} | ||
|
||
// BootstrapConfig runs cfg.Queries bootstrap queries every cfg.BucketPeriod. | ||
func (dht *IpfsDHT) Bootstrap(ctx context.Context) error { | ||
triggerBootstrapFnc := func() { | ||
logger.Infof("triggerBootstrapFnc: RT only has %d peers which is less than the min threshold of %d, triggering self & bucket bootstrap", | ||
dht.routingTable.Size(), minRTBootstrapThreshold) | ||
type bootstrapReq struct { | ||
errChan chan error | ||
} | ||
|
||
if err := dht.selfWalk(ctx); err != nil { | ||
logger.Warningf("triggerBootstrapFnc: self walk: error: %s", err) | ||
} | ||
func makeBootstrapReq() *bootstrapReq { | ||
errChan := make(chan error, 1) | ||
return &bootstrapReq{errChan} | ||
} | ||
|
||
if err := dht.bootstrapBuckets(ctx); err != nil { | ||
logger.Warningf("triggerBootstrapFnc: bootstrap buckets: error bootstrapping: %s", err) | ||
} | ||
} | ||
// Bootstrap i | ||
func (dht *IpfsDHT) startBootstrapping() error { | ||
// scan the RT table periodically & do a random walk on k-buckets that haven't been queried since the given bucket period | ||
dht.proc.Go(func(proc process.Process) { | ||
ctx := processctx.OnClosingContext(proc) | ||
|
||
// we should query for self periodically so we can discover closer peers | ||
go func() { | ||
for { | ||
err := dht.selfWalk(ctx) | ||
if err != nil { | ||
logger.Warningf("self walk: error: %s", err) | ||
} | ||
select { | ||
case <-time.After(dht.bootstrapCfg.SelfQueryInterval): | ||
case <-ctx.Done(): | ||
return | ||
scanInterval := time.NewTicker(dht.bootstrapCfg.BucketPeriod) | ||
defer scanInterval.Stop() | ||
|
||
var ( | ||
lastSelfWalk time.Time | ||
) | ||
|
||
// run bootstrap if option is set | ||
if dht.triggerAutoBootstrap { | ||
if err := dht.doBootstrap(ctx, true, &lastSelfWalk); err != nil { | ||
logger.Warningf("bootstrap error: %s", err) | ||
} | ||
} | ||
}() | ||
|
||
// scan the RT table periodically & do a random walk on k-buckets that haven't been queried since the given bucket period | ||
go func() { | ||
for { | ||
err := dht.bootstrapBuckets(ctx) | ||
if err != nil { | ||
logger.Warningf("bootstrap buckets: error bootstrapping: %s", err) | ||
} | ||
select { | ||
case <-time.After(dht.bootstrapCfg.RoutingTableScanInterval): | ||
case <-dht.triggerBootstrap: | ||
triggerBootstrapFnc() | ||
case now := <-scanInterval.C: | ||
walkSelf := now.After(lastSelfWalk.Add(dht.bootstrapCfg.SelfQueryInterval)) | ||
if err := dht.doBootstrap(ctx, walkSelf, &lastSelfWalk); err != nil { | ||
logger.Warning("bootstrap error: %s", err) | ||
} | ||
case req := <-dht.triggerBootstrap: | ||
logger.Infof("triggering a bootstrap: RT has %d peers", dht.routingTable.Size()) | ||
err := dht.doBootstrap(ctx, true, &lastSelfWalk) | ||
select { | ||
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. If the err channel always has 1 slot, we shouldn't need to do this. We should be able to just run: req.errChan <- err
close(req.errChan) 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. @Stebalien: Oh ofcourse, but we can't rely on clients always sending a buffered channel. 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. Isn't this internal? 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. It really is just a precautionary measure. If in the future, a go-routine within Dht ("client") decides to a trigger a bootstrap & passes an unbuffered channel because of whatever reason(programmer oversight for example), the whole bootstrapping loop comes to a halt unless the "client" reads from it. We don't want to rely on that. 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. Meh, either way. IMO, the idiom of passing respone channels with a size of 1 is common enough that users should always check how a channel is being used before passing an unbuffered response channel. But either way works. |
||
case req.errChan <- err: | ||
close(req.errChan) | ||
default: | ||
} | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
func (dht *IpfsDHT) doBootstrap(ctx context.Context, walkSelf bool, latestSelfWalk *time.Time) error { | ||
if walkSelf { | ||
if err := dht.selfWalk(ctx); err != nil { | ||
return fmt.Errorf("self walk: error: %s", err) | ||
} else { | ||
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. nit: unnecessary else. |
||
*latestSelfWalk = time.Now() | ||
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. Might as well just make this a field in the |
||
} | ||
} | ||
|
||
if err := dht.bootstrapBuckets(ctx); err != nil { | ||
return fmt.Errorf("bootstrap buckets: error bootstrapping: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
@@ -166,11 +187,15 @@ func (dht *IpfsDHT) selfWalk(ctx context.Context) error { | |
return err | ||
} | ||
|
||
// synchronous bootstrap. | ||
func (dht *IpfsDHT) bootstrapOnce(ctx context.Context) error { | ||
if err := dht.selfWalk(ctx); err != nil { | ||
return errors.Wrap(err, "failed bootstrap while searching for self") | ||
} else { | ||
return dht.bootstrapBuckets(ctx) | ||
// Bootstrap tells the DHT to get into a bootstrapped state. | ||
// | ||
// Note: the context is ignored. | ||
func (dht *IpfsDHT) Bootstrap(_ context.Context) error { | ||
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. Bootstrap is actually supposed to be async (at the moment). We can change this, but we'll need to propagate the change up the interfaces. 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. As discussed below, this will now be async. |
||
req := makeBootstrapReq() | ||
select { | ||
case dht.triggerBootstrap <- req: | ||
return <-req.errChan | ||
default: | ||
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. This is going to behave inconsistently. If we're currently bootstrapping, we'll return immediately. If we're going to block, a new bootstrap call should probably try to "join" an existing one. However, that will add some complexity. 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. @hsanjuan what is cluster's specific need:
Note: queries should still work even if the DHT isn't bootstrapped, as long as you have some peers. 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. I need a peer to get start getting "well-connected" after the first contact to other peer(s) [before that the peer would have had no connections). I don't need to wait until the end of the bootstrap round (it can just happen in background) You may say "well, call Bootstrap()" then and not before, but things are a bit more tricky (the dht may have already been bootstrapped externally and what not). Being able to trigger a bootstrap round on demand (independent from the recurring configured one) is handy here. 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.
This should now be automatic.
SGTM. I agree bootstrap should actually trigger a bootstrap. @aarshkshah1992 given this, I wouldn't wait. I'd just trigger the bootstrap and move on. |
||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,9 @@ func (nn *netNotifiee) Connected(n network.Network, v network.Conn) { | |
if dht.host.Network().Connectedness(p) == network.Connected { | ||
bootstrap := dht.routingTable.Size() <= minRTBootstrapThreshold | ||
dht.Update(dht.Context(), p) | ||
if bootstrap { | ||
if bootstrap && dht.triggerAutoBootstrap { | ||
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. Some tests rely on a specific number of peers in the RT/ specific number of results in a query after a peer has finished connecting to other peers. However, this code causes the peer to connect to more peers/adds more peers to the RT than it intended to in the test & that causes the test to fail. So, we need to disable this feature for tests. |
||
select { | ||
case dht.triggerBootstrap <- struct{}{}: | ||
case dht.triggerBootstrap <- makeBootstrapReq(): | ||
default: | ||
} | ||
} | ||
|
@@ -80,9 +80,9 @@ func (nn *netNotifiee) testConnection(v network.Conn) { | |
if dht.host.Network().Connectedness(p) == network.Connected { | ||
bootstrap := dht.routingTable.Size() <= minRTBootstrapThreshold | ||
dht.Update(dht.Context(), p) | ||
if bootstrap { | ||
if bootstrap && dht.triggerAutoBootstrap { | ||
select { | ||
case dht.triggerBootstrap <- struct{}{}: | ||
case dht.triggerBootstrap <- makeBootstrapReq(): | ||
default: | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,20 +21,20 @@ var ( | |
|
||
// BootstrapConfig specifies parameters used for bootstrapping the DHT. | ||
type BootstrapConfig struct { | ||
BucketPeriod time.Duration // how long to wait for a k-bucket to be queried before doing a random walk on it | ||
Timeout time.Duration // how long to wait for a bootstrap query to run | ||
RoutingTableScanInterval time.Duration // how often to scan the RT for k-buckets that haven't been queried since the given period | ||
SelfQueryInterval time.Duration // how often to query for self | ||
BucketPeriod time.Duration // how long to wait for a k-bucket to be queried before doing a random walk on it | ||
Timeout time.Duration // how long to wait for a bootstrap query to run | ||
SelfQueryInterval time.Duration // how often to query for self | ||
} | ||
|
||
// Options is a structure containing all the options that can be used when constructing a DHT. | ||
type Options struct { | ||
Datastore ds.Batching | ||
Validator record.Validator | ||
Client bool | ||
Protocols []protocol.ID | ||
BucketSize int | ||
BootstrapConfig BootstrapConfig | ||
Datastore ds.Batching | ||
Validator record.Validator | ||
Client bool | ||
Protocols []protocol.ID | ||
BucketSize int | ||
BootstrapConfig BootstrapConfig | ||
TriggerAutoBootstrap bool | ||
} | ||
|
||
// Apply applies the given options to this Option | ||
|
@@ -63,14 +63,13 @@ var Defaults = func(o *Options) error { | |
// same as that mentioned in the kad dht paper | ||
BucketPeriod: 1 * time.Hour, | ||
|
||
// since the default bucket period is 1 hour, a scan interval of 30 minutes sounds reasonable | ||
RoutingTableScanInterval: 30 * time.Minute, | ||
|
||
Timeout: 10 * time.Second, | ||
|
||
SelfQueryInterval: 1 * time.Hour, | ||
} | ||
|
||
o.TriggerAutoBootstrap = true | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -149,3 +148,11 @@ func BucketSize(bucketSize int) Option { | |
return nil | ||
} | ||
} | ||
|
||
// DisableAutoBootstrap disables auto bootstrap on the dht | ||
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. So, all this option actually does is skip the initial bootstrap. We'll still auto-bootstrap. Given that, really, we only care about this in tests (as far as I know), we could:
(We could also not start bootstrapping till the user calls 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. This option does two things actually:
I agree that we only need to do this for tests for now. But, what's the harm in having a visible option to disable this should someone wish to run the Dht without this side effect ? 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. Ah, I see. Yeah, that makes sense (although it's still a bit confusing as it doesn't completely disable automatic bootstrap). But some good documentation should be enough. 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. Hm. Actually, I think this is still too confusing and too weird of an option to expose in a public API. I can't think of a single (real) use-case for it and I can think of plenty of ways in which it can be used incorrectly. If we're going to have a public option to disable automatic bootstrapping, it should completely disable automatic bootstrapping. We can probably just have this option modify the bootstrap config to set the bootstrap intervals to 0 (and then special-case 0 to disable the ticker). That way, we'll only bootstrap on request. |
||
func DisableAutoBootstrap() Option { | ||
return func(o *Options) error { | ||
o.TriggerAutoBootstrap = false | ||
return nil | ||
} | ||
} |
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.
We no longer need the
bootstrapReq
, as far as I can tell.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.
@Stebalien Indeed. Don't know how I missed it. Thanks.