Skip to content

refactor: created the local cache instance only once #1233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 26 additions & 39 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,17 @@ type cachedData struct {
}

type LocalCache struct {
stop chan struct{}

wg sync.WaitGroup
mu sync.RWMutex
URLs map[string]cachedData //URLs
}

func NewLocalCache(cleanupInterval time.Duration) *LocalCache {
lc := &LocalCache{
// NewLocalCache creates a new LocalCache instance
func NewLocalCache() *LocalCache {
return &LocalCache{
URLs: make(map[string]cachedData),
stop: make(chan struct{}),
}

lc.wg.Add(1)
go func(cleanupInterval time.Duration) {
defer lc.wg.Done()
lc.cleanupLoop(cleanupInterval)
}(cleanupInterval)

return lc
}

func (lc *LocalCache) cleanupLoop(interval time.Duration) {
t := time.NewTicker(interval)
defer t.Stop()

for {
select {
case <-lc.stop:
return
case <-t.C:
lc.mu.Lock()
for url, cu := range lc.URLs {
if cu.expireAtTimestamp <= time.Now().Unix() {
delete(lc.URLs, url)
}
}
lc.mu.Unlock()
}
}
}

func (lc *LocalCache) StopCleanup() {
close(lc.stop)
lc.wg.Wait()
}

func (lc *LocalCache) Update(data []byte, url string, expireAtTimestamp int64) {
lc.mu.Lock()
defer lc.mu.Unlock()
Expand All @@ -80,3 +44,26 @@ func (lc *LocalCache) Read(url string) ([]byte, bool) {

return cacheData.Result, true
}

// ClearAll deletes all entries in the cache
func (lc *LocalCache) ClearAll() {
lc.mu.Lock()
defer lc.mu.Unlock()

for key := range lc.URLs {
delete(lc.URLs, key)
}
}

// Cleanup removes expired cache entries
func (lc *LocalCache) Cleanup() {
lc.mu.Lock()
defer lc.mu.Unlock()

for url, data := range lc.URLs {
// Remove expired data after the expireAtTimestamp is passed
if data.expireAtTimestamp <= time.Now().Unix() {
delete(lc.URLs, url)
}
}
}
12 changes: 3 additions & 9 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ package cmd
import (
"encoding/hex"
"errors"
Types "github.com/ethereum/go-ethereum/core/types"
"math/big"
"razor/cache"
"razor/core"
"razor/core/types"
"razor/pkg/bindings"
"razor/utils"
"sync"
"time"

Types "github.com/ethereum/go-ethereum/core/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
Expand Down Expand Up @@ -79,8 +76,8 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se

var wg sync.WaitGroup

log.Debug("Creating a local cache which will store API result and expire at the end of commit state")
commitParams.LocalCache = cache.NewLocalCache(time.Second * time.Duration(core.StateLength))
// Clean up any expired API results cache data before performing the commit
commitParams.LocalCache.Cleanup()

log.Debug("Iterating over all the collections...")
for i := 0; i < int(numActiveCollections); i++ {
Expand Down Expand Up @@ -129,7 +126,6 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
if err != nil {
// Returning the first error from the error channel
log.Error("Error in getting collection data: ", err)
commitParams.LocalCache.StopCleanup()
return types.CommitData{}, err
}
}
Expand All @@ -139,8 +135,6 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
log.Debug("HandleCommitState: SeqAllottedCollections: ", seqAllottedCollections)
log.Debug("HandleCommitState: Leaves: ", leavesOfTree)

commitParams.LocalCache.StopCleanup()

return types.CommitData{
AssignedCollections: assignedCollections,
SeqAllottedCollections: seqAllottedCollections,
Expand Down
5 changes: 2 additions & 3 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"razor/utils"
"reflect"
"testing"
"time"
)

func TestCommit(t *testing.T) {
Expand Down Expand Up @@ -226,7 +225,7 @@ func TestHandleCommitState(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
localCache := cache.NewLocalCache(time.Second * 10)
localCache := cache.NewLocalCache()
commitParams := &types.CommitParams{
LocalCache: localCache,
}
Expand Down Expand Up @@ -396,7 +395,7 @@ func BenchmarkHandleCommitState(b *testing.B) {
for _, v := range table {
b.Run(fmt.Sprintf("Number_Of_Active_Collections%d", v.numActiveCollections), func(b *testing.B) {
for i := 0; i < b.N; i++ {
localCache := cache.NewLocalCache(time.Second * 10)
localCache := cache.NewLocalCache()
commitParams := &types.CommitParams{
LocalCache: localCache,
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os/signal"
"path/filepath"
"razor/accounts"
"razor/cache"
"razor/core"
"razor/core/types"
"razor/logger"
Expand Down Expand Up @@ -118,6 +119,7 @@ func (*UtilsStruct) ExecuteVote(flagSet *pflag.FlagSet) {
utils.CheckError("Error in initializing asset cache: ", err)

commitParams := &types.CommitParams{
LocalCache: cache.NewLocalCache(), // Creating a local cache which will store API results
JobsCache: jobsCache,
CollectionsCache: collectionsCache,
HttpClient: httpClient,
Expand Down Expand Up @@ -421,6 +423,9 @@ func (*UtilsStruct) InitiateCommit(client *ethclient.Client, config types.Config
log.Debug("Updating GlobalCommitDataStruct with latest commitData and epoch...")
updateGlobalCommitDataStruct(commitData, epoch)
log.Debugf("InitiateCommit: Global commit data struct: %+v", globalCommitDataStruct)

log.Debug("Clearing up the cache storing API results after successful commit...")
commitParams.LocalCache.ClearAll()
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions cmd/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ func TestInitiateCommit(t *testing.T) {
)

commitParams := &types.CommitParams{
LocalCache: cache.NewLocalCache(),
JobsCache: cache.NewJobsCache(),
CollectionsCache: cache.NewCollectionsCache(),
FromBlockToCheckForEvents: big.NewInt(1),
Expand Down
2 changes: 1 addition & 1 deletion utils/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetDataFromAPI(commitParams *types.CommitParams, dataSourceURLStruct types.
return nil, err
}

// Storing the data into cache
// Storing the API results data into cache
commitParams.LocalCache.Update(response, cacheKey, time.Now().Add(time.Second*time.Duration(core.StateLength)).Unix())
return response, nil
}
Expand Down
2 changes: 1 addition & 1 deletion utils/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestGetDataFromAPI(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
localCache := cache.NewLocalCache(time.Second * 10)
localCache := cache.NewLocalCache()
commitParams := &types.CommitParams{
LocalCache: localCache,
HttpClient: httpClient,
Expand Down
4 changes: 2 additions & 2 deletions utils/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ func TestGetDataToCommitFromJobs(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
UtilsInterface = &UtilsStruct{}
commitParams := &types.CommitParams{
LocalCache: cache.NewLocalCache(time.Second * 10),
LocalCache: cache.NewLocalCache(),
HttpClient: httpClient,
}

Expand Down Expand Up @@ -787,7 +787,7 @@ func TestGetDataToCommitFromJob(t *testing.T) {
utils := StartRazor(optionsPackageStruct)

commitParams := &types.CommitParams{
LocalCache: cache.NewLocalCache(time.Second * 10),
LocalCache: cache.NewLocalCache(),
HttpClient: httpClient,
}

Expand Down
Loading