From c3f264f7c117fe56795d2a373c753a3bde7e79ec Mon Sep 17 00:00:00 2001 From: Edd Robinson Date: Wed, 23 Mar 2016 12:46:35 +0000 Subject: [PATCH] Creating CQ with identical query does not error Partially fixes #6094. Prior to this when passing the same query and CQ name in a CREATE CONTINUOUS QUERY command an error would be returned. This means the command was not behaving in a similar way to other commands.a Now when running the command with the same CQ name and query string no error will be returned. Note, this change does not parse the query, it simply compares a normalised query string to the existing one on the CQ. --- CHANGELOG.md | 1 + services/meta/client_test.go | 15 ++++++++++++--- services/meta/data.go | 11 +++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33b3db7bb54..471377deaf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - [#5152](https://github.com/influxdata/influxdb/issues/5152): Fix where filters when a tag and a filter are combined with OR. - [#5728](https://github.com/influxdata/influxdb/issues/5728): Properly handle semi-colons as part of the main query loop. - [#6065](https://github.com/influxdata/influxdb/pull/6065): Wait for a process termination on influxdb restart @simnv +- [#6099](https://github.com/influxdata/influxdb/pull/6099): Ensure CREATE RETENTION POLICY and CREATE CONTINUOUS QUERY are idempotent in the correct way. ## v0.11.0 [unreleased] diff --git a/services/meta/client_test.go b/services/meta/client_test.go index 4b1f5d20bad..bae2fb85c0c 100644 --- a/services/meta/client_test.go +++ b/services/meta/client_test.go @@ -550,9 +550,18 @@ func TestMetaClient_ContinuousQueries(t *testing.T) { t.Fatal(err) } - // Recreate an existing CQ - if err := c.CreateContinuousQuery("db0", "cq0", `SELECT max(value) INTO foo_max FROM foo GROUP BY time(10m)`); err == nil || err.Error() != `continuous query already exists` { - t.Fatalf("unexpected error: %s", err) + // Recreating an existing CQ with the exact same query should not + // return an error. + if err := c.CreateContinuousQuery("db0", "cq0", `SELECT count(value) INTO foo_count FROM foo GROUP BY time(10m)`); err != nil { + t.Fatalf("got error %q, but didn't expect one", err) + } + + // Recreating an existing CQ with a different query should return + // an error. + if err := c.CreateContinuousQuery("db0", "cq0", `SELECT min(value) INTO foo_max FROM foo GROUP BY time(20m)`); err == nil { + t.Fatal("didn't get and error, but expected one") + } else if got, exp := err, meta.ErrContinuousQueryExists; got.Error() != exp.Error() { + t.Fatalf("got %v, expected %v", got, exp) } // Create a few more CQ's diff --git a/services/meta/data.go b/services/meta/data.go index f2b86cc6d02..32eb7a1dcb6 100644 --- a/services/meta/data.go +++ b/services/meta/data.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "sort" + "strings" "sync" "time" @@ -401,8 +402,14 @@ func (data *Data) CreateContinuousQuery(database, name, query string) error { } // Ensure the name doesn't already exist. - for i := range di.ContinuousQueries { - if di.ContinuousQueries[i].Name == name { + for _, cq := range di.ContinuousQueries { + if cq.Name == name { + // If the query string is the same, we'll silently return, + // otherwise we'll assume the user might be trying to + // overwrite an existing CQ with a different query. + if strings.ToLower(cq.Query) == strings.ToLower(query) { + return nil + } return ErrContinuousQueryExists } }