Skip to content

Commit

Permalink
sql: fix epoch ATX ID cache deadlock (#5738)
Browse files Browse the repository at this point in the history
## Motivation

SQL query cache deadlocks b/c during transactions, SQLite lock is held while cache update lock is being acquired.
  • Loading branch information
ivan4th committed Mar 19, 2024
1 parent 891a74f commit a355025
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ See [RELEASE](./RELEASE.md) for workflow instructions.
* [#5735](https://github.com/spacemeshos/go-spacemesh/pull/5735) Don't hold database connections for long in fetcher
streaming mode

* [#5738](https://github.com/spacemeshos/go-spacemesh/pull/5738) sql: fix epoch ATX ID cache deadlock

## Release v1.4.1

### Improvements
Expand Down
1 change: 1 addition & 0 deletions activation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ func (h *Handler) storeAtx(ctx context.Context, atx *types.VerifiedActivationTx)
}); err != nil {
return nil, fmt.Errorf("store atx: %w", err)
}
atxs.AtxAdded(h.cdb, atx)
if proof != nil {
h.cdb.CacheMalfeasanceProof(atx.SmesherID, proof)
h.tortoise.OnMalfeasance(atx.SmesherID)
Expand Down
2 changes: 2 additions & 0 deletions fetch/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ func testHandleEpochInfoReqWithQueryCache(
for i := 0; i < 10; i++ {
vatx := newAtx(t, epoch)
require.NoError(t, atxs.Add(th.cdb, vatx))
atxs.AtxAdded(th.cdb, vatx)
expected.AtxIDs = append(expected.AtxIDs, vatx.ID())
}

Expand All @@ -379,6 +380,7 @@ func testHandleEpochInfoReqWithQueryCache(
// Add another ATX which should be appended to the cached slice
vatx := newAtx(t, epoch)
require.NoError(t, atxs.Add(th.cdb, vatx))
atxs.AtxAdded(th.cdb, vatx)
expected.AtxIDs = append(expected.AtxIDs, vatx.ID())
require.Equal(t, 12, qc.QueryCount())

Expand Down
6 changes: 5 additions & 1 deletion sql/atxs/atxs.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,13 @@ func Add(db sql.Executor, atx *types.VerifiedActivationTx) error {
if err != nil {
return fmt.Errorf("insert ATX ID %v: %w", atx.ID(), err)
}
return nil
}

// AtxAdded updates epoch query cache with new ATX, if the query cache is enabled.
func AtxAdded(db sql.Executor, atx *types.VerifiedActivationTx) {
epochCacheKey := sql.QueryCacheKey(CacheKindEpochATXs, atx.PublishEpoch.String())
sql.AppendToCachedSlice(db, epochCacheKey, atx.ID())
return nil
}

type Filter func(types.ATXID) bool
Expand Down
2 changes: 2 additions & 0 deletions sql/atxs/atxs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ func TestGetIDsByEpochCached(t *testing.T) {

for _, atx := range []*types.VerifiedActivationTx{atx1, atx2, atx3, atx4} {
require.NoError(t, atxs.Add(db, atx))
atxs.AtxAdded(db, atx)
}

require.Equal(t, 4, db.QueryCount())
Expand Down Expand Up @@ -475,6 +476,7 @@ func TestGetIDsByEpochCached(t *testing.T) {
atxs.Add(tx, atx5)
return nil
}))
atxs.AtxAdded(db, atx5)
require.Equal(t, 8, db.QueryCount())

ids3, err := atxs.GetIDsByEpoch(ctx, db, e3)
Expand Down

0 comments on commit a355025

Please sign in to comment.