Module: Lich::SQLiteBusyWait::Backup

Defined in:
documented/common/sqlite_connection.rb

Overview

Prepended to SQLite3::Backup. Only backups with a marked connection at either end change behaviour. sqlite3_backup_step takes both connections' mutexes and can wait for a lock through the Ruby busy handler, so:

  • #step defers interrupts as Statement#step does: an interrupt ends the wait, the mutexes are released, and it is delivered in place of SQLITE_BUSY.
  • A shared connection may be the source. #initialize, #step and #finish run under its Monitor, so other threads wait on the Monitor rather than on the mutex with the GVL held. #finish defers interrupts while it waits for the Monitor, so a kill in ensure backup.finish can't skip sqlite3_backup_finish and leave the destination locked; the kill can take as long as the Monitor's holder (up to its busy timeout).
  • A shared connection may not be the destination (#initialize raises ArgumentError): SQLite requires nothing else use the destination until #finish, and a Monitor only serializes single calls.

Pooled and plain ends are neither tracked nor serialized. Until #finish returns nothing else may use them (no other thread, no other backup, and no Sequel call on the same pool from the backing-up thread), or a caller blocks on the mutex with the GVL held and the process freezes or the destination is corrupted. Close statements on both ends first, so GC can't finalize one mid-wait, and call #finish in an ensure. #remaining and #pagecount only read counters.

Instance Method Summary collapse

Instance Method Details

#finish ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Finishes the backup with interrupts deferred if the source is a SerializedDatabase. Ensures the backup completes and releases all locks before delivering any queued interrupts.



364
365
366
367
368
# File 'documented/common/sqlite_connection.rb', line 364

def finish
  return super unless @lich_monitor

  Thread.handle_interrupt(Object => :never) { @lich_monitor.synchronize { super } }
end

#initialize(dstdb, dstname, srcdb, srcname) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a SQLite backup, setting up busy wait handling for marked connections. Raises if the destination is a SerializedDatabase, since SQLite requires exclusive access to the destination until #finish.

Parameters:

  • dstdb (SQLite3::Database) —

    destination database

  • dstname (String) —

    destination database name (e.g., "main")

  • srcdb (SQLite3::Database) —

    source database

  • srcname (String) —

    source database name (e.g., "main")

Raises:

  • (ArgumentError) —

    if dstdb is a SerializedDatabase



335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'documented/common/sqlite_connection.rb', line 335

def initialize(dstdb, dstname, srcdb, srcname)
  if dstdb.is_a?(SerializedDatabase)
    raise ArgumentError, 'a shared lich.db3 connection cannot be a backup destination: ' \
                         'SQLite requires that no other thread use it until the backup finishes; ' \
                         'back up into a pooled or plain connection'
  end

  if dstdb.is_a?(Connection) || srcdb.is_a?(Connection)
    @lich_busy_wait = true
    @lich_monitor = srcdb.instance_variable_get(:@lich_monitor) if srcdb.is_a?(SerializedDatabase)
  end
  synchronize_source { super }
end

#step(pages) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Steps the backup, copying at most pages pages with interrupts deferred. If either the source or destination is marked with the Connection module, steps through busy wait handling.

Parameters:

  • pages (Integer) —

    maximum number of pages to copy in this step



354
355
356
357
358
# File 'documented/common/sqlite_connection.rb', line 354

def step(pages)
  return super unless @lich_busy_wait

  synchronize_source { SQLiteBusyWait.wait_uninterrupted { super } }
end