Skip to content

Commit 7dbbf36

Browse files
committed
Make FlightRecorderApplicationStartup thread safe
Prior to this commit, in some cases application context startup steps could be created concurrently, which could cause issues with the current implementation tracking the parent/child relationship between steps. This commit ensures that the flight recorder implementation is using thread safe collection implementations for that. Fixes gh-26941
1 parent 5204d73 commit 7dbbf36

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

spring-core/src/main/java/org/springframework/core/metrics/jfr/FlightRecorderApplicationStartup.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,9 @@
1616

1717
package org.springframework.core.metrics.jfr;
1818

19-
import java.util.ArrayDeque;
2019
import java.util.Deque;
20+
import java.util.concurrent.ConcurrentLinkedDeque;
21+
import java.util.concurrent.atomic.AtomicLong;
2122

2223
import org.springframework.core.metrics.ApplicationStartup;
2324
import org.springframework.core.metrics.StartupStep;
@@ -37,24 +38,23 @@
3738
*/
3839
public class FlightRecorderApplicationStartup implements ApplicationStartup {
3940

40-
private long currentSequenceId;
41+
private final AtomicLong currentSequenceId = new AtomicLong(0);
4142

4243
private final Deque<Long> currentSteps;
4344

4445

4546
public FlightRecorderApplicationStartup() {
46-
this.currentSequenceId = 0;
47-
this.currentSteps = new ArrayDeque<>();
48-
this.currentSteps.offerFirst(0L);
47+
this.currentSteps = new ConcurrentLinkedDeque<>();
48+
this.currentSteps.offerFirst(this.currentSequenceId.get());
4949
}
5050

5151

5252
@Override
5353
public StartupStep start(String name) {
54-
FlightRecorderStartupStep step = new FlightRecorderStartupStep(++this.currentSequenceId, name,
55-
this.currentSteps.getFirst(), committedStep -> this.currentSteps.removeFirst());
56-
this.currentSteps.offerFirst(this.currentSequenceId);
57-
return step;
54+
long sequenceId = this.currentSequenceId.incrementAndGet();
55+
this.currentSteps.offerFirst(sequenceId);
56+
return new FlightRecorderStartupStep(sequenceId, name,
57+
this.currentSteps.getFirst(), committedStep -> this.currentSteps.removeFirstOccurrence(sequenceId));
5858
}
5959

6060
}

0 commit comments

Comments
 (0)