|  | @@ -1,20 +1,18 @@
 | 
	
		
			
				|  |  |  package com.tzld.piaoquan.ad.engine.service.score;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 | 
	
		
			
				|  |  | -import com.google.common.base.Stopwatch;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
 | 
	
		
			
				|  |  | +import com.tzld.piaoquan.ad.engine.commons.thread.ThreadPoolFactory;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.ad.engine.commons.util.*;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.ad.engine.service.feature.Feature;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.ad.engine.service.feature.FeatureService;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.ad.engine.service.score.dto.AdPlatformCreativeDTO;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.ad.engine.service.score.param.RankRecommendRequestParam;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
 | 
	
		
			
				|  |  |  import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  |  import org.apache.commons.collections4.MapUtils;
 | 
	
		
			
				|  |  |  import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | -import org.apache.hadoop.util.StopWatch;
 | 
	
		
			
				|  |  | -import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  import org.xm.Similarity;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -23,6 +21,7 @@ import java.io.IOException;
 | 
	
		
			
				|  |  |  import java.io.InputStream;
 | 
	
		
			
				|  |  |  import java.io.InputStreamReader;
 | 
	
		
			
				|  |  |  import java.util.*;
 | 
	
		
			
				|  |  | +import java.util.concurrent.CountDownLatch;
 | 
	
		
			
				|  |  |  import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  |  import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -37,6 +36,9 @@ public class RankService687 extends RankStrategyXGBBasic {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private Map<String, Double> bucketsLen = new HashMap<>();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Value("${similarity.concurrent: false}")
 | 
	
		
			
				|  |  | +    private boolean similarityConcurrent;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public List<AdRankItem> adItemRank(RankRecommendRequestParam request, ScoreParam scoreParam) {
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -75,54 +77,121 @@ public class RankService687 extends RankStrategyXGBBasic {
 | 
	
		
			
				|  |  |          long time1 = System.currentTimeMillis();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          List<AdRankItem> adRankItems = new ArrayList<>(request.getAdIdList().size());
 | 
	
		
			
				|  |  | -        for (AdPlatformCreativeDTO dto : request.getAdIdList()) {
 | 
	
		
			
				|  |  | -            long time20 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | -            AdRankItem adRankItem = new AdRankItem();
 | 
	
		
			
				|  |  | -            adRankItem.setAdId(dto.getCreativeId());
 | 
	
		
			
				|  |  | -            adRankItem.setCreativeCode(dto.getCreativeCode());
 | 
	
		
			
				|  |  | -            adRankItem.setAdVerId(dto.getAdVerId());
 | 
	
		
			
				|  |  | -            adRankItem.setVideoId(request.getVideoId());
 | 
	
		
			
				|  |  | -            adRankItem.setCpa(dto.getCpa());
 | 
	
		
			
				|  |  | -            adRankItem.setId(dto.getAdId());
 | 
	
		
			
				|  |  | -            adRankItem.setCampaignId(dto.getCampaignId());
 | 
	
		
			
				|  |  | -            adRankItem.setCpm(ObjUtil.nullOrDefault(dto.getCpm(), 90).doubleValue());
 | 
	
		
			
				|  |  | +        if (similarityConcurrent) {
 | 
	
		
			
				|  |  | +            for (AdPlatformCreativeDTO dto : request.getAdIdList()) {
 | 
	
		
			
				|  |  | +                AdRankItem adRankItem = new AdRankItem();
 | 
	
		
			
				|  |  | +                adRankItem.setAdId(dto.getCreativeId());
 | 
	
		
			
				|  |  | +                adRankItem.setCreativeCode(dto.getCreativeCode());
 | 
	
		
			
				|  |  | +                adRankItem.setAdVerId(dto.getAdVerId());
 | 
	
		
			
				|  |  | +                adRankItem.setVideoId(request.getVideoId());
 | 
	
		
			
				|  |  | +                adRankItem.setCpa(dto.getCpa());
 | 
	
		
			
				|  |  | +                adRankItem.setId(dto.getAdId());
 | 
	
		
			
				|  |  | +                adRankItem.setCampaignId(dto.getCampaignId());
 | 
	
		
			
				|  |  | +                adRankItem.setCpm(ObjUtil.nullOrDefault(dto.getCpm(), 90).doubleValue());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            String cidStr = dto.getCreativeId().toString();
 | 
	
		
			
				|  |  | -            Map<String, String> cidFeatureMap = new HashMap<>();
 | 
	
		
			
				|  |  | -            Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
 | 
	
		
			
				|  |  | -            Map<String, String> b1Feature = cidFeature.getOrDefault("alg_cid_feature_basic_info", new HashMap<>());
 | 
	
		
			
				|  |  | +                String cidStr = dto.getCreativeId().toString();
 | 
	
		
			
				|  |  | +                Map<String, String> cidFeatureMap = new HashMap<>();
 | 
	
		
			
				|  |  | +                Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
 | 
	
		
			
				|  |  | +                Map<String, String> b1Feature = cidFeature.getOrDefault("alg_cid_feature_basic_info", new HashMap<>());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            Map<String, Map<String, String>> adVerFeature = allAdVerFeature.getOrDefault(dto.getAdVerId(), new HashMap<>());
 | 
	
		
			
				|  |  | +                Map<String, Map<String, String>> adVerFeature = allAdVerFeature.getOrDefault(dto.getAdVerId(), new HashMap<>());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            Map<String, String> d1Feature = cidFeature.getOrDefault("alg_cid_feature_vid_cf", new HashMap<>());
 | 
	
		
			
				|  |  | +                Map<String, String> d1Feature = cidFeature.getOrDefault("alg_cid_feature_vid_cf", new HashMap<>());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            this.handleB1Feature(b1Feature, cidFeatureMap, cidStr);
 | 
	
		
			
				|  |  | +                this.handleB1Feature(b1Feature, cidFeatureMap, cidStr);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            this.handleB2ToB5AndB8ToB9Feature(cidFeature, adVerFeature, cidFeatureMap);
 | 
	
		
			
				|  |  | +                this.handleB2ToB5AndB8ToB9Feature(cidFeature, adVerFeature, cidFeatureMap);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            this.handleB6ToB7Feature(cidFeature, cidFeatureMap);
 | 
	
		
			
				|  |  | +                this.handleB6ToB7Feature(cidFeature, cidFeatureMap);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            this.handleC1UIFeature(midTimeDiffMap, actionStaticMap, cidFeatureMap, cidStr);
 | 
	
		
			
				|  |  | +                this.handleC1UIFeature(midTimeDiffMap, actionStaticMap, cidFeatureMap, cidStr);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            this.handleD1Feature(d1Feature, cidFeatureMap);
 | 
	
		
			
				|  |  | +                this.handleD1Feature(d1Feature, cidFeatureMap);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            this.handleD2Feature(vidRankMaps, cidFeatureMap, cidStr);
 | 
	
		
			
				|  |  | +                this.handleD2Feature(vidRankMaps, cidFeatureMap, cidStr);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            String title = b1Feature.getOrDefault("cidtitle", "");
 | 
	
		
			
				|  |  | -            long time21 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | -            this.handleE1AndE2Feature(e1Feature, e2Feature, title, cidFeatureMap);
 | 
	
		
			
				|  |  | -            long time22 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | -            this.handleD3AndB1Feature(d3Feature, title, cidFeatureMap);
 | 
	
		
			
				|  |  | -            long time23 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | -            log.info("cost={} other={} handleE1AndE2Feature={} handleD3AndB1Feature={}", time23 - time20,
 | 
	
		
			
				|  |  | -                    time21 - time20, time22 - time21, time23 - time22);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            adRankItem.setFeatureMap(cidFeatureMap);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            adRankItems.add(adRankItem);
 | 
	
		
			
				|  |  | +                adRankItem.setFeatureMap(cidFeatureMap);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | +                adRankItems.add(adRankItem);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            CountDownLatch cdl = new CountDownLatch(adRankItems.size());
 | 
	
		
			
				|  |  | +            for (AdRankItem item : adRankItems) {
 | 
	
		
			
				|  |  | +                ThreadPoolFactory.defaultPool().submit(() -> {
 | 
	
		
			
				|  |  | +                    try {
 | 
	
		
			
				|  |  | +                        String cidStr = String.valueOf(item.getAdId());
 | 
	
		
			
				|  |  | +                        Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
 | 
	
		
			
				|  |  | +                        Map<String, String> b1Feature = cidFeature.getOrDefault("alg_cid_feature_basic_info", new HashMap<>());
 | 
	
		
			
				|  |  | +                        String title = b1Feature.getOrDefault("cidtitle", "");
 | 
	
		
			
				|  |  | +                        long time21 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +                        this.handleE1AndE2Feature(e1Feature, e2Feature, title, item.getFeatureMap());
 | 
	
		
			
				|  |  | +                        long time22 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +                        this.handleD3AndB1Feature(d3Feature, title, item.getFeatureMap());
 | 
	
		
			
				|  |  | +                        long time23 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +                        log.info("cost={} handleE1AndE2Feature={} handleD3AndB1Feature={}", time23 - time21, time22 - time21, time23 - time22);
 | 
	
		
			
				|  |  | +                    } finally{
 | 
	
		
			
				|  |  | +                        cdl.countDown();
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                cdl.await(200, TimeUnit.MILLISECONDS);
 | 
	
		
			
				|  |  | +            } catch (Exception e) {
 | 
	
		
			
				|  |  | +                log.error("handleE1AndE2Feature and handleD3AndB1Feature wait timeout", e);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            for (AdPlatformCreativeDTO dto : request.getAdIdList()) {
 | 
	
		
			
				|  |  | +                long time20 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +                AdRankItem adRankItem = new AdRankItem();
 | 
	
		
			
				|  |  | +                adRankItem.setAdId(dto.getCreativeId());
 | 
	
		
			
				|  |  | +                adRankItem.setCreativeCode(dto.getCreativeCode());
 | 
	
		
			
				|  |  | +                adRankItem.setAdVerId(dto.getAdVerId());
 | 
	
		
			
				|  |  | +                adRankItem.setVideoId(request.getVideoId());
 | 
	
		
			
				|  |  | +                adRankItem.setCpa(dto.getCpa());
 | 
	
		
			
				|  |  | +                adRankItem.setId(dto.getAdId());
 | 
	
		
			
				|  |  | +                adRankItem.setCampaignId(dto.getCampaignId());
 | 
	
		
			
				|  |  | +                adRankItem.setCpm(ObjUtil.nullOrDefault(dto.getCpm(), 90).doubleValue());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                String cidStr = dto.getCreativeId().toString();
 | 
	
		
			
				|  |  | +                Map<String, String> cidFeatureMap = new HashMap<>();
 | 
	
		
			
				|  |  | +                Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
 | 
	
		
			
				|  |  | +                Map<String, String> b1Feature = cidFeature.getOrDefault("alg_cid_feature_basic_info", new HashMap<>());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                Map<String, Map<String, String>> adVerFeature = allAdVerFeature.getOrDefault(dto.getAdVerId(), new HashMap<>());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                Map<String, String> d1Feature = cidFeature.getOrDefault("alg_cid_feature_vid_cf", new HashMap<>());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +                this.handleB1Feature(b1Feature, cidFeatureMap, cidStr);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                this.handleB2ToB5AndB8ToB9Feature(cidFeature, adVerFeature, cidFeatureMap);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                this.handleB6ToB7Feature(cidFeature, cidFeatureMap);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                this.handleC1UIFeature(midTimeDiffMap, actionStaticMap, cidFeatureMap, cidStr);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                this.handleD1Feature(d1Feature, cidFeatureMap);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                this.handleD2Feature(vidRankMaps, cidFeatureMap, cidStr);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                String title = b1Feature.getOrDefault("cidtitle", "");
 | 
	
		
			
				|  |  | +                long time21 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +                this.handleE1AndE2Feature(e1Feature, e2Feature, title, cidFeatureMap);
 | 
	
		
			
				|  |  | +                long time22 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +                this.handleD3AndB1Feature(d3Feature, title, cidFeatureMap);
 | 
	
		
			
				|  |  | +                long time23 = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +                log.info("cost={} other={} handleE1AndE2Feature={} handleD3AndB1Feature={}", time23 - time20,
 | 
	
		
			
				|  |  | +                        time21 - time20, time22 - time21, time23 - time22);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                adRankItem.setFeatureMap(cidFeatureMap);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                adRankItems.add(adRankItem);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          //
 | 
	
		
			
				|  |  |  
 |