| 1 |
|
| # Shared::Arena::Lease - one holder at a time, a successor when it dies. |
| 2 |
|
| # |
| 3 |
|
| # Leader election on a fork-shared arena: whoever holds the lease is the one, |
| 4 |
|
| # and when the holder stops renewing - it exited, crashed, or wedged - a |
| 5 |
|
| # successor takes it over. The deadline carries correctness; the pid check only |
| 6 |
|
| # makes a handover happen sooner when the holder is provably gone. |
| 7 |
|
| |
| 8 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena PREFIX = sar_ |
| 9 |
|
| |
| 10 |
|
| # $arena->lease($name, ttl => 30) |
| 11 |
|
| # |
| 12 |
|
| # `ttl` (seconds, default 30) is how long an acquire or renew keeps the lease |
| 13 |
|
| # before it lapses. A holder must renew inside it; the length is a property of |
| 14 |
|
| # this handle, applied by acquire/renew unless one is passed there. |
| 15 |
|
| SV * |
| 16 |
|
| sar_lease(self, name, ...) |
| 17 |
|
| SV *self |
| 18 |
|
| SV *name |
| 19 |
|
| PREINIT: |
| 20 |
|
| sa_region *arena; |
| 21 |
|
| sa_lease *lh; |
| 22 |
|
| const char *nm; |
| 23 |
|
| STRLEN nlen; |
| 24 |
97 |
| double ttl = 30.0; |
| 25 |
97 |
| int err = SA_E_OK; |
| 26 |
|
| I32 i; |
| 27 |
|
| SV *obj; |
| 28 |
|
| CODE: |
| 29 |
97 |
| arena = SA_SELF(sa_region, self); |
| 30 |
97 |
▶ | if (!arena || !arena->map.base) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!arena || !arena->map.base) |
97 |
0 |
97 |
| if (!arena || !arena->map.base) |
0 |
97 |
97 |
|
| 31 |
0 |
| croak("Shared::Arena: this region is released"); |
| 32 |
97 |
| nm = SvPV(name, nlen); |
| 33 |
193 |
▶ | for (i = 2; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i + 1 < items; i += 2) { |
96 |
97 |
193 |
|
| 34 |
96 |
| const char *o = SvPV_nolen(ST(i)); |
| 35 |
96 |
▶ | if (strEQ(o, "ttl")) ttl = SvNV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "ttl")) ttl = SvNV(ST(i + 1)); |
96 |
0 |
96 |
|
| 36 |
|
| } |
| 37 |
97 |
▶ | if (!(ttl > 0.0)) ttl = 30.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(ttl > 0.0)) ttl = 30.0; |
0 |
97 |
97 |
|
| 38 |
|
| #if SA_HAVE_ATOMICS |
| 39 |
97 |
| lh = sa_lease_bind(arena, nm, (uint32_t)nlen, &err); |
| 40 |
97 |
▶ | if (!lh) croak("Shared::Arena: the lease '%s' %s", nm, sa_strerror(err)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lh) croak("Shared::Arena: the lease '%s' %s", nm, sa_strerror(err)); |
0 |
97 |
97 |
|
| 41 |
97 |
| lh->ttl_ms = (uint64_t)(ttl * 1000.0 + 0.5); |
| 42 |
97 |
▶ | if (!lh->ttl_ms) lh->ttl_ms = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lh->ttl_ms) lh->ttl_ms = 1; |
0 |
97 |
97 |
|
| 43 |
|
| #else |
| 44 |
|
| croak("Shared::Arena: leases need atomics this build does not have"); |
| 45 |
|
| #endif |
| 46 |
97 |
| obj = newSV(0); |
| 47 |
97 |
| sv_setref_pv(obj, "Shared::Arena::Lease", (void *)lh); |
| 48 |
97 |
| sv_magicext(SvRV(obj), SvRV(self), PERL_MAGIC_ext, NULL, NULL, 0); |
| 49 |
97 |
| SvREFCNT_inc(SvRV(self)); |
| 50 |
97 |
| RETVAL = obj; |
| 51 |
|
| OUTPUT: |
| 52 |
|
| RETVAL |
| 53 |
|
| |
| 54 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena::Lease PREFIX = sal_ |
| 55 |
|
| |
| 56 |
|
| # Take the lease, or extend it if we already hold it. True when this process |
| 57 |
|
| # holds it afterwards, false when somebody else holds a current one. |
| 58 |
|
| # |
| 59 |
|
| # In list context returns (held, stole): `stole` is true when this acquire took |
| 60 |
|
| # over a holder that had lapsed or died - i.e. you have just become leader after |
| 61 |
|
| # a predecessor failed, not started from a free lease. A caller that must run |
| 62 |
|
| # recovery on takeover keys off it. |
| 63 |
|
| void |
| 64 |
|
| sal_acquire(self, ...) |
| 65 |
|
| SV *self |
| 66 |
|
| PREINIT: |
| 67 |
|
| sa_lease *lh; |
| 68 |
|
| uint64_t ttl_ms; |
| 69 |
207 |
| int held = 0, stole = 0; |
| 70 |
|
| I32 i; |
| 71 |
|
| PPCODE: |
| 72 |
207 |
| lh = SA_SELF(sa_lease, self); |
| 73 |
207 |
▶ | if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
0 |
207 |
207 |
|
| 74 |
207 |
| ttl_ms = lh->ttl_ms; |
| 75 |
207 |
▶ | for (i = 1; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) { |
0 |
207 |
207 |
|
| 76 |
0 |
| const char *o = SvPV_nolen(ST(i)); |
| 77 |
0 |
▶ | if (strEQ(o, "ttl")) ttl_ms = (uint64_t)(SvNV(ST(i + 1)) * 1000.0 + 0.5); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "ttl")) ttl_ms = (uint64_t)(SvNV(ST(i + 1)) * 1000.0 + 0.5); |
0 |
0 |
0 |
|
| 78 |
|
| } |
| 79 |
207 |
▶ | if (!ttl_ms) ttl_ms = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ttl_ms) ttl_ms = 1; |
0 |
207 |
207 |
|
| 80 |
|
| #if SA_HAVE_ATOMICS |
| 81 |
207 |
| held = (sa_lease_acquire(lh, ttl_ms, &stole) == SA_LEASE_HELD); |
| 82 |
|
| #endif |
| 83 |
207 |
▶ | if (GIMME_V == G_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V == G_ARRAY) { |
11 |
196 |
207 |
|
| 84 |
11 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
11 |
11 |
|
| 85 |
11 |
▶ | mPUSHi(held ? 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| mPUSHi(held ? 1 : 0); |
11 |
0 |
11 |
|
| 86 |
11 |
▶ | mPUSHi(stole ? 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| mPUSHi(stole ? 1 : 0); |
11 |
0 |
11 |
|
| 87 |
|
| } |
| 88 |
|
| else { |
| 89 |
196 |
▶ | mPUSHi(held ? 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| mPUSHi(held ? 1 : 0); |
95 |
101 |
196 |
|
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| # Extend a lease we hold. True while we still hold it at the generation we |
| 93 |
|
| # acquired; false once a successor has taken it - a holder that comes back from |
| 94 |
|
| # a long pause finds out here rather than acting on a lease it has lost. |
| 95 |
|
| int |
| 96 |
|
| sal_renew(self, ...) |
| 97 |
|
| SV *self |
| 98 |
|
| PREINIT: |
| 99 |
|
| sa_lease *lh; |
| 100 |
|
| uint64_t ttl_ms; |
| 101 |
|
| I32 i; |
| 102 |
|
| CODE: |
| 103 |
33 |
| lh = SA_SELF(sa_lease, self); |
| 104 |
33 |
▶ | if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
0 |
33 |
33 |
|
| 105 |
33 |
| ttl_ms = lh->ttl_ms; |
| 106 |
33 |
▶ | for (i = 1; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) { |
0 |
33 |
33 |
|
| 107 |
0 |
| const char *o = SvPV_nolen(ST(i)); |
| 108 |
0 |
▶ | if (strEQ(o, "ttl")) ttl_ms = (uint64_t)(SvNV(ST(i + 1)) * 1000.0 + 0.5); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "ttl")) ttl_ms = (uint64_t)(SvNV(ST(i + 1)) * 1000.0 + 0.5); |
0 |
0 |
0 |
|
| 109 |
|
| } |
| 110 |
33 |
▶ | if (!ttl_ms) ttl_ms = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ttl_ms) ttl_ms = 1; |
0 |
33 |
33 |
|
| 111 |
|
| #if SA_HAVE_ATOMICS |
| 112 |
33 |
▶ | RETVAL = (sa_lease_renew(lh, ttl_ms) == SA_LEASE_HELD) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (sa_lease_renew(lh, ttl_ms) == SA_LEASE_HELD) ? 1 : 0; |
19 |
14 |
33 |
|
| 113 |
|
| #else |
| 114 |
|
| RETVAL = 0; |
| 115 |
|
| #endif |
| 116 |
|
| OUTPUT: |
| 117 |
|
| RETVAL |
| 118 |
|
| |
| 119 |
|
| # Give the lease up now, so a successor takes it at once rather than after the |
| 120 |
|
| # deadline. True when we held it and released it. |
| 121 |
|
| int |
| 122 |
|
| sal_release(self) |
| 123 |
|
| SV *self |
| 124 |
|
| PREINIT: |
| 125 |
|
| sa_lease *lh; |
| 126 |
|
| CODE: |
| 127 |
80 |
| lh = SA_SELF(sa_lease, self); |
| 128 |
80 |
▶ | if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
0 |
80 |
80 |
|
| 129 |
|
| #if SA_HAVE_ATOMICS |
| 130 |
80 |
| RETVAL = sa_lease_release(lh); |
| 131 |
|
| #else |
| 132 |
|
| RETVAL = 0; |
| 133 |
|
| #endif |
| 134 |
|
| OUTPUT: |
| 135 |
|
| RETVAL |
| 136 |
|
| |
| 137 |
|
| # Do we hold it right now, at our generation, unlapsed? |
| 138 |
|
| int |
| 139 |
|
| sal_mine(self) |
| 140 |
|
| SV *self |
| 141 |
|
| PREINIT: |
| 142 |
|
| sa_lease *lh; |
| 143 |
|
| CODE: |
| 144 |
38 |
| lh = SA_SELF(sa_lease, self); |
| 145 |
38 |
▶ | if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
0 |
38 |
38 |
|
| 146 |
|
| #if SA_HAVE_ATOMICS |
| 147 |
38 |
| RETVAL = sa_lease_mine(lh); |
| 148 |
|
| #else |
| 149 |
|
| RETVAL = 0; |
| 150 |
|
| #endif |
| 151 |
|
| OUTPUT: |
| 152 |
|
| RETVAL |
| 153 |
|
| |
| 154 |
|
| # The pid that effectively holds the lease now, or 0 when it is free or lapsed. |
| 155 |
|
| # A lapsed holder reports 0, not its stale pid: a successor could take it this |
| 156 |
|
| # instant, so the old pid is not an answer a caller should act on. |
| 157 |
|
| UV |
| 158 |
|
| sal_holder(self) |
| 159 |
|
| SV *self |
| 160 |
|
| PREINIT: |
| 161 |
|
| sa_lease *lh; |
| 162 |
|
| CODE: |
| 163 |
37 |
| lh = SA_SELF(sa_lease, self); |
| 164 |
37 |
▶ | if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
0 |
37 |
37 |
|
| 165 |
|
| #if SA_HAVE_ATOMICS |
| 166 |
37 |
| RETVAL = (UV)sa_lease_holder(lh); |
| 167 |
|
| #else |
| 168 |
|
| RETVAL = 0; |
| 169 |
|
| #endif |
| 170 |
|
| OUTPUT: |
| 171 |
|
| RETVAL |
| 172 |
|
| |
| 173 |
|
| # The fencing token of this handle's tenure: the generation the last acquire |
| 174 |
|
| # won at. Stamp it on writes the lease protects, and the resource can reject a |
| 175 |
|
| # write from a leader that was superseded while it was paused - the late write |
| 176 |
|
| # carries an older fence than the one the successor now uses. |
| 177 |
|
| UV |
| 178 |
|
| sal_fence(self) |
| 179 |
|
| SV *self |
| 180 |
|
| PREINIT: |
| 181 |
|
| sa_lease *lh; |
| 182 |
|
| CODE: |
| 183 |
67 |
| lh = SA_SELF(sa_lease, self); |
| 184 |
67 |
▶ | if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
0 |
67 |
67 |
|
| 185 |
|
| #if SA_HAVE_ATOMICS |
| 186 |
67 |
| RETVAL = (UV)sa_lease_fence(lh); |
| 187 |
|
| #else |
| 188 |
|
| RETVAL = 0; |
| 189 |
|
| #endif |
| 190 |
|
| OUTPUT: |
| 191 |
|
| RETVAL |
| 192 |
|
| |
| 193 |
|
| # name / held / holder / fence / acquires / steals |
| 194 |
|
| # |
| 195 |
|
| # `acquires` is every time the lease changed hands, including the first take and |
| 196 |
|
| # every takeover; `steals` is the subset that took it from a lapsed or dead |
| 197 |
|
| # holder. A steals count climbing in steady state means holders are dying or |
| 198 |
|
| # failing to renew - the number that says the lease is doing its job, or that |
| 199 |
|
| # the ttl is too short for how often the holder actually renews. |
| 200 |
|
| void |
| 201 |
|
| sal_stats(self) |
| 202 |
|
| SV *self |
| 203 |
|
| PREINIT: |
| 204 |
|
| sa_lease *lh; |
| 205 |
|
| PPCODE: |
| 206 |
0 |
| lh = SA_SELF(sa_lease, self); |
| 207 |
0 |
▶ | if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!lh) croak("Shared::Arena::Lease: this lease is released"); |
0 |
0 |
0 |
|
| 208 |
0 |
▶ | EXTEND(SP, 12); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 12); |
0 |
0 |
0 |
|
| 209 |
0 |
| mPUSHp("name", 4); |
| 210 |
0 |
| mPUSHp(lh->slot->name, strlen(lh->slot->name)); |
| 211 |
|
| #if SA_HAVE_ATOMICS |
| 212 |
0 |
| mPUSHp("held", 4); mPUSHi(sa_lease_mine(lh)); |
| 213 |
0 |
| mPUSHp("holder", 6); mPUSHu((UV)sa_lease_holder(lh)); |
| 214 |
0 |
| mPUSHp("fence", 5); mPUSHu((UV)lh->gen); |
| 215 |
0 |
| mPUSHp("acquires", 8); |
| 216 |
0 |
| mPUSHu((UV)sa_at_load64_acq(&lh->slot->acquires)); |
| 217 |
0 |
| mPUSHp("steals", 6); |
| 218 |
0 |
| mPUSHu((UV)sa_at_load64_acq(&lh->slot->steals)); |
| 219 |
|
| #endif |
| 220 |
|
| |
| 221 |
|
| void |
| 222 |
|
| sal_DESTROY(self) |
| 223 |
|
| SV *self |
| 224 |
|
| PREINIT: |
| 225 |
|
| sa_lease *lh; |
| 226 |
|
| CODE: |
| 227 |
97 |
| lh = SA_SELF(sa_lease, self); |
| 228 |
|
| /* A handle going out of scope is NOT a release: the lease in the |
| 229 |
|
| * mapping outlives it, and a process that still holds it keeps holding |
| 230 |
|
| * it until it lapses or calls release. Freeing the handle only drops |
| 231 |
|
| * this process's view. */ |
| 232 |
97 |
▶ | if (lh) { sa_lease_free(lh); sv_setiv(SvRV(self), 0); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (lh) { sa_lease_free(lh); sv_setiv(SvRV(self), 0); } |
97 |
0 |
97 |
|