2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2023-03-21 10:38:44 +00:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
2022-06-16 15:21:10 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Option represents write-cache configuration option.
|
|
|
|
type Option func(*options)
|
|
|
|
|
2022-09-21 05:58:05 +00:00
|
|
|
// meta is an interface for a metabase.
|
|
|
|
type metabase interface {
|
|
|
|
Exists(meta.ExistsPrm) (meta.ExistsRes, error)
|
2022-11-17 06:34:15 +00:00
|
|
|
StorageID(meta.StorageIDPrm) (meta.StorageIDRes, error)
|
2022-10-20 12:15:46 +00:00
|
|
|
UpdateStorageID(meta.UpdateStorageIDPrm) (meta.UpdateStorageIDRes, error)
|
2022-09-21 05:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// blob is an interface for the blobstor.
|
|
|
|
type blob interface {
|
|
|
|
Put(common.PutPrm) (common.PutRes, error)
|
|
|
|
NeedsCompression(obj *objectSDK.Object) bool
|
2023-03-13 11:37:35 +00:00
|
|
|
Exists(ctx context.Context, res common.ExistsPrm) (common.ExistsRes, error)
|
2022-09-21 05:58:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
type options struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2021-04-06 10:56:06 +00:00
|
|
|
// path is a path to a directory for write-cache.
|
|
|
|
path string
|
|
|
|
// blobstor is the main persistent storage.
|
2022-09-21 05:58:05 +00:00
|
|
|
blobstor blob
|
2021-04-06 10:56:06 +00:00
|
|
|
// metabase is the metabase instance.
|
2022-09-21 05:58:05 +00:00
|
|
|
metabase metabase
|
2021-04-06 10:56:06 +00:00
|
|
|
// maxObjectSize is the maximum size of the object stored in the write-cache.
|
|
|
|
maxObjectSize uint64
|
|
|
|
// smallObjectSize is the maximum size of the object stored in the database.
|
|
|
|
smallObjectSize uint64
|
|
|
|
// workersCount is the number of workers flushing objects in parallel.
|
|
|
|
workersCount int
|
2021-09-08 09:32:20 +00:00
|
|
|
// maxCacheSize is the maximum total size of all objects saved in cache (DB + FS).
|
|
|
|
// 1 GiB by default.
|
|
|
|
maxCacheSize uint64
|
2022-09-01 05:56:21 +00:00
|
|
|
// objCounters contains atomic counters for the number of objects stored in cache.
|
|
|
|
objCounters counters
|
2022-06-16 15:21:10 +00:00
|
|
|
// maxBatchSize is the maximum batch size for the small object database.
|
|
|
|
maxBatchSize int
|
|
|
|
// maxBatchDelay is the maximum batch wait time for the small object database.
|
|
|
|
maxBatchDelay time.Duration
|
2022-10-28 10:09:38 +00:00
|
|
|
// noSync is true iff FSTree allows unsynchronized writes.
|
|
|
|
noSync bool
|
2022-10-20 10:40:25 +00:00
|
|
|
// reportError is the function called when encountering disk errors in background workers.
|
|
|
|
reportError func(string, error)
|
2023-03-21 10:38:44 +00:00
|
|
|
// openFile is the function called internally by bbolt to open database files. Useful for hermetic testing.
|
|
|
|
openFile func(string, int, fs.FileMode) (*os.File, error)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger sets logger.
|
2022-09-28 07:41:01 +00:00
|
|
|
func WithLogger(log *logger.Logger) Option {
|
2021-04-06 10:56:06 +00:00
|
|
|
return func(o *options) {
|
2022-09-28 07:41:01 +00:00
|
|
|
o.log = &logger.Logger{Logger: log.With(zap.String("component", "WriteCache"))}
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPath sets path to writecache db.
|
|
|
|
func WithPath(path string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBlobstor sets main object storage.
|
|
|
|
func WithBlobstor(bs *blobstor.BlobStor) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.blobstor = bs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMetabase sets metabase.
|
|
|
|
func WithMetabase(db *meta.DB) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.metabase = db
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMaxObjectSize sets maximum object size to be stored in write-cache.
|
|
|
|
func WithMaxObjectSize(sz uint64) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
if sz > 0 {
|
|
|
|
o.maxObjectSize = sz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSmallObjectSize sets maximum object size to be stored in write-cache.
|
|
|
|
func WithSmallObjectSize(sz uint64) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
if sz > 0 {
|
|
|
|
o.smallObjectSize = sz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithFlushWorkersCount(c int) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
if c > 0 {
|
|
|
|
o.workersCount = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 09:32:20 +00:00
|
|
|
|
|
|
|
// WithMaxCacheSize sets maximum write-cache size in bytes.
|
|
|
|
func WithMaxCacheSize(sz uint64) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.maxCacheSize = sz
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 15:21:10 +00:00
|
|
|
|
|
|
|
// WithMaxBatchSize sets max batch size for the small object database.
|
|
|
|
func WithMaxBatchSize(sz int) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
if sz > 0 {
|
|
|
|
o.maxBatchSize = sz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMaxBatchDelay sets max batch delay for the small object database.
|
|
|
|
func WithMaxBatchDelay(d time.Duration) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
if d > 0 {
|
|
|
|
o.maxBatchDelay = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 10:09:38 +00:00
|
|
|
|
|
|
|
// WithNoSync sets an option to allow returning to caller on PUT before write is persisted.
|
|
|
|
// Note, that we use this flag for FSTree only and DO NOT use it for a bolt DB because
|
|
|
|
// we cannot yet properly handle the corrupted database during the startup. This SHOULD NOT
|
|
|
|
// be relied upon and may be changed in future.
|
|
|
|
func WithNoSync(noSync bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.noSync = noSync
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 10:40:25 +00:00
|
|
|
|
|
|
|
// WithReportErrorFunc sets error reporting function.
|
|
|
|
func WithReportErrorFunc(f func(string, error)) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.reportError = f
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 10:38:44 +00:00
|
|
|
|
|
|
|
// WithOpenFile sets the OpenFile function to use internally by bolt. Useful for hermetic testing.
|
|
|
|
func WithOpenFile(f func(string, int, fs.FileMode) (*os.File, error)) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.openFile = f
|
|
|
|
}
|
|
|
|
}
|