Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/94164.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 94164
summary: Avoid NPE in Stateless Get/mGet
area: CRUD
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import org.elasticsearch.action.support.single.shard.TransportSingleShardAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.PlainShardIterator;
import org.elasticsearch.cluster.routing.ShardIterator;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.Writeable;
Expand All @@ -27,6 +30,7 @@
import org.elasticsearch.transport.TransportService;

import java.io.IOException;
import java.util.stream.Collectors;

/**
* Performs the get operation.
Expand Down Expand Up @@ -67,14 +71,27 @@ protected boolean resolveIndex(GetRequest request) {

@Override
protected ShardIterator shards(ClusterState state, InternalRequest request) {
return clusterService.operationRouting()
ShardIterator iterator = clusterService.operationRouting()
.getShards(
clusterService.state(),
request.concreteIndex(),
request.request().id(),
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(
iterator.shardId(),
iterator.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList())
);
} else {
return iterator;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import org.elasticsearch.action.support.single.shard.TransportSingleShardAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.PlainShardIterator;
import org.elasticsearch.cluster.routing.ShardIterator;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.Writeable;
Expand All @@ -29,6 +32,7 @@
import org.elasticsearch.transport.TransportService;

import java.io.IOException;
import java.util.stream.Collectors;

import static org.elasticsearch.core.Strings.format;

Expand Down Expand Up @@ -81,8 +85,21 @@ protected boolean resolveIndex(MultiGetShardRequest request) {

@Override
protected ShardIterator shards(ClusterState state, InternalRequest request) {
return 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(
iterator.shardId(),
iterator.getShardRoutings().stream().filter(ShardRouting::isPromotableToPrimary).collect(Collectors.toList())
);
} else {
return iterator;
}
}

@Override
Expand Down