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;
... ... @@ -24,13 +26,20 @@ 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,12 +33,20 @@ 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) {
... ... @@ -65,10 +78,18 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
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) {
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,6 +122,9 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public List<SearchResult> doMutiSearch(final String indexName, final List<SearchParam> searchParams) {
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(searchParams==null?0:searchParams.size());
try {
if (searchParams == null || searchParams.isEmpty()) {
return new ArrayList<SearchResult>();
}
... ... @@ -104,6 +136,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
this.publishSearchResultEvent(indexName, searchParam, searchResult);
}
return results;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
/**
... ... @@ -114,6 +151,9 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public Map<String, Object> doGetCommon(final String indexName, final String id) {
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(StringUtils.isBlank(id)?0:1);
try {
if (StringUtils.isBlank(id)) {
return null;
}
... ... @@ -128,6 +168,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
return null;
}
return response.getSourceAsMap();
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
/**
... ... @@ -137,6 +182,9 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
* @return
*/
public List<Map<String, Object>> doMultiGetCommon(final String indexName, final Collection<?> idList) throws Exception {
long begin = System.currentTimeMillis();
performanceMonitor.addVisitCount(idList==null||idList.isEmpty()?0:1);
try {
if (idList == null || idList.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
... ... @@ -157,6 +205,11 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
}
}
return results;
} catch (Exception e) {
throw e;
}finally{
performanceMonitor.addCost(System.currentTimeMillis() - begin);
}
}
}
... ...
... ... @@ -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">
... ... @@ -269,6 +284,12 @@
<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">
<level value="INFO"/>
... ...