Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy initialize Public Share Manager #4490

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/unreleased/lazy-initialize-publicsharemanager.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Lazy initialize public share manager

Unlike the share manager the public share manager was initializing its data structure on startup. This can lead to failed ocis
starts (in single binary case) or to restarting `sharing` pods when running in containerized environment.

https://github.com/cs3org/reva/pull/4490
53 changes: 41 additions & 12 deletions pkg/publicshare/manager/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ func NewFile(c map[string]interface{}) (publicshare.Manager, error) {
}

p := file.New(conf.File)
if err := p.Init(context.Background()); err != nil {
return nil, err
}

return New(conf.GatewayAddr, conf.SharePasswordHashCost, conf.JanitorRunInterval, conf.EnableExpiredSharesCleanup, p)
}

Expand All @@ -89,10 +85,6 @@ func NewMemory(c map[string]interface{}) (publicshare.Manager, error) {
conf.init()
p := memory.New()

if err := p.Init(context.Background()); err != nil {
return nil, err
}

return New(conf.GatewayAddr, conf.SharePasswordHashCost, conf.JanitorRunInterval, conf.EnableExpiredSharesCleanup, p)
}

Expand All @@ -111,10 +103,6 @@ func NewCS3(c map[string]interface{}) (publicshare.Manager, error) {
}
p := cs3.New(s)

if err := p.Init(context.Background()); err != nil {
return nil, err
}

return New(conf.GatewayAddr, conf.SharePasswordHashCost, conf.JanitorRunInterval, conf.EnableExpiredSharesCleanup, p)
}

Expand Down Expand Up @@ -174,6 +162,10 @@ type manager struct {
enableExpiredSharesCleanup bool
}

func (m *manager) init() error {
return m.persistence.Init(context.Background())
}

func (m *manager) startJanitorRun() {
if !m.enableExpiredSharesCleanup {
return
Expand All @@ -200,6 +192,10 @@ func (m *manager) Dump(ctx context.Context, shareChan chan<- *publicshare.WithPa
m.mutex.Lock()
defer m.mutex.Unlock()

if err := m.init(); err != nil {
return err
}

db, err := m.persistence.Read(ctx)
if err != nil {
return err
Expand All @@ -222,6 +218,10 @@ func (m *manager) Load(ctx context.Context, shareChan <-chan *publicshare.WithPa
m.mutex.Lock()
defer m.mutex.Unlock()

if err := m.init(); err != nil {
return err
}

db, err := m.persistence.Read(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -296,6 +296,10 @@ func (m *manager) CreatePublicShare(ctx context.Context, u *user.User, rInfo *pr
m.mutex.Lock()
defer m.mutex.Unlock()

if err := m.init(); err != nil {
return nil, err
}

encShare, err := utils.MarshalProtoV1ToJSON(&ps.PublicShare)
if err != nil {
return nil, err
Expand Down Expand Up @@ -385,6 +389,10 @@ func (m *manager) UpdatePublicShare(ctx context.Context, u *user.User, req *link
m.mutex.Lock()
defer m.mutex.Unlock()

if err := m.init(); err != nil {
return nil, err
}

db, err := m.persistence.Read(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -420,6 +428,10 @@ func (m *manager) GetPublicShare(ctx context.Context, u *user.User, ref *link.Pu
m.mutex.Lock()
defer m.mutex.Unlock()

if err := m.init(); err != nil {
return nil, err
}

if ref.GetToken() != "" {
ps, pw, err := m.getByToken(ctx, ref.GetToken())
if err != nil {
Expand Down Expand Up @@ -473,6 +485,10 @@ func (m *manager) ListPublicShares(ctx context.Context, u *user.User, filters []
m.mutex.Lock()
defer m.mutex.Unlock()

if err := m.init(); err != nil {
return nil, err
}

log := appctx.GetLogger(ctx)

db, err := m.persistence.Read(ctx)
Expand Down Expand Up @@ -555,6 +571,10 @@ func (m *manager) cleanupExpiredShares() {
m.mutex.Lock()
defer m.mutex.Unlock()

if err := m.init(); err != nil {
return
}

db, _ := m.persistence.Read(context.Background())

for _, v := range db {
Expand Down Expand Up @@ -594,6 +614,11 @@ func (m *manager) revokeExpiredPublicShare(ctx context.Context, s *link.PublicSh
func (m *manager) RevokePublicShare(ctx context.Context, _ *user.User, ref *link.PublicShareReference) error {
m.mutex.Lock()
defer m.mutex.Unlock()

if err := m.init(); err != nil {
return err
}

return m.revokePublicShare(ctx, ref)
}

Expand Down Expand Up @@ -651,6 +676,10 @@ func (m *manager) GetPublicShareByToken(ctx context.Context, token string, auth
m.mutex.Lock()
defer m.mutex.Unlock()

if err := m.init(); err != nil {
return nil, err
}

db, err := m.persistence.Read(ctx)
if err != nil {
return nil, err
Expand Down
23 changes: 21 additions & 2 deletions pkg/publicshare/manager/json/persistence/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,36 @@ import (
"fmt"
"os"
"path/filepath"
"sync"

"github.com/cs3org/reva/v2/pkg/publicshare/manager/json/persistence"
)

type file struct {
path string
initialized bool
lock *sync.RWMutex
}

// New returns a new Cache instance
func New(path string) persistence.Persistence {
return &file{
path: path,
lock: &sync.RWMutex{},
}
}

func (p *file) Init(_ context.Context) error {
if p.isInitialized() {
return nil
}

p.lock.Lock()
defer p.lock.Unlock()
if p.initialized {
return nil
}

// attempt to create the db file
var fi os.FileInfo
var err error
Expand All @@ -65,7 +78,7 @@ func (p *file) Init(_ context.Context) error {
}

func (p *file) Read(_ context.Context) (persistence.PublicShares, error) {
if !p.initialized {
if !p.isInitialized() {
return nil, fmt.Errorf("not initialized")
}
db := map[string]interface{}{}
Expand All @@ -80,7 +93,7 @@ func (p *file) Read(_ context.Context) (persistence.PublicShares, error) {
}

func (p *file) Write(_ context.Context, db persistence.PublicShares) error {
if !p.initialized {
if !p.isInitialized() {
return fmt.Errorf("not initialized")
}
dbAsJSON, err := json.Marshal(db)
Expand All @@ -90,3 +103,9 @@ func (p *file) Write(_ context.Context, db persistence.PublicShares) error {

return os.WriteFile(p.path, dbAsJSON, 0644)
}

func (p *file) isInitialized() bool {
p.lock.RLock()
defer p.lock.RUnlock()
return p.initialized
}
Loading