Skip to content

Commit 6af7bbb

Browse files
authored
Merge pull request #32 from Random-Liu/remove-get-start-point
NPD: Remove get start point
2 parents f265f79 + aa9e268 commit 6af7bbb

File tree

2 files changed

+11
-49
lines changed

2 files changed

+11
-49
lines changed

pkg/kernelmonitor/kernel_log_watcher.go

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package kernelmonitor
1818

1919
import (
20-
"bufio"
21-
"fmt"
2220
"os"
2321
"time"
2422

@@ -97,19 +95,11 @@ func (k *kernelLogWatcher) Watch() (<-chan *types.KernelLog, error) {
9795
glog.Infof("kernel log %q is not found, kernel monitor doesn't support the os distro", path)
9896
return nil, nil
9997
}
100-
start, err := k.getStartPoint(path)
101-
if err != nil {
102-
return nil, err
103-
}
104-
// TODO(random-liu): If the file gets recreated during this interval, the logic
105-
// will go wrong here.
10698
// TODO(random-liu): Rate limit tail file.
10799
// TODO(random-liu): Figure out what happens if log lines are removed.
100+
// Notice that, kernel log watcher doesn't look back to the rolled out logs.
101+
var err error
108102
k.tl, err = tail.TailFile(path, tail.Config{
109-
Location: &tail.SeekInfo{
110-
Offset: start,
111-
Whence: os.SEEK_SET,
112-
},
113103
Poll: true,
114104
ReOpen: true,
115105
Follow: true,
@@ -132,6 +122,10 @@ func (k *kernelLogWatcher) watchLoop() {
132122
close(k.logCh)
133123
k.tomb.Done()
134124
}()
125+
lookback, err := parseDuration(k.cfg.Lookback)
126+
if err != nil {
127+
glog.Fatalf("failed to parse duration %q: %v", k.cfg.Lookback, err)
128+
}
135129
for {
136130
select {
137131
case line := <-k.tl.Lines:
@@ -145,6 +139,10 @@ func (k *kernelLogWatcher) watchLoop() {
145139
glog.Infof("Unable to parse line: %q, %v", line, err)
146140
continue
147141
}
142+
// If the log is older than look back duration, discard it.
143+
if k.clock.Since(log.Timestamp) > lookback {
144+
continue
145+
}
148146
k.logCh <- log
149147
case <-k.tomb.Stopping():
150148
k.tl.Stop()
@@ -154,42 +152,6 @@ func (k *kernelLogWatcher) watchLoop() {
154152
}
155153
}
156154

157-
// getStartPoint finds the start point to parse the log. The start point is either
158-
// the line at (now - lookback) or the first line of kernel log.
159-
// Notice that, kernel log watcher doesn't look back to the rolled out logs.
160-
func (k *kernelLogWatcher) getStartPoint(path string) (int64, error) {
161-
f, err := os.Open(path)
162-
if err != nil {
163-
return 0, fmt.Errorf("failed to open file %q: %v", path, err)
164-
}
165-
defer f.Close()
166-
lookback, err := parseDuration(k.cfg.Lookback)
167-
if err != nil {
168-
return 0, fmt.Errorf("failed to parse duration %q: %v", k.cfg.Lookback, err)
169-
}
170-
start := int64(0)
171-
reader := bufio.NewReader(f)
172-
done := false
173-
for !done {
174-
line, err := reader.ReadString('\n')
175-
if err != nil {
176-
if len(line) == 0 {
177-
// No need to continue parsing if nothing is read
178-
break
179-
}
180-
done = true
181-
}
182-
log, err := k.trans.Translate(line)
183-
if err != nil {
184-
glog.Infof("unable to parse line: %q, %v", line, err)
185-
} else if k.clock.Since(log.Timestamp) <= lookback {
186-
break
187-
}
188-
start += int64(len(line))
189-
}
190-
return start, nil
191-
}
192-
193155
func parseDuration(s string) (time.Duration, error) {
194156
// If the duration is not configured, just return 0 by default
195157
if s == "" {

pkg/kernelmonitor/kernel_log_watcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/pivotal-golang/clock/fakeclock"
2929
)
3030

31-
func TestGetStartPoint(t *testing.T) {
31+
func TestWatch(t *testing.T) {
3232
// now is a fake time
3333
now := time.Date(time.Now().Year(), time.January, 2, 3, 4, 5, 0, time.Local)
3434
fakeClock := fakeclock.NewFakeClock(now)

0 commit comments

Comments
 (0)