From e0031ddc42e73043cbe5fe04315fa9bf11e947ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 9 Mar 2016 19:29:45 +0100 Subject: [PATCH] DatabasePool concurrency tests --- GRDB/Core/DatabasePool.swift | 18 ++- GRDB/Core/Utils.swift | 6 +- TODO.md | 6 - .../Core/DatabasePool/DatabasePoolTests.swift | 147 ++++++++++++++++++ 4 files changed, 163 insertions(+), 14 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 2a7c7e14f..b604f8071 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -1,6 +1,6 @@ public final class DatabasePool { - public convenience init(path: String, configuration: Configuration = Configuration(), maxReaderCount: Int = 5) throws { - precondition(maxReaderCount > 1, "maxReaderCount must be at least 1") + public convenience init(path: String, configuration: Configuration = Configuration(), maximumReaderCount: Int = 5) throws { + precondition(maximumReaderCount > 1, "maximumReaderCount must be at least 1") // Shared database schema cache let schemaCache = SharedDatabaseSchemaCache() @@ -8,7 +8,7 @@ public final class DatabasePool { // Readers var readConfig = configuration readConfig.readonly = true - let readerPool = Pool(size: maxReaderCount) { + let readerPool = Pool(maximumCount: maximumReaderCount) { try! SerializedDatabase(path: path, configuration: readConfig, schemaCache: schemaCache) } @@ -50,11 +50,11 @@ public final class DatabasePool { } } - public func checkpoint() throws { + public func checkpoint(kind: CheckpointKind = .Passive) throws { try writer.inDatabase { db in // TODO: read https://www.sqlite.org/c3ref/wal_checkpoint_v2.html and // check whether we need a busy handler on writer and/or readers. - let code = sqlite3_wal_checkpoint_v2(db.sqliteConnection, nil, SQLITE_CHECKPOINT_TRUNCATE, nil, nil) + let code = sqlite3_wal_checkpoint_v2(db.sqliteConnection, nil, kind.rawValue, nil, nil) guard code == SQLITE_OK else { throw DatabaseError(code: code, message: db.lastErrorMessage, sql: nil) } @@ -69,3 +69,11 @@ public final class DatabasePool { self.writer = writer } } + +// See https://www.sqlite.org/c3ref/wal_checkpoint_v2.html +public enum CheckpointKind: Int32 { + case Passive = 0 // SQLITE_CHECKPOINT_PASSIVE + case Full = 1 // SQLITE_CHECKPOINT_FULL + case Restart = 2 // SQLITE_CHECKPOINT_RESTART + case Truncate = 3 // SQLITE_CHECKPOINT_TRUNCATE +} diff --git a/GRDB/Core/Utils.swift b/GRDB/Core/Utils.swift index 9b841db5e..32a4a88a4 100644 --- a/GRDB/Core/Utils.swift +++ b/GRDB/Core/Utils.swift @@ -165,11 +165,11 @@ final class Pool { private let queue: dispatch_queue_t // protects availableElements private let semaphore: dispatch_semaphore_t // limits the number of elements - init(size: Int, makeElement: () -> T) { - precondition(size > 0, "Pool size must be at least 1") + init(maximumCount: Int, makeElement: () -> T) { + precondition(maximumCount > 0, "Pool size must be at least 1") self.makeElement = makeElement self.queue = dispatch_queue_create("com.github.groue.GRDB.Pool", nil) - self.semaphore = dispatch_semaphore_create(size) + self.semaphore = dispatch_semaphore_create(maximumCount) } func get(@noescape block: (T) throws -> U) rethrows -> U { diff --git a/TODO.md b/TODO.md index 36c4ee395..808a37352 100644 --- a/TODO.md +++ b/TODO.md @@ -11,13 +11,7 @@ - [ ] REGEXP https://www.sqlite.org/lang_expr.html - [ ] CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END https://www.sqlite.org/lang_expr.html - [ ] DatabasePool - - [ ] Tests :-) - [ ] Forbid transactions and save points in readers - - [ ] DatabasePool.checkpoint() https://www.sqlite.org/c3ref/wal_checkpoint_v2.html - - [ ] Concurrency tests: - - [ ] Iterate rows concurrently. - - [ ] Iterate rows, and concurrently, update the database rows. Is the selection modified? - - [ ] Iterate rows, and concurrently, update the database rows and then checkpoint. Is the selection modified? Not sure: diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolTests.swift index a17e7b7cf..5a89f87f5 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolTests.swift @@ -16,4 +16,151 @@ class DatabasePoolTests: GRDBTestCase { } } + func testConcurrentRead() { + assertNoError { + try dbPool.write { db in + try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") + for _ in 0..<3 { + try db.execute("INSERT INTO items (id) VALUES (NULL)") + } + } + + // Block 1 Block 2 + // SELECT * FROM items SELECT * FROM items + // step step + // > + let s1 = dispatch_semaphore_create(0) + // step + // < + let s2 = dispatch_semaphore_create(0) + // step step + // step end + // end + + let block1 = { () in + self.dbPool.read { db in + let generator = Row.fetch(db, "SELECT * FROM items").generate() + XCTAssertTrue(generator.next() != nil) + dispatch_semaphore_signal(s1) + dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER) + XCTAssertTrue(generator.next() != nil) + XCTAssertTrue(generator.next() != nil) + XCTAssertTrue(generator.next() == nil) + } + } + let block2 = { () in + self.dbPool.read { db in + let generator = Row.fetch(db, "SELECT * FROM items").generate() + XCTAssertTrue(generator.next() != nil) + dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) + XCTAssertTrue(generator.next() != nil) + dispatch_semaphore_signal(s2) + XCTAssertTrue(generator.next() != nil) + XCTAssertTrue(generator.next() == nil) + } + } + let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT) + dispatch_apply(2, queue) { index in + [block1, block2][index]() + } + } + } + + func testConcurrentReadWrite() { + assertNoError { + try dbPool.write { db in + try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") + for _ in 0..<2 { + try db.execute("INSERT INTO items (id) VALUES (NULL)") + } + } + + // Block 1 Block 2 + // SELECT * FROM items + // step + // > + let s1 = dispatch_semaphore_create(0) + // DELETE * FROM items + // < + let s2 = dispatch_semaphore_create(0) + // step + // end + + let block1 = { () in + self.dbPool.read { db in + let generator = Row.fetch(db, "SELECT * FROM items").generate() + XCTAssertTrue(generator.next() != nil) + dispatch_semaphore_signal(s1) + dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER) + XCTAssertTrue(generator.next() != nil) + XCTAssertTrue(generator.next() == nil) + } + } + let block2 = { () in + do { + dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) + defer { dispatch_semaphore_signal(s2) } + try self.dbPool.write { db in + try db.execute("DELETE FROM items") + } + } catch { + XCTFail("error: \(error)") + } + } + let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT) + dispatch_apply(2, queue) { index in + [block1, block2][index]() + } + } + } + + func testConcurrentReadWritePlusCheckpoint() { + assertNoError { + try dbPool.write { db in + try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") + for _ in 0..<2 { + try db.execute("INSERT INTO items (id) VALUES (NULL)") + } + } + + // Block 1 Block 2 + // SELECT * FROM items + // step + // > + let s1 = dispatch_semaphore_create(0) + // DELETE * FROM items + // Checkpoint + // < + let s2 = dispatch_semaphore_create(0) + // step + // end + + let block1 = { () in + self.dbPool.read { db in + let generator = Row.fetch(db, "SELECT * FROM items").generate() + XCTAssertTrue(generator.next() != nil) + dispatch_semaphore_signal(s1) + dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER) + XCTAssertTrue(generator.next() != nil) + XCTAssertTrue(generator.next() == nil) + } + } + let block2 = { () in + do { + dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) + defer { dispatch_semaphore_signal(s2) } + try self.dbPool.write { db in + try db.execute("DELETE FROM items") + } + try self.dbPool.checkpoint() // Default checkpoint should not fail if there is a reader (block1) + } catch { + XCTFail("error: \(error)") + } + } + let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT) + dispatch_apply(2, queue) { index in + [block1, block2][index]() + } + } + } }