/
model.go
215 lines (192 loc) · 5.81 KB
/
model.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package eas
import (
"errors"
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/alibaba/pairec/v2/log"
"github.com/alibaba/pairec/v2/pkg/eas"
tensorflow_serving "github.com/alibaba/pairec/v2/pkg/tensorflow_serving/apis"
"github.com/alibaba/pairec/v2/recconf"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
Eas_Processor_FM = "ALINK_FM"
Eas_Processor_PMML = "PMML"
Eas_Processor_TF = "TensorFlow"
Eas_Processor_TFServing = "TFServing"
Eas_Processor_EASYREC = "EasyRec"
Eas_Processor_LINUCB = "Linucb"
)
type EasModel struct {
retryTimes int
name string
request IEasRequest
}
func NewEasModel(name string) *EasModel {
return &EasModel{name: name, retryTimes: 2}
}
func (m *EasModel) Init(conf *recconf.AlgoConfig) error {
if conf.EasConf.Processor == Eas_Processor_PMML {
req := PMMLRequest{}
req.SetUrl(conf.EasConf.Url)
req.SetAuth(conf.EasConf.Auth)
req.SetTimeout(conf.EasConf.Timeout)
req.SetResponseFunc(conf.EasConf.ResponseFuncName)
m.request = &req
if conf.EasConf.RetryTimes > 0 {
m.retryTimes = conf.EasConf.RetryTimes
}
return nil
} else if conf.EasConf.Processor == Eas_Processor_TF {
req := TFRequest{}
req.SetUrl(conf.EasConf.Url)
req.SetAuth(conf.EasConf.Auth)
req.SetSignatureName(conf.EasConf.SignatureName)
req.SetTimeout(conf.EasConf.Timeout)
req.SetResponseFunc(conf.EasConf.ResponseFuncName)
if len(conf.EasConf.Outputs) > 0 {
req.SetOutputs(conf.EasConf.Outputs)
}
m.request = &req
if conf.EasConf.RetryTimes > 0 {
m.retryTimes = conf.EasConf.RetryTimes
}
return nil
} else if conf.EasConf.Processor == Eas_Processor_FM {
req := NewFMRequest()
req.SetUrl(conf.EasConf.Url)
req.SetAuth(conf.EasConf.Auth)
req.SetTimeout(conf.EasConf.Timeout)
req.SetResponseFunc(conf.EasConf.ResponseFuncName)
m.request = req
if conf.EasConf.RetryTimes > 0 {
m.retryTimes = conf.EasConf.RetryTimes
}
return nil
} else if conf.EasConf.Processor == Eas_Processor_EASYREC && conf.EasConf.EndpointType == eas.EndpointTypeDocker {
req := HttpEasyrecRequest{}
req.SetUrl(conf.EasConf.Url)
req.SetAuth(conf.EasConf.Auth)
req.SetTimeout(conf.EasConf.Timeout)
req.SetResponseFunc(conf.EasConf.ResponseFuncName)
m.request = &req
if conf.EasConf.RetryTimes > 0 {
m.retryTimes = conf.EasConf.RetryTimes
}
return nil
} else if conf.EasConf.Processor == Eas_Processor_EASYREC {
req := EasyrecRequest{}
req.SetUrl(conf.EasConf.Url)
req.SetAuth(conf.EasConf.Auth)
req.SetTimeout(conf.EasConf.Timeout)
req.SetResponseFunc(conf.EasConf.ResponseFuncName)
var client *eas.PredictClient
if conf.EasConf.EndpointType == eas.EndpointTypeDirect {
url := strings.ReplaceAll(conf.EasConf.Url, "http://", "")
index := strings.Index(url, "/api/predict/")
endpoint := url[:index]
strs := strings.Split(endpoint, ".")
region := strs[2]
for i := 0; i < len(strs); i++ {
if strs[i] == "pai-eas" {
region = strs[i-1]
break
}
}
name := url[index+len("/api/predict/"):]
client = eas.NewPredictClient(fmt.Sprintf("pai-eas-vpc.%s.aliyuncs.com", region), name)
client.SetEndpointType(eas.EndpointTypeDirect)
} else {
url := strings.ReplaceAll(conf.EasConf.Url, "http://", "")
index := strings.Index(url, "/api/predict/")
endpoint := url[:index]
name := url[index+len("/api/predict/"):]
client = eas.NewPredictClient(endpoint, name)
}
client.SetToken(conf.EasConf.Auth)
client.SetTimeout(conf.EasConf.Timeout)
client.SetHttpTransport(&http.Transport{
MaxConnsPerHost: 300,
MaxIdleConnsPerHost: 300,
MaxIdleConns: 300,
TLSHandshakeTimeout: 100 * time.Millisecond,
ExpectContinueTimeout: 200 * time.Millisecond,
DialContext: (&net.Dialer{
Timeout: 100 * time.Millisecond, // 100ms
KeepAlive: 10 * time.Minute,
}).DialContext,
})
if conf.EasConf.RetryTimes > 0 {
m.retryTimes = conf.EasConf.RetryTimes
}
client.SetRetryCount(m.retryTimes - 1)
// if use eas sdk, we should not retry
m.retryTimes = 1
client.Init()
req.EasClient = client
m.request = &req
return nil
} else if conf.EasConf.Processor == Eas_Processor_LINUCB {
req := LincubRequest{}
req.SetUrl(conf.EasConf.Url)
req.SetAuth(conf.EasConf.Auth)
req.SetTimeout(conf.EasConf.Timeout)
req.SetResponseFunc(conf.EasConf.ResponseFuncName)
m.request = &req
if conf.EasConf.RetryTimes > 0 {
m.retryTimes = conf.EasConf.RetryTimes
}
return nil
} else if conf.EasConf.Processor == Eas_Processor_TFServing {
req := TFServingRequest{}
req.SetUrl(conf.EasConf.Url)
req.SetModelName(conf.EasConf.ModelName)
req.SetAuth(conf.EasConf.Auth)
req.SetSignatureName(conf.EasConf.SignatureName)
req.SetTimeout(conf.EasConf.Timeout)
req.SetResponseFunc(conf.EasConf.ResponseFuncName)
if len(conf.EasConf.Outputs) > 0 {
req.SetOutputs(conf.EasConf.Outputs)
}
var opts []grpc.DialOption
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.Dial(req.url, opts...)
if err != nil {
panic(fmt.Sprintf("fail to dial: %v", err))
}
client := tensorflow_serving.NewPredictionServiceClient(conn)
req.Client = client
m.request = &req
if conf.EasConf.RetryTimes > 0 {
m.retryTimes = conf.EasConf.RetryTimes
}
return nil
}
return errors.New("not found eas Processor:" + conf.EasConf.Processor)
}
func (m *EasModel) Run(algoData interface{}) (interface{}, error) {
retryTimes := m.retryTimes
var (
data interface{}
err error
)
for {
retryTimes--
data, err = m.request.Invoke(algoData)
if err != nil && retryTimes == 0 {
return data, err
} else if err == nil {
break
} else if err != nil {
log.Warning("eas request fail :" + err.Error())
}
}
if m.request.GetResponseFunc() != nil {
return m.request.GetResponseFunc()(data)
}
return data, err
}