/
config.go
220 lines (188 loc) · 5.54 KB
/
config.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
// Copyright 2014 The mqrouter Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dbrouter
import (
"context"
"fmt"
"github.com/coreos/etcd/client"
"github.com/shawnfeng/sutil/scontext"
"github.com/shawnfeng/sutil/setcd"
"github.com/shawnfeng/sutil/slog/slog"
"sync"
"time"
)
const (
DB_TYPE_MONGO = "mongo"
DB_TYPE_MYSQL = "mysql"
DB_TYPE_POSTGRES = "postgres"
)
const (
CONFIG_TYPE_SIMPLE = iota
CONFIG_TYPE_ETCD
)
type Config struct {
DBName string
DBType string
DBAddr []string
UserName string
PassWord string
TimeOut time.Duration
}
type Configer interface {
GetConfig(ctx context.Context, instance string) *Config
GetInstance(ctx context.Context, cluster, table string) (instance string)
GetConfigByGroup(ctx context.Context, instance, group string) *Config
GetGroups(ctx context.Context) []string
}
func NewConfiger(configType int, data []byte, dbChangeChan chan dbConfigChange) (Configer, error) {
switch configType {
case CONFIG_TYPE_SIMPLE:
close(dbChangeChan)
return NewSimpleConfiger(data)
case CONFIG_TYPE_ETCD:
return NewEtcdConfiger(context.TODO(), dbChangeChan)
default:
return nil, fmt.Errorf("configType %d error", configType)
}
}
type SimpleConfig struct {
parser *Parser
}
func NewSimpleConfiger(data []byte) (Configer, error) {
parser, err := NewParser(data)
return &SimpleConfig{
parser: parser,
}, err
}
func (m *SimpleConfig) GetConfig(ctx context.Context, instance string) *Config {
group := scontext.GetControlRouteGroupWithDefault(ctx, DefaultGroup)
info := m.parser.GetConfig(instance, group)
return &Config{
DBType: info.DBType,
DBAddr: info.DBAddr,
DBName: info.DBName,
UserName: info.UserName,
PassWord: info.PassWord,
TimeOut: 3 * time.Second,
}
}
func (m *SimpleConfig) GetConfigByGroup(ctx context.Context, instance, group string) *Config {
info := m.parser.GetConfig(instance, group)
return &Config{
DBType: info.DBType,
DBAddr: info.DBAddr,
DBName: info.DBName,
UserName: info.UserName,
PassWord: info.PassWord,
TimeOut: 3 * time.Second,
}
}
func (m *SimpleConfig) GetInstance(ctx context.Context, cluster, table string) (instance string) {
instance = m.parser.GetInstance(cluster, table)
return instance
}
func (m *SimpleConfig) GetGroups(ctx context.Context) []string {
var groups []string
for group, _ := range m.parser.dbIns {
groups = append(groups, group)
}
return groups
}
type EtcdConfig struct {
etcdAddr []string
parser *Parser
parserMu sync.RWMutex
}
func NewEtcdConfiger(ctx context.Context, dbChangeChan chan dbConfigChange) (Configer, error) {
fun := "NewEtcdConfiger -->"
etcdConfig := &EtcdConfig{
// TODO etcd address如何获取
etcdAddr: []string{"http://infra0.etcd.ibanyu.com:20002", "http://infra1.etcd.ibanyu.com:20002", "http://infra2.etcd.ibanyu.com:20002", "http://infra3.etcd.ibanyu.com:20002", "http://infra4.etcd.ibanyu.com:20002", "http://old0.etcd.ibanyu.com:20002", "http://old1.etcd.ibanyu.com:20002", "http://old2.etcd.ibanyu.com:20002"},
}
err := etcdConfig.init(ctx, dbChangeChan)
if err != nil {
slog.Errorf(ctx, "%s init etcd configer err: %s", fun, err.Error())
return nil, err
}
return etcdConfig, nil
}
func (m *EtcdConfig) init(ctx context.Context, dbChangeChan chan dbConfigChange) error {
fun := "EtcdConfig.init -->"
etcdInstance, err := setcd.NewEtcdInstance(m.etcdAddr)
if err != nil {
return err
}
initCh := make(chan error)
var initOnce sync.Once
etcdInstance.Watch(ctx, "/roc/db/route", func(response *client.Response) {
parser, er := NewParser([]byte(response.Node.Value))
if er != nil {
slog.Errorf(ctx, "%s init db parser err: ", fun, er.Error())
} else {
slog.Infof(ctx, "succeed to init new parser")
if oldParser := m.getParser(ctx); oldParser != nil {
dbConfigChange := compareParsers(*oldParser, *parser)
slog.Infof(ctx, "parser changes: %+v", dbConfigChange)
m.setParser(ctx, parser)
dbChangeChan <- dbConfigChange
} else {
m.setParser(ctx, parser)
}
}
initOnce.Do(func() {
initCh <- er
})
})
// 做一次同步,等parser初始化完成
err = <-initCh
close(initCh)
return err
}
func (m *EtcdConfig) getParser(ctx context.Context) *Parser {
m.parserMu.RLock()
defer m.parserMu.RUnlock()
return m.parser
}
func (m *EtcdConfig) setParser(ctx context.Context, parser *Parser) {
m.parserMu.Lock()
defer m.parserMu.Unlock()
m.parser = parser
}
func (m *EtcdConfig) GetConfig(ctx context.Context, instance string) *Config {
group := scontext.GetControlRouteGroupWithDefault(ctx, DefaultGroup)
parser := m.getParser(ctx)
info := parser.GetConfig(instance, group)
return &Config{
DBType: info.DBType,
DBAddr: info.DBAddr,
DBName: info.DBName,
UserName: info.UserName,
PassWord: info.PassWord,
TimeOut: 3 * time.Second,
}
}
func (m *EtcdConfig) GetConfigByGroup(ctx context.Context, instance, group string) *Config {
parser := m.getParser(ctx)
info := parser.GetConfig(instance, group)
return &Config{
DBType: info.DBType,
DBAddr: info.DBAddr,
DBName: info.DBName,
UserName: info.UserName,
PassWord: info.PassWord,
TimeOut: 3 * time.Second,
}
}
func (m *EtcdConfig) GetInstance(ctx context.Context, cluster, table string) (instance string) {
parser := m.getParser(ctx)
return parser.GetInstance(cluster, table)
}
func (m *EtcdConfig) GetGroups(ctx context.Context) []string {
var groups []string
parser := m.getParser(ctx)
for group, _ := range parser.dbIns {
groups = append(groups, group)
}
return groups
}