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 potential deadlock of exited thread delay queue. #373

Merged
merged 4 commits into from
Nov 30, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
### Bug fixes
-
-
-
- Fix potential deadlock of exited thread delay queue. ([#373](https://github.com/CloudDectective-Harmonycloud/kindling/pull/373))
- Fix the bug that cpuEvent cache size continuously increases even if trace profiling is not enabled.([#362](https://github.com/CloudDectective-Harmonycloud/kindling/pull/362))
- Fix the bug that duplicate CPU events are indexed into Elasticsearch. ([#359](https://github.com/KindlingProject/kindling/pull/359))
- Implement the delay queue for exited thread, so as to avoid losing the data in the period before the thread exits. ([#365](https://github.com/CloudDectective-Harmonycloud/kindling/pull/365))
Expand Down
12 changes: 5 additions & 7 deletions collector/pkg/component/analyzer/cpuanalyzer/cpu_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,10 @@ func (ca *CpuAnalyzer) PutEventToSegments(pid uint32, tid uint32, threadName str
}

func (ca *CpuAnalyzer) trimExitedThread(pid uint32, tid uint32) {
ca.lock.Lock()
defer ca.lock.Unlock()
tidEventsMap := ca.cpuPidEvents[pid]
if tidEventsMap == nil {
return
}
ca.tidExpiredQueue.queueMutex.Lock()
defer ca.tidExpiredQueue.queueMutex.Unlock()
ca.telemetry.Logger.Debugf("Receive a procexit pid=%d, tid=%d, which will be deleted from map after 10 seconds. ", pid, tid)
ca.AddTidToDeleteCache(time.Now(), pid, tid)

cacheElem := deleteTid{pid: pid, tid: tid, exitTime: time.Now()}
ca.tidExpiredQueue.Push(cacheElem)
}
36 changes: 13 additions & 23 deletions collector/pkg/component/analyzer/cpuanalyzer/delete_tid.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ func (dq *tidDeleteQueue) Pop() {
}
}

//Add procexit tid
func (ca *CpuAnalyzer) AddTidToDeleteCache(curTime time.Time, pid uint32, tid uint32) {
cacheElem := deleteTid{pid: pid, tid: tid, exitTime: curTime}
ca.tidExpiredQueue.queueMutex.Lock()
defer ca.tidExpiredQueue.queueMutex.Unlock()
ca.tidExpiredQueue.Push(cacheElem)
}

func (ca *CpuAnalyzer) DeleteTid(tidEventsMap map[uint32]*TimeSegments, tid uint32) {
ca.lock.Lock()
defer ca.lock.Unlock()
delete(tidEventsMap, tid)
}

func (ca *CpuAnalyzer) TidDelete(interval time.Duration, expiredDuration time.Duration) {
for {
select {
Expand All @@ -64,19 +50,23 @@ func (ca *CpuAnalyzer) TidDelete(interval time.Duration, expiredDuration time.Du
if elem == nil {
break
}
if elem.exitTime.Add(expiredDuration).Before(now) {
if elem.exitTime.Add(expiredDuration).After(now) {
break
}
//Delete expired threads (current_time >= thread_exit_time + interval_time).
func() {
ca.lock.Lock()
defer ca.lock.Unlock()
tidEventsMap := ca.cpuPidEvents[elem.pid]
if tidEventsMap == nil {
ca.tidExpiredQueue.Pop()
continue
} else {
ca.telemetry.Logger.Debugf("Delete expired thread... pid=%d, tid=%d", elem.pid, elem.tid)
//fmt.Printf("Go Test: Delete expired thread... pid=%d, tid=%d\n", elem.pid, elem.tid)
delete(tidEventsMap, elem.tid)
ca.tidExpiredQueue.Pop()
}
ca.telemetry.Logger.Debugf("Delete expired thread... pid=%d, tid=%d", elem.pid, elem.tid)
//fmt.Printf("Go Test: Delete expired thread... pid=%d, tid=%d\n", elem.pid, elem.tid)
ca.DeleteTid(tidEventsMap, elem.tid)
ca.tidExpiredQueue.Pop()
} else {
break
}
}()
}
}()
}
Expand Down
13 changes: 4 additions & 9 deletions collector/pkg/component/analyzer/cpuanalyzer/delete_tid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,16 @@ func TestDeleteQueue(t *testing.T) {
//check tid which exist in queue but not in the map
if i%4 != 0 {
ca.PutEventToSegments(uint32(i), uint32(i)+5, "threadname"+strconv.Itoa(i+100), ev)
time.Sleep(timeDuration)
}

var queueLen int

func() {
cacheElem := deleteTid{uint32(i), uint32(i) + 5, curTime.Add(timeDuration)}
ca.trimExitedThread(uint32(i), uint32(i)+5)

ca.tidExpiredQueue.queueMutex.Lock()
defer ca.tidExpiredQueue.queueMutex.Unlock()
queueLen = len(ca.tidExpiredQueue.queue)

cacheElem := deleteTid{uint32(i), uint32(i) + 5, curTime.Add(timeDuration)}
ca.tidExpiredQueue.Push(cacheElem)
visit = append(visit, cacheElem)
if len(ca.tidExpiredQueue.queue) != queueLen+1 {
t.Errorf("the length of queue is not added, expection: %d but: %d\n", queueLen+1, len(ca.tidExpiredQueue.queue))
}
}()

t.Logf("pid=%d, tid=%d enter time=%s\n", uint32(i), uint32(i)+5, curTime.Format("2006-01-02 15:04:05.000"))
Expand Down