-
Notifications
You must be signed in to change notification settings - Fork 13
/
pairec.go
175 lines (157 loc) · 4.96 KB
/
pairec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package pairec
import (
"encoding/json"
"flag"
"io"
"net/http"
"os"
"github.com/alibaba/pairec/v2/datasource/graph"
"github.com/alibaba/pairec/v2/datasource/hbase_thrift"
"github.com/alibaba/pairec/v2/datasource/kafka"
"github.com/alibaba/pairec/v2/datasource/opensearch"
"github.com/alibaba/pairec/v2/abtest"
"github.com/alibaba/pairec/v2/algorithm"
"github.com/alibaba/pairec/v2/config"
"github.com/alibaba/pairec/v2/datasource/beengine"
"github.com/alibaba/pairec/v2/datasource/datahub"
"github.com/alibaba/pairec/v2/datasource/ha3engine"
"github.com/alibaba/pairec/v2/datasource/hbase"
"github.com/alibaba/pairec/v2/datasource/sls"
"github.com/alibaba/pairec/v2/filter"
"github.com/alibaba/pairec/v2/persist/clickhouse"
"github.com/alibaba/pairec/v2/persist/fs"
"github.com/alibaba/pairec/v2/persist/holo"
"github.com/alibaba/pairec/v2/persist/lindorm"
"github.com/alibaba/pairec/v2/persist/mysqldb"
"github.com/alibaba/pairec/v2/persist/redisdb"
"github.com/alibaba/pairec/v2/persist/tablestoredb"
"github.com/alibaba/pairec/v2/recconf"
"github.com/alibaba/pairec/v2/service"
"github.com/alibaba/pairec/v2/service/feature"
"github.com/alibaba/pairec/v2/service/general_rank"
"github.com/alibaba/pairec/v2/service/metrics"
"github.com/alibaba/pairec/v2/service/pipeline"
"github.com/alibaba/pairec/v2/service/rank"
"github.com/alibaba/pairec/v2/service/recall/berecall"
"github.com/alibaba/pairec/v2/sort"
"github.com/alibaba/pairec/v2/web"
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "go.uber.org/automaxprocs"
)
type hookfunc func() error
var (
hooks = make([]hookfunc, 0)
)
var configFile string
func init() {
flag.StringVar(&configFile, "config", "", "config file path")
flag.BoolVar(&config.AppConfig.WarmUpData, "warm-up-data", false, "create eas warm up data flag")
}
func AddStartHook(hf ...hookfunc) {
hooks = append(hooks, hf...)
}
func Run() {
mode := os.Getenv("RUN_MODE")
if mode != "COMMAND" {
flag.Parse()
}
configName := os.Getenv("CONFIG_NAME")
// if CONFIG_NAME is set, so load the pairec config from abtest server
// first create the abtest client connect to the server use the env params
if configName != "" {
abtest.LoadFromEnvironment()
ListenConfig(configName)
} else {
// load config from local file
err := recconf.LoadConfig(configFile)
if err != nil {
panic(err)
}
}
registerRouteInfo()
runStartHook()
PairecApp.Run()
}
func runBeforeStart() {
mysqldb.Load(recconf.Config)
redisdb.Load(recconf.Config)
tablestoredb.Load(recconf.Config)
sls.Load(recconf.Config)
kafka.Load(recconf.Config)
datahub.Load(recconf.Config)
beengine.Load(recconf.Config)
graph.Load(recconf.Config)
ha3engine.Load(recconf.Config)
opensearch.Load(recconf.Config)
hbase.Load(recconf.Config)
hbase_thrift.Load(recconf.Config)
holo.Load(recconf.Config)
lindorm.Load(recconf.Config)
fs.Load(recconf.Config)
clickhouse.Load(recconf.Config)
//abtest.Load(recconf.Config)
algorithm.Load(recconf.Config) // holo must be loaded before loading some algorithm
register(recconf.Config)
}
func runStartHook() {
runBeforeStart()
// first register hook
AddStartHook(func() error {
filter.Load(recconf.Config)
berecall.RegisterFilterWithConfig(recconf.Config)
sort.Load(recconf.Config)
// gdb.Load(recconf.Config)
service.Load(recconf.Config)
feature.UserLoadFeatureConfig(recconf.Config)
feature.LoadFeatureConfig(recconf.Config)
general_rank.LoadGeneralRankWithConfig(recconf.Config)
rank.LoadColdStartRankConfig(recconf.Config)
pipeline.LoadPipelineConfigs(recconf.Config)
// clean log dir
ClearDir(recconf.Config.LogConf)
return nil
})
for _, hf := range hooks {
if err := hf(); err != nil {
panic(err)
}
}
}
func registerRouteInfo() {
// use for listen http server state
HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "success")
})
HandleFunc("/route_paths", func(w http.ResponseWriter, r *http.Request) {
paths := PairecApp.Handlers.GetRoutePath()
var result []string
for _, p := range paths {
if p == "/ping" ||
p == "/route_paths" ||
p == "/api/recommend" ||
p == "/api/recall" ||
p == "/api/feature_reply" ||
p == "/metrics" ||
p == "/custom_metrics" {
continue
}
result = append(result, p)
}
w.Header().Add("content-type", "application/json")
w.WriteHeader(http.StatusOK)
d, _ := json.Marshal(result)
io.WriteString(w, string(d))
})
// register recommend Controller
Route("/api/recommend", &web.RecommendController{})
Route("/api/recall", &web.UserRecallController{})
Route("/api/callback", &web.CallBackController{})
Route("/api/feature_reply", &web.FeatureReplyController{})
Route("/api/embedding", &web.EmbeddingController{})
HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
promhttp.Handler().ServeHTTP(w, r)
})
HandleFunc("/custom_metrics", func(w http.ResponseWriter, r *http.Request) {
promhttp.HandlerFor(metrics.CustomRegister, promhttp.HandlerOpts{}).ServeHTTP(w, r)
})
}