storage: address comments

Address some comments:
* fix persist driver func names for better understanding
* modify some logic, add some returned error etc

Signed-off-by: Wei Zhang <zhangwei555@huawei.com>
This commit is contained in:
Wei Zhang 2019-02-09 03:09:49 +08:00
parent 6e4149d86c
commit 504c706bea
6 changed files with 36 additions and 43 deletions

View File

@ -384,7 +384,7 @@ func (c *Container) GetAnnotations() map[string]string {
// storeContainer stores a container config. // storeContainer stores a container config.
func (c *Container) storeContainer() error { func (c *Container) storeContainer() error {
if err := c.sandbox.newStore.Dump(); err != nil { if err := c.sandbox.newStore.ToDisk(); err != nil {
return err return err
} }
return c.store.Store(store.Configuration, *(c.config)) return c.store.Store(store.Configuration, *(c.config))
@ -442,7 +442,8 @@ func (c *Container) setContainerState(state types.StateString) error {
return err return err
} }
if err = c.sandbox.newStore.Dump(); err != nil { // flush data to storage
if err = c.sandbox.newStore.ToDisk(); err != nil {
return err return err
} }
return nil return nil

View File

@ -14,7 +14,6 @@ import (
) )
var ( var (
errSandboxPersistNotExist = errors.New("sandbox doesn't exist in persist data")
errContainerPersistNotExist = errors.New("container doesn't exist in persist data") errContainerPersistNotExist = errors.New("container doesn't exist in persist data")
) )
@ -93,50 +92,35 @@ func (s *Sandbox) dumpDevices(ss *persistapi.SandboxState, cs map[string]persist
return nil return nil
} }
// PersistVersion set persist data version to current version in runtime // versionCallback set persist data version to current version in runtime
func (s *Sandbox) persistVersion() { func (s *Sandbox) verSaveCallback() {
s.newStore.RegisterHook("version", func(ss *persistapi.SandboxState, cs map[string]persistapi.ContainerState) error { s.newStore.AddSaveCallback("version", func(ss *persistapi.SandboxState, cs map[string]persistapi.ContainerState) error {
ss.PersistVersion = persistapi.CurPersistVersion ss.PersistVersion = persistapi.CurPersistVersion
return nil return nil
}) })
} }
// PersistState register hook to set sandbox and container state to persist // PersistState register hook to set sandbox and container state to persist
func (s *Sandbox) persistState() { func (s *Sandbox) stateSaveCallback() {
s.newStore.RegisterHook("state", s.dumpState) s.newStore.AddSaveCallback("state", s.dumpState)
} }
// PersistHvState register hook to save hypervisor state to persist data // PersistHvState register hook to save hypervisor state to persist data
func (s *Sandbox) persistHvState() { func (s *Sandbox) hvStateSaveCallback() {
s.newStore.RegisterHook("hypervisor", s.dumpHypervisor) s.newStore.AddSaveCallback("hypervisor", s.dumpHypervisor)
} }
// PersistDevices register hook to save device informations // PersistDevices register hook to save device informations
func (s *Sandbox) persistDevices() { func (s *Sandbox) devicesSaveCallback() {
s.newStore.RegisterHook("devices", s.dumpDevices) s.newStore.AddSaveCallback("devices", s.dumpDevices)
} }
func (s *Sandbox) getSbxAndCntStates() (*persistapi.SandboxState, map[string]persistapi.ContainerState, error) { func (s *Sandbox) getSbxAndCntStates() (*persistapi.SandboxState, map[string]persistapi.ContainerState, error) {
ss, cs, err := s.newStore.GetStates() if err := s.newStore.Restore(s.id); err != nil {
if err != nil {
return nil, nil, err return nil, nil, err
} }
if len(cs) == 0 { return s.newStore.GetStates()
if err := s.newStore.Restore(s.id); err != nil {
return nil, nil, err
}
ss, cs, err = s.newStore.GetStates()
if err != nil {
return nil, nil, err
}
if len(cs) == 0 {
return nil, nil, errSandboxPersistNotExist
}
}
return ss, cs, nil
} }
// Restore will restore sandbox data from persist file on disk // Restore will restore sandbox data from persist file on disk

View File

@ -7,10 +7,17 @@ package persistapi
// PersistDriver is interface describing operations to save/restore persist data // PersistDriver is interface describing operations to save/restore persist data
type PersistDriver interface { type PersistDriver interface {
// Dump persist data to // ToDisk flushes data to disk(or other storage media such as a remote db)
Dump() error ToDisk() error
// AddSaveCallback addes callback function named `name` to driver storage list
// The callback functions will be invoked when calling `ToDisk()`, notice that
// callback functions are not order guaranteed,
AddSaveCallback(name string, f SetFunc)
// Restore will restore all data for sandbox with `sid` from storage.
// We only support get data for one whole sandbox
Restore(sid string) error Restore(sid string) error
// Destroy will remove everything from storage
Destroy() error Destroy() error
// GetStates will return SandboxState and ContainerState(s) directly
GetStates() (*SandboxState, map[string]ContainerState, error) GetStates() (*SandboxState, map[string]ContainerState, error)
RegisterHook(name string, f SetFunc)
} }

View File

@ -70,8 +70,8 @@ func (fs *FS) sandboxDir() (string, error) {
return filepath.Join(runStoragePath, fs.sandboxState.SandboxContainer), nil return filepath.Join(runStoragePath, fs.sandboxState.SandboxContainer), nil
} }
// Dump sandboxState and containerState to disk // ToDisk sandboxState and containerState to disk
func (fs *FS) Dump() (retErr error) { func (fs *FS) ToDisk() (retErr error) {
// call registered hooks to set sandboxState and containerState // call registered hooks to set sandboxState and containerState
for _, fun := range fs.setFuncs { for _, fun := range fs.setFuncs {
fun(fs.sandboxState, fs.containerState) fun(fs.sandboxState, fs.containerState)
@ -214,8 +214,8 @@ func (fs *FS) GetStates() (*persistapi.SandboxState, map[string]persistapi.Conta
return fs.sandboxState, fs.containerState, nil return fs.sandboxState, fs.containerState, nil
} }
// RegisterHook registers processing hooks for Dump // AddSaveCallback registers processing hooks for Dump
func (fs *FS) RegisterHook(name string, f persistapi.SetFunc) { func (fs *FS) AddSaveCallback(name string, f persistapi.SetFunc) {
// only accept last registered hook with same name // only accept last registered hook with same name
fs.setFuncs[name] = f fs.setFuncs[name] = f
} }

View File

@ -16,9 +16,9 @@ type initFunc (func() (persistapi.PersistDriver, error))
var ( var (
supportedDrivers = map[string]initFunc{ supportedDrivers = map[string]initFunc{
"fs": fs.Init, "fs": fs.Init,
} }
defaultDriver = "fs"
) )
// GetDriver returns new PersistDriver according to driver name // GetDriver returns new PersistDriver according to driver name

View File

@ -477,10 +477,10 @@ func createSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Fac
} }
s.devManager = deviceManager.NewDeviceManager(sandboxConfig.HypervisorConfig.BlockDeviceDriver, devices) s.devManager = deviceManager.NewDeviceManager(sandboxConfig.HypervisorConfig.BlockDeviceDriver, devices)
// register persist hook for now, data will be written to disk by Dump() // register persist hook for now, data will be written to disk by ToDisk()
s.persistState() s.stateSaveCallback()
s.persistHvState() s.hvStateSaveCallback()
s.persistDevices() s.devicesSaveCallback()
if err := s.Restore(); err == nil && s.state.State != "" { if err := s.Restore(); err == nil && s.state.State != "" {
return s, nil return s, nil
@ -498,7 +498,7 @@ func createSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Fac
// if sandbox doesn't exist, set persist version to current version // if sandbox doesn't exist, set persist version to current version
// otherwise do nothing // otherwise do nothing
s.persistVersion() s.verSaveCallback()
// Below code path is called only during create, because of earlier check. // Below code path is called only during create, because of earlier check.
if err := s.agent.createSandbox(s); err != nil { if err := s.agent.createSandbox(s); err != nil {
@ -608,7 +608,8 @@ func (s *Sandbox) storeSandbox() error {
} }
} }
if err = s.newStore.Dump(); err != nil { // flush data to storage
if err = s.newStore.ToDisk(); err != nil {
return err return err
} }