|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.ilm.action; |
9 | 9 |
|
| 10 | +import org.elasticsearch.ElasticsearchException; |
10 | 11 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
11 | 12 | import org.elasticsearch.cluster.metadata.LifecycleExecutionState; |
12 | 13 | import org.elasticsearch.cluster.metadata.ProjectMetadata; |
| 14 | +import org.elasticsearch.common.Strings; |
13 | 15 | import org.elasticsearch.index.IndexVersion; |
14 | 16 | import org.elasticsearch.test.ESTestCase; |
15 | 17 | import org.elasticsearch.xcontent.NamedXContentRegistry; |
16 | 18 | import org.elasticsearch.xcontent.ParseField; |
| 19 | +import org.elasticsearch.xcontent.ToXContent; |
| 20 | +import org.elasticsearch.xcontent.XContentFactory; |
| 21 | +import org.elasticsearch.xcontent.XContentParser; |
| 22 | +import org.elasticsearch.xcontent.XContentParserConfiguration; |
| 23 | +import org.elasticsearch.xcontent.XContentType; |
17 | 24 | import org.elasticsearch.xpack.core.ilm.ErrorStep; |
18 | 25 | import org.elasticsearch.xpack.core.ilm.IndexLifecycleExplainResponse; |
19 | 26 | import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; |
|
31 | 38 |
|
32 | 39 | import static org.elasticsearch.cluster.metadata.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; |
33 | 40 | import static org.elasticsearch.xpack.ilm.action.TransportExplainLifecycleAction.getIndexLifecycleExplainResponse; |
| 41 | +import static org.hamcrest.Matchers.equalTo; |
34 | 42 | import static org.hamcrest.Matchers.is; |
35 | 43 | import static org.hamcrest.Matchers.notNullValue; |
36 | 44 | import static org.hamcrest.Matchers.nullValue; |
@@ -217,6 +225,84 @@ public void testGetIndexLifecycleExplainResponse_rolloverOnlyIfHasDocuments_adds |
217 | 225 | assertThat(rolloverAction.getConditions().getMinDocs(), is(1L)); |
218 | 226 | } |
219 | 227 |
|
| 228 | + public void testPreviousStepInfoTruncationDoesNotBreakExplainJson() throws Exception { |
| 229 | + final String policyName = "policy"; |
| 230 | + final String indexName = "index"; |
| 231 | + |
| 232 | + final String longReasonMessage = "a".repeat(LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH); |
| 233 | + final String errorJsonWhichWillBeTruncated = Strings.toString((builder, params) -> { |
| 234 | + ElasticsearchException.generateThrowableXContent( |
| 235 | + builder, |
| 236 | + ToXContent.EMPTY_PARAMS, |
| 237 | + new IllegalArgumentException(longReasonMessage) |
| 238 | + ); |
| 239 | + return builder; |
| 240 | + }); |
| 241 | + |
| 242 | + final LifecycleExecutionState stateWithTruncatedStepInfo = LifecycleExecutionState.builder() |
| 243 | + .setPhase("hot") |
| 244 | + .setAction("rollover") |
| 245 | + .setStep("some_step") |
| 246 | + .setPhaseDefinition(PHASE_DEFINITION) |
| 247 | + .setStepInfo(errorJsonWhichWillBeTruncated) |
| 248 | + .build(); |
| 249 | + |
| 250 | + // Simulate transition to next step where previous_step_info is copied |
| 251 | + final LifecycleExecutionState stateWithPreviousStepInfo = LifecycleExecutionState.builder(stateWithTruncatedStepInfo) |
| 252 | + .setPreviousStepInfo(stateWithTruncatedStepInfo.stepInfo()) |
| 253 | + .setStepInfo(null) |
| 254 | + .build(); |
| 255 | + |
| 256 | + final IndexMetadata indexMetadata = IndexMetadata.builder(indexName) |
| 257 | + .settings(settings(IndexVersion.current()).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) |
| 258 | + .numberOfShards(1) |
| 259 | + .numberOfReplicas(0) |
| 260 | + .putCustom(ILM_CUSTOM_METADATA_KEY, stateWithPreviousStepInfo.asMap()) |
| 261 | + .build(); |
| 262 | + |
| 263 | + final ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) |
| 264 | + .put(indexMetadata, true) |
| 265 | + .putCustom( |
| 266 | + IndexLifecycleMetadata.TYPE, |
| 267 | + new IndexLifecycleMetadata( |
| 268 | + Map.of(policyName, LifecyclePolicyMetadataTests.createRandomPolicyMetadata(policyName)), |
| 269 | + randomFrom(OperationMode.values()) |
| 270 | + ) |
| 271 | + ) |
| 272 | + .build(); |
| 273 | + |
| 274 | + final IndexLifecycleExplainResponse response = TransportExplainLifecycleAction.getIndexLifecycleExplainResponse( |
| 275 | + indexName, |
| 276 | + project, |
| 277 | + false, |
| 278 | + true, |
| 279 | + REGISTRY, |
| 280 | + randomBoolean() |
| 281 | + ); |
| 282 | + |
| 283 | + final String serialized = Strings.toString(response); |
| 284 | + // test we produce valid JSON |
| 285 | + try ( |
| 286 | + XContentParser p = XContentFactory.xContent(XContentType.JSON) |
| 287 | + .createParser(XContentParserConfiguration.EMPTY.withRegistry(REGISTRY), serialized) |
| 288 | + ) { |
| 289 | + final IndexLifecycleExplainResponse deserialized = IndexLifecycleExplainResponse.PARSER.apply(p, null); |
| 290 | + assertThat(deserialized.toString(), equalTo(response.toString())); |
| 291 | + final String actualPreviousStepInfo = deserialized.getPreviousStepInfo().utf8ToString(); |
| 292 | + |
| 293 | + final String expectedPreviousStepInfoFormat = """ |
| 294 | + {"type":"illegal_argument_exception","reason":"%s"}"""; |
| 295 | + final int jsonCharsBeforeReasonValue = Strings.format(expectedPreviousStepInfoFormat, "").length() - 2; // -2 for `"}` |
| 296 | + final String expectedReason = Strings.format( |
| 297 | + "%s... (%d chars truncated)", |
| 298 | + "a".repeat(LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH - jsonCharsBeforeReasonValue), |
| 299 | + jsonCharsBeforeReasonValue |
| 300 | + ); |
| 301 | + final String expectedPreviousStepInfo = Strings.format(expectedPreviousStepInfoFormat, expectedReason); |
| 302 | + assertThat(actualPreviousStepInfo, equalTo(expectedPreviousStepInfo)); |
| 303 | + } |
| 304 | + } |
| 305 | + |
220 | 306 | private static IndexLifecycleMetadata createIndexLifecycleMetadata() { |
221 | 307 | return new IndexLifecycleMetadata( |
222 | 308 | Map.of(POLICY_NAME, LifecyclePolicyMetadataTests.createRandomPolicyMetadata(POLICY_NAME)), |
|
0 commit comments