Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jetbrains.annotations.ApiStatus;
Expand All @@ -19,7 +19,14 @@ public final class DefaultCompositePerformanceCollector implements CompositePerf
private static final long TRANSACTION_COLLECTION_INTERVAL_MILLIS = 100;
private static final long TRANSACTION_COLLECTION_TIMEOUT_MILLIS = 30000;
private final @NotNull AutoClosableReentrantLock timerLock = new AutoClosableReentrantLock();
private volatile @Nullable Timer timer = null;
private volatile @Nullable Future<?> collectFuture = null;

/**
* Incremented on close() so an in-flight collect run doesn't reschedule itself after its chain
* was cancelled. Guarded by timerLock.
*/
private long generation = 0;

private final @NotNull Map<String, CompositeData> compositeDataMap = new ConcurrentHashMap<>();
private final @NotNull List<IPerformanceSnapshotCollector> snapshotCollectors;
private final @NotNull List<IPerformanceContinuousCollector> continuousCollectors;
Expand Down Expand Up @@ -71,6 +78,7 @@ public void start(final @NotNull ITransaction transaction) {
}

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void start(final @NotNull String id) {
if (hasNoCollectors) {
options
Expand All @@ -88,61 +96,75 @@ public void start(final @NotNull String id) {
}
if (!isStarted.getAndSet(true)) {
try (final @NotNull ISentryLifecycleToken ignored = timerLock.acquire()) {
if (timer == null) {
timer = new Timer(true);
}
// We schedule the timer to call setup() on collectors immediately in the background.
timer.schedule(
new TimerTask() {
@Override
public void run() {
for (IPerformanceSnapshotCollector collector : snapshotCollectors) {
collector.setup();
}
}
},
0L);
// We schedule the timer to start after a delay, so we let some time pass between setup()
// and collect() calls.
final long currentGeneration = generation;
// We schedule the executor to call setup() on collectors immediately in the background.
options
.getTimerExecutorService()
.schedule(
() -> {
for (IPerformanceSnapshotCollector collector : snapshotCollectors) {
collector.setup();
}
},
0L);
// We schedule the collection to start after a delay, so we let some time pass between
// setup() and collect() calls.
// This way ICollectors that collect average stats based on time intervals, like
// AndroidCpuCollector, can have an actual time interval to evaluate.
final @NotNull List<ITransaction> timedOutTransactions = new ArrayList<>();
TimerTask timerTask =
new TimerTask() {
@Override
public void run() {
timedOutTransactions.clear();

final @NotNull PerformanceCollectionData tempData =
new PerformanceCollectionData(options.getDateProvider().now().nanoTimestamp());

// Enrich tempData using collectors
for (IPerformanceSnapshotCollector collector : snapshotCollectors) {
collector.collect(tempData);
}

// Add the enriched tempData to all transactions/profiles/objects that collect data.
// Then Check if that object timed out.
for (CompositeData data : compositeDataMap.values()) {
if (data.addDataAndCheckTimeout(tempData, tempData.getNanoTimestamp())) {
// timed out
if (data.transaction != null) {
timedOutTransactions.add(data.transaction);
}
}
}
// Stop timed out transactions outside compositeDataMap loop, as stop() modifies the
// map
for (final @NotNull ITransaction t : timedOutTransactions) {
stop(t);
}
}
};
timer.schedule(
timerTask,
TRANSACTION_COLLECTION_INTERVAL_MILLIS,
TRANSACTION_COLLECTION_INTERVAL_MILLIS);
collectFuture =
options
.getTimerExecutorService()
.schedule(
() -> collectAndReschedule(currentGeneration),
TRANSACTION_COLLECTION_INTERVAL_MILLIS);
} catch (RejectedExecutionException e) {
options
.getLogger()
.log(SentryLevel.WARNING, "Failed to schedule performance collection.", e);
}
}
}

private void collectAndReschedule(final long scheduledGeneration) {
final @NotNull PerformanceCollectionData tempData =
new PerformanceCollectionData(options.getDateProvider().now().nanoTimestamp());

// Enrich tempData using collectors
for (IPerformanceSnapshotCollector collector : snapshotCollectors) {
collector.collect(tempData);
}

// Add the enriched tempData to all transactions/profiles/objects that collect data.
// Then Check if that object timed out.
final @NotNull List<ITransaction> timedOutTransactions = new ArrayList<>();
for (CompositeData data : compositeDataMap.values()) {
if (data.addDataAndCheckTimeout(tempData, tempData.getNanoTimestamp())) {
// timed out
if (data.transaction != null) {
timedOutTransactions.add(data.transaction);
}
}
}
// Stop timed out transactions outside compositeDataMap loop, as stop() modifies the map
for (final @NotNull ITransaction t : timedOutTransactions) {
stop(t);
}

try (final @NotNull ISentryLifecycleToken ignored = timerLock.acquire()) {
// stopping a timed out transaction above may have closed this collector; only reschedule if
// this run still belongs to the current collection chain
if (scheduledGeneration == generation) {
collectFuture =
options
.getTimerExecutorService()
.schedule(
() -> collectAndReschedule(scheduledGeneration),
TRANSACTION_COLLECTION_INTERVAL_MILLIS);
}
} catch (RejectedExecutionException e) {
options
.getLogger()
.log(SentryLevel.WARNING, "Failed to reschedule performance collection.", e);
}
}

Expand Down Expand Up @@ -201,9 +223,10 @@ public void close() {
}
if (isStarted.getAndSet(false)) {
try (final @NotNull ISentryLifecycleToken ignored = timerLock.acquire()) {
if (timer != null) {
timer.cancel();
timer = null;
generation++;
if (collectFuture != null) {
collectFuture.cancel(false);
collectFuture = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package io.sentry

import io.sentry.test.getCtor
import io.sentry.test.getProperty
import io.sentry.test.injectForField
import io.sentry.util.thread.ThreadChecker
import java.util.Timer
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -34,7 +34,8 @@ class DefaultCompositePerformanceCollectorTest {
val id1 = "id1"
val scopes: IScopes = mock()
val options = SentryOptions()
var mockTimer: Timer? = null
// a real executor so scheduled collection tasks actually run, recording schedule delays
val executorService = RecordingExecutorService(SentryExecutorService(options))

val mockCpuCollector: IPerformanceSnapshotCollector =
object : IPerformanceSnapshotCollector {
Expand All @@ -47,6 +48,7 @@ class DefaultCompositePerformanceCollectorTest {

init {
whenever(scopes.options).thenReturn(options)
options.setTimerExecutorService(executorService)
}

fun getSut(
Expand All @@ -65,14 +67,23 @@ class DefaultCompositePerformanceCollectorTest {
optionsConfiguration.configure(options)
transaction1 = SentryTracer(TransactionContext("", ""), scopes)
transaction2 = SentryTracer(TransactionContext("", ""), scopes)
val collector = DefaultCompositePerformanceCollector(options)
val timer: Timer = collector.getProperty("timer") ?: Timer(true)
mockTimer = spy(timer)
collector.injectForField("timer", mockTimer)
return collector
return DefaultCompositePerformanceCollector(options)
}
}

private class RecordingExecutorService(private val delegate: ISentryExecutorService) :
ISentryExecutorService by delegate {
val scheduledDelays = CopyOnWriteArrayList<Long>()

override fun schedule(runnable: Runnable, delayMillis: Long): Future<*> {
scheduledDelays.add(delayMillis)
return delegate.schedule(runnable, delayMillis)
}
}

private fun CompositePerformanceCollector.collectFuture(): Future<*>? =
getProperty<Future<*>?>("collectFuture")

@Test
fun `when null param is provided, invalid argument is thrown`() {
val ctor = className.getCtor(ctorTypes)
Expand All @@ -85,7 +96,7 @@ class DefaultCompositePerformanceCollectorTest {
val collector = fixture.getSut(null, null)
assertTrue(fixture.options.performanceCollectors.isEmpty())
collector.start(fixture.transaction1)
verify(fixture.mockTimer, never())!!.schedule(any(), any<Long>(), any())
assertTrue(fixture.executorService.scheduledDelays.isEmpty())
}

@Test
Expand All @@ -100,52 +111,52 @@ class DefaultCompositePerformanceCollectorTest {
}

@Test
fun `when start, timer is scheduled every 100 milliseconds`() {
fun `when start, collection is scheduled every 100 milliseconds`() {
val collector = fixture.getSut()
collector.start(fixture.transaction1)
verify(fixture.mockTimer)!!.schedule(any(), any<Long>(), eq(100))
assertTrue(fixture.executorService.scheduledDelays.contains(100L))
}

@Test
fun `when start with a string, timer is scheduled every 100 milliseconds`() {
fun `when start with a string, collection is scheduled every 100 milliseconds`() {
val collector = fixture.getSut()
collector.start(fixture.id1)
verify(fixture.mockTimer)!!.schedule(any(), any<Long>(), eq(100))
assertTrue(fixture.executorService.scheduledDelays.contains(100L))
}

@Test
fun `when stop, timer is stopped`() {
fun `when stop, collection is stopped`() {
val collector = fixture.getSut()
collector.start(fixture.transaction1)
assertNotNull(collector.collectFuture())
collector.stop(fixture.transaction1)
verify(fixture.mockTimer)!!.schedule(any(), any<Long>(), eq(100))
verify(fixture.mockTimer)!!.cancel()
assertNull(collector.collectFuture())
}

@Test
fun `when stop with a string, timer is stopped`() {
fun `when stop with a string, collection is stopped`() {
val collector = fixture.getSut()
collector.start(fixture.id1)
assertNotNull(collector.collectFuture())
collector.stop(fixture.id1)
verify(fixture.mockTimer)!!.schedule(any(), any<Long>(), eq(100))
verify(fixture.mockTimer)!!.cancel()
assertNull(collector.collectFuture())
}

@Test
fun `stopping a not collected transaction return null`() {
val collector = fixture.getSut()
val data = collector.stop(fixture.transaction1)
verify(fixture.mockTimer, never())!!.schedule(any(), any<Long>(), eq(100))
verify(fixture.mockTimer, never())!!.cancel()
assertTrue(fixture.executorService.scheduledDelays.isEmpty())
assertNull(collector.collectFuture())
assertNull(data)
}

@Test
fun `stopping a not collected id return null`() {
val collector = fixture.getSut()
val data = collector.stop(fixture.id1)
verify(fixture.mockTimer, never())!!.schedule(any(), any<Long>(), eq(100))
verify(fixture.mockTimer, never())!!.cancel()
assertTrue(fixture.executorService.scheduledDelays.isEmpty())
assertNull(collector.collectFuture())
assertNull(data)
}

Expand All @@ -159,16 +170,16 @@ class DefaultCompositePerformanceCollectorTest {
Thread.sleep(300)

val data1 = collector.stop(fixture.transaction1)
// There is still a transaction and an id running: the timer shouldn't stop now
verify(fixture.mockTimer, never())!!.cancel()
// There is still a transaction and an id running: the collection shouldn't stop now
assertNotNull(collector.collectFuture())

val data2 = collector.stop(fixture.transaction2)
// There is still an id running: the timer shouldn't stop now
verify(fixture.mockTimer, never())!!.cancel()
// There is still an id running: the collection shouldn't stop now
assertNotNull(collector.collectFuture())

val data3 = collector.stop(fixture.id1)
// There are no more transactions or ids running: the time should stop now
verify(fixture.mockTimer)!!.cancel()
// There are no more transactions or ids running: the collection should stop now
assertNull(collector.collectFuture())

assertNotNull(data1)
assertNotNull(data2)
Expand Down Expand Up @@ -196,14 +207,14 @@ class DefaultCompositePerformanceCollectorTest {
it.addPerformanceCollector(mockCollector)
}
collector.start(fixture.transaction1)
verify(fixture.mockTimer, never())!!.cancel()
assertNotNull(collector.collectFuture())

// Let's sleep to make the collector get values
Thread.sleep(300)

// When the collector gets the values, it checks the current date, set 31 seconds after the
// begin. This means it should stop itself
verify(fixture.mockTimer)!!.cancel()
assertNull(collector.collectFuture())

// When the collector times out, the data collection for spans is stopped, too
verify(mockCollector).onSpanFinished(eq(fixture.transaction1))
Expand All @@ -224,14 +235,14 @@ class DefaultCompositePerformanceCollectorTest {
whenever(mockDateProvider.now()).thenReturn(dates[0], dates[0], dates[0], dates[1])
val collector = fixture.getSut { it.dateProvider = mockDateProvider }
collector.start(fixture.transaction1)
verify(fixture.mockTimer, never())!!.cancel()
assertNotNull(collector.collectFuture())

// Let's sleep to make the collector get values
Thread.sleep(300)

// When the collector gets the values, it checks the current date, set 30 seconds after the
// begin. This means it should continue without being cancelled
verify(fixture.mockTimer, never())!!.cancel()
assertNotNull(collector.collectFuture())

// Data is deleted after the collector times out
val data1 = collector.stop(fixture.transaction1)
Expand Down Expand Up @@ -298,14 +309,14 @@ class DefaultCompositePerformanceCollectorTest {
}

@Test
fun `when close, timer is stopped and data is cleared`() {
fun `when close, collection is stopped and data is cleared`() {
val collector = fixture.getSut()
collector.start(fixture.transaction1)
collector.close()

// Timer was canceled
verify(fixture.mockTimer)!!.schedule(any(), any<Long>(), eq(100))
verify(fixture.mockTimer)!!.cancel()
// Collection was canceled
assertTrue(fixture.executorService.scheduledDelays.contains(100L))
assertNull(collector.collectFuture())

// Data was cleared
assertNull(collector.stop(fixture.transaction1))
Expand Down
Loading