2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2021-05-11 05:11:29 -04:00
|
|
|
package dsync
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
id = "1234-5678"
|
|
|
|
source = "main.go"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testSimpleWriteLock(t *testing.T, duration time.Duration) (locked bool) {
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm1 := NewDRWMutex(ds, "simplelock")
|
2021-03-03 21:36:43 -05:00
|
|
|
ctx1, cancel1 := context.WithCancel(context.Background())
|
2022-10-10 18:14:32 -04:00
|
|
|
if !drwm1.GetRLock(ctx1, cancel1, id, source, Options{Timeout: time.Second}) {
|
2019-11-13 15:17:45 -05:00
|
|
|
panic("Failed to acquire read lock")
|
|
|
|
}
|
|
|
|
// fmt.Println("1st read lock acquired, waiting...")
|
|
|
|
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm2 := NewDRWMutex(ds, "simplelock")
|
2021-03-03 21:36:43 -05:00
|
|
|
ctx2, cancel2 := context.WithCancel(context.Background())
|
2022-10-10 18:14:32 -04:00
|
|
|
if !drwm2.GetRLock(ctx2, cancel2, id, source, Options{Timeout: time.Second}) {
|
2019-11-13 15:17:45 -05:00
|
|
|
panic("Failed to acquire read lock")
|
|
|
|
}
|
|
|
|
// fmt.Println("2nd read lock acquired, waiting...")
|
|
|
|
|
|
|
|
go func() {
|
2022-03-01 14:14:28 -05:00
|
|
|
time.Sleep(2 * testDrwMutexAcquireTimeout)
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm1.RUnlock()
|
2019-11-13 15:17:45 -05:00
|
|
|
// fmt.Println("1st read lock released, waiting...")
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2022-03-01 14:14:28 -05:00
|
|
|
time.Sleep(3 * testDrwMutexAcquireTimeout)
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm2.RUnlock()
|
2019-11-13 15:17:45 -05:00
|
|
|
// fmt.Println("2nd read lock released, waiting...")
|
|
|
|
}()
|
|
|
|
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm3 := NewDRWMutex(ds, "simplelock")
|
2019-11-13 15:17:45 -05:00
|
|
|
// fmt.Println("Trying to acquire write lock, waiting...")
|
2021-03-03 21:36:43 -05:00
|
|
|
ctx3, cancel3 := context.WithCancel(context.Background())
|
2022-10-10 18:14:32 -04:00
|
|
|
locked = drwm3.GetLock(ctx3, cancel3, id, source, Options{Timeout: duration})
|
2019-11-13 15:17:45 -05:00
|
|
|
if locked {
|
|
|
|
// fmt.Println("Write lock acquired, waiting...")
|
2022-03-01 14:14:28 -05:00
|
|
|
time.Sleep(testDrwMutexAcquireTimeout)
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm3.Unlock()
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
// fmt.Println("Write lock failed due to timeout")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleWriteLockAcquired(t *testing.T) {
|
2022-03-01 14:14:28 -05:00
|
|
|
locked := testSimpleWriteLock(t, 10*testDrwMutexAcquireTimeout)
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
expected := true
|
|
|
|
if locked != expected {
|
|
|
|
t.Errorf("TestSimpleWriteLockAcquired(): \nexpected %#v\ngot %#v", expected, locked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleWriteLockTimedOut(t *testing.T) {
|
2022-03-01 14:14:28 -05:00
|
|
|
locked := testSimpleWriteLock(t, testDrwMutexAcquireTimeout)
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
expected := false
|
|
|
|
if locked != expected {
|
|
|
|
t.Errorf("TestSimpleWriteLockTimedOut(): \nexpected %#v\ngot %#v", expected, locked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDualWriteLock(t *testing.T, duration time.Duration) (locked bool) {
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm1 := NewDRWMutex(ds, "duallock")
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
// fmt.Println("Getting initial write lock")
|
2021-03-03 21:36:43 -05:00
|
|
|
ctx1, cancel1 := context.WithCancel(context.Background())
|
2022-10-10 18:14:32 -04:00
|
|
|
if !drwm1.GetLock(ctx1, cancel1, id, source, Options{Timeout: time.Second}) {
|
2019-11-13 15:17:45 -05:00
|
|
|
panic("Failed to acquire initial write lock")
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2022-03-01 14:14:28 -05:00
|
|
|
time.Sleep(3 * testDrwMutexAcquireTimeout)
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm1.Unlock()
|
2019-11-13 15:17:45 -05:00
|
|
|
// fmt.Println("Initial write lock released, waiting...")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// fmt.Println("Trying to acquire 2nd write lock, waiting...")
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm2 := NewDRWMutex(ds, "duallock")
|
2021-03-03 21:36:43 -05:00
|
|
|
ctx2, cancel2 := context.WithCancel(context.Background())
|
2022-10-10 18:14:32 -04:00
|
|
|
locked = drwm2.GetLock(ctx2, cancel2, id, source, Options{Timeout: duration})
|
2019-11-13 15:17:45 -05:00
|
|
|
if locked {
|
|
|
|
// fmt.Println("2nd write lock acquired, waiting...")
|
2022-03-01 14:14:28 -05:00
|
|
|
time.Sleep(testDrwMutexAcquireTimeout)
|
2019-11-13 15:17:45 -05:00
|
|
|
|
2022-10-10 18:14:32 -04:00
|
|
|
drwm2.Unlock()
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
// fmt.Println("2nd write lock failed due to timeout")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDualWriteLockAcquired(t *testing.T) {
|
2022-03-01 14:14:28 -05:00
|
|
|
locked := testDualWriteLock(t, 10*testDrwMutexAcquireTimeout)
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
expected := true
|
|
|
|
if locked != expected {
|
|
|
|
t.Errorf("TestDualWriteLockAcquired(): \nexpected %#v\ngot %#v", expected, locked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDualWriteLockTimedOut(t *testing.T) {
|
2022-03-01 14:14:28 -05:00
|
|
|
locked := testDualWriteLock(t, testDrwMutexAcquireTimeout)
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
expected := false
|
|
|
|
if locked != expected {
|
|
|
|
t.Errorf("TestDualWriteLockTimedOut(): \nexpected %#v\ngot %#v", expected, locked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test cases below are copied 1 to 1 from sync/rwmutex_test.go (adapted to use DRWMutex)
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
2020-06-14 10:43:10 -04:00
|
|
|
func parallelReader(ctx context.Context, m *DRWMutex, clocked, cunlock, cdone chan bool) {
|
2021-03-03 21:36:43 -05:00
|
|
|
if m.GetRLock(ctx, nil, id, source, Options{Timeout: time.Second}) {
|
2019-11-13 15:17:45 -05:00
|
|
|
clocked <- true
|
|
|
|
<-cunlock
|
|
|
|
m.RUnlock()
|
|
|
|
cdone <- true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func doTestParallelReaders(numReaders, gomaxprocs int) {
|
|
|
|
runtime.GOMAXPROCS(gomaxprocs)
|
2020-06-14 10:43:10 -04:00
|
|
|
m := NewDRWMutex(ds, "test-parallel")
|
2019-11-13 15:17:45 -05:00
|
|
|
|
|
|
|
clocked := make(chan bool)
|
|
|
|
cunlock := make(chan bool)
|
|
|
|
cdone := make(chan bool)
|
|
|
|
for i := 0; i < numReaders; i++ {
|
2020-06-14 10:43:10 -04:00
|
|
|
go parallelReader(context.Background(), m, clocked, cunlock, cdone)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
// Wait for all parallel RLock()s to succeed.
|
|
|
|
for i := 0; i < numReaders; i++ {
|
|
|
|
<-clocked
|
|
|
|
}
|
|
|
|
for i := 0; i < numReaders; i++ {
|
|
|
|
cunlock <- true
|
|
|
|
}
|
|
|
|
// Wait for the goroutines to finish.
|
|
|
|
for i := 0; i < numReaders; i++ {
|
|
|
|
<-cdone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestParallelReaders(t *testing.T) {
|
|
|
|
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1))
|
|
|
|
doTestParallelReaders(1, 4)
|
|
|
|
doTestParallelReaders(3, 4)
|
|
|
|
doTestParallelReaders(4, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
2022-10-10 18:14:32 -04:00
|
|
|
func reader(resource string, numIterations int, activity *int32, cdone chan bool) {
|
|
|
|
rwm := NewDRWMutex(ds, resource)
|
2019-11-13 15:17:45 -05:00
|
|
|
for i := 0; i < numIterations; i++ {
|
2021-03-03 21:36:43 -05:00
|
|
|
if rwm.GetRLock(context.Background(), nil, id, source, Options{Timeout: time.Second}) {
|
2019-11-13 15:17:45 -05:00
|
|
|
n := atomic.AddInt32(activity, 1)
|
|
|
|
if n < 1 || n >= 10000 {
|
|
|
|
panic(fmt.Sprintf("wlock(%d)\n", n))
|
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
}
|
|
|
|
atomic.AddInt32(activity, -1)
|
|
|
|
rwm.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cdone <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
2022-10-10 18:14:32 -04:00
|
|
|
func writer(resource string, numIterations int, activity *int32, cdone chan bool) {
|
|
|
|
rwm := NewDRWMutex(ds, resource)
|
2019-11-13 15:17:45 -05:00
|
|
|
for i := 0; i < numIterations; i++ {
|
2021-03-03 21:36:43 -05:00
|
|
|
if rwm.GetLock(context.Background(), nil, id, source, Options{Timeout: time.Second}) {
|
2019-11-13 15:17:45 -05:00
|
|
|
n := atomic.AddInt32(activity, 10000)
|
|
|
|
if n != 10000 {
|
|
|
|
panic(fmt.Sprintf("wlock(%d)\n", n))
|
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
}
|
|
|
|
atomic.AddInt32(activity, -10000)
|
|
|
|
rwm.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cdone <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
2022-03-01 14:14:28 -05:00
|
|
|
func hammerRWMutex(t *testing.T, gomaxprocs, numReaders, numIterations int) {
|
|
|
|
t.Run(fmt.Sprintf("%d-%d-%d", gomaxprocs, numReaders, numIterations), func(t *testing.T) {
|
2022-10-10 18:14:32 -04:00
|
|
|
resource := "test"
|
2022-03-01 14:14:28 -05:00
|
|
|
runtime.GOMAXPROCS(gomaxprocs)
|
|
|
|
// Number of active readers + 10000 * number of active writers.
|
|
|
|
var activity int32
|
|
|
|
cdone := make(chan bool)
|
2022-10-10 18:14:32 -04:00
|
|
|
go writer(resource, numIterations, &activity, cdone)
|
2022-03-01 14:14:28 -05:00
|
|
|
var i int
|
|
|
|
for i = 0; i < numReaders/2; i++ {
|
2022-10-10 18:14:32 -04:00
|
|
|
go reader(resource, numIterations, &activity, cdone)
|
2022-03-01 14:14:28 -05:00
|
|
|
}
|
2022-10-10 18:14:32 -04:00
|
|
|
go writer(resource, numIterations, &activity, cdone)
|
2022-03-01 14:14:28 -05:00
|
|
|
for ; i < numReaders; i++ {
|
2022-10-10 18:14:32 -04:00
|
|
|
go reader(resource, numIterations, &activity, cdone)
|
2022-03-01 14:14:28 -05:00
|
|
|
}
|
|
|
|
// Wait for the 2 writers and all readers to finish.
|
|
|
|
for i := 0; i < 2+numReaders; i++ {
|
|
|
|
<-cdone
|
|
|
|
}
|
|
|
|
})
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestRWMutex(t *testing.T) {
|
|
|
|
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1))
|
2020-04-14 21:39:03 -04:00
|
|
|
n := 100
|
2019-11-13 15:17:45 -05:00
|
|
|
if testing.Short() {
|
|
|
|
n = 5
|
|
|
|
}
|
2022-03-01 14:14:28 -05:00
|
|
|
hammerRWMutex(t, 1, 1, n)
|
|
|
|
hammerRWMutex(t, 1, 3, n)
|
|
|
|
hammerRWMutex(t, 1, 10, n)
|
|
|
|
hammerRWMutex(t, 4, 1, n)
|
|
|
|
hammerRWMutex(t, 4, 3, n)
|
|
|
|
hammerRWMutex(t, 4, 10, n)
|
|
|
|
hammerRWMutex(t, 10, 1, n)
|
|
|
|
hammerRWMutex(t, 10, 3, n)
|
|
|
|
hammerRWMutex(t, 10, 10, n)
|
|
|
|
hammerRWMutex(t, 10, 5, n)
|
2019-11-13 15:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestUnlockPanic(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if recover() == nil {
|
|
|
|
t.Fatalf("unlock of unlocked RWMutex did not panic")
|
|
|
|
}
|
|
|
|
}()
|
2020-06-14 10:43:10 -04:00
|
|
|
mu := NewDRWMutex(ds, "test")
|
2019-11-13 15:17:45 -05:00
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestUnlockPanic2(t *testing.T) {
|
2022-03-01 14:14:28 -05:00
|
|
|
mu := NewDRWMutex(ds, "test-unlock-panic-2")
|
2019-11-13 15:17:45 -05:00
|
|
|
defer func() {
|
|
|
|
if recover() == nil {
|
|
|
|
t.Fatalf("unlock of unlocked RWMutex did not panic")
|
|
|
|
}
|
2022-03-01 14:14:28 -05:00
|
|
|
mu.RUnlock() // Unlock, so -test.count > 1 works
|
2019-11-13 15:17:45 -05:00
|
|
|
}()
|
|
|
|
mu.RLock(id, source)
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestRUnlockPanic(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if recover() == nil {
|
|
|
|
t.Fatalf("read unlock of unlocked RWMutex did not panic")
|
|
|
|
}
|
|
|
|
}()
|
2020-06-14 10:43:10 -04:00
|
|
|
mu := NewDRWMutex(ds, "test")
|
2019-11-13 15:17:45 -05:00
|
|
|
mu.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestRUnlockPanic2(t *testing.T) {
|
2022-03-01 14:14:28 -05:00
|
|
|
mu := NewDRWMutex(ds, "test-runlock-panic-2")
|
2019-11-13 15:17:45 -05:00
|
|
|
defer func() {
|
|
|
|
if recover() == nil {
|
|
|
|
t.Fatalf("read unlock of unlocked RWMutex did not panic")
|
|
|
|
}
|
2022-03-01 14:14:28 -05:00
|
|
|
mu.Unlock() // Unlock, so -test.count > 1 works
|
2019-11-13 15:17:45 -05:00
|
|
|
}()
|
|
|
|
mu.Lock(id, source)
|
|
|
|
mu.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func benchmarkRWMutex(b *testing.B, localWork, writeRatio int) {
|
2021-10-18 11:39:59 -04:00
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
2019-11-13 15:17:45 -05:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
foo := 0
|
|
|
|
for pb.Next() {
|
2022-10-10 18:14:32 -04:00
|
|
|
rwm := NewDRWMutex(ds, "test")
|
2019-11-13 15:17:45 -05:00
|
|
|
foo++
|
|
|
|
if foo%writeRatio == 0 {
|
|
|
|
rwm.Lock(id, source)
|
|
|
|
rwm.Unlock()
|
|
|
|
} else {
|
|
|
|
rwm.RLock(id, source)
|
2020-05-18 12:59:45 -04:00
|
|
|
for i := 0; i != localWork; i++ {
|
2019-11-13 15:17:45 -05:00
|
|
|
foo *= 2
|
|
|
|
foo /= 2
|
|
|
|
}
|
|
|
|
rwm.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ = foo
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func BenchmarkRWMutexWrite100(b *testing.B) {
|
|
|
|
benchmarkRWMutex(b, 0, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func BenchmarkRWMutexWrite10(b *testing.B) {
|
|
|
|
benchmarkRWMutex(b, 0, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func BenchmarkRWMutexWorkWrite100(b *testing.B) {
|
|
|
|
benchmarkRWMutex(b, 100, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func BenchmarkRWMutexWorkWrite10(b *testing.B) {
|
|
|
|
benchmarkRWMutex(b, 100, 10)
|
|
|
|
}
|