2015-12-18 00:07:04 +00:00
|
|
|
package supervisor
|
2015-11-05 23:29:53 +00:00
|
|
|
|
|
|
|
import (
|
2016-01-06 21:32:46 +00:00
|
|
|
"io/ioutil"
|
2015-11-05 23:29:53 +00:00
|
|
|
"os"
|
2015-12-07 23:19:56 +00:00
|
|
|
"path/filepath"
|
2015-12-04 21:31:17 +00:00
|
|
|
"sync"
|
2015-12-14 22:43:00 +00:00
|
|
|
"time"
|
2015-11-05 23:29:53 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-12-18 20:17:53 +00:00
|
|
|
"github.com/docker/containerd/chanotify"
|
2015-12-16 21:53:11 +00:00
|
|
|
"github.com/docker/containerd/eventloop"
|
2015-12-01 19:56:08 +00:00
|
|
|
"github.com/docker/containerd/runtime"
|
2015-11-05 23:29:53 +00:00
|
|
|
)
|
|
|
|
|
2015-12-18 00:07:04 +00:00
|
|
|
const (
|
|
|
|
statsInterval = 1 * time.Second
|
|
|
|
defaultBufferSize = 2048 // size of queue in eventloop
|
|
|
|
)
|
2015-12-14 22:43:00 +00:00
|
|
|
|
2015-12-18 00:07:04 +00:00
|
|
|
// New returns an initialized Process supervisor.
|
2016-01-06 21:32:46 +00:00
|
|
|
func New(stateDir string, tasks chan *StartTask, oom bool) (*Supervisor, error) {
|
2015-11-05 23:29:53 +00:00
|
|
|
if err := os.MkdirAll(stateDir, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
machine, err := CollectMachineInformation()
|
2015-11-05 23:49:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-05 23:29:53 +00:00
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
monitor, err := NewMonitor()
|
2015-12-03 19:49:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-05 23:49:13 +00:00
|
|
|
s := &Supervisor{
|
2015-12-14 22:43:00 +00:00
|
|
|
stateDir: stateDir,
|
|
|
|
containers: make(map[string]*containerInfo),
|
|
|
|
tasks: tasks,
|
|
|
|
machine: machine,
|
|
|
|
subscribers: make(map[chan *Event]struct{}),
|
|
|
|
statsCollector: newStatsCollector(statsInterval),
|
2015-12-18 00:07:04 +00:00
|
|
|
el: eventloop.NewChanLoop(defaultBufferSize),
|
2016-01-06 21:32:46 +00:00
|
|
|
monitor: monitor,
|
2015-11-07 00:44:52 +00:00
|
|
|
}
|
2015-12-16 00:22:53 +00:00
|
|
|
if oom {
|
2015-12-18 20:17:53 +00:00
|
|
|
s.notifier = chanotify.New()
|
|
|
|
go func() {
|
|
|
|
for id := range s.notifier.Chan() {
|
|
|
|
e := NewEvent(OOMEventType)
|
2016-01-22 16:15:13 +00:00
|
|
|
e.ID = id.(string)
|
2015-12-18 20:17:53 +00:00
|
|
|
s.SendEvent(e)
|
|
|
|
}
|
|
|
|
}()
|
2015-12-16 00:22:53 +00:00
|
|
|
}
|
2015-12-01 18:55:13 +00:00
|
|
|
// register default event handlers
|
|
|
|
s.handlers = map[EventType]Handler{
|
2015-12-04 00:07:53 +00:00
|
|
|
ExecExitEventType: &ExecExitEvent{s},
|
|
|
|
ExitEventType: &ExitEvent{s},
|
|
|
|
StartContainerEventType: &StartEvent{s},
|
|
|
|
DeleteEventType: &DeleteEvent{s},
|
|
|
|
GetContainerEventType: &GetContainersEvent{s},
|
|
|
|
SignalEventType: &SignalEvent{s},
|
|
|
|
AddProcessEventType: &AddProcessEvent{s},
|
|
|
|
UpdateContainerEventType: &UpdateEvent{s},
|
|
|
|
CreateCheckpointEventType: &CreateCheckpointEvent{s},
|
2015-12-04 22:00:07 +00:00
|
|
|
DeleteCheckpointEventType: &DeleteCheckpointEvent{s},
|
2015-12-14 22:43:00 +00:00
|
|
|
StatsEventType: &StatsEvent{s},
|
|
|
|
UnsubscribeStatsEventType: &UnsubscribeStatsEvent{s},
|
2015-12-16 17:39:28 +00:00
|
|
|
StopStatsEventType: &StopStatsEvent{s},
|
2015-12-01 18:55:13 +00:00
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
go s.exitHandler()
|
|
|
|
if err := s.restore(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-05 23:29:53 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2015-12-11 19:27:33 +00:00
|
|
|
type containerInfo struct {
|
|
|
|
container runtime.Container
|
2015-12-14 22:15:26 +00:00
|
|
|
copier *copier
|
2015-12-11 19:27:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 23:29:53 +00:00
|
|
|
type Supervisor struct {
|
|
|
|
// stateDir is the directory on the system to store container runtime state information.
|
2015-12-10 22:11:00 +00:00
|
|
|
stateDir string
|
2015-12-11 19:27:33 +00:00
|
|
|
containers map[string]*containerInfo
|
2015-12-10 22:11:00 +00:00
|
|
|
handlers map[EventType]Handler
|
|
|
|
events chan *Event
|
|
|
|
tasks chan *StartTask
|
|
|
|
// we need a lock around the subscribers map only because additions and deletions from
|
|
|
|
// the map are via the API so we cannot really control the concurrency
|
2015-12-10 21:56:49 +00:00
|
|
|
subscriberLock sync.RWMutex
|
2015-12-10 20:30:04 +00:00
|
|
|
subscribers map[chan *Event]struct{}
|
2015-12-04 21:31:17 +00:00
|
|
|
machine Machine
|
2015-12-14 22:43:00 +00:00
|
|
|
statsCollector *statsCollector
|
2015-12-18 20:17:53 +00:00
|
|
|
notifier *chanotify.Notifier
|
2015-12-16 21:53:11 +00:00
|
|
|
el eventloop.EventLoop
|
2016-01-06 21:32:46 +00:00
|
|
|
monitor *Monitor
|
2015-11-05 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 22:11:00 +00:00
|
|
|
// Stop closes all tasks and sends a SIGTERM to each container's pid1 then waits for they to
|
|
|
|
// terminate. After it has handled all the SIGCHILD events it will close the signals chan
|
|
|
|
// and exit. Stop is a non-blocking call and will return after the containers have been signaled
|
2016-01-06 21:32:46 +00:00
|
|
|
func (s *Supervisor) Stop() {
|
2015-12-04 21:31:17 +00:00
|
|
|
// Close the tasks channel so that no new containers get started
|
|
|
|
close(s.tasks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes any open files in the supervisor but expects that Stop has been
|
|
|
|
// callsed so that no more containers are started.
|
2015-11-10 22:24:34 +00:00
|
|
|
func (s *Supervisor) Close() error {
|
2015-12-07 22:24:40 +00:00
|
|
|
return nil
|
2015-11-10 22:24:34 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 22:11:00 +00:00
|
|
|
// Events returns an event channel that external consumers can use to receive updates
|
|
|
|
// on container events
|
2015-12-10 20:30:04 +00:00
|
|
|
func (s *Supervisor) Events() chan *Event {
|
2015-12-10 21:56:49 +00:00
|
|
|
s.subscriberLock.Lock()
|
|
|
|
defer s.subscriberLock.Unlock()
|
2015-12-18 00:07:04 +00:00
|
|
|
c := make(chan *Event, defaultBufferSize)
|
2015-12-10 20:30:04 +00:00
|
|
|
EventSubscriberCounter.Inc(1)
|
|
|
|
s.subscribers[c] = struct{}{}
|
|
|
|
return c
|
2015-12-01 23:49:24 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 22:11:00 +00:00
|
|
|
// Unsubscribe removes the provided channel from receiving any more events
|
2015-12-10 20:30:04 +00:00
|
|
|
func (s *Supervisor) Unsubscribe(sub chan *Event) {
|
2015-12-10 21:56:49 +00:00
|
|
|
s.subscriberLock.Lock()
|
|
|
|
defer s.subscriberLock.Unlock()
|
2015-12-01 23:49:24 +00:00
|
|
|
delete(s.subscribers, sub)
|
2015-12-10 21:56:49 +00:00
|
|
|
close(sub)
|
2015-12-10 20:30:04 +00:00
|
|
|
EventSubscriberCounter.Dec(1)
|
2015-12-01 23:49:24 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 22:11:00 +00:00
|
|
|
// notifySubscribers will send the provided event to the external subscribers
|
|
|
|
// of the events channel
|
|
|
|
func (s *Supervisor) notifySubscribers(e *Event) {
|
2015-12-10 21:56:49 +00:00
|
|
|
s.subscriberLock.RLock()
|
|
|
|
defer s.subscriberLock.RUnlock()
|
2015-12-01 23:49:24 +00:00
|
|
|
for sub := range s.subscribers {
|
2015-12-10 21:56:49 +00:00
|
|
|
// do a non-blocking send for the channel
|
|
|
|
select {
|
|
|
|
case sub <- e:
|
|
|
|
default:
|
|
|
|
logrus.WithField("event", e.Type).Warn("event not sent to subscriber")
|
|
|
|
}
|
2015-12-01 23:49:24 +00:00
|
|
|
}
|
2015-11-30 23:46:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 00:40:57 +00:00
|
|
|
// Start is a non-blocking call that runs the supervisor for monitoring contianer processes and
|
2015-11-05 23:29:53 +00:00
|
|
|
// executing new containers.
|
|
|
|
//
|
2015-12-10 22:11:00 +00:00
|
|
|
// This event loop is the only thing that is allowed to modify state of containers and processes
|
|
|
|
// therefore it is save to do operations in the handlers that modify state of the system or
|
|
|
|
// state of the Supervisor
|
2015-12-03 01:44:39 +00:00
|
|
|
func (s *Supervisor) Start() error {
|
2015-12-08 17:37:31 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"stateDir": s.stateDir,
|
|
|
|
}).Debug("Supervisor started")
|
2015-12-16 21:53:11 +00:00
|
|
|
return s.el.Start()
|
2015-11-05 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 19:49:56 +00:00
|
|
|
// Machine returns the machine information for which the
|
|
|
|
// supervisor is executing on.
|
|
|
|
func (s *Supervisor) Machine() Machine {
|
|
|
|
return s.machine
|
|
|
|
}
|
|
|
|
|
2015-12-10 22:11:00 +00:00
|
|
|
// SendEvent sends the provided event the the supervisors main event loop
|
2015-11-10 22:24:34 +00:00
|
|
|
func (s *Supervisor) SendEvent(evt *Event) {
|
2015-12-16 21:53:11 +00:00
|
|
|
EventsCounter.Inc(1)
|
|
|
|
s.el.Send(&commonEvent{data: evt, sv: s})
|
2015-11-05 23:29:53 +00:00
|
|
|
}
|
2015-12-11 01:07:21 +00:00
|
|
|
|
2016-01-06 21:32:46 +00:00
|
|
|
func (s *Supervisor) exitHandler() {
|
|
|
|
for p := range s.monitor.Exits() {
|
|
|
|
e := NewEvent(ExitEventType)
|
|
|
|
e.Process = p
|
|
|
|
s.SendEvent(e)
|
2015-12-11 01:07:21 +00:00
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Supervisor) monitorProcess(p runtime.Process) error {
|
|
|
|
return s.monitor.Monitor(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Supervisor) restore() error {
|
|
|
|
dirs, err := ioutil.ReadDir(s.stateDir)
|
2015-12-11 19:27:33 +00:00
|
|
|
if err != nil {
|
2016-01-06 21:32:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, d := range dirs {
|
|
|
|
if !d.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
id := d.Name()
|
|
|
|
container, err := runtime.Load(s.stateDir, id)
|
|
|
|
if err != nil {
|
|
|
|
if err == runtime.ErrContainerExited {
|
|
|
|
logrus.WithField("id", id).Debug("containerd: container exited while away")
|
|
|
|
// TODO: fire events to do the removal
|
|
|
|
if err := os.RemoveAll(filepath.Join(s.stateDir, id)); err != nil {
|
|
|
|
logrus.WithField("error", err).Warn("containerd: remove container state")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
processes, err := container.Processes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ContainersCounter.Inc(1)
|
|
|
|
s.containers[id] = &containerInfo{
|
|
|
|
container: container,
|
|
|
|
}
|
|
|
|
logrus.WithField("id", id).Debug("containerd: container restored")
|
|
|
|
for _, p := range processes {
|
|
|
|
if err := s.monitorProcess(p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 01:07:21 +00:00
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
return nil
|
2015-12-11 01:07:21 +00:00
|
|
|
}
|