gitlab-org--gitlab-foss/spec/services/users/last_push_event_service_spec.rb
Yorick Peterse 83355336dd
Rework how recent push events are retrieved
Whenever you push to a branch GitLab will show a button to create a
merge request (should one not exist already). The underlying code to
display this data was quite inefficient. For example, it involved
multiple slow queries just to figure out what the most recent push event
was.

This commit changes the way this data is retrieved so it's much faster.
This is achieved by caching the ID of the last push event on every push,
which is then retrieved when loading certain pages. Database queries are
only executed if necessary and the cached data is removed automatically
once a merge request has been created, or 2 hours after being stored.

A trade-off of this approach is that we _only_ track the last event.
Previously if you were to push to branch A and B then create a merge
request for branch B we'd still show the widget for branch A. As of this
commit this is no longer the case, instead we will only show the widget
for the branch you pushed to most recently. Once a merge request exists
the widget is no longer displayed. Alternative solutions are either too
complex and/or too slow, hence the decision was made to settle for this
trade-off.

Performance Impact
------------------

In the best case scenario (= a user didn't push anything for more than 2
hours) we perform a single Redis GET per page. Should there be cached
data we will run a single (and lightweight) SQL query to get the
event data from the database. If a merge request already exists we will
run an additional DEL to remove the cache key.

The difference in response timings can vary a bit per project. On
GitLab.com the 99th percentile of time spent in User#recent_push hovers
between 100 milliseconds and 1 second, while the mean hovers around 50
milliseconds. With the changes in this MR the expected time spent in
User#recent_push is expected to be reduced down to just a few
milliseconds.

Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/35990
2017-09-08 00:46:16 +02:00

112 lines
3.2 KiB
Ruby

require 'spec_helper'
describe Users::LastPushEventService do
let(:user) { build(:user, id: 1) }
let(:project) { build(:project, id: 2) }
let(:event) { build(:push_event, id: 3, author: user, project: project) }
let(:service) { described_class.new(user) }
describe '#cache_last_push_event' do
it "caches the event for the event's project and current user" do
expect(service).to receive(:set_key)
.ordered
.with('last-push-event/1/2', 3)
expect(service).to receive(:set_key)
.ordered
.with('last-push-event/1', 3)
service.cache_last_push_event(event)
end
it 'caches the event for the origin project when pushing to a fork' do
source = build(:project, id: 5)
allow(project).to receive(:forked?).and_return(true)
allow(project).to receive(:forked_from_project).and_return(source)
expect(service).to receive(:set_key)
.ordered
.with('last-push-event/1/2', 3)
expect(service).to receive(:set_key)
.ordered
.with('last-push-event/1', 3)
expect(service).to receive(:set_key)
.ordered
.with('last-push-event/1/5', 3)
service.cache_last_push_event(event)
end
end
describe '#last_event_for_user' do
it 'returns the last push event for the current user' do
expect(service).to receive(:find_cached_event)
.with('last-push-event/1')
.and_return(event)
expect(service.last_event_for_user).to eq(event)
end
it 'returns nil when no push event could be found' do
expect(service).to receive(:find_cached_event)
.with('last-push-event/1')
.and_return(nil)
expect(service.last_event_for_user).to be_nil
end
end
describe '#last_event_for_project' do
it 'returns the last push event for the given project' do
expect(service).to receive(:find_cached_event)
.with('last-push-event/1/2')
.and_return(event)
expect(service.last_event_for_project(project)).to eq(event)
end
it 'returns nil when no push event could be found' do
expect(service).to receive(:find_cached_event)
.with('last-push-event/1/2')
.and_return(nil)
expect(service.last_event_for_project(project)).to be_nil
end
end
describe '#find_cached_event', :use_clean_rails_memory_store_caching do
context 'with a non-existing cache key' do
it 'returns nil' do
expect(service.find_cached_event('bla')).to be_nil
end
end
context 'with an existing cache key' do
before do
service.cache_last_push_event(event)
end
it 'returns a PushEvent when no merge requests exist for the event' do
allow(service).to receive(:find_event_in_database)
.with(event.id)
.and_return(event)
expect(service.find_cached_event('last-push-event/1')).to eq(event)
end
it 'removes the cache key when no event could be found and returns nil' do
allow(PushEvent).to receive(:without_existing_merge_requests)
.and_return(PushEvent.none)
expect(Rails.cache).to receive(:delete)
.with('last-push-event/1')
.and_call_original
expect(service.find_cached_event('last-push-event/1')).to be_nil
end
end
end
end