Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
persist: demo code
Browse files Browse the repository at this point in the history
DO NOT MERGE!

more demo codes for persist data usage

Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
WeiZhang555 committed Jan 6, 2019
1 parent 99895c7 commit 071fa08
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 125 deletions.
12 changes: 12 additions & 0 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ func startSandbox(s *Sandbox) (*Sandbox, error) {
return nil, err
}

if err = s.storeSandbox(); err != nil {
return nil, err
}

return s, nil
}

Expand Down Expand Up @@ -276,6 +280,10 @@ func StopSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
return nil, err
}

if err = s.storeSandbox(); err != nil {
return nil, err
}

return s, nil
}

Expand Down Expand Up @@ -407,6 +415,10 @@ func CreateContainer(ctx context.Context, sandboxID string, containerConfig Cont
return nil, nil, err
}

if err = s.storeSandbox(); err != nil {
return nil, nil, err
}

return s, c, nil
}

Expand Down
21 changes: 0 additions & 21 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,21 +339,6 @@ func (c *Container) SetPid(pid int) error {
return c.storeProcess()
}

func (c *Container) setStateBlockIndex(index int) error {
c.state.BlockIndex = index

err := c.sandbox.storage.storeContainerResource(c.sandbox.id, c.id, stateFileType, c.state)
if err != nil {
return err
}

c.sandbox.PersistState()
if err = c.sandbox.store.Dump(); err != nil {
return err
}
return nil
}

func (c *Container) setStateFstype(fstype string) error {
c.state.Fstype = fstype

Expand All @@ -363,9 +348,6 @@ func (c *Container) setStateFstype(fstype string) error {
}

c.sandbox.PersistState()
if err = c.sandbox.store.Dump(); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -426,9 +408,6 @@ func (c *Container) setContainerState(state stateString) error {
}

c.sandbox.PersistState()
if err = c.sandbox.store.Dump(); err != nil {
return err
}
return nil
}

Expand Down
89 changes: 82 additions & 7 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,11 @@ func (s *Sandbox) CreateContainer(contConfig ContainerConfig) (VCContainer, erro
if err := s.setupCgroups(); err != nil {
return nil, err
}

if err = s.storeSandbox(); err != nil {
return nil, err
}

return c, nil
}

Expand All @@ -1365,6 +1370,11 @@ func (s *Sandbox) StartContainer(containerID string) (VCContainer, error) {
if err != nil {
return nil, err
}

if err = s.storeSandbox(); err != nil {
return nil, err
}

//Fixme Container delete from sandbox, need to update resources

return c, nil
Expand All @@ -1383,6 +1393,9 @@ func (s *Sandbox) StopContainer(containerID string) (VCContainer, error) {
return nil, err
}

if err = s.storeSandbox(); err != nil {
return nil, err
}
return c, nil
}

Expand All @@ -1395,7 +1408,14 @@ func (s *Sandbox) KillContainer(containerID string, signal syscall.Signal, all b
}

// Send a signal to the process.
return c.kill(signal, all)
if err := c.kill(signal, all); err != nil {
return err
}

if err = s.storeSandbox(); err != nil {
return err
}
return nil
}

// DeleteContainer deletes a container from the sandbox
Expand Down Expand Up @@ -1430,6 +1450,9 @@ func (s *Sandbox) DeleteContainer(containerID string) (VCContainer, error) {
return nil, err
}

if err = s.storeSandbox(); err != nil {
return nil, err
}
return c, nil
}

Expand Down Expand Up @@ -1500,7 +1523,13 @@ func (s *Sandbox) UpdateContainer(containerID string, resources specs.LinuxResou
return err
}

return c.storeContainer()
if err := c.storeContainer(); err != nil {
return err
}
if err = s.storeSandbox(); err != nil {
return err
}
return nil
}

// StatsContainer return the stats of a running container
Expand All @@ -1527,7 +1556,14 @@ func (s *Sandbox) PauseContainer(containerID string) error {
}

// Pause the container.
return c.pause()
if err := c.pause(); err != nil {
return err
}

if err = s.storeSandbox(); err != nil {
return err
}
return nil
}

// ResumeContainer resumes a paused container.
Expand All @@ -1539,7 +1575,14 @@ func (s *Sandbox) ResumeContainer(containerID string) error {
}

// Resume the container.
return c.resume()
if err := c.resume(); err != nil {
return err
}

if err = s.storeSandbox(); err != nil {
return err
}
return nil
}

// createContainers registers all containers to the proxy, create the
Expand Down Expand Up @@ -1567,6 +1610,10 @@ func (s *Sandbox) createContainers() error {
}
}

if err := s.storeSandbox(); err != nil {
return err
}

return nil
}

Expand All @@ -1587,6 +1634,10 @@ func (s *Sandbox) Start() error {
}
}

if err := s.storeSandbox(); err != nil {
return err
}

s.Logger().Info("Sandbox is started")

return nil
Expand Down Expand Up @@ -1622,7 +1673,15 @@ func (s *Sandbox) Stop() error {
}

// Remove the network.
return s.removeNetwork()
if err := s.removeNetwork(); err != nil {
return err
}

if err := s.storeSandbox(); err != nil {
return err
}

return nil
}

// Pause pauses the sandbox
Expand All @@ -1639,7 +1698,15 @@ func (s *Sandbox) Pause() error {
s.monitor.stop()
}

return s.pauseSetStates()
if err := s.pauseSetStates(); err != nil {
return err
}

if err := s.storeSandbox(); err != nil {
return err
}

return nil
}

// Resume resumes the sandbox
Expand All @@ -1648,7 +1715,15 @@ func (s *Sandbox) Resume() error {
return err
}

return s.resumeSetStates()
if err := s.resumeSetStates(); err != nil {
return err
}

if err := s.storeSandbox(); err != nil {
return err
}

return nil
}

// list lists all sandbox running on the host.
Expand Down
97 changes: 0 additions & 97 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,103 +933,6 @@ func TestSandboxGetContainer(t *testing.T) {
}
}

func TestContainerSetStateBlockIndex(t *testing.T) {
containers := []ContainerConfig{
{
ID: "100",
},
}

hConfig := newHypervisorConfig(nil, nil)
sandbox, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NoopNetworkModel, NetworkConfig{}, containers, nil)
if err != nil {
t.Fatal(err)
}
defer cleanUp()

fs := &filesystem{}
sandbox.storage = fs

c := sandbox.GetContainer("100")
if c == nil {
t.Fatal()
}

path := filepath.Join(runStoragePath, testSandboxID, c.ID())
err = os.MkdirAll(path, dirMode)
if err != nil {
t.Fatal(err)
}

stateFilePath := filepath.Join(path, stateFile)

os.Remove(stateFilePath)

f, err := os.Create(stateFilePath)
if err != nil {
t.Fatal(err)
}

state := State{
State: "stopped",
Fstype: "vfs",
}

cImpl, ok := c.(*Container)
assert.True(t, ok)

cImpl.state = state

stateData := `{
"state":"stopped",
"fstype":"vfs"
}`

n, err := f.WriteString(stateData)
if err != nil || n != len(stateData) {
f.Close()
t.Fatal()
}
f.Close()

_, err = os.Stat(stateFilePath)
if err != nil {
t.Fatal(err)
}

newIndex := 20
if err := cImpl.setStateBlockIndex(newIndex); err != nil {
t.Fatal(err)
}

if cImpl.state.BlockIndex != newIndex {
t.Fatal()
}

fileData, err := ioutil.ReadFile(stateFilePath)
if err != nil {
t.Fatal()
}

var res State
err = json.Unmarshal([]byte(string(fileData)), &res)
if err != nil {
t.Fatal(err)
}

if res.BlockIndex != newIndex {
t.Fatal()
}

if res.Fstype != state.Fstype {
t.Fatal()
}

if res.State != state.State {
t.Fatal()
}
}

func TestContainerStateSetFstype(t *testing.T) {
var err error

Expand Down

0 comments on commit 071fa08

Please sign in to comment.