Skip to content

Commit

Permalink
Merge pull request #1534 from Expensify/ionatan_notifyplugins
Browse files Browse the repository at this point in the history
Add notifyPlugins method
  • Loading branch information
iwiznia authored Jul 14, 2023
2 parents 0c9708f + a72257a commit 7b56577
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions BedrockPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class BedrockPlugin {
// re-open those connections if not handled elsewhere in the plugin.
virtual void onAttach() {}

// Called when a node changes state
virtual void stateChanged(SQLite& db, SQLiteNodeState newState) {}

// Map of plugin names to functions that will return a new plugin of the given type.
static map<string, function<BedrockPlugin*(BedrockServer&)>> g_registeredPluginList;

Expand Down
6 changes: 6 additions & 0 deletions BedrockServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2356,3 +2356,9 @@ void BedrockServer::waitForHTTPS(unique_ptr<BedrockCommand>&& command) {
const atomic<SQLiteNodeState>& BedrockServer::getState() const {
return _nodeStateSnapshot == SQLiteNodeState::UNKNOWN ? _replicationState : _nodeStateSnapshot;
}

void BedrockServer::notifyStateChangeToPlugins(SQLite& db, SQLiteNodeState newState) {
for (auto plugin : plugins) {
plugin.second->stateChanged(db, newState);
}
}
3 changes: 3 additions & 0 deletions BedrockServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,7 @@ class BedrockServer : public SQLiteServer {
// syncNode while the sync thread exists, it's a shared pointer to allow for the last socket thread using it to
// destroy the pool at shutdown.
shared_ptr<SQLitePool> _dbPool;

// We call this method whenever a node changes state
void notifyStateChangeToPlugins(SQLite& db, SQLiteNodeState newState) override;
};
3 changes: 3 additions & 0 deletions sqlitecluster/SQLiteNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,9 @@ void SQLiteNode::_changeState(SQLiteNodeState newState) {
_localCommitNotifier.notifyThrough(_db.getCommitCount());

if (newState != _state) {
// First, we notify all plugins about the state change
_server.notifyStateChangeToPlugins(_db, newState);

// If we were following, and now we're not, we give up an any replications.
if (_state == SQLiteNodeState::FOLLOWING) {
_replicationThreadsShouldExit = true;
Expand Down
3 changes: 3 additions & 0 deletions sqlitecluster/SQLiteServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ class SQLiteServer : public STCPManager {

// When a node connects to the cluster, this function will be called on the sync thread.
virtual void onNodeLogin(SQLitePeer* peer) = 0;

// We call this method whenever a node changes state
virtual void notifyStateChangeToPlugins(SQLite& db, SQLiteNodeState newState) = 0;
};
1 change: 1 addition & 0 deletions test/tests/SQLiteNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TestServer : public SQLiteServer {

virtual bool canStandDown() { return true; }
virtual void onNodeLogin(SQLitePeer* peer) { }
virtual void notifyStateChangeToPlugins(SQLite& db, SQLiteNodeState newState) {}
};

struct SQLiteNodeTest : tpunit::TestFixture {
Expand Down

0 comments on commit 7b56577

Please sign in to comment.