-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
344 lines (314 loc) · 12.5 KB
/
instance.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
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 logics
import (
"context"
"encoding/json"
"net/http"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/mapstr"
"configcenter/src/common/metadata"
"configcenter/src/common/util"
)
//var handEventDataChan chan chanItem // := make(chan chanItem, 10000)
func (lgc *Logics) HandleHostProcDataChange(ctx context.Context, eventData *metadata.EventInst) {
switch eventData.ObjType {
case metadata.EventObjTypeProcModule:
handEventDataChan <- chanItem{ctx: ctx, eventData: eventData, opFunc: lgc.eventProcInstByProcModule}
case metadata.EventObjTypeModuleTransfer:
handEventDataChan <- chanItem{ctx: ctx, eventData: eventData, opFunc: lgc.eventHostModuleChangeProcHostInstNum}
case common.BKInnerObjIDHost:
handEventDataChan <- chanItem{ctx: ctx, eventData: eventData, opFunc: lgc.eventProcInstByHostInfo}
case common.BKInnerObjIDProc:
handEventDataChan <- chanItem{ctx: ctx, eventData: eventData, opFunc: lgc.eventProcInstByProcess}
}
}
func (lgc *Logics) bgHandle() {
lgc.reshReshInitChan(lgc.ProcHostInst)
go func() {
defer lgc.bgHandle()
for {
select {
case item := <-handEventDataChan:
err := item.opFunc(item.ctx, item.eventData)
if nil != err {
blog.Error(err.Error())
}
case item := <-refreshHostInstModuleIDChan:
if nil == item {
break
}
err := lgc.HandleProcInstNumByModuleID(context.Background(), item.Header, item.AppID, item.ModuleID)
if nil != err {
blog.Errorf("HandleProcInstNumByModuleID error %s", err.Error())
}
}
}
}()
}
func (lgc *Logics) HandleProcInstNumByModuleID(ctx context.Context, header http.Header, appID, moduleID int64) error {
//defErr := lgc.CCErr.CreateDefaultCCErrorIf(util.GetLanguage(header))
maxInstID, procInst, err := lgc.getProcInstInfoByModuleID(ctx, appID, moduleID, header)
if nil != err {
blog.Errorf("handleInstanceNum getProcInstInfoByModuleID error %s", err.Error())
return err
}
var hostInfos map[int64]*metadata.GseHost
hostInfos, err = lgc.getHostByModuleID(ctx, header, moduleID)
if nil != err {
blog.Errorf("handleInstanceNum getHostByModuleID error %s", err.Error())
return err
}
setID, procIDs, err := lgc.getModuleBindProc(ctx, appID, moduleID, header)
if nil != err {
return err
}
instProc := make([]*metadata.ProcInstanceModel, 0)
procInfos, err := lgc.getProcInfoByID(ctx, procIDs, header)
isExistHostInst := make(map[string]metadata.ProcInstanceModel)
for procID, info := range procInfos {
for hostID, _ := range hostInfos {
procInstInfo, ok := procInst[getInlineProcInstKey(hostID, moduleID)]
hostInstID := uint64(0)
if !ok {
maxInstID++
hostInstID = maxInstID
isExistHostInst[getInlineProcInstKey(hostID, moduleID)] = procInstInfo
} else {
hostInstID = procInstInfo.HostInstanID
}
instProc = append(instProc, GetProcInstModel(appID, setID, moduleID, hostID, procID, info.FunID, info.ProcNum, hostInstID)...)
}
}
err = lgc.setProcInstDetallStatusUnregister(ctx, header, appID, moduleID)
if nil != err {
return err
}
err = lgc.handleProcInstNumDataHandle(ctx, header, appID, moduleID, instProc)
if nil != err {
return err
}
gseHost := make([]metadata.GseHost, 0)
for _, host := range hostInfos {
gseHost = append(gseHost, *host)
}
if 0 != len(gseHost) {
for _, info := range procInfos {
err = lgc.RegisterProcInstanceToGse(info.AppID, moduleID, info.ProcID, gseHost, info.ProcInfo, header)
if nil != err {
blog.Errorf("RegisterProcInstanceToGse error %s", err.Error())
continue
}
}
}
err = lgc.unregisterProcInstDetall(ctx, header, appID, moduleID)
if nil != err {
return err
}
return nil
}
func (lgc *Logics) eventHostModuleChangeProcHostInstNum(ctx context.Context, eventData *metadata.EventInst) error {
var header http.Header = make(http.Header, 0)
header.Set(common.BKHTTPOwnerID, eventData.OwnerID)
header.Set(common.BKHTTPHeaderUser, common.BKProcInstanceOpUser)
for _, hostInfos := range eventData.Data {
var data interface{}
if metadata.EventActionDelete == eventData.Action {
data = hostInfos.PreData
} else {
data = hostInfos.CurData
}
mapData, err := mapstr.NewFromInterface(data)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("productHostInstanceNum event data not map[string]interface{} item %v raw josn %s", hostInfos, string(byteData))
return err
}
appID, err := mapData.Int64(common.BKAppIDField)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("productHostInstanceNum event data appID not integer item %v raw josn %s", hostInfos, string(byteData))
return err
}
moduleID, err := mapData.Int64(common.BKModuleIDField)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("productHostInstanceNum event data moduleID not integer item %v raw josn %s", hostInfos, string(byteData))
return err
}
lgc.addEventRefreshModuleItem(appID, moduleID, header)
}
return nil
}
func (lgc *Logics) eventProcInstByProcModule(ctx context.Context, eventData *metadata.EventInst) error {
if metadata.EventTypeRelation != eventData.EventType {
return nil
}
var header http.Header = make(http.Header, 0)
header.Set(common.BKHTTPOwnerID, eventData.OwnerID)
header.Set(common.BKHTTPHeaderUser, common.BKProcInstanceOpUser)
for _, data := range eventData.Data {
var iData interface{}
if metadata.EventActionDelete == eventData.Action {
// delete process bind module relation, unregister process info
iData = data.PreData
} else {
// compare pre-change data with the current data and find the newly added data to register process info
iData = data.CurData
}
mapData, err := mapstr.NewFromInterface(iData)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByProcModule event data not map[string]interface{} item %v raw josn %s", data, string(byteData))
return err
}
appID, err := mapData.Int64(common.BKAppIDField)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByProcModule event data appID not integer item %v raw josn %s", data, string(byteData))
return err
}
moduleName, err := mapData.String(common.BKModuleNameField)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByProcModule event data appID not integer item %v raw josn %s", data, string(byteData))
return err
}
moduleID, err := lgc.HandleProcInstNumByModuleName(ctx, header, appID, moduleName)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByProcModule HandleProcInstNumByModuleName error %s item %v raw josn %s", err.Error(), data, string(byteData))
return err
}
lgc.addEventRefreshModuleItems(appID, moduleID, header)
}
return nil
}
func (lgc *Logics) eventProcInstByProcess(ctx context.Context, eventData *metadata.EventInst) error {
if metadata.EventActionUpdate != eventData.Action {
// create proccess not refresh process instance , because not bind module
return nil
}
var header http.Header = make(http.Header, 0)
header.Set(common.BKHTTPOwnerID, eventData.OwnerID)
header.Set(common.BKHTTPHeaderUser, common.BKProcInstanceOpUser)
for _, data := range eventData.Data {
mapData, err := mapstr.NewFromInterface(data.CurData)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByProcess event data not map[string]interface{} item %v raw josn %s", data, string(byteData))
return err
}
procID, err := mapData.Int64(common.BKProcessIDField)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByProcess process id not integer error %s item %v raw josn %s", err.Error(), data, string(byteData))
return err
}
appID, err := mapData.Int64(common.BKAppIDField)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByProcess application id not integer error %s item %v raw josn %s", err.Error(), data, string(byteData))
return err
}
mdouleID, err := lgc.getModuleIDByProcID(ctx, appID, procID, header)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByProcess get process bind module info by appID %d, procID %d error %s item %v raw josn %s", appID, procID, err.Error(), data, string(byteData))
return err
}
lgc.addEventRefreshModuleItems(appID, mdouleID, header)
}
return nil
}
func (lgc *Logics) eventProcInstByHostInfo(ctx context.Context, eventData *metadata.EventInst) error {
if metadata.EventActionUpdate == eventData.Action {
var header http.Header = make(http.Header, 0)
header.Set(common.BKHTTPOwnerID, eventData.OwnerID)
header.Set(common.BKHTTPHeaderUser, common.BKProcInstanceOpUser)
// host clouid id change
for _, data := range eventData.Data {
mapCurData, err := mapstr.NewFromInterface(data.CurData)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByHostInfo event current data not map[string]interface{} item %v raw josn %s", data, string(byteData))
return err
}
mapPreData, err := mapstr.NewFromInterface(data.PreData)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByHostInfo event pre-data not map[string]interface{} item %v raw josn %s", data, string(byteData))
return err
}
curData, err := mapCurData.Int64(common.BKCloudIDField)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByHostInfo event current data cloud id not int item %v raw josn %s", data, string(byteData))
return err
}
preData, err := mapPreData.Int64(common.BKCloudIDField)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByHostInfo event pre-data cloud id not int item %v raw josn %s", data, string(byteData))
return err
}
if curData != preData {
hostID, err := mapCurData.Int64(common.BKHostIDField)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByHostInfo event hostID not int item %v raw josn %s", data, string(byteData))
return err
}
hostModule, err := lgc.GetModuleIDByHostID(ctx, header, hostID)
if nil != err {
byteData, _ := json.Marshal(eventData)
blog.Errorf("eventProcInstByHostInfo event hostID %s get module err :%s item %v raw josn %s", hostID, err.Error(), data, string(byteData))
return err
}
for _, item := range hostModule {
lgc.addEventRefreshModuleItem(item.AppID, item.ModuleID, header)
}
}
}
}
return nil
}
func (lgc *Logics) HandleProcInstNumByModuleName(ctx context.Context, header http.Header, appID int64, moduleName string) ([]int64, error) {
moduleConds := new(metadata.SearchParams)
moduleConds.Condition = map[string]interface{}{
common.BKAppIDField: appID,
common.BKModuleNameField: moduleName,
}
defErr := lgc.CCErr.CreateDefaultCCErrorIf(util.GetLanguage(header))
moduleConds.Page = map[string]interface{}{"start": 0, "limit": common.BKNoLimit}
moduleInfos, err := lgc.CoreAPI.TopoServer().Instance().InstSearch(ctx, util.GetOwnerID(header), common.BKInnerObjIDModule, header, moduleConds)
if nil != err {
blog.Errorf("HandleProcInstNumByModuleName get module by name %s error %s", moduleName, err.Error())
return nil, defErr.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !moduleInfos.Result {
blog.Errorf("HandleProcInstNumByModuleName get module by name %s error %s ", moduleName, moduleInfos.ErrMsg)
return nil, defErr.New(moduleInfos.Code, moduleInfos.ErrMsg)
}
moduleIDs := make([]int64, 0)
for _, module := range moduleInfos.Data.Info {
moduleID, err := module.Int64(common.BKModuleIDField)
if nil != err {
byteData, _ := json.Marshal(module)
blog.Errorf("refreshProcInstByProcModule get module by name %s not module id error item %v ", moduleName, string(byteData))
return nil, err
}
moduleIDs = append(moduleIDs, moduleID)
}
return moduleIDs, nil
}