2
votes

So I store a "User" entity in the Datastore, which is visible in the Datastore Viewer and I can see all properties that I expect, which includes a property called contacts that contains a list of type long (representing IDs of people in that user's contact list). This is stored using userEntity.setProperty("contacts", ids); where ids is a List<Long>.

However, when I use Java on App Engine to extract this entity (without any projections, just a filter on the email address of the user I'm looking for) I receive an entity object that doesn't have the contacts property.

Is there any reason why I wouldn't be receiving the full set of properties? The code I'm using is (hopefully) relatively standard:

Query.Filter emailFilter = new Query.FilterPredicate("email", Query.FilterOperator.EQUAL, email);
Query q = new Query("User").setFilter(emailFilter);
Entity userEntity = datastoreService.prepare(q).asSingleEntity();
logger.info(userEntity.toString());

This outputs:

<Entity [User(5156288090603520)]:
lastName = Doe
updatedTs = 1414005061759
email = [email protected]
firstName = John
ownerEmail = [email protected]
>

Which is everything I see in the viewer except the contacts property that contains the list. As such if I try userEntity.getProperty("contacts"); then I get a null object. If I run a GQL query in App Engine Console:

SELECT contacts FROM User

I also get the expected result. Any idea why my Java query doesn't return this property?

1
Have you recently updated the entity and added the contacts field? It's possible that you are hitting eventual consistency -- it is possible that your indexes have been updated but they still are returning the old entity.Patrick Costello
I did wonder whether that might be the case, but the other properties that were modified in the same transaction appear to have updated. Is it possible that they are updated independently?aiszatt
Thanks @patrick-costello, you were right. I had thought eventual consistency meant a few seconds/minutes not hours!aiszatt
Usually it does! Unfortunately every once in a while things can take a while. I would recommend increasing the eventual consistency in the development server when testing your app so you can get a better sense of exactly how things will be different under eventual consistency: cloud.google.com/appengine/docs/java/tools/…Patrick Costello

1 Answers

0
votes

In regards to what @Patrick Costello is posting in the comments, I'd add that yeah, eventual consistency is the cause of this, but you can also attempt to alleviate these side effects of eventual consistency by adding entities to entity groups using ancestors. From this link:

To obtain strongly consistent query results, you need to use an ancestor query limiting the results to a single entity group. This works because entity groups are a unit of consistency as well as transactionality.