83355336dd
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
83 lines
2.1 KiB
Ruby
83 lines
2.1 KiB
Ruby
module Users
|
|
# Service class for caching and retrieving the last push event of a user.
|
|
class LastPushEventService
|
|
EXPIRATION = 2.hours
|
|
|
|
def initialize(user)
|
|
@user = user
|
|
end
|
|
|
|
# Caches the given push event for the current user in the Rails cache.
|
|
#
|
|
# event - An instance of PushEvent to cache.
|
|
def cache_last_push_event(event)
|
|
keys = [
|
|
project_cache_key(event.project),
|
|
user_cache_key
|
|
]
|
|
|
|
if event.project.forked?
|
|
keys << project_cache_key(event.project.forked_from_project)
|
|
end
|
|
|
|
keys.each { |key| set_key(key, event.id) }
|
|
end
|
|
|
|
# Returns the last PushEvent for the current user.
|
|
#
|
|
# This method will return nil if no event was found.
|
|
def last_event_for_user
|
|
find_cached_event(user_cache_key)
|
|
end
|
|
|
|
# Returns the last PushEvent for the current user and the given project.
|
|
#
|
|
# project - An instance of Project for which to retrieve the PushEvent.
|
|
#
|
|
# This method will return nil if no event was found.
|
|
def last_event_for_project(project)
|
|
find_cached_event(project_cache_key(project))
|
|
end
|
|
|
|
def find_cached_event(cache_key)
|
|
event_id = get_key(cache_key)
|
|
|
|
return unless event_id
|
|
|
|
unless (event = find_event_in_database(event_id))
|
|
# We don't want to keep querying the same data over and over when a
|
|
# merge request has been created, thus we remove the key if no event
|
|
# (meaning an MR was created) is returned.
|
|
Rails.cache.delete(cache_key)
|
|
end
|
|
|
|
event
|
|
end
|
|
|
|
private
|
|
|
|
def find_event_in_database(id)
|
|
PushEvent
|
|
.without_existing_merge_requests
|
|
.find_by(id: id)
|
|
end
|
|
|
|
def user_cache_key
|
|
"last-push-event/#{@user.id}"
|
|
end
|
|
|
|
def project_cache_key(project)
|
|
"last-push-event/#{@user.id}/#{project.id}"
|
|
end
|
|
|
|
def get_key(key)
|
|
Rails.cache.read(key, raw: true)
|
|
end
|
|
|
|
def set_key(key, value)
|
|
# We're using raw values here since this takes up less space and we don't
|
|
# store complex objects.
|
|
Rails.cache.write(key, value, raw: true, expires_in: EXPIRATION)
|
|
end
|
|
end
|
|
end
|