Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / swarmkit / manager / orchestrator / slot.go
1 package orchestrator
2
3 import (
4         "github.com/docker/swarmkit/api"
5         "github.com/docker/swarmkit/manager/state/store"
6 )
7
8 // Slot is a list of the running tasks occupying a certain slot. Generally this
9 // will only be one task, but some rolling update situations involve
10 // temporarily having two running tasks in the same slot. Note that this use of
11 // "slot" is more generic than the Slot number for replicated services - a node
12 // is also considered a slot for global services.
13 type Slot []*api.Task
14
15 // GetRunnableAndDeadSlots returns two maps of slots. The first contains slots
16 // that have at least one task with a desired state above NEW and lesser or
17 // equal to RUNNING. The second is for slots that only contain tasks with a
18 // desired state above RUNNING.
19 func GetRunnableAndDeadSlots(s *store.MemoryStore, serviceID string) (map[uint64]Slot, map[uint64]Slot, error) {
20         var (
21                 tasks []*api.Task
22                 err   error
23         )
24         s.View(func(tx store.ReadTx) {
25                 tasks, err = store.FindTasks(tx, store.ByServiceID(serviceID))
26         })
27         if err != nil {
28                 return nil, nil, err
29         }
30
31         runningSlots := make(map[uint64]Slot)
32         for _, t := range tasks {
33                 if t.DesiredState <= api.TaskStateRunning {
34                         runningSlots[t.Slot] = append(runningSlots[t.Slot], t)
35                 }
36         }
37
38         deadSlots := make(map[uint64]Slot)
39         for _, t := range tasks {
40                 if _, exists := runningSlots[t.Slot]; !exists {
41                         deadSlots[t.Slot] = append(deadSlots[t.Slot], t)
42                 }
43         }
44
45         return runningSlots, deadSlots, nil
46 }