Skip to content

Commit

Permalink
refac: plugin cach
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Jan 1, 2023
1 parent c1e9351 commit b8fea7d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 73 deletions.
79 changes: 17 additions & 62 deletions ignite/services/plugin/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"encoding/gob"
"fmt"
"net"
"path/filepath"
"path"

hplugin "github.com/hashicorp/go-plugin"

chainconfig "github.com/ignite/cli/ignite/config"
"github.com/ignite/cli/ignite/pkg/cache"
)

Expand All @@ -17,86 +16,48 @@ const (
cacheNamespace = "plugin.rpc.context"
)

var (
storage *cache.Storage
storageCache *cache.Cache[ConfigContext]
)
var storageCache *cache.Cache[hplugin.ReattachConfig]

func init() {
gob.Register(hplugin.ReattachConfig{})
gob.Register(net.UnixAddr{})
}

type ConfigContext struct {
Plugin hplugin.ReattachConfig
Addr net.UnixAddr
gob.Register(&net.UnixAddr{})
}

func WritePluginConfigCache(pluginPath string, conf hplugin.ReattachConfig) error {
if pluginPath == "" {
return fmt.Errorf("provided path is invalid: %s", pluginPath)
}

if conf.Addr == nil {
return fmt.Errorf("plugin Address info cannot be empty")
}

confCont := ConfigContext{}
// TODO: figure out a better way of resolving the type of network connection is established between plugin server and host
// currently this will always be a unix network socket. but this might not be the case moving forward.
ua, err := net.ResolveUnixAddr(conf.Addr.Network(), conf.Addr.String())
if err != nil {
return err
}

confCont.Addr = *ua
conf.Addr = nil
confCont.Plugin = conf

cache, err := newCache()
if err != nil {
return err
}

cache.Put(pluginPath, confCont)

return err
return cache.Put(pluginPath, conf)
}

func ReadPluginConfigCache(pluginPath string, ref *hplugin.ReattachConfig) error {
func ReadPluginConfigCache(pluginPath string) (hplugin.ReattachConfig, error) {
if pluginPath == "" {
return fmt.Errorf("provided path is invalid: %s", pluginPath)
return hplugin.ReattachConfig{}, fmt.Errorf("provided path is invalid: %s", pluginPath)
}

cache, err := newCache()
if err != nil {
return err
return hplugin.ReattachConfig{}, err
}

confCont, err := cache.Get(pluginPath)
if err != nil {
return err
}

*ref = confCont.Plugin
ref.Addr = &confCont.Addr

return nil
return cache.Get(pluginPath)
}

func CheckPluginConfCache(pluginPath string) bool {
if pluginPath == "" {
return false
}

cache, err := newCache()
if err != nil {
return false
}
if _, err := cache.Get(pluginPath); err != nil {
return false
}
return true
_, err = cache.Get(pluginPath)
return err == nil
}

func DeletePluginConfCache(pluginPath string) error {
Expand All @@ -107,27 +68,21 @@ func DeletePluginConfCache(pluginPath string) error {
if err != nil {
return err
}

if err := cache.Delete(pluginPath); err != nil {
return err
}

return nil
return cache.Delete(pluginPath)
}

func newCache() (*cache.Cache[ConfigContext], error) {
cacheRootDir, err := chainconfig.DirPath()
func newCache() (*cache.Cache[hplugin.ReattachConfig], error) {
cacheRootDir, err := PluginsPath()
if err != nil {
return nil, err
}
if storage == nil {
storageTmp, err := cache.NewStorage(filepath.Join(cacheRootDir, cacheFileName))
if storageCache == nil {
storage, err := cache.NewStorage(path.Join(cacheRootDir, cacheFileName))
if err != nil {
return nil, err
}
storage = &storageTmp
cacheTmp := cache.New[ConfigContext](*storage, cacheNamespace)
storageCache = &cacheTmp
c := cache.New[hplugin.ReattachConfig](storage, cacheNamespace)
storageCache = &c
}

return storageCache, nil
Expand Down
6 changes: 2 additions & 4 deletions ignite/services/plugin/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func TestReadWritePluginConfigCache(t *testing.T) {
err := WritePluginConfigCache(path, rc)
require.NoError(t, err)

c := hplugin.ReattachConfig{}
err = ReadPluginConfigCache(path, &c)
c, err := ReadPluginConfigCache(path)
require.NoError(t, err)
require.Equal(t, rc, c)
})
Expand Down Expand Up @@ -74,9 +73,8 @@ func TestPluginCacheDelete(t *testing.T) {
err = DeletePluginConfCache(path)
require.NoError(t, err)

c := hplugin.ReattachConfig{}
// there should be an error after deleting the config from the cache
err = ReadPluginConfigCache(path, &c)
_, err = ReadPluginConfigCache(path)
require.Error(t, err)
})

Expand Down
3 changes: 1 addition & 2 deletions ignite/services/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ func (p *Plugin) load(ctx context.Context) {
})

if CheckPluginConfCache(p.Path) {
rconf := hplugin.ReattachConfig{}
err := ReadPluginConfigCache(p.Path, &rconf)
rconf, err := ReadPluginConfigCache(p.Path)
if err != nil {
p.Error = err
return
Expand Down
12 changes: 7 additions & 5 deletions ignite/services/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,21 +397,23 @@ func TestPluginLoadSharedHost(t *testing.T) {
assert.False(CheckPluginConfCache(plugins[0].Path), "once host is killed the cache should be cleared")
}()

var hostConf *hplugin.ReattachConfig
for i := 0; i < len(plugins); i++ {
if tt.sharesHost {
assert.True(CheckPluginConfCache(plugins[i].Path), "sharedHost must have a cache entry")
if i == 0 {
// first plugin is the host
assert.True(plugins[i].isHost, "first plugin is the host")
// Assert reattach config has been saved for non host
rconf := plugins[i].client.ReattachConfig()
var ref hplugin.ReattachConfig
if assert.NoError(ReadPluginConfigCache(plugins[i].Path, &ref)) {
assert.Equal(rconf, &ref, "wrong cache entry for plugin host")
// Assert reattach config has been saved
hostConf = plugins[i].client.ReattachConfig()
ref, err := ReadPluginConfigCache(plugins[i].Path)
if assert.NoError(err) {
assert.Equal(hostConf, &ref, "wrong cache entry for plugin host")
}
} else {
// plugins after first aren't host
assert.False(plugins[i].isHost, "plugin %d can't be host", i)
assert.Equal(hostConf, plugins[i].client.ReattachConfig(), "ReattachConfig different from host plugin")
}
} else {
assert.False(plugins[i].isHost, "plugin %d can't be host if sharedHost is disabled", i)
Expand Down

0 comments on commit b8fea7d

Please sign in to comment.