Skip to content
Merged
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
[receiver/fluentforward] Ensure all established connections are prope…
…rly closed on shutdown

Fixes shutdown behavior so that all existing connections are closed cleanly.
Adds tests to verify proper connection closure.

Fixes #44433
  • Loading branch information
tokuhirom committed Dec 24, 2025
commit de2d83eea18c196d0e3b4a7ba91e773e7f0d8acd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
component: receiver/fluentforward


# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Ensure all established connections are properly closed on shutdown in the fluentforward receiver. The shutdown process now reliably closes all active connections.


# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [44433]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
- Fixes shutdown behavior so that all existing connections are closed cleanly.
- Adds tests to verify proper connection closure.

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
4 changes: 3 additions & 1 deletion receiver/fluentforwardreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ func (r *fluentReceiver) Shutdown(context.Context) error {
if r.listener == nil {
return nil
}
r.listener.Close()
// Close the listener to stop accepting new connections
_ = r.listener.Close()
r.server.closeAllConns()
r.cancel()
return nil
}
74 changes: 65 additions & 9 deletions receiver/fluentforwardreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
Expand All @@ -28,7 +29,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/fluentforwardreceiver/internal/metadata"
)

func setupServer(t *testing.T) (func() net.Conn, *consumertest.LogsSink, *observer.ObservedLogs, context.CancelFunc) {
func setupServer(t *testing.T) (func() net.Conn, *consumertest.LogsSink, *observer.ObservedLogs, context.CancelFunc, receiver.Logs) {
ctx, cancel := context.WithCancel(t.Context())

next := new(consumertest.LogsSink)
Expand Down Expand Up @@ -57,7 +58,7 @@ func setupServer(t *testing.T) (func() net.Conn, *consumertest.LogsSink, *observ
assert.NoError(t, receiver.Shutdown(ctx))
}()

return connect, next, logObserver, cancel
return connect, next, logObserver, cancel, receiver
}

func waitForConnectionClose(t *testing.T, conn net.Conn) {
Expand All @@ -71,7 +72,7 @@ func waitForConnectionClose(t *testing.T, conn net.Conn) {

// Make sure malformed events don't cause panics.
func TestMessageEventConversionMalformed(t *testing.T) {
connect, _, observedLogs, cancel := setupServer(t)
connect, _, observedLogs, cancel, _ := setupServer(t)
defer cancel()

eventBytes := parseHexDump("testdata/message-event")
Expand All @@ -94,7 +95,7 @@ func TestMessageEventConversionMalformed(t *testing.T) {
}

func TestMessageEvent(t *testing.T) {
connect, next, _, cancel := setupServer(t)
connect, next, _, cancel, _ := setupServer(t)
defer cancel()

eventBytes := parseHexDump("testdata/message-event")
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestMessageEvent(t *testing.T) {
}

func TestForwardEvent(t *testing.T) {
connect, next, _, cancel := setupServer(t)
connect, next, _, cancel, _ := setupServer(t)
defer cancel()

eventBytes := parseHexDump("testdata/forward-event")
Expand Down Expand Up @@ -172,7 +173,7 @@ func TestForwardEvent(t *testing.T) {
}

func TestEventAcknowledgment(t *testing.T) {
connect, _, logs, cancel := setupServer(t)
connect, _, logs, cancel, _ := setupServer(t)
defer func() { fmt.Printf("%v\n", logs.All()) }()
defer cancel()

Expand Down Expand Up @@ -203,7 +204,7 @@ func TestEventAcknowledgment(t *testing.T) {
}

func TestForwardPackedEvent(t *testing.T) {
connect, next, _, cancel := setupServer(t)
connect, next, _, cancel, _ := setupServer(t)
defer cancel()

eventBytes := parseHexDump("testdata/forward-packed")
Expand Down Expand Up @@ -265,7 +266,7 @@ func TestForwardPackedEvent(t *testing.T) {
}

func TestForwardPackedCompressedEvent(t *testing.T) {
connect, next, _, cancel := setupServer(t)
connect, next, _, cancel, _ := setupServer(t)
defer cancel()

eventBytes := parseHexDump("testdata/forward-packed-compressed")
Expand Down Expand Up @@ -370,7 +371,7 @@ func makeSampleEvent(tag string) []byte {
}

func TestHighVolume(t *testing.T) {
connect, next, _, cancel := setupServer(t)
connect, next, _, cancel, _ := setupServer(t)
defer cancel()

const totalRoutines = 8
Expand Down Expand Up @@ -406,3 +407,58 @@ func TestHighVolume(t *testing.T) {
return total == totalRoutines*totalMessagesPerRoutine
}, 10*time.Second, 100*time.Millisecond)
}

// TestReceiverShutdownRefusesNewConnections verifies the graceful shutdown of a receiver, ensuring no new connections can be established afterward.
func TestReceiverShutdownRefusesNewConnections(t *testing.T) {
connect, next, _, cancel, receiver := setupServer(t)
defer cancel()

eventBytes := parseHexDump("testdata/message-event")

conn := connect()
n, err := conn.Write(eventBytes)
require.NoError(t, err)
require.Equal(t, len(eventBytes), n)
require.NoError(t, conn.Close())

var converted []plog.Logs
require.Eventually(t, func() bool {
converted = next.AllLogs()
return len(converted) == 1
}, 5*time.Second, 10*time.Millisecond)

// shutdown the receiver
require.NoError(t, receiver.Shutdown(t.Context()))

// New connection will be refused
_, err = net.Dial("tcp", receiver.(*fluentReceiver).listener.Addr().String())
require.Error(t, err)
}

// TestReceiverShutdownClosesExistingConnections verifies the graceful shutdown of a receiver, ensuring existing connections will be closed.
func TestReceiverShutdownClosesExistingConnections(t *testing.T) {
connect, next, _, cancel, receiver := setupServer(t)
defer cancel()

eventBytes := parseHexDump("testdata/message-event")

conn := connect()
n, err := conn.Write(eventBytes)
require.NoError(t, err)
require.Equal(t, len(eventBytes), n)

var converted []plog.Logs
require.Eventually(t, func() bool {
converted = next.AllLogs()
return len(converted) == 1
}, 5*time.Second, 10*time.Millisecond)

// shutdown the receiver
require.NoError(t, receiver.Shutdown(t.Context()))

// Existing connection will be refused
require.Eventually(t, func() bool {
_, err := conn.Write(eventBytes)
return err != nil
}, 5*time.Second, 1*time.Second)
}
26 changes: 26 additions & 0 deletions receiver/fluentforwardreceiver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"net"
"sync"
"time"

"github.com/tinylib/msgp/msgp"
Expand All @@ -27,13 +28,16 @@ type server struct {
outCh chan<- event
logger *zap.Logger
telemetryBuilder *metadata.TelemetryBuilder
conns map[net.Conn]struct{}
mu sync.Mutex
}

func newServer(outCh chan<- event, logger *zap.Logger, telemetryBuilder *metadata.TelemetryBuilder) *server {
return &server{
outCh: outCh,
logger: logger,
telemetryBuilder: telemetryBuilder,
conns: make(map[net.Conn]struct{}),
}
}

Expand Down Expand Up @@ -69,9 +73,11 @@ func (s *server) handleConnections(ctx context.Context, listener net.Listener) {
s.telemetryBuilder.FluentOpenedConnections.Add(ctx, 1)

s.logger.Debug("Got connection", zap.String("remoteAddr", conn.RemoteAddr().String()))
s.addConn(conn)

go func() {
defer s.telemetryBuilder.FluentClosedConnections.Add(ctx, 1)
defer s.removeConn(conn)

err := s.handleConn(ctx, conn)
if err != nil {
Expand Down Expand Up @@ -203,3 +209,23 @@ func determineNextEventMode(peeker peeker) (eventMode, error) {
return unknownMode, fmt.Errorf("unable to determine next event mode for type %v", secondElmType)
}
}

func (s *server) addConn(c net.Conn) {
s.mu.Lock()
s.conns[c] = struct{}{}
s.mu.Unlock()
}

func (s *server) removeConn(c net.Conn) {
s.mu.Lock()
delete(s.conns, c)
s.mu.Unlock()
}

func (s *server) closeAllConns() {
s.mu.Lock()
for c := range s.conns {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe clear the map too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need, the connections will exit and delete from the map.

_ = c.Close() // Ignore errors
}
s.mu.Unlock()
}