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

Added decRef calls to SearchResponse tests #102040

Merged
merged 2 commits into from
Nov 13, 2023

Conversation

quux00
Copy link
Contributor

@quux00 quux00 commented Nov 10, 2023

Tests covered these sections of the ES repo
x-pack/plugin/monitoring
x-pack/plugin/rank-rrf
x-pack/plugin/rollup
x-pack/plugin/search-business-rules

The decRef was either added explicitly or implicitly by using ElasticsearchAssertions.assertResponse.

@quux00 quux00 added >non-issue :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v8.12.0 labels Nov 10, 2023
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search (Team:Search)

Copy link
Member

@original-brownbear original-brownbear left a comment

Choose a reason for hiding this comment

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

Looks just fine, just one (I think) actual tiny inaccuracy and one nit :)

});

final SearchResponse response = client().prepareSearch(monitoringIndex).get();
final SearchHits hits = response.getHits();
try {
Copy link
Member

Choose a reason for hiding this comment

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

Why not use the assertResponse pattern here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried, but when I did that, the assertMonitoringDoc method (currently line 184) could no longer be resolved for reasons that weren't clear to me. And (more minor) toMap said it threw an unhandled Exception, so it would have been clunky to handle as well.

Copy link
Member

Choose a reason for hiding this comment

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

Fair point lets leave it this way :)

Copy link
Contributor

Choose a reason for hiding this comment

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

You can use assertCheckedResponse and then you don't need to manually do it in the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Excellent. Thanks for the tip.

for (final SearchHit hit : searchResponse.get().getHits()) {
final Map<String, Object> searchHit = toMap(hit);
assertMonitoringDoc(searchHit, MonitoredSystem.ES, MonitoringService.MIN_INTERVAL);
searchResponseRef.set(response);
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't we have to use setAndGet here and release whatever non-null response that might already be in the reference ?

Copy link
Contributor Author

@quux00 quux00 Nov 10, 2023

Choose a reason for hiding this comment

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

We could do that for safety, but I don't think it's necessary (?). The AtomicReference.set call should only be invoked once, since the assertBusy "loop" stops only after none of the asserts throw an Exception. I catch the Exception above and decRef those failed SearchResponses and then re-throw, so the setter should only be called once when the assertBusy finally returns without error.

So I could add your idea for safety or put in an assert that the searchResponseRef is null before setting it.

Copy link
Contributor

@iverase iverase Nov 13, 2023

Choose a reason for hiding this comment

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

I don't understand what we need all of this. I think this should be enough:

        whenExportersAreReady(() -> {
            assertBusy(() -> {
                assertCheckedResponse(client().prepareSearch(".monitoring-es-*")
                        .setCollapse(new CollapseBuilder("type"))
                        .addSort("timestamp", SortOrder.DESC) , response -> {
                        assertThat(response.status(), is(RestStatus.OK));
                        assertThat(
                            "Expecting a minimum number of 6 docs, one per collector",
                            response.getHits().getHits().length,
                            greaterThanOrEqualTo(6)
                        );

                        for (final SearchHit hit : response.getHits()) {
                            final Map<String, Object> searchHit = toMap(hit);
                            assertMonitoringDoc(searchHit, MonitoredSystem.ES, MonitoringService.MIN_INTERVAL);
                        }
                    });
            });
        });

Copy link
Member

@original-brownbear original-brownbear left a comment

Choose a reason for hiding this comment

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

LGTM, sorry I reviewed this with ?w=1 which made this mostly an easy exercise but made me misread the atomic ref bit :) -> all good, thanks!

Tests covered these sections of the ES repo
x-pack/plugin/monitoring
x-pack/plugin/rank-rrf
x-pack/plugin/rollup
x-pack/plugin/search-business-rules

The decRef was either added explicitly or implicitly by using ElasticsearchAssertions.assertResponse.
@quux00 quux00 force-pushed the xpack/decRef-search-response-in-tests branch from 6895892 to 7c6cd85 Compare November 13, 2023 14:05
Copy link
Contributor

@iverase iverase left a comment

Choose a reason for hiding this comment

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

LGTM

@quux00 quux00 merged commit 57ae150 into elastic:main Nov 13, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>non-issue :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v8.12.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants