Skip to content

Commit 4493768

Browse files
committed
Remove obsolete Metadata.FORMAT field and usages
The only production usage is for cleaning up all global state files. It is replaced by directly calling the relevant method without creating the FORAMT instance. Test only usages are either replaced by equivalent method calls or dropped. Relates: #114698
1 parent b0daa5a commit 4493768

File tree

7 files changed

+41
-94
lines changed

7 files changed

+41
-94
lines changed

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.elasticsearch.core.FixForMultiProject;
4141
import org.elasticsearch.core.Nullable;
4242
import org.elasticsearch.core.Tuple;
43-
import org.elasticsearch.gateway.MetadataStateFormat;
4443
import org.elasticsearch.index.Index;
4544
import org.elasticsearch.index.IndexNotFoundException;
4645
import org.elasticsearch.index.IndexVersion;
@@ -50,7 +49,6 @@
5049
import org.elasticsearch.xcontent.NamedObjectNotFoundException;
5150
import org.elasticsearch.xcontent.NamedXContentRegistry;
5251
import org.elasticsearch.xcontent.ToXContent;
53-
import org.elasticsearch.xcontent.XContentBuilder;
5452
import org.elasticsearch.xcontent.XContentParser;
5553

5654
import java.io.IOException;
@@ -2051,30 +2049,6 @@ static <C extends MetadataCustom<C>> void parseCustomObject(
20512049
}
20522050
}
20532051

2054-
private static final ToXContent.Params FORMAT_PARAMS;
2055-
static {
2056-
Map<String, String> params = Maps.newMapWithExpectedSize(2);
2057-
params.put("binary", "true");
2058-
params.put(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_GATEWAY);
2059-
FORMAT_PARAMS = new ToXContent.MapParams(params);
2060-
}
2061-
2062-
/**
2063-
* State format for {@link Metadata} to write to and load from disk
2064-
*/
2065-
public static final MetadataStateFormat<Metadata> FORMAT = new MetadataStateFormat<>(GLOBAL_STATE_FILE_PREFIX) {
2066-
2067-
@Override
2068-
public void toXContent(XContentBuilder builder, Metadata state) throws IOException {
2069-
ChunkedToXContent.wrapAsToXContent(state).toXContent(builder, FORMAT_PARAMS);
2070-
}
2071-
2072-
@Override
2073-
public Metadata fromXContent(XContentParser parser) throws IOException {
2074-
return Builder.fromXContent(parser);
2075-
}
2076-
};
2077-
20782052
private volatile Metadata.ProjectLookup projectLookup = null;
20792053

20802054
/**

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.apache.logging.log4j.LogManager;
1313
import org.apache.logging.log4j.Logger;
14+
import org.apache.lucene.store.NIOFSDirectory;
1415
import org.elasticsearch.Version;
1516
import org.elasticsearch.cluster.metadata.IndexMetadata;
1617
import org.elasticsearch.cluster.metadata.Manifest;
@@ -25,6 +26,8 @@
2526
import java.util.List;
2627
import java.util.function.Predicate;
2728

29+
import static org.elasticsearch.cluster.metadata.Metadata.GLOBAL_STATE_FILE_PREFIX;
30+
2831
/**
2932
* Handles writing and loading {@link Manifest}, {@link Metadata} and {@link IndexMetadata} as used for cluster state persistence in
3033
* versions prior to {@link Version#V_7_6_0}, used to read this older format during an upgrade from these versions.
@@ -75,20 +78,13 @@ List<IndexMetadata> loadIndicesStates(Predicate<String> excludeIndexPathIdsPredi
7578
return indexMetadataList;
7679
}
7780

78-
/**
79-
* Loads the global state, *without* index state
80-
*/
81-
Metadata loadGlobalState() throws IOException {
82-
return Metadata.FORMAT.loadLatestState(logger, namedXContentRegistry, nodeEnv.nodeDataPaths());
83-
}
84-
8581
/**
8682
* Creates empty cluster state file on disk, deleting global metadata and unreferencing all index metadata
8783
* (only used for dangling indices at that point).
8884
*/
8985
public void unreferenceAll() throws IOException {
9086
Manifest.FORMAT.writeAndCleanup(Manifest.empty(), nodeEnv.nodeDataPaths()); // write empty file so that indices become unreferenced
91-
Metadata.FORMAT.cleanupOldFiles(Long.MAX_VALUE, nodeEnv.nodeDataPaths());
87+
MetadataStateFormat.cleanupOldFiles(GLOBAL_STATE_FILE_PREFIX, Long.MAX_VALUE, nodeEnv.nodeDataPaths(), NIOFSDirectory::new);
9288
}
9389

9490
/**

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.common.lucene.store.IndexOutputOutputStream;
2424
import org.elasticsearch.common.lucene.store.InputStreamIndexInput;
2525
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
26+
import org.elasticsearch.core.CheckedFunction;
2627
import org.elasticsearch.core.IOUtils;
2728
import org.elasticsearch.core.Nullable;
2829
import org.elasticsearch.core.Tuple;
@@ -349,12 +350,29 @@ protected Directory newDirectory(Path dir) throws IOException {
349350
* @param currentGeneration state generation to keep.
350351
* @param locations state paths.
351352
*/
352-
public void cleanupOldFiles(final long currentGeneration, Path[] locations) {
353-
final String fileNameToKeep = getStateFileName(currentGeneration);
353+
public void cleanupOldFiles(final long currentGeneration, final Path[] locations) {
354+
cleanupOldFiles(prefix, currentGeneration, locations, this::newDirectory);
355+
}
356+
357+
/**
358+
* Clean ups all state files not matching passed generation.
359+
*
360+
* @param prefix filename prefix for filtering state files.
361+
* @param currentGeneration state generation to keep.
362+
* @param locations state paths.
363+
* @param directoryFunc function to create a {@link Directory} for the given path.
364+
*/
365+
public static void cleanupOldFiles(
366+
final String prefix,
367+
final long currentGeneration,
368+
final Path[] locations,
369+
final CheckedFunction<Path, Directory, IOException> directoryFunc
370+
) {
371+
final String fileNameToKeep = getStateFileName(prefix, currentGeneration);
354372
for (Path location : locations) {
355373
logger.trace("cleanupOldFiles: cleaning up {}", location);
356374
Path stateLocation = location.resolve(STATE_DIR_NAME);
357-
try (Directory stateDir = newDirectory(stateLocation)) {
375+
try (Directory stateDir = directoryFunc.apply(stateLocation)) {
358376
for (String file : stateDir.listAll()) {
359377
if (file.startsWith(prefix) && file.equals(fileNameToKeep) == false) {
360378
deleteFileIgnoreExceptions(stateLocation, stateDir, file);
@@ -412,6 +430,10 @@ List<Path> findStateFilesByGeneration(final long generation, Path... locations)
412430
}
413431

414432
public String getStateFileName(long generation) {
433+
return getStateFileName(prefix, generation);
434+
}
435+
436+
public static String getStateFileName(String prefix, long generation) {
415437
return prefix + generation + STATE_FILE_EXTENSION;
416438
}
417439

server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ public void testXContentWithIndexGraveyard() throws IOException {
629629
final Metadata originalMeta = Metadata.builder().put(ProjectMetadata.builder(projectId).indexGraveyard(graveyard)).build();
630630
final XContentBuilder builder = JsonXContent.contentBuilder();
631631
builder.startObject();
632-
Metadata.FORMAT.toXContent(builder, originalMeta);
632+
ChunkedToXContent.wrapAsToXContent(originalMeta).toXContent(builder, formatParams());
633633
builder.endObject();
634634
try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
635635
final Metadata fromXContentMeta = Metadata.fromXContent(parser);
@@ -647,7 +647,7 @@ public void testXContentClusterUUID() throws IOException {
647647
.build();
648648
final XContentBuilder builder = JsonXContent.contentBuilder();
649649
builder.startObject();
650-
Metadata.FORMAT.toXContent(builder, originalMeta);
650+
ChunkedToXContent.wrapAsToXContent(originalMeta).toXContent(builder, formatParams());
651651
builder.endObject();
652652
try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
653653
final Metadata fromXContentMeta = Metadata.fromXContent(parser);
@@ -732,7 +732,7 @@ public void testXContentWithCoordinationMetadata() throws IOException {
732732

733733
final XContentBuilder builder = JsonXContent.contentBuilder();
734734
builder.startObject();
735-
Metadata.FORMAT.toXContent(builder, metadata);
735+
ChunkedToXContent.wrapAsToXContent(metadata).toXContent(builder, formatParams());
736736
builder.endObject();
737737

738738
try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
@@ -3345,6 +3345,10 @@ private static CreateIndexResult createIndices(int numIndices, int numBackingInd
33453345
return new CreateIndexResult(indices, backingIndices, b.build());
33463346
}
33473347

3348+
private static ToXContent.Params formatParams() {
3349+
return new ToXContent.MapParams(Map.of("binary", "true", Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_GATEWAY));
3350+
}
3351+
33483352
private static class CreateIndexResult {
33493353
final List<Index> indices;
33503354
final List<Index> backingIndices;

server/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetadataTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ public void testSimpleJsonFromAndTo() throws IOException {
133133

134134
XContentBuilder builder = JsonXContent.contentBuilder();
135135
builder.startObject();
136-
Metadata.FORMAT.toXContent(builder, metadata);
136+
ChunkedToXContent.wrapAsToXContent(metadata)
137+
.toXContent(
138+
builder,
139+
new ToXContent.MapParams(Map.of("binary", "true", Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_GATEWAY))
140+
);
137141
builder.endObject();
138142

139143
Metadata parsedMetadata;

server/src/test/java/org/elasticsearch/gateway/MetaStateServiceTests.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
package org.elasticsearch.gateway;
1010

1111
import org.elasticsearch.cluster.metadata.IndexMetadata;
12-
import org.elasticsearch.cluster.metadata.Metadata;
1312
import org.elasticsearch.common.UUIDs;
14-
import org.elasticsearch.common.settings.Settings;
1513
import org.elasticsearch.env.NodeEnvironment;
1614
import org.elasticsearch.index.Index;
1715
import org.elasticsearch.index.IndexVersion;
@@ -53,20 +51,4 @@ public void testWriteLoadIndex() throws Exception {
5351
public void testLoadMissingIndex() throws Exception {
5452
assertThat(metaStateService.loadIndexState(new Index("test1", "test1UUID")), nullValue());
5553
}
56-
57-
public void testWriteLoadGlobal() throws Exception {
58-
Metadata metadata = Metadata.builder().persistentSettings(Settings.builder().put("test1", "value1").build()).build();
59-
MetaStateWriterUtils.writeGlobalState(env, "test_write", metadata);
60-
assertThat(metaStateService.loadGlobalState().persistentSettings(), equalTo(metadata.persistentSettings()));
61-
}
62-
63-
public void testWriteGlobalStateWithIndexAndNoIndexIsLoaded() throws Exception {
64-
Metadata metadata = Metadata.builder().persistentSettings(Settings.builder().put("test1", "value1").build()).build();
65-
IndexMetadata index = indexMetadata("test1");
66-
Metadata metadataWithIndex = Metadata.builder(metadata).put(index, true).build();
67-
68-
MetaStateWriterUtils.writeGlobalState(env, "test_write", metadataWithIndex);
69-
assertThat(metaStateService.loadGlobalState().persistentSettings(), equalTo(metadata.persistentSettings()));
70-
assertThat(metaStateService.loadGlobalState().getProject().hasIndex("test1"), equalTo(false));
71-
}
7254
}

test/framework/src/main/java/org/elasticsearch/gateway/MetaStateWriterUtils.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import org.apache.logging.log4j.Logger;
1414
import org.elasticsearch.Version;
1515
import org.elasticsearch.cluster.metadata.IndexMetadata;
16-
import org.elasticsearch.cluster.metadata.Manifest;
17-
import org.elasticsearch.cluster.metadata.Metadata;
1816
import org.elasticsearch.env.NodeEnvironment;
1917
import org.elasticsearch.index.Index;
2018

@@ -29,22 +27,6 @@ private MetaStateWriterUtils() {
2927
throw new AssertionError("static methods only");
3028
}
3129

32-
/**
33-
* Writes manifest file (represented by {@link Manifest}) to disk and performs cleanup of old manifest state file if
34-
* the write succeeds or newly created manifest state if the write fails.
35-
*
36-
* @throws WriteStateException if exception when writing state occurs. See also {@link WriteStateException#isDirty()}
37-
*/
38-
public static void writeManifestAndCleanup(NodeEnvironment nodeEnv, String reason, Manifest manifest) throws WriteStateException {
39-
logger.trace("[_meta] writing state, reason [{}]", reason);
40-
try {
41-
long generation = Manifest.FORMAT.writeAndCleanup(manifest, nodeEnv.nodeDataPaths());
42-
logger.trace("[_meta] state written (generation: {})", generation);
43-
} catch (WriteStateException ex) {
44-
throw new WriteStateException(ex.isDirty(), "[_meta]: failed to write meta state", ex);
45-
}
46-
}
47-
4830
/**
4931
* Writes the index state.
5032
* <p>
@@ -65,21 +47,4 @@ public static long writeIndex(NodeEnvironment nodeEnv, String reason, IndexMetad
6547
}
6648
}
6749

68-
/**
69-
* Writes the global state, *without* the indices states.
70-
*
71-
* @throws WriteStateException if exception when writing state occurs. {@link WriteStateException#isDirty()} will always return
72-
* false, because new global state file is not yet referenced by manifest file.
73-
*/
74-
static long writeGlobalState(NodeEnvironment nodeEnv, String reason, Metadata metadata) throws WriteStateException {
75-
logger.trace("[_global] writing state, reason [{}]", reason);
76-
try {
77-
long generation = Metadata.FORMAT.write(metadata, nodeEnv.nodeDataPaths());
78-
logger.trace("[_global] state written");
79-
return generation;
80-
} catch (WriteStateException ex) {
81-
throw new WriteStateException(false, "[_global]: failed to write global state", ex);
82-
}
83-
}
84-
8550
}

0 commit comments

Comments
 (0)