Skip to content
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

Oncorruption logging #188

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
69 changes: 68 additions & 1 deletion src/com/amplitude/api/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Pair;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -21,6 +22,72 @@

class DatabaseHelper extends SQLiteOpenHelper {

private static class DatabaseCorruptionHandler implements DatabaseErrorHandler {
djih marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void onCorruption(SQLiteDatabase dbObj) {

Diagnostics.getLogger().logError(
"DB: corruption detected, deleting database files", new Throwable()
);

// Android's DefaultDatabaseErrorHandler
logger.e(TAG, "Corruption reported by sqlite on database: " + dbObj.getPath());
// is the corruption detected even before database could be 'opened'?
if (!dbObj.isOpen()) {
// database files are not even openable. delete this database file.
// NOTE if the database has attached databases, then any of them could be corrupt.
// and not deleting all of them could cause corrupted database file to remain and
// make the application crash on database open operation. To avoid this problem,
// the application should provide its own {@link DatabaseErrorHandler} impl class
// to delete ALL files of the database (including the attached databases).
deleteDatabaseFile(dbObj.getPath());
return;
}
else {
List<Pair<String, String>> attachedDbs = null;
try {
// Close the database, which will cause subsequent operations to fail.
// before that, get the attached database list first.
try {
attachedDbs = dbObj.getAttachedDbs();
} catch (SQLiteException e) {
/* ignore */
}
try {
dbObj.close();
} catch (SQLiteException e) {
/* ignore */
}
} finally {
// Delete all files of this corrupt database and/or attached databases
if (attachedDbs != null) {
for (Pair<String, String> p : attachedDbs) {
deleteDatabaseFile(p.second);
}
} else {
// attachedDbs = null is possible when the database is so corrupt that even
// "PRAGMA database_list;" also fails. delete the main database file
deleteDatabaseFile(dbObj.getPath());
}
}
}
}

private void deleteDatabaseFile(String fileName) {
if (fileName.equalsIgnoreCase(":memory:") || fileName.trim().length() == 0) {
return;
}
logger.e(TAG, "deleting the database file: " + fileName);
try {
SQLiteDatabase.deleteDatabase(new File(fileName));
} catch (Exception e) {
/* print warning and ignore exception */
logger.w(TAG, "delete failed: " + e.getMessage());
}
}
}

static final Map<String, DatabaseHelper> instances = new HashMap<String, DatabaseHelper>();

private static final String TAG = "com.amplitude.api.DatabaseHelper";
Expand Down Expand Up @@ -79,7 +146,7 @@ protected DatabaseHelper(Context context) {
}

protected DatabaseHelper(Context context, String instance) {
super(context, getDatabaseName(instance), null, Constants.DATABASE_VERSION);
super(context, getDatabaseName(instance), null, Constants.DATABASE_VERSION, new DatabaseCorruptionHandler());
file = context.getDatabasePath(getDatabaseName(instance));
instanceName = Utils.normalizeInstanceName(instance);
}
Expand Down
29 changes: 28 additions & 1 deletion src/com/amplitude/api/Diagnostics.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.amplitude.api;

import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

import org.json.JSONArray;
Expand Down Expand Up @@ -111,10 +113,35 @@ public void run() {
if (event == null) {
event = new JSONObject();
try {

// add attributes to diagnostic event

event.put("error", AmplitudeClient.truncate(error));
event.put("timestamp", System.currentTimeMillis());
event.put("device_id", deviceId);
event.put("count", 1);
event.put("library", String.format("amplitude-android/%s", Constants.VERSION));

// get memory stats
Runtime runtime = Runtime.getRuntime();
long usedMemInMB = (runtime.totalMemory() - runtime.freeMemory()) / 1048576L;
long maxHeapSizeInMB = runtime.maxMemory() / 1048576L;
long availHeapSizeInMB = maxHeapSizeInMB - usedMemInMB;
event.put("used_mem_mb", usedMemInMB);
event.put("max_heap_mb", maxHeapSizeInMB);
event.put("heap_avail_mb", availHeapSizeInMB);

// get disk stats
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
}
else {
bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
}
long megAvailable = bytesAvailable / (1024 * 1024);
event.put("disk_avail_mb", megAvailable);

if (exception != null) {
String stackTrace = Log.getStackTraceString(exception);
Expand All @@ -123,7 +150,7 @@ public void run() {
}
}

// unsent queues are full, make room by removing
// add unsent errors to queue. if full, make room by removing
if (unsentErrorStrings.size() >= diagnosticEventMaxCount) {
for (int i = 0; i < DIAGNOSTIC_EVENT_MIN_COUNT; i++) {
String errorString = unsentErrorStrings.remove(0);
Expand Down
3 changes: 3 additions & 0 deletions test/com/amplitude/api/DiagnosticsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,17 @@ public void testLogError() {
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(0)).optString("error"), "test_error");
assertTrue(logger.unsentErrors.get(logger.unsentErrorStrings.get(0)).optLong("timestamp") >= timestamp);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(0)).optInt("count"), 1);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(0)).optString("library"), "amplitude-android/2.21.0");
assertEquals(logger.unsentErrorStrings.get(1), "test_error1");
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(1)).optString("error"), "test_error1");
assertTrue(logger.unsentErrors.get(logger.unsentErrorStrings.get(1)).optLong("timestamp") >= timestamp);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(1)).optInt("count"), 1);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(1)).optString("library"), "amplitude-android/2.21.0");
assertEquals(logger.unsentErrorStrings.get(2), "test_error2");
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(2)).optString("error"), "test_error2");
assertTrue(logger.unsentErrors.get(logger.unsentErrorStrings.get(2)).optLong("timestamp") >= timestamp);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(2)).optInt("count"), 1);
assertEquals(logger.unsentErrors.get(logger.unsentErrorStrings.get(2)).optString("library"), "amplitude-android/2.21.0");

// test truncation
logger.setDiagnosticEventMaxCount(7);
Expand Down