Skip to content

Commit e945eb4

Browse files
committed
Added new implementation that makes the distributor accept multiple HA pairs (cluster, replica) in the same requets/batch. This can be enabled with a new flag, accept_mixed_ha_samples, an will take effect only if accept_ha_samples is set to true.
Fixed test by reducing the number of ingesters to 2 and replication factor to 2. Added config reference. Do not remove replica label if cluster label is not present. Added more HA mixed replicas tests with no cluster and replica labels and with cluster label only. Added e2e test for mixed HA samples in the same request. Refactored distributor mixed HA samples logic. Signed-off-by: eduardscaueru <edi_scaueru@yahoo.com>
1 parent e070ec6 commit e945eb4

File tree

6 files changed

+369
-2
lines changed

6 files changed

+369
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [FEATURE] Ruler: Minimize chances of missed rule group evaluations that can occur due to OOM kills, bad underlying nodes, or due to an unhealthy ruler that appears in the ring as healthy. This feature is enabled via `-ruler.enable-ha-evaluation` flag. #6129
1010
* [FEATURE] Store Gateway: Add an in-memory chunk cache. #6245
1111
* [FEATURE] Chunk Cache: Support multi level cache and add metrics. #6249
12+
* [FEATURE] Distributor: Accept multiple HA Tracker pairs in the same request. #6256
1213
* [ENHANCEMENT] Ingester: Add `blocks-storage.tsdb.wal-compression-type` to support zstd wal compression type. #6232
1314
* [ENHANCEMENT] Query Frontend: Add info field to query response. #6207
1415
* [ENHANCEMENT] Query Frontend: Add peakSample in query stats response. #6188

docs/configuration/config-file-reference.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,6 +3163,12 @@ The `limits_config` configures default and per-tenant limits imposed by Cortex s
31633163
# CLI flag: -distributor.ha-tracker.enable-for-all-users
31643164
[accept_ha_samples: <boolean> | default = false]
31653165
3166+
# Flag to enable handling of samples with mixed external labels identifying
3167+
# replicas in an HA Prometheus setup. Supported only if
3168+
# -distributor.ha-tracker.enable-for-all-users is true.
3169+
# CLI flag: -distributor.ha-tracker.mixed-ha-samples
3170+
[accept_mixed_ha_samples: <boolean> | default = false]
3171+
31663172
# Prometheus label to look for in samples to identify a Prometheus HA cluster.
31673173
# CLI flag: -distributor.ha-tracker.cluster
31683174
[ha_cluster_label: <string> | default = "cluster"]
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//go:build requires_docker
2+
// +build requires_docker
3+
4+
package integration
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
"time"
10+
11+
"github.com/prometheus/common/model"
12+
"github.com/prometheus/prometheus/prompb"
13+
"github.com/stretchr/testify/require"
14+
15+
"github.com/cortexproject/cortex/integration/e2e"
16+
e2edb "github.com/cortexproject/cortex/integration/e2e/db"
17+
"github.com/cortexproject/cortex/integration/e2ecortex"
18+
)
19+
20+
func TestDistriubtorAcceptMixedHASamplesRunningInMicroservicesMode(t *testing.T) {
21+
const blockRangePeriod = 5 * time.Minute
22+
23+
t.Run(fmt.Sprintf("%s", "Distriubtor accept mixed HA samples in the same request"), func(t *testing.T) {
24+
s, err := e2e.NewScenario(networkName)
25+
require.NoError(t, err)
26+
defer s.Close()
27+
28+
// Configure the querier to only look in ingester
29+
// and enbale distributor ha tracker with mixed samples.
30+
distributorFlags := map[string]string{
31+
"-distributor.ha-tracker.enable": "true",
32+
"-distributor.ha-tracker.enable-for-all-users": "true",
33+
"-distributor.ha-tracker.mixed-ha-samples": "true",
34+
"-distributor.ha-tracker.cluster": "cluster",
35+
"-distributor.ha-tracker.replica": "__replica__",
36+
"-distributor.ha-tracker.store": "etcd",
37+
"-distributor.ha-tracker.etcd.endpoints": "etcd:2379",
38+
}
39+
querierFlags := mergeFlags(BlocksStorageFlags(), map[string]string{
40+
"-querier.query-store-after": (1 * time.Hour).String(),
41+
})
42+
flags := mergeFlags(BlocksStorageFlags(), map[string]string{
43+
"-blocks-storage.tsdb.block-ranges-period": blockRangePeriod.String(),
44+
"-blocks-storage.tsdb.ship-interval": "5s",
45+
"-blocks-storage.tsdb.retention-period": ((blockRangePeriod * 2) - 1).String(),
46+
"-blocks-storage.bucket-store.max-chunk-pool-bytes": "1",
47+
})
48+
49+
// Start dependencies.
50+
consul := e2edb.NewConsul()
51+
etcd := e2edb.NewETCD()
52+
minio := e2edb.NewMinio(9000, flags["-blocks-storage.s3.bucket-name"])
53+
require.NoError(t, s.StartAndWaitReady(consul, etcd, minio))
54+
55+
// Start Cortex components.
56+
distributor := e2ecortex.NewDistributor("distributor", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), distributorFlags, "")
57+
ingester := e2ecortex.NewIngester("ingester", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "")
58+
require.NoError(t, s.StartAndWaitReady(distributor, ingester))
59+
60+
// Wait until both the distributor and ingester have updated the ring.
61+
require.NoError(t, distributor.WaitSumMetrics(e2e.Equals(512), "cortex_ring_tokens_total"))
62+
63+
c, err := e2ecortex.NewClient(distributor.HTTPEndpoint(), "", "", "", "user-1")
64+
require.NoError(t, err)
65+
66+
// Push some series to Cortex.
67+
series1Timestamp := time.Now()
68+
series2Timestamp := series1Timestamp.Add(-2 * time.Second)
69+
series3Timestamp := series1Timestamp.Add(-4 * time.Second)
70+
series4Timestamp := series1Timestamp.Add(-6 * time.Second)
71+
series5Timestamp := series1Timestamp.Add(-8 * time.Second)
72+
series6Timestamp := series1Timestamp.Add(-10 * time.Second)
73+
series7Timestamp := series1Timestamp.Add(-12 * time.Second)
74+
series1, _ := generateSeries("foo", series1Timestamp, prompb.Label{Name: "__replica__", Value: "replica0"}, prompb.Label{Name: "cluster", Value: "cluster0"})
75+
series2, _ := generateSeries("foo", series2Timestamp, prompb.Label{Name: "__replica__", Value: "replica1"}, prompb.Label{Name: "cluster", Value: "cluster0"})
76+
series3, _ := generateSeries("foo", series3Timestamp, prompb.Label{Name: "__replica__", Value: "replica0"}, prompb.Label{Name: "cluster", Value: "cluster1"})
77+
series4, _ := generateSeries("foo", series4Timestamp, prompb.Label{Name: "__replica__", Value: "replica1"}, prompb.Label{Name: "cluster", Value: "cluster1"})
78+
series5, _ := generateSeries("foo", series5Timestamp, prompb.Label{Name: "__replica__", Value: "replicaNoCluster"})
79+
series6, _ := generateSeries("foo", series6Timestamp, prompb.Label{Name: "cluster", Value: "clusterNoReplica"})
80+
series7, _ := generateSeries("foo", series7Timestamp, prompb.Label{Name: "other", Value: "label"})
81+
82+
res, err := c.Push([]prompb.TimeSeries{series1[0], series2[0], series3[0], series4[0], series5[0], series6[0], series7[0]})
83+
require.NoError(t, err)
84+
require.Equal(t, 200, res.StatusCode)
85+
86+
// Wait until the samples have been deduped.
87+
require.NoError(t, distributor.WaitSumMetrics(e2e.Equals(2), "cortex_distributor_deduped_samples_total"))
88+
require.NoError(t, distributor.WaitSumMetrics(e2e.Equals(3), "cortex_distributor_non_ha_samples_received_total"))
89+
90+
// Start the querier and store-gateway, and configure them to frequently sync blocks fast enough to trigger consistency check.
91+
storeGateway := e2ecortex.NewStoreGateway("store-gateway", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "")
92+
querier := e2ecortex.NewQuerier("querier", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), mergeFlags(querierFlags, flags), "")
93+
require.NoError(t, s.StartAndWaitReady(querier, storeGateway))
94+
95+
// Wait until the querier and store-gateway have updated the ring, and wait until the blocks are old enough for consistency check
96+
require.NoError(t, querier.WaitSumMetrics(e2e.Equals(512*2), "cortex_ring_tokens_total"))
97+
require.NoError(t, storeGateway.WaitSumMetrics(e2e.Equals(512), "cortex_ring_tokens_total"))
98+
99+
// Query back the series.
100+
c, err = e2ecortex.NewClient("", querier.HTTPEndpoint(), "", "", "user-1")
101+
require.NoError(t, err)
102+
103+
// Query back the series (only in the ingesters).
104+
result, err := c.Query("foo[5m]", series1Timestamp)
105+
require.NoError(t, err)
106+
107+
require.Equal(t, model.ValMatrix, result.Type())
108+
m := result.(model.Matrix)
109+
require.Equal(t, 5, m.Len())
110+
numValidHA := 0
111+
numNonHA := 0
112+
for _, ss := range m {
113+
replicaLabel, okReplica := ss.Metric["__replica__"]
114+
if okReplica {
115+
require.Equal(t, string(replicaLabel), "replicaNoCluster")
116+
}
117+
clusterLabel, okCluster := ss.Metric["cluster"]
118+
if okCluster {
119+
require.Equal(t, string(clusterLabel) == "cluster1" || string(clusterLabel) == "cluster0" || string(clusterLabel) == "clusterNoReplica", true)
120+
if clusterLabel == "cluster1" || clusterLabel == "cluster0" {
121+
numValidHA++
122+
}
123+
}
124+
if (okReplica && !okCluster && replicaLabel == "replicaNoCluster") || (okCluster && !okReplica && clusterLabel == "clusterNoReplica") || (!okCluster && !okReplica) {
125+
numNonHA++
126+
}
127+
require.NotEmpty(t, ss.Values)
128+
for _, v := range ss.Values {
129+
require.NotEmpty(t, v)
130+
}
131+
}
132+
require.Equal(t, numNonHA, 3)
133+
require.Equal(t, numValidHA, 2)
134+
135+
// Ensure no service-specific metrics prefix is used by the wrong service.
136+
assertServiceMetricsPrefixes(t, Distributor, distributor)
137+
assertServiceMetricsPrefixes(t, Ingester, ingester)
138+
assertServiceMetricsPrefixes(t, StoreGateway, storeGateway)
139+
assertServiceMetricsPrefixes(t, Querier, querier)
140+
})
141+
}

pkg/distributor/distributor.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co
652652
// Cache user limit with overrides so we spend less CPU doing locking. See issue #4904
653653
limits := d.limits.GetOverridesForUser(userID)
654654

655-
if limits.AcceptHASamples && len(req.Timeseries) > 0 {
655+
if limits.AcceptHASamples && len(req.Timeseries) > 0 && !limits.AcceptMixedHASamples {
656656
cluster, replica := findHALabels(limits.HAReplicaLabel, limits.HAClusterLabel, req.Timeseries[0].Labels)
657657
removeReplica, err = d.checkSample(ctx, userID, cluster, replica, limits)
658658
if err != nil {
@@ -859,6 +859,22 @@ func (d *Distributor) prepareSeriesKeys(ctx context.Context, req *cortexpb.Write
859859
// check each sample and discard if outside limits.
860860
skipLabelNameValidation := d.cfg.SkipLabelNameValidation || req.GetSkipLabelNameValidation()
861861
for _, ts := range req.Timeseries {
862+
if limits.AcceptHASamples && limits.AcceptMixedHASamples {
863+
cluster, replica := findHALabels(limits.HAReplicaLabel, limits.HAClusterLabel, ts.Labels)
864+
if cluster != "" && replica != "" {
865+
_, err := d.checkSample(ctx, userID, cluster, replica, limits)
866+
if err != nil {
867+
// discard sample
868+
d.dedupedSamples.WithLabelValues(userID, cluster).Add(float64(len(ts.Samples) + len(ts.Histograms)))
869+
continue
870+
}
871+
removeReplica = true // valid HA sample
872+
} else {
873+
removeReplica = false // non HA sample
874+
d.nonHASamples.WithLabelValues(userID).Add(float64(len(ts.Samples) + len(ts.Histograms)))
875+
}
876+
}
877+
862878
// Use timestamp of latest sample in the series. If samples for series are not ordered, metric for user may be wrong.
863879
if len(ts.Samples) > 0 {
864880
latestSampleTimestampMs = max(latestSampleTimestampMs, ts.Samples[len(ts.Samples)-1].TimestampMs)

0 commit comments

Comments
 (0)