-
Notifications
You must be signed in to change notification settings - Fork 597
fix(loki): Make drain forward entries with fallback timeout #5830
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df01429
fix(loki): Make drain forward entries with fallback timeout
kalleep 31558aa
Potential fix for pull request finding
kalleep d3b29c9
Update internal/component/common/loki/drain.go
kalleep d76b2ad
add leak detection and call cancel in defer
kalleep 9657ebd
Add test to make sure we forward one entry and discard the rest
kalleep 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 hidden or 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
|
kalleep marked this conversation as resolved.
|
This file contains hidden or 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 |
|---|---|---|
| @@ -1,38 +1,113 @@ | ||
| package loki | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "sync" | ||
| "testing" | ||
| "testing/synctest" | ||
| "time" | ||
|
|
||
| "github.com/grafana/loki/pkg/push" | ||
| "github.com/prometheus/common/model" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/goleak" | ||
| ) | ||
|
|
||
| func TestDrain(t *testing.T) { | ||
| recv := NewLogsReceiver() | ||
| defer goleak.VerifyNone(t) | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Go(func() { | ||
| for range 10 { | ||
| entry := Entry{ | ||
| t.Run("forwards while fn runs", func(t *testing.T) { | ||
|
kalleep marked this conversation as resolved.
|
||
| recv := NewLogsReceiver() | ||
| collector := NewCollectingHandler() | ||
| defer collector.Stop() | ||
|
|
||
| var producer sync.WaitGroup | ||
| producer.Go(func() { | ||
| recv.Chan() <- Entry{ | ||
| Labels: model.LabelSet{"test": "label"}, | ||
| Entry: push.Entry{ | ||
| Timestamp: time.Now(), | ||
| Line: "test log entry", | ||
| Line: "forwarded", | ||
| }, | ||
| } | ||
| recv.Chan() <- entry | ||
| } | ||
| }) | ||
|
|
||
| Drain(recv, NewFanout([]LogsReceiver{collector.Receiver()}), time.Second, func() { | ||
| require.Eventually(t, func() bool { | ||
| return len(collector.Received()) == 1 | ||
| }, time.Second, 10*time.Millisecond) | ||
| }) | ||
|
|
||
| producer.Wait() | ||
| require.Len(t, collector.Received(), 1) | ||
| require.Equal(t, "forwarded", collector.Received()[0].Line) | ||
| }) | ||
|
|
||
| completed := false | ||
| Drain(recv, func() { | ||
| time.Sleep(100 * time.Millisecond) | ||
| completed = true | ||
| t.Run("falls back to discard when forwarding blocks", func(t *testing.T) { | ||
| recv := NewLogsReceiver() | ||
| blockedRecv := NewLogsReceiver() | ||
|
|
||
| var producer sync.WaitGroup | ||
| producer.Go(func() { | ||
| for range 2 { | ||
| recv.Chan() <- Entry{ | ||
| Labels: model.LabelSet{"test": "label"}, | ||
| Entry: push.Entry{ | ||
| Timestamp: time.Now(), | ||
| Line: "blocked", | ||
| }, | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| Drain(recv, NewFanout([]LogsReceiver{blockedRecv}), 20*time.Millisecond, func() { | ||
| time.Sleep(100 * time.Millisecond) | ||
| }) | ||
|
|
||
| producer.Wait() | ||
| }) | ||
|
|
||
| wg.Wait() | ||
| require.True(t, completed, "Drain should complete without deadlock") | ||
| t.Run("forwards one entry and discard rest", func(t *testing.T) { | ||
| synctest.Test(t, func(t *testing.T) { | ||
| recv := NewLogsReceiver() | ||
| // Use a buffered channel so the first entry can always be forwarded to the fanout. | ||
| consumer := NewLogsReceiver(WithChannel(make(chan Entry, 1))) | ||
|
|
||
| var producerWG sync.WaitGroup | ||
| producerWG.Go(func() { | ||
| for i := range 3 { | ||
| recv.Chan() <- Entry{ | ||
| Entry: push.Entry{ | ||
| Timestamp: time.Now(), | ||
| Line: strconv.Itoa(i), | ||
| }, | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Go(func() { | ||
| Drain(recv, NewFanout([]LogsReceiver{consumer}), 100*time.Millisecond, func() { | ||
| // Wait until the producer has finished sending all entries. | ||
| producerWG.Wait() | ||
| }) | ||
| }) | ||
|
|
||
| // Wait until all go routines are blocked and advance time. | ||
| synctest.Wait() | ||
| time.Sleep(101 * time.Millisecond) | ||
| wg.Wait() | ||
|
|
||
| // Make sure we only get the first entry. | ||
| entry := <-consumer.Chan() | ||
| require.Equal(t, "0", entry.Line) | ||
| synctest.Wait() | ||
|
|
||
| select { | ||
| case extra := <-consumer.Chan(): | ||
| t.Fatalf("unexpected extra forwarded entry: %q", extra.Line) | ||
| default: | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.