-
Notifications
You must be signed in to change notification settings - Fork 8
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
Mkysel/payer blocking #441
Draft
mkysel
wants to merge
6
commits into
main
Choose a base branch
from
mkysel/payer-blocking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+580
−5
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package metadata_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xmtp/xmtpd/pkg/proto/xmtpv4/metadata_api" | ||
"testing" | ||
"time" | ||
|
||
"github.com/xmtp/xmtpd/pkg/api/message" | ||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
"github.com/xmtp/xmtpd/pkg/testutils" | ||
testUtilsApi "github.com/xmtp/xmtpd/pkg/testutils/api" | ||
envelopeTestUtils "github.com/xmtp/xmtpd/pkg/testutils/envelopes" | ||
"github.com/xmtp/xmtpd/pkg/topic" | ||
) | ||
|
||
var ( | ||
topicA = topic.NewTopic(topic.TOPIC_KIND_GROUP_MESSAGES_V1, []byte("topicA")).Bytes() | ||
topicB = topic.NewTopic(topic.TOPIC_KIND_GROUP_MESSAGES_V1, []byte("topicB")).Bytes() | ||
) | ||
var allRows []queries.InsertGatewayEnvelopeParams | ||
|
||
func setupTest( | ||
t *testing.T, | ||
) (metadata_api.MetadataApiClient, *sql.DB, testUtilsApi.ApiServerMocks, func()) { | ||
allRows = []queries.InsertGatewayEnvelopeParams{ | ||
// Initial rows | ||
{ | ||
OriginatorNodeID: 1, | ||
OriginatorSequenceID: 1, | ||
Topic: topicA, | ||
OriginatorEnvelope: testutils.Marshal( | ||
t, | ||
envelopeTestUtils.CreateOriginatorEnvelopeWithTopic(t, 1, 1, topicA), | ||
), | ||
}, | ||
{ | ||
OriginatorNodeID: 2, | ||
OriginatorSequenceID: 1, | ||
Topic: topicA, | ||
OriginatorEnvelope: testutils.Marshal( | ||
t, | ||
envelopeTestUtils.CreateOriginatorEnvelopeWithTopic(t, 2, 1, topicA), | ||
), | ||
}, | ||
// Later rows | ||
{ | ||
OriginatorNodeID: 1, | ||
OriginatorSequenceID: 2, | ||
Topic: topicB, | ||
OriginatorEnvelope: testutils.Marshal( | ||
t, | ||
envelopeTestUtils.CreateOriginatorEnvelopeWithTopic(t, 1, 2, topicB), | ||
), | ||
}, | ||
{ | ||
OriginatorNodeID: 2, | ||
OriginatorSequenceID: 2, | ||
Topic: topicB, | ||
OriginatorEnvelope: testutils.Marshal( | ||
t, | ||
envelopeTestUtils.CreateOriginatorEnvelopeWithTopic(t, 2, 2, topicB), | ||
), | ||
}, | ||
{ | ||
OriginatorNodeID: 1, | ||
OriginatorSequenceID: 3, | ||
Topic: topicA, | ||
OriginatorEnvelope: testutils.Marshal( | ||
t, | ||
envelopeTestUtils.CreateOriginatorEnvelopeWithTopic(t, 1, 3, topicA), | ||
), | ||
}, | ||
} | ||
|
||
return testUtilsApi.NewTestMetadataAPIClient(t) | ||
} | ||
|
||
func insertInitialRows(t *testing.T, store *sql.DB) { | ||
testutils.InsertGatewayEnvelopes(t, store, []queries.InsertGatewayEnvelopeParams{ | ||
allRows[0], allRows[1], | ||
}) | ||
time.Sleep(message.SubscribeWorkerPollTime + 100*time.Millisecond) | ||
} | ||
|
||
func insertAdditionalRows(t *testing.T, store *sql.DB, notifyChan ...chan bool) { | ||
testutils.InsertGatewayEnvelopes(t, store, []queries.InsertGatewayEnvelopeParams{ | ||
allRows[2], allRows[3], allRows[4], | ||
}, notifyChan...) | ||
} | ||
|
||
func TestGetCursorBasic(t *testing.T) { | ||
client, db, _, cleanup := setupTest(t) | ||
defer cleanup() | ||
insertInitialRows(t, db) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
cursor, err := client.GetSyncCursor(ctx, &metadata_api.GetSyncCursorRequest{}) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, cursor) | ||
|
||
expectedCursor := map[uint32]uint64{ | ||
1: 1, | ||
2: 1, | ||
} | ||
|
||
require.Equal(t, expectedCursor, cursor.LatestSync.NodeIdToSequenceId) | ||
|
||
insertAdditionalRows(t, db) | ||
require.Eventually(t, func() bool { | ||
expectedCursor := map[uint32]uint64{ | ||
1: 3, | ||
2: 2, | ||
} | ||
|
||
cursor, err := client.GetSyncCursor(ctx, &metadata_api.GetSyncCursorRequest{}) | ||
if err != nil { | ||
t.Logf("Error fetching sync cursor: %v", err) | ||
return false | ||
} | ||
if cursor == nil { | ||
t.Log("Cursor is nil") | ||
return false | ||
} | ||
|
||
return assert.ObjectsAreEqual(expectedCursor, cursor.LatestSync.NodeIdToSequenceId) | ||
}, 500*time.Millisecond, 50*time.Millisecond) | ||
} | ||
|
||
func TestSubscribeSyncCursorBasic(t *testing.T) { | ||
client, db, _, cleanup := setupTest(t) | ||
defer cleanup() | ||
insertInitialRows(t, db) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
stream, err := client.SubscribeSyncCursor(ctx, &metadata_api.GetSyncCursorRequest{}) | ||
require.NoError(t, err) | ||
require.NotNil(t, stream) | ||
|
||
firstUpdate, err := stream.Recv() | ||
require.NoError(t, err) | ||
require.NotNil(t, firstUpdate) | ||
|
||
expectedCursor := map[uint32]uint64{ | ||
1: 1, | ||
2: 1, | ||
} | ||
|
||
require.Equal(t, expectedCursor, firstUpdate.LatestSync.NodeIdToSequenceId) | ||
|
||
insertAdditionalRows(t, db) | ||
|
||
require.Eventually(t, func() bool { | ||
expectedCursor := map[uint32]uint64{ | ||
1: 3, | ||
2: 2, | ||
} | ||
|
||
update, err := stream.Recv() | ||
if err != nil { | ||
t.Logf("Error receiving sync cursor update: %v", err) | ||
return false | ||
} | ||
if update == nil { | ||
t.Log("Received nil update from stream") | ||
return false | ||
} | ||
|
||
return assert.ObjectsAreEqual(expectedCursor, update.LatestSync.NodeIdToSequenceId) | ||
}, 500*time.Millisecond, 50*time.Millisecond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package payer | ||
|
||
import ( | ||
"context" | ||
"github.com/xmtp/xmtpd/pkg/proto/xmtpv4/metadata_api" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type MetadataApiClientConstructor interface { | ||
NewMetadataApiClient(nodeId uint32) (metadata_api.MetadataApiClient, error) | ||
} | ||
type DefaultMetadataApiClientConstructor struct { | ||
clientManager *ClientManager | ||
} | ||
|
||
func (c *DefaultMetadataApiClientConstructor) NewMetadataApiClient( | ||
nodeId uint32, | ||
) (metadata_api.MetadataApiClient, error) { | ||
conn, err := c.clientManager.GetClient(nodeId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return metadata_api.NewMetadataApiClient(conn), nil | ||
} | ||
|
||
type NodeCursorTracker struct { | ||
ctx context.Context | ||
log *zap.Logger | ||
metadataApiClient MetadataApiClientConstructor | ||
} | ||
|
||
func NewNodeCursorTracker(ctx context.Context, | ||
log *zap.Logger, metadataApiClient MetadataApiClientConstructor) *NodeCursorTracker { | ||
return &NodeCursorTracker{ctx: ctx, log: log, metadataApiClient: metadataApiClient} | ||
} | ||
|
||
func (ct *NodeCursorTracker) BlockUntilDesiredCursorReached( | ||
ctx context.Context, | ||
nodeId uint32, | ||
desiredOriginatorId uint32, | ||
desiredSequenceId uint64, | ||
) error { | ||
// TODO(mkysel) ideally we wouldn't create and tear down the stream for every request | ||
|
||
client, err := ct.metadataApiClient.NewMetadataApiClient(nodeId) | ||
if err != nil { | ||
return err | ||
} | ||
stream, err := client.SubscribeSyncCursor(ctx, &metadata_api.GetSyncCursorRequest{}) | ||
if err != nil { | ||
return err | ||
} | ||
for { | ||
select { | ||
case <-ct.ctx.Done(): | ||
// server is shutting down | ||
return status.Errorf(codes.Canceled, "node terminated. Cancelled wait for cursor") | ||
case <-ctx.Done(): | ||
// client has shut down | ||
return nil | ||
default: | ||
resp, err := stream.Recv() | ||
if err != nil { | ||
if status.Code(err) == codes.Canceled { | ||
return nil | ||
} | ||
// TODO(mkysel): proper handling of failures | ||
return err | ||
} | ||
if err != nil || resp == nil || resp.LatestSync == nil { | ||
return status.Errorf(codes.Internal, "error getting node cursor: %v", err) | ||
} | ||
derefMap := resp.LatestSync.NodeIdToSequenceId | ||
seqId, exists := derefMap[desiredOriginatorId] | ||
if !exists { | ||
continue // Wait for the originator ID to appear | ||
} | ||
|
||
// Check if the sequence ID has reached the desired value | ||
if seqId >= desiredSequenceId { | ||
return nil // Desired state achieved | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider adding stream cleanup.
The stream created by
SubscribeSyncCursor
should be properly closed to prevent resource leaks.stream, err := client.SubscribeSyncCursor(ctx, &metadata_api.GetSyncCursorRequest{}) if err != nil { return err } + defer stream.CloseSend()
📝 Committable suggestion