Skip to content

Commit

Permalink
feat: improves performance of recorder synchronization when there are…
Browse files Browse the repository at this point in the history
… a large number of process data
  • Loading branch information
ZhengYa-0110 committed Jan 16, 2025
1 parent fa445ec commit 263d80c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
10 changes: 10 additions & 0 deletions server/controller/recorder/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,3 +1471,13 @@ func (c *Cache) refreshVIP() {

c.AddVIPs(vips)
}

// RefreshVTaps refreshes vtap data for every regular domain synchronization
func (c *Cache) RefreshVTaps() {
var vtaps []*mysql.VTap
if err := c.metadata.DB.Find(&vtaps).Error; err != nil {
log.Error(dbQueryResourceFailed(ctrlrcommon.RESOURCE_TYPE_VTAP_EN, err))
return
}
c.ToolDataSet.RefreshVTaps(vtaps)
}
33 changes: 18 additions & 15 deletions server/controller/recorder/cache/tool/data_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ type DataSet struct {

podLcuuidToID map[string]int
podIDToLcuuid map[int]string

vtapIDToType map[int]int
vtapIDToLaunchServerID map[int]int
}

func NewDataSet(md *rcommon.Metadata) *DataSet {
Expand Down Expand Up @@ -192,6 +195,9 @@ func NewDataSet(md *rcommon.Metadata) *DataSet {

podLcuuidToID: make(map[string]int),
podIDToLcuuid: make(map[int]string),

vtapIDToType: make(map[int]int),
vtapIDToLaunchServerID: make(map[int]int),
}
}

Expand Down Expand Up @@ -1002,6 +1008,16 @@ func (t *DataSet) DeletePod(lcuuid string) {
log.Info(deleteFromToolMap(ctrlrcommon.RESOURCE_TYPE_POD_EN, lcuuid), t.metadata.LogPrefixes)
}

func (t *DataSet) RefreshVTaps(v []*metadbmodel.VTap) {
t.vtapIDToType = make(map[int]int)
t.vtapIDToLaunchServerID = make(map[int]int)
for _, item := range v {
t.vtapIDToType[item.ID] = item.Type
t.vtapIDToLaunchServerID[item.ID] = item.LaunchServerID
}
log.Infof("refreshed %s count: %d", ctrlrcommon.RESOURCE_TYPE_VTAP_EN, len(v))
}

func (t *DataSet) GetRegionIDByLcuuid(lcuuid string) (int, bool) {
id, exists := t.regionLcuuidToID[lcuuid]
if exists {
Expand Down Expand Up @@ -2351,13 +2367,6 @@ func (t *DataSet) GetPodIDByContainerIDWithoutLog(containerID string) (int, bool
if exists {
return podID, true
}

var pod *metadbmodel.Pod
result := t.metadata.DB.Where("container_ids like ?", "%"+containerID+"%").Find(&pod)
if result.RowsAffected == 1 {
t.AddPod(pod)
return t.containerIDToPodID[containerID], true
}
return 0, false
}

Expand All @@ -2367,14 +2376,8 @@ func (t *DataSet) GetProcessDeviceTypeAndID(containerID string, vtapID uint32) (
deviceType = common.VIF_DEVICE_TYPE_POD
deviceID = podID
} else {
var vtap *metadbmodel.VTap
if err := t.metadata.DB.Where("id = ?", vtapID).First(&vtap).Error; err != nil { // TODO @weiqiang 放入缓存
log.Error(err, t.metadata.LogPrefixes)
}
if vtap != nil {
deviceType = common.VTAP_TYPE_TO_DEVICE_TYPE[vtap.Type]
deviceID = vtap.LaunchServerID
}
deviceType = common.VTAP_TYPE_TO_DEVICE_TYPE[t.vtapIDToType[int(vtapID)]]
deviceID = t.vtapIDToLaunchServerID[int(vtapID)]
}
return
}
Expand Down
4 changes: 4 additions & 0 deletions server/controller/recorder/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func (d *domain) shouldRefresh(cloudData cloudmodel.Resource) error {
func (d *domain) refresh(cloudData cloudmodel.Resource) {
log.Info("domain refresh started", d.metadata.LogPrefixes)

// TODO refactor
// for process
d.cache.RefreshVTaps()

// 指定创建及更新操作的资源顺序
// 基本原则:无依赖资源优先;实时性需求高资源优先
listener := listener.NewWholeDomain(d.metadata.Domain.Lcuuid, d.cache, d.eventQueue)
Expand Down
4 changes: 4 additions & 0 deletions server/controller/recorder/sub_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ func (s *subDomain) tryRefresh(cloudData cloudmodel.SubDomainResource) error {
func (s *subDomain) refresh(cloudData cloudmodel.SubDomainResource) {
log.Info("sub_domain sync refresh started", s.metadata.LogPrefixes)

// TODO refactor
// for process
s.cache.RefreshVTaps()

listener := listener.NewWholeSubDomain(s.metadata.Domain.Lcuuid, s.metadata.SubDomain.Lcuuid, s.cache, s.eventQueue)
subDomainUpdatersInUpdateOrder := s.getUpdatersInOrder(cloudData)
s.executeUpdaters(subDomainUpdatersInUpdateOrder)
Expand Down
9 changes: 6 additions & 3 deletions server/controller/recorder/updater/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ func (p *Process) generateDBItemToAdd(cloudItem *cloudmodel.Process) (*metadbmod
var vmID int
if deviceType == common.VIF_DEVICE_TYPE_POD ||
deviceType == common.VIF_DEVICE_TYPE_POD_NODE {
id, ok := p.cache.ToolDataSet.GetVMIDByPodNodeID(podNodeID)
if ok {
vmID = id
if podNodeID != 0 {
id, ok := p.cache.ToolDataSet.GetVMIDByPodNodeID(podNodeID)
if ok {
vmID = id
}

}
} else {
vmID = deviceID
Expand Down

0 comments on commit 263d80c

Please sign in to comment.