Skip to content

Commit 1eb8d5b

Browse files
author
Yannick Welsch
committed
Prefer nodes that previously held primary shard for primary shard allocation
1 parent 12df1e7 commit 1eb8d5b

File tree

4 files changed

+81
-47
lines changed

4 files changed

+81
-47
lines changed

core/src/main/java/org/elasticsearch/gateway/PrimaryShardAllocator.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Collections;
3939
import java.util.Comparator;
4040
import java.util.HashMap;
41+
import java.util.LinkedList;
4142
import java.util.List;
4243
import java.util.Map;
4344
import java.util.Set;
@@ -175,8 +176,8 @@ public boolean allocateUnassigned(RoutingAllocation allocation) {
175176
*/
176177
protected NodesAndVersions buildAllocationIdBasedNodes(ShardRouting shard, boolean matchAnyShard, Set<String> ignoreNodes,
177178
Set<String> lastActiveAllocationIds, AsyncShardFetch.FetchResult<TransportNodesListGatewayStartedShards.NodeGatewayStartedShards> shardState) {
178-
List<DiscoveryNode> matchingNodes = new ArrayList<>();
179-
List<DiscoveryNode> nonMatchingNodes = new ArrayList<>();
179+
LinkedList<DiscoveryNode> matchingNodes = new LinkedList<>();
180+
LinkedList<DiscoveryNode> nonMatchingNodes = new LinkedList<>();
180181
long highestVersion = -1;
181182
for (TransportNodesListGatewayStartedShards.NodeGatewayStartedShards nodeShardState : shardState.getData().values()) {
182183
DiscoveryNode node = nodeShardState.getNode();
@@ -200,10 +201,18 @@ protected NodesAndVersions buildAllocationIdBasedNodes(ShardRouting shard, boole
200201

201202
if (allocationId != null) {
202203
if (lastActiveAllocationIds.contains(allocationId)) {
203-
matchingNodes.add(node);
204+
if (nodeShardState.primary()) {
205+
matchingNodes.addFirst(node);
206+
} else {
207+
matchingNodes.addLast(node);
208+
}
204209
highestVersion = Math.max(highestVersion, nodeShardState.version());
205210
} else if (matchAnyShard) {
206-
nonMatchingNodes.add(node);
211+
if (nodeShardState.primary()) {
212+
nonMatchingNodes.addFirst(node);
213+
} else {
214+
nonMatchingNodes.addLast(node);
215+
}
207216
highestVersion = Math.max(highestVersion, nodeShardState.version());
208217
}
209218
}

core/src/main/java/org/elasticsearch/gateway/TransportNodesListGatewayStartedShards.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ protected NodeGatewayStartedShards nodeOperation(NodeRequest request) {
139139
} catch (Exception exception) {
140140
logger.trace("{} can't open index for shard [{}] in path [{}]", exception, shardId, shardStateMetaData, (shardPath != null) ? shardPath.resolveIndex() : "");
141141
String allocationId = shardStateMetaData.allocationId != null ? shardStateMetaData.allocationId.getId() : null;
142-
return new NodeGatewayStartedShards(clusterService.localNode(), shardStateMetaData.version, allocationId, exception);
142+
return new NodeGatewayStartedShards(clusterService.localNode(), shardStateMetaData.version, allocationId, shardStateMetaData.primary, exception);
143143
}
144144
}
145145
// old shard metadata doesn't have the actual index UUID so we need to check if the actual uuid in the metadata
@@ -150,11 +150,11 @@ protected NodeGatewayStartedShards nodeOperation(NodeRequest request) {
150150
} else {
151151
logger.debug("{} shard state info found: [{}]", shardId, shardStateMetaData);
152152
String allocationId = shardStateMetaData.allocationId != null ? shardStateMetaData.allocationId.getId() : null;
153-
return new NodeGatewayStartedShards(clusterService.localNode(), shardStateMetaData.version, allocationId);
153+
return new NodeGatewayStartedShards(clusterService.localNode(), shardStateMetaData.version, allocationId, shardStateMetaData.primary);
154154
}
155155
}
156156
logger.trace("{} no local shard info found", shardId);
157-
return new NodeGatewayStartedShards(clusterService.localNode(), -1, null);
157+
return new NodeGatewayStartedShards(clusterService.localNode(), -1, null, false);
158158
} catch (Exception e) {
159159
throw new ElasticsearchException("failed to load started shards", e);
160160
}
@@ -279,18 +279,20 @@ public static class NodeGatewayStartedShards extends BaseNodeResponse {
279279

280280
private long version = -1;
281281
private String allocationId = null;
282+
private boolean primary = false;
282283
private Throwable storeException = null;
283284

284285
public NodeGatewayStartedShards() {
285286
}
286-
public NodeGatewayStartedShards(DiscoveryNode node, long version, String allocationId) {
287-
this(node, version, allocationId, null);
287+
public NodeGatewayStartedShards(DiscoveryNode node, long version, String allocationId, boolean primary) {
288+
this(node, version, allocationId, primary, null);
288289
}
289290

290-
public NodeGatewayStartedShards(DiscoveryNode node, long version, String allocationId, Throwable storeException) {
291+
public NodeGatewayStartedShards(DiscoveryNode node, long version, String allocationId, boolean primary, Throwable storeException) {
291292
super(node);
292293
this.version = version;
293294
this.allocationId = allocationId;
295+
this.primary = primary;
294296
this.storeException = storeException;
295297
}
296298

@@ -302,6 +304,10 @@ public String allocationId() {
302304
return this.allocationId;
303305
}
304306

307+
public boolean primary() {
308+
return this.primary;
309+
}
310+
305311
public Throwable storeException() {
306312
return this.storeException;
307313
}
@@ -311,6 +317,7 @@ public void readFrom(StreamInput in) throws IOException {
311317
super.readFrom(in);
312318
version = in.readLong();
313319
allocationId = in.readOptionalString();
320+
primary = in.readBoolean();
314321
if (in.readBoolean()) {
315322
storeException = in.readThrowable();
316323
}
@@ -321,6 +328,7 @@ public void writeTo(StreamOutput out) throws IOException {
321328
super.writeTo(out);
322329
out.writeLong(version);
323330
out.writeOptionalString(allocationId);
331+
out.writeBoolean(primary);
324332
if (storeException != null) {
325333
out.writeBoolean(true);
326334
out.writeThrowable(storeException);

0 commit comments

Comments
 (0)