Skip to content

Reduce useless io calls #4333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
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 @@ -19,6 +19,7 @@
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -146,22 +147,25 @@ public static boolean hasStartupCrashMarker(final @NotNull SentryOptions options

final File lastAnrMarker = new File(cacheDirPath, LAST_ANR_REPORT);
try {
if (lastAnrMarker.exists() && lastAnrMarker.canRead()) {
final String content = FileUtils.readText(lastAnrMarker);
// we wrapped into try-catch already
//noinspection ConstantConditions
return content.equals("null") ? null : Long.parseLong(content.trim());
} else {
final String content = FileUtils.readText(lastAnrMarker);
// we wrapped into try-catch already
//noinspection ConstantConditions
return content.equals("null") ? null : Long.parseLong(content.trim());
} catch (Throwable e) {
final char[] buf = new char[64];
new String(buf, 0, 0);
if (e instanceof FileNotFoundException) {
options
.getLogger()
.log(DEBUG, "Last ANR marker does not exist. %s.", lastAnrMarker.getAbsolutePath());
} else {
options.getLogger().log(ERROR, "Error reading last ANR marker", e);
}
} catch (Throwable e) {
options.getLogger().log(ERROR, "Error reading last ANR marker", e);
}
return null;
}

@TestOnly
private void writeLastReportedAnrMarker(final @Nullable Long timestamp) {
final String cacheDirPath = options.getCacheDirPath();
if (cacheDirPath == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ private CpuInfoUtils() {}
if (!cpuDir.getName().matches("cpu[0-9]+")) continue;
File cpuMaxFreqFile = new File(cpuDir, CPUINFO_MAX_FREQ_PATH);

if (!cpuMaxFreqFile.exists() || !cpuMaxFreqFile.canRead()) continue;

long khz;
try {
String content = FileUtils.readText(cpuMaxFreqFile);
Expand Down
23 changes: 4 additions & 19 deletions sentry/src/main/java/io/sentry/DirectoryProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,19 @@ public void processDirectory(final @NotNull File directory) {
try {
logger.log(SentryLevel.DEBUG, "Processing dir. %s", directory.getAbsolutePath());

if (!directory.exists()) {
logger.log(
SentryLevel.WARNING,
"Directory '%s' doesn't exist. No cached events to send.",
directory.getAbsolutePath());
return;
}
if (!directory.isDirectory()) {
logger.log(
SentryLevel.ERROR, "Cache dir %s is not a directory.", directory.getAbsolutePath());
return;
}

final File[] listFiles = directory.listFiles();
if (listFiles == null) {
final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name));
if (filteredListFiles == null) {
logger.log(SentryLevel.ERROR, "Cache dir %s is null.", directory.getAbsolutePath());
return;
}

final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name));

logger.log(
SentryLevel.DEBUG,
"Processing %d items from cache dir %s",
filteredListFiles != null ? filteredListFiles.length : 0,
filteredListFiles.length,
directory.getAbsolutePath());

for (File file : listFiles) {
for (File file : filteredListFiles) {
// it ignores .sentry-native database folder and new ones that might come up
if (!file.isFile()) {
logger.log(SentryLevel.DEBUG, "File %s is not a File.", file.getAbsolutePath());
Expand Down
15 changes: 7 additions & 8 deletions sentry/src/main/java/io/sentry/cache/EnvelopeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,17 @@ public void discard(final @NotNull SentryEnvelope envelope) {
Objects.requireNonNull(envelope, "Envelope is required.");

final File envelopeFile = getEnvelopeFile(envelope);
if (envelopeFile.exists()) {
if (envelopeFile.delete()) {
options
.getLogger()
.log(DEBUG, "Discarding envelope from cache: %s", envelopeFile.getAbsolutePath());

if (!envelopeFile.delete()) {
options
.getLogger()
.log(ERROR, "Failed to delete envelope: %s", envelopeFile.getAbsolutePath());
}
} else {
options.getLogger().log(DEBUG, "Envelope was not cached: %s", envelopeFile.getAbsolutePath());
options
.getLogger()
.log(
DEBUG,
"Envelope was not cached or could not be deleted: %s",
envelopeFile.getAbsolutePath());
}
}

Expand Down
Loading