Skip to content

Commit

Permalink
Merge pull request #84 from vinted/vinted_pull_deadlock_fix
Browse files Browse the repository at this point in the history
*: pull deadlock fix
  • Loading branch information
GiedriusS authored Feb 1, 2024
2 parents 03dc1a8 + d1c032f commit a81df7a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
8 changes: 0 additions & 8 deletions pkg/receive/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,14 +641,6 @@ func (h *Handler) forward(ctx context.Context, tenantHTTP string, r replica, wre
span, ctx := tracing.StartSpan(ctx, "receive_fanout_forward")
defer span.Finish()

// It is possible that hashring is ready in testReady() but unready now,
// so need to lock here.
h.mtx.RLock()
if h.hashring == nil {
h.mtx.RUnlock()
return errors.New("hashring is not ready")
}

var replicas []uint64
if r.replicated {
replicas = []uint64{r.n}
Expand Down
76 changes: 76 additions & 0 deletions pkg/receive/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1709,3 +1709,79 @@ func TestDistributeSeries(t *testing.T) {
require.Equal(t, 1, labelpb.ZLabelsToPromLabels(remote[endpointReplica{endpoint: "http://localhost:9090", replica: 0}].timeSeries[1].Labels).Len())
require.Equal(t, map[string]struct{}{"bar": {}, "boo": {}}, hr.seenTenants)
}

func TestHandlerFlippingHashrings(t *testing.T) {
h := NewHandler(log.NewLogfmtLogger(os.Stderr), &Options{})
t.Cleanup(h.Close)

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

h1, err := newSimpleHashring([]Endpoint{
{
Address: "http://localhost:9090",
},
})
require.NoError(t, err)
h2, err := newSimpleHashring([]Endpoint{
{
Address: "http://localhost:9091",
},
})
require.NoError(t, err)

h.Hashring(h1)

var wg sync.WaitGroup

wg.Add(2)
go func() {
defer wg.Done()

for {
select {
case <-time.After(50 * time.Millisecond):
case <-ctx.Done():
return
}

err := h.handleRequest(ctx, 0, "test", &prompb.WriteRequest{
Timeseries: []prompb.TimeSeries{
{
Labels: labelpb.ZLabelsFromPromLabels(labels.FromStrings("foo", "bar")),
Samples: []prompb.Sample{
{
Timestamp: time.Now().Unix(),
Value: 123,
},
},
},
},
})
require.Error(t, err)
}
}()
go func() {
defer wg.Done()
var flipper bool

for {
select {
case <-time.After(200 * time.Millisecond):
case <-ctx.Done():
return
}

if flipper {
h.Hashring(h2)
} else {
h.Hashring(h1)
}
flipper = !flipper
}
}()

<-time.After(1 * time.Second)
cancel()
wg.Wait()
}

0 comments on commit a81df7a

Please sign in to comment.