GeezerVison is showing github.com — Batch (and Transaction) dataset_id should match that of Key / Query objects when added · Issue #447 · googleapis/google-cloud-python
Batch (and Transaction) dataset_id should match that of Key / Query objects when added #447DescriptionProposed fix:
Do not ask for dataset_id in the constructor and instead gather it from the keys and queries used in the transaction.
This may require implementing #495 and will require removing the self.begin() call from Transaction.__enter__. (Can't begin until a query or entity is encountered.)
Title was: "Transaction does not rollback correctly when multiple datasets involved"
Witness the following code:
>>> from gcloud import datastore
>>> cnxn = datastore.get_connection()
>>> dataset1 = cnxn.dataset('PROJ1')
>>> dataset2 = cnxn.dataset('PROJ2')
>>> e1 = dataset1.entity('foo')
>>> e2 = dataset2.entity('foo')
>>> e1['bar'] = e2['bar'] = 0
>>> e1.save()
<Entity[{'kind': u'foo', 'id': 5686683802533888L}] {'bar': 0}>
>>> e2.save()
<Entity[{'kind': u'foo', 'id': 5659313586569216L}] {'bar': 0}>
>>> with dataset1.transaction():
... e1.delete()
... e2.delete()
...
>>> e1_still = dataset1.get_entity(e1.key())
>>> e1_still is None
True
>>> e2_still = dataset2.get_entity(e2.key())
>>> e2_still
<Entity[{'kind': u'foo', 'id': 5659313586569216L}] {u'bar': 0L}>Activitydhermes commented on Dec 19, 2014 ContributorAuthor More actions UPDATE: This is because the transaction ignores the dataset associated with e2. To see this, we use the key
<Key[{'kind': 'foo', 'id': 2}]>
in two different datasets and delete the one from dataset2 in the transaction for dataset1. After doing this, we check if that key still exists in dataset1 (it should) and dataset2 (it should, but because the transaction failed since two different datasets were used).
>>> from gcloud import datastore
>>> cnxn = datastore.get_connection()
>>> dataset1 = cnxn.dataset('PROJ1')
>>> dataset2 = cnxn.dataset('PROJ2')
>>>
>>> e1_1 = dataset1.entity('foo')
>>> e1_1.key(e1_1.key().id(1))
<Entity[{'kind': 'foo', 'id': 1}] {}>
>>> e1_2 = dataset1.entity('foo')
>>> e1_2.key(e1_2.key().id(2))
<Entity[{'kind': 'foo', 'id': 2}] {}>
>>>
>>> e2_2 = dataset2.entity('foo')
>>> e2_2.key(e2_2.key().id(2))
<Entity[{'kind': 'foo', 'id': 2}] {}>
>>>
>>> e1_1['bar'] = e1_2['bar'] = e2_2['bar'] = 0
>>>
>>> e1_1.save()
<Entity[{'kind': 'foo', 'id': 1}] {'bar': 0}>
>>> e1_2.save()
<Entity[{'kind': 'foo', 'id': 2}] {'bar': 0}>
>>> e2_2.save()
<Entity[{'kind': 'foo', 'id': 2}] {'bar': 0}>
>>>
>>> with dataset1.transaction():
... e1_1.delete()
... e2_2.delete()
...
>>> e1_1_still = dataset1.get_entity(e1_1.key())
>>> e1_1_still is None
True
>>> e1_2_still = dataset1.get_entity(e1_2.key())
>>> e1_2_still is None
True
>>> e2_2_still = dataset2.get_entity(e2_2.key())
>>> e2_2_still
<Entity[{'kind': u'foo', 'id': 2L}] {u'bar': 0L}>changed the title [-]Transaction does not rollback correctly when multiple datasets involved[/-] [+]Transaction dataset_id should come from Key / Query involved[/+] on Jan 7, 2015changed the title [-]Transaction dataset_id should come from Key / Query involved[/-] [+]Transaction dataset_id should come from Key / Query objects involved[/+] on Jan 7, 2015changed the title [-]Transaction dataset_id should come from Key / Query objects involved[/-] [+]Batch (and Transaction) dataset_id should come from Key / Query objects involved[/+] on Jan 9, 2015tseaver commented on Jan 15, 2015 Note that calling beginTransaction requires the dataset ID (we can't do the RPC without it). We already made the dataset ID optional, falling back to the implicit one, but we cannot possibly rely on the keys, because we don't know them yet when we start the transaction.
I think it would be reasonable to say that users who don't want to use the implicit dataset ID know that when they create the batch / transaction, and can easily pass it. We would then change the code in put / delete to reject any key not matching the one set in __init__. dhermes commented on Jan 15, 2015 ContributorAuthor More actions Yes I also came to this conclusion. Implicit saves us.
This re-opens the question about reading the dataset ID from every single key via _get_dataset_id_from_keys. changed the title [-]Batch (and Transaction) dataset_id should come from Key / Query objects involved[/-] [+]Batch (and Transaction) dataset_id should match that of Key / Query objects when added[/+] on Jan 15, 2015dhermes commented on Jan 15, 2015 ContributorAuthor More actions @tseaver I renamed to reflect the change.
Do you want to tackle this or should I? tseaver commented on Jan 15, 2015
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|