Skip to content

Commit e6409d1

Browse files
ywangdkderusso
authored andcommitted
Remove obsolete Metadata.FORMAT field and usages (elastic#129519)
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: elastic#114698
1 parent d1b87a3 commit e6409d1

File tree

6 files changed

+33
-91
lines changed

6 files changed

+33
-91
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;
@@ -2101,30 +2099,6 @@ static <C extends MetadataCustom<C>> void parseCustomObject(
21012099
}
21022100
}
21032101

2104-
private static final ToXContent.Params FORMAT_PARAMS;
2105-
static {
2106-
Map<String, String> params = Maps.newMapWithExpectedSize(2);
2107-
params.put("binary", "true");
2108-
params.put(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_GATEWAY);
2109-
FORMAT_PARAMS = new ToXContent.MapParams(params);
2110-
}
2111-
2112-
/**
2113-
* State format for {@link Metadata} to write to and load from disk
2114-
*/
2115-
public static final MetadataStateFormat<Metadata> FORMAT = new MetadataStateFormat<>(GLOBAL_STATE_FILE_PREFIX) {
2116-
2117-
@Override
2118-
public void toXContent(XContentBuilder builder, Metadata state) throws IOException {
2119-
ChunkedToXContent.wrapAsToXContent(state).toXContent(builder, FORMAT_PARAMS);
2120-
}
2121-
2122-
@Override
2123-
public Metadata fromXContent(XContentParser parser) throws IOException {
2124-
return Builder.fromXContent(parser);
2125-
}
2126-
};
2127-
21282102
private volatile Metadata.ProjectLookup projectLookup = null;
21292103

21302104
/**

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
import org.elasticsearch.xcontent.NamedXContentRegistry;
2222

2323
import java.io.IOException;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
2426
import java.util.ArrayList;
2527
import java.util.List;
2628
import java.util.function.Predicate;
2729

30+
import static org.elasticsearch.cluster.metadata.Metadata.GLOBAL_STATE_FILE_PREFIX;
31+
2832
/**
2933
* Handles writing and loading {@link Manifest}, {@link Metadata} and {@link IndexMetadata} as used for cluster state persistence in
3034
* versions prior to {@link Version#V_7_6_0}, used to read this older format during an upgrade from these versions.
@@ -75,20 +79,29 @@ List<IndexMetadata> loadIndicesStates(Predicate<String> excludeIndexPathIdsPredi
7579
return indexMetadataList;
7680
}
7781

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-
8582
/**
8683
* Creates empty cluster state file on disk, deleting global metadata and unreferencing all index metadata
8784
* (only used for dangling indices at that point).
8885
*/
8986
public void unreferenceAll() throws IOException {
9087
Manifest.FORMAT.writeAndCleanup(Manifest.empty(), nodeEnv.nodeDataPaths()); // write empty file so that indices become unreferenced
91-
Metadata.FORMAT.cleanupOldFiles(Long.MAX_VALUE, nodeEnv.nodeDataPaths());
88+
cleanUpGlobalStateFiles();
89+
}
90+
91+
private void cleanUpGlobalStateFiles() throws IOException {
92+
for (Path location : nodeEnv.nodeDataPaths()) {
93+
logger.trace("cleanupOldFiles: cleaning up {} for global state files", location);
94+
final Path stateLocation = location.resolve(MetadataStateFormat.STATE_DIR_NAME);
95+
try (var paths = Files.list(stateLocation)) {
96+
paths.filter(file -> file.getFileName().toString().startsWith(GLOBAL_STATE_FILE_PREFIX)).forEach(file -> {
97+
try {
98+
Files.deleteIfExists(file);
99+
} catch (IOException e) {
100+
logger.trace("failed to delete global state file: {}", file);
101+
}
102+
});
103+
}
104+
}
92105
}
93106

94107
/**

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)