DatabasePool.asyncConcurrentRead

This commit is contained in:
Gwendal Roué committed 2019-06-17 19:35:11 +02:00
1 parent 7526276f98
commit 3cea3db2b4
3 files changed
+286 -85

No files matched your search

+99 -5
View File
@@ -416,6 +416,17 @@ extension DatabasePool : DatabaseReader {
}
public func concurrentRead<T>(_ block: @escaping (Database) throws -> T) -> DatabaseFuture<T> {
// The semaphore that blocks until futureResult is defined:
let futureSemaphore = DispatchSemaphore(value: 0)
var futureResult: DatabaseResult<T>? = nil
#if compiler(>=5.0)
asyncConcurrentRead { db in
// Fetch and release the future
futureResult = DatabaseResult { try block(db.get()) }
futureSemaphore.signal()
}
#else
// Check that we're on the writer queue...
writer.execute { db in
// ... and that no transaction is opened.
@@ -431,10 +442,6 @@ extension DatabasePool : DatabaseReader {
// isolation has been established:
let isolationSemaphore = DispatchSemaphore(value: 0)
// The semaphore that blocks until futureResult is defined:
let futureSemaphore = DispatchSemaphore(value: 0)
var futureResult: DatabaseResult<T>? = nil
do {
let (reader, releaseReader) = try readerPool.get()
reader.async { db in
@@ -462,11 +469,13 @@ extension DatabasePool : DatabaseReader {
futureSemaphore.signal()
}
} catch {
return DatabaseFuture { throw error }
isolationSemaphore.signal()
futureResult = .failure(error)
}
// Block the writer queue until snapshot isolation success or error
_ = isolationSemaphore.wait(timeout: .distantFuture)
#endif
return DatabaseFuture {
// Block the future until results are fetched
@@ -475,6 +484,91 @@ extension DatabasePool : DatabaseReader {
}
}
#if compiler(>=5.0)
/// Asynchronously executes a read-only block in a protected dispatch queue.
///
/// This method must be called from a writing dispatch queue, outside of any
/// transaction. You'll get a fatal error otherwise.
///
/// The *block* argument is guaranteed to see the database in the last
/// committed state at the moment this method is called. Eventual concurrent
/// database updates are *not visible* inside the block.
///
/// This method returns as soon as the isolation guarantees described above
/// are established.
///
/// In the example below, the number of players is fetched concurrently with
/// the player insertion. Yet the future is guaranteed to return zero:
///
/// try writer.asyncWriteWithoutTransaction { db in
/// // Delete all players
/// try Player.deleteAll()
///
/// // Count players concurrently
/// writer.asyncConcurrentRead { db in
/// // Guaranteed to be zero
/// let count = try Player.fetchCount()
/// }
///
/// // Insert a player
/// try Player(...).insert(db)
/// }
///
/// Starting SQLite 3.8.0 (iOS 8.2+, OSX 10.10+, custom SQLite builds and
/// SQLCipher), attempts to write in the database from this meethod throw a
/// DatabaseError of resultCode `SQLITE_READONLY`.
///
/// - parameter block: A block that accesses the database.
public func asyncConcurrentRead(_ block: @escaping (Result<Database, Error>) -> Void) {
// Check that we're on the writer queue...
writer.execute { db in
// ... and that no transaction is opened.
GRDBPrecondition(!db.isInsideTransaction, """
concurrentRead must not be called from inside a transaction. \
If this error is raised from a DatabasePool.write block, use \
DatabasePool.writeWithoutTransaction instead (and use \
transactions when needed).
""")
}
// The semaphore that blocks the writing dispatch queue until snapshot
// isolation has been established:
let isolationSemaphore = DispatchSemaphore(value: 0)
do {
let (reader, releaseReader) = try readerPool.get()
reader.async { db in
defer {
try? db.commit() // Ignore commit error
releaseReader()
}
do {
try db.beginSnapshotIsolation()
} catch {
isolationSemaphore.signal()
block(.failure(error))
return
}
// Release the writer queue
isolationSemaphore.signal()
// Reset the schema cache before running user code in snapshot isolation
db.schemaCache = SimpleDatabaseSchemaCache()
// Fetch and release the future
block(.success(db))
}
} catch {
isolationSemaphore.signal()
block(.failure(error))
}
// Block the writer queue until snapshot isolation success or error
_ = isolationSemaphore.wait(timeout: .distantFuture)
}
#endif
/// Returns a reader that can be used from the current dispatch queue,
/// if any.
private var currentReader: SerializedDatabase? {
@@ -11,6 +11,16 @@ extension TableRecord {
/// all requests by the `TableRecord.databaseSelection` property, or
/// for individual requests with the `TableRecord.select` method.
public static func all() -> QueryInterfaceRequest<Self> {
// TODO: could we delay selection definition until we know what we are
// really fetching?
//
// Use case:
//
// // Would be nice if it would only fetch SubPlayer columns
// Player.all().asRequest(of: SubPlayer.self)
//
// // Would be nice if it would fetch extra ExtendedPlayer columns
// Player.all().asRequest(of: ExtendedPlayer.self)
let relation = SQLRelation(
source: .table(tableName: databaseTableName, alias: nil),
selection: databaseSelection)
@@ -69,7 +69,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testDatabasePoolFundamental2() throws {
// Constraint: the sum of values, the balance, must remain zero.
//
@@ -210,7 +210,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
}
XCTAssertEqual(id, 1)
}
func testReadFromPreviousNonWALDatabase() throws {
do {
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
@@ -227,7 +227,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
XCTAssertEqual(id, 1)
}
}
func testWriteOpensATransaction() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -243,7 +243,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
}
}
}
func testWriteWithoutTransactionDoesNotOpenATransaction() throws {
let dbPool = try makeDatabasePool()
try dbPool.writeWithoutTransaction { db in
@@ -268,7 +268,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
}
}
}
func testReadError() throws {
let dbPool = try makeDatabasePool()
@@ -303,7 +303,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testConcurrentRead() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -354,7 +354,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testReadMethodIsolationOfStatement() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -403,7 +403,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testReadMethodIsolationOfStatementWithCheckpoint() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -454,7 +454,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testReadBlockIsolationStartingWithRead() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -506,7 +506,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testReadBlockIsolationStartingWithSelect() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -550,7 +550,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testReadBlockIsolationStartingWithWrite() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -608,7 +608,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testReadBlockIsolationStartingWithWriteTransaction() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -669,7 +669,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testUnsafeReadMethodIsolationOfStatement() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -720,7 +720,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testUnsafeReadMethodIsolationOfStatementWithCheckpoint() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -771,7 +771,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
blocks[index]()
}
}
func testUnsafeReadMethodIsolationOfBlock() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
@@ -816,70 +816,6 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
}
}
func testConcurrentReadOpensATransaction() throws {
let dbPool = try makeDatabasePool()
let future = dbPool.writeWithoutTransaction { db in
dbPool.concurrentRead { db in
XCTAssertTrue(db.isInsideTransaction)
do {
try db.execute(sql: "BEGIN DEFERRED TRANSACTION")
XCTFail("Expected error")
} catch {
}
}
}
try future.wait()
}
func testConcurrentReadOutsideOfTransaction() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
try db.create(table: "persons") { t in
t.column("id", .integer).primaryKey()
}
}
// Writer Reader
// dbPool.writeWithoutTransaction {
// >
// dbPool.concurrentRead {
// <
// INSERT INTO items (id) VALUES (NULL)
// >
let s1 = DispatchSemaphore(value: 0)
// } SELECT COUNT(*) FROM persons -> 0
// <
// }
let future: DatabaseFuture<Int> = try dbPool.writeWithoutTransaction { db in
let future: DatabaseFuture<Int> = dbPool.concurrentRead { db in
_ = s1.wait(timeout: .distantFuture)
return try! Int.fetchOne(db, sql: "SELECT COUNT(*) FROM persons")!
}
try db.execute(sql: "INSERT INTO persons DEFAULT VALUES")
s1.signal()
return future
}
XCTAssertEqual(try future.wait(), 0)
}
func testConcurrentReadError() throws {
let dbPool = try makeDatabasePool()
try dbPool.writeWithoutTransaction { db in
try db.execute(sql: "PRAGMA locking_mode=EXCLUSIVE")
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
let future = dbPool.concurrentRead { db in
fatalError("Should not run")
}
do {
try future.wait()
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_BUSY)
XCTAssertEqual(error.message!, "database is locked")
}
}
}
func testLongRunningReadTransaction() throws {
// A test for a "user-defined" DatabaseSnapshot based on DatabaseQueue
let dbName = "test.sqlite"
@@ -911,7 +847,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
try XCTAssertEqual(Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t")!, 2)
}
}
func testIssue80() throws {
// See https://github.com/groue/GRDB.swift/issues/80
//
@@ -1089,4 +1025,165 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
try test(qos: .userInitiated)
}
}
// MARK: - ConcurrentRead
func testConcurrentReadOpensATransaction() throws {
let dbPool = try makeDatabasePool()
let future = dbPool.writeWithoutTransaction { db in
dbPool.concurrentRead { db in
XCTAssertTrue(db.isInsideTransaction)
do {
try db.execute(sql: "BEGIN DEFERRED TRANSACTION")
XCTFail("Expected error")
} catch {
}
}
}
try future.wait()
}
func testConcurrentReadOutsideOfTransaction() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
try db.create(table: "persons") { t in
t.column("id", .integer).primaryKey()
}
}
// Writer Reader
// dbPool.writeWithoutTransaction {
// >
// dbPool.concurrentRead {
// <
// INSERT INTO items (id) VALUES (NULL)
// >
let s1 = DispatchSemaphore(value: 0)
// } SELECT COUNT(*) FROM persons -> 0
// <
// }
let future: DatabaseFuture<Int> = try dbPool.writeWithoutTransaction { db in
let future: DatabaseFuture<Int> = dbPool.concurrentRead { db in
_ = s1.wait(timeout: .distantFuture)
return try! Int.fetchOne(db, sql: "SELECT COUNT(*) FROM persons")!
}
try db.execute(sql: "INSERT INTO persons DEFAULT VALUES")
s1.signal()
return future
}
XCTAssertEqual(try future.wait(), 0)
}
func testConcurrentReadError() throws {
let dbPool = try makeDatabasePool()
try dbPool.writeWithoutTransaction { db in
try db.execute(sql: "PRAGMA locking_mode=EXCLUSIVE")
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
let future = dbPool.concurrentRead { db in
fatalError("Should not run")
}
do {
try future.wait()
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_BUSY)
XCTAssertEqual(error.message!, "database is locked")
}
}
}
// MARK: - AsyncConcurrentRead
#if compiler(>=5.0)
func testAsyncConcurrentReadOpensATransaction() throws {
let dbPool = try makeDatabasePool()
var isInsideTransaction: Bool? = nil
let expectation = self.expectation(description: "read")
dbPool.writeWithoutTransaction { db in
dbPool.asyncConcurrentRead { result in
do {
let db = try result.get()
isInsideTransaction = db.isInsideTransaction
do {
try db.execute(sql: "BEGIN DEFERRED TRANSACTION")
XCTFail("Expected error")
} catch {
}
} catch {
XCTFail("Unexpected error: \(error)")
}
expectation.fulfill()
}
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(isInsideTransaction, true)
}
#endif
#if compiler(>=5.0)
func testAsyncConcurrentReadOutsideOfTransaction() throws {
let dbPool = try makeDatabasePool()
try dbPool.write { db in
try db.create(table: "persons") { t in
t.column("id", .integer).primaryKey()
}
}
// Writer Reader
// dbPool.writeWithoutTransaction {
// >
// dbPool.concurrentRead {
// <
// INSERT INTO items (id) VALUES (NULL)
// >
let s1 = DispatchSemaphore(value: 0)
// } SELECT COUNT(*) FROM persons -> 0
// <
// }
var count: Int? = nil
let expectation = self.expectation(description: "read")
try dbPool.writeWithoutTransaction { db in
dbPool.asyncConcurrentRead { result in
do {
_ = s1.wait(timeout: .distantFuture)
let db = try result.get()
count = try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM persons")!
} catch {
XCTFail("Unexpected error: \(error)")
}
expectation.fulfill()
}
try db.execute(sql: "INSERT INTO persons DEFAULT VALUES")
s1.signal()
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(count, 0)
}
#endif
#if compiler(>=5.0)
func testAsyncConcurrentReadError() throws {
let dbPool = try makeDatabasePool()
var readError: DatabaseError? = nil
let expectation = self.expectation(description: "read")
try dbPool.writeWithoutTransaction { db in
try db.execute(sql: "PRAGMA locking_mode=EXCLUSIVE")
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
dbPool.asyncConcurrentRead { result in
guard case let .failure(error) = result,
let dbError = error as? DatabaseError
else {
XCTFail("Unexpected result: \(result)")
return
}
readError = dbError
expectation.fulfill()
}
waitForExpectations(timeout: 1, handler: nil)
XCTAssertEqual(readError!.resultCode, .SQLITE_BUSY)
XCTAssertEqual(readError!.message!, "database is locked")
}
}
#endif
}