1010import com .park .utmstack .service .MailService ;
1111import com .park .utmstack .service .UtmSpaceNotificationControlService ;
1212import com .park .utmstack .service .application_events .ApplicationEventService ;
13+ import com .park .utmstack .service .index_policy .IndexPolicyService ;
1314import com .park .utmstack .util .chart_builder .IndexPropertyType ;
1415import com .park .utmstack .util .exceptions .OpenSearchIndexNotFoundException ;
1516import com .park .utmstack .util .exceptions .UtmElasticsearchException ;
4344import java .time .temporal .ChronoUnit ;
4445import java .util .*;
4546import 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+ }
0 commit comments