Commit 06945b48 authored by Kamil Trzciński's avatar Kamil Trzciński

Make `RackMiddleware` to prefer `sticking` instead of `model.sticking`

Before this change we would store `model` for the purpose of understanding
sticking context for a object. This changes to prefer `sticking` which
is directly connected to `LB` context on which the `sticking` is evaluated.
parent 773de4a3
......@@ -38,8 +38,8 @@ module Gitlab
def unstick_or_continue_sticking(env)
namespaces_and_ids = sticking_namespaces(env)
namespaces_and_ids.each do |(model, namespace, id)|
model.sticking.unstick_or_continue_sticking(namespace, id)
namespaces_and_ids.each do |(sticking, namespace, id)|
sticking.unstick_or_continue_sticking(namespace, id)
end
end
......@@ -47,8 +47,8 @@ module Gitlab
def stick_if_necessary(env)
namespaces_and_ids = sticking_namespaces(env)
namespaces_and_ids.each do |model, namespace, id|
model.sticking.stick_if_necessary(namespace, id)
namespaces_and_ids.each do |sticking, namespace, id|
sticking.stick_if_necessary(namespace, id)
end
end
......@@ -74,7 +74,7 @@ module Gitlab
# models that support load balancing. In the future (if we
# determined this to be OK) we may be able to relax this.
::Gitlab::Database::LoadBalancing.base_models.map do |model|
[model, :user, warden.user.id]
[model.sticking, :user, warden.user.id]
end
elsif env[STICK_OBJECT].present?
env[STICK_OBJECT].to_a
......
......@@ -42,7 +42,7 @@ module Gitlab
@model.class_attribute(:sticking)
@model.connection = ConnectionProxy.new(lb)
@model.sticking = Sticking.new(lb, @model)
@model.sticking = Sticking.new(lb)
end
def setup_service_discovery
......
......@@ -10,9 +10,8 @@ module Gitlab
# the primary.
EXPIRATION = 30
def initialize(load_balancer, model)
def initialize(load_balancer)
@load_balancer = load_balancer
@model = model
end
# Unsticks or continues sticking the current request.
......@@ -28,7 +27,7 @@ module Gitlab
unstick_or_continue_sticking(namespace, id)
env[RackMiddleware::STICK_OBJECT] ||= Set.new
env[RackMiddleware::STICK_OBJECT] << [@model, namespace, id]
env[RackMiddleware::STICK_OBJECT] << [self, namespace, id]
end
# Sticks to the primary if a write was performed.
......
......@@ -6,12 +6,12 @@ RSpec.describe Gitlab::Database::LoadBalancing::RackMiddleware, :redis do
let(:app) { double(:app) }
let(:middleware) { described_class.new(app) }
let(:warden_user) { double(:warden, user: double(:user, id: 42)) }
let(:single_sticking_object) { Set.new([[ActiveRecord::Base, :user, 42]]) }
let(:single_sticking_object) { Set.new([[ActiveRecord::Base.sticking, :user, 42]]) }
let(:multiple_sticking_objects) do
Set.new([
[ActiveRecord::Base, :user, 42],
[ActiveRecord::Base, :runner, '123456789'],
[ActiveRecord::Base, :runner, '1234']
[ActiveRecord::Base.sticking, :user, 42],
[ActiveRecord::Base.sticking, :runner, '123456789'],
[ActiveRecord::Base.sticking, :runner, '1234']
])
end
......
......@@ -4,7 +4,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Database::LoadBalancing::Sticking, :redis do
let(:sticking) do
described_class.new(ActiveRecord::Base.connection.load_balancer, ActiveRecord::Base)
described_class.new(ActiveRecord::Base.connection.load_balancer)
end
after do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment