Skip to content

Commit

Permalink
Creating CQ with identical query does not error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
e-dard committed Mar 23, 2016
1 parent 06bbd6e commit c3f264f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
15 changes: 12 additions & 3 deletions services/meta/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions services/meta/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit c3f264f

Please sign in to comment.