Skip to content

Commit

Permalink
Added new implementation that makes the distributor accept multiple H…
Browse files Browse the repository at this point in the history
…A 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>
  • Loading branch information
eduardscaueru committed Oct 23, 2024
1 parent e070ec6 commit e945eb4
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [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
* [FEATURE] Store Gateway: Add an in-memory chunk cache. #6245
* [FEATURE] Chunk Cache: Support multi level cache and add metrics. #6249
* [FEATURE] Distributor: Accept multiple HA Tracker pairs in the same request. #6256
* [ENHANCEMENT] Ingester: Add `blocks-storage.tsdb.wal-compression-type` to support zstd wal compression type. #6232
* [ENHANCEMENT] Query Frontend: Add info field to query response. #6207
* [ENHANCEMENT] Query Frontend: Add peakSample in query stats response. #6188
Expand Down
6 changes: 6 additions & 0 deletions docs/configuration/config-file-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3163,6 +3163,12 @@ The `limits_config` configures default and per-tenant limits imposed by Cortex s
# CLI flag: -distributor.ha-tracker.enable-for-all-users
[accept_ha_samples: <boolean> | default = false]
# Flag to enable handling of samples with mixed external labels identifying
# replicas in an HA Prometheus setup. Supported only if
# -distributor.ha-tracker.enable-for-all-users is true.
# CLI flag: -distributor.ha-tracker.mixed-ha-samples
[accept_mixed_ha_samples: <boolean> | default = false]
# Prometheus label to look for in samples to identify a Prometheus HA cluster.
# CLI flag: -distributor.ha-tracker.cluster
[ha_cluster_label: <string> | default = "cluster"]
Expand Down
141 changes: 141 additions & 0 deletions integration/distributor_mixed_ha_samples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//go:build requires_docker
// +build requires_docker

package integration

import (
"fmt"
"testing"
"time"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/require"

"github.com/cortexproject/cortex/integration/e2e"
e2edb "github.com/cortexproject/cortex/integration/e2e/db"
"github.com/cortexproject/cortex/integration/e2ecortex"
)

func TestDistriubtorAcceptMixedHASamplesRunningInMicroservicesMode(t *testing.T) {
const blockRangePeriod = 5 * time.Minute

t.Run(fmt.Sprintf("%s", "Distriubtor accept mixed HA samples in the same request"), func(t *testing.T) {
s, err := e2e.NewScenario(networkName)
require.NoError(t, err)
defer s.Close()

// Configure the querier to only look in ingester
// and enbale distributor ha tracker with mixed samples.
distributorFlags := map[string]string{
"-distributor.ha-tracker.enable": "true",
"-distributor.ha-tracker.enable-for-all-users": "true",
"-distributor.ha-tracker.mixed-ha-samples": "true",
"-distributor.ha-tracker.cluster": "cluster",
"-distributor.ha-tracker.replica": "__replica__",
"-distributor.ha-tracker.store": "etcd",
"-distributor.ha-tracker.etcd.endpoints": "etcd:2379",
}
querierFlags := mergeFlags(BlocksStorageFlags(), map[string]string{
"-querier.query-store-after": (1 * time.Hour).String(),
})
flags := mergeFlags(BlocksStorageFlags(), map[string]string{
"-blocks-storage.tsdb.block-ranges-period": blockRangePeriod.String(),
"-blocks-storage.tsdb.ship-interval": "5s",
"-blocks-storage.tsdb.retention-period": ((blockRangePeriod * 2) - 1).String(),
"-blocks-storage.bucket-store.max-chunk-pool-bytes": "1",
})

// Start dependencies.
consul := e2edb.NewConsul()
etcd := e2edb.NewETCD()
minio := e2edb.NewMinio(9000, flags["-blocks-storage.s3.bucket-name"])
require.NoError(t, s.StartAndWaitReady(consul, etcd, minio))

// Start Cortex components.
distributor := e2ecortex.NewDistributor("distributor", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), distributorFlags, "")
ingester := e2ecortex.NewIngester("ingester", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "")
require.NoError(t, s.StartAndWaitReady(distributor, ingester))

// Wait until both the distributor and ingester have updated the ring.
require.NoError(t, distributor.WaitSumMetrics(e2e.Equals(512), "cortex_ring_tokens_total"))

c, err := e2ecortex.NewClient(distributor.HTTPEndpoint(), "", "", "", "user-1")
require.NoError(t, err)

// Push some series to Cortex.
series1Timestamp := time.Now()
series2Timestamp := series1Timestamp.Add(-2 * time.Second)
series3Timestamp := series1Timestamp.Add(-4 * time.Second)
series4Timestamp := series1Timestamp.Add(-6 * time.Second)
series5Timestamp := series1Timestamp.Add(-8 * time.Second)
series6Timestamp := series1Timestamp.Add(-10 * time.Second)
series7Timestamp := series1Timestamp.Add(-12 * time.Second)
series1, _ := generateSeries("foo", series1Timestamp, prompb.Label{Name: "__replica__", Value: "replica0"}, prompb.Label{Name: "cluster", Value: "cluster0"})
series2, _ := generateSeries("foo", series2Timestamp, prompb.Label{Name: "__replica__", Value: "replica1"}, prompb.Label{Name: "cluster", Value: "cluster0"})
series3, _ := generateSeries("foo", series3Timestamp, prompb.Label{Name: "__replica__", Value: "replica0"}, prompb.Label{Name: "cluster", Value: "cluster1"})
series4, _ := generateSeries("foo", series4Timestamp, prompb.Label{Name: "__replica__", Value: "replica1"}, prompb.Label{Name: "cluster", Value: "cluster1"})
series5, _ := generateSeries("foo", series5Timestamp, prompb.Label{Name: "__replica__", Value: "replicaNoCluster"})
series6, _ := generateSeries("foo", series6Timestamp, prompb.Label{Name: "cluster", Value: "clusterNoReplica"})
series7, _ := generateSeries("foo", series7Timestamp, prompb.Label{Name: "other", Value: "label"})

res, err := c.Push([]prompb.TimeSeries{series1[0], series2[0], series3[0], series4[0], series5[0], series6[0], series7[0]})
require.NoError(t, err)
require.Equal(t, 200, res.StatusCode)

// Wait until the samples have been deduped.
require.NoError(t, distributor.WaitSumMetrics(e2e.Equals(2), "cortex_distributor_deduped_samples_total"))
require.NoError(t, distributor.WaitSumMetrics(e2e.Equals(3), "cortex_distributor_non_ha_samples_received_total"))

// Start the querier and store-gateway, and configure them to frequently sync blocks fast enough to trigger consistency check.
storeGateway := e2ecortex.NewStoreGateway("store-gateway", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "")
querier := e2ecortex.NewQuerier("querier", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), mergeFlags(querierFlags, flags), "")
require.NoError(t, s.StartAndWaitReady(querier, storeGateway))

// Wait until the querier and store-gateway have updated the ring, and wait until the blocks are old enough for consistency check
require.NoError(t, querier.WaitSumMetrics(e2e.Equals(512*2), "cortex_ring_tokens_total"))
require.NoError(t, storeGateway.WaitSumMetrics(e2e.Equals(512), "cortex_ring_tokens_total"))

// Query back the series.
c, err = e2ecortex.NewClient("", querier.HTTPEndpoint(), "", "", "user-1")
require.NoError(t, err)

// Query back the series (only in the ingesters).
result, err := c.Query("foo[5m]", series1Timestamp)
require.NoError(t, err)

require.Equal(t, model.ValMatrix, result.Type())
m := result.(model.Matrix)
require.Equal(t, 5, m.Len())
numValidHA := 0
numNonHA := 0
for _, ss := range m {
replicaLabel, okReplica := ss.Metric["__replica__"]
if okReplica {
require.Equal(t, string(replicaLabel), "replicaNoCluster")
}
clusterLabel, okCluster := ss.Metric["cluster"]
if okCluster {
require.Equal(t, string(clusterLabel) == "cluster1" || string(clusterLabel) == "cluster0" || string(clusterLabel) == "clusterNoReplica", true)
if clusterLabel == "cluster1" || clusterLabel == "cluster0" {
numValidHA++
}
}
if (okReplica && !okCluster && replicaLabel == "replicaNoCluster") || (okCluster && !okReplica && clusterLabel == "clusterNoReplica") || (!okCluster && !okReplica) {
numNonHA++
}
require.NotEmpty(t, ss.Values)
for _, v := range ss.Values {
require.NotEmpty(t, v)
}
}
require.Equal(t, numNonHA, 3)
require.Equal(t, numValidHA, 2)

// Ensure no service-specific metrics prefix is used by the wrong service.
assertServiceMetricsPrefixes(t, Distributor, distributor)
assertServiceMetricsPrefixes(t, Ingester, ingester)
assertServiceMetricsPrefixes(t, StoreGateway, storeGateway)
assertServiceMetricsPrefixes(t, Querier, querier)
})
}
18 changes: 17 additions & 1 deletion pkg/distributor/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co
// Cache user limit with overrides so we spend less CPU doing locking. See issue #4904
limits := d.limits.GetOverridesForUser(userID)

if limits.AcceptHASamples && len(req.Timeseries) > 0 {
if limits.AcceptHASamples && len(req.Timeseries) > 0 && !limits.AcceptMixedHASamples {
cluster, replica := findHALabels(limits.HAReplicaLabel, limits.HAClusterLabel, req.Timeseries[0].Labels)
removeReplica, err = d.checkSample(ctx, userID, cluster, replica, limits)
if err != nil {
Expand Down Expand Up @@ -859,6 +859,22 @@ func (d *Distributor) prepareSeriesKeys(ctx context.Context, req *cortexpb.Write
// check each sample and discard if outside limits.
skipLabelNameValidation := d.cfg.SkipLabelNameValidation || req.GetSkipLabelNameValidation()
for _, ts := range req.Timeseries {
if limits.AcceptHASamples && limits.AcceptMixedHASamples {
cluster, replica := findHALabels(limits.HAReplicaLabel, limits.HAClusterLabel, ts.Labels)
if cluster != "" && replica != "" {
_, err := d.checkSample(ctx, userID, cluster, replica, limits)
if err != nil {
// discard sample
d.dedupedSamples.WithLabelValues(userID, cluster).Add(float64(len(ts.Samples) + len(ts.Histograms)))
continue
}
removeReplica = true // valid HA sample
} else {
removeReplica = false // non HA sample
d.nonHASamples.WithLabelValues(userID).Add(float64(len(ts.Samples) + len(ts.Histograms)))
}
}

// Use timestamp of latest sample in the series. If samples for series are not ordered, metric for user may be wrong.
if len(ts.Samples) > 0 {
latestSampleTimestampMs = max(latestSampleTimestampMs, ts.Samples[len(ts.Samples)-1].TimestampMs)
Expand Down
Loading

0 comments on commit e945eb4

Please sign in to comment.