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

[fix] Close Elasticsearch client properly #4754

Merged
merged 7 commits into from
Sep 12, 2023

Conversation

Lauquik
Copy link
Contributor

@Lauquik Lauquik commented Sep 12, 2023

Which problem is this PR solving?

Description of the changes

  • Resolved an issue where the Elasticsearch client was not being closed correctly.

How was this change tested?

  • manually on local env

Checklist

Signed-off-by: Lauquik <laukik.22010776@viit.ac.in>
Signed-off-by: Lauquik <laukik.22010776@viit.ac.in>
@Lauquik Lauquik requested a review from a team as a code owner September 12, 2023 14:31
@@ -360,7 +361,14 @@ func TestPasswordFromFileErrors(t *testing.T) {

logger, buf := testutils.NewEchoLogger(t)
require.NoError(t, f.Initialize(metrics.NullFactory, logger))
defer f.Close()
Copy link
Member

Choose a reason for hiding this comment

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

why do we need to do manual closing?

Copy link
Contributor Author

@Lauquik Lauquik Sep 12, 2023

Choose a reason for hiding this comment

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

when i tested it, this was the error that i encountered

--- FAIL: TestPasswordFromFileErrors (0.05s)
    logger.go:130: 2023-09-12T22:33:23.038+0530 INFO    Elasticsearch detected  {"version": 6}
    logger.go:130: 2023-09-12T22:33:23.038+0530 INFO    loaded new password of length 14 from file
    logger.go:130: 2023-09-12T22:33:23.038+0530 ERROR   failed to recreate Elasticsearch client with new password       {"error": "no servers specified"}
    logger.go:130: 2023-09-12T22:33:23.038+0530 INFO    loaded new password of length 14 from file
    logger.go:130: 2023-09-12T22:33:23.038+0530 ERROR   failed to recreate Elasticsearch client with new password       {"error": "no servers specified"}
    logger.go:130: 2023-09-12T22:33:23.038+0530 ERROR   failed to reload password for Elasticsearch client      {"error": "open /tmp/TestPasswordFromFileErrors2482467054/001/pwd: no such file or directory"}
    logger.go:130: 2023-09-12T22:33:23.038+0530 ERROR   failed to reload password for Elasticsearch client      {"error": "open /tmp/TestPasswordFromFileErrors2482467054/001/pwd: no such file or directory"}
    logger.go:130: 2023-09-12T22:33:23.038+0530 INFO    Received event  {"event": "REMOVE        \"/tmp/TestPasswordFromFileErrors2482467054/001/pwd\""}
    logger.go:130: 2023-09-12T22:33:23.038+0530 WARN    Unable to read the file {"file": "/tmp/TestPasswordFromFileErrors2482467054/001/pwd", "error": "open /tmp/TestPasswordFromFileErrors2482467054/001/pwd: no such file or directory"}
    logger.go:130: 2023-09-12T22:33:23.038+0530 ERROR   failed to reload password for Elasticsearch client      {"error": "open /tmp/TestPasswordFromFileErrors2482467054/001/pwd: no such file or directory"}
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xc88bd5]

goroutine 120 [running]:
testing.tRunner.func1.2({0xd89360, 0x1631cc0})
        /usr/local/go/src/testing/testing.go:1545 +0x238
testing.tRunner.func1()
        /usr/local/go/src/testing/testing.

i dont' know the exact reason but looks like when clients are closed onClientPasswordChange() function was unable to
loadTokenFromFile and threw the error

Copy link
Member

Choose a reason for hiding this comment

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

is this the full stack trace of the panic? It does not show any of our functions.

Copy link
Contributor Author

@Lauquik Lauquik Sep 12, 2023

Choose a reason for hiding this comment

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

goroutine 120 [running]:
testing.tRunner.func1.2({0xd89360, 0x1631cc0})
        /usr/local/go/src/testing/testing.go:1545 +0x238
testing.tRunner.func1()
        /usr/local/go/src/testing/testing.go:1548 +0x397
panic({0xd89360?, 0x1631cc0?})
        /usr/local/go/src/runtime/panic.go:914 +0x21f
github.com/jaegertracing/jaeger/plugin/storage/es.(*Factory).getArchiveClient(...)
        /mnt/j/cncf/jaeger/plugin/storage/es/factory.go:144
github.com/jaegertracing/jaeger/plugin/storage/es.(*Factory).Close(0xc00023c230)
        /mnt/j/cncf/jaeger/plugin/storage/es/factory.go:292 +0x275
github.com/jaegertracing/jaeger/plugin/storage/es.TestPasswordFromFileErrors(0xc00070e1a0)
        /mnt/j/cncf/jaeger/plugin/storage/es/factory_test.go:386 +0x585
testing.tRunner(0xc00070e1a0, 0xf1c0d8)
        /usr/local/go/src/testing/testing.go:1595 +0xff
created by testing.(*T).Run in goroutine 1
        /usr/local/go/src/testing/testing.go:1648 +0x3ad
exit status 2
FAIL    github.com/jaegertracing/jaeger/plugin/storage/es       0.207s

this is the remaining error log but here onClientPasswordChange is not mentioned i figured it out by myself , i maybe wrong
but after executing defer f.Close() the test calls onClientPasswordChange() this function
and "f.logger.Error("failed to reload password for Elasticsearch client", zap.Error(err)) " this error handle is presenet in that function (onClientPasswordChange)

and we can see the Zap.Error log which says {"error": "no servers specified"}

i tried rerun the test without closing factory resources and the test passed , i manyally closed the watchers and test still passed but after clsoing clients it didn't passed

errs = append(errs,
f.Options.GetPrimary().TLS.Close(),
f.getArchiveClient().Close(),
f.getPrimaryClient().Close())
Copy link
Member

Choose a reason for hiding this comment

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

I was thinking this would need to be more complicated, but I think you're right, as long as we close the watchers above, there's no more concurrency with the clients being swapped, so it's safe to close them like this.
👍

@Lauquik Lauquik changed the title Lavquik Close Elasticsearch client properly Sep 12, 2023
Signed-off-by: Yuri Shkuro <github@ysh.us>
@yurishkuro yurishkuro changed the title Close Elasticsearch client properly [fix] Close Elasticsearch client properly Sep 12, 2023
Signed-off-by: Yuri Shkuro <github@ysh.us>
@codecov
Copy link

codecov bot commented Sep 12, 2023

Codecov Report

Patch coverage is 83.33% of modified lines.

Files Changed Coverage
plugin/storage/es/factory.go 83.33%

📢 Thoughts on this report? Let us know!.

yurishkuro and others added 3 commits September 12, 2023 17:17
Signed-off-by: Yuri Shkuro <github@ysh.us>
Signed-off-by: Yuri Shkuro <github@ysh.us>
@yurishkuro yurishkuro enabled auto-merge (squash) September 12, 2023 21:20
@yurishkuro yurishkuro merged commit 18f3c2a into jaegertracing:main Sep 12, 2023
30 of 31 checks passed
@yurishkuro
Copy link
Member

@Lauquik thanks for PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[test] Flaky test TestPasswordFromFile in plugin/storage/es/factory_test.go
2 participants