Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(metaserver): refactor notify init in metaserver #596

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 22 additions & 100 deletions pkg/metaserver/agent/metric/metric_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,85 +97,21 @@ func (m *MetricsNotifierManagerImpl) notifySystem() {
m.RLock()
defer m.RUnlock()

for _, reg := range m.registeredNotifier[types.MetricsScopeNode] {
v, err := m.metricStore.GetNodeMetric(reg.Req.MetricName)
if err != nil {
continue
} else if v.Time == nil {
v.Time = &now
}

if reg.LastNotify.Equal(*v.Time) {
continue
} else {
reg.LastNotify = *v.Time
}

reg.Response <- types.NotifiedResponse{
Req: reg.Req,
MetricData: v,
}
}

for _, reg := range m.registeredNotifier[types.MetricsScopeDevice] {
v, err := m.metricStore.GetDeviceMetric(reg.Req.DeviceID, reg.Req.MetricName)
if err != nil {
continue
} else if v.Time == nil {
v.Time = &now
}

if reg.LastNotify.Equal(*v.Time) {
continue
} else {
reg.LastNotify = *v.Time
}

reg.Response <- types.NotifiedResponse{
Req: reg.Req,
MetricData: v,
}
}

for n, reg := range m.registeredNotifier[types.MetricsScopeNuma] {
v, err := m.metricStore.GetNumaMetric(reg.Req.NumaID, reg.Req.MetricName)
if err != nil {
continue
} else if v.Time == nil {
v.Time = &now
}

if m.registeredNotifier[types.MetricsScopeNuma][n].LastNotify.Equal(*v.Time) {
continue
} else {
reg.LastNotify = *v.Time
}

reg.Response <- types.NotifiedResponse{
Req: reg.Req,
MetricData: v,
}
}
m.processRegisteredNotifier(types.MetricsScopeNode, now, func(reg *types.NotifiedData) (utilmetric.MetricData, error) {
return m.metricStore.GetNodeMetric(reg.Req.MetricName)
})

for n, reg := range m.registeredNotifier[types.MetricsScopeCPU] {
v, err := m.metricStore.GetCPUMetric(reg.Req.CoreID, reg.Req.MetricName)
if err != nil {
continue
} else if v.Time == nil {
v.Time = &now
}
m.processRegisteredNotifier(types.MetricsScopeDevice, now, func(reg *types.NotifiedData) (utilmetric.MetricData, error) {
return m.metricStore.GetDeviceMetric(reg.Req.DeviceID, reg.Req.MetricName)
})

if reg.LastNotify.Equal(*v.Time) {
continue
} else {
m.registeredNotifier[types.MetricsScopeCPU][n].LastNotify = *v.Time
}
m.processRegisteredNotifier(types.MetricsScopeNuma, now, func(reg *types.NotifiedData) (utilmetric.MetricData, error) {
return m.metricStore.GetNumaMetric(reg.Req.NumaID, reg.Req.MetricName)
})

reg.Response <- types.NotifiedResponse{
Req: reg.Req,
MetricData: v,
}
}
m.processRegisteredNotifier(types.MetricsScopeCPU, now, func(reg *types.NotifiedData) (utilmetric.MetricData, error) {
return m.metricStore.GetCPUMetric(reg.Req.CoreID, reg.Req.MetricName)
})
}

// notifySystem notifies pod-related data
Expand All @@ -184,32 +120,18 @@ func (m *MetricsNotifierManagerImpl) notifyPods() {
m.RLock()
defer m.RUnlock()

for _, reg := range m.registeredNotifier[types.MetricsScopeContainer] {
v, err := m.metricStore.GetContainerMetric(reg.Req.PodUID, reg.Req.ContainerName, reg.Req.MetricName)
if err != nil {
continue
} else if v.Time == nil {
v.Time = &now
}

if reg.LastNotify.Equal(*v.Time) {
continue
} else {
reg.LastNotify = *v.Time
}

reg.Response <- types.NotifiedResponse{
Req: reg.Req,
MetricData: v,
}
}
m.processRegisteredNotifier(types.MetricsScopeContainer, now, func(reg *types.NotifiedData) (utilmetric.MetricData, error) {
return m.metricStore.GetContainerMetric(reg.Req.PodUID, reg.Req.ContainerName, reg.Req.MetricName)
})

for _, reg := range m.registeredNotifier[types.MetricsScopeContainerNUMA] {
if reg.Req.NumaNode == "" {
continue
}
m.processRegisteredNotifier(types.MetricsScopeContainerNUMA, now, func(reg *types.NotifiedData) (utilmetric.MetricData, error) {
return m.metricStore.GetContainerNumaMetric(reg.Req.PodUID, reg.Req.ContainerName, fmt.Sprintf("%v", reg.Req.NumaNode), reg.Req.MetricName)
})
}

v, err := m.metricStore.GetContainerNumaMetric(reg.Req.PodUID, reg.Req.ContainerName, fmt.Sprintf("%v", reg.Req.NumaNode), reg.Req.MetricName)
func (m *MetricsNotifierManagerImpl) processRegisteredNotifier(scope types.MetricsScope, now time.Time, getMetricFunc func(reg *types.NotifiedData) (utilmetric.MetricData, error)) {
for _, reg := range m.registeredNotifier[scope] {
v, err := getMetricFunc(reg)
if err != nil {
continue
} else if v.Time == nil {
Expand Down
Loading