github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/pyramid/params/params.go (about)

     1  package params
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  
     7  	"github.com/mitchellh/go-homedir"
     8  	"github.com/treeverse/lakefs/pkg/block"
     9  	"github.com/treeverse/lakefs/pkg/config"
    10  	"github.com/treeverse/lakefs/pkg/logging"
    11  )
    12  
    13  // RelativePath is the path of the file under TierFS, used in the Eviction interface.
    14  type RelativePath string
    15  
    16  // Eviction abstracts eviction control.
    17  type Eviction interface {
    18  	// Touch indicates the eviction that the file has been used now
    19  	Touch(path RelativePath)
    20  
    21  	// Store orders the eviction to Store the path.
    22  	// returns true iff the eviction accepted the path.
    23  	Store(path RelativePath, filesize int64) bool
    24  }
    25  
    26  // LocalDiskParams is pyramid.FS params that are identical for all file-systems
    27  // in a single lakeFS instance.
    28  type LocalDiskParams struct {
    29  	// TotalAllocatedBytes is the maximum number of bytes an instance of TierFS can
    30  	// allocate to local files.  It is not a hard limit - there may be short period of
    31  	// times where TierFS uses more disk due to ongoing writes and slow disk cleanups.
    32  	TotalAllocatedBytes int64
    33  
    34  	// BaseDir names a directory for TierFS to store local copies of files.
    35  	BaseDir string
    36  }
    37  
    38  type SharedParams struct {
    39  	// Logger receives all logs for this FS.
    40  	Logger logging.Logger
    41  
    42  	// Local holds all local configuration.
    43  	Local LocalDiskParams
    44  
    45  	// Adapter is used to write to underlying storage.
    46  	Adapter block.Adapter
    47  
    48  	// BlockStoragePrefix is the prefix prepended to lakeFS metadata files in
    49  	// the blockstore.
    50  	BlockStoragePrefix string
    51  
    52  	// Eviction is the cache to use to evict objects from local storage.  Only
    53  	// configurable in testing.
    54  	Eviction Eviction
    55  
    56  	// PebbleSSTableCacheSizeBytes is the size (in bytes) of the cache used by the Pebble
    57  	// SSTable library.  This cache is shared between all readers active on the system.
    58  	PebbleSSTableCacheSizeBytes int64
    59  }
    60  
    61  type ExtParams struct {
    62  	SharedParams
    63  
    64  	// RangeAllocationProportion is the proportion allocated to range TierFS instance.
    65  	// The rest of the allocation is to be used by the meta-range TierFS instance.
    66  	// TODO(itai): make this configurable for more than 2 TierFS instances.
    67  	RangeAllocationProportion     float64
    68  	MetaRangeAllocationProportion float64
    69  }
    70  
    71  type InstanceParams struct {
    72  	SharedParams
    73  
    74  	// FSName is the unique filesystem name for this TierFS instance.
    75  	// If two TierFS instances have the same name, behaviour is undefined.
    76  	FSName string
    77  
    78  	// DiskAllocProportion is the proportion of the SharedParams.LocalDiskParams.TotalAllocatedBytes the TierFS instance
    79  	// is allowed to use. Each instance treats the multiplication of the two as its cap.
    80  	DiskAllocProportion float64
    81  }
    82  
    83  var ErrInvalidProportion = fmt.Errorf("%w: total proportion isn't 1.0", config.ErrBadConfiguration)
    84  
    85  // AllocatedBytes returns the maximum bytes an instance of TierFS is allowed to use.
    86  func (ip InstanceParams) AllocatedBytes() int64 {
    87  	return int64(ip.DiskAllocProportion * float64(ip.Local.TotalAllocatedBytes))
    88  }
    89  
    90  func (p ExtParams) WithLogger(logger logging.Logger) ExtParams {
    91  	p.Logger = logger
    92  	return p
    93  }
    94  
    95  // NewCommittedTierFSParams returns parameters for building a tierFS.
    96  // Caller must separately build and populate Adapter.
    97  func NewCommittedTierFSParams(c *config.Config, adapter block.Adapter) (*ExtParams, error) {
    98  	const floatSumTolerance = 1e-6
    99  	rangePro := c.Committed.LocalCache.RangeProportion
   100  	metaRangePro := c.Committed.LocalCache.MetaRangeProportion
   101  	if math.Abs(rangePro+metaRangePro-1) > floatSumTolerance {
   102  		return nil, fmt.Errorf("range_proportion(%f) and metarange_proportion(%f): %w", rangePro, metaRangePro, ErrInvalidProportion)
   103  	}
   104  
   105  	localCacheDir, err := homedir.Expand(c.Committed.LocalCache.Dir)
   106  	if err != nil {
   107  		return nil, fmt.Errorf("expand %s: %w", c.Committed.LocalCache.Dir, err)
   108  	}
   109  
   110  	logger := logging.ContextUnavailable().WithField("module", "pyramid")
   111  	return &ExtParams{
   112  		RangeAllocationProportion:     rangePro,
   113  		MetaRangeAllocationProportion: metaRangePro,
   114  		SharedParams: SharedParams{
   115  			Logger:             logger,
   116  			Adapter:            adapter,
   117  			BlockStoragePrefix: c.Committed.BlockStoragePrefix,
   118  			Local: LocalDiskParams{
   119  				BaseDir:             localCacheDir,
   120  				TotalAllocatedBytes: c.Committed.LocalCache.SizeBytes,
   121  			},
   122  			PebbleSSTableCacheSizeBytes: c.Committed.SSTable.Memory.CacheSizeBytes,
   123  		},
   124  	}, nil
   125  }