Skip to content
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

Store latest prices into redis cache #2321

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions node/pkg/common/keys/keys.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package keys

import (
"strconv"
)

func SubmissionDataStreamKey(name string) string {
return "submissionDataStream:" + name
}

func FeedData(feedID int32) string {
return "feedData:" + strconv.Itoa(int(feedID))
}
34 changes: 34 additions & 0 deletions node/pkg/common/types/types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package types

import (
"context"
"encoding/json"
"fmt"
"sync"
"time"

"bisonai.com/miko/node/pkg/common/keys"
"bisonai.com/miko/node/pkg/db"
)

type Proxy struct {
Expand Down Expand Up @@ -75,6 +79,36 @@ func (m *LatestFeedDataMap) GetLatestFeedData(feedIds []int32) ([]*FeedData, err
result = append(result, feedData)
}
}

return result, nil
}

func (m *LatestFeedDataMap) GetLatestFeedDataFromCache(ctx context.Context, feedIds []int32) ([]*FeedData, error) {
if len(feedIds) == 0 {
return nil, nil
}

queryingKeys := make([]string, 0, len(feedIds))
for _, feedId := range feedIds {
queryingKeys = append(queryingKeys, keys.FeedData(feedId))
}
nick-bisonai marked this conversation as resolved.
Show resolved Hide resolved

result, err := db.MGetObject[*FeedData](ctx, queryingKeys)
if err != nil {
return nil, err
}
Comment on lines +96 to +99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle potential nil result from db.MGetObject

Ensure that the result from db.MGetObject is not nil before proceeding to prevent possible nil pointer dereferences.

Apply this diff to safeguard against nil results:

result, err := db.MGetObject[*FeedData](ctx, queryingKeys)
if err != nil {
	return nil, err
}
+if result == nil {
+	result = []*FeedData{}
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
result, err := db.MGetObject[*FeedData](ctx, queryingKeys)
if err != nil {
return nil, err
}
result, err := db.MGetObject[*FeedData](ctx, queryingKeys)
if err != nil {
return nil, err
}
if result == nil {
result = []*FeedData{}
}


if len(result) != 0 {
err = m.SetLatestFeedData(result)
if err != nil {
return nil, err
}
}
Comment on lines +101 to +106
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Potential deadlock risk in cache update.

The SetLatestFeedData method acquires a write lock while GetLatestFeedDataFromCache might be called concurrently. Consider updating the local cache asynchronously or ensuring proper lock ordering to prevent potential deadlocks.

Suggested fix:

 	if len(result) != 0 {
-		err = m.SetLatestFeedData(result)
-		if err != nil {
-			return nil, err
-		}
+		go func(data []*FeedData) {
+			if err := m.SetLatestFeedData(data); err != nil {
+				// Consider logging the error
+			}
+		}(result)
 	}

Committable suggestion skipped: line range outside the PR's diff.


if result == nil {
result = []*FeedData{}
}

return result, nil
}

Expand Down
8 changes: 7 additions & 1 deletion node/pkg/fetcher/localaggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,11 @@ func (c *LocalAggregator) collect(ctx context.Context) ([]*FeedData, error) {
for i, feed := range c.Feeds {
feedIds[i] = feed.ID
}
return c.latestFeedDataMap.GetLatestFeedData(feedIds)

result, err := c.latestFeedDataMap.GetLatestFeedData(feedIds)
if err != nil || len(result) == 0 {
return c.latestFeedDataMap.GetLatestFeedDataFromCache(ctx, feedIds)
}

return result, nil
}
16 changes: 16 additions & 0 deletions node/pkg/websocketfetcher/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"bisonai.com/miko/node/pkg/chain/websocketchainreader"
"bisonai.com/miko/node/pkg/common/keys"
"bisonai.com/miko/node/pkg/common/types"
"bisonai.com/miko/node/pkg/db"
"bisonai.com/miko/node/pkg/websocketfetcher/common"
Expand Down Expand Up @@ -43,6 +44,7 @@ import (
const (
DefaultStoreInterval = 200 * time.Millisecond
DefaultBufferSize = 3000
warmCacheTTL = time.Minute
nick-bisonai marked this conversation as resolved.
Show resolved Hide resolved
)

type AppConfig struct {
Expand Down Expand Up @@ -303,7 +305,21 @@ func (a *App) storeFeedData(ctx context.Context) {
if err != nil {
log.Error().Err(err).Msg("error in setting latest feed data")
}

err = a.StoreIntoRedisCache(ctx, batch)
if err != nil {
log.Error().Err(err).Msg("error in storing into redis cache")
}
default:
return
}
}

func (a *App) StoreIntoRedisCache(ctx context.Context, batch []*types.FeedData) error {
batchStoreEntries := make(map[string]any)
for _, feedData := range batch {
batchStoreEntries[keys.FeedData(feedData.FeedID)] = feedData
}

return db.MSetObjectWithExp(ctx, batchStoreEntries, warmCacheTTL)
}
nick-bisonai marked this conversation as resolved.
Show resolved Hide resolved