|
| 1 | +package compactor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/oklog/ulid" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "github.com/thanos-io/thanos/pkg/block/metadata" |
| 12 | + |
| 13 | + cortex_tsdb "github.com/cortexproject/cortex/pkg/storage/tsdb" |
| 14 | +) |
| 15 | + |
| 16 | +func TestHashIngesterID(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + first string |
| 19 | + second string |
| 20 | + expectedEqual bool |
| 21 | + }{ |
| 22 | + { |
| 23 | + first: "ingester-0", |
| 24 | + second: "ingester-0", |
| 25 | + expectedEqual: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + first: "ingester-0", |
| 29 | + second: "ingester-1", |
| 30 | + expectedEqual: false, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + for _, testCase := range tests { |
| 35 | + firstHash := hashIngesterID(testCase.first) |
| 36 | + secondHash := hashIngesterID(testCase.second) |
| 37 | + assert.Equal(t, testCase.expectedEqual, firstHash == secondHash) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestShardByIngesterID_DistributionForKubernetesStatefulSets(t *testing.T) { |
| 42 | + const ( |
| 43 | + numShards = 3 |
| 44 | + distributionThreshold = 0.8 |
| 45 | + ) |
| 46 | + |
| 47 | + for _, numIngesters := range []int{10, 30, 50, 100} { |
| 48 | + // Generate the ingester IDs. |
| 49 | + ids := make([]string, numIngesters) |
| 50 | + for i := 0; i < numIngesters; i++ { |
| 51 | + ids[i] = fmt.Sprintf("ingester-%d", i) |
| 52 | + } |
| 53 | + |
| 54 | + // Compute the shard for each ingester. |
| 55 | + distribution := map[string][]string{} |
| 56 | + for _, id := range ids { |
| 57 | + shard := shardByIngesterID(id, numShards) |
| 58 | + distribution[shard] = append(distribution[shard], id) |
| 59 | + } |
| 60 | + |
| 61 | + // Ensure the distribution is fair. |
| 62 | + minSizePerShard := distributionThreshold * (float64(numIngesters) / float64(numShards)) |
| 63 | + |
| 64 | + for _, ingesters := range distribution { |
| 65 | + assert.GreaterOrEqual(t, len(ingesters), int(minSizePerShard)) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestBlocksShardingFilter(t *testing.T) { |
| 71 | + block1 := ulid.MustNew(1, nil) |
| 72 | + block2 := ulid.MustNew(2, nil) |
| 73 | + block3 := ulid.MustNew(3, nil) |
| 74 | + |
| 75 | + tests := map[string]struct { |
| 76 | + numShards uint32 |
| 77 | + input map[ulid.ULID]map[string]string |
| 78 | + expected map[ulid.ULID]map[string]string |
| 79 | + }{ |
| 80 | + "blocks from the same ingester should go into the same shard": { |
| 81 | + numShards: 3, |
| 82 | + input: map[ulid.ULID]map[string]string{ |
| 83 | + block1: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 84 | + block2: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 85 | + block3: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 86 | + }, |
| 87 | + expected: map[ulid.ULID]map[string]string{ |
| 88 | + block1: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 89 | + block2: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 90 | + block3: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + "blocks from the different ingesters should be sharded": { |
| 94 | + numShards: 3, |
| 95 | + input: map[ulid.ULID]map[string]string{ |
| 96 | + block1: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 97 | + block2: {cortex_tsdb.IngesterIDExternalLabel: "ingester-1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 98 | + block3: {cortex_tsdb.IngesterIDExternalLabel: "ingester-2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 99 | + }, |
| 100 | + expected: map[ulid.ULID]map[string]string{ |
| 101 | + block1: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 102 | + block2: {cortex_tsdb.ShardIDExternalLabel: "1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 103 | + block3: {cortex_tsdb.ShardIDExternalLabel: "0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 104 | + }, |
| 105 | + }, |
| 106 | + "blocks without ingester ID should not be mangled": { |
| 107 | + numShards: 3, |
| 108 | + input: map[ulid.ULID]map[string]string{ |
| 109 | + block1: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 110 | + block2: {cortex_tsdb.ShardIDExternalLabel: "1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 111 | + block3: {cortex_tsdb.ShardIDExternalLabel: "0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 112 | + }, |
| 113 | + expected: map[ulid.ULID]map[string]string{ |
| 114 | + block1: {cortex_tsdb.ShardIDExternalLabel: "2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 115 | + block2: {cortex_tsdb.ShardIDExternalLabel: "1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 116 | + block3: {cortex_tsdb.ShardIDExternalLabel: "0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + "should remove the ingester ID external label if sharding is disabled": { |
| 120 | + numShards: 1, |
| 121 | + input: map[ulid.ULID]map[string]string{ |
| 122 | + block1: {cortex_tsdb.IngesterIDExternalLabel: "ingester-0", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 123 | + block2: {cortex_tsdb.IngesterIDExternalLabel: "ingester-1", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 124 | + block3: {cortex_tsdb.IngesterIDExternalLabel: "ingester-2", cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 125 | + }, |
| 126 | + expected: map[ulid.ULID]map[string]string{ |
| 127 | + block1: {cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 128 | + block2: {cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 129 | + block3: {cortex_tsdb.TenantIDExternalLabel: "user-1"}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + for testName, testData := range tests { |
| 135 | + t.Run(testName, func(t *testing.T) { |
| 136 | + metas := map[ulid.ULID]*metadata.Meta{} |
| 137 | + for id, lbls := range testData.input { |
| 138 | + metas[id] = &metadata.Meta{Thanos: metadata.Thanos{Labels: lbls}} |
| 139 | + } |
| 140 | + |
| 141 | + f := NewBlocksShardingFilter(testData.numShards) |
| 142 | + err := f.Filter(context.Background(), metas, nil) |
| 143 | + require.NoError(t, err) |
| 144 | + assert.Len(t, metas, len(testData.expected)) |
| 145 | + |
| 146 | + for expectedID, expectedLbls := range testData.expected { |
| 147 | + assert.NotNil(t, metas[expectedID]) |
| 148 | + assert.Equal(t, metas[expectedID].Thanos.Labels, expectedLbls) |
| 149 | + } |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments