Introduce network serial caching in sync fast path, optimize DB reads, and add granular cache invalidation

This commit is contained in:
mlsmaycon
2026-04-24 20:50:47 +02:00
parent 4dddafc5a1
commit d3ea28734c
8 changed files with 178 additions and 7 deletions
+24
View File
@@ -2658,6 +2658,30 @@ func (s *SqlStore) GetAccountNetwork(ctx context.Context, lockStrength LockingSt
return accountNetwork.Network, nil
}
// GetAccountNetworkSerial returns only the network.serial column for an
// account, avoiding the overhead of materialising the full Network struct
// (which carries a JSON-serialised CIDR and other columns). Used by the Sync
// fast path to check whether the peer's cached serial still matches without
// paying the full-row read cost on contended DBs.
func (s *SqlStore) GetAccountNetworkSerial(ctx context.Context, lockStrength LockingStrength, accountID string) (uint64, error) {
tx := s.db
if lockStrength != LockingStrengthNone {
tx = tx.Clauses(clause.Locking{Strength: string(lockStrength)})
}
var serial uint64
if err := tx.Model(&types.Account{}).
Select("network_serial").
Where(idQueryCondition, accountID).
Take(&serial).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return 0, status.NewAccountNotFoundError(accountID)
}
return 0, status.Errorf(status.Internal, "issue getting network serial from store: %s", err)
}
return serial, nil
}
func (s *SqlStore) GetPeerByPeerPubKey(ctx context.Context, lockStrength LockingStrength, peerKey string) (*nbpeer.Peer, error) {
tx := s.db
if lockStrength != LockingStrengthNone {
+4
View File
@@ -188,6 +188,10 @@ type Store interface {
GetTakenIPs(ctx context.Context, lockStrength LockingStrength, accountId string) ([]net.IP, error)
IncrementNetworkSerial(ctx context.Context, accountId string) error
GetAccountNetwork(ctx context.Context, lockStrength LockingStrength, accountId string) (*types.Network, error)
// GetAccountNetworkSerial returns only the network.serial column; used by
// the Sync fast path to skip the full-row GetAccountNetwork read when all
// we need is the serial.
GetAccountNetworkSerial(ctx context.Context, lockStrength LockingStrength, accountId string) (uint64, error)
GetInstallationID() string
SaveInstallationID(ctx context.Context, ID string) error
+15
View File
@@ -1032,6 +1032,21 @@ func (mr *MockStoreMockRecorder) GetAccountNetwork(ctx, lockStrength, accountId
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccountNetwork", reflect.TypeOf((*MockStore)(nil).GetAccountNetwork), ctx, lockStrength, accountId)
}
// GetAccountNetworkSerial mocks base method.
func (m *MockStore) GetAccountNetworkSerial(ctx context.Context, lockStrength LockingStrength, accountId string) (uint64, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetAccountNetworkSerial", ctx, lockStrength, accountId)
ret0, _ := ret[0].(uint64)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetAccountNetworkSerial indicates an expected call of GetAccountNetworkSerial.
func (mr *MockStoreMockRecorder) GetAccountNetworkSerial(ctx, lockStrength, accountId interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccountNetworkSerial", reflect.TypeOf((*MockStore)(nil).GetAccountNetworkSerial), ctx, lockStrength, accountId)
}
// GetAccountNetworks mocks base method.
func (m *MockStore) GetAccountNetworks(ctx context.Context, lockStrength LockingStrength, accountID string) ([]*types1.Network, error) {
m.ctrl.T.Helper()