debian-mirror-gitlab/doc/development/github_importer.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

332 lines
14 KiB
Markdown
Raw Normal View History

2021-01-29 00:20:46 +05:30
---
stage: none
group: unassigned
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2021-01-29 00:20:46 +05:30
---
2018-03-17 18:26:18 +05:30
# Working with the GitHub importer
In GitLab 10.2 a new version of the GitHub importer was introduced. This new
importer performs its work in parallel using Sidekiq, greatly reducing the time
necessary to import GitHub projects into a GitLab instance.
The GitHub importer offers two different types of importers: a sequential
importer and a parallel importer. The Rake task `import:github` uses the
2023-05-27 22:25:52 +05:30
sequential importer, and everything else uses the parallel importer. The
difference between these two importers is:
- The sequential importer does all the work in a single thread, so it's more suited for debugging purposes or Rake tasks.
- The parallel importer uses Sidekiq.
2018-03-17 18:26:18 +05:30
## Requirements
2019-03-02 22:35:43 +05:30
- GitLab CE 10.2.0 or newer.
- Sidekiq workers that process the `github_importer` and
2018-03-17 18:26:18 +05:30
`github_importer_advance_stage` queues (this is enabled by default).
2019-03-02 22:35:43 +05:30
- Octokit (used for interacting with the GitHub API).
2018-03-17 18:26:18 +05:30
## Code structure
The importer's codebase is broken up into the following directories:
2019-03-02 22:35:43 +05:30
- `lib/gitlab/github_import`: this directory contains most of the code such as
2018-03-17 18:26:18 +05:30
the classes used for importing resources.
2019-03-02 22:35:43 +05:30
- `app/workers/gitlab/github_import`: this directory contains the Sidekiq
2018-03-17 18:26:18 +05:30
workers.
2019-03-02 22:35:43 +05:30
- `app/workers/concerns/gitlab/github_import`: this directory contains a few
2018-03-17 18:26:18 +05:30
modules reused by the various Sidekiq workers.
## Architecture overview
2020-04-08 14:13:33 +05:30
When a GitHub project is imported, we schedule and execute a job for the
`RepositoryImportWorker` worker as all other importers. However, unlike other
importers, we don't immediately perform the work necessary. Instead work is
2018-03-17 18:26:18 +05:30
divided into separate stages, with each stage consisting out of a set of Sidekiq
jobs that are executed. Between every stage a job is scheduled that periodically
checks if all work of the current stage is completed, advancing the import
process to the next stage when this is the case. The worker handling this is
called `Gitlab::GithubImport::AdvanceStageWorker`.
## Stages
### 1. RepositoryImportWorker
2021-02-22 17:27:13 +05:30
This worker starts the import process by scheduling a job for the
2018-03-17 18:26:18 +05:30
next worker.
### 2. Stage::ImportRepositoryWorker
2021-02-22 17:27:13 +05:30
This worker imports the repository and wiki, scheduling the next stage when
2018-03-17 18:26:18 +05:30
done.
### 3. Stage::ImportBaseDataWorker
2021-02-22 17:27:13 +05:30
This worker imports base data such as labels, milestones, and releases. This
work is done in a single thread because it can be performed fast enough that we
2018-03-17 18:26:18 +05:30
don't need to perform this work in parallel.
### 4. Stage::ImportPullRequestsWorker
2021-02-22 17:27:13 +05:30
This worker imports all pull requests. For every pull request a job for the
2018-03-17 18:26:18 +05:30
`Gitlab::GithubImport::ImportPullRequestWorker` worker is scheduled.
2021-10-27 15:23:28 +05:30
### 5. Stage::ImportPullRequestsMergedByWorker
2022-10-11 01:57:18 +05:30
This worker imports the pull requests' _merged-by_ user information. The
2022-08-27 11:52:29 +05:30
[_List pull requests_](https://docs.github.com/en/rest/pulls#list-pull-requests)
2021-10-27 15:23:28 +05:30
API doesn't provide this information. Therefore, this stage must fetch each merged pull request
individually to import this information. A
`Gitlab::GithubImport::ImportPullRequestMergedByWorker` job is scheduled for each fetched pull
request.
2023-01-13 00:05:48 +05:30
### 6. Stage::ImportPullRequestsReviewRequestsWorker
2021-10-27 15:23:28 +05:30
2023-01-13 00:05:48 +05:30
This worker imports assigned reviewers of pull requests. For each pull request, this worker:
- Fetches all assigned review requests.
- Schedules a `Gitlab::GithubImport::PullRequests::ImportReviewRequestWorker` job for each fetched review request.
### 7. Stage::ImportPullRequestsReviewsWorker
This worker imports reviews of pull requests. For each pull request, this worker:
2021-10-27 15:23:28 +05:30
- Fetches all the pages of reviews.
- Schedules a `Gitlab::GithubImport::ImportPullRequestReviewWorker` job for each fetched review.
2023-01-13 00:05:48 +05:30
### 8. Stage::ImportIssuesAndDiffNotesWorker
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
This worker imports all issues and pull request comments. For every issue, we
2018-03-17 18:26:18 +05:30
schedule a job for the `Gitlab::GithubImport::ImportIssueWorker` worker. For
2020-04-08 14:13:33 +05:30
pull request comments, we instead schedule jobs for the
2018-03-17 18:26:18 +05:30
`Gitlab::GithubImport::DiffNoteImporter` worker.
This worker processes both issues and diff notes in parallel so we don't need to
schedule a separate stage and wait for the previous one to complete.
Issues are imported separately from pull requests because only the "issues" API
includes labels for both issue and pull requests. Importing issues and setting
label links in the same worker removes the need for performing a separate crawl
through the API data, reducing the number of API calls necessary to import a
project.
2023-01-13 00:05:48 +05:30
### 9. Stage::ImportIssueEventsWorker
2022-10-11 01:57:18 +05:30
This worker imports all issues and pull request events. For every event, we
schedule a job for the `Gitlab::GithubImport::ImportIssueEventWorker` worker.
We can import both issues and pull request events by single stage because of a specific aspect of the GitHub API. It looks like that under the hood, issues and pull requests
GitHub are stored in a single table. Therefore, they have globally-unique IDs and so:
- Every pull request is an issue.
- Issues aren't pull requests.
Therefore, both issues and pull requests have a common API for most related things.
2022-11-25 23:54:43 +05:30
NOTE:
This stage is optional and can consume significant extra import time (controlled by `Gitlab::GithubImport::Settings`).
2023-01-13 00:05:48 +05:30
### 10. Stage::ImportNotesWorker
2018-03-17 18:26:18 +05:30
This worker imports regular comments for both issues and pull requests. For
2020-04-08 14:13:33 +05:30
every comment, we schedule a job for the
2018-03-17 18:26:18 +05:30
`Gitlab::GithubImport::ImportNoteWorker` worker.
2021-02-22 17:27:13 +05:30
Regular comments have to be imported at the end because the GitHub API used
2018-03-17 18:26:18 +05:30
returns comments for both issues and pull requests. This means we have to wait
for all issues and pull requests to be imported before we can import regular
comments.
2023-01-13 00:05:48 +05:30
### 11. Stage::ImportAttachmentsWorker
2022-10-11 01:57:18 +05:30
2022-11-25 23:54:43 +05:30
This worker imports note attachments that are linked inside Markdown.
For each entity with Markdown text in the project, we schedule a job of:
- `Gitlab::GithubImport::ImportReleaseAttachmentsWorker` for every release.
- `Gitlab::GithubImport::ImportNoteAttachmentsWorker` for every note.
- `Gitlab::GithubImport::ImportIssueAttachmentsWorker` for every issue.
- `Gitlab::GithubImport::ImportMergeRequestAttachmentsWorker` for every merge request.
2022-10-11 01:57:18 +05:30
Each job:
2022-11-25 23:54:43 +05:30
1. Iterates over all attachment links inside of a specific record.
2022-10-11 01:57:18 +05:30
1. Downloads the attachment.
1. Replaces the old link with a newly-generated link to GitLab.
2022-11-25 23:54:43 +05:30
NOTE:
It's an optional stage that could consume significant extra import time (controlled by `Gitlab::GithubImport::Settings`).
2023-01-13 00:05:48 +05:30
### 12. Stage::ImportProtectedBranchesWorker
2022-10-11 01:57:18 +05:30
This worker imports protected branch rules.
For every rule that exists on GitHub, we schedule a job of
`Gitlab::GithubImport::ImportProtectedBranchWorker`.
Each job compares the branch protection rules from GitHub and GitLab and applies
the strictest of the rules to the branches in GitLab.
2023-01-13 00:05:48 +05:30
### 13. Stage::FinishImportWorker
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
This worker completes the import process by performing some housekeeping
2018-03-17 18:26:18 +05:30
(such as flushing any caches) and by marking the import as completed.
## Advancing stages
Advancing stages is done in one of two ways:
2019-02-15 15:39:39 +05:30
- Scheduling the worker for the next stage directly.
- Scheduling a job for `Gitlab::GithubImport::AdvanceStageWorker` which will
2018-03-17 18:26:18 +05:30
advance the stage when all work of the current stage has been completed.
The first approach should only be used by workers that perform all their work in
a single thread, while `AdvanceStageWorker` should be used for everything else.
2023-05-27 22:25:52 +05:30
When you schedule a job, `AdvanceStageWorker`
2021-02-22 17:27:13 +05:30
is given a project ID, a list of Redis keys, and the name of the next
2018-03-17 18:26:18 +05:30
stage. The Redis keys (produced by `Gitlab::JobWaiter`) are used to check if the
2023-05-27 22:25:52 +05:30
running stage has been completed or not. If the stage has not yet been
2021-02-22 17:27:13 +05:30
completed `AdvanceStageWorker` reschedules itself. After a stage finishes
`AdvanceStageworker` refreshes the import JID (more on this below) and
2018-03-17 18:26:18 +05:30
schedule the worker of the next stage.
2021-02-22 17:27:13 +05:30
To reduce the number of `AdvanceStageWorker` jobs scheduled this worker
briefly waits for jobs to complete before deciding what the next action should
be. For small projects, this may slow down the import process a bit, but it
also reduces pressure on the system as a whole.
2018-03-17 18:26:18 +05:30
2023-03-04 22:38:38 +05:30
## Refreshing import job IDs
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
GitLab includes a worker called `Gitlab::Import::StuckProjectImportJobsWorker`
2021-02-22 17:27:13 +05:30
that periodically runs and marks project imports as failed if they have been
2020-06-23 00:09:42 +05:30
running for more than 15 hours. For GitHub projects, this poses a bit of a
problem: importing large projects could take several hours depending on how
often we hit the GitHub rate limit (more on this below), but we don't want
`Gitlab::Import::StuckProjectImportJobsWorker` to mark our import as failed because of this.
2018-03-17 18:26:18 +05:30
To prevent this from happening we periodically refresh the expiration time of
the import process. This works by storing the JID of the import job in the
2023-03-04 22:38:38 +05:30
database, then refreshing this JID TTL at various stages throughout the import
2019-02-15 15:39:39 +05:30
process. This is done by calling `ProjectImportState#refresh_jid_expiration`. By
2018-03-17 18:26:18 +05:30
refreshing this TTL we can ensure our import does not get marked as failed so
long we're still performing work.
## GitHub rate limit
2020-04-08 14:13:33 +05:30
GitHub has a rate limit of 5,000 API calls per hour. The number of requests
2018-03-17 18:26:18 +05:30
necessary to import a project is largely dominated by the number of unique users
2021-09-30 23:02:18 +05:30
involved in a project (for example, issue authors). Other data such as issue pages
2020-03-13 15:44:24 +05:30
and comments typically only requires a few dozen requests to import. This is
2022-11-25 23:54:43 +05:30
because we need the Email address of users to map them to GitLab users.
2018-03-17 18:26:18 +05:30
We handle this by doing the following:
2021-02-22 17:27:13 +05:30
1. After we hit the rate limit all jobs automatically reschedule themselves
2018-03-17 18:26:18 +05:30
in such a way that they are not executed until the rate limit has been reset.
2019-02-15 15:39:39 +05:30
1. We cache the mapping of GitHub users to GitLab users in Redis.
2018-03-17 18:26:18 +05:30
More information on user caching can be found below.
## Caching user lookups
When mapping GitHub users to GitLab users we need to (in the worst case)
perform:
1. One API call to get the user's Email address.
2019-02-15 15:39:39 +05:30
1. Two database queries to see if a corresponding GitLab user exists. One query
2021-02-22 17:27:13 +05:30
tries to find the user based on the GitHub user ID, while the second query
2018-03-17 18:26:18 +05:30
is used to find the user using their GitHub Email address.
2021-10-27 15:23:28 +05:30
To avoid mismatching users, the search by GitHub user ID is not done when importing from GitHub
Enterprise.
2018-03-17 18:26:18 +05:30
Because this process is quite expensive we cache the result of these lookups in
Redis. For every user looked up we store three keys:
2019-02-15 15:39:39 +05:30
- A Redis key mapping GitHub usernames to their Email addresses.
- A Redis key mapping a GitHub Email addresses to a GitLab user ID.
- A Redis key mapping a GitHub user ID to GitLab user ID.
2018-03-17 18:26:18 +05:30
There are two types of lookups we cache:
2019-02-15 15:39:39 +05:30
- A positive lookup, meaning we found a GitLab user ID.
- A negative lookup, meaning we didn't find a GitLab user ID. Caching this
2018-03-17 18:26:18 +05:30
prevents us from performing the same work for users that we know don't exist
in our GitLab database.
The expiration time of these keys is 24 hours. When retrieving the cache of a
2020-04-08 14:13:33 +05:30
positive lookup, we refresh the TTL automatically. The TTL of false lookups is
2018-03-17 18:26:18 +05:30
never refreshed.
2020-04-08 14:13:33 +05:30
Because of this caching layer, it's possible newly registered GitLab accounts
2021-02-22 17:27:13 +05:30
aren't linked to their corresponding GitHub accounts. This, however, is resolved
after the cached keys expire.
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
The user cache lookup is shared across projects. This means that the greater the number of
projects that are imported, fewer GitHub API calls are needed.
2018-03-17 18:26:18 +05:30
The code for this resides in:
2019-03-02 22:35:43 +05:30
- `lib/gitlab/github_import/user_finder.rb`
- `lib/gitlab/github_import/caching.rb`
2018-03-17 18:26:18 +05:30
## Mapping labels and milestones
To reduce pressure on the database we do not query it when setting labels and
2020-04-08 14:13:33 +05:30
milestones on issues and merge requests. Instead, we cache this data when we
2018-03-17 18:26:18 +05:30
import labels and milestones, then we reuse this cache when assigning them to
issues/merge requests. Similar to the user lookups these cache keys are expired
automatically after 24 hours of not being used.
2020-04-08 14:13:33 +05:30
Unlike the user lookup caches, these label and milestone caches are scoped to the
2018-03-17 18:26:18 +05:30
project that is being imported.
The code for this resides in:
2019-03-02 22:35:43 +05:30
- `lib/gitlab/github_import/label_finder.rb`
- `lib/gitlab/github_import/milestone_finder.rb`
- `lib/gitlab/github_import/caching.rb`
2021-09-30 23:02:18 +05:30
## Logs
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/48512/diffs) in GitLab 13.7.
> - Number of imported objects [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/64256) in GitLab 14.1.
2021-10-27 15:23:28 +05:30
> - `Gitlab::GithubImport::Logger` [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/65968) in GitLab 14.2.
> - `import_source` [renamed](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/67726) to `import_type` in GitLab 14.2.
2021-09-30 23:02:18 +05:30
The import progress can be checked in the `logs/importer.log` file. Each relevant import is logged
2021-10-27 15:23:28 +05:30
with `"import_type": "github"` and the `"project_id"`.
2021-09-30 23:02:18 +05:30
The last log entry reports the number of objects fetched and imported:
```json
{
"message": "GitHub project import finished",
"duration_s": 347.25,
"objects_imported": {
"fetched": {
"diff_note": 93,
"issue": 321,
"note": 794,
"pull_request": 108,
"pull_request_merged_by": 92,
"pull_request_review": 81
},
"imported": {
"diff_note": 93,
"issue": 321,
"note": 794,
"pull_request": 108,
"pull_request_merged_by": 92,
"pull_request_review": 81
}
},
"import_source": "github",
"project_id": 47,
"import_stage": "Gitlab::GithubImport::Stage::FinishImportWorker"
}
```
2021-11-11 11:23:49 +05:30
## Metrics dashboards
To assess the GitHub importer health, the [GitHub importer dashboard](https://dashboards.gitlab.net/d/importers-github-importer/importers-github-importer)
provides information about the total number of objects fetched vs. imported over time.