Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Main (unreleased)

- Remove extraneous `output` stage from the `cri` stage pipeline in `loki.process`. (@kalleep)

- Fix issue in `loki.source.docker` where scheduling containers to tail could take too long, this would increase the chance of Alloy missing shortlived jobs. (@kalleep)

- Fix potential deadlock in `loki.source.docker` when component is shutting down. (@kalleep)

v1.12.0
-----------------

Expand Down
63 changes: 63 additions & 0 deletions internal/component/common/loki/fanout.go
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:
Copy link
Contributor

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() <- entry because the receiver is too busy or dead, which would mean we are holding on to f.mut.RLock() and thus we are blocking all the other operations such as UpdateChildren?

I'm a bit unsure about correctness of this.

Copy link
Contributor Author

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.file and have not seen any problems there but we could test it out.

}
}
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
}
27 changes: 27 additions & 0 deletions internal/component/loki/source/consume.go
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
}
}
}
}
45 changes: 45 additions & 0 deletions internal/component/loki/source/consume_test.go
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()
})
}
Loading
Loading