Skip to content

Commit

Permalink
Merge pull request #1448 from 0chain/fix/alloc-cache
Browse files Browse the repository at this point in the history
Load terms in repo and add on delete cascade
  • Loading branch information
dabasov authored Jun 29, 2024
2 parents c692dcb + 713bddd commit 82375a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
20 changes: 11 additions & 9 deletions code/go/0chain.net/blobbercore/allocation/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ func FetchAllocationFromEventsDB(ctx context.Context, allocationID string, alloc
}

if err == nil {
// load related terms
var terms []*Terms
err = tx.Model(terms).
Where("allocation_id = ?", a.ID).
Find(&terms).Error
if err != nil {
return nil, common.NewError("bad_db_operation", err.Error()) // unexpected DB error
if len(a.Terms) == 0 {
// load related terms
var terms []*Terms
err = tx.Model(terms).
Where("allocation_id = ?", a.ID).
Find(&terms).Error
if err != nil {
return nil, common.NewError("bad_db_operation", err.Error()) // unexpected DB error
}
a.Terms = terms // set field
}
a.Terms = terms // set field
return // found in DB
return // found in DB
}

sa, err := requestAllocation(allocationID)
Expand Down
7 changes: 6 additions & 1 deletion code/go/0chain.net/blobbercore/allocation/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
const (
SQLWhereGetById = "allocations.id = ?"
SQLWhereGetByTx = "allocations.tx = ?"
lruSize = 100
lruSize = 500
)

var (
Expand Down Expand Up @@ -141,6 +141,11 @@ func (r *Repository) GetByTx(ctx context.Context, allocationID, txHash string) (
cache[allocationID] = AllocationCache{
Allocation: alloc,
}
//get allocation terms
err = alloc.LoadTerms(ctx)
if err != nil {
return alloc, err
}
r.setAllocToGlobalCache(alloc)
return alloc, err
}
Expand Down
7 changes: 7 additions & 0 deletions goose/migrations/1718355236_terms_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- +goose Up
-- +goose StatementBegin
DELETE FROM terms WHERE allocation_id NOT IN (SELECT id FROM allocations);
ALTER TABLE ONLY terms DROP CONSTRAINT fk_terms_allocation;
ALTER TABLE ONLY terms ADD CONSTRAINT fk_terms_allocation foreign key (allocation_id) references allocations(id) ON DELETE CASCADE;
CREATE INDEX idx_terms_allocation_id ON terms USING HASH(allocation_id);
-- +goose StatementEnd

0 comments on commit 82375a2

Please sign in to comment.