Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
feature: store mgr return localStorage as default store
Browse files Browse the repository at this point in the history
Signed-off-by: lowzj <[email protected]>
  • Loading branch information
lowzj committed May 29, 2019
1 parent 1dce2d8 commit 8f880b4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
22 changes: 21 additions & 1 deletion supernode/store/local_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
40 changes: 38 additions & 2 deletions supernode/store/store_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package store

import (
"fmt"
"path"
"sync"

"github.com/dragonflyoss/Dragonfly/supernode/config"
"github.com/dragonflyoss/Dragonfly/supernode/plugins"
Expand All @@ -38,21 +40,55 @@ 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 {
return store, nil
}
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
}

0 comments on commit 8f880b4

Please sign in to comment.