-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix elasticsearch re-connection after network error
When the Elasticsearch client fails to publish events, it ends up calling `Close` in the connection (that is reused). To cancel the in-flight requests, the context is cancelled and a new one is created to used in future requests. The callback to check the version holds a reference to the connection via a closure, now the Elasticsearch client holds a pointer to that connection, so whenever Close is called, the callback can create a request with the new, not cancelled, context. An integration test is added to ensure the ES output can always recover from network errors.
- Loading branch information
Showing
7 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -16057,6 +16057,29 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected]/LI | |
limitations under the License. | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/elastic/mock-es | ||
Version: v0.0.0-20240712014503-e5b47ece0015 | ||
Licence type (autodetected): Apache-2.0 | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/github.com/elastic/[email protected]/LICENSE: | ||
|
||
Copyright 2024 Elasticsearch B.V. | ||
|
||
Licensed 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. | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/elastic/tk-btf | ||
Version: v0.1.0 | ||
|
@@ -47725,6 +47748,36 @@ Contents of probable licence file $GOMODCACHE/github.com/matttproud/golang_proto | |
limitations under the License. | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/mileusna/useragent | ||
Version: v1.3.4 | ||
Licence type (autodetected): MIT | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/github.com/mileusna/[email protected]/LICENSE.md: | ||
|
||
MIT License | ||
|
||
Copyright (c) 2017 Miloš Mileusnić | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/minio/asm2plan9s | ||
Version: v0.0.0-20200509001527-cdd76441f9d8 | ||
|
This file contains 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 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 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 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 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 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,142 @@ | ||
// 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. | ||
|
||
//go:build integration | ||
|
||
package integration | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gofrs/uuid/v5" | ||
"github.com/rcrowley/go-metrics" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/mock-es/pkg/api" | ||
) | ||
|
||
var esCfg = ` | ||
mockbeat: | ||
logging: | ||
level: debug | ||
selectors: | ||
- publisher_pipeline_output | ||
- esclientleg | ||
queue.mem: | ||
events: 4096 | ||
flush.min_events: 8 | ||
flush.timeout: 0.1s | ||
output.elasticsearch: | ||
allow_older_versions: true | ||
hosts: | ||
- "http://localhost:4242" | ||
backoff: | ||
init: 0.1s | ||
max: 0.2s | ||
` | ||
|
||
func TestESOutputRecoversFromNetworkError(t *testing.T) { | ||
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test") | ||
mockbeat.WriteConfigFile(esCfg) | ||
|
||
s, mr := startMockES(t, "localhost:4242") | ||
|
||
mockbeat.Start() | ||
|
||
// 1. Wait for one _bulk call | ||
waitForEventToBePublished(t, mr) | ||
|
||
// 2. Stop the mock-es server | ||
if err := s.Close(); err != nil { | ||
t.Fatalf("cannot close mock-es server: %s", err) | ||
} | ||
|
||
// 3. Wait for connection error logs | ||
mockbeat.WaitForLogs( | ||
`Get \"http://localhost:4242\": dial tcp 127.0.0.1:4242: connect: connection refused`, | ||
2*time.Second, | ||
"did not find connection refused error") | ||
|
||
mockbeat.WaitForLogs( | ||
"Attempting to reconnect to backoff(elasticsearch(http://localhost:4242)) with 2 reconnect attempt(s)", | ||
2*time.Second, | ||
"did not find two tries to reconnect") | ||
|
||
// 4. Restart mock-es on the same port | ||
s, mr = startMockES(t, "localhost:4242") | ||
|
||
// 5. Wait for reconnection logs | ||
mockbeat.WaitForLogs( | ||
"Connection to backoff(elasticsearch(http://localhost:4242)) established", | ||
5*time.Second, // There is a backoff, so ensure we wait enough | ||
"did not find re connection confirmation") | ||
|
||
// 6. Ensure one new call to _bulk is made | ||
waitForEventToBePublished(t, mr) | ||
s.Close() | ||
} | ||
|
||
func startMockES(t *testing.T, addr string) (*http.Server, metrics.Registry) { | ||
uid := uuid.Must(uuid.NewV4()) | ||
mr := metrics.NewRegistry() | ||
es := api.NewAPIHandler(uid, "foo2", mr, time.Now().Add(24*time.Hour), 0, 0, 0, 0, 0) | ||
|
||
s := http.Server{Addr: addr, Handler: es} | ||
go func() { | ||
if err := s.ListenAndServe(); !errors.Is(http.ErrServerClosed, err) { | ||
t.Errorf("could not start mock-es server: %s", err) | ||
} | ||
}() | ||
|
||
require.Eventually(t, func() bool { | ||
resp, err := http.Get("http://" + addr) | ||
if err != nil { | ||
io.Copy(io.Discard, resp.Body) | ||
resp.Body.Close() | ||
return false | ||
} | ||
return true | ||
}, | ||
time.Second, time.Millisecond, "first server must be up") | ||
|
||
return &s, mr | ||
} | ||
|
||
func waitForEventToBePublished(t *testing.T, mr metrics.Registry) { | ||
t.Helper() | ||
require.Eventually(t, func() bool { | ||
total := mr.Get("bulk.create.total") | ||
if total == nil { | ||
return false | ||
} | ||
|
||
sc, ok := total.(*metrics.StandardCounter) | ||
if !ok { | ||
t.Fatalf("expecting 'bulk.create.total' to be *metrics.StandardCounter, but got '%T' instead", | ||
total, | ||
) | ||
} | ||
|
||
return sc.Count() > 1 | ||
}, | ||
10*time.Second, 100*time.Millisecond, | ||
"at least one bulk request must be made") | ||
} |