1---
2stage: Create
3group: Gitaly
4info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
5type: reference
6---
7
8# Recovery options
9
10Gitaly Cluster can [recover from certain types of failure](recovery.md).
11
12## Primary Node Failure
13
14Gitaly Cluster recovers from a failing primary Gitaly node by promoting a healthy secondary as the
15new primary.
16
17In GitLab 14.1 and later, Gitaly Cluster:
18
19- Elects a healthy secondary with a fully up to date copy of the repository as the new primary.
20- Repository becomes unavailable if there are no fully up to date copies of it on healthy secondaries.
21
22To minimize data loss in GitLab 13.0 to 14.0, Gitaly Cluster:
23
24- Switches repositories that are outdated on the new primary to [read-only mode](#read-only-mode).
25- Elects the secondary with the least unreplicated writes from the primary to be the new
26  primary. Because there can still be some unreplicated writes,
27  [data loss can occur](#check-for-data-loss).
28
29### Read-only mode
30
31> - Introduced in GitLab 13.0 as [generally available](https://about.gitlab.com/handbook/product/gitlab-the-product/#generally-available-ga).
32> - Between GitLab 13.0 and GitLab 13.2, read-only mode applied to the whole virtual storage and occurred whenever failover occurred.
33> - [In GitLab 13.3 and later](https://gitlab.com/gitlab-org/gitaly/-/issues/2862), read-only mode applies on a per-repository basis and only occurs if a new primary is out of date.
34new primary. If the failed primary contained unreplicated writes, [data loss can occur](#check-for-data-loss).
35> - Removed in GitLab 14.1. Instead, repositories [become unavailable](#unavailable-repositories).
36
37When Gitaly Cluster switches to a new primary in GitLab 13.0 to 14.0, repositories enter
38read-only mode if they are out of date. This can happen after failing over to an outdated
39secondary. Read-only mode eases data recovery efforts by preventing writes that may conflict
40with the unreplicated writes on other nodes.
41
42To enable writes again in GitLab 13.0 to 14.0, an administrator can:
43
441. [Check](#check-for-data-loss) for data loss.
451. Attempt to [recover](#data-recovery) missing data.
461. Either [enable writes](#enable-writes-or-accept-data-loss) in the virtual storage or
47   [accept data loss](#enable-writes-or-accept-data-loss) if necessary, depending on the version of
48   GitLab.
49
50## Unavailable repositories
51
52> - From GitLab 13.0 through 14.0, repositories became read-only if they were outdated on the primary but fully up to date on a healthy secondary. `dataloss` sub-command displays read-only repositories by default through these versions.
53> - Since GitLab 14.1, Praefect contains more responsive failover logic which immediately fails over to one of the fully up to date secondaries rather than placing the repository in read-only mode. Since GitLab 14.1, the `dataloss` sub-command displays repositories which are unavailable due to having no fully up to date copies on healthy Gitaly nodes.
54
55A repository is unavailable if all of its up to date replicas are unavailable. Unavailable repositories are
56not accessible through Praefect to prevent serving stale data that may break automated tooling.
57
58### Check for data loss
59
60The Praefect `dataloss` subcommand identifies:
61
62- Copies of repositories in GitLab 13.0 to GitLab 14.0 that at are likely to be outdated.
63  This can help identify potential data loss after a failover.
64- Repositories in GitLab 14.1 and later that are unavailable. This helps identify potential
65  data loss and repositories which are no longer accessible because all of their up-to-date
66  replicas copies are unavailable.
67
68The following parameters are available:
69
70- `-virtual-storage` that specifies which virtual storage to check. Because they might require
71  an administrator to intervene, the default behavior is to display:
72  - In GitLab 13.0 to 14.0, copies of read-only repositories.
73  - In GitLab 14.1 and later, unavailable repositories.
74- In GitLab 14.1 and later, [`-partially-unavailable`](#unavailable-replicas-of-available-repositories)
75  that specifies whether to include in the output repositories that are available but have
76  some assigned copies that are not available.
77
78NOTE:
79`dataloss` is still in beta and the output format is subject to change.
80
81To check for repositories with outdated primaries or for unavailable repositories, run:
82
83```shell
84sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml dataloss [-virtual-storage <virtual-storage>]
85```
86
87Every configured virtual storage is checked if none is specified:
88
89```shell
90sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml dataloss
91```
92
93Repositories are listed in the output that have either:
94
95- An outdated copy of the repository on the primary, in GitLab 13.0 to GitLab 14.0.
96- No healthy and fully up-to-date copies available, in GitLab 14.1 and later.
97
98The following information is printed for each repository:
99
100- A repository's relative path to the storage directory identifies each repository and groups the related
101  information.
102- The repository's current status is printed in parentheses next to the disk path:
103  - In GitLab 13.0 to 14.0, either `(read-only)` if the repository's primary node is outdated
104    and can't accept writes. Otherwise, `(writable)`.
105  - In GitLab 14.1 and later, `(unavailable)` is printed next to the disk path if the
106    repository is unavailable.
107- The primary field lists the repository's current primary. If the repository has no primary, the field shows
108  `No Primary`.
109- The In-Sync Storages lists replicas which have replicated the latest successful write and all writes
110  preceding it.
111- The Outdated Storages lists replicas which contain an outdated copy of the repository. Replicas which have no copy
112  of the repository but should contain it are also listed here. The maximum number of changes the replica is missing
113  is listed next to replica. It's important to notice that the outdated replicas may be fully up to date or contain
114  later changes but Praefect can't guarantee it.
115
116Additional information includes:
117
118- Whether a node is assigned to host the repository is listed with each node's status.
119  `assigned host` is printed next to nodes that are assigned to store the repository. The
120  text is omitted if the node contains a copy of the repository but is not assigned to store
121  the repository. Such copies aren't kept in sync by Praefect, but may act as replication
122  sources to bring assigned copies up to date.
123- In GitLab 14.1 and later, `unhealthy` is printed next to the copies that are located
124  on unhealthy Gitaly nodes.
125
126Example output:
127
128```shell
129Virtual storage: default
130  Outdated repositories:
131    @hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.git (unavailable):
132      Primary: gitaly-1
133      In-Sync Storages:
134        gitaly-2, assigned host, unhealthy
135      Outdated Storages:
136        gitaly-1 is behind by 3 changes or less, assigned host
137        gitaly-3 is behind by 3 changes or less
138```
139
140A confirmation is printed out when every repository is available. For example:
141
142```shell
143Virtual storage: default
144  All repositories are available!
145```
146
147#### Unavailable replicas of available repositories
148
149NOTE:
150In GitLab 14.0 and earlier, the flag is `-partially-replicated` and the output shows any repositories with assigned nodes with outdated
151copies.
152
153To also list information of repositories which are available but are unavailable from some of the assigned nodes,
154use the `-partially-unavailable` flag.
155
156A repository is available if there is a healthy, up to date replica available. Some of the assigned secondary
157replicas may be temporarily unavailable for access while they are waiting to replicate the latest changes.
158
159```shell
160sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml dataloss [-virtual-storage <virtual-storage>] [-partially-unavailable]
161```
162
163Example output:
164
165```shell
166Virtual storage: default
167  Outdated repositories:
168    @hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.git:
169      Primary: gitaly-1
170      In-Sync Storages:
171        gitaly-1, assigned host
172      Outdated Storages:
173        gitaly-2 is behind by 3 changes or less, assigned host
174        gitaly-3 is behind by 3 changes or less
175```
176
177With the `-partially-unavailable` flag set, a confirmation is printed out if every assigned replica is fully up to
178date and healthy.
179
180For example:
181
182```shell
183Virtual storage: default
184  All repositories are fully available on all assigned storages!
185```
186
187### Check repository checksums
188
189To check a project's repository checksums across on all Gitaly nodes, run the
190[replicas Rake task](../raketasks/praefect.md#replica-checksums) on the main GitLab node.
191
192### Accept data loss
193
194WARNING:
195`accept-dataloss` causes permanent data loss by overwriting other versions of the repository. Data
196[recovery efforts](#data-recovery) must be performed before using it.
197
198If it is not possible to bring one of the up to date replicas back online, you may have to accept data
199loss. When accepting data loss, Praefect marks the chosen replica of the repository as the latest version
200and replicates it to the other assigned Gitaly nodes. This process overwrites any other version of the
201repository so care must be taken.
202
203```shell
204sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml accept-dataloss
205-virtual-storage <virtual-storage> -repository <relative-path> -authoritative-storage <storage-name>
206```
207
208### Enable writes or accept data loss
209
210WARNING:
211`accept-dataloss` causes permanent data loss by overwriting other versions of the repository.
212Data [recovery efforts](#data-recovery) must be performed before using it.
213
214Praefect provides the following subcommands to re-enable writes or accept data loss:
215
216- In GitLab 13.2 and earlier, `enable-writes` to re-enable virtual storage for writes after
217  data recovery attempts:
218
219  ```shell
220  sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml enable-writes -virtual-storage <virtual-storage>
221  ```
222
223- In GitLab 13.3 and later, if it is not possible to bring one of the up to date nodes back
224  online, you may have to accept data loss:
225
226  ```shell
227  sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml accept-dataloss -virtual-storage <virtual-storage> -repository <relative-path> -authoritative-storage <storage-name>
228  ```
229
230  When accepting data loss, Praefect:
231
232  1. Marks the chosen copy of the repository as the latest version.
233  1. Replicates the copy to the other assigned Gitaly nodes.
234
235  This process overwrites any other copy of the repository so care must be taken.
236
237## Data recovery
238
239If a Gitaly node fails replication jobs for any reason, it ends up hosting outdated versions of the
240affected repositories. Praefect provides tools for:
241
242- [Automatic](#automatic-reconciliation) reconciliation, for GitLab 13.4 and later.
243- [Manual](#manual-reconciliation) reconciliation, for:
244  - GitLab 13.3 and earlier.
245  - Repositories upgraded to GitLab 13.4 and later without entries in the `repositories` table. In
246    GitLab 13.6 and later, [a migration is run](https://gitlab.com/gitlab-org/gitaly/-/issues/3033)
247    when Praefect starts for these repositories.
248
249These tools reconcile the outdated repositories to bring them fully up to date again.
250
251### Automatic reconciliation
252
253> [Introduced](https://gitlab.com/gitlab-org/gitaly/-/issues/2717) in GitLab 13.4.
254
255Praefect automatically reconciles repositories that are not up to date. By default, this is done every
256five minutes. For each outdated repository on a healthy Gitaly node, the Praefect picks a
257random, fully up-to-date replica of the repository on another healthy Gitaly node to replicate from. A
258replication job is scheduled only if there are no other replication jobs pending for the target
259repository.
260
261The reconciliation frequency can be changed via the configuration. The value can be any valid
262[Go duration value](https://pkg.go.dev/time#ParseDuration). Values below 0 disable the feature.
263
264Examples:
265
266```ruby
267praefect['reconciliation_scheduling_interval'] = '5m' # the default value
268```
269
270```ruby
271praefect['reconciliation_scheduling_interval'] = '30s' # reconcile every 30 seconds
272```
273
274```ruby
275praefect['reconciliation_scheduling_interval'] = '0' # disable the feature
276```
277
278### Manual reconciliation
279
280WARNING:
281The `reconcile` sub-command was removed in GitLab 14.1. Use [automatic reconciliation](#automatic-reconciliation) instead.
282Manual reconciliation may produce excess replication jobs and is limited in functionality. Manual reconciliation does not
283work when [repository-specific primary nodes](praefect.md#repository-specific-primary-nodes) are enabled.
284
285The Praefect `reconcile` sub-command allows for the manual reconciliation between two Gitaly nodes. The
286command replicates every repository on a later version on the reference storage to the target storage.
287
288```shell
289sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml reconcile -virtual <virtual-storage> -reference <up-to-date-storage> -target <outdated-storage> -f
290```
291
292- Replace the placeholder `<virtual-storage>` with the virtual storage containing the Gitaly node storage to be checked.
293- Replace the placeholder `<up-to-date-storage>` with the Gitaly storage name containing up to date repositories.
294- Replace the placeholder `<outdated-storage>` with the Gitaly storage name containing outdated repositories.
295
296### Manually remove repositories
297
298> - [Introduced](https://gitlab.com/gitlab-org/gitaly/-/merge_requests/3767) in GitLab 14.3.
299> - [Introduced](https://gitlab.com/gitlab-org/gitaly/-/merge_requests/4054) in GitLab 14.6, support for dry-run mode.
300
301The `remove-repository` Praefect sub-command removes a repository from a Gitaly Cluster, and all state associated with a given repository including:
302
303- On-disk repositories on all relevant Gitaly nodes.
304- Any database state tracked by Praefect.
305
306In GitLab 14.6 and later, by default, the command operates in dry-run mode. In earlier versions, the command didn't support dry-run mode. For example:
307
308```shell
309sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml remove-repository -virtual-storage <virtual-storage> -repository <repository>
310```
311
312- Replace `<virtual-storage>` with the name of the virtual storage containing the repository.
313- Replace `<repository>` with the relative path of the repository to remove.
314- In GitLab 14.6 and later, add `-apply` to run the command outside of dry-run mode and remove the repository. For example:
315
316  ```shell
317  sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml remove-repository -virtual-storage <virtual-storage> -repository <repository> -apply
318  ```
319
320- `-virtual-storage` is the virtual storage the repository is located in. Virtual storages are configured in `/etc/gitlab/gitlab.rb` under `praefect['virtual_storages]` and looks like the following:
321
322  ```ruby
323  praefect['virtual_storages'] = {
324    'default' => {
325      ...
326    },
327    'storage-1' => {
328      ...
329    }
330  }
331  ```
332
333  In this example, the virtual storage to specify is `default` or `storage-1`.
334
335- `-repository` is the repository's relative path in the storage [beginning with `@hashed`](../repository_storage_types.md#hashed-storage).
336  For example:
337
338  ```plaintext
339  @hashed/f5/ca/f5ca38f748a1d6eaf726b8a42fb575c3c71f1864a8143301782de13da2d9202b.git
340  ```
341
342Parts of the repository can continue to exist after running `remove-repository`. This can be because of:
343
344- A deletion error.
345- An in-flight RPC call targeting the repository.
346
347If this occurs, run `remove-repository` again.
348
349### Manually list untracked repositories
350
351> [Introduced](https://gitlab.com/gitlab-org/gitaly/-/merge_requests/3926) in GitLab 14.4.
352
353The `list-untracked-repositories` Praefect sub-command lists repositories of the Gitaly Cluster that both:
354
355- Exist for at least one Gitaly storage.
356- Aren't tracked in the Praefect database.
357
358The command outputs:
359
360- Result to `STDOUT` and the command's logs.
361- Errors to `STDERR`.
362
363Each entry is a complete JSON string with a newline at the end (configurable using the
364`-delimiter` flag). For example:
365
366```plaintext
367sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml list-untracked-repositories
368{"virtual_storage":"default","storage":"gitaly-1","relative_path":"@hashed/ab/cd/abcd123456789012345678901234567890123456789012345678901234567890.git"}
369{"virtual_storage":"default","storage":"gitaly-1","relative_path":"@hashed/ab/cd/abcd123456789012345678901234567890123456789012345678901234567891.git"}
370```
371
372### Manually track repositories
373
374> - [Introduced](https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/5658) in GitLab 14.4.
375> - [Introduced](https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/5789) in GitLab 14.6, support for immediate replication.
376
377The `track-repository` Praefect sub-command adds repositories on disk to the Praefect database to be tracked.
378
379```shell
380sudo /opt/gitlab/embedded/bin/praefect -config /var/opt/gitlab/praefect/config.toml track-repository -virtual-storage <virtual-storage> -repository <repository> -replicate-immediately
381```
382
383- `-virtual-storage` is the virtual storage the repository is located in. Virtual storages are configured in `/etc/gitlab/gitlab.rb` under `praefect['virtual_storages]` and looks like the following:
384
385  ```ruby
386  praefect['virtual_storages'] = {
387    'default' => {
388      ...
389    },
390    'storage-1' => {
391      ...
392    }
393  }
394  ```
395
396  In this example, the virtual storage to specify is `default` or `storage-1`.
397
398- `-repository` is the repository's relative path in the storage [beginning with `@hashed`](../repository_storage_types.md#hashed-storage).
399  For example:
400
401  ```plaintext
402  @hashed/f5/ca/f5ca38f748a1d6eaf726b8a42fb575c3c71f1864a8143301782de13da2d9202b.git
403  ```
404
405- `-authoritative-storage` is the storage we want Praefect to treat as the primary. Required if
406  [per-repository replication](praefect.md#configure-replication-factor) is set as the replication strategy.
407- `-replicate-immediately`, available in GitLab 14.6 and later, causes the command to replicate the repository to its secondaries immediately.
408   Otherwise, replication jobs are scheduled for execution in the database and are picked up by a Praefect background process.
409
410The command outputs:
411
412- Results to `STDOUT` and the command's logs.
413- Errors to `STDERR`.
414
415This command fails if:
416
417- The repository is already being tracked by the Praefect database.
418- The repository does not exist on disk.
419