Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement current_shard_dirty and fix 454
  • Loading branch information
oortcloud1996 committed Mar 19, 2020
commit 5532e2092009d6f11bb97a57cb869f95a17205f9
3 changes: 2 additions & 1 deletion lib/octopus/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Proxy

delegate :current_model, :current_model=,
:current_shard, :current_shard=,
:current_shard_dirty, :current_shard_dirty=,
:current_group, :current_group=,
:current_slave_group, :current_slave_group=,
:current_load_balance_options, :current_load_balance_options=,
Expand Down Expand Up @@ -269,7 +270,7 @@ def should_clean_connection_proxy?(method)

# Try to use slaves if and only if `replicated: true` is specified in `shards.yml` and no slaves groups are defined
def should_send_queries_to_replicated_databases?(method)
replicated && method.to_s =~ /select/ && !block && !slaves_grouped?
replicated && method.to_s =~ /select/ && (!block || !current_shard_dirty) && !slaves_grouped?
end

def send_queries_to_selected_slave(method, *args, &block)
Expand Down
9 changes: 9 additions & 0 deletions lib/octopus/proxy_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Octopus
class ProxyConfig
CURRENT_MODEL_KEY = 'octopus.current_model'.freeze
CURRENT_SHARD_KEY = 'octopus.current_shard'.freeze
CURRENT_SHARD_DIRTY_KEY = 'octopus.current_shard_dirty'.freeze
CURRENT_GROUP_KEY = 'octopus.current_group'.freeze
CURRENT_SLAVE_GROUP_KEY = 'octopus.current_slave_group'.freeze
CURRENT_LOAD_BALANCE_OPTIONS_KEY = 'octopus.current_load_balance_options'.freeze
Expand All @@ -25,6 +26,14 @@ def current_model=(model)
Thread.current[CURRENT_MODEL_KEY] = model.is_a?(ActiveRecord::Base) ? model.class : model
end

def current_shard_dirty
Thread.current[CURRENT_SHARD_DIRTY_KEY] ||= false
end

def current_shard_dirty=(value)
Thread.current[CURRENT_SHARD_DIRTY_KEY] = value
end

def current_shard
Thread.current[CURRENT_SHARD_KEY] ||= Octopus.master_shard
end
Expand Down
1 change: 1 addition & 0 deletions lib/octopus/scope_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def connection
if @klass.custom_octopus_connection && @klass.allowed_shard?(@current_shard)
# Force use of proxy, given we called 'using' explicitly to get here
@klass.connection_proxy.current_model = @klass
@klass.connection_proxy.current_shard_dirty = true
@klass.connection_proxy
else
@klass.connection
Expand Down