-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10896 from filecoin-project/feat/chainindex-shard…
…ed-mutex feat: chainstore: sharded mutex for filling chain height index
- Loading branch information
Showing
5 changed files
with
266 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package shardedmutex | ||
|
||
import ( | ||
"hash/maphash" | ||
"sync" | ||
) | ||
|
||
const cacheline = 64 | ||
|
||
// padding a mutex to a cacheline improves performance as the cachelines are not contested | ||
// name old time/op new time/op delta | ||
// Locks-8 74.6ns ± 7% 12.3ns ± 2% -83.54% (p=0.000 n=20+18) | ||
type paddedMutex struct { | ||
mt sync.Mutex | ||
pad [cacheline - 8]uint8 | ||
} | ||
|
||
type ShardedMutex struct { | ||
shards []paddedMutex | ||
} | ||
|
||
// New creates a new ShardedMutex with N shards | ||
func New(nShards int) ShardedMutex { | ||
if nShards < 1 { | ||
panic("n_shards cannot be less than 1") | ||
} | ||
return ShardedMutex{ | ||
shards: make([]paddedMutex, nShards), | ||
} | ||
} | ||
|
||
func (sm ShardedMutex) Shards() int { | ||
return len(sm.shards) | ||
} | ||
|
||
func (sm ShardedMutex) Lock(shard int) { | ||
sm.shards[shard].mt.Lock() | ||
} | ||
|
||
func (sm ShardedMutex) Unlock(shard int) { | ||
sm.shards[shard].mt.Unlock() | ||
} | ||
|
||
func (sm ShardedMutex) GetLock(shard int) sync.Locker { | ||
return &sm.shards[shard].mt | ||
} | ||
|
||
type ShardedMutexFor[K any] struct { | ||
inner ShardedMutex | ||
|
||
hasher func(maphash.Seed, K) uint64 | ||
seed maphash.Seed | ||
} | ||
|
||
func NewFor[K any](hasher func(maphash.Seed, K) uint64, nShards int) ShardedMutexFor[K] { | ||
return ShardedMutexFor[K]{ | ||
inner: New(nShards), | ||
hasher: hasher, | ||
seed: maphash.MakeSeed(), | ||
} | ||
} | ||
|
||
func (sm ShardedMutexFor[K]) shardFor(key K) int { | ||
return int(sm.hasher(sm.seed, key) % uint64(len(sm.inner.shards))) | ||
} | ||
|
||
func (sm ShardedMutexFor[K]) Lock(key K) { | ||
sm.inner.Lock(sm.shardFor(key)) | ||
} | ||
func (sm ShardedMutexFor[K]) Unlock(key K) { | ||
sm.inner.Unlock(sm.shardFor(key)) | ||
} | ||
func (sm ShardedMutexFor[K]) GetLock(key K) sync.Locker { | ||
return sm.inner.GetLock(sm.shardFor(key)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package shardedmutex | ||
|
||
import ( | ||
"fmt" | ||
"hash/maphash" | ||
"runtime" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestLockingDifferentShardsDoesNotBlock(t *testing.T) { | ||
shards := 16 | ||
sm := New(shards) | ||
done := make(chan struct{}) | ||
go func() { | ||
select { | ||
case <-done: | ||
return | ||
case <-time.After(5 * time.Second): | ||
panic("test locked up") | ||
} | ||
}() | ||
for i := 0; i < shards; i++ { | ||
sm.Lock(i) | ||
} | ||
|
||
close(done) | ||
} | ||
func TestLockingSameShardsBlocks(t *testing.T) { | ||
shards := 16 | ||
sm := New(shards) | ||
wg := sync.WaitGroup{} | ||
wg.Add(shards) | ||
ch := make(chan int, shards) | ||
|
||
for i := 0; i < shards; i++ { | ||
go func(i int) { | ||
if i != 15 { | ||
sm.Lock(i) | ||
} | ||
wg.Done() | ||
wg.Wait() | ||
sm.Lock((15 + i) % shards) | ||
ch <- i | ||
sm.Unlock(i) | ||
}(i) | ||
} | ||
|
||
wg.Wait() | ||
for i := 0; i < 2*shards; i++ { | ||
runtime.Gosched() | ||
} | ||
for i := 0; i < shards; i++ { | ||
if a := <-ch; a != i { | ||
t.Errorf("got %d instead of %d", a, i) | ||
} | ||
} | ||
} | ||
|
||
func TestShardedByString(t *testing.T) { | ||
shards := 16 | ||
sm := NewFor(maphash.String, shards) | ||
|
||
wg1 := sync.WaitGroup{} | ||
wg1.Add(shards * 20) | ||
wg2 := sync.WaitGroup{} | ||
wg2.Add(shards * 20) | ||
|
||
active := atomic.Int32{} | ||
max := atomic.Int32{} | ||
|
||
for i := 0; i < shards*20; i++ { | ||
go func(i int) { | ||
wg1.Done() | ||
wg1.Wait() | ||
sm.Lock(fmt.Sprintf("goroutine %d", i)) | ||
activeNew := active.Add(1) | ||
for { | ||
curMax := max.Load() | ||
if curMax >= activeNew { | ||
break | ||
} | ||
if max.CompareAndSwap(curMax, activeNew) { | ||
break | ||
} | ||
} | ||
for j := 0; j < 100; j++ { | ||
runtime.Gosched() | ||
} | ||
active.Add(-1) | ||
sm.Unlock(fmt.Sprintf("goroutine %d", i)) | ||
wg2.Done() | ||
}(i) | ||
} | ||
|
||
wg2.Wait() | ||
|
||
if max.Load() != 16 { | ||
t.Fatal("max load not achieved", max.Load()) | ||
} | ||
|
||
} | ||
|
||
func BenchmarkShardedMutex(b *testing.B) { | ||
shards := 16 | ||
sm := New(shards) | ||
|
||
done := atomic.Int32{} | ||
go func() { | ||
for { | ||
sm.Lock(0) | ||
sm.Unlock(0) | ||
if done.Load() != 0 { | ||
return | ||
} | ||
} | ||
}() | ||
for i := 0; i < 100; i++ { | ||
runtime.Gosched() | ||
} | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
sm.Lock(1) | ||
sm.Unlock(1) | ||
} | ||
done.Add(1) | ||
} | ||
|
||
func BenchmarkShardedMutexOf(b *testing.B) { | ||
shards := 16 | ||
sm := NewFor(maphash.String, shards) | ||
|
||
str1 := "string1" | ||
str2 := "string2" | ||
|
||
done := atomic.Int32{} | ||
go func() { | ||
for { | ||
sm.Lock(str1) | ||
sm.Unlock(str1) | ||
if done.Load() != 0 { | ||
return | ||
} | ||
} | ||
}() | ||
for i := 0; i < 100; i++ { | ||
runtime.Gosched() | ||
} | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
sm.Lock(str2) | ||
sm.Unlock(str2) | ||
} | ||
done.Add(1) | ||
} |