|
@@ -88,10 +88,14 @@ public class RecommendService {
|
|
|
private FlowPoolService flowPoolService;
|
|
|
@Autowired
|
|
|
private StatisticsLogService statisticsLogService;
|
|
|
+ @Autowired
|
|
|
+ private TimerLogService timerLogService;
|
|
|
|
|
|
@Autowired
|
|
|
private ABTestRemoteService abTestRemoteService;
|
|
|
|
|
|
+ private ThreadLocal<Map<String, Object>> timerLogMapTL = ThreadLocal.withInitial(HashMap::new);
|
|
|
+
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
}
|
|
@@ -105,60 +109,73 @@ public class RecommendService {
|
|
|
}
|
|
|
|
|
|
public RecommendResponse recommend(RecommendRequest request, int recommendType) {
|
|
|
- if (request == null) {
|
|
|
+ try {
|
|
|
+ timerLogMapTL.get().put("traceId", TraceUtils.currentTraceId());
|
|
|
+
|
|
|
+ if (request == null) {
|
|
|
+ return RecommendResponse.newBuilder()
|
|
|
+ .setResult(Result.newBuilder().setCode(1).setMessage("success"))
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ timerLogMapTL.get().put("appType", request.getAppType());
|
|
|
+ timerLogMapTL.get().put("recommendType", recommendType);
|
|
|
+
|
|
|
+ if (request.getVersionAuditStatus() == 1) {
|
|
|
+ timerLogMapTL.get().put("special", true);
|
|
|
+ return specialMidRecommend(request);
|
|
|
+ }
|
|
|
+ Stopwatch stopwatch = Stopwatch.createStarted();
|
|
|
+ if (StringUtils.isNotBlank(request.getMid())
|
|
|
+ && redisTemplate.opsForSet().isMember("special:mid", request.getMid())) {
|
|
|
+ return specialMidRecommend(request);
|
|
|
+ }
|
|
|
+ stopwatch.reset().start();
|
|
|
+ RecommendParam param = genRecommendParam(request, recommendType);
|
|
|
+ long genRecommendParamTime = stopwatch.stop().elapsed(TimeUnit.MILLISECONDS);
|
|
|
+ timerLogMapTL.get().put("genRecommendParamTime", genRecommendParamTime);
|
|
|
+ log.info("genRecommendParam={},genRecommendParam cost={}", JSONUtils.toJson(param),
|
|
|
+ genRecommendParamTime);
|
|
|
+ List<Video> videos = videoRecommend(param);
|
|
|
+ stopwatch.reset().start();
|
|
|
+ updateCache(request, param, videos);
|
|
|
+ long updateCacheTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
|
|
|
+ timerLogMapTL.get().put("updateCacheTime", updateCacheTime);
|
|
|
+ timerLogService.log(timerLogMapTL.get());
|
|
|
+ logStatisticsInfo(request, param, videos);
|
|
|
+ // 更新position
|
|
|
+ List<VideoProto> vps = new ArrayList<>();
|
|
|
+ for (int i = 0; i < videos.size(); i++) {
|
|
|
+ Map<String, List<String>> pushFromIndex = videos.get(i).getPushFromIndex();
|
|
|
+ List<PushFromIndex> pushFromIndexList = new ArrayList<>();
|
|
|
+ if (MapUtils.isNotEmpty(pushFromIndex)) {
|
|
|
+ pushFromIndex.forEach((k, v) -> {
|
|
|
+ PushFromIndex.Builder builder = PushFromIndex.newBuilder().setPushFrom(k);
|
|
|
+ v.forEach(builder::addIndex);
|
|
|
+ pushFromIndexList.add(builder.build());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ vps.add(VideoProto.newBuilder()
|
|
|
+ .setPosition(i + 1)
|
|
|
+ .setPushFrom(Strings.nullToEmpty(videos.get(i).getPushFrom()))
|
|
|
+ .setAbCode(Strings.nullToEmpty(videos.get(i).getAbCode()))
|
|
|
+ .setVideoId(videos.get(i).getVideoId())
|
|
|
+ .setRovScore(videos.get(i).getRovScore())
|
|
|
+ .setSortScore(videos.get(i).getSortScore())
|
|
|
+ .setFlowPool(Strings.nullToEmpty(videos.get(i).getFlowPool()))
|
|
|
+ .setIsInFlowPool(videos.get(i).isInFlowPool() ? 1 : 0)
|
|
|
+ .setRand(videos.get(i).getRand())
|
|
|
+ .addAllPushFromIndex(pushFromIndexList)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
return RecommendResponse.newBuilder()
|
|
|
.setResult(Result.newBuilder().setCode(1).setMessage("success"))
|
|
|
+ .addAllVideo(vps)
|
|
|
.build();
|
|
|
+ } finally {
|
|
|
+ timerLogMapTL.remove();
|
|
|
}
|
|
|
- if (request.getVersionAuditStatus() == 1) {
|
|
|
- return specialMidRecommend(request);
|
|
|
- }
|
|
|
- Stopwatch stopwatch = Stopwatch.createStarted();
|
|
|
- if (StringUtils.isNotBlank(request.getMid())
|
|
|
- && redisTemplate.opsForSet().isMember("special:mid", request.getMid())) {
|
|
|
- return specialMidRecommend(request);
|
|
|
- }
|
|
|
- stopwatch.reset().start();
|
|
|
-
|
|
|
- RecommendParam param = genRecommendParam(request, recommendType);
|
|
|
- log.info("genRecommendParam={},genRecommendParam cost={}", JSONUtils.toJson(param),
|
|
|
- stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
|
|
|
- stopwatch.reset().start();
|
|
|
- List<Video> videos = videoRecommend(param);
|
|
|
- stopwatch.reset().start();
|
|
|
- updateCache(request, param, videos);
|
|
|
-
|
|
|
- logStatisticsInfo(request, param, videos);
|
|
|
- // 更新position
|
|
|
- List<VideoProto> vps = new ArrayList<>();
|
|
|
- for (int i = 0; i < videos.size(); i++) {
|
|
|
- Map<String, List<String>> pushFromIndex = videos.get(i).getPushFromIndex();
|
|
|
- List<PushFromIndex> pushFromIndexList = new ArrayList<>();
|
|
|
- if (MapUtils.isNotEmpty(pushFromIndex)) {
|
|
|
- pushFromIndex.forEach((k, v) -> {
|
|
|
- PushFromIndex.Builder builder = PushFromIndex.newBuilder().setPushFrom(k);
|
|
|
- v.forEach(builder::addIndex);
|
|
|
- pushFromIndexList.add(builder.build());
|
|
|
- });
|
|
|
- }
|
|
|
- vps.add(VideoProto.newBuilder()
|
|
|
- .setPosition(i + 1)
|
|
|
- .setPushFrom(Strings.nullToEmpty(videos.get(i).getPushFrom()))
|
|
|
- .setAbCode(Strings.nullToEmpty(videos.get(i).getAbCode()))
|
|
|
- .setVideoId(videos.get(i).getVideoId())
|
|
|
- .setRovScore(videos.get(i).getRovScore())
|
|
|
- .setSortScore(videos.get(i).getSortScore())
|
|
|
- .setFlowPool(Strings.nullToEmpty(videos.get(i).getFlowPool()))
|
|
|
- .setIsInFlowPool(videos.get(i).isInFlowPool() ? 1 : 0)
|
|
|
- .setRand(videos.get(i).getRand())
|
|
|
- .addAllPushFromIndex(pushFromIndexList)
|
|
|
- .build());
|
|
|
- }
|
|
|
-
|
|
|
- return RecommendResponse.newBuilder()
|
|
|
- .setResult(Result.newBuilder().setCode(1).setMessage("success"))
|
|
|
- .addAllVideo(vps)
|
|
|
- .build();
|
|
|
|
|
|
}
|
|
|
|
|
@@ -478,13 +495,17 @@ public class RecommendService {
|
|
|
private List<Video> videoRecommend(RecommendParam param) {
|
|
|
Stopwatch stopwatch = Stopwatch.createStarted();
|
|
|
RecallResult recallResult = recallService.recall(convertToRecallParam(param));
|
|
|
- log.info("recallResult={}, videoRecommend recallResult cost={}", recallResult,
|
|
|
- stopwatch.elapsed(TimeUnit.MILLISECONDS));
|
|
|
+
|
|
|
+ long recallTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
|
|
|
+ timerLogMapTL.get().put("recallTime", recallTime);
|
|
|
+ log.info("recallResult={}, videoRecommend recallResult cost={}", recallResult, recallTime);
|
|
|
stopwatch.reset().start();
|
|
|
|
|
|
RankResult rankResult = rankRouter.rank(convertToRankParam(param, recallResult));
|
|
|
- log.info("rankResult={}, videoRecommend rank cost={}", rankResult,
|
|
|
- stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
|
|
|
+
|
|
|
+ long rankTime = stopwatch.elapsed(TimeUnit.MILLISECONDS);
|
|
|
+ timerLogMapTL.get().put("rankTime", rankTime);
|
|
|
+ log.info("rankResult={}, videoRecommend rank cost={}", rankResult, rankTime);
|
|
|
|
|
|
|
|
|
if (rankResult == null || CollectionUtils.isEmpty(rankResult.getVideos())) {
|