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

Improved staging shard performance #2034

Merged
merged 2 commits into from
May 4, 2022
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
23 changes: 10 additions & 13 deletions domain/consensus/model/staging_area.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import "github.com/pkg/errors"
import (
"github.com/pkg/errors"
)

// StagingShard is an interface that enables every store to have it's own Commit logic
// See StagingArea for more details
Expand All @@ -19,29 +21,27 @@ type StagingShardID uint64
// When the StagingArea is being Committed, it goes over all it's shards, and commits those one-by-one.
// Since Commit happens in a DatabaseTransaction, a StagingArea is atomic.
type StagingArea struct {
shards []StagingShard
shards map[StagingShardID]StagingShard
isCommitted bool
}

// NewStagingArea creates a new, empty staging area.
func NewStagingArea() *StagingArea {
return &StagingArea{
shards: []StagingShard{},
shards: make(map[StagingShardID]StagingShard),
isCommitted: false,
}
}

// GetOrCreateShard attempts to retrieve a shard with the given name.
// If it does not exist - a new shard is created using `createFunc`.
func (sa *StagingArea) GetOrCreateShard(shardID StagingShardID, createFunc func() StagingShard) StagingShard {
for uint64(len(sa.shards)) <= uint64(shardID) {
sa.shards = append(sa.shards, nil)
shard, ok := sa.shards[shardID]
if !ok {
shard = createFunc()
sa.shards[shardID] = shard
}
if sa.shards[shardID] == nil {
sa.shards[shardID] = createFunc()
}

return sa.shards[shardID]
return shard
}

// Commit goes over all the Shards in the StagingArea and commits them, inside the provided database transaction.
Expand All @@ -52,9 +52,6 @@ func (sa *StagingArea) Commit(dbTx DBTransaction) error {
}

for _, shard := range sa.shards {
if shard == nil { // since sa.shards is an array and not a map, some shard slots might be empty.
continue
}
err := shard.Commit(dbTx)
if err != nil {
return err
Expand Down