diff --git a/docs/changelog/94164.yaml b/docs/changelog/94164.yaml new file mode 100644 index 0000000000000..b55e09225d7a0 --- /dev/null +++ b/docs/changelog/94164.yaml @@ -0,0 +1,5 @@ +pr: 94164 +summary: Avoid NPE in Stateless Get/mGet +area: CRUD +type: bug +issues: [] diff --git a/server/src/main/java/org/elasticsearch/action/get/TransportGetAction.java b/server/src/main/java/org/elasticsearch/action/get/TransportGetAction.java index 8b78d9c4761b4..c08eec6dd3cc1 100644 --- a/server/src/main/java/org/elasticsearch/action/get/TransportGetAction.java +++ b/server/src/main/java/org/elasticsearch/action/get/TransportGetAction.java @@ -71,7 +71,7 @@ protected boolean resolveIndex(GetRequest request) { @Override protected ShardIterator shards(ClusterState state, InternalRequest request) { - ShardIterator shards = clusterService.operationRouting() + ShardIterator iterator = clusterService.operationRouting() .getShards( clusterService.state(), request.concreteIndex(), @@ -79,15 +79,18 @@ protected ShardIterator shards(ClusterState state, InternalRequest request) { request.request().routing(), request.request().preference() ); + if (iterator == null) { + return null; + } // If it is stateless, only route isPromotableToPrimary shards. This is a temporary workaround until a more cohesive solution can be // implemented for search shards. if (DiscoveryNode.isStateless(clusterService.getSettings())) { return new PlainShardIterator( - shards.shardId(), - shards.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList()) + iterator.shardId(), + iterator.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList()) ); } else { - return shards; + return iterator; } } diff --git a/server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java b/server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java index ea7535efe77d6..a173ff4407968 100644 --- a/server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java +++ b/server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java @@ -85,17 +85,20 @@ protected boolean resolveIndex(MultiGetShardRequest request) { @Override protected ShardIterator shards(ClusterState state, InternalRequest request) { - ShardIterator shards = clusterService.operationRouting() + ShardIterator iterator = clusterService.operationRouting() .getShards(state, request.request().index(), request.request().shardId(), request.request().preference()); + if (iterator == null) { + return null; + } // If it is stateless, only route promotable shards. This is a temporary workaround until a more cohesive solution can be // implemented for search shards. if (DiscoveryNode.isStateless(clusterService.getSettings())) { return new PlainShardIterator( - shards.shardId(), - shards.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList()) + iterator.shardId(), + iterator.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList()) ); } else { - return shards; + return iterator; } }