| 1 | # ABSTRACT: Lock management via Git refs | ||
| 2 | |||
| 3 | package App::karr::Lock; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use App::karr::Git; | ||
| 8 | |||
| 9 | |||
| 10 | # Fallback when the caller names no TTL. App::karr::Cmd::Pick passes the board's | ||
| 11 | # `lock_timeout` instead; this only covers direct programmatic use. | ||
| 12 | use constant DEFAULT_TTL => 300; | ||
| 13 | |||
| 14 | # Outside refs/karr/, so no board refspec can reach it -- the same shape as the | ||
| 15 | # refs/karr-remote/ mirror and the refs/karr-conflict/ parking area in | ||
| 16 | # App::karr::Git, and for the same reason: state that must never be published. | ||
| 17 | use constant LOCK_ROOT => 'refs/karr-local/tasks/'; | ||
| 18 | |||
| 19 | # Where locks were before #93. Read, never written. | ||
| 20 | use constant LEGACY_LOCK_ROOT => 'refs/karr/tasks/'; | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my ( $class, %args ) = @_; | ||
| 24 | my $git = $args{git}; | ||
| 25 | unless ($git) { | ||
| 26 | $git = App::karr::Git->new( dir => $args{dir} // '.' ); | ||
| 27 | } | ||
| 28 | return bless { | ||
| 29 | git => $git, | ||
| 30 | task_id => $args{task_id}, | ||
| 31 | ttl => $args{ttl}, | ||
| 32 | }, $class; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub task_id { shift->{task_id} } | ||
| 37 | sub git { shift->{git} } | ||
| 38 | |||
| 39 | |||
| 40 | sub ttl { | ||
| 41 | my ($self) = @_; | ||
| 42 | return defined $self->{ttl} ? $self->{ttl} : DEFAULT_TTL; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub ref_name { | ||
| 47 | my ( $self, $task_id ) = @_; | ||
| 48 | $task_id //= $self->task_id; | ||
| 49 | return LOCK_ROOT . "$task_id/lock"; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub legacy_ref_name { | ||
| 54 | my ( $self, $task_id ) = @_; | ||
| 55 | $task_id //= $self->task_id; | ||
| 56 | return LEGACY_LOCK_ROOT . "$task_id/lock"; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub get { | ||
| 61 | my ( $self, $task_id ) = @_; | ||
| 62 | my $ref = $self->ref_name($task_id); | ||
| 63 | my $content = $self->git->read_ref($ref); | ||
| 64 | return $content; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | # Acquisition is one compare-and-swap per attempt, never a read followed by an | ||
| 69 | # unguarded write. The old version checked the ref and then wrote it, so every | ||
| 70 | # contender passed the check and every contender was told it had the lock -- | ||
| 71 | # 16 forked agents, 16 "acquired" (#46). | ||
| 72 | # | ||
| 73 | # The OID read here is what the write is guarded against, so any outcome other | ||
| 74 | # than "the ref is still exactly as I judged it" fails and is retried against a | ||
| 75 | # fresh read. That is also what makes the expiry/steal below safe: a holder that | ||
| 76 | # refreshes its lock between the moment we decide it is stale and the moment we | ||
| 77 | # take it over moves the ref, and the takeover loses instead of overwriting a | ||
| 78 | # live lock. | ||
| 79 | sub acquire { | ||
| 80 | my ( $self, $task_id, $email ) = @_; | ||
| 81 | $task_id //= $self->task_id; | ||
| 82 | my $ref = $self->ref_name($task_id); | ||
| 83 | my $git = $self->git; | ||
| 84 | |||
| 85 | return $git->retry_contended( "the lock on task $task_id", sub { | ||
| 86 | my ( $oid, $current ) = $git->read_ref_with_oid($ref); | ||
| 87 | |||
| 88 | my $broke = ''; | ||
| 89 | if ( defined $oid && length $current && $current ne $email ) { | ||
| 90 | # Held by somebody else: a final answer, not contention. Say so | ||
| 91 | # rather than dying, which is what the raw libgit2 lock error used | ||
| 92 | # to do -- unless the lock has expired, in which case its holder is | ||
| 93 | # gone and leaving it there would make the task unpickable forever. | ||
| 94 | return ( 0, "locked by $current" ) unless $self->expired($oid); | ||
| 95 | $broke = " (broke stale lock held by $current)"; | ||
| 96 | } | ||
| 97 | |||
| 98 | # $oid undef => create-if-absent, the exclusive case. $oid set => this | ||
| 99 | # is our own lock being refreshed, or a stale one being taken over; | ||
| 100 | # either way the write is guarded against the ref not having moved since | ||
| 101 | # the read above. | ||
| 102 | return () unless $git->write_ref_cas( $ref, $email, $oid ); | ||
| 103 | return ( 1, "acquired$broke" ); | ||
| 104 | } ); | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | # Whether the lock commit $oid points at is older than the TTL. Takes the OID | ||
| 109 | # rather than a task id so the age judged and the OID a takeover is guarded | ||
| 110 | # against are the same revision. | ||
| 111 | sub expired { | ||
| 112 | my ( $self, $oid ) = @_; | ||
| 113 | my $ttl = $self->ttl; | ||
| 114 | return 0 unless $ttl && $ttl > 0; # a zero/negative TTL disables expiry | ||
| 115 | my $held_since = $self->git->commit_time($oid); | ||
| 116 | # No readable timestamp is no evidence the holder is dead, and refusing to | ||
| 117 | # steal is the safe direction -- `karr unlock` is still there. | ||
| 118 | return 0 unless defined $held_since; | ||
| 119 | return ( time - $held_since ) > $ttl ? 1 : 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | # Every lock currently held, with its holder, its age, and whether it has | ||
| 124 | # expired. Reported rather than acted on: seeing who holds what and for how long | ||
| 125 | # is the first half of getting out of a stuck board (#45). | ||
| 126 | # | ||
| 127 | # Both namespaces are walked, and a lock still sitting in the board namespace is | ||
| 128 | # marked rather than hidden. Those are the ones a clone cannot have written | ||
| 129 | # itself -- an older karr, or a pull from a remote that was given one (#93) -- | ||
| 130 | # and leaving them out of the only command that can see locks would make them | ||
| 131 | # invisible as well as inert. | ||
| 132 | sub locks { | ||
| 133 | my ($self) = @_; | ||
| 134 | my @locks; | ||
| 135 | for my $root ( LOCK_ROOT, LEGACY_LOCK_ROOT ) { | ||
| 136 | for my $ref ( $self->git->list_refs($root) ) { | ||
| 137 | next unless $ref =~ m{\A\Q$root\E(\d+)/lock\z}; | ||
| 138 | my $id = $1; | ||
| 139 | my ( $oid, $owner ) = $self->git->read_ref_with_oid($ref); | ||
| 140 | next unless defined $oid; | ||
| 141 | my $held_since = $self->git->commit_time($oid); | ||
| 142 | push @locks, { | ||
| 143 | task_id => 0 + $id, | ||
| 144 | owner => $owner, | ||
| 145 | held_since => $held_since, | ||
| 146 | age => defined $held_since ? time - $held_since : undef, | ||
| 147 | expired => $self->expired($oid) ? 1 : 0, | ||
| 148 | legacy => $root eq LEGACY_LOCK_ROOT ? 1 : 0, | ||
| 149 | }; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | return sort { $a->{task_id} <=> $b->{task_id} | ||
| 153 | || $a->{legacy} <=> $b->{legacy} } @locks; | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | # Drop a lock regardless of who holds it or how old it is. release() refuses to | ||
| 158 | # touch another agent's lock, which is right for the pick path and useless as an | ||
| 159 | # escape hatch -- the whole problem is that the holder is never coming back. | ||
| 160 | # | ||
| 161 | # Clears the legacy ref as well, because that is the only way a board that was | ||
| 162 | # published with locks in it (#93) ever gets clean again. | ||
| 163 | sub break_lock { | ||
| 164 | my ( $self, $task_id ) = @_; | ||
| 165 | $task_id //= $self->task_id; | ||
| 166 | |||
| 167 | my $owner; | ||
| 168 | my $broke = 0; | ||
| 169 | for my $ref ( $self->ref_name($task_id), $self->legacy_ref_name($task_id) ) { | ||
| 170 | next unless $self->git->ref_exists($ref); | ||
| 171 | $owner //= $self->git->read_ref($ref); | ||
| 172 | |||
| 173 | # The return value is deliberately not consulted: after | ||
| 174 | # App::karr::Git::delete_ref, the lock is gone whether this call | ||
| 175 | # removed it or another unlock did in the same breath, and a delete | ||
| 176 | # that was refused raises instead of answering. That is what makes | ||
| 177 | # $broke honest -- until #119 it only meant "the ref was there when I | ||
| 178 | # looked", and a refused delete was announced as a broken lock while | ||
| 179 | # the holder kept the card. Anything that goes back to reading a soft | ||
| 180 | # answer out of delete_ref has to earn this line again. | ||
| 181 | $self->git->delete_ref($ref); | ||
| 182 | $broke = 1; | ||
| 183 | } | ||
| 184 | return ( 0, "not locked" ) unless $broke; | ||
| 185 | return ( 1, $owner ); | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | # Giving a lock back is a guarded delete: the holder is re-read and the removal | ||
| 190 | # is guarded against that exact revision, so a lock that was broken and re-taken | ||
| 191 | # between the two is not dropped by whoever held it before (#94). An unguarded | ||
| 192 | # delete here could evict a live holder that has nothing to do with this call. | ||
| 193 | sub release { | ||
| 194 | my ( $self, $task_id, $email ) = @_; | ||
| 195 | $task_id //= $self->task_id; | ||
| 196 | my $ref = $self->ref_name($task_id); | ||
| 197 | my $git = $self->git; | ||
| 198 | |||
| 199 | return $git->retry_contended( "the lock on task $task_id", sub { | ||
| 200 | my ( $oid, $current ) = $git->read_ref_with_oid($ref); | ||
| 201 | |||
| 202 | # Nothing of ours to give back: already released, already broken, or | ||
| 203 | # expired and taken over. Not an error -- release is the tail of a pick | ||
| 204 | # that has otherwise finished. | ||
| 205 | return ( 1, "released" ) unless defined $oid; | ||
| 206 | return ( 0, "locked by $current" ) | ||
| 207 | if length $current && $current ne $email; | ||
| 208 | |||
| 209 | return () unless $git->delete_ref_cas( $ref, $oid ); | ||
| 210 | return ( 1, "released" ); | ||
| 211 | } ); | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | 1; | ||
| 216 | |||
| 217 | __END__ |