2023-09-19 10:50:14 +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"
|
2023-09-22 10:07:32 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-09-19 10:50:14 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rebuilder struct {
|
2023-10-03 08:58:35 +00:00
|
|
|
mtx *sync.Mutex
|
|
|
|
wg *sync.WaitGroup
|
|
|
|
cancel func()
|
|
|
|
limiter RebuildWorkerLimiter
|
2023-09-19 10:50:14 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 08:58:35 +00:00
|
|
|
func newRebuilder(l RebuildWorkerLimiter) *rebuilder {
|
2023-09-19 10:50:14 +00:00
|
|
|
return &rebuilder{
|
2023-10-03 08:58:35 +00:00
|
|
|
mtx: &sync.Mutex{},
|
|
|
|
wg: &sync.WaitGroup{},
|
|
|
|
cancel: nil,
|
|
|
|
limiter: l,
|
2023-09-19 10:50:14 +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()
|
|
|
|
|
|
|
|
r.start(ctx, bs, mb, log)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rebuilder) start(ctx context.Context, bs *blobstor.BlobStor, mb *meta.DB, log *logger.Logger) {
|
|
|
|
if r.cancel != nil {
|
|
|
|
r.stop(log)
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
r.cancel = cancel
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
|
|
|
|
log.Info(logs.BlobstoreRebuildStarted)
|
2023-10-03 08:58:35 +00:00
|
|
|
if err := bs.Rebuild(ctx, &mbStorageIDUpdate{mb: mb}, r.limiter); err != nil {
|
2023-09-19 10:50:14 +00:00
|
|
|
log.Warn(logs.FailedToRebuildBlobstore, zap.Error(err))
|
|
|
|
} else {
|
|
|
|
log.Info(logs.BlobstoreRebuildCompletedSuccessfully)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rebuilder) Stop(log *logger.Logger) {
|
|
|
|
r.mtx.Lock()
|
|
|
|
defer r.mtx.Unlock()
|
|
|
|
|
|
|
|
r.stop(log)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rebuilder) stop(log *logger.Logger) {
|
|
|
|
if r.cancel == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.cancel()
|
|
|
|
r.wg.Wait()
|
|
|
|
r.cancel = nil
|
|
|
|
log.Info(logs.BlobstoreRebuildStopped)
|
|
|
|
}
|
|
|
|
|
|
|
|
var errMBIsNotAvailable = errors.New("metabase is not available")
|
|
|
|
|
|
|
|
type mbStorageIDUpdate struct {
|
|
|
|
mb *meta.DB
|
|
|
|
}
|
|
|
|
|
2023-09-22 10:07:32 +00:00
|
|
|
func (u *mbStorageIDUpdate) UpdateStorageID(ctx context.Context, addr oid.Address, storageID []byte) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-09-19 10:50:14 +00:00
|
|
|
if u.mb == nil {
|
|
|
|
return errMBIsNotAvailable
|
|
|
|
}
|
2023-09-22 10:07:32 +00:00
|
|
|
|
|
|
|
var prm meta.UpdateStorageIDPrm
|
|
|
|
prm.SetAddress(addr)
|
2023-09-19 10:50:14 +00:00
|
|
|
prm.SetStorageID(storageID)
|
2023-09-22 10:07:32 +00:00
|
|
|
_, err := u.mb.UpdateStorageID(ctx, prm)
|
2023-09-19 10:50:14 +00:00
|
|
|
return err
|
|
|
|
}
|