2022-06-01 07:06:32 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2022-07-27 18:38:28 +00:00
|
|
|
"errors"
|
2023-02-15 14:53:42 +00:00
|
|
|
"sync"
|
2022-07-27 18:38:28 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
storagelog "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/log"
|
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-06-01 07:06:32 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2022-11-17 08:59:27 +00:00
|
|
|
"go.uber.org/zap"
|
2022-06-01 07:06:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *cache) initFlushMarks() {
|
2023-02-15 14:53:42 +00:00
|
|
|
var localWG sync.WaitGroup
|
|
|
|
|
|
|
|
localWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer localWG.Done()
|
|
|
|
|
|
|
|
c.fsTreeFlushMarkUpdate()
|
|
|
|
}()
|
|
|
|
|
|
|
|
localWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer localWG.Done()
|
|
|
|
|
|
|
|
c.dbFlushMarkUpdate()
|
|
|
|
}()
|
|
|
|
|
|
|
|
c.initWG.Add(1)
|
|
|
|
c.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
defer c.initWG.Done()
|
|
|
|
|
|
|
|
localWG.Wait()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.stopInitCh:
|
|
|
|
return
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
c.initialized.Store(true)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
var errStopIter = errors.New("stop iteration")
|
|
|
|
|
|
|
|
func (c *cache) fsTreeFlushMarkUpdate() {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Info(logs.WritecacheFillingFlushMarksForObjectsInFSTree)
|
2022-06-01 07:06:32 +00:00
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
var prm common.IteratePrm
|
|
|
|
prm.LazyHandler = func(addr oid.Address, _ func() ([]byte, error)) error {
|
2023-02-15 14:53:42 +00:00
|
|
|
select {
|
|
|
|
case <-c.closeCh:
|
|
|
|
return errStopIter
|
|
|
|
case <-c.stopInitCh:
|
|
|
|
return errStopIter
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2022-11-17 08:59:27 +00:00
|
|
|
flushed, needRemove := c.flushStatus(addr)
|
|
|
|
if flushed {
|
2022-06-01 07:06:32 +00:00
|
|
|
c.store.flushed.Add(addr.EncodeToString(), true)
|
2022-11-17 08:59:27 +00:00
|
|
|
if needRemove {
|
|
|
|
var prm common.DeletePrm
|
|
|
|
prm.Address = addr
|
|
|
|
|
|
|
|
_, err := c.fsTree.Delete(prm)
|
|
|
|
if err == nil {
|
2022-12-20 12:29:05 +00:00
|
|
|
storagelog.Write(c.log,
|
|
|
|
storagelog.AddressField(addr),
|
|
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
|
|
storagelog.OpField("fstree DELETE"),
|
|
|
|
)
|
2022-11-17 08:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-01 07:06:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
2022-07-07 12:03:45 +00:00
|
|
|
}
|
|
|
|
_, _ = c.fsTree.Iterate(prm)
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Info(logs.WritecacheFinishedUpdatingFSTreeFlushMarks)
|
2023-02-15 14:53:42 +00:00
|
|
|
}
|
2022-06-01 07:06:32 +00:00
|
|
|
|
2023-02-15 14:53:42 +00:00
|
|
|
func (c *cache) dbFlushMarkUpdate() {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Info(logs.WritecacheFillingFlushMarksForObjectsInDatabase)
|
2022-06-01 07:06:32 +00:00
|
|
|
|
|
|
|
var m []string
|
2022-11-17 08:59:27 +00:00
|
|
|
var indices []int
|
2022-06-01 07:06:32 +00:00
|
|
|
var lastKey []byte
|
|
|
|
var batchSize = flushBatchSize
|
|
|
|
for {
|
2023-02-15 14:53:42 +00:00
|
|
|
select {
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
|
|
|
case <-c.stopInitCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2022-06-01 07:06:32 +00:00
|
|
|
m = m[:0]
|
2023-02-14 06:23:24 +00:00
|
|
|
indices = indices[:0]
|
2022-06-01 07:06:32 +00:00
|
|
|
|
|
|
|
// We put objects in batches of fixed size to not interfere with main put cycle a lot.
|
|
|
|
_ = c.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
cs := b.Cursor()
|
|
|
|
for k, _ := cs.Seek(lastKey); k != nil && len(m) < batchSize; k, _ = cs.Next() {
|
|
|
|
m = append(m, string(k))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
var addr oid.Address
|
|
|
|
for i := range m {
|
|
|
|
if err := addr.DecodeString(m[i]); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-17 08:59:27 +00:00
|
|
|
flushed, needRemove := c.flushStatus(addr)
|
|
|
|
if flushed {
|
2022-06-01 07:06:32 +00:00
|
|
|
c.store.flushed.Add(addr.EncodeToString(), true)
|
2022-11-17 08:59:27 +00:00
|
|
|
if needRemove {
|
|
|
|
indices = append(indices, i)
|
|
|
|
}
|
2022-06-01 07:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2022-11-17 08:59:27 +00:00
|
|
|
|
|
|
|
err := c.db.Batch(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
for _, j := range indices {
|
|
|
|
if err := b.Delete([]byte(m[j])); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
for _, j := range indices {
|
2022-12-20 12:29:05 +00:00
|
|
|
storagelog.Write(c.log,
|
|
|
|
zap.String("address", m[j]),
|
|
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
|
|
storagelog.OpField("db DELETE"),
|
|
|
|
)
|
2022-11-17 08:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-01 07:06:32 +00:00
|
|
|
lastKey = append([]byte(m[len(m)-1]), 0)
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Info(logs.WritecacheFinishedUpdatingFlushMarks)
|
2022-06-01 07:06:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 08:59:27 +00:00
|
|
|
// flushStatus returns info about the object state in the main storage.
|
|
|
|
// First return value is true iff object exists.
|
|
|
|
// Second return value is true iff object can be safely removed.
|
|
|
|
func (c *cache) flushStatus(addr oid.Address) (bool, bool) {
|
|
|
|
var existsPrm meta.ExistsPrm
|
|
|
|
existsPrm.SetAddress(addr)
|
2022-06-01 07:06:32 +00:00
|
|
|
|
2022-11-17 08:59:27 +00:00
|
|
|
_, err := c.metabase.Exists(existsPrm)
|
2022-07-27 18:38:28 +00:00
|
|
|
if err != nil {
|
2022-11-17 08:59:27 +00:00
|
|
|
needRemove := errors.Is(err, meta.ErrObjectIsExpired) || errors.As(err, new(apistatus.ObjectAlreadyRemoved))
|
|
|
|
return needRemove, needRemove
|
2022-07-27 18:38:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 08:59:27 +00:00
|
|
|
var prm meta.StorageIDPrm
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
|
|
|
|
mRes, _ := c.metabase.StorageID(prm)
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := c.blobstor.Exists(context.TODO(), common.ExistsPrm{Address: addr, StorageID: mRes.StorageID()})
|
2022-11-17 08:59:27 +00:00
|
|
|
return err == nil && res.Exists, false
|
2022-06-01 07:06:32 +00:00
|
|
|
}
|