Class SqliteStorageLayer<T extends IReplicationOrderedPacket>
- Direct Known Subclasses:
SqliteRedoLogFileStorage
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Connectionprotected final org.slf4j.Loggerprotected final ReentrantLockprotected final intprotected final intprotected final intprotected final intprotected final intprotected static final Stringprotected final intprotected final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcloseQuietly(AutoCloseable closeable) protected voidvoidDeletes this member's database and its sqlite sidecars (-wal/-shm/-journal), and nothing else.protected TdeserializePacket(ResultSet resultSet) protected voidprotected voidexecuteCreateTable(String sqlQuery) protected longexecuteDelete(String sqlQuery) protected longexecuteInsert(PreparedStatement statement) protected longexecuteScalarLong(String sqlQuery) 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.protected intgetPacketCount(T packet) protected StringgetPacketOperationTypeName(T packet) protected StringgetPacketTypeName(T packet) protected StringgetPacketUID(T packet) protected intgetPacketWeight(T packet) protected ConnectionRead cursors must not share the writer connection.protected longprotected voidOpens the database on first use, not in the constructor.protected booleanprotected byte[]serializePacket(T packet)
-
Field Details
-
logger
protected final org.slf4j.Logger logger -
TABLE_NAME
- See Also:
-
connection
-
modifierLock
-
COLUMN_NAMES
-
REDO_KEY_COLUMN_INDEX
protected final int REDO_KEY_COLUMN_INDEX- See Also:
-
TYPE_NAME_COLUMN_INDEX
protected final int TYPE_NAME_COLUMN_INDEX- See Also:
-
OPERATION_TYPE_COLUMN_INDEX
protected final int OPERATION_TYPE_COLUMN_INDEX- See Also:
-
UID_COLUMN_INDEX
protected final int UID_COLUMN_INDEX- See Also:
-
PACKET_COUNT_COLUMN_INDEX
protected final int PACKET_COUNT_COLUMN_INDEX- See Also:
-
PACKET_WEIGHT_COLUMN_INDEX
protected final int PACKET_WEIGHT_COLUMN_INDEX- See Also:
-
PACKET_COLUMN_INDEX
protected final int PACKET_COLUMN_INDEX- See Also:
-
-
Constructor Details
-
SqliteStorageLayer
-
-
Method Details
-
initIfNeeded
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 aCREATE TABLEthat most of them never need. The byte-buffer storage has always worked this way - its file appears inByteBufferRedoLogFileStorage.initIfNeeded(), called fromappendBatch()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
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
-
getStorageRedoLogSize
protected long getStorageRedoLogSize() -
executeCreateTable
- Throws:
SQLException
-
executeInsert
- Throws:
SQLException
-
executeScalarLong
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 - seegetReaderConnection().- Throws:
SQLException
-
executeDelete
- Throws:
SQLException
-
serializePacket
-
deserializePacket
-
dropTable
- 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
-
getPacketTypeName
-
getPacketUID
-
getPacketWeight
-
getPacketOperationTypeName
-