|
10 | 10 | import org.apache.http.impl.client.BasicCredentialsProvider; |
11 | 11 | import org.apache.http.ssl.SSLContextBuilder; |
12 | 12 | import org.apache.http.ssl.SSLContexts; |
| 13 | +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; |
13 | 14 | import org.elasticsearch.action.bulk.BulkProcessor; |
14 | 15 | import org.elasticsearch.action.bulk.BulkRequest; |
15 | 16 | import org.elasticsearch.action.bulk.BulkResponse; |
16 | 17 | import org.elasticsearch.action.index.IndexRequest; |
| 18 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
17 | 19 | import org.elasticsearch.client.RequestOptions; |
18 | 20 | import org.elasticsearch.client.RestClient; |
19 | 21 | import org.elasticsearch.client.RestHighLevelClient; |
| 22 | +import org.elasticsearch.client.indices.GetIndexRequest; |
20 | 23 | import org.elasticsearch.common.unit.ByteSizeUnit; |
21 | 24 | import org.elasticsearch.common.unit.ByteSizeValue; |
22 | 25 | import org.elasticsearch.common.unit.TimeValue; |
23 | 26 | import scouter.server.Configure; |
24 | 27 | import scouter.server.Logger; |
| 28 | +import scouter.util.DateUtil; |
25 | 29 |
|
26 | 30 | import java.io.IOException; |
27 | | -import java.util.Arrays; |
28 | | -import java.util.List; |
29 | | -import java.util.Map; |
| 31 | +import java.util.*; |
| 32 | +import java.util.stream.Collectors; |
| 33 | +import java.util.stream.IntStream; |
30 | 34 |
|
31 | 35 |
|
32 | 36 | /** |
@@ -146,8 +150,48 @@ public void flush() { |
146 | 150 | this.bulkProcessor.flush(); |
147 | 151 | } |
148 | 152 |
|
149 | | - public void deleteIndex(List<String> strings, int esIndexDuration) { |
| 153 | + public void deleteIndex(List<String> indexPattern, int esIndexDuration) { |
150 | 154 |
|
151 | 155 |
|
| 156 | + final int _start = esIndexDuration * 2; // before endDay |
| 157 | + final int _end = esIndexDuration; // total counter; |
| 158 | + final Calendar cal = GregorianCalendar.getInstance(); |
| 159 | + IntStream.iterate(_start, n -> n-1 ) |
| 160 | + .limit(_end) |
| 161 | + .mapToObj(x ->{ |
| 162 | + // 현재 기준 이전 날짜를 가져오기 |
| 163 | + cal.add(5, x > 0 ? (x * -1) : x); |
| 164 | + return cal.getTime().getTime(); |
| 165 | + }) |
| 166 | + .map(_ts -> DateUtil.format(_ts,"yyyy-MM-dd")) |
| 167 | + .collect(Collectors.toList()) |
| 168 | + .stream() |
| 169 | + .map(_day -> { |
| 170 | + List<String> pattern = new ArrayList<>(); |
| 171 | + for(String pt : indexPattern) { |
| 172 | + pattern.add(String.join("", pt, "*", _day, "*")); |
| 173 | + } |
| 174 | + return pattern; |
| 175 | + } ) |
| 176 | + .flatMap(List::stream) |
| 177 | + .forEach(_indexName ->{ |
| 178 | + try { |
| 179 | + |
| 180 | + GetIndexRequest getRequest = new GetIndexRequest(_indexName); |
| 181 | + DeleteIndexRequest delRequest = new DeleteIndexRequest(_indexName); |
| 182 | + |
| 183 | + if(this.highLevelClient.indices().exists(getRequest, RequestOptions.DEFAULT)) { |
| 184 | + AcknowledgedResponse acknowledgedResponse= this.highLevelClient.indices().delete(delRequest,RequestOptions.DEFAULT); |
| 185 | + if(acknowledgedResponse.isAcknowledged()){ |
| 186 | + Logger.println("index pattern delete : success",_indexName); |
| 187 | + } |
| 188 | + |
| 189 | + } |
| 190 | + }catch (Throwable e){ |
| 191 | + Logger.printStackTrace("ES-DELETE-ERROR", e); |
| 192 | + } |
| 193 | + }); |
152 | 194 | } |
| 195 | + |
| 196 | + |
153 | 197 | } |
0 commit comments