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

mysql: use a stable unique ID for stats #546

Merged
merged 2 commits into from
Apr 25, 2022
Merged

mysql: use a stable unique ID for stats #546

merged 2 commits into from
Apr 25, 2022

Conversation

jakedt
Copy link
Member

@jakedt jakedt commented Apr 20, 2022

No description provided.

@jakedt jakedt requested a review from vroldanbet April 20, 2022 14:50
@jakedt jakedt requested a review from a team as a code owner April 20, 2022 14:50
@github-actions github-actions bot added the area/datastore Affects the storage system label Apr 20, 2022
Comment on lines 16 to 13
func insertUniqueID(driver *MySQLDriver) string {
return fmt.Sprintf(`INSERT INTO %s (unique_id) VALUES ("%s");`,
driver.Metadata(),
uuid.NewString(),
)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We put special care into the design of mysql's datastore migrations to be data-migration free, as that is incompatible with migration tooling used in our production systems (skeema).

My suggestion is to follow the same strategy used for "tuple transaction zero": to insert on runtime. The PR #539 offers a good foundation to do this, and there is a pattern that guarantees there is no race (i.e. several spicedb processes attempting to insert at the same time on startup).

@github-actions github-actions bot added the area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools) label Apr 22, 2022
@@ -491,6 +498,20 @@ func (mds *Datastore) seedDatabase(ctx context.Context) error {
return fmt.Errorf("seedDatabase: failed to get last inserted id: %w", err)
}

uuidSQL, uuidArgs, err := sb.
Copy link
Contributor

Choose a reason for hiding this comment

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

if multiple SpiceDB processes attempt to do this there will be most likely a race, and multiple rows will be inserted. You could solve this by adding an ID column and inserting on a known ID (e.g. zero), and use INSERT IGNORE. That's the strategy used with the zeroth transaction.

@jakedt
Copy link
Member Author

jakedt commented Apr 22, 2022

Updated

vroldanbet
vroldanbet previously approved these changes Apr 25, 2022
Copy link
Contributor

@vroldanbet vroldanbet left a comment

Choose a reason for hiding this comment

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

LGTM, left a remark on the seeding logic being interleaved.

Comment on lines 501 to 521
uuidSQL, uuidArgs, err := sb.
Insert(mds.driver.Metadata()).
Options("IGNORE").
Columns(metadataIDColumn, metadataUniqueIDColumn).
Values(0, uuid.NewString()).
ToSql()
if err != nil {
return fmt.Errorf("seedDatabase: failed to prepare SQL: %w", err)
}

_, err = tx.ExecContext(ctx, uuidSQL, uuidArgs...)
if err != nil {
return fmt.Errorf("seedDatabase: failed to insert unique ID: %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.

You probably want to move the whole "UUID seeding" block after want to if lastInsertID != noLastInsertID {, since it seems like interleaving 2 different concerns.

You may also want to do the same lastInsertID check so you can emit a log message indicating the UUID was successfully inserted

@jakedt
Copy link
Member Author

jakedt commented Apr 25, 2022

Update.

Copy link
Contributor

@vroldanbet vroldanbet left a comment

Choose a reason for hiding this comment

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

:shipit:

@jakedt jakedt enabled auto-merge April 25, 2022 14:10
@jakedt jakedt merged commit b8c6205 into main Apr 25, 2022
@jakedt jakedt deleted the stable-mysql-id branch April 25, 2022 14:28
@github-actions github-actions bot locked and limited conversation to collaborators Apr 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/datastore Affects the storage system area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants