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

Shutdown notifications go routines #109

Merged
merged 2 commits into from
Oct 26, 2020
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
69 changes: 69 additions & 0 deletions benchmarks/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
basicnode "github.com/ipld/go-ipld-prime/node/basic"
ipldselector "github.com/ipld/go-ipld-prime/traversal/selector"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
peer "github.com/libp2p/go-libp2p-core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -61,6 +62,74 @@ func BenchmarkRoundtripSuccess(b *testing.B) {
b.Run("test-p2p-stress-10-128MB-1KB-chunks", func(b *testing.B) {
p2pStrestTest(ctx, b, 10, allFilesUniformSize(128*(1<<20), 1<<10, 1024), tdm)
})
b.Run("test-repeated-disconnects-20-10000", func(b *testing.B) {
benchmarkRepeatedDisconnects(ctx, b, 20, allFilesUniformSize(10000, defaultUnixfsChunkSize, defaultUnixfsLinksPerLevel), tdm)
})
}

func benchmarkRepeatedDisconnects(ctx context.Context, b *testing.B, numnodes int, df distFunc, tdm *tempDirMaker) {
ctx, cancel := context.WithCancel(ctx)
mn := mocknet.New(ctx)
net := tn.StreamNet(ctx, mn)
ig := testinstance.NewTestInstanceGenerator(ctx, net, nil, tdm)
instances, err := ig.Instances(numnodes + 1)
require.NoError(b, err)
var allCids [][]cid.Cid
for i := 0; i < b.N; i++ {
thisCids := df(ctx, b, instances[1:])
allCids = append(allCids, thisCids)
}
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)

allSelector := ssb.ExploreRecursive(ipldselector.RecursionLimitNone(),
ssb.ExploreAll(ssb.ExploreRecursiveEdge())).Node()

runtime.GC()
b.ResetTimer()
b.ReportAllocs()
fetcher := instances[0]
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
require.NoError(b, err)
start := time.Now()
for j := 0; j < numnodes; j++ {
instance := instances[j+1]
_, errChan := fetcher.Exchange.Request(ctx, instance.Peer, cidlink.Link{Cid: allCids[i][j]}, allSelector)

wg.Add(1)
go func(other peer.ID) {
defer func() {
mn.DisconnectPeers(fetcher.Peer, other)
wg.Done()
}()
for {
select {
case <-ctx.Done():
return
case err, ok := <-errChan:
if !ok {
return
}
b.Fatalf("received error on request: %s", err.Error())
}
}
}(instance.Peer)
}
wg.Wait()
result := runStats{
Time: time.Since(start),
Name: b.Name(),
}
benchmarkLog = append(benchmarkLog, result)

cancel()
}
cancel()
time.Sleep(100 * time.Millisecond)
b.Logf("Number of running go-routines: %d", runtime.NumGoroutine())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider tracking using https://github.com/uber-go/goleak. If we use that liberally, we should be able to detect all leaks.

testinstance.Close(instances)
ig.Close()
}

func p2pStrestTest(ctx context.Context, b *testing.B, numfiles int, df distFunc, tdm *tempDirMaker) {
Expand Down
2 changes: 2 additions & 0 deletions messagequeue/messagequeue.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func (mq *MessageQueue) Shutdown() {
}

func (mq *MessageQueue) runQueue() {
defer mq.eventPublisher.Shutdown()
mq.eventPublisher.Startup()
for {
select {
case <-mq.outgoingWork:
Expand Down
2 changes: 1 addition & 1 deletion messagequeue/messagequeue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func TestProcessingNotification(t *testing.T) {
messageNetwork := &fakeMessageNetwork{nil, nil, messageSender, &waitGroup}

messageQueue := New(ctx, peer, messageNetwork)
messageQueue.Startup()
waitGroup.Add(1)
blks := testutil.GenerateBlocksOfSize(3, 128)

Expand All @@ -164,7 +165,6 @@ func TestProcessingNotification(t *testing.T) {
messageQueue.AddResponses(newMessage.Responses(), blks, notifee)

// wait for send attempt
messageQueue.Startup()
waitGroup.Wait()

var message gsmsg.GraphSyncMessage
Expand Down
2 changes: 2 additions & 0 deletions notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ func TestSubscribeOn(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
ps := notifications.NewPublisher()
ps.Startup()
testPublisher(ctx, t, ps)
ps.Shutdown()
})
}

Expand Down
5 changes: 4 additions & 1 deletion notifications/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ func NewPublisher() Publisher {
cmdChan: make(chan cmd),
closed: make(chan struct{}),
}
go ps.start()
return ps
}

func (ps *publisher) Startup() {
go ps.start()
}

// Publish publishes an event for the given message id
func (ps *publisher) Publish(topic Topic, event Event) {
ps.lk.RLock()
Expand Down
1 change: 1 addition & 0 deletions notifications/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Publisher interface {
Close(Topic)
Publish(Topic, Event)
Shutdown()
Startup()
Subscribable
}

Expand Down
2 changes: 2 additions & 0 deletions responsemanager/peerresponsemanager/peerresponsesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ func (prs *peerResponseSender) signalWork() {
}

func (prs *peerResponseSender) run() {
defer prs.publisher.Shutdown()
prs.publisher.Startup()
for {
select {
case <-prs.ctx.Done():
Expand Down