Authored by 胡古飞

删除真实名称为productindex的索引

... ... @@ -56,7 +56,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent
@Autowired
private IndexRebuildListenerMgr indexRebuildListenerMgr;
ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
... ... @@ -105,7 +105,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent
throw new RuntimeException("初始化索引服务失败,Exception: ", e);
}
}
@Override
public ClusterHealthStatus getClusterHealthStatus(String yohoIndexName) {
IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
... ... @@ -115,7 +115,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent
IElasticsearchClient client = index.getIndexClient();
return client.getClusterHealthStatus(yohoIndexName);
}
@Override
public IYohoIndex getIndex(String yohoIndexName) {
return this.nameToIndexMap.get(yohoIndexName);
... ... @@ -193,32 +193,38 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent
return;
}
IElasticsearchClient client = index.getIndexClient();
// 1、获取真实索引的正则规则
String regEx = yohoIndexHelper.getTemplateIndexNamePattern(yohoIndexName);
String regEx = yohoIndexHelper.getRealIndexNamePattern(yohoIndexName);
Pattern p = Pattern.compile(regEx);
// 2、获取ES集群中真实索引名称对临时索引名称
Map<String, List<String>> realNameToAliasesMap = client.getRealNameToAliasesMap();
// 3、删除realIndexName对应的索引
// 3、找出yohoIndexName对应的非法索引
List<String> tempRealIndexNames = new ArrayList<String>();
for (Map.Entry<String, List<String>> entry : realNameToAliasesMap.entrySet()) {
String realIndexName = entry.getKey();
if (!p.matcher(realIndexName).find()) {
// 1)如果真实索引名称和yohoIndexName相同,则可能是增量创建的索引,视为非法
if (realIndexName.equals(yohoIndexName)) {
tempRealIndexNames.add(realIndexName);
continue;
}
boolean validate = false;
for (String alias : entry.getValue()) {
if (yohoIndexName.equals(alias)) {
validate = true;
break;
}
// 2)如果真实索引名称非法,则无视
if (!p.matcher(realIndexName).find()) {
continue;
}
if (!validate) {
client.deleteIndex(realIndexName);
INDEX_REBUILD_LOG.info("[client={}] 删除遗留的临时索引:[id={}]", client.getName(), realIndexName);
// 3)如果别名中不包含yohoIndexName,则视为失败的临时索引
List<String> aliasNames = entry.getValue();
if (!aliasNames.contains(yohoIndexName)) {
tempRealIndexNames.add(realIndexName);
continue;
}
}
// 4、删除yohoIndexName对应的非法索引
if (tempRealIndexNames != null && !tempRealIndexNames.isEmpty()) {
client.deleteIndexs(tempRealIndexNames);
INDEX_REBUILD_LOG.info("[client={}] 删除遗留的临时索引:[tempRealIndexNames={}]", client.getName(), tempRealIndexNames);
}
}
@Override
... ... @@ -413,19 +419,21 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent
client.addIndexData(realIndexName, yohoIndexName, id, data);
}
// @Override
// public void addIndexData(final String yohoIndexName, final List<Map<String, Object>> dataList) throws Exception {
// IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
// if (index == null) {
// return;
// }
// IElasticsearchClient client = index.getIndexClient();
// String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
// if (StringUtils.isBlank(realIndexName)) {
// return;
// }
// client.addIndexData(realIndexName, yohoIndexName, dataList);
// }
// @Override
// public void addIndexData(final String yohoIndexName, final
// List<Map<String, Object>> dataList) throws Exception {
// IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
// if (index == null) {
// return;
// }
// IElasticsearchClient client = index.getIndexClient();
// String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName,
// client);
// if (StringUtils.isBlank(realIndexName)) {
// return;
// }
// client.addIndexData(realIndexName, yohoIndexName, dataList);
// }
@Override
public void deleteIndexData(final String yohoIndexName, final String id) throws Exception {
... ... @@ -441,19 +449,21 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent
client.deleteIndexData(realIndexName, yohoIndexName, id);
}
// @Override
// public void deleteIndexDataByQuery(final String yohoIndexName, final String query) throws Exception {
// IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
// if (index == null) {
// return;
// }
// IElasticsearchClient client = index.getIndexClient();
// String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
// if (StringUtils.isBlank(realIndexName)) {
// return;
// }
// client.deleteIndexDataByQuery(realIndexName, yohoIndexName, query);
// }
// @Override
// public void deleteIndexDataByQuery(final String yohoIndexName, final
// String query) throws Exception {
// IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
// if (index == null) {
// return;
// }
// IElasticsearchClient client = index.getIndexClient();
// String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName,
// client);
// if (StringUtils.isBlank(realIndexName)) {
// return;
// }
// client.deleteIndexDataByQuery(realIndexName, yohoIndexName, query);
// }
@Override
public void updateIndexData(final String yohoIndexName, final String id, final Object data) throws Exception {
... ... @@ -551,19 +561,19 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent
}
@Override
public BulkResponse bulk(List<ESBluk> esBluks){
public BulkResponse bulk(List<ESBluk> esBluks) {
IYohoIndex index = this.nameToIndexMap.get(esBluks.get(0).getIndexName());
if (index == null) {
return null;
}
IElasticsearchClient client = index.getIndexClient();
BulkResponse bulkResponse = client.bulk(esBluks);
if(bulkResponse.hasFailures()){
logger.warn("bulk failure",bulkResponse.buildFailureMessage());
}
BulkResponse bulkResponse = client.bulk(esBluks);
if (bulkResponse.hasFailures()) {
logger.warn("bulk failure", bulkResponse.buildFailureMessage());
}
return bulkResponse;
}
@Override
public boolean checkHealth(String indexName) {
IYohoIndex index = this.nameToIndexMap.get(indexName);
... ...