Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions pkg/guestagent/guestagent_linux.go
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Squash the commits
  • Add Signed-off-by line to the commit message to sign the DCO
  • Make the PR title descriptive, please

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"reflect"
"time"

"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -48,13 +49,11 @@ func New(ctx context.Context, ticker ticker.Ticker, runtimeDir string) (Agent, e
var _ Agent = (*agent)(nil)

type agent struct {
// Ticker is like time.Ticker.
// We can't use inotify for /proc/net/tcp, so we need this ticker to
// reload /proc/net/tcp.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove?

ticker ticker.Ticker
socketLister *sockets.Lister
kubernetesServiceWatcher *kubernetesservice.ServiceWatcher
runtimeDir string
recentChtimes map[string]time.Time
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a comment line to explain the key

}

type eventState struct {
Expand Down Expand Up @@ -211,13 +210,39 @@ func (a *agent) Info(ctx context.Context) (*api.Info, error) {

func (a *agent) HandleInotify(event *api.Inotify) {
location := event.MountPath
if _, err := os.Stat(location); err == nil {
local := event.Time.AsTime().Local()
err := os.Chtimes(location, local, local)
if err != nil {
logrus.Errorf("error in inotify handle. Event: %s, Error: %s", event, err)
fi, err := os.Stat(location)
if err != nil {
return
}
if fi.IsDir() {
return
}

now := time.Now()

// If we called Chtimes on this path recently, this is an echo:
// our Chtimes → virtiofs → macOS FSEvents → host agent → back here.
// Skip to prevent reload loops and reduce virtiofs traffic.
if lastTouch, ok := a.recentChtimes[location]; ok {
if now.Sub(lastTouch) < time.Second {
return
}
}

local := event.Time.AsTime().Local()
if err := os.Chtimes(location, local, local); err != nil {
logrus.Errorf("error in inotify handle. Event: %s, Error: %s", event, err)
}

if a.recentChtimes == nil {
a.recentChtimes = make(map[string]time.Time)
}
a.recentChtimes[location] = now

// Prevent unbounded growth during bulk operations
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use LRU algorithm or something?

if len(a.recentChtimes) > 10000 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10000 should be defined as a const

a.recentChtimes = make(map[string]time.Time)
}
}

func (a *agent) Close() error {
Expand Down
34 changes: 25 additions & 9 deletions pkg/hostagent/inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"path/filepath"
"strings"
"time"

"github.com/rjeczalik/notify"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -40,6 +41,15 @@ func (a *HostAgent) startInotify(ctx context.Context) error {
return err
}

// Trailing-edge debounce: accumulate events and only flush after
// a quiet period. During bulk operations (yarn install, nuxt build)
// the timer keeps resetting so no events are forwarded until the
// burst settles — preventing virtiofs virtqueue contention.
const quietPeriod = 200 * time.Millisecond
timer := time.NewTimer(quietPeriod)
timer.Stop()
pending := make(map[string]os.FileInfo)

for {
select {
case <-ctx.Done():
Expand All @@ -50,19 +60,25 @@ func (a *HostAgent) startInotify(ctx context.Context) error {
if err != nil {
continue
}

if stat.IsDir() {
continue
}
if filterEvents(watchEvent, stat) {
continue
}

watchPath = translateToGuestPath(watchPath, mountSymlinks, mountLocations)

utcTimestamp := timestamppb.New(stat.ModTime().UTC())
event := &guestagentapi.Inotify{MountPath: watchPath, Time: utcTimestamp}
err = inotifyClient.Send(event)
if err != nil {
logrus.WithError(err).Warn("failed to send inotify")
pending[watchPath] = stat
timer.Reset(quietPeriod)

case <-timer.C:
for wp, st := range pending {
guestPath := translateToGuestPath(wp, mountSymlinks, mountLocations)
utcTimestamp := timestamppb.New(st.ModTime().UTC())
event := &guestagentapi.Inotify{MountPath: guestPath, Time: utcTimestamp}
if err := inotifyClient.Send(event); err != nil {
logrus.WithError(err).Warn("failed to send inotify")
}
}
pending = make(map[string]os.FileInfo)
}
}
}
Expand Down
Loading