Skip to content

Commit

Permalink
[Bugfix]修复es索引create/delete死循环问题 (didi#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard authored and ZQKC committed May 30, 2023
1 parent 7bfe787 commit ffc115c
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.IntStream;

import static com.xiaojukeji.know.streaming.km.common.constant.ESConstant.*;

Expand Down Expand Up @@ -68,13 +69,11 @@ public void checkCurrentDayIndexExist(){
String indexTemplate = templateLoaderUtil.getContextByFileName(indexName);
esOpClient.createIndexTemplateIfNotExist(indexName, indexTemplate);

//检查最近7天索引存在不存
for(int i = 0; i < INDEX_DAYS; i++){
String realIndex = IndexNameUtils.genDailyIndexName(indexName, i);
if(esOpClient.indexExist(realIndex)){continue;}

esOpClient.createIndex(realIndex);
}
int retainDays = indexExpireDays > INDEX_DAYS ? INDEX_DAYS : indexExpireDays;
// 检查最近【retainDays】天索引存在不存
IntStream.range(0, retainDays).mapToObj(i -> IndexNameUtils.genDailyIndexName(indexName, i))
.filter(realIndex -> !esOpClient.indexExist(realIndex))
.forEach(realIndex -> esOpClient.createIndex(realIndex));
} catch (Exception e) {
LOGGER.error("method=checkCurrentDayIndexExist||errMsg=exception!", e);
}
Expand All @@ -94,8 +93,7 @@ public void delExpireIndex(){
indexExpireDays, indexList.subList(indexExpireDays, size));
}

indexList.subList(indexExpireDays, size).stream().forEach(
s -> esOpClient.delIndexByName(s));
indexList.subList(indexExpireDays, size).forEach(s -> esOpClient.delIndexByName(s));
}
}

Expand Down

0 comments on commit ffc115c

Please sign in to comment.