IndexRebuildJob.java
4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.yoho.search.consumer.job;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.index.common.IYohoIndexService;
import com.yoho.search.consumer.index.rebuild.RebuildFlagService;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class IndexRebuildJob implements ApplicationEventPublisherAware {
@Autowired
private IYohoIndexService yohoIndexService;
@Autowired
private RebuildFlagService rebuildFlagService;
@Autowired
private TplAdaptorJob tplAdaptorJob;
private static final Logger logger = LoggerFactory.getLogger(IndexRebuildJob.class);
private ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
@Scheduled(cron = "0 0 3 * * ?")
public void execute() {
long begin = System.currentTimeMillis();
logger.info("indexRebuildJob execute start----[begin={}]", begin);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_BRAND);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_SIZE);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PRODUCT_COLOR);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PRODUCT_SORT);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_STYLE);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_STANDARD);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_ROBOTQUESTION);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PRODUCT_PRICE_PLAN);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_HELPER);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_SHOPS);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_GLOBAL);
try {
// 重建完productpool索引,等待1分钟,再执行其他索引的重建
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PRODUCT_POOL);
logger.info("indexRebuild productpool success, wait 1 min for rebuild storagesku... ");
Thread.sleep(1 * 60 * 1000L);
} catch (Exception e) {
}
try {
// 重建完storagesku索引,等待1分钟,再执行其他索引的重建
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_STORAGE_SKU_INDEX);
logger.info("indexRebuild storagesku success, wait 1 min for rebuild productindex... ");
Thread.sleep(1 * 60 * 1000L);
} catch (Exception e) {
}
try {
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
logger.info("indexRebuild productindex success, wait 2 min for doTplAdaptor ... ");
Thread.sleep(2 * 60 * 1000L);
} catch (Exception e) {
}
// // 重建完productindex索引,等待2分钟,执行全球购融合索引的重建
// tplAdaptorJob.doTplAdaptor();
logger.info("indexRebuildJob execute end----[end={}][cost={}]", System.currentTimeMillis(), (System.currentTimeMillis() - begin));
}
@Scheduled(cron="0 0 4 * * ?")
public void rebuildSuggestIndex() {
// suggest索引由于需要依赖productindex索引计算关键词的count 因此单独放在4点重建
long begin = System.currentTimeMillis();
logger.info("indexRebuildJob rebuildSuggestIndex start----[begin={}]", begin);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_SUGGEST);
logger.info("indexRebuildJob rebuildSuggestIndex end----[end={}][cost={}]", System.currentTimeMillis(), (System.currentTimeMillis() - begin));
}
@Scheduled(cron = "0 50 * * * ?")
public void rebuildTblProductIndex() {
long begin = System.currentTimeMillis();
logger.info("indexRebuildJob rebuildTblProductIndex start----[begin={}]", begin);
this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_TBLPRODUCT);
logger.info("indexRebuildJob rebuildTblProductIndex end----[end={}][cost={}]", System.currentTimeMillis(), (System.currentTimeMillis() - begin));
}
private void rebuildIndexWithlog(String indexName) {
try {
long begin = System.currentTimeMillis();
logger.info("rebuild index start----[indexName={}][begin={}]", indexName, begin);
rebuildFlagService.waitingRebuildingIndex();
yohoIndexService.rebuild(indexName);
logger.info("rebuild index end----[indexName={}][end={}][cost={}ms]", indexName, System.currentTimeMillis(), (System.currentTimeMillis() - begin));
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.INDEXREBUILDJOB_REBUILDINDEXWITHLOG.getEventName(), EventReportEnum.INDEXREBUILDJOB_REBUILDINDEXWITHLOG
.getFunctionName(), EventReportEnum.INDEXREBUILDJOB_REBUILDINDEXWITHLOG.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
logger.error(e.getMessage(), e);
}
}
}