forked from goodrain/rainbond
/
datacenter.go
242 lines (217 loc) · 6.36 KB
/
datacenter.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
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package config
import (
"context"
"fmt"
"github.com/coreos/etcd/mvcc/mvccpb"
"github.com/pquerna/ffjson/ffjson"
"github.com/goodrain/rainbond/cmd/node/option"
"github.com/goodrain/rainbond/node/api/model"
"github.com/goodrain/rainbond/node/core/store"
"github.com/goodrain/rainbond/util"
"github.com/Sirupsen/logrus"
client "github.com/coreos/etcd/clientv3"
)
//DataCenterConfig 数据中心配置
type DataCenterConfig struct {
config *model.GlobalConfig
options *option.Conf
ctx context.Context
cancel context.CancelFunc
//group config 不持久化
groupConfigs map[string]*GroupContext
}
var dataCenterConfig *DataCenterConfig
//GetDataCenterConfig 获取
func GetDataCenterConfig() *DataCenterConfig {
if dataCenterConfig == nil {
return CreateDataCenterConfig()
}
return dataCenterConfig
}
//CreateDataCenterConfig 创建
func CreateDataCenterConfig() *DataCenterConfig {
ctx, cancel := context.WithCancel(context.Background())
dataCenterConfig = &DataCenterConfig{
options: option.Config,
ctx: ctx,
cancel: cancel,
config: &model.GlobalConfig{
Configs: make(map[string]*model.ConfigUnit),
},
groupConfigs: make(map[string]*GroupContext),
}
res, err := store.DefalutClient.Get(dataCenterConfig.options.ConfigStoragePath+"/global", client.WithPrefix())
if err != nil {
logrus.Error("load datacenter config error.", err.Error())
}
if len(res.Kvs) < 1 {
// dgc := model.CreateDefaultGlobalConfig()
// err := dataCenterConfig.PutDataCenterConfig(dgc)
// if err != nil {
// logrus.Error("put datacenter config error,", err.Error())
// }
dgc := &model.GlobalConfig{
Configs: make(map[string]*model.ConfigUnit),
}
dataCenterConfig.config = dgc
} else {
for _, kv := range res.Kvs {
dataCenterConfig.PutConfigKV(kv)
}
}
return dataCenterConfig
}
//Start 启动,监听配置变化
func (d *DataCenterConfig) Start() {
go util.Exec(d.ctx, func() error {
ctx, cancel := context.WithCancel(d.ctx)
defer cancel()
logrus.Info("datacenter config listener start")
ch := store.DefalutClient.WatchByCtx(ctx, d.options.ConfigStoragePath+"/global", client.WithPrefix())
for event := range ch {
for _, e := range event.Events {
switch {
case e.IsCreate(), e.IsModify():
d.PutConfigKV(e.Kv)
case e.Type == client.EventTypeDelete:
d.DeleteConfig(util.GetIDFromKey(string(e.Kv.Key)))
}
}
}
return nil
}, 1)
}
//Stop 停止监听
func (d *DataCenterConfig) Stop() {
d.cancel()
logrus.Info("datacenter config listener stop")
}
//GetDataCenterConfig 获取配置
func (d *DataCenterConfig) GetDataCenterConfig() (*model.GlobalConfig, error) {
return d.config, nil
}
//PutDataCenterConfig 更改配置
func (d *DataCenterConfig) PutDataCenterConfig(c *model.GlobalConfig) (err error) {
if c == nil {
return
}
for k, v := range c.Configs {
d.config.Add(*v)
_, err = store.DefalutClient.Put(d.options.ConfigStoragePath+"/global/"+k, v.String())
}
return err
}
//GetConfig 获取全局配置
func (d *DataCenterConfig) GetConfig(name string) *model.ConfigUnit {
return d.config.Get(name)
}
//CacheConfig 更新配置缓存
func (d *DataCenterConfig) CacheConfig(c *model.ConfigUnit) error {
if c.Name == "" {
return fmt.Errorf("config name can not be empty")
}
logrus.Debugf("add config %v", c)
//将值类型由[]interface{} 转 []string
if c.ValueType == "array" {
switch c.Value.(type) {
case []interface{}:
var data []string
for _, v := range c.Value.([]interface{}) {
data = append(data, v.(string))
}
c.Value = data
}
oldC := d.config.Get(c.Name)
if oldC != nil {
switch oldC.Value.(type) {
case string:
value := append(c.Value.([]string), oldC.Value.(string))
util.Deweight(&value)
c.Value = value
case []string:
value := append(c.Value.([]string), oldC.Value.([]string)...)
util.Deweight(&value)
c.Value = value
default:
}
}
}
d.config.Add(*c)
return nil
}
//PutConfig 增加or更新配置
func (d *DataCenterConfig) PutConfig(c *model.ConfigUnit) error {
if c.Name == "" {
return fmt.Errorf("config name can not be empty")
}
logrus.Debugf("add config %v", c)
//将值类型由[]interface{} 转 []string
if c.ValueType == "array" {
switch c.Value.(type) {
case []interface{}:
var data []string
for _, v := range c.Value.([]interface{}) {
data = append(data, v.(string))
}
c.Value = data
}
oldC := d.config.Get(c.Name)
if oldC != nil {
switch oldC.Value.(type) {
case string:
value := append(c.Value.([]string), oldC.Value.(string))
util.Deweight(&value)
c.Value = value
case []string:
value := append(c.Value.([]string), oldC.Value.([]string)...)
util.Deweight(&value)
c.Value = value
default:
}
}
}
d.config.Add(*c)
//持久化
_, err := store.DefalutClient.Put(d.options.ConfigStoragePath+"/global/"+c.Name, c.String())
if err != nil {
logrus.Error("put datacenter config to etcd error.", err.Error())
return err
}
return nil
}
//PutConfigKV 更新
func (d *DataCenterConfig) PutConfigKV(kv *mvccpb.KeyValue) {
var cn model.ConfigUnit
if err := ffjson.Unmarshal(kv.Value, &cn); err == nil {
d.CacheConfig(&cn)
} else {
logrus.Errorf("parse config error,%s", err.Error())
}
}
//DeleteConfig 删除配置
func (d *DataCenterConfig) DeleteConfig(name string) {
d.config.Delete(name)
}
//GetGroupConfig get group config
func (d *DataCenterConfig) GetGroupConfig(groupID string) *GroupContext {
if c, ok := d.groupConfigs[groupID]; ok {
return c
}
c := NewGroupContext(groupID)
d.groupConfigs[groupID] = c
return c
}