Class SqliteStorageLayer<T extends IReplicationOrderedPacket>

java.lang.Object
com.gigaspaces.internal.server.space.redolog.storage.SqliteStorageLayer<T>
Direct Known Subclasses:
SqliteRedoLogFileStorage

public abstract class SqliteStorageLayer<T extends IReplicationOrderedPacket> extends Object
  • Field Details

  • Constructor Details

  • Method Details

    • initIfNeeded

      protected void initIfNeeded() throws StorageException
      Opens the database on first use, not in the constructor. A swap redo-log is a contingency: most members never overflow their in-memory backlog, and creating a database file plus a table for every member of every space at startup costs a JDBC connect, a native library unpack and a CREATE TABLE that most of them never need. The byte-buffer storage has always worked this way - its file appears in ByteBufferRedoLogFileStorage.initIfNeeded(), called from appendBatch() alone - and doing it eagerly here is what produced the startup race on the shared redo-log directory.

      Callers that write must invoke this first; callers that read must be able to answer "empty" while isInitialized() is false, exactly as the byte-buffer storage does.

      Note that failures now surface on the first swap rather than at space startup: a broken sqlite setup (missing driver, unwritable directory) no longer prevents a space from starting, it fails when the disk is first actually needed. This too matches the byte-buffer behaviour.

      Throws:
      StorageException
    • isInitialized

      protected boolean isInitialized()
      Returns:
      whether the database has been opened; while false there is nothing on disk to read.
    • getReaderConnection

      protected Connection getReaderConnection() throws SQLException
      Read cursors must not share the writer connection. A cursor handed back to a caller is walked long after the statement was executed - the replication feed iterates the backlog while feeder threads keep inserting and deleting - and a JDBC connection gives no concurrency guarantee for that: the cursor and the writes end up serialized against each other inside the driver. journal_mode=wal is already enabled, but WAL only lets a reader run alongside a writer when the two use separate connections, so a single shared connection threw away the very property this needs.

      One dedicated reader connection is enough (readers here are few - one replication feed per backlog) and, unlike a connection per iterator, an iterator that is never closed cannot exhaust connections.

      Throws:
      SQLException
    • closeReaderConnection

      protected void closeReaderConnection()
    • closeQuietly

      protected void closeQuietly(AutoCloseable closeable)
    • getStorageRedoLogSize

      protected long getStorageRedoLogSize()
    • executeCreateTable

      protected void executeCreateTable(String sqlQuery) throws SQLException
      Throws:
      SQLException
    • executeInsert

      protected long executeInsert(PreparedStatement statement) throws SQLException
      Throws:
      SQLException
    • executeScalarLong

      protected long executeScalarLong(String sqlQuery) throws SQLException
      Runs a single-value query on the writer connection and consumes it before returning, so the statement and the result set never outlive the call. Queries whose rows are consumed lazily must not go through the writer connection at all - see getReaderConnection().
      Throws:
      SQLException
    • executeDelete

      protected long executeDelete(String sqlQuery) throws SQLException
      Throws:
      SQLException
    • serializePacket

      protected byte[] serializePacket(T packet)
    • deserializePacket

      protected T deserializePacket(ResultSet resultSet)
    • dropTable

      protected void dropTable() throws SQLException
      Throws:
      SQLException
    • deleteDataFile

      public void deleteDataFile()
      Deletes this member's database and its sqlite sidecars (-wal/-shm/-journal), and nothing else.

      Every member of a space shares this directory and names its file after its container, so the previous filter - name.matches(dbName + ".*") - also matched every member whose container name merely starts with this one's. A primary is exactly that prefix of its own backup ("..._container1" against "..._container1_1"), so a restarting primary deleted the redo-log of a live backup. Nothing failed at that moment - the backup's open connection kept working on the unlinked inode - and the damage surfaced only once something opened the database again and found an empty file.

    • getPacketCount

      protected int getPacketCount(T packet)
    • getPacketTypeName

      protected String getPacketTypeName(T packet)
    • getPacketUID

      protected String getPacketUID(T packet)
    • getPacketWeight

      protected int getPacketWeight(T packet)
    • getPacketOperationTypeName

      protected String getPacketOperationTypeName(T packet)