删除真实名称为productindex的索引
Showing
1 changed file
with
58 additions
and
48 deletions
@@ -56,7 +56,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -56,7 +56,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
56 | @Autowired | 56 | @Autowired |
57 | private IndexRebuildListenerMgr indexRebuildListenerMgr; | 57 | private IndexRebuildListenerMgr indexRebuildListenerMgr; |
58 | ApplicationEventPublisher publisher; | 58 | ApplicationEventPublisher publisher; |
59 | - | 59 | + |
60 | @Override | 60 | @Override |
61 | public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { | 61 | public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { |
62 | this.publisher = applicationEventPublisher; | 62 | this.publisher = applicationEventPublisher; |
@@ -105,7 +105,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -105,7 +105,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
105 | throw new RuntimeException("初始化索引服务失败,Exception: ", e); | 105 | throw new RuntimeException("初始化索引服务失败,Exception: ", e); |
106 | } | 106 | } |
107 | } | 107 | } |
108 | - | 108 | + |
109 | @Override | 109 | @Override |
110 | public ClusterHealthStatus getClusterHealthStatus(String yohoIndexName) { | 110 | public ClusterHealthStatus getClusterHealthStatus(String yohoIndexName) { |
111 | IYohoIndex index = this.nameToIndexMap.get(yohoIndexName); | 111 | IYohoIndex index = this.nameToIndexMap.get(yohoIndexName); |
@@ -115,7 +115,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -115,7 +115,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
115 | IElasticsearchClient client = index.getIndexClient(); | 115 | IElasticsearchClient client = index.getIndexClient(); |
116 | return client.getClusterHealthStatus(yohoIndexName); | 116 | return client.getClusterHealthStatus(yohoIndexName); |
117 | } | 117 | } |
118 | - | 118 | + |
119 | @Override | 119 | @Override |
120 | public IYohoIndex getIndex(String yohoIndexName) { | 120 | public IYohoIndex getIndex(String yohoIndexName) { |
121 | return this.nameToIndexMap.get(yohoIndexName); | 121 | return this.nameToIndexMap.get(yohoIndexName); |
@@ -193,32 +193,38 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -193,32 +193,38 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
193 | return; | 193 | return; |
194 | } | 194 | } |
195 | IElasticsearchClient client = index.getIndexClient(); | 195 | IElasticsearchClient client = index.getIndexClient(); |
196 | - | ||
197 | // 1、获取真实索引的正则规则 | 196 | // 1、获取真实索引的正则规则 |
198 | - String regEx = yohoIndexHelper.getTemplateIndexNamePattern(yohoIndexName); | 197 | + String regEx = yohoIndexHelper.getRealIndexNamePattern(yohoIndexName); |
199 | Pattern p = Pattern.compile(regEx); | 198 | Pattern p = Pattern.compile(regEx); |
200 | 199 | ||
201 | // 2、获取ES集群中真实索引名称对临时索引名称 | 200 | // 2、获取ES集群中真实索引名称对临时索引名称 |
202 | Map<String, List<String>> realNameToAliasesMap = client.getRealNameToAliasesMap(); | 201 | Map<String, List<String>> realNameToAliasesMap = client.getRealNameToAliasesMap(); |
203 | 202 | ||
204 | - // 3、删除realIndexName对应的索引 | 203 | + // 3、找出yohoIndexName对应的非法索引 |
204 | + List<String> tempRealIndexNames = new ArrayList<String>(); | ||
205 | for (Map.Entry<String, List<String>> entry : realNameToAliasesMap.entrySet()) { | 205 | for (Map.Entry<String, List<String>> entry : realNameToAliasesMap.entrySet()) { |
206 | String realIndexName = entry.getKey(); | 206 | String realIndexName = entry.getKey(); |
207 | - if (!p.matcher(realIndexName).find()) { | 207 | + // 1)如果真实索引名称和yohoIndexName相同,则可能是增量创建的索引,视为非法 |
208 | + if (realIndexName.equals(yohoIndexName)) { | ||
209 | + tempRealIndexNames.add(realIndexName); | ||
208 | continue; | 210 | continue; |
209 | } | 211 | } |
210 | - boolean validate = false; | ||
211 | - for (String alias : entry.getValue()) { | ||
212 | - if (yohoIndexName.equals(alias)) { | ||
213 | - validate = true; | ||
214 | - break; | ||
215 | - } | 212 | + // 2)如果真实索引名称非法,则无视 |
213 | + if (!p.matcher(realIndexName).find()) { | ||
214 | + continue; | ||
216 | } | 215 | } |
217 | - if (!validate) { | ||
218 | - client.deleteIndex(realIndexName); | ||
219 | - INDEX_REBUILD_LOG.info("[client={}] 删除遗留的临时索引:[id={}]", client.getName(), realIndexName); | 216 | + // 3)如果别名中不包含yohoIndexName,则视为失败的临时索引 |
217 | + List<String> aliasNames = entry.getValue(); | ||
218 | + if (!aliasNames.contains(yohoIndexName)) { | ||
219 | + tempRealIndexNames.add(realIndexName); | ||
220 | + continue; | ||
220 | } | 221 | } |
221 | } | 222 | } |
223 | + // 4、删除yohoIndexName对应的非法索引 | ||
224 | + if (tempRealIndexNames != null && !tempRealIndexNames.isEmpty()) { | ||
225 | + client.deleteIndexs(tempRealIndexNames); | ||
226 | + INDEX_REBUILD_LOG.info("[client={}] 删除遗留的临时索引:[tempRealIndexNames={}]", client.getName(), tempRealIndexNames); | ||
227 | + } | ||
222 | } | 228 | } |
223 | 229 | ||
224 | @Override | 230 | @Override |
@@ -413,19 +419,21 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -413,19 +419,21 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
413 | client.addIndexData(realIndexName, yohoIndexName, id, data); | 419 | client.addIndexData(realIndexName, yohoIndexName, id, data); |
414 | } | 420 | } |
415 | 421 | ||
416 | -// @Override | ||
417 | -// public void addIndexData(final String yohoIndexName, final List<Map<String, Object>> dataList) throws Exception { | ||
418 | -// IYohoIndex index = this.nameToIndexMap.get(yohoIndexName); | ||
419 | -// if (index == null) { | ||
420 | -// return; | ||
421 | -// } | ||
422 | -// IElasticsearchClient client = index.getIndexClient(); | ||
423 | -// String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
424 | -// if (StringUtils.isBlank(realIndexName)) { | ||
425 | -// return; | ||
426 | -// } | ||
427 | -// client.addIndexData(realIndexName, yohoIndexName, dataList); | ||
428 | -// } | 422 | + // @Override |
423 | + // public void addIndexData(final String yohoIndexName, final | ||
424 | + // List<Map<String, Object>> dataList) throws Exception { | ||
425 | + // IYohoIndex index = this.nameToIndexMap.get(yohoIndexName); | ||
426 | + // if (index == null) { | ||
427 | + // return; | ||
428 | + // } | ||
429 | + // IElasticsearchClient client = index.getIndexClient(); | ||
430 | + // String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, | ||
431 | + // client); | ||
432 | + // if (StringUtils.isBlank(realIndexName)) { | ||
433 | + // return; | ||
434 | + // } | ||
435 | + // client.addIndexData(realIndexName, yohoIndexName, dataList); | ||
436 | + // } | ||
429 | 437 | ||
430 | @Override | 438 | @Override |
431 | public void deleteIndexData(final String yohoIndexName, final String id) throws Exception { | 439 | public void deleteIndexData(final String yohoIndexName, final String id) throws Exception { |
@@ -441,19 +449,21 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -441,19 +449,21 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
441 | client.deleteIndexData(realIndexName, yohoIndexName, id); | 449 | client.deleteIndexData(realIndexName, yohoIndexName, id); |
442 | } | 450 | } |
443 | 451 | ||
444 | -// @Override | ||
445 | -// public void deleteIndexDataByQuery(final String yohoIndexName, final String query) throws Exception { | ||
446 | -// IYohoIndex index = this.nameToIndexMap.get(yohoIndexName); | ||
447 | -// if (index == null) { | ||
448 | -// return; | ||
449 | -// } | ||
450 | -// IElasticsearchClient client = index.getIndexClient(); | ||
451 | -// String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
452 | -// if (StringUtils.isBlank(realIndexName)) { | ||
453 | -// return; | ||
454 | -// } | ||
455 | -// client.deleteIndexDataByQuery(realIndexName, yohoIndexName, query); | ||
456 | -// } | 452 | + // @Override |
453 | + // public void deleteIndexDataByQuery(final String yohoIndexName, final | ||
454 | + // String query) throws Exception { | ||
455 | + // IYohoIndex index = this.nameToIndexMap.get(yohoIndexName); | ||
456 | + // if (index == null) { | ||
457 | + // return; | ||
458 | + // } | ||
459 | + // IElasticsearchClient client = index.getIndexClient(); | ||
460 | + // String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, | ||
461 | + // client); | ||
462 | + // if (StringUtils.isBlank(realIndexName)) { | ||
463 | + // return; | ||
464 | + // } | ||
465 | + // client.deleteIndexDataByQuery(realIndexName, yohoIndexName, query); | ||
466 | + // } | ||
457 | 467 | ||
458 | @Override | 468 | @Override |
459 | public void updateIndexData(final String yohoIndexName, final String id, final Object data) throws Exception { | 469 | public void updateIndexData(final String yohoIndexName, final String id, final Object data) throws Exception { |
@@ -551,19 +561,19 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -551,19 +561,19 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
551 | } | 561 | } |
552 | 562 | ||
553 | @Override | 563 | @Override |
554 | - public BulkResponse bulk(List<ESBluk> esBluks){ | 564 | + public BulkResponse bulk(List<ESBluk> esBluks) { |
555 | IYohoIndex index = this.nameToIndexMap.get(esBluks.get(0).getIndexName()); | 565 | IYohoIndex index = this.nameToIndexMap.get(esBluks.get(0).getIndexName()); |
556 | if (index == null) { | 566 | if (index == null) { |
557 | return null; | 567 | return null; |
558 | } | 568 | } |
559 | IElasticsearchClient client = index.getIndexClient(); | 569 | IElasticsearchClient client = index.getIndexClient(); |
560 | - BulkResponse bulkResponse = client.bulk(esBluks); | ||
561 | - if(bulkResponse.hasFailures()){ | ||
562 | - logger.warn("bulk failure",bulkResponse.buildFailureMessage()); | ||
563 | - } | 570 | + BulkResponse bulkResponse = client.bulk(esBluks); |
571 | + if (bulkResponse.hasFailures()) { | ||
572 | + logger.warn("bulk failure", bulkResponse.buildFailureMessage()); | ||
573 | + } | ||
564 | return bulkResponse; | 574 | return bulkResponse; |
565 | } | 575 | } |
566 | - | 576 | + |
567 | @Override | 577 | @Override |
568 | public boolean checkHealth(String indexName) { | 578 | public boolean checkHealth(String indexName) { |
569 | IYohoIndex index = this.nameToIndexMap.get(indexName); | 579 | IYohoIndex index = this.nameToIndexMap.get(indexName); |
-
Please register or login to post a comment