orbit-lock 0.3.10

Fleet-shared keyed locks over Orbit shared memory.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Fleet-shared keyed locks over Orbit shared memory.
//!
//! Current ownership lives in a fixed-capacity state table. A dedicated
//! notified ring carries successful transitions for reactive consumers.

mod error;
mod fence;
mod layout;
mod protocol;
mod state;

use std::sync::{Arc, Mutex};
use std::time::Duration;

use bytes::Bytes;
use orbit_core::fleet::FleetLaneCursor;
use orbit_core::{Fleet, NetId64, RingLoss};

pub use error::{Error, Result};
pub use fence::{Fence, FenceToken};
pub use layout::{
    DefaultLockLayout, LOCK_EVENT_RING_CAPACITY, LOCK_EVENT_RING_KIND,
    LOCK_EVENT_RING_PAYLOAD_CAPACITY, LOCK_EVENT_RING_SPEC, LOCK_STATE_KIND, LockLayout,
};
pub use state::{LOCK_STATE_CAPACITY, LOCK_STATE_PAYLOAD_MAX};

#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
pub use orbit_core::RingEventFd;

use layout::LockEventRecord;
use state::LockStateStore;

/// Typed namespace for lock keys.
pub trait LockType {
    const NAMESPACE: &'static str;
}

/// Resource identity used for contention.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct LockKey {
    namespace: Bytes,
    label: Bytes,
}

impl LockKey {
    pub fn new<T: LockType>(label: impl Into<Bytes>) -> Self {
        Self {
            namespace: Bytes::from_static(T::NAMESPACE.as_bytes()),
            label: label.into(),
        }
    }

    /// Construct a dynamically namespaced key.
    pub fn from_parts(namespace: impl Into<Bytes>, label: impl Into<Bytes>) -> Self {
        Self {
            namespace: namespace.into(),
            label: label.into(),
        }
    }

    pub fn namespace(&self) -> &[u8] {
        &self.namespace
    }

    pub fn label(&self) -> &[u8] {
        &self.label
    }
}

/// Opaque caller-chosen identity for one lock tenure.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct LockOwner(Bytes);

impl LockOwner {
    pub fn new(value: impl Into<Bytes>) -> Self {
        Self(value.into())
    }

    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

impl From<Bytes> for LockOwner {
    fn from(value: Bytes) -> Self {
        Self(value)
    }
}

impl From<String> for LockOwner {
    fn from(value: String) -> Self {
        Self(Bytes::from(value))
    }
}

impl From<&str> for LockOwner {
    fn from(value: &str) -> Self {
        Self(Bytes::copy_from_slice(value.as_bytes()))
    }
}

/// One current lock tenure returned by the authoritative table.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LockLease {
    pub lock_id: NetId64,
    pub key: LockKey,
    pub owner: LockOwner,
    /// Host-monotonic milliseconds at which this tenure began.
    pub acquired_at_ms: u64,
    /// Host-monotonic expiry deadline.
    pub expires_at_ms: u64,
    pub state_revision: u64,
}

impl LockLease {
    /// Strictly increasing token for successive lock tenures.
    pub fn fencing_token(&self) -> FenceToken {
        FenceToken::new(self.lock_id.counter())
    }

    pub fn is_expired_at(&self, now_ms: u64) -> bool {
        self.expires_at_ms <= now_ms
    }
}

/// Result of one atomic acquisition attempt.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LockAcquire {
    Acquired(LockLease),
    Occupied(LockLease),
}

impl LockAcquire {
    pub fn acquired(self) -> Option<LockLease> {
        match self {
            Self::Acquired(lease) => Some(lease),
            Self::Occupied(_) => None,
        }
    }

    pub fn holder(&self) -> &LockLease {
        match self {
            Self::Acquired(lease) | Self::Occupied(lease) => lease,
        }
    }
}

/// One successful current-state change.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LockTransition {
    Acquired(LockLease),
    Renewed(LockLease),
    Released {
        lock_id: NetId64,
        key: LockKey,
        owner: LockOwner,
        state_revision: u64,
    },
}

impl LockTransition {
    pub fn lock_id(&self) -> NetId64 {
        match self {
            Self::Acquired(lease) | Self::Renewed(lease) => lease.lock_id,
            Self::Released { lock_id, .. } => *lock_id,
        }
    }

    pub fn key(&self) -> &LockKey {
        match self {
            Self::Acquired(lease) | Self::Renewed(lease) => &lease.key,
            Self::Released { key, .. } => key,
        }
    }

    pub fn owner(&self) -> &LockOwner {
        match self {
            Self::Acquired(lease) | Self::Renewed(lease) => &lease.owner,
            Self::Released { owner, .. } => owner,
        }
    }

    pub fn state_revision(&self) -> u64 {
        match self {
            Self::Acquired(lease) | Self::Renewed(lease) => lease.state_revision,
            Self::Released { state_revision, .. } => *state_revision,
        }
    }
}

/// Transition decoded from the notified ring.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LockEvent {
    pub event_id: NetId64,
    pub transition: LockTransition,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LockCursor {
    inner: FleetLaneCursor,
}

/// Result of advancing one lock-event cursor.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LockPoll {
    pub events: Vec<LockEvent>,
    pub loss: RingLoss,
    pub malformed: u64,
}

impl LockPoll {
    pub fn is_empty(&self) -> bool {
        self.events.is_empty() && self.loss.is_empty() && self.malformed == 0
    }
}

/// Fleet-shared lock handle. Clones share one event cursor.
#[derive(Clone)]
pub struct Lock<L: LockLayout = DefaultLockLayout> {
    fleet: Arc<Fleet>,
    state: LockStateStore<L>,
    cursor: Arc<Mutex<LockCursor>>,
}

impl Lock<DefaultLockLayout> {
    /// Subscribe after all currently committed transition events.
    pub fn new(fleet: Arc<Fleet>) -> Result<Self> {
        Self::open(fleet, false)
    }

    /// Replay retained transitions. Intended for tests and diagnostics.
    pub fn replay_retained(fleet: Arc<Fleet>) -> Result<Self> {
        Self::open(fleet, true)
    }
}

impl<L: LockLayout> Lock<L> {
    /// Subscribe with a custom storage layout.
    pub fn with_layout(fleet: Arc<Fleet>) -> Result<Self> {
        Self::open(fleet, false)
    }

    /// Replay retained transitions with a custom storage layout.
    /// Intended for tests and diagnostics.
    pub fn replay_layout(fleet: Arc<Fleet>) -> Result<Self> {
        Self::open(fleet, true)
    }

    fn open(fleet: Arc<Fleet>, replay_retained: bool) -> Result<Self> {
        layout::validate::<L>()?;
        let state = LockStateStore::new(&fleet);
        let cursor = LockCursor {
            inner: if replay_retained {
                fleet.lane_cursor_from_start::<LockEventRecord<L>>()
            } else {
                fleet.lane_cursor_at_head::<LockEventRecord<L>>()
            },
        };
        Ok(Self {
            fleet,
            state,
            cursor: Arc::new(Mutex::new(cursor)),
        })
    }

    pub fn try_acquire(
        &self,
        key: &LockKey,
        owner: &LockOwner,
        ttl: Duration,
    ) -> Result<LockAcquire> {
        let now_ms = monotonic_ms()?;
        let expires_at_ms = deadline(now_ms, ttl)?;
        self.state
            .acquire(key, owner, now_ms, expires_at_ms, |transition| {
                self.publish(transition).map(|_| ())
            })
    }

    /// Renew only if this exact tenure is still current.
    pub fn renew(&self, lease: &LockLease, ttl: Duration) -> Result<Option<LockLease>> {
        let now_ms = monotonic_ms()?;
        let expires_at_ms = deadline(now_ms, ttl)?;
        self.state
            .renew_id(lease, now_ms, expires_at_ms, |transition| {
                self.publish(transition).map(|_| ())
            })
    }

    /// Renew by a caller-retained owner identity.
    pub fn renew_owned(
        &self,
        key: &LockKey,
        owner: &LockOwner,
        ttl: Duration,
    ) -> Result<Option<LockLease>> {
        let now_ms = monotonic_ms()?;
        let expires_at_ms = deadline(now_ms, ttl)?;
        self.state
            .renew_owner(key, owner, now_ms, expires_at_ms, |transition| {
                self.publish(transition).map(|_| ())
            })
    }

    /// Release only if this exact tenure is still current.
    pub fn release(&self, lease: &LockLease) -> Result<bool> {
        let now_ms = monotonic_ms()?;
        self.state.release_id(lease, now_ms, |transition| {
            self.publish(transition).map(|_| ())
        })
    }

    /// Release only when the current owner bytes match.
    pub fn release_owned(&self, key: &LockKey, owner: &LockOwner) -> Result<bool> {
        let now_ms = monotonic_ms()?;
        self.state.release_owner(key, owner, now_ms, |transition| {
            self.publish(transition).map(|_| ())
        })
    }

    /// Release the current tenure without checking its owner.
    ///
    /// This is the explicit administrative escape hatch required by lock
    /// providers such as Laravel's `forceRelease`. Normal callers should use
    /// [`Self::release`] or [`Self::release_owned`].
    pub fn force_release(&self, key: &LockKey) -> Result<bool> {
        let now_ms = monotonic_ms()?;
        self.state.force_release(key, now_ms, |transition| {
            self.publish(transition).map(|_| ())
        })
    }

    /// Read authoritative current state for one key.
    pub fn current(&self, key: &LockKey) -> Result<Option<LockLease>> {
        self.state.current(key, monotonic_ms()?)
    }

    /// Restore the current tenure only when its owner bytes match.
    pub fn restore(&self, key: &LockKey, owner: &LockOwner) -> Result<Option<LockLease>> {
        Ok(self
            .current(key)?
            .filter(|lease| lease.owner.as_bytes() == owner.as_bytes()))
    }

    /// Drain every currently visible transition from all writer lanes.
    pub fn poll(&self) -> LockPoll {
        let raw = {
            let mut cursor = self
                .cursor
                .lock()
                .unwrap_or_else(|error| error.into_inner());
            self.fleet
                .poll_lanes::<LockEventRecord<L>>(&mut cursor.inner)
        };
        let mut events = Vec::with_capacity(raw.frames.len());
        let mut malformed = 0;
        for frame in raw.frames {
            match protocol::decode(&frame) {
                Some(event) => events.push(event),
                None => malformed += 1,
            }
        }
        events.sort_by_key(|event| event.transition.state_revision());
        LockPoll {
            events,
            loss: raw.loss,
            malformed,
        }
    }

    /// Clear lock state and transition history during quiescent owner boot.
    pub fn reset_transport(&self) -> Result<()> {
        let mut cursor = self
            .cursor
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        self.state.reset()?;
        self.fleet.reset_ring::<LockEventRecord<L>>()?;
        cursor.inner = self.fleet.lane_cursor_at_head::<LockEventRecord<L>>();
        Ok(())
    }

    /// Remove the state and event SHM objects.
    #[cfg(unix)]
    pub fn unlink(&self) -> Result<()> {
        let ring = self.fleet.shm_ring::<LockEventRecord<L>>()?;
        let ring_result = ring.unlink();
        let state_result = self.state.unlink();
        ring_result?;
        state_result
    }

    /// Create this process' readiness fd for lock transitions.
    #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
    pub fn event_fd(&self) -> Result<RingEventFd> {
        self.fleet
            .ring_event_fd::<LockEventRecord<L>>()
            .map_err(Error::Io)
    }

    fn publish(&self, transition: &LockTransition) -> Result<LockEvent> {
        let (kind, payload) = protocol::encode::<L>(transition)?;
        let event_id = {
            #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
            {
                self.fleet.publish_notified::<LockEventRecord<L>>(
                    kind,
                    transition.state_revision(),
                    payload,
                )?
            }
            #[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "macos")))]
            {
                self.fleet
                    .publish::<LockEventRecord<L>>(kind, transition.state_revision(), payload)
            }
        };
        Ok(LockEvent {
            event_id,
            transition: transition.clone(),
        })
    }
}

fn deadline(now_ms: u64, ttl: Duration) -> Result<u64> {
    if ttl.is_zero() {
        return Err(Error::TtlZero);
    }
    let ttl_ms = u64::try_from(ttl.as_millis().max(1)).map_err(|_| Error::TtlOverflow)?;
    now_ms.checked_add(ttl_ms).ok_or(Error::TtlOverflow)
}

#[cfg(unix)]
fn monotonic_ms() -> Result<u64> {
    let mut value = std::mem::MaybeUninit::<libc::timespec>::uninit();
    let result = unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, value.as_mut_ptr()) };
    if result != 0 {
        return Err(Error::Io(std::io::Error::last_os_error()));
    }
    let value = unsafe { value.assume_init() };
    let seconds = u64::try_from(value.tv_sec).map_err(|_| Error::TtlOverflow)?;
    let nanos = u64::try_from(value.tv_nsec).map_err(|_| Error::TtlOverflow)?;
    Ok(seconds
        .saturating_mul(1_000)
        .saturating_add(nanos / 1_000_000))
}

#[cfg(not(unix))]
fn monotonic_ms() -> Result<u64> {
    use std::sync::LazyLock;
    use std::time::Instant;

    static START: LazyLock<Instant> = LazyLock::new(Instant::now);
    Ok(START.elapsed().as_millis().min(u128::from(u64::MAX)) as u64)
}