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/>.
|
2020-06-08 14:28:40 -04:00
|
|
|
|
|
|
|
package lsync_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-01-02 12:15:06 -05:00
|
|
|
"runtime"
|
2020-06-08 14:28:40 -04:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
. "github.com/minio/minio/internal/lsync"
|
2020-06-08 14:28:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testSimpleWriteLock(t *testing.T, duration time.Duration) (locked bool) {
|
2020-06-14 10:43:10 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
lrwm := NewLRWMutex()
|
2020-06-08 14:28:40 -04:00
|
|
|
|
2020-06-14 10:43:10 -04:00
|
|
|
if !lrwm.GetRLock(ctx, "", "object1", time.Second) {
|
2020-06-08 14:28:40 -04:00
|
|
|
panic("Failed to acquire read lock")
|
|
|
|
}
|
|
|
|
// fmt.Println("1st read lock acquired, waiting...")
|
|
|
|
|
2020-06-14 10:43:10 -04:00
|
|
|
if !lrwm.GetRLock(ctx, "", "object1", time.Second) {
|
2020-06-08 14:28:40 -04:00
|
|
|
panic("Failed to acquire read lock")
|
|
|
|
}
|
|
|
|
// fmt.Println("2nd read lock acquired, waiting...")
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
lrwm.RUnlock()
|
|
|
|
// fmt.Println("1st read lock released, waiting...")
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
lrwm.RUnlock()
|
|
|
|
// fmt.Println("2nd read lock released, waiting...")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// fmt.Println("Trying to acquire write lock, waiting...")
|
2020-06-14 10:43:10 -04:00
|
|
|
locked = lrwm.GetLock(ctx, "", "", duration)
|
2020-06-08 14:28:40 -04:00
|
|
|
if locked {
|
|
|
|
// fmt.Println("Write lock acquired, waiting...")
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
lrwm.Unlock()
|
|
|
|
} else {
|
2023-03-04 23:57:35 -05:00
|
|
|
t.Log("Write lock failed due to timeout")
|
2020-06-08 14:28:40 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleWriteLockAcquired(t *testing.T) {
|
|
|
|
locked := testSimpleWriteLock(t, 5*time.Second)
|
|
|
|
|
|
|
|
expected := true
|
|
|
|
if locked != expected {
|
|
|
|
t.Errorf("TestSimpleWriteLockAcquired(): \nexpected %#v\ngot %#v", expected, locked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleWriteLockTimedOut(t *testing.T) {
|
|
|
|
locked := testSimpleWriteLock(t, time.Second)
|
|
|
|
|
|
|
|
expected := false
|
|
|
|
if locked != expected {
|
|
|
|
t.Errorf("TestSimpleWriteLockTimedOut(): \nexpected %#v\ngot %#v", expected, locked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDualWriteLock(t *testing.T, duration time.Duration) (locked bool) {
|
2020-06-14 10:43:10 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
lrwm := NewLRWMutex()
|
2020-06-08 14:28:40 -04:00
|
|
|
|
|
|
|
// fmt.Println("Getting initial write lock")
|
2020-06-14 10:43:10 -04:00
|
|
|
if !lrwm.GetLock(ctx, "", "", time.Second) {
|
2020-06-08 14:28:40 -04:00
|
|
|
panic("Failed to acquire initial write lock")
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
lrwm.Unlock()
|
|
|
|
// fmt.Println("Initial write lock released, waiting...")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// fmt.Println("Trying to acquire 2nd write lock, waiting...")
|
2020-06-14 10:43:10 -04:00
|
|
|
locked = lrwm.GetLock(ctx, "", "", duration)
|
2020-06-08 14:28:40 -04:00
|
|
|
if locked {
|
|
|
|
// fmt.Println("2nd write lock acquired, waiting...")
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
lrwm.Unlock()
|
|
|
|
} else {
|
2023-03-04 23:57:35 -05:00
|
|
|
t.Log("2nd write lock failed due to timeout")
|
2020-06-08 14:28:40 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDualWriteLockAcquired(t *testing.T) {
|
|
|
|
locked := testDualWriteLock(t, 3*time.Second)
|
|
|
|
|
|
|
|
expected := true
|
|
|
|
if locked != expected {
|
|
|
|
t.Errorf("TestDualWriteLockAcquired(): \nexpected %#v\ngot %#v", expected, locked)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDualWriteLockTimedOut(t *testing.T) {
|
|
|
|
locked := testDualWriteLock(t, time.Second)
|
|
|
|
|
|
|
|
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 LRWMutex)
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
2020-06-14 10:43:10 -04:00
|
|
|
func parallelReader(ctx context.Context, m *LRWMutex, clocked, cunlock, cdone chan bool) {
|
|
|
|
if m.GetRLock(ctx, "", "", time.Second) {
|
2020-06-08 14:28:40 -04: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 := NewLRWMutex()
|
2020-06-08 14:28:40 -04: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)
|
2020-06-08 14:28:40 -04: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
|
|
|
|
func reader(rwm *LRWMutex, numIterations int, activity *int32, cdone chan bool) {
|
|
|
|
for i := 0; i < numIterations; i++ {
|
2020-06-14 10:43:10 -04:00
|
|
|
if rwm.GetRLock(context.Background(), "", "", time.Second) {
|
2020-06-08 14:28:40 -04: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
|
|
|
|
func writer(rwm *LRWMutex, numIterations int, activity *int32, cdone chan bool) {
|
|
|
|
for i := 0; i < numIterations; i++ {
|
2020-06-14 10:43:10 -04:00
|
|
|
if rwm.GetLock(context.Background(), "", "", time.Second) {
|
2020-06-08 14:28:40 -04: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
|
|
|
|
func HammerRWMutex(gomaxprocs, numReaders, numIterations int) {
|
|
|
|
runtime.GOMAXPROCS(gomaxprocs)
|
|
|
|
// Number of active readers + 10000 * number of active writers.
|
|
|
|
var activity int32
|
2020-06-14 10:43:10 -04:00
|
|
|
rwm := NewLRWMutex()
|
2020-06-08 14:28:40 -04:00
|
|
|
cdone := make(chan bool)
|
|
|
|
go writer(rwm, numIterations, &activity, cdone)
|
|
|
|
var i int
|
|
|
|
for i = 0; i < numReaders/2; i++ {
|
|
|
|
go reader(rwm, numIterations, &activity, cdone)
|
|
|
|
}
|
|
|
|
go writer(rwm, numIterations, &activity, cdone)
|
|
|
|
for ; i < numReaders; i++ {
|
|
|
|
go reader(rwm, numIterations, &activity, cdone)
|
|
|
|
}
|
|
|
|
// Wait for the 2 writers and all readers to finish.
|
|
|
|
for i := 0; i < 2+numReaders; i++ {
|
|
|
|
<-cdone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestRWMutex(t *testing.T) {
|
|
|
|
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(-1))
|
|
|
|
n := 1000
|
|
|
|
if testing.Short() {
|
|
|
|
n = 5
|
|
|
|
}
|
|
|
|
HammerRWMutex(1, 1, n)
|
|
|
|
HammerRWMutex(1, 3, n)
|
|
|
|
HammerRWMutex(1, 10, n)
|
|
|
|
HammerRWMutex(4, 1, n)
|
|
|
|
HammerRWMutex(4, 3, n)
|
|
|
|
HammerRWMutex(4, 10, n)
|
|
|
|
HammerRWMutex(10, 1, n)
|
|
|
|
HammerRWMutex(10, 3, n)
|
|
|
|
HammerRWMutex(10, 10, n)
|
|
|
|
HammerRWMutex(10, 5, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestDRLocker(t *testing.T) {
|
2020-06-14 10:43:10 -04:00
|
|
|
wl := NewLRWMutex()
|
2020-06-08 14:28:40 -04:00
|
|
|
var rl sync.Locker
|
|
|
|
wlocked := make(chan bool, 1)
|
|
|
|
rlocked := make(chan bool, 1)
|
|
|
|
rl = wl.DRLocker()
|
|
|
|
n := 10
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
rl.Lock()
|
|
|
|
rl.Lock()
|
|
|
|
rlocked <- true
|
|
|
|
wl.Lock()
|
|
|
|
wlocked <- true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
<-rlocked
|
|
|
|
rl.Unlock()
|
|
|
|
select {
|
|
|
|
case <-wlocked:
|
|
|
|
t.Fatal("RLocker() didn't read-lock it")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
rl.Unlock()
|
|
|
|
<-wlocked
|
|
|
|
select {
|
|
|
|
case <-rlocked:
|
|
|
|
t.Fatal("RLocker() didn't respect the write lock")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
wl.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 := NewLRWMutex()
|
2020-06-08 14:28:40 -04:00
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestUnlockPanic2(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 := NewLRWMutex()
|
2020-06-08 14:28:40 -04:00
|
|
|
mu.RLock()
|
|
|
|
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 := NewLRWMutex()
|
2020-06-08 14:28:40 -04:00
|
|
|
mu.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Borrowed from rwmutex_test.go
|
|
|
|
func TestRUnlockPanic2(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 := NewLRWMutex()
|
2020-06-08 14:28:40 -04:00
|
|
|
mu.Lock()
|
|
|
|
mu.RUnlock()
|
|
|
|
}
|