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

[aggregator] Move placement checks to a background job #3315

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
130 changes: 65 additions & 65 deletions src/aggregator/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
const (
uninitializedCutoverNanos = math.MinInt64
uninitializedShardSetID = 0
placementCheckInterval = 10 * time.Second
)

var (
Expand Down Expand Up @@ -174,10 +175,50 @@ func (agg *aggregator) Open() error {
agg.wg.Add(1)
go agg.tick()
}

agg.wg.Add(1)
go agg.placementTick()
agg.state = aggregatorOpen
return nil
}

func (agg *aggregator) placementTick() {
defer agg.wg.Done()

ticker := time.NewTicker(placementCheckInterval)
defer ticker.Stop()

m := agg.metrics.placement

for {
select {
case <-ticker.C:
case <-agg.placementManager.C():
case <-agg.doneCh:
return
}

agg.RLock()
placement, err := agg.placementManager.Placement()
if err != nil {
m.updateFailures.Inc(1)
continue
}

if !agg.shouldProcessPlacementWithLock(placement) {
agg.RUnlock()
continue
}
agg.RUnlock()

agg.Lock()
if err := agg.processPlacementWithLock(placement); err != nil {
m.updateFailures.Inc(1)
}
agg.Unlock()
}
}

func (agg *aggregator) AddUntimed(
metric unaggregated.MetricUnion,
metadatas metadata.StagedMetadatas,
Expand Down Expand Up @@ -280,12 +321,6 @@ func (agg *aggregator) AddPassthrough(
return nil
}

pw, err := agg.passWriter()
if err != nil {
agg.metrics.addPassthrough.ReportError(err)
return err
}

mp := aggregated.ChunkedMetricWithStoragePolicy{
ChunkedMetric: aggregated.ChunkedMetric{
ChunkedID: id.ChunkedID{
Expand All @@ -298,7 +333,14 @@ func (agg *aggregator) AddPassthrough(
StoragePolicy: storagePolicy,
}

if err := pw.Write(mp); err != nil {
agg.RLock()
defer agg.RUnlock()

if agg.state != aggregatorOpen {
return errAggregatorNotOpenOrClosed
}

if err := agg.passthroughWriter.Write(mp); err != nil {
agg.metrics.addPassthrough.ReportError(err)
return err
}
Expand Down Expand Up @@ -342,21 +384,6 @@ func (agg *aggregator) Close() error {
return nil
}

func (agg *aggregator) passWriter() (writer.Writer, error) {
agg.RLock()
defer agg.RUnlock()

if agg.state != aggregatorOpen {
return nil, errAggregatorNotOpenOrClosed
}

if agg.electionManager.ElectionState() == FollowerState {
return writer.NewBlackholeWriter(), nil
}
Comment on lines -353 to -355
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

should be fine to remove this 2nd check, as double-writing metrics in this rare case is not the end of the world.


return agg.passthroughWriter, nil
}

func (agg *aggregator) shardFor(id id.RawID) (*aggregatorShard, error) {
var (
numShards = agg.currNumShards.Load()
Expand All @@ -368,52 +395,17 @@ func (agg *aggregator) shardFor(id id.RawID) (*aggregatorShard, error) {
}

agg.RLock()
shard, err := agg.shardForWithLock(id, shardID, noUpdateShards)
if err == nil || err != errActivePlacementChanged {
agg.RUnlock()
return shard, err
if int(shardID) >= len(agg.shards) {
return nil, errShardNotOwned
}
shard := agg.shards[shardID]
agg.RUnlock()

agg.Lock()
shard, err = agg.shardForWithLock(id, shardID, updateShards)
agg.Unlock()

return shard, err
}

func (agg *aggregator) shardForWithLock(
id id.RawID,
shardID uint32,
updateShardsType updateShardsType,
) (*aggregatorShard, error) {
if agg.state != aggregatorOpen {
return nil, errAggregatorNotOpenOrClosed
}

placement, err := agg.placementManager.Placement()
if err != nil {
return nil, err
}

if agg.shouldProcessPlacementWithLock(placement) {
if updateShardsType == noUpdateShards {
return nil, errActivePlacementChanged
}
if err := agg.processPlacementWithLock(placement); err != nil {
return nil, err
}
// check if number of shards in placement changed, and recalculate shardID if needed
if int32(placement.NumShards()) != agg.currNumShards.Load() {
shardID = agg.shardFn(id, uint32(placement.NumShards()))
}
}

if int(shardID) >= len(agg.shards) || agg.shards[shardID] == nil {
if shard == nil {
return nil, errShardNotOwned
}

return agg.shards[shardID], nil
return shard, nil
}

func (agg *aggregator) processPlacementWithLock(
Expand All @@ -423,7 +415,13 @@ func (agg *aggregator) processPlacementWithLock(
if !agg.shouldProcessPlacementWithLock(newPlacement) {
return nil
}
var newShardSet shard.Shards

var (
metrics = agg.metrics.placement
newShardSet shard.Shards
)

metrics.cutoverChanged.Inc(1)
instance, err := agg.placementManager.InstanceFrom(newPlacement)
if err == nil {
newShardSet = instance.Shards()
Expand Down Expand Up @@ -452,7 +450,8 @@ func (agg *aggregator) processPlacementWithLock(
return err
}

agg.metrics.placement.updated.Inc(1)
metrics.updated.Inc(1)

return nil
}

Expand All @@ -462,7 +461,6 @@ func (agg *aggregator) shouldProcessPlacementWithLock(
// If there is no placement yet, or the placement has been updated,
// process this placement.
if agg.currPlacement == nil || agg.currPlacement != newPlacement {
agg.metrics.placement.cutoverChanged.Inc(1)
return true
}
return false
Expand Down Expand Up @@ -1001,12 +999,14 @@ func newAggregatorShardsMetrics(scope tally.Scope) aggregatorShardsMetrics {
type aggregatorPlacementMetrics struct {
cutoverChanged tally.Counter
updated tally.Counter
updateFailures tally.Counter
}

func newAggregatorPlacementMetrics(scope tally.Scope) aggregatorPlacementMetrics {
return aggregatorPlacementMetrics{
cutoverChanged: scope.Counter("placement-changed"),
updated: scope.Counter("updated"),
updateFailures: scope.Counter("update-failures"),
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/aggregator/aggregator/aggregator_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions src/aggregator/aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ func TestAggregatorOpenInstanceNotInPlacement(t *testing.T) {
testPlacement := placement.NewPlacement().SetCutoverNanos(5678)

placementManager.EXPECT().Open().Return(nil)
placementManager.EXPECT().C().Return(make(chan struct{})).AnyTimes()
placementManager.EXPECT().InstanceID().Return(agg.opts.PlacementManager().InstanceID())
placementManager.EXPECT().Placement().Return(testPlacement, nil)
placementManager.EXPECT().Placement().Return(testPlacement, nil).AnyTimes()
placementManager.EXPECT().InstanceFrom(testPlacement).Return(nil, ErrInstanceNotFoundInPlacement)

require.NoError(t, agg.Open())
Expand Down Expand Up @@ -324,7 +325,7 @@ func TestAggregatorAddUntimedNotOpen(t *testing.T) {

agg, _ := testAggregator(t, ctrl)
err := agg.AddUntimed(testUntimedMetric, testStagedMetadatas)
require.Equal(t, errAggregatorNotOpenOrClosed, err)
require.Equal(t, errShardNotOwned, err)
}

func TestAggregatorAddUntimedNotResponsibleForShard(t *testing.T) {
Expand Down Expand Up @@ -364,6 +365,8 @@ func TestAggregatorAddUntimedSuccessWithPlacementUpdate(t *testing.T) {
require.NoError(t, agg.Open())
require.Equal(t, int64(testPlacementCutover), agg.currPlacement.CutoverNanos())

existingShard := agg.shards[3]

newShardAssignment := []shard.Shard{
shard.NewShard(0).SetState(shard.Initializing).SetCutoverNanos(5000).SetCutoffNanos(20000),
shard.NewShard(1).SetState(shard.Initializing).SetCutoverNanos(5500).SetCutoffNanos(25000),
Expand All @@ -385,7 +388,6 @@ func TestAggregatorAddUntimedSuccessWithPlacementUpdate(t *testing.T) {
time.Sleep(100 * time.Millisecond)
}

existingShard := agg.shards[3]
err = agg.AddUntimed(testUntimedMetric, testStagedMetadatas)
require.NoError(t, err)
require.Equal(t, 5, len(agg.shards))
Expand All @@ -412,14 +414,15 @@ func TestAggregatorAddUntimedSuccessWithPlacementUpdate(t *testing.T) {
}
require.Equal(t, 1, len(agg.shards[1].metricMap.entries))
require.Equal(t, newPlacementCutoverNanos, agg.currPlacement.CutoverNanos())

for {
existingShard.RLock()
closed := existingShard.closed
existingShard.RUnlock()

if closed {
break
}

time.Sleep(100 * time.Millisecond)
}
}
Expand All @@ -430,7 +433,7 @@ func TestAggregatorAddTimedNotOpen(t *testing.T) {

agg, _ := testAggregator(t, ctrl)
err := agg.AddTimed(testTimedMetric, testTimedMetadata)
require.Equal(t, errAggregatorNotOpenOrClosed, err)
require.Equal(t, errShardNotOwned, err)
}

func TestAggregatorAddTimedNotResponsibleForShard(t *testing.T) {
Expand Down Expand Up @@ -474,6 +477,8 @@ func TestAggregatorAddTimedSuccessWithPlacementUpdate(t *testing.T) {
require.NoError(t, agg.Open())
require.Equal(t, int64(testPlacementCutover), agg.currPlacement.CutoverNanos())

existingShard := agg.shards[3]

newShardAssignment := []shard.Shard{
shard.NewShard(0).SetState(shard.Initializing).SetCutoverNanos(5000).SetCutoffNanos(20000),
shard.NewShard(1).SetState(shard.Initializing).SetCutoverNanos(5500).SetCutoffNanos(25000),
Expand All @@ -495,7 +500,6 @@ func TestAggregatorAddTimedSuccessWithPlacementUpdate(t *testing.T) {
time.Sleep(100 * time.Millisecond)
}

existingShard := agg.shards[3]
err = agg.AddTimed(testTimedMetric, testTimedMetadata)
require.NoError(t, err)
require.Equal(t, 5, len(agg.shards))
Expand Down Expand Up @@ -540,7 +544,7 @@ func TestAggregatorAddForwardedNotOpen(t *testing.T) {

agg, _ := testAggregator(t, ctrl)
err := agg.AddForwarded(testForwardedMetric, testForwardMetadata)
require.Equal(t, errAggregatorNotOpenOrClosed, err)
require.Equal(t, errShardNotOwned, err)
}

func TestAggregatorAddForwardedNotResponsibleForShard(t *testing.T) {
Expand Down Expand Up @@ -584,6 +588,8 @@ func TestAggregatorAddForwardedSuccessWithPlacementUpdate(t *testing.T) {
require.NoError(t, agg.Open())
require.Equal(t, int64(testPlacementCutover), agg.currPlacement.CutoverNanos())

existingShard := agg.shards[3]

newShardAssignment := []shard.Shard{
shard.NewShard(0).SetState(shard.Initializing).SetCutoverNanos(5000).SetCutoffNanos(20000),
shard.NewShard(1).SetState(shard.Initializing).SetCutoverNanos(5500).SetCutoffNanos(25000),
Expand All @@ -605,7 +611,6 @@ func TestAggregatorAddForwardedSuccessWithPlacementUpdate(t *testing.T) {
time.Sleep(100 * time.Millisecond)
}

existingShard := agg.shards[3]
err = agg.AddForwarded(testForwardedMetric, testForwardMetadata)
require.NoError(t, err)
require.Equal(t, 5, len(agg.shards))
Expand Down Expand Up @@ -1042,10 +1047,10 @@ func testAggregatorWithCustomPlacements(
ctrl *gomock.Controller,
proto *placementpb.PlacementSnapshots,
) (*aggregator, kv.Store) {
watcher, store := testWatcherWithPlacementProto(t, testPlacementKey, proto)
watcherOpts, store := testWatcherOptsWithPlacementProto(t, testPlacementKey, proto)
placementManagerOpts := NewPlacementManagerOptions().
SetInstanceID(testInstanceID).
SetWatcher(watcher)
SetWatcherOptions(watcherOpts)
placementManager := NewPlacementManager(placementManagerOpts)
opts := testOptions(ctrl).
SetEntryCheckInterval(0).
Expand All @@ -1054,20 +1059,19 @@ func testAggregatorWithCustomPlacements(
}

// nolint: unparam
func testWatcherWithPlacementProto(
func testWatcherOptsWithPlacementProto(
t *testing.T,
placementKey string,
proto *placementpb.PlacementSnapshots,
) (placement.Watcher, kv.Store) {
) (placement.WatcherOptions, kv.Store) {
t.Helper()
store := mem.NewStore()
_, err := store.SetIfNotExists(placementKey, proto)
require.NoError(t, err)
placementWatcherOpts := placement.NewWatcherOptions().
SetStagedPlacementKey(placementKey).
SetStagedPlacementStore(store)
placementWatcher := placement.NewPlacementsWatcher(placementWatcherOpts)
return placementWatcher, store
return placementWatcherOpts, store
}

// nolint: unparam
Expand Down
Loading