From 071fa08d47e3141655692a288f8977a4284e47f0 Mon Sep 17 00:00:00 2001 From: Wei Zhang Date: Sun, 6 Jan 2019 23:05:05 +0800 Subject: [PATCH] persist: demo code DO NOT MERGE! more demo codes for persist data usage Signed-off-by: Wei Zhang --- virtcontainers/api.go | 12 +++++ virtcontainers/container.go | 21 -------- virtcontainers/sandbox.go | 89 ++++++++++++++++++++++++++++--- virtcontainers/sandbox_test.go | 97 ---------------------------------- 4 files changed, 94 insertions(+), 125 deletions(-) diff --git a/virtcontainers/api.go b/virtcontainers/api.go index c5162488c9..404dd8dcc9 100644 --- a/virtcontainers/api.go +++ b/virtcontainers/api.go @@ -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 } @@ -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 } @@ -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 } diff --git a/virtcontainers/container.go b/virtcontainers/container.go index ca568804f3..a9eba63a5b 100644 --- a/virtcontainers/container.go +++ b/virtcontainers/container.go @@ -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 @@ -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 } @@ -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 } diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index d1f1554d59..75e5374f82 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -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 } @@ -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 @@ -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 } @@ -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 @@ -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 } @@ -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 @@ -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. @@ -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 @@ -1567,6 +1610,10 @@ func (s *Sandbox) createContainers() error { } } + if err := s.storeSandbox(); err != nil { + return err + } + return nil } @@ -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 @@ -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 @@ -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 @@ -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. diff --git a/virtcontainers/sandbox_test.go b/virtcontainers/sandbox_test.go index 08604efb79..5686227b1b 100644 --- a/virtcontainers/sandbox_test.go +++ b/virtcontainers/sandbox_test.go @@ -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