Skip to content

Commit

Permalink
use mutex array
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Aug 29, 2023
1 parent ff9e25a commit 36d0e3b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
12 changes: 5 additions & 7 deletions share/eds/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ type Store struct {
// lastGCResult is only stored on the store for testing purposes.
lastGCResult atomic.Pointer[dagstore.GCResult]

// lock is used to lock parallel operations
lock sync.Map
// stripedLocks is used to synchronize parallel operations
stripedLocks [256]sync.Mutex

metrics *metrics
}
Expand Down Expand Up @@ -201,11 +201,9 @@ func (s *Store) Put(ctx context.Context, root share.DataHash, square *rsmt2d.Ext
}

func (s *Store) put(ctx context.Context, root share.DataHash, square *rsmt2d.ExtendedDataSquare) (err error) {
m, _ := s.lock.LoadOrStore(root.String(), sync.Mutex{})
lock := m.(sync.Mutex)
lock.Lock()
defer lock.Unlock()
defer s.lock.Delete(root.String())
lk := &s.stripedLocks[root[len(root)-1]]
lk.Lock()
defer lk.Unlock()

// if root already exists, short-circuit
if has, _ := s.Has(ctx, root); has {
Expand Down
26 changes: 26 additions & 0 deletions share/eds/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package eds

import (
"context"
"github.com/filecoin-project/dagstore"
"os"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -171,6 +173,30 @@ func TestEDSStore(t *testing.T) {
assert.Contains(t, hashesOut, hash)
}
})

t.Run("Parallel put", func(t *testing.T) {
const amount = 20
eds, dah := randomEDS(t)

wg := sync.WaitGroup{}
for i := 1; i < amount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
err := edsStore.Put(ctx, dah.Hash(), eds)
if err != nil {
require.ErrorIs(t, err, dagstore.ErrShardExists)
}
}()
}
wg.Wait()

eds, err := edsStore.Get(ctx, dah.Hash())
require.NoError(t, err)
newDah, err := da.NewDataAvailabilityHeader(eds)
require.NoError(t, err)
require.Equal(t, dah.Hash(), newDah.Hash())
})
}

// TestEDSStore_GC verifies that unused transient shards are collected by the GC periodically.
Expand Down

0 comments on commit 36d0e3b

Please sign in to comment.