forked from bfenetworks/bfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbal_table.go
296 lines (242 loc) · 7.52 KB
/
bal_table.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
// Copyright (c) 2019 Baidu, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// table for maintain backend cluster
package bfe_balance
import (
"fmt"
"strings"
"sync"
)
import (
"github.com/baidu/go-lib/log"
)
import (
"github.com/baidu/bfe/bfe_balance/backend"
"github.com/baidu/bfe/bfe_balance/bal_gslb"
"github.com/baidu/bfe/bfe_config/bfe_cluster_conf/cluster_table_conf"
"github.com/baidu/bfe/bfe_config/bfe_cluster_conf/gslb_conf"
"github.com/baidu/bfe/bfe_route"
)
// BalMap holds mappings from clusterName to BalanceGslb.
type BalMap map[string]*bal_gslb.BalanceGslb
type BalTable struct {
lock sync.RWMutex
balTable BalMap // from cluster to balancer
versions BalVersion
}
type BalVersion struct {
ClusterTableConfVer string // cluster table conf version
GslbConfTimeStamp string // timestamp of gslb-conf
GslbConfSrc string // which gslb-scheduler come from?
}
type BalTableState struct {
Balancers map[string]*bal_gslb.GslbState // state of cluster
BackendNum int // size of backendTable
}
func NewBalTable(checkConfFetcher backend.CheckConfFetcher) *BalTable {
t := new(BalTable)
t.balTable = make(BalMap)
backend.SetCheckConfFetcher(checkConfFetcher)
return t
}
func (t *BalTable) BalTableConfLoad(gslbConfFilename, clusterTableFilename string) (
gslb_conf.GslbConf, cluster_table_conf.ClusterTableConf, error) {
var gslbConf gslb_conf.GslbConf
var backendConf cluster_table_conf.ClusterTableConf
var err error
gslbConf, err = gslb_conf.GslbConfLoad(gslbConfFilename)
if err != nil {
log.Logger.Error("gslb_conf.GslbConfLoad err [%s]", err)
return gslbConf, backendConf, err
}
backendConf, err = cluster_table_conf.ClusterTableLoad(clusterTableFilename)
if err != nil {
log.Logger.Error("clusterBackendConfLoad err [%s]", err)
}
return gslbConf, backendConf, err
}
func (t *BalTable) Init(gslbConfFilename, clusterTableFilename string) error {
gslbConf, backendConf, err := t.BalTableConfLoad(gslbConfFilename, clusterTableFilename)
if err != nil {
log.Logger.Error("BalTable conf load err %s", err)
return err
}
// init gslb
if err := t.gslbInit(gslbConf); err != nil {
log.Logger.Error("clusterTable gslb init err [%s]", err)
return err
}
// init backend
if err := t.backendInit(backendConf); err != nil {
log.Logger.Error("clusterTable backend init err [%s]", err)
return err
}
log.Logger.Info("init bal table success")
return nil
}
func (t *BalTable) gslbInit(gslbConfs gslb_conf.GslbConf) error {
fails := make([]string, 0)
for clusterName, gslbConf := range *gslbConfs.Clusters {
bal := bal_gslb.NewBalanceGslb(clusterName)
err := bal.Init(gslbConf)
if err != nil {
log.Logger.Error("BalTable.gslbInit():err[%s] in bal_gslb.GslbInit() for %s",
err.Error(), clusterName)
fails = append(fails, clusterName)
continue
}
t.balTable[clusterName] = bal
}
// update versions
t.versions.GslbConfTimeStamp = *gslbConfs.Ts
t.versions.GslbConfSrc = *gslbConfs.Hostname
if len(fails) != 0 {
return fmt.Errorf("error in ClusterTable.gslbInit() for [%s]",
strings.Join(fails, ","))
}
return nil
}
func (t *BalTable) backendInit(backendConfs cluster_table_conf.ClusterTableConf) error {
fails := make([]string, 0)
for clusterName, bal := range t.balTable {
// get gslbConf
backendConf, ok := (*backendConfs.Config)[clusterName]
if !ok {
// external checking guarantee. should not come here in theory
log.Logger.Error("BalTable.backendInit():no backend conf for %s", clusterName)
fails = append(fails, clusterName)
continue
}
// initialize
err := bal.BackendInit(backendConf)
if err != nil {
log.Logger.Error("ClusterTable.backendInit():err[%s] in cluster.BackendInit() for %s",
err.Error(), clusterName)
fails = append(fails, clusterName)
continue
}
}
// update versions
t.versions.ClusterTableConfVer = *backendConfs.Version
if len(fails) != 0 {
return fmt.Errorf("error in ClusterTable.backendInit() for [%s]",
strings.Join(fails, ","))
}
return nil
}
// SetGslbBasic sets gslb basic conf (from server data conf) for BalTable.
//
// Note:
// - SetGslbBasic() is called after server reload gslb conf or server data conf
// - SetGslbBasic() should be concurrency safe
func (t *BalTable) SetGslbBasic(clusterTable *bfe_route.ClusterTable) {
t.lock.Lock()
defer t.lock.Unlock()
if clusterTable == nil {
return
}
for clusterName, bal := range t.balTable {
cluster, err := clusterTable.Lookup(clusterName)
if err != nil {
continue
}
bal.SetGslbBasic(*cluster.GslbBasic)
}
}
func (t *BalTable) BalTableReload(gslbConfs gslb_conf.GslbConf,
backendConfs cluster_table_conf.ClusterTableConf) error {
t.lock.Lock()
var fails []string
bmNew := make(BalMap)
for clusterName, gslbConf := range *gslbConfs.Clusters {
bal, ok := t.balTable[clusterName]
if !ok {
// new one balance
bal = bal_gslb.NewBalanceGslb(clusterName)
} else {
delete(t.balTable, clusterName)
}
// update balance
if err := bal.Reload(gslbConf); err != nil {
log.Logger.Error("BalTableReload():err[%s] in bal.Reload() for %s",
err.Error(), clusterName)
fails = append(fails, clusterName)
}
bmNew[clusterName] = bal
}
// remove bal not in configure file
for _, remainder := range t.balTable {
remainder.Release()
}
t.balTable = bmNew
for clusterName, bal := range t.balTable {
backendConf, ok1 := (*backendConfs.Config)[clusterName]
if !ok1 {
// never comes here
log.Logger.Error("BalTableReload():no backend conf for %s", clusterName)
fails = append(fails, clusterName)
continue
}
if err := bal.BackendReload(backendConf); err != nil {
log.Logger.Error("BalTableReload():err[%s] in bal.BackendReload() for %s",
err.Error(), clusterName)
fails = append(fails, clusterName)
}
}
// update versions
t.versions.ClusterTableConfVer = *backendConfs.Version
t.versions.GslbConfTimeStamp = *gslbConfs.Ts
t.versions.GslbConfSrc = *gslbConfs.Hostname
t.lock.Unlock()
if len(fails) != 0 {
return fmt.Errorf("error in BalTableReload() for [%s]", strings.Join(fails, ","))
}
return nil
}
func (t *BalTable) lookup(clusterName string) (*bal_gslb.BalanceGslb, error) {
bal, ok := t.balTable[clusterName]
if !ok {
return nil, fmt.Errorf("no bal found for %s", clusterName)
}
return bal, nil
}
// Lookup lookup BalanceGslb by cluster name.
func (t *BalTable) Lookup(clusterName string) (*bal_gslb.BalanceGslb, error) {
t.lock.Lock()
res, err := t.lookup(clusterName)
t.lock.Unlock()
return res, err
}
func NewBalTableState() *BalTableState {
state := new(BalTableState)
state.Balancers = make(map[string]*bal_gslb.GslbState)
return state
}
// GetState returnes state of BalTable.
func (t *BalTable) GetState() *BalTableState {
state := NewBalTableState()
t.lock.Lock()
// go through clusters
for name, bal := range t.balTable {
gs := bal_gslb.State(bal)
state.Balancers[name] = gs
state.BackendNum += gs.BackendNum
}
t.lock.Unlock()
return state
}
// GetVersions returnes versions of BalTable.
func (t *BalTable) GetVersions() BalVersion {
return t.versions
}