gitlab-org--gitlab-foss/spec/support
Yorick Peterse 88e627cf14
Fix race conditions for AuthorizedProjectsWorker
There were two cases that could be problematic:

1. Because sometimes AuthorizedProjectsWorker would be scheduled in a
   transaction it was possible for a job to run/complete before a
   COMMIT; resulting in it either producing an error, or producing no
   new data.

2. When scheduling jobs the code would not wait until completion. This
   could lead to a user creating a project and then immediately trying
   to push to it. Usually this will work fine, but given enough load it
   might take a few seconds before a user has access.

The first one is problematic, the second one is mostly just annoying
(but annoying enough to warrant a solution).

This commit changes two things to deal with this:

1. Sidekiq scheduling now takes places after a COMMIT, this is ensured
   by scheduling using Rails' after_commit hook instead of doing so in
   an arbitrary method.

2. When scheduling jobs the calling thread now waits for all jobs to
   complete.

Solution 2 requires tracking of job completions. Sidekiq provides a way
to find a job by its ID, but this involves scanning over the entire
queue; something that is very in-efficient for large queues. As such a
more efficient solution is necessary. There are two main Gems that can
do this in a more efficient manner:

* sidekiq-status
* sidekiq_status

No, this is not a joke. Both Gems do a similar thing (but slightly
different), and the only difference in their name is a dash vs an
underscore. Both Gems however provide far more than just checking if a
job has been completed, and both have their problems. sidekiq-status
does not appear to be actively maintained, with the last release being
in 2015. It also has some issues during testing as API calls are not
stubbed in any way. sidekiq_status on the other hand does not appear to
be very popular, and introduces a similar amount of code.

Because of this I opted to write a simple home grown solution. After
all, all we need is storing a job ID somewhere so we can efficiently
look it up; we don't need extra web UIs (as provided by sidekiq-status)
or complex APIs to update progress, etc.

This is where Gitlab::SidekiqStatus comes in handy. This namespace
contains some code used for tracking, removing, and looking up job IDs;
all without having to scan over an entire queue. Data is removed
explicitly, but also expires automatically just in case.

Using this API we can now schedule jobs in a fork-join like manner: we
schedule the jobs in Sidekiq, process them in parallel, then wait for
completion. By using Sidekiq we can leverage all the benefits such as
being able to scale across multiple cores and hosts, retrying failed
jobs, etc.

The one downside is that we need to make sure we can deal with
unexpected increases in job processing timings. To deal with this the
class Gitlab::JobWaiter (used for waiting for jobs to complete) will
only wait a number of seconds (30 by default). Once this timeout is
reached it will simply return.

For GitLab.com almost all AuthorizedProjectWorker jobs complete in
seconds, only very rarely do we spike to job timings of around a minute.
These in turn seem to be the result of external factors (e.g. deploys),
in which case a user is most likely not able to use the system anyway.

In short, this new solution should ensure that jobs are processed
properly and that in almost all cases a user has access to their
resources whenever they need to have access.
2017-01-25 13:22:15 +01:00
..
api Add some API endpoints for time tracking. 2017-01-18 10:48:16 -06:00
banzai Fix Markdown styling inside reference links 2016-11-01 09:49:30 +00:00
controllers Namespace access token session key in Import::GithubController 2016-12-19 17:35:51 +01:00
cycle_analytics_helpers refactor fetcher and fixed specs 2017-01-17 11:32:55 +01:00
features Replace wording for slash command confirmation message 2016-12-26 08:31:24 -05:00
gitlab_stubs
import_export Merge branch 'fix/import-export-symlink-vulnerability' into 'security' 2016-11-03 16:04:18 +01:00
matchers Absorb gitlab_git 2017-01-04 19:09:28 -05:00
services Check if user can read issue before being assigned 2016-12-27 19:25:17 -02:00
api_helpers.rb
capybara.rb Pass --load-images=no to PhantomJS via Capybara/Poltergeist 2016-11-28 16:50:08 +08:00
capybara_helpers.rb
carrierwave.rb Clear Carrierwave upload after each test example 2016-11-23 09:51:50 +01:00
chat_slash_commands_shared_examples.rb Improve after feedback 2016-12-19 15:40:06 +01:00
cycle_analytics_helpers.rb Merge branch 'issue_23548_dev' into 'master' 2016-11-09 12:25:17 +01:00
database_connection_helpers.rb Write a spec covering the race condition during group deletion. 2016-11-18 14:27:13 +05:30
db_cleaner.rb Precalculate user's authorized projects in database 2016-11-18 20:25:45 +02:00
email_format_shared_examples.rb
email_helpers.rb Test against notification email 2016-10-21 18:22:09 +08:00
factory_girl.rb
fake_u2f_device.rb Improved the u2f flow 2016-12-27 00:18:17 +00:00
filter_spec_helper.rb
fixture_helpers.rb
git_helpers.rb Generate branch/file names more randomly to avoid failures. 2016-09-07 11:26:07 +05:30
git_http_helpers.rb Added git http requests tests for user with LDAP identity 2016-09-28 08:43:21 +02:00
import_spec_helper.rb Namespace access token session key in Import::GithubController 2016-12-19 17:35:51 +01:00
issue_helpers.rb
issue_tracker_service_shared_example.rb Prevent wrong markdown on issue ids when project has Jira service activated 2016-10-19 13:46:04 -02:00
javascript_fixtures_helpers.rb Merge branch 'fix-u2f-fixtures' into 26226-generate-all-haml-fixtures-within-teaspoon-fixtures-task 2017-01-05 14:10:43 -06:00
jira_service_helper.rb Allow enabling and disabling commit and MR events for JIRA 2016-11-18 15:17:51 -02:00
kubernetes_helpers.rb Add terminals to the Kubernetes deployment service 2016-12-19 19:53:04 +00:00
ldap_helpers.rb Request only the LDAP attributes we need 2016-09-09 13:14:57 -05:00
login_helpers.rb fix: removed signed_out notification 2016-12-12 15:04:48 +05:30
markdown_feature.rb
mentionable_shared_examples.rb Simplify Mentionable concern instance methods 2016-10-04 10:45:48 +02:00
merge_request_helpers.rb
notify_shared_examples.rb Merge remote-tracking branch 'upstream/master' into pipeline-notifications 2016-10-25 23:03:47 +08:00
omni_auth.rb
project_features_apply_to_issuables_shared_examples.rb Merge branch '22481-honour-issue-visibility-for-groups' into 'security' 2016-11-09 12:24:13 +01:00
project_hook_data_shared_example.rb
query_recorder.rb Added QueryRecorder to test N+1 fix on Milestone#show 2016-12-20 16:59:48 +00:00
rake_helpers.rb Move task helpers to a module 2016-11-30 12:20:21 +01:00
reactive_caching_helpers.rb Merge branch '24185-legacy-ci-status-reactive-cache' into 'security' 2017-01-12 17:39:46 -05:00
reference_parser_helpers.rb
reference_parser_shared_examples.rb Merge branch 'issue_23548_dev' into 'master' 2016-11-09 12:25:17 +01:00
repo_helpers.rb
search_helpers.rb Fix broken commits search 2016-11-08 12:03:23 +02:00
seed_helper.rb Use configured git rather than system git 2017-01-05 13:46:26 +01:00
seed_repo.rb Absorb gitlab_git 2017-01-04 19:09:28 -05:00
select2_helper.rb Trigger change even in select2 test helper to produce production-like behaviour 2016-10-24 13:14:00 +03:00
services_shared_context.rb Grapify the service API 2016-12-09 17:20:12 +01:00
setup_builds_storage.rb Clear test build storage directory before each example 2016-11-23 13:51:05 +01:00
sidekiq.rb Fix race conditions for AuthorizedProjectsWorker 2017-01-25 13:22:15 +01:00
slack_mattermost_notifications_shared_examples.rb Do not override incoming webhook channel for mattermost and slack 2016-12-22 16:16:10 -02:00
slash_commands_helpers.rb Move write_note into SlashCommandsHelper and update other dependent specs 2016-09-08 18:52:55 -04:00
snippets_shared_examples.rb Fix snippets pagination 2016-09-26 12:42:12 +02:00
stub_configuration.rb Move task helpers to a module 2016-11-30 12:20:21 +01:00
stub_env.rb Introduce "stub_env" test helper for safely stubbing environment variables 2017-01-09 18:19:48 +01:00
stub_gitlab_calls.rb
stub_gitlab_data.rb
taskable_shared_examples.rb add "x of y tasks completed" on issuable 2016-10-28 14:01:36 -02:00
test_env.rb Mark MR as WIP when pushing WIP commits 2017-01-19 12:30:34 +01:00
time_tracking_shared_examples.rb Backport timetracking frontend to CE. 2017-01-15 11:10:04 -05:00
updating_mentions_shared_examples.rb
upload_helpers.rb Add Gitlab::Middleware::Multipart 2016-12-15 12:26:36 +01:00
wait_for_ajax.rb Wait for ajax for every merge request spinach test 2016-10-18 18:55:11 +03:00
wait_for_vue_resource.rb Fixed tests 2016-09-15 18:20:36 -04:00
webmock.rb
workhorse_helpers.rb