Skip to content

Commit 3e58589

Browse files
authored
Ensure SLM stats does not block an in-place upgrade from 7.4 (… (#48412)
7.5+ for SLM requires [stats] object to exist in the cluster state. When doing an in-place upgrade from 7.4 to 7.5+ [stats] does not exist in cluster state, result in an exception on startup [1]. This commit moves the [stats] to be an optional object in the parser and if not found will default to an empty stats object. [1] Caused by: java.lang.IllegalArgumentException: Required [stats]
1 parent aca2631 commit 3e58589

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleMetadata.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class SnapshotLifecycleMetadata implements XPackMetaDataCustom {
6262
throw new IllegalArgumentException("ordered " + POLICIES_FIELD.getPreferredName() + " are not supported");
6363
}, POLICIES_FIELD);
6464
PARSER.declareString(ConstructingObjectParser.constructorArg(), OPERATION_MODE_FIELD);
65-
PARSER.declareObject(ConstructingObjectParser.constructorArg(), (v, o) -> SnapshotLifecycleStats.parse(v), STATS_FIELD);
65+
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (v, o) -> SnapshotLifecycleStats.parse(v), STATS_FIELD);
6666
}
6767

6868
private final Map<String, SnapshotLifecyclePolicyMetadata> snapshotConfigurations;
@@ -74,7 +74,7 @@ public SnapshotLifecycleMetadata(Map<String, SnapshotLifecyclePolicyMetadata> sn
7474
SnapshotLifecycleStats slmStats) {
7575
this.snapshotConfigurations = new HashMap<>(snapshotConfigurations);
7676
this.operationMode = operationMode;
77-
this.slmStats = slmStats;
77+
this.slmStats = slmStats != null ? slmStats : new SnapshotLifecycleStats();
7878
}
7979

8080
public SnapshotLifecycleMetadata(StreamInput in) throws IOException {

x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java

+43
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@
1010
import org.elasticsearch.client.Request;
1111
import org.elasticsearch.client.Response;
1212
import org.elasticsearch.client.ResponseException;
13+
import org.elasticsearch.common.Strings;
1314
import org.elasticsearch.common.settings.Settings;
1415
import org.elasticsearch.common.util.concurrent.ThreadContext;
16+
import org.elasticsearch.common.xcontent.DeprecationHandler;
17+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
1518
import org.elasticsearch.common.xcontent.ObjectPath;
19+
import org.elasticsearch.common.xcontent.XContentBuilder;
20+
import org.elasticsearch.common.xcontent.XContentParser;
21+
import org.elasticsearch.common.xcontent.XContentType;
22+
import org.elasticsearch.common.xcontent.json.JsonXContent;
1623
import org.elasticsearch.common.xcontent.support.XContentMapValues;
1724
import org.elasticsearch.rest.RestStatus;
1825
import org.elasticsearch.rest.action.document.RestGetAction;
@@ -21,13 +28,16 @@
2128
import org.elasticsearch.test.StreamsUtils;
2229
import org.elasticsearch.test.rest.ESRestTestCase;
2330
import org.elasticsearch.upgrades.AbstractFullClusterRestartTestCase;
31+
import org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicy;
32+
import org.elasticsearch.xpack.slm.SnapshotLifecycleStats;
2433
import org.hamcrest.Matcher;
2534
import org.junit.Before;
2635

2736
import java.io.IOException;
2837
import java.nio.charset.StandardCharsets;
2938
import java.util.ArrayList;
3039
import java.util.Base64;
40+
import java.util.Collections;
3141
import java.util.List;
3242
import java.util.Locale;
3343
import java.util.Map;
@@ -293,6 +303,39 @@ public void testRollupAfterRestart() throws Exception {
293303
assertRollUpJob("rollup-job-test");
294304
}
295305
}
306+
307+
public void testSlmStats() throws IOException {
308+
SnapshotLifecyclePolicy slmPolicy = new SnapshotLifecyclePolicy("test-policy", "test-policy", "* * * 31 FEB ? *", "test-repo",
309+
Collections.singletonMap("indices", Collections.singletonList("*")), null);
310+
if (isRunningAgainstOldCluster() && getOldClusterVersion().onOrAfter(Version.V_7_4_0)) {
311+
Request createRepoRequest = new Request("PUT", "_snapshot/test-repo");
312+
String repoCreateJson = "{" +
313+
" \"type\": \"fs\"," +
314+
" \"settings\": {" +
315+
" \"location\": \"test-repo\"" +
316+
" }" +
317+
"}";
318+
createRepoRequest.setJsonEntity(repoCreateJson);
319+
Request createSlmPolicyRequest = new Request("PUT", "_slm/policy/test-policy");
320+
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
321+
String createSlmPolicyJson = Strings.toString(slmPolicy.toXContent(builder, null));
322+
createSlmPolicyRequest.setJsonEntity(createSlmPolicyJson);
323+
}
324+
325+
client().performRequest(createRepoRequest);
326+
client().performRequest(createSlmPolicyRequest);
327+
}
328+
329+
if (isRunningAgainstOldCluster() == false || getOldClusterVersion().onOrAfter(Version.V_7_5_0)) {
330+
Response response = client().performRequest(new Request("GET", "_slm/stats"));
331+
XContentType xContentType = XContentType.fromMediaTypeOrFormat(response.getEntity().getContentType().getValue());
332+
try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY,
333+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, response.getEntity().getContent())) {
334+
assertEquals(new SnapshotLifecycleStats(), SnapshotLifecycleStats.parse(parser));
335+
}
336+
}
337+
338+
}
296339

297340
public void testRollupIDSchemeAfterRestart() throws Exception {
298341
assumeTrue("Rollup can be tested with 6.3.0 and onwards", getOldClusterVersion().onOrAfter(Version.V_6_3_0));

0 commit comments

Comments
 (0)