Skip to content

Commit

Permalink
fix(qrm): only one periodical handler get executed (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzhhb authored Aug 4, 2023
1 parent bfe880c commit aab4bf9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
13 changes: 7 additions & 6 deletions pkg/agent/utilcomponent/periodicalhandler/periodical_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestPeriodicalHandlerManager(t *testing.T) {
}()

testNum := 5
testNum1 := 1

gName := "test_group"
hName := "test_handler"
Expand All @@ -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:
Expand All @@ -117,7 +131,7 @@ testLoop1:
testLoop2:
for {
lock.RLock()
if testNum == 10 {
if testNum == 10 && testNum1 == 2 {
lock.RUnlock()
break
}
Expand All @@ -132,7 +146,8 @@ testLoop2:
}

lock.RLock()
as.Equal(testNum, 10)
as.Equal(10, testNum)
as.Equal(2, testNum1)
lock.RUnlock()
cancel()
}

0 comments on commit aab4bf9

Please sign in to comment.