2022-09-21 10:43:31 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2023-12-27 11:37:22 +00:00
|
|
|
"sync"
|
2023-05-19 08:17:19 +00:00
|
|
|
"time"
|
2023-04-12 14:01:29 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
2023-05-19 08:17:19 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/metrics"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-12-27 11:37:22 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-09-21 10:43:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FlushWriteCachePrm groups the parameters of FlushWriteCache operation.
|
|
|
|
type FlushWriteCachePrm struct {
|
2022-09-26 08:54:21 +00:00
|
|
|
shardID *shard.ID
|
|
|
|
ignoreErrors bool
|
2023-12-27 05:20:15 +00:00
|
|
|
seal bool
|
2022-09-21 10:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetShardID is an option to set shard ID.
|
|
|
|
//
|
|
|
|
// Option is required.
|
|
|
|
func (p *FlushWriteCachePrm) SetShardID(id *shard.ID) {
|
|
|
|
p.shardID = id
|
|
|
|
}
|
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
// SetIgnoreErrors sets errors ignore flag.
|
2022-09-26 08:54:21 +00:00
|
|
|
func (p *FlushWriteCachePrm) SetIgnoreErrors(ignore bool) {
|
|
|
|
p.ignoreErrors = ignore
|
|
|
|
}
|
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
// SetSeal sets seal flag.
|
|
|
|
func (p *FlushWriteCachePrm) SetSeal(v bool) {
|
|
|
|
p.seal = v
|
|
|
|
}
|
|
|
|
|
2022-09-21 10:43:31 +00:00
|
|
|
// FlushWriteCacheRes groups the resulting values of FlushWriteCache operation.
|
|
|
|
type FlushWriteCacheRes struct{}
|
|
|
|
|
|
|
|
// FlushWriteCache flushes write-cache on a single shard.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (e *StorageEngine) FlushWriteCache(ctx context.Context, p FlushWriteCachePrm) (FlushWriteCacheRes, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "StorageEngine.FlushWriteCache",
|
|
|
|
trace.WithAttributes(
|
2023-12-27 11:37:22 +00:00
|
|
|
attribute.String("shard_id", p.shardID.String()),
|
2023-04-12 14:01:29 +00:00
|
|
|
attribute.Bool("ignore_errors", p.ignoreErrors),
|
2023-12-27 05:20:15 +00:00
|
|
|
attribute.Bool("seal", p.seal),
|
2023-04-12 14:01:29 +00:00
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-09-21 10:43:31 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
sh, ok := e.shards[p.shardID.String()]
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return FlushWriteCacheRes{}, errShardNotFound
|
|
|
|
}
|
|
|
|
|
2022-09-26 08:54:21 +00:00
|
|
|
var prm shard.FlushWriteCachePrm
|
|
|
|
prm.SetIgnoreErrors(p.ignoreErrors)
|
2023-12-27 05:20:15 +00:00
|
|
|
prm.SetSeal(p.seal)
|
2022-09-26 08:54:21 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
return FlushWriteCacheRes{}, sh.FlushWriteCache(ctx, prm)
|
2022-09-21 10:43:31 +00:00
|
|
|
}
|
2023-05-19 08:17:19 +00:00
|
|
|
|
2023-12-27 11:37:22 +00:00
|
|
|
type SealWriteCachePrm struct {
|
|
|
|
ShardIDs []*shard.ID
|
|
|
|
IgnoreErrors bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShardSealResult struct {
|
|
|
|
ShardID *shard.ID
|
|
|
|
Success bool
|
|
|
|
ErrorMsg string
|
|
|
|
}
|
|
|
|
|
|
|
|
type SealWriteCacheRes struct {
|
|
|
|
ShardResults []ShardSealResult
|
|
|
|
}
|
|
|
|
|
|
|
|
// SealWriteCache flushed all data to blobstore and moves write-cache to degraded read only mode.
|
|
|
|
func (e *StorageEngine) SealWriteCache(ctx context.Context, prm SealWriteCachePrm) (SealWriteCacheRes, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "StorageEngine.SealWriteCache",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.Int("shard_id_count", len(prm.ShardIDs)),
|
|
|
|
attribute.Bool("ignore_errors", prm.IgnoreErrors),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
res := SealWriteCacheRes{
|
|
|
|
ShardResults: make([]ShardSealResult, 0, len(prm.ShardIDs)),
|
|
|
|
}
|
|
|
|
resGuard := &sync.Mutex{}
|
|
|
|
|
|
|
|
eg, egCtx := errgroup.WithContext(ctx)
|
|
|
|
for _, shardID := range prm.ShardIDs {
|
|
|
|
shardID := shardID
|
|
|
|
eg.Go(func() error {
|
|
|
|
e.mtx.RLock()
|
|
|
|
sh, ok := e.shards[shardID.String()]
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
resGuard.Lock()
|
|
|
|
defer resGuard.Unlock()
|
|
|
|
res.ShardResults = append(res.ShardResults, ShardSealResult{
|
|
|
|
ShardID: shardID,
|
|
|
|
ErrorMsg: errShardNotFound.Error(),
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sh.SealWriteCache(egCtx, shard.SealWriteCachePrm{IgnoreErrors: prm.IgnoreErrors})
|
|
|
|
|
|
|
|
resGuard.Lock()
|
|
|
|
defer resGuard.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
res.ShardResults = append(res.ShardResults, ShardSealResult{
|
|
|
|
ShardID: shardID,
|
|
|
|
ErrorMsg: err.Error(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res.ShardResults = append(res.ShardResults, ShardSealResult{
|
|
|
|
ShardID: shardID,
|
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return SealWriteCacheRes{}, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-05-19 08:17:19 +00:00
|
|
|
type writeCacheMetrics struct {
|
|
|
|
shardID string
|
|
|
|
metrics metrics.WriteCacheMetrics
|
|
|
|
}
|
|
|
|
|
2024-02-13 06:09:47 +00:00
|
|
|
func (m *writeCacheMetrics) SetShardID(id string) {
|
|
|
|
m.shardID = id
|
|
|
|
}
|
|
|
|
|
2023-05-19 08:17:19 +00:00
|
|
|
func (m *writeCacheMetrics) Get(d time.Duration, success bool, st writecache.StorageType) {
|
2023-06-13 12:05:45 +00:00
|
|
|
m.metrics.AddMethodDuration(m.shardID, "Get", success, d, st.String())
|
2023-05-19 08:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) Delete(d time.Duration, success bool, st writecache.StorageType) {
|
2023-06-13 12:05:45 +00:00
|
|
|
m.metrics.AddMethodDuration(m.shardID, "Delete", success, d, st.String())
|
2023-05-19 08:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) Put(d time.Duration, success bool, st writecache.StorageType) {
|
2023-06-13 12:05:45 +00:00
|
|
|
m.metrics.AddMethodDuration(m.shardID, "Put", success, d, st.String())
|
2023-05-19 08:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) SetEstimateSize(db, fstree uint64) {
|
|
|
|
m.metrics.SetEstimateSize(m.shardID, db, writecache.StorageTypeDB.String())
|
|
|
|
m.metrics.SetEstimateSize(m.shardID, fstree, writecache.StorageTypeFSTree.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) SetMode(mode mode.Mode) {
|
|
|
|
m.metrics.SetMode(m.shardID, mode.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) SetActualCounters(db, fstree uint64) {
|
|
|
|
m.metrics.SetActualCount(m.shardID, db, writecache.StorageTypeDB.String())
|
|
|
|
m.metrics.SetActualCount(m.shardID, fstree, writecache.StorageTypeFSTree.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) Flush(success bool, st writecache.StorageType) {
|
2023-06-13 12:05:45 +00:00
|
|
|
m.metrics.IncOperationCounter(m.shardID, "Flush", metrics.NullBool{Bool: success, Valid: true}, st.String())
|
2023-05-19 08:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) Evict(st writecache.StorageType) {
|
2023-06-13 12:05:45 +00:00
|
|
|
m.metrics.IncOperationCounter(m.shardID, "Evict", metrics.NullBool{}, st.String())
|
2023-05-19 08:17:19 +00:00
|
|
|
}
|
2023-06-13 12:14:28 +00:00
|
|
|
|
|
|
|
func (m *writeCacheMetrics) Close() {
|
|
|
|
m.metrics.Close(m.shardID)
|
|
|
|
}
|