Use one InotifyWatcher

This commit is contained in:
Victor Marmol 2015-05-21 13:03:08 -07:00
parent 6f568b9c8e
commit 392e1be9e5
2 changed files with 12 additions and 16 deletions

View File

@ -36,6 +36,9 @@ type rawFactory struct {
// Information about mounted filesystems. // Information about mounted filesystems.
fsInfo fs.FsInfo fsInfo fs.FsInfo
// Watcher for inotify events.
watcher *InotifyWatcher
} }
func (self *rawFactory) String() string { func (self *rawFactory) String() string {
@ -43,7 +46,7 @@ func (self *rawFactory) String() string {
} }
func (self *rawFactory) NewContainerHandler(name string) (container.ContainerHandler, error) { 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. // 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") 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") glog.Infof("Registering Raw factory")
factory := &rawFactory{ factory := &rawFactory{
machineInfoFactory: machineInfoFactory, machineInfoFactory: machineInfoFactory,
fsInfo: fsInfo, fsInfo: fsInfo,
cgroupSubsystems: &cgroupSubsystems, cgroupSubsystems: &cgroupSubsystems,
watcher: watcher,
} }
container.RegisterContainerHandlerFactory(factory) container.RegisterContainerHandlerFactory(factory)
return nil return nil

View File

@ -62,7 +62,7 @@ type rawContainerHandler struct {
externalMounts []mount 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. // Create the cgroup paths.
cgroupPaths := make(map[string]string, len(cgroupSubsystems.MountPoints)) cgroupPaths := make(map[string]string, len(cgroupSubsystems.MountPoints))
for key, val := range cgroupSubsystems.MountPoints { for key, val := range cgroupSubsystems.MountPoints {
@ -106,6 +106,7 @@ func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSu
fsInfo: fsInfo, fsInfo: fsInfo,
hasNetwork: hasNetwork, hasNetwork: hasNetwork,
externalMounts: externalMounts, externalMounts: externalMounts,
watcher: watcher,
}, nil }, nil
} }
@ -501,15 +502,6 @@ func (self *rawContainerHandler) processEvent(event *inotify.Event, events chan
} }
func (self *rawContainerHandler) WatchSubcontainers(events chan container.SubcontainerEvent) error { 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. // Watch this container (all its cgroups) and all subdirectories.
for _, cgroupPath := range self.cgroupPaths { for _, cgroupPath := range self.cgroupPaths {
_, err := self.watchDirectory(cgroupPath, self.name) _, err := self.watchDirectory(cgroupPath, self.name)
@ -533,7 +525,6 @@ func (self *rawContainerHandler) WatchSubcontainers(events chan container.Subcon
err := self.watcher.Close() err := self.watcher.Close()
if err == nil { if err == nil {
self.stopWatcher <- err self.stopWatcher <- err
self.watcher = nil
return return
} }
} }
@ -544,10 +535,6 @@ func (self *rawContainerHandler) WatchSubcontainers(events chan container.Subcon
} }
func (self *rawContainerHandler) StopWatchingSubcontainers() error { 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. // Rendezvous with the watcher thread.
self.stopWatcher <- nil self.stopWatcher <- nil
return <-self.stopWatcher return <-self.stopWatcher