2023-06-22 11:55:30 +00:00
|
|
|
package writecachebadger
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-16 15:21:10 +00:00
|
|
|
"time"
|
|
|
|
|
2023-06-22 11:55:30 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Option represents write-cache configuration option.
|
|
|
|
type Option func(*options)
|
|
|
|
|
|
|
|
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.
|
2023-08-09 12:54:08 +00:00
|
|
|
blobstor writecache.MainStorage
|
2021-04-06 10:56:06 +00:00
|
|
|
// metabase is the metabase instance.
|
2023-08-09 12:54:08 +00:00
|
|
|
metabase writecache.Metabase
|
2021-04-06 10:56:06 +00:00
|
|
|
// maxObjectSize is the maximum size of the object stored in the write-cache.
|
|
|
|
maxObjectSize 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-10-20 10:40:25 +00:00
|
|
|
// reportError is the function called when encountering disk errors in background workers.
|
|
|
|
reportError func(string, error)
|
2023-05-18 14:19:41 +00:00
|
|
|
// metrics is metrics implementation
|
2023-06-22 11:55:30 +00:00
|
|
|
metrics writecache.Metrics
|
|
|
|
// gcInterval is the interval duration to run the GC cycle.
|
|
|
|
gcInterval time.Duration
|
2023-11-17 14:41:13 +00:00
|
|
|
// disableBackgroundFlush is for testing purposes only.
|
|
|
|
disableBackgroundFlush bool
|
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.
|
2023-08-09 12:54:08 +00:00
|
|
|
func WithBlobstor(bs writecache.MainStorage) Option {
|
2021-04-06 10:56:06 +00:00
|
|
|
return func(o *options) {
|
|
|
|
o.blobstor = bs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMetabase sets metabase.
|
2023-08-09 12:54:08 +00:00
|
|
|
func WithMetabase(db writecache.Metabase) Option {
|
2021-04-06 10:56:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2023-06-22 11:55:30 +00:00
|
|
|
// WithMetrics sets metrics implementation.
|
|
|
|
func WithMetrics(metrics writecache.Metrics) Option {
|
2023-03-21 10:38:44 +00:00
|
|
|
return func(o *options) {
|
2023-06-22 11:55:30 +00:00
|
|
|
o.metrics = metrics
|
2023-03-21 10:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-18 14:19:41 +00:00
|
|
|
|
2023-06-22 11:55:30 +00:00
|
|
|
// WithGCInterval sets the duration of the interval to run GC cycles.
|
|
|
|
func WithGCInterval(d time.Duration) Option {
|
2023-05-18 14:19:41 +00:00
|
|
|
return func(o *options) {
|
2023-06-22 11:55:30 +00:00
|
|
|
o.gcInterval = d
|
2023-05-18 14:19:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-17 14:41:13 +00:00
|
|
|
|
|
|
|
// WithDisableBackgroundFlush disables background flush, for testing purposes only.
|
|
|
|
func WithDisableBackgroundFlush() Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.disableBackgroundFlush = true
|
|
|
|
}
|
|
|
|
}
|