Skip to content

feat(kernelLogWatcher): enable revive kmsg parser if channel closed #1004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 46 additions & 10 deletions pkg/systemlogmonitor/logwatchers/kmsg/log_watcher_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ import (
"k8s.io/node-problem-detector/pkg/util/tomb"
)

const (
reviveRetries = 10
reviveDuration = 5 * time.Second
)

type kernelLogWatcher struct {
cfg types.WatcherConfig
startTime time.Time
logCh chan *logtypes.Log
tomb *tomb.Tomb

kmsgParser kmsgparser.Parser
kmsgParser kmsgparser.Parser
reviveCount int
}

// NewKmsgWatcher creates a watcher which will read messages from /dev/kmsg
Expand All @@ -55,22 +61,17 @@ func NewKmsgWatcher(cfg types.WatcherConfig) types.LogWatcher {
startTime: startTime,
tomb: tomb.NewTomb(),
// Arbitrary capacity
logCh: make(chan *logtypes.Log, 100),
logCh: make(chan *logtypes.Log, 100),
reviveCount: 0,
}
}

var _ types.WatcherCreateFunc = NewKmsgWatcher

func (k *kernelLogWatcher) Watch() (<-chan *logtypes.Log, error) {
if k.kmsgParser == nil {
// nil-check to make mocking easier
parser, err := kmsgparser.NewParser()
if err != nil {
return nil, fmt.Errorf("failed to create kmsg parser: %v", err)
}
k.kmsgParser = parser
if err := k.SetKmsgParser(); err != nil {
return nil, err
}

go k.watchLoop()
return k.logCh, nil
}
Expand Down Expand Up @@ -99,6 +100,9 @@ func (k *kernelLogWatcher) watchLoop() {
return
case msg, ok := <-kmsgs:
if !ok {
if val, ok := k.cfg.PluginConfig["revive"]; ok && val == "true" {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a sample config for using revive?

k.reviveMyself()
Copy link
Member

Choose a reason for hiding this comment

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

Before revive, add a log line about we are starting to revive.

}
klog.Error("Kmsg channel closed")
Copy link
Member

Choose a reason for hiding this comment

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

This log line should be before the revive.

return
}
Expand All @@ -120,3 +124,35 @@ func (k *kernelLogWatcher) watchLoop() {
}
}
}

// create a new kmsg parser and sets it to the watcher.
func (k *kernelLogWatcher) SetKmsgParser() error {
parser, err := kmsgparser.NewParser()
if err != nil {
return fmt.Errorf("failed to create kmsg parser: %v", err)
}
k.kmsgParser = parser
return nil
}

// revive ourselves if the kmsg channel is closed
Copy link
Member

Choose a reason for hiding this comment

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

I am curious about the error you observe when the channel is closed. Do you see the "Kmsg channel closed" log? Is it always associated with high load of the node?

// close the old kmsg parser and create a new one
// enter the watch loop again
func (k *kernelLogWatcher) reviveMyself() {
// if k.reviveCount >= reviveRetries {
Copy link
Member

Choose a reason for hiding this comment

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

Uncomment?

// klog.Errorf("Failed to revive kmsg parser after %d retries", reviveRetries)
Copy link
Member

Choose a reason for hiding this comment

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

For the retries, is there any time boundary needed? For example, if the revive happens 10 times over a year, it may be fine. But if revive happens 10 times over a minute, something is wrong.

// return
// }
// klog.Infof("Reviving kmsg parser, attempt %d of %d", k.reviveCount, reviveRetries)
klog.Infof("Reviving kmsg parser, attempt %d", k.reviveCount)
if err := k.kmsgParser.Close(); err != nil {
klog.Errorf("Failed to close kmsg parser: %v", err)
}
time.Sleep(reviveDuration)
if err := k.SetKmsgParser(); err != nil {
klog.Errorf("Failed to revive kmsg parser: %v", err)
return
}
k.reviveCount++
k.watchLoop()
}