Skip to content

Commit

Permalink
Merge pull request google#1962 from dashpole/cherrypick_1959
Browse files Browse the repository at this point in the history
Revert "Switch from Inotify to Fsnotify"
  • Loading branch information
dashpole authored Jun 11, 2018
2 parents f6359ff + fdcae55 commit 49c4fae
Show file tree
Hide file tree
Showing 29 changed files with 522 additions and 2,364 deletions.
45 changes: 24 additions & 21 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion container/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func DebugInfo(watches map[string][]string) map[string][]string {
lines = append(lines, fmt.Sprintf("\t%s", cg))
}
}
out["Fsnotify watches"] = lines
out["Inotify watches"] = lines

return out
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ package common
import (
"sync"

"github.com/fsnotify/fsnotify"
"golang.org/x/exp/inotify"
)

// Watcher for container-related fsnotify events in the cgroup hierarchy.
// Watcher for container-related inotify events in the cgroup hierarchy.
//
// Implementation is thread-safe.
type FsnotifyWatcher struct {
// Underlying fsnotify watcher.
watcher *fsnotify.Watcher
type InotifyWatcher struct {
// Underlying inotify watcher.
watcher *inotify.Watcher

// Map of containers being watched to cgroup paths watched for that container.
containersWatched map[string]map[string]bool
Expand All @@ -34,28 +34,28 @@ type FsnotifyWatcher struct {
lock sync.Mutex
}

func NewFsnotifyWatcher() (*FsnotifyWatcher, error) {
w, err := fsnotify.NewWatcher()
func NewInotifyWatcher() (*InotifyWatcher, error) {
w, err := inotify.NewWatcher()
if err != nil {
return nil, err
}

return &FsnotifyWatcher{
return &InotifyWatcher{
watcher: w,
containersWatched: make(map[string]map[string]bool),
}, nil
}

// Add a watch to the specified directory. Returns if the container was already being watched.
func (iw *FsnotifyWatcher) AddWatch(containerName, dir string) (bool, error) {
func (iw *InotifyWatcher) AddWatch(containerName, dir string) (bool, error) {
iw.lock.Lock()
defer iw.lock.Unlock()

cgroupsWatched, alreadyWatched := iw.containersWatched[containerName]

// Register an fsnotify notification.
// Register an inotify notification.
if !cgroupsWatched[dir] {
err := iw.watcher.Add(dir)
err := iw.watcher.AddWatch(dir, inotify.IN_CREATE|inotify.IN_DELETE|inotify.IN_MOVE)
if err != nil {
return alreadyWatched, err
}
Expand All @@ -74,7 +74,7 @@ func (iw *FsnotifyWatcher) AddWatch(containerName, dir string) (bool, error) {
}

// Remove watch from the specified directory. Returns if this was the last watch on the specified container.
func (iw *FsnotifyWatcher) RemoveWatch(containerName, dir string) (bool, error) {
func (iw *InotifyWatcher) RemoveWatch(containerName, dir string) (bool, error) {
iw.lock.Lock()
defer iw.lock.Unlock()

Expand All @@ -84,9 +84,9 @@ func (iw *FsnotifyWatcher) RemoveWatch(containerName, dir string) (bool, error)
return false, nil
}

// Remove the fsnotify watch if it exists.
// Remove the inotify watch if it exists.
if cgroupsWatched[dir] {
err := iw.watcher.Remove(dir)
err := iw.watcher.RemoveWatch(dir)
if err != nil {
return false, nil
}
Expand All @@ -103,22 +103,22 @@ func (iw *FsnotifyWatcher) RemoveWatch(containerName, dir string) (bool, error)
}

// Errors are returned on this channel.
func (iw *FsnotifyWatcher) Error() chan error {
return iw.watcher.Errors
func (iw *InotifyWatcher) Error() chan error {
return iw.watcher.Error
}

// Events are returned on this channel.
func (iw *FsnotifyWatcher) Event() chan fsnotify.Event {
return iw.watcher.Events
func (iw *InotifyWatcher) Event() chan *inotify.Event {
return iw.watcher.Event
}

// Closes the fsnotify watcher.
func (iw *FsnotifyWatcher) Close() error {
// Closes the inotify watcher.
func (iw *InotifyWatcher) Close() error {
return iw.watcher.Close()
}

// Returns a map of containers to the cgroup paths being watched.
func (iw *FsnotifyWatcher) GetWatches() map[string][]string {
func (iw *InotifyWatcher) GetWatches() map[string][]string {
out := make(map[string][]string, len(iw.containersWatched))
for k, v := range iw.containersWatched {
out[k] = mapToSlice(v)
Expand Down
6 changes: 3 additions & 3 deletions container/raw/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type rawFactory struct {
// Information about mounted filesystems.
fsInfo fs.FsInfo

// Watcher for fsnotify events.
watcher *common.FsnotifyWatcher
// Watcher for inotify events.
watcher *common.InotifyWatcher

// List of metrics to be ignored.
ignoreMetrics map[container.MetricKind]struct{}
Expand Down Expand Up @@ -78,7 +78,7 @@ func Register(machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo, igno
return fmt.Errorf("failed to find supported cgroup mounts for the raw factory")
}

watcher, err := common.NewFsnotifyWatcher()
watcher, err := common.NewInotifyWatcher()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion container/raw/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func isRootCgroup(name string) bool {
return name == "/"
}

func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSubsystems, machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo, watcher *common.FsnotifyWatcher, rootFs string, ignoreMetrics container.MetricSet) (container.ContainerHandler, error) {
func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSubsystems, machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo, watcher *common.InotifyWatcher, rootFs string, ignoreMetrics container.MetricSet) (container.ContainerHandler, error) {
cgroupPaths := common.MakeCgroupPaths(cgroupSubsystems.MountPoints, name)

cHints, err := common.GetContainerHintsFromFile(*common.ArgContainerHints)
Expand Down
22 changes: 12 additions & 10 deletions manager/watcher/raw/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"github.com/google/cadvisor/container/libcontainer"
"github.com/google/cadvisor/manager/watcher"

"github.com/fsnotify/fsnotify"
"github.com/golang/glog"
"golang.org/x/exp/inotify"
)

type rawContainerWatcher struct {
Expand All @@ -37,8 +37,8 @@ type rawContainerWatcher struct {

cgroupSubsystems *libcontainer.CgroupSubsystems

// Fsnotify event watcher.
watcher *common.FsnotifyWatcher
// Inotify event watcher.
watcher *common.InotifyWatcher

// Signal for watcher thread to stop.
stopWatcher chan error
Expand All @@ -53,7 +53,7 @@ func NewRawContainerWatcher() (watcher.ContainerWatcher, error) {
return nil, fmt.Errorf("failed to find supported cgroup mounts for the raw factory")
}

watcher, err := common.NewFsnotifyWatcher()
watcher, err := common.NewInotifyWatcher()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func (self *rawContainerWatcher) watchDirectory(events chan watcher.ContainerEve
if cleanup {
_, err := self.watcher.RemoveWatch(containerName, dir)
if err != nil {
glog.Warningf("Failed to remove fsnotify watch for %q: %v", dir, err)
glog.Warningf("Failed to remove inotify watch for %q: %v", dir, err)
}
}
}()
Expand Down Expand Up @@ -163,16 +163,18 @@ func (self *rawContainerWatcher) watchDirectory(events chan watcher.ContainerEve
return alreadyWatching, nil
}

func (self *rawContainerWatcher) processEvent(event fsnotify.Event, events chan watcher.ContainerEvent) error {
// Convert the fsnotify event type to a container create or delete.
func (self *rawContainerWatcher) processEvent(event *inotify.Event, events chan watcher.ContainerEvent) error {
// Convert the inotify event type to a container create or delete.
var eventType watcher.ContainerEventType
switch {
case event.Op == fsnotify.Create:
case (event.Mask & inotify.IN_CREATE) > 0:
eventType = watcher.ContainerAdd
case event.Op == fsnotify.Remove:
case (event.Mask & inotify.IN_DELETE) > 0:
eventType = watcher.ContainerDelete
case event.Op == fsnotify.Rename:
case (event.Mask & inotify.IN_MOVED_FROM) > 0:
eventType = watcher.ContainerDelete
case (event.Mask & inotify.IN_MOVED_TO) > 0:
eventType = watcher.ContainerAdd
default:
// Ignore other events.
return nil
Expand Down
14 changes: 7 additions & 7 deletions utils/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"sync"
"time"

"github.com/fsnotify/fsnotify"
"github.com/golang/glog"
"golang.org/x/exp/inotify"
)

type Tail struct {
Expand All @@ -35,7 +35,7 @@ type Tail struct {
filename string
file *os.File
stop chan bool
watcher *fsnotify.Watcher
watcher *inotify.Watcher
}

const (
Expand All @@ -60,9 +60,9 @@ func newTail(filename string) (*Tail, error) {
}
var err error
t.stop = make(chan bool)
t.watcher, err = fsnotify.NewWatcher()
t.watcher, err = inotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("fsnotify init failed on %s: %v", t.filename, err)
return nil, fmt.Errorf("inotify init failed on %s: %v", t.filename, err)
}
// Initialize readerErr as io.EOF, so that the reader can work properly
// during initialization.
Expand Down Expand Up @@ -141,15 +141,15 @@ func (t *Tail) watchFile() error {
defer t.file.Close()

watchDir := filepath.Dir(t.filename)
err = t.watcher.Add(watchDir)
err = t.watcher.AddWatch(watchDir, inotify.IN_MOVED_FROM|inotify.IN_DELETE)
if err != nil {
return fmt.Errorf("Failed to add watch to directory %s: %v", watchDir, err)
}
defer t.watcher.Remove(watchDir)
defer t.watcher.RemoveWatch(watchDir)

for {
select {
case event := <-t.watcher.Events:
case event := <-t.watcher.Event:
eventPath := filepath.Clean(event.Name) // Directory events have an extra '/'
if eventPath == t.filename {
glog.V(4).Infof("Log file %s moved/deleted", t.filename)
Expand Down
5 changes: 0 additions & 5 deletions vendor/github.com/fsnotify/fsnotify/.editorconfig

This file was deleted.

Loading

0 comments on commit 49c4fae

Please sign in to comment.