recconf.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package recconf
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "gitlab.alibaba-inc.com/pai_biz_arch/pairec/config"
  6. )
  7. var (
  8. Config *RecommendConfig
  9. adapterName = "json"
  10. )
  11. const (
  12. DaoConf_Adapter_Mysql = "mysql"
  13. DaoConf_Adapter_Redis = "redis"
  14. DaoConf_Adapter_TableStore = "tablestore"
  15. DaoConf_Adapter_HBase = "hbase"
  16. DaoConf_Adapter_Hologres = "hologres"
  17. DataSource_Type_Kafka = "kafka"
  18. DataSource_Type_Datahub = "datahub"
  19. )
  20. func init() {
  21. Config = newRecommendConfig()
  22. }
  23. type RecommendConfig struct {
  24. RunMode string // Run Mode: daily | product
  25. Region string
  26. ListenConf ListenConfig
  27. FeatureConfs map[string]SceneFeatureConfig
  28. SortNames map[string][]string
  29. FilterNames map[string][]string
  30. AlgoConfs []AlgoConfig
  31. RecallConfs []RecallConfig
  32. FilterConfs []FilterConfig
  33. RedisConfs map[string]RedisConfig
  34. MysqlConfs map[string]MysqlConfig
  35. HologresConfs map[string]HologresConfig
  36. KafkaConfs map[string]KafkaConfig
  37. DatahubConfs map[string]DatahubConfig
  38. HBaseConfs map[string]HBaseConfig
  39. TableStoreConfs map[string]TableStoreConfig
  40. SceneConfs map[string]map[string]CategoryConfig
  41. RankConf map[string]RankConfig
  42. LogConf LogConfig
  43. ABTestConf ABTestConfig
  44. CallBackConfs map[string]CallBackConfig
  45. GeneralRankConfs map[string]GeneralRankConfig
  46. ColdStartRankConfs map[string]ColdStartRankConfig
  47. DPPConf []DPPSortConfig
  48. }
  49. type ListenConfig struct {
  50. HttpAddr string
  51. HttpPort int
  52. }
  53. type DaoConfig struct {
  54. Adapter string
  55. AdapterType string
  56. RedisName string
  57. RedisPrefix string
  58. RedisDefaultKey string
  59. MysqlName string
  60. MysqlTable string
  61. Config string
  62. TableStoreName string
  63. TableStoreTableName string
  64. HBasePrefix string
  65. HBaseName string
  66. HBaseTable string
  67. ColumnFamily string
  68. Qualifier string
  69. // hologres
  70. HologresName string
  71. HologresTableName string
  72. }
  73. type SceneFeatureConfig struct {
  74. FeatureLoadConfs []FeatureLoadConfig
  75. AsynLoadFeature bool
  76. }
  77. type FeatureLoadConfig struct {
  78. FeatureDaoConf FeatureDaoConfig
  79. Features []FeatureConfig
  80. }
  81. type FeatureDaoConfig struct {
  82. DaoConfig
  83. FeatureKey string
  84. FeatureStore string // user or item
  85. UserFeatureKeyName string
  86. ItemFeatureKeyName string
  87. UserSelectFields string
  88. ItemSelectFields string
  89. FeatureType string // per feature type has different way of build
  90. SequenceLength int
  91. SequenceName string
  92. SequenceEvent string
  93. SequenceDelim string
  94. // SequencePlayTime filter event by as least play time
  95. // like play event need to large than 10s, so set value is "play:10000", timeunit is ms
  96. // if has more than one event to filter, use ';' as delim , like "play:10000;read:50000"
  97. SequencePlayTime string
  98. SequenceOfflineTableName string
  99. // SequenceDimFields fetch other dimension fields from db
  100. SequenceDimFields string
  101. }
  102. type FeatureConfig struct {
  103. FeatureType string
  104. FeatureName string
  105. FeatureSource string
  106. FeatureStore string // user or item
  107. RemoveFeatureSource bool // delete feature source
  108. Normalizer string
  109. }
  110. type AlgoConfig struct {
  111. Name string
  112. Type string
  113. EasConf EasConfig
  114. VectorConf VectorConfig
  115. LookupConf LookupConfig
  116. SeldonConf SeldonConfig
  117. }
  118. type LookupConfig struct {
  119. FieldName string
  120. }
  121. type EasConfig struct {
  122. Processor string
  123. Url string
  124. Auth string
  125. SignatureName string
  126. Timeout int
  127. RetryTimes int
  128. ResponseFuncName string
  129. Outputs []string
  130. }
  131. type SeldonConfig struct {
  132. Url string
  133. ResponseFuncName string
  134. }
  135. type VectorConfig struct {
  136. ServerAddress string
  137. Timeout int64
  138. }
  139. type RecallConfig struct {
  140. Name string
  141. RecallType string
  142. RecallCount int
  143. RecallAlgo string
  144. ItemType string
  145. CacheAdapter string
  146. CacheConfig string
  147. CachePrefix string
  148. CacheTime int // cache time by seconds
  149. Triggers []TriggerConfig
  150. HologresVectorConf HologresVectorConfig
  151. UserCollaborativeDaoConf UserCollaborativeDaoConfig
  152. ItemCollaborativeDaoConf ItemCollaborativeDaoConfig
  153. User2ItemDaoConf User2ItemDaoConfig
  154. UserTopicDaoConf UserTopicDaoConfig
  155. DaoConf DaoConfig
  156. VectorDaoConf VectorDaoConfig
  157. ColdStartDaoConf ColdStartDaoConfig
  158. }
  159. type ColdStartDaoConfig struct {
  160. SqlDaoConfig
  161. TimeInterval int // second
  162. }
  163. type SqlDaoConfig struct {
  164. DaoConfig
  165. WhereClause string
  166. PrimaryKey string
  167. SelectFields string
  168. }
  169. type HologresVectorConfig struct {
  170. VectorTable string // example: "item_emb_{partition}", '{partition}' will be replaced by partition info
  171. VectorKeyField string
  172. VectorEmbeddingField string
  173. }
  174. type UserCollaborativeDaoConfig struct {
  175. DaoConfig
  176. User2ItemTable string
  177. Item2ItemTable string
  178. }
  179. type ItemCollaborativeDaoConfig struct {
  180. DaoConfig
  181. Item2ItemTable string
  182. }
  183. type User2ItemDaoConfig struct {
  184. DaoConfig
  185. User2ItemTable string
  186. Item2ItemTable string
  187. }
  188. type UserTopicDaoConfig struct {
  189. DaoConfig
  190. UserTopicTable string
  191. TopicItemTable string
  192. }
  193. type VectorDaoConfig struct {
  194. DaoConfig
  195. EmbeddingField string
  196. KeyField string
  197. // set the following fields to get partition info,
  198. // if not set, '{partition}' in table name won't be replaced (if it exists)
  199. PartitionInfoTable string
  200. PartitionInfoField string
  201. }
  202. type RedisConfig struct {
  203. Host string
  204. Port int
  205. Password string
  206. DbNum int
  207. MaxIdle int
  208. ConnectTimeout int
  209. ReadTimeout int
  210. WriteTimeout int
  211. }
  212. type MysqlConfig struct {
  213. DSN string
  214. }
  215. type HologresConfig struct {
  216. DSN string
  217. }
  218. type KafkaConfig struct {
  219. BootstrapServers string
  220. Topic string
  221. }
  222. type DatahubConfig struct {
  223. AccessId string
  224. AccessKey string
  225. Endpoint string
  226. ProjectName string
  227. TopicName string
  228. }
  229. type HBaseConfig struct {
  230. ZKQuorum string
  231. }
  232. type TableStoreConfig struct {
  233. EndPoint string
  234. InstanceName string
  235. AccessKeyId string
  236. AccessKeySecret string
  237. RoleArn string
  238. }
  239. type SceneConfig struct {
  240. Categories []string
  241. }
  242. type CategoryConfig struct {
  243. RecallNames []string
  244. AlwaysRank bool // when recall size is not enough, also need to rank
  245. }
  246. type RankConfig struct {
  247. RankAlgoList []string
  248. RankScore string
  249. Processor string
  250. BatchCount int
  251. }
  252. type OperatorValueConfig struct {
  253. Type string // "property", "function"
  254. Name string
  255. From string // item or user
  256. }
  257. type LogConfig struct {
  258. RetensionDays int
  259. DiskSize int // unit : G, if value = 20, the true size is 20G
  260. LogLevel string // valid value is DEBUG, INFO , ERROR , FATAL
  261. Output string // valid value is file, console
  262. }
  263. type ABTestConfig struct {
  264. Host string
  265. Token string
  266. }
  267. type FilterConfig struct {
  268. Name string
  269. FilterType string
  270. DaoConf DaoConfig
  271. MaxItems int
  272. TimeInterval int // second
  273. RetainNum int
  274. WriteLog bool
  275. WriteLogExcludeScenes []string
  276. GenerateItemDataFuncName string
  277. AdjustCountConfs []AdjustCountConfig
  278. ItemStateDaoConf ItemStateDaoConfig
  279. FilterEvaluableExpression string
  280. FilterParams []FilterParamConfig
  281. }
  282. type FilterParamConfig struct {
  283. Name string
  284. Operator string
  285. Type string // string, int, int64
  286. Value interface{}
  287. }
  288. type ItemStateDaoConfig struct {
  289. DaoConfig
  290. ItemFieldName string
  291. WhereClause string
  292. SelectFields string
  293. }
  294. type AdjustCountConfig struct {
  295. RecallName string
  296. Count int
  297. Type string
  298. }
  299. type CallBackConfig struct {
  300. DataSource DataSourceConfig
  301. }
  302. type GeneralRankConfig struct {
  303. FeatureLoadConfs []FeatureLoadConfig
  304. RankConf RankConfig
  305. RetainNum int
  306. BatchCount int
  307. }
  308. type ColdStartRankConfig struct {
  309. RecallName string
  310. AlgoName string
  311. }
  312. type DataSourceConfig struct {
  313. Name string
  314. Type string
  315. }
  316. type TriggerConfig struct {
  317. TriggerKey string
  318. DefaultValue string
  319. Boundaries []int
  320. }
  321. type DPPSortConfig struct {
  322. Name string
  323. DaoConf DaoConfig
  324. TableName string
  325. TableSuffixParam string
  326. TablePKey string
  327. EmbeddingColumn string
  328. EmbeddingSeparator string
  329. Alpha float64
  330. CacheTimeInMinutes int
  331. EmbeddingHookNames []string
  332. NormalizeEmb string
  333. WindowSize int
  334. EmbMissedThreshold float64
  335. FilterRetrieveIds []string
  336. EnsurePositiveSim string
  337. }
  338. func newRecommendConfig() *RecommendConfig {
  339. conf := RecommendConfig{
  340. RunMode: "daily",
  341. ListenConf: ListenConfig{
  342. HttpAddr: "",
  343. HttpPort: 80,
  344. },
  345. // SortNames: []string{"item_score"},
  346. }
  347. return &conf
  348. }
  349. func CopyConfig(src, dst *RecommendConfig, filters ...func(string) bool) {
  350. srcVal := reflect.ValueOf(src).Elem()
  351. srcType := reflect.TypeOf(src).Elem()
  352. dstVal := reflect.ValueOf(dst).Elem()
  353. numOfFields := srcVal.NumField()
  354. for i := 0; i < numOfFields; i++ {
  355. fieldType := srcType.Field(i)
  356. flag := true
  357. for _, filter := range filters {
  358. flag = filter(fieldType.Name)
  359. if flag == false {
  360. break
  361. }
  362. }
  363. if flag == false {
  364. continue
  365. }
  366. elemField := dstVal.FieldByName(fieldType.Name)
  367. if elemField.CanSet() {
  368. fieldVal := srcVal.Field(i)
  369. elemField.Set(fieldVal)
  370. }
  371. }
  372. }
  373. // parammeter:
  374. // filePath: config file path
  375. func Load(filePath string) error {
  376. configer, err := config.NewConfig(adapterName, filePath)
  377. if err != nil {
  378. return err
  379. }
  380. rawdata := configer.RawData()
  381. err = json.Unmarshal(rawdata, Config)
  382. if err != nil {
  383. return err
  384. }
  385. return nil
  386. }
  387. /**
  388. func loadConfig(filePath string) error {
  389. content, err := ioutil.ReadFile(filePath)
  390. if err != nil {
  391. return err
  392. }
  393. if len(content) == 0 {
  394. return errors.New("config content empty")
  395. }
  396. conf := RecommendConfig{}
  397. err = json.Unmarshal(content, &conf)
  398. if err != nil {
  399. return err
  400. }
  401. Config = &conf
  402. log.Info("load config success")
  403. return nil
  404. }
  405. **/