@@ -109,7 +109,14 @@ public void testMergeShardFailures() throws InterruptedException {
109109 SearchTimeProvider searchTimeProvider = new SearchTimeProvider (0 , 0 , () -> 0 );
110110 SearchResponseMerger merger = new SearchResponseMerger (0 , 0 , SearchContext .TRACK_TOTAL_HITS_ACCURATE ,
111111 searchTimeProvider , flag -> null );
112- PriorityQueue <Tuple <ShardId , ShardSearchFailure >> priorityQueue = new PriorityQueue <>(Comparator .comparing (Tuple ::v1 ));
112+ PriorityQueue <Tuple <SearchShardTarget , ShardSearchFailure >> priorityQueue = new PriorityQueue <>(Comparator .comparing (Tuple ::v1 ,
113+ (o1 , o2 ) -> {
114+ int compareTo = o1 .getShardId ().compareTo (o2 .getShardId ());
115+ if (compareTo != 0 ) {
116+ return compareTo ;
117+ }
118+ return o1 .getClusterAlias ().compareTo (o2 .getClusterAlias ());
119+ }));
113120 int numIndices = numResponses * randomIntBetween (1 , 3 );
114121 Iterator <Map .Entry <String , Index []>> indicesPerCluster = randomRealisticIndices (numIndices , numResponses ).entrySet ().iterator ();
115122 for (int i = 0 ; i < numResponses ; i ++) {
@@ -120,15 +127,46 @@ public void testMergeShardFailures() throws InterruptedException {
120127 ShardSearchFailure [] shardSearchFailures = new ShardSearchFailure [numFailures ];
121128 for (int j = 0 ; j < numFailures ; j ++) {
122129 ShardId shardId = new ShardId (randomFrom (indices ), j );
123- ShardSearchFailure failure ;
124- if (randomBoolean ()) {
125- SearchShardTarget searchShardTarget = new SearchShardTarget (randomAlphaOfLength (6 ), shardId , clusterAlias , null );
126- failure = new ShardSearchFailure (new IllegalArgumentException (), searchShardTarget );
127- } else {
128- ElasticsearchException elasticsearchException = new ElasticsearchException (new IllegalArgumentException ());
129- elasticsearchException .setShard (shardId );
130- failure = new ShardSearchFailure (elasticsearchException );
131- }
130+ SearchShardTarget searchShardTarget = new SearchShardTarget (randomAlphaOfLength (6 ), shardId , clusterAlias , null );
131+ ShardSearchFailure failure = new ShardSearchFailure (new IllegalArgumentException (), searchShardTarget );
132+ shardSearchFailures [j ] = failure ;
133+ priorityQueue .add (Tuple .tuple (searchShardTarget , failure ));
134+ }
135+ SearchResponse searchResponse = new SearchResponse (InternalSearchResponse .empty (), null ,
136+ 1 , 1 , 0 , 100L , shardSearchFailures , SearchResponse .Clusters .EMPTY );
137+ addResponse (merger , searchResponse );
138+ }
139+ awaitResponsesAdded ();
140+ assertEquals (numResponses , merger .numResponses ());
141+ SearchResponse .Clusters clusters = SearchResponseTests .randomClusters ();
142+ SearchResponse mergedResponse = merger .getMergedResponse (clusters );
143+ assertSame (clusters , mergedResponse .getClusters ());
144+ assertEquals (numResponses , mergedResponse .getTotalShards ());
145+ assertEquals (numResponses , mergedResponse .getSuccessfulShards ());
146+ assertEquals (0 , mergedResponse .getSkippedShards ());
147+ assertEquals (priorityQueue .size (), mergedResponse .getFailedShards ());
148+ ShardSearchFailure [] shardFailures = mergedResponse .getShardFailures ();
149+ assertEquals (priorityQueue .size (), shardFailures .length );
150+ for (ShardSearchFailure shardFailure : shardFailures ) {
151+ ShardSearchFailure expected = priorityQueue .poll ().v2 ();
152+ assertSame (expected , shardFailure );
153+ }
154+ }
155+
156+ public void testMergeShardFailuresNullShardTarget () throws InterruptedException {
157+ SearchTimeProvider searchTimeProvider = new SearchTimeProvider (0 , 0 , () -> 0 );
158+ SearchResponseMerger merger = new SearchResponseMerger (0 , 0 , SearchContext .TRACK_TOTAL_HITS_ACCURATE ,
159+ searchTimeProvider , flag -> null );
160+ PriorityQueue <Tuple <ShardId , ShardSearchFailure >> priorityQueue = new PriorityQueue <>(Comparator .comparing (Tuple ::v1 ));
161+ for (int i = 0 ; i < numResponses ; i ++) {
162+ int numFailures = randomIntBetween (1 , 10 );
163+ ShardSearchFailure [] shardSearchFailures = new ShardSearchFailure [numFailures ];
164+ for (int j = 0 ; j < numFailures ; j ++) {
165+ String index = "index-" + i ;
166+ ShardId shardId = new ShardId (index , index + "-uuid" , j );
167+ ElasticsearchException elasticsearchException = new ElasticsearchException (new IllegalArgumentException ());
168+ elasticsearchException .setShard (shardId );
169+ ShardSearchFailure failure = new ShardSearchFailure (elasticsearchException );
132170 shardSearchFailures [j ] = failure ;
133171 priorityQueue .add (Tuple .tuple (shardId , failure ));
134172 }
0 commit comments