1818import org .elasticsearch .index .query .RangeQueryBuilder ;
1919import org .elasticsearch .index .shard .ShardId ;
2020import org .elasticsearch .plugins .Plugin ;
21- import org .elasticsearch .test .hamcrest .ElasticsearchAssertions ;
2221import org .elasticsearch .test .transport .MockTransportService ;
2322import org .elasticsearch .transport .TransportService ;
2423import org .elasticsearch .xpack .esql .action .AbstractEsqlIntegTestCase ;
3029import java .util .Map ;
3130import java .util .Set ;
3231
32+ import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
3333import static org .elasticsearch .xpack .esql .EsqlTestUtils .as ;
3434import static org .elasticsearch .xpack .esql .EsqlTestUtils .getValuesList ;
3535import static org .hamcrest .Matchers .containsString ;
@@ -48,7 +48,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
4848 * Make sure that we don't send data-node requests to the target shards which won't match the query
4949 */
5050 public void testCanMatch () {
51- ElasticsearchAssertions . assertAcked (
51+ assertAcked (
5252 client ().admin ()
5353 .indices ()
5454 .prepareCreate ("events_2022" )
@@ -60,9 +60,7 @@ public void testCanMatch() {
6060 .add (new IndexRequest ().source ("@timestamp" , "2022-05-02" , "uid" , "u1" ))
6161 .add (new IndexRequest ().source ("@timestamp" , "2022-12-15" , "uid" , "u1" ))
6262 .get ();
63- ElasticsearchAssertions .assertAcked (
64- client ().admin ().indices ().prepareCreate ("events_2023" ).setMapping ("@timestamp" , "type=date" , "uid" , "type=keyword" )
65- );
63+ assertAcked (client ().admin ().indices ().prepareCreate ("events_2023" ).setMapping ("@timestamp" , "type=date" , "uid" , "type=keyword" ));
6664 client ().prepareBulk ("events_2023" )
6765 .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE )
6866 .add (new IndexRequest ().source ("@timestamp" , "2023-01-15" , "uid" , "u2" ))
@@ -72,15 +70,17 @@ public void testCanMatch() {
7270 .get ();
7371 try {
7472 Set <String > queriedIndices = ConcurrentCollections .newConcurrentSet ();
75- for (TransportService ts : internalCluster ().getInstances (TransportService .class )) {
76- MockTransportService transportService = (MockTransportService ) ts ;
77- transportService .addRequestHandlingBehavior (ComputeService .DATA_ACTION_NAME , (handler , request , channel , task ) -> {
78- DataNodeRequest dataNodeRequest = (DataNodeRequest ) request ;
79- for (ShardId shardId : dataNodeRequest .shardIds ()) {
80- queriedIndices .add (shardId .getIndexName ());
73+ for (TransportService transportService : internalCluster ().getInstances (TransportService .class )) {
74+ as (transportService , MockTransportService .class ).addRequestHandlingBehavior (
75+ ComputeService .DATA_ACTION_NAME ,
76+ (handler , request , channel , task ) -> {
77+ DataNodeRequest dataNodeRequest = (DataNodeRequest ) request ;
78+ for (ShardId shardId : dataNodeRequest .shardIds ()) {
79+ queriedIndices .add (shardId .getIndexName ());
80+ }
81+ handler .messageReceived (request , channel , task );
8182 }
82- handler .messageReceived (request , channel , task );
83- });
83+ );
8484 }
8585 try (EsqlQueryResponse resp = run ("from events_*" , randomPragmas (), new RangeQueryBuilder ("@timestamp" ).gte ("2023-01-01" ))) {
8686 assertThat (getValuesList (resp ), hasSize (4 ));
@@ -118,14 +118,14 @@ public void testCanMatch() {
118118 queriedIndices .clear ();
119119 }
120120 } finally {
121- for (TransportService ts : internalCluster ().getInstances (TransportService .class )) {
122- (( MockTransportService ) ts ).clearAllRules ();
121+ for (TransportService transportService : internalCluster ().getInstances (TransportService .class )) {
122+ as ( transportService , MockTransportService . class ).clearAllRules ();
123123 }
124124 }
125125 }
126126
127127 public void testAliasFilters () {
128- ElasticsearchAssertions . assertAcked (
128+ assertAcked (
129129 client ().admin ()
130130 .indices ()
131131 .prepareCreate ("employees" )
@@ -141,7 +141,7 @@ public void testAliasFilters() {
141141 .add (new IndexRequest ().source ("emp_no" , 106 , "dept" , "sales" , "hired" , "2012-08-09" , "salary" , 30.1 ))
142142 .get ();
143143
144- ElasticsearchAssertions . assertAcked (
144+ assertAcked (
145145 client ().admin ()
146146 .indices ()
147147 .prepareAliases (TEST_REQUEST_TIMEOUT , TEST_REQUEST_TIMEOUT )
@@ -209,11 +209,10 @@ public void testAliasFilters() {
209209 }
210210 }
211211
212- @ AwaitsFix (bugUrl = "https://github.com/elastic/elasticsearch/issues/103749" )
213212 public void testFailOnUnavailableShards () throws Exception {
214213 internalCluster ().ensureAtLeastNumDataNodes (2 );
215214 String logsOnlyNode = internalCluster ().startDataOnlyNode ();
216- ElasticsearchAssertions . assertAcked (
215+ assertAcked (
217216 client ().admin ()
218217 .indices ()
219218 .prepareCreate ("events" )
@@ -230,7 +229,7 @@ public void testFailOnUnavailableShards() throws Exception {
230229 .add (new IndexRequest ().source ("timestamp" , 2 , "message" , "b" ))
231230 .add (new IndexRequest ().source ("timestamp" , 3 , "message" , "c" ))
232231 .get ();
233- ElasticsearchAssertions . assertAcked (
232+ assertAcked (
234233 client ().admin ()
235234 .indices ()
236235 .prepareCreate ("logs" )
@@ -246,12 +245,28 @@ public void testFailOnUnavailableShards() throws Exception {
246245 .add (new IndexRequest ().source ("timestamp" , 10 , "message" , "aa" ))
247246 .add (new IndexRequest ().source ("timestamp" , 11 , "message" , "bb" ))
248247 .get ();
248+
249+ // when all shards available
249250 try (EsqlQueryResponse resp = run ("from events,logs | KEEP timestamp,message" )) {
250251 assertThat (getValuesList (resp ), hasSize (5 ));
251- internalCluster ().stopNode (logsOnlyNode );
252- ensureClusterSizeConsistency ();
253- Exception error = expectThrows (Exception .class , () -> run ("from events,logs | KEEP timestamp,message" ));
254- assertThat (error .getMessage (), containsString ("no shard copies found" ));
252+ }
253+
254+ internalCluster ().stopNode (logsOnlyNode );
255+ ensureClusterSizeConsistency ();
256+
257+ // when one shard is unavailable
258+ expectThrows (
259+ Exception .class ,
260+ containsString ("index [logs] has no active shard copy" ),
261+ () -> run ("from events,logs | KEEP timestamp,message" )
262+ );
263+ expectThrows (
264+ Exception .class ,
265+ containsString ("index [logs] has no active shard copy" ),
266+ () -> run ("from * | KEEP timestamp,message" )
267+ );
268+ try (EsqlQueryResponse resp = run ("from events,logs | KEEP timestamp,message" , null , null , true )) {
269+ assertThat (getValuesList (resp ), hasSize (3 ));
255270 }
256271 }
257272
@@ -261,9 +276,7 @@ public void testSkipOnIndexName() {
261276 Map <String , Integer > indexToNumDocs = new HashMap <>();
262277 for (int i = 0 ; i < numIndices ; i ++) {
263278 String index = "events-" + i ;
264- ElasticsearchAssertions .assertAcked (
265- client ().admin ().indices ().prepareCreate (index ).setMapping ("timestamp" , "type=long" , "message" , "type=keyword" )
266- );
279+ assertAcked (client ().admin ().indices ().prepareCreate (index ).setMapping ("timestamp" , "type=long" , "message" , "type=keyword" ));
267280 BulkRequestBuilder bulk = client ().prepareBulk (index ).setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
268281 int docs = between (1 , 5 );
269282 long timestamp = 1 ;
@@ -274,15 +287,17 @@ public void testSkipOnIndexName() {
274287 indexToNumDocs .put (index , docs );
275288 }
276289 Set <String > queriedIndices = ConcurrentCollections .newConcurrentSet ();
277- for (TransportService ts : internalCluster ().getInstances (TransportService .class )) {
278- MockTransportService mockTransportService = as (ts , MockTransportService .class );
279- mockTransportService .addRequestHandlingBehavior (ComputeService .DATA_ACTION_NAME , (handler , request , channel , task ) -> {
280- DataNodeRequest dataNodeRequest = (DataNodeRequest ) request ;
281- for (ShardId shardId : dataNodeRequest .shardIds ()) {
282- queriedIndices .add (shardId .getIndexName ());
290+ for (TransportService transportService : internalCluster ().getInstances (TransportService .class )) {
291+ as (transportService , MockTransportService .class ).addRequestHandlingBehavior (
292+ ComputeService .DATA_ACTION_NAME ,
293+ (handler , request , channel , task ) -> {
294+ DataNodeRequest dataNodeRequest = (DataNodeRequest ) request ;
295+ for (ShardId shardId : dataNodeRequest .shardIds ()) {
296+ queriedIndices .add (shardId .getIndexName ());
297+ }
298+ handler .messageReceived (request , channel , task );
283299 }
284- handler .messageReceived (request , channel , task );
285- });
300+ );
286301 }
287302 try {
288303 for (int i = 0 ; i < numIndices ; i ++) {
@@ -294,9 +309,8 @@ public void testSkipOnIndexName() {
294309 assertThat (queriedIndices , equalTo (Set .of (index )));
295310 }
296311 } finally {
297- for (TransportService ts : internalCluster ().getInstances (TransportService .class )) {
298- MockTransportService mockTransportService = as (ts , MockTransportService .class );
299- mockTransportService .clearAllRules ();
312+ for (TransportService transportService : internalCluster ().getInstances (TransportService .class )) {
313+ as (transportService , MockTransportService .class ).clearAllRules ();
300314 }
301315 }
302316 }
0 commit comments