From 8f880b4a921ceb673f0cc7dc14afc2ecd297f8c1 Mon Sep 17 00:00:00 2001 From: lowzj Date: Tue, 28 May 2019 22:22:15 +0800 Subject: [PATCH] feature: store mgr return localStorage as default store Signed-off-by: lowzj --- supernode/store/local_storage_test.go | 22 ++++++++++++++- supernode/store/store_mgr.go | 40 +++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/supernode/store/local_storage_test.go b/supernode/store/local_storage_test.go index 04e06ed4c..b657511b9 100644 --- a/supernode/store/local_storage_test.go +++ b/supernode/store/local_storage_test.go @@ -64,7 +64,7 @@ func (s *LocalStorageSuite) SetUpSuite(c *check.C) { plugins.Initialize(cfg) // init StorageManager - sm, err := NewManager() + sm, err := NewManager(nil) c.Assert(err, check.IsNil) // init store with local storage @@ -291,6 +291,26 @@ func (s *LocalStorageSuite) BenchmarkPutSerial(c *check.C) { } } +func (s *LocalStorageSuite) TestManager_Get(c *check.C) { + cfg := &config.Config{ + BaseProperties: &config.BaseProperties{ + HomeDir: path.Join(s.workHome, "test_mgr"), + }, + } + mgr, _ := NewManager(cfg) + + st, err := mgr.Get(LocalStorageDriver) + c.Check(err, check.IsNil) + c.Check(st, check.NotNil) + _, ok := st.driver.(*localStorage) + c.Check(ok, check.Equals, true) + + st, err = mgr.Get("testMgr") + c.Check(err, check.NotNil) + c.Check(st, check.IsNil) +} + +// ----------------------------------------------------------------------------- // helper function func (s *LocalStorageSuite) checkStat(raw *Raw, c *check.C) { diff --git a/supernode/store/store_mgr.go b/supernode/store/store_mgr.go index df1f8db80..03a7c303e 100644 --- a/supernode/store/store_mgr.go +++ b/supernode/store/store_mgr.go @@ -18,6 +18,8 @@ package store import ( "fmt" + "path" + "sync" "github.com/dragonflyoss/Dragonfly/supernode/config" "github.com/dragonflyoss/Dragonfly/supernode/plugins" @@ -38,17 +40,26 @@ func Register(name string, builder StorageBuilder) { // Manager manage stores. type Manager struct { + cfg *config.Config + + defaultStorage *Store + mutex sync.Mutex } // NewManager create a store manager. -func NewManager() (*Manager, error) { - return &Manager{}, nil +func NewManager(cfg *config.Config) (*Manager, error) { + return &Manager{ + cfg: cfg, + }, nil } // Get a store from manager with specified name. func (sm *Manager) Get(name string) (*Store, error) { v := plugins.GetPlugin(config.StoragePlugin, name) if v == nil { + if name == LocalStorageDriver { + return sm.getDefaultStorage() + } return nil, fmt.Errorf("not existed storage: %s", name) } if store, ok := v.(*Store); ok { @@ -56,3 +67,28 @@ func (sm *Manager) Get(name string) (*Store, error) { } return nil, fmt.Errorf("get store error: unknown reason") } + +func (sm *Manager) getDefaultStorage() (*Store, error) { + if sm.defaultStorage != nil { + return sm.defaultStorage, nil + } + + sm.mutex.Lock() + defer sm.mutex.Unlock() + + // check again to avoid initializing repeatedly + if sm.defaultStorage != nil { + return sm.defaultStorage, nil + } + + if sm.cfg == nil { + return nil, fmt.Errorf("cannot init local storage without home path") + } + cfg := fmt.Sprintf("baseDir: %s", path.Join(sm.cfg.HomeDir, "repo")) + s, err := NewStore(LocalStorageDriver, NewLocalStorage, cfg) + if err != nil { + return nil, err + } + sm.defaultStorage = s + return sm.defaultStorage, nil +}