alltyme / appengine-ndb-experiment

Automatically exported from code.google.com/p/appengine-ndb-experiment
Other
0 stars 0 forks source link

transaction_deadline for @ndb.transactional() #218

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Unless I'm very mistaken, this code passes deadline=2 to every put().

@ndb.transactional(deadline=2)
def a():
    Model(b=1).put()
    Model(b=2).put()

So effectively, the transaction can take 2+2 seconds. What I'd like instead, is 
an additional transaction_deadline=2 argument, which causes the transaction to 
fail if the transaction takes >2 seconds to complete.

You can try to emulate this by splitting an imagined transaction_deadline 
argument into individual deadline arguments, but it's pointless to try this 
with more complicated transactions. Even if it wasn't, adding additional calls 
means you have to recalculate and so forth. Better to just set this on the 
transaction itself.

The use case is to return (or retry) user requests quickly, rather than wait 
for the default timeout. If the transaction doesn't succeed right away, it more 
often than not times out all the way. It's rare enough with HRD, but still.

Original issue reported on code.google.com by pdknsk on 21 Oct 2012 at 7:49

GoogleCodeExporter commented 9 years ago
Actually, the deadline given to @ndb.transactional() only applies to the 
underlying begin_transaction() and commit() (or rollback()) operations. The 
put() calls are unaffected.

You can probably implement this yourself easily, but you'll have to explicitly 
compute and pass the deadline to each put() call:

@ndb.transactional()
def txn():
    end_time = time.time() + 2
    Model(b=1).put(deadline=end_time - time.time())
    Model(b=2).put(deadline=end_time - time.time())

Original comment by guido@google.com on 24 Oct 2012 at 2:13

GoogleCodeExporter commented 9 years ago
I had mistakenly interpreted the following snippet in the docs as "calls 
inherit **ctx_options from the decorator", rather than "decorator inherits 
**ctx_options from Context".

"For some transaction-related functions, the following transaction options are 
available (along with the inherited context options listed above)."

https://developers.google.com/appengine/docs/python/ndb/functions

I'll probably have to use a max(0,ms) though, because of this snippet I noticed 
in the source. I assume this means -1 reverts to default timeout, and 0 raises 
an exception immediately.

      deadline: A double specifying the deadline for this call as the number of
                seconds from the current time. Ignored if non-positive.

PS. Thanks!

Original comment by pdknsk on 24 Oct 2012 at 1:05

GoogleCodeExporter commented 9 years ago
I'd be suspicious of timeout 0. It may well mean "use default".

Original comment by guido@google.com on 24 Oct 2012 at 1:58

GoogleCodeExporter commented 9 years ago
When using retries, is the transaction cached somehow and just re-committed, or 
basically re-run completely? In the former case this might have unexpected 
results.

Original comment by pdknsk on 24 Oct 2012 at 2:17

GoogleCodeExporter commented 9 years ago
The function is re-run until it commits successfully or until you run out of 
retries or until an unexpected exception happens.

Original comment by guido@google.com on 24 Oct 2012 at 2:42

GoogleCodeExporter commented 9 years ago
I have discarded the idea, because of the additional internal transaction 
deadlines you mentioned (which I'm not familiar with). Essentially I could've 
only made this work as transactional_deadline = deadline*2 + 
deadline_in_function, which is fine more or less, but is too hackish for me.

Original comment by pdknsk on 1 Nov 2012 at 5:39