From 38da6f822dc8319fdcd8f428c39764019dd50551 Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Thu, 28 Jul 2022 15:55:10 +0500 Subject: [PATCH] fix(test): Fixed cluster-specific test TestRemoveMetricsByPrefix. In case of Redis cluster, keys are distributed by nodes, so need to count keys from all masters. Added keys counting for all master nodes of cluster. --- database/redis/metric_test.go | 125 ++++++++++++++++++++++++++++------ 1 file changed, 106 insertions(+), 19 deletions(-) diff --git a/database/redis/metric_test.go b/database/redis/metric_test.go index fa7d06738..878f5735f 100644 --- a/database/redis/metric_test.go +++ b/database/redis/metric_test.go @@ -871,7 +871,7 @@ func TestRemoveMetricsByPrefix(t *testing.T) { client := *dataBase.client const pattern = "my.test.*.metric*" - Convey("Given metrics with pattern my.test.*", t, func() { + Convey("Given metrics with pattern my.test.*", t, func(c C) { for i := 1; i <= 10; i++ { err := dataBase.SaveMetrics( map[string]*moira.MatchedMetric{ @@ -900,29 +900,116 @@ func TestRemoveMetricsByPrefix(t *testing.T) { So(err, ShouldBeNil) } - result := client.Keys(dataBase.context, "moira-metric-data:my.test*").Val() - So(len(result), ShouldResemble, 20) - result = client.Keys(dataBase.context, "moira-metric-retention:my.test*").Val() - So(len(result), ShouldResemble, 20) + var metricsCount int32 + incrementMetric := func(ctx context.Context, shard redis.UniversalClient) error { + result := len(shard.Keys(dataBase.context, "moira-metric-data:my.test*").Val()) + atomic.AddInt32(&metricsCount, int32(result)) + return nil + } + switch cl := client.(type) { + case *redis.ClusterClient: + err := cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error { + err := incrementMetric(dataBase.context, shard) + c.So(err, ShouldBeNil) + return nil + }) + So(err, ShouldBeNil) + default: + err := incrementMetric(dataBase.context, cl) + So(err, ShouldBeNil) + } + So(atomic.LoadInt32(&metricsCount), ShouldEqual, 20) + + var retentionsCount int32 + incrementRetention := func(ctx context.Context, shard redis.UniversalClient) error { + result := len(shard.Keys(dataBase.context, "moira-metric-retention:my.test*").Val()) + atomic.AddInt32(&retentionsCount, int32(result)) + return nil + } + switch cl := client.(type) { + case *redis.ClusterClient: + err := cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error { + err := incrementRetention(dataBase.context, shard) + c.So(err, ShouldBeNil) + return nil + }) + So(err, ShouldBeNil) + default: + err := incrementRetention(dataBase.context, cl) + So(err, ShouldBeNil) + } + So(atomic.LoadInt32(&retentionsCount), ShouldEqual, 20) patternMetricsCount := client.SCard(dataBase.context, "moira-pattern-metrics:my.test.*.metric*").Val() - So(patternMetricsCount, ShouldResemble, int64(20)) + So(patternMetricsCount, ShouldEqual, int64(20)) Convey("When remove metrics by prefix my.test.super. was called", func() { err := dataBase.RemoveMetricsByPrefix("my.test.super.") So(err, ShouldBeNil) - Convey("No metric data for metrics with this prefix should not exist", func() { - result = client.Keys(dataBase.context, "moira-metric-data:my.test*").Val() - So(len(result), ShouldResemble, 10) - result = client.Keys(dataBase.context, "moira-metric-retention:my.test*").Val() - So(len(result), ShouldResemble, 10) - result = client.Keys(dataBase.context, "moira-metric-data:my.test.mega.*").Val() - So(len(result), ShouldResemble, 10) - result = client.Keys(dataBase.context, "moira-metric-retention:my.test.mega.*").Val() - So(len(result), ShouldResemble, 10) + Convey("No metric data for metrics with this prefix should exist", func() { + var allMetricsCount int + switch cl := client.(type) { + case *redis.ClusterClient: + err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error { + result := shard.Keys(dataBase.context, "moira-metric-data:my.test*").Val() + allMetricsCount += len(result) + return nil + }) + So(err, ShouldBeNil) + default: + result := client.Keys(dataBase.context, "moira-metric-data:my.test*").Val() + allMetricsCount += len(result) + } + So(allMetricsCount, ShouldEqual, 10) + + var allRetentionsCount int + switch cl := client.(type) { + case *redis.ClusterClient: + err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error { + result := shard.Keys(dataBase.context, "moira-metric-retention:my.test*").Val() + allRetentionsCount += len(result) + return nil + }) + So(err, ShouldBeNil) + default: + result := client.Keys(dataBase.context, "moira-metric-retention:my.test*").Val() + allRetentionsCount += len(result) + } + So(allRetentionsCount, ShouldEqual, 10) + + var megaMetricsCount int + switch cl := client.(type) { + case *redis.ClusterClient: + err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error { + result := shard.Keys(dataBase.context, "moira-metric-data:my.test.mega.*").Val() + megaMetricsCount += len(result) + return nil + }) + So(err, ShouldBeNil) + default: + result := client.Keys(dataBase.context, "moira-metric-data:my.test.mega.*").Val() + megaMetricsCount += len(result) + } + So(megaMetricsCount, ShouldEqual, 10) + + var megaRetentionsCount int + switch cl := client.(type) { + case *redis.ClusterClient: + err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error { + result := shard.Keys(dataBase.context, "moira-metric-retention:my.test.mega.*").Val() + megaRetentionsCount += len(result) + return nil + }) + So(err, ShouldBeNil) + default: + result := client.Keys(dataBase.context, "moira-metric-retention:my.test.mega.*").Val() + megaRetentionsCount += len(result) + } + So(megaRetentionsCount, ShouldEqual, 10) + patternMetricsCount := client.SCard(dataBase.context, "moira-pattern-metrics:my.test.*.metric*").Val() - So(patternMetricsCount, ShouldResemble, int64(10)) + So(patternMetricsCount, ShouldEqual, int64(10)) }) }) }) @@ -1017,7 +1104,7 @@ func TestRemoveAllMetrics(t *testing.T) { switch cl := client.(type) { case *redis.ClusterClient: err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error { - result := client.Keys(dataBase.context, "moira-metric-data:*").Val() + result := shard.Keys(dataBase.context, "moira-metric-data:*").Val() metricsCount += len(result) return nil }) @@ -1032,7 +1119,7 @@ func TestRemoveAllMetrics(t *testing.T) { switch cl := client.(type) { case *redis.ClusterClient: err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error { - result := client.Keys(dataBase.context, "moira-metric-retention:*").Val() + result := shard.Keys(dataBase.context, "moira-metric-retention:*").Val() retentionsCount += len(result) return nil }) @@ -1047,7 +1134,7 @@ func TestRemoveAllMetrics(t *testing.T) { switch cl := client.(type) { case *redis.ClusterClient: err = cl.ForEachMaster(dataBase.context, func(ctx context.Context, shard *redis.Client) error { - result := client.Keys(dataBase.context, "moira-pattern-metrics*").Val() + result := shard.Keys(dataBase.context, "moira-pattern-metrics*").Val() patternsCount += len(result) return nil })