Skip to content

Commit

Permalink
Use LazyEvaluator for heavy SentryOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Oct 8, 2024
1 parent 5ed2cfe commit b6f9dd0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 52 deletions.
1 change: 1 addition & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -5698,6 +5698,7 @@ public final class io/sentry/util/JsonSerializationUtils {
public final class io/sentry/util/LazyEvaluator {
public fun <init> (Lio/sentry/util/LazyEvaluator$Evaluator;)V
public fun getValue ()Ljava/lang/Object;
public fun setValue (Ljava/lang/Object;)V
}

public abstract interface class io/sentry/util/LazyEvaluator$Evaluator {
Expand Down
55 changes: 16 additions & 39 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import io.sentry.transport.ITransportGate;
import io.sentry.transport.NoOpEnvelopeCache;
import io.sentry.transport.NoOpTransportGate;
import io.sentry.util.Objects;
import io.sentry.util.LazyEvaluator;
import io.sentry.util.Platform;
import io.sentry.util.SampleRateUtils;
import io.sentry.util.StringUtils;
Expand Down Expand Up @@ -118,15 +118,13 @@ public class SentryOptions {
/** minimum LogLevel to be used if debug is enabled */
private @NotNull SentryLevel diagnosticLevel = DEFAULT_DIAGNOSTIC_LEVEL;

/** Envelope reader interface */
private volatile @Nullable IEnvelopeReader envelopeReader;

private final @NotNull Object envelopeReaderLock = new Object();

/** Serializer interface to serialize/deserialize json events */
private volatile @Nullable ISerializer serializer;
private final @NotNull LazyEvaluator<ISerializer> serializer =
new LazyEvaluator<>(() -> new JsonSerializer(this));

private final @NotNull Object serializerLock = new Object();
/** Envelope reader interface */
private final @NotNull LazyEvaluator<IEnvelopeReader> envelopeReader =
new LazyEvaluator<>(() -> new EnvelopeReader(serializer.getValue()));

/** Max depth when serializing object graphs with reflection. * */
private int maxDepth = 100;
Expand Down Expand Up @@ -419,9 +417,9 @@ public class SentryOptions {
private boolean traceOptionsRequests = true;

/** Date provider to retrieve the current date from. */
@ApiStatus.Internal private volatile @Nullable SentryDateProvider dateProvider;

private final @NotNull Object dateProviderLock = new Object();
@ApiStatus.Internal
private final @NotNull LazyEvaluator<SentryDateProvider> dateProvider =
new LazyEvaluator<>(() -> new SentryAutoDateProvider());

private final @NotNull List<IPerformanceCollector> performanceCollectors = new ArrayList<>();

Expand Down Expand Up @@ -610,14 +608,7 @@ public void setDiagnosticLevel(@Nullable final SentryLevel diagnosticLevel) {
* @return the serializer
*/
public @NotNull ISerializer getSerializer() {
if (serializer == null) {
synchronized (serializerLock) {
if (serializer == null) {
serializer = new JsonSerializer(this);
}
}
}
return Objects.requireNonNull(serializer, "Serializer was null");
return serializer.getValue();
}

/**
Expand All @@ -626,7 +617,7 @@ public void setDiagnosticLevel(@Nullable final SentryLevel diagnosticLevel) {
* @param serializer the serializer
*/
public void setSerializer(@Nullable ISerializer serializer) {
this.serializer = serializer != null ? serializer : NoOpSerializer.getInstance();
this.serializer.setValue(serializer != null ? serializer : NoOpSerializer.getInstance());
}

/**
Expand All @@ -648,19 +639,12 @@ public void setMaxDepth(int maxDepth) {
}

public @NotNull IEnvelopeReader getEnvelopeReader() {
if (envelopeReader == null) {
synchronized (envelopeReaderLock) {
if (envelopeReader == null) {
envelopeReader = new EnvelopeReader(getSerializer());
}
}
}
return Objects.requireNonNull(envelopeReader, "EnvelopeReader was null");
return envelopeReader.getValue();
}

public void setEnvelopeReader(final @Nullable IEnvelopeReader envelopeReader) {
this.envelopeReader =
envelopeReader != null ? envelopeReader : NoOpEnvelopeReader.getInstance();
this.envelopeReader.setValue(
envelopeReader != null ? envelopeReader : NoOpEnvelopeReader.getInstance());
}

/**
Expand Down Expand Up @@ -2231,14 +2215,7 @@ public void setIgnoredCheckIns(final @Nullable List<String> ignoredCheckIns) {
/** Returns the current {@link SentryDateProvider} that is used to retrieve the current date. */
@ApiStatus.Internal
public @NotNull SentryDateProvider getDateProvider() {
if (dateProvider == null) {
synchronized (dateProviderLock) {
if (dateProvider == null) {
dateProvider = new SentryAutoDateProvider();
}
}
}
return Objects.requireNonNull(dateProvider, "DateProvider is not set");
return dateProvider.getValue();
}

/**
Expand All @@ -2249,7 +2226,7 @@ public void setIgnoredCheckIns(final @Nullable List<String> ignoredCheckIns) {
*/
@ApiStatus.Internal
public void setDateProvider(final @NotNull SentryDateProvider dateProvider) {
this.dateProvider = dateProvider;
this.dateProvider.setValue(dateProvider);
}

/**
Expand Down
15 changes: 8 additions & 7 deletions sentry/src/main/java/io/sentry/cache/CacheStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.sentry.SentryOptions;
import io.sentry.Session;
import io.sentry.clientreport.DiscardReason;
import io.sentry.util.LazyEvaluator;
import io.sentry.util.Objects;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
Expand All @@ -36,8 +37,9 @@ abstract class CacheStrategy {
@SuppressWarnings("CharsetObjectCanBeUsed")
protected static final Charset UTF_8 = Charset.forName("UTF-8");

protected final @NotNull SentryOptions options;
protected final @NotNull ISerializer serializer;
protected @NotNull SentryOptions options;
protected final @NotNull LazyEvaluator<ISerializer> serializer =
new LazyEvaluator<>(() -> options.getSerializer());
protected final @NotNull File directory;
private final int maxSize;

Expand All @@ -48,7 +50,6 @@ abstract class CacheStrategy {
Objects.requireNonNull(directoryPath, "Directory is required.");
this.options = Objects.requireNonNull(options, "SentryOptions is required.");

this.serializer = options.getSerializer();
this.directory = new File(directoryPath);

this.maxSize = maxSize;
Expand Down Expand Up @@ -177,7 +178,7 @@ private void moveInitFlagIfNecessary(
&& currentSession.getSessionId().equals(session.getSessionId())) {
session.setInitAsTrue();
try {
newSessionItem = SentryEnvelopeItem.fromSession(serializer, session);
newSessionItem = SentryEnvelopeItem.fromSession(serializer.getValue(), session);
// remove item from envelope items so we can replace with the new one that has the
// init flag true
itemsIterator.remove();
Expand Down Expand Up @@ -216,7 +217,7 @@ private void moveInitFlagIfNecessary(

private @Nullable SentryEnvelope readEnvelope(final @NotNull File file) {
try (final InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
return serializer.deserializeEnvelope(inputStream);
return serializer.getValue().deserializeEnvelope(inputStream);
} catch (IOException e) {
options.getLogger().log(ERROR, "Failed to deserialize the envelope.", e);
}
Expand Down Expand Up @@ -258,7 +259,7 @@ private boolean isSessionType(final @Nullable SentryEnvelopeItem item) {
try (final Reader reader =
new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(item.getData()), UTF_8))) {
return serializer.deserialize(reader, Session.class);
return serializer.getValue().deserialize(reader, Session.class);
} catch (Throwable e) {
options.getLogger().log(ERROR, "Failed to deserialize the session.", e);
}
Expand All @@ -268,7 +269,7 @@ private boolean isSessionType(final @Nullable SentryEnvelopeItem item) {
private void saveNewEnvelope(
final @NotNull SentryEnvelope envelope, final @NotNull File file, final long timestamp) {
try (final OutputStream outputStream = new FileOutputStream(file)) {
serializer.serialize(envelope, outputStream);
serializer.getValue().serialize(envelope, outputStream);
// we need to set the same timestamp so the sorting from oldest to newest wont break.
file.setLastModified(timestamp);
} catch (Throwable e) {
Expand Down
12 changes: 6 additions & 6 deletions sentry/src/main/java/io/sentry/cache/EnvelopeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void store(final @NotNull SentryEnvelope envelope, final @NotNull Hint hi
try (final Reader reader =
new BufferedReader(
new InputStreamReader(new FileInputStream(currentSessionFile), UTF_8))) {
final Session session = serializer.deserialize(reader, Session.class);
final Session session = serializer.getValue().deserialize(reader, Session.class);
if (session != null) {
writeSessionToDisk(previousSessionFile, session);
}
Expand Down Expand Up @@ -204,7 +204,7 @@ private void tryEndPreviousSession(final @NotNull Hint hint) {
try (final Reader reader =
new BufferedReader(
new InputStreamReader(new FileInputStream(previousSessionFile), UTF_8))) {
final Session session = serializer.deserialize(reader, Session.class);
final Session session = serializer.getValue().deserialize(reader, Session.class);
if (session != null) {
final AbnormalExit abnormalHint = (AbnormalExit) sdkHint;
final @Nullable Long abnormalExitTimestamp = abnormalHint.timestamp();
Expand Down Expand Up @@ -263,7 +263,7 @@ private void updateCurrentSession(
try (final Reader reader =
new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(item.getData()), UTF_8))) {
final Session session = serializer.deserialize(reader, Session.class);
final Session session = serializer.getValue().deserialize(reader, Session.class);
if (session == null) {
options
.getLogger()
Expand Down Expand Up @@ -304,7 +304,7 @@ private void writeEnvelopeToDisk(
}

try (final OutputStream outputStream = new FileOutputStream(file)) {
serializer.serialize(envelope, outputStream);
serializer.getValue().serialize(envelope, outputStream);
} catch (Throwable e) {
options
.getLogger()
Expand All @@ -324,7 +324,7 @@ private void writeSessionToDisk(final @NotNull File file, final @NotNull Session

try (final OutputStream outputStream = new FileOutputStream(file);
final Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8))) {
serializer.serialize(session, writer);
serializer.getValue().serialize(session, writer);
} catch (Throwable e) {
options
.getLogger()
Expand Down Expand Up @@ -388,7 +388,7 @@ public void discard(final @NotNull SentryEnvelope envelope) {
for (final File file : allCachedEnvelopes) {
try (final InputStream is = new BufferedInputStream(new FileInputStream(file))) {

ret.add(serializer.deserializeEnvelope(is));
ret.add(serializer.getValue().deserializeEnvelope(is));
} catch (FileNotFoundException e) {
options
.getLogger()
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/util/LazyEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public LazyEvaluator(final @NotNull Evaluator<T> evaluator) {
return value;
}

public void setValue(final @Nullable T value) {
synchronized (this) {
this.value = value;
}
}

public interface Evaluator<T> {
@NotNull
T evaluate();
Expand Down

0 comments on commit b6f9dd0

Please sign in to comment.