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

[SYNC] Sync stuck fix2 #3976

Merged
merged 3 commits into from
Jan 7, 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
3 changes: 2 additions & 1 deletion api/service/legacysync/syncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,9 @@ func getSyncStatusExpiration(role nodeconfig.Role) time.Duration {
func (status *syncStatus) Get(fallback func() SyncCheckResult) SyncCheckResult {
status.lock.RLock()
if !status.expired() {
result := status.lastResult
status.lock.RUnlock()
return status.lastResult
return result
}
status.lock.RUnlock()

Expand Down
47 changes: 47 additions & 0 deletions api/service/legacysync/syncing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"math/big"
"reflect"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

nodeconfig "github.com/harmony-one/harmony/internal/configs/node"

Expand Down Expand Up @@ -250,3 +253,47 @@ func makeTestBlock(bn uint64, parentHash common.Hash) *types.Block {
block := types.NewBlockWithHeader(testHeader)
return block
}

func TestSyncStatus_Get_Concurrency(t *testing.T) {
t.Skip()

ss := newSyncStatus(nodeconfig.Validator)
ss.expiration = 2 * time.Second
var (
total int32
updated int32
wg sync.WaitGroup
stop = make(chan struct{})
)

fb := func() SyncCheckResult {
time.Sleep(1 * time.Second)
atomic.AddInt32(&updated, 1)
return SyncCheckResult{IsInSync: true}
}
for i := 0; i != 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()

t := time.NewTicker(20 * time.Millisecond)
defer t.Stop()
for {
select {
case <-stop:
return
case <-t.C:
atomic.AddInt32(&total, 1)
ss.Get(fb)
}
}
}()
}

time.Sleep(10 * time.Second)
close(stop)
wg.Wait()

fmt.Printf("updated %v times\n", updated)
fmt.Printf("total %v times\n", total)
}