From 5ba847ec91b0eb8f012a8374a26d8db3eeab5b07 Mon Sep 17 00:00:00 2001 From: danieldoglas Date: Tue, 24 Dec 2024 15:46:35 +0000 Subject: [PATCH 1/2] adding logic to skip leader --- sqlitecluster/SQLiteNode.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sqlitecluster/SQLiteNode.cpp b/sqlitecluster/SQLiteNode.cpp index d40372b92..c62e3ed7d 100644 --- a/sqlitecluster/SQLiteNode.cpp +++ b/sqlitecluster/SQLiteNode.cpp @@ -2197,6 +2197,7 @@ void SQLiteNode::_updateSyncPeer() { SQLitePeer* newSyncPeer = nullptr; uint64_t commitCount = _db.getCommitCount(); + bool isLeaderValidPeer = false; for (auto peer : _peerList) { // If either of these conditions are true, then we can't use this peer. if (!peer->loggedIn || peer->commitCount <= commitCount) { @@ -2208,6 +2209,13 @@ void SQLiteNode::_updateSyncPeer() continue; } + // We want to sync, if possible, from a peer that is not the leader. So at this point, skip choosing it + // as the newSyncPeer. + if (peer == _leadPeer) { + isLeaderValidPeer = true; + continue; + } + // Any peer that makes it to here is a usable peer, so it's by default better than nothing. if (!newSyncPeer) { newSyncPeer = peer; @@ -2230,6 +2238,12 @@ void SQLiteNode::_updateSyncPeer() } } + // If we reached this point, it means that there are no other available peers to sync from, but leader + // was a valid choice. In this case, let's use it as the newSyncPeer. + if (!newSyncPeer && isLeaderValidPeer) { + newSyncPeer = _leadPeer; + } + // Log that we've changed peers. if (_syncPeer != newSyncPeer) { string from, to; From a92f259b6d4f5bfd2b103b8731819cf791082731 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 31 Dec 2024 01:08:25 -0300 Subject: [PATCH 2/2] addressing comments --- sqlitecluster/SQLiteNode.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sqlitecluster/SQLiteNode.cpp b/sqlitecluster/SQLiteNode.cpp index 465972743..a87e25f96 100644 --- a/sqlitecluster/SQLiteNode.cpp +++ b/sqlitecluster/SQLiteNode.cpp @@ -2201,7 +2201,6 @@ void SQLiteNode::_updateSyncPeer() { SQLitePeer* newSyncPeer = nullptr; uint64_t commitCount = _db.getCommitCount(); - bool isLeaderValidPeer = false; for (auto peer : _peerList) { // If either of these conditions are true, then we can't use this peer. if (!peer->loggedIn || peer->commitCount <= commitCount) { @@ -2216,7 +2215,6 @@ void SQLiteNode::_updateSyncPeer() // We want to sync, if possible, from a peer that is not the leader. So at this point, skip choosing it // as the newSyncPeer. if (peer == _leadPeer) { - isLeaderValidPeer = true; continue; } @@ -2244,7 +2242,7 @@ void SQLiteNode::_updateSyncPeer() // If we reached this point, it means that there are no other available peers to sync from, but leader // was a valid choice. In this case, let's use it as the newSyncPeer. - if (!newSyncPeer && isLeaderValidPeer) { + if (!newSyncPeer && _leadPeer) { newSyncPeer = _leadPeer; }