Skip to content

Clean up old sessions in the NDK #2680

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

Merged
merged 1 commit into from
May 21, 2021
Merged
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 @@ -22,5 +22,7 @@ interface CrashFilesManager {

File getSessionFileDirectory(String sessionId);

void deleteSessionFilesDirectory(String sessionId);
void deleteSessionFileDirectory(String sessionId);

void cleanOldSessionFileDirectories();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

class CrashpadController implements NativeComponentController {

@SuppressWarnings("CharsetObjectCanBeUsed") // StandardCharsets requires API level 19.
private static final Charset UTF_8 = Charset.forName("UTF-8");

private static final String SESSION_METADATA_FILE = "session.json";
private static final String APP_METADATA_FILE = "app.json";
private static final String DEVICE_METADATA_FILE = "device.json";
Expand All @@ -48,6 +50,7 @@ class CrashpadController implements NativeComponentController {
@Override
public boolean initialize(String sessionId) {
boolean initSuccess = false;
filesManager.cleanOldSessionFileDirectories();
final File crashReportDirectory = filesManager.getSessionFileDirectory(sessionId);
try {
if (crashReportDirectory != null) {
Expand All @@ -71,7 +74,8 @@ public boolean hasCrashDataForSession(String sessionId) {

@Override
public boolean finalizeSession(String sessionId) {
filesManager.deleteSessionFilesDirectory(sessionId);
filesManager.deleteSessionFileDirectory(sessionId);
filesManager.cleanOldSessionFileDirectories();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

class NdkCrashFilesManager implements CrashFilesManager {
private static final Comparator<? super File> LATEST_SESSION_FIRST =
(f1, f2) -> f2.getName().compareTo(f1.getName());
private static final int MAX_SESSIONS = 8;

private final File rootPath;

Expand All @@ -38,10 +43,21 @@ public File getSessionFileDirectory(String sessionId) {
}

@Override
public void deleteSessionFilesDirectory(String sessionId) {
public void deleteSessionFileDirectory(String sessionId) {
recursiveDelete(new File(rootPath, sessionId));
}

@Override
public void cleanOldSessionFileDirectories() {
File[] sessionFileDirectories = rootPath.listFiles(File::isDirectory);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd double check with Marc about JDK version requirements - this syntax may not be supported.

Copy link
Contributor

@mrichards mrichards May 21, 2021

Choose a reason for hiding this comment

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

It should work now that we're requiring everyone to update to Java 8 (as of the M95 release). I think this syntax is 100% resolved in the compiler but it is probably worth testing on an API 19 emulator, which I believe is the oldest we support.

Copy link
Contributor

Choose a reason for hiding this comment

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

Correction: API 16 is the oldest one we support.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested it on an emulator with API 16 and it works fine. We use this syntax in a few other places already.

if (sessionFileDirectories != null && sessionFileDirectories.length > MAX_SESSIONS) {
Arrays.sort(sessionFileDirectories, LATEST_SESSION_FIRST);
for (int i = MAX_SESSIONS; i < sessionFileDirectories.length; i++) {
recursiveDelete(sessionFileDirectories[i]);
}
}
}

@Nullable
private static File prepareDirectory(File file) {
if (file != null) {
Expand Down