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

TXM In-memory: add priority queue: STEP 1 #12121

Closed
wants to merge 25 commits into from

Conversation

poopoothegorilla
Copy link
Contributor

@poopoothegorilla poopoothegorilla commented Feb 21, 2024

This PR is the start of a chain of PRs to make the review process easier. The PR adds a priority queue that will be used by the in-memory TXM.

NOTES:

Copy link
Contributor

I see that you haven't updated any README files. Would it make sense to do so?

Copy link
Contributor

I see that you haven't updated any CHANGELOG files. Would it make sense to do so?

Comment on lines 49 to 52
pq.Lock()
defer pq.Unlock()

return heap.Pop(pq).(*txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE])
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to capture these low level ops as a separate clean generic heap, without the tx queue specifics? Then this can just have a field of type Heap[*txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE] without mixing in explicit casts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm... I might not be understanding what you are proposing fully. We will need a method which has the ability to remove a Tx by ID (for pruning purposes)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lets hold off on refactoring to make more generic for a future PR after all this stuff gets merged

Copy link
Collaborator

@patrick-dowell patrick-dowell left a comment

Choose a reason for hiding this comment

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

Overall this looks reasonable, but I notice a lot of redundant code, mainly I think because TxPriorityQueue is thinly wrapping PriorityQueue. I wonder if it'd be a little cleaner to just collapse these two files together and save some code lines / complexity.

common/txmgr/priority_queue.go Outdated Show resolved Hide resolved
Copy link
Contributor

@prashantkumar1982 prashantkumar1982 left a comment

Choose a reason for hiding this comment

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

I think this is good to go.
Please add unit-tests and mark this PR as Ready for Review.
And lets merge it as soon as you get 2 approvals.

pq.Lock()

if pq.ph.Len() == pq.ph.Cap() {
heap.Pop(pq.ph)
Copy link
Contributor

Choose a reason for hiding this comment

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

Restating my understanding:

priorityHeap is meant to store tx ordered by creation time, with the oldest tx at index 0, and the newest tx at index length - 1.

The current implementation of Pop on priorityHeap:

func (pq *priorityHeap[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) Pop() any {
	old := pq.txs
	n := len(old)
	tx := old[n-1]
	old[n-1] = nil // avoid memory leak
	pq.txs = old[0 : n-1]
	delete(pq.idToIndex, tx.ID)
	return tx
}

Here, IFIUC, we remove the tx at index length - 1 from both the array and the index map, which is the tx with the most recent creation time.

Then, in AddTx, we have

	if pq.ph.Len() == pq.ph.Cap() {
		heap.Pop(pq.ph)
        }

So, when the queue is full, doesn't the current code remove the newest transaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is using the heap pkg... so that Pop is actually https://cs.opensource.google/go/go/+/refs/tags/go1.22.1:src/container/heap/heap.go;l=59

So it should take the item that is next on the Heap

dhaidashenko
dhaidashenko previously approved these changes Mar 21, 2024
Copy link
Contributor

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Copy link
Contributor

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale label Aug 29, 2024
@github-actions github-actions bot closed this Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants