diff --git a/CHANGELOG.md b/CHANGELOG.md index a86b2cd9cc..c8c3f95425 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,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] OTLP: Add `-distributor.otlp-max-recv-msg-size` flag to limit OTLP request size in bytes. #6333 * [ENHANCEMENT] S3 Bucket Client: Add a list objects version configs to configure list api object version. #6280 * [ENHANCEMENT] OpenStack Swift: Add application credential configs for Openstack swift object storage backend. #6255 diff --git a/docs/configuration/config-file-reference.md b/docs/configuration/config-file-reference.md index 211ad25711..deb79957b7 100644 --- a/docs/configuration/config-file-reference.md +++ b/docs/configuration/config-file-reference.md @@ -3250,6 +3250,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: | default = false] +# [Experimental] 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: -experimental.distributor.ha-tracker.mixed-ha-samples +[accept_mixed_ha_samples: | default = false] + # Prometheus label to look for in samples to identify a Prometheus HA cluster. # CLI flag: -distributor.ha-tracker.cluster [ha_cluster_label: | default = "cluster"] diff --git a/docs/configuration/v1-guarantees.md b/docs/configuration/v1-guarantees.md index c162754780..94da14fb10 100644 --- a/docs/configuration/v1-guarantees.md +++ b/docs/configuration/v1-guarantees.md @@ -54,7 +54,9 @@ Currently experimental features are: - Metric relabeling in the distributor. - Scalable query-frontend (when using query-scheduler) - Ingester: do not unregister from ring on shutdown (`-ingester.unregister-on-shutdown=false`) -- Distributor: do not extend writes on unhealthy ingesters (`-distributor.extend-writes=false`) +- Distributor: + - Do not extend writes on unhealthy ingesters (`-distributor.extend-writes=false`) + - Accept multiple HA pairs in the same request (enabled via `-experimental.distributor.ha-tracker.mixed-ha-samples=true`) - Tenant Deletion in Purger, for blocks storage. - Query-frontend: query stats tracking (`-frontend.query-stats-enabled`) - Blocks storage bucket index diff --git a/integration/distributor_mixed_ha_samples_test.go b/integration/distributor_mixed_ha_samples_test.go new file mode 100644 index 0000000000..b58a0599b2 --- /dev/null +++ b/integration/distributor_mixed_ha_samples_test.go @@ -0,0 +1,140 @@ +//go:build requires_docker +// +build requires_docker + +package integration + +import ( + "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 TestDistributorAcceptMixedHASamplesRunningInMicroservicesMode(t *testing.T) { + const blockRangePeriod = 5 * time.Minute + + t.Run("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", + "-experimental.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) + }) +} diff --git a/pkg/distributor/distributor.go b/pkg/distributor/distributor.go index 4725f6dff1..844d8d1793 100644 --- a/pkg/distributor/distributor.go +++ b/pkg/distributor/distributor.go @@ -175,6 +175,11 @@ type InstanceLimits struct { MaxInflightPushRequests int `yaml:"max_inflight_push_requests"` } +type HAPair struct { + Cluster string + Replica string +} + type OTLPConfig struct { ConvertAllAttributes bool `yaml:"convert_all_attributes"` DisableTargetInfo bool `yaml:"disable_target_info"` @@ -665,7 +670,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 { @@ -868,10 +873,39 @@ func (d *Distributor) prepareSeriesKeys(ctx context.Context, req *cortexpb.Write } }() + var haPairs map[HAPair]error + if limits.AcceptHASamples && limits.AcceptMixedHASamples && len(req.Timeseries) > 0 { + haPairs = d.computeHAPairs(req.Timeseries, ctx, userID, limits) + } + // For each timeseries, compute a hash to distribute across ingesters; // check each sample and discard if outside limits. skipLabelNameValidation := d.cfg.SkipLabelNameValidation || req.GetSkipLabelNameValidation() for _, ts := range req.Timeseries { + if len(haPairs) > 0 { + cluster, replica := findHALabels(limits.HAReplicaLabel, limits.HAClusterLabel, ts.Labels) + + if cluster != "" && replica != "" { + err := haPairs[HAPair{Cluster: cluster, Replica: replica}] + if err != nil { + // discard sample (non valid HA sample) + if errors.Is(err, ha.ReplicasNotMatchError{}) { + // These samples have been deduped. + d.dedupedSamples.WithLabelValues(userID, cluster).Add(float64(len(ts.Samples) + len(ts.Histograms))) + } + if errors.Is(err, ha.TooManyReplicaGroupsError{}) { + d.validateMetrics.DiscardedSamples.WithLabelValues(validation.TooManyHAClusters, userID).Add(float64(len(ts.Samples) + len(ts.Histograms))) + } + + continue + } + removeReplica = true // valid HA sample + } else { + // non HA sample + removeReplica = false + 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) @@ -1480,6 +1514,30 @@ func findHALabels(replicaLabel, clusterLabel string, labels []cortexpb.LabelAdap return cluster, replica } +func (d *Distributor) computeHAPairs(timeseries []cortexpb.PreallocTimeseries, ctx context.Context, userID string, limits *validation.Limits) map[HAPair]error { + var haPairs = make(map[HAPair]error) + clusterLabel := limits.HAClusterLabel + replicaLabel := limits.HAReplicaLabel + + for _, ts := range timeseries { + cluster, replica := findHALabels(replicaLabel, clusterLabel, ts.Labels) + if cluster != "" && replica != "" { + haPair := HAPair{Cluster: cluster, Replica: replica} + _, ok := haPairs[haPair] + if !ok { + removeReplica, err := d.checkSample(ctx, userID, cluster, replica, limits) + if err == nil && !removeReplica { + continue + } + + haPairs[haPair] = err + } + } + } + + return haPairs +} + func getLimitFromLabelHints(hints *storage.LabelHints) int { if hints != nil { return hints.Limit diff --git a/pkg/distributor/distributor_test.go b/pkg/distributor/distributor_test.go index dce3d05c91..3681d13b21 100644 --- a/pkg/distributor/distributor_test.go +++ b/pkg/distributor/distributor_test.go @@ -1029,6 +1029,102 @@ func TestDistributor_PushHAInstances(t *testing.T) { } } +func TestDistributor_PushMixedHAInstances(t *testing.T) { + t.Parallel() + ctx := user.InjectOrgID(context.Background(), "user") + + for i, tc := range []struct { + enableTracker bool + acceptMixedHASamples bool + samples int + expectedResponse *cortexpb.WriteResponse + expectedCode int32 + }{ + { + enableTracker: true, + acceptMixedHASamples: true, + samples: 5, + expectedResponse: emptyResponse, + expectedCode: 202, + }, + } { + for _, shardByAllLabels := range []bool{true} { + tc := tc + shardByAllLabels := shardByAllLabels + for _, enableHistogram := range []bool{false} { + enableHistogram := enableHistogram + t.Run(fmt.Sprintf("[%d](shardByAllLabels=%v, histogram=%v)", i, shardByAllLabels, enableHistogram), func(t *testing.T) { + t.Parallel() + var limits validation.Limits + flagext.DefaultValues(&limits) + limits.AcceptHASamples = true + limits.AcceptMixedHASamples = tc.acceptMixedHASamples + limits.MaxLabelValueLength = 25 + + ds, ingesters, _, _ := prepare(t, prepConfig{ + numIngesters: 2, + happyIngesters: 2, + numDistributors: 1, + replicationFactor: 2, + shardByAllLabels: shardByAllLabels, + limits: &limits, + enableTracker: tc.enableTracker, + }) + + d := ds[0] + + request := makeWriteRequestHAMixedSamples(tc.samples, enableHistogram) + response, _ := d.Push(ctx, request) + assert.Equal(t, tc.expectedResponse, response) + + for i := range ingesters { + timeseries := ingesters[i].series() + assert.Equal(t, 5, len(timeseries)) + clusters := make(map[string]int) + replicas := make(map[string]int) + for _, v := range timeseries { + replicaLabel := "" + clusterLabel := "" + for _, label := range v.Labels { + if label.Name == "__replica__" { + replicaLabel = label.Value + _, ok := replicas[label.Value] + if !ok { + replicas[label.Value] = 1 + } else { + assert.Fail(t, fmt.Sprintf("Two timeseries with same replica label, %s, were found, but only one should be present", label.Value)) + } + } + if label.Name == "cluster" { + clusterLabel = label.Value + _, ok := clusters[label.Value] + if !ok { + clusters[label.Value] = 1 + } else { + assert.Fail(t, fmt.Sprintf("Two timeseries with same cluster label, %s, were found, but only one should be present", label.Value)) + } + } + } + if clusterLabel == "" && replicaLabel != "" { + assert.Equal(t, "replicaNoCluster", replicaLabel) + } + assert.Equal(t, tc.samples, len(v.Samples)) + } + assert.Equal(t, 3, len(clusters)) + for _, nr := range clusters { + assert.Equal(t, true, nr == 1) + } + assert.Equal(t, 1, len(replicas)) + for _, nr := range clusters { + assert.Equal(t, true, nr == 1) + } + } + }) + } + } + } +} + func TestDistributor_PushQuery(t *testing.T) { t.Parallel() const shuffleShardSize = 5 @@ -2829,7 +2925,7 @@ func prepare(tb testing.TB, cfg prepConfig) ([]*Distributor, []*mockIngester, [] EnableHATracker: true, KVStore: kv.Config{Mock: mock}, UpdateTimeout: 100 * time.Millisecond, - FailoverTimeout: time.Second, + FailoverTimeout: time.Hour, } cfg.limits.HAMaxClusters = 100 } @@ -2948,6 +3044,106 @@ func makeWriteRequestHA(samples int, replica, cluster string, histogram bool) *c return request } +func makeWriteRequestHAMixedSamples(samples int, histogram bool) *cortexpb.WriteRequest { + request := &cortexpb.WriteRequest{} + + for _, haPair := range []struct { + cluster string + replica string + }{ + { + cluster: "cluster0", + replica: "replica0", + }, + { + cluster: "cluster0", + replica: "replica1", + }, + { + cluster: "cluster1", + replica: "replica0", + }, + { + cluster: "cluster1", + replica: "replica1", + }, + { + cluster: "", + replica: "replicaNoCluster", + }, + { + cluster: "clusterNoReplica", + replica: "", + }, + { + cluster: "", + replica: "", + }, + } { + cluster := haPair.cluster + replica := haPair.replica + var ts cortexpb.PreallocTimeseries + if cluster == "" && replica == "" { + ts = cortexpb.PreallocTimeseries{ + TimeSeries: &cortexpb.TimeSeries{ + Labels: []cortexpb.LabelAdapter{ + {Name: "__name__", Value: "foo"}, + {Name: "bar", Value: "baz"}, + }, + }, + } + } else if cluster == "" && replica != "" { + ts = cortexpb.PreallocTimeseries{ + TimeSeries: &cortexpb.TimeSeries{ + Labels: []cortexpb.LabelAdapter{ + {Name: "__name__", Value: "foo"}, + {Name: "__replica__", Value: replica}, + {Name: "bar", Value: "baz"}, + }, + }, + } + } else if cluster != "" && replica == "" { + ts = cortexpb.PreallocTimeseries{ + TimeSeries: &cortexpb.TimeSeries{ + Labels: []cortexpb.LabelAdapter{ + {Name: "__name__", Value: "foo"}, + {Name: "bar", Value: "baz"}, + {Name: "cluster", Value: cluster}, + }, + }, + } + } else { + ts = cortexpb.PreallocTimeseries{ + TimeSeries: &cortexpb.TimeSeries{ + Labels: []cortexpb.LabelAdapter{ + {Name: "__name__", Value: "foo"}, + {Name: "__replica__", Value: replica}, + {Name: "bar", Value: "baz"}, + {Name: "cluster", Value: cluster}, + }, + }, + } + } + if histogram { + ts.Histograms = []cortexpb.Histogram{ + cortexpb.HistogramToHistogramProto(int64(samples), tsdbutil.GenerateTestHistogram(samples)), + } + } else { + var s = make([]cortexpb.Sample, 0) + for i := 0; i < samples; i++ { + sample := cortexpb.Sample{ + Value: float64(i), + TimestampMs: int64(i), + } + s = append(s, sample) + } + ts.Samples = s + } + request.Timeseries = append(request.Timeseries, ts) + } + return request +} + func makeWriteRequestExemplar(seriesLabels []string, timestamp int64, exemplarLabels []string) *cortexpb.WriteRequest { return &cortexpb.WriteRequest{ Timeseries: []cortexpb.PreallocTimeseries{ diff --git a/pkg/util/validation/limits.go b/pkg/util/validation/limits.go index d63d02908f..11bdf9c461 100644 --- a/pkg/util/validation/limits.go +++ b/pkg/util/validation/limits.go @@ -119,6 +119,7 @@ type Limits struct { IngestionRateStrategy string `yaml:"ingestion_rate_strategy" json:"ingestion_rate_strategy"` IngestionBurstSize int `yaml:"ingestion_burst_size" json:"ingestion_burst_size"` AcceptHASamples bool `yaml:"accept_ha_samples" json:"accept_ha_samples"` + AcceptMixedHASamples bool `yaml:"accept_mixed_ha_samples" json:"accept_mixed_ha_samples"` HAClusterLabel string `yaml:"ha_cluster_label" json:"ha_cluster_label"` HAReplicaLabel string `yaml:"ha_replica_label" json:"ha_replica_label"` HAMaxClusters int `yaml:"ha_max_clusters" json:"ha_max_clusters"` @@ -221,6 +222,7 @@ func (l *Limits) RegisterFlags(f *flag.FlagSet) { f.StringVar(&l.IngestionRateStrategy, "distributor.ingestion-rate-limit-strategy", "local", "Whether the ingestion rate limit should be applied individually to each distributor instance (local), or evenly shared across the cluster (global).") f.IntVar(&l.IngestionBurstSize, "distributor.ingestion-burst-size", 50000, "Per-user allowed ingestion burst size (in number of samples).") f.BoolVar(&l.AcceptHASamples, "distributor.ha-tracker.enable-for-all-users", false, "Flag to enable, for all users, handling of samples with external labels identifying replicas in an HA Prometheus setup.") + f.BoolVar(&l.AcceptMixedHASamples, "experimental.distributor.ha-tracker.mixed-ha-samples", false, "[Experimental] 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.") f.StringVar(&l.HAClusterLabel, "distributor.ha-tracker.cluster", "cluster", "Prometheus label to look for in samples to identify a Prometheus HA cluster.") f.StringVar(&l.HAReplicaLabel, "distributor.ha-tracker.replica", "__replica__", "Prometheus label to look for in samples to identify a Prometheus HA replica.") f.IntVar(&l.HAMaxClusters, "distributor.ha-tracker.max-clusters", 0, "Maximum number of clusters that HA tracker will keep track of for single user. 0 to disable the limit.") @@ -549,6 +551,11 @@ func (o *Overrides) AcceptHASamples(userID string) bool { return o.GetOverridesForUser(userID).AcceptHASamples } +// AcceptMixedHASamples returns whether the distributor should track and accept samples from mixed HA replicas for this user. +func (o *Overrides) AcceptMixedHASamples(userID string) bool { + return o.GetOverridesForUser(userID).AcceptMixedHASamples +} + // HAClusterLabel returns the cluster label to look for when deciding whether to accept a sample from a Prometheus HA replica. func (o *Overrides) HAClusterLabel(userID string) string { return o.GetOverridesForUser(userID).HAClusterLabel