Documentation
¶
Overview ¶
Package chunk defines the immutable content-addressed units used by compactfs.
Index ¶
- Constants
- Variables
- func CPUCodec(name string) (codec.Codec, error)
- func CanonicalCodec(name string) (string, bool)
- func DecodeVerified(ctx context.Context, ref Ref, encoded []byte) ([]byte, error)
- func Hash(data []byte) string
- func IsRawCodec(name string) bool
- func Key(hash string) (string, error)
- func ValidateHash(hash string) error
- func ValidateLocation(ref Ref) error
- type ChunkingOptions
- type CodecInfo
- type Metrics
- type Ref
- type ReferenceResolver
- type RepresentationPutter
- type Span
- type Splitter
- type Store
- func (s *Store) CheckCodec(name string) error
- func (s *Store) ExistingReference(ctx context.Context, hash string, rawSize int64) (Ref, error)
- func (s *Store) Get(ctx context.Context, ref Ref, dst io.Writer) error
- func (s *Store) Metrics() Metrics
- func (s *Store) OpenRawRange(ctx context.Context, ref Ref, offset, size int64) (*os.File, error)
- func (s *Store) Put(ctx context.Context, raw []byte) (Ref, error)
- func (s *Store) PutFile(ctx context.Context, source *os.File) (Ref, error)
- func (s *Store) PutFileWithHash(ctx context.Context, source *os.File, expectedHash string) (Ref, error)
Constants ¶
const ( Fixed = "fixed" FastCDC = "fastcdc" CDCCodec = "compactfs-cdc-v2" MaxParts = 65536 )
const ( // HashAlgorithm identifies content addressing in backend keys and diagnostics. HashAlgorithm = "xxh3-128" // HashSize is the byte length of an uncompressed chunk's content digest. HashSize = 16 // StagedCodec identifies durable raw data awaiting background encoding. // These references require the matching local writeback cache to mount. StagedCodec = "compactfs-raw-v1" // DefaultSize is the default uncompressed chunk size. It is large enough to // amortize object-storage requests while keeping random reads bounded. DefaultSize = 4 * 1024 * 1024 )
Variables ¶
var ErrChecksumMismatch = errors.New("chunk checksum mismatch")
ErrChecksumMismatch means staged bytes no longer match their content address.
var ErrUnknownCodec = errors.New("unknown chunk codec")
ErrUnknownCodec means a chunk ref selects an encoding that is not part of this reader's codec registry. Data is never decoded with the current encoder as a guess.
Functions ¶
func CanonicalCodec ¶ added in v1.0.1
CanonicalCodec maps hardware producer names to their actual wire format.
func DecodeVerified ¶ added in v1.0.3
DecodeVerified verifies a complete bounded immutable representation before exposing raw bytes. The writer rejects expansion beyond the claimed size.
func Hash ¶
Hash returns the canonical 32-character XXH3-128 digest of raw chunk data. XXH3 is a non-cryptographic hash intended for fast content addressing.
func IsRawCodec ¶ added in v1.0.1
IsRawCodec reports whether a ref can use direct range reads and needs no decoder. Producer aliases such as fpga-raw have the same representation.
func ValidateHash ¶ added in v1.0.2
ValidateHash checks the canonical content digest without constructing a key.
func ValidateLocation ¶ added in v1.0.3
ValidateLocation checks the optional distributed representation locator. A locator is all-or-nothing and only belongs to an immutable leaf; CDC parents carry independently located leaves in their manifest instead.
Types ¶
type ChunkingOptions ¶ added in v1.0.1
ChunkingOptions selects backend chunk boundaries inside each raw write window. Zero selects fixed chunks. CDC never changes the addressing window used by random writes, sparse files, or the durable NVMe cache.
func (ChunkingOptions) ContentSpans ¶ added in v1.0.1
func (o ChunkingOptions) ContentSpans(data []byte) ([]Span, error)
ContentSpans returns deterministic byte ranges. A short final span is allowed; every other span is between MinSize and MaxSize. Boundaries depend on content rather than caller request sizes.
func (ChunkingOptions) Normalize ¶ added in v1.0.1
func (o ChunkingOptions) Normalize(window int) (ChunkingOptions, error)
Normalize validates and fills CDC bounds. The target is a power of two; default bounds are window/32, window/8 and window/2 (rounded as needed).
type CodecInfo ¶ added in v1.0.1
CodecInfo describes one codec name accepted by the frozen chunk format. FPGA names identify the producer but canonicalize to the same wire format as their CPU counterpart.
func CodecRegistry ¶ added in v1.0.1
func CodecRegistry() []CodecInfo
CodecRegistry returns a copy ordered by stored name for CLI and diagnostics.
func LookupCodec ¶ added in v1.0.1
LookupCodec returns the stable registry entry for a stored ref name.
type Metrics ¶
type Metrics struct {
PutOps int64
PutBytes int64
PutNanos int64
PutErrors int64
EncodeOps int64
EncodeBytes int64
EncodeNanos int64
EncodeErrors int64
EncodeBufferAllocations int64
EncodeBufferReuses int64
EncodeBufferPoolBytes int64
BackendStatOps int64
BackendStatNanos int64
BackendStatErrors int64
BackendPutOps int64
BackendPutBytes int64
BackendPutNanos int64
BackendPutErrors int64
BackendImportOps int64
BackendImportBytes int64
BackendImportNanos int64
BackendImportFallbacks int64
BackendRangeOps int64
BackendRangeBytes int64
BackendGetOps int64
BackendGetBytes int64
BackendGetNanos int64
BackendGetErrors int64
CacheGetOps int64
CacheGetBytes int64
CacheGetNanos int64
CacheGetErrors int64
CacheHits int64
CacheMisses int64
CachePutOps int64
CachePutBytes int64
CachePutNanos int64
CachePutErrors int64
DecodeOps int64
DecodeBytes int64
DecodeNanos int64
DecodeErrors int64
}
Metrics is a cumulative snapshot of chunk-store activity. Durations are in nanoseconds so callers can calculate throughput without depending on a particular presentation format.
type Ref ¶
type Ref struct {
Hash string `json:"hash"`
RawSize int64 `json:"raw_size"`
StoredSize int64 `json:"stored_size"`
Codec string `json:"codec"`
ContentType string `json:"content_type,omitempty"`
// DataVolumeID is an optional, stable distributed placement locator. It is
// independent of the content hash and never names a physical Data server.
// EncodedHash pins the exact stored representation without an index lookup.
// Local and staged references leave both fields empty.
DataVolumeID string `json:"data_volume_id,omitempty"`
EncodedHash string `json:"encoded_hash,omitempty"`
// Manifest is a binary immutable list of content-defined child references.
// It is non-empty only for CDCCodec; string keeps Ref comparable for CAS.
Manifest string `json:"-"`
}
Ref identifies one compressed chunk in a file manifest.
type ReferenceResolver ¶ added in v1.0.3
type ReferenceResolver interface {
ResolveChunkReference(context.Context, string, int64) (Ref, error)
}
ReferenceResolver optionally exposes the canonical immutable representation held by a backend's content directory. It avoids probing encoded payloads and carries stable distributed locators without coupling chunk to an RPC.
type RepresentationPutter ¶ added in v1.0.3
RepresentationPutter optionally publishes encoded bytes with their original content identity. Backends with a content directory use this instead of guessing raw hash or codec from opaque Backend.Put bytes. The reader is borrowed, consumed before return and never closed. Publication is immutable; a concurrent winner may return backend.ErrExists and is then resolved through ReferenceResolver. An advertised implementation must not silently fall back to a metadata-free write.
type Splitter ¶
type Splitter struct {
Size int
}
Splitter cuts data into fixed-size, non-overlapping spans. Fixed chunks make random FUSE writes predictable and allow direct chunk-index replacement.
type Store ¶
type Store struct {
// Admission owns one complete top-level upload, including all CDC leaves and
// retries. Put, PutFile and PutFileWithHash share the same admission boundary.
Admission func(context.Context) (func(), error)
Backend backend.Backend
// Chunking is immutable while the store is serving requests. Core validates
// and fixes it in the volume format before starting any worker.
Chunking ChunkingOptions
Codec codec.Codec
Fallback codec.Codec
Cache *cache.Cache
// SkipWriteCache avoids storing a second encoded copy when a durable raw
// writeback cache already owns newly written data. Backend reads may still
// populate Cache for objects whose raw copy has been evicted.
SkipWriteCache bool
// Decoders contains codecs that may be referenced by existing chunk
// metadata even when Codec has since changed. Built-in gzip and zstd
// decoders are supplied automatically; this map is for application codecs.
Decoders map[string]codec.Codec
// MaxAttempts is the total number of attempts for one immutable chunk. A
// value below two disables retries. The filesystem config supplies a
// bounded default for asynchronous write-back.
MaxAttempts int
// RetryBackoff is the delay before the second attempt. Each subsequent
// delay doubles and is capped at one second.
RetryBackoff time.Duration
// contains filtered or unexported fields
}
Store owns immutable compressed chunks and performs content-addressed deduplication. The hash is always over raw bytes, so codec changes do not silently alias incompatible data.
func (*Store) CheckCodec ¶ added in v1.0.1
CheckCodec reports whether this store can decode a persisted ref name. The check includes caller-provided decoders and the frozen built-in registry; it deliberately does not substitute the current primary encoder.
func (*Store) ExistingReference ¶ added in v1.0.3
ExistingReference preserves the codec of the already committed representation.
func (*Store) OpenRawRange ¶
OpenRawRange opens a read-only local object without materializing its whole chunk. Only raw references qualify. The caller owns the immutable fd and may read the validated range [offset, offset+size). ErrNotSupported requests the normal cache/decode path without performing remote I/O. BackendGetBytes and BackendRangeBytes count requested logical range bytes, not disk sector I/O.
func (*Store) PutFile ¶
PutFile stores one immutable regular file as a chunk. Linux maps its bytes read-only instead of copying the whole chunk into another anonymous buffer. Raw representations can use backend.FileImporter to reuse durable file data. Its offset and ownership are preserved. The source inode must remain immutable even after return because a backend may share it with the object; callers may close or unlink the source. Hashing and retries match Put.
func (*Store) PutFileWithHash ¶
func (s *Store) PutFileWithHash(ctx context.Context, source *os.File, expectedHash string) (Ref, error)
PutFileWithHash verifies a staged file against its expected content address before encoding or publishing it. The immutability and ownership requirements are the same as PutFile. A mismatch returns ErrChecksumMismatch without any backend publication; expectedHash must be a canonical XXH3-128 digest.