|
|
package com.yoho.search.consumer.service.logic;
|
|
|
|
|
|
import com.yoho.core.config.ConfigReader;
|
|
|
import com.yoho.search.dal.ScoreRuleMapper;
|
|
|
import com.yoho.search.dal.model.ScoreRule;
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
import org.apache.curator.framework.CuratorFramework;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -10,9 +9,10 @@ import org.springframework.beans.factory.InitializingBean; |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import com.yoho.core.config.ConfigReader;
|
|
|
import com.yoho.search.base.constants.ScoreRuleConstants;
|
|
|
import com.yoho.search.dal.ScoreRuleMapper;
|
|
|
import com.yoho.search.dal.model.ScoreRule;
|
|
|
|
|
|
/**
|
|
|
* Created by ginozhang on 2017/4/12.
|
...
|
...
|
@@ -20,79 +20,74 @@ import java.util.List; |
|
|
@Component
|
|
|
public class ScoreRuleLogicService implements InitializingBean {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ScoreRuleLogicService.class);
|
|
|
|
|
|
private static final List<String> SUPPORT_RULE_TYPES = Arrays.asList("BRAND_INCREASE_RULE","NEW_ARRIVAL_PAGE_DECAY_RULE","OTHER_PAGE_DECAY_RULE");
|
|
|
|
|
|
private static final String CONFIG_ROOT_PATH = "/yh/config";
|
|
|
|
|
|
private static final String FUNCTION_SCORE_RULE_KEY_TEMPLATE = "search.function.score.%s";
|
|
|
|
|
|
private static final String DEFAULT_RULE_VALUES = "-1";
|
|
|
|
|
|
@Autowired
|
|
|
private ScoreRuleMapper scoreRuleMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private ConfigReader configReader;
|
|
|
|
|
|
@Resource(name = "curatorFramework")
|
|
|
private CuratorFramework client;
|
|
|
|
|
|
@Override
|
|
|
public void afterPropertiesSet() throws Exception {
|
|
|
for (String ruleType : SUPPORT_RULE_TYPES) {
|
|
|
ScoreRule scoreRule = scoreRuleMapper.selectByRuleType(ruleType);
|
|
|
if (scoreRule != null) {
|
|
|
tryUpdateFunctionScoreRule(scoreRule);
|
|
|
} else {
|
|
|
tryRemoveFunctionScoreRule(ruleType);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void tryUpdateFunctionScoreRule(ScoreRule scoreRule) {
|
|
|
if (!SUPPORT_RULE_TYPES.contains(scoreRule.getRuleType())) {
|
|
|
logger.warn("[func=ScoreRuleLogicService.tryUpdateFunctionScoreRule][ruleType={}][message=invalid rule type.]", scoreRule.getRuleType());
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
String key = String.format(FUNCTION_SCORE_RULE_KEY_TEMPLATE, scoreRule.getRuleType().toLowerCase());
|
|
|
String srcValue = configReader.getString(key, DEFAULT_RULE_VALUES);
|
|
|
if (!scoreRule.getRuleValues().equals(srcValue)) {
|
|
|
logger.info("[func=ScoreRuleLogicService.tryUpdateFunctionScoreRule][ruleType={}][srcValue={}][newValue={}]", scoreRule.getRuleType(), srcValue, scoreRule.getRuleValues());
|
|
|
publishToZk(key, scoreRule.getRuleValues());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void tryRemoveFunctionScoreRule(String ruleType) {
|
|
|
if (!SUPPORT_RULE_TYPES.contains(ruleType)) {
|
|
|
logger.warn("[func=ScoreRuleLogicService.tryRemoveFunctionScoreRule][ruleType={}][message=invalid rule type.]", ruleType);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
String key = String.format(FUNCTION_SCORE_RULE_KEY_TEMPLATE, ruleType.toLowerCase());
|
|
|
String srcValue = configReader.getString(key, DEFAULT_RULE_VALUES);
|
|
|
if (!DEFAULT_RULE_VALUES.equals(srcValue)) {
|
|
|
logger.info("[func=ScoreRuleLogicService.tryRemoveFunctionScoreRule][ruleType={}][srcValue={}]", ruleType, srcValue);
|
|
|
publishToZk(key, DEFAULT_RULE_VALUES);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private synchronized void publishToZk(String key, String value) {
|
|
|
try {
|
|
|
if (client.checkExists().forPath(CONFIG_ROOT_PATH) == null) {
|
|
|
client.create().creatingParentContainersIfNeeded().forPath(CONFIG_ROOT_PATH);
|
|
|
}
|
|
|
|
|
|
String path = CONFIG_ROOT_PATH + "/" + key;
|
|
|
if (this.client.checkExists().forPath(path) == null) {
|
|
|
this.client.create().forPath(path, value.getBytes("UTF-8"));
|
|
|
} else {
|
|
|
this.client.setData().forPath(path, value.getBytes("UTF-8"));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error("publish function score rule " + key + " to zk failed!", e);
|
|
|
}
|
|
|
}
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ScoreRuleLogicService.class);
|
|
|
|
|
|
private static final String CONFIG_ROOT_PATH = "/yh/config";
|
|
|
|
|
|
private static final String DEFAULT_RULE_VALUES = "-1";
|
|
|
|
|
|
@Autowired
|
|
|
private ScoreRuleMapper scoreRuleMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private ConfigReader configReader;
|
|
|
|
|
|
@Resource(name = "curatorFramework")
|
|
|
private CuratorFramework client;
|
|
|
|
|
|
@Override
|
|
|
public void afterPropertiesSet() throws Exception {
|
|
|
for (String ruleType : ScoreRuleConstants.getSupportScoreRule()) {
|
|
|
ScoreRule scoreRule = scoreRuleMapper.selectByRuleType(ruleType);
|
|
|
if (scoreRule != null) {
|
|
|
tryUpdateFunctionScoreRule(scoreRule);
|
|
|
} else {
|
|
|
tryRemoveFunctionScoreRule(ruleType);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void tryUpdateFunctionScoreRule(ScoreRule scoreRule) {
|
|
|
if (!ScoreRuleConstants.isScoreRuleSupport(scoreRule.getRuleType())) {
|
|
|
logger.warn("[func=ScoreRuleLogicService.tryUpdateFunctionScoreRule][ruleType={}][message=invalid rule type.]", scoreRule.getRuleType());
|
|
|
return;
|
|
|
}
|
|
|
String key = ScoreRuleConstants.getZkDynamicConfigKey(scoreRule.getRuleType());
|
|
|
String srcValue = configReader.getString(key, DEFAULT_RULE_VALUES);
|
|
|
if (!scoreRule.getRuleValues().equals(srcValue)) {
|
|
|
logger.info("[func=ScoreRuleLogicService.tryUpdateFunctionScoreRule][ruleType={}][srcValue={}][newValue={}]", scoreRule.getRuleType(), srcValue,
|
|
|
scoreRule.getRuleValues());
|
|
|
publishToZk(key, scoreRule.getRuleValues());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void tryRemoveFunctionScoreRule(String ruleType) {
|
|
|
if (!ScoreRuleConstants.isScoreRuleSupport(ruleType)) {
|
|
|
logger.warn("[func=ScoreRuleLogicService.tryRemoveFunctionScoreRule][ruleType={}][message=invalid rule type.]", ruleType);
|
|
|
return;
|
|
|
}
|
|
|
String key = ScoreRuleConstants.getZkDynamicConfigKey(ruleType);
|
|
|
String srcValue = configReader.getString(key, DEFAULT_RULE_VALUES);
|
|
|
if (!DEFAULT_RULE_VALUES.equals(srcValue)) {
|
|
|
logger.info("[func=ScoreRuleLogicService.tryRemoveFunctionScoreRule][ruleType={}][srcValue={}]", ruleType, srcValue);
|
|
|
publishToZk(key, DEFAULT_RULE_VALUES);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private synchronized void publishToZk(String key, String value) {
|
|
|
try {
|
|
|
if (client.checkExists().forPath(CONFIG_ROOT_PATH) == null) {
|
|
|
client.create().creatingParentContainersIfNeeded().forPath(CONFIG_ROOT_PATH);
|
|
|
}
|
|
|
|
|
|
String path = CONFIG_ROOT_PATH + "/" + key;
|
|
|
if (this.client.checkExists().forPath(path) == null) {
|
|
|
this.client.create().forPath(path, value.getBytes("UTF-8"));
|
|
|
} else {
|
|
|
this.client.setData().forPath(path, value.getBytes("UTF-8"));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error("publish function score rule " + key + " to zk failed!", e);
|
|
|
}
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|