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

Removing explicit SearchResponse usages in tests - v2 #102021

Merged

Conversation

pmpailis
Copy link
Contributor

@pmpailis pmpailis commented Nov 10, 2023

Tests covered in this PR:

  • org.elasticsearch.index.reindex.BulkByScrollUsesAllScrollDocumentsAfterConflictsIntegTests
  • org.elasticsearch.reindex.ClientScrollableHitSourceTests
  • org.elasticsearch.search.CrossClusterSearchUnavailableClusterIT
  • org.elasticsearch.searchCCSDuelIT
  • org.elasticsearch.xpack.analytics.aggregations.metrics.HistogramPercentileAggregationTests
  • org.elasticsearch.xpack.CcrIntegTestCase
  • org.elasticsearch.snapshots.sourceonly.SourceOnlySnapshotIT
  • org.elasticsearch.xpack.downsample.DownsampleClusterDisruptionIT
  • org.elasticsearch.xpack.downsample.DownsampleTransportFailureIT
  • org.elasticsearch.xpack.downsample.ILMDownsampleDisruptionIT
  • org.elasticsearch.xpack.downsample.DownsampleDataStreamTests

@pmpailis pmpailis added >test Issues or PRs that are addressing/adding tests :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team auto-merge Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) v8.12.0 labels Nov 10, 2023
@elasticsearchmachine
Copy link
Collaborator

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

@pmpailis pmpailis removed the auto-merge Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) label Nov 10, 2023
@JVerwolf
Copy link
Contributor

@elasticmachine update branch

if (scriptEnabled && searchHit.getSourceAsMap().containsKey(RETURN_NOOP_FIELD)) {
conflictingOps--;
SearchResponse searchResponse = null;
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can use assertResponse here too so we don't need to decref in the test:

       int conflictingOps = randomIntBetween(maxDocs, numDocs);
       final int finalConflictingOps = conflictingOps;
        final List<SearchHit> docsModifiedConcurrently = new ArrayList<>();
        assertResponse(prepareSearch(sourceIndex).setSize(numDocs) // Get all indexed docs
            .addSort(SORTING_FIELD, SortOrder.DESC), response -> {
            // Modify a subset of the target documents concurrently
            final List<SearchHit> originalDocs = Arrays.asList(response.getHits().getHits());
            docsModifiedConcurrently.addAll(randomSubsetOf(finalConflictingOps, originalDocs));
        });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are some subsequent calls to things initialized in the snippet above, which end up going up until the end of the method (including further assertions). So, instead of having a very large wrapper using assertResponse or declaring things outside of the consumer, I opted for a try/finally block. WDYT? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

As I said bellow, we should try to call decRef only from ElasticAssertions for maintainability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, fair enough! Updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As we also have a nested assertBusy that throws a generic Exception, is it ok to update the signature of assertCheckedResponse to throw an Exception, or do we want to keep it restricted to only specific types (currently it's IOException, ExecutionException, InterruptedException ) ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the assertBusy needs to be nested? you just need to collect the result like in my example?

docsModifiedConcurrently.addAll(randomSubsetOf(finalConflictingOps, originalDocs));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦 Yeah you're right.. For some reason I missed the declarations outside of the wrapper in your example (hence why I mentioned them as an "alternative" in the subsequent comment). Apologies for the noise. :/

SearchResponse responseRaw = null;
SearchResponse responsePreAgg = null;
SearchResponse responseBoth = null;
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can nest assertResponse calls here like:

 assertResponse(
            client().prepareSearch("raw").addAggregation(builder),
            responseRaw -> assertResponse(
                client().prepareSearch("pre_agg").addAggregation(builder),
                responsePreAgg -> assertResponse(client().prepareSearch("pre_agg", "raw").addAggregation(builder), responseBoth -> {
                    InternalHDRPercentiles percentilesRaw = responseRaw.getAggregations().get("agg");
                    InternalHDRPercentiles percentilesPreAgg = responsePreAgg.getAggregations().get("agg");
                    InternalHDRPercentiles percentilesBoth = responseBoth.getAggregations().get("agg");
                    for (int i = 1; i < 100; i++) {
                        assertEquals(percentilesRaw.percentile(i), percentilesPreAgg.percentile(i), 0.0);
                        assertEquals(percentilesRaw.percentile(i), percentilesBoth.percentile(i), 0.0);
                    }
                })
            )
        );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tbh opted to avoid multiple nesting calls to be more straightforward to read, but I can see that we also want to move the decRef responsibility outside of each test method whenever's possible. Updated.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should avoid calling decRef all over the tests as this makes very difficult to maintain. We should minimise as much as possible the places where we call that method.

assertSame(SearchResponse.Clusters.EMPTY, response.getClusters());
assertEquals(10, response.getHits().getTotalHits().value);
assertEquals(10, response.getHits().getHits().length);
SearchResponse searchResponseIndex = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is the right solution here. I open #102049 to remove the high level rest client all together.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for handling this @iverase ! Reverting in favor of #102049 .

…erIT changes & using nested assertResponse HistogramPercentileAggregationTests"
@pmpailis pmpailis added the auto-merge Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) label Nov 13, 2023
@pmpailis
Copy link
Contributor Author

@elasticmachine update branch

@pmpailis pmpailis removed the auto-merge Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) label Nov 13, 2023
failedShards = searchResponse.getFailedShards();
if (searchResponse.getClusters() != null && searchResponse.getClusters() != SearchResponse.Clusters.EMPTY) {
clusters = searchResponse.getClusters();
SearchResponse searchResponse = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is production code, we should not change it in this PR as it is label as test. Let's do those changes once we have all test in place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeap, you're right. Shouldn't have been included in the PR :/
Reverted.

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.

Let's revert the changes on the production code, that should be done in a different PR. Otherwise LGTM

@pmpailis
Copy link
Contributor Author

Thanks for the 👀 @iverase! (and apologies for the trouble and stupid issues!)

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

@pmpailis pmpailis merged commit 25b80ac into elastic:main Nov 13, 2023
13 checks passed
@pmpailis pmpailis deleted the fix/remove-explicit-search-response-test-v2 branch February 20, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team >test Issues or PRs that are addressing/adding tests v8.12.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants