Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 40 additions & 12 deletions sentry/src/main/java/io/sentry/Breadcrumb.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public final class Breadcrumb implements JsonUnknown, JsonSerializable, Comparab
/** The type of breadcrumb. */
private @Nullable String type;

private static final @NotNull Map<String, @NotNull Object> EMPTY_DATA = Collections.emptyMap();

/** Data associated with this breadcrumb. */
private @NotNull Map<String, @NotNull Object> data = new ConcurrentHashMap<>();
private volatile @NotNull Map<String, @NotNull Object> data = EMPTY_DATA;

/** Dotted strings that indicate what the crumb is or where it comes from. */
private @Nullable String category;
Expand Down Expand Up @@ -78,9 +80,11 @@ public Breadcrumb(final long timestamp) {
this.type = breadcrumb.type;
this.category = breadcrumb.category;
this.origin = breadcrumb.origin;
final Map<String, Object> dataClone = CollectionUtils.newConcurrentHashMap(breadcrumb.data);
if (dataClone != null) {
this.data = dataClone;
if (!breadcrumb.data.isEmpty()) {
final Map<String, Object> dataClone = CollectionUtils.newConcurrentHashMap(breadcrumb.data);
if (dataClone != null) {
this.data = dataClone;
}
Comment thread
cursor[bot] marked this conversation as resolved.
}
this.unknown = CollectionUtils.newConcurrentHashMap(breadcrumb.unknown);
this.level = breadcrumb.level;
Expand All @@ -100,7 +104,7 @@ public static Breadcrumb fromMap(
@NotNull Date timestamp = DateUtils.getCurrentDateTime();
String message = null;
String type = null;
@NotNull Map<String, Object> data = new ConcurrentHashMap<>();
Map<String, Object> data = null;
String category = null;
String origin = null;
SentryLevel level = null;
Expand Down Expand Up @@ -129,6 +133,9 @@ public static Breadcrumb fromMap(
if (untypedData != null) {
for (Map.Entry<Object, Object> dataEntry : untypedData.entrySet()) {
if (dataEntry.getKey() instanceof String && dataEntry.getValue() != null) {
if (data == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be called from multiple threads and if so, is this thread safe?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data is a local variable here so I guess in this case it should be good

data = new ConcurrentHashMap<>();
}
data.put((String) dataEntry.getKey(), dataEntry.getValue());
} else {
options
Expand Down Expand Up @@ -166,7 +173,9 @@ public static Breadcrumb fromMap(
final Breadcrumb breadcrumb = new Breadcrumb(timestamp);
breadcrumb.message = message;
breadcrumb.type = type;
breadcrumb.data = data;
if (data != null) {
breadcrumb.data = data;
}
breadcrumb.category = category;
breadcrumb.origin = origin;
breadcrumb.level = level;
Expand Down Expand Up @@ -494,7 +503,7 @@ public static Breadcrumb fromMap(
breadcrumb.setData("view.tag", viewTag);
}
for (final Map.Entry<String, Object> entry : additionalData.entrySet()) {
breadcrumb.getData().put(entry.getKey(), entry.getValue());
breadcrumb.setData(entry.getKey(), entry.getValue());
}
breadcrumb.setLevel(SentryLevel.INFO);
return breadcrumb;
Expand Down Expand Up @@ -598,6 +607,20 @@ public void setType(@Nullable String type) {
this.type = type;
}

private @NotNull Map<String, @NotNull Object> getOrCreateData() {
Map<String, @NotNull Object> currentData = data;
if (currentData == EMPTY_DATA) {
synchronized (this) {
currentData = data;
if (currentData == EMPTY_DATA) {
currentData = new ConcurrentHashMap<>();
data = currentData;
}
}
}
return currentData;
}

/**
* Returns the data map
*
Expand Down Expand Up @@ -636,7 +659,7 @@ public void setData(@Nullable String key, @Nullable Object value) {
if (value == null) {
removeData(key);
} else {
data.put(key, value);
getOrCreateData().put(key, value);
}
}

Expand All @@ -649,7 +672,10 @@ public void removeData(@Nullable String key) {
if (key == null) {
return;
}
data.remove(key);
final Map<String, @NotNull Object> currentData = data;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is confusing to me, not sure it is worth the perf optimization, if any.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents UnsupportedOperationException when trying to remove from the immutable map.

if (currentData != EMPTY_DATA) {
currentData.remove(key);
}
}

/**
Comment thread
sentry[bot] marked this conversation as resolved.
Expand Down Expand Up @@ -859,7 +885,7 @@ public static final class Deserializer implements JsonDeserializer<Breadcrumb> {
@NotNull Date timestamp = DateUtils.getCurrentDateTime();
String message = null;
String type = null;
@NotNull Map<String, Object> data = new ConcurrentHashMap<>();
Map<String, Object> data = null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change and the corresponding change on 913 seem safe.

String category = null;
String origin = null;
SentryLevel level = null;
Expand All @@ -884,7 +910,7 @@ public static final class Deserializer implements JsonDeserializer<Breadcrumb> {
Map<String, Object> deserializedData =
CollectionUtils.newConcurrentHashMap(
(Map<String, Object>) reader.nextObjectOrNull());
if (deserializedData != null) {
if (deserializedData != null && !deserializedData.isEmpty()) {
data = deserializedData;
}
break;
Expand Down Expand Up @@ -913,7 +939,9 @@ public static final class Deserializer implements JsonDeserializer<Breadcrumb> {
Breadcrumb breadcrumb = new Breadcrumb(timestamp);
breadcrumb.message = message;
breadcrumb.type = type;
breadcrumb.data = data;
if (data != null) {
breadcrumb.data = data;
}
breadcrumb.category = category;
breadcrumb.origin = origin;
breadcrumb.level = level;
Expand Down
27 changes: 27 additions & 0 deletions sentry/src/test/java/io/sentry/BreadcrumbTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.sentry

import java.util.Date
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
Expand Down Expand Up @@ -329,6 +332,30 @@ class BreadcrumbTest {
breadcrumb.removeData(null)
}

@Test
fun `concurrent first writes keep all data entries`() {
val breadcrumb = Breadcrumb()
val count = 32
val executor = Executors.newFixedThreadPool(count)
val start = CountDownLatch(1)
val futures =
(0 until count).map { index ->
executor.submit {
start.await()
breadcrumb.setData("key-$index", index)
}
}

start.countDown()
futures.forEach { it.get(5, TimeUnit.SECONDS) }
executor.shutdown()

assertEquals(count, breadcrumb.data.size)
for (index in 0 until count) {
assertEquals(index, breadcrumb.data["key-$index"])
}
}

class TestKey(val id: Long) {
override fun toString(): String = id.toString()
}
Expand Down
Loading