-
Notifications
You must be signed in to change notification settings - Fork 829
Added per-tenant in-process sharding support to compactor #2599
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
Merged
pracucci
merged 9 commits into
cortexproject:master
from
pracucci:parallelise-compactor
May 18, 2020
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b2c4005
Added per-tenant in-process sharding support to compactor
pracucci 9aa37d9
Added concurrency config option
pracucci 3a5666b
Fixed filter
pracucci 0467868
Updated CHANGELOG
pracucci 20ad436
Improved distribution test
pracucci e9e9f89
Fixed external labels removal at query time
pracucci b559cc3
Fixed removal of external labels when querying back blocks from storage
pracucci bf51fd4
Added unit test
pracucci df62410
Fixed linter
pracucci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package compactor | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"github.com/oklog/ulid" | ||
"github.com/thanos-io/thanos/pkg/block/metadata" | ||
"github.com/thanos-io/thanos/pkg/extprom" | ||
|
||
"github.com/cortexproject/cortex/pkg/ingester/client" | ||
cortex_tsdb "github.com/cortexproject/cortex/pkg/storage/tsdb" | ||
) | ||
|
||
type BlocksShardingFilter struct { | ||
shards uint32 | ||
} | ||
|
||
func NewBlocksShardingFilter(shards uint32) *BlocksShardingFilter { | ||
return &BlocksShardingFilter{ | ||
shards: shards, | ||
} | ||
} | ||
|
||
func (f *BlocksShardingFilter) Filter(_ context.Context, metas map[ulid.ULID]*metadata.Meta, _ *extprom.TxGaugeVec) error { | ||
for _, m := range metas { | ||
// Just remove the ingester ID label if sharding is disabled. | ||
if f.shards <= 1 { | ||
delete(m.Thanos.Labels, cortex_tsdb.IngesterIDExternalLabel) | ||
continue | ||
} | ||
|
||
// Skip any block already containing the shard ID to avoid any manipulation | ||
// (ie. in case the sharding algorithm changes over the time). | ||
if _, ok := m.Thanos.Labels[cortex_tsdb.ShardIDExternalLabel]; ok { | ||
continue | ||
} | ||
|
||
// Skip any block without the ingester ID, which means the block has been generated | ||
// before the introduction of the ingester ID. | ||
ingesterID, ok := m.Thanos.Labels[cortex_tsdb.IngesterIDExternalLabel] | ||
if !ok { | ||
continue | ||
} | ||
|
||
// Compute the shard ID and replace the ingester label with the shard label | ||
// so that the compactor will group blocks based on the shard. | ||
shardID := shardByIngesterID(ingesterID, f.shards) | ||
delete(m.Thanos.Labels, cortex_tsdb.IngesterIDExternalLabel) | ||
m.Thanos.Labels[cortex_tsdb.ShardIDExternalLabel] = shardID | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// hashIngesterID returns a 32-bit hash of the ingester ID. | ||
func hashIngesterID(id string) uint32 { | ||
h := client.HashNew32() | ||
h = client.HashAdd32(h, id) | ||
return h | ||
} | ||
|
||
// shardByIngesterID returns the shard given an ingester ID. | ||
func shardByIngesterID(id string, numShards uint32) string { | ||
return strconv.Itoa(int(hashIngesterID(id) % numShards)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package compactor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/oklog/ulid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/thanos-io/thanos/pkg/block/metadata" | ||
|
||
cortex_tsdb "github.com/cortexproject/cortex/pkg/storage/tsdb" | ||
) | ||
|
||
func TestHashIngesterID(t *testing.T) { | ||
tests := []struct { | ||
first string | ||
second string | ||
expectedEqual bool | ||
}{ | ||
{ | ||
first: "ingester-0", | ||
second: "ingester-0", | ||
expectedEqual: true, | ||
}, | ||
{ | ||
first: "ingester-0", | ||
second: "ingester-1", | ||
expectedEqual: false, | ||
}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
firstHash := hashIngesterID(testCase.first) | ||
secondHash := hashIngesterID(testCase.second) | ||
assert.Equal(t, testCase.expectedEqual, firstHash == secondHash) | ||
} | ||
} | ||
|
||
func TestShardByIngesterID_DistributionForKubernetesStatefulSets(t *testing.T) { | ||
const ( | ||
numShards = 3 | ||
distributionThreshold = 0.8 | ||
) | ||
|
||
for _, numIngesters := range []int{10, 30, 50, 100} { | ||
// Generate the ingester IDs. | ||
ids := make([]string, numIngesters) | ||
for i := 0; i < numIngesters; i++ { | ||
ids[i] = fmt.Sprintf("ingester-%d", i) | ||
} | ||
|
||
// Compute the shard for each ingester. | ||
distribution := map[string][]string{} | ||
for _, id := range ids { | ||
shard := shardByIngesterID(id, numShards) | ||
distribution[shard] = append(distribution[shard], id) | ||
} | ||
|
||
// Ensure the distribution is fair. | ||
minSizePerShard := distributionThreshold * (float64(numIngesters) / float64(numShards)) | ||
|
||
for _, ingesters := range distribution { | ||
assert.GreaterOrEqual(t, len(ingesters), int(minSizePerShard)) | ||
} | ||
} | ||
} | ||
|
||
func TestBlocksShardingFilter(t *testing.T) { | ||
block1 := ulid.MustNew(1, nil) | ||
block2 := ulid.MustNew(2, nil) | ||
block3 := ulid.MustNew(3, nil) | ||
|
||
tests := map[string]struct { | ||
numShards uint32 | ||
input map[ulid.ULID]map[string]string | ||
expected map[ulid.ULID]map[string]string | ||
}{ | ||
"blocks from the same ingester should go into the same shard": { | ||
numShards: 3, | ||
input: map[ulid.ULID]map[string]string{ | ||
block1: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block2: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block3: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
}, | ||
expected: map[ulid.ULID]map[string]string{ | ||
block1: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block2: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block3: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
}, | ||
}, | ||
"blocks from the different ingesters should be sharded": { | ||
numShards: 3, | ||
input: map[ulid.ULID]map[string]string{ | ||
block1: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block2: {cortex_tsdb.IngesterIDExternalLabel: "ingester-1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block3: {cortex_tsdb.IngesterIDExternalLabel: "ingester-2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
}, | ||
expected: map[ulid.ULID]map[string]string{ | ||
block1: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block2: {cortex_tsdb.ShardIDExternalLabel: "1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block3: {cortex_tsdb.ShardIDExternalLabel: "0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
}, | ||
}, | ||
"blocks without ingester ID should not be mangled": { | ||
numShards: 3, | ||
input: map[ulid.ULID]map[string]string{ | ||
block1: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block2: {cortex_tsdb.ShardIDExternalLabel: "1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block3: {cortex_tsdb.ShardIDExternalLabel: "0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
}, | ||
expected: map[ulid.ULID]map[string]string{ | ||
block1: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block2: {cortex_tsdb.ShardIDExternalLabel: "1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block3: {cortex_tsdb.ShardIDExternalLabel: "0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
}, | ||
}, | ||
"should remove the ingester ID external label if sharding is disabled": { | ||
numShards: 1, | ||
input: map[ulid.ULID]map[string]string{ | ||
block1: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block2: {cortex_tsdb.IngesterIDExternalLabel: "ingester-1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block3: {cortex_tsdb.IngesterIDExternalLabel: "ingester-2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
}, | ||
expected: map[ulid.ULID]map[string]string{ | ||
block1: {cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block2: {cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
block3: {cortex_tsdb.TenantIDExternalLabel: "user-1"}, | ||
}, | ||
}, | ||
} | ||
|
||
for testName, testData := range tests { | ||
t.Run(testName, func(t *testing.T) { | ||
metas := map[ulid.ULID]*metadata.Meta{} | ||
for id, lbls := range testData.input { | ||
metas[id] = &metadata.Meta{Thanos: metadata.Thanos{Labels: lbls}} | ||
} | ||
|
||
f := NewBlocksShardingFilter(testData.numShards) | ||
err := f.Filter(context.Background(), metas, nil) | ||
require.NoError(t, err) | ||
assert.Len(t, metas, len(testData.expected)) | ||
|
||
for expectedID, expectedLbls := range testData.expected { | ||
assert.NotNil(t, metas[expectedID]) | ||
assert.Equal(t, metas[expectedID].Thanos.Labels, expectedLbls) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.