-
Notifications
You must be signed in to change notification settings - Fork 112
/
dynamicConfig.go
438 lines (399 loc) · 11.4 KB
/
dynamicConfig.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package motan
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
URL "net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
vlog "github.com/weibocom/motan-go/log"
"github.com/weibocom/motan-go/core"
)
const dynamicConfigRegistrySnapshot = "registry.snap"
const dynamicConfigFilterPrefix = "af_"
type DynamicConfigurer struct {
runtimePath string
registrySnapshot string
registerNodes map[string]*core.URL
subscribeNodes map[string]*core.URL
regLock sync.Mutex
subLock sync.Mutex
saveLock sync.Mutex
agent *Agent
}
type RegistrySnapInfoStorage struct {
RegisterNodes []*core.URL `json:"register_nodes"`
SubscribeNodes []*core.URL `json:"subscribe_nodes"`
}
func NewDynamicConfigurer(agent *Agent) *DynamicConfigurer {
configurer := &DynamicConfigurer{
registrySnapshot: agent.runtimedir + string(filepath.Separator) + dynamicConfigRegistrySnapshot,
agent: agent,
registerNodes: make(map[string]*core.URL),
subscribeNodes: make(map[string]*core.URL),
}
configurer.initialize()
return configurer
}
func (c *DynamicConfigurer) initialize() {
if c.agent.recover {
c.doRecover()
}
}
func (c *DynamicConfigurer) doRecover() error {
bytes, err := ioutil.ReadFile(c.registrySnapshot)
if err != nil {
vlog.Warningln("Read configuration snapshot file error: " + err.Error())
return err
}
registerSnapInfo := new(RegistrySnapInfoStorage)
err = json.Unmarshal(bytes, registerSnapInfo)
if err != nil {
vlog.Errorln("Parse snapshot string error: " + err.Error())
return err
}
// recover just redo register and subscribe
for _, node := range registerSnapInfo.RegisterNodes {
vlog.Infof("Recover register node: %v", node)
c.doRegister(node)
}
for _, node := range registerSnapInfo.SubscribeNodes {
vlog.Infof("Recover subscribe node: %v", node)
c.doSubscribe(node)
}
return nil
}
func (c *DynamicConfigurer) Register(url *core.URL) error {
err := c.doRegister(url)
if err != nil {
return err
}
c.saveSnapshot()
return nil
}
func (c *DynamicConfigurer) doRegister(url *core.URL) error {
regGroupSuffix := os.Getenv(core.RegGroupSuffix)
if regGroupSuffix != "" && !strings.HasSuffix(url.Group, regGroupSuffix) {
url.Group += regGroupSuffix
}
c.regLock.Lock()
defer c.regLock.Unlock()
if _, ok := c.registerNodes[url.GetIdentityWithRegistry()]; ok {
return nil
}
err := c.agent.ExportService(url)
if err != nil {
vlog.Warningf("dynamic register failed, error: %s", err.Error())
} else {
c.registerNodes[url.GetIdentityWithRegistry()] = url
}
return nil
}
func (c *DynamicConfigurer) Unregister(url *core.URL) error {
err := c.doUnregister(url)
if err != nil {
return err
}
c.saveSnapshot()
return nil
}
func (c *DynamicConfigurer) doUnregister(url *core.URL) error {
c.regLock.Lock()
defer c.regLock.Unlock()
if _, ok := c.registerNodes[url.GetIdentityWithRegistry()]; ok {
c.agent.UnexportService(url)
delete(c.registerNodes, url.GetIdentityWithRegistry())
}
return nil
}
func (c *DynamicConfigurer) Subscribe(url *core.URL) error {
err := c.doSubscribe(url)
if err != nil {
return err
}
subGroupSuffix := os.Getenv(core.SubGroupSuffix)
if subGroupSuffix != "" && !strings.HasSuffix(url.Group, subGroupSuffix) {
newUrl := url.Copy()
newUrl.Group = url.Group + subGroupSuffix
err = c.doSubscribe(newUrl)
if err != nil {
return err
}
}
c.saveSnapshot()
return nil
}
func (c *DynamicConfigurer) doSubscribe(url *core.URL) error {
c.subLock.Lock()
defer c.subLock.Unlock()
if _, ok := c.subscribeNodes[url.GetIdentity()]; ok {
return nil
}
c.subscribeNodes[url.GetIdentity()] = url
err := c.agent.SubscribeService(url)
if err != nil {
vlog.Warningf("dynamic subscribe failed, error: %s", err.Error())
}
return nil
}
func (c *DynamicConfigurer) saveSnapshot() {
c.saveLock.Lock()
defer c.saveLock.Unlock()
bytes, err := json.Marshal(c.getRegistryInfo())
if err != nil {
vlog.Errorln("Convert registry information to json error: " + err.Error())
return
}
err = ioutil.WriteFile(c.registrySnapshot, bytes, 0644)
if err != nil {
vlog.Errorln("Write registry snapshot file error: " + err.Error())
return
}
}
func (c *DynamicConfigurer) getRegistryInfo() *RegistrySnapInfoStorage {
registrySnapInfo := RegistrySnapInfoStorage{}
c.regLock.Lock()
defer c.regLock.Unlock()
registerNodes := make([]*core.URL, 0, len(c.registerNodes))
for _, node := range c.registerNodes {
registerNodes = append(registerNodes, node.Copy())
}
c.subLock.Lock()
defer c.subLock.Unlock()
subscribeNodes := make([]*core.URL, 0, len(c.subscribeNodes))
for _, node := range c.subscribeNodes {
subscribeNodes = append(subscribeNodes, node.Copy())
}
registrySnapInfo.RegisterNodes = registerNodes
registrySnapInfo.SubscribeNodes = subscribeNodes
return ®istrySnapInfo
}
type DynamicConfigurerHandler struct {
agent *Agent
lock sync.Mutex
}
func (h *DynamicConfigurerHandler) SetAgent(agent *Agent) {
h.lock.Lock()
defer h.lock.Unlock()
h.agent = agent
}
func (h *DynamicConfigurerHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json;charset=utf-8")
switch req.RequestURI {
case "/registry/register":
h.register(res, req)
case "/registry/unregister":
h.unregister(res, req)
case "/registry/subscribe":
h.subscribe(res, req)
case "/registry/list":
h.list(res, req)
case "/registry/info":
h.info(res, req)
default:
res.WriteHeader(http.StatusNotFound)
}
}
func (h *DynamicConfigurerHandler) readURLsFromRequest(req *http.Request) ([]*core.URL, error) {
bytes, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
url := new(core.URL)
err = json.Unmarshal(bytes, url)
if err != nil {
return nil, err
}
//add additional service group from environment
if v := os.Getenv(core.GroupEnvironmentName); v != "" {
if url.Group == "" {
url.Group = v
} else {
url.Group += "," + v
}
}
groups := core.SlicesUnique(core.TrimSplit(url.Group, core.GroupNameSeparator))
urls := []*core.URL{}
for _, group := range groups {
u := url.Copy()
u.Group = group
urls = append(urls, u)
}
return urls, nil
}
func (h *DynamicConfigurerHandler) readURLs(req *http.Request) ([]*core.URL, error) {
urls, err := h.readURLsFromRequest(req)
if err != nil {
return nil, err
}
err = h.parseURLs(urls)
if err != nil {
return nil, err
}
return urls, nil
}
func (h *DynamicConfigurerHandler) parseURLs(urls []*core.URL) error {
for _, u := range urls {
_, e := h.parseURL(u)
if e != nil {
return e
}
}
return nil
}
func (h *DynamicConfigurerHandler) parseURL(url *core.URL) (*core.URL, error) {
if url.Group == "" {
url.Group = url.GetParam(core.GroupKey, "")
delete(url.Parameters, core.GroupKey)
}
registryID := ""
// such as 'direct://localhost:9981'
proxyRegistry := url.GetParam(core.ProxyRegistryKey, "")
if proxyRegistry != "" {
for id, url := range h.agent.Context.RegistryURLs {
if fmt.Sprintf("%s://%s:%d", url.Protocol, url.Host, url.Port) == proxyRegistry {
registryID = id
vlog.Infof("find proxy registry by ProxyRegistryKey, id:%s", registryID)
break
}
}
}
if registryID == "" {
// find proper registry
proxyRegistryUrlString := url.GetParam(core.ProxyRegistryUrlString, "")
if proxyRegistryUrlString != "" {
unescapeString, err := URL.QueryUnescape(proxyRegistryUrlString)
if err == nil {
ProxyRegistryUrl := core.FromExtInfo(unescapeString)
if ProxyRegistryUrl != nil {
for id, url := range h.agent.Context.RegistryURLs {
if registryEquals(ProxyRegistryUrl, url) {
registryID = id
vlog.Infof("find proxy registry by registryEquals, id:%s", registryID)
break
}
}
// TODO 动态添加 registry url, 需要考虑并发 问题
}
}
}
}
if registryID == "" {
return nil, errors.New("registry not found")
}
url.PutParam(core.RegistryKey, registryID)
filters := ""
agentFilter := make([]string, 0, 8)
for _, f := range core.TrimSplit(url.GetParam(core.FilterKey, ""), ",") {
if !strings.HasPrefix(f, dynamicConfigFilterPrefix) {
continue
}
if f == dynamicConfigFilterPrefix {
continue
}
agentFilter = append(agentFilter, strings.TrimSpace(f[len(dynamicConfigFilterPrefix):]))
}
if len(agentFilter) > 0 {
filters = strings.Join(agentFilter, ",")
}
if filters != "" {
url.PutParam(core.FilterKey, filters)
}
//final filters: defaultFilter + globalFilter + filters
finalFilters := h.agent.Context.MergeFilterSet(
h.agent.Context.GetDefaultFilterSet(url),
h.agent.Context.GetGlobalFilterSet(url),
h.agent.Context.GetEnvGlobalFilterSet(),
core.GetRelevantFilters(),
h.agent.Context.GetFilterSet(url.GetStringParamsWithDefault(core.FilterKey, ""), ""),
)
if len(finalFilters) > 0 {
url.PutParam(core.FilterKey, h.agent.Context.FilterSetToStr(finalFilters))
}
return url, nil
}
func (h *DynamicConfigurerHandler) register(res http.ResponseWriter, req *http.Request) {
urls, err := h.readURLs(req)
if err != nil {
writeHandlerResponse(res, http.StatusBadRequest, err.Error(), nil)
return
}
for _, url := range urls {
url.PutParam(core.ProxyKey, url.Protocol+":"+url.GetPortStr())
url.PutParam(core.ExportKey, url.Protocol+":"+strconv.Itoa(h.agent.eport))
h.agent.initProxyServiceURL(url)
err = h.agent.configurer.Register(url)
if err != nil {
writeHandlerResponse(res, http.StatusInternalServerError, err.Error(), nil)
return
}
}
writeHandlerResponse(res, http.StatusOK, "ok", nil)
}
func (h *DynamicConfigurerHandler) unregister(res http.ResponseWriter, req *http.Request) {
urls, err := h.readURLs(req)
if err != nil {
writeHandlerResponse(res, http.StatusBadRequest, err.Error(), nil)
return
}
for _, url := range urls {
url.PutParam(core.ProxyKey, url.Protocol+":"+url.GetPortStr())
url.PutParam(core.ExportKey, url.Protocol+":"+strconv.Itoa(h.agent.eport))
h.agent.initProxyServiceURL(url)
err = h.agent.configurer.Unregister(url)
if err != nil {
writeHandlerResponse(res, http.StatusInternalServerError, err.Error(), nil)
return
}
}
writeHandlerResponse(res, http.StatusOK, "ok", nil)
}
func (h *DynamicConfigurerHandler) subscribe(res http.ResponseWriter, req *http.Request) {
urls, err := h.readURLs(req)
if err != nil {
writeHandlerResponse(res, http.StatusBadRequest, err.Error(), nil)
return
}
for _, url := range urls {
url.Host = ""
url.Port = 0
err = h.agent.configurer.Subscribe(url)
if err != nil {
writeHandlerResponse(res, http.StatusInternalServerError, err.Error(), nil)
return
}
}
writeHandlerResponse(res, http.StatusOK, "ok", nil)
}
func (h *DynamicConfigurerHandler) list(res http.ResponseWriter, req *http.Request) {
writeHandlerResponse(res, http.StatusOK, "ok", h.agent.configurer.getRegistryInfo())
}
func (h *DynamicConfigurerHandler) info(res http.ResponseWriter, req *http.Request) {
writeHandlerResponse(res, http.StatusOK, "ok", struct {
MeshPort int `json:"mesh_port"`
}{MeshPort: h.agent.port})
}
func writeHandlerResponse(res http.ResponseWriter, code int, message string, body interface{}) {
res.WriteHeader(code)
m := make(map[string]interface{})
m["code"] = code
if message != "" {
m["message"] = message
}
if body != nil {
m["body"] = body
}
bytes, _ := json.Marshal(m)
res.Write(bytes)
}
func registryEquals(reg1 *core.URL, reg2 *core.URL) bool {
if reg1 != nil && reg2 != nil {
return reg1.Protocol == reg2.Protocol && reg1.Host == reg2.Host && reg1.Port == reg2.Port
}
return false
}