On Jun 2, 10:06 am, Nate Wiger <[email protected]> wrote:
> I was looking through the new sharding plugin, which looks cool, and I
> had a question about the algorithm.  Specifically, this from the
> announcement:
>
> "... The sharding plugin also works with associations, so associated
> records are retrieved from the same shard the main object was
> retreived from."
>
> I read these two links but they didn't answer my question:
>
> http://sequel.heroku.com/2010/05/16/sharding-for-models/http://sequel.rubyforge.org/rdoc/files/doc/sharding_rdoc.html
>
> My question is:  How can I guarantee that association objects wind up
> in the same shard automatically?  This seems very tricky.

This is what the sharding plugin aims to do.  It's certainly possible
there are still cases it doesn't cover, but it is reasonably well
specced.

> For example, consider this situation (using the example hash approach
> from the rdoc):
>
> 1. Create a new user with username "Bob".  Hash based on username.
> 2. Create a new message from user Bob to user Sue.  Hash based on
> "from" username? (Bob)
> 3. Sue replies to Bob.  Now "from" user is Sue, which works ok for
> Sue's sent mail, but Bob's inbox breaks.

I guess by "Hash based on username", you mean partition/shard based on
username, and Bob and Sue are on different shards?  The sharding
plugin assumes that all associated objects are on the same shard as
the main object, so that's not a scenario it attempts to address.

> Even if I used the association proxy and went through it, it seems
> like it would still only work one way:
>
>
>    user = User['Bob']
>    user.add_message(:to => 'Sue')
>    user = User['Sue']
>    user.messages   # wrong shard

Note that in order for the sharding plugin to be used, you have to set
which shard to use.  So in general User['Bob'] won't work, you'll have
to use User.server(:shard_a).first(:name=>'Bob').  Unless of course
you've overridden User.[] to pick the shard internally.

> Maybe I'm missing something obvious here.  It seems like some
> associations would work, but others wouldn't (which is fine, just
> trying to understand the plugin).

The sharding plugin carries the assumption that associated objects are
on the same shard as the current object.  If that is not the case, it
won't be able to do everything automatically.  However, you should
still be able to use the plugin, you may just need to override some
methods yourself.

In this case, assuming you want a user's inbox stored on the same
shard as the receiver (not the sender), you'd override
User#_add_message to work off the associated user's shard, not the
main users shard.

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to