Skip to content

Commit 096a524

Browse files
authored
Merge pull request #1969 from utmstack/hotfix/index_removal
Address issue #1968
2 parents 36d07d7 + b4f3bf4 commit 096a524

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

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

Lines changed: 17 additions & 5 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;
@@ -43,6 +44,7 @@
4344
import java.time.temporal.ChronoUnit;
4445
import java.util.*;
4546
import java.util.stream.Collectors;
47+
import java.util.concurrent.TimeUnit;
4648

4749
/**
4850
* @author Leonardo M. López
@@ -56,16 +58,19 @@ public class ElasticsearchService {
5658
private final UserRepository userRepository;
5759
private final MailService mailService;
5860
private final UtmSpaceNotificationControlService spaceNotificationControlService;
61+
private final IndexPolicyService indexPolicyService;
5962
private final OpensearchClientBuilder client;
6063

6164
public ElasticsearchService(ApplicationEventService eventService, UserRepository userRepository,
6265
MailService mailService,
6366
UtmSpaceNotificationControlService spaceNotificationControlService,
67+
IndexPolicyService indexPolicyService,
6468
OpensearchClientBuilder client) {
6569
this.eventService = eventService;
6670
this.userRepository = userRepository;
6771
this.mailService = mailService;
6872
this.spaceNotificationControlService = spaceNotificationControlService;
73+
this.indexPolicyService = indexPolicyService;
6974
this.client = client;
7075
}
7176

@@ -278,6 +283,16 @@ private void deleteOldestIndices() {
278283

279284
// Indices are returned from oldest to newest ordered by creation.date asc
280285
for (IndicesRecord index : indices) {
286+
Optional<ElasticCluster> opt = getClusterStatus();
287+
288+
if (opt.isEmpty() || opt.get().getResume().getDiskUsedPercent() < 70)
289+
break;
290+
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+
281296
try {
282297
// Delete oldest indices
283298
deleteIndex(Collections.singletonList(index.index()));
@@ -286,15 +301,12 @@ private void deleteOldestIndices() {
286301
"Docs Count: %3$s\n" +
287302
"Size: %4$s",
288303
index.index(), index.creationDateString(), index.docsCount(), index.storeSize()), ApplicationEventType.INFO);
304+
TimeUnit.SECONDS.sleep(10);
289305
} catch (Exception e) {
290306
String msg = String.format("%1$s: Fail to delete index: %2$s with message: %3$s", ctx, index.index(), e.getMessage());
291307
eventService.createEvent(msg, ApplicationEventType.WARNING);
292308
}
293309

294-
Optional<ElasticCluster> opt = getClusterStatus();
295-
296-
if (opt.isEmpty() || opt.get().getResume().getDiskUsedPercent() < 70)
297-
break;
298310
}
299311
} catch (Exception e) {
300312
String msg = String.format("%1$s: %2$s", ctx, e.getMessage());
@@ -407,4 +419,4 @@ public <T> SearchSqlResponse<T> searchBySql(SqlQueryRequest request, Class<T> re
407419
throw new RuntimeException(ctx + ": " + e.getMessage());
408420
}
409421
}
410-
}
422+
}

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

Lines changed: 54 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,59 @@ 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 = this.getCurrentState(indexInfo)
60+
if (state != null) {
61+
return Constants.STATE_DELETE.equals(state) || Constants.STATE_SAFE_DELETE.equals(state);
62+
}
63+
}
64+
}
65+
return false;
66+
} catch (Exception e) {
67+
log.error("{}: {}", ctx, e.getMessage());
68+
return false;
69+
}
70+
}
71+
72+
73+
private String getCurrentState(JsonObject indexInfo) {
74+
if (indexInfo.has("state") && indexInfo.get("state").isJsonObject()) {
75+
JsonObject stateObj = indexInfo.getAsJsonObject("state");
76+
if (stateObj.has("name")) {
77+
return stateObj.get("name").getAsString();
78+
}
79+
}
80+
81+
String[] legacyPaths = {
82+
"index.plugins.index_state_management.current_state",
83+
"index.opendistro.index_state_management.current_state",
84+
"opendistro.index_state_management.current_state"
85+
};
86+
87+
return Arrays.stream(legacyPaths)
88+
.filter(indexInfo::has)
89+
.map(path -> indexInfo.get(path).getAsString())
90+
.findFirst()
91+
.orElse(null);
92+
}
93+
4094
@EventListener(IndexPatternsReadyEvent.class)
4195
public void init() throws Exception {
4296
final String ctx = CLASSNAME + ".init";

0 commit comments

Comments
 (0)