raftapply

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package raftapply applies committed R3a deterministic entry bytes to a local TreeDB replica.

This package owns the committed-bytes apply boundary, deterministic rejection results, the create-collection command lowering slice, LogicalDigestV1, fake bounded stores used by tests, and append-only durable stores for apply progress plus result/idempotency metadata.

DurableApplyProgressStore and DurableApplyResultStore use caller-owned metadata directories. Their default files are apply-progress-v1.log and apply-results-v1.log. Each file has a versioned header and checksummed frames; open rebuilds lookup indexes from the log and fails closed on truncation, corruption, unsupported versions, digest conflicts, idempotency conflicts, or non-monotonic progress metadata.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyCommittedEntryV1

func ApplyCommittedEntryV1(db *backenddb.DB, entryBytes []byte, meta ApplyMetadataV1, opts Options) (raftentry.ApplyResultV1, error)

ApplyCommittedEntryV1 applies committed deterministic entry bytes through a short-lived harness.

func DurableApplyProgressStorePath

func DurableApplyProgressStorePath(dir string) string

DurableApplyProgressStorePath returns the default progress metadata file under a caller-owned Raft/apply metadata directory.

func DurableApplyResultStorePath

func DurableApplyResultStorePath(dir string) string

DurableApplyResultStorePath returns the default result/idempotency metadata file under a caller-owned Raft/apply metadata directory.

func ErrorCodeOf

func ErrorCodeOf(err error) (raftentry.DeterministicErrorCodeV1, bool)

ErrorCodeOf extracts stable deterministic error names returned by this package or by the predecessor raftentry decoder.

Types

type ApplyFaultContextV1

type ApplyFaultContextV1 struct {
	EntryID           raftentry.ApplyEntryID
	CommandDigest     raftentry.CommandDigestV1
	AppliedCommandLSN uint64
}

type ApplyMetadataV1

type ApplyMetadataV1 struct {
	EntryID                 raftentry.ApplyEntryID
	LocalDurabilityBoundary LocalDurabilityBoundaryV1
	SyncLocalCommandWAL     bool

	// CurrentCatalogVersion is the caller-observed local catalog version used
	// to enforce deterministic expected-catalog-version guards before mutation.
	CurrentCatalogVersion    uint64
	HasCurrentCatalogVersion bool

	ScopeRule       raftentry.ScopeRuleV1
	DatabaseScope   string
	CatalogScope    string
	RequestMetadata raftentry.RequestMetadataV1
	ExpectedTarget  *raftentry.TargetIdentityV1
}

ApplyMetadataV1 is explicit per-entry metadata carried beside committed deterministic entry bytes. None of these fields are native-wire request structs or handler inputs.

type ApplyProgressRecordV1

type ApplyProgressRecordV1 struct {
	EntryID           raftentry.ApplyEntryID
	CommandDigest     raftentry.CommandDigestV1
	AppliedCommandLSN uint64
	LogicalDigestV1   LogicalDigestV1
}

ApplyProgressRecordV1 records the apply-progress transition made after an entry has reached the selected local durability and visibility boundary.

type ApplyProgressStore

type ApplyProgressStore interface {
	CheckCanApply(raftentry.ApplyEntryID) error
	CheckCanRecordApplied(ApplyProgressRecordV1) error
	RecordApplied(ApplyProgressRecordV1) error
	LookupApplyProgress(raftentry.ApplyEntryID) (ApplyProgressRecordV1, bool, error)
	LastApplied() (raftentry.ApplyEntryID, bool)
}

ApplyProgressStore checks monotonic apply order and records durable progress.

type ApplyResultRecordV1

type ApplyResultRecordV1 struct {
	EntryID                 raftentry.ApplyEntryID
	CommandDigest           raftentry.CommandDigestV1
	IdempotencyKey          []byte
	AppliedCommandLSN       uint64
	ProgressLogicalDigestV1 LogicalDigestV1
	Result                  raftentry.ApplyResultV1
}

ApplyResultRecordV1 is the fake/durable result-store unit keyed by ApplyEntryID. The result payload is intentionally bounded separately from the deterministic entry bytes.

type ApplyResultStore

type ApplyResultStore interface {
	LookupApplyResult(raftentry.ApplyEntryID) (ApplyResultRecordV1, bool, error)
	LookupApplyResultByIdempotencyKey([]byte) (ApplyResultRecordV1, bool, error)
	CheckCanRecordApplyResult(ApplyResultRecordV1) error
	RecordApplyResult(ApplyResultRecordV1) error
}

ApplyResultStore records deterministic apply results for idempotency/replay.

type CommandWALApplySeam

CommandWALApplySeam is the local command-WAL append/finalize boundary added by commandwalapply. Rejection tests inject a counting seam to prove this boundary is not called before fail-closed validation completes.

type DurableApplyProgressStore

type DurableApplyProgressStore struct {
	// contains filtered or unexported fields
}

DurableApplyProgressStore is an append-only implementation of ApplyProgressStore. It enforces the same strictly-increasing-index and non-decreasing-term rules as MemoryApplyProgressStore, then fsyncs each accepted record by default.

func OpenDurableApplyProgressStore

func OpenDurableApplyProgressStore(dir string, opts DurableApplyStoreOptions) (*DurableApplyProgressStore, error)

OpenDurableApplyProgressStore opens the default progress metadata file in dir.

func OpenDurableApplyProgressStoreFile

func OpenDurableApplyProgressStoreFile(path string, opts DurableApplyStoreOptions) (*DurableApplyProgressStore, error)

OpenDurableApplyProgressStoreFile opens a progress metadata file at path.

func (*DurableApplyProgressStore) CheckCanApply

func (*DurableApplyProgressStore) CheckCanRecordApplied

func (s *DurableApplyProgressStore) CheckCanRecordApplied(record ApplyProgressRecordV1) error

func (*DurableApplyProgressStore) Close

func (s *DurableApplyProgressStore) Close() error

func (*DurableApplyProgressStore) LastApplied

func (*DurableApplyProgressStore) LastAppliedRecord

func (s *DurableApplyProgressStore) LastAppliedRecord() (ApplyProgressRecordV1, bool)

func (*DurableApplyProgressStore) Len

func (s *DurableApplyProgressStore) Len() int

func (*DurableApplyProgressStore) LookupApplyProgress

func (*DurableApplyProgressStore) RecordApplied

func (s *DurableApplyProgressStore) RecordApplied(record ApplyProgressRecordV1) error

type DurableApplyResultStore

type DurableApplyResultStore struct {
	// contains filtered or unexported fields
}

DurableApplyResultStore is an append-only implementation of ApplyResultStore. It persists records before they are inserted into the in-memory lookup maps, and rebuilds those maps from checksummed records when reopened.

func OpenDurableApplyResultStore

func OpenDurableApplyResultStore(dir string, opts DurableApplyStoreOptions) (*DurableApplyResultStore, error)

OpenDurableApplyResultStore opens the default result metadata file in dir.

func OpenDurableApplyResultStoreFile

func OpenDurableApplyResultStoreFile(path string, opts DurableApplyStoreOptions) (*DurableApplyResultStore, error)

OpenDurableApplyResultStoreFile opens a result metadata file at path.

func (*DurableApplyResultStore) CheckCanRecordApplyResult

func (s *DurableApplyResultStore) CheckCanRecordApplyResult(record ApplyResultRecordV1) error

func (*DurableApplyResultStore) Close

func (s *DurableApplyResultStore) Close() error

func (*DurableApplyResultStore) Len

func (s *DurableApplyResultStore) Len() int

func (*DurableApplyResultStore) LookupApplyResult

func (*DurableApplyResultStore) LookupApplyResultByIdempotencyKey

func (s *DurableApplyResultStore) LookupApplyResultByIdempotencyKey(key []byte) (ApplyResultRecordV1, bool, error)

func (*DurableApplyResultStore) RecordApplyResult

func (s *DurableApplyResultStore) RecordApplyResult(record ApplyResultRecordV1) error

type DurableApplyStoreOptions

type DurableApplyStoreOptions struct {
	MaxRecords  int
	MaxIndex    uint64
	DisableSync bool

	// AllowInitialIndexGap accepts a first TreeDB command index above 1 for
	// consensus logs that reserve lower indexes for internal membership entries.
	// Later gaps are accepted when indexes strictly increase.
	AllowInitialIndexGap bool
}

DurableApplyStoreOptions configures the append-only durable R3a metadata stores. By default, records are fsynced as they are appended; tests and microbenchmarks may set DisableSync to isolate encode/index overhead.

type Error

type Error struct {
	Code raftentry.DeterministicErrorCodeV1
	Err  error
}

Error carries stable deterministic apply error names through fake stores and the harness boundary.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type FaultInjector

type FaultInjector interface {
	InjectApplyFault(FaultPointV1, ApplyFaultContextV1) error
}

type FaultPointV1

type FaultPointV1 string
const (
	FaultBeforeLocalWALAppendV1             FaultPointV1 = "before-local-wal-append-v1"
	FaultAfterLocalWALAppendBeforeVisibleV1 FaultPointV1 = "after-local-wal-append-before-visible-v1"
	FaultAfterVisibleBeforeResultRecordV1   FaultPointV1 = "after-visible-before-result-record-v1"
	FaultAfterResultRecordBeforeProgressV1  FaultPointV1 = "after-result-record-before-progress-v1"
	FaultAfterProgressRecordV1              FaultPointV1 = "after-progress-record-v1"
)

type Harness

type Harness struct {
	// contains filtered or unexported fields
}

Harness applies committed deterministic entry bytes to one local DB handle.

func NewHarness

func NewHarness(db *backenddb.DB, opts Options) *Harness

NewHarness constructs an R3a apply harness for committed deterministic bytes.

func (*Harness) ApplyCommittedEntryV1

func (h *Harness) ApplyCommittedEntryV1(entryBytes []byte, meta ApplyMetadataV1) (raftentry.ApplyResultV1, error)

ApplyCommittedEntryV1 decodes, validates, classifies, and applies the first R3a command slice through local command WAL and normal catalog executors.

func (*Harness) PreflightCommandEntryV1

func (h *Harness) PreflightCommandEntryV1(entryBytes []byte, meta ApplyMetadataV1) (PreflightResultV1, error)

PreflightCommandEntryV1 decodes and checks the local deterministic boundary a command would need to pass before visible apply. It does not append local command-WAL frames, record idempotency/results, or advance apply progress.

func (*Harness) PreflightDecodedCommandEntryV1

func (h *Harness) PreflightDecodedCommandEntryV1(entry raftentry.CommandEntryV1, meta ApplyMetadataV1) (PreflightResultV1, error)

PreflightDecodedCommandEntryV1 checks a caller-provided decoded entry against the local deterministic boundary without re-decoding entry bytes.

type LocalDurabilityBoundaryV1

type LocalDurabilityBoundaryV1 string

LocalDurabilityBoundaryV1 names the local durability boundary a committed deterministic entry is allowed to use before it becomes locally visible.

const (
	// LocalDurabilityCommandWALV1 requires a local command-WAL frame before any
	// visible mutation, result/idempotency record, or apply-progress advance.
	LocalDurabilityCommandWALV1 LocalDurabilityBoundaryV1 = "local-command-wal-v1"
)

type LogicalDigestOptionsV1

type LogicalDigestOptionsV1 struct {
	ScopeRule     raftentry.ScopeRuleV1
	DatabaseScope string
	CatalogScope  string
}

type LogicalDigestV1

type LogicalDigestV1 [32]byte

LogicalDigestV1 is a convergence digest over the supported logical catalog state. It intentionally excludes physical root/page IDs, WAL LSNs, value-log RIDs, segment names, and filesystem paths.

func LogicalDigestV1ForDB

func LogicalDigestV1ForDB(db *backenddb.DB, opts LogicalDigestOptionsV1) (LogicalDigestV1, error)

LogicalDigestV1ForDB hashes the canonical catalog-create payload and materialized collection contents for each listed collection plus stable scope identity. The source of truth is the collection catalog API, not local storage layout.

func (LogicalDigestV1) Hex

func (d LogicalDigestV1) Hex() string

type MemoryApplyProgressStore

type MemoryApplyProgressStore struct {
	// contains filtered or unexported fields
}

MemoryApplyProgressStore is a bounded fake apply-progress store for tests and early harness wiring. The first in-memory entry must start at index 1; later entries enforce strictly increasing indexes, allowing gaps, and non-decreasing terms.

func NewMemoryApplyProgressStore

func NewMemoryApplyProgressStore(maxRecords int, maxIndex uint64) *MemoryApplyProgressStore

func (*MemoryApplyProgressStore) CheckCanApply

func (*MemoryApplyProgressStore) CheckCanRecordApplied

func (s *MemoryApplyProgressStore) CheckCanRecordApplied(record ApplyProgressRecordV1) error

func (*MemoryApplyProgressStore) LastApplied

func (*MemoryApplyProgressStore) LastAppliedRecord

func (s *MemoryApplyProgressStore) LastAppliedRecord() (ApplyProgressRecordV1, bool)

func (*MemoryApplyProgressStore) Len

func (s *MemoryApplyProgressStore) Len() int

func (*MemoryApplyProgressStore) LookupApplyProgress

func (*MemoryApplyProgressStore) RecordApplied

func (s *MemoryApplyProgressStore) RecordApplied(record ApplyProgressRecordV1) error

type MemoryApplyResultStore

type MemoryApplyResultStore struct {
	// contains filtered or unexported fields
}

MemoryApplyResultStore is a bounded fake result/idempotency store for tests and early harness wiring. It is not durable.

func NewMemoryApplyResultStore

func NewMemoryApplyResultStore(maxRecords int) *MemoryApplyResultStore

func (*MemoryApplyResultStore) CheckCanRecordApplyResult

func (s *MemoryApplyResultStore) CheckCanRecordApplyResult(record ApplyResultRecordV1) error

func (*MemoryApplyResultStore) Len

func (s *MemoryApplyResultStore) Len() int

func (*MemoryApplyResultStore) LookupApplyResult

func (*MemoryApplyResultStore) LookupApplyResultByIdempotencyKey

func (s *MemoryApplyResultStore) LookupApplyResultByIdempotencyKey(key []byte) (ApplyResultRecordV1, bool, error)

func (*MemoryApplyResultStore) RecordApplyResult

func (s *MemoryApplyResultStore) RecordApplyResult(record ApplyResultRecordV1) error

type Options

type Options struct {
	DecodeLimits        nativewire.Limits
	ProgressStore       ApplyProgressStore
	ResultStore         ApplyResultStore
	CommandWALApplySeam CommandWALApplySeam
	FaultInjector       FaultInjector
}

Options wires the harness to deterministic decode limits, fake or durable stores, and the command-WAL seam. Nil stores are allowed for early tests but mean duplicates/progress cannot be durably recorded.

type PreflightResultV1

type PreflightResultV1 struct {
	KnownIdempotencyReplay bool
}

func PreflightCommandEntryV1

func PreflightCommandEntryV1(db *backenddb.DB, entryBytes []byte, meta ApplyMetadataV1, opts Options) (PreflightResultV1, error)

PreflightCommandEntryV1 validates deterministic/catalog apply acceptability without assigning, recording, or advancing an apply entry ID.

func PreflightDecodedCommandEntryV1

func PreflightDecodedCommandEntryV1(db *backenddb.DB, entry raftentry.CommandEntryV1, meta ApplyMetadataV1, opts Options) (PreflightResultV1, error)

PreflightDecodedCommandEntryV1 validates an entry that the submitter already decoded with the same deterministic limits used for this apply boundary.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL