|
1 | 1 | package com.park.utmstack.service.index_policy; |
2 | 2 |
|
3 | 3 | import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonObject; |
4 | 5 | import com.park.utmstack.config.Constants; |
5 | 6 | import com.park.utmstack.domain.index_pattern.enums.SystemIndexPattern; |
6 | 7 | import com.park.utmstack.domain.index_policy.*; |
@@ -37,6 +38,45 @@ public IndexPolicyService(ApplicationContext applicationContext, |
37 | 38 | this.client = client; |
38 | 39 | } |
39 | 40 |
|
| 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 | + |
40 | 80 | @EventListener(IndexPatternsReadyEvent.class) |
41 | 81 | public void init() throws Exception { |
42 | 82 | final String ctx = CLASSNAME + ".init"; |
|
0 commit comments