Skip to content

Commit

Permalink
lock instances, Timer differently, snake_case log
Browse files Browse the repository at this point in the history
and remove debris
  • Loading branch information
gulducat committed Apr 6, 2023
1 parent b772ad5 commit 1813c9b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
34 changes: 26 additions & 8 deletions client/dynamicplugins/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"fmt"
"sync"
"time"

"github.com/hashicorp/nomad/helper"
)

const (
Expand Down Expand Up @@ -308,6 +310,23 @@ func (d *dynamicRegistry) ListPlugins(ptype string) []*PluginInfo {
// Callers should pass in a context with a sensible timeout
// for the plugin they're expecting to find.
func (d *dynamicRegistry) WaitForPlugin(ctx context.Context, ptype, name string) (*PluginInfo, error) {
// this is our actual goal, which may be run repeatedly
findPlugin := func() *PluginInfo {
for _, p := range d.ListPlugins(ptype) {
if p.Name == name {
return p
}
}
return nil
}

// try immediately first, before any timers get involved
if p := findPlugin(); p != nil {
return p, nil
}

// next, loop until found or context is done

// these numbers are almost arbitrary...
delay := 200 // milliseconds between checks, will backoff
maxDelay := 5000 // up to 5 seconds between each check
Expand All @@ -317,22 +336,21 @@ func (d *dynamicRegistry) WaitForPlugin(ctx context.Context, ptype, name string)
ctx, cancel := context.WithTimeout(ctx, 24*time.Hour)
defer cancel()

timer := time.NewTimer(time.Duration(delay) * time.Millisecond)
timer, stop := helper.NewSafeTimer(time.Duration(delay) * time.Millisecond)
defer stop()
for {
for _, p := range d.ListPlugins(ptype) {
if p.Name == name {
return p, nil
}
}

//log.Printf("WaitForPlugin delay: %d", delay) // TODO: get a logger in here?
select {
case <-ctx.Done():
// an externally-defined timeout wins the day
return nil, ctx.Err()
case <-timer.C:
// continue after our internal delay
}

if p := findPlugin(); p != nil {
return p, nil
}

if delay < maxDelay {
delay += delay
}
Expand Down
4 changes: 3 additions & 1 deletion client/pluginmanager/csimanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func New(config *Config) Manager {
}

return &csiManager{
logger: config.Logger.Named("csiManager"),
logger: config.Logger.Named("csi_manager"),
eventer: config.TriggerNodeEvent,
registry: config.DynamicRegistry,
instances: make(map[string]map[string]*instanceManager),
Expand Down Expand Up @@ -84,6 +84,8 @@ func (c *csiManager) WaitForPlugin(ctx context.Context, pType, pID string) error
if err != nil {
return fmt.Errorf("%s plugin '%s' did not become ready: %w", pType, pID, err)
}
c.instancesLock.Lock()
defer c.instancesLock.Unlock()
c.ensureInstance(p)
return nil
}
Expand Down

0 comments on commit 1813c9b

Please sign in to comment.