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

better session handling #4

Merged
merged 3 commits into from
Jan 9, 2024
Merged

better session handling #4

merged 3 commits into from
Jan 9, 2024

Conversation

lavrukov
Copy link
Contributor

@lavrukov lavrukov commented Dec 26, 2023

If we received a TLI or BadSession inside a transaction, this means that the transaction was invalidated on the YDB side and sending commit() or rollback() is incorrect, since in this case they always generate a BadSession.

In order to write tests for this behavior that will work both on the real YDB and on the mock, we have to add a check for read locks inside the InMemoryRepository.

@lavrukov lavrukov force-pushed the better-session-handling branch 8 times, most recently from 4d73176 to a25c603 Compare December 28, 2023 00:58
@@ -863,6 +866,30 @@ public void schemaWithHint() {
repository.schema(HintAutoPartitioningByLoad.class).create();
}


Copy link
Collaborator

@nvamelichev nvamelichev Dec 28, 2023

Choose a reason for hiding this comment

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

todo: please remove excessive newline (same in ydb-repository-v2 probably)

tx.table(Project.class).save(new Project(id1, "name1")); // make tx available for TLI

assertThatExceptionOfType(OptimisticLockException.class)
.isThrownBy(() -> tx.table(Project.class).find(id2));
Copy link
Collaborator

@nvamelichev nvamelichev Dec 28, 2023

Choose a reason for hiding this comment

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

question: Modern YDB versions can now return OptimisticLock from statements, can they?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this test works over latest docker image (23.3).

assertThatExceptionOfType(OptimisticLockException.class)
.isThrownBy(() -> tx.table(Project.class).find(id2));

tx.commit(); // Commit shouldn't throw an BadSession or other exceptions
Copy link
Collaborator

@nvamelichev nvamelichev Dec 28, 2023

Choose a reason for hiding this comment

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

question: Is it correct to call commit() on a transaction that got invalidated (allowing no-error rollback() after getting OptimisticLock I do understand)?
I think trying to commit an invalidated transaction should definitely cause IllegalStateException or something like that, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this PR, I wanted to touch the old logic to a minimum. commit() is controlled from TxManger, it cannot make a commit after TLI or BadSession, but maybe someone other can. To reduce the likelihood of strange illegals falling out, I did not check it here. This is what this test shows.
But still, it’s really more correct to throw an exception here, I’ll do it.

Also I have a proposal-PR which make works with session and exceptions more understandable (maybe), but I pull it after this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@lavrukov lavrukov force-pushed the better-session-handling branch 2 times, most recently from cf4d01c to 82f3a6e Compare December 29, 2023 11:24
@lavrukov lavrukov force-pushed the better-session-handling branch from 82f3a6e to 31406f1 Compare December 29, 2023 11:25
@lavrukov lavrukov requested a review from nvamelichev January 1, 2024 15:13
Copy link
Collaborator

@nvamelichev nvamelichev left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@nvamelichev
Copy link
Collaborator

I will look at the in-memory changes more thoroughly on Monday, January 8. New locking logic seems a bit strange, e.g. you can get a writable tx shard that (seemingly) never checks for TLI

@nvamelichev nvamelichev self-assigned this Jan 5, 2024
@lavrukov
Copy link
Contributor Author

lavrukov commented Jan 8, 2024

I will look at the in-memory changes more thoroughly on Monday, January 8. New locking logic seems a bit strange, e.g. you can get a writable tx shard that (seemingly) never checks for TLI

Correct. I decided not to add lock checking for write operations because I could not reproduce the TLI for this case over a docker container. Looks like write operations can't return TLI right now. I showed it in test writeDontProduceTLI()

@nvamelichev
Copy link
Collaborator

Looks like write operations can't return TLI right now. I showed it in test writeDontProduceTLI()

Are we sure that this is the case always, for recent versions of YDB? Maybe it's better to be cautious and raise TLI here even if YDB currently does not.

@lavrukov
Copy link
Contributor Author

lavrukov commented Jan 9, 2024

Are we sure that this is the case always, for recent versions of YDB? Maybe it's better to be cautious and raise TLI here even if YDB currently does not.

We have no any guarantees about future, but now we can't reproduce it, it works without it and legacy in-memory don't have consistency checks on write operations too. Given all this, I don't think we have to enhance checks here.

@nvamelichev
Copy link
Collaborator

legacy in-memory don't have consistency checks on write operations too

This is a good argument :-)

I've looked at the code once again. Now I see that throwing TLI from writes will be an optimization, anyway: commit() will throw TLI if the data you wrote to in transaction has been changed by another transaction.

@nvamelichev nvamelichev merged commit 9e0e385 into main Jan 9, 2024
2 checks passed
@nvamelichev nvamelichev deleted the better-session-handling branch January 9, 2024 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants