2024-08-29 08:34:18 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2024-08-29 09:11:19 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2024-08-29 08:34:18 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2024-08-29 09:11:19 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2024-08-29 08:34:18 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-09-04 06:30:56 +00:00
|
|
|
var ErrRebuildInProgress = errors.New("shard rebuild in progress")
|
|
|
|
|
2024-08-29 08:34:18 +00:00
|
|
|
type RebuildWorkerLimiter interface {
|
|
|
|
AcquireWorkSlot(ctx context.Context) error
|
|
|
|
ReleaseWorkSlot()
|
|
|
|
}
|
|
|
|
|
|
|
|
type rebuildLimiter struct {
|
|
|
|
semaphore chan struct{}
|
|
|
|
}
|
|
|
|
|
2024-08-29 10:51:09 +00:00
|
|
|
func NewRebuildLimiter(workersCount uint32) RebuildWorkerLimiter {
|
2024-08-29 08:34:18 +00:00
|
|
|
return &rebuildLimiter{
|
|
|
|
semaphore: make(chan struct{}, workersCount),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *rebuildLimiter) AcquireWorkSlot(ctx context.Context) error {
|
|
|
|
select {
|
|
|
|
case l.semaphore <- struct{}{}:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *rebuildLimiter) ReleaseWorkSlot() {
|
|
|
|
<-l.semaphore
|
|
|
|
}
|
|
|
|
|
|
|
|
type rebuildTask struct {
|
2024-09-04 06:30:56 +00:00
|
|
|
limiter RebuildWorkerLimiter
|
|
|
|
fillPercent int
|
2024-08-29 08:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rebuilder struct {
|
2024-09-04 06:30:56 +00:00
|
|
|
mtx *sync.Mutex
|
|
|
|
wg *sync.WaitGroup
|
|
|
|
cancel func()
|
|
|
|
done chan struct{}
|
|
|
|
tasks chan rebuildTask
|
2024-08-29 08:34:18 +00:00
|
|
|
}
|
|
|
|
|
2024-09-04 06:30:56 +00:00
|
|
|
func newRebuilder() *rebuilder {
|
2024-08-29 08:34:18 +00:00
|
|
|
return &rebuilder{
|
2024-09-04 06:30:56 +00:00
|
|
|
mtx: &sync.Mutex{},
|
|
|
|
wg: &sync.WaitGroup{},
|
|
|
|
tasks: make(chan rebuildTask),
|
2024-08-29 08:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rebuilder) Start(ctx context.Context, bs *blobstor.BlobStor, mb *meta.DB, log *logger.Logger) {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
if r.done != nil {
|
|
|
|
return // already started
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
r.cancel = cancel
|
|
|
|
r.done = make(chan struct{})
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.done:
|
|
|
|
return
|
|
|
|
case t, ok := <-r.tasks:
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2024-09-04 06:30:56 +00:00
|
|
|
runRebuild(ctx, bs, mb, log, t.fillPercent, t.limiter)
|
2024-08-29 08:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func runRebuild(ctx context.Context, bs *blobstor.BlobStor, mb *meta.DB, log *logger.Logger,
|
2024-09-04 06:30:56 +00:00
|
|
|
fillPercent int, limiter RebuildWorkerLimiter,
|
2024-08-29 08:34:18 +00:00
|
|
|
) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2024-10-21 07:22:54 +00:00
|
|
|
log.Info(ctx, logs.BlobstoreRebuildStarted)
|
2024-09-04 06:30:56 +00:00
|
|
|
if err := bs.Rebuild(ctx, &mbStorageIDUpdate{mb: mb}, limiter, fillPercent); err != nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
log.Warn(ctx, logs.FailedToRebuildBlobstore, zap.Error(err))
|
2024-08-29 08:34:18 +00:00
|
|
|
} else {
|
2024-10-21 07:22:54 +00:00
|
|
|
log.Info(ctx, logs.BlobstoreRebuildCompletedSuccessfully)
|
2024-08-29 08:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 06:30:56 +00:00
|
|
|
func (r *rebuilder) ScheduleRebuild(ctx context.Context, limiter RebuildWorkerLimiter, fillPercent int,
|
2024-08-29 08:34:18 +00:00
|
|
|
) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case r.tasks <- rebuildTask{
|
2024-09-04 06:30:56 +00:00
|
|
|
limiter: limiter,
|
|
|
|
fillPercent: fillPercent,
|
2024-08-29 08:34:18 +00:00
|
|
|
}:
|
|
|
|
return nil
|
2024-09-04 06:30:56 +00:00
|
|
|
default:
|
|
|
|
return ErrRebuildInProgress
|
2024-08-29 08:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rebuilder) Stop(log *logger.Logger) {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
if r.done != nil {
|
|
|
|
close(r.done)
|
|
|
|
}
|
|
|
|
if r.cancel != nil {
|
|
|
|
r.cancel()
|
|
|
|
}
|
|
|
|
r.wg.Wait()
|
|
|
|
r.cancel = nil
|
|
|
|
r.done = nil
|
2024-10-21 07:22:54 +00:00
|
|
|
log.Info(context.Background(), logs.BlobstoreRebuildStopped)
|
2024-08-29 08:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errMBIsNotAvailable = errors.New("metabase is not available")
|
|
|
|
|
|
|
|
type mbStorageIDUpdate struct {
|
|
|
|
mb *meta.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *mbStorageIDUpdate) UpdateStorageID(ctx context.Context, addr oid.Address, storageID []byte) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.mb == nil {
|
|
|
|
return errMBIsNotAvailable
|
|
|
|
}
|
|
|
|
|
|
|
|
var prm meta.UpdateStorageIDPrm
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
prm.SetStorageID(storageID)
|
|
|
|
_, err := u.mb.UpdateStorageID(ctx, prm)
|
|
|
|
return err
|
|
|
|
}
|
2024-08-29 09:11:19 +00:00
|
|
|
|
|
|
|
type RebuildPrm struct {
|
|
|
|
ConcurrencyLimiter RebuildWorkerLimiter
|
|
|
|
TargetFillPercent uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shard) ScheduleRebuild(ctx context.Context, p RebuildPrm) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.ScheduleRebuild",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("shard_id", s.ID().String()),
|
|
|
|
attribute.Int64("target_fill_percent", int64(p.TargetFillPercent)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
|
|
|
if s.info.Mode.ReadOnly() {
|
|
|
|
return ErrReadOnlyMode
|
|
|
|
}
|
|
|
|
if s.info.Mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2024-09-04 06:30:56 +00:00
|
|
|
return s.rb.ScheduleRebuild(ctx, p.ConcurrencyLimiter, int(p.TargetFillPercent))
|
2024-08-29 09:11:19 +00:00
|
|
|
}
|