Skip to content

Commit

Permalink
pd-client: fix a data race in pd client (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao committed Jun 10, 2017
1 parent 645a096 commit ce8db87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pd-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,15 @@ type TSFuture interface {
}

func (req *tsoRequest) Wait() (int64, int64, error) {
defer func() { cmdDuration.WithLabelValues("tso").Observe(time.Since(req.start).Seconds()) }()
select {
case err := <-req.done:
defer tsoReqPool.Put(req)
if err != nil {
cmdFailedDuration.WithLabelValues("tso").Observe(time.Since(req.start).Seconds())
return 0, 0, errors.Trace(err)
}
physical, logical := req.physical, req.logical
tsoReqPool.Put(req)
cmdDuration.WithLabelValues("tso").Observe(time.Since(req.start).Seconds())
return physical, logical, err
case <-req.ctx.Done():
return 0, 0, errors.Trace(req.ctx.Err())
Expand Down
20 changes: 20 additions & 0 deletions pd-client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net"
"os"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -175,6 +176,25 @@ func (s *testClientSuite) TestTSO(c *C) {
}
}

func (s *testClientSuite) TestTSORace(c *C) {
var wg sync.WaitGroup
begin := make(chan struct{})
count := 10
wg.Add(count)
for i := 0; i < count; i++ {
go func() {
<-begin
for i := 0; i < 100; i++ {
_, _, err := s.client.GetTS(context.Background())
c.Assert(err, IsNil)
}
wg.Done()
}()
}
close(begin)
wg.Wait()
}

func (s *testClientSuite) TestGetRegion(c *C) {
req := &pdpb.RegionHeartbeatRequest{
Header: newHeader(s.srv),
Expand Down

0 comments on commit ce8db87

Please sign in to comment.