|
22 | 22 | import org.elasticsearch.rest.RestStatus; |
23 | 23 | import org.elasticsearch.search.SearchShardTarget; |
24 | 24 | import org.elasticsearch.test.ESTestCase; |
| 25 | +import org.elasticsearch.transport.NodeDisconnectedException; |
25 | 26 | import org.elasticsearch.xcontent.ToXContent; |
26 | 27 | import org.elasticsearch.xcontent.XContent; |
27 | 28 | import org.elasticsearch.xcontent.XContentParser; |
|
31 | 32 |
|
32 | 33 | import static org.hamcrest.CoreMatchers.hasItem; |
33 | 34 | import static org.hamcrest.Matchers.hasSize; |
| 35 | +import static org.hamcrest.Matchers.is; |
34 | 36 |
|
35 | 37 | public class SearchPhaseExecutionExceptionTests extends ESTestCase { |
36 | 38 |
|
@@ -168,4 +170,57 @@ public void testPhaseFailureWithSearchShardFailure() { |
168 | 170 |
|
169 | 171 | assertEquals(actual.status(), RestStatus.BAD_REQUEST); |
170 | 172 | } |
| 173 | + |
| 174 | + public void testOnlyWithCodesThatDoNotRequirePrecedence() { |
| 175 | + int pickedIndex = randomIntBetween(0, 1); |
| 176 | + |
| 177 | + // Pick one of these exceptions randomly. |
| 178 | + var searchExceptions = new ElasticsearchException[] { |
| 179 | + new ElasticsearchException("simulated"), |
| 180 | + new NodeDisconnectedException(null, "unused message", "unused action", null) }; |
| 181 | + |
| 182 | + // Status codes that map to searchExceptions. |
| 183 | + var expectedStatusCodes = new RestStatus[] { RestStatus.INTERNAL_SERVER_ERROR, RestStatus.BAD_GATEWAY }; |
| 184 | + |
| 185 | + ShardSearchFailure shardFailure1 = new ShardSearchFailure( |
| 186 | + searchExceptions[pickedIndex], |
| 187 | + new SearchShardTarget("nodeID", new ShardId("someIndex", "someUUID", 1), null) |
| 188 | + ); |
| 189 | + |
| 190 | + ShardSearchFailure shardFailure2 = new ShardSearchFailure( |
| 191 | + searchExceptions[pickedIndex], |
| 192 | + new SearchShardTarget("nodeID", new ShardId("someIndex", "someUUID", 2), null) |
| 193 | + ); |
| 194 | + |
| 195 | + SearchPhaseExecutionException ex = new SearchPhaseExecutionException( |
| 196 | + "search", |
| 197 | + "all shards failed", |
| 198 | + new ShardSearchFailure[] { shardFailure1, shardFailure2 } |
| 199 | + ); |
| 200 | + |
| 201 | + assertThat(ex.status(), is(expectedStatusCodes[pickedIndex])); |
| 202 | + } |
| 203 | + |
| 204 | + public void testWithRetriableCodesThatTakePrecedence() { |
| 205 | + // Maps to a 500. |
| 206 | + ShardSearchFailure shardFailure1 = new ShardSearchFailure( |
| 207 | + new ElasticsearchException("simulated"), |
| 208 | + new SearchShardTarget("nodeID", new ShardId("someIndex", "someUUID", 1), null) |
| 209 | + ); |
| 210 | + |
| 211 | + // Maps to a 502. |
| 212 | + ShardSearchFailure shardFailure2 = new ShardSearchFailure( |
| 213 | + new NodeDisconnectedException(null, "unused message", "unused action", null), |
| 214 | + new SearchShardTarget("nodeID", new ShardId("someIndex", "someUUID", 2), null) |
| 215 | + ); |
| 216 | + |
| 217 | + SearchPhaseExecutionException ex = new SearchPhaseExecutionException( |
| 218 | + "search", |
| 219 | + "all shards failed", |
| 220 | + new ShardSearchFailure[] { shardFailure1, shardFailure2 } |
| 221 | + ); |
| 222 | + |
| 223 | + // The 502 takes precedence over 500. |
| 224 | + assertThat(ex.status(), is(RestStatus.BAD_GATEWAY)); |
| 225 | + } |
171 | 226 | } |
0 commit comments