-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add PostgreSQL support for rollback backend #16757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
sfan5
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rollback code is a bit neglected, but surprisingly many people still use it and I guess adding another backend is okay.
src/server/rollback.cpp
Outdated
| endSaveActions(); | ||
| action_todisk_buffer.clear(); | ||
| } catch (...) { | ||
| rollbackSaveActions(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems somewhat assuming of a database-like backend. I would have expected for flush to be fully implemented in the child class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made "flush()" pure virtual in the base class and extracted the common buffer iteration logic to a "flushBufferContents()" helper method. Each backend now implements its own "flush()" method with backend-specific transaction handling, allowing for future backends that don't use database transactions.
src/server/rollback_postgresql.cpp
Outdated
|
|
||
| RollbackManagerPostgreSQL::~RollbackManagerPostgreSQL() | ||
| { | ||
| flush(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this safe if connectToDatabase fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Thanks. Added an "if (initialized())" check before calling "flush()" in the destructor. This ensures that "flush()" is called only if the database connection was successfully established. The same safety check was also added to the SQLite backend for consistency.
Extract the large cache loading blocks from RollbackManagerPostgreSQL constructor into loadActorCache() and loadNodeCache() helper methods. This reduces the constructor from ~33 lines to 5 lines, addressing sfan5's feedback about large code blocks.
src/server/rollback_sqlite3.cpp
Outdated
| // Invalid format - bind 0,0,0 as fallback | ||
| SQLOK(sqlite3_bind_int(stmt_do, 10, 0)); | ||
| SQLOK(sqlite3_bind_int(stmt_do, 11, 0)); | ||
| SQLOK(sqlite3_bind_int(stmt_do, 12, 0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was not part of the original code. Did you ensure that these code blocks are functionally identical?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested it and it works fine.
SQLOK expands to.
if (sqlite3_bind_int(stmt_do, 10, 0) != SQLITE_OK) {
throw FileNotGoodException(...);
}
This might be a little paranoid. I can remove it if you like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point was the fallback to 0,0,0. I think the previous code would throw an std::out_of_range exception if npos was returned from one of the find calls. If that's the case, then this new code would be unused - even worse - it would become code that barely anyone would want to touch due to the rick of breaking things.
EDIT: Also 0,0,0 is a valid node position, potentially causing unexpected side-effects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was being blind there. That is a brain fart. Damn.
parseNodemetaLocation() should throw a std::invalid_argument on invalid format instead of returning false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I believe that would be appropriate. Of course, having a few unittest lines for that would be greatly appreciated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added unittest TestRollbackBackendBase
Co-authored-by: SmallJoker <[email protected]>
Co-authored-by: SmallJoker <[email protected]>
Improve error handling in Server::createRollbackManager - Throw exception if world.mt cannot be read, matching openModStorageDatabase pattern - Remove unused backend_source variable and simplify logging - Simplify backend selection using world_mt.getNoEx() instead of if-else block Enhance error messages and help output - Add Server::getRollbackBackends() to dynamically list available backends - Use dynamic backend list in error message for unsupported backends - Add rollback backends to print_help() output in main.cpp - Remove redundant USE_POSTGRESQL=OFF specific error message Improve destructor safety and consistency - Move db != nullptr check from SQLite destructor to flush() method - Move initialized() check from PostgreSQL destructor to flush() method - Ensures consistent safety checks across both backends Restore spacing alignment for readability - Add space padding after sqlite3_bind_* function names - Matches existing pattern in rollback_sqlite3.cpp Rename RollbackManager to RollbackMgr for line length compliance - Use RollbackMgr abbreviation following existing codebase patterns - Rename RollbackManager → RollbackMgr - Rename RollbackManagerSQLite3 → RollbackMgrSQLite3 - Rename RollbackManagerPostgreSQL → RollbackMgrPostgreSQL - Break long function signatures to ensure all lines are under 95 characters
…ng bool The original code would throw std::out_of_range when substr() was called with npos, making the fallback code that bound 0,0,0 unreachable dead code. This change makes parseNodemetaLocation() throw std::invalid_argument on invalid format, matching the original behavior and eliminating the dead code. Changes: Change parseNodemetaLocation() signature from bool to void Throw std::invalid_argument exceptions instead of returning false Remove unreachable fallback blocks in SQLite and PostgreSQL backends Add #include <stdexcept> to rollback.cpp This addresses SmallJoker's feedback.
Address SmallJoker's request for unit tests to verify that parseNodemetaLocation() correctly throws std::invalid_argument on invalid format. Tests cover: Valid format parsing (basic, negative, zero coordinates) Invalid format exceptions (missing prefix, missing commas, empty string)
Summary
Adds PostgreSQL support to the rollback backend, allowing servers that already use PostgreSQL as their primary database backend to use rollback without relying on SQLite.
Details
Testing