Skip to content

Commit abd861b

Browse files
Remove Dead Code + Duplication in o.e.c.routing (elastic#36678)
* Removed obviously unused fields+methods * Inlined public methods that only had one caller * Simplified `Optional` chain * Simplified some obviously redundant conditions
1 parent cbbd230 commit abd861b

11 files changed

+73
-164
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/OperationRouting.java

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.elasticsearch.cluster.routing;
2121

22-
import org.apache.logging.log4j.LogManager;
23-
import org.apache.logging.log4j.Logger;
2422
import org.elasticsearch.Version;
2523
import org.elasticsearch.cluster.ClusterState;
2624
import org.elasticsearch.cluster.metadata.IndexMetaData;
@@ -33,7 +31,6 @@
3331
import org.elasticsearch.common.settings.Settings;
3432
import org.elasticsearch.index.IndexNotFoundException;
3533
import org.elasticsearch.index.shard.ShardId;
36-
import org.elasticsearch.index.shard.ShardNotFoundException;
3734
import org.elasticsearch.node.ResponseCollectorService;
3835

3936
import java.util.ArrayList;
@@ -47,8 +44,6 @@
4744

4845
public class OperationRouting {
4946

50-
private static final Logger logger = LogManager.getLogger(OperationRouting.class);
51-
5247
public static final Setting<Boolean> USE_ADAPTIVE_REPLICA_SELECTION_SETTING =
5348
Setting.boolSetting("cluster.routing.use_adaptive_replica_selection", true,
5449
Setting.Property.Dynamic, Setting.Property.NodeScope);
@@ -129,7 +124,7 @@ private Set<IndexShardRoutingTable> computeTargetedShards(ClusterState clusterSt
129124
for (String r : effectiveRouting) {
130125
final int routingPartitionSize = indexMetaData.getRoutingPartitionSize();
131126
for (int partitionOffset = 0; partitionOffset < routingPartitionSize; partitionOffset++) {
132-
set.add(shardRoutingTable(indexRouting, calculateScaledShardId(indexMetaData, r, partitionOffset)));
127+
set.add(RoutingTable.shardRoutingTable(indexRouting, calculateScaledShardId(indexMetaData, r, partitionOffset)));
133128
}
134129
}
135130
} else {
@@ -146,15 +141,7 @@ private ShardIterator preferenceActiveShardIterator(IndexShardRoutingTable index
146141
@Nullable ResponseCollectorService collectorService,
147142
@Nullable Map<String, Long> nodeCounts) {
148143
if (preference == null || preference.isEmpty()) {
149-
if (awarenessAttributes.isEmpty()) {
150-
if (useAdaptiveReplicaSelection) {
151-
return indexShard.activeInitializingShardsRankedIt(collectorService, nodeCounts);
152-
} else {
153-
return indexShard.activeInitializingShardsRandomIt();
154-
}
155-
} else {
156-
return indexShard.preferAttributesActiveInitializingShardsIt(awarenessAttributes, nodes);
157-
}
144+
return shardRoutings(indexShard, nodes, collectorService, nodeCounts);
158145
}
159146
if (preference.charAt(0) == '_') {
160147
Preference preferenceType = Preference.parse(preference);
@@ -181,15 +168,7 @@ private ShardIterator preferenceActiveShardIterator(IndexShardRoutingTable index
181168
}
182169
// no more preference
183170
if (index == -1 || index == preference.length() - 1) {
184-
if (awarenessAttributes.isEmpty()) {
185-
if (useAdaptiveReplicaSelection) {
186-
return indexShard.activeInitializingShardsRankedIt(collectorService, nodeCounts);
187-
} else {
188-
return indexShard.activeInitializingShardsRandomIt();
189-
}
190-
} else {
191-
return indexShard.preferAttributesActiveInitializingShardsIt(awarenessAttributes, nodes);
192-
}
171+
return shardRoutings(indexShard, nodes, collectorService, nodeCounts);
193172
} else {
194173
// update the preference and continue
195174
preference = preference.substring(index + 1);
@@ -232,12 +211,17 @@ private ShardIterator preferenceActiveShardIterator(IndexShardRoutingTable index
232211
}
233212
}
234213

235-
private IndexShardRoutingTable shardRoutingTable(IndexRoutingTable indexRouting, int shardId) {
236-
IndexShardRoutingTable indexShard = indexRouting.shard(shardId);
237-
if (indexShard == null) {
238-
throw new ShardNotFoundException(new ShardId(indexRouting.getIndex(), shardId));
214+
private ShardIterator shardRoutings(IndexShardRoutingTable indexShard, DiscoveryNodes nodes,
215+
@Nullable ResponseCollectorService collectorService, @Nullable Map<String, Long> nodeCounts) {
216+
if (awarenessAttributes.isEmpty()) {
217+
if (useAdaptiveReplicaSelection) {
218+
return indexShard.activeInitializingShardsRankedIt(collectorService, nodeCounts);
219+
} else {
220+
return indexShard.activeInitializingShardsRandomIt();
221+
}
222+
} else {
223+
return indexShard.preferAttributesActiveInitializingShardsIt(awarenessAttributes, nodes);
239224
}
240-
return indexShard;
241225
}
242226

243227
protected IndexRoutingTable indexRoutingTable(ClusterState clusterState, String index) {

server/src/main/java/org/elasticsearch/cluster/routing/RoutingChangesObserver.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ public interface RoutingChangesObserver {
5959
*/
6060
void relocationSourceRemoved(ShardRouting removedReplicaRelocationSource);
6161

62-
/**
63-
* Called on started primary shard after it has been promoted from replica to primary and is reinitialized due to shadow replicas.
64-
*/
65-
void startedPrimaryReinitialized(ShardRouting startedPrimaryShard, ShardRouting initializedShard);
66-
6762
/**
6863
* Called when started replica is promoted to primary.
6964
*/
@@ -117,11 +112,6 @@ public void relocationSourceRemoved(ShardRouting removedReplicaRelocationSource)
117112

118113
}
119114

120-
@Override
121-
public void startedPrimaryReinitialized(ShardRouting startedPrimaryShard, ShardRouting initializedShard) {
122-
123-
}
124-
125115
@Override
126116
public void replicaPromoted(ShardRouting replicaShard) {
127117

@@ -190,13 +180,6 @@ public void relocationSourceRemoved(ShardRouting removedReplicaRelocationSource)
190180
}
191181
}
192182

193-
@Override
194-
public void startedPrimaryReinitialized(ShardRouting startedPrimaryShard, ShardRouting initializedShard) {
195-
for (RoutingChangesObserver routingChangesObserver : routingChangesObservers) {
196-
routingChangesObserver.startedPrimaryReinitialized(startedPrimaryShard, initializedShard);
197-
}
198-
}
199-
200183
@Override
201184
public void replicaPromoted(ShardRouting replicaShard) {
202185
for (RoutingChangesObserver routingChangesObserver : routingChangesObservers) {

server/src/main/java/org/elasticsearch/cluster/routing/RoutingException.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@
2929
*/
3030
public class RoutingException extends ElasticsearchException {
3131

32-
public RoutingException(String message) {
33-
super(message);
34-
}
35-
3632
public RoutingException(String message, Throwable cause) {
3733
super(message, cause);
3834
}
3935

4036
public RoutingException(StreamInput in) throws IOException{
4137
super(in);
4238
}
43-
}
39+
}

server/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,7 @@ assert getByAllocationId(failedShard.shardId(), failedShard.allocationId().getId
577577
if (failedShard.relocatingNodeId() == null) {
578578
if (failedShard.primary()) {
579579
// promote active replica to primary if active replica exists (only the case for shadow replicas)
580-
ShardRouting activeReplica = activeReplicaWithHighestVersion(failedShard.shardId());
581-
if (activeReplica == null) {
582-
moveToUnassigned(failedShard, unassignedInfo);
583-
} else {
584-
movePrimaryToUnassignedAndDemoteToReplica(failedShard, unassignedInfo);
585-
promoteReplicaToPrimary(activeReplica, routingChangesObserver);
586-
}
580+
unassignPrimaryAndPromoteActiveReplicaIfExists(failedShard, unassignedInfo, routingChangesObserver);
587581
} else {
588582
// initializing shard that is not relocation target, just move to unassigned
589583
moveToUnassigned(failedShard, unassignedInfo);
@@ -601,32 +595,36 @@ assert getByAllocationId(failedShard.shardId(), failedShard.allocationId().getId
601595
cancelRelocation(sourceShard);
602596
remove(failedShard);
603597
}
604-
routingChangesObserver.shardFailed(failedShard, unassignedInfo);
605598
} else {
606599
assert failedShard.active();
607600
if (failedShard.primary()) {
608601
// promote active replica to primary if active replica exists
609-
ShardRouting activeReplica = activeReplicaWithHighestVersion(failedShard.shardId());
610-
if (activeReplica == null) {
611-
moveToUnassigned(failedShard, unassignedInfo);
612-
} else {
613-
movePrimaryToUnassignedAndDemoteToReplica(failedShard, unassignedInfo);
614-
promoteReplicaToPrimary(activeReplica, routingChangesObserver);
615-
}
602+
unassignPrimaryAndPromoteActiveReplicaIfExists(failedShard, unassignedInfo, routingChangesObserver);
616603
} else {
617-
assert failedShard.primary() == false;
618604
if (failedShard.relocating()) {
619605
remove(failedShard);
620606
} else {
621607
moveToUnassigned(failedShard, unassignedInfo);
622608
}
623609
}
624-
routingChangesObserver.shardFailed(failedShard, unassignedInfo);
625610
}
611+
routingChangesObserver.shardFailed(failedShard, unassignedInfo);
626612
assert node(failedShard.currentNodeId()).getByShardId(failedShard.shardId()) == null : "failedShard " + failedShard +
627613
" was matched but wasn't removed";
628614
}
629615

616+
private void unassignPrimaryAndPromoteActiveReplicaIfExists(ShardRouting failedShard, UnassignedInfo unassignedInfo,
617+
RoutingChangesObserver routingChangesObserver) {
618+
assert failedShard.primary();
619+
ShardRouting activeReplica = activeReplicaWithHighestVersion(failedShard.shardId());
620+
if (activeReplica == null) {
621+
moveToUnassigned(failedShard, unassignedInfo);
622+
} else {
623+
movePrimaryToUnassignedAndDemoteToReplica(failedShard, unassignedInfo);
624+
promoteReplicaToPrimary(activeReplica, routingChangesObserver);
625+
}
626+
}
627+
630628
private void promoteReplicaToPrimary(ShardRouting activeReplica, RoutingChangesObserver routingChangesObserver) {
631629
// if the activeReplica was relocating before this call to failShard, its relocation was cancelled earlier when we
632630
// failed initializing replica shards (and moved replica relocation source back to started)
@@ -1168,10 +1166,6 @@ private static final class Recoveries {
11681166
private int incoming = 0;
11691167
private int outgoing = 0;
11701168

1171-
int getTotal() {
1172-
return incoming + outgoing;
1173-
}
1174-
11751169
void addOutgoing(int howMany) {
11761170
assert outgoing + howMany >= 0 : outgoing + howMany+ " must be >= 0";
11771171
outgoing += howMany;

0 commit comments

Comments
 (0)