Skip to content

Commit 5afe131

Browse files
fix[backend](index-removal): added index verification before removal
1 parent 8b9dc72 commit 5afe131

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

backend/src/main/java/com/park/utmstack/service/elasticsearch/ElasticsearchService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.park.utmstack.service.MailService;
1111
import com.park.utmstack.service.UtmSpaceNotificationControlService;
1212
import com.park.utmstack.service.application_events.ApplicationEventService;
13+
import com.park.utmstack.service.index_policy.IndexPolicyService;
1314
import com.park.utmstack.util.chart_builder.IndexPropertyType;
1415
import com.park.utmstack.util.exceptions.OpenSearchIndexNotFoundException;
1516
import com.park.utmstack.util.exceptions.UtmElasticsearchException;
@@ -57,16 +58,19 @@ public class ElasticsearchService {
5758
private final UserRepository userRepository;
5859
private final MailService mailService;
5960
private final UtmSpaceNotificationControlService spaceNotificationControlService;
61+
private final IndexPolicyService indexPolicyService;
6062
private final OpensearchClientBuilder client;
6163

6264
public ElasticsearchService(ApplicationEventService eventService, UserRepository userRepository,
6365
MailService mailService,
6466
UtmSpaceNotificationControlService spaceNotificationControlService,
67+
IndexPolicyService indexPolicyService,
6568
OpensearchClientBuilder client) {
6669
this.eventService = eventService;
6770
this.userRepository = userRepository;
6871
this.mailService = mailService;
6972
this.spaceNotificationControlService = spaceNotificationControlService;
73+
this.indexPolicyService = indexPolicyService;
7074
this.client = client;
7175
}
7276

@@ -284,6 +288,11 @@ private void deleteOldestIndices() {
284288
if (opt.isEmpty() || opt.get().getResume().getDiskUsedPercent() < 70)
285289
break;
286290

291+
if (!indexPolicyService.isIndexRemovable(index.index())) {
292+
log.info("{} Skipping index {} because it is not in a removable state", ctx, index.index());
293+
continue;
294+
}
295+
287296
try {
288297
// Delete oldest indices
289298
deleteIndex(Collections.singletonList(index.index()));

backend/src/main/java/com/park/utmstack/service/index_policy/IndexPolicyService.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.park.utmstack.service.index_policy;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
45
import com.park.utmstack.config.Constants;
56
import com.park.utmstack.domain.index_pattern.enums.SystemIndexPattern;
67
import com.park.utmstack.domain.index_policy.*;
@@ -37,6 +38,45 @@ public IndexPolicyService(ApplicationContext applicationContext,
3738
this.client = client;
3839
}
3940

41+
/**
42+
* Check if an index is in a removable state (STATE_DELETE or STATE_SAFE_DELETE)
43+
*
44+
* @param index: Index name
45+
* @return True if removable, false otherwise
46+
*/
47+
public boolean isIndexRemovable(String index) {
48+
final String ctx = CLASSNAME + ".isIndexRemovable";
49+
try {
50+
final String url = String.format("_plugins/_ism/explain/%1$s", index);
51+
try (Response rs = client.getClient().executeHttpRequest(url, null, null, HttpMethod.GET)) {
52+
if (!rs.isSuccessful() || rs.body() == null)
53+
return false;
54+
55+
String body = rs.body().string();
56+
JsonObject json = new Gson().fromJson(body, JsonObject.class);
57+
if (json != null && json.has(index)) {
58+
JsonObject indexInfo = json.getAsJsonObject(index);
59+
String state = null;
60+
if (indexInfo.has("index.plugins.index_state_management.current_state")) {
61+
state = indexInfo.get("index.plugins.index_state_management.current_state").getAsString();
62+
} else if (indexInfo.has("index.opendistro.index_state_management.current_state")) {
63+
state = indexInfo.get("index.opendistro.index_state_management.current_state").getAsString();
64+
} else if (indexInfo.has("opendistro.index_state_management.current_state")) {
65+
state = indexInfo.get("opendistro.index_state_management.current_state").getAsString();
66+
}
67+
68+
if (state != null) {
69+
return Constants.STATE_DELETE.equals(state) || Constants.STATE_SAFE_DELETE.equals(state);
70+
}
71+
}
72+
}
73+
return false;
74+
} catch (Exception e) {
75+
log.error("{}: {}", ctx, e.getMessage());
76+
return false;
77+
}
78+
}
79+
4080
@EventListener(IndexPatternsReadyEvent.class)
4181
public void init() throws Exception {
4282
final String ctx = CLASSNAME + ".init";

0 commit comments

Comments
 (0)