slot.go 1.4 KB

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