...
|
...
|
@@ -16,7 +16,6 @@ import com.yohomars.search.index.model.ESBluk; |
|
|
import com.yohomars.search.index.model.IYohoIndex;
|
|
|
import com.yohomars.search.index.model.impl.YohoIndexImpl;
|
|
|
import com.yohomars.search.index.service.IYohoIndexService;
|
|
|
import com.yohomars.search.index.updater.IndexUpdater;
|
|
|
import com.yohomars.search.utils.FileUtils;
|
|
|
import com.yohomars.search.utils.Index;
|
|
|
import com.yohomars.search.utils.JaxbBinder;
|
...
|
...
|
@@ -52,8 +51,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService { |
|
|
|
|
|
@Autowired
|
|
|
private YohoIndexHelper yohoIndexHelper;
|
|
|
@Autowired
|
|
|
private RebuildFlagService rebuildFlagService;
|
|
|
|
|
|
@Autowired
|
|
|
private YohoIndexDataLoader yohoIndexDataLoader;
|
|
|
|
...
|
...
|
@@ -322,14 +320,14 @@ public class YohoIndexServiceImpl implements IYohoIndexService { |
|
|
* @param yohoIndexName 索引名称
|
|
|
*/
|
|
|
@Override
|
|
|
public void rebuild(final String yohoIndexName) {
|
|
|
public void rebuildIndex(final String yohoIndexName) {
|
|
|
final IYohoIndex yohoIndex = this.getIndex(yohoIndexName);
|
|
|
if (yohoIndex == null) {
|
|
|
return;
|
|
|
}
|
|
|
// 如果是重建的social_user的索引,则将记在redis中的lastUpdateTime删除,不然getTotalCount时不能获取到全部的数据
|
|
|
// 将记在redis中的lastUpdateTime删除,不然getTotalCount时不能获取到全部的数据
|
|
|
Index index = Index.getIndex(yohoIndexName);
|
|
|
if(index == Index.social_user){
|
|
|
if(index != null){
|
|
|
String key = index.getKey();
|
|
|
INDEX_REBUILD_LOG.info("delete redisKey={},yohoIndexName={}",key,yohoIndexName);
|
|
|
if(redisValueHelper.hasKey(key)){
|
...
|
...
|
@@ -338,67 +336,62 @@ public class YohoIndexServiceImpl implements IYohoIndexService { |
|
|
}
|
|
|
try {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[1.rebuild begin], begin=[{}] ", yohoIndexName, begin);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[1.rebuildIndex begin], begin=[{}] ", yohoIndexName, begin);
|
|
|
|
|
|
// 0、当前正在全量建索引
|
|
|
rebuildFlagService.updateBuilding(true);
|
|
|
IElasticsearchClient client = yohoIndex.getIndexClient();
|
|
|
final IndexBuilder indexBuilder = yohoIndex.getIndexBuilder();
|
|
|
|
|
|
// 1、删除因停机而产生的临时索引
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[2.deleteTempIndex]", yohoIndexName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[2.deleteTempIndex]", yohoIndexName);
|
|
|
this.deleteTempIndex(yohoIndexName);
|
|
|
|
|
|
// 2、获取临时索引的别名
|
|
|
String yohoTemplateIndexName = yohoIndexHelper.genTempIndexName(yohoIndexName);
|
|
|
|
|
|
// 3、创建临时索引
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[3.createTempIndex begin],mappingContent=[{}],setting=[{}],properties=[{}]", yohoIndexName, yohoIndex.getMappingContent(), yohoIndex.getProperties());
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[3.createTempIndex begin],mappingContent=[{}],setting=[{}],properties=[{}]", yohoIndexName, yohoIndex.getMappingContent(), yohoIndex.getProperties());
|
|
|
String tempIndexRealName = this.createIndex(yohoIndexName, yohoTemplateIndexName, false);
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[3.createTempIndex success],tempIndexRealName=[{}],", yohoIndexName, tempIndexRealName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[3.createTempIndex success],tempIndexRealName=[{}],", yohoIndexName, tempIndexRealName);
|
|
|
|
|
|
// 4、针对批量插入数据,进行索引的优化设置
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[4.optimumSettingsForBulkIndexing begin]", yohoIndexName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[4.optimumSettingsForBulkIndexing begin]", yohoIndexName);
|
|
|
Map<String, String> originalSettings = client.optimumSettingsForBulkIndexing(tempIndexRealName);
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[4.optimumSettingsForBulkIndexing success],originalSettings=[{}]", yohoIndexName, originalSettings);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[4.optimumSettingsForBulkIndexing success],originalSettings=[{}]", yohoIndexName, originalSettings);
|
|
|
|
|
|
// 5、重建数据
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[5.loadAllData begin]", yohoIndexName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[5.loadAllData begin]", yohoIndexName);
|
|
|
if (!yohoIndexDataLoader.loadAllData(yohoIndexName, tempIndexRealName, indexBuilder, client)) {
|
|
|
INDEX_REBUILD_LOG.error("rebuild [{}],loadAllData fail,direct return...", yohoIndexName);
|
|
|
INDEX_REBUILD_LOG.error("rebuildIndex [{}],loadAllData fail,direct return...", yohoIndexName);
|
|
|
return;
|
|
|
}else{
|
|
|
// 目前只有social_user的索引有增量更新操作
|
|
|
// 如果是重建的social_user的索引,则记住当前时间作为最后更新时间,增量更新索引时根据该时间获取变动的数据
|
|
|
if(index == Index.social_user){
|
|
|
// 记住当前时间作为最后更新时间,增量更新索引时根据该时间获取变动的数据
|
|
|
if(index != null){
|
|
|
INDEX_REBUILD_LOG.info("loadAllData success will set currentTime to redis with key={}",index.getKey());
|
|
|
redisValueHelper.set(index.getKey(),System.currentTimeMillis(),1, TimeUnit.DAYS);
|
|
|
}
|
|
|
}
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[5.loadAllData success]", yohoIndexName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[5.loadAllData success]", yohoIndexName);
|
|
|
|
|
|
// 6、刷新缓存
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[6.refreshIndex begin],tempIndexRealName=[{}]", yohoIndexName, tempIndexRealName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[6.refreshIndex begin],tempIndexRealName=[{}]", yohoIndexName, tempIndexRealName);
|
|
|
client.refreshIndex(tempIndexRealName);
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[6.refreshIndex success]", yohoIndexName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[6.refreshIndex success]", yohoIndexName);
|
|
|
|
|
|
// 7、恢复原来的索引设置
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[7.setIndexSettings begin],tempIndexRealName=[{}],originalSettings=[{}]", yohoIndexName, tempIndexRealName, originalSettings);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[7.setIndexSettings begin],tempIndexRealName=[{}],originalSettings=[{}]", yohoIndexName, tempIndexRealName, originalSettings);
|
|
|
client.setIndexSettings(tempIndexRealName, originalSettings);
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[7.setIndexSettings success]", yohoIndexName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[7.setIndexSettings success]", yohoIndexName);
|
|
|
|
|
|
// 8、索引替换:将老索引的索引名加到新索引上,并删除老索引
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[8.replaceIndex begin],yohoIndexName=[{}],yohoTemplateIndexName=[{}]", yohoIndexName, yohoIndexName, yohoTemplateIndexName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[8.replaceIndex begin],yohoIndexName=[{}],yohoTemplateIndexName=[{}]", yohoIndexName, yohoIndexName, yohoTemplateIndexName);
|
|
|
this.replaceIndex(yohoIndexName, yohoTemplateIndexName);
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[8.replaceIndex success],yohoIndexName=[{}]", yohoIndexName, yohoIndexName);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[8.replaceIndex success],yohoIndexName=[{}]", yohoIndexName, yohoIndexName);
|
|
|
|
|
|
// 9、记录耗时时间
|
|
|
INDEX_REBUILD_LOG.info("rebuild [{}],step=[9.rebuild success],cost=[{}]s", yohoIndexName, (System.currentTimeMillis()-begin)/1000);
|
|
|
INDEX_REBUILD_LOG.info("rebuildIndex [{}],step=[9.rebuildIndex success],cost=[{}]s", yohoIndexName, (System.currentTimeMillis()-begin)/1000);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
INDEX_REBUILD_LOG.error(e.getMessage(), e);
|
|
|
} finally {
|
|
|
rebuildFlagService.updateBuilding(false);
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -407,39 +400,37 @@ public class YohoIndexServiceImpl implements IYohoIndexService { |
|
|
* @param yohoIndexName
|
|
|
*/
|
|
|
@Override
|
|
|
public void buildAppended(final String yohoIndexName) {
|
|
|
public void appendIndex(final String yohoIndexName) {
|
|
|
final IYohoIndex yohoIndex = this.getIndex(yohoIndexName);
|
|
|
if (yohoIndex == null) {
|
|
|
return;
|
|
|
}
|
|
|
Index index = Index.getIndex(yohoIndexName);
|
|
|
if(!this.indexExists(yohoIndexName)){
|
|
|
INDEX_REBUILD_LOG.info("YohoIndex does not exists when buildAppended with indexName=[{}] will call rebuild instead", yohoIndexName);
|
|
|
this.rebuild(yohoIndexName);
|
|
|
INDEX_REBUILD_LOG.info("YohoIndex does not exists when appendIndex with indexName=[{}] will call rebuildIndex instead", yohoIndexName);
|
|
|
this.rebuildIndex(yohoIndexName);
|
|
|
}else{
|
|
|
try {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
INDEX_REBUILD_LOG.info("buildAppended [{}],step=[1.buildAppended begin], begin=[{}] ", yohoIndexName, begin);
|
|
|
INDEX_REBUILD_LOG.info("appendIndex [{}],step=[1.appendIndex begin], begin=[{}] ", yohoIndexName, begin);
|
|
|
|
|
|
IElasticsearchClient client = yohoIndex.getIndexClient();
|
|
|
final IndexAppender indexAppender = yohoIndex.getIndexAppender();
|
|
|
|
|
|
// 2、更新数据
|
|
|
if (yohoIndexDataLoader.loadAppendedData(yohoIndexName, indexAppender, client)) {
|
|
|
// 如果是重建的social_user的索引,则记住当前时间作为最后更新时间,增量更新索引时根据该时间获取变动的数据
|
|
|
if(index == Index.social_user){
|
|
|
// 记住当前时间作为最后更新时间,下一次增量更新索引时根据该时间获取变动的数据
|
|
|
if(index != null){
|
|
|
INDEX_REBUILD_LOG.info("loadAppendData success will set currentTime to redis with key={}",index.getKey());
|
|
|
redisValueHelper.set(index.getKey(), System.currentTimeMillis(), 1, TimeUnit.DAYS);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 3、记录耗时时间
|
|
|
INDEX_REBUILD_LOG.info("buildAppended [{}],step=[3.buildAppended success],cost=[{}]s", yohoIndexName, (System.currentTimeMillis()-begin)/1000);
|
|
|
INDEX_REBUILD_LOG.info("appendIndex [{}],step=[3.appendIndex success],cost=[{}]s", yohoIndexName, (System.currentTimeMillis()-begin)/1000);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
INDEX_REBUILD_LOG.error("buildAppended [{}] error:",yohoIndexName, e);
|
|
|
} finally {
|
|
|
rebuildFlagService.updateBuilding(false);
|
|
|
INDEX_REBUILD_LOG.error("appendIndex [{}] error:",yohoIndexName, e);
|
|
|
}
|
|
|
}
|
|
|
}
|
...
|
...
|
|