-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[FCM] Recovery logic for a corrupt database #15573
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
Changes from all commits
3cd2386
387e537
21db2f6
3d99000
778872d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -489,6 +489,12 @@ - (void)removeDatabase { | |
| }); | ||
| } | ||
|
|
||
| - (void)createTable { | ||
| [self createTableWithName:kTableOutgoingRmqMessages command:kCreateTableOutgoingRmqMessages]; | ||
| [self createTableWithName:kTableLastRmqId command:kCreateTableLastRmqId]; | ||
| [self createTableWithName:kTableS2DRmqIds command:kCreateTableS2DRmqIds]; | ||
| } | ||
|
|
||
| - (void)openDatabase { | ||
| dispatch_async(_databaseOperationQueue, ^{ | ||
| NSFileManager *fileManager = [NSFileManager defaultManager]; | ||
|
|
@@ -512,26 +518,44 @@ - (void)openDatabase { | |
| NSAssert(NO, errorMessage); | ||
| return; | ||
| } | ||
| [self createTableWithName:kTableOutgoingRmqMessages command:kCreateTableOutgoingRmqMessages]; | ||
|
|
||
| [self createTableWithName:kTableLastRmqId command:kCreateTableLastRmqId]; | ||
| [self createTableWithName:kTableS2DRmqIds command:kCreateTableS2DRmqIds]; | ||
| [self createTable]; | ||
| } else { | ||
| // Calling sqlite3_open should create the database, since the file doesn't exist. | ||
| // The file exists, try to open it. If it fails, it might be corrupt. | ||
| int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; | ||
| #ifdef SQLITE_OPEN_FILEPROTECTION_NONE | ||
| flags |= SQLITE_OPEN_FILEPROTECTION_NONE; | ||
| #endif | ||
| int result = sqlite3_open_v2([path UTF8String], &self->_database, flags, NULL); | ||
|
|
||
| // If opening the database failed, it might be corrupt. Try to recover by deleting and | ||
| // recreating it. | ||
| if (result != SQLITE_OK) { | ||
| NSString *errorString = FIRMessagingStringFromSQLiteResult(result); | ||
| NSString *errorMessage = | ||
| [NSString stringWithFormat:@"Could not create RMQ database at path %@, error: %@", path, | ||
| errorString]; | ||
| FIRMessagingLoggerError(kFIRMessagingMessageCodeRmq2PersistentStoreErrorCreatingDatabase, | ||
| @"%@", errorMessage); | ||
| NSAssert(NO, errorMessage); | ||
| didOpenDatabase = NO; | ||
| FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase, | ||
| @"Could not open RMQ database at path: %@. " | ||
| "Will delete and try to recreate it.", | ||
| path); | ||
| NSError *removeError; | ||
| if (![[NSFileManager defaultManager] removeItemAtPath:path error:&removeError]) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we know the exact error code SQLite returns when the database is corrupt? Deleting the database for all error codes seems a bit extreme.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to #14880 , the error code is 14
How about updating the code to only delete the file if the result is
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry about the quick merge. Addressing feedback in #15678 |
||
| FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase, | ||
| @"Failed to delete corrupt database at %@: %@", path, removeError); | ||
| } | ||
| // After deleting, try to open it again. | ||
| result = sqlite3_open_v2([path UTF8String], &self->_database, flags, NULL); | ||
| // If it still fails after the recovery attempt, then assert and crash. | ||
| if (result != SQLITE_OK) { | ||
| NSString *errorString = FIRMessagingStringFromSQLiteResult(result); | ||
| NSString *errorMessage = [NSString | ||
| stringWithFormat:@"Could not open or create RMQ database at path %@, error: %@", path, | ||
| errorString]; | ||
| FIRMessagingLoggerError(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase, | ||
| @"%@", errorMessage); | ||
| NSAssert(NO, errorMessage); | ||
| didOpenDatabase = NO; // Still failed, so indicate database did not open. | ||
| } else { | ||
| // Successfully recreated after corruption, so treat as a new database for table creation. | ||
| didOpenDatabase = YES; // Indicate successful opening after recreation. | ||
ncooke3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [self createTable]; | ||
| } | ||
| } else { | ||
| [self updateDBWithStringRmqID]; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.