Skip to content

Commit 2b1b1b3

Browse files
Remove Dead Code and Duplication from Metadata (#64938)
Just some random spots I found while researching other things.
1 parent cd72f45 commit 2b1b1b3

File tree

6 files changed

+43
-111
lines changed

6 files changed

+43
-111
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/get/TransportGetIndexAction.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.elasticsearch.threadpool.ThreadPool;
3939
import org.elasticsearch.transport.TransportService;
4040

41-
import java.io.IOException;
4241
import java.util.List;
4342
import java.util.stream.Collectors;
4443
import java.util.stream.StreamSupport;
@@ -82,13 +81,8 @@ protected void doMasterOperation(final GetIndexRequest request, String[] concret
8281
switch (feature) {
8382
case MAPPINGS:
8483
if (!doneMappings) {
85-
try {
86-
mappingsResult = state.metadata().findMappings(concreteIndices, indicesService.getFieldFilter());
87-
doneMappings = true;
88-
} catch (IOException e) {
89-
listener.onFailure(e);
90-
return;
91-
}
84+
mappingsResult = state.metadata().findMappings(concreteIndices, indicesService.getFieldFilter());
85+
doneMappings = true;
9286
}
9387
break;
9488
case ALIASES:
@@ -123,8 +117,6 @@ protected void doMasterOperation(final GetIndexRequest request, String[] concret
123117
throw new IllegalStateException("feature [" + feature + "] is not valid");
124118
}
125119
}
126-
listener.onResponse(
127-
new GetIndexResponse(concreteIndices, mappingsResult, aliasesResult, settings, defaultSettings, dataStreams)
128-
);
120+
listener.onResponse(new GetIndexResponse(concreteIndices, mappingsResult, aliasesResult, settings, defaultSettings, dataStreams));
129121
}
130122
}

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetMappingsAction.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@
2626
import org.elasticsearch.action.support.master.info.TransportClusterInfoAction;
2727
import org.elasticsearch.cluster.ClusterState;
2828
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
29-
import org.elasticsearch.cluster.metadata.MappingMetadata;
3029
import org.elasticsearch.cluster.service.ClusterService;
31-
import org.elasticsearch.common.collect.ImmutableOpenMap;
3230
import org.elasticsearch.common.inject.Inject;
3331
import org.elasticsearch.indices.IndicesService;
3432
import org.elasticsearch.threadpool.ThreadPool;
3533
import org.elasticsearch.transport.TransportService;
3634

37-
import java.io.IOException;
38-
3935
public class TransportGetMappingsAction extends TransportClusterInfoAction<GetMappingsRequest, GetMappingsResponse> {
4036

4137
private static final Logger logger = LogManager.getLogger(TransportGetMappingsAction.class);
@@ -55,12 +51,6 @@ public TransportGetMappingsAction(TransportService transportService, ClusterServ
5551
protected void doMasterOperation(final GetMappingsRequest request, String[] concreteIndices, final ClusterState state,
5652
final ActionListener<GetMappingsResponse> listener) {
5753
logger.trace("serving getMapping request based on version {}", state.version());
58-
try {
59-
ImmutableOpenMap<String, MappingMetadata> result =
60-
state.metadata().findMappings(concreteIndices, indicesService.getFieldFilter());
61-
listener.onResponse(new GetMappingsResponse(result));
62-
} catch (IOException e) {
63-
listener.onFailure(e);
64-
}
54+
listener.onResponse(new GetMappingsResponse(state.metadata().findMappings(concreteIndices, indicesService.getFieldFilter())));
6555
}
6656
}

server/src/main/java/org/elasticsearch/cluster/ClusterState.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.cluster;
2121

22-
import com.carrotsearch.hppc.cursors.ObjectCursor;
2322
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
2423
import org.elasticsearch.Version;
2524
import org.elasticsearch.cluster.block.ClusterBlock;
@@ -323,7 +322,7 @@ public enum Metric {
323322
ROUTING_NODES("routing_nodes"),
324323
CUSTOMS("customs");
325324

326-
private static Map<String, Metric> valueToEnum;
325+
private static final Map<String, Metric> valueToEnum;
327326

328327
static {
329328
valueToEnum = new HashMap<>();
@@ -365,7 +364,6 @@ public String toString() {
365364
}
366365

367366
@Override
368-
@SuppressWarnings("unchecked")
369367
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
370368
EnumSet<Metric> metrics = Metric.parseString(params.param("metric", "_all"), true);
371369

@@ -646,19 +644,7 @@ public void writeTo(StreamOutput out) throws IOException {
646644
routingTable.writeTo(out);
647645
nodes.writeTo(out);
648646
blocks.writeTo(out);
649-
// filter out custom states not supported by the other node
650-
int numberOfCustoms = 0;
651-
for (final ObjectCursor<Custom> cursor : customs.values()) {
652-
if (VersionedNamedWriteable.shouldSerialize(out, cursor.value)) {
653-
numberOfCustoms++;
654-
}
655-
}
656-
out.writeVInt(numberOfCustoms);
657-
for (final ObjectCursor<Custom> cursor : customs.values()) {
658-
if (VersionedNamedWriteable.shouldSerialize(out, cursor.value)) {
659-
out.writeNamedWriteable(cursor.value);
660-
}
661-
}
647+
VersionedNamedWriteable.writeVersionedWritables(out, customs);
662648
if (out.getVersion().before(Version.V_8_0_0)) {
663649
out.writeVInt(-1); // used to be minimumMasterNodesOnPublishingMaster, which was used in 7.x for BWC with 6.x
664650
}

server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,7 @@ private ImmutableOpenMap<String, List<AliasMetadata>> findAliases(final String[]
386386
*
387387
*/
388388
public ImmutableOpenMap<String, MappingMetadata> findMappings(String[] concreteIndices,
389-
Function<String, Predicate<String>> fieldFilter)
390-
throws IOException {
389+
Function<String, Predicate<String>> fieldFilter) {
391390
assert concreteIndices != null;
392391
if (concreteIndices.length == 0) {
393392
return ImmutableOpenMap.of();
@@ -560,22 +559,7 @@ public String resolveWriteIndexRouting(@Nullable String routing, String aliasOrI
560559
if (writeIndex == null) {
561560
throw new IllegalArgumentException("alias [" + aliasOrIndex + "] does not have a write index");
562561
}
563-
AliasMetadata aliasMd = writeIndex.getAliases().get(result.getName());
564-
if (aliasMd.indexRouting() != null) {
565-
if (aliasMd.indexRouting().indexOf(',') != -1) {
566-
throw new IllegalArgumentException("index/alias [" + aliasOrIndex + "] provided with routing value ["
567-
+ aliasMd.getIndexRouting() + "] that resolved to several routing values, rejecting operation");
568-
}
569-
if (routing != null) {
570-
if (!routing.equals(aliasMd.indexRouting())) {
571-
throw new IllegalArgumentException("Alias [" + aliasOrIndex + "] has index routing associated with it ["
572-
+ aliasMd.indexRouting() + "], and was provided with routing value [" + routing + "], rejecting operation");
573-
}
574-
}
575-
// Alias routing overrides the parent routing (if any).
576-
return aliasMd.indexRouting();
577-
}
578-
return routing;
562+
return resolveRouting(routing, aliasOrIndex, writeIndex.getAliases().get(result.getName()));
579563
}
580564

581565
/**
@@ -596,7 +580,10 @@ public String resolveIndexRouting(@Nullable String routing, String aliasOrIndex)
596580
if (result.getIndices().size() > 1) {
597581
rejectSingleIndexOperation(aliasOrIndex, result);
598582
}
599-
AliasMetadata aliasMd = alias.getFirstAliasMetadata();
583+
return resolveRouting(routing, aliasOrIndex, alias.getFirstAliasMetadata());
584+
}
585+
586+
private static String resolveRouting(@Nullable String routing, String aliasOrIndex, AliasMetadata aliasMd) {
600587
if (aliasMd.indexRouting() != null) {
601588
if (aliasMd.indexRouting().indexOf(',') != -1) {
602589
throw new IllegalArgumentException("index/alias [" + aliasOrIndex + "] provided with routing value [" +
@@ -709,10 +696,6 @@ public ImmutableOpenMap<String, Custom> customs() {
709696
return this.customs;
710697
}
711698

712-
public ImmutableOpenMap<String, Custom> getCustoms() {
713-
return this.customs;
714-
}
715-
716699
/**
717700
* The collection of index deletions in the cluster.
718701
*/
@@ -748,28 +731,6 @@ public int getTotalOpenIndexShards() {
748731
return this.totalOpenIndexShards;
749732
}
750733

751-
/**
752-
* Identifies whether the array containing type names given as argument refers to all types
753-
* The empty or null array identifies all types
754-
*
755-
* @param types the array containing types
756-
* @return true if the provided array maps to all types, false otherwise
757-
*/
758-
public static boolean isAllTypes(String[] types) {
759-
return types == null || types.length == 0 || isExplicitAllType(types);
760-
}
761-
762-
/**
763-
* Identifies whether the array containing type names given as argument explicitly refers to all types
764-
* The empty or null array doesn't explicitly map to all types
765-
*
766-
* @param types the array containing index names
767-
* @return true if the provided array explicitly maps to all types, false otherwise
768-
*/
769-
public static boolean isExplicitAllType(String[] types) {
770-
return types != null && types.length == 1 && ALL.equals(types[0]);
771-
}
772-
773734
/**
774735
* @param concreteIndex The concrete index to check if routing is required
775736
* @return Whether routing is required according to the mapping for the specified index and type
@@ -973,19 +934,7 @@ public void writeTo(StreamOutput out) throws IOException {
973934
for (ObjectCursor<IndexTemplateMetadata> cursor : templates.values()) {
974935
cursor.value.writeTo(out);
975936
}
976-
// filter out custom states not supported by the other node
977-
int numberOfCustoms = 0;
978-
for (final ObjectCursor<Custom> cursor : customs.values()) {
979-
if (VersionedNamedWriteable.shouldSerialize(out, cursor.value)) {
980-
numberOfCustoms++;
981-
}
982-
}
983-
out.writeVInt(numberOfCustoms);
984-
for (final ObjectCursor<Custom> cursor : customs.values()) {
985-
if (VersionedNamedWriteable.shouldSerialize(out, cursor.value)) {
986-
out.writeNamedWriteable(cursor.value);
987-
}
988-
}
937+
VersionedNamedWriteable.writeVersionedWritables(out, customs);
989938
}
990939

991940
public static Builder builder() {
@@ -1215,8 +1164,7 @@ public Builder indexGraveyard(final IndexGraveyard indexGraveyard) {
12151164
}
12161165

12171166
public IndexGraveyard indexGraveyard() {
1218-
IndexGraveyard graveyard = (IndexGraveyard) getCustom(IndexGraveyard.TYPE);
1219-
return graveyard;
1167+
return (IndexGraveyard) getCustom(IndexGraveyard.TYPE);
12201168
}
12211169

12221170
public Builder updateSettings(Settings settings, String... indices) {
@@ -1275,10 +1223,6 @@ public Builder persistentSettings(Settings settings) {
12751223
return this;
12761224
}
12771225

1278-
public DiffableStringMap hashesOfConsistentSettings() {
1279-
return this.hashesOfConsistentSettings;
1280-
}
1281-
12821226
public Builder hashesOfConsistentSettings(DiffableStringMap hashesOfConsistentSettings) {
12831227
this.hashesOfConsistentSettings = hashesOfConsistentSettings;
12841228
return this;

server/src/main/java/org/elasticsearch/common/io/stream/VersionedNamedWriteable.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
package org.elasticsearch.common.io.stream;
2121

22+
import com.carrotsearch.hppc.cursors.ObjectCursor;
2223
import org.elasticsearch.Version;
24+
import org.elasticsearch.common.collect.ImmutableOpenMap;
25+
26+
import java.io.IOException;
2327

2428
/**
2529
* A {@link NamedWriteable} that has a minimum version associated with it.
@@ -50,4 +54,27 @@ static <T extends VersionedNamedWriteable> boolean shouldSerialize(final StreamO
5054
return out.getVersion().onOrAfter(custom.getMinimalSupportedVersion());
5155
}
5256

57+
/**
58+
* Writes all those values in the given map to {@code out} that pass the version check in {@link #shouldSerialize} as a list.
59+
*
60+
* @param out stream to write to
61+
* @param customs map of customs
62+
* @param <T> type of customs in map
63+
*/
64+
static <T extends VersionedNamedWriteable> void writeVersionedWritables(StreamOutput out, ImmutableOpenMap<String, T> customs)
65+
throws IOException {
66+
// filter out custom states not supported by the other node
67+
int numberOfCustoms = 0;
68+
for (final ObjectCursor<T> cursor : customs.values()) {
69+
if (shouldSerialize(out, cursor.value)) {
70+
numberOfCustoms++;
71+
}
72+
}
73+
out.writeVInt(numberOfCustoms);
74+
for (final ObjectCursor<T> cursor : customs.values()) {
75+
if (shouldSerialize(out, cursor.value)) {
76+
out.writeNamedWriteable(cursor.value);
77+
}
78+
}
79+
}
5380
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappings.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class ElasticsearchMappings {
9797
private ElasticsearchMappings() {
9898
}
9999

100-
static String[] mappingRequiresUpdate(ClusterState state, String[] concreteIndices, Version minVersion) throws IOException {
100+
static String[] mappingRequiresUpdate(ClusterState state, String[] concreteIndices, Version minVersion) {
101101
List<String> indicesToUpdate = new ArrayList<>();
102102

103103
ImmutableOpenMap<String, MappingMetadata> currentMapping = state.metadata().findMappings(concreteIndices,
@@ -156,14 +156,7 @@ public static void addDocMappingIfMissing(String alias,
156156
String[] concreteIndices = indexAbstraction.getIndices().stream().map(IndexMetadata::getIndex).map(Index::getName)
157157
.toArray(String[]::new);
158158

159-
String[] indicesThatRequireAnUpdate;
160-
try {
161-
indicesThatRequireAnUpdate = mappingRequiresUpdate(state, concreteIndices, Version.CURRENT);
162-
} catch (IOException e) {
163-
listener.onFailure(e);
164-
return;
165-
}
166-
159+
final String[] indicesThatRequireAnUpdate = mappingRequiresUpdate(state, concreteIndices, Version.CURRENT);
167160
if (indicesThatRequireAnUpdate.length > 0) {
168161
try {
169162
String mapping = mappingSupplier.get();

0 commit comments

Comments
 (0)