Skip to content

Commit 0698d73

Browse files
prdoyleelasticsearchmachine
andauthored
Add length limit FileSettingsHealthIndicatorService.description (#121334)
* Add length limit FileSettingsHealthIndicatorService.description * [CI] Auto commit changes from spotless * Add javadocs explaining `fileSettings.descriptionLengthLimit` setting --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent f7901f0 commit 0698d73

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

server/src/main/java/org/elasticsearch/node/NodeConstruction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ public Map<String, String> searchFields() {
10671067
actionModule.getReservedClusterStateService().installStateHandler(new ReservedRepositoryAction(repositoriesService));
10681068
actionModule.getReservedClusterStateService().installStateHandler(new ReservedPipelineAction());
10691069

1070-
FileSettingsHealthIndicatorService fileSettingsHealthIndicatorService = new FileSettingsHealthIndicatorService();
1070+
FileSettingsHealthIndicatorService fileSettingsHealthIndicatorService = new FileSettingsHealthIndicatorService(settings);
10711071
FileSettingsService fileSettingsService = new FileSettingsService(
10721072
clusterService,
10731073
actionModule.getReservedClusterStateService(),

server/src/main/java/org/elasticsearch/reservedstate/service/FileSettingsService.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.elasticsearch.cluster.metadata.ReservedStateMetadata;
2222
import org.elasticsearch.cluster.service.ClusterService;
2323
import org.elasticsearch.common.file.MasterNodeFileWatchingService;
24+
import org.elasticsearch.common.settings.Setting;
25+
import org.elasticsearch.common.settings.Settings;
2426
import org.elasticsearch.env.Environment;
2527
import org.elasticsearch.health.HealthIndicatorDetails;
2628
import org.elasticsearch.health.HealthIndicatorImpact;
@@ -212,7 +214,7 @@ protected void onProcessFileChangesException(Exception e) {
212214
}
213215

214216
@Override
215-
protected void processInitialFileMissing() throws ExecutionException, InterruptedException, IOException {
217+
protected void processInitialFileMissing() throws ExecutionException, InterruptedException {
216218
PlainActionFuture<ActionResponse.Empty> completion = new PlainActionFuture<>();
217219
logger.info("setting file [{}] not found, initializing [{}] as empty", watchedFile(), NAMESPACE);
218220
stateService.initEmpty(NAMESPACE, completion);
@@ -236,11 +238,29 @@ public static class FileSettingsHealthIndicatorService implements HealthIndicato
236238
)
237239
);
238240

241+
/**
242+
* We want a length limit so we don't blow past the indexing limit in the case of a long description string.
243+
* This is an {@code OperatorDynamic} setting so that if the truncation hampers troubleshooting efforts,
244+
* the operator could override it and retry the operation without necessarily restarting the cluster.
245+
*/
246+
public static final String DESCRIPTION_LENGTH_LIMIT_KEY = "fileSettings.descriptionLengthLimit";
247+
static final Setting<Integer> DESCRIPTION_LENGTH_LIMIT = Setting.intSetting(
248+
DESCRIPTION_LENGTH_LIMIT_KEY,
249+
100,
250+
1, // Need room for the ellipsis
251+
Setting.Property.OperatorDynamic
252+
);
253+
254+
private final Settings settings;
239255
private boolean isActive = false;
240256
private long changeCount = 0;
241257
private long failureStreak = 0;
242258
private String mostRecentFailure = null;
243259

260+
public FileSettingsHealthIndicatorService(Settings settings) {
261+
this.settings = settings;
262+
}
263+
244264
public synchronized void startOccurred() {
245265
isActive = true;
246266
failureStreak = 0;
@@ -262,7 +282,16 @@ public synchronized void successOccurred() {
262282

263283
public synchronized void failureOccurred(String description) {
264284
++failureStreak;
265-
mostRecentFailure = description;
285+
mostRecentFailure = limitLength(description);
286+
}
287+
288+
private String limitLength(String description) {
289+
int descriptionLengthLimit = DESCRIPTION_LENGTH_LIMIT.get(settings);
290+
if (description.length() > descriptionLengthLimit) {
291+
return description.substring(0, descriptionLengthLimit - 1) + "…";
292+
} else {
293+
return description;
294+
}
266295
}
267296

268297
@Override

server/src/test/java/org/elasticsearch/action/ingest/ReservedPipelineActionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void setup() {
138138
clusterService,
139139
mock(ReservedClusterStateService.class),
140140
newEnvironment(Settings.EMPTY),
141-
new FileSettingsService.FileSettingsHealthIndicatorService()
141+
new FileSettingsService.FileSettingsHealthIndicatorService(Settings.EMPTY)
142142
)
143143
);
144144
}

server/src/test/java/org/elasticsearch/reservedstate/service/FileSettingsHealthIndicatorServiceTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.reservedstate.service;
1111

12+
import org.elasticsearch.common.settings.Settings;
1213
import org.elasticsearch.health.HealthIndicatorDetails;
1314
import org.elasticsearch.health.HealthIndicatorResult;
1415
import org.elasticsearch.health.SimpleHealthIndicatorDetails;
@@ -21,6 +22,7 @@
2122

2223
import static org.elasticsearch.health.HealthStatus.GREEN;
2324
import static org.elasticsearch.health.HealthStatus.YELLOW;
25+
import static org.elasticsearch.reservedstate.service.FileSettingsService.FileSettingsHealthIndicatorService.DESCRIPTION_LENGTH_LIMIT_KEY;
2426
import static org.elasticsearch.reservedstate.service.FileSettingsService.FileSettingsHealthIndicatorService.FAILURE_SYMPTOM;
2527
import static org.elasticsearch.reservedstate.service.FileSettingsService.FileSettingsHealthIndicatorService.INACTIVE_SYMPTOM;
2628
import static org.elasticsearch.reservedstate.service.FileSettingsService.FileSettingsHealthIndicatorService.NO_CHANGES_SYMPTOM;
@@ -37,7 +39,7 @@ public class FileSettingsHealthIndicatorServiceTests extends ESTestCase {
3739

3840
@Before
3941
public void initialize() {
40-
healthIndicatorService = new FileSettingsHealthIndicatorService();
42+
healthIndicatorService = new FileSettingsHealthIndicatorService(Settings.EMPTY);
4143
}
4244

4345
public void testInitiallyGreen() {}
@@ -101,4 +103,28 @@ public void testGreenYellowYellowGreen() {
101103
healthIndicatorService.calculate(false, null)
102104
);
103105
}
106+
107+
public void testDescriptionIsTruncated() {
108+
checkTruncatedDescription(9, "123456789", "123456789");
109+
checkTruncatedDescription(8, "123456789", "1234567…");
110+
checkTruncatedDescription(1, "12", "…");
111+
}
112+
113+
private void checkTruncatedDescription(int lengthLimit, String description, String expectedTruncatedDescription) {
114+
var service = new FileSettingsHealthIndicatorService(Settings.builder().put(DESCRIPTION_LENGTH_LIMIT_KEY, lengthLimit).build());
115+
service.startOccurred();
116+
service.changeOccurred();
117+
service.failureOccurred(description);
118+
assertEquals(
119+
new HealthIndicatorResult(
120+
"file_settings",
121+
YELLOW,
122+
FAILURE_SYMPTOM,
123+
new SimpleHealthIndicatorDetails(Map.of("failure_streak", 1L, "most_recent_failure", expectedTruncatedDescription)),
124+
STALE_SETTINGS_IMPACT,
125+
List.of()
126+
),
127+
service.calculate(false, null)
128+
);
129+
}
104130
}

server/src/test/java/org/elasticsearch/reservedstate/service/FileSettingsServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void setUp() throws Exception {
138138
List.of(new ReservedClusterSettingsAction(clusterSettings))
139139
)
140140
);
141-
healthIndicatorService = spy(new FileSettingsHealthIndicatorService());
141+
healthIndicatorService = spy(new FileSettingsHealthIndicatorService(Settings.EMPTY));
142142
fileSettingsService = spy(new FileSettingsService(clusterService, controller, env, healthIndicatorService));
143143
}
144144

0 commit comments

Comments
 (0)