| 1 |
# Concurrency |
| 2 |
|
| 3 |
## Introduction |
| 4 |
|
| 5 |
Concurrent editing is a key feature of DVCSs -- that's why they're called |
| 6 |
*Distributed* Version Control Systems. A DVCS that didn't let users edit files |
| 7 |
and create commits on separate machines at the same time wouldn't be much |
| 8 |
of a distributed VCS. |
| 9 |
|
| 10 |
When conflicting changes are made in different clones, a DVCS will have to deal |
| 11 |
with that when you push or pull. For example, when using Mercurial, if the |
| 12 |
remote has updated a bookmark called `main` (Mercurial's bookmarks are similar |
| 13 |
to Git's branches) and you had updated the same bookmark locally but made it |
| 14 |
point to a different target, Mercurial would add a bookmark called `main@origin` |
| 15 |
to indicate the conflict. Git instead prevents the conflict by renaming pulled |
| 16 |
branches to `origin/main` whether or not there was a conflict. However, most |
| 17 |
DVCSs treat local concurrency quite differently, typically by using lock files |
| 18 |
to prevent concurrent edits. Unlike those DVCSs, Jujutsu treats concurrent edits |
| 19 |
the same whether they're made locally or remotely. |
| 20 |
|
| 21 |
One problem with using lock files is that they don't work when the clone is in a |
| 22 |
distributed file system. Most clones are of course not stored in distributed |
| 23 |
file systems, but it is a *big* problem when they are (Mercurial repos |
| 24 |
frequently get corrupted, for example). |
| 25 |
|
| 26 |
Another problem with using lock files is related to complexity of |
| 27 |
implementation. The simplest way of using lock files is to take coarse-grained |
| 28 |
locks early: every command that may modify the repo takes a lock at the very |
| 29 |
beginning. However, that means that operations that wouldn't actually conflict |
| 30 |
would still have to wait for each other. The user experience can be improved by |
| 31 |
using finer-grained locks and/or taking the locks later. The drawback of that is |
| 32 |
complexity. For example, you need to verify that any assumptions you made before |
| 33 |
locking are still valid after you take the lock. |
| 34 |
|
| 35 |
To avoid depending on lock files, Jujutsu takes a different approach by |
| 36 |
accepting that concurrent changes can always happen. It instead exposes any |
| 37 |
conflicting changes to the user, much like other DVCSs do for conflicting |
| 38 |
changes made remotely. |
| 39 |
|
| 40 |
### Syncing with `rsync`, NFS, Dropbox, etc |
| 41 |
|
| 42 |
Jujutsu's lock-free concurrency means that it's possible to update copies of the |
| 43 |
clone on different machines and then let `rsync` (or Dropbox, or NFS, etc.) |
| 44 |
merge them. The working copy may mismatch what's supposed to be checked out, but |
| 45 |
no changes to the repo will be lost (added commits, moved bookmarks, etc.). If |
| 46 |
conflicting changes were made, they will appear as conflicts. For example, if a |
| 47 |
bookmark was moved to two different locations, they will appear in `jj log` in |
| 48 |
both locations but with `??` after the name, and `jj status` will also inform |
| 49 |
the user about the conflict. |
| 50 |
|
| 51 |
Note that, for now, there are known bugs in this area. Most notably, with the |
| 52 |
Git backend, [repository corruption is possible because the backend is not |
| 53 |
entirely lock-free](https://github.com/jj-vcs/jj/issues/2193). If that |
| 54 |
corruption occurs, there is an easy recovery path: `jj debug reindex` |
| 55 |
(documented in the linked issue). |
| 56 |
|
| 57 |
Moreover, such use of Jujutsu is not currently thoroughly tested, |
| 58 |
especially in the context of [colocated |
| 59 |
repositories](../glossary.md#colocated-workspaces). While the contents of commits |
| 60 |
should be safe, concurrent modification of a repository from different computers |
| 61 |
might conceivably lose some bookmark pointers. Note that, unlike in pure |
| 62 |
Git, losing a bookmark pointer does not lead to losing commits. |
| 63 |
|
| 64 |
## Operation log |
| 65 |
|
| 66 |
The most important piece in the lock-free design is the "operation log". That is |
| 67 |
what allows us to detect and merge divergent operations. |
| 68 |
|
| 69 |
The operation log is similar to a commit DAG (such as in |
| 70 |
[Git's object model](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects)), |
| 71 |
but each commit object is instead an "operation" and each tree object is instead |
| 72 |
a "view". The view object contains the set of visible head commits, bookmarks, |
| 73 |
tags, and the working-copy commit in each workspace. The operation object |
| 74 |
contains a pointer to the view object (like how commit objects point to tree |
| 75 |
objects), pointers to parent operation(s) (like how commit objects point to |
| 76 |
parent commit(s)), and metadata about the operation. These types are defined |
| 77 |
in `op_store.proto` The operation log is normally linear. |
| 78 |
It becomes non-linear if there are divergent operations. |
| 79 |
|
| 80 |
When a command starts, it loads the repo at the latest operation. Because the |
| 81 |
associated view object completely defines the repo state, the running command |
| 82 |
will not see any changes made by other processes thereafter. When the operation |
| 83 |
completes, it is written with the start operation as parent. The operation |
| 84 |
cannot fail to commit (except for disk failures and such). It is left for the |
| 85 |
next command to notice if there were divergent operations. It will have to be |
| 86 |
able to do that anyway since the concurrent operation could have arrived via a |
| 87 |
distributed file system. This model -- where each operation sees a consistent |
| 88 |
view of the repo and is guaranteed to be able to commit their changes -- greatly |
| 89 |
simplifies the implementation of commands. |
| 90 |
|
| 91 |
It is possible to load the repo at a particular operation with |
| 92 |
`jj --at-operation=<operation ID> <command>`. If the command is mutational, that |
| 93 |
will result in a fork in the operation log. That works exactly the same as if |
| 94 |
any later operations had not existed when the command started. In other words, |
| 95 |
running commands on a repo loaded at an earlier operation works the same way as |
| 96 |
if the operations had been concurrent. This can be useful for simulating |
| 97 |
divergent operations. |
| 98 |
|
| 99 |
### Merging divergent operations |
| 100 |
|
| 101 |
If Jujutsu tries to load the repo and finds multiple heads in the operation log, |
| 102 |
it will do a 3-way merge of the view objects based on their common ancestor |
| 103 |
(possibly several 3-way merges if there were more than two heads). Conflicts |
| 104 |
are recorded in the resulting view object. For example, if bookmark `main` was |
| 105 |
moved from commit A to commit B in one operation and moved to commit C in a |
| 106 |
concurrent operation, then `main` will be recorded as "moved from A to B or C". |
| 107 |
See the `RefTarget` definition in `op_store.proto`. |
| 108 |
|
| 109 |
Because we allow bookmarks (etc.) to be in a conflicted state rather than just |
| 110 |
erroring out when there are multiple heads, the user can continue to use the |
| 111 |
repo, including performing further operations on the repo. Of course, some |
| 112 |
commands will fail when using a conflicted bookmark. For example, |
| 113 |
`jj new main` when `main` is in a conflicted state will result in an error |
| 114 |
telling you that `main` resolved to multiple revisions. |
| 115 |
|
| 116 |
### Storage |
| 117 |
|
| 118 |
The operation objects and view objects are stored in content-addressed storage |
| 119 |
just like Git commits are. That makes them safe to write without locking. |
| 120 |
|
| 121 |
We also need a way of finding the current head of the operation log. We do that |
| 122 |
by keeping the ID of the current head(s) as a file in a directory. The ID is the |
| 123 |
name of the file; it has no contents. When an operation completes, we add a file |
| 124 |
pointing to the new operation and then remove the file pointing to the old |
| 125 |
operation. Writing the new file is what makes the operation visible (if the old |
| 126 |
file didn't get properly deleted, then future readers will take care of that). |
| 127 |
This scheme ensures that transactions are atomic. |