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

feat(admin): speed up fetching cluster metadata #480

Merged
merged 1 commit into from
Jul 28, 2023
Merged
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
28 changes: 24 additions & 4 deletions internal/admin/mrmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/pkg/errors"
"golang.org/x/sync/singleflight"

"github.com/kakao/varlog/pkg/mrc"
"github.com/kakao/varlog/pkg/mrc/mrconnector"
Expand Down Expand Up @@ -88,6 +89,7 @@ type mrManager struct {
mu sync.RWMutex
connector mrconnector.Connector

sfg singleflight.Group
dirty bool
updated time.Time
meta *varlogpb.MetadataDescriptor
Expand Down Expand Up @@ -401,19 +403,37 @@ func (mrm *mrManager) RemovePeer(ctx context.Context, nodeID types.NodeID) error
}

func (mrm *mrManager) ClusterMetadata(ctx context.Context) (*varlogpb.MetadataDescriptor, error) {
mrm.mu.Lock()
defer mrm.mu.Unlock()
// fail-fast
if err := ctx.Err(); err != nil {
return nil, ctx.Err()
}

// fast path
mrm.mu.RLock()
if !mrm.dirty && time.Since(mrm.updated) <= ReloadInterval {
meta := mrm.meta
mrm.mu.RUnlock()
return meta, nil
}
mrm.mu.RUnlock()

if mrm.dirty || time.Since(mrm.updated) > ReloadInterval {
// slow path
md, err, _ := mrm.sfg.Do("cluster_metadata", func() (interface{}, error) {
meta, err := mrm.clusterMetadata(ctx)
if err != nil {
return nil, fmt.Errorf("cluster metadata: %w", err)
}
mrm.mu.Lock()
mrm.meta = meta
mrm.dirty = false
mrm.updated = time.Now()
mrm.mu.Unlock()
return meta, nil
})
if err != nil {
return nil, err
}
return mrm.meta, nil
return md.(*varlogpb.MetadataDescriptor), nil
}

func (mrm *mrManager) StorageNode(ctx context.Context, storageNodeID types.StorageNodeID) (*varlogpb.StorageNodeDescriptor, error) {
Expand Down