mirror of
https://github.com/hanzoai/replicate.git
synced 2026-10-30 18:23:20 +00:00
* file: add file:// Leaser for filesystem-backed CAS Mirrors the S3 Leaser CAS semantics using POSIX O_EXCL for initial creation and content-hash ETag matching for renew/release. Atomic rename for the cutover. Same Leaser interface contract — callers swap backends without code changes. Use cases: per-process / single-node leases for dev + tests, container-local coordination where S3 is overkill, and cross-process tests of leader/standby patterns without spinning up MinIO. Tests cover: fresh acquire, expired-lease takeover, contended acquire (LeaseExistsError), renew bumps expiry + etag without touching generation, renew loses race to a stealer (ErrLeaseNotHeld), release roundtrip, release after steal (ErrLeaseNotHeld), and concurrent acquire with exactly one winner across 32 goroutines. * file: harden Leaser — MaxTTL clamp + foreign-owner renew guard Closes red findings #1 (HIGH) and #4 (MEDIUM) on the per-market shard layer's lease coordinator. #1 — Server-side MaxTTL clamp: - Add Leaser.MaxTTL (default 30s when zero). - Acquire + Renew reject TTL > MaxTTL with ErrTTLExceedsMax. Without this, one malicious or buggy caller pins the lease for an arbitrary duration regardless of liveness. - readLease rejects an on-disk ExpiresAt > now + MaxTTL with ErrLeaseCorrupt — defense in depth against a writer that bypassed the API. #4 — Owner check in RenewLease: - ETag CAS alone permits a caller who read another Leaser's ETag from disk to craft a renew call carrying any Owner string. We now read the on-disk record first and reject when existing.Owner != l.Owner with ErrLeaseNotHeld. ETag CAS remains primary; Owner check is belt-and-suspenders. Tests: - TestLeaser_RejectsTTLBeyondMax (acquire, renew, zero MaxTTL falls back to default) - TestLeaser_ReadRejectsCorruptedTTL (hand-write 1h ExpiresAt, Acquire rejects) - TestLeaser_RejectsRenewByForeignOwner (foreign Owner with stolen ETag → ErrLeaseNotHeld) All existing leaser tests continue to pass. * leaser: surface MaxLeaseTTL() on the interface Heartbeat schedulers depend on the leaser's TTL ceiling to validate that their renew cadence is at least 2x tighter than the server-side bound. Without surfacing it, callers either hardcode the bound or compute it from the leaser type — both brittle. The struct field stays MaxTTL (configuration knob); the interface method is MaxLeaseTTL() to avoid the Go name-collision rule ("field and method with the same name"). file:// and s3:// both default to DefaultLeaseMaxTTL (30s) when unset. * dblock: expose SQLite-compatible byte-range lock as a public package External callers that mirror Replica.applyLTXFile (e.g. shard-layer exclusive-lock primitive so their apply pipeline interlocks with SQLite's own locking and with the upstream applier. dblock.LockExclusive / dblock.Unlock target the SQLite pending-byte (0x40000000) and shared-region addresses, matching the existing internal.LockFileExclusive contract used by Replica.applyLTXFile. The internal/lock_*.go shims now forward to dblock so there is one canonical implementation across the module. Behavior identical to the previous internal/ implementation — file-level tests in file/ pass. * chore: bump go to 1.26.4 Picks up upstream security fixes for crypto/x509, mime, net/textproto, crypto/fips140. No dependency bumps — only the toolchain directive. go test -race ./... is green. * chore: fix gofmt + goimports drift across repo Pre-existing formatting drift on main exposed by go-imports-repo pre-commit hook. Pure whitespace alignment + import ordering — no logic changes. Touches 16 files unrelated to the file:// Leaser PR.
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package replicate
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"time"
|
||
)
|
||
|
||
var ErrLeaseNotHeld = errors.New("lease not held")
|
||
|
||
type LeaseExistsError struct {
|
||
Owner string
|
||
ExpiresAt time.Time
|
||
}
|
||
|
||
func (e *LeaseExistsError) Error() string {
|
||
if e.Owner != "" {
|
||
return fmt.Sprintf("lease already held by %s until %s", e.Owner, e.ExpiresAt.Format(time.RFC3339))
|
||
}
|
||
return fmt.Sprintf("lease already held until %s", e.ExpiresAt.Format(time.RFC3339))
|
||
}
|
||
|
||
type Leaser interface {
|
||
Type() string
|
||
// MaxLeaseTTL reports the server-side ceiling on lease lifetime.
|
||
// Implementations MUST surface a non-zero value in production —
|
||
// callers (heartbeat schedulers) depend on it to validate that
|
||
// their renew cadence is at least 2× tighter than the ceiling.
|
||
// Zero means "unbounded" — only acceptable in tests/dev.
|
||
MaxLeaseTTL() time.Duration
|
||
AcquireLease(ctx context.Context) (*Lease, error)
|
||
RenewLease(ctx context.Context, lease *Lease) (*Lease, error)
|
||
ReleaseLease(ctx context.Context, lease *Lease) error
|
||
}
|
||
|
||
type Lease struct {
|
||
Generation int64 `json:"generation"`
|
||
ExpiresAt time.Time `json:"expires_at"`
|
||
Owner string `json:"owner,omitempty"`
|
||
ETag string `json:"-"`
|
||
}
|
||
|
||
func (l *Lease) IsExpired() bool {
|
||
return time.Now().After(l.ExpiresAt)
|
||
}
|
||
|
||
func (l *Lease) TTL() time.Duration {
|
||
return time.Until(l.ExpiresAt)
|
||
}
|