2018-09-18 17:08:46 +00:00
|
|
|
package singleton
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2019-01-22 22:42:49 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/loader"
|
2019-01-23 14:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
2018-09-18 17:08:46 +00:00
|
|
|
)
|
|
|
|
|
2018-09-28 17:09:01 +00:00
|
|
|
var (
|
|
|
|
// SingletonPluginExited is returned when the dispense is called and the
|
|
|
|
// existing plugin has exited. The caller should retry, and this will issue
|
|
|
|
// a new plugin instance.
|
|
|
|
SingletonPluginExited = fmt.Errorf("singleton plugin exited")
|
|
|
|
)
|
|
|
|
|
2018-09-18 17:08:46 +00:00
|
|
|
// SingletonLoader is used to only load a single external plugin at a time.
|
|
|
|
type SingletonLoader struct {
|
|
|
|
// Loader is the underlying plugin loader that we wrap to give a singleton
|
|
|
|
// behavior.
|
|
|
|
loader loader.PluginCatalog
|
|
|
|
|
|
|
|
// instances is a mapping of the plugin to a future which holds a plugin
|
|
|
|
// instance
|
|
|
|
instances map[loader.PluginID]*future
|
|
|
|
instanceLock sync.Mutex
|
|
|
|
|
|
|
|
// logger is the logger used by the singleton
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSingletonLoader wraps a plugin catalog and provides singleton behavior on
|
|
|
|
// top by caching running instances.
|
|
|
|
func NewSingletonLoader(logger log.Logger, catalog loader.PluginCatalog) *SingletonLoader {
|
|
|
|
return &SingletonLoader{
|
|
|
|
loader: catalog,
|
|
|
|
logger: logger.Named("singleton_plugin_loader"),
|
|
|
|
instances: make(map[loader.PluginID]*future, 4),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catalog returns the catalog of all plugins keyed by plugin type
|
|
|
|
func (s *SingletonLoader) Catalog() map[string][]*base.PluginInfoResponse {
|
|
|
|
return s.loader.Catalog()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispense returns the plugin given its name and type. This will also
|
|
|
|
// configure the plugin. If there is an instance of an already running plugin,
|
|
|
|
// this is used.
|
2018-12-18 00:40:58 +00:00
|
|
|
func (s *SingletonLoader) Dispense(name, pluginType string, config *base.AgentConfig, logger log.Logger) (loader.PluginInstance, error) {
|
2018-10-17 02:21:15 +00:00
|
|
|
return s.getPlugin(false, name, pluginType, logger, config, nil)
|
2018-09-18 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reattach is used to reattach to a previously launched external plugin.
|
|
|
|
func (s *SingletonLoader) Reattach(name, pluginType string, config *plugin.ReattachConfig) (loader.PluginInstance, error) {
|
2018-10-17 02:21:15 +00:00
|
|
|
return s.getPlugin(true, name, pluginType, nil, nil, config)
|
2018-09-18 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getPlugin is a helper that either dispenses or reattaches to a plugin using
|
|
|
|
// futures to ensure only a single instance is retrieved
|
2018-10-17 02:21:15 +00:00
|
|
|
func (s *SingletonLoader) getPlugin(reattach bool, name, pluginType string, logger log.Logger,
|
2018-12-18 00:40:58 +00:00
|
|
|
nomadConfig *base.AgentConfig, config *plugin.ReattachConfig) (loader.PluginInstance, error) {
|
2018-10-17 02:21:15 +00:00
|
|
|
|
2018-09-18 17:08:46 +00:00
|
|
|
// Lock the instance map to prevent races
|
|
|
|
s.instanceLock.Lock()
|
|
|
|
|
|
|
|
// Check if there is a future already
|
|
|
|
id := loader.PluginID{Name: name, PluginType: pluginType}
|
|
|
|
f, ok := s.instances[id]
|
|
|
|
|
|
|
|
// Create the future and go get a plugin
|
|
|
|
if !ok {
|
|
|
|
f = newFuture()
|
|
|
|
s.instances[id] = f
|
|
|
|
|
|
|
|
if reattach {
|
|
|
|
go s.reattach(f, name, pluginType, config)
|
|
|
|
} else {
|
2018-10-17 02:21:15 +00:00
|
|
|
go s.dispense(f, name, pluginType, nomadConfig, logger)
|
2018-09-18 17:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock so that the created future can be shared
|
|
|
|
s.instanceLock.Unlock()
|
|
|
|
|
|
|
|
i, err := f.wait().result()
|
|
|
|
if err != nil {
|
|
|
|
s.clearFuture(id, f)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.Exited() {
|
|
|
|
s.clearFuture(id, f)
|
2018-09-28 17:09:01 +00:00
|
|
|
return nil, SingletonPluginExited
|
2018-09-18 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispense should be called in a go routine to not block and creates the
|
|
|
|
// desired plugin, setting the results in the future.
|
2018-12-18 00:40:58 +00:00
|
|
|
func (s *SingletonLoader) dispense(f *future, name, pluginType string, config *base.AgentConfig, logger log.Logger) {
|
2018-10-17 02:21:15 +00:00
|
|
|
i, err := s.loader.Dispense(name, pluginType, config, logger)
|
2018-09-18 17:08:46 +00:00
|
|
|
f.set(i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// reattach should be called in a go routine to not block and reattaches to the
|
|
|
|
// desired plugin, setting the results in the future.
|
|
|
|
func (s *SingletonLoader) reattach(f *future, name, pluginType string, config *plugin.ReattachConfig) {
|
|
|
|
i, err := s.loader.Reattach(name, pluginType, config)
|
|
|
|
f.set(i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearFuture clears the future from the instances map only if the futures
|
|
|
|
// match. This prevents clearing the unintented instance.
|
|
|
|
func (s *SingletonLoader) clearFuture(id loader.PluginID, f *future) {
|
|
|
|
s.instanceLock.Lock()
|
|
|
|
defer s.instanceLock.Unlock()
|
|
|
|
if f.equal(s.instances[id]) {
|
|
|
|
delete(s.instances, id)
|
|
|
|
}
|
|
|
|
}
|