You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR modifies the Transaction class to add a new begin_later argument.
When True, the transaction will not initialize itself at the start of the context manager __enter__ block. Instead, it will only begin at the first rpc. If the first rpc is a read call, this allows us to create the transaction at the same time as the call, saving a network call.
This new behaviour is enabled by default, and is aligned with changes in other languages
For reviewers
In Python, a transaction is created in a context manager, and typically interacted with through the client (which passes through to the Transaction object when needed):
with client.transaction(begin_later=True):
client.query(query)
client.get(entity)
client.put(entity)
client.delete(entity)
if begin_later is False, it will call begin automatically when entering that context manager block (using __enter__ method), retrieving a transaction_id from the server. Otherwise, begin is deferred until a get/query/commit call forces the transaction to talk to the backend
States
The state machine looks like this. Green lines denote transitions that are only valid when begin_later=True orange lines denote transitions that are valid when begin_later=False
Starts out in INITIAL state
if begin_later is true, it allows you to immediately start collecting put and get mutations. Otherwise any rpcs in the INITIAL state will throw an exception
When begin is called and the transaction receives and ID from the server, it moves into IN_PROGRESS state
if begin_later is false, it will immediately call begin when enter is called (called by the with statement)
if begin_later is true, it will call begin when it encounters the first get or query call
user can also always call begin manually
a rollback from either state (or a commit with no content from INITIAL) will result in the terminal ABORTED state
a commit from IN_PROGRESS results in the terminal FINISHED state
any invalid attempted transitions will raise an exception
This PR modifies the Transaction class to add a new begin_later argument.
When True, the transaction will not initialize itself at the start of the context manager __enter__ block. Instead, it will only begin at the first rpc. If the first rpc is a read call, this allows us to create the transaction at the same time as the call, saving a network call.
This new behaviour is enabled by default, and is aligned with changes in other languages
Hi Daniel, it looks like begin_later is by default set to False so by default disabled? Do we want this behavior to be enabled by default?
Yes, I believe it's intended to be off by default. Having it on or off may be more efficient depending on the circumstances, so we opted to keep it off by default (correct me if I'm misremembering @danieljbruce@bhshkh)
This file contains hidden or 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
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
api: datastoreIssues related to the googleapis/python-datastore API.size: lPull request size is large.
5 participants
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.
This PR modifies the Transaction class to add a new
begin_laterargument.When True, the transaction will not initialize itself at the start of the context manager
__enter__block. Instead, it will only begin at the first rpc. If the first rpc is a read call, this allows us to create the transaction at the same time as the call, saving a network call.This new behaviour is enabled by default, and is aligned with changes in other languages
For reviewers
In Python, a transaction is created in a context manager, and typically interacted with through the client (which passes through to the Transaction object when needed):
if begin_later is False, it will call
beginautomatically when entering that context manager block (using __enter__ method), retrieving a transaction_id from the server. Otherwise, begin is deferred until a get/query/commit call forces the transaction to talk to the backendStates
The state machine looks like this. Green lines denote transitions that are only valid when
begin_later=Trueorange lines denote transitions that are valid whenbegin_later=Falsebeginis called and the transaction receives and ID from the server, it moves into IN_PROGRESS statewithstatement)getorquerycallbeginmanually