Skip to content
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

[7.17](backport #41960) Add upstream deadlock warning to the logstash output #42109

Open
wants to merge 2 commits into
base: 7.17
Choose a base branch
from
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
55 changes: 32 additions & 23 deletions libbeat/outputs/logstash/async.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to Elasticsearch B.V. under one or more contributor

Check failure on line 1 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (windows)

: # github.com/elastic/beats/v7/libbeat/outputs/logstash [github.com/elastic/beats/v7/libbeat/outputs/logstash.test]

Check failure on line 1 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (linux)

: # github.com/elastic/beats/v7/libbeat/outputs/logstash [github.com/elastic/beats/v7/libbeat/outputs/logstash.test]

Check failure on line 1 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

: # github.com/elastic/beats/v7/libbeat/outputs/logstash [github.com/elastic/beats/v7/libbeat/outputs/logstash.test]
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
Expand Down Expand Up @@ -46,13 +46,14 @@
}

type msgRef struct {
client *asyncClient
count atomic.Uint32
batch publisher.Batch
slice []publisher.Event
err error
win *window
batchSize int
client *asyncClient
count atomic.Uint32
batch publisher.Batch
slice []publisher.Event
err error
win *window
batchSize int
deadlockListener *deadlockListener
}

func newAsyncClient(
Expand Down Expand Up @@ -146,13 +147,13 @@
}

ref := &msgRef{
client: c,
count: atomic.MakeUint32(1),
batch: batch,
slice: events,
batchSize: len(events),
win: c.win,
err: nil,
client: c,
batch: batch,
slice: events,
batchSize: len(events),
win: c.win,
err: nil,
deadlockListener: newDeadlockListener(c.log, logstashDeadlockTimeout),
}
defer ref.dec()

Expand Down Expand Up @@ -229,6 +230,7 @@
return client
}

<<<<<<< HEAD

Check failure on line 233 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (windows)

syntax error: non-declaration statement outside function body

Check failure on line 233 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (windows)

expected declaration, found '<<' (typecheck)

Check failure on line 233 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (linux)

syntax error: non-declaration statement outside function body

Check failure on line 233 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (linux)

expected declaration, found '<<' (typecheck)

Check failure on line 233 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

syntax error: non-declaration statement outside function body

Check failure on line 233 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

expected declaration, found '<<' (typecheck)
func (r *msgRef) callback(seq uint32, err error) {
if err != nil {
r.fail(seq, err)
Expand All @@ -239,24 +241,29 @@

func (r *msgRef) done(n uint32) {
r.client.observer.Acked(int(n))
=======

Check failure on line 244 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (windows)

syntax error: unexpected ==, expected }

Check failure on line 244 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (linux)

syntax error: unexpected ==, expected }

Check failure on line 244 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

syntax error: unexpected ==, expected }
func (r *msgRef) callback(n uint32, err error) {
r.client.observer.AckedEvents(int(n))
>>>>>>> 0e62bf8f0 (Add upstream deadlock warning to the logstash output (#41960))

Check failure on line 247 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (windows)

syntax error: unexpected >>, expected }

Check failure on line 247 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (windows)

invalid character U+0023 '#'

Check failure on line 247 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (linux)

syntax error: unexpected >>, expected }

Check failure on line 247 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (linux)

invalid character U+0023 '#'

Check failure on line 247 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

syntax error: unexpected >>, expected }

Check failure on line 247 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

invalid character U+0023 '#'
r.slice = r.slice[n:]
if r.win != nil {
r.win.tryGrowWindow(r.batchSize)
}
r.dec()
}

func (r *msgRef) fail(n uint32, err error) {
r.deadlockListener.ack(int(n))
if r.err == nil {
r.err = err
}
r.slice = r.slice[n:]
// If publishing is windowed, update the window size.
if r.win != nil {
r.win.shrinkWindow()
if err != nil {
r.win.shrinkWindow()
} else {
r.win.tryGrowWindow(r.batchSize)
}
}
<<<<<<< HEAD

r.client.observer.Acked(int(n))

=======
>>>>>>> 0e62bf8f0 (Add upstream deadlock warning to the logstash output (#41960))

Check failure on line 266 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (windows)

invalid character U+0023 '#'

Check failure on line 266 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (linux)

invalid character U+0023 '#'

Check failure on line 266 in libbeat/outputs/logstash/async.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

invalid character U+0023 '#'
r.dec()
}

Expand All @@ -266,6 +273,8 @@
return
}

r.deadlockListener.close()

if L := len(r.slice); L > 0 {
r.client.observer.Failed(L)
}
Expand Down
95 changes: 95 additions & 0 deletions libbeat/outputs/logstash/deadlock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package logstash

import (
"time"

"github.com/elastic/elastic-agent-libs/logp"
)

type deadlockListener struct {
log *logp.Logger
timeout time.Duration
ticker *time.Ticker

ackChan chan int

doneChan chan struct{}
}

const logstashDeadlockTimeout = 5 * time.Minute

func newDeadlockListener(log *logp.Logger, timeout time.Duration) *deadlockListener {
if timeout <= 0 {
return nil
}
r := &deadlockListener{
log: log,
timeout: timeout,
ticker: time.NewTicker(timeout),

ackChan: make(chan int),
doneChan: make(chan struct{}),
}
go r.run()
return r
}

func (r *deadlockListener) run() {
defer r.ticker.Stop()
defer close(r.doneChan)
for {
select {
case n, ok := <-r.ackChan:
if !ok {
// Listener has been closed
return
}
if n > 0 {
// If progress was made, reset the countdown.
r.ticker.Reset(r.timeout)
}
case <-r.ticker.C:
// No progress was made within the timeout, log error so users
// know there is likely a problem with the upstream host
r.log.Errorf("Logstash batch hasn't reported progress in the last %v, the Logstash host may be stalled. This problem can be prevented by configuring Logstash to use PipelineBusV1 or by upgrading Logstash to 8.17+, for details see https://github.com/elastic/logstash/issues/16657", r.timeout)
return
}
}
}

func (r *deadlockListener) ack(n int) {
if r == nil {
return
}
// Send the new ack to the run loop, unless it has already shut down in
// which case it can be safely ignored.
select {
case r.ackChan <- n:
case <-r.doneChan:
}
}

func (r *deadlockListener) close() {
if r == nil {
return
}
// Signal the run loop to shut down
close(r.ackChan)
}
51 changes: 51 additions & 0 deletions libbeat/outputs/logstash/deadlock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package logstash

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-libs/logp"
)

func TestDeadlockListener(t *testing.T) {
const timeout = 5 * time.Millisecond
log := logp.NewLogger("test")
listener := newDeadlockListener(log, timeout)

// Verify that the listener doesn't trigger when receiving regular acks
for i := 0; i < 5; i++ {
time.Sleep(timeout / 2)
listener.ack(1)
}
select {
case <-listener.doneChan:
require.Fail(t, "Deadlock listener should not trigger unless there is no progress for the configured time interval")
case <-time.After(timeout / 2):
}

// Verify that the listener does trigger when the acks stop
select {
case <-time.After(timeout):
require.Fail(t, "Deadlock listener should trigger when there is no progress for the configured time interval")
case <-listener.doneChan:
}
}
11 changes: 8 additions & 3 deletions libbeat/outputs/logstash/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
return nil
}

deadlockListener := newDeadlockListener(c.log, logstashDeadlockTimeout)
defer deadlockListener.close()
for len(events) > 0 {
// check if we need to reconnect
if c.ticker != nil {
Expand Down Expand Up @@ -146,14 +148,16 @@
n, len(events), c.Host())

events = events[n:]
<<<<<<< HEAD

Check failure on line 151 in libbeat/outputs/logstash/sync.go

View workflow job for this annotation

GitHub Actions / lint (windows)

syntax error: unexpected <<, expected }

Check failure on line 151 in libbeat/outputs/logstash/sync.go

View workflow job for this annotation

GitHub Actions / lint (linux)

syntax error: unexpected <<, expected }

Check failure on line 151 in libbeat/outputs/logstash/sync.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

syntax error: unexpected <<, expected }
st.Acked(n)
=======

Check failure on line 153 in libbeat/outputs/logstash/sync.go

View workflow job for this annotation

GitHub Actions / lint (windows)

syntax error: unexpected ==, expected }

Check failure on line 153 in libbeat/outputs/logstash/sync.go

View workflow job for this annotation

GitHub Actions / lint (linux)

syntax error: unexpected ==, expected }

Check failure on line 153 in libbeat/outputs/logstash/sync.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

syntax error: unexpected ==, expected }
st.AckedEvents(n)
deadlockListener.ack(n)
>>>>>>> 0e62bf8f0 (Add upstream deadlock warning to the logstash output (#41960))

Check failure on line 156 in libbeat/outputs/logstash/sync.go

View workflow job for this annotation

GitHub Actions / lint (windows)

invalid character U+0023 '#' (typecheck)

Check failure on line 156 in libbeat/outputs/logstash/sync.go

View workflow job for this annotation

GitHub Actions / lint (linux)

invalid character U+0023 '#' (typecheck)

Check failure on line 156 in libbeat/outputs/logstash/sync.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

invalid character U+0023 '#' (typecheck)
if err != nil {
// return batch to pipeline before reporting/counting error
batch.RetryEvents(events)

if c.win != nil {
c.win.shrinkWindow()
}
_ = c.Close()

c.log.Errorf("Failed to publish events caused by: %+v", err)
Expand Down Expand Up @@ -182,6 +186,7 @@

n, err := c.sendEvents(events)
if err != nil {
c.win.shrinkWindow()
return n, err
}

Expand Down
Loading