Authored by unknown

update PerformanceMonitor

package com.yoho.search.common.interceptor;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
... ... @@ -12,6 +13,7 @@ import org.springframework.web.servlet.ModelAndView;
import com.yoho.core.common.monitor.ThreadProfile;
import com.yoho.core.common.utils.HttpRequestUtils;
import com.yoho.search.base.monitor.PerformanceMonitor;
import com.yoho.search.common.downgrade.DownGradeService;
import com.yoho.search.common.utils.HttpServletRequestUtils;
... ... @@ -23,14 +25,21 @@ import com.yoho.search.common.utils.HttpServletRequestUtils;
public class ControllerCostInterceptor implements HandlerInterceptor {
private static final Logger CONTROLLER_COST = LoggerFactory.getLogger("CONTROLLER_COST");
private static final Logger CONTROLLER_PERFORMANCE = LoggerFactory.getLogger("CONTROLLER_PERFORMANCE");
private static final ThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<Long>("ThreadLocal StartTime");
@Autowired
private PerformanceMonitor monitor;
@Autowired
private DownGradeService downGradeService;
private PerformanceMonitor monitor;
@PostConstruct
void init(){
monitor = new PerformanceMonitor("CONTROLLER_PERFORMANCE",CONTROLLER_PERFORMANCE, 10);
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) throws Exception {
long beginTime = System.currentTimeMillis();// 1、开始时间
... ...
package com.yoho.search.common.interceptor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class PerformanceMonitor {
private static final Logger logger = LoggerFactory.getLogger(PerformanceMonitor.class);
private static final Logger CONTROLLER_PERFORMANCE = LoggerFactory.getLogger("CONTROLLER_PERFORMANCE");
private AtomicLong totalVisit = new AtomicLong(0);
private AtomicLong totalCost = new AtomicLong(0);
private AtomicLong over500msCount = new AtomicLong(0);
private AtomicLong over1000msCount = new AtomicLong(0);
private final int MonitorPeriodInSecond = 10;
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
@PostConstruct
void init() {
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
logAndClear();
}
}, 0, MonitorPeriodInSecond, TimeUnit.SECONDS);
}
private void logAndClear() {
try {
long visitCount = totalVisit.longValue();
long vostMs = totalCost.longValue();
long average = visitCount == 0 ? 0 : (vostMs / visitCount);
long over500msCnt = over500msCount.longValue();
double over500msCntPercent = visitCount == 0 ? 0 : (over500msCnt * 100L / visitCount);
long over1000msCnt = over1000msCount.longValue();
double over1000msCntPercent = visitCount == 0 ? 0 : (over1000msCnt * 100L / visitCount);
CONTROLLER_PERFORMANCE.info("Performance.Monitoring,Peroid [{}]s,totalVisit[{}],totalCost[{}] ms,AverageCost[{}]ms,over500msCount[{}][{}%],over1000msCnt[{}][{}%]", MonitorPeriodInSecond, visitCount,
vostMs, average,over500msCnt,over500msCntPercent,over1000msCnt,over1000msCntPercent);
totalVisit.set(0);
totalCost.set(0);
over500msCount.set(0);
over1000msCount.set(0);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
public void addVisitCount() {
totalVisit.incrementAndGet();
}
public void addCost(long cost) {
totalCost.addAndGet(cost);
if(cost>=1000){
over1000msCount.incrementAndGet();
}
if(cost>=500){
over500msCount.incrementAndGet();
}
}
}
... ... @@ -8,17 +8,22 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.index.query.QueryBuilder;
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.stereotype.Service;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.monitor.PerformanceMonitor;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.core.es.IElasticsearchClient;
import com.yoho.search.core.es.impl.YohoIndexHelper;
... ... @@ -28,13 +33,21 @@ import com.yoho.search.core.es.model.SearchResult;
@Service
public class SearchCommonService implements ApplicationEventPublisherAware {
private static final Logger ES_PERFORMANCE = LoggerFactory.getLogger("ES_PERFORMANCE");
@Autowired
private ESClientMgr esClientMgr;
@Autowired
private YohoIndexHelper yohoIndexHelper;
private ApplicationEventPublisher publisher;
private PerformanceMonitor performanceMonitor;
@PostConstruct
void init(){
performanceMonitor = new PerformanceMonitor("ES_PERFORMANCE",ES_PERFORMANCE, 10);
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
... ... @@ -65,12 +78,20 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
IElasticsearchClient client = esClientMgr.getClient(indexName);
SearchResult searchResult = client.search(indexName, indexName, searchParam);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
return searchResult;
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount();
try {
IElasticsearchClient client = esClientMgr.getClient(indexName);
SearchResult searchResult = client.search(indexName, indexName, searchParam);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
return searchResult;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
/**
* 根据多个索引查询
*
... ... @@ -79,10 +100,18 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public SearchResult doSearch(final List<String> indexNames, final SearchParam searchParam) {
IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
return searchResult;
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount();
try {
IElasticsearchClient client = esClientMgr.getClient(indexNames.get(0));
SearchResult searchResult = client.search(indexNames, indexNames, searchParam);
this.publishSearchResultEvent(indexNames.toString(), searchParam, searchResult);
return searchResult;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
/**
... ... @@ -93,17 +122,25 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public List<SearchResult> doMutiSearch(final String indexName, final List<SearchParam> searchParams) {
if (searchParams == null || searchParams.isEmpty()) {
return new ArrayList<SearchResult>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<SearchResult> results = client.multiSearch(indexName, indexName, searchParams);
for (int i = 0; i < searchParams.size(); i++) {
SearchResult searchResult = results.get(i);
SearchParam searchParam = searchParams.get(i);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(searchParams==null?0:searchParams.size());
try {
if (searchParams == null || searchParams.isEmpty()) {
return new ArrayList<SearchResult>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<SearchResult> results = client.multiSearch(indexName, indexName, searchParams);
for (int i = 0; i < searchParams.size(); i++) {
SearchResult searchResult = results.get(i);
SearchParam searchParam = searchParams.get(i);
this.publishSearchResultEvent(indexName, searchParam, searchResult);
}
return results;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
return results;
}
/**
... ... @@ -114,20 +151,28 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public Map<String, Object> doGetCommon(final String indexName, final String id) {
if (StringUtils.isBlank(id)) {
return null;
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames == null || realIndexNames.isEmpty()) {
return null;
}
GetResponse response = client.get(realIndexNames.get(0), indexName, id);
// 判断是否为空
if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
return null;
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(StringUtils.isBlank(id)?0:1);
try {
if (StringUtils.isBlank(id)) {
return null;
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames == null || realIndexNames.isEmpty()) {
return null;
}
GetResponse response = client.get(realIndexNames.get(0), indexName, id);
// 判断是否为空
if (response == null || response.getSource() == null || response.getSource().isEmpty()) {
return null;
}
return response.getSourceAsMap();
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
return response.getSourceAsMap();
}
/**
... ... @@ -137,26 +182,34 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public List<Map<String, Object>> doMultiGetCommon(final String indexName, final Collection<?> idList) throws Exception {
if (idList == null || idList.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames == null || realIndexNames.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
Set<String> idSet = new HashSet<String>();
for (Object id : idList) {
idSet.add(id.toString());
}
MultiGetResponse response = client.multiGet(realIndexNames.get(0), indexName, idSet, null);
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (MultiGetItemResponse item : response.getResponses()) {
if (item.getResponse().isExists()) {
results.add(item.getResponse().getSource());
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(idList==null||idList.isEmpty()?0:1);
try {
if (idList == null || idList.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
IElasticsearchClient client = esClientMgr.getClient(indexName);
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(indexName, client);
if (realIndexNames == null || realIndexNames.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
Set<String> idSet = new HashSet<String>();
for (Object id : idList) {
idSet.add(id.toString());
}
MultiGetResponse response = client.multiGet(realIndexNames.get(0), indexName, idSet, null);
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (MultiGetItemResponse item : response.getResponses()) {
if (item.getResponse().isExists()) {
results.add(item.getResponse().getSource());
}
}
return results;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
return results;
}
}
... ...
... ... @@ -148,7 +148,7 @@
</encoder>
</appender>
<!-- controller-cost appender -->
<!-- controller-performance appender -->
<appender name="CONTROLLER_PERFORMANCE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.home}/logs/%d{yyyy-MM-dd}/controller-performance.log</fileNamePattern>
... ... @@ -163,6 +163,21 @@
</encoder>
</appender>
<!-- es-performance appender -->
<appender name="ES_PERFORMANCE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.home}/logs/%d{yyyy-MM-dd}/es-performance.log</fileNamePattern>
<!-- 日志最大的保存天数 -->
<maxHistory>${maxHistory}</maxHistory>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>${maxFileSize}</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-1relative - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{0} -- %msg%n</pattern>
</encoder>
</appender>
<!-- downgrade appender -->
<appender name="DOWNGRADE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
... ... @@ -268,6 +283,12 @@
<level value="INFO"/>
<appender-ref ref="CONTROLLER_PERFORMANCE_APPENDER"/>
</logger>
<!-- ES耗时统计 -->
<logger name="ES_PERFORMANCE" additivity="false">
<level value="INFO"/>
<appender-ref ref="ES_PERFORMANCE_APPENDER"/>
</logger>
<!-- 降级日志 -->
<logger name="DOWNGRADE" additivity="false">
... ...