On Wed, Dec 9, 2020 at 1:37 PM Geff Hanoian <[email protected]> wrote:

> Jeremy,
>
> Thanks for everything.  You are always super help.  Cleanest software I've
> ever used.  I think everyone on this forum owes you a lifetime supply of
> scotch or whatever you drink. :)
>
> I did what you requested:
> 1. made a super small app that just exercised this problem
> 2. Added the logging as defined above (super helpful!)
>
> What I found is, it seems like DB.synchronize does not allocate anything
> when using a replica.  Is DB.synchronize correct for replicas?
>
> The following are the snip of code and log entries:  ( can post more of
> the code I just didn't want to inundate ).
>
> tester-primary.rb and tester-replicas.rb lines 25-28:
>   DB.synchronize do
>     x = Trip.order(:id).first.inspect
>     y = Template.order(:id).first.inspect
>   end
>
> app/models/template.rb:  ( super simple )
> class Template < Sequel::Model
>   many_to_one :user
> end
>
> Querying primary: (seems correct, one NULL for both)
> I, [2020-12-09T13:36:43.722029 #39120]  INFO -- : (0.000873s) (conn:
> 47271528492740) (source: tester-primary.rb:25:in `block in <top
> (required)>') SELECT NULL
> I, [2020-12-09T13:36:43.723006 #39120]  INFO -- : (0.000753s) (conn:
> 47271528492740) (source: tester-primary.rb:26:in `block (2 levels) in <top
> (required)>') SELECT * FROM "trips" ORDER BY "id" LIMIT 1
> I, [2020-12-09T13:36:43.723889 #39120]  INFO -- : (0.000452s) (conn:
> 47271528492740) (source: tester-primary.rb:27:in `block (2 levels) in <top
> (required)>') SELECT * FROM "templates" ORDER BY "id" LIMIT 1
>
> Query replica: (3 SELECT NULL)
> I, [2020-12-09T13:35:51.502009 #39117]  INFO -- : (0.000418s) (conn:
> 46966930197660) (source: tester-replica.rb:25:in `block in <top
> (required)>') SELECT NULL
> I, [2020-12-09T13:35:51.502847 #39117]  INFO -- : (0.000605s) (conn:
> 46966930244680) (source: tester-replica.rb:26:in `block (2 levels) in <top
> (required)>') SELECT NULL
> I, [2020-12-09T13:35:51.504222 #39117]  INFO -- : (0.001229s) (conn:
> 46966930244680) (source: tester-replica.rb:26:in `block (2 levels) in <top
> (required)>') SELECT * FROM "trips" ORDER BY "id" LIMIT 1
> I, [2020-12-09T13:35:51.505495 #39117]  INFO -- : (0.000398s) (conn:
> 46966930244680) (source: tester-replica.rb:27:in `block (2 levels) in <top
> (required)>') SELECT NULL
> I, [2020-12-09T13:35:51.506530 #39117]  INFO -- : (0.000874s) (conn:
> 46966930244680) (source: tester-replica.rb:27:in `block (2 levels) in <top
> (required)>') SELECT * FROM "templates" ORDER BY "id" LIMIT 1
>

In Query replica, the first NULL (for the DB.synchronize line) is for a
different connection than your other ones.  Since you didn't provide a
shard to use, DB.synchronize will use the default shard.  However, internal
queries to get the columns will use the read_only shard by default, since
there isn't already a connection checked out for the read_only sharded, it
will checkout a connection for each query.  You want to make sure you pass
the appropriate shard name to DB.synchronize.  So if all access inside the
block should use the read_only shard, you want to checkout the read_only
shard:

DB.synchronize(:read_only) do

If you want to use this block for both the default and read only cases,
you'll have to pick the argument you want to pass:

DB.synchronize(use_replica ? :read_only : :default) do

Where you have appropriately defined use_replica.

Alternatively, you could look into using the server_block extension, which
allows all queries in a block to use a specified shard by default.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSeaQ%3DedJbt21Y45BNLvBy7Y%2B4MgtmppivjX18yEFF-%3Dhw%40mail.gmail.com.

Reply via email to