Skip to content

Commit

Permalink
sandbox: add stateful sandbox config
Browse files Browse the repository at this point in the history
When enabled, do not release in memory sandbox resources in VC APIs,
and callers are expected to call sandbox.Release() to release the in
memory resources.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Jul 23, 2018
1 parent 7a6f205 commit d69fbcf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
34 changes: 17 additions & 17 deletions virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func SetLogger(logger logrus.FieldLogger) {
func CreateSandbox(sandboxConfig SandboxConfig, factory Factory) (VCSandbox, error) {
s, err := createSandboxFromConfig(sandboxConfig, factory)
if err == nil {
s.Release()
s.releaseStatelessSandbox()
}

return s, err
Expand Down Expand Up @@ -91,7 +91,7 @@ func DeleteSandbox(sandboxID string) (VCSandbox, error) {
if err != nil {
return nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

// Delete it.
if err := s.Delete(); err != nil {
Expand Down Expand Up @@ -155,7 +155,7 @@ func StartSandbox(sandboxID string) (VCSandbox, error) {
if err != nil {
return nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

return startSandbox(s)
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func StopSandbox(sandboxID string) (VCSandbox, error) {
if err != nil {
return nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

// Stop it.
err = s.stop()
Expand Down Expand Up @@ -221,7 +221,7 @@ func RunSandbox(sandboxConfig SandboxConfig, factory Factory) (VCSandbox, error)
if err != nil {
return nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

lockFile, err := rwLockSandbox(s.id)
if err != nil {
Expand Down Expand Up @@ -280,7 +280,7 @@ func StatusSandbox(sandboxID string) (SandboxStatus, error) {
unlockSandbox(lockFile)
return SandboxStatus{}, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

// We need to potentially wait for a separate container.stop() routine
// that needs to be terminated before we return from this function.
Expand Down Expand Up @@ -332,7 +332,7 @@ func CreateContainer(sandboxID string, containerConfig ContainerConfig) (VCSandb
if err != nil {
return nil, nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

c, err := s.CreateContainer(containerConfig)
if err != nil {
Expand Down Expand Up @@ -364,7 +364,7 @@ func DeleteContainer(sandboxID, containerID string) (VCContainer, error) {
if err != nil {
return nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

return s.DeleteContainer(containerID)
}
Expand All @@ -390,7 +390,7 @@ func StartContainer(sandboxID, containerID string) (VCContainer, error) {
if err != nil {
return nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

c, err := s.StartContainer(containerID)
if err != nil {
Expand Down Expand Up @@ -421,7 +421,7 @@ func StopContainer(sandboxID, containerID string) (VCContainer, error) {
if err != nil {
return nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

// Fetch the container.
c, err := s.findContainer(containerID)
Expand Down Expand Up @@ -459,7 +459,7 @@ func EnterContainer(sandboxID, containerID string, cmd Cmd) (VCSandbox, VCContai
if err != nil {
return nil, nil, nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

c, process, err := s.EnterContainer(containerID, cmd)
if err != nil {
Expand Down Expand Up @@ -490,7 +490,7 @@ func StatusContainer(sandboxID, containerID string) (ContainerStatus, error) {
unlockSandbox(lockFile)
return ContainerStatus{}, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

// We need to potentially wait for a separate container.stop() routine
// that needs to be terminated before we return from this function.
Expand Down Expand Up @@ -577,7 +577,7 @@ func KillContainer(sandboxID, containerID string, signal syscall.Signal, all boo
if err != nil {
return err
}
defer s.Release()
defer s.releaseStatelessSandbox()

// Fetch the container.
c, err := s.findContainer(containerID)
Expand Down Expand Up @@ -627,7 +627,7 @@ func ProcessListContainer(sandboxID, containerID string, options ProcessListOpti
if err != nil {
return nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

// Fetch the container.
c, err := s.findContainer(containerID)
Expand Down Expand Up @@ -659,7 +659,7 @@ func UpdateContainer(sandboxID, containerID string, resources specs.LinuxResourc
if err != nil {
return err
}
defer s.Release()
defer s.releaseStatelessSandbox()

return s.UpdateContainer(containerID, resources)
}
Expand All @@ -685,7 +685,7 @@ func StatsContainer(sandboxID, containerID string) (ContainerStats, error) {
if err != nil {
return ContainerStats{}, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

return s.StatsContainer(containerID)
}
Expand All @@ -709,7 +709,7 @@ func togglePauseContainer(sandboxID, containerID string, pause bool) error {
if err != nil {
return err
}
defer s.Release()
defer s.releaseStatelessSandbox()

// Fetch the container.
c, err := s.findContainer(containerID)
Expand Down
22 changes: 19 additions & 3 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ func TestStatusContainerStateReady(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer p2.Release()
defer p2.releaseStatelessSandbox()

expectedStatus := ContainerStatus{
ID: contID,
Expand Down Expand Up @@ -1885,7 +1885,7 @@ func TestStatusContainerStateRunning(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer p2.Release()
defer p2.releaseStatelessSandbox()

expectedStatus := ContainerStatus{
ID: contID,
Expand Down Expand Up @@ -2335,7 +2335,23 @@ func TestFetchSandbox(t *testing.T) {

fetched, err := FetchSandbox(s.ID())
assert.Nil(t, err, "%v", err)
assert.True(t, fetched == s, "fetched sandboxed do not match")
assert.True(t, fetched != s, "fetched stateless sandboxes should not match")
}

func TestFetchStatefulSandbox(t *testing.T) {
cleanUp()

config := newTestSandboxConfigNoop()

config.Stateful = true
s, err := CreateSandbox(config, nil)
if s == nil || err != nil {
t.Fatal(err)
}

fetched, err := FetchSandbox(s.ID())
assert.Nil(t, err, "%v", err)
assert.Equal(t, fetched, s, "fetched stateful sandboxed should match")
}

func TestFetchNonExistingSandbox(t *testing.T) {
Expand Down
16 changes: 15 additions & 1 deletion virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ type SandboxConfig struct {

// SharePidNs sets all containers to share the same sandbox level pid namespace.
SharePidNs bool

// Stateful keeps sandbox resources in memory across APIs. Users will be responsible
// for calling Release() to release the memory resources.
Stateful bool
}

func (s *Sandbox) startProxy() error {
Expand Down Expand Up @@ -468,6 +472,7 @@ type Sandbox struct {

shmSize uint64
sharePidNs bool
stateful bool
}

// ID returns the sandbox identifier string.
Expand Down Expand Up @@ -550,6 +555,14 @@ func (s *Sandbox) Release() error {
return s.agent.disconnect()
}

func (s *Sandbox) releaseStatelessSandbox() error {
if s.stateful {
return nil
}

return s.Release()
}

// Status gets the status of the sandbox
// TODO: update container status properly, see kata-containers/runtime#253
func (s *Sandbox) Status() SandboxStatus {
Expand Down Expand Up @@ -752,6 +765,7 @@ func newSandbox(sandboxConfig SandboxConfig, factory Factory) (*Sandbox, error)
wg: &sync.WaitGroup{},
shmSize: sandboxConfig.ShmSize,
sharePidNs: sandboxConfig.SharePidNs,
stateful: sandboxConfig.Stateful,
}

if err = globalSandboxList.addSandbox(s); err != nil {
Expand Down Expand Up @@ -1391,7 +1405,7 @@ func togglePauseSandbox(sandboxID string, pause bool) (*Sandbox, error) {
if err != nil {
return nil, err
}
defer s.Release()
defer s.releaseStatelessSandbox()

if pause {
err = s.Pause()
Expand Down

0 comments on commit d69fbcf

Please sign in to comment.