-
Notifications
You must be signed in to change notification settings - Fork 269
/
metadata.go
231 lines (200 loc) · 6.23 KB
/
metadata.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
/*
* Copyright (c) 2024 Yunshan Networks
*
* 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.
*/
package metadata
import (
"context"
"sync/atomic"
"time"
mapset "github.com/deckarep/golang-set"
"github.com/op/go-logging"
"gorm.io/gorm"
"github.com/deepflowio/deepflow/message/trident"
"github.com/deepflowio/deepflow/server/controller/trisolaris/config"
"github.com/deepflowio/deepflow/server/controller/trisolaris/pushmanager"
. "github.com/deepflowio/deepflow/server/controller/trisolaris/utils"
)
var log = logging.MustGetLogger("trisolaris/metadata")
type MetaData struct {
dbDataCache *atomic.Value // *DBDataCache 数据库缓存
platformDataOP *PlatformDataOP
groupDataOP *GroupDataOP
tapType *TapType
policyDataOP *PolicyDataOP
startTime int64
chPlatformData chan struct{}
chTapType chan struct{}
chPolicy chan struct{}
chGroup chan struct{}
config *config.Config
db *gorm.DB
ctx context.Context
cancel context.CancelFunc
ORGID
}
func NewMetaData(db *gorm.DB, cfg *config.Config, orgID int, pctx context.Context) *MetaData {
ctx, cancel := context.WithCancel(pctx)
dbDataCache := &atomic.Value{}
dbDataCache.Store(newDBDataCache(ORGID(orgID), cfg))
metaData := &MetaData{
dbDataCache: dbDataCache,
tapType: newTapType(db),
chPlatformData: make(chan struct{}, 1),
chTapType: make(chan struct{}, 1),
chPolicy: make(chan struct{}, 1),
chGroup: make(chan struct{}, 1),
config: cfg,
db: db,
ctx: ctx,
cancel: cancel,
ORGID: ORGID(orgID),
}
metaData.platformDataOP = newPlatformDataOP(db, metaData)
metaData.groupDataOP = newGroupDataOP(metaData)
metaData.policyDataOP = newPolicyDaTaOP(metaData, cfg.BillingMethod)
return metaData
}
func (m *MetaData) generateDbDataCache() {
dbDataCache := newDBDataCache(m.ORGID, m.config)
dbDataCache.GetDataCacheFromDB(m.db)
m.updateDBDataCache(dbDataCache)
}
func (m *MetaData) GetDBDataCache() *DBDataCache {
return m.dbDataCache.Load().(*DBDataCache)
}
func (m *MetaData) updateDBDataCache(d *DBDataCache) {
m.dbDataCache.Store(d)
}
func (m *MetaData) PutChPlatformData() {
select {
case m.chPlatformData <- struct{}{}:
default:
}
}
func (m *MetaData) PutChPolicy() {
select {
case m.chPolicy <- struct{}{}:
default:
}
}
func (m *MetaData) PutChGroup() {
select {
case m.chGroup <- struct{}{}:
default:
}
}
func (m *MetaData) PutChTapType() {
select {
case m.chTapType <- struct{}{}:
default:
}
}
func (m *MetaData) GetPlatformDataOP() *PlatformDataOP {
return m.platformDataOP
}
func (m *MetaData) GetPolicyDataOP() *PolicyDataOP {
return m.policyDataOP
}
func (m *MetaData) GetGroupDataOP() *GroupDataOP {
return m.groupDataOP
}
func (m *MetaData) GetTapTypes() []*trident.TapType {
return m.tapType.getTapTypes()
}
func (m *MetaData) GetTridentGroups() []byte {
return m.groupDataOP.getTridentGroups()
}
func (m *MetaData) GetTridentGroupsVersion() uint64 {
return m.groupDataOP.getTridentGroupsVersion()
}
func (m *MetaData) GetDropletGroups() []byte {
return m.groupDataOP.getDropletGroups()
}
func (m *MetaData) GetDropletGroupsVersion() uint64 {
return m.groupDataOP.getDropletGroupsVersion()
}
func (m *MetaData) GetDropletPolicyVersion() uint64 {
return m.policyDataOP.getDropletPolicyVersion()
}
func (m *MetaData) GetDropletPolicyStr() []byte {
return m.policyDataOP.getDropletPolicyStr()
}
func (m *MetaData) GetVTapPolicyVersion(vtapID int, functions mapset.Set) uint64 {
return m.policyDataOP.getVTapPolicyVersion(vtapID, functions)
}
func (m *MetaData) GetVTapPolicyString(vtapID int, functions mapset.Set) []byte {
return m.policyDataOP.getVTapPolicyString(vtapID, functions)
}
func (m *MetaData) GetPlatformVips() []string {
return m.config.PlatformVips
}
func (m *MetaData) GetStartTime() int64 {
return m.startTime
}
func (m *MetaData) InitData(startTime int64) {
m.startTime = startTime
m.generateDbDataCache()
m.platformDataOP.initData()
m.groupDataOP.SetStartTime(startTime)
m.groupDataOP.generateGroupData()
m.tapType.generateTapTypes()
m.policyDataOP.generatePolicyData()
}
func (m *MetaData) timedRefreshMetaData() {
interval := time.Duration(m.config.MetaDataRefreshInterval)
ticker := time.NewTicker(interval * time.Second).C
for {
select {
case <-ticker:
log.Info(m.Log("start generate metaData from timed"))
m.generateDbDataCache()
m.platformDataOP.GeneratePlatformData()
m.groupDataOP.generateGroupData()
m.policyDataOP.generatePolicyData()
m.tapType.generateTapTypes()
log.Info(m.Log("end generate metaData from timed"))
case <-m.chPlatformData:
log.Info(m.Log("start generate platform data from rpc"))
time.Sleep(time.Duration(m.config.PlatformDataRefreshDelayTime) * time.Second)
log.Info("processing generate platform data from rpc")
m.generateDbDataCache()
m.platformDataOP.GeneratePlatformData()
log.Info(m.Log("end generate platform data from rpc"))
case <-m.chPolicy:
log.Info(m.Log("start generate policy from rpc"))
m.generateDbDataCache()
m.groupDataOP.generateGroupData()
m.policyDataOP.generatePolicyData()
log.Info(m.Log("end generate policy from rpc"))
pushmanager.Broadcast(m.GetORGID())
case <-m.chGroup:
log.Info(m.Log("start generate group from rpc"))
m.generateDbDataCache()
m.groupDataOP.generateGroupData()
log.Info(m.Log("end generate group from rpc"))
pushmanager.Broadcast(m.GetORGID())
case <-m.chTapType:
log.Info(m.Log("start generate tap type from rpc"))
m.tapType.generateTapTypes()
log.Info(m.Log("end generate tap type from rpc"))
case <-m.ctx.Done():
log.Info(m.Log("exit generate metaData"))
return
}
}
}
func (m *MetaData) TimedRefreshMetaData() {
go m.timedRefreshMetaData()
}