Skip to content

Commit 9b52bb8

Browse files
committed
Fix healthcheck and retries
This commit fixes #1179 in that it still executes a request even if all nodes are dead and retries are disabled. Also, this commit re-enables adding healthchecks if configured in the config options. They were commented out in an earlier commit by mistake. See #1179
1 parent 5aa44b1 commit 9b52bb8

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

CONTRIBUTORS

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Josh Chorlton [@jchorl](https://github.com/jchorl)
114114
Jpnock [@Jpnock](https://github.com/Jpnock)
115115
jun [@coseyo](https://github.com/coseyo)
116116
Junpei Tsuji [@jun06t](https://github.com/jun06t)
117+
Karen Yang [@kyangtt](https://github.com/kyangtt)
117118
kartlee [@kartlee](https://github.com/kartlee)
118119
Keith Hatton [@khatton-ft](https://github.com/khatton-ft)
119120
kel [@liketic](https://github.com/liketic)

client.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,9 @@ func configToOptions(cfg *config.Config) ([]ClientOptionFunc, error) {
465465
if cfg.Sniff != nil {
466466
options = append(options, SetSniff(*cfg.Sniff))
467467
}
468-
/*
469-
if cfg.Healthcheck != nil {
470-
options = append(options, SetHealthcheck(*cfg.Healthcheck))
471-
}
472-
*/
468+
if cfg.Healthcheck != nil {
469+
options = append(options, SetHealthcheck(*cfg.Healthcheck))
470+
}
473471
}
474472
return options, nil
475473
}
@@ -1275,6 +1273,7 @@ func (c *Client) PerformRequest(ctx context.Context, opt PerformRequestOptions)
12751273
basicAuthPassword := c.basicAuthPassword
12761274
sendGetBodyAs := c.sendGetBodyAs
12771275
gzipEnabled := c.gzipEnabled
1276+
healthcheckEnabled := c.healthcheckEnabled
12781277
retrier := c.retrier
12791278
if opt.Retrier != nil {
12801279
retrier = opt.Retrier
@@ -1307,6 +1306,10 @@ func (c *Client) PerformRequest(ctx context.Context, opt PerformRequestOptions)
13071306
if !retried {
13081307
// Force a healtcheck as all connections seem to be dead.
13091308
c.healthcheck(ctx, timeout, false)
1309+
if healthcheckEnabled {
1310+
retried = true
1311+
continue
1312+
}
13101313
}
13111314
wait, ok, rerr := retrier.Retry(ctx, n, nil, nil, err)
13121315
if rerr != nil {

client_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,42 @@ func TestPerformRequestWithMaxResponseSize(t *testing.T) {
12961296
}
12971297
}
12981298

1299+
func TestPerformRequestOnNoConnectionsWithHealthcheckRevival(t *testing.T) {
1300+
fail := func(r *http.Request) (*http.Response, error) {
1301+
return nil, errors.New("request failed")
1302+
}
1303+
tr := &failingTransport{path: "/fail", fail: fail}
1304+
httpClient := &http.Client{Transport: tr}
1305+
client, err := NewClient(SetHttpClient(httpClient), SetMaxRetries(0), SetHealthcheck(true))
1306+
if err != nil {
1307+
t.Fatal(err)
1308+
}
1309+
1310+
// Run against a failing endpoint to mark connection as dead
1311+
res, err := client.PerformRequest(context.TODO(), PerformRequestOptions{
1312+
Method: "GET",
1313+
Path: "/fail",
1314+
})
1315+
if err == nil {
1316+
t.Fatal(err)
1317+
}
1318+
if res != nil {
1319+
t.Fatal("expected no response")
1320+
}
1321+
1322+
// Forced healthcheck should bring connection back to life and complete request
1323+
res, err = client.PerformRequest(context.TODO(), PerformRequestOptions{
1324+
Method: "GET",
1325+
Path: "/",
1326+
})
1327+
if err != nil {
1328+
t.Fatal(err)
1329+
}
1330+
if res == nil {
1331+
t.Fatal("expected response to be != nil")
1332+
}
1333+
}
1334+
12991335
// failingTransport will run a fail callback if it sees a given URL path prefix.
13001336
type failingTransport struct {
13011337
path string // path prefix to look for

docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3'
22

33
services:
44
elasticsearch:
5-
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.5.0
5+
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.5.1
66
hostname: elasticsearch
77
environment:
88
- cluster.name=elasticsearch
@@ -27,7 +27,7 @@ services:
2727
ports:
2828
- 9200:9200
2929
platinum:
30-
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
30+
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.1
3131
hostname: elasticsearch-platinum
3232
environment:
3333
- cluster.name=platinum

0 commit comments

Comments
 (0)