Skip to content

Conversation

@johnnyjoy
Copy link

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

  • Implements a PostgreSQL-backed rollback storage alongside the existing SQLite implementation
  • Wires backend selection through existing server configuration
  • Adds documentation and configuration examples for PostgreSQL rollback usage
  • Includes a CMake preset for building a server-only PostgreSQL configuration

Testing

  • Built server with PostgreSQL enabled
  • Verified rollback recording and lookup using a PostgreSQL-backed server
  • Confirmed existing SQLite-based rollback behavior remains unchanged

@Zughy Zughy added @ Startup / Config / Util Feature ✨ PRs that add or enhance a feature Roadmap: Needs approval The change is not part of the current roadmap and needs to be approved by coredevs beforehand labels Dec 17, 2025
Copy link
Collaborator

@sfan5 sfan5 left a 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.

@sfan5 sfan5 self-requested a review December 19, 2025 19:36
@sfan5 sfan5 added Roadmap: supported by core dev PR not adhering to the roadmap, yet some core dev decided to take care of it and removed Roadmap: Needs approval The change is not part of the current roadmap and needs to be approved by coredevs beforehand labels Dec 19, 2025
endSaveActions();
action_todisk_buffer.clear();
} catch (...) {
rollbackSaveActions();
Copy link
Collaborator

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.

Copy link
Author

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.


RollbackManagerPostgreSQL::~RollbackManagerPostgreSQL()
{
flush();
Copy link
Collaborator

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?

Copy link
Author

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.
@sfan5 sfan5 self-requested a review December 27, 2025 21:27
Comment on lines 348 to 351
// 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));
Copy link
Member

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?

Copy link
Author

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.

Copy link
Member

@SmallJoker SmallJoker Dec 28, 2025

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.

Copy link
Author

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?

Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added unittest TestRollbackBackendBase

@Zughy Zughy added the Action / change needed Code still needs changes (PR) / more information requested (Issues) label Dec 28, 2025
johnnyjoy and others added 2 commits December 28, 2025 10:20
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
@SmallJoker SmallJoker self-requested a review December 28, 2025 20:24
…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)
@Zughy Zughy removed the Action / change needed Code still needs changes (PR) / more information requested (Issues) label Dec 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature ✨ PRs that add or enhance a feature Roadmap: supported by core dev PR not adhering to the roadmap, yet some core dev decided to take care of it @ Startup / Config / Util

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants