Skip to content

Commit 49767fc

Browse files
Some Cleanup in o.e.gateway Package (#42108) (#42568)
* Removing obvious dead code * Removing redundant listener interface
1 parent a5ca20a commit 49767fc

17 files changed

+46
-120
lines changed

server/src/main/java/org/elasticsearch/gateway/BaseGatewayShardAllocator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public abstract AllocateUnassignedDecision makeAllocationDecision(ShardRouting u
9393
* Builds decisions for all nodes in the cluster, so that the explain API can provide information on
9494
* allocation decisions for each node, while still waiting to allocate the shard (e.g. due to fetching shard data).
9595
*/
96-
protected List<NodeAllocationResult> buildDecisionsForAllNodes(ShardRouting shard, RoutingAllocation allocation) {
96+
protected static List<NodeAllocationResult> buildDecisionsForAllNodes(ShardRouting shard, RoutingAllocation allocation) {
9797
List<NodeAllocationResult> results = new ArrayList<>();
9898
for (RoutingNode node : allocation.routingNodes()) {
9999
Decision decision = allocation.deciders().canAllocate(shard, node, allocation);

server/src/main/java/org/elasticsearch/gateway/DanglingIndicesState.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.carrotsearch.hppc.cursors.ObjectCursor;
2323
import org.apache.logging.log4j.LogManager;
2424
import org.apache.logging.log4j.Logger;
25+
import org.elasticsearch.action.ActionListener;
2526
import org.elasticsearch.cluster.ClusterChangedEvent;
2627
import org.elasticsearch.cluster.ClusterStateListener;
2728
import org.elasticsearch.cluster.metadata.IndexGraveyard;
@@ -163,14 +164,14 @@ private void allocateDanglingIndices() {
163164
}
164165
try {
165166
allocateDangledIndices.allocateDangled(Collections.unmodifiableCollection(new ArrayList<>(danglingIndices.values())),
166-
new LocalAllocateDangledIndices.Listener() {
167+
new ActionListener<LocalAllocateDangledIndices.AllocateDangledResponse>() {
167168
@Override
168169
public void onResponse(LocalAllocateDangledIndices.AllocateDangledResponse response) {
169170
logger.trace("allocated dangled");
170171
}
171172

172173
@Override
173-
public void onFailure(Throwable e) {
174+
public void onFailure(Exception e) {
174175
logger.info("failed to send allocated dangled", e);
175176
}
176177
}

server/src/main/java/org/elasticsearch/gateway/Gateway.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.elasticsearch.common.settings.Settings;
3232
import org.elasticsearch.discovery.zen.ElectMasterService;
3333
import org.elasticsearch.index.Index;
34-
import org.elasticsearch.indices.IndicesService;
3534

3635
import java.util.Arrays;
3736
import java.util.function.Function;
@@ -45,12 +44,9 @@ public class Gateway {
4544
private final TransportNodesListGatewayMetaState listGatewayMetaState;
4645

4746
private final int minimumMasterNodes;
48-
private final IndicesService indicesService;
4947

5048
public Gateway(final Settings settings, final ClusterService clusterService,
51-
final TransportNodesListGatewayMetaState listGatewayMetaState,
52-
final IndicesService indicesService) {
53-
this.indicesService = indicesService;
49+
final TransportNodesListGatewayMetaState listGatewayMetaState) {
5450
this.clusterService = clusterService;
5551
this.listGatewayMetaState = listGatewayMetaState;
5652
this.minimumMasterNodes = ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.get(settings);

server/src/main/java/org/elasticsearch/gateway/GatewayException.java

-8
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@
2626

2727
public class GatewayException extends ElasticsearchException {
2828

29-
public GatewayException(String msg) {
30-
super(msg);
31-
}
32-
33-
public GatewayException(String msg, Throwable cause) {
34-
super(msg, cause);
35-
}
36-
3729
public GatewayException(StreamInput in) throws IOException {
3830
super(in);
3931
}

server/src/main/java/org/elasticsearch/gateway/GatewayMetaState.java

+8-17
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@
4444
import org.elasticsearch.common.collect.Tuple;
4545
import org.elasticsearch.common.settings.Settings;
4646
import org.elasticsearch.common.unit.TimeValue;
47-
import org.elasticsearch.env.NodeEnvironment;
4847
import org.elasticsearch.index.Index;
49-
import org.elasticsearch.indices.IndicesService;
5048
import org.elasticsearch.plugins.MetaDataUpgrader;
5149
import org.elasticsearch.transport.TransportService;
5250

@@ -76,28 +74,23 @@
7674
public class GatewayMetaState implements ClusterStateApplier, CoordinationState.PersistedState {
7775
protected static final Logger logger = LogManager.getLogger(GatewayMetaState.class);
7876

79-
private final NodeEnvironment nodeEnv;
8077
private final MetaStateService metaStateService;
8178
private final Settings settings;
8279
private final ClusterService clusterService;
83-
private final IndicesService indicesService;
8480
private final TransportService transportService;
8581

8682
//there is a single thread executing updateClusterState calls, hence no volatile modifier
8783
protected Manifest previousManifest;
8884
protected ClusterState previousClusterState;
8985
protected boolean incrementalWrite;
9086

91-
public GatewayMetaState(Settings settings, NodeEnvironment nodeEnv, MetaStateService metaStateService,
87+
public GatewayMetaState(Settings settings, MetaStateService metaStateService,
9288
MetaDataIndexUpgradeService metaDataIndexUpgradeService, MetaDataUpgrader metaDataUpgrader,
93-
TransportService transportService, ClusterService clusterService,
94-
IndicesService indicesService) throws IOException {
89+
TransportService transportService, ClusterService clusterService) throws IOException {
9590
this.settings = settings;
96-
this.nodeEnv = nodeEnv;
9791
this.metaStateService = metaStateService;
9892
this.transportService = transportService;
9993
this.clusterService = clusterService;
100-
this.indicesService = indicesService;
10194

10295
upgradeMetaData(metaDataIndexUpgradeService, metaDataUpgrader);
10396
initializeClusterState(ClusterName.CLUSTER_NAME_SETTING.get(settings));
@@ -184,7 +177,7 @@ protected void upgradeMetaData(MetaDataIndexUpgradeService metaDataIndexUpgradeS
184177
}
185178
}
186179

187-
protected boolean isMasterOrDataNode() {
180+
private boolean isMasterOrDataNode() {
188181
return DiscoveryNode.isMasterNode(settings) || DiscoveryNode.isDataNode(settings);
189182
}
190183

@@ -312,13 +305,12 @@ long writeIndex(String reason, IndexMetaData metaData) throws WriteStateExceptio
312305
}
313306
}
314307

315-
long writeManifestAndCleanup(String reason, Manifest manifest) throws WriteStateException {
308+
void writeManifestAndCleanup(String reason, Manifest manifest) throws WriteStateException {
316309
assert finished == false : FINISHED_MSG;
317310
try {
318-
long generation = metaStateService.writeManifestAndCleanup(reason, manifest);
311+
metaStateService.writeManifestAndCleanup(reason, manifest);
319312
commitCleanupActions.forEach(Runnable::run);
320313
finished = true;
321-
return generation;
322314
} catch (WriteStateException e) {
323315
// if Manifest write results in dirty WriteStateException it's not safe to remove
324316
// new metadata files, because if Manifest was actually written to disk and its deletion
@@ -346,7 +338,7 @@ void rollback() {
346338
*
347339
* @throws WriteStateException if exception occurs. See also {@link WriteStateException#isDirty()}.
348340
*/
349-
protected void updateClusterState(ClusterState newState, ClusterState previousState)
341+
private void updateClusterState(ClusterState newState, ClusterState previousState)
350342
throws WriteStateException {
351343
MetaData newMetaData = newState.metaData();
352344

@@ -406,7 +398,7 @@ public static Set<Index> getRelevantIndices(ClusterState state, ClusterState pre
406398
}
407399

408400
private static boolean isDataOnlyNode(ClusterState state) {
409-
return ((state.nodes().getLocalNode().isMasterNode() == false) && state.nodes().getLocalNode().isDataNode());
401+
return state.nodes().getLocalNode().isMasterNode() == false && state.nodes().getLocalNode().isDataNode();
410402
}
411403

412404
/**
@@ -535,8 +527,7 @@ private static Set<Index> getRelevantIndicesOnDataOnlyNode(ClusterState state, C
535527
}
536528

537529
private static Set<Index> getRelevantIndicesForMasterEligibleNode(ClusterState state) {
538-
Set<Index> relevantIndices;
539-
relevantIndices = new HashSet<>();
530+
Set<Index> relevantIndices = new HashSet<>();
540531
// we have to iterate over the metadata to make sure we also capture closed indices
541532
for (IndexMetaData indexMetaData : state.metaData()) {
542533
relevantIndices.add(indexMetaData.getIndex());

server/src/main/java/org/elasticsearch/gateway/GatewayService.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
4242
import org.elasticsearch.discovery.Discovery;
4343
import org.elasticsearch.discovery.zen.ZenDiscovery;
44-
import org.elasticsearch.indices.IndicesService;
4544
import org.elasticsearch.rest.RestStatus;
4645
import org.elasticsearch.threadpool.ThreadPool;
4746

@@ -94,7 +93,7 @@ public class GatewayService extends AbstractLifecycleComponent implements Cluste
9493
public GatewayService(final Settings settings, final AllocationService allocationService, final ClusterService clusterService,
9594
final ThreadPool threadPool,
9695
final TransportNodesListGatewayMetaState listGatewayMetaState,
97-
final IndicesService indicesService, final Discovery discovery) {
96+
final Discovery discovery) {
9897
this.allocationService = allocationService;
9998
this.clusterService = clusterService;
10099
this.threadPool = threadPool;
@@ -125,7 +124,7 @@ public GatewayService(final Settings settings, final AllocationService allocatio
125124
recoveryRunnable = () ->
126125
clusterService.submitStateUpdateTask("local-gateway-elected-state", new RecoverStateUpdateTask());
127126
} else {
128-
final Gateway gateway = new Gateway(settings, clusterService, listGatewayMetaState, indicesService);
127+
final Gateway gateway = new Gateway(settings, clusterService, listGatewayMetaState);
129128
recoveryRunnable = () ->
130129
gateway.performStateRecovery(new GatewayRecoveryListener());
131130
}

server/src/main/java/org/elasticsearch/gateway/LocalAllocateDangledIndices.java

+2-11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.logging.log4j.Logger;
2424
import org.apache.logging.log4j.message.ParameterizedMessage;
2525
import org.elasticsearch.Version;
26+
import org.elasticsearch.action.ActionListener;
2627
import org.elasticsearch.cluster.ClusterState;
2728
import org.elasticsearch.cluster.ClusterStateUpdateTask;
2829
import org.elasticsearch.cluster.block.ClusterBlocks;
@@ -76,7 +77,7 @@ public LocalAllocateDangledIndices(TransportService transportService, ClusterSer
7677
new AllocateDangledRequestHandler());
7778
}
7879

79-
public void allocateDangled(Collection<IndexMetaData> indices, final Listener listener) {
80+
public void allocateDangled(Collection<IndexMetaData> indices, ActionListener<AllocateDangledResponse> listener) {
8081
ClusterState clusterState = clusterService.state();
8182
DiscoveryNode masterNode = clusterState.nodes().getMasterNode();
8283
if (masterNode == null) {
@@ -110,12 +111,6 @@ public String executor() {
110111
});
111112
}
112113

113-
public interface Listener {
114-
void onResponse(AllocateDangledResponse response);
115-
116-
void onFailure(Throwable e);
117-
}
118-
119114
class AllocateDangledRequestHandler implements TransportRequestHandler<AllocateDangledRequest> {
120115
@Override
121116
public void messageReceived(final AllocateDangledRequest request, final TransportChannel channel, Task task) throws Exception {
@@ -257,10 +252,6 @@ public static class AllocateDangledResponse extends TransportResponse {
257252
this.ack = ack;
258253
}
259254

260-
public boolean ack() {
261-
return ack;
262-
}
263-
264255
@Override
265256
public void readFrom(StreamInput in) throws IOException {
266257
super.readFrom(in);

server/src/main/java/org/elasticsearch/gateway/MetaStateService.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,11 @@ MetaData loadGlobalState() throws IOException {
198198
*
199199
* @throws WriteStateException if exception when writing state occurs. See also {@link WriteStateException#isDirty()}
200200
*/
201-
public long writeManifestAndCleanup(String reason, Manifest manifest) throws WriteStateException {
201+
public void writeManifestAndCleanup(String reason, Manifest manifest) throws WriteStateException {
202202
logger.trace("[_meta] writing state, reason [{}]", reason);
203203
try {
204204
long generation = MANIFEST_FORMAT.writeAndCleanup(manifest, nodeEnv.nodeDataPaths());
205205
logger.trace("[_meta] state written (generation: {})", generation);
206-
return generation;
207206
} catch (WriteStateException ex) {
208207
throw new WriteStateException(ex.isDirty(), "[_meta]: failed to write meta state", ex);
209208
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ protected static NodeShardsResult buildNodeShardsResult(ShardRouting shard, bool
297297
/**
298298
* Split the list of node shard states into groups yes/no/throttle based on allocation deciders
299299
*/
300-
private NodesToAllocate buildNodesToAllocate(RoutingAllocation allocation,
301-
List<NodeGatewayStartedShards> nodeShardStates,
302-
ShardRouting shardRouting,
303-
boolean forceAllocate) {
300+
private static NodesToAllocate buildNodesToAllocate(RoutingAllocation allocation,
301+
List<NodeGatewayStartedShards> nodeShardStates,
302+
ShardRouting shardRouting,
303+
boolean forceAllocate) {
304304
List<DecidedNode> yesNodeShards = new ArrayList<>();
305305
List<DecidedNode> throttledNodeShards = new ArrayList<>();
306306
List<DecidedNode> noNodeShards = new ArrayList<>();

server/src/main/java/org/elasticsearch/gateway/PriorityComparator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public final int compare(ShardRouting o1, ShardRouting o2) {
5656
return cmp;
5757
}
5858

59-
private int priority(Settings settings) {
59+
private static int priority(Settings settings) {
6060
return IndexMetaData.INDEX_PRIORITY_SETTING.get(settings);
6161
}
6262

63-
private long timeCreated(Settings settings) {
63+
private static long timeCreated(Settings settings) {
6464
return settings.getAsLong(IndexMetaData.SETTING_CREATION_DATE, -1L);
6565
}
6666

server/src/main/java/org/elasticsearch/gateway/ReplicaShardAllocator.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ public AllocateUnassignedDecision makeAllocationDecision(final ShardRouting unas
243243
* YES or THROTTLE). If in explain mode, also returns the node-level explanations as the second element
244244
* in the returned tuple.
245245
*/
246-
private Tuple<Decision, Map<String, NodeAllocationResult>> canBeAllocatedToAtLeastOneNode(ShardRouting shard,
247-
RoutingAllocation allocation) {
246+
private static Tuple<Decision, Map<String, NodeAllocationResult>> canBeAllocatedToAtLeastOneNode(ShardRouting shard,
247+
RoutingAllocation allocation) {
248248
Decision madeDecision = Decision.NO;
249249
final boolean explain = allocation.debugDecision();
250250
Map<String, NodeAllocationResult> nodeDecisions = explain ? new HashMap<>() : null;
@@ -260,7 +260,7 @@ private Tuple<Decision, Map<String, NodeAllocationResult>> canBeAllocatedToAtLea
260260
if (explain) {
261261
madeDecision = decision;
262262
} else {
263-
return Tuple.tuple(decision, nodeDecisions);
263+
return Tuple.tuple(decision, null);
264264
}
265265
} else if (madeDecision.type() == Decision.Type.NO && decision.type() == Decision.Type.THROTTLE) {
266266
madeDecision = decision;
@@ -276,8 +276,8 @@ private Tuple<Decision, Map<String, NodeAllocationResult>> canBeAllocatedToAtLea
276276
* Takes the store info for nodes that have a shard store and adds them to the node decisions,
277277
* leaving the node explanations untouched for those nodes that do not have any store information.
278278
*/
279-
private List<NodeAllocationResult> augmentExplanationsWithStoreInfo(Map<String, NodeAllocationResult> nodeDecisions,
280-
Map<String, NodeAllocationResult> withShardStores) {
279+
private static List<NodeAllocationResult> augmentExplanationsWithStoreInfo(Map<String, NodeAllocationResult> nodeDecisions,
280+
Map<String, NodeAllocationResult> withShardStores) {
281281
if (nodeDecisions == null || withShardStores == null) {
282282
return null;
283283
}
@@ -295,8 +295,8 @@ private List<NodeAllocationResult> augmentExplanationsWithStoreInfo(Map<String,
295295
/**
296296
* Finds the store for the assigned shard in the fetched data, returns null if none is found.
297297
*/
298-
private TransportNodesListShardStoreMetaData.StoreFilesMetaData findStore(ShardRouting shard, RoutingAllocation allocation,
299-
AsyncShardFetch.FetchResult<NodeStoreFilesMetaData> data) {
298+
private static TransportNodesListShardStoreMetaData.StoreFilesMetaData findStore(ShardRouting shard, RoutingAllocation allocation,
299+
AsyncShardFetch.FetchResult<NodeStoreFilesMetaData> data) {
300300
assert shard.currentNodeId() != null;
301301
DiscoveryNode primaryNode = allocation.nodes().get(shard.currentNodeId());
302302
if (primaryNode == null) {

server/src/main/java/org/elasticsearch/gateway/TransportNodesListGatewayMetaState.java

-22
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,10 @@ public Request() {
9494
public Request(String... nodesIds) {
9595
super(nodesIds);
9696
}
97-
98-
@Override
99-
public void readFrom(StreamInput in) throws IOException {
100-
super.readFrom(in);
101-
}
102-
103-
@Override
104-
public void writeTo(StreamOutput out) throws IOException {
105-
super.writeTo(out);
106-
}
10797
}
10898

10999
public static class NodesGatewayMetaState extends BaseNodesResponse<NodeGatewayMetaState> {
110100

111-
NodesGatewayMetaState() {
112-
}
113-
114101
public NodesGatewayMetaState(ClusterName clusterName, List<NodeGatewayMetaState> nodes, List<FailedNodeException> failures) {
115102
super(clusterName, nodes, failures);
116103
}
@@ -135,15 +122,6 @@ public NodeRequest() {
135122
super(nodeId);
136123
}
137124

138-
@Override
139-
public void readFrom(StreamInput in) throws IOException {
140-
super.readFrom(in);
141-
}
142-
143-
@Override
144-
public void writeTo(StreamOutput out) throws IOException {
145-
super.writeTo(out);
146-
}
147125
}
148126

149127
public static class NodeGatewayMetaState extends BaseNodeResponse {

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

+3-8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
import java.io.IOException;
5353
import java.util.List;
54+
import java.util.Objects;
5455

5556
/**
5657
* This transport action is used to fetch the shard version from each node during primary allocation in {@link GatewayAllocator}.
@@ -318,14 +319,8 @@ public boolean equals(Object o) {
318319

319320
NodeGatewayStartedShards that = (NodeGatewayStartedShards) o;
320321

321-
if (primary != that.primary) {
322-
return false;
323-
}
324-
if (allocationId != null ? !allocationId.equals(that.allocationId) : that.allocationId != null) {
325-
return false;
326-
}
327-
return storeException != null ? storeException.equals(that.storeException) : that.storeException == null;
328-
322+
return primary == that.primary && Objects.equals(allocationId, that.allocationId)
323+
&& Objects.equals(storeException, that.storeException);
329324
}
330325

331326
@Override

0 commit comments

Comments
 (0)