2015-12-18 00:07:04 +00:00
|
|
|
package supervisor
|
2015-11-05 23:29:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-12-04 21:31:17 +00:00
|
|
|
"os/signal"
|
2015-12-07 23:19:56 +00:00
|
|
|
"path/filepath"
|
2015-12-04 21:31:17 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
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-06 21:01:55 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
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.
|
|
|
|
func New(id, 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
|
|
|
|
}
|
2015-11-06 00:16:11 +00:00
|
|
|
// register counters
|
2015-12-07 23:19:56 +00:00
|
|
|
r, err := newRuntime(filepath.Join(stateDir, id))
|
2015-11-05 23:49:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-05 23:29:53 +00:00
|
|
|
}
|
2015-12-07 23:19:56 +00:00
|
|
|
machine, err := CollectMachineInformation(id)
|
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),
|
|
|
|
processes: make(map[int]*containerInfo),
|
|
|
|
runtime: r,
|
|
|
|
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),
|
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
|
|
|
}
|
|
|
|
// start the container workers for concurrent container starts
|
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
|
|
|
|
processes map[int]*containerInfo
|
2015-12-10 22:11:00 +00:00
|
|
|
handlers map[EventType]Handler
|
|
|
|
runtime runtime.Runtime
|
|
|
|
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
|
|
|
|
containerGroup sync.WaitGroup
|
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
|
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
|
2015-12-04 21:31:17 +00:00
|
|
|
func (s *Supervisor) Stop(sig chan os.Signal) {
|
|
|
|
// Close the tasks channel so that no new containers get started
|
|
|
|
close(s.tasks)
|
|
|
|
// send a SIGTERM to all containers
|
2015-12-11 19:27:33 +00:00
|
|
|
for id, i := range s.containers {
|
|
|
|
c := i.container
|
2015-12-04 21:31:17 +00:00
|
|
|
logrus.WithField("id", id).Debug("sending TERM to container processes")
|
|
|
|
procs, err := c.Processes()
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("id", id).Warn("get container processes")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(procs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mainProc := procs[0]
|
|
|
|
if err := mainProc.Signal(syscall.SIGTERM); err != nil {
|
|
|
|
pid, _ := mainProc.Pid()
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"id": id,
|
|
|
|
"pid": pid,
|
|
|
|
"error": err,
|
|
|
|
}).Error("send SIGTERM to process")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
logrus.Debug("waiting for containers to exit")
|
|
|
|
s.containerGroup.Wait()
|
|
|
|
logrus.Debug("all containers exited")
|
2015-12-18 20:17:53 +00:00
|
|
|
if s.notifier != nil {
|
|
|
|
s.notifier.Close()
|
|
|
|
}
|
2015-12-04 21:31:17 +00:00
|
|
|
// stop receiving signals and close the channel
|
|
|
|
signal.Stop(sig)
|
|
|
|
close(sig)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
"runtime": s.runtime.Type(),
|
|
|
|
"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
|
|
|
// getContainerForPid returns the container where the provided pid is the pid1 or main
|
|
|
|
// process in the container
|
2015-12-01 19:56:08 +00:00
|
|
|
func (s *Supervisor) getContainerForPid(pid int) (runtime.Container, error) {
|
2015-12-11 19:27:33 +00:00
|
|
|
for _, i := range s.containers {
|
|
|
|
container := i.container
|
2015-11-06 21:01:55 +00:00
|
|
|
cpid, err := container.Pid()
|
|
|
|
if err != nil {
|
|
|
|
if lerr, ok := err.(libcontainer.Error); ok {
|
|
|
|
if lerr.Code() == libcontainer.ProcessNotExecuted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logrus.WithField("error", err).Error("containerd: get container pid")
|
|
|
|
}
|
|
|
|
if pid == cpid {
|
|
|
|
return container, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errNoContainerForPid
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-12-14 22:40:50 +00:00
|
|
|
func (s *Supervisor) copyIO(stdin, stdout, stderr string, i *runtime.IO) (*copier, error) {
|
2015-12-14 22:15:26 +00:00
|
|
|
config := &ioConfig{
|
2015-12-11 01:07:21 +00:00
|
|
|
Stdin: i.Stdin,
|
|
|
|
Stdout: i.Stdout,
|
|
|
|
Stderr: i.Stderr,
|
2015-12-14 22:15:26 +00:00
|
|
|
StdoutPath: stdout,
|
|
|
|
StderrPath: stderr,
|
2015-12-14 22:40:50 +00:00
|
|
|
StdinPath: stdin,
|
2015-12-11 01:07:21 +00:00
|
|
|
}
|
2015-12-14 22:15:26 +00:00
|
|
|
l, err := newCopier(config)
|
2015-12-11 19:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-12-11 01:07:21 +00:00
|
|
|
}
|
2015-12-11 19:27:33 +00:00
|
|
|
return l, nil
|
2015-12-11 01:07:21 +00:00
|
|
|
}
|