Skip to content

Commit d86138b

Browse files
committed
niels niels
1 parent e87f846 commit d86138b

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ public Map<String, String> asMap() {
290290
/**
291291
* Truncates a potentially long JSON string to ensure it does not exceed {@link #MAXIMUM_STEP_INFO_STRING_LENGTH}. If truncation
292292
* occurs, an explanation suffix is appended to the truncated string indicating <i>approximately</i> how many characters were removed.
293+
* We return an approximation because we're valuing code simplicity over accuracy in this area.
294+
*
293295
* @param json the JSON string to potentially truncate
294296
* @return the original JSON string if its length is within the limit, otherwise a truncated version with an explanation suffix - in
295297
* correct JSON format

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionStateTests.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.HashMap;
1616
import java.util.Map;
1717

18+
import static org.hamcrest.Matchers.endsWith;
1819
import static org.hamcrest.Matchers.equalTo;
1920
import static org.hamcrest.Matchers.hasLength;
2021
import static org.hamcrest.Matchers.not;
@@ -36,15 +37,8 @@ public void testTruncatingStepInfo() {
3637
+ randomAlphanumericOfLength(LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH + 100)
3738
+ "\"}";
3839
LifecycleExecutionState newState = LifecycleExecutionState.builder(state).setStepInfo(longStepInfo).build();
39-
assertThat(newState.stepInfo().length(), equalTo(LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH));
40-
assertThat(
41-
newState.stepInfo()
42-
.substring(
43-
LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH - 28,
44-
LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH
45-
),
46-
equalTo("... (~111 chars truncated)\"}")
47-
);
40+
assertThat(newState.stepInfo(), hasLength(LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH));
41+
assertThat(newState.stepInfo(), endsWith("... (~111 chars truncated)\"}"));
4842
}
4943

5044
public void testEmptyValuesAreNotSerialized() {
@@ -154,11 +148,13 @@ public void testGetCurrentStepKey() {
154148
assertNull(error6.getMessage());
155149
}
156150

157-
public void testPotentiallyTruncateLongJsonWithExplanationNotTruncated() {
151+
/** test that strings with length less than or equal to maximum string length are not truncated and returned as-is */
152+
public void testPotentiallyTruncateLongJsonWithExplanationNoNeedToTruncate() {
158153
final String input = randomAlphaOfLengthBetween(0, LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH);
159154
assertSame(input, LifecycleExecutionState.potentiallyTruncateLongJsonWithExplanation(input));
160155
}
161156

157+
/** test that string with length one character over the max limit has truncation applied to it and has correct explanation */
162158
public void testPotentiallyTruncateLongJsonWithExplanationOneCharTruncated() {
163159
final String jsonBaseFormat = "{\"key\": \"%s\"}";
164160
final int baseLength = Strings.format(jsonBaseFormat, "").length();
@@ -175,6 +171,7 @@ public void testPotentiallyTruncateLongJsonWithExplanationOneCharTruncated() {
175171
assertEquals(expectedOutput, actualOutput);
176172
}
177173

174+
/** test that string with length two characters over the max limit has truncation applied to it and has correct explanation */
178175
public void testPotentiallyTruncateLongJsonWithExplanationTwoCharsTruncated() {
179176
final String jsonBaseFormat = "{\"key\": \"%s\"}";
180177
final int baseLength = Strings.format(jsonBaseFormat, "").length();

x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ExplainLifecycleIT.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,19 @@ public void testStepInfoPreservedOnAutoRetry() throws Exception {
320320
}, 30, TimeUnit.SECONDS);
321321
}
322322

323+
/**
324+
* Test that, when there is an ILM <code>previous_step_info</code> that has had truncation applied to it (due to it being too long),
325+
* the truncation:
326+
* <ul>
327+
* <li>doesn't break the JSON returned in <code>/{index}/_ilm/explain</code></li>
328+
* <li>truncates as expected</li>
329+
* </ul>
330+
* We test this by creating an ILM policy that rolls-over at 1 document, and an index pattern that has a non-existing rollover alias
331+
* that has a very long name (to trip the truncation due to the rollover alias name being in the <code>previous_step_info</code>).
332+
* <p>
333+
* We then index a document, wait for attempted rollover, and assert that we get valid JSON and expected truncated message
334+
* in <code>/{index}/_ilm/explain</code>.
335+
*/
323336
public void testTruncatedPreviousStepInfoDoesNotBreakExplainJson() throws Exception {
324337
final String policyName = "policy-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT);
325338

0 commit comments

Comments
 (0)