From aab4bf9a548f90fb029239b4c4b79642ad5786f2 Mon Sep 17 00:00:00 2001 From: zhou hongbin <131335757+zzzzhhb@users.noreply.github.com> Date: Fri, 4 Aug 2023 16:02:16 +0800 Subject: [PATCH] fix(qrm): only one periodical handler get executed (#186) --- .../periodicalhandler/periodical_handler.go | 13 ++++++------ .../periodical_handler_test.go | 21 ++++++++++++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/agent/utilcomponent/periodicalhandler/periodical_handler.go b/pkg/agent/utilcomponent/periodicalhandler/periodical_handler.go index 06484dbb1..cd984288a 100644 --- a/pkg/agent/utilcomponent/periodicalhandler/periodical_handler.go +++ b/pkg/agent/utilcomponent/periodicalhandler/periodical_handler.go @@ -80,7 +80,8 @@ func (phm *PeriodicalHandlerManager) Run(ctx context.Context) { defer handlerMtx.Unlock() for groupName, groupHandlerCtxs := range handlerCtxs { - for handlerName, handlerCtx := range groupHandlerCtxs { + for handlerName := range groupHandlerCtxs { + handlerCtx := groupHandlerCtxs[handlerName] if handlerCtx == nil { general.Warningf("nil handlerCtx") continue @@ -113,7 +114,7 @@ func (phm *PeriodicalHandlerManager) Run(ctx context.Context) { // the first key is the handlers group name // the second key is the handler name var handlerCtxs = make(map[string]map[string]*HandlerCtx) -var handlerMtx sync.RWMutex +var handlerMtx sync.Mutex func RegisterPeriodicalHandler(groupName, handlerName string, handler Handler, interval time.Duration) (err error) { if groupName == "" || handlerName == "" { @@ -170,8 +171,8 @@ func RegisterPeriodicalHandler(groupName, handlerName string, handler Handler, i } func ReadyToStartHandlersByGroup(groupName string) { - handlerMtx.RLock() - defer handlerMtx.RUnlock() + handlerMtx.Lock() + defer handlerMtx.Unlock() general.InfoS("called", "groupName", groupName) @@ -199,8 +200,8 @@ func ReadyToStartHandlersByGroup(groupName string) { } func StopHandlersByGroup(groupName string) { - handlerMtx.RLock() - defer handlerMtx.RUnlock() + handlerMtx.Lock() + defer handlerMtx.Unlock() general.InfoS("called", "groupName", groupName) diff --git a/pkg/agent/utilcomponent/periodicalhandler/periodical_handler_test.go b/pkg/agent/utilcomponent/periodicalhandler/periodical_handler_test.go index b190286c0..c6aab1fdf 100644 --- a/pkg/agent/utilcomponent/periodicalhandler/periodical_handler_test.go +++ b/pkg/agent/utilcomponent/periodicalhandler/periodical_handler_test.go @@ -81,6 +81,7 @@ func TestPeriodicalHandlerManager(t *testing.T) { }() testNum := 5 + testNum1 := 1 gName := "test_group" hName := "test_handler" @@ -95,12 +96,25 @@ func TestPeriodicalHandlerManager(t *testing.T) { }, time.Second) + hName1 := "test_handler1" + _ = RegisterPeriodicalHandler(gName, hName1, func(coreConf *config.Configuration, + extraConf interface{}, + dynamicConf *dynamicconfig.DynamicAgentConfiguration, + emitter metrics.MetricEmitter, + metaServer *metaserver.MetaServer) { + lock.Lock() + testNum1 = 2 + lock.Unlock() + }, + time.Second) + ticker := time.After(2 * time.Second) testLoop1: for { lock.RLock() - as.Equal(testNum, 5) + as.Equal(5, testNum) + as.Equal(1, testNum1) lock.RUnlock() select { case <-ticker: @@ -117,7 +131,7 @@ testLoop1: testLoop2: for { lock.RLock() - if testNum == 10 { + if testNum == 10 && testNum1 == 2 { lock.RUnlock() break } @@ -132,7 +146,8 @@ testLoop2: } lock.RLock() - as.Equal(testNum, 10) + as.Equal(10, testNum) + as.Equal(2, testNum1) lock.RUnlock() cancel() }