Skip to content

Commit

Permalink
[aggregator] Prevent tcp client panic on nil placement (#3139)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcerniauskas authored Feb 1, 2021
1 parent 54474fd commit c8ce265
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/aggregator/client/tcp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package client

import (
"errors"
"fmt"
"math"
"time"
Expand All @@ -39,7 +40,11 @@ import (
xerrors "github.com/m3db/m3/src/x/errors"
)

var _ AdminClient = (*TCPClient)(nil)
var (
_ AdminClient = (*TCPClient)(nil)

errNilPlacement = errors.New("placement is nil")
)

// TCPClient sends metrics to M3 Aggregator via over custom TCP protocol.
type TCPClient struct {
Expand Down Expand Up @@ -229,6 +234,9 @@ func (c *TCPClient) ActivePlacement() (placement.Placement, int, error) {
return nil, 0, err
}
defer onStagedPlacementDoneFn()
if stagedPlacement == nil {
return nil, 0, errNilPlacement
}

placement, onPlacementDoneFn, err := stagedPlacement.ActivePlacement()
if err != nil {
Expand All @@ -247,6 +255,9 @@ func (c *TCPClient) ActivePlacementVersion() (int, error) {
return 0, err
}
defer onStagedPlacementDoneFn()
if stagedPlacement == nil {
return 0, errNilPlacement
}

return stagedPlacement.Version(), nil
}
Expand Down Expand Up @@ -274,6 +285,10 @@ func (c *TCPClient) write(
if err != nil {
return err
}
if stagedPlacement == nil {
onStagedPlacementDoneFn()
return errNilPlacement
}
placement, onPlacementDoneFn, err := stagedPlacement.ActivePlacement()
if err != nil {
onStagedPlacementDoneFn()
Expand Down
25 changes: 25 additions & 0 deletions src/aggregator/client/tcp_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,31 @@ func TestTCPClientWriteUntimedMetricActiveStagedPlacementError(t *testing.T) {
}
}

func TestTCPClientWriteUntimedMetricActiveStagedPlacementNil(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

watcher := placement.NewMockStagedPlacementWatcher(ctrl)
watcher.EXPECT().ActiveStagedPlacement().
Return(nil, func() {}, nil).
MinTimes(1)
c := mustNewTestTCPClient(t, testOptions())
c.placementWatcher = watcher

for _, input := range []unaggregated.MetricUnion{testCounter, testBatchTimer, testGauge} {
var err error
switch input.Type {
case metric.CounterType:
err = c.WriteUntimedCounter(input.Counter(), testStagedMetadatas)
case metric.TimerType:
err = c.WriteUntimedBatchTimer(input.BatchTimer(), testStagedMetadatas)
case metric.GaugeType:
err = c.WriteUntimedGauge(input.Gauge(), testStagedMetadatas)
}
require.Equal(t, errNilPlacement, err)
}
}

func TestTCPClientWriteUntimedMetricActivePlacementError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down

0 comments on commit c8ce265

Please sign in to comment.