Dynamoid icon indicating copy to clipboard operation
Dynamoid copied to clipboard

u = User.first shows old data, u.reload shows current data

Open anamba opened this issue 12 years ago • 7 comments

Sometimes I get different data (same object saved at different points in time) after reloading:

1.9.3p194 :009 > u = User.first ; u.first_name => "Aa" 1.9.3p194 :010 > u.reload ; u.first_name => "Aaron"

In the log, it shows that User.first uses SCAN and u.reload uses BATCH GET ITEM. I was thinking that might be a clue.

I also suspect I may not be using partitioning correctly. If I just turn it off, will it make everything simpler?

The u.reload version is the correct (latest) one. This has been maddening when trying to do a password change, because the password change completes successfully, but then when I try to log in with the new password, the system compares it to some older version of the password hash, and it fails.

anamba avatar May 14 '12 01:05 anamba

By the way, it does not appear to be a consistency issue. 3 hours later:

1.9.3p194 :009 > u = User.where(:username => 'aaron').consistent.first ; u.first_name => "Aa" 1.9.3p194 :010 > u.reload ; u.first_name => "Aaron"

anamba avatar May 14 '12 04:05 anamba

can you post your user model

btw SCAN operation is not consistent. you should use only primary/composite key in your where query if you want consistent result.

ananthakumaran avatar May 14 '12 04:05 ananthakumaran

I don't think consistency is the issue. It does seem related to partitioning, like it is returning one of the older versions instead of the latest.

Here's the User model in question: https://gist.github.com/2691817

anamba avatar May 14 '12 04:05 anamba

try to on/off partitioning and check whether partition is the problem.

User.where(:username => 'aaron').consistent.first is not going to return consistent result. User.where(:id => 'xxx').consistent.first will return consistent result.

ananthakumaran avatar May 14 '12 04:05 ananthakumaran

So that means in my authenticate method, the reload is not actually a workaround, but the only proper way to do things? Seems odd.

Is there a good way to mess with partitioning settings, or do I need to delete and recreate the tables?

anamba avatar May 14 '12 05:05 anamba

So that means in my authenticate method, the reload is not actually a workaround, but the only proper way to do things? >Seems odd.

You can handle it in two ways.

  • make the email as the primary key
  • add index for email field

ananthakumaran avatar May 14 '12 05:05 ananthakumaran

I have tried to make email address and/or username 100% unique in the past, but each time I've encountered situations that require me to relax that restriction, so I can't make those the primary key.

I will try the indexing approach...

anamba avatar May 14 '12 23:05 anamba