-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid Needless Cache Status Fetches in SearchableSnapshotAllocator #66433
Merged
original-brownbear
merged 3 commits into
elastic:master
from
original-brownbear:avoid-unnecessary-fetches
Dec 16, 2020
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,17 +5,17 @@ | |
*/ | ||
package org.elasticsearch.xpack.searchablesnapshots; | ||
|
||
import com.carrotsearch.hppc.cursors.ObjectCursor; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.FailedNodeException; | ||
import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
import org.elasticsearch.cluster.routing.RecoverySource; | ||
import org.elasticsearch.cluster.routing.RerouteService; | ||
import org.elasticsearch.cluster.routing.RoutingNode; | ||
import org.elasticsearch.cluster.routing.ShardRouting; | ||
import org.elasticsearch.cluster.routing.UnassignedInfo; | ||
|
@@ -27,11 +27,13 @@ | |
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; | ||
import org.elasticsearch.cluster.routing.allocation.decider.Decision; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.Priority; | ||
import org.elasticsearch.common.collect.Tuple; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.unit.ByteSizeValue; | ||
import org.elasticsearch.common.util.concurrent.ConcurrentCollections; | ||
import org.elasticsearch.gateway.AsyncShardFetch; | ||
import org.elasticsearch.gateway.ReplicaShardAllocator; | ||
import org.elasticsearch.index.shard.ShardId; | ||
import org.elasticsearch.repositories.IndexId; | ||
import org.elasticsearch.snapshots.Snapshot; | ||
|
@@ -60,9 +62,9 @@ public class SearchableSnapshotAllocator implements ExistingShardsAllocator { | |
|
||
private static final Logger logger = LogManager.getLogger(SearchableSnapshotAllocator.class); | ||
|
||
private static final ActionListener<ClusterRerouteResponse> REROUTE_LISTENER = new ActionListener<>() { | ||
private static final ActionListener<ClusterState> REROUTE_LISTENER = new ActionListener<>() { | ||
@Override | ||
public void onResponse(ClusterRerouteResponse clusterRerouteResponse) { | ||
public void onResponse(ClusterState clusterRerouteResponse) { | ||
logger.trace("reroute succeeded after loading snapshot cache information"); | ||
} | ||
|
||
|
@@ -78,8 +80,11 @@ public void onFailure(Exception e) { | |
|
||
private final Client client; | ||
|
||
public SearchableSnapshotAllocator(Client client) { | ||
private final RerouteService rerouteService; | ||
|
||
public SearchableSnapshotAllocator(Client client, RerouteService rerouteService) { | ||
this.client = client; | ||
this.rerouteService = rerouteService; | ||
} | ||
|
||
@Override | ||
|
@@ -151,20 +156,15 @@ private AllocateUnassignedDecision decideAllocation(RoutingAllocation allocation | |
return AllocateUnassignedDecision.no(UnassignedInfo.AllocationStatus.FETCHING_SHARD_DATA, null); | ||
} | ||
|
||
final AsyncShardFetch.FetchResult<NodeCacheFilesMetadata> fetchedCacheData = fetchData(shardRouting, allocation); | ||
if (fetchedCacheData.hasData() == false) { | ||
return AllocateUnassignedDecision.no(UnassignedInfo.AllocationStatus.FETCHING_SHARD_DATA, null); | ||
} | ||
|
||
final boolean explain = allocation.debugDecision(); | ||
final MatchingNodes matchingNodes = findMatchingNodes(shardRouting, allocation, fetchedCacheData, explain); | ||
assert explain == false || matchingNodes.nodeDecisions != null : "in explain mode, we must have individual node decisions"; | ||
|
||
// pre-check if it can be allocated to any node that currently exists, so we won't list the cache sizes for it for nothing | ||
// TODO: in the following logic, we do not account for existing cache size when handling disk space checks, should and can we | ||
// reliably do this in a world of concurrent cache evictions or are we ok with the cache size just being a best effort hint | ||
// here? | ||
Tuple<Decision, Map<String, NodeAllocationResult>> result = canBeAllocatedToAtLeastOneNode(shardRouting, allocation); | ||
Tuple<Decision, Map<String, NodeAllocationResult>> result = ReplicaShardAllocator.canBeAllocatedToAtLeastOneNode( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a test that we do not trigger any reads or reroutes when deciders say no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing, I pushed 0cb0017 :) |
||
shardRouting, | ||
allocation | ||
); | ||
Decision allocateDecision = result.v1(); | ||
if (allocateDecision.type() != Decision.Type.YES && (explain == false || asyncFetchStore.get(shardRouting.shardId()) == null)) { | ||
// only return early if we are not in explain mode, or we are in explain mode but we have not | ||
|
@@ -176,6 +176,14 @@ private AllocateUnassignedDecision decideAllocation(RoutingAllocation allocation | |
); | ||
} | ||
|
||
final AsyncShardFetch.FetchResult<NodeCacheFilesMetadata> fetchedCacheData = fetchData(shardRouting, allocation); | ||
if (fetchedCacheData.hasData() == false) { | ||
return AllocateUnassignedDecision.no(UnassignedInfo.AllocationStatus.FETCHING_SHARD_DATA, null); | ||
} | ||
|
||
final MatchingNodes matchingNodes = findMatchingNodes(shardRouting, allocation, fetchedCacheData, explain); | ||
assert explain == false || matchingNodes.nodeDecisions != null : "in explain mode, we must have individual node decisions"; | ||
|
||
List<NodeAllocationResult> nodeDecisions = augmentExplanationsWithStoreInfo(result.v2(), matchingNodes.nodeDecisions); | ||
if (allocateDecision.type() != Decision.Type.YES) { | ||
return AllocateUnassignedDecision.no(UnassignedInfo.AllocationStatus.fromDecision(allocateDecision.type()), nodeDecisions); | ||
|
@@ -283,7 +291,7 @@ public void onFailure(Exception e) { | |
} | ||
}, () -> { | ||
if (asyncFetch.data() != null) { | ||
client.admin().cluster().prepareReroute().execute(REROUTE_LISTENER); | ||
rerouteService.reroute("async_shard_cache_fetch", Priority.HIGH, REROUTE_LISTENER); | ||
} | ||
}) | ||
); | ||
|
@@ -313,45 +321,6 @@ private static List<NodeAllocationResult> augmentExplanationsWithStoreInfo( | |
return augmented; | ||
} | ||
|
||
/** | ||
* Determines if the shard can be allocated on at least one node based on the allocation deciders. | ||
* | ||
* Returns the best allocation decision for allocating the shard on any node (i.e. YES if at least one | ||
* node decided YES, THROTTLE if at least one node decided THROTTLE, and NO if none of the nodes decided | ||
* YES or THROTTLE). If in explain mode, also returns the node-level explanations as the second element | ||
* in the returned tuple. | ||
* TODO: dry this method up against ReplicaShardAllocator | ||
*/ | ||
private static Tuple<Decision, Map<String, NodeAllocationResult>> canBeAllocatedToAtLeastOneNode( | ||
ShardRouting shard, | ||
RoutingAllocation allocation | ||
) { | ||
Decision madeDecision = Decision.NO; | ||
final boolean explain = allocation.debugDecision(); | ||
Map<String, NodeAllocationResult> nodeDecisions = explain ? new HashMap<>() : null; | ||
for (ObjectCursor<DiscoveryNode> cursor : allocation.nodes().getDataNodes().values()) { | ||
RoutingNode node = allocation.routingNodes().node(cursor.value.getId()); | ||
if (node == null) { | ||
continue; | ||
} | ||
// if we can't allocate it on a node, ignore it | ||
Decision decision = allocation.deciders().canAllocate(shard, node, allocation); | ||
if (decision.type() == Decision.Type.YES && madeDecision.type() != Decision.Type.YES) { | ||
if (explain) { | ||
madeDecision = decision; | ||
} else { | ||
return Tuple.tuple(decision, null); | ||
} | ||
} else if (madeDecision.type() == Decision.Type.NO && decision.type() == Decision.Type.THROTTLE) { | ||
madeDecision = decision; | ||
} | ||
if (explain) { | ||
nodeDecisions.put(node.nodeId(), new NodeAllocationResult(node.node(), null, decision)); | ||
} | ||
} | ||
return Tuple.tuple(madeDecision, nodeDecisions); | ||
} | ||
|
||
private MatchingNodes findMatchingNodes( | ||
ShardRouting shard, | ||
RoutingAllocation allocation, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a revert of making this public yesterday now that it's not needed for tests any longer.