Browse Source

LowScoreStrategy threadPool

wangyunpeng 16 hours ago
parent
commit
a68dba10c5

+ 59 - 32
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/filter/strategy/LowScoreStrategy.java

@@ -1,5 +1,8 @@
 package com.tzld.longarticle.recommend.server.service.recommend.filter.strategy;
 
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import com.tzld.longarticle.recommend.server.common.CommonThreadPoolExecutor;
 import com.tzld.longarticle.recommend.server.model.dto.Content;
 import com.tzld.longarticle.recommend.server.model.dto.ContentHisPublishArticle;
 import com.tzld.longarticle.recommend.server.service.recommend.config.AccountContentPoolConfigService;
@@ -13,6 +16,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.*;
 
 @Component
 @Slf4j
@@ -23,42 +29,63 @@ public class LowScoreStrategy implements FilterStrategy {
     @Autowired
     AccountContentPoolConfigService accountContentPoolConfigService;
 
+    private final static ExecutorService pool = new CommonThreadPoolExecutor(
+            5,
+            5,
+            0L, TimeUnit.SECONDS,
+            new LinkedBlockingQueue<>(100),
+            new ThreadFactoryBuilder().setNameFormat("LowScoreStrategy-%d").build(),
+            new ThreadPoolExecutor.AbortPolicy());
+
     @Override
     public FilterResult filter(FilterParam param) {
-        long start = System.currentTimeMillis();
         FilterResult filterResult = new FilterResult();
-        filterResult.setContentIds(new ArrayList<>());
-        filterResult.setFilterContent(new ArrayList<>());
-        for (Content content : param.getContents()) {
-            String[] contentPools = accountContentPoolConfigService.getContentPools(param.getAccountName());
-            if (!contentPools[2].equals(content.getContentPoolType())) {
-                filterResult.getContentIds().add(content.getId());
-                continue;
-            }
-            if (CollectionUtils.isEmpty(content.getHisPublishArticleList())) {
-                filterResult.getContentIds().add(content.getId());
-                continue;
-            }
-            long publishCount = content.getHisPublishArticleList().stream().filter(ContentHisPublishArticle::isInnerAccount).count();
-            if (publishCount == 0) {
-                filterResult.getContentIds().add(content.getId());
-                continue;
-            }
-            int hisViewCount = content.getHisPublishArticleList().stream().filter(ContentHisPublishArticle::isInnerAccount)
-                    .mapToInt(ContentHisPublishArticle::getViewCount).sum();
-            int hisAvgViewCount = content.getHisPublishArticleList().stream().filter(ContentHisPublishArticle::isInnerAccount)
-                    .mapToInt(ContentHisPublishArticle::getAvgViewCount).sum();
-            double rate = (hisViewCount * 1.0) / hisAvgViewCount;
-            int sumAvgViewCount = content.getHisPublishArticleList().stream().filter(ContentHisPublishArticle::isInnerAccount)
-                    .mapToInt(ContentHisPublishArticle::getAvgViewCount).sum();
-            if (((publishCount >= 2 && rate < 0.8)
-                    || (publishCount == 1 && rate < 0.5)) && sumAvgViewCount > 100) {
-                content.setFilterReason("低评分内容");
-                filterResult.getFilterContent().add(content);
-            } else {
-                filterResult.getContentIds().add(content.getId());
-            }
+        List<String> result = Collections.synchronizedList(new ArrayList<>(param.getContents().size()));
+        List<Content> filterContents = Collections.synchronizedList(new ArrayList<>());
+        List<List<Content>> partitions = Lists.partition(param.getContents(), 1000);
+        CountDownLatch cdl = new CountDownLatch(partitions.size());
+        for (List<Content> partition : partitions) {
+            pool.submit(() -> {
+                try {
+                    for (Content content : partition) {
+                        String[] contentPools = accountContentPoolConfigService.getContentPools(param.getAccountName());
+                        if (!contentPools[2].equals(content.getContentPoolType())
+                                || CollectionUtils.isEmpty(content.getHisPublishArticleList())) {
+                            result.add(content.getId());
+                            continue;
+                        }
+                        long publishCount = content.getHisPublishArticleList().stream().filter(ContentHisPublishArticle::isInnerAccount).count();
+                        if (publishCount == 0) {
+                            result.add(content.getId());
+                            continue;
+                        }
+                        int hisViewCount = content.getHisPublishArticleList().stream().filter(ContentHisPublishArticle::isInnerAccount)
+                                .mapToInt(ContentHisPublishArticle::getViewCount).sum();
+                        int hisAvgViewCount = content.getHisPublishArticleList().stream().filter(ContentHisPublishArticle::isInnerAccount)
+                                .mapToInt(ContentHisPublishArticle::getAvgViewCount).sum();
+                        double rate = (hisViewCount * 1.0) / hisAvgViewCount;
+                        int sumAvgViewCount = content.getHisPublishArticleList().stream().filter(ContentHisPublishArticle::isInnerAccount)
+                                .mapToInt(ContentHisPublishArticle::getAvgViewCount).sum();
+                        if (((publishCount >= 2 && rate < 0.8)
+                                || (publishCount == 1 && rate < 0.5)) && sumAvgViewCount > 100) {
+                            content.setFilterReason("低评分内容");
+                            filterContents.add(content);
+                        } else {
+                            result.add(content.getId());
+                        }
+                    }
+                } finally {
+                    cdl.countDown();
+                }
+            });
+        }
+        try {
+            cdl.await();
+        } catch (InterruptedException e) {
+            log.error("LowScoreStrategy filter error", e);
         }
+        filterResult.setContentIds(result);
+        filterResult.setFilterContent(filterContents);
         return filterResult;
     }
 }

+ 1 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/filter/strategy/SensitiveStrategy.java

@@ -41,7 +41,7 @@ public class SensitiveStrategy implements FilterStrategy {
             5,
             5,
             0L, TimeUnit.SECONDS,
-            new LinkedBlockingQueue<>(100),
+            new LinkedBlockingQueue<>(200),
             new ThreadFactoryBuilder().setNameFormat("SensitiveStrategy-%d").build(),
             new ThreadPoolExecutor.AbortPolicy());