forked from containers/podman
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
file logger: fix podman logs --tail with partial lines
There is a problem where our tail code does not handles correctly partial log lines. This makes podman logs --tail output possibly incorrect lines when k8s-file is used. This manifests as flake in CI because partial lines are only sometimes written, basically always when the output is flushed before writing a newline. For our code we must not count partial lines which was already done but the important thing we must keep reading backwards until the next full (F) line. This is because all partial (P) lines still must be added to the full line. See the added tests for details on how the log file looks like. While fixing this, I rework the tail logic a bit, there is absolutely no reason to read the lines in a separate goroutine just to pass the lines back via channel. We can do this in the same routine. The logic is very simple, read the lines backwards, append lines to result and then at the end invert the result slice as tail must return the lines in the correct order. This more efficient then having to allocate two different slices or to prepend the line as this would require a new allocation for each line. Lastly the readFromLogFile() function wrote the lines back to the log line channel in the same routine as the log lines we read, this was bad and causes a deadlock when the returned lines are bigger than the channel size. There is no reason to allocate a big channel size we can just write the log lines in a different goroutine, in this case the main routine were read the logs anyway. A new system test and unit tests have been added to check corner cases. Fixes containers#19545 Signed-off-by: Paul Holzinger <pholzing@redhat.com>
- Loading branch information
1 parent
717bcdb
commit 5390bbe
Showing
6 changed files
with
239 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package logs | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var logTime time.Time | ||
|
||
func init() { | ||
logTime, _ = time.Parse(LogTimeFormat, "2023-08-07T19:56:34.223758260-06:00") | ||
} | ||
|
||
func makeTestLogLine(typ, msg string) *LogLine { | ||
return &LogLine{ | ||
Device: "stdout", | ||
ParseLogType: typ, | ||
Msg: msg, | ||
Time: logTime, | ||
} | ||
} | ||
|
||
func TestGetTailLog(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fileContent string | ||
tail int | ||
want []*LogLine | ||
}{ | ||
{ | ||
name: "simple tail", | ||
fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1 | ||
2023-08-07T19:56:34.223758260-06:00 stdout F line2 | ||
2023-08-07T19:56:34.223758260-06:00 stdout F line3 | ||
`, | ||
tail: 2, | ||
want: []*LogLine{makeTestLogLine("F", "line2"), makeTestLogLine("F", "line3")}, | ||
}, | ||
{ | ||
name: "simple tail with more tail than lines", | ||
fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1 | ||
2023-08-07T19:56:34.223758260-06:00 stdout F line2 | ||
2023-08-07T19:56:34.223758260-06:00 stdout F line3 | ||
`, | ||
tail: 10, | ||
want: []*LogLine{makeTestLogLine("F", "line1"), makeTestLogLine("F", "line2"), makeTestLogLine("F", "line3")}, | ||
}, | ||
{ | ||
name: "tail with partial logs", | ||
fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1 | ||
2023-08-07T19:56:34.223758260-06:00 stdout P lin | ||
2023-08-07T19:56:34.223758260-06:00 stdout F e2 | ||
2023-08-07T19:56:34.223758260-06:00 stdout F line3 | ||
`, | ||
tail: 2, | ||
want: []*LogLine{makeTestLogLine("P", "lin"), makeTestLogLine("F", "e2"), makeTestLogLine("F", "line3")}, | ||
}, | ||
{ | ||
name: "tail with partial logs and more than lines", | ||
fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1 | ||
2023-08-07T19:56:34.223758260-06:00 stdout P lin | ||
2023-08-07T19:56:34.223758260-06:00 stdout F e2 | ||
2023-08-07T19:56:34.223758260-06:00 stdout F line3 | ||
`, | ||
tail: 10, | ||
want: []*LogLine{makeTestLogLine("F", "line1"), makeTestLogLine("P", "lin"), makeTestLogLine("F", "e2"), makeTestLogLine("F", "line3")}, | ||
}, | ||
{ | ||
name: "multiple partial lines in a row", | ||
fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1 | ||
2023-08-07T19:56:34.223758260-06:00 stdout P l | ||
2023-08-07T19:56:34.223758260-06:00 stdout P i | ||
2023-08-07T19:56:34.223758260-06:00 stdout P n | ||
2023-08-07T19:56:34.223758260-06:00 stdout P e | ||
2023-08-07T19:56:34.223758260-06:00 stdout F 2 | ||
2023-08-07T19:56:34.223758260-06:00 stdout F line3 | ||
`, | ||
tail: 2, | ||
want: []*LogLine{makeTestLogLine("P", "l"), makeTestLogLine("P", "i"), makeTestLogLine("P", "n"), | ||
makeTestLogLine("P", "e"), makeTestLogLine("F", "2"), makeTestLogLine("F", "line3")}, | ||
}, | ||
{ | ||
name: "partial line at the end", | ||
fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1 | ||
2023-08-07T19:56:34.223758260-06:00 stdout P lin | ||
`, | ||
tail: 1, | ||
want: []*LogLine{makeTestLogLine("P", "lin")}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
dir := t.TempDir() | ||
file := filepath.Join(dir, "log") | ||
f, err := os.Create(file) | ||
assert.NoError(t, err, "create log file") | ||
_, err = f.WriteString(tt.fileContent) | ||
assert.NoError(t, err, "write log file") | ||
f.Close() | ||
got, err := getTailLog(file, tt.tail) | ||
assert.NoError(t, err, "getTailLog()") | ||
assert.Equal(t, tt.want, got, "log lines") | ||
}) | ||
} | ||
} | ||
|
||
func TestGetTailLogBigFiles(t *testing.T) { | ||
dir := t.TempDir() | ||
file := filepath.Join(dir, "log") | ||
f, err := os.Create(file) | ||
assert.NoError(t, err, "create log file") | ||
want := make([]*LogLine, 0, 2000) | ||
for i := 0; i < 1000; i++ { | ||
_, err = f.WriteString(`2023-08-07T19:56:34.223758260-06:00 stdout P lin | ||
2023-08-07T19:56:34.223758260-06:00 stdout F e2 | ||
`) | ||
assert.NoError(t, err, "write log file") | ||
want = append(want, makeTestLogLine("P", "lin"), makeTestLogLine("F", "e2")) | ||
} | ||
f.Close() | ||
|
||
// try a big tail greater than the lines | ||
got, err := getTailLog(file, 5000) | ||
assert.NoError(t, err, "getTailLog()") | ||
assert.Equal(t, want, got, "all log lines") | ||
|
||
// try a smaller than lines tail | ||
got, err = getTailLog(file, 100) | ||
assert.NoError(t, err, "getTailLog()") | ||
// this will return the last 200 lines because of partial + full and we only count full lines for tail. | ||
assert.Equal(t, want[1800:2000], got, "tail 100 log lines") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters