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

share: handle rows concurrently #241

Merged
merged 8 commits into from
Jan 6, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG-PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Month, DD, YYYY
- [feat(node): extract overrides from Config into Settings #292](https://github.com/celestiaorg/celestia-node/pull/292) [@Wondertan](https://github.com/Wondertan)

- [docker] Created `docker/` dir with `Dockerfile` and `entrypoint.sh` script.
- [chore(share): handle rows concurrently in GetSharesByNamespace #241](https://github.com/celestiaorg/celestia-node/pull/241) [@vgonkivs](https://github.com/vgonkivs)

### BUG FIXES

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ test:
@go test -v ./...
.PHONY: test

## benchmark: Running all benchmarks
benchmark:
@echo "--> Running benchmarks"
@go test -run="none" -bench=. -benchtime=100x -benchmem ./...
.PHONY: benchmark

PB_PKGS=$(shell find . -name 'pb' -type d)
PB_CORE=$(shell go list -f {{.Dir}} -m github.com/tendermint/tendermint)
PB_GOGO=$(shell go list -f {{.Dir}} -m github.com/gogo/protobuf)
Expand Down
34 changes: 27 additions & 7 deletions service/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,37 @@ func (s *service) GetSharesByNamespace(ctx context.Context, root *Root, nID name
return nil, ipld.ErrNotFoundInRange
}

namespacedShares := make([]Share, 0)
type res struct {
nodes []format.Node
err error
}
resultCh := make(chan *res)

ctx, cancel := context.WithCancel(ctx)
defer cancel()

Wondertan marked this conversation as resolved.
Show resolved Hide resolved
for _, rootCID := range rowRootCIDs {
nodes, err := ipld.GetLeavesByNamespace(ctx, s.dag, rootCID, nID)
if err != nil {
return nil, err
}
go func(rootCID cid.Cid) {
nodes, err := ipld.GetLeavesByNamespace(ctx, s.dag, rootCID, nID)
resultCh <- &res{nodes: nodes, err: err}
}(rootCID)
}

for _, node := range nodes {
namespacedShares = append(namespacedShares, node.RawData()[1:])
namespacedShares := make([]Share, 0)
for i := 0; i < len(rowRootCIDs); i++ {
select {
case result := <-resultCh:
renaynay marked this conversation as resolved.
Show resolved Hide resolved
if result.err != nil {
return nil, result.err
}
for _, node := range result.nodes {
namespacedShares = append(namespacedShares, node.RawData()[1:])
}
case <-ctx.Done():
return nil, ctx.Err()
}
}

return namespacedShares, nil
}

Expand Down
37 changes: 36 additions & 1 deletion service/share/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,42 @@ func TestService_GetSharesByNamespace(t *testing.T) {
shares, err := serv.GetSharesByNamespace(context.Background(), root, randNID)
require.NoError(t, err)
assert.Len(t, shares, tt.expectedShareCount)
assert.Equal(t, randNID, []byte(shares[0].NamespaceID()))
for _, value := range shares {
assert.Equal(t, randNID, []byte(value.NamespaceID()))
}
})
}
}

func TestService_GetSharesByNamespaceNotFoundInRange(t *testing.T) {
serv, root := RandServiceWithSquare(t, 1)
root.RowsRoots = nil

shares, err := serv.GetSharesByNamespace(context.Background(), root, []byte(""))
assert.Len(t, shares, 0)
require.Error(t, err, "namespaceID not found in range")
}

func BenchmarkService_GetSharesByNamespace(b *testing.B) {
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
var tests = []struct {
amountShares int
}{
{amountShares: 4},
{amountShares: 16},
{amountShares: 128},
}

for _, tt := range tests {
b.Run(strconv.Itoa(tt.amountShares), func(b *testing.B) {
t := &testing.T{}
serv, root := RandServiceWithSquare(t, tt.amountShares)
randNID := root.RowsRoots[(len(root.RowsRoots)-1)/2][:8]
root.RowsRoots[(len(root.RowsRoots) / 2)] = root.RowsRoots[(len(root.RowsRoots)-1)/2]
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := serv.GetSharesByNamespace(context.Background(), root, randNID)
require.NoError(t, err)
}
})
}
}