Authored by hugufei

召回配置优化

1 package com.yoho.search.recall.config; 1 package com.yoho.search.recall.config;
2 2
3 -import com.alibaba.fastjson.JSON;  
4 import com.yoho.search.dal.model.CsRecallConfigCommon; 3 import com.yoho.search.dal.model.CsRecallConfigCommon;
5 import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService; 4 import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;
6 import org.apache.commons.lang.StringUtils; 5 import org.apache.commons.lang.StringUtils;
@@ -27,36 +26,39 @@ class RecallConfigCommonService { @@ -27,36 +26,39 @@ class RecallConfigCommonService {
27 26
28 private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor(); 27 private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
29 28
30 - private Map<String, Map<Integer, ConfigSizeInterval>> typePageConfigCache = new HashMap<>(); 29 + private Map<String, Map<Integer, ConfigSizeInterval>> recallConfigCommonCache = new HashMap<>();
31 30
32 @PostConstruct 31 @PostConstruct
33 void init() { 32 void init() {
34 - schedule.scheduleAtFixedRate(() -> loadConfig(), 0, 1, TimeUnit.MINUTES); 33 + schedule.scheduleAtFixedRate(() -> loadRecallConfigCommonCache(), 0, 1, TimeUnit.MINUTES);
35 } 34 }
36 35
37 - private void loadConfig() { 36 + private void loadRecallConfigCommonCache() {
38 try { 37 try {
39 List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll(); 38 List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();
40 - Map<String, Map<Integer, ConfigSizeInterval>> temp = new HashMap<>(); 39 + Map<String, Map<Integer, ConfigSizeInterval>> tempCache = new HashMap<>();
41 for (CsRecallConfigCommon csRecallConfigCommon : configList) { 40 for (CsRecallConfigCommon csRecallConfigCommon : configList) {
42 - Map<Integer, ConfigSizeInterval> pageConfigs = temp.get(csRecallConfigCommon.getConfigType());  
43 - if (pageConfigs == null) {  
44 - pageConfigs = new HashMap<>();  
45 - temp.put(csRecallConfigCommon.getConfigType(), pageConfigs); 41 + //1、类型判断
  42 + String configKey = csRecallConfigCommon.getConfigType();
  43 + if (!tempCache.containsKey(configKey)) {
  44 + tempCache.put(configKey, new HashMap<>());
46 } 45 }
47 - int size = csRecallConfigCommon.getSize(); 46 + //2、生成页面结果
  47 + int pageId = csRecallConfigCommon.getConfigPage();
  48 + int size = csRecallConfigCommon.getSize();
48 int interval = csRecallConfigCommon.getInterval(); 49 int interval = csRecallConfigCommon.getInterval();
49 - pageConfigs.put(csRecallConfigCommon.getConfigPage(), new ConfigSizeInterval(size, interval)); 50 + ConfigSizeInterval configSizeInterval = new ConfigSizeInterval(size, interval);
  51 + tempCache.get(configKey).put(pageId, configSizeInterval);
50 } 52 }
51 - typePageConfigCache = temp;  
52 - System.out.println(JSON.toJSONString(typePageConfigCache)); 53 + recallConfigCommonCache = tempCache;
  54 + logger.info("loadRecallConfigCommonCache success,recallConfigCommonCache size is[{}]", recallConfigCommonCache.size());
53 } catch (Exception e) { 55 } catch (Exception e) {
54 - logger.error(e.getMessage(), e); 56 + logger.error("loadRecallConfigCommonCache error,exception is:" + e.getMessage(), e);
55 } 57 }
56 } 58 }
57 59
58 private ConfigSizeInterval queryCommonConfig(String configKey, int configPage) { 60 private ConfigSizeInterval queryCommonConfig(String configKey, int configPage) {
59 - Map<Integer, ConfigSizeInterval> pageConfigMap = typePageConfigCache.get(configKey); 61 + Map<Integer, ConfigSizeInterval> pageConfigMap = recallConfigCommonCache.get(configKey);
60 if (pageConfigMap == null) { 62 if (pageConfigMap == null) {
61 return null; 63 return null;
62 } 64 }
1 package com.yoho.search.recall.config; 1 package com.yoho.search.recall.config;
2 2
3 -import com.alibaba.fastjson.JSON;  
4 import com.yoho.search.core.personalized.models.SortBrand; 3 import com.yoho.search.core.personalized.models.SortBrand;
5 import com.yoho.search.dal.model.CsRecallConfigProduct; 4 import com.yoho.search.dal.model.CsRecallConfigProduct;
6 import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService; 5 import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService;
@@ -25,59 +24,60 @@ class RecallConfigProductService { @@ -25,59 +24,60 @@ class RecallConfigProductService {
25 @Autowired 24 @Autowired
26 private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService; 25 private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService;
27 26
28 - private Map<String, Map<Integer, ConfigSknCount>> typePageConfigCache = new HashMap<>(); 27 + private Map<String, Map<Integer, ConfigSknCount>> recallSknCountConfigCache = new HashMap<>();
29 28
30 private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor(); 29 private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
31 30
32 @PostConstruct 31 @PostConstruct
33 void init() { 32 void init() {
34 - schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfig(), 0, 1, TimeUnit.MINUTES); 33 + schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfigCache(), 0, 1, TimeUnit.MINUTES);
35 } 34 }
36 35
37 - private void loadRecallSknCountConfig() { 36 + private void loadRecallSknCountConfigCache() {
38 try { 37 try {
39 - Map<String, Map<Integer, ConfigSknCount>> tempTypePageConfigCache = new HashMap<>();  
40 List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll(); 38 List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll();
  39 + Map<String, Map<Integer, ConfigSknCount>> tempCache = new HashMap<>();
41 for (CsRecallConfigProduct productConfig : configList) { 40 for (CsRecallConfigProduct productConfig : configList) {
  41 + //1、类型判断
42 String configType = productConfig.getConfigType(); 42 String configType = productConfig.getConfigType();
43 String configTypeId = productConfig.getConfigTypeId(); 43 String configTypeId = productConfig.getConfigTypeId();
44 - int pageId = productConfig.getConfigPage();  
45 - String configKey = this.buildConfiKey(configType,configTypeId); 44 + String configKey = this.buildConfiKey(configType, configTypeId);
  45 + if (!tempCache.containsKey(configKey)) {
  46 + tempCache.put(configKey, new HashMap<>());
  47 + }
  48 + //2、状态判断
46 int configStatus = productConfig.getConfigStatus(); 49 int configStatus = productConfig.getConfigStatus();
47 - if(configStatus==1){  
48 - this.addElement(configKey,pageId,this.genProductCountConfig(productConfig),tempTypePageConfigCache); 50 + if (configStatus != 1) {
  51 + continue;
49 } 52 }
  53 + //3、生成页面结果
  54 + int pageId = productConfig.getConfigPage();
  55 + ConfigSknCount configSknCount = this.genProductCountConfig(productConfig);
  56 + tempCache.get(configKey).put(pageId, configSknCount);
50 } 57 }
51 - typePageConfigCache = tempTypePageConfigCache;  
52 - System.out.println(JSON.toJSONString(typePageConfigCache)); 58 + recallSknCountConfigCache = tempCache;
  59 + logger.info("loadRecallSknCountConfigCache success,recallSknCountConfigCache size is[{}]", recallSknCountConfigCache.size());
53 } catch (Exception e) { 60 } catch (Exception e) {
54 - logger.error(e.getMessage(), e); 61 + logger.error("loadRecallSknCountConfigCache error,exception is:" + e.getMessage(), e);
55 } 62 }
56 } 63 }
57 64
58 private String buildConfiKey(String configType, String configTypeId) { 65 private String buildConfiKey(String configType, String configTypeId) {
59 if (configType.equalsIgnoreCase(RecallConfigConstants.SORT)) { 66 if (configType.equalsIgnoreCase(RecallConfigConstants.SORT)) {
60 - return this.buildCacheKey(RecallConfigConstants.SORT,configTypeId); 67 + return this.buildCacheKey(RecallConfigConstants.SORT, configTypeId);
61 } else if (configType.equalsIgnoreCase(RecallConfigConstants.BRAND)) { 68 } else if (configType.equalsIgnoreCase(RecallConfigConstants.BRAND)) {
62 - return this.buildCacheKey(RecallConfigConstants.BRAND,configTypeId);  
63 - }else if (configType.equalsIgnoreCase(RecallConfigConstants.SORT_BRAND)){  
64 - return this.buildCacheKey(RecallConfigConstants.SORT_BRAND,configTypeId);  
65 - }else {  
66 - return this.buildCacheKey(configType,configTypeId); 69 + return this.buildCacheKey(RecallConfigConstants.BRAND, configTypeId);
  70 + } else if (configType.equalsIgnoreCase(RecallConfigConstants.SORT_BRAND)) {
  71 + return this.buildCacheKey(RecallConfigConstants.SORT_BRAND, configTypeId);
  72 + } else {
  73 + return this.buildCacheKey(configType, configTypeId);
67 } 74 }
68 } 75 }
69 76
70 - private String buildCacheKey(String type,String cacheKey){ 77 + private String buildCacheKey(String type, String cacheKey) {
71 return type + "_" + cacheKey; 78 return type + "_" + cacheKey;
72 } 79 }
73 80
74 - private void addElement(String configKey, int pageId, ConfigSknCount configSknCount, Map<String, Map<Integer, ConfigSknCount>> map) {  
75 - if (!map.containsKey(configKey)) {  
76 - map.put(configKey, new HashMap<>());  
77 - }  
78 - map.get(configKey).put(pageId, configSknCount);  
79 - }  
80 -  
81 private ConfigSknCount genProductCountConfig(CsRecallConfigProduct configProduct) { 81 private ConfigSknCount genProductCountConfig(CsRecallConfigProduct configProduct) {
82 int newShelve = configProduct.getNewShelve(); 82 int newShelve = configProduct.getNewShelve();
83 int promotion = configProduct.getPromotion(); 83 int promotion = configProduct.getPromotion();
@@ -118,8 +118,8 @@ class RecallConfigProductService { @@ -118,8 +118,8 @@ class RecallConfigProductService {
118 */ 118 */
119 public ConfigSknCount queryRecallSknCount(int pageId, String sortBrandTypeKey, SortBrand sortBrand) { 119 public ConfigSknCount queryRecallSknCount(int pageId, String sortBrandTypeKey, SortBrand sortBrand) {
120 //1、先取【配置的品牌*品类】的配置 120 //1、先取【配置的品牌*品类】的配置
121 - ConfigSknCount configSknCount = this.queryConfigBySortBrandWithType(pageId,RecallConfigConstants.SORT_BRAND, sortBrand);  
122 - if(configSknCount !=null){ 121 + ConfigSknCount configSknCount = this.queryConfigBySortBrandWithType(pageId, RecallConfigConstants.SORT_BRAND, sortBrand);
  122 + if (configSknCount != null) {
123 return configSknCount; 123 return configSknCount;
124 } 124 }
125 //2、再取【配置的品牌或品类】配置 125 //2、再取【配置的品牌或品类】配置
@@ -148,6 +148,7 @@ class RecallConfigProductService { @@ -148,6 +148,7 @@ class RecallConfigProductService {
148 148
149 /** 149 /**
150 * 查询【品类*品牌】在页面上的召回配置,没有则取pageId=0的数据 150 * 查询【品类*品牌】在页面上的召回配置,没有则取pageId=0的数据
  151 + *
151 * @param pageId 152 * @param pageId
152 * @param sortBrandType 153 * @param sortBrandType
153 * @param sortBrand 154 * @param sortBrand
@@ -160,29 +161,31 @@ class RecallConfigProductService { @@ -160,29 +161,31 @@ class RecallConfigProductService {
160 161
161 /** 162 /**
162 * 查询中分类在页面上的召回配置,没有则取pageId=0的数据 163 * 查询中分类在页面上的召回配置,没有则取pageId=0的数据
  164 + *
163 * @param pageId 165 * @param pageId
164 * @param middleSortId 166 * @param middleSortId
165 * @return 167 * @return
166 */ 168 */
167 private ConfigSknCount queryConfigBySort(int pageId, int middleSortId) { 169 private ConfigSknCount queryConfigBySort(int pageId, int middleSortId) {
168 - String sortKey = this.buildCacheKey(RecallConfigConstants.SORT,String.valueOf(middleSortId)); 170 + String sortKey = this.buildCacheKey(RecallConfigConstants.SORT, String.valueOf(middleSortId));
169 return this.getRecallSknCount(sortKey, pageId); 171 return this.getRecallSknCount(sortKey, pageId);
170 } 172 }
171 173
172 /** 174 /**
173 * 查询品牌在页面上的召回配置,没有则取pageId=0的数据 175 * 查询品牌在页面上的召回配置,没有则取pageId=0的数据
  176 + *
174 * @param pageId 177 * @param pageId
175 * @param brandId 178 * @param brandId
176 * @return 179 * @return
177 */ 180 */
178 private ConfigSknCount queryConfigByBrand(int pageId, int brandId) { 181 private ConfigSknCount queryConfigByBrand(int pageId, int brandId) {
179 - String brandKey = this.buildCacheKey(RecallConfigConstants.BRAND,String.valueOf(brandId)); 182 + String brandKey = this.buildCacheKey(RecallConfigConstants.BRAND, String.valueOf(brandId));
180 return this.getRecallSknCount(brandKey, pageId); 183 return this.getRecallSknCount(brandKey, pageId);
181 } 184 }
182 185
183 186
184 private ConfigSknCount getRecallSknCount(String key, int pageId) { 187 private ConfigSknCount getRecallSknCount(String key, int pageId) {
185 - Map<Integer, ConfigSknCount> pageConfig = typePageConfigCache.get(key); 188 + Map<Integer, ConfigSknCount> pageConfig = recallSknCountConfigCache.get(key);
186 if (pageConfig == null) { 189 if (pageConfig == null) {
187 return null; 190 return null;
188 } 191 }
1 package com.yoho.search.recall.config; 1 package com.yoho.search.recall.config;
2 2
3 -import com.yoho.search.base.utils.CollectionUtils;  
4 import com.yoho.search.base.utils.ConvertUtils; 3 import com.yoho.search.base.utils.ConvertUtils;
5 import com.yoho.search.core.personalized.models.SortBrand; 4 import com.yoho.search.core.personalized.models.SortBrand;
6 import com.yoho.search.dal.model.CsRecallConfigSortBrand; 5 import com.yoho.search.dal.model.CsRecallConfigSortBrand;
7 import com.yoho.search.recall.models.personal.PagePersonalFactor; 6 import com.yoho.search.recall.models.personal.PagePersonalFactor;
8 import com.yoho.search.service.base.index.CsRecallConfigSortBrandIndexBaseService; 7 import com.yoho.search.service.base.index.CsRecallConfigSortBrandIndexBaseService;
  8 +import org.slf4j.Logger;
  9 +import org.slf4j.LoggerFactory;
9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Component; 11 import org.springframework.stereotype.Component;
11 12
@@ -21,10 +22,12 @@ import java.util.concurrent.TimeUnit; @@ -21,10 +22,12 @@ import java.util.concurrent.TimeUnit;
21 @Component 22 @Component
22 class RecallConfigSortBrandService { 23 class RecallConfigSortBrandService {
23 24
  25 + private static final Logger logger = LoggerFactory.getLogger(RecallConfigSortBrandService.class);
  26 +
24 @Autowired 27 @Autowired
25 private CsRecallConfigSortBrandIndexBaseService csRecallConfigSortBrandIndexBaseService; 28 private CsRecallConfigSortBrandIndexBaseService csRecallConfigSortBrandIndexBaseService;
26 29
27 - private List<SortBrand> configSortBrands = new ArrayList<>(); 30 + private List<SortBrand> configSortBrandsCache = new ArrayList<>();
28 31
29 private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor(); 32 private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
30 33
@@ -33,17 +36,22 @@ class RecallConfigSortBrandService { @@ -33,17 +36,22 @@ class RecallConfigSortBrandService {
33 schedule.scheduleAtFixedRate(() -> loadConfigSortBrands(), 0, 1, TimeUnit.MINUTES); 36 schedule.scheduleAtFixedRate(() -> loadConfigSortBrands(), 0, 1, TimeUnit.MINUTES);
34 } 37 }
35 38
36 - private void loadConfigSortBrands(){  
37 - List<SortBrand> temp = new ArrayList<>();  
38 - List<CsRecallConfigSortBrand> csRecallConfigSortBrands = csRecallConfigSortBrandIndexBaseService.queryAll();  
39 - for (CsRecallConfigSortBrand csRecallConfigSortBrand:csRecallConfigSortBrands){  
40 - Integer misort = csRecallConfigSortBrand.getSortId();  
41 - List<Integer> brandIds = ConvertUtils.stringToIntList(csRecallConfigSortBrand.getBrandIds(),",");  
42 - for (Integer brandId:brandIds){  
43 - temp.add(new SortBrand(misort,brandId)); 39 + private void loadConfigSortBrands() {
  40 + try {
  41 + List<SortBrand> tempCache = new ArrayList<>();
  42 + List<CsRecallConfigSortBrand> csRecallConfigSortBrands = csRecallConfigSortBrandIndexBaseService.queryAll();
  43 + for (CsRecallConfigSortBrand csRecallConfigSortBrand : csRecallConfigSortBrands) {
  44 + Integer misort = csRecallConfigSortBrand.getSortId();
  45 + List<Integer> brandIds = ConvertUtils.stringToIntList(csRecallConfigSortBrand.getBrandIds(), ",");
  46 + for (Integer brandId : brandIds) {
  47 + tempCache.add(new SortBrand(misort, brandId));
  48 + }
44 } 49 }
  50 + configSortBrandsCache = tempCache;
  51 + logger.info("loadConfigSortBrands success,configSortBrandsCache size is[{}]", configSortBrandsCache.size());
  52 + } catch (Exception e) {
  53 + logger.error("loadConfigSortBrands error,exception is:" + e.getMessage(), e);
45 } 54 }
46 - configSortBrands = temp;  
47 } 55 }
48 56
49 57
@@ -97,8 +105,8 @@ class RecallConfigSortBrandService { @@ -97,8 +105,8 @@ class RecallConfigSortBrandService {
97 if (size <= 0) { 105 if (size <= 0) {
98 return new ArrayList<>(); 106 return new ArrayList<>();
99 } 107 }
100 - //return this.randomConfigSortBrands(this.configSortBrands,pageSortBrandKeys, filterSortBrandKeys,size);  
101 - return this.randomConfigSortBrandForTest(pageFactor, pageSortBrandKeys, filterSortBrandKeys, size); 108 + return this.randomConfigSortBrands(this.configSortBrandsCache, pageSortBrandKeys, filterSortBrandKeys, size);
  109 + //return this.randomConfigSortBrandForTest(pageFactor, pageSortBrandKeys, filterSortBrandKeys, size);
102 } 110 }
103 111
104 } 112 }