-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Avoid splitting log line across two files #4282
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
@@ -168,6 +169,72 @@ func TestFileRotator_RotateFiles(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestFileRotator_RotateFiles_Boundary(t *testing.T) { | ||
t.Parallel() | ||
var path string | ||
var err error | ||
if path, err = ioutil.TempDir("", pathPrefix); err != nil { | ||
t.Fatalf("test setup err: %v", err) | ||
} | ||
defer os.RemoveAll(path) | ||
|
||
fr, err := NewFileRotator(path, baseFileName, 10, 5, logger) | ||
if err != nil { | ||
t.Fatalf("test setup err: %v", err) | ||
} | ||
|
||
// We will write two times: | ||
// 1st: Write with new lines spanning two files | ||
// 2nd: Write long string with no new lines | ||
expectations := [][]byte{ | ||
[]byte("ab\n"), | ||
[]byte("cdef\n"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would suggest adding test cases with new line in the beginning, and middle of string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. str1 captures the middle but I can add one for the beginning |
||
[]byte("12345"), | ||
[]byte("6789"), | ||
} | ||
str1 := "ab\ncdef\n" | ||
str2 := "123456789" | ||
|
||
nw, err := fr.Write([]byte(str1)) | ||
if err != nil { | ||
t.Fatalf("got error while writing: %v", err) | ||
} | ||
|
||
if nw != len(str1) { | ||
t.Fatalf("expected %v, got %v", len(str1), nw) | ||
} | ||
|
||
nw, err = fr.Write([]byte(str2)) | ||
if err != nil { | ||
t.Fatalf("got error while writing: %v", err) | ||
} | ||
|
||
if nw != len(str2) { | ||
t.Fatalf("expected %v, got %v", len(str2), nw) | ||
} | ||
|
||
var lastErr error | ||
testutil.WaitForResult(func() (bool, error) { | ||
|
||
for i, exp := range expectations { | ||
fname := filepath.Join(path, fmt.Sprintf("redis.stdout.%d", i)) | ||
fi, err := os.Stat(fname) | ||
if err != nil { | ||
lastErr = err | ||
return false, nil | ||
} | ||
if int(fi.Size()) != len(exp) { | ||
lastErr = fmt.Errorf("expected size: %v, actual: %v", len(exp), fi.Size()) | ||
return false, nil | ||
} | ||
} | ||
|
||
return true, nil | ||
}, func(err error) { | ||
t.Fatalf("%v", lastErr) | ||
}) | ||
} | ||
|
||
func TestFileRotator_WriteRemaining(t *testing.T) { | ||
t.Parallel() | ||
var path string | ||
|
@@ -289,3 +356,48 @@ func TestFileRotator_PurgeOldFiles(t *testing.T) { | |
t.Fatalf("%v", lastErr) | ||
}) | ||
} | ||
|
||
func BenchmarkRotator(b *testing.B) { | ||
kb := 1024 | ||
for _, inputSize := range []int{kb, 2 * kb, 4 * kb, 8 * kb, 16 * kb, 32 * kb, 64 * kb, 128 * kb, 256 * kb} { | ||
b.Run(fmt.Sprintf("%dKB", inputSize/kb), func(b *testing.B) { | ||
benchmarkRotatorWithInputSize(inputSize, b) | ||
}) | ||
} | ||
} | ||
|
||
func benchmarkRotatorWithInputSize(size int, b *testing.B) { | ||
var path string | ||
var err error | ||
if path, err = ioutil.TempDir("", pathPrefix); err != nil { | ||
b.Fatalf("test setup err: %v", err) | ||
} | ||
defer os.RemoveAll(path) | ||
|
||
fr, err := NewFileRotator(path, baseFileName, 5, 1024*1024, logger) | ||
if err != nil { | ||
b.Fatalf("test setup err: %v", err) | ||
} | ||
b.ResetTimer() | ||
|
||
// run the Fib function b.N times | ||
for n := 0; n < b.N; n++ { | ||
// Generate some input | ||
data := make([]byte, size) | ||
_, err := rand.Read(data) | ||
if err != nil { | ||
b.Fatalf("Error generating date: %v", err) | ||
} | ||
|
||
// Insert random new lines | ||
for i := 0; i < 100; i++ { | ||
index := rand.Intn(size) | ||
data[index] = '\n' | ||
} | ||
|
||
// Write the data | ||
if _, err := fr.Write(data); err != nil { | ||
b.Fatalf("Failed to write data: %v", err) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this conditional work right if the first char in p is a new line, i.e idx=0?
IIUC it does work, It would write just the new line char to the buffer and then get back to the for loop.
Might want to make a unit test for such edge cases though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that is what the minus 1 is for. Since a write of p[n:n] actually writes 1 byte so I have to check if there is room for whatever index + 1