-
Notifications
You must be signed in to change notification settings - Fork 504
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
exp/lighthorizon: Isolate cursor advancement code to its own interface #4484
Merged
Shaptic
merged 8 commits into
stellar:lighthorizon
from
Shaptic:lighthorizon_fixCursors
Jul 28, 2022
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
11ca28d
Minor cleanup to constants, add some docstrings
Shaptic 869a2f9
Move cursor manipulation code to a separate interface
Shaptic 1c4c7f9
Undo rename
Shaptic 1b8a332
Rename to not be specific to transactions
Shaptic b53237f
Add explanations to test cases
Shaptic 0e9e0d9
Small test refactor to improve readability and long-running lines
Shaptic ec42eae
Combine tx and op tests into subtests
Shaptic 0f0b519
Fix how IndexStore is mocked out
Shaptic 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package services | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/stellar/go/exp/lighthorizon/index" | ||
"github.com/stellar/go/toid" | ||
) | ||
|
||
// CursorManager describes a way to control how a cursor advances for a | ||
// particular indexing strategy. | ||
type CursorManager interface { | ||
Begin(cursor int64) (int64, error) | ||
Advance() (int64, error) | ||
} | ||
|
||
type AccountActivityCursorManager struct { | ||
AccountId string | ||
|
||
store index.Store | ||
lastCursor *toid.ID | ||
} | ||
|
||
func NewCursorManagerForAccountActivity(store index.Store, accountId string) *AccountActivityCursorManager { | ||
return &AccountActivityCursorManager{AccountId: accountId, store: store} | ||
} | ||
|
||
func (c *AccountActivityCursorManager) Begin(cursor int64) (int64, error) { | ||
freq := checkpointManager.GetCheckpointFrequency() | ||
id := toid.Parse(cursor) | ||
lastCheckpoint := uint32(0) | ||
if id.LedgerSequence >= int32(checkpointManager.GetCheckpointFrequency()) { | ||
lastCheckpoint = index.GetCheckpointNumber(uint32(id.LedgerSequence)) | ||
} | ||
|
||
fmt.Println("got", id.LedgerSequence, "checkpoint #", lastCheckpoint) | ||
|
||
// We shouldn't take the provided cursor for granted: instead, we should | ||
// skip ahead to the first active ledger that's >= the given cursor. | ||
// | ||
// For example, someone might say ?cursor=0 but the first active checkpoint | ||
// is actually 40M ledgers in. | ||
firstCheckpoint, err := c.store.NextActive(c.AccountId, allTransactionsIndex, lastCheckpoint) | ||
if err != nil { | ||
return cursor, err | ||
} | ||
|
||
nextLedger := (firstCheckpoint - 1) * freq | ||
|
||
// However, if the given cursor is actually *more* specific than the index | ||
// can give us (e.g. somewhere *within* an active checkpoint range), prefer | ||
// it rather than starting over. | ||
if nextLedger < uint32(id.LedgerSequence) { | ||
better := toid.Parse(cursor) | ||
c.lastCursor = &better | ||
return cursor, nil | ||
} | ||
|
||
c.lastCursor = toid.New(int32(nextLedger), 1, 1) | ||
return c.lastCursor.ToInt64(), nil | ||
} | ||
|
||
func (c *AccountActivityCursorManager) Advance() (int64, error) { | ||
if c.lastCursor == nil { | ||
panic("invalid cursor, call Begin() first") | ||
} | ||
|
||
// Advancing the cursor means deciding whether or not we need to query the | ||
// index. | ||
|
||
lastLedger := uint32(c.lastCursor.LedgerSequence) | ||
freq := checkpointManager.GetCheckpointFrequency() | ||
|
||
if checkpointManager.IsCheckpoint(lastLedger) { | ||
// If the last cursor we looked at was a checkpoint ledger, then we need | ||
// to jump ahead to the next checkpoint. | ||
checkpoint := index.GetCheckpointNumber(uint32(c.lastCursor.LedgerSequence)) | ||
checkpoint, err := c.store.NextActive(c.AccountId, allTransactionsIndex, checkpoint+1) | ||
|
||
if err != nil { | ||
return c.lastCursor.ToInt64(), err | ||
} | ||
|
||
// We add a -1 here because an active checkpoint indicates that an | ||
// account had activity in the *previous* 64 ledgers, so we need to | ||
// backtrack to that ledger range. | ||
c.lastCursor = toid.New(int32((checkpoint-1)*freq), 1, 1) | ||
} else { | ||
// Otherwise, we can just bump the ledger number. | ||
c.lastCursor = toid.New(int32(lastLedger+1), 1, 1) | ||
} | ||
|
||
return c.lastCursor.ToInt64(), nil | ||
} | ||
|
||
var _ CursorManager = (*AccountActivityCursorManager)(nil) // ensure conformity to the interface |
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,74 @@ | ||
package services | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/stellar/go/exp/lighthorizon/index" | ||
"github.com/stellar/go/historyarchive" | ||
"github.com/stellar/go/keypair" | ||
"github.com/stellar/go/toid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
checkpointMgr = historyarchive.NewCheckpointManager(0) | ||
) | ||
|
||
func TestAccountTransactionCursorManager(t *testing.T) { | ||
freq := int32(checkpointMgr.GetCheckpointFrequency()) | ||
accountId := keypair.MustRandom().Address() | ||
|
||
// Create an index and fill it with some checkpoint details. | ||
store, err := index.NewFileStore(index.StoreConfig{ | ||
Shaptic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Url: "file://" + t.TempDir(), | ||
Workers: 4, | ||
}) | ||
require.NoError(t, err) | ||
|
||
for _, checkpoint := range []uint32{ | ||
1, 5, 10, | ||
} { | ||
require.NoError(t, store.AddParticipantsToIndexes( | ||
checkpoint, allTransactionsIndex, []string{accountId})) | ||
} | ||
|
||
cursorMgr := NewCursorManagerForAccountActivity(store, accountId) | ||
|
||
cursor := toid.New(1, 1, 1) | ||
var nextCursor int64 | ||
|
||
nextCursor, err = cursorMgr.Begin(cursor.ToInt64()) | ||
require.NoError(t, err) | ||
assert.EqualValues(t, 1, getLedgerFromCursor(nextCursor)) | ||
|
||
cursor.LedgerSequence = freq / 2 | ||
nextCursor, err = cursorMgr.Begin(cursor.ToInt64()) | ||
require.NoError(t, err) | ||
assert.EqualValues(t, cursor.LedgerSequence, getLedgerFromCursor(nextCursor)) | ||
|
||
cursor.LedgerSequence = 2 * freq | ||
nextCursor, err = cursorMgr.Begin(cursor.ToInt64()) | ||
require.NoError(t, err) | ||
assert.EqualValues(t, 4*freq, getLedgerFromCursor(nextCursor)) | ||
|
||
for i := int32(1); i < freq; i++ { | ||
nextCursor, err = cursorMgr.Advance() | ||
require.NoError(t, err) | ||
assert.EqualValues(t, 4*freq+i, getLedgerFromCursor(nextCursor)) | ||
} | ||
|
||
nextCursor, err = cursorMgr.Advance() | ||
require.NoError(t, err) | ||
assert.EqualValues(t, 9*freq, getLedgerFromCursor(nextCursor)) | ||
|
||
for i := int32(1); i < freq; i++ { | ||
nextCursor, err = cursorMgr.Advance() | ||
require.NoError(t, err) | ||
assert.EqualValues(t, 9*freq+i, getLedgerFromCursor(nextCursor)) | ||
} | ||
|
||
_, err = cursorMgr.Advance() | ||
assert.ErrorIs(t, err, io.EOF) | ||
} |
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
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.
would Low from
checkpointManager.GetCheckpointRange(firstCheckpoint)
work here instead of manually doing the math?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.
Unfortunately, no. The checkpoint values provided by the index represent the checkpoint number, whereas checkpointManager works on ledgers. To clarify the difference: if the index returns 2, that means the ledger range is
[64, 127]
. So the upscale byfreq
is always necessary to do manually.