Skip to content

Commit acacc5e

Browse files
authored
shards: only trigger rescan on .zoekt files changing (#801)
Any write to the index dir triggered a scan. This means on busy instances we are constantly rescanning, leading to an over-representation in CPU profiles around watch. The events are normally writes to our temporary files. By only considering events for .zoekt files (which is what scan reads) we can avoid the constant scan calls. Just in case we also introduce a re-scan every minute in case we miss an event. There is error handling around this, but I thought it is just more reliable to call scan every once in a while. Note: this doesn't represent significant CPU use, but it does muddy the CPU profiler output. So this makes it easier to understand trends in our continuous cpu profiling. Test Plan: CI
1 parent 764fe4f commit acacc5e

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

shards/watcher.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ func versionFromPath(path string) (string, int) {
117117
}
118118

119119
func (s *DirectoryWatcher) scan() error {
120+
// NOTE: if you change which file extensions are read, please update the
121+
// watch implementation.
120122
fs, err := filepath.Glob(filepath.Join(s.dir, "*.zoekt"))
121123
if err != nil {
122124
return err
@@ -216,21 +218,38 @@ func (s *DirectoryWatcher) watch() error {
216218
signal := make(chan struct{}, 1)
217219

218220
go func() {
221+
notify := func() {
222+
select {
223+
case signal <- struct{}{}:
224+
default:
225+
}
226+
}
227+
228+
ticker := time.NewTicker(time.Minute)
229+
219230
for {
220231
select {
221-
case <-watcher.Events:
222-
select {
223-
case signal <- struct{}{}:
224-
default:
232+
case event := <-watcher.Events:
233+
// Only notify if a file we read in has changed. This is important to
234+
// avoid all the events writing to temporary files.
235+
if strings.HasSuffix(event.Name, ".zoekt") || strings.HasSuffix(event.Name, ".meta") {
236+
notify()
225237
}
238+
239+
case <-ticker.C:
240+
// Periodically just double check the disk
241+
notify()
242+
226243
case err := <-watcher.Errors:
227244
// Ignore ErrEventOverflow since we rely on the presence of events so
228245
// safe to ignore.
229246
if err != nil && err != fsnotify.ErrEventOverflow {
230247
log.Println("watcher error:", err)
231248
}
249+
232250
case <-s.quit:
233251
watcher.Close()
252+
ticker.Stop()
234253
close(signal)
235254
return
236255
}

0 commit comments

Comments
 (0)