-
Notifications
You must be signed in to change notification settings - Fork 492
loki.source.docker: update to use shared scheduler #5026
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
kalleep
wants to merge
24
commits into
main
Choose a base branch
from
kalleep/loki-source-docker-scheduling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+890
−941
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5cadcfa
move scheduler to source package
kalleep f9ef577
Add ext interface and move info creation into sources
kalleep e032c44
loki.source.docker: refactor to use shared scheduler
kalleep 52f7376
Extract common function to drain handler
kalleep 48d3263
Add changelog
kalleep 37c4ad2
update comment
kalleep 9497325
only stop position once
kalleep f651b67
Add shared Fanout and Consume loop
kalleep 6b04acc
simplify unlock
kalleep 5d30796
Add note
kalleep b257447
fix
kalleep b1d1389
Update internal/component/loki/source/docker/docker.go
kalleep f376e13
Update internal/component/loki/source/docker/tailer.go
kalleep 906add6
Fix DebugSource
kalleep 3b35a6e
fix logic around docker container scheduling
kalleep 87fdfc5
Clenup position when target is removed
kalleep b7db8db
lint
kalleep bd07151
Add tests
kalleep 083a2a3
remove duplicated test
kalleep 06ed29e
Add reset
kalleep 7ebfcf8
Update internal/component/loki/source/docker/tailer.go
kalleep c15fc98
Update internal/component/loki/source/docker/tailer.go
kalleep 627a9cc
Check if channel was closed
kalleep 6a9d45c
Update changelog
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
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package loki | ||
|
|
||
| import ( | ||
| "context" | ||
| "reflect" | ||
| "sync" | ||
| ) | ||
|
|
||
| // NewFanout creates a new Fanout that will send log entries to the provided | ||
| // list of LogsReceivers. | ||
| func NewFanout(children []LogsReceiver) *Fanout { | ||
| return &Fanout{ | ||
| children: children, | ||
| } | ||
| } | ||
|
|
||
| // Fanout distributes log entries to multiple LogsReceivers. | ||
| // It is thread-safe and allows the list of receivers to be updated dynamically | ||
| type Fanout struct { | ||
| mut sync.RWMutex | ||
| children []LogsReceiver | ||
| } | ||
|
|
||
| // Send forwards a log entry to all registered receivers. It returns an error | ||
| // if the context is cancelled while sending. | ||
| func (f *Fanout) Send(ctx context.Context, entry Entry) error { | ||
| f.mut.RLock() | ||
| defer f.mut.RUnlock() | ||
| for _, recv := range f.children { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case recv.Chan() <- entry: | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // UpdateChildren updates the list of receivers that will receive log entries. | ||
| func (f *Fanout) UpdateChildren(children []LogsReceiver) { | ||
| f.mut.RLock() | ||
| if receiversChanged(f.children, children) { | ||
| // Upgrade lock to write. | ||
| f.mut.RUnlock() | ||
| f.mut.Lock() | ||
| f.children = children | ||
| f.mut.Unlock() | ||
| } else { | ||
| f.mut.RUnlock() | ||
| } | ||
| } | ||
|
|
||
| func receiversChanged(prev, next []LogsReceiver) bool { | ||
| if len(prev) != len(next) { | ||
| return true | ||
| } | ||
| for i := range prev { | ||
| if !reflect.DeepEqual(prev[i], next[i]) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
kalleep marked this conversation as resolved.
Show resolved
Hide 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package source | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/grafana/alloy/internal/component/common/loki" | ||
| ) | ||
|
|
||
| // Consume continuously reads log entries from recv and forwards them to the fanout f. | ||
| // It runs until ctx is cancelled or an error occurs while sending to the fanout. | ||
| // | ||
| // This function is typically used in component Run methods to handle the forwarding | ||
| // of log entries from a component's internal handler to downstream receivers. | ||
| // The fanout allows entries to be sent to multiple receivers concurrently. | ||
| func Consume(ctx context.Context, recv loki.LogsReceiver, f *loki.Fanout) { | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case entry := <-recv.Chan(): | ||
| // NOTE: the only error we can get is context.Canceled. | ||
| if err := f.Send(ctx, entry); err != nil { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
kalleep marked this conversation as resolved.
Show resolved
Hide 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package source | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/grafana/loki/pkg/push" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/grafana/alloy/internal/component/common/loki" | ||
| ) | ||
|
|
||
| func TestConsume(t *testing.T) { | ||
| consumer := loki.NewLogsReceiver() | ||
| producer := loki.NewLogsReceiver() | ||
| fanout := loki.NewFanout([]loki.LogsReceiver{consumer}) | ||
|
|
||
| t.Run("should fanout any consumed entries", func(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
|
|
||
| wg := sync.WaitGroup{} | ||
| wg.Go(func() { | ||
| Consume(ctx, producer, fanout) | ||
| }) | ||
|
|
||
| producer.Chan() <- loki.Entry{Entry: push.Entry{Line: "1"}} | ||
| e := <-consumer.Chan() | ||
| require.Equal(t, "1", e.Entry.Line) | ||
| cancel() | ||
| wg.Wait() | ||
| }) | ||
|
|
||
| t.Run("should stop if context is canceled while trying to fanout", func(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| wg := sync.WaitGroup{} | ||
| wg.Go(func() { | ||
| Consume(ctx, producer, fanout) | ||
| }) | ||
|
|
||
| producer.Chan() <- loki.Entry{Entry: push.Entry{Line: "1"}} | ||
| cancel() | ||
| wg.Wait() | ||
| }) | ||
| } |
Oops, something went wrong.
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.
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.
Is it possible that we will be stuck on this
recv.Chan() <- entrybecause the receiver is too busy or dead, which would mean we are holding on tof.mut.RLock()and thus we are blocking all the other operations such asUpdateChildren?I'm a bit unsure about correctness of this.
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.
This is what we where doing in
loki.source.fileand have not seen any problems there but we could test it out.