Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 82f7b6c

Browse files
committed
XPod: add snapContainers as snapshot of containers
It handles the issue that XPod.updatePodInfo works without XPod.resourceLock but access XPod.containers. Signed-off-by: Hui Zhu <teawater@hyper.sh>
1 parent 78f3912 commit 82f7b6c

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

daemon/pod/container.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ func (c *Container) CreatedAt() time.Time {
135135
return ct
136136
}
137137

138-
func (c *Container) Info() *apitypes.Container {
139-
c.status.RLock()
140-
defer c.status.RUnlock()
138+
func (c *Container) InfoLocked() *apitypes.Container {
141139
cinfo := &apitypes.Container{
142140
Name: c.RuntimeName(),
143141
ContainerID: c.Id(),
@@ -188,9 +186,13 @@ func (c *Container) Info() *apitypes.Container {
188186
return cinfo
189187
}
190188

191-
func (c *Container) InfoStatus() *apitypes.ContainerStatus {
189+
func (c *Container) Info() *apitypes.Container {
192190
c.status.RLock()
193191
defer c.status.RUnlock()
192+
return c.InfoLocked()
193+
}
194+
195+
func (c *Container) InfoStatusLocked() *apitypes.ContainerStatus {
194196
s := &apitypes.ContainerStatus{
195197
Name: c.SpecName(),
196198
ContainerID: c.Id(),
@@ -226,6 +228,12 @@ func (c *Container) InfoStatus() *apitypes.ContainerStatus {
226228
return s
227229
}
228230

231+
func (c *Container) InfoStatus() *apitypes.ContainerStatus {
232+
c.status.RLock()
233+
defer c.status.RUnlock()
234+
return c.InfoStatusLocked()
235+
}
236+
229237
// Container life cycle operations:
230238
func (c *Container) Add() error {
231239
return nil

daemon/pod/decommission.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ func (p *XPod) RemoveContainer(id string) error {
276276
}
277277
p.factory.registry.ReleaseContainer(id, c.SpecName())
278278
delete(p.containers, id)
279+
p.statusLock.Lock()
280+
delete(p.snapContainers, id)
281+
p.statusLock.Unlock()
279282

280283
//remove volumes from daemondb
281284
for _, vName := range removedVols {

daemon/pod/persist.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ func (p *XPod) loadContainer(id string) error {
381381
return err
382382
}
383383
p.containers[c.Id()] = c
384+
p.statusLock.Lock()
385+
p.snapContainers[c.Id()] = c
386+
p.statusLock.Unlock()
384387
return nil
385388
}
386389

daemon/pod/pod.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ type XPod struct {
8080
initCond *sync.Cond
8181

8282
//Protected by statusLock
83-
snapVolumes map[string]*apitypes.PodVolume
83+
snapVolumes map[string]*apitypes.PodVolume
84+
snapContainers map[string]*Container
8485
}
8586

8687
// The Log infrastructure, to add pod name as prefix of the log message.
@@ -341,9 +342,9 @@ func (p *XPod) updatePodInfo() error {
341342
defer p.statusLock.Unlock()
342343

343344
var (
344-
containers = make([]*apitypes.Container, 0, len(p.containers))
345+
containers = make([]*apitypes.Container, 0, len(p.snapContainers))
345346
volumes = make([]*apitypes.PodVolume, 0, len(p.snapVolumes))
346-
containerStatus = make([]*apitypes.ContainerStatus, 0, len(p.containers))
347+
containerStatus = make([]*apitypes.ContainerStatus, 0, len(p.snapContainers))
347348
)
348349

349350
p.info.Spec.Labels = p.labels
@@ -354,9 +355,9 @@ func (p *XPod) updatePodInfo() error {
354355
p.info.Spec.Volumes = volumes
355356

356357
succeeeded := "Succeeded"
357-
for _, c := range p.containers {
358-
ci := c.Info()
359-
cs := c.InfoStatus()
358+
for _, c := range p.snapContainers {
359+
ci := c.InfoLocked()
360+
cs := c.InfoStatusLocked()
360361
containers = append(containers, ci)
361362
containerStatus = append(containerStatus, cs)
362363
if cs.Phase == "failed" {

daemon/pod/provision.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,22 @@ func newXPod(factory *PodFactory, spec *apitypes.UserPod) (*XPod, error) {
8787
factory.hosts = HostsCreator(spec.Id)
8888
factory.logCreator = initLogCreator(factory, spec)
8989
p := &XPod{
90-
name: spec.Id,
91-
logPrefix: fmt.Sprintf("Pod[%s] ", spec.Id),
92-
globalSpec: spec.CloneGlobalPart(),
93-
containers: make(map[string]*Container),
94-
volumes: make(map[string]*Volume),
95-
interfaces: make(map[string]*Interface),
96-
portMappings: spec.Portmappings,
97-
labels: spec.Labels,
98-
prestartExecs: [][]string{},
99-
execs: make(map[string]*Exec),
100-
resourceLock: &sync.Mutex{},
101-
statusLock: &sync.RWMutex{},
102-
stoppedChan: make(chan bool, 1),
103-
factory: factory,
104-
snapVolumes: make(map[string]*apitypes.PodVolume),
90+
name: spec.Id,
91+
logPrefix: fmt.Sprintf("Pod[%s] ", spec.Id),
92+
globalSpec: spec.CloneGlobalPart(),
93+
containers: make(map[string]*Container),
94+
volumes: make(map[string]*Volume),
95+
interfaces: make(map[string]*Interface),
96+
portMappings: spec.Portmappings,
97+
labels: spec.Labels,
98+
prestartExecs: [][]string{},
99+
execs: make(map[string]*Exec),
100+
resourceLock: &sync.Mutex{},
101+
statusLock: &sync.RWMutex{},
102+
stoppedChan: make(chan bool, 1),
103+
factory: factory,
104+
snapVolumes: make(map[string]*apitypes.PodVolume),
105+
snapContainers: make(map[string]*Container),
105106
}
106107
p.initCond = sync.NewCond(p.statusLock.RLocker())
107108
return p, nil
@@ -144,6 +145,9 @@ func (p *XPod) doContainerCreate(c *apitypes.UserContainer) (string, error) {
144145
}
145146

146147
p.containers[pc.Id()] = pc
148+
p.statusLock.Lock()
149+
p.snapContainers[pc.Id()] = pc
150+
p.statusLock.Unlock()
147151

148152
vols := pc.volumes()
149153
nvs := make([]string, 0, len(vols))
@@ -370,6 +374,9 @@ func (p *XPod) initResources(spec *apitypes.UserPod, allowCreate bool) error {
370374
return err
371375
}
372376
p.containers[c.Id()] = c
377+
p.statusLock.Lock()
378+
p.snapContainers[c.Id()] = c
379+
p.statusLock.Unlock()
373380

374381
vols := c.volumes()
375382
for _, vol := range vols {

0 commit comments

Comments
 (0)