-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scheduled cleaning in javatraces, and add null pointer checking (#514)
Signed-off-by: Hui <anthonyhui@126.com> Signed-off-by: AnthonyHui <48346142+hwz779866221@users.noreply.github.com> Signed-off-by: anthonyhui <anthonyhui@126.com> Co-authored-by: Hui <anthonyhui@126.com>
- Loading branch information
1 parent
42e5ad5
commit c9d4476
Showing
6 changed files
with
173 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
collector/pkg/component/analyzer/cpuanalyzer/delete_javatrace.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cpuanalyzer | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type javaTraceDeleteQueue struct { | ||
queueMutex sync.Mutex | ||
queue []deleteVal | ||
} | ||
|
||
type deleteVal struct { | ||
key string | ||
enterTime time.Time | ||
} | ||
|
||
func newJavaTraceDeleteQueue() *javaTraceDeleteQueue { | ||
return &javaTraceDeleteQueue{queue: make([]deleteVal, 0)} | ||
} | ||
|
||
func (dq *javaTraceDeleteQueue) GetFront() *deleteVal { | ||
if len(dq.queue) > 0 { | ||
return &dq.queue[0] | ||
} | ||
return nil | ||
} | ||
|
||
func (dq *javaTraceDeleteQueue) Push(elem deleteVal) { | ||
dq.queue = append(dq.queue, elem) | ||
} | ||
|
||
func (dq *javaTraceDeleteQueue) Pop() { | ||
if len(dq.queue) > 0 { | ||
dq.queue = dq.queue[1:len(dq.queue)] | ||
} | ||
} | ||
|
||
func (ca *CpuAnalyzer) JavaTraceDelete(interval time.Duration, expiredDuration time.Duration) { | ||
for { | ||
select { | ||
case <-ca.stopProfileChan: | ||
return | ||
case <-time.After(interval): | ||
ca.telemetry.Logger.Debug("Start regular cleaning of javatrace...") | ||
now := time.Now() | ||
func() { | ||
ca.javaTraceExpiredQueue.queueMutex.Lock() | ||
defer ca.javaTraceExpiredQueue.queueMutex.Unlock() | ||
for { | ||
val := ca.javaTraceExpiredQueue.GetFront() | ||
if val == nil { | ||
break | ||
} | ||
if val.enterTime.Add(expiredDuration).After(now) { | ||
break | ||
} | ||
|
||
func() { | ||
ca.jtlock.Lock() | ||
defer ca.jtlock.Unlock() | ||
event := ca.javaTraces[val.key] | ||
if event == nil { | ||
ca.javaTraceExpiredQueue.Pop() | ||
} else { | ||
ca.telemetry.Logger.Debugf("Delete expired javatrace... pid=%s, tid=%s", event.PidString, event.TraceId) | ||
delete(ca.javaTraces, val.key) | ||
ca.javaTraceExpiredQueue.Pop() | ||
} | ||
}() | ||
} | ||
}() | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
collector/pkg/component/analyzer/cpuanalyzer/delete_javatrace_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cpuanalyzer | ||
|
||
import ( | ||
"math/rand" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Kindling-project/kindling/collector/pkg/component" | ||
) | ||
|
||
var cnt int | ||
|
||
func TestJavaTraceDeleteQueue(t *testing.T) { | ||
|
||
jt := make(map[string]*TransactionIdEvent, 100000) | ||
testTelemetry := component.NewTelemetryManager().GetGlobalTelemetryTools() | ||
mycfg := &Config{SegmentSize: 40, JavaTraceDeleteInterval: 15, JavaTraceExpirationTime: 10} | ||
ca = &CpuAnalyzer{javaTraces: jt, telemetry: testTelemetry, cfg: mycfg} | ||
ca.javaTraceExpiredQueue = newJavaTraceDeleteQueue() | ||
go ca.JavaTraceDelete(1*time.Second, 1*time.Second) | ||
|
||
for i := 0; i < 20; i++ { | ||
ev := new(TransactionIdEvent) | ||
ev.TraceId = strconv.Itoa(rand.Intn(10000)) | ||
ev.PidString = strconv.Itoa(rand.Intn(10000)) | ||
ev.IsEntry = 1 | ||
key := ev.TraceId + ev.PidString | ||
ca.javaTraces[key] = ev | ||
val := new(deleteVal) | ||
val.key = ev.TraceId + ev.PidString | ||
val.enterTime = time.Now() | ||
ca.javaTraceExpiredQueue.Push(*val) | ||
t.Logf("pid=%s, tid=%s enter time=%s\n", ev.PidString, ev.TraceId, val.enterTime.Format("2006-01-02 15:04:05.000")) | ||
cnt++ | ||
} | ||
time.Sleep(5 * time.Second) | ||
|
||
if len(ca.javaTraces) != 0 { | ||
t.Fatalf("The number of javatraces that entering and exiting the queue is not equal! "+ | ||
"enterCount=%d , len of javatrace is : %d\n", cnt, len(ca.javaTraces)) | ||
} else { | ||
t.Logf("All javatraces have cleaned normally. enterCount=%d\n", cnt) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters