Skip to content

Conversation

@winder
Copy link
Contributor

@winder winder commented Aug 19, 2021

Summary

Add a cache in the ParticipationRegistry to optimize asynchronous data access and updates.

Test Plan

Update unit tests.

@codecov-commenter
Copy link

codecov-commenter commented Aug 19, 2021

Codecov Report

Merging #2769 (bd9c508) into feature/partkey (d24bf1e) will increase coverage by 0.04%.
The diff coverage is 61.48%.

Impacted file tree graph

@@                 Coverage Diff                 @@
##           feature/partkey    #2769      +/-   ##
===================================================
+ Coverage            47.41%   47.46%   +0.04%     
===================================================
  Files                  351      352       +1     
  Lines                56498    56747     +249     
===================================================
+ Hits                 26790    26934     +144     
- Misses               26690    26786      +96     
- Partials              3018     3027       +9     
Impacted Files Coverage Δ
data/account/msgp_gen.go 34.09% <34.09%> (ø)
node/node.go 26.58% <66.66%> (-0.05%) ⬇️
data/account/participationRegistry.go 78.90% <83.66%> (+10.87%) ⬆️
data/account/participation.go 38.14% <87.50%> (+1.68%) ⬆️
network/wsPeer.go 72.14% <0.00%> (-3.07%) ⬇️
catchup/peerSelector.go 98.95% <0.00%> (-1.05%) ⬇️
catchup/service.go 69.35% <0.00%> (+0.77%) ⬆️
ledger/blockqueue.go 83.90% <0.00%> (+1.72%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d24bf1e...bd9c508. Read the comment docs.

@winder winder linked an issue Aug 20, 2021 that may be closed by this pull request
Copy link
Contributor

@tsachiherman tsachiherman left a comment

Choose a reason for hiding this comment

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

Overall looks great. Few small requests.

winder added 2 commits August 25, 2021 13:56
- add logger
- ParcipationRecord Duplicate function
- remove unused get function
- more tests regarding cache desynchronization
@winder winder requested a review from tsachiherman August 25, 2021 20:28
if _, ok := db.cache[id]; ok {
return ParticipationID{}, ErrAlreadyInserted
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can dodge around making the second query to get the primary key by analyzing the result from the first query ( which, btw, need to have it's result being tested ).
The result in the first query would have LastInsertId, which is the primary key you're after.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice tip about LastInsertId!

Is an insert that doesn't have an error insufficient error checking to know that an insert was performed? This is really tedious stuff:

		result, err := tx.Exec(
			insertKeysetQuery,
			id[:],
			record.Parent[:],
			record.FirstValid,
			record.LastValid,
			record.KeyDilution)
		if err != nil {
			return fmt.Errorf("unable to insert keyset: %w", err)
		}
		rows, err := result.RowsAffected()
		if err != nil {
			return fmt.Errorf("unable to insert keyset: %w", err)
		}
		if rows != 1 {
			return fmt.Errorf("unexpected number of rows")
		}
		pk, err := result.LastInsertId()
		if err != nil {
			return fmt.Errorf("unable to insert keyset: %w", err)
		}

Copy link
Contributor

Choose a reason for hiding this comment

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

The number of rows affected by an insert could be greater than one. I don't know if in sqlite there is such a use case, but in Postgres you can have ON CONFLICT which would make it a nop.

db.mutex.Lock()
defer db.mutex.Unlock()

matches := make([]ParticipationRecord, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

using

matches := make([]ParticipationRecord, 0, len(db.cache))

would avoid potential reallocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, we can do better. Anything but 1 match is an error so I can exit in the loop if a second is found.

@winder winder merged commit 65feafd into algorand:feature/partkey Aug 25, 2021
@winder winder deleted the will/registry-cache branch October 29, 2021 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ParticipationRegistry In-Memory Cache

3 participants