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

fix(qrm): only one periodical handler get executed #186

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
}