[#222] Add writeCache instance to shard

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
remotes/KirillovDenis/release/v0.21.1
Alex Vanin 2020-12-01 10:35:25 +03:00
parent 2eb83624cf
commit 1b76458684
2 changed files with 50 additions and 22 deletions

View File

@ -6,12 +6,15 @@ import (
// Open opens all Shard's components. // Open opens all Shard's components.
func (s *Shard) Open() error { func (s *Shard) Open() error {
for _, component := range []interface { components := []interface{ Open() error }{
Open() error s.blobStor, s.metaBase,
}{ }
s.blobStor,
s.metaBase, if s.hasWriteCache() {
} { components = append(components, s.writeCache)
}
for _, component := range components {
if err := component.Open(); err != nil { if err := component.Open(); err != nil {
return errors.Wrapf(err, "could not open %s", component) return errors.Wrapf(err, "could not open %s", component)
} }
@ -22,12 +25,15 @@ func (s *Shard) Open() error {
// Init initializes all Shard's components. // Init initializes all Shard's components.
func (s *Shard) Init() error { func (s *Shard) Init() error {
for _, component := range []interface { components := []interface{ Init() error }{
Init() error s.blobStor, s.metaBase,
}{ }
s.blobStor,
s.metaBase, if s.hasWriteCache() {
} { components = append(components, s.writeCache)
}
for _, component := range components {
if err := component.Init(); err != nil { if err := component.Init(); err != nil {
return errors.Wrapf(err, "could not initialize %s", component) return errors.Wrapf(err, "could not initialize %s", component)
} }
@ -38,12 +44,15 @@ func (s *Shard) Init() error {
// Close releases all Shard's components. // Close releases all Shard's components.
func (s *Shard) Close() error { func (s *Shard) Close() error {
for _, component := range []interface { components := []interface{ Close() error }{
Close() error s.blobStor, s.metaBase,
}{ }
s.blobStor,
s.metaBase, if s.hasWriteCache() {
} { components = append(components, s.writeCache)
}
for _, component := range components {
if err := component.Close(); err != nil { if err := component.Close(); err != nil {
return errors.Wrapf(err, "could not close %s", component) return errors.Wrapf(err, "could not close %s", component)
} }

View File

@ -13,6 +13,8 @@ type Shard struct {
mode *atomic.Uint32 mode *atomic.Uint32
writeCache *blobstor.BlobStor
blobStor *blobstor.BlobStor blobStor *blobstor.BlobStor
metaBase *meta.DB metaBase *meta.DB
@ -45,11 +47,23 @@ func New(opts ...Option) *Shard {
opts[i](c) opts[i](c)
} }
var writeCache *blobstor.BlobStor
if c.useWriteCache {
writeCache = blobstor.New(
blobstor.WithBlobovniczaShallowDepth(0),
blobstor.WithBlobovniczaShallowWidth(1),
blobstor.WithLogger(c.log),
// ? what about path
)
}
return &Shard{ return &Shard{
cfg: c, cfg: c,
mode: atomic.NewUint32(0), // TODO: init with particular mode mode: atomic.NewUint32(0), // TODO: init with particular mode
blobStor: blobstor.New(c.blobOpts...), blobStor: blobstor.New(c.blobOpts...),
metaBase: meta.New(c.metaOpts...), metaBase: meta.New(c.metaOpts...),
writeCache: writeCache,
} }
} }
@ -87,3 +101,8 @@ func WithWriteCache(use bool) Option {
c.useWriteCache = use c.useWriteCache = use
} }
} }
// hasWriteCache returns bool if write cache exists on shars.
func (s Shard) hasWriteCache() bool {
return s.cfg.useWriteCache
}