cache

package
v0.104.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const CacheDirEnvName = "OCI_CACHE_DIR"

CacheDirEnvName is the name of the environment variable that configures cache directory.

View Source
const GCHighThreshold float64 = 0.85

GCHighThreshold defines the default percent of disk usage which triggers files garbage collection.

View Source
const GCLowThreshold float64 = 0.80

GCLowThreshold defines the default percent of disk usage to which files garbage collection attempts to free.

View Source
const PreservedHitsProportion = 0.5

PreservedHitsProportion defines the default percent of hits that should be preserved.

View Source
const ResetInterval time.Duration = 1 * time.Hour

ResetInterval defines the default interval when the hit reset should run.

Variables

View Source
var (
	// DiskUsage discloses disk used by the blueprint store
	DiskUsage = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: lsv1alpha1.LandscaperMetricsNamespaceName,
			Subsystem: storeSubsystemName,
			Name:      "disk_usage_bytes",
			Help:      "Bytes on disk currently used by blueprint store instance.",
		},
	)

	// StoredItems discloses the number of items stored by the blueprint store.
	StoredItems = prometheus.NewGauge(
		prometheus.GaugeOpts{
			Namespace: lsv1alpha1.LandscaperMetricsNamespaceName,
			Subsystem: storeSubsystemName,
			Name:      "items_total",
			Help:      "Total number of items currently stored by the blueprint store.",
		},
	)

	// CacheMemoryUsage discloses memory used by caches
	CacheMemoryUsage = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: ociClientNamespaceName,
			Subsystem: cacheSubsystemName,
			Name:      "memory_usage_bytes",
			Help:      "Bytes in memory currently used by cache instance.",
		},
		[]string{"id"},
	)

	// CacheDiskUsage discloses disk used by caches
	CacheDiskUsage = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: ociClientNamespaceName,
			Subsystem: cacheSubsystemName,
			Name:      "disk_usage_bytes",
			Help:      "Bytes on disk currently used by cache instance.",
		},
		[]string{"id"},
	)

	// CachedItems discloses the number of items stored by caches
	CachedItems = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: ociClientNamespaceName,
			Subsystem: cacheSubsystemName,
			Name:      "items_total",
			Help:      "Total number of items currently cached by instance.",
		},
		[]string{"id"},
	)

	// CacheHitsDisk discloses the number of hits for items cached on disk
	CacheHitsDisk = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: ociClientNamespaceName,
			Subsystem: cacheSubsystemName,
			Name:      "disk_hits_total",
			Help:      "Total number of hits for items cached on disk by an instance.",
		},
		[]string{"id"},
	)

	// CacheHitsMemory discloses the number of hits for items cached in memory
	CacheHitsMemory = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: ociClientNamespaceName,
			Subsystem: cacheSubsystemName,
			Name:      "memory_hits_total",
			Help:      "Total number of hits for items cached in memory by an instance.",
		},
		[]string{"id"},
	)
)
View Source
var (
	// ErrNotFound is a error that indicates that the file is not cached
	ErrNotFound = errors.New("not cached")
)

Functions

func CalculatePriority

func CalculatePriority(entry IndexEntry, minHits, maxHits int64, oldest, newest time.Time) float64

CalculatePriority calculates the gc priority of a index entry. A lower priority means that is more likely to be deleted.

func InjectCacheInto

func InjectCacheInto(obj interface{}, cache Cache) error

InjectCacheInto injects a cache if the given object implements the InjectCache interface.

func NewCache

func NewCache(log logr.Logger, options ...Option) (*layeredCache, error)

NewCache creates a new cache with the given options. It uses by default a tmp fs

func NewInMemoryCache

func NewInMemoryCache() *inmemoryCache

NewInMemoryCache creates a new in memory cache.

func Path

func Path(desc ocispecv1.Descriptor) string

func RegisterStoreMetrics

func RegisterStoreMetrics(reg prometheus.Registerer)

RegisterStoreMetrics allows to register blueprint store metrics with a given prometheus registerer

Types

type Cache

type Cache interface {
	io.Closer
	Get(desc ocispecv1.Descriptor) (io.ReadCloser, error)
	Add(desc ocispecv1.Descriptor, reader io.ReadCloser) error
}

Cache is the interface for a oci cache

type FileSystem

type FileSystem struct {

	// Configuration
	vfs.FileSystem
	// Size is the size of the filesystem in bytes.
	// If the value is 0 there is no limit and no garbage collection will happen.
	Size int64
	// GCHighThreshold defines the percent of disk usage which triggers files garbage collection.
	GCHighThreshold float64
	// GCLowThreshold defines the percent of disk usage to which files garbage collection attempts to free.
	GCLowThreshold float64
	// ResetInterval defines the interval when the hit reset should run.
	ResetInterval time.Duration
	// PreservedHitsProportion defines the percent of hits that should be preserved.
	PreservedHitsProportion float64
	// contains filtered or unexported fields
}

FileSystem is a internal representation of FileSystem with a optional max size and a garbage collection.

func NewCacheFilesystem

func NewCacheFilesystem(log logr.Logger, fs vfs.FileSystem, gcOpts GarbageCollectionConfiguration) (*FileSystem, error)

NewCacheFilesystem creates a new FileSystem cache. It acts as a replacement for a vfs filesystem that restricts the size of the filesystem. The garbage collection is triggered when a file is created. When the filesystem is not used anymore fs.Close() should be called to gracefully free resources.

func (*FileSystem) Close

func (fs *FileSystem) Close() error

Close implements the io.Closer interface. It should be called when the cache is not used anymore.

func (*FileSystem) Create

func (fs *FileSystem) Create(path string, size int64) (vfs.File, error)

func (*FileSystem) CurrentSize

func (fs *FileSystem) CurrentSize() int64

CurrentSize returns the current size of the filesystem

func (*FileSystem) DeleteAll

func (fs *FileSystem) DeleteAll() error

DeleteAll removes all files in the filesystem

func (*FileSystem) OpenFile

func (fs *FileSystem) OpenFile(name string, flags int, perm os.FileMode) (vfs.File, error)

func (*FileSystem) Remove

func (fs *FileSystem) Remove(name string) error

func (*FileSystem) RunGarbageCollection

func (fs *FileSystem) RunGarbageCollection()

RunGarbageCollection runs the garbage collection of the filesystem. The gc only deletes items when the max size reached a certain threshold. If that threshold is reached the files are deleted with the following priority: - least hits - oldest - random

func (*FileSystem) StartResetInterval

func (fs *FileSystem) StartResetInterval()

StartResetInterval starts the reset counter for the cache hits.

func (*FileSystem) WithMetrics

func (fs *FileSystem) WithMetrics(itemsCount, usage prometheus.Gauge, hits prometheus.Counter)

WithMetrics adds prometheus metrics to the filesystem that are set by the filesystem.

type GarbageCollectionConfiguration

type GarbageCollectionConfiguration struct {
	// Size is the size of the filesystem.
	// If the value is 0 there is no limit and no garbage collection will happen.
	// See the kubernetes quantity docs for detailed description of the format
	// https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go
	Size string
	// GCHighThreshold defines the percent of disk usage which triggers files garbage collection.
	GCHighThreshold float64
	// GCLowThreshold defines the percent of disk usage to which files garbage collection attempts to free.
	GCLowThreshold float64
	// ResetInterval defines the interval when the hit reset should run.
	ResetInterval time.Duration
	// PreservedHitsProportion defines the percent of hits that should be preserved.
	PreservedHitsProportion float64
}

GarbageCollectionConfiguration contains all options for the cache garbage collection.

func (GarbageCollectionConfiguration) ApplyOptions

func (o GarbageCollectionConfiguration) ApplyOptions(fs *FileSystem) error

ApplyOptions parses and applies the options to the filesystem. It also applies defaults

func (GarbageCollectionConfiguration) Merge

Merge merges 2 gc configurations whereas the defined values are overwritten.

type Index

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

func NewIndex

func NewIndex() Index

NewIndex creates a new index structure

func (*Index) Add

func (i *Index) Add(name string, size int64, createdAt time.Time)

Add adds a entry to the index.

func (*Index) DeepCopy

func (i *Index) DeepCopy() *Index

DeepCopy creates a deep copy of the current index.

func (*Index) Get

func (i *Index) Get(name string) IndexEntry

Get return the index entry with the given name.

func (*Index) Hit

func (i *Index) Hit(name string)

Hit increases the hit count for the file.

func (*Index) Len

func (i *Index) Len() int

Len returns the number of items that are currently in the index.

func (*Index) PriorityList

func (i *Index) PriorityList() []IndexEntry

PriorityList returns a entries of all entries sorted by their gc priority. The entry with the lowest priority is the first item.

func (*Index) Remove

func (i *Index) Remove(name string)

Remove removes the entry from the index.

func (*Index) Reset

func (i *Index) Reset()

Reset resets the hit counter for all entries. The reset preserves 20% if the old hits.

type IndexEntry

type IndexEntry struct {
	// Name is the name if the file.
	Name string
	// Size is the size of the file in bytes.
	Size int64
	// Is the number of hits since the last gc
	Hits int64
	// CreatedAt is the time when the file ha been created.
	CreatedAt time.Time
	// HitsSinceLastReset is the number hits since the last reset interval
	HitsSinceLastReset int64
}

type Info

type Info struct {
	// Size is the max size of the filesystem in bytes.
	// If the value is 0 there is no limit and no garbage collection will happen.
	// +optional
	Size int64 `json:"size"`
	// CurrentSize is the current size of the cache
	CurrentSize int64 `json:"currentSize"`
	// ItemsCount is the number of items that are currently managed by the cache.
	ItemsCount int64 `json:"items"`
}

Info contains additional information about the cache

type InfoInterface

type InfoInterface interface {
	Info() (Info, error)
}

InfoInterface describes an interface that can be optionally exposed by a cache to give additional information.

type InjectCache

type InjectCache interface {
	InjectCache(c Cache) error
}

InjectCache is a interface to inject a cache.

type Option

type Option interface {
	ApplyOption(options *Options)
}

Option is the interface to specify different cache options

type Options

type Options struct {
	// InMemoryOverlay specifies if a overlayFs InMemory cache should be used
	InMemoryOverlay bool

	// InMemoryGCConfig defines the garbage collection configuration for the in memory cache.
	InMemoryGCConfig GarbageCollectionConfiguration

	// BasePath specifies the Base path for the os filepath.
	// Will be defaulted to a temp filepath if not specified
	BasePath string

	// BaseGCConfig defines the garbage collection configuration for the in base cache.
	BaseGCConfig GarbageCollectionConfiguration

	// UID is the identity of a cache, if not specified a UID will be generated
	UID string
}

Options contains all oci cache options to configure the oci cache.

func (*Options) ApplyDefaults

func (o *Options) ApplyDefaults()

ApplyDefaults sets defaults for the options.

func (*Options) ApplyOptions

func (o *Options) ApplyOptions(opts []Option) *Options

ApplyOptions applies the given entries options on these options, and then returns itself (for convenient chaining).

type PruneInterface

type PruneInterface interface {
	Prune() error
}

PruneInterface describes an interface that can be optionally exposed by a cache to manually prune the cache.

type WithBaseGCConfig

type WithBaseGCConfig GarbageCollectionConfiguration

WithBaseGCConfig overwrites the base garbage collection settings.

func (WithBaseGCConfig) ApplyOption

func (p WithBaseGCConfig) ApplyOption(options *Options)

type WithBasePath

type WithBasePath string

WithBasePath is the options to specify a base path

func (WithBasePath) ApplyOption

func (p WithBasePath) ApplyOption(options *Options)

type WithBaseSize

type WithBaseSize string

WithBaseSize sets the max size of the base file system. See the kubernetes quantity docs for detailed description of the format https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go

func (WithBaseSize) ApplyOption

func (p WithBaseSize) ApplyOption(options *Options)

type WithGCConfig

type WithGCConfig GarbageCollectionConfiguration

WithGCConfig overwrites the garbage collection settings for all caches.

func (WithGCConfig) ApplyOption

func (p WithGCConfig) ApplyOption(options *Options)

type WithInMemoryGCConfig

type WithInMemoryGCConfig GarbageCollectionConfiguration

WithBaseGCConfig overwrites the in memory garbage collection settings.

func (WithInMemoryGCConfig) ApplyOption

func (p WithInMemoryGCConfig) ApplyOption(options *Options)

type WithInMemoryOverlay

type WithInMemoryOverlay bool

WithInMemoryOverlay is the options to specify the usage of a in memory overlayFs

func (WithInMemoryOverlay) ApplyOption

func (w WithInMemoryOverlay) ApplyOption(options *Options)

type WithInMemoryOverlaySize

type WithInMemoryOverlaySize string

WithInMemoryOverlaySize sets the max size of the overly file system. See the kubernetes quantity docs for detailed description of the format https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go

func (WithInMemoryOverlaySize) ApplyOption

func (p WithInMemoryOverlaySize) ApplyOption(options *Options)

type WithUID

type WithUID string

WithUID is the option to give a cache an identity

func (WithUID) ApplyOption

func (p WithUID) ApplyOption(options *Options)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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