From 392e1be9e5d7880b72c6ef9931e5390fbee949df Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Thu, 21 May 2015 13:03:08 -0700 Subject: [PATCH] Use one InotifyWatcher --- container/raw/factory.go | 11 ++++++++++- container/raw/handler.go | 17 ++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/container/raw/factory.go b/container/raw/factory.go index 27020eeb..eac7e72e 100644 --- a/container/raw/factory.go +++ b/container/raw/factory.go @@ -36,6 +36,9 @@ type rawFactory struct { // Information about mounted filesystems. fsInfo fs.FsInfo + + // Watcher for inotify events. + watcher *InotifyWatcher } func (self *rawFactory) String() string { @@ -43,7 +46,7 @@ func (self *rawFactory) String() string { } func (self *rawFactory) NewContainerHandler(name string) (container.ContainerHandler, error) { - return newRawContainerHandler(name, self.cgroupSubsystems, self.machineInfoFactory, self.fsInfo) + return newRawContainerHandler(name, self.cgroupSubsystems, self.machineInfoFactory, self.fsInfo, self.watcher) } // The raw factory can handle any container. If --docker_only is set to false, non-docker containers are ignored. @@ -61,11 +64,17 @@ func Register(machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo) erro return fmt.Errorf("failed to find supported cgroup mounts for the raw factory") } + watcher, err := NewInotifyWatcher() + if err != nil { + return err + } + glog.Infof("Registering Raw factory") factory := &rawFactory{ machineInfoFactory: machineInfoFactory, fsInfo: fsInfo, cgroupSubsystems: &cgroupSubsystems, + watcher: watcher, } container.RegisterContainerHandlerFactory(factory) return nil diff --git a/container/raw/handler.go b/container/raw/handler.go index d3c1029c..160477f6 100644 --- a/container/raw/handler.go +++ b/container/raw/handler.go @@ -62,7 +62,7 @@ type rawContainerHandler struct { externalMounts []mount } -func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSubsystems, machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo) (container.ContainerHandler, error) { +func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSubsystems, machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo, watcher *InotifyWatcher) (container.ContainerHandler, error) { // Create the cgroup paths. cgroupPaths := make(map[string]string, len(cgroupSubsystems.MountPoints)) for key, val := range cgroupSubsystems.MountPoints { @@ -106,6 +106,7 @@ func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSu fsInfo: fsInfo, hasNetwork: hasNetwork, externalMounts: externalMounts, + watcher: watcher, }, nil } @@ -501,15 +502,6 @@ func (self *rawContainerHandler) processEvent(event *inotify.Event, events chan } func (self *rawContainerHandler) WatchSubcontainers(events chan container.SubcontainerEvent) error { - // Lazily initialize the watcher so we don't use it when not asked to. - if self.watcher == nil { - w, err := NewInotifyWatcher() - if err != nil { - return err - } - self.watcher = w - } - // Watch this container (all its cgroups) and all subdirectories. for _, cgroupPath := range self.cgroupPaths { _, err := self.watchDirectory(cgroupPath, self.name) @@ -533,7 +525,6 @@ func (self *rawContainerHandler) WatchSubcontainers(events chan container.Subcon err := self.watcher.Close() if err == nil { self.stopWatcher <- err - self.watcher = nil return } } @@ -544,10 +535,6 @@ func (self *rawContainerHandler) WatchSubcontainers(events chan container.Subcon } func (self *rawContainerHandler) StopWatchingSubcontainers() error { - if self.watcher == nil { - return fmt.Errorf("can't stop watch that has not started for container %q", self.name) - } - // Rendezvous with the watcher thread. self.stopWatcher <- nil return <-self.stopWatcher