From 205b906076ed1428f6f9daf0544cc79bb2ea8e4f Mon Sep 17 00:00:00 2001 From: Stanislav Kozlov Date: Tue, 19 Mar 2019 17:10:43 +1100 Subject: [PATCH 1/5] Remove timestamp from Prometheus push gateway --- metrics_prometheus.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/metrics_prometheus.go b/metrics_prometheus.go index 841ff1d9..472a2597 100644 --- a/metrics_prometheus.go +++ b/metrics_prometheus.go @@ -13,13 +13,14 @@ import ( "bufio" "bytes" "fmt" - log "github.com/Sirupsen/logrus" "hash/fnv" "net/http" "net/url" "os" "regexp" "strings" + + log "github.com/Sirupsen/logrus" ) // @@ -171,8 +172,7 @@ func (p *metricsPrometheusOutputHandler) buildMetric( } else { delim = " " } - - buf.WriteString(fmt.Sprintf("%s%v %v\n", delim, sensor.val, ts)) + buf.WriteString(fmt.Sprintf("%s%v\n", delim, sensor.val)) } func (p *metricsPrometheusOutputHandler) worker(m *metricsOutputModule) { From a13299538cdf0931c8b532485bcf195b20b99b42 Mon Sep 17 00:00:00 2001 From: Stan Kozlov Date: Wed, 23 Dec 2020 13:59:26 +1100 Subject: [PATCH 2/5] Remove auth for influx --- metrics_influx.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/metrics_influx.go b/metrics_influx.go index 5edf3f5c..04b6a5b6 100644 --- a/metrics_influx.go +++ b/metrics_influx.go @@ -25,11 +25,12 @@ package main import ( "bufio" "fmt" - log "github.com/Sirupsen/logrus" - "github.com/influxdata/influxdb/client/v2" "os" "runtime" "time" + + log "github.com/Sirupsen/logrus" + "github.com/influxdata/influxdb/client/v2" ) const ( @@ -127,11 +128,11 @@ func (w *metricsInfluxOutputWorker) worker(m *metricsOutputModule) { // // Add tls config here. if !outputHandler.standalone { - var user, passw string - user, passw, err = outputHandler.auth.getUP() + // var user, passw string + // user, passw, err = outputHandler.auth.getUP() if err == nil { influxClient, err = client.NewHTTPClient(client.HTTPConfig{ - Addr: w.influxServer, Username: user, Password: passw, + Addr: w.influxServer, }) } } From c37de9b8940562010496caf634e2973f722cb93d Mon Sep 17 00:00:00 2001 From: Stan Kozlov Date: Wed, 23 Dec 2020 14:12:48 +1100 Subject: [PATCH 3/5] remove promt --- crypt.go | 62 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/crypt.go b/crypt.go index 9c5e2d32..0c387f32 100644 --- a/crypt.go +++ b/crypt.go @@ -11,7 +11,6 @@ package main import ( - "bufio" "crypto/rand" "crypto/rsa" "crypto/sha256" @@ -19,10 +18,9 @@ import ( "encoding/base64" "encoding/pem" "fmt" - log "github.com/Sirupsen/logrus" - "golang.org/x/crypto/ssh/terminal" "io/ioutil" - "os" + + log "github.com/Sirupsen/logrus" ) func collect_pkey(pemFile string) (err error, key *rsa.PrivateKey) { @@ -96,34 +94,36 @@ type cryptUserPasswordCollector struct { func cryptCollect(name string, authenticator string) ( error, string, string) { - b := bufio.NewReader(os.Stdin) - for { - fmt.Printf("\nCRYPT Client [%s],[%v]\n Enter username: ", - name, authenticator) - user, more, err := b.ReadLine() - if more { - fmt.Printf("Username too long") - continue - } - if err != nil { - fmt.Printf("Failed to collect username") - continue - } - if string(user) == "" { - fmt.Println("Empty username, try again") - continue - } - fmt.Printf(" Enter password: ") - pw, err := terminal.ReadPassword(int(os.Stdin.Fd())) - if err != nil { - fmt.Printf("Failed to collect password") - continue - } + // b := bufio.NewReader(os.Stdin) + // for { + // fmt.Printf("\nCRYPT Client [%s],[%v]\n Enter username: ", + // name, authenticator) + // user, more, err := b.ReadLine() + // if more { + // fmt.Printf("Username too long") + // continue + // } + // if err != nil { + // fmt.Printf("Failed to collect username") + // continue + // } + // if string(user) == "" { + // fmt.Println("Empty username, try again") + // continue + // } + // fmt.Printf(" Enter password: ") + // pw, err := terminal.ReadPassword(int(os.Stdin.Fd())) + // if err != nil { + // fmt.Printf("Failed to collect password") + // continue + // } + + // fmt.Printf("\n") + + // return nil, string(user), string(pw) + // } + return nil, string("admin"), string("admin") - fmt.Printf("\n") - - return nil, string(user), string(pw) - } } func (c *cryptUserPasswordCollector) getUP() (string, string, error) { From ad659437e0aabf56292de3d3525c84c24424b9dc Mon Sep 17 00:00:00 2001 From: Stan Kozlov Date: Wed, 23 Dec 2020 15:26:51 +1100 Subject: [PATCH 4/5] replace lib with latest --- codec.go | 2 +- codec_gpb.go | 2 +- crypt.go | 2 +- glide.lock | 2 +- glide.yaml | 2 +- mdt_msg_samples/samples.go | 2 +- message_router.go | 2 +- message_router_test.go | 2 +- metamonitoring.go | 2 +- metrics.go | 2 +- metrics_influx.go | 4 ++-- metrics_prometheus.go | 2 +- pipeline.go | 2 +- pipeline_test.go | 2 +- replay.go | 2 +- tap.go | 2 +- vendor/github.com/Sirupsen/logrus/README.md | 18 +++++++++--------- .../Sirupsen/logrus/alt_exit_test.go | 2 +- vendor/github.com/Sirupsen/logrus/doc.go | 4 ++-- .../Sirupsen/logrus/examples/basic/basic.go | 2 +- .../Sirupsen/logrus/examples/hook/hook.go | 2 +- .../Sirupsen/logrus/hooks/syslog/README.md | 8 ++++---- .../Sirupsen/logrus/hooks/syslog/syslog.go | 2 +- .../logrus/hooks/syslog/syslog_test.go | 2 +- .../Sirupsen/logrus/hooks/test/test.go | 2 +- .../Sirupsen/logrus/hooks/test/test_test.go | 2 +- .../Sirupsen/logrus/json_formatter.go | 2 +- .../evalphobia/logrus_fluent/README.md | 2 +- .../evalphobia/logrus_fluent/fluent.go | 2 +- .../evalphobia/logrus_fluent/fluent_test.go | 2 +- .../influxdata/influxdb/client/README.md | 8 ++++---- .../influxdata/influxdb/client/v2/client.go | 5 +++-- .../influxdb/client/v2/example_test.go | 2 +- .../influxdb/services/subscriber/http.go | 2 +- .../influxdata/influxdb/stress/basic.go | 2 +- .../influxdata/influxdb/stress/stress_test.go | 2 +- .../influxdata/influxdb/stress/v2/main.go | 2 +- .../influxdb/stress/v2/statement/report.go | 2 +- .../stress/v2/stress_client/reporting.go | 2 +- .../stress/v2/stress_client/response.go | 2 +- .../stress/v2/stress_client/stressTest.go | 2 +- .../stress/v2/stress_client/stressTest_test.go | 2 +- vendor/github.com/prometheus/common/README.md | 2 +- .../common/log/eventlog_formatter.go | 2 +- vendor/github.com/prometheus/common/log/log.go | 2 +- .../prometheus/common/log/log_test.go | 2 +- .../prometheus/common/log/syslog_formatter.go | 2 +- xport_grpc.go | 2 +- xport_grpc_out.go | 2 +- xport_grpc_out_test.go | 2 +- xport_grpc_test.go | 2 +- xport_kafka.go | 2 +- xport_tcp.go | 2 +- xport_udp.go | 2 +- xport_udp_test.go | 2 +- 55 files changed, 73 insertions(+), 72 deletions(-) diff --git a/codec.go b/codec.go index 44f0a519..36dce07b 100644 --- a/codec.go +++ b/codec.go @@ -12,7 +12,7 @@ package main import ( "encoding/json" "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/prometheus/client_golang/prometheus" "io/ioutil" ) diff --git a/codec_gpb.go b/codec_gpb.go index 17694ccc..1372040b 100644 --- a/codec_gpb.go +++ b/codec_gpb.go @@ -15,7 +15,7 @@ import ( "encoding/base64" "encoding/json" "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" telem "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go" pdt "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go/old/telemetry" "github.com/golang/protobuf/jsonpb" diff --git a/crypt.go b/crypt.go index 0c387f32..a460be5e 100644 --- a/crypt.go +++ b/crypt.go @@ -20,7 +20,7 @@ import ( "fmt" "io/ioutil" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" ) func collect_pkey(pemFile string) (err error, key *rsa.PrivateKey) { diff --git a/glide.lock b/glide.lock index 7ad870cb..a63869f7 100644 --- a/glide.lock +++ b/glide.lock @@ -656,7 +656,7 @@ imports: version: 1f30fe9094a513ce4c700b9a54458bbb0c96996c - name: github.com/Shopify/sarama version: 0fb560e5f7fbcaee2f75e3c34174320709f69944 -- name: github.com/Sirupsen/logrus +- name: github.com/sirupsen/logrus version: 881bee4e20a5d11a6a88a5667c6f292072ac1963 - name: github.com/tinylib/msgp version: 38a6f61a768dc24552dad94611923841b53acc4f diff --git a/glide.yaml b/glide.yaml index 9e772d60..cbb46b4c 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,7 +1,7 @@ package: bigmuddy-network-telemetry-pipeline import: - package: github.com/Shopify/sarama -- package: github.com/Sirupsen/logrus +- package: github.com/sirupsen/logrus - package: github.com/dlintw/goconf - package: github.com/evalphobia/logrus_fluent - package: github.com/golang/protobuf diff --git a/mdt_msg_samples/samples.go b/mdt_msg_samples/samples.go index c4eb323c..6c0b0a51 100644 --- a/mdt_msg_samples/samples.go +++ b/mdt_msg_samples/samples.go @@ -13,7 +13,7 @@ package mdt_msg_samples import ( "bufio" "encoding/json" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" telem "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go" "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" diff --git a/message_router.go b/message_router.go index 75e66c72..ad1b9c1c 100644 --- a/message_router.go +++ b/message_router.go @@ -9,7 +9,7 @@ package main import ( - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "time" ) diff --git a/message_router_test.go b/message_router_test.go index a932bcec..f4842bd1 100644 --- a/message_router_test.go +++ b/message_router_test.go @@ -10,7 +10,7 @@ package main import ( "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" telem "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go" "github.com/dlintw/goconf" "testing" diff --git a/metamonitoring.go b/metamonitoring.go index 0be8bc5d..13f4f453 100644 --- a/metamonitoring.go +++ b/metamonitoring.go @@ -13,7 +13,7 @@ package main // the aim of having stats scraped by prometheus. // import ( - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/prometheus/client_golang/prometheus" "net/http" ) diff --git a/metrics.go b/metrics.go index 82c59897..d9158f4d 100644 --- a/metrics.go +++ b/metrics.go @@ -15,7 +15,7 @@ import ( "bufio" "encoding/json" "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/prometheus/client_golang/prometheus" "io/ioutil" "os" diff --git a/metrics_influx.go b/metrics_influx.go index 04b6a5b6..93c1725b 100644 --- a/metrics_influx.go +++ b/metrics_influx.go @@ -29,8 +29,8 @@ import ( "runtime" "time" - log "github.com/Sirupsen/logrus" - "github.com/influxdata/influxdb/client/v2" + client "github.com/influxdata/influxdb1-client/v2" + log "github.com/sirupsen/logrus" ) const ( diff --git a/metrics_prometheus.go b/metrics_prometheus.go index 472a2597..38999399 100644 --- a/metrics_prometheus.go +++ b/metrics_prometheus.go @@ -20,7 +20,7 @@ import ( "regexp" "strings" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" ) // diff --git a/pipeline.go b/pipeline.go index e34f975a..714ce376 100644 --- a/pipeline.go +++ b/pipeline.go @@ -23,7 +23,7 @@ import ( "bytes" "flag" "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" telem "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go" "github.com/dlintw/goconf" "github.com/evalphobia/logrus_fluent" diff --git a/pipeline_test.go b/pipeline_test.go index e73d0725..cbfb4065 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -11,7 +11,7 @@ package main import ( "bytes" "encoding/binary" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" samples "github.com/cisco/bigmuddy-network-telemetry-pipeline/mdt_msg_samples" "net" "testing" diff --git a/replay.go b/replay.go index 491597bf..80e5b378 100644 --- a/replay.go +++ b/replay.go @@ -16,7 +16,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "io" "os" "time" diff --git a/tap.go b/tap.go index d996cb83..d7c225e2 100644 --- a/tap.go +++ b/tap.go @@ -16,7 +16,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "os" "time" ) diff --git a/vendor/github.com/Sirupsen/logrus/README.md b/vendor/github.com/Sirupsen/logrus/README.md index 05d678ad..896a01f9 100644 --- a/vendor/github.com/Sirupsen/logrus/README.md +++ b/vendor/github.com/Sirupsen/logrus/README.md @@ -1,4 +1,4 @@ -# Logrus :walrus: [![Build Status](https://travis-ci.org/Sirupsen/logrus.svg?branch=master)](https://travis-ci.org/Sirupsen/logrus) [![GoDoc](https://godoc.org/github.com/Sirupsen/logrus?status.svg)](https://godoc.org/github.com/Sirupsen/logrus) +# Logrus :walrus: [![Build Status](https://travis-ci.org/Sirupsen/logrus.svg?branch=master)](https://travis-ci.org/Sirupsen/logrus) [![GoDoc](https://godoc.org/github.com/sirupsen/logrus?status.svg)](https://godoc.org/github.com/sirupsen/logrus) **Seeing weird case-sensitive problems?** See [this issue](https://github.com/sirupsen/logrus/issues/451#issuecomment-264332021). @@ -60,7 +60,7 @@ The simplest way to use Logrus is simply the package-level exported logger: package main import ( - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" ) func main() { @@ -71,7 +71,7 @@ func main() { ``` Note that it's completely api-compatible with the stdlib logger, so you can -replace your `log` imports everywhere with `log "github.com/Sirupsen/logrus"` +replace your `log` imports everywhere with `log "github.com/sirupsen/logrus"` and you'll now have the flexibility of Logrus. You can customize it all you want: @@ -80,7 +80,7 @@ package main import ( "os" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" ) func init() { @@ -129,7 +129,7 @@ application, you can also create an instance of the `logrus` Logger: package main import ( - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) // Create a new instance of the logger. You can have any number of instances. @@ -182,9 +182,9 @@ Logrus comes with [built-in hooks](hooks/). Add those, or your custom hook, in ```go import ( - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "aibrake" - logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog" + logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" "log/syslog" ) @@ -209,7 +209,7 @@ Note: Syslog hook also support connecting to local syslog (Ex. "/dev/log" or "/v | [Airbrake](https://github.com/gemnasium/logrus-airbrake-hook) | Send errors to the Airbrake API V3. Uses the official [`gobrake`](https://github.com/airbrake/gobrake) behind the scenes. | | [Airbrake "legacy"](https://github.com/gemnasium/logrus-airbrake-legacy-hook) | Send errors to an exception tracking service compatible with the Airbrake API V2. Uses [`airbrake-go`](https://github.com/tobi/airbrake-go) behind the scenes. | | [Papertrail](https://github.com/polds/logrus-papertrail-hook) | Send errors to the [Papertrail](https://papertrailapp.com) hosted logging service via UDP. | -| [Syslog](https://github.com/Sirupsen/logrus/blob/master/hooks/syslog/syslog.go) | Send errors to remote syslog server. Uses standard library `log/syslog` behind the scenes. | +| [Syslog](https://github.com/sirupsen/logrus/blob/master/hooks/syslog/syslog.go) | Send errors to remote syslog server. Uses standard library `log/syslog` behind the scenes. | | [Bugsnag](https://github.com/Shopify/logrus-bugsnag/blob/master/bugsnag.go) | Send errors to the Bugsnag exception tracking service. | | [Sentry](https://github.com/evalphobia/logrus_sentry) | Send errors to the Sentry error logging and aggregation service. | | [Hiprus](https://github.com/nubo/hiprus) | Send errors to a channel in hipchat. | @@ -289,7 +289,7 @@ could do: ```go import ( - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" ) init() { diff --git a/vendor/github.com/Sirupsen/logrus/alt_exit_test.go b/vendor/github.com/Sirupsen/logrus/alt_exit_test.go index 022b7783..d1829634 100644 --- a/vendor/github.com/Sirupsen/logrus/alt_exit_test.go +++ b/vendor/github.com/Sirupsen/logrus/alt_exit_test.go @@ -44,7 +44,7 @@ var testprog = []byte(` package main import ( - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "flag" "fmt" "io/ioutil" diff --git a/vendor/github.com/Sirupsen/logrus/doc.go b/vendor/github.com/Sirupsen/logrus/doc.go index dddd5f87..da67aba0 100644 --- a/vendor/github.com/Sirupsen/logrus/doc.go +++ b/vendor/github.com/Sirupsen/logrus/doc.go @@ -7,7 +7,7 @@ The simplest way to use Logrus is simply the package-level exported logger: package main import ( - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" ) func main() { @@ -21,6 +21,6 @@ The simplest way to use Logrus is simply the package-level exported logger: Output: time="2015-09-07T08:48:33Z" level=info msg="A walrus appears" animal=walrus number=1 size=10 -For a full guide visit https://github.com/Sirupsen/logrus +For a full guide visit https://github.com/sirupsen/logrus */ package logrus diff --git a/vendor/github.com/Sirupsen/logrus/examples/basic/basic.go b/vendor/github.com/Sirupsen/logrus/examples/basic/basic.go index a1623ec0..b22468d9 100644 --- a/vendor/github.com/Sirupsen/logrus/examples/basic/basic.go +++ b/vendor/github.com/Sirupsen/logrus/examples/basic/basic.go @@ -1,7 +1,7 @@ package main import ( - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) var log = logrus.New() diff --git a/vendor/github.com/Sirupsen/logrus/examples/hook/hook.go b/vendor/github.com/Sirupsen/logrus/examples/hook/hook.go index 3187f6d3..c8470c38 100644 --- a/vendor/github.com/Sirupsen/logrus/examples/hook/hook.go +++ b/vendor/github.com/Sirupsen/logrus/examples/hook/hook.go @@ -1,7 +1,7 @@ package main import ( - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "gopkg.in/gemnasium/logrus-airbrake-hook.v2" ) diff --git a/vendor/github.com/Sirupsen/logrus/hooks/syslog/README.md b/vendor/github.com/Sirupsen/logrus/hooks/syslog/README.md index 066704b3..f9538871 100644 --- a/vendor/github.com/Sirupsen/logrus/hooks/syslog/README.md +++ b/vendor/github.com/Sirupsen/logrus/hooks/syslog/README.md @@ -5,8 +5,8 @@ ```go import ( "log/syslog" - "github.com/Sirupsen/logrus" - logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog" + "github.com/sirupsen/logrus" + logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" ) func main() { @@ -24,8 +24,8 @@ If you want to connect to local syslog (Ex. "/dev/log" or "/var/run/syslog" or " ```go import ( "log/syslog" - "github.com/Sirupsen/logrus" - logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog" + "github.com/sirupsen/logrus" + logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" ) func main() { diff --git a/vendor/github.com/Sirupsen/logrus/hooks/syslog/syslog.go b/vendor/github.com/Sirupsen/logrus/hooks/syslog/syslog.go index a36e2003..204f0016 100644 --- a/vendor/github.com/Sirupsen/logrus/hooks/syslog/syslog.go +++ b/vendor/github.com/Sirupsen/logrus/hooks/syslog/syslog.go @@ -4,7 +4,7 @@ package logrus_syslog import ( "fmt" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "log/syslog" "os" ) diff --git a/vendor/github.com/Sirupsen/logrus/hooks/syslog/syslog_test.go b/vendor/github.com/Sirupsen/logrus/hooks/syslog/syslog_test.go index 42762dc1..8d7fbe45 100644 --- a/vendor/github.com/Sirupsen/logrus/hooks/syslog/syslog_test.go +++ b/vendor/github.com/Sirupsen/logrus/hooks/syslog/syslog_test.go @@ -1,7 +1,7 @@ package logrus_syslog import ( - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "log/syslog" "testing" ) diff --git a/vendor/github.com/Sirupsen/logrus/hooks/test/test.go b/vendor/github.com/Sirupsen/logrus/hooks/test/test.go index 06881253..48c06ab2 100644 --- a/vendor/github.com/Sirupsen/logrus/hooks/test/test.go +++ b/vendor/github.com/Sirupsen/logrus/hooks/test/test.go @@ -3,7 +3,7 @@ package test import ( "io/ioutil" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) // test.Hook is a hook designed for dealing with logs in test scenarios. diff --git a/vendor/github.com/Sirupsen/logrus/hooks/test/test_test.go b/vendor/github.com/Sirupsen/logrus/hooks/test/test_test.go index d69455ba..3f55cfe3 100644 --- a/vendor/github.com/Sirupsen/logrus/hooks/test/test_test.go +++ b/vendor/github.com/Sirupsen/logrus/hooks/test/test_test.go @@ -3,7 +3,7 @@ package test import ( "testing" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) diff --git a/vendor/github.com/Sirupsen/logrus/json_formatter.go b/vendor/github.com/Sirupsen/logrus/json_formatter.go index 266554e9..8cedc3a5 100644 --- a/vendor/github.com/Sirupsen/logrus/json_formatter.go +++ b/vendor/github.com/Sirupsen/logrus/json_formatter.go @@ -47,7 +47,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) { switch v := v.(type) { case error: // Otherwise errors are ignored by `encoding/json` - // https://github.com/Sirupsen/logrus/issues/137 + // https://github.com/sirupsen/logrus/issues/137 data[k] = v.Error() default: data[k] = v diff --git a/vendor/github.com/evalphobia/logrus_fluent/README.md b/vendor/github.com/evalphobia/logrus_fluent/README.md index ebbf5580..5a434710 100644 --- a/vendor/github.com/evalphobia/logrus_fluent/README.md +++ b/vendor/github.com/evalphobia/logrus_fluent/README.md @@ -7,7 +7,7 @@ ```go import ( - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "github.com/evalphobia/logrus_fluent" ) diff --git a/vendor/github.com/evalphobia/logrus_fluent/fluent.go b/vendor/github.com/evalphobia/logrus_fluent/fluent.go index 8cbcee5c..4306339c 100644 --- a/vendor/github.com/evalphobia/logrus_fluent/fluent.go +++ b/vendor/github.com/evalphobia/logrus_fluent/fluent.go @@ -1,7 +1,7 @@ package logrus_fluent import ( - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "github.com/fluent/fluent-logger-golang/fluent" ) diff --git a/vendor/github.com/evalphobia/logrus_fluent/fluent_test.go b/vendor/github.com/evalphobia/logrus_fluent/fluent_test.go index dc11b8ed..6b99f730 100644 --- a/vendor/github.com/evalphobia/logrus_fluent/fluent_test.go +++ b/vendor/github.com/evalphobia/logrus_fluent/fluent_test.go @@ -8,7 +8,7 @@ import ( "strings" "testing" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) var ( diff --git a/vendor/github.com/influxdata/influxdb/client/README.md b/vendor/github.com/influxdata/influxdb/client/README.md index 47abd2e6..2ec8afbc 100644 --- a/vendor/github.com/influxdata/influxdb/client/README.md +++ b/vendor/github.com/influxdata/influxdb/client/README.md @@ -1,12 +1,12 @@ # InfluxDB Client -[![GoDoc](https://godoc.org/github.com/influxdata/influxdb?status.svg)](http://godoc.org/github.com/influxdata/influxdb/client/v2) +[![GoDoc](https://godoc.org/github.com/influxdata/influxdb?status.svg)](http://godoc.org/github.com/influxdata/influxdb1-client/v2) ## Description **NOTE:** The Go client library now has a "v2" version, with the old version being deprecated. The new version can be imported at -`import "github.com/influxdata/influxdb/client/v2"`. It is not backwards-compatible. +`import "github.com/influxdata/influxdb1-client/v2"`. It is not backwards-compatible. A Go client library written and maintained by the **InfluxDB** team. This package provides convenience functions to read and write time series data. @@ -52,7 +52,7 @@ import ( "log" "time" - "github.com/influxdata/influxdb/client/v2" + "github.com/influxdata/influxdb1-client/v2" ) const ( @@ -283,7 +283,7 @@ cause fields to have differing timestamps when processed by the server. ## Go Docs Please refer to -[http://godoc.org/github.com/influxdata/influxdb/client/v2](http://godoc.org/github.com/influxdata/influxdb/client/v2) +[http://godoc.org/github.com/influxdata/influxdb1-client/v2](http://godoc.org/github.com/influxdata/influxdb1-client/v2) for documentation. ## See Also diff --git a/vendor/github.com/influxdata/influxdb/client/v2/client.go b/vendor/github.com/influxdata/influxdb/client/v2/client.go index becafd73..01c7c465 100644 --- a/vendor/github.com/influxdata/influxdb/client/v2/client.go +++ b/vendor/github.com/influxdata/influxdb/client/v2/client.go @@ -1,4 +1,4 @@ -package client // import "github.com/influxdata/influxdb/client/v2" +package client // import "github.com/influxdata/influxdb1-client/v2" import ( "bytes" @@ -9,6 +9,7 @@ import ( "io/ioutil" "net/http" "net/url" + "path" "time" "github.com/influxdata/influxdb/models" @@ -361,7 +362,7 @@ func (c *client) Write(bp BatchPoints) error { } u := c.url - u.Path = "write" + u.Path = path.Join(u.Path, "write") req, err := http.NewRequest("POST", u.String(), &b) if err != nil { return err diff --git a/vendor/github.com/influxdata/influxdb/client/v2/example_test.go b/vendor/github.com/influxdata/influxdb/client/v2/example_test.go index 68bb24bc..21288aa4 100644 --- a/vendor/github.com/influxdata/influxdb/client/v2/example_test.go +++ b/vendor/github.com/influxdata/influxdb/client/v2/example_test.go @@ -6,7 +6,7 @@ import ( "os" "time" - "github.com/influxdata/influxdb/client/v2" + "github.com/influxdata/influxdb1-client/v2" ) // Create a new client diff --git a/vendor/github.com/influxdata/influxdb/services/subscriber/http.go b/vendor/github.com/influxdata/influxdb/services/subscriber/http.go index a407ea67..24beac4e 100644 --- a/vendor/github.com/influxdata/influxdb/services/subscriber/http.go +++ b/vendor/github.com/influxdata/influxdb/services/subscriber/http.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "time" - "github.com/influxdata/influxdb/client/v2" + "github.com/influxdata/influxdb1-client/v2" "github.com/influxdata/influxdb/coordinator" ) diff --git a/vendor/github.com/influxdata/influxdb/stress/basic.go b/vendor/github.com/influxdata/influxdb/stress/basic.go index 36ad9c2f..697e864a 100644 --- a/vendor/github.com/influxdata/influxdb/stress/basic.go +++ b/vendor/github.com/influxdata/influxdb/stress/basic.go @@ -11,7 +11,7 @@ import ( "sync" "time" - "github.com/influxdata/influxdb/client/v2" + "github.com/influxdata/influxdb1-client/v2" ) const backoffInterval = time.Duration(500 * time.Millisecond) diff --git a/vendor/github.com/influxdata/influxdb/stress/stress_test.go b/vendor/github.com/influxdata/influxdb/stress/stress_test.go index 0f5b7062..8e6f0249 100644 --- a/vendor/github.com/influxdata/influxdb/stress/stress_test.go +++ b/vendor/github.com/influxdata/influxdb/stress/stress_test.go @@ -12,7 +12,7 @@ import ( "testing" "time" - "github.com/influxdata/influxdb/client/v2" + "github.com/influxdata/influxdb1-client/v2" "github.com/influxdata/influxdb/models" ) diff --git a/vendor/github.com/influxdata/influxdb/stress/v2/main.go b/vendor/github.com/influxdata/influxdb/stress/v2/main.go index 0675809a..318f05f7 100644 --- a/vendor/github.com/influxdata/influxdb/stress/v2/main.go +++ b/vendor/github.com/influxdata/influxdb/stress/v2/main.go @@ -5,7 +5,7 @@ import ( "log" "time" - influx "github.com/influxdata/influxdb/client/v2" + influx "github.com/influxdata/influxdb1-client/v2" "github.com/influxdata/influxdb/stress/v2/stress_client" "github.com/influxdata/influxdb/stress/v2/stressql" ) diff --git a/vendor/github.com/influxdata/influxdb/stress/v2/statement/report.go b/vendor/github.com/influxdata/influxdb/stress/v2/statement/report.go index 897ff7f4..935fc840 100644 --- a/vendor/github.com/influxdata/influxdb/stress/v2/statement/report.go +++ b/vendor/github.com/influxdata/influxdb/stress/v2/statement/report.go @@ -8,7 +8,7 @@ import ( "sort" "time" - influx "github.com/influxdata/influxdb/client/v2" + influx "github.com/influxdata/influxdb1-client/v2" ) // TODO: Refactor this file to utilize a common interface diff --git a/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/reporting.go b/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/reporting.go index 35bfed26..0c822671 100644 --- a/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/reporting.go +++ b/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/reporting.go @@ -5,7 +5,7 @@ import ( "strconv" "time" - influx "github.com/influxdata/influxdb/client/v2" + influx "github.com/influxdata/influxdb1-client/v2" ) // reporting.go contains functions to emit tags and points from various parts of stressClient diff --git a/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/response.go b/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/response.go index ac6a942e..17c4761d 100644 --- a/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/response.go +++ b/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/response.go @@ -3,7 +3,7 @@ package stressClient import ( "log" - influx "github.com/influxdata/influxdb/client/v2" + influx "github.com/influxdata/influxdb1-client/v2" ) // Response holds data scraped from InfluxDB HTTP responses turned into a *influx.Point for reporting diff --git a/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest.go b/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest.go index 51fbd33c..5965dae2 100644 --- a/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest.go +++ b/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest.go @@ -5,7 +5,7 @@ import ( "log" "sync" - influx "github.com/influxdata/influxdb/client/v2" + influx "github.com/influxdata/influxdb1-client/v2" ) // NewStressTest creates the backend for the stress test diff --git a/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest_test.go b/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest_test.go index 4efdea3e..a2f7c506 100644 --- a/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest_test.go +++ b/vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest_test.go @@ -4,7 +4,7 @@ import ( "testing" "time" - influx "github.com/influxdata/influxdb/client/v2" + influx "github.com/influxdata/influxdb1-client/v2" ) func NewBlankTestPoint() *influx.Point { diff --git a/vendor/github.com/prometheus/common/README.md b/vendor/github.com/prometheus/common/README.md index 98f6ce24..11a58494 100644 --- a/vendor/github.com/prometheus/common/README.md +++ b/vendor/github.com/prometheus/common/README.md @@ -6,7 +6,7 @@ components and libraries. * **config**: Common configuration structures * **expfmt**: Decoding and encoding for the exposition format -* **log**: A logging wrapper around [logrus](https://github.com/Sirupsen/logrus) +* **log**: A logging wrapper around [logrus](https://github.com/sirupsen/logrus) * **model**: Shared data structures * **route**: A routing wrapper around [httprouter](https://github.com/julienschmidt/httprouter) using `context.Context` * **version**: Version informations and metric diff --git a/vendor/github.com/prometheus/common/log/eventlog_formatter.go b/vendor/github.com/prometheus/common/log/eventlog_formatter.go index 6d41284c..12b4ba41 100644 --- a/vendor/github.com/prometheus/common/log/eventlog_formatter.go +++ b/vendor/github.com/prometheus/common/log/eventlog_formatter.go @@ -21,7 +21,7 @@ import ( "golang.org/x/sys/windows/svc/eventlog" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) func init() { diff --git a/vendor/github.com/prometheus/common/log/log.go b/vendor/github.com/prometheus/common/log/log.go index efad4842..79b96eb5 100644 --- a/vendor/github.com/prometheus/common/log/log.go +++ b/vendor/github.com/prometheus/common/log/log.go @@ -25,7 +25,7 @@ import ( "strconv" "strings" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) type levelFlag string diff --git a/vendor/github.com/prometheus/common/log/log_test.go b/vendor/github.com/prometheus/common/log/log_test.go index 953adb79..2cd2b18e 100644 --- a/vendor/github.com/prometheus/common/log/log_test.go +++ b/vendor/github.com/prometheus/common/log/log_test.go @@ -18,7 +18,7 @@ import ( "regexp" "testing" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) func TestFileLineLogging(t *testing.T) { diff --git a/vendor/github.com/prometheus/common/log/syslog_formatter.go b/vendor/github.com/prometheus/common/log/syslog_formatter.go index 64f5fdac..fb3a744e 100644 --- a/vendor/github.com/prometheus/common/log/syslog_formatter.go +++ b/vendor/github.com/prometheus/common/log/syslog_formatter.go @@ -20,7 +20,7 @@ import ( "log/syslog" "os" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) var _ logrus.Formatter = (*syslogger)(nil) diff --git a/xport_grpc.go b/xport_grpc.go index edf92926..14c9c296 100644 --- a/xport_grpc.go +++ b/xport_grpc.go @@ -21,7 +21,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" dialin "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go/mdt_grpc_dialin" dialout "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go/mdt_grpc_dialout" "golang.org/x/net/context" diff --git a/xport_grpc_out.go b/xport_grpc_out.go index ef4b58a8..51608944 100644 --- a/xport_grpc_out.go +++ b/xport_grpc_out.go @@ -6,7 +6,7 @@ package main import ( "encoding/json" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/prometheus/client_golang/prometheus" "golang.org/x/net/context" "google.golang.org/grpc" diff --git a/xport_grpc_out_test.go b/xport_grpc_out_test.go index c493a855..b3abc2b8 100644 --- a/xport_grpc_out_test.go +++ b/xport_grpc_out_test.go @@ -2,7 +2,7 @@ package main import ( _ "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" samples "github.com/cisco/bigmuddy-network-telemetry-pipeline/mdt_msg_samples" "github.com/dlintw/goconf" "golang.org/x/net/context" diff --git a/xport_grpc_test.go b/xport_grpc_test.go index cbfeaa5a..2a308e74 100644 --- a/xport_grpc_test.go +++ b/xport_grpc_test.go @@ -10,7 +10,7 @@ package main import ( "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" telem "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go" dialin "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go/mdt_grpc_dialin" "github.com/golang/protobuf/proto" diff --git a/xport_kafka.go b/xport_kafka.go index e75eebc4..d972499d 100644 --- a/xport_kafka.go +++ b/xport_kafka.go @@ -14,7 +14,7 @@ import ( "encoding/json" "fmt" "github.com/Shopify/sarama" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/prometheus/client_golang/prometheus" cluster "gopkg.in/bsm/sarama-cluster.v2" "io/ioutil" diff --git a/xport_tcp.go b/xport_tcp.go index 48f4c020..888b6383 100644 --- a/xport_tcp.go +++ b/xport_tcp.go @@ -12,7 +12,7 @@ package main import ( "encoding/hex" "encoding/json" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "io" "net" "sync" diff --git a/xport_udp.go b/xport_udp.go index 6a8b4c3e..83f6530f 100644 --- a/xport_udp.go +++ b/xport_udp.go @@ -13,7 +13,7 @@ import ( "encoding/hex" "encoding/json" _ "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/prometheus/client_golang/prometheus" "net" "time" diff --git a/xport_udp_test.go b/xport_udp_test.go index 1d077b11..a9800e3b 100644 --- a/xport_udp_test.go +++ b/xport_udp_test.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/binary" "fmt" - log "github.com/Sirupsen/logrus" + log "github.com/sirupsen/logrus" samples "github.com/cisco/bigmuddy-network-telemetry-pipeline/mdt_msg_samples" "github.com/dlintw/goconf" "net" From b8af1ef1e1831b80a7433dd4fcc5d06e33e5ca88 Mon Sep 17 00:00:00 2001 From: Stan Kozlov Date: Wed, 23 Dec 2020 15:36:40 +1100 Subject: [PATCH 5/5] Remove old lib --- .../influxdata/influxdb/.dockerignore | 1 - .../influxdb/.github/ISSUE_TEMPLATE.md | 54 - .../influxdb/.github/PULL_REQUEST_TEMPLATE.md | 12 - .../github.com/influxdata/influxdb/.gitignore | 73 - .../influxdata/influxdb/.hooks/pre-commit | 23 - .../influxdata/influxdb/.mention-bot | 6 - .../influxdata/influxdb/CHANGELOG.md | 2470 --- .../influxdata/influxdb/CODING_GUIDELINES.md | 82 - .../influxdata/influxdb/CONTRIBUTING.md | 280 - .../github.com/influxdata/influxdb/DOCKER.md | 30 - .../github.com/influxdata/influxdb/Dockerfile | 24 - .../influxdb/Dockerfile_build_ubuntu32 | 35 - .../influxdb/Dockerfile_build_ubuntu64 | 38 - .../influxdb/Dockerfile_build_ubuntu64_git | 43 - .../influxdb/Dockerfile_test_ubuntu32 | 12 - vendor/github.com/influxdata/influxdb/Godeps | 20 - vendor/github.com/influxdata/influxdb/LICENSE | 20 - .../influxdb/LICENSE_OF_DEPENDENCIES.md | 25 - .../github.com/influxdata/influxdb/Makefile | 39 - .../github.com/influxdata/influxdb/QUERIES.md | 180 - .../github.com/influxdata/influxdb/README.md | 71 - vendor/github.com/influxdata/influxdb/TODO.md | 9 - .../influxdata/influxdb/appveyor.yml | 37 - .../influxdata/influxdb/build-docker.sh | 9 - .../github.com/influxdata/influxdb/build.py | 987 - .../github.com/influxdata/influxdb/build.sh | 22 - .../influxdata/influxdb/circle-test.sh | 35 - .../github.com/influxdata/influxdb/circle.yml | 41 - .../influxdata/influxdb/client/README.md | 292 - .../influxdb/client/example_test.go | 113 - .../influxdata/influxdb/client/influxdb.go | 829 - .../influxdb/client/influxdb_test.go | 831 - .../influxdata/influxdb/client/v2/client.go | 502 - .../influxdb/client/v2/client_test.go | 480 - .../influxdb/client/v2/example_test.go | 265 - .../influxdata/influxdb/client/v2/udp.go | 112 - .../influxdata/influxdb/cmd/influx/cli/cli.go | 1006 - .../cmd/influx/cli/cli_internal_test.go | 58 - .../influxdb/cmd/influx/cli/cli_test.go | 523 - .../influxdb/cmd/influx/cli/parser.go | 34 - .../cmd/influx/cli/parser_internal_test.go | 90 - .../influxdata/influxdb/cmd/influx/main.go | 119 - .../influxdb/cmd/influx_inspect/README.md | 107 - .../cmd/influx_inspect/dumptsm/dumptsm.go | 334 - .../influx_inspect/dumptsm/dumptsm_test.go | 3 - .../cmd/influx_inspect/export/export.go | 407 - .../cmd/influx_inspect/export/export_test.go | 337 - .../influxdb/cmd/influx_inspect/help/help.go | 42 - .../cmd/influx_inspect/help/help_test.go | 3 - .../influxdb/cmd/influx_inspect/main.go | 84 - .../cmd/influx_inspect/report/report.go | 184 - .../cmd/influx_inspect/report/report_test.go | 3 - .../cmd/influx_inspect/verify/verify.go | 119 - .../cmd/influx_inspect/verify/verify_test.go | 3 - .../influxdb/cmd/influx_stress/README.md | 43 - .../cmd/influx_stress/examples/template.toml | 92 - .../cmd/influx_stress/influx_stress.go | 70 - .../influxdb/cmd/influx_tsm/README.md | 152 - .../influxdb/cmd/influx_tsm/b1/reader.go | 269 - .../influxdb/cmd/influx_tsm/bz1/reader.go | 370 - .../influxdb/cmd/influx_tsm/converter.go | 118 - .../influxdb/cmd/influx_tsm/main.go | 413 - .../influxdb/cmd/influx_tsm/stats/stats.go | 54 - .../influxdb/cmd/influx_tsm/tracker.go | 130 - .../influxdb/cmd/influx_tsm/tsdb/codec.go | 119 - .../influxdb/cmd/influx_tsm/tsdb/database.go | 239 - .../cmd/influx_tsm/tsdb/internal/meta.pb.go | 122 - .../influxdb/cmd/influx_tsm/tsdb/types.go | 60 - .../influxdb/cmd/influxd/backup/backup.go | 376 - .../influxdb/cmd/influxd/help/help.go | 45 - .../influxdata/influxdb/cmd/influxd/main.go | 180 - .../influxdb/cmd/influxd/restore/restore.go | 386 - .../cmd/influxd/run/backup_restore_test.go | 106 - .../influxdb/cmd/influxd/run/command.go | 260 - .../influxdb/cmd/influxd/run/config.go | 294 - .../cmd/influxd/run/config_command.go | 92 - .../influxdb/cmd/influxd/run/config_test.go | 257 - .../influxdb/cmd/influxd/run/server.go | 631 - .../cmd/influxd/run/server_bench_test.go | 136 - .../cmd/influxd/run/server_helpers_test.go | 473 - .../cmd/influxd/run/server_suite_test.go | 518 - .../influxdb/cmd/influxd/run/server_test.go | 7090 ------- .../influxdb/cmd/influxd/run/server_test.md | 150 - .../influxdata/influxdb/cmd/parse.go | 28 - .../influxdata/influxdb/coordinator/config.go | 47 - .../influxdb/coordinator/config_test.go | 24 - .../influxdb/coordinator/meta_client.go | 35 - .../influxdb/coordinator/meta_client_test.go | 160 - .../influxdb/coordinator/points_writer.go | 374 - .../points_writer_internal_test.go | 46 - .../coordinator/points_writer_test.go | 636 - .../coordinator/statement_executor.go | 1236 -- .../coordinator/statement_executor_test.go | 374 - .../github.com/influxdata/influxdb/errors.go | 44 - .../influxdata/influxdb/etc/burn-in/.rvmrc | 1 - .../influxdata/influxdb/etc/burn-in/Gemfile | 4 - .../influxdb/etc/burn-in/Gemfile.lock | 14 - .../influxdb/etc/burn-in/burn-in.rb | 79 - .../influxdata/influxdb/etc/burn-in/log.rb | 23 - .../influxdb/etc/burn-in/random_gaussian.rb | 31 - .../influxdb/etc/burn-in/random_points.rb | 29 - .../influxdb/etc/config.sample.toml | 439 - .../github.com/influxdata/influxdb/gobuild.sh | 18 - .../influxdata/influxdb/importer/README.md | 214 - .../influxdb/importer/v8/importer.go | 240 - .../influxdata/influxdb/influxdb.go | 1 - .../influxdata/influxdb/influxql/README.md | 1048 - .../influxdata/influxdb/influxql/ast.go | 4754 ----- .../influxdata/influxdb/influxql/ast_test.go | 1433 -- .../influxdb/influxql/call_iterator.go | 1294 -- .../influxdb/influxql/call_iterator_test.go | 944 - .../influxdata/influxdb/influxql/cast.go | 41 - .../influxdata/influxdb/influxql/doc.go | 59 - .../influxdata/influxdb/influxql/emitter.go | 221 - .../influxdb/influxql/emitter_test.go | 125 - .../influxdb/influxql/functions.gen.go | 1669 -- .../influxdb/influxql/functions.gen.go.tmpl | 219 - .../influxdata/influxdb/influxql/functions.go | 759 - .../influxdb/influxql/functions_test.go | 498 - .../influxdata/influxdb/influxql/influxql.go | 7 - .../influxdb/influxql/internal/internal.pb.go | 528 - .../influxdb/influxql/internal/internal.proto | 72 - .../influxdb/influxql/iterator.gen.go | 9664 ---------- .../influxdb/influxql/iterator.gen.go.tmpl | 1586 -- .../influxdata/influxdb/influxql/iterator.go | 1223 -- .../influxdb/influxql/iterator_test.go | 1213 -- .../influxdata/influxdb/influxql/linear.go | 21 - .../influxdata/influxdb/influxql/monitor.go | 23 - .../influxql/neldermead/neldermead.go | 235 - .../influxql/neldermead/neldermead_test.go | 64 - .../influxdata/influxdb/influxql/parser.go | 2953 --- .../influxdb/influxql/parser_test.go | 3041 --- .../influxdata/influxdb/influxql/point.gen.go | 823 - .../influxdb/influxql/point.gen.go.tmpl | 226 - .../influxdata/influxdb/influxql/point.go | 339 - .../influxdb/influxql/point_test.go | 187 - .../influxdb/influxql/query_executor.go | 416 - .../influxdb/influxql/query_executor_test.go | 372 - .../influxdata/influxdb/influxql/result.go | 113 - .../influxdata/influxdb/influxql/sanitize.go | 47 - .../influxdb/influxql/sanitize_test.go | 49 - .../influxdata/influxdb/influxql/scanner.go | 590 - .../influxdb/influxql/scanner_test.go | 297 - .../influxdata/influxdb/influxql/select.go | 1058 - .../influxdb/influxql/select_test.go | 2962 --- .../influxdb/influxql/statement_rewriter.go | 230 - .../influxql/statement_rewriter_test.go | 161 - .../influxdb/influxql/task_manager.go | 262 - .../influxdata/influxdb/influxql/tmpldata | 30 - .../influxdata/influxdb/influxql/token.go | 317 - .../influxdb/internal/meta_client.go | 157 - .../influxdata/influxdb/man/Makefile | 41 - .../influxdata/influxdb/man/README.md | 38 - .../influxdata/influxdb/man/footer.txt | 13 - .../influxdata/influxdb/man/influx.txt | 81 - .../influxdb/man/influx_inspect.txt | 86 - .../influxdata/influxdb/man/influx_stress.txt | 52 - .../influxdata/influxdb/man/influx_tsm.txt | 58 - .../influxdb/man/influxd-backup.txt | 37 - .../influxdb/man/influxd-config.txt | 40 - .../influxdb/man/influxd-restore.txt | 37 - .../influxdata/influxdb/man/influxd-run.txt | 32 - .../influxdb/man/influxd-version.txt | 17 - .../influxdata/influxdb/man/influxd.txt | 40 - .../influxdata/influxdb/models/consistency.go | 46 - .../influxdata/influxdb/models/inline_fnv.go | 27 - .../influxdb/models/inline_fnv_test.go | 29 - .../influxdb/models/inline_strconv_parse.go | 38 - .../models/inline_strconv_parse_test.go | 103 - .../influxdata/influxdb/models/points.go | 1902 -- .../influxdata/influxdb/models/points_test.go | 2236 --- .../influxdata/influxdb/models/rows.go | 59 - .../influxdata/influxdb/models/statistic.go | 40 - .../influxdb/models/statistic_test.go | 55 - .../influxdata/influxdb/models/time.go | 74 - .../influxdata/influxdb/monitor/README.md | 46 - .../influxdata/influxdb/monitor/build_info.go | 22 - .../influxdata/influxdb/monitor/config.go | 44 - .../influxdb/monitor/config_test.go | 30 - .../monitor/diagnostics/diagnostics.go | 41 - .../influxdata/influxdb/monitor/go_runtime.go | 21 - .../influxdata/influxdb/monitor/network.go | 23 - .../influxdata/influxdb/monitor/reporter.go | 8 - .../influxdata/influxdb/monitor/service.go | 477 - .../influxdata/influxdb/monitor/system.go | 28 - .../github.com/influxdata/influxdb/nightly.sh | 57 - vendor/github.com/influxdata/influxdb/node.go | 116 - .../github.com/influxdata/influxdb/package.sh | 571 - .../influxdata/influxdb/pkg/README.md | 5 - .../influxdata/influxdb/pkg/deep/equal.go | 184 - .../influxdata/influxdb/pkg/escape/bytes.go | 102 - .../influxdb/pkg/escape/bytes_test.go | 68 - .../influxdata/influxdb/pkg/escape/strings.go | 19 - .../influxdb/pkg/escape/strings_test.go | 115 - .../influxdata/influxdb/pkg/limiter/fixed.go | 18 - .../influxdata/influxdb/pkg/pool/bytes.go | 42 - .../influxdata/influxdb/pkg/pool/generic.go | 40 - .../influxdata/influxdb/pkg/slices/strings.go | 40 - .../influxdb/scripts/influxdb.service | 19 - .../influxdata/influxdb/scripts/init.sh | 233 - .../influxdata/influxdb/scripts/logrotate | 8 - .../influxdb/scripts/post-install.sh | 73 - .../influxdb/scripts/post-uninstall.sh | 56 - .../influxdb/scripts/pre-install.sh | 16 - .../influxdb/services/admin/README.md | 23 - .../influxdb/services/admin/admin.go | 4 - .../influxdb/services/admin/assets/README.md | 4 - .../services/admin/assets/css/admin.css | 87 - .../services/admin/assets/css/bootstrap.css | 6584 ------- .../assets/css/dropdowns-enhancement.css | 294 - .../fonts/glyphicons-halflings-regular.eot | Bin 20127 -> 0 bytes .../fonts/glyphicons-halflings-regular.svg | 288 - .../fonts/glyphicons-halflings-regular.ttf | Bin 45404 -> 0 bytes .../fonts/glyphicons-halflings-regular.woff | Bin 23424 -> 0 bytes .../fonts/glyphicons-halflings-regular.woff2 | Bin 18028 -> 0 bytes .../services/admin/assets/img/favicon.ico | Bin 2410 -> 0 bytes .../admin/assets/img/influxdb-light400.png | Bin 19775 -> 0 bytes .../influxdb/services/admin/assets/index.html | 227 - .../services/admin/assets/js/admin.js | 529 - .../influxdb/services/admin/config.go | 24 - .../influxdb/services/admin/config_test.go | 32 - .../influxdb/services/admin/service.go | 119 - .../influxdb/services/admin/service_test.go | 33 - .../influxdb/services/admin/statik/README.md | 3 - .../influxdb/services/admin/statik/statik.go | 10 - .../influxdb/services/collectd/README.md | 37 - .../services/collectd/collectd_test.conf | 209 - .../influxdb/services/collectd/config.go | 129 - .../influxdb/services/collectd/config_test.go | 32 - .../influxdb/services/collectd/service.go | 427 - .../services/collectd/service_test.go | 609 - .../services/collectd/test_client/README.md | 3 - .../services/collectd/test_client/client.go | 73 - .../services/continuous_querier/config.go | 36 - .../continuous_querier/config_test.go | 27 - .../continuous_querier/continuous_queries.md | 235 - .../services/continuous_querier/service.go | 478 - .../continuous_querier/service_test.go | 633 - .../influxdb/services/graphite/README.md | 192 - .../influxdb/services/graphite/config.go | 255 - .../influxdb/services/graphite/config_test.go | 170 - .../influxdb/services/graphite/errors.go | 14 - .../influxdb/services/graphite/parser.go | 419 - .../influxdb/services/graphite/parser_test.go | 717 - .../influxdb/services/graphite/service.go | 473 - .../services/graphite/service_test.go | 309 - .../influxdb/services/httpd/config.go | 47 - .../influxdb/services/httpd/config_test.go | 55 - .../influxdb/services/httpd/handler.go | 1159 -- .../influxdb/services/httpd/handler_test.go | 739 - .../influxdb/services/httpd/listen.go | 51 - .../influxdb/services/httpd/listen_test.go | 96 - .../services/httpd/response_logger.go | 166 - .../services/httpd/response_writer.go | 174 - .../influxdb/services/httpd/service.go | 214 - .../influxdb/services/meta/client.go | 1042 - .../influxdb/services/meta/client_test.go | 1100 -- .../influxdb/services/meta/config.go | 37 - .../influxdb/services/meta/config_test.go | 26 - .../influxdata/influxdb/services/meta/data.go | 1530 -- .../services/meta/data_internal_test.go | 56 - .../influxdb/services/meta/data_test.go | 111 - .../influxdb/services/meta/errors.go | 113 - .../influxdb/services/meta/file_unix.go | 10 - .../influxdb/services/meta/file_windows.go | 14 - .../services/meta/internal/meta.pb.go | 1971 -- .../services/meta/internal/meta.proto | 393 - .../influxdb/services/meta/meta_test.go | 7 - .../services/meta/query_authorizer.go | 110 - .../services/meta/write_authorizer.go | 27 - .../influxdb/services/opentsdb/README.md | 10 - .../influxdb/services/opentsdb/config.go | 96 - .../influxdb/services/opentsdb/config_test.go | 41 - .../influxdb/services/opentsdb/handler.go | 197 - .../influxdb/services/opentsdb/service.go | 474 - .../services/opentsdb/service_test.go | 295 - .../influxdb/services/precreator/README.md | 13 - .../influxdb/services/precreator/config.go | 32 - .../services/precreator/config_test.go | 31 - .../influxdb/services/precreator/service.go | 94 - .../services/precreator/service_test.go | 55 - .../influxdb/services/retention/config.go | 18 - .../services/retention/config_test.go | 27 - .../influxdb/services/retention/service.go | 136 - .../influxdb/services/snapshotter/client.go | 77 - .../influxdb/services/snapshotter/service.go | 282 - .../services/snapshotter/service_test.go | 1 - .../influxdb/services/subscriber/config.go | 80 - .../services/subscriber/config_test.go | 111 - .../influxdb/services/subscriber/http.go | 72 - .../influxdb/services/subscriber/service.go | 438 - .../services/subscriber/service_test.go | 443 - .../influxdb/services/subscriber/udp.go | 42 - .../influxdb/services/udp/README.md | 128 - .../influxdb/services/udp/config.go | 97 - .../influxdb/services/udp/config_test.go | 43 - .../influxdb/services/udp/service.go | 302 - .../influxdb/services/udp/service_test.go | 160 - .../influxdata/influxdb/stress/DESIGN.md | 47 - .../influxdata/influxdb/stress/README.md | 115 - .../influxdata/influxdb/stress/basic.go | 696 - .../influxdata/influxdb/stress/config.go | 145 - .../influxdata/influxdb/stress/run.go | 335 - .../influxdata/influxdb/stress/stress.toml | 54 - .../influxdata/influxdb/stress/stress_test.go | 599 - .../stress/stress_test_server/server.go | 73 - .../influxdata/influxdb/stress/template.go | 64 - .../influxdata/influxdb/stress/util.go | 132 - .../influxdata/influxdb/stress/v2/DESIGN.md | 164 - .../influxdata/influxdb/stress/v2/README.md | 177 - .../influxdb/stress/v2/influx_stress_v2.png | Bin 93150 -> 0 bytes .../influxdb/stress/v2/iql/default.iql | 13 - .../influxdb/stress/v2/iql/file.iql | 45 - .../influxdata/influxdb/stress/v2/main.go | 59 - .../influxdb/stress/v2/statement/exec.go | 32 - .../influxdb/stress/v2/statement/exec_test.go | 41 - .../influxdb/stress/v2/statement/function.go | 176 - .../stress/v2/statement/function_test.go | 143 - .../influxdb/stress/v2/statement/go.go | 40 - .../influxdb/stress/v2/statement/go_test.go | 41 - .../influxdb/stress/v2/statement/influxql.go | 69 - .../stress/v2/statement/influxql_test.go | 44 - .../influxdb/stress/v2/statement/insert.go | 214 - .../stress/v2/statement/insert_test.go | 50 - .../influxdb/stress/v2/statement/query.go | 157 - .../stress/v2/statement/query_test.go | 42 - .../influxdb/stress/v2/statement/report.go | 237 - .../stress/v2/statement/report_test.go | 210 - .../stress/v2/statement/response_time.go | 40 - .../stress/v2/statement/response_time_test.go | 45 - .../influxdb/stress/v2/statement/set.go | 59 - .../influxdb/stress/v2/statement/set_test.go | 92 - .../influxdb/stress/v2/statement/statement.go | 32 - .../influxdb/stress/v2/statement/template.go | 47 - .../stress/v2/statement/template_test.go | 72 - .../influxdb/stress/v2/statement/timestamp.go | 51 - .../stress/v2/statement/timestamp_test.go | 31 - .../influxdb/stress/v2/statement/wait.go | 32 - .../influxdb/stress/v2/statement/wait_test.go | 41 - .../stress/v2/stress_client/commune.go | 58 - .../stress/v2/stress_client/commune_test.go | 49 - .../stress/v2/stress_client/directive.go | 19 - .../stress/v2/stress_client/directive_test.go | 20 - .../stress/v2/stress_client/package.go | 22 - .../stress/v2/stress_client/package_test.go | 16 - .../stress/v2/stress_client/reporting.go | 95 - .../stress/v2/stress_client/reporting_test.go | 92 - .../stress/v2/stress_client/response.go | 45 - .../stress/v2/stress_client/response_test.go | 17 - .../stress/v2/stress_client/stressTest.go | 172 - .../v2/stress_client/stressTest_test.go | 32 - .../stress/v2/stress_client/stress_client.go | 175 - .../v2/stress_client/stress_client_query.go | 74 - .../v2/stress_client/stress_client_write.go | 112 - .../stress/v2/stress_client/tracer.go | 19 - .../stress/v2/stress_client/tracer_test.go | 17 - .../influxdb/stress/v2/stress_client/util.go | 89 - .../influxdb/stress/v2/stressql/parser.go | 158 - .../stress/v2/stressql/parser_test.go | 16 - .../stress/v2/stressql/statement/parser.go | 727 - .../v2/stressql/statement/parser_test.go | 243 - .../github.com/influxdata/influxdb/tcp/mux.go | 236 - .../influxdata/influxdb/tcp/mux_test.go | 137 - vendor/github.com/influxdata/influxdb/test.sh | 206 - .../influxdata/influxdb/tests/README.md | 4 - .../influxdb/tests/create_future_writes.sh | 22 - .../tests/create_write_multiple_query.sh | 14 - .../tests/create_write_single_query.sh | 19 - ..._with_multiple_measurements_values_tags.sh | 23 - ...e_write_single_with_multiple_tags_query.sh | 11 - .../influxdb/tests/distinct-data-scenarios.sh | 35 - .../influxdb/tests/read_write_gzip.sh | 15 - .../influxdb/tests/siege/.gitignore | 1 - .../influxdata/influxdb/tests/siege/README.md | 66 - .../influxdata/influxdb/tests/siege/urlgen | 107 - .../influxdata/influxdb/tests/tmux/3_shards | 28 - .../influxdata/influxdb/tests/tmux/README.md | 31 - .../influxdb/tests/tmux/sample.json | 16000 ---------------- .../influxdata/influxdb/tests/tmux/seed.sh | 13 - .../influxdb/tests/tmux/server_8086.toml | 7 - .../influxdb/tests/tmux/server_8087.toml | 7 - .../influxdb/tests/tmux/server_8088.toml | 7 - .../influxdb/tests/urlgen/urlgen.go | 58 - .../influxdata/influxdb/toml/toml.go | 72 - .../influxdata/influxdb/toml/toml_test.go | 45 - .../influxdata/influxdb/tsdb/README.md | 91 - .../influxdata/influxdb/tsdb/batcher.go | 149 - .../influxdata/influxdb/tsdb/batcher_test.go | 146 - .../influxdata/influxdb/tsdb/config.go | 118 - .../influxdata/influxdb/tsdb/config_test.go | 48 - .../influxdata/influxdb/tsdb/cursor.go | 13 - .../influxdata/influxdb/tsdb/doc.go | 5 - .../influxdata/influxdb/tsdb/engine.go | 131 - .../influxdata/influxdb/tsdb/engine/engine.go | 6 - .../influxdb/tsdb/engine/tsm1/DESIGN.md | 451 - .../influxdb/tsdb/engine/tsm1/bit_reader.go | 132 - .../tsdb/engine/tsm1/bit_reader_test.go | 177 - .../influxdb/tsdb/engine/tsm1/bool.go | 163 - .../influxdb/tsdb/engine/tsm1/bool_test.go | 161 - .../influxdb/tsdb/engine/tsm1/cache.go | 691 - .../tsdb/engine/tsm1/cache_race_test.go | 178 - .../influxdb/tsdb/engine/tsm1/cache_test.go | 770 - .../influxdb/tsdb/engine/tsm1/compact.go | 1363 -- .../influxdb/tsdb/engine/tsm1/compact_test.go | 2265 --- .../influxdb/tsdb/engine/tsm1/cursor.go | 77 - .../influxdb/tsdb/engine/tsm1/encoding.gen.go | 922 - .../tsdb/engine/tsm1/encoding.gen.go.tmpl | 194 - .../tsdb/engine/tsm1/encoding.gen.go.tmpldata | 22 - .../influxdb/tsdb/engine/tsm1/encoding.go | 826 - .../tsdb/engine/tsm1/encoding_test.go | 1438 -- .../influxdb/tsdb/engine/tsm1/engine.go | 1648 -- .../influxdb/tsdb/engine/tsm1/engine_test.go | 1037 - .../tsdb/engine/tsm1/file_store.gen.go | 659 - .../tsdb/engine/tsm1/file_store.gen.go.tmpl | 168 - .../engine/tsm1/file_store.gen.go.tmpldata | 18 - .../influxdb/tsdb/engine/tsm1/file_store.go | 1216 -- .../tsdb/engine/tsm1/file_store_test.go | 2462 --- .../influxdb/tsdb/engine/tsm1/file_unix.go | 20 - .../influxdb/tsdb/engine/tsm1/file_windows.go | 18 - .../influxdb/tsdb/engine/tsm1/float.go | 278 - .../influxdb/tsdb/engine/tsm1/float_test.go | 286 - .../influxdb/tsdb/engine/tsm1/int.go | 314 - .../influxdb/tsdb/engine/tsm1/int_test.go | 646 - .../influxdb/tsdb/engine/tsm1/iterator.gen.go | 1890 -- .../tsdb/engine/tsm1/iterator.gen.go.tmpl | 560 - .../tsdb/engine/tsm1/iterator.gen.go.tmpldata | 30 - .../influxdb/tsdb/engine/tsm1/iterator.go | 48 - .../influxdb/tsdb/engine/tsm1/mmap_solaris.go | 32 - .../influxdb/tsdb/engine/tsm1/mmap_unix.go | 31 - .../influxdb/tsdb/engine/tsm1/mmap_windows.go | 117 - .../influxdb/tsdb/engine/tsm1/pools.go | 143 - .../influxdb/tsdb/engine/tsm1/reader.go | 1275 -- .../influxdb/tsdb/engine/tsm1/reader_test.go | 1436 -- .../influxdb/tsdb/engine/tsm1/ring.go | 299 - .../influxdb/tsdb/engine/tsm1/ring_test.go | 111 - .../influxdb/tsdb/engine/tsm1/string.go | 121 - .../influxdb/tsdb/engine/tsm1/string_test.go | 177 - .../influxdb/tsdb/engine/tsm1/timestamp.go | 395 - .../tsdb/engine/tsm1/timestamp_test.go | 603 - .../influxdb/tsdb/engine/tsm1/tombstone.go | 324 - .../tsdb/engine/tsm1/tombstone_test.go | 236 - .../influxdb/tsdb/engine/tsm1/wal.go | 1024 - .../influxdb/tsdb/engine/tsm1/wal_test.go | 732 - .../influxdb/tsdb/engine/tsm1/writer.go | 606 - .../influxdb/tsdb/engine/tsm1/writer_test.go | 632 - .../influxdb/tsdb/internal/meta.pb.go | 157 - .../influxdb/tsdb/internal/meta.proto | 27 - .../influxdata/influxdb/tsdb/meta.go | 2080 -- .../influxdata/influxdb/tsdb/meta_test.go | 393 - .../influxdata/influxdb/tsdb/shard.go | 1435 -- .../influxdata/influxdb/tsdb/shard_test.go | 977 - .../influxdata/influxdb/tsdb/store.go | 1069 -- .../influxdata/influxdb/tsdb/store_test.go | 532 - .../influxdata/influxdb/uuid/uuid.go | 95 - 454 files changed, 166795 deletions(-) delete mode 100644 vendor/github.com/influxdata/influxdb/.dockerignore delete mode 100644 vendor/github.com/influxdata/influxdb/.github/ISSUE_TEMPLATE.md delete mode 100644 vendor/github.com/influxdata/influxdb/.github/PULL_REQUEST_TEMPLATE.md delete mode 100644 vendor/github.com/influxdata/influxdb/.gitignore delete mode 100755 vendor/github.com/influxdata/influxdb/.hooks/pre-commit delete mode 100644 vendor/github.com/influxdata/influxdb/.mention-bot delete mode 100644 vendor/github.com/influxdata/influxdb/CHANGELOG.md delete mode 100644 vendor/github.com/influxdata/influxdb/CODING_GUIDELINES.md delete mode 100644 vendor/github.com/influxdata/influxdb/CONTRIBUTING.md delete mode 100644 vendor/github.com/influxdata/influxdb/DOCKER.md delete mode 100644 vendor/github.com/influxdata/influxdb/Dockerfile delete mode 100644 vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu32 delete mode 100644 vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu64 delete mode 100644 vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu64_git delete mode 100644 vendor/github.com/influxdata/influxdb/Dockerfile_test_ubuntu32 delete mode 100644 vendor/github.com/influxdata/influxdb/Godeps delete mode 100644 vendor/github.com/influxdata/influxdb/LICENSE delete mode 100644 vendor/github.com/influxdata/influxdb/LICENSE_OF_DEPENDENCIES.md delete mode 100644 vendor/github.com/influxdata/influxdb/Makefile delete mode 100644 vendor/github.com/influxdata/influxdb/QUERIES.md delete mode 100644 vendor/github.com/influxdata/influxdb/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/TODO.md delete mode 100644 vendor/github.com/influxdata/influxdb/appveyor.yml delete mode 100755 vendor/github.com/influxdata/influxdb/build-docker.sh delete mode 100755 vendor/github.com/influxdata/influxdb/build.py delete mode 100755 vendor/github.com/influxdata/influxdb/build.sh delete mode 100755 vendor/github.com/influxdata/influxdb/circle-test.sh delete mode 100644 vendor/github.com/influxdata/influxdb/circle.yml delete mode 100644 vendor/github.com/influxdata/influxdb/client/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/client/example_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/client/influxdb.go delete mode 100644 vendor/github.com/influxdata/influxdb/client/influxdb_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/client/v2/client.go delete mode 100644 vendor/github.com/influxdata/influxdb/client/v2/client_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/client/v2/example_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/client/v2/udp.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_internal_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser_internal_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx/main.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/dumptsm/dumptsm.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/dumptsm/dumptsm_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/export/export.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/export/export_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/help/help.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/help/help_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/main.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/report/report.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/report/report_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/verify/verify.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_inspect/verify/verify_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_stress/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_stress/examples/template.toml delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_stress/influx_stress.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/b1/reader.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/bz1/reader.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/converter.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/main.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/stats/stats.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tracker.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/codec.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/database.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/internal/meta.pb.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/types.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/backup/backup.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/help/help.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/main.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/restore/restore.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/backup_restore_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/command.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/config_command.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/server.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_bench_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_helpers_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_suite_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_test.md delete mode 100644 vendor/github.com/influxdata/influxdb/cmd/parse.go delete mode 100644 vendor/github.com/influxdata/influxdb/coordinator/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/coordinator/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/coordinator/meta_client.go delete mode 100644 vendor/github.com/influxdata/influxdb/coordinator/meta_client_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/coordinator/points_writer.go delete mode 100644 vendor/github.com/influxdata/influxdb/coordinator/points_writer_internal_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/coordinator/points_writer_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/coordinator/statement_executor.go delete mode 100644 vendor/github.com/influxdata/influxdb/coordinator/statement_executor_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/errors.go delete mode 100644 vendor/github.com/influxdata/influxdb/etc/burn-in/.rvmrc delete mode 100644 vendor/github.com/influxdata/influxdb/etc/burn-in/Gemfile delete mode 100644 vendor/github.com/influxdata/influxdb/etc/burn-in/Gemfile.lock delete mode 100644 vendor/github.com/influxdata/influxdb/etc/burn-in/burn-in.rb delete mode 100644 vendor/github.com/influxdata/influxdb/etc/burn-in/log.rb delete mode 100644 vendor/github.com/influxdata/influxdb/etc/burn-in/random_gaussian.rb delete mode 100644 vendor/github.com/influxdata/influxdb/etc/burn-in/random_points.rb delete mode 100644 vendor/github.com/influxdata/influxdb/etc/config.sample.toml delete mode 100755 vendor/github.com/influxdata/influxdb/gobuild.sh delete mode 100644 vendor/github.com/influxdata/influxdb/importer/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/importer/v8/importer.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxdb.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/ast.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/ast_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/call_iterator.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/call_iterator_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/cast.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/doc.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/emitter.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/emitter_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/functions.gen.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/functions.gen.go.tmpl delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/functions.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/functions_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/influxql.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/internal/internal.pb.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/internal/internal.proto delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/iterator.gen.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/iterator.gen.go.tmpl delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/iterator.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/iterator_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/linear.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/monitor.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/neldermead/neldermead.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/neldermead/neldermead_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/parser.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/parser_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/point.gen.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/point.gen.go.tmpl delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/point.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/point_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/query_executor.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/query_executor_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/result.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/sanitize.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/sanitize_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/scanner.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/scanner_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/select.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/select_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/statement_rewriter.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/statement_rewriter_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/task_manager.go delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/tmpldata delete mode 100644 vendor/github.com/influxdata/influxdb/influxql/token.go delete mode 100644 vendor/github.com/influxdata/influxdb/internal/meta_client.go delete mode 100644 vendor/github.com/influxdata/influxdb/man/Makefile delete mode 100644 vendor/github.com/influxdata/influxdb/man/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/man/footer.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influx.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influx_inspect.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influx_stress.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influx_tsm.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influxd-backup.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influxd-config.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influxd-restore.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influxd-run.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influxd-version.txt delete mode 100644 vendor/github.com/influxdata/influxdb/man/influxd.txt delete mode 100644 vendor/github.com/influxdata/influxdb/models/consistency.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/inline_fnv.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/inline_fnv_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/inline_strconv_parse.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/inline_strconv_parse_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/points.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/points_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/rows.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/statistic.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/statistic_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/models/time.go delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/build_info.go delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/diagnostics/diagnostics.go delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/go_runtime.go delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/network.go delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/reporter.go delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/monitor/system.go delete mode 100755 vendor/github.com/influxdata/influxdb/nightly.sh delete mode 100644 vendor/github.com/influxdata/influxdb/node.go delete mode 100755 vendor/github.com/influxdata/influxdb/package.sh delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/deep/equal.go delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/escape/bytes.go delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/escape/bytes_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/escape/strings.go delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/escape/strings_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/limiter/fixed.go delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/pool/bytes.go delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/pool/generic.go delete mode 100644 vendor/github.com/influxdata/influxdb/pkg/slices/strings.go delete mode 100644 vendor/github.com/influxdata/influxdb/scripts/influxdb.service delete mode 100755 vendor/github.com/influxdata/influxdb/scripts/init.sh delete mode 100644 vendor/github.com/influxdata/influxdb/scripts/logrotate delete mode 100644 vendor/github.com/influxdata/influxdb/scripts/post-install.sh delete mode 100644 vendor/github.com/influxdata/influxdb/scripts/post-uninstall.sh delete mode 100755 vendor/github.com/influxdata/influxdb/scripts/pre-install.sh delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/admin.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/css/admin.css delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/css/bootstrap.css delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/css/dropdowns-enhancement.css delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/fonts/glyphicons-halflings-regular.eot delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/fonts/glyphicons-halflings-regular.svg delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/fonts/glyphicons-halflings-regular.ttf delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/fonts/glyphicons-halflings-regular.woff delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/fonts/glyphicons-halflings-regular.woff2 delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/img/favicon.ico delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/img/influxdb-light400.png delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/index.html delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/assets/js/admin.js delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/service_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/statik/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/admin/statik/statik.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/collectd/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/collectd/collectd_test.conf delete mode 100644 vendor/github.com/influxdata/influxdb/services/collectd/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/collectd/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/collectd/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/collectd/service_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/collectd/test_client/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/collectd/test_client/client.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/continuous_querier/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/continuous_querier/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/continuous_querier/continuous_queries.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/continuous_querier/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/continuous_querier/service_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/graphite/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/graphite/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/graphite/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/graphite/errors.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/graphite/parser.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/graphite/parser_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/graphite/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/graphite/service_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/httpd/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/httpd/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/httpd/handler.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/httpd/handler_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/httpd/listen.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/httpd/listen_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/httpd/response_logger.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/httpd/response_writer.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/httpd/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/client.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/client_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/data.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/data_internal_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/data_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/errors.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/file_unix.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/file_windows.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/internal/meta.pb.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/internal/meta.proto delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/meta_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/query_authorizer.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/meta/write_authorizer.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/opentsdb/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/opentsdb/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/opentsdb/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/opentsdb/handler.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/opentsdb/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/opentsdb/service_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/precreator/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/precreator/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/precreator/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/precreator/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/precreator/service_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/retention/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/retention/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/retention/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/snapshotter/client.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/snapshotter/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/snapshotter/service_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/subscriber/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/subscriber/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/subscriber/http.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/subscriber/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/subscriber/service_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/subscriber/udp.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/udp/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/services/udp/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/udp/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/udp/service.go delete mode 100644 vendor/github.com/influxdata/influxdb/services/udp/service_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/DESIGN.md delete mode 100644 vendor/github.com/influxdata/influxdb/stress/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/stress/basic.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/run.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/stress.toml delete mode 100644 vendor/github.com/influxdata/influxdb/stress/stress_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/stress_test_server/server.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/template.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/util.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/DESIGN.md delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/influx_stress_v2.png delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/iql/default.iql delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/iql/file.iql delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/main.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/exec.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/exec_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/function.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/function_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/go.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/go_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/influxql.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/influxql_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/insert.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/insert_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/query.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/query_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/report.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/report_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/response_time.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/response_time_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/set.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/set_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/statement.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/template.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/template_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/timestamp.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/timestamp_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/wait.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/statement/wait_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/commune.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/commune_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/directive.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/directive_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/package.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/package_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/reporting.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/reporting_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/response.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/response_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stressTest_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stress_client.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stress_client_query.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/stress_client_write.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/tracer.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/tracer_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stress_client/util.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stressql/parser.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stressql/parser_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stressql/statement/parser.go delete mode 100644 vendor/github.com/influxdata/influxdb/stress/v2/stressql/statement/parser_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tcp/mux.go delete mode 100644 vendor/github.com/influxdata/influxdb/tcp/mux_test.go delete mode 100755 vendor/github.com/influxdata/influxdb/test.sh delete mode 100644 vendor/github.com/influxdata/influxdb/tests/README.md delete mode 100755 vendor/github.com/influxdata/influxdb/tests/create_future_writes.sh delete mode 100755 vendor/github.com/influxdata/influxdb/tests/create_write_multiple_query.sh delete mode 100755 vendor/github.com/influxdata/influxdb/tests/create_write_single_query.sh delete mode 100755 vendor/github.com/influxdata/influxdb/tests/create_write_single_with_multiple_measurements_values_tags.sh delete mode 100755 vendor/github.com/influxdata/influxdb/tests/create_write_single_with_multiple_tags_query.sh delete mode 100755 vendor/github.com/influxdata/influxdb/tests/distinct-data-scenarios.sh delete mode 100755 vendor/github.com/influxdata/influxdb/tests/read_write_gzip.sh delete mode 100644 vendor/github.com/influxdata/influxdb/tests/siege/.gitignore delete mode 100644 vendor/github.com/influxdata/influxdb/tests/siege/README.md delete mode 100755 vendor/github.com/influxdata/influxdb/tests/siege/urlgen delete mode 100755 vendor/github.com/influxdata/influxdb/tests/tmux/3_shards delete mode 100644 vendor/github.com/influxdata/influxdb/tests/tmux/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/tests/tmux/sample.json delete mode 100755 vendor/github.com/influxdata/influxdb/tests/tmux/seed.sh delete mode 100644 vendor/github.com/influxdata/influxdb/tests/tmux/server_8086.toml delete mode 100644 vendor/github.com/influxdata/influxdb/tests/tmux/server_8087.toml delete mode 100644 vendor/github.com/influxdata/influxdb/tests/tmux/server_8088.toml delete mode 100644 vendor/github.com/influxdata/influxdb/tests/urlgen/urlgen.go delete mode 100644 vendor/github.com/influxdata/influxdb/toml/toml.go delete mode 100644 vendor/github.com/influxdata/influxdb/toml/toml_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/README.md delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/batcher.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/batcher_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/config.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/config_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/cursor.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/doc.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/engine.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/DESIGN.md delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/bit_reader.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/bit_reader_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/bool.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/bool_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/cache.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/cache_race_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/cache_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/compact.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/compact_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/cursor.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/encoding.gen.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/encoding.gen.go.tmpl delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/encoding.gen.go.tmpldata delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/encoding.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/encoding_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/engine.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/engine_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/file_store.gen.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/file_store.gen.go.tmpl delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/file_store.gen.go.tmpldata delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/file_store.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/file_store_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/file_unix.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/file_windows.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/float.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/float_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/int.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/int_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/iterator.gen.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/iterator.gen.go.tmpl delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/iterator.gen.go.tmpldata delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/iterator.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/mmap_solaris.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/mmap_unix.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/mmap_windows.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/pools.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/reader.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/reader_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/ring.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/ring_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/string.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/string_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/timestamp.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/timestamp_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/tombstone.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/tombstone_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/wal.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/wal_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/writer.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/engine/tsm1/writer_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/internal/meta.pb.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/internal/meta.proto delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/meta.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/meta_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/shard.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/shard_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/store.go delete mode 100644 vendor/github.com/influxdata/influxdb/tsdb/store_test.go delete mode 100644 vendor/github.com/influxdata/influxdb/uuid/uuid.go diff --git a/vendor/github.com/influxdata/influxdb/.dockerignore b/vendor/github.com/influxdata/influxdb/.dockerignore deleted file mode 100644 index 378eac25..00000000 --- a/vendor/github.com/influxdata/influxdb/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -build diff --git a/vendor/github.com/influxdata/influxdb/.github/ISSUE_TEMPLATE.md b/vendor/github.com/influxdata/influxdb/.github/ISSUE_TEMPLATE.md deleted file mode 100644 index 65cf3640..00000000 --- a/vendor/github.com/influxdata/influxdb/.github/ISSUE_TEMPLATE.md +++ /dev/null @@ -1,54 +0,0 @@ -### Directions -_GitHub Issues are reserved for actionable bug reports and feature requests._ -_General questions should be sent to the [InfluxDB mailing list](https://groups.google.com/forum/#!forum/influxdb)._ - -_Before opening an issue, search for similar bug reports or feature requests on GitHub Issues._ -_If no similar issue can be found, fill out either the "Bug Report" or the "Feature Request" section below. -_Erase the other section and everything on and above this line._ - -### Bug report - -__System info:__ [Include InfluxDB version, operating system name, and other relevant details] - -__Steps to reproduce:__ - -1. [First Step] -2. [Second Step] -3. [and so on...] - -__Expected behavior:__ [What you expected to happen] - -__Actual behavior:__ [What actually happened] - -__Additional info:__ [Include gist of relevant config, logs, etc.] - -Also, if this is an issue of for performance, locking, etc the following commands are useful to create debug information for the team. - -``` -curl -o block.txt "http://localhost:8086/debug/pprof/block?debug=1" -curl -o goroutine.txt "http://localhost:8086/debug/pprof/goroutine?debug=1" -curl -o heap.txt "http://localhost:8086/debug/pprof/heap?debug=1" -curl -o vars.txt "http://localhost:8086/debug/vars" -iostat -xd 1 30 > iostat.txt -influx -execute "show shards" > shards.txt -influx -execute "show stats" > stats.txt -influx -execute "show diagnostics" > diagnostics.txt -``` - -Please run those if possible and link them from a [gist](http://gist.github.com). - -*Please note, the quickest way to fix a bug is to open a Pull Request.* - - -### Feature Request - -Opening a feature request kicks off a discussion. -Requests may be closed if we're not actively planning to work on them. - -__Proposal:__ [Description of the feature] - -__Current behavior:__ [What currently happens] - -__Desired behavior:__ [What you would like to happen] - -__Use case:__ [Why is this important (helps with prioritizing requests)] diff --git a/vendor/github.com/influxdata/influxdb/.github/PULL_REQUEST_TEMPLATE.md b/vendor/github.com/influxdata/influxdb/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b2416b05..00000000 --- a/vendor/github.com/influxdata/influxdb/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,12 +0,0 @@ -###### Required for all non-trivial PRs -- [ ] Rebased/mergable -- [ ] Tests pass -- [ ] CHANGELOG.md updated -- [ ] Sign [CLA](https://influxdata.com/community/cla/) (if not already signed) - -###### Required only if applicable -_You can erase any checkboxes below this note if they are not applicable to your Pull Request._ -- [ ] [InfluxQL Spec](https://github.com/influxdata/influxdb/blob/master/influxql/README.md) updated -- [ ] Provide example syntax -- [ ] Update man page when modifying a command -- [ ] [InfluxData Documentation](https://github.com/influxdata/docs.influxdata.com): issue filed or pull request submitted \ diff --git a/vendor/github.com/influxdata/influxdb/.gitignore b/vendor/github.com/influxdata/influxdb/.gitignore deleted file mode 100644 index 6c13a672..00000000 --- a/vendor/github.com/influxdata/influxdb/.gitignore +++ /dev/null @@ -1,73 +0,0 @@ -# Keep editor-specific, non-project specific ignore rules in global .gitignore: -# https://help.github.com/articles/ignoring-files/#create-a-global-gitignore - -*~ -src/ - -config.json -/bin/ - -/query/a.out* - -# ignore generated files. -cmd/influxd/version.go - -# executables - -*.test - -influx_tsm -**/influx_tsm -!**/influx_tsm/ - -influx_stress -**/influx_stress -!**/influx_stress/ - -influxd -**/influxd -!**/influxd/ - -influx -**/influx -!**/influx/ - -influxdb -**/influxdb -!**/influxdb/ - -influx_inspect -**/influx_inspect -!**/influx_inspect/ - -/benchmark-tool -/main -/benchmark-storage -godef -gosym -gocode -inspect-raft - -# dependencies -out_rpm/ -packages/ - -# autconf -autom4te.cache/ -config.log -config.status - -# log file -influxdb.log -benchmark.log - -# config file -config.toml - -# test data files -integration/migration_data/ - -# man outputs -man/*.xml -man/*.1 -man/*.1.gz diff --git a/vendor/github.com/influxdata/influxdb/.hooks/pre-commit b/vendor/github.com/influxdata/influxdb/.hooks/pre-commit deleted file mode 100755 index 6cf240bf..00000000 --- a/vendor/github.com/influxdata/influxdb/.hooks/pre-commit +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash - -fmtcount=`git ls-files | grep '.go$' | xargs gofmt -l 2>&1 | wc -l` -if [ $fmtcount -gt 0 ]; then - echo "Some files aren't formatted, please run 'go fmt ./...' to format your source code before committing" - exit 1 -fi - -vetcount=`go tool vet ./ 2>&1 | wc -l` -if [ $vetcount -gt 0 ]; then - echo "Some files aren't passing vet heuristics, please run 'go vet ./...' to see the errors it flags and correct your source code before committing" - exit 1 -fi -exit 0 - -# Ensure FIXME lines are removed before commit. -fixme_lines=$(git diff --cached | grep ^+ | grep -v pre-commit | grep FIXME | sed 's_^+\s*__g') -if [ "$fixme_lines" != "" ]; then - echo "Please remove the following lines:" - echo -e "$fixme_lines" - exit 1 -fi - diff --git a/vendor/github.com/influxdata/influxdb/.mention-bot b/vendor/github.com/influxdata/influxdb/.mention-bot deleted file mode 100644 index 5f8689bb..00000000 --- a/vendor/github.com/influxdata/influxdb/.mention-bot +++ /dev/null @@ -1,6 +0,0 @@ -{ - "maxReviewers": 3, - "fileBlacklist": ["CHANGELOG.md"], - "userBlacklist": ["pauldix", "toddboom", "aviau", "mark-rushakoff"], - "requiredOrgs": ["influxdata"] -} diff --git a/vendor/github.com/influxdata/influxdb/CHANGELOG.md b/vendor/github.com/influxdata/influxdb/CHANGELOG.md deleted file mode 100644 index 00fcf94d..00000000 --- a/vendor/github.com/influxdata/influxdb/CHANGELOG.md +++ /dev/null @@ -1,2470 +0,0 @@ -## v1.2.0 [unreleased] - -### Release Notes - -### Deprecations - -The stress tool `influx_stress` will be removed in a subsequent release. We recommend using [`influx-stress`](https://github.com/influxdata/influx-stress) as a replacement. - -### Features - -- [#7723](https://github.com/influxdata/influxdb/pull/7723): Remove the override of GOMAXPROCS. -- [#7669](https://github.com/influxdata/influxdb/issues/7669): Uncomment section headers from the default configuration file. -- [#7633](https://github.com/influxdata/influxdb/pull/7633): improve write performance significantly. -- [#7601](https://github.com/influxdata/influxdb/issues/7601): Prune data in meta store for deleted shards. -- [#7554](https://github.com/influxdata/influxdb/pull/7554): update latest dependencies with Godeps. -- [#7368](https://github.com/influxdata/influxdb/pull/7368): Introduce syntax for marking a partial response with chunking. -- [#7356](https://github.com/influxdata/influxdb/issues/7356): Use X-Forwarded-For IP address in HTTP logger if present. -- [#7066](https://github.com/influxdata/influxdb/issues/7066): Add support for secure transmission via collectd. -- [#7036](https://github.com/influxdata/influxdb/issues/7036): Switch logging to use structured logging everywhere. -- [#3188](https://github.com/influxdata/influxdb/issues/3188): [CLI feature request] USE retention policy for queries. -- [#7709](https://github.com/influxdata/influxdb/pull/7709): Add clear command to cli. - -### Bugfixes - -- [#7741](https://github.com/influxdata/influxdb/pull/7741): Fix string quoting and significantly improve performance of `influx_inspect export`. -- [#7698](https://github.com/influxdata/influxdb/pull/7698): CLI was caching db/rp for insert into statements. -- [#7659](https://github.com/influxdata/influxdb/issues/7659): Fix CLI import bug when using self-signed SSL certificates. -- [#7656](https://github.com/influxdata/influxdb/issues/7656): Fix cross-platform backup/restore @allenpetersen -- [#7650](https://github.com/influxdata/influxdb/issues/7650): Ensures that all user privileges associated with a database are removed when the database is dropped. -- [#7634](https://github.com/influxdata/influxdb/issues/7634): Return the time from a percentile call on an integer. -- [#7621](https://github.com/influxdata/influxdb/issues/7621): Expand string and boolean fields when using a wildcard with `sample()`. -- [#7616](https://github.com/influxdata/influxdb/pull/7616): Fix chuid argument order in init script @ccasey -- [#7615](https://github.com/influxdata/influxdb/issues/7615): Reject invalid subscription urls @allenpetersen -- [#7396](https://github.com/influxdata/influxdb/issues/7396): CLI should use spaces for alignment, not tabs. -- [#6527](https://github.com/influxdata/influxdb/issues/6527): 0.12.2 Influx CLI client PRECISION returns "Unknown precision.... -- [#7740](https://github.com/influxdata/influxdb/issues/7740): Fix parse key panic when missing tag value @oiooj -- [#7563](https://github.com/influxdata/influxdb/issues/7563): RP should not allow `INF` or `0` as a shard duration. - -## v1.1.1 [2016-12-06] - -### Features - -- [#7684](https://github.com/influxdata/influxdb/issues/7684): Update Go version to 1.7.4. - -### Bugfixes - -- [#7679](https://github.com/influxdata/influxdb/pull/7679): Fix string fields w/ trailing slashes -- [#7661](https://github.com/influxdata/influxdb/pull/7661): Quote the empty string as an ident. -- [#7625](https://github.com/influxdata/influxdb/issues/7625): Fix incorrect tag value in error message. - -### Security - -[Go 1.7.4](https://golang.org/doc/devel/release.html#go1.7.minor) was released to address two security issues. This release includes these security fixes. - -## v1.1.0 [2016-11-14] - -### Release Notes - -This release is built with go 1.7.3 and provides many performance optimizations, stability changes and a few new query capabilities. If upgrading from a prior version, please read the configuration changes below section before upgrading. - -### Deprecations - -The admin interface is deprecated and will be removed in a subsequent release. The configuration setting to enable the admin UI is now disabled by default, but can be enabled if necessary. We recommend using [Chronograf](https://github.com/influxdata/chronograf) or [Grafana](https://github.com/grafana/grafana) as a replacement. - -### Configuration Changes - -The following configuration changes may need to changed before upgrading to `1.1.0` from prior versions. - -#### `[admin]` Section - -* `enabled` now default to false. If you are currently using the admin interaface, you will need to change this value to `true` to re-enable it. The admin interface is currently deprecated and will be removed in a subsequent release. - -#### `[data]` Section - -* `max-values-per-tag` was added with a default of 100,000, but can be disabled by setting it to `0`. Existing measurements with tags that exceed this limit will continue to load, but writes that would cause the tags cardinality to increase will be dropped and a `partial write` error will be returned to the caller. This limit can be used to prevent high cardinality tag values from being written to a measurement. -* `cache-max-memory-size` has been increased to from `524288000` to `1048576000`. This setting is the maximum amount of RAM, in bytes, a shard cache can use before it rejects writes with an error. Setting this value to `0` disables the limit. -* `cache-snapshot-write-cold-duration` has been decreased from `1h` to `10m`. This setting determines how long values will stay in the shard cache while the shard is cold for writes. -* `compact-full-write-cold-duration` has been decreased from `24h` to `4h`. The shorter duration allows cold shards to be compacted to an optimal state more quickly. - -### Features - -The query language has been extended with a few new features: - -- [#7442](https://github.com/influxdata/influxdb/pull/7442): Support regex on fields keys in select clause -- [#7403](https://github.com/influxdata/influxdb/pull/7403): New `linear` fill option -- [#7388](https://github.com/influxdata/influxdb/pull/7388): New `cumulative_sum` function -- [#7295](https://github.com/influxdata/influxdb/pull/7295): Support `ON` for `SHOW` commands - - -All Changes: - -- [#7496](https://github.com/influxdata/influxdb/pull/7496): Filter out series within shards that do not have data for that series. -- [#7495](https://github.com/influxdata/influxdb/pull/7495): Rewrite regexes of the form host = /^server-a$/ to host = 'server-a', to take advantage of the tsdb index. -- [#7480](https://github.com/influxdata/influxdb/pull/7480): Improve compaction planning performance by caching tsm file stats. -- [#7473](https://github.com/influxdata/influxdb/pull/7473): Align binary math expression streams by time. -- [#7470](https://github.com/influxdata/influxdb/pull/7470): Reduce map allocations when computing the TagSet of a measurement. -- [#7463](https://github.com/influxdata/influxdb/pull/7463): Make input plugin services open/close idempotent. -- [#7441](https://github.com/influxdata/influxdb/pull/7441): Speed up shutdown by closing shards concurrently. -- [#7415](https://github.com/influxdata/influxdb/pull/7415): Add sample function to query language. -- [#7403](https://github.com/influxdata/influxdb/pull/7403): Add `fill(linear)` to query language. -- [#7388](https://github.com/influxdata/influxdb/pull/7388): Implement cumulative_sum() function. -- [#7320](https://github.com/influxdata/influxdb/issues/7320): Update defaults in config for latest best practices -- [#7305](https://github.com/influxdata/influxdb/pull/7305): UDP Client: Split large points. Thanks @vlasad -- [#7281](https://github.com/influxdata/influxdb/pull/7281): Add stats for active compactions, compaction errors. -- [#7268](https://github.com/influxdata/influxdb/pull/7268): More man pages for the other tools we package and compress man pages fully. -- [#7146](https://github.com/influxdata/influxdb/issues/7146): Add max-values-per-tag to limit high tag cardinality data -- [#7136](https://github.com/influxdata/influxdb/pull/7136): Update jwt-go dependency to version 3. -- [#7135](https://github.com/influxdata/influxdb/pull/7135): Support enable HTTP service over unix domain socket. Thanks @oiooj -- [#7120](https://github.com/influxdata/influxdb/issues/7120): Add additional statistics to query executor. -- [#7115](https://github.com/influxdata/influxdb/issues/7115): Feature request: `influx inspect -export` should dump WAL files. -- [#7099](https://github.com/influxdata/influxdb/pull/7099): Implement text/csv content encoding for the response writer. -- [#6992](https://github.com/influxdata/influxdb/issues/6992): Support tools for running async queries. -- [#6962](https://github.com/influxdata/influxdb/issues/6962): Support ON and use default database for SHOW commands. -- [#6896](https://github.com/influxdata/influxdb/issues/6896): Correctly read in input from a non-interactive stream for the CLI. -- [#6894](https://github.com/influxdata/influxdb/issues/6894): Support `INFLUX_USERNAME` and `INFLUX_PASSWORD` for setting username/password in the CLI. -- [#6704](https://github.com/influxdata/influxdb/issues/6704): Optimize first/last when no group by interval is present. -- [#5955](https://github.com/influxdata/influxdb/issues/5955): Make regex work on field and dimension keys in SELECT clause. -- [#4461](https://github.com/influxdata/influxdb/issues/4461): Change default time boundaries for raw queries. -- [#3634](https://github.com/influxdata/influxdb/issues/3634): Support mixed duration units. - -### Bugfixes - -- [#7606](https://github.com/influxdata/influxdb/pull/7606): Avoid deadlock when `max-row-limit` is hit. -- [#7564](https://github.com/influxdata/influxdb/issues/7564): Fix incorrect grouping when multiple aggregates are used with sparse data. -- [#7548](https://github.com/influxdata/influxdb/issues/7548): Fix output duration units for SHOW QUERIES. -- [#7526](https://github.com/influxdata/influxdb/issues/7526): Truncate the version string when linking to the documentation. -- [#7494](https://github.com/influxdata/influxdb/issues/7494): influx_inspect: export does not escape field keys. -- [#7482](https://github.com/influxdata/influxdb/issues/7482): Fix issue where point would be written to wrong shard. -- [#7448](https://github.com/influxdata/influxdb/pull/7448): Fix Retention Policy Inconsistencies -- [#7436](https://github.com/influxdata/influxdb/issues/7436): Remove accidentally added string support for the stddev call. -- [#7431](https://github.com/influxdata/influxdb/issues/7431): Remove /data/process_continuous_queries endpoint. -- [#7392](https://github.com/influxdata/influxdb/pull/7392): Enable https subscriptions to work with custom CA certificates. -- [#7385](https://github.com/influxdata/influxdb/pull/7385): Reduce query planning allocations -- [#7382](https://github.com/influxdata/influxdb/issues/7382): Shard stats include wal path tag so disk bytes make more sense. -- [#7334](https://github.com/influxdata/influxdb/issues/7334): Panic with unread show series iterators during drop database -- [#7297](https://github.com/influxdata/influxdb/issues/7297): Use consistent column output from the CLI for column formatted responses. -- [#7285](https://github.com/influxdata/influxdb/issues/7285): Correctly use password-type field in Admin UI. Thanks @dandv! -- [#7231](https://github.com/influxdata/influxdb/issues/7231): Duplicate parsing bug in ALTER RETENTION POLICY. -- [#7226](https://github.com/influxdata/influxdb/issues/7226): Fix database locked up when deleting shards -- [#7196](https://github.com/influxdata/influxdb/issues/7196): Fix mmap dereferencing, fixes #7183, #7180 -- [#7177](https://github.com/influxdata/influxdb/issues/7177): Fix base64 encoding issue with /debug/vars stats. -- [#7161](https://github.com/influxdata/influxdb/issues/7161): Drop measurement causes cache max memory exceeded error. -- [#7152](https://github.com/influxdata/influxdb/issues/7152): Decrement number of measurements only once when deleting the last series from a measurement. -- [#7053](https://github.com/influxdata/influxdb/issues/7053): Delete statement returns an error when retention policy or database is specified -- [#7013](https://github.com/influxdata/influxdb/issues/7013): Fix the dollar sign so it properly handles reserved keywords. -- [#2792](https://github.com/influxdata/influxdb/issues/2792): Exceeding max retention policy duration gives incorrect error message -- [#1834](https://github.com/influxdata/influxdb/issues/1834): Drop time when used as a tag or field key. - -## v1.0.2 [2016-10-05] - -### Bugfixes - -- [#7391](https://github.com/influxdata/influxdb/issues/7391): Fix RLE integer decoding producing negative numbers -- [#7335](https://github.com/influxdata/influxdb/pull/7335): Avoid stat syscall when planning compactions -- [#7330](https://github.com/influxdata/influxdb/issues/7330): Subscription data loss under high write load -- [#7150](https://github.com/influxdata/influxdb/issues/7150): Do not automatically reset the shard duration when using ALTER RETENTION POLICY -- [#5878](https://github.com/influxdata/influxdb/issues/5878): Ensure correct shard groups created when retention policy has been altered. - -## v1.0.1 [2016-09-26] - -### Bugfixes - -- [#7315](https://github.com/influxdata/influxdb/issues/7315): Prevent users from manually using system queries since incorrect use would result in a panic. -- [#7299](https://github.com/influxdata/influxdb/issues/7299): Ensure fieldsCreated stat available in shard measurement. -- [#7272](https://github.com/influxdata/influxdb/issues/7272): Report cmdline and memstats in /debug/vars. -- [#7271](https://github.com/influxdata/influxdb/issues/7271): Fixing typo within example configuration file. Thanks @andyfeller! -- [#7270](https://github.com/influxdata/influxdb/issues/7270): Implement time math for lazy time literals. -- [#7226](https://github.com/influxdata/influxdb/issues/7226): Fix database locked up when deleting shards -- [#7110](https://github.com/influxdata/influxdb/issues/7110): Skip past points at the same time in derivative call within a merged series. -- [#6846](https://github.com/influxdata/influxdb/issues/6846): Read an invalid JSON response as an error in the influx client. - -## v1.0.0 [2016-09-08] - -### Release Notes - -### Breaking changes - -* `max-series-per-database` was added with a default of 1M but can be disabled by setting it to `0`. Existing databases with series that exceed this limit will continue to load but writes that would create new series will fail. -* Config option `[cluster]` has been replaced with `[coordinator]` -* Support for config options `[collectd]` and `[opentsdb]` has been removed; use `[[collectd]]` and `[[opentsdb]]` instead. -* Config option `data-logging-enabled` within the `[data]` section, has been renamed to `trace-logging-enabled`, and defaults to `false`. -* The keywords `IF`, `EXISTS`, and `NOT` where removed for this release. This means you no longer need to specify `IF NOT EXISTS` for `DROP DATABASE` or `IF EXISTS` for `CREATE DATABASE`. If these are specified, a query parse error is returned. -* The Shard `writePointsFail` stat has been renamed to `writePointsErr` for consistency with other stats. - -With this release the systemd configuration files for InfluxDB will use the system configured default for logging and will no longer write files to `/var/log/influxdb` by default. On most systems, the logs will be directed to the systemd journal and can be accessed by `journalctl -u influxdb.service`. Consult the systemd journald documentation for configuring journald. - -### Features - -- [#7199](https://github.com/influxdata/influxdb/pull/7199): Add mode function. Thanks @agaurav. -- [#7194](https://github.com/influxdata/influxdb/issues/7194): Support negative timestamps for the query engine. -- [#7172](https://github.com/influxdata/influxdb/pull/7172): Write path stats -- [#7095](https://github.com/influxdata/influxdb/pull/7095): Add MaxSeriesPerDatabase config setting. -- [#7065](https://github.com/influxdata/influxdb/issues/7065): Remove IF EXISTS/IF NOT EXISTS from influxql language. -- [#7050](https://github.com/influxdata/influxdb/pull/7050): Update go package library dependencies. -- [#7046](https://github.com/influxdata/influxdb/pull/7046): Add tsm file export to influx_inspect tool. -- [#7011](https://github.com/influxdata/influxdb/issues/7011): Create man pages for commands. -- [#6959](https://github.com/influxdata/influxdb/issues/6959): Return 403 Forbidden when authentication succeeds but authorization fails. -- [#6938](https://github.com/influxdata/influxdb/issues/6938): Added favicon -- [#6928](https://github.com/influxdata/influxdb/issues/6928): Run continuous query for multiple buckets rather than one per bucket. -- [#6909](https://github.com/influxdata/influxdb/issues/6909): Log the CQ execution time when continuous query logging is enabled. -- [#6900](https://github.com/influxdata/influxdb/pull/6900): Trim BOM from Windows Notepad-saved config files. -- [#6889](https://github.com/influxdata/influxdb/pull/6889): Update help and remove unused config options from the configuration file. -- [#6820](https://github.com/influxdata/influxdb/issues/6820): Add NodeID to execution options -- [#6812](https://github.com/influxdata/influxdb/pull/6812): Make httpd logger closer to Common (& combined) Log Format. -- [#6805](https://github.com/influxdata/influxdb/issues/6805): Allow any variant of the help option to trigger the help. -- [#6713](https://github.com/influxdata/influxdb/pull/6713): Reduce allocations during query parsing. -- [#6686](https://github.com/influxdata/influxdb/pull/6686): Optimize timestamp run-length decoding -- [#6664](https://github.com/influxdata/influxdb/pull/6664): Adds monitoring statistic for on-disk shard size. -- [#6655](https://github.com/influxdata/influxdb/issues/6655): Add HTTP(s) based subscriptions. -- [#6654](https://github.com/influxdata/influxdb/pull/6654): Add new HTTP statistics to monitoring -- [#6623](https://github.com/influxdata/influxdb/pull/6623): Speed up drop database -- [#6621](https://github.com/influxdata/influxdb/pull/6621): Add Holt-Winter forecasting function. -- [#6609](https://github.com/influxdata/influxdb/pull/6609): Add support for JWT token authentication. -- [#6593](https://github.com/influxdata/influxdb/pull/6593): Add ability to create snapshots of shards. -- [#6585](https://github.com/influxdata/influxdb/pull/6585): Parallelize iterators -- [#6559](https://github.com/influxdata/influxdb/issues/6559): Teach the http service how to enforce connection limits. -- [#6519](https://github.com/influxdata/influxdb/issues/6519): Support cast syntax for selecting a specific type. -- [#6507](https://github.com/influxdata/influxdb/issues/6507): Refactor monitor service to avoid expvar and write monitor statistics on a truncated time interval. -- [#5906](https://github.com/influxdata/influxdb/issues/5906): Dynamically update the documentation link in the admin UI. -- [#5750](https://github.com/influxdata/influxdb/issues/5750): Support wildcards in aggregate functions. -- [#5655](https://github.com/influxdata/influxdb/issues/5655): Support specifying a retention policy for the graphite service. -- [#5500](https://github.com/influxdata/influxdb/issues/5500): Add extra trace logging to tsm engine. -- [#5499](https://github.com/influxdata/influxdb/issues/5499): Add stats and diagnostics to the TSM engine. -- [#4532](https://github.com/influxdata/influxdb/issues/4532): Support regex selection in SHOW TAG VALUES for the key. -- [#3733](https://github.com/influxdata/influxdb/issues/3733): Modify the default retention policy name and make it configurable. -- [#3541](https://github.com/influxdata/influxdb/issues/3451): Update SHOW FIELD KEYS to return the field type with the field key. -- [#2926](https://github.com/influxdata/influxdb/issues/2926): Support bound parameters in the parser. -- [#1310](https://github.com/influxdata/influxdb/issues/1310): Add https-private-key option to httpd config. -- [#1110](https://github.com/influxdata/influxdb/issues/1110): Support loading a folder for collectd typesdb files. - -### Bugfixes - -- [#7243](https://github.com/influxdata/influxdb/issues/7243): Optimize queries that compare a tag value to an empty string. -- [#7240](https://github.com/influxdata/influxdb/issues/7240): Allow blank lines in the line protocol input. -- [#7225](https://github.com/influxdata/influxdb/issues/7225): runtime: goroutine stack exceeds 1000000000-byte limit -- [#7218](https://github.com/influxdata/influxdb/issues/7218): Fix alter retention policy when all options are used. -- [#7127](https://github.com/influxdata/influxdb/pull/7127): Concurrent series limit -- [#7125](https://github.com/influxdata/influxdb/pull/7125): Ensure gzip writer is closed in influx_inspect export -- [#7119](https://github.com/influxdata/influxdb/pull/7119): Fix CREATE DATABASE when dealing with default values. -- [#7088](https://github.com/influxdata/influxdb/pull/7088): Fix UDP pointsRx being incremented twice. -- [#7084](https://github.com/influxdata/influxdb/pull/7084): Tombstone memory improvements -- [#7081](https://github.com/influxdata/influxdb/issues/7081): Hardcode auto generated RP names to autogen -- [#7080](https://github.com/influxdata/influxdb/pull/7080): Ensure IDs can't clash when managing Continuous Queries. -- [#7074](https://github.com/influxdata/influxdb/issues/7074): Continuous full compactions -- [#7043](https://github.com/influxdata/influxdb/pull/7043): Remove limiter from walkShards -- [#7032](https://github.com/influxdata/influxdb/pull/7032): Copy tags in influx_stress to avoid a concurrent write panic on a map. -- [#7028](https://github.com/influxdata/influxdb/pull/7028): Do not run continuous queries that have no time span. -- [#7025](https://github.com/influxdata/influxdb/issues/7025): Move the CQ interval by the group by offset. -- [#6990](https://github.com/influxdata/influxdb/issues/6990): Fix panic parsing empty key -- [#6986](https://github.com/influxdata/influxdb/pull/6986): update connection settings when changing hosts in cli. -- [#6968](https://github.com/influxdata/influxdb/issues/6968): Always use the demo config when outputting a new config. -- [#6965](https://github.com/influxdata/influxdb/pull/6965): Minor improvements to init script. Removes sysvinit-utils as package dependency. -- [#6952](https://github.com/influxdata/influxdb/pull/6952): Fix compaction planning with large TSM files -- [#6946](https://github.com/influxdata/influxdb/issues/6946): Duplicate data for the same timestamp -- [#6942](https://github.com/influxdata/influxdb/pull/6942): Fix panic: truncate the slice when merging the caches. -- [#6934](https://github.com/influxdata/influxdb/pull/6934): Fix regex binary encoding for a measurement. -- [#6911](https://github.com/influxdata/influxdb/issues/6911): Fix fill(previous) when used with math operators. -- [#6883](https://github.com/influxdata/influxdb/pull/6883): Rename dumptsmdev to dumptsm in influx_inspect. -- [#6882](https://github.com/influxdata/influxdb/pull/6882): Remove a double lock in the tsm1 index writer. -- [#6869](https://github.com/influxdata/influxdb/issues/6869): Remove FieldCodec from tsdb package. -- [#6864](https://github.com/influxdata/influxdb/pull/6864): Allow a non-admin to call "use" for the influx cli. -- [#6859](https://github.com/influxdata/influxdb/issues/6859): Set the condition cursor instead of aux iterator when creating a nil condition cursor. -- [#6855](https://github.com/influxdata/influxdb/pull/6855): Update `stress/v2` to work with clusters, ssl, and username/password auth. Code cleanup -- [#6850](https://github.com/influxdata/influxdb/pull/6850): Modify the max nanosecond time to be one nanosecond less. -- [#6835](https://github.com/influxdata/influxdb/pull/6835): Include sysvinit-tools as an rpm dependency. -- [#6834](https://github.com/influxdata/influxdb/pull/6834): Add port to all graphite log output to help with debugging multiple endpoints -- [#6829](https://github.com/influxdata/influxdb/issues/6829): Fix panic: runtime error: index out of range -- [#6824](https://github.com/influxdata/influxdb/issues/6824): Remove systemd output redirection. -- [#6819](https://github.com/influxdata/influxdb/issues/6819): Database unresponsive after DROP MEASUREMENT -- [#6796](https://github.com/influxdata/influxdb/issues/6796): Out of Memory Error when Dropping Measurement -- [#6771](https://github.com/influxdata/influxdb/issues/6771): Fix the point validation parser to identify and sort tags correctly. -- [#6760](https://github.com/influxdata/influxdb/issues/6760): Prevent panic in concurrent auth cache write -- [#6756](https://github.com/influxdata/influxdb/issues/6756): Set X-Influxdb-Version header on every request (even 404 requests). -- [#6753](https://github.com/influxdata/influxdb/issues/6753): Prevent panic if there are no values. -- [#6738](https://github.com/influxdata/influxdb/issues/6738): Time sorting broken with overwritten points -- [#6727](https://github.com/influxdata/influxdb/issues/6727): queries with strings that look like dates end up with date types, not string types -- [#6720](https://github.com/influxdata/influxdb/issues/6720): Concurrent map read write panic. Thanks @arussellsaw -- [#6708](https://github.com/influxdata/influxdb/issues/6708): Drop writes from before the retention policy time window. -- [#6702](https://github.com/influxdata/influxdb/issues/6702): Fix SELECT statement required privileges. -- [#6701](https://github.com/influxdata/influxdb/issues/6701): Filter out sources that do not match the shard database/retention policy. -- [#6693](https://github.com/influxdata/influxdb/pull/6693): Truncate the shard group end time if it exceeds MaxNanoTime. -- [#6685](https://github.com/influxdata/influxdb/issues/6685): Batch SELECT INTO / CQ writes -- [#6683](https://github.com/influxdata/influxdb/issues/6683): Fix compaction planning re-compacting large TSM files -- [#6676](https://github.com/influxdata/influxdb/issues/6676): Ensures client sends correct precision when inserting points. -- [#6672](https://github.com/influxdata/influxdb/issues/6672): Accept points with trailing whitespace. -- [#6663](https://github.com/influxdata/influxdb/issues/6663): Fixing panic in SHOW FIELD KEYS. -- [#6661](https://github.com/influxdata/influxdb/issues/6661): Disable limit optimization when using an aggregate. -- [#6652](https://github.com/influxdata/influxdb/issues/6652): Fix panic: interface conversion: tsm1.Value is \*tsm1.StringValue, not \*tsm1.FloatValue -- [#6650](https://github.com/influxdata/influxdb/issues/6650): Data race when dropping a database immediately after writing to it -- [#6648](https://github.com/influxdata/influxdb/issues/6648): Make sure admin exists before authenticating query. -- [#6644](https://github.com/influxdata/influxdb/issues/6644): Print the query executor's stack trace on a panic to the log. -- [#6641](https://github.com/influxdata/influxdb/issues/6641): Fix read tombstones: EOF -- [#6629](https://github.com/influxdata/influxdb/issues/6629): query-log-enabled in config not ignored anymore. -- [#6624](https://github.com/influxdata/influxdb/issues/6624): Ensure clients requesting gzip encoded bodies don't receive empty body -- [#6618](https://github.com/influxdata/influxdb/pull/6618): Optimize shard loading -- [#6611](https://github.com/influxdata/influxdb/issues/6611): Queries slow down hundreds times after overwriting points -- [#6607](https://github.com/influxdata/influxdb/issues/6607): SHOW TAG VALUES accepts != and !~ in WHERE clause. -- [#6604](https://github.com/influxdata/influxdb/pull/6604): Remove old cluster code -- [#6599](https://github.com/influxdata/influxdb/issues/6599): Ensure that future points considered in SHOW queries. -- [#6595](https://github.com/influxdata/influxdb/issues/6595): Fix full compactions conflicting with level compactions -- [#6557](https://github.com/influxdata/influxdb/issues/6557): Overwriting points on large series can cause memory spikes during compactions -- [#6543](https://github.com/influxdata/influxdb/issues/6543): Fix parseFill to check for fill ident before attempting to parse an expression. -- [#6406](https://github.com/influxdata/influxdb/issues/6406): Max index entries exceeded -- [#6250](https://github.com/influxdata/influxdb/issues/6250): Slow startup time -- [#6235](https://github.com/influxdata/influxdb/issues/6235): Fix measurement field panic in tsm1 engine. -- [#5501](https://github.com/influxdata/influxdb/issues/5501): Queries against files that have just been compacted need to point to new files -- [#2048](https://github.com/influxdata/influxdb/issues/2048): Check that retention policies exist before creating CQ - -## v0.13.0 [2016-05-12] - -### Release Notes - -With this release InfluxDB is moving to Go v1.6. - -### Features - -- [#6534](https://github.com/influxdata/influxdb/pull/6534): Move to Go v1.6.2 (over Go v1.4.3) -- [#6533](https://github.com/influxdata/influxdb/issues/6533): Optimize SHOW SERIES -- [#6522](https://github.com/influxdata/influxdb/pull/6522): Dump TSM files to line protocol -- [#6502](https://github.com/influxdata/influxdb/pull/6502): Add ability to copy shard via rpc calls. Remove deprecated copier service. -- [#6494](https://github.com/influxdata/influxdb/issues/6494): Support booleans for min() and max(). -- [#6484](https://github.com/influxdata/influxdb/pull/6484): Query language support for DELETE -- [#6483](https://github.com/influxdata/influxdb/pull/6483): Delete series support for TSM -- [#6444](https://github.com/influxdata/influxdb/pull/6444): Allow setting the config path through an environment variable and default config path. -- [#6429](https://github.com/influxdata/influxdb/issues/6429): Log slow queries if they pass a configurable threshold. -- [#6394](https://github.com/influxdata/influxdb/pull/6394): Allow time math with integer timestamps. -- [#6334](https://github.com/influxdata/influxdb/pull/6334): Allow environment variables to be set per input type. -- [#6292](https://github.com/influxdata/influxdb/issues/6292): Allow percentile to be used as a selector. -- [#6290](https://github.com/influxdata/influxdb/issues/6290): Add POST /query endpoint and warning messages for using GET with write operations. -- [#6263](https://github.com/influxdata/influxdb/pull/6263): Reduce UDP Service allocation size. -- [#6237](https://github.com/influxdata/influxdb/issues/6237): Enable continuous integration testing on Windows platform via AppVeyor. Thanks @mvadu -- [#6228](https://github.com/influxdata/influxdb/pull/6228): Support for multiple listeners for collectd and OpenTSDB inputs. -- [#6213](https://github.com/influxdata/influxdb/pull/6213): Make logging output location more programmatically configurable. -- [#5707](https://github.com/influxdata/influxdb/issues/5707): Return a deprecated message when IF NOT EXISTS is used. -- [#5502](https://github.com/influxdata/influxdb/issues/5502): Add checksum verification to TSM inspect tool -- [#4675](https://github.com/influxdata/influxdb/issues/4675): Allow derivative() function to be used with ORDER BY desc. -- [#3558](https://github.com/influxdata/influxdb/issues/3558): Support field math inside a WHERE clause. -- [#3247](https://github.com/influxdata/influxdb/issues/3247): Implement derivatives across intervals for aggregate queries. -- [#3166](https://github.com/influxdata/influxdb/issues/3166): Sort the series keys inside of a tag set so output is deterministic. -- [#2074](https://github.com/influxdata/influxdb/issues/2074): Support offset argument in the GROUP BY time(...) call. -- [#1856](https://github.com/influxdata/influxdb/issues/1856): Add `elapsed` function that returns the time delta between subsequent points. - -### Bugfixes - -- [#6505](https://github.com/influxdata/influxdb/issues/6505): Add regex literal to InfluxQL spec for FROM clause. -- [#6496](https://github.com/influxdata/influxdb/issues/6496): Fix parsing escaped series key when loading database index -- [#6495](https://github.com/influxdata/influxdb/issues/6495): Fix aggregate returns when data is missing from some shards. -- [#6491](https://github.com/influxdata/influxdb/pull/6491): Fix the CLI not to enter an infinite loop when the liner has an error. -- [#6480](https://github.com/influxdata/influxdb/issues/6480): Fix SHOW statements' rewriting bug -- [#6477](https://github.com/influxdata/influxdb/pull/6477): Don't catch SIGQUIT or SIGHUP signals. -- [#6470](https://github.com/influxdata/influxdb/pull/6470): Remove SHOW SERVERS & DROP SERVER support -- [#6468](https://github.com/influxdata/influxdb/issues/6468): Panic with truncated wal segments -- [#6462](https://github.com/influxdata/influxdb/pull/6462): Add safer locking to CreateFieldIfNotExists -- [#6458](https://github.com/influxdata/influxdb/pull/6458): Make it clear when the CLI version is unknown. -- [#6457](https://github.com/influxdata/influxdb/issues/6457): Retention policy cleanup does not remove series -- [#6439](https://github.com/influxdata/influxdb/issues/6439): Overwriting points returning old values -- [#6427](https://github.com/influxdata/influxdb/pull/6427): Fix setting uint config options via env vars -- [#6425](https://github.com/influxdata/influxdb/pull/6425): Close idle tcp connections in HTTP client to prevent tcp conn leak. -- [#6419](https://github.com/influxdata/influxdb/issues/6419): Fix panic in transform iterator on division. @thbourlove -- [#6398](https://github.com/influxdata/influxdb/issues/6398): Fix CREATE RETENTION POLICY parsing so it doesn't consume tokens it shouldn't. -- [#6382](https://github.com/influxdata/influxdb/pull/6382): Removed dead code from the old query engine. -- [#6361](https://github.com/influxdata/influxdb/pull/6361): Fix cluster/pool release of connection -- [#6296](https://github.com/influxdata/influxdb/issues/6296): Allow the implicit time field to be renamed again. -- [#6294](https://github.com/influxdata/influxdb/issues/6294): Fix panic running influx_inspect info. -- [#6287](https://github.com/influxdata/influxdb/issues/6287): Fix data race in Influx Client. -- [#6283](https://github.com/influxdata/influxdb/pull/6283): Fix GROUP BY tag to produce consistent results when a series has no tags. -- [#6277](https://github.com/influxdata/influxdb/pull/6277): Fix deadlock in tsm1/file_store -- [#6270](https://github.com/influxdata/influxdb/issues/6270): tsm1 query engine alloc reduction -- [#6261](https://github.com/influxdata/influxdb/issues/6261): High CPU usage and slow query with DISTINCT -- [#6252](https://github.com/influxdata/influxdb/pull/6252): Remove TSDB listener accept message @simnv -- [#6202](https://github.com/influxdata/influxdb/pull/6202): Check default SHARD DURATION when recreating the same database. -- [#6109](https://github.com/influxdata/influxdb/issues/6109): Cache maximum memory size exceeded on startup -- [#5890](https://github.com/influxdata/influxdb/issues/5890): Return the time with a selector when there is no group by interval. -- [#3883](https://github.com/influxdata/influxdb/issues/3883): Improve query sanitization to prevent a password leak in the logs. -- [#3773](https://github.com/influxdata/influxdb/issues/3773): Support empty tags for all WHERE equality operations. -- [#3369](https://github.com/influxdata/influxdb/issues/3369): Detect when a timer literal will overflow or underflow the query engine. - -## v0.12.2 [2016-04-20] - -### Bugfixes - -- [#6431](https://github.com/influxdata/influxdb/pull/6431): Fix panic in transform iterator on division. @thbourlove -- [#6414](https://github.com/influxdata/influxdb/pull/6414): Send "Connection: close" header for queries. -- [#6413](https://github.com/influxdata/influxdb/pull/6413): Prevent goroutine leak from persistent http connections. Thanks @aaronknister. -- [#6383](https://github.com/influxdata/influxdb/pull/6383): Recover from a panic during query execution. -- [#6379](https://github.com/influxdata/influxdb/issues/6379): Validate the first argument to percentile() is a variable. -- [#6271](https://github.com/influxdata/influxdb/issues/6271): Fixed deadlock in tsm1 file store. - -## v0.12.1 [2016-04-08] - -### Bugfixes - -- [#6257](https://github.com/influxdata/influxdb/issues/6257): CreateShardGroup was incrementing meta data index even when it was idempotent. -- [#6248](https://github.com/influxdata/influxdb/issues/6248): Panic using incorrectly quoted "queries" field key. -- [#6229](https://github.com/influxdata/influxdb/issues/6229): Fixed aggregate queries with no GROUP BY to include the end time. -- [#6225](https://github.com/influxdata/influxdb/pull/6225): Refresh admin assets. -- [#6223](https://github.com/influxdata/influxdb/issues/6223): Failure to start/run on Windows. Thanks @mvadu -- [#6206](https://github.com/influxdata/influxdb/issues/6206): Handle nil values from the tsm1 cursor correctly. -- [#6190](https://github.com/influxdata/influxdb/pull/6190): Fix race on measurementFields. - - -## v0.12.0 [2016-04-05] -### Release Notes -Upgrading to this release requires a little more than just installing the new binary and starting it up. The upgrade process is very quick and should only require a minute of downtime or less. Details on [upgrading to 0.12 are here](https://docs.influxdata.com/influxdb/v0.12/administration/upgrading/). - -This release removes all of the old clustering code. It operates as a standalone server. For a free open source HA setup see the [InfluxDB Relay](https://github.com/influxdata/influxdb-relay). - -### Features - -- [#6193](https://github.com/influxdata/influxdb/pull/6193): Fix TypeError when processing empty results in admin UI. Thanks @jonseymour! -- [#6166](https://github.com/influxdata/influxdb/pull/6166): Teach influxdb client how to use chunked queries and use in the CLI. -- [#6158](https://github.com/influxdata/influxdb/pull/6158): Update influxd to detect an upgrade from `0.11` to `0.12`. Minor restore bug fixes. -- [#6149](https://github.com/influxdata/influxdb/pull/6149): Kill running queries when server is shutdown. -- [#6148](https://github.com/influxdata/influxdb/pull/6148): Build script is now compatible with Python 3. Added ability to create detached signatures for packages. Build script now uses Python logging facility for messages. -- [#6116](https://github.com/influxdata/influxdb/pull/6116): Allow `httpd` service to be extensible for routes -- [#6115](https://github.com/influxdata/influxdb/issues/6115): Support chunking query results mid-series. Limit non-chunked output. -- [#6112](https://github.com/influxdata/influxdb/issues/6112): Implement simple moving average function. -- [#6111](https://github.com/influxdata/influxdb/pull/6111): Add ability to build static assest. Improved handling of TAR and ZIP package outputs. -- [#6102](https://github.com/influxdata/influxdb/issues/6102): Limit series count in selection -- [#6079](https://github.com/influxdata/influxdb/issues/6079): Limit the maximum number of concurrent queries. -- [#6078](https://github.com/influxdata/influxdb/issues/6078): Limit bucket count in selection. -- [#6077](https://github.com/influxdata/influxdb/issues/6077): Limit point count in selection. -- [#6075](https://github.com/influxdata/influxdb/issues/6075): Limit the maximum running time of a query. -- [#6073](https://github.com/influxdata/influxdb/pull/6073): Iterator stats -- [#6060](https://github.com/influxdata/influxdb/pull/6060): Add configurable shard duration to retention policies -- [#6025](https://github.com/influxdata/influxdb/pull/6025): Remove deprecated JSON write path. -- [#6012](https://github.com/influxdata/influxdb/pull/6012): Add DROP SHARD support. -- [#5939](https://github.com/influxdata/influxdb/issues/5939): Support viewing and killing running queries. -- [#5744](https://github.com/influxdata/influxdb/issues/5744): Add integer literal support to the query language. -- [#5372](https://github.com/influxdata/influxdb/pull/5372): Faster shard loading -- [#1825](https://github.com/influxdata/influxdb/issues/1825): Implement difference function. - -### Bugfixes - -- [#6178](https://github.com/influxdata/influxdb/issues/6178): Ensure SHARD DURATION is checked when recreating a retention policy -- [#6153](https://github.com/influxdata/influxdb/issues/6153): Check SHARD DURATION when recreating the same database -- [#6152](https://github.com/influxdata/influxdb/issues/6152): Allow SHARD DURATION to be specified in isolation when creating a database -- [#6140](https://github.com/influxdata/influxdb/issues/6140): Ensure Shard engine not accessed when closed. -- [#6131](https://github.com/influxdata/influxdb/issues/6061): Fix write throughput regression with large number of measurments -- [#6110](https://github.com/influxdata/influxdb/issues/6110): Fix for 0.9 upgrade path when using RPM -- [#6094](https://github.com/influxdata/influxdb/issues/6094): Ensure CREATE RETENTION POLICY and CREATE CONTINUOUS QUERY are idempotent in the correct way. -- [#6065](https://github.com/influxdata/influxdb/pull/6065): Wait for a process termination on influxdb restart @simnv -- [#6061](https://github.com/influxdata/influxdb/issues/6061): [0.12 / master] POST to /write does not write points if request has header 'Content-Type: application/x-www-form-urlencoded' -- [#5728](https://github.com/influxdata/influxdb/issues/5728): Properly handle semi-colons as part of the main query loop. -- [#5554](https://github.com/influxdata/influxdb/issues/5554): Can't run in alpine linux -- [#5252](https://github.com/influxdata/influxdb/issues/5252): Release tarballs contain specific attributes on '.' -- [#5152](https://github.com/influxdata/influxdb/issues/5152): Fix where filters when a tag and a filter are combined with OR. - -## v0.11.1 [2016-03-31] - -### Bugfixes - -- [#6168](https://github.com/influxdata/influxdb/pull/6168): Remove per measurement statsitics -- [#6129](https://github.com/influxdata/influxdb/pull/6129): Fix default continuous query lease host -- [#6121](https://github.com/influxdata/influxdb/issues/6121): Fix panic: slice index out of bounds in TSM index -- [#6092](https://github.com/influxdata/influxdb/issues/6092): Upgrading directly from 0.9.6.1 to 0.11.0 fails -- [#3932](https://github.com/influxdata/influxdb/issues/3932): Invalid timestamp format should throw an error. - -## v0.11.0 [2016-03-22] - -### Release Notes - -There were some important breaking changes in this release. Here's a list of the important things to know before upgrading: - -* [SHOW SERIES output has changed](https://github.com/influxdata/influxdb/pull/5937). See [new output in this test diff](https://github.com/influxdata/influxdb/pull/5937/files#diff-0cb24c2b7420b4db507ee3496c371845L263). -* [SHOW TAG VALUES output has changed](https://github.com/influxdata/influxdb/pull/5853) -* JSON write endpoint is disabled by default and will be removed in the next release. You can [turn it back on](https://github.com/influxdata/influxdb/pull/5512) in this release. -* b1/bz1 shards are no longer supported. You must migrate all old shards to TSM using [the migration tool](https://github.com/influxdata/influxdb/blob/master/cmd/influx_tsm/README.md). -* On queries to create databases, retention policies, and users, the default behavior has changed to create `IF NOT EXISTS`. If they already exist, no error will be returned. -* On queries with a selector like `min`, `max`, `first`, and `last` the time returned will be the time for the bucket of the group by window. [Selectors for the time for the specific point](https://github.com/influxdata/influxdb/issues/5926) will be added later. - -### Features - -- [#5994](https://github.com/influxdata/influxdb/issues/5994): Single server -- [#5862](https://github.com/influxdata/influxdb/pull/5862): Make Admin UI dynamically fetch both client and server versions -- [#5844](https://github.com/influxdata/influxdb/pull/5844): Tag TSM engine stats with database and retention policy -- [#5758](https://github.com/influxdata/influxdb/pull/5758): TSM engine stats for cache, WAL, and filestore. Thanks @jonseymour -- [#5737](https://github.com/influxdata/influxdb/pull/5737): Admin UI: Display results of multiple queries, not just the first query. Thanks @Vidhuran! -- [#5720](https://github.com/influxdata/influxdb/pull/5720): Admin UI: New button to generate permalink to queries -- [#5706](https://github.com/influxdata/influxdb/pull/5706): Cluster setup cleanup -- [#5691](https://github.com/influxdata/influxdb/pull/5691): Remove associated shard data when retention policies are dropped. -- [#5681](https://github.com/influxdata/influxdb/pull/5681): Stats: Add durations, number currently active to httpd and query executor -- [#5666](https://github.com/influxdata/influxdb/pull/5666): Manage dependencies with gdm -- [#5602](https://github.com/influxdata/influxdb/pull/5602): Simplify cluster startup for scripting and deployment -- [#5598](https://github.com/influxdata/influxdb/pull/5598): Client: Add Ping to v2 client @PSUdaemon -- [#5596](https://github.com/influxdata/influxdb/pull/5596): Build improvements for ARM architectures. Also removed `--goarm` and `--pkgarch` build flags. -- [#5593](https://github.com/influxdata/influxdb/issues/5593): Modify `SHOW TAG VALUES` output for the new query engine to normalize the output. -- [#5562](https://github.com/influxdata/influxdb/pull/5562): Graphite: Support matching fields multiple times (@chrusty) -- [#5550](https://github.com/influxdata/influxdb/pull/5550): Enabled golint for tsdb/engine/wal. @gabelev -- [#5541](https://github.com/influxdata/influxdb/pull/5541): Client: Support for adding custom TLS Config for HTTP client. -- [#5512](https://github.com/influxdata/influxdb/pull/5512): HTTP: Add config option to enable HTTP JSON write path which is now disabled by default. -- [#5419](https://github.com/influxdata/influxdb/pull/5419): Graphite: Support matching tags multiple times Thanks @m4ce -- [#5336](https://github.com/influxdata/influxdb/pull/5366): Enabled golint for influxql. @gabelev -- [#4299](https://github.com/influxdata/influxdb/pull/4299): Client: Reject uint64 Client.Point.Field values. Thanks @arussellsaw -- [#4125](https://github.com/influxdata/influxdb/pull/4125): Admin UI: Fetch and display server version on connect. Thanks @alexiri! -- [#2715](https://github.com/influxdata/influxdb/issues/2715): Support using field regex comparisons in the WHERE clause - -### Bugfixes - -- [#6042](https://github.com/influxdata/influxdb/issues/6042): CreateDatabase failure on Windows, regression from v0.11.0 RC @mvadu -- [#6006](https://github.com/influxdata/influxdb/pull/6006): Fix deadlock while running backups -- [#5965](https://github.com/influxdata/influxdb/issues/5965): InfluxDB panic crashes while parsing "-" as Float -- [#5963](https://github.com/influxdata/influxdb/pull/5963): Fix possible deadlock -- [#5949](https://github.com/influxdata/influxdb/issues/5949): Return error message when improper types are used in SELECT -- [#5937](https://github.com/influxdata/influxdb/pull/5937): Rewrite SHOW SERIES to use query engine -- [#5924](https://github.com/influxdata/influxdb/issues/5924): Missing data after using influx\_tsm -- [#5889](https://github.com/influxdata/influxdb/issues/5889): Fix writing partial TSM index when flush file fails -- [#5880](https://github.com/influxdata/influxdb/issues/5880): TCP connection closed after write (regression/change from 0.9.6) -- [#5865](https://github.com/influxdata/influxdb/issues/5865): Conversion to tsm fails with exceeds max index value -- [#5854](https://github.com/influxdata/influxdb/issues/5854): failures of tests in tsdb/engine/tsm1 when compiled with go master -- [#5842](https://github.com/influxdata/influxdb/issues/5842): Add SeriesList binary marshaling -- [#5841](https://github.com/influxdata/influxdb/pull/5841): Reduce tsm allocations by converting time.Time to int64 -- [#5835](https://github.com/influxdata/influxdb/issues/5835): Make CREATE USER default to IF NOT EXISTS -- [#5832](https://github.com/influxdata/influxdb/issues/5832): tsm: cache: need to check that snapshot has been sorted @jonseymour -- [#5814](https://github.com/influxdata/influxdb/issues/5814): Run CQs with the same name from different databases -- [#5787](https://github.com/influxdata/influxdb/pull/5787): HTTP: Add QueryAuthorizer instance to httpd service’s handler. @chris-ramon -- [#5754](https://github.com/influxdata/influxdb/issues/5754): Adding a node as meta only results in a data node also being registered -- [#5753](https://github.com/influxdata/influxdb/pull/5753): Ensures that drop-type commands work correctly in a cluster -- [#5724](https://github.com/influxdata/influxdb/issues/5724): influx\_tsm doesn't close file handles properly -- [#5719](https://github.com/influxdata/influxdb/issues/5719): Fix cache not deduplicating points -- [#5716](https://github.com/influxdata/influxdb/pull/5716): models: improve handling of points with empty field names or with no fields. -- [#5699](https://github.com/influxdata/influxdb/issues/5699): Fix potential thread safety issue in cache @jonseymour -- [#5696](https://github.com/influxdata/influxdb/issues/5696): Do not drop the database when creating with a retention policy -- [#5695](https://github.com/influxdata/influxdb/pull/5695): Remove meta servers from node.json -- [#5664](https://github.com/influxdata/influxdb/issues/5664): panic in model.Points.scanTo #5664 -- [#5656](https://github.com/influxdata/influxdb/issues/5656): influx\_tsm: panic during conversion -- [#5628](https://github.com/influxdata/influxdb/issues/5628): Crashed the server with a bad derivative query -- [#5624](https://github.com/influxdata/influxdb/pull/5624): Fix golint issues in client v2 package @PSUDaemon -- [#5610](https://github.com/influxdata/influxdb/issues/5610): Write into fully-replicated cluster is not replicated across all shards -- [#5606](https://github.com/influxdata/influxdb/issues/5606): TSM conversion reproducibly drops data silently -- [#5594](https://github.com/influxdata/influxdb/pull/5594): Fix missing url params on lease redirect - @oldmantaiter -- [#5590](https://github.com/influxdata/influxdb/pull/5590): Fix panic when dropping subscription for unknown retention policy. -- [#5557](https://github.com/influxdata/influxdb/issues/5630): Fixes panic when surrounding the select statement arguments in brackets -- [#5535](https://github.com/influxdata/influxdb/pull/5535): Update README for referring to Collectd -- [#5532](https://github.com/influxdata/influxdb/issues/5532): user passwords not changeable in cluster -- [#5510](https://github.com/influxdata/influxdb/pull/5510): Optimize ReducePercentile @bsideup -- [#5489](https://github.com/influxdata/influxdb/pull/5489): Fixes multiple issues causing tests to fail on windows. Thanks @runner-mei -- [#5376](https://github.com/influxdata/influxdb/pull/5376): Fix golint issues in models package. @nuss-justin -- [#5375](https://github.com/influxdata/influxdb/pull/5375): Lint tsdb and tsdb/engine package @nuss-justin -- [#5182](https://github.com/influxdata/influxdb/pull/5182): Graphite: Fix an issue where the default template would be used instead of a more specific one. Thanks @flisky -- [#4688](https://github.com/influxdata/influxdb/issues/4688): admin UI doesn't display results for some SHOW queries - -## v0.10.3 [2016-03-09] - -### Bugfixes - -- [#5924](https://github.com/influxdata/influxdb/issues/5924): Missing data after using influx\_tsm -- [#5716](https://github.com/influxdata/influxdb/pull/5716): models: improve handling of points with empty field names or with no fields. -- [#5594](https://github.com/influxdata/influxdb/pull/5594): Fix missing url params on lease redirect - @oldmantaiter - -## v0.10.2 [2016-03-03] - -### Bugfixes - -- [#5880](https://github.com/influxdata/influxdb/issues/5880): TCP connection closed after write (regression/change from 0.9.6) -- [#5865](https://github.com/influxdata/influxdb/issues/5865): Conversion to tsm fails with exceeds max index value -- [#5861](https://github.com/influxdata/influxdb/pull/5861): Fix panic when dropping subscription for unknown retention policy. -- [#5857](https://github.com/influxdata/influxdb/issues/5857): panic in tsm1.Values.Deduplicate -- [#5832](https://github.com/influxdata/influxdb/issues/5832): tsm: cache: need to check that snapshot has been sorted @jonseymour -- [#5719](https://github.com/influxdata/influxdb/issues/5719): Fix cache not deduplicating points -- [#5699](https://github.com/influxdata/influxdb/issues/5699): Fix potential thread safety issue in cache @jonseymour - -## v0.10.1 [2016-02-18] - -### Bugfixes - -- [#5724](https://github.com/influxdata/influxdb/issues/5724): influx\_tsm doesn't close file handles properly -- [#5696](https://github.com/influxdata/influxdb/issues/5696): Do not drop the database when creating with a retention policy -- [#5656](https://github.com/influxdata/influxdb/issues/5656): influx\_tsm: panic during conversion -- [#5606](https://github.com/influxdata/influxdb/issues/5606): TSM conversion reproducibly drops data silently -- [#5303](https://github.com/influxdata/influxdb/issues/5303): Protect against stateful mappers returning nothing in the raw executor - -## v0.10.0 [2016-02-04] - -### Release Notes - -This release now uses the TSM storage engine. Old bz1 and b1 shards can still be read, but in a future release you will be required to migrate old shards to TSM. For new shards getting created, or new installations, the TSM storage engine will be used. - -This release also changes how clusters are setup. The config file has changed so have a look at the new example. Also, upgrading a single node works, but for upgrading clusters, you'll need help from us. Sent us a note at contact@influxdb.com if you need assistance upgrading a cluster. - -### Features - -- [#5565](https://github.com/influxdata/influxdb/pull/5565): Add configuration for time precision with UDP services. - @tpitale -- [#5522](https://github.com/influxdata/influxdb/pull/5522): Optimize tsm1 cache to reduce memory consumption and GC scan time. -- [#5460](https://github.com/influxdata/influxdb/pull/5460): Prevent exponential growth in CLI history. Thanks @sczk! -- [#5459](https://github.com/influxdata/influxdb/pull/5459): Create `/status` endpoint for health checks. -- [#5226](https://github.com/influxdata/influxdb/pull/5226): b\*1 to tsm1 shard conversion tool. -- [#5226](https://github.com/influxdata/influxdb/pull/5226): b*1 to tsm1 shard conversion tool. -- [#5224](https://github.com/influxdata/influxdb/pull/5224): Online backup/incremental backup. Restore (for TSM). -- [#5201](https://github.com/influxdata/influxdb/pull/5201): Allow max UDP buffer size to be configurable. Thanks @sebito91 -- [#5194](https://github.com/influxdata/influxdb/pull/5194): Custom continuous query options per query rather than per node. -- [#5183](https://github.com/influxdata/influxdb/pull/5183): CLI confirms database exists when USE executed. Thanks @pires - -### Bugfixes - -- [#5505](https://github.com/influxdata/influxdb/issues/5505): Clear authCache in meta.Client when password changes. -- [#5504](https://github.com/influxdata/influxdb/issues/5504): create retention policy on unexistant DB crash InfluxDB -- [#5479](https://github.com/influxdata/influxdb/issues/5479): Bringing up a node as a meta only node causes panic -- [#5478](https://github.com/influxdata/influxdb/issues/5478): panic: interface conversion: interface is float64, not int64 -- [#5475](https://github.com/influxdata/influxdb/issues/5475): Ensure appropriate exit code returned for non-interactive use of CLI. -- [#5469](https://github.com/influxdata/influxdb/issues/5469): Conversion from bz1 to tsm doesn't work as described -- [#5455](https://github.com/influxdata/influxdb/issues/5455): panic: runtime error: slice bounds out of range when loading corrupted wal segment -- [#5449](https://github.com/influxdata/influxdb/issues/5449): panic when dropping collectd points -- [#5382](https://github.com/influxdata/influxdb/pull/5382): Fixes some escaping bugs with tag keys and values. -- [#5350](https://github.com/influxdata/influxdb/issues/5350): 'influxd backup' should create backup directory -- [#5349](https://github.com/influxdata/influxdb/issues/5349): Validate metadata blob for 'influxd backup' -- [#5264](https://github.com/influxdata/influxdb/pull/5264): Fix panic: runtime error: slice bounds out of range -- [#5262](https://github.com/influxdata/influxdb/issues/5262): Fix a panic when a tag value was empty. -- [#5244](https://github.com/influxdata/influxdb/issues/5244): panic: ensure it's safe to close engine multiple times. -- [#5193](https://github.com/influxdata/influxdb/issues/5193): Missing data a minute before current time. Comes back later. -- [#5186](https://github.com/influxdata/influxdb/pull/5186): Fix database creation with retention statement parsing. Fixes [#5077](https://github.com/influxdata/influxdb/issues/5077). Thanks @pires -- [#5178](https://github.com/influxdata/influxdb/pull/5178): SHOW FIELD shouldn't consider VALUES to be valid. Thanks @pires -- [#5158](https://github.com/influxdata/influxdb/pull/5158): Fix panic when writing invalid input to the line protocol. -- [#5129](https://github.com/influxdata/influxdb/pull/5129): Ensure precision flag is respected by CLI. Thanks @e-dard -- [#5079](https://github.com/influxdata/influxdb/pull/5079): Ensure tsm WAL encoding buffer can handle large batches. -- [#5078](https://github.com/influxdata/influxdb/issues/5078): influx non-interactive mode - INSERT must be handled. Thanks @grange74 -- [#5064](https://github.com/influxdata/influxdb/pull/5064): Full support for parenthesis in SELECT clause, fixes [#5054](https://github.com/influxdata/influxdb/issues/5054). Thanks @mengjinglei -- [#5059](https://github.com/influxdata/influxdb/pull/5059): Fix unmarshal of database error by client code. Thanks @farshidtz -- [#5042](https://github.com/influxdata/influxdb/issues/5042): Count with fill(none) will drop 0 valued intervals. -- [#5016](https://github.com/influxdata/influxdb/pull/5016): Don't panic if Meta data directory not writable. Thanks @oiooj -- [#4940](https://github.com/influxdata/influxdb/pull/4940): Fix distributed aggregate query query error. Thanks @li-ang -- [#4735](https://github.com/influxdata/influxdb/issues/4735): Fix panic when merging empty results. -- [#4622](https://github.com/influxdata/influxdb/issues/4622): Fix panic when passing too large of timestamps to OpenTSDB input. -- [#4303](https://github.com/influxdata/influxdb/issues/4303): Don't drop measurements or series from multiple databases. - -## v0.9.6 [2015-12-09] - -### Release Notes -This release has an updated design and implementation of the TSM storage engine. If you had been using tsm1 as your storage engine prior to this release (either 0.9.5.x or 0.9.6 nightly builds) you will have to start with a fresh database. - -If you had TSM configuration options set, those have been updated. See the the updated sample configuration for more details: https://github.com/influxdata/influxdb/blob/master/etc/config.sample.toml#L98-L125 - -### Features - -- [#4790](https://github.com/influxdata/influxdb/pull/4790): Allow openTSDB point-level error logging to be disabled -- [#4728](https://github.com/influxdata/influxdb/pull/4728): SHOW SHARD GROUPS. By @mateuszdyminski -- [#4841](https://github.com/influxdata/influxdb/pull/4841): Improve point parsing speed. Lint models pacakge. Thanks @e-dard! -- [#4889](https://github.com/influxdata/influxdb/pull/4889): Implement close notifier and timeout on executors -- [#2676](https://github.com/influxdata/influxdb/issues/2676), [#4866](https://github.com/influxdata/influxdb/pull/4866): Add support for specifying default retention policy in database create. Thanks @pires! -- [#4848](https://github.com/influxdata/influxdb/pull/4848): Added framework for cluster integration testing. -- [#4872](https://github.com/influxdata/influxdb/pull/4872): Add option to disable logging for meta service. -- [#4787](https://github.com/influxdata/influxdb/issues/4787): Now builds on Solaris - -### Bugfixes - -- [#4849](https://github.com/influxdata/influxdb/issues/4849): Derivative works with count, mean, median, sum, first, last, max, min, and percentile. -- [#4984](https://github.com/influxdata/influxdb/pull/4984): Allow math on fields, fixes regression. Thanks @mengjinglei -- [#4666](https://github.com/influxdata/influxdb/issues/4666): Fix panic in derivative with invalid values. -- [#4404](https://github.com/influxdata/influxdb/issues/4404): Return better error for currently unsupported DELETE queries. -- [#4858](https://github.com/influxdata/influxdb/pull/4858): Validate nested aggregations in queries. Thanks @viru -- [#4921](https://github.com/influxdata/influxdb/pull/4921): Error responses should be JSON-formatted. Thanks @pires -- [#4974](https://github.com/influxdata/influxdb/issues/4974) Fix Data Race in TSDB when setting measurement field name -- [#4876](https://github.com/influxdata/influxdb/pull/4876): Complete lint for monitor and services packages. Thanks @e-dard! -- [#4833](https://github.com/influxdata/influxdb/pull/4833), [#4927](https://github.com/influxdata/influxdb/pull/4927): Fix SHOW MEASURMENTS for clusters. Thanks @li-ang! -- [#4918](https://github.com/influxdata/influxdb/pull/4918): Restore can hang, Fix [issue #4806](https://github.com/influxdata/influxdb/issues/4806). Thanks @oiooj -- [#4855](https://github.com/influxdata/influxdb/pull/4855): Fix race in TCP proxy shutdown. Thanks @runner-mei! -- [#4411](https://github.com/influxdata/influxdb/pull/4411): Add Access-Control-Expose-Headers to HTTP responses -- [#4768](https://github.com/influxdata/influxdb/pull/4768): CLI history skips blank lines. Thanks @pires -- [#4766](https://github.com/influxdata/influxdb/pull/4766): Update CLI usage output. Thanks @aneshas -- [#4804](https://github.com/influxdata/influxdb/pull/4804): Complete lint for services/admin. Thanks @nii236 -- [#4796](https://github.com/influxdata/influxdb/pull/4796): Check point without fields. Thanks @CrazyJvm -- [#4815](https://github.com/influxdata/influxdb/pull/4815): Added `Time` field into aggregate output across the cluster. Thanks @li-ang -- [#4817](https://github.com/influxdata/influxdb/pull/4817): Fix Min,Max,Top,Bottom function when query distributed node. Thanks @mengjinglei -- [#4878](https://github.com/influxdata/influxdb/pull/4878): Fix String() function for several InfluxQL statement types -- [#4913](https://github.com/influxdata/influxdb/pull/4913): Fix b1 flush deadlock -- [#3170](https://github.com/influxdata/influxdb/issues/3170), [#4921](https://github.com/influxdata/influxdb/pull/4921): Database does not exist error is now JSON. Thanks @pires! -- [#5029](https://github.com/influxdata/influxdb/pull/5029): Drop UDP point on bad parse. - -## v0.9.5 [2015-11-20] - -### Release Notes - -- Field names for the internal stats have been changed to be more inline with Go style. -- 0.9.5 is reverting to Go 1.4.2 due to unresolved issues with Go 1.5.1. - -There are breaking changes in this release: -- The filesystem hierarchy for packages has been changed, namely: - - Binaries are now located in `/usr/bin` (previously `/opt/influxdb`) - - Configuration files are now located in `/etc/influxdb` (previously `/etc/opt/influxdb`) - - Data directories are now located in `/var/lib/influxdb` (previously `/var/opt/influxdb`) - - Scripts are now located in `/usr/lib/influxdb/scripts` (previously `/opt/influxdb`) - -### Features - -- [#4702](https://github.com/influxdata/influxdb/pull/4702): Support 'history' command at CLI -- [#4098](https://github.com/influxdata/influxdb/issues/4098): Enable `golint` on the code base - uuid subpackage -- [#4141](https://github.com/influxdata/influxdb/pull/4141): Control whether each query should be logged -- [#4065](https://github.com/influxdata/influxdb/pull/4065): Added precision support in cmd client. Thanks @sbouchex -- [#4140](https://github.com/influxdata/influxdb/pull/4140): Make storage engine configurable -- [#4161](https://github.com/influxdata/influxdb/pull/4161): Implement bottom selector function -- [#4204](https://github.com/influxdata/influxdb/pull/4204): Allow module-level selection for SHOW STATS -- [#4208](https://github.com/influxdata/influxdb/pull/4208): Allow module-level selection for SHOW DIAGNOSTICS -- [#4196](https://github.com/influxdata/influxdb/pull/4196): Export tsdb.Iterator -- [#4198](https://github.com/influxdata/influxdb/pull/4198): Add basic cluster-service stats -- [#4262](https://github.com/influxdata/influxdb/pull/4262): Allow configuration of UDP retention policy -- [#4265](https://github.com/influxdata/influxdb/pull/4265): Add statistics for Hinted-Handoff -- [#4284](https://github.com/influxdata/influxdb/pull/4284): Add exponential backoff for hinted-handoff failures -- [#4310](https://github.com/influxdata/influxdb/pull/4310): Support dropping non-Raft nodes. Work mostly by @corylanou -- [#4348](https://github.com/influxdata/influxdb/pull/4348): Public ApplyTemplate function for graphite parser. -- [#4178](https://github.com/influxdata/influxdb/pull/4178): Support fields in graphite parser. Thanks @roobert! -- [#4409](https://github.com/influxdata/influxdb/pull/4409): wire up INTO queries. -- [#4379](https://github.com/influxdata/influxdb/pull/4379): Auto-create database for UDP input. -- [#4375](https://github.com/influxdata/influxdb/pull/4375): Add Subscriptions so data can be 'forked' out of InfluxDB to another third party. -- [#4506](https://github.com/influxdata/influxdb/pull/4506): Register with Enterprise service and upload stats, if token is available. -- [#4516](https://github.com/influxdata/influxdb/pull/4516): Hinted-handoff refactor, with new statistics and diagnostics -- [#4501](https://github.com/influxdata/influxdb/pull/4501): Allow filtering SHOW MEASUREMENTS by regex. -- [#4547](https://github.com/influxdata/influxdb/pull/4547): Allow any node to be dropped, even a raft node (even the leader). -- [#4600](https://github.com/influxdata/influxdb/pull/4600): ping endpoint can wait for leader -- [#4648](https://github.com/influxdata/influxdb/pull/4648): UDP Client (v2 client) -- [#4690](https://github.com/influxdata/influxdb/pull/4690): SHOW SHARDS now includes database and policy. Thanks @pires -- [#4676](https://github.com/influxdata/influxdb/pull/4676): UDP service listener performance enhancements -- [#4659](https://github.com/influxdata/influxdb/pull/4659): Support IF EXISTS for DROP DATABASE. Thanks @ch33hau -- [#4721](https://github.com/influxdata/influxdb/pull/4721): Export tsdb.InterfaceValues -- [#4681](https://github.com/influxdata/influxdb/pull/4681): Increase default buffer size for collectd and graphite listeners -- [#4685](https://github.com/influxdata/influxdb/pull/4685): Automatically promote node to raft peer if drop server results in removing a raft peer. -- [#4846](https://github.com/influxdata/influxdb/pull/4846): Allow NaN as a valid value on the graphite service; discard these points silently (graphite compatibility). Thanks @jsternberg! - -### Bugfixes - -- [#4193](https://github.com/influxdata/influxdb/issues/4193): Less than or equal to inequality is not inclusive for time in where clause -- [#4235](https://github.com/influxdata/influxdb/issues/4235): "ORDER BY DESC" doesn't properly order -- [#4789](https://github.com/influxdata/influxdb/pull/4789): Decode WHERE fields during aggregates. Fix [issue #4701](https://github.com/influxdata/influxdb/issues/4701). -- [#4778](https://github.com/influxdata/influxdb/pull/4778): If there are no points to count, count is 0. -- [#4715](https://github.com/influxdata/influxdb/pull/4715): Fix panic during Raft-close. Fix [issue #4707](https://github.com/influxdata/influxdb/issues/4707). Thanks @oiooj -- [#4643](https://github.com/influxdata/influxdb/pull/4643): Fix panic during backup restoration. Thanks @oiooj -- [#4632](https://github.com/influxdata/influxdb/pull/4632): Fix parsing of IPv6 hosts in client package. Thanks @miguelxpn -- [#4389](https://github.com/influxdata/influxdb/pull/4389): Don't add a new segment file on each hinted-handoff purge cycle. -- [#4166](https://github.com/influxdata/influxdb/pull/4166): Fix parser error on invalid SHOW -- [#3457](https://github.com/influxdata/influxdb/issues/3457): [0.9.3] cannot select field names with prefix + "." that match the measurement name -- [#4704](https://github.com/influxdata/influxdb/pull/4704). Tighten up command parsing within CLI. Thanks @pires -- [#4225](https://github.com/influxdata/influxdb/pull/4225): Always display diags in name-sorted order -- [#4111](https://github.com/influxdata/influxdb/pull/4111): Update pre-commit hook for go vet composites -- [#4136](https://github.com/influxdata/influxdb/pull/4136): Return an error-on-write if target retention policy does not exist. Thanks for the report @ymettier -- [#4228](https://github.com/influxdata/influxdb/pull/4228): Add build timestamp to version information. -- [#4124](https://github.com/influxdata/influxdb/issues/4124): Missing defer/recover/panic idiom in HTTPD service -- [#4238](https://github.com/influxdata/influxdb/pull/4238): Fully disable hinted-handoff service if so requested. -- [#4165](https://github.com/influxdata/influxdb/pull/4165): Tag all Go runtime stats when writing to internal database. -- [#4586](https://github.com/influxdata/influxdb/pull/4586): Exit when invalid engine is selected -- [#4118](https://github.com/influxdata/influxdb/issues/4118): Return consistent, correct result for SHOW MEASUREMENTS with multiple AND conditions -- [#4191](https://github.com/influxdata/influxdb/pull/4191): Correctly marshal remote mapper responses. Fixes [#4170](https://github.com/influxdata/influxdb/issues/4170) -- [#4222](https://github.com/influxdata/influxdb/pull/4222): Graphite TCP connections should not block shutdown -- [#4180](https://github.com/influxdata/influxdb/pull/4180): Cursor & SelectMapper Refactor -- [#1577](https://github.com/influxdata/influxdb/issues/1577): selectors (e.g. min, max, first, last) should have equivalents to return the actual point -- [#4264](https://github.com/influxdata/influxdb/issues/4264): Refactor map functions to use list of values -- [#4278](https://github.com/influxdata/influxdb/pull/4278): Fix error marshalling across the cluster -- [#4149](https://github.com/influxdata/influxdb/pull/4149): Fix derivative unnecessarily requires aggregate function. Thanks @peekeri! -- [#4674](https://github.com/influxdata/influxdb/pull/4674): Fix panic during restore. Thanks @simcap. -- [#4725](https://github.com/influxdata/influxdb/pull/4725): Don't list deleted shards during SHOW SHARDS. -- [#4237](https://github.com/influxdata/influxdb/issues/4237): DERIVATIVE() edge conditions -- [#4263](https://github.com/influxdata/influxdb/issues/4263): derivative does not work when data is missing -- [#4293](https://github.com/influxdata/influxdb/pull/4293): Ensure shell is invoked when touching PID file. Thanks @christopherjdickson -- [#4296](https://github.com/influxdata/influxdb/pull/4296): Reject line protocol ending with '-'. Fixes [#4272](https://github.com/influxdata/influxdb/issues/4272) -- [#4333](https://github.com/influxdata/influxdb/pull/4333): Retry monitor storage creation and storage only on Leader. -- [#4276](https://github.com/influxdata/influxdb/issues/4276): Walk DropSeriesStatement & check for empty sources -- [#4465](https://github.com/influxdata/influxdb/pull/4465): Actually display a message if the CLI can't connect to the database. -- [#4342](https://github.com/influxdata/influxdb/pull/4342): Fix mixing aggregates and math with non-aggregates. Thanks @kostya-sh. -- [#4349](https://github.com/influxdata/influxdb/issues/4349): If HH can't unmarshal a block, skip that block. -- [#4502](https://github.com/influxdata/influxdb/pull/4502): Don't crash on Graphite close, if Graphite not fully open. Thanks for the report @ranjib -- [#4354](https://github.com/influxdata/influxdb/pull/4353): Fully lock node queues during hinted handoff. Fixes one cause of missing data on clusters. -- [#4357](https://github.com/influxdata/influxdb/issues/4357): Fix similar float values encoding overflow Thanks @dgryski! -- [#4344](https://github.com/influxdata/influxdb/issues/4344): Make client.Write default to client.precision if none is given. -- [#3429](https://github.com/influxdata/influxdb/issues/3429): Incorrect parsing of regex containing '/' -- [#4374](https://github.com/influxdata/influxdb/issues/4374): Add tsm1 quickcheck tests -- [#4644](https://github.com/influxdata/influxdb/pull/4644): Check for response errors during token check, fixes issue [#4641](https://github.com/influxdata/influxdb/issues/4641) -- [#4377](https://github.com/influxdata/influxdb/pull/4377): Hinted handoff should not process dropped nodes -- [#4365](https://github.com/influxdata/influxdb/issues/4365): Prevent panic in DecodeSameTypeBlock -- [#4280](https://github.com/influxdata/influxdb/issues/4280): Only drop points matching WHERE clause -- [#4443](https://github.com/influxdata/influxdb/pull/4443): Fix race condition while listing store's shards. Fixes [#4442](https://github.com/influxdata/influxdb/issues/4442) -- [#4410](https://github.com/influxdata/influxdb/pull/4410): Fix infinite recursion in statement string(). Thanks @kostya-sh -- [#4360](https://github.com/influxdata/influxdb/issues/4360): Aggregate Selectors overwrite values during post-processing -- [#4421](https://github.com/influxdata/influxdb/issues/4421): Fix line protocol accepting tags with no values -- [#4434](https://github.com/influxdata/influxdb/pull/4434): Allow 'E' for scientific values. Fixes [#4433](https://github.com/influxdata/influxdb/issues/4433) -- [#4431](https://github.com/influxdata/influxdb/issues/4431): Add tsm1 WAL QuickCheck -- [#4438](https://github.com/influxdata/influxdb/pull/4438): openTSDB service shutdown fixes -- [#4447](https://github.com/influxdata/influxdb/pull/4447): Fixes to logrotate file. Thanks @linsomniac. -- [#3820](https://github.com/influxdata/influxdb/issues/3820): Fix js error in admin UI. -- [#4460](https://github.com/influxdata/influxdb/issues/4460): tsm1 meta lint -- [#4415](https://github.com/influxdata/influxdb/issues/4415): Selector (like max, min, first, etc) return a string instead of timestamp -- [#4472](https://github.com/influxdata/influxdb/issues/4472): Fix 'too many points in GROUP BY interval' error -- [#4475](https://github.com/influxdata/influxdb/issues/4475): Fix SHOW TAG VALUES error message. -- [#4486](https://github.com/influxdata/influxdb/pull/4486): Fix missing comments for runner package -- [#4497](https://github.com/influxdata/influxdb/pull/4497): Fix sequence in meta proto -- [#3367](https://github.com/influxdata/influxdb/issues/3367): Negative timestamps are parsed correctly by the line protocol. -- [#4563](https://github.com/influxdata/influxdb/pull/4536): Fix broken subscriptions updates. -- [#4538](https://github.com/influxdata/influxdb/issues/4538): Dropping database under a write load causes panics -- [#4582](https://github.com/influxdata/influxdb/pull/4582): Correct logging tags in cluster and TCP package. Thanks @oiooj -- [#4513](https://github.com/influxdata/influxdb/issues/4513): TSM1: panic: runtime error: index out of range -- [#4521](https://github.com/influxdata/influxdb/issues/4521): TSM1: panic: decode of short block: got 1, exp 9 -- [#4587](https://github.com/influxdata/influxdb/pull/4587): Prevent NaN float values from being stored -- [#4596](https://github.com/influxdata/influxdb/pull/4596): Skip empty string for start position when parsing line protocol @Thanks @ch33hau -- [#4610](https://github.com/influxdata/influxdb/pull/4610): Make internal stats names consistent with Go style. -- [#4625](https://github.com/influxdata/influxdb/pull/4625): Correctly handle bad write requests. Thanks @oiooj. -- [#4650](https://github.com/influxdata/influxdb/issues/4650): Importer should skip empty lines -- [#4651](https://github.com/influxdata/influxdb/issues/4651): Importer doesn't flush out last batch -- [#4602](https://github.com/influxdata/influxdb/issues/4602): Fixes data race between PointsWriter and Subscriber services. -- [#4691](https://github.com/influxdata/influxdb/issues/4691): Enable toml test `TestConfig_Encode`. -- [#4283](https://github.com/influxdata/influxdb/pull/4283): Disable HintedHandoff if configuration is not set. -- [#4703](https://github.com/influxdata/influxdb/pull/4703): Complete lint for cmd/influx. Thanks @pablolmiranda - -## v0.9.4 [2015-09-14] - -### Release Notes -With this release InfluxDB is moving to Go 1.5. - -### Features - -- [#4050](https://github.com/influxdata/influxdb/pull/4050): Add stats to collectd -- [#3771](https://github.com/influxdata/influxdb/pull/3771): Close idle Graphite TCP connections -- [#3755](https://github.com/influxdata/influxdb/issues/3755): Add option to build script. Thanks @fg2it -- [#3863](https://github.com/influxdata/influxdb/pull/3863): Move to Go 1.5 -- [#3892](https://github.com/influxdata/influxdb/pull/3892): Support IF NOT EXISTS for CREATE DATABASE -- [#3916](https://github.com/influxdata/influxdb/pull/3916): New statistics and diagnostics support. Graphite first to be instrumented. -- [#3901](https://github.com/influxdata/influxdb/pull/3901): Add consistency level option to influx cli Thanks @takayuki -- [#4048](https://github.com/influxdata/influxdb/pull/4048): Add statistics to Continuous Query service -- [#4049](https://github.com/influxdata/influxdb/pull/4049): Add stats to the UDP input -- [#3876](https://github.com/influxdata/influxdb/pull/3876): Allow the following syntax in CQs: INTO "1hPolicy".:MEASUREMENT -- [#3975](https://github.com/influxdata/influxdb/pull/3975): Add shard copy service -- [#3986](https://github.com/influxdata/influxdb/pull/3986): Support sorting by time desc -- [#3930](https://github.com/influxdata/influxdb/pull/3930): Wire up TOP aggregate function - fixes [#1821](https://github.com/influxdata/influxdb/issues/1821) -- [#4045](https://github.com/influxdata/influxdb/pull/4045): Instrument cluster-level points writer -- [#3996](https://github.com/influxdata/influxdb/pull/3996): Add statistics to httpd package -- [#4003](https://github.com/influxdata/influxdb/pull/4033): Add logrotate configuration. -- [#4043](https://github.com/influxdata/influxdb/pull/4043): Add stats and batching to openTSDB input -- [#4042](https://github.com/influxdata/influxdb/pull/4042): Add pending batches control to batcher -- [#4006](https://github.com/influxdata/influxdb/pull/4006): Add basic statistics for shards -- [#4072](https://github.com/influxdata/influxdb/pull/4072): Add statistics for the WAL. - -### Bugfixes - -- [#4042](https://github.com/influxdata/influxdb/pull/4042): Set UDP input batching defaults as needed. -- [#3785](https://github.com/influxdata/influxdb/issues/3785): Invalid time stamp in graphite metric causes panic -- [#3804](https://github.com/influxdata/influxdb/pull/3804): init.d script fixes, fixes issue 3803. -- [#3823](https://github.com/influxdata/influxdb/pull/3823): Deterministic ordering for first() and last() -- [#3869](https://github.com/influxdata/influxdb/issues/3869): Seemingly deadlocked when ingesting metrics via graphite plugin -- [#3856](https://github.com/influxdata/influxdb/pull/3856): Minor changes to retention enforcement. -- [#3884](https://github.com/influxdata/influxdb/pull/3884): Fix two panics in WAL that can happen at server startup -- [#3868](https://github.com/influxdata/influxdb/pull/3868): Add shell option to start the daemon on CentOS. Thanks @SwannCroiset. -- [#3886](https://github.com/influxdata/influxdb/pull/3886): Prevent write timeouts due to lock contention in WAL -- [#3574](https://github.com/influxdata/influxdb/issues/3574): Querying data node causes panic -- [#3913](https://github.com/influxdata/influxdb/issues/3913): Convert meta shard owners to objects -- [#4026](https://github.com/influxdata/influxdb/pull/4026): Support multiple Graphite inputs. Fixes issue [#3636](https://github.com/influxdata/influxdb/issues/3636) -- [#3927](https://github.com/influxdata/influxdb/issues/3927): Add WAL lock to prevent timing lock contention -- [#3928](https://github.com/influxdata/influxdb/issues/3928): Write fails for multiple points when tag starts with quote -- [#3901](https://github.com/influxdata/influxdb/pull/3901): Unblock relaxed write consistency level Thanks @takayuki! -- [#3950](https://github.com/influxdata/influxdb/pull/3950): Limit bz1 quickcheck tests to 10 iterations on CI -- [#3977](https://github.com/influxdata/influxdb/pull/3977): Silence wal logging during testing -- [#3931](https://github.com/influxdata/influxdb/pull/3931): Don't precreate shard groups entirely in the past -- [#3960](https://github.com/influxdata/influxdb/issues/3960): possible "catch up" bug with nodes down in a cluster -- [#3980](https://github.com/influxdata/influxdb/pull/3980): 'service stop' waits until service actually stops. Fixes issue #3548. -- [#4016](https://github.com/influxdata/influxdb/pull/4016): Shutdown Graphite UDP on SIGTERM. -- [#4034](https://github.com/influxdata/influxdb/pull/4034): Rollback bolt tx on mapper open error -- [#3848](https://github.com/influxdata/influxdb/issues/3848): restart influxdb causing panic -- [#3881](https://github.com/influxdata/influxdb/issues/3881): panic: runtime error: invalid memory address or nil pointer dereference -- [#3926](https://github.com/influxdata/influxdb/issues/3926): First or last value of `GROUP BY time(x)` is often null. Fixed by [#4038](https://github.com/influxdata/influxdb/pull/4038) -- [#4053](https://github.com/influxdata/influxdb/pull/4053): Prohibit dropping default retention policy. -- [#4060](https://github.com/influxdata/influxdb/pull/4060): Don't log EOF error in openTSDB input. -- [#3978](https://github.com/influxdata/influxdb/issues/3978): [0.9.3] (regression) cannot use GROUP BY * with more than a single field in SELECT clause -- [#4058](https://github.com/influxdata/influxdb/pull/4058): Disable bz1 recompression -- [#3902](https://github.com/influxdata/influxdb/issues/3902): [0.9.3] DB should not crash when using invalid expression "GROUP BY time" -- [#3718](https://github.com/influxdata/influxdb/issues/3718): Derivative query with group by time but no aggregate function should fail parse - -## v0.9.3 [2015-08-26] - -### Release Notes - -There are breaking changes in this release. - - To store data points as integers you must now append `i` to the number if using the line protocol. - - If you have a UDP input configured, you should check the UDP section of [the new sample configuration file](https://github.com/influxdata/influxdb/blob/master/etc/config.sample.toml) to learn how to modify existing configuration files, as 0.9.3 now expects multiple UDP inputs. - - Configuration files must now have an entry for `wal-dir` in the `[data]` section. Check [new sample configuration file](https://github.com/influxdata/influxdb/blob/master/etc/config.sample.toml) for more details. - - The implicit `GROUP BY *` that was added to every `SELECT *` has been removed. Instead any tags in the data are now part of the columns in the returned query. - -Please see the *Features* section below for full details. - -### Features - -- [#3376](https://github.com/influxdata/influxdb/pull/3376): Support for remote shard query mapping -- [#3372](https://github.com/influxdata/influxdb/pull/3372): Support joining nodes to existing cluster -- [#3426](https://github.com/influxdata/influxdb/pull/3426): Additional logging for continuous queries. Thanks @jhorwit2 -- [#3478](https://github.com/influxdata/influxdb/pull/3478): Support incremental cluster joins -- [#3519](https://github.com/influxdata/influxdb/pull/3519): **--BREAKING CHANGE--** Update line protocol to require trailing i for field values that are integers -- [#3529](https://github.com/influxdata/influxdb/pull/3529): Add TLS support for OpenTSDB plugin. Thanks @nathanielc -- [#3421](https://github.com/influxdata/influxdb/issues/3421): Should update metastore and cluster if IP or hostname changes -- [#3502](https://github.com/influxdata/influxdb/pull/3502): Importer for 0.8.9 data via the CLI -- [#3564](https://github.com/influxdata/influxdb/pull/3564): Fix alias, maintain column sort order -- [#3585](https://github.com/influxdata/influxdb/pull/3585): Additional test coverage for non-existent fields -- [#3246](https://github.com/influxdata/influxdb/issues/3246): Allow overriding of configuration parameters using environment variables -- [#3599](https://github.com/influxdata/influxdb/pull/3599): **--BREAKING CHANGE--** Support multiple UDP inputs. Thanks @tpitale -- [#3636](https://github.com/influxdata/influxdb/pull/3639): Cap auto-created retention policy replica count at 3 -- [#3641](https://github.com/influxdata/influxdb/pull/3641): Logging enhancements and single-node rename -- [#3635](https://github.com/influxdata/influxdb/pull/3635): Add build branch to version output. -- [#3115](https://github.com/influxdata/influxdb/pull/3115): Various init.d script improvements. Thanks @KoeSystems. -- [#3628](https://github.com/influxdata/influxdb/pull/3628): Wildcard expansion of tags and fields for raw queries -- [#3721](https://github.com/influxdata/influxdb/pull/3721): interpret number literals compared against time as nanoseconds from epoch -- [#3514](https://github.com/influxdata/influxdb/issues/3514): Implement WAL outside BoltDB with compaction -- [#3544](https://github.com/influxdata/influxdb/pull/3544): Implement compression on top of BoltDB -- [#3795](https://github.com/influxdata/influxdb/pull/3795): Throttle import -- [#3584](https://github.com/influxdata/influxdb/pull/3584): Import/export documenation - -### Bugfixes - -- [#3405](https://github.com/influxdata/influxdb/pull/3405): Prevent database panic when fields are missing. Thanks @jhorwit2 -- [#3411](https://github.com/influxdata/influxdb/issues/3411): 500 timeout on write -- [#3420](https://github.com/influxdata/influxdb/pull/3420): Catch opentsdb malformed tags. Thanks @nathanielc. -- [#3404](https://github.com/influxdata/influxdb/pull/3404): Added support for escaped single quotes in query string. Thanks @jhorwit2 -- [#3414](https://github.com/influxdata/influxdb/issues/3414): Shard mappers perform query re-writing -- [#3525](https://github.com/influxdata/influxdb/pull/3525): check if fields are valid during parse time. -- [#3511](https://github.com/influxdata/influxdb/issues/3511): Sending a large number of tag causes panic -- [#3288](https://github.com/influxdata/influxdb/issues/3288): Run go fuzz on the line-protocol input -- [#3545](https://github.com/influxdata/influxdb/issues/3545): Fix parsing string fields with newlines -- [#3579](https://github.com/influxdata/influxdb/issues/3579): Revert breaking change to `client.NewClient` function -- [#3580](https://github.com/influxdata/influxdb/issues/3580): Do not allow wildcards with fields in select statements -- [#3530](https://github.com/influxdata/influxdb/pull/3530): Aliasing a column no longer works -- [#3436](https://github.com/influxdata/influxdb/issues/3436): Fix panic in hinted handoff queue processor -- [#3401](https://github.com/influxdata/influxdb/issues/3401): Derivative on non-numeric fields panics db -- [#3583](https://github.com/influxdata/influxdb/issues/3583): Inserting value in scientific notation with a trailing i causes panic -- [#3611](https://github.com/influxdata/influxdb/pull/3611): Fix query arithmetic with integers -- [#3326](https://github.com/influxdata/influxdb/issues/3326): simple regex query fails with cryptic error -- [#3618](https://github.com/influxdata/influxdb/pull/3618): Fix collectd stats panic on i386. Thanks @richterger -- [#3625](https://github.com/influxdata/influxdb/pull/3625): Don't panic when aggregate and raw queries are in a single statement -- [#3629](https://github.com/influxdata/influxdb/pull/3629): Use sensible batching defaults for Graphite. -- [#3638](https://github.com/influxdata/influxdb/pull/3638): Cluster config fixes and removal of meta.peers config field -- [#3640](https://github.com/influxdata/influxdb/pull/3640): Shutdown Graphite service when signal received. -- [#3632](https://github.com/influxdata/influxdb/issues/3632): Make single-node host renames more seamless -- [#3656](https://github.com/influxdata/influxdb/issues/3656): Silence snapshotter logger for testing -- [#3651](https://github.com/influxdata/influxdb/pull/3651): Fully remove series when dropped. -- [#3517](https://github.com/influxdata/influxdb/pull/3517): Batch CQ writes to avoid timeouts. Thanks @dim. -- [#3522](https://github.com/influxdata/influxdb/pull/3522): Consume CQ results on request timeouts. Thanks @dim. -- [#3646](https://github.com/influxdata/influxdb/pull/3646): Fix nil FieldCodec panic. -- [#3672](https://github.com/influxdata/influxdb/pull/3672): Reduce in-memory index by 20%-30% -- [#3673](https://github.com/influxdata/influxdb/pull/3673): Improve query performance by removing unnecessary tagset sorting. -- [#3676](https://github.com/influxdata/influxdb/pull/3676): Improve query performance by memomizing mapper output keys. -- [#3686](https://github.com/influxdata/influxdb/pull/3686): Ensure 'p' parameter is not logged, even on OPTIONS requests. -- [#3687](https://github.com/influxdata/influxdb/issues/3687): Fix panic: runtime error: makeslice: len out of range in hinted handoff -- [#3697](https://github.com/influxdata/influxdb/issues/3697): Correctly merge non-chunked results for same series. Fix issue #3242. -- [#3708](https://github.com/influxdata/influxdb/issues/3708): Fix double escaping measurement name during cluster replication -- [#3704](https://github.com/influxdata/influxdb/issues/3704): cluster replication issue for measurement name containing backslash -- [#3681](https://github.com/influxdata/influxdb/issues/3681): Quoted measurement names fail -- [#3681](https://github.com/influxdata/influxdb/issues/3682): Fix inserting string value with backslashes -- [#3735](https://github.com/influxdata/influxdb/issues/3735): Append to small bz1 blocks -- [#3736](https://github.com/influxdata/influxdb/pull/3736): Update shard group duration with retention policy changes. Thanks for the report @papylhomme -- [#3539](https://github.com/influxdata/influxdb/issues/3539): parser incorrectly accepts NaN as numerical value, but not always -- [#3790](https://github.com/influxdata/influxdb/pull/3790): Fix line protocol parsing equals in measurements and NaN values -- [#3778](https://github.com/influxdata/influxdb/pull/3778): Don't panic if SELECT on time. -- [#3824](https://github.com/influxdata/influxdb/issues/3824): tsdb.Point.MarshalBinary needs to support all number types -- [#3828](https://github.com/influxdata/influxdb/pull/3828): Support all number types when decoding a point -- [#3853](https://github.com/influxdata/influxdb/pull/3853): Use 4KB default block size for bz1 -- [#3607](https://github.com/influxdata/influxdb/issues/3607): Fix unable to query influxdb due to deadlock in metastore. Thanks @ccutrer! - -## v0.9.2 [2015-07-24] - -### Features -- [#3177](https://github.com/influxdata/influxdb/pull/3177): Client supports making HTTPS requests. Thanks @jipperinbham -- [#3299](https://github.com/influxdata/influxdb/pull/3299): Refactor query engine for distributed query support. -- [#3334](https://github.com/influxdata/influxdb/pull/3334): Clean shutdown of influxd. Thanks @mcastilho - -### Bugfixes - -- [#3180](https://github.com/influxdata/influxdb/pull/3180): Log GOMAXPROCS, version, and commit on startup. -- [#3218](https://github.com/influxdata/influxdb/pull/3218): Allow write timeouts to be configurable. -- [#3184](https://github.com/influxdata/influxdb/pull/3184): Support basic auth in admin interface. Thanks @jipperinbham! -- [#3236](https://github.com/influxdata/influxdb/pull/3236): Fix display issues in admin interface. -- [#3232](https://github.com/influxdata/influxdb/pull/3232): Set logging prefix for metastore. -- [#3230](https://github.com/influxdata/influxdb/issues/3230): panic: unable to parse bool value -- [#3245](https://github.com/influxdata/influxdb/issues/3245): Error using graphite plugin with multiple filters -- [#3223](https://github.com/influxdata/influxdb/issues/323): default graphite template cannot have extra tags -- [#3255](https://github.com/influxdata/influxdb/pull/3255): Flush WAL on start-up as soon as possible. -- [#3289](https://github.com/influxdata/influxdb/issues/3289): InfluxDB crashes on floats without decimal -- [#3298](https://github.com/influxdata/influxdb/pull/3298): Corrected WAL & flush parameters in default config. Thanks @jhorwit2 -- [#3152](https://github.com/influxdata/influxdb/issues/3159): High CPU Usage with unsorted writes -- [#3307](https://github.com/influxdata/influxdb/pull/3307): Fix regression parsing boolean values True/False -- [#3304](https://github.com/influxdata/influxdb/pull/3304): Fixed httpd logger to log user from query params. Thanks @jhorwit2 -- [#3332](https://github.com/influxdata/influxdb/pull/3332): Add SLIMIT and SOFFSET to string version of AST. -- [#3335](https://github.com/influxdata/influxdb/pull/3335): Don't drop all data on DROP DATABASE. Thanks to @PierreF for the report -- [#2761](https://github.com/influxdata/influxdb/issues/2761): Make SHOW RETENTION POLICIES consistent with other queries. -- [#3356](https://github.com/influxdata/influxdb/pull/3356): Disregard semicolons after database name in use command. Thanks @timraymond. -- [#3351](https://github.com/influxdata/influxdb/pull/3351): Handle malformed regex comparisons during parsing. Thanks @rnubel -- [#3244](https://github.com/influxdata/influxdb/pull/3244): Wire up admin privilege grant and revoke. -- [#3259](https://github.com/influxdata/influxdb/issues/3259): Respect privileges for queries. -- [#3256](https://github.com/influxdata/influxdb/pull/3256): Remove unnecessary timeout in WaitForLeader(). Thanks @cannium. -- [#3380](https://github.com/influxdata/influxdb/issues/3380): Parser fix, only allow ORDER BY ASC and ORDER BY time ASC. -- [#3319](https://github.com/influxdata/influxdb/issues/3319): restarting process irrevocably BREAKS measurements with spaces -- [#3453](https://github.com/influxdata/influxdb/issues/3453): Remove outdated `dump` command from CLI. -- [#3463](https://github.com/influxdata/influxdb/issues/3463): Fix aggregate queries and time precision on where clauses. - -## v0.9.1 [2015-07-02] - -### Features - -- [2650](https://github.com/influxdata/influxdb/pull/2650): Add SHOW GRANTS FOR USER statement. Thanks @n1tr0g. -- [3125](https://github.com/influxdata/influxdb/pull/3125): Graphite Input Protocol Parsing -- [2746](https://github.com/influxdata/influxdb/pull/2746): New Admin UI/interface -- [3036](https://github.com/influxdata/influxdb/pull/3036): Write Ahead Log (WAL) -- [3014](https://github.com/influxdata/influxdb/issues/3014): Implement Raft snapshots - -### Bugfixes - -- [3013](https://github.com/influxdata/influxdb/issues/3013): Panic error with inserting values with commas -- [#2956](https://github.com/influxdata/influxdb/issues/2956): Type mismatch in derivative -- [#2908](https://github.com/influxdata/influxdb/issues/2908): Field mismatch error messages need to be updated -- [#2931](https://github.com/influxdata/influxdb/pull/2931): Services and reporting should wait until cluster has leader. -- [#2943](https://github.com/influxdata/influxdb/issues/2943): Ensure default retention policies are fully replicated -- [#2948](https://github.com/influxdata/influxdb/issues/2948): Field mismatch error message to include measurement name -- [#2919](https://github.com/influxdata/influxdb/issues/2919): Unable to insert negative floats -- [#2935](https://github.com/influxdata/influxdb/issues/2935): Hook CPU and memory profiling back up. -- [#2960](https://github.com/influxdata/influxdb/issues/2960): Cluster Write Errors. -- [#2928](https://github.com/influxdata/influxdb/pull/2928): Start work to set InfluxDB version in HTTP response headers. Thanks @neonstalwart. -- [#2969](https://github.com/influxdata/influxdb/pull/2969): Actually set HTTP version in responses. -- [#2993](https://github.com/influxdata/influxdb/pull/2993): Don't log each UDP batch. -- [#2994](https://github.com/influxdata/influxdb/pull/2994): Don't panic during wilcard expansion if no default database specified. -- [#3002](https://github.com/influxdata/influxdb/pull/3002): Remove measurement from shard's index on DROP MEASUREMENT. -- [#3021](https://github.com/influxdata/influxdb/pull/3021): Correct set HTTP write trace logging. Thanks @vladlopes. -- [#3027](https://github.com/influxdata/influxdb/pull/3027): Enforce minimum retention policy duration of 1 hour. -- [#3030](https://github.com/influxdata/influxdb/pull/3030): Fix excessive logging of shard creation. -- [#3038](https://github.com/influxdata/influxdb/pull/3038): Don't check deleted shards for precreation. Thanks @vladlopes. -- [#3033](https://github.com/influxdata/influxdb/pull/3033): Add support for marshaling `uint64` in client. -- [#3090](https://github.com/influxdata/influxdb/pull/3090): Remove database from TSDB index on DROP DATABASE. -- [#2944](https://github.com/influxdata/influxdb/issues/2944): Don't require "WHERE time" when creating continuous queries. -- [#3075](https://github.com/influxdata/influxdb/pull/3075): GROUP BY correctly when different tags have same value. -- [#3078](https://github.com/influxdata/influxdb/pull/3078): Fix CLI panic on malformed INSERT. -- [#2102](https://github.com/influxdata/influxdb/issues/2102): Re-work Graphite input and metric processing -- [#2996](https://github.com/influxdata/influxdb/issues/2996): Graphite Input Parsing -- [#3136](https://github.com/influxdata/influxdb/pull/3136): Fix various issues with init.d script. Thanks @ miguelcnf. -- [#2996](https://github.com/influxdata/influxdb/issues/2996): Graphite Input Parsing -- [#3127](https://github.com/influxdata/influxdb/issues/3127): Trying to insert a number larger than the largest signed 64-bit number kills influxd -- [#3131](https://github.com/influxdata/influxdb/pull/3131): Copy batch tags to each point before marshalling -- [#3155](https://github.com/influxdata/influxdb/pull/3155): Instantiate UDP batcher before listening for UDP traffic, otherwise a panic may result. -- [#2678](https://github.com/influxdata/influxdb/issues/2678): Server allows tags with an empty string for the key and/or value -- [#3061](https://github.com/influxdata/influxdb/issues/3061): syntactically incorrect line protocol insert panics the database -- [#2608](https://github.com/influxdata/influxdb/issues/2608): drop measurement while writing points to that measurement has race condition that can panic -- [#3183](https://github.com/influxdata/influxdb/issues/3183): using line protocol measurement names cannot contain commas -- [#3193](https://github.com/influxdata/influxdb/pull/3193): Fix panic for SHOW STATS and in collectd -- [#3102](https://github.com/influxdata/influxdb/issues/3102): Add authentication cache -- [#3209](https://github.com/influxdata/influxdb/pull/3209): Dump Run() errors to stderr -- [#3217](https://github.com/influxdata/influxdb/pull/3217): Allow WAL partition flush delay to be configurable. - -## v0.9.0 [2015-06-11] - -### Bugfixes - -- [#2869](https://github.com/influxdata/influxdb/issues/2869): Adding field to existing measurement causes panic -- [#2849](https://github.com/influxdata/influxdb/issues/2849): RC32: Frequent write errors -- [#2700](https://github.com/influxdata/influxdb/issues/2700): Incorrect error message in database EncodeFields -- [#2897](https://github.com/influxdata/influxdb/pull/2897): Ensure target Graphite database exists -- [#2898](https://github.com/influxdata/influxdb/pull/2898): Ensure target openTSDB database exists -- [#2895](https://github.com/influxdata/influxdb/pull/2895): Use Graphite input defaults where necessary -- [#2900](https://github.com/influxdata/influxdb/pull/2900): Use openTSDB input defaults where necessary -- [#2886](https://github.com/influxdata/influxdb/issues/2886): Refactor backup & restore -- [#2804](https://github.com/influxdata/influxdb/pull/2804): BREAKING: change time literals to be single quoted in InfluxQL. Thanks @nvcook42! -- [#2906](https://github.com/influxdata/influxdb/pull/2906): Restrict replication factor to the cluster size -- [#2905](https://github.com/influxdata/influxdb/pull/2905): Restrict clusters to 3 peers -- [#2904](https://github.com/influxdata/influxdb/pull/2904): Re-enable server reporting. -- [#2917](https://github.com/influxdata/influxdb/pull/2917): Fix int64 field values. -- [#2920](https://github.com/influxdata/influxdb/issues/2920): Ensure collectd database exists - -## v0.9.0-rc33 [2015-06-09] - -### Bugfixes - -- [#2816](https://github.com/influxdata/influxdb/pull/2816): Enable UDP service. Thanks @renan- -- [#2824](https://github.com/influxdata/influxdb/pull/2824): Add missing call to WaitGroup.Done in execConn. Thanks @liyichao -- [#2823](https://github.com/influxdata/influxdb/pull/2823): Convert OpenTSDB to a service. -- [#2838](https://github.com/influxdata/influxdb/pull/2838): Set auto-created retention policy period to infinite. -- [#2829](https://github.com/influxdata/influxdb/pull/2829): Re-enable Graphite support as a new Service-style component. -- [#2814](https://github.com/influxdata/influxdb/issues/2814): Convert collectd to a service. -- [#2852](https://github.com/influxdata/influxdb/pull/2852): Don't panic when altering retention policies. Thanks for the report @huhongbo -- [#2857](https://github.com/influxdata/influxdb/issues/2857): Fix parsing commas in string field values. -- [#2833](https://github.com/influxdata/influxdb/pull/2833): Make the default config valid. -- [#2859](https://github.com/influxdata/influxdb/pull/2859): Fix panic on aggregate functions. -- [#2878](https://github.com/influxdata/influxdb/pull/2878): Re-enable shard precreation. -- [2865](https://github.com/influxdata/influxdb/pull/2865) -- Return an empty set of results if database does not exist in shard metadata. - -### Features -- [2858](https://github.com/influxdata/influxdb/pull/2858): Support setting openTSDB write consistency. - -## v0.9.0-rc32 [2015-06-07] - -### Release Notes - -This released introduced an updated write path and clustering design. The data format has also changed, so you'll need to wipe out your data to upgrade from RC31. There should be no other data changes before v0.9.0 is released. - -### Features -- [#1997](https://github.com/influxdata/influxdb/pull/1997): Update SELECT * to return tag values. -- [#2599](https://github.com/influxdata/influxdb/issues/2599): Add "epoch" URL param and return JSON time values as epoch instead of date strings. -- [#2682](https://github.com/influxdata/influxdb/issues/2682): Adding pr checklist to CONTRIBUTING.md -- [#2683](https://github.com/influxdata/influxdb/issues/2683): Add batching support to Graphite inputs. -- [#2687](https://github.com/influxdata/influxdb/issues/2687): Add batching support to Collectd inputs. -- [#2696](https://github.com/influxdata/influxdb/pull/2696): Add line protocol. This is now the preferred way to write data. -- [#2751](https://github.com/influxdata/influxdb/pull/2751): Add UDP input. UDP only supports the line protocol now. -- [#2684](https://github.com/influxdata/influxdb/pull/2684): Include client timeout configuration. Thanks @vladlopes! - -### Bugfixes -- [#2776](https://github.com/influxdata/influxdb/issues/2776): Re-implement retention policy enforcement. -- [#2635](https://github.com/influxdata/influxdb/issues/2635): Fix querying against boolean field in WHERE clause. -- [#2644](https://github.com/influxdata/influxdb/issues/2644): Make SHOW queries work with FROM //. -- [#2501](https://github.com/influxdata/influxdb/issues/2501): Name the FlagSet for the shell and add a version flag. Thanks @neonstalwart -- [#2647](https://github.com/influxdata/influxdb/issues/2647): Fixes typos in sample config file - thanks @claws! - -## v0.9.0-rc31 [2015-05-21] - -### Features -- [#1822](https://github.com/influxdata/influxdb/issues/1822): Wire up DERIVATIVE aggregate -- [#1477](https://github.com/influxdata/influxdb/issues/1477): Wire up non_negative_derivative function -- [#2557](https://github.com/influxdata/influxdb/issues/2557): Fix false positive error with `GROUP BY time` -- [#1891](https://github.com/influxdata/influxdb/issues/1891): Wire up COUNT DISTINCT aggregate -- [#1989](https://github.com/influxdata/influxdb/issues/1989): Implement `SELECT tagName FROM m` - -### Bugfixes -- [#2545](https://github.com/influxdata/influxdb/pull/2545): Use "value" as the field name for graphite input. Thanks @cannium. -- [#2558](https://github.com/influxdata/influxdb/pull/2558): Fix client response check - thanks @vladlopes! -- [#2566](https://github.com/influxdata/influxdb/pull/2566): Wait until each data write has been commited by the Raft cluster. -- [#2602](https://github.com/influxdata/influxdb/pull/2602): CLI execute command exits without cleaning up liner package. -- [#2610](https://github.com/influxdata/influxdb/pull/2610): Fix shard group creation -- [#2596](https://github.com/influxdata/influxdb/pull/2596): RC30: `panic: runtime error: index out of range` when insert data points. -- [#2592](https://github.com/influxdata/influxdb/pull/2592): Should return an error if user attempts to group by a field. -- [#2499](https://github.com/influxdata/influxdb/pull/2499): Issuing a select query with tag as a values causes panic. -- [#2612](https://github.com/influxdata/influxdb/pull/2612): Query planner should validate distinct is passed a field. -- [#2531](https://github.com/influxdata/influxdb/issues/2531): Fix select with 3 or more terms in where clause. -- [#2564](https://github.com/influxdata/influxdb/issues/2564): Change "name" to "measurement" in JSON for writes. - -## PRs -- [#2569](https://github.com/influxdata/influxdb/pull/2569): Add derivative functions -- [#2598](https://github.com/influxdata/influxdb/pull/2598): Implement tag support in SELECT statements -- [#2624](https://github.com/influxdata/influxdb/pull/2624): Remove references to SeriesID in `DROP SERIES` handlers. - -## v0.9.0-rc30 [2015-05-12] - -### Release Notes - -This release has a breaking API change for writes -- the field previously called `timestamp` has been renamed to `time`. - -### Features -- [#2254](https://github.com/influxdata/influxdb/pull/2254): Add Support for OpenTSDB HTTP interface. Thanks @tcolgate -- [#2525](https://github.com/influxdata/influxdb/pull/2525): Serve broker diagnostics over HTTP -- [#2186](https://github.com/influxdata/influxdb/pull/2186): The default status code for queries is now `200 OK` -- [#2298](https://github.com/influxdata/influxdb/pull/2298): Successful writes now return a status code of `204 No Content` - thanks @neonstalwart! -- [#2549](https://github.com/influxdata/influxdb/pull/2549): Raft election timeout to 5 seconds, so system is more forgiving of CPU loads. -- [#2568](https://github.com/influxdata/influxdb/pull/2568): Wire up SELECT DISTINCT. - -### Bugfixes -- [#2535](https://github.com/influxdata/influxdb/pull/2535): Return exit status 0 if influxd already running. Thanks @haim0n. -- [#2521](https://github.com/influxdata/influxdb/pull/2521): Don't truncate topic data until fully replicated. -- [#2509](https://github.com/influxdata/influxdb/pull/2509): Parse config file correctly during restore. Thanks @neonstalwart -- [#2536](https://github.com/influxdata/influxdb/issues/2532): Set leader ID on restart of single-node cluster. -- [#2448](https://github.com/influxdata/influxdb/pull/2448): Fix inconsistent data type - thanks @cannium! -- [#2108](https://github.com/influxdata/influxdb/issues/2108): Change `timestamp` to `time` - thanks @neonstalwart! -- [#2539](https://github.com/influxdata/influxdb/issues/2539): Add additional vote request logging. -- [#2541](https://github.com/influxdata/influxdb/issues/2541): Update messaging client connection index with every message. -- [#2542](https://github.com/influxdata/influxdb/issues/2542): Throw parser error for invalid aggregate without where time. -- [#2548](https://github.com/influxdata/influxdb/issues/2548): Return an error when numeric aggregate applied to non-numeric data. -- [#2487](https://github.com/influxdata/influxdb/issues/2487): Aggregate query with exact timestamp causes panic. Thanks @neonstalwart! -- [#2552](https://github.com/influxdata/influxdb/issues/2552): Run CQ that is actually passed into go-routine. -- [#2553](https://github.com/influxdata/influxdb/issues/2553): Fix race condition during CQ execution. -- [#2557](https://github.com/influxdata/influxdb/issues/2557): RC30 WHERE time filter Regression. - -## v0.9.0-rc29 [2015-05-05] - -### Features -- [#2410](https://github.com/influxdata/influxdb/pull/2410): If needed, brokers respond with data nodes for peer shard replication. -- [#2469](https://github.com/influxdata/influxdb/pull/2469): Reduce default max topic size from 1GB to 50MB. -- [#1824](https://github.com/influxdata/influxdb/pull/1824): Wire up MEDIAN aggregate. Thanks @neonstalwart! - -### Bugfixes -- [#2446](https://github.com/influxdata/influxdb/pull/2446): Correctly count number of queries executed. Thanks @neonstalwart -- [#2452](https://github.com/influxdata/influxdb/issues/2452): Fix panic with shard stats on multiple clusters -- [#2453](https://github.com/influxdata/influxdb/pull/2453): Do not require snapshot on Log.WriteEntriesTo(). -- [#2460](https://github.com/influxdata/influxdb/issues/2460): Collectd input should use "value" for fields values. Fixes 2412. Thanks @josh-padnick -- [#2465](https://github.com/influxdata/influxdb/pull/2465): HTTP response logging paniced with chunked requests. Thanks @Jackkoz -- [#2475](https://github.com/influxdata/influxdb/pull/2475): RLock server when checking if shards groups are required during write. -- [#2471](https://github.com/influxdata/influxdb/issues/2471): Function calls normalized to be lower case. Fixes percentile not working when called uppercase. Thanks @neonstalwart -- [#2281](https://github.com/influxdata/influxdb/issues/2281): Fix Bad Escape error when parsing regex - -## v0.9.0-rc28 [2015-04-27] - -### Features -- [#2410](https://github.com/influxdata/influxdb/pull/2410) Allow configuration of Raft timers -- [#2354](https://github.com/influxdata/influxdb/pull/2354) Wire up STDDEV. Thanks @neonstalwart! - -### Bugfixes -- [#2374](https://github.com/influxdata/influxdb/issues/2374): Two different panics during SELECT percentile -- [#2404](https://github.com/influxdata/influxdb/pull/2404): Mean and percentile function fixes -- [#2408](https://github.com/influxdata/influxdb/pull/2408): Fix snapshot 500 error -- [#1896](https://github.com/influxdata/influxdb/issues/1896): Excessive heartbeater logging of "connection refused" on cluster node stop -- [#2418](https://github.com/influxdata/influxdb/pull/2418): Fix raft node getting stuck in candidate state -- [#2415](https://github.com/influxdata/influxdb/pull/2415): Raft leader ID now set on election after failover. Thanks @xiaost -- [#2426](https://github.com/influxdata/influxdb/pull/2426): Fix race condition around listener address in openTSDB server. -- [#2426](https://github.com/influxdata/influxdb/pull/2426): Fix race condition around listener address in Graphite server. -- [#2429](https://github.com/influxdata/influxdb/pull/2429): Ensure no field value is null. -- [#2431](https://github.com/influxdata/influxdb/pull/2431): Always append shard path in diags. Thanks @marcosnils -- [#2441](https://github.com/influxdata/influxdb/pull/2441): Correctly release server RLock during "drop series". -- [#2445](https://github.com/influxdata/influxdb/pull/2445): Read locks and data race fixes - -## v0.9.0-rc27 [04-23-2015] - -### Features -- [#2398](https://github.com/influxdata/influxdb/pull/2398) Track more stats and report errors for shards. - -### Bugfixes -- [#2370](https://github.com/influxdata/influxdb/pull/2370): Fix data race in openTSDB endpoint. -- [#2371](https://github.com/influxdata/influxdb/pull/2371): Don't set client to nil when closing broker Fixes #2352 -- [#2372](https://github.com/influxdata/influxdb/pull/2372): Fix data race in graphite endpoint. -- [#2373](https://github.com/influxdata/influxdb/pull/2373): Actually allow HTTP logging to be controlled. -- [#2376](https://github.com/influxdata/influxdb/pull/2376): Encode all types of integers. Thanks @jtakkala. -- [#2376](https://github.com/influxdata/influxdb/pull/2376): Add shard path to existing diags value. Fix issue #2369. -- [#2386](https://github.com/influxdata/influxdb/pull/2386): Fix shard datanodes stats getting appended too many times -- [#2393](https://github.com/influxdata/influxdb/pull/2393): Fix default hostname for connecting to cluster. -- [#2390](https://github.com/influxdata/influxdb/pull/2390): Handle large sums when calculating means - thanks @neonstalwart! -- [#2391](https://github.com/influxdata/influxdb/pull/2391): Unable to write points through Go client when authentication enabled -- [#2400](https://github.com/influxdata/influxdb/pull/2400): Always send auth headers for client requests if present - -## v0.9.0-rc26 [04-21-2015] - -### Features -- [#2301](https://github.com/influxdata/influxdb/pull/2301): Distributed query load balancing and failover -- [#2336](https://github.com/influxdata/influxdb/pull/2336): Handle distributed queries when shards != data nodes -- [#2353](https://github.com/influxdata/influxdb/pull/2353): Distributed Query/Clustering Fixes - -### Bugfixes -- [#2297](https://github.com/influxdata/influxdb/pull/2297): create /var/run during startup. Thanks @neonstalwart. -- [#2312](https://github.com/influxdata/influxdb/pull/2312): Re-use httpclient for continuous queries -- [#2318](https://github.com/influxdata/influxdb/pull/2318): Remove pointless use of 'done' channel for collectd. -- [#2242](https://github.com/influxdata/influxdb/pull/2242): Distributed Query should balance requests -- [#2243](https://github.com/influxdata/influxdb/pull/2243): Use Limit Reader instead of fixed 1MB/1GB slice for DQ -- [#2190](https://github.com/influxdata/influxdb/pull/2190): Implement failover to other data nodes for distributed queries -- [#2324](https://github.com/influxdata/influxdb/issues/2324): Race in Broker.Close()/Broker.RunContinousQueryProcessing() -- [#2325](https://github.com/influxdata/influxdb/pull/2325): Cluster open fixes -- [#2326](https://github.com/influxdata/influxdb/pull/2326): Fix parse error in CREATE CONTINUOUS QUERY -- [#2300](https://github.com/influxdata/influxdb/pull/2300): Refactor integration tests. Properly close Graphite/OpenTSDB listeners. -- [#2338](https://github.com/influxdata/influxdb/pull/2338): Fix panic if tag key isn't double quoted when it should have been -- [#2340](https://github.com/influxdata/influxdb/pull/2340): Fix SHOW DIAGNOSTICS panic if any shard was non-local. -- [#2351](https://github.com/influxdata/influxdb/pull/2351): Fix data race by rlocking shard during diagnostics. -- [#2348](https://github.com/influxdata/influxdb/pull/2348): Data node fail to join cluster in 0.9.0rc25 -- [#2343](https://github.com/influxdata/influxdb/pull/2343): Node falls behind Metastore updates -- [#2334](https://github.com/influxdata/influxdb/pull/2334): Test Partial replication is very problematic -- [#2272](https://github.com/influxdata/influxdb/pull/2272): clustering: influxdb 0.9.0-rc23 panics when doing a GET with merge_metrics in a -- [#2350](https://github.com/influxdata/influxdb/pull/2350): Issue fix for :influxd -hostname localhost. -- [#2367](https://github.com/influxdata/influxdb/pull/2367): PR for issue #2350 - Always use localhost, not host name. - -## v0.9.0-rc25 [2015-04-15] - -### Bugfixes -- [#2282](https://github.com/influxdata/influxdb/pull/2282): Use "value" as field name for OpenTSDB input. -- [#2283](https://github.com/influxdata/influxdb/pull/2283): Fix bug when restarting an entire existing cluster. -- [#2293](https://github.com/influxdata/influxdb/pull/2293): Open cluster listener before starting broker. -- [#2287](https://github.com/influxdata/influxdb/pull/2287): Fix data race during SHOW RETENTION POLICIES. -- [#2288](https://github.com/influxdata/influxdb/pull/2288): Fix expression parsing bug. -- [#2294](https://github.com/influxdata/influxdb/pull/2294): Fix async response flushing (invalid chunked response error). - -## Features -- [#2276](https://github.com/influxdata/influxdb/pull/2276): Broker topic truncation. -- [#2292](https://github.com/influxdata/influxdb/pull/2292): Wire up drop CQ statement - thanks @neonstalwart! -- [#2290](https://github.com/influxdata/influxdb/pull/2290): Allow hostname argument to override default config - thanks @neonstalwart! -- [#2295](https://github.com/influxdata/influxdb/pull/2295): Use nil as default return value for MapCount - thanks @neonstalwart! -- [#2246](https://github.com/influxdata/influxdb/pull/2246): Allow HTTP logging to be controlled. - -## v0.9.0-rc24 [2015-04-13] - -### Bugfixes -- [#2255](https://github.com/influxdata/influxdb/pull/2255): Fix panic when changing default retention policy. -- [#2257](https://github.com/influxdata/influxdb/pull/2257): Add "snapshotting" pseudo state & log entry cache. -- [#2261](https://github.com/influxdata/influxdb/pull/2261): Support int64 value types. -- [#2191](https://github.com/influxdata/influxdb/pull/2191): Case-insensitive check for "fill" -- [#2274](https://github.com/influxdata/influxdb/pull/2274): Snapshot and HTTP API endpoints -- [#2265](https://github.com/influxdata/influxdb/pull/2265): Fix auth for CLI. - -## v0.9.0-rc23 [2015-04-11] - -### Features -- [#2202](https://github.com/influxdata/influxdb/pull/2202): Initial implementation of Distributed Queries -- [#2202](https://github.com/influxdata/influxdb/pull/2202): 64-bit Series IDs. INCOMPATIBLE WITH PREVIOUS DATASTORES. - -### Bugfixes -- [#2225](https://github.com/influxdata/influxdb/pull/2225): Make keywords completely case insensitive -- [#2228](https://github.com/influxdata/influxdb/pull/2228): Accept keyword default unquoted in ALTER RETENTION POLICY statement -- [#2236](https://github.com/influxdata/influxdb/pull/2236): Immediate term changes, fix stale write issue, net/http/pprof -- [#2213](https://github.com/influxdata/influxdb/pull/2213): Seed random number generator for election timeout. Thanks @cannium. - -## v0.9.0-rc22 [2015-04-09] - -### Features -- [#2214](https://github.com/influxdata/influxdb/pull/2214): Added the option to influx CLI to execute single command and exit. Thanks @n1tr0g - -### Bugfixes -- [#2223](https://github.com/influxdata/influxdb/pull/2223): Always notify term change on RequestVote - -## v0.9.0-rc21 [2015-04-09] - -### Features -- [#870](https://github.com/influxdata/influxdb/pull/870): Add support for OpenTSDB telnet input protocol. Thanks @tcolgate -- [#2180](https://github.com/influxdata/influxdb/pull/2180): Allow http write handler to decode gzipped body -- [#2175](https://github.com/influxdata/influxdb/pull/2175): Separate broker and data nodes -- [#2158](https://github.com/influxdata/influxdb/pull/2158): Allow user password to be changed. Thanks @n1tr0g -- [#2201](https://github.com/influxdata/influxdb/pull/2201): Bring back config join URLs -- [#2121](https://github.com/influxdata/influxdb/pull/2121): Parser refactor - -### Bugfixes -- [#2181](https://github.com/influxdata/influxdb/pull/2181): Fix panic on "SHOW DIAGNOSTICS". -- [#2170](https://github.com/influxdata/influxdb/pull/2170): Make sure queries on missing tags return 200 status. -- [#2197](https://github.com/influxdata/influxdb/pull/2197): Lock server during Open(). -- [#2200](https://github.com/influxdata/influxdb/pull/2200): Re-enable Continuous Queries. -- [#2203](https://github.com/influxdata/influxdb/pull/2203): Fix race condition on continuous queries. -- [#2217](https://github.com/influxdata/influxdb/pull/2217): Only revert to follower if new term is greater. -- [#2219](https://github.com/influxdata/influxdb/pull/2219): Persist term change to disk when candidate. Thanks @cannium - -## v0.9.0-rc20 [2015-04-04] - -### Features -- [#2128](https://github.com/influxdata/influxdb/pull/2128): Data node discovery from brokers -- [#2142](https://github.com/influxdata/influxdb/pull/2142): Support chunked queries -- [#2154](https://github.com/influxdata/influxdb/pull/2154): Node redirection -- [#2168](https://github.com/influxdata/influxdb/pull/2168): Return raft term from vote, add term logging - -### Bugfixes -- [#2147](https://github.com/influxdata/influxdb/pull/2147): Set Go Max procs in a better location -- [#2137](https://github.com/influxdata/influxdb/pull/2137): Refactor `results` to `response`. Breaking Go Client change. -- [#2151](https://github.com/influxdata/influxdb/pull/2151): Ignore replay commands on the metastore. -- [#2152](https://github.com/influxdata/influxdb/issues/2152): Influxd process with stats enabled crashing with 'Unsuported protocol scheme for ""' -- [#2156](https://github.com/influxdata/influxdb/pull/2156): Propagate error when resolving UDP address in Graphite UDP server. -- [#2163](https://github.com/influxdata/influxdb/pull/2163): Fix up paths for default data and run storage. -- [#2164](https://github.com/influxdata/influxdb/pull/2164): Append STDOUT/STDERR in initscript. -- [#2165](https://github.com/influxdata/influxdb/pull/2165): Better name for config section for stats and diags. -- [#2165](https://github.com/influxdata/influxdb/pull/2165): Monitoring database and retention policy are not configurable. -- [#2167](https://github.com/influxdata/influxdb/pull/2167): Add broker log recovery. -- [#2166](https://github.com/influxdata/influxdb/pull/2166): Don't panic if presented with a field of unknown type. -- [#2149](https://github.com/influxdata/influxdb/pull/2149): Fix unit tests for win32 when directory doesn't exist. -- [#2150](https://github.com/influxdata/influxdb/pull/2150): Fix unit tests for win32 when a connection is refused. - -## v0.9.0-rc19 [2015-04-01] - -### Features -- [#2143](https://github.com/influxdata/influxdb/pull/2143): Add raft term logging. - -### Bugfixes -- [#2145](https://github.com/influxdata/influxdb/pull/2145): Encode toml durations correctly which fixes default configuration generation `influxd config`. - -## v0.9.0-rc18 [2015-03-31] - -### Bugfixes -- [#2100](https://github.com/influxdata/influxdb/pull/2100): Use channel to synchronize collectd shutdown. -- [#2100](https://github.com/influxdata/influxdb/pull/2100): Synchronize access to shard index. -- [#2131](https://github.com/influxdata/influxdb/pull/2131): Optimize marshalTags(). -- [#2130](https://github.com/influxdata/influxdb/pull/2130): Make fewer calls to marshalTags(). -- [#2105](https://github.com/influxdata/influxdb/pull/2105): Support != for tag values. Fix issue #2097, thanks to @smonkewitz for bug report. -- [#2105](https://github.com/influxdata/influxdb/pull/2105): Support !~ tags values. -- [#2138](https://github.com/influxdata/influxdb/pull/2136): Use map for marshaledTags cache. - -## v0.9.0-rc17 [2015-03-29] - -### Features -- [#2076](https://github.com/influxdata/influxdb/pull/2076): Separate stdout and stderr output in init.d script -- [#2091](https://github.com/influxdata/influxdb/pull/2091): Support disabling snapshot endpoint. -- [#2081](https://github.com/influxdata/influxdb/pull/2081): Support writing diagnostic data into the internal database. -- [#2095](https://github.com/influxdata/influxdb/pull/2095): Improved InfluxDB client docs. Thanks @derailed - -### Bugfixes -- [#2093](https://github.com/influxdata/influxdb/pull/2093): Point precision not marshalled correctly. Thanks @derailed -- [#2084](https://github.com/influxdata/influxdb/pull/2084): Allowing leading underscores in identifiers. -- [#2080](https://github.com/influxdata/influxdb/pull/2080): Graphite logs in seconds, not milliseconds. -- [#2101](https://github.com/influxdata/influxdb/pull/2101): SHOW DATABASES should name returned series "databases". -- [#2104](https://github.com/influxdata/influxdb/pull/2104): Include NEQ when calculating field filters. -- [#2112](https://github.com/influxdata/influxdb/pull/2112): Set GOMAXPROCS on startup. This may have been causing extra leader elections, which would cause a number of other bugs or instability. -- [#2111](https://github.com/influxdata/influxdb/pull/2111) and [#2025](https://github.com/influxdata/influxdb/issues/2025): Raft stability fixes. Non-contiguous log error and others. -- [#2114](https://github.com/influxdata/influxdb/pull/2114): Correctly start influxd on platforms without start-stop-daemon. - -## v0.9.0-rc16 [2015-03-24] - -### Features -- [#2058](https://github.com/influxdata/influxdb/pull/2058): Track number of queries executed in stats. -- [#2059](https://github.com/influxdata/influxdb/pull/2059): Retention policies sorted by name on return to client. -- [#2061](https://github.com/influxdata/influxdb/pull/2061): Implement SHOW DIAGNOSTICS. -- [#2064](https://github.com/influxdata/influxdb/pull/2064): Allow init.d script to return influxd version. -- [#2053](https://github.com/influxdata/influxdb/pull/2053): Implment backup and restore. -- [#1631](https://github.com/influxdata/influxdb/pull/1631): Wire up DROP CONTINUOUS QUERY. - -### Bugfixes -- [#2037](https://github.com/influxdata/influxdb/pull/2037): Don't check 'configExists' at Run() level. -- [#2039](https://github.com/influxdata/influxdb/pull/2039): Don't panic if getting current user fails. -- [#2034](https://github.com/influxdata/influxdb/pull/2034): GROUP BY should require an aggregate. -- [#2040](https://github.com/influxdata/influxdb/pull/2040): Add missing top-level help for config command. -- [#2057](https://github.com/influxdata/influxdb/pull/2057): Move racy "in order" test to integration test suite. -- [#2060](https://github.com/influxdata/influxdb/pull/2060): Reload server shard map on restart. -- [#2068](https://github.com/influxdata/influxdb/pull/2068): Fix misspelled JSON field. -- [#2067](https://github.com/influxdata/influxdb/pull/2067): Fixed issue where some queries didn't properly pull back data (introduced in RC15). Fixing intervals for GROUP BY. - -## v0.9.0-rc15 [2015-03-19] - -### Features -- [#2000](https://github.com/influxdata/influxdb/pull/2000): Log broker path when broker fails to start. Thanks @gst. -- [#2007](https://github.com/influxdata/influxdb/pull/2007): Track shard-level stats. - -### Bugfixes -- [#2001](https://github.com/influxdata/influxdb/pull/2001): Ensure measurement not found returns status code 200. -- [#1985](https://github.com/influxdata/influxdb/pull/1985): Set content-type JSON header before actually writing header. Thanks @dstrek. -- [#2003](https://github.com/influxdata/influxdb/pull/2003): Set timestamp when writing monitoring stats. -- [#2004](https://github.com/influxdata/influxdb/pull/2004): Limit group by to MaxGroupByPoints (currently 100,000). -- [#2016](https://github.com/influxdata/influxdb/pull/2016): Fixing bucket alignment for group by. Thanks @jnutzmann -- [#2021](https://github.com/influxdata/influxdb/pull/2021): Remove unnecessary formatting from log message. Thanks @simonkern - - -## v0.9.0-rc14 [2015-03-18] - -### Bugfixes -- [#1999](https://github.com/influxdata/influxdb/pull/1999): Return status code 200 for measurement not found errors on show series. - -## v0.9.0-rc13 [2015-03-17] - -### Features -- [#1974](https://github.com/influxdata/influxdb/pull/1974): Add time taken for request to the http server logs. - -### Bugfixes -- [#1971](https://github.com/influxdata/influxdb/pull/1971): Fix leader id initialization. -- [#1975](https://github.com/influxdata/influxdb/pull/1975): Require `q` parameter for query endpoint. -- [#1969](https://github.com/influxdata/influxdb/pull/1969): Print loaded config. -- [#1987](https://github.com/influxdata/influxdb/pull/1987): Fix config print startup statement for when no config is provided. -- [#1990](https://github.com/influxdata/influxdb/pull/1990): Drop measurement was taking too long due to transactions. - -## v0.9.0-rc12 [2015-03-15] - -### Bugfixes -- [#1942](https://github.com/influxdata/influxdb/pull/1942): Sort wildcard names. -- [#1957](https://github.com/influxdata/influxdb/pull/1957): Graphite numbers are always float64. -- [#1955](https://github.com/influxdata/influxdb/pull/1955): Prohibit creation of databases with no name. Thanks @dullgiulio -- [#1952](https://github.com/influxdata/influxdb/pull/1952): Handle delete statement with an error. Thanks again to @dullgiulio - -### Features -- [#1935](https://github.com/influxdata/influxdb/pull/1935): Implement stateless broker for Raft. -- [#1936](https://github.com/influxdata/influxdb/pull/1936): Implement "SHOW STATS" and self-monitoring - -### Features -- [#1909](https://github.com/influxdata/influxdb/pull/1909): Implement a dump command. - -## v0.9.0-rc11 [2015-03-13] - -### Bugfixes -- [#1917](https://github.com/influxdata/influxdb/pull/1902): Creating Infinite Retention Policy Failed. -- [#1758](https://github.com/influxdata/influxdb/pull/1758): Add Graphite Integration Test. -- [#1929](https://github.com/influxdata/influxdb/pull/1929): Default Retention Policy incorrectly auto created. -- [#1930](https://github.com/influxdata/influxdb/pull/1930): Auto create database for graphite if not specified. -- [#1908](https://github.com/influxdata/influxdb/pull/1908): Cosmetic CLI output fixes. -- [#1931](https://github.com/influxdata/influxdb/pull/1931): Add default column to SHOW RETENTION POLICIES. -- [#1937](https://github.com/influxdata/influxdb/pull/1937): OFFSET should be allowed to be 0. - -### Features -- [#1902](https://github.com/influxdata/influxdb/pull/1902): Enforce retention policies to have a minimum duration. -- [#1906](https://github.com/influxdata/influxdb/pull/1906): Add show servers to query language. -- [#1925](https://github.com/influxdata/influxdb/pull/1925): Add `fill(none)`, `fill(previous)`, and `fill()` to queries. - -## v0.9.0-rc10 [2015-03-09] - -### Bugfixes -- [#1867](https://github.com/influxdata/influxdb/pull/1867): Fix race accessing topic replicas map -- [#1864](https://github.com/influxdata/influxdb/pull/1864): fix race in startStateLoop -- [#1753](https://github.com/influxdata/influxdb/pull/1874): Do Not Panic on Missing Dirs -- [#1877](https://github.com/influxdata/influxdb/pull/1877): Broker clients track broker leader -- [#1862](https://github.com/influxdata/influxdb/pull/1862): Fix memory leak in `httpd.serveWait`. Thanks @mountkin -- [#1883](https://github.com/influxdata/influxdb/pull/1883): RLock server during retention policy enforcement. Thanks @grisha -- [#1868](https://github.com/influxdata/influxdb/pull/1868): Use `BatchPoints` for `client.Write` method. Thanks @vladlopes, @georgmu, @d2g, @evanphx, @akolosov. -- [#1881](https://github.com/influxdata/influxdb/pull/1881): Update documentation for `client` package. Misc library tweaks. -- Fix queries with multiple where clauses on tags, times and fields. Fix queries that have where clauses on fields not in the select - -### Features -- [#1875](https://github.com/influxdata/influxdb/pull/1875): Support trace logging of Raft. -- [#1895](https://github.com/influxdata/influxdb/pull/1895): Auto-create a retention policy when a database is created. -- [#1897](https://github.com/influxdata/influxdb/pull/1897): Pre-create shard groups. -- [#1900](https://github.com/influxdata/influxdb/pull/1900): Change `LIMIT` to `SLIMIT` and implement `LIMIT` and `OFFSET` - -## v0.9.0-rc9 [2015-03-06] - -### Bugfixes -- [#1872](https://github.com/influxdata/influxdb/pull/1872): Fix "stale term" errors with raft - -## v0.9.0-rc8 [2015-03-05] - -### Bugfixes -- [#1836](https://github.com/influxdata/influxdb/pull/1836): Store each parsed shell command in history file. -- [#1789](https://github.com/influxdata/influxdb/pull/1789): add --config-files option to fpm command. Thanks @kylezh -- [#1859](https://github.com/influxdata/influxdb/pull/1859): Queries with a `GROUP BY *` clause were returning a 500 if done against a measurement that didn't exist - -### Features -- [#1755](https://github.com/influxdata/influxdb/pull/1848): Support JSON data ingest over UDP -- [#1857](https://github.com/influxdata/influxdb/pull/1857): Support retention policies with infinite duration -- [#1858](https://github.com/influxdata/influxdb/pull/1858): Enable detailed tracing of write path - -## v0.9.0-rc7 [2015-03-02] - -### Features -- [#1813](https://github.com/influxdata/influxdb/pull/1813): Queries for missing measurements or fields now return a 200 with an error message in the series JSON. -- [#1826](https://github.com/influxdata/influxdb/pull/1826), [#1827](https://github.com/influxdata/influxdb/pull/1827): Fixed queries with `WHERE` clauses against fields. - -### Bugfixes - -- [#1744](https://github.com/influxdata/influxdb/pull/1744): Allow retention policies to be modified without specifying replication factor. Thanks @kylezh -- [#1809](https://github.com/influxdata/influxdb/pull/1809): Packaging post-install script unconditionally removes init.d symlink. Thanks @sineos - -## v0.9.0-rc6 [2015-02-27] - -### Bugfixes - -- [#1780](https://github.com/influxdata/influxdb/pull/1780): Malformed identifiers get through the parser -- [#1775](https://github.com/influxdata/influxdb/pull/1775): Panic "index out of range" on some queries -- [#1744](https://github.com/influxdata/influxdb/pull/1744): Select shard groups which completely encompass time range. Thanks @kylezh. - -## v0.9.0-rc5 [2015-02-27] - -### Bugfixes - -- [#1752](https://github.com/influxdata/influxdb/pull/1752): remove debug log output from collectd. -- [#1720](https://github.com/influxdata/influxdb/pull/1720): Parse Series IDs as unsigned 32-bits. -- [#1767](https://github.com/influxdata/influxdb/pull/1767): Drop Series was failing across shards. Issue #1761. -- [#1773](https://github.com/influxdata/influxdb/pull/1773): Fix bug when merging series together that have unequal number of points in a group by interval -- [#1771](https://github.com/influxdata/influxdb/pull/1771): Make `SHOW SERIES` return IDs and support `LIMIT` and `OFFSET` - -### Features - -- [#1698](https://github.com/influxdata/influxdb/pull/1698): Wire up DROP MEASUREMENT - -## v0.9.0-rc4 [2015-02-24] - -### Bugfixes - -- Fix authentication issue with continuous queries -- Print version in the log on startup - -## v0.9.0-rc3 [2015-02-23] - -### Features - -- [#1659](https://github.com/influxdata/influxdb/pull/1659): WHERE against regexes: `WHERE =~ '.*asdf' -- [#1580](https://github.com/influxdata/influxdb/pull/1580): Add support for fields with bool, int, or string data types -- [#1687](https://github.com/influxdata/influxdb/pull/1687): Change `Rows` to `Series` in results output. BREAKING API CHANGE -- [#1629](https://github.com/influxdata/influxdb/pull/1629): Add support for `DROP SERIES` queries -- [#1632](https://github.com/influxdata/influxdb/pull/1632): Add support for `GROUP BY *` to return all series within a measurement -- [#1689](https://github.com/influxdata/influxdb/pull/1689): Change `SHOW TAG VALUES WITH KEY="foo"` to use the key name in the result. BREAKING API CHANGE -- [#1699](https://github.com/influxdata/influxdb/pull/1699): Add CPU and memory profiling options to daemon -- [#1672](https://github.com/influxdata/influxdb/pull/1672): Add index tracking to metastore. Makes downed node recovery actually work -- [#1591](https://github.com/influxdata/influxdb/pull/1591): Add `spread` aggregate function -- [#1576](https://github.com/influxdata/influxdb/pull/1576): Add `first` and `last` aggregate functions -- [#1573](https://github.com/influxdata/influxdb/pull/1573): Add `stddev` aggregate function -- [#1565](https://github.com/influxdata/influxdb/pull/1565): Add the admin interface back into the server and update for new API -- [#1562](https://github.com/influxdata/influxdb/pull/1562): Enforce retention policies -- [#1700](https://github.com/influxdata/influxdb/pull/1700): Change `Values` to `Fields` on writes. BREAKING API CHANGE -- [#1706](https://github.com/influxdata/influxdb/pull/1706): Add support for `LIMIT` and `OFFSET`, which work on the number of series returned in a query. To limit the number of data points use a `WHERE time` clause - -### Bugfixes - -- [#1636](https://github.com/influxdata/influxdb/issues/1636): Don't store number of fields in raw data. THIS IS A BREAKING DATA CHANGE. YOU MUST START WITH A FRESH DATABASE -- [#1701](https://github.com/influxdata/influxdb/pull/1701), [#1667](https://github.com/influxdata/influxdb/pull/1667), [#1663](https://github.com/influxdata/influxdb/pull/1663), [#1615](https://github.com/influxdata/influxdb/pull/1615): Raft fixes -- [#1644](https://github.com/influxdata/influxdb/pull/1644): Add batching support for significantly improved write performance -- [#1704](https://github.com/influxdata/influxdb/pull/1704): Fix queries that pull back raw data (i.e. ones without aggregate functions) -- [#1718](https://github.com/influxdata/influxdb/pull/1718): Return an error on write if any of the points are don't have at least one field -- [#1806](https://github.com/influxdata/influxdb/pull/1806): Fix regex parsing. Change regex syntax to use / delimiters. - - -## v0.9.0-rc1,2 [no public release] - -### Features - -- Support for tags added -- New queries for showing measurement names, tag keys, and tag values -- Renamed shard spaces to retention policies -- Deprecated matching against regex in favor of explicit writing and querying on retention policies -- Pure Go InfluxQL parser -- Switch to BoltDB as underlying datastore -- BoltDB backed metastore to store schema information -- Updated HTTP API to only have two endpoints `/query` and `/write` -- Added all administrative functions to the query language -- Change cluster architecture to have brokers and data nodes -- Switch to streaming Raft implementation -- In memory inverted index of the tag data -- Pure Go implementation! - -## v0.8.6 [2014-11-15] - -### Features - -- [Issue #973](https://github.com/influxdata/influxdb/issues/973). Support - joining using a regex or list of time series -- [Issue #1068](https://github.com/influxdata/influxdb/issues/1068). Print - the processor chain when the query is started - -### Bugfixes - -- [Issue #584](https://github.com/influxdata/influxdb/issues/584). Don't - panic if the process died while initializing -- [Issue #663](https://github.com/influxdata/influxdb/issues/663). Make - sure all sub servies are closed when are stopping InfluxDB -- [Issue #671](https://github.com/influxdata/influxdb/issues/671). Fix - the Makefile package target for Mac OSX -- [Issue #800](https://github.com/influxdata/influxdb/issues/800). Use - su instead of sudo in the init script. This fixes the startup problem - on RHEL 6. -- [Issue #925](https://github.com/influxdata/influxdb/issues/925). Don't - generate invalid query strings for single point queries -- [Issue #943](https://github.com/influxdata/influxdb/issues/943). Don't - take two snapshots at the same time -- [Issue #947](https://github.com/influxdata/influxdb/issues/947). Exit - nicely if the daemon doesn't have permission to write to the log. -- [Issue #959](https://github.com/influxdata/influxdb/issues/959). Stop using - closed connections in the protobuf client. -- [Issue #978](https://github.com/influxdata/influxdb/issues/978). Check - for valgrind and mercurial in the configure script -- [Issue #996](https://github.com/influxdata/influxdb/issues/996). Fill should - fill the time range even if no points exists in the given time range -- [Issue #1008](https://github.com/influxdata/influxdb/issues/1008). Return - an appropriate exit status code depending on whether the process exits - due to an error or exits gracefully. -- [Issue #1024](https://github.com/influxdata/influxdb/issues/1024). Hitting - open files limit causes influxdb to create shards in loop. -- [Issue #1069](https://github.com/influxdata/influxdb/issues/1069). Fix - deprecated interface endpoint in Admin UI. -- [Issue #1076](https://github.com/influxdata/influxdb/issues/1076). Fix - the timestamps of data points written by the collectd plugin. (Thanks, - @renchap for reporting this bug) -- [Issue #1078](https://github.com/influxdata/influxdb/issues/1078). Make sure - we don't resurrect shard directories for shards that have already expired -- [Issue #1085](https://github.com/influxdata/influxdb/issues/1085). Set - the connection string of the local raft node -- [Issue #1092](https://github.com/influxdata/influxdb/issues/1093). Set - the connection string of the local node in the raft snapshot. -- [Issue #1100](https://github.com/influxdata/influxdb/issues/1100). Removing - a non-existent shard space causes the cluster to panic. -- [Issue #1113](https://github.com/influxdata/influxdb/issues/1113). A nil - engine.ProcessorChain causes a panic. - -## v0.8.5 [2014-10-27] - -### Features - -- [Issue #1055](https://github.com/influxdata/influxdb/issues/1055). Allow - graphite and collectd input plugins to have separate binding address - -### Bugfixes - -- [Issue #1058](https://github.com/influxdata/influxdb/issues/1058). Use - the query language instead of the continuous query endpoints that - were removed in 0.8.4 -- [Issue #1022](https://github.com/influxdata/influxdb/issues/1022). Return - an +Inf or NaN instead of panicing when we encounter a divide by zero -- [Issue #821](https://github.com/influxdata/influxdb/issues/821). Don't - scan through points when we hit the limit -- [Issue #1051](https://github.com/influxdata/influxdb/issues/1051). Fix - timestamps when the collectd is used and low resolution timestamps - is set. - -## v0.8.4 [2014-10-24] - -### Bugfixes - -- Remove the continuous query api endpoints since the query language - has all the features needed to list and delete continuous queries. -- [Issue #778](https://github.com/influxdata/influxdb/issues/778). Selecting - from a non-existent series should give a better error message indicating - that the series doesn't exist -- [Issue #988](https://github.com/influxdata/influxdb/issues/988). Check - the arguments of `top()` and `bottom()` -- [Issue #1021](https://github.com/influxdata/influxdb/issues/1021). Make - redirecting to standard output and standard error optional instead of - going to `/dev/null`. This can now be configured by setting `$STDOUT` - in `/etc/default/influxdb` -- [Issue #985](https://github.com/influxdata/influxdb/issues/985). Make - sure we drop a shard only when there's no one using it. Otherwise, the - shard can be closed when another goroutine is writing to it which will - cause random errors and possibly corruption of the database. - -### Features - -- [Issue #1047](https://github.com/influxdata/influxdb/issues/1047). Allow - merge() to take a list of series (as opposed to a regex in #72) - -## v0.8.4-rc.1 [2014-10-21] - -### Bugfixes - -- [Issue #1040](https://github.com/influxdata/influxdb/issues/1040). Revert - to older raft snapshot if the latest one is corrupted -- [Issue #1004](https://github.com/influxdata/influxdb/issues/1004). Querying - for data outside of existing shards returns an empty response instead of - throwing a `Couldn't lookup columns` error -- [Issue #1020](https://github.com/influxdata/influxdb/issues/1020). Change - init script exit codes to conform to the lsb standards. (Thanks, @spuder) -- [Issue #1011](https://github.com/influxdata/influxdb/issues/1011). Fix - the tarball for homebrew so that rocksdb is included and the directory - structure is clean -- [Issue #1007](https://github.com/influxdata/influxdb/issues/1007). Fix - the content type when an error occurs and the client requests - compression. -- [Issue #916](https://github.com/influxdata/influxdb/issues/916). Set - the ulimit in the init script with a way to override the limit -- [Issue #742](https://github.com/influxdata/influxdb/issues/742). Fix - rocksdb for Mac OSX -- [Issue #387](https://github.com/influxdata/influxdb/issues/387). Aggregations - with group by time(1w), time(1m) and time(1y) (for week, month and - year respectively) will cause the start time and end time of the bucket - to fall on the logical boundaries of the week, month or year. -- [Issue #334](https://github.com/influxdata/influxdb/issues/334). Derivative - for queries with group by time() and fill(), will take the difference - between the first value in the bucket and the first value of the next - bucket. -- [Issue #972](https://github.com/influxdata/influxdb/issues/972). Don't - assign duplicate server ids - -### Features - -- [Issue #722](https://github.com/influxdata/influxdb/issues/722). Add - an install target to the Makefile -- [Issue #1032](https://github.com/influxdata/influxdb/issues/1032). Include - the admin ui static assets in the binary -- [Issue #1019](https://github.com/influxdata/influxdb/issues/1019). Upgrade - to rocksdb 3.5.1 -- [Issue #992](https://github.com/influxdata/influxdb/issues/992). Add - an input plugin for collectd. (Thanks, @kimor79) -- [Issue #72](https://github.com/influxdata/influxdb/issues/72). Support merge - for multiple series using regex syntax - -## v0.8.3 [2014-09-24] - -### Bugfixes - -- [Issue #885](https://github.com/influxdata/influxdb/issues/885). Multiple - queries separated by semicolons work as expected. Queries are process - sequentially -- [Issue #652](https://github.com/influxdata/influxdb/issues/652). Return an - error if an invalid column is used in the where clause -- [Issue #794](https://github.com/influxdata/influxdb/issues/794). Fix case - insensitive regex matching -- [Issue #853](https://github.com/influxdata/influxdb/issues/853). Move - cluster config from raft to API. -- [Issue #714](https://github.com/influxdata/influxdb/issues/714). Don't - panic on invalid boolean operators. -- [Issue #843](https://github.com/influxdata/influxdb/issues/843). Prevent blank database names -- [Issue #780](https://github.com/influxdata/influxdb/issues/780). Fix - fill() for all aggregators -- [Issue #923](https://github.com/influxdata/influxdb/issues/923). Enclose - table names in double quotes in the result of GetQueryString() -- [Issue #923](https://github.com/influxdata/influxdb/issues/923). Enclose - table names in double quotes in the result of GetQueryString() -- [Issue #967](https://github.com/influxdata/influxdb/issues/967). Return an - error if the storage engine can't be created -- [Issue #954](https://github.com/influxdata/influxdb/issues/954). Don't automatically - create shards which was causing too many shards to be created when used with - grafana -- [Issue #939](https://github.com/influxdata/influxdb/issues/939). Aggregation should - ignore null values and invalid values, e.g. strings with mean(). -- [Issue #964](https://github.com/influxdata/influxdb/issues/964). Parse - big int in queries properly. - -## v0.8.2 [2014-09-05] - -### Bugfixes - -- [Issue #886](https://github.com/influxdata/influxdb/issues/886). Update shard space to not set defaults - -- [Issue #867](https://github.com/influxdata/influxdb/issues/867). Add option to return shard space mappings in list series - -### Bugfixes - -- [Issue #652](https://github.com/influxdata/influxdb/issues/652). Return - a meaningful error if an invalid column is used in where clause - after joining multiple series - -## v0.8.2 [2014-09-08] - -### Features - -- Added API endpoint to update shard space definitions - -### Bugfixes - -- [Issue #886](https://github.com/influxdata/influxdb/issues/886). Shard space regexes reset after restart of InfluxDB - -## v0.8.1 [2014-09-03] - -- [Issue #896](https://github.com/influxdata/influxdb/issues/896). Allow logging to syslog. Thanks @malthe - -### Bugfixes - -- [Issue #868](https://github.com/influxdata/influxdb/issues/868). Don't panic when upgrading a snapshot from 0.7.x -- [Issue #887](https://github.com/influxdata/influxdb/issues/887). The first continuous query shouldn't trigger backfill if it had backfill disabled -- [Issue #674](https://github.com/influxdata/influxdb/issues/674). Graceful exit when config file is invalid. (Thanks, @DavidBord) -- [Issue #857](https://github.com/influxdata/influxdb/issues/857). More informative list servers api. (Thanks, @oliveagle) - -## v0.8.0 [2014-08-22] - -### Features - -- [Issue #850](https://github.com/influxdata/influxdb/issues/850). Makes the server listing more informative - -### Bugfixes - -- [Issue #779](https://github.com/influxdata/influxdb/issues/779). Deleting expired shards isn't thread safe. -- [Issue #860](https://github.com/influxdata/influxdb/issues/860). Load database config should validate shard spaces. -- [Issue #862](https://github.com/influxdata/influxdb/issues/862). Data migrator should have option to set delay time. - -## v0.8.0-rc.5 [2014-08-15] - -### Features - -- [Issue #376](https://github.com/influxdata/influxdb/issues/376). List series should support regex filtering -- [Issue #745](https://github.com/influxdata/influxdb/issues/745). Add continuous queries to the database config -- [Issue #746](https://github.com/influxdata/influxdb/issues/746). Add data migration tool for 0.8.0 - -### Bugfixes - -- [Issue #426](https://github.com/influxdata/influxdb/issues/426). Fill should fill the entire time range that is requested -- [Issue #740](https://github.com/influxdata/influxdb/issues/740). Don't emit non existent fields when joining series with different fields -- [Issue #744](https://github.com/influxdata/influxdb/issues/744). Admin site should have all assets locally -- [Issue #767](https://github.com/influxdata/influxdb/issues/768). Remove shards whenever they expire -- [Issue #781](https://github.com/influxdata/influxdb/issues/781). Don't emit non existent fields when joining series with different fields -- [Issue #791](https://github.com/influxdata/influxdb/issues/791). Move database config loader to be an API endpoint -- [Issue #809](https://github.com/influxdata/influxdb/issues/809). Migration path from 0.7 -> 0.8 -- [Issue #811](https://github.com/influxdata/influxdb/issues/811). Gogoprotobuf removed `ErrWrongType`, which is depended on by Raft -- [Issue #820](https://github.com/influxdata/influxdb/issues/820). Query non-local shard with time range to avoid getting back points not in time range -- [Issue #827](https://github.com/influxdata/influxdb/issues/827). Don't leak file descriptors in the WAL -- [Issue #830](https://github.com/influxdata/influxdb/issues/830). List series should return series in lexicographic sorted order -- [Issue #831](https://github.com/influxdata/influxdb/issues/831). Move create shard space to be db specific - -## v0.8.0-rc.4 [2014-07-29] - -### Bugfixes - -- [Issue #774](https://github.com/influxdata/influxdb/issues/774). Don't try to parse "inf" shard retention policy -- [Issue #769](https://github.com/influxdata/influxdb/issues/769). Use retention duration when determining expired shards. (Thanks, @shugo) -- [Issue #736](https://github.com/influxdata/influxdb/issues/736). Only db admins should be able to drop a series -- [Issue #713](https://github.com/influxdata/influxdb/issues/713). Null should be a valid fill value -- [Issue #644](https://github.com/influxdata/influxdb/issues/644). Graphite api should write data in batches to the coordinator -- [Issue #740](https://github.com/influxdata/influxdb/issues/740). Panic when distinct fields are selected from an inner join -- [Issue #781](https://github.com/influxdata/influxdb/issues/781). Panic when distinct fields are added after an inner join - -## v0.8.0-rc.3 [2014-07-21] - -### Bugfixes - -- [Issue #752](https://github.com/influxdata/influxdb/issues/752). `./configure` should use goroot to find gofmt -- [Issue #758](https://github.com/influxdata/influxdb/issues/758). Clarify the reason behind graphite input plugin not starting. (Thanks, @otoolep) -- [Issue #759](https://github.com/influxdata/influxdb/issues/759). Don't revert the regex in the shard space. (Thanks, @shugo) -- [Issue #760](https://github.com/influxdata/influxdb/issues/760). Removing a server should remove it from the shard server ids. (Thanks, @shugo) -- [Issue #772](https://github.com/influxdata/influxdb/issues/772). Add sentinel values to all db. This caused the last key in the db to not be fetched properly. - - -## v0.8.0-rc.2 [2014-07-15] - -- This release is to fix a build error in rc1 which caused rocksdb to not be available -- Bump up the `max-open-files` option to 1000 on all storage engines -- Lower the `write-buffer-size` to 1000 - -## v0.8.0-rc.1 [2014-07-15] - -### Features - -- [Issue #643](https://github.com/influxdata/influxdb/issues/643). Support pretty print json. (Thanks, @otoolep) -- [Issue #641](https://github.com/influxdata/influxdb/issues/641). Support multiple storage engines -- [Issue #665](https://github.com/influxdata/influxdb/issues/665). Make build tmp directory configurable in the make file. (Thanks, @dgnorton) -- [Issue #667](https://github.com/influxdata/influxdb/issues/667). Enable compression on all GET requests and when writing data -- [Issue #648](https://github.com/influxdata/influxdb/issues/648). Return permissions when listing db users. (Thanks, @nicolai86) -- [Issue #682](https://github.com/influxdata/influxdb/issues/682). Allow continuous queries to run without backfill (Thanks, @dhammika) -- [Issue #689](https://github.com/influxdata/influxdb/issues/689). **REQUIRES DATA MIGRATION** Move metadata into raft -- [Issue #255](https://github.com/influxdata/influxdb/issues/255). Support millisecond precision using `ms` suffix -- [Issue #95](https://github.com/influxdata/influxdb/issues/95). Drop database should not be synchronous -- [Issue #571](https://github.com/influxdata/influxdb/issues/571). Add support for arbitrary number of shard spaces and retention policies -- Default storage engine changed to RocksDB - -### Bugfixes - -- [Issue #651](https://github.com/influxdata/influxdb/issues/651). Change permissions of symlink which fix some installation issues. (Thanks, @Dieterbe) -- [Issue #670](https://github.com/influxdata/influxdb/issues/670). Don't warn on missing influxdb user on fresh installs -- [Issue #676](https://github.com/influxdata/influxdb/issues/676). Allow storing high precision integer values without losing any information -- [Issue #695](https://github.com/influxdata/influxdb/issues/695). Prevent having duplicate field names in the write payload. (Thanks, @seunglee150) -- [Issue #731](https://github.com/influxdata/influxdb/issues/731). Don't enable the udp plugin if the `enabled` option is set to false -- [Issue #733](https://github.com/influxdata/influxdb/issues/733). Print an `INFO` message when the input plugin is disabled -- [Issue #707](https://github.com/influxdata/influxdb/issues/707). Graphite input plugin should work payload delimited by any whitespace character -- [Issue #734](https://github.com/influxdata/influxdb/issues/734). Don't buffer non replicated writes -- [Issue #465](https://github.com/influxdata/influxdb/issues/465). Recreating a currently deleting db or series doesn't bring back the old data anymore -- [Issue #358](https://github.com/influxdata/influxdb/issues/358). **BREAKING** List series should return as a single series -- [Issue #499](https://github.com/influxdata/influxdb/issues/499). **BREAKING** Querying non-existent database or series will return an error -- [Issue #570](https://github.com/influxdata/influxdb/issues/570). InfluxDB crashes during delete/drop of database -- [Issue #592](https://github.com/influxdata/influxdb/issues/592). Drop series is inefficient - -## v0.7.3 [2014-06-13] - -### Bugfixes - -- [Issue #637](https://github.com/influxdata/influxdb/issues/637). Truncate log files if the last request wasn't written properly -- [Issue #646](https://github.com/influxdata/influxdb/issues/646). CRITICAL: Duplicate shard ids for new shards if old shards are deleted. - -## v0.7.2 [2014-05-30] - -### Features - -- [Issue #521](https://github.com/influxdata/influxdb/issues/521). MODE works on all datatypes (Thanks, @richthegeek) - -### Bugfixes - -- [Issue #418](https://github.com/influxdata/influxdb/pull/418). Requests or responses larger than MAX_REQUEST_SIZE break things. -- [Issue #606](https://github.com/influxdata/influxdb/issues/606). InfluxDB will fail to start with invalid permission if log.txt didn't exist -- [Issue #602](https://github.com/influxdata/influxdb/issues/602). Merge will fail to work across shards - -### Features - -## v0.7.1 [2014-05-29] - -### Bugfixes - -- [Issue #579](https://github.com/influxdata/influxdb/issues/579). Reject writes to nonexistent databases -- [Issue #597](https://github.com/influxdata/influxdb/issues/597). Force compaction after deleting data - -### Features - -- [Issue #476](https://github.com/influxdata/influxdb/issues/476). Support ARM architecture -- [Issue #578](https://github.com/influxdata/influxdb/issues/578). Support aliasing for expressions in parenthesis -- [Issue #544](https://github.com/influxdata/influxdb/pull/544). Support forcing node removal from a cluster -- [Issue #591](https://github.com/influxdata/influxdb/pull/591). Support multiple udp input plugins (Thanks, @tpitale) -- [Issue #600](https://github.com/influxdata/influxdb/pull/600). Report version, os, arch, and raftName once per day. - -## v0.7.0 [2014-05-23] - -### Bugfixes - -- [Issue #557](https://github.com/influxdata/influxdb/issues/557). Group by time(1y) doesn't work while time(365d) works -- [Issue #547](https://github.com/influxdata/influxdb/issues/547). Add difference function (Thanks, @mboelstra) -- [Issue #550](https://github.com/influxdata/influxdb/issues/550). Fix tests on 32-bit ARM -- [Issue #524](https://github.com/influxdata/influxdb/issues/524). Arithmetic operators and where conditions don't play nice together -- [Issue #561](https://github.com/influxdata/influxdb/issues/561). Fix missing query in parsing errors -- [Issue #563](https://github.com/influxdata/influxdb/issues/563). Add sample config for graphite over udp -- [Issue #537](https://github.com/influxdata/influxdb/issues/537). Incorrect query syntax causes internal error -- [Issue #565](https://github.com/influxdata/influxdb/issues/565). Empty series names shouldn't cause a panic -- [Issue #575](https://github.com/influxdata/influxdb/issues/575). Single point select doesn't interpret timestamps correctly -- [Issue #576](https://github.com/influxdata/influxdb/issues/576). We shouldn't set timestamps and sequence numbers when listing cq -- [Issue #560](https://github.com/influxdata/influxdb/issues/560). Use /dev/urandom instead of /dev/random -- [Issue #502](https://github.com/influxdata/influxdb/issues/502). Fix a - race condition in assigning id to db+series+field (Thanks @ohurvitz - for reporting this bug and providing a script to repro) - -### Features - -- [Issue #567](https://github.com/influxdata/influxdb/issues/567). Allow selecting from multiple series names by separating them with commas (Thanks, @peekeri) - -### Deprecated - -- [Issue #460](https://github.com/influxdata/influxdb/issues/460). Don't start automatically after installing -- [Issue #529](https://github.com/influxdata/influxdb/issues/529). Don't run influxdb as root -- [Issue #443](https://github.com/influxdata/influxdb/issues/443). Use `name` instead of `username` when returning cluster admins - -## v0.6.5 [2014-05-19] - -### Features - -- [Issue #551](https://github.com/influxdata/influxdb/issues/551). Add TOP and BOTTOM aggregate functions (Thanks, @chobie) - -### Bugfixes - -- [Issue #555](https://github.com/influxdata/influxdb/issues/555). Fix a regression introduced in the raft snapshot format - -## v0.6.4 [2014-05-16] - -### Features - -- Make the write batch size configurable (also applies to deletes) -- Optimize writing to multiple series -- [Issue #546](https://github.com/influxdata/influxdb/issues/546). Add UDP support for Graphite API (Thanks, @peekeri) - -### Bugfixes - -- Fix a bug in shard logic that caused short term shards to be clobbered with long term shards -- [Issue #489](https://github.com/influxdata/influxdb/issues/489). Remove replication factor from CreateDatabase command - -## v0.6.3 [2014-05-13] - -### Features - -- [Issue #505](https://github.com/influxdata/influxdb/issues/505). Return a version header with http the response (Thanks, @majst01) -- [Issue #520](https://github.com/influxdata/influxdb/issues/520). Print the version to the log file - -### Bugfixes - -- [Issue #516](https://github.com/influxdata/influxdb/issues/516). Close WAL log/index files when they aren't being used -- [Issue #532](https://github.com/influxdata/influxdb/issues/532). Don't log graphite connection EOF as an error -- [Issue #535](https://github.com/influxdata/influxdb/issues/535). WAL Replay hangs if response isn't received -- [Issue #538](https://github.com/influxdata/influxdb/issues/538). Don't panic if the same series existed twice in the request with different columns -- [Issue #536](https://github.com/influxdata/influxdb/issues/536). Joining the cluster after shards are creating shouldn't cause new nodes to panic -- [Issue #539](https://github.com/influxdata/influxdb/issues/539). count(distinct()) with fill shouldn't panic on empty groups -- [Issue #534](https://github.com/influxdata/influxdb/issues/534). Create a new series when interpolating - -## v0.6.2 [2014-05-09] - -### Bugfixes - -- [Issue #511](https://github.com/influxdata/influxdb/issues/511). Don't automatically create the database when a db user is created -- [Issue #512](https://github.com/influxdata/influxdb/issues/512). Group by should respect null values -- [Issue #518](https://github.com/influxdata/influxdb/issues/518). Filter Infinities and NaNs from the returned json -- [Issue #522](https://github.com/influxdata/influxdb/issues/522). Committing requests while replaying caused the WAL to skip some log files -- [Issue #369](https://github.com/influxdata/influxdb/issues/369). Fix some edge cases with WAL recovery - -## v0.6.1 [2014-05-06] - -### Bugfixes - -- [Issue #500](https://github.com/influxdata/influxdb/issues/500). Support `y` suffix in time durations -- [Issue #501](https://github.com/influxdata/influxdb/issues/501). Writes with invalid payload should be rejected -- [Issue #507](https://github.com/influxdata/influxdb/issues/507). New cluster admin passwords don't propagate properly to other nodes in a cluster -- [Issue #508](https://github.com/influxdata/influxdb/issues/508). Don't replay WAL entries for servers with no shards -- [Issue #464](https://github.com/influxdata/influxdb/issues/464). Admin UI shouldn't draw graphs for string columns -- [Issue #480](https://github.com/influxdata/influxdb/issues/480). Large values on the y-axis get cut off - -## v0.6.0 [2014-05-02] - -### Feature - -- [Issue #477](https://github.com/influxdata/influxdb/issues/477). Add a udp json interface (Thanks, Julien Ammous) -- [Issue #491](https://github.com/influxdata/influxdb/issues/491). Make initial root password settable through env variable (Thanks, Edward Muller) - -### Bugfixes - -- [Issue #469](https://github.com/influxdata/influxdb/issues/469). Drop continuous queries when a database is dropped -- [Issue #431](https://github.com/influxdata/influxdb/issues/431). Don't log to standard output if a log file is specified in the config file -- [Issue #483](https://github.com/influxdata/influxdb/issues/483). Return 409 if a database already exist (Thanks, Edward Muller) -- [Issue #486](https://github.com/influxdata/influxdb/issues/486). Columns used in the target of continuous query shouldn't be inserted in the time series -- [Issue #490](https://github.com/influxdata/influxdb/issues/490). Database user password's cannot be changed (Thanks, Edward Muller) -- [Issue #495](https://github.com/influxdata/influxdb/issues/495). Enforce write permissions properly - -## v0.5.12 [2014-04-29] - -### Bugfixes - -- [Issue #419](https://github.com/influxdata/influxdb/issues/419),[Issue #478](https://github.com/influxdata/influxdb/issues/478). Allow hostname, raft and protobuf ports to be changed, without requiring manual intervention from the user - -## v0.5.11 [2014-04-25] - -### Features - -- [Issue #471](https://github.com/influxdata/influxdb/issues/471). Read and write permissions should be settable through the http api - -### Bugfixes - -- [Issue #323](https://github.com/influxdata/influxdb/issues/323). Continuous queries should guard against data loops -- [Issue #473](https://github.com/influxdata/influxdb/issues/473). Engine memory optimization - -## v0.5.10 [2014-04-22] - -### Features - -- [Issue #463](https://github.com/influxdata/influxdb/issues/463). Allow series names to use any character (escape by wrapping in double quotes) -- [Issue #447](https://github.com/influxdata/influxdb/issues/447). Allow @ in usernames -- [Issue #466](https://github.com/influxdata/influxdb/issues/466). Allow column names to use any character (escape by wrapping in double quotes) - -### Bugfixes - -- [Issue #458](https://github.com/influxdata/influxdb/issues/458). Continuous queries with group by time() and a column should insert sequence numbers of 1 -- [Issue #457](https://github.com/influxdata/influxdb/issues/457). Deleting series that start with capital letters should work - -## v0.5.9 [2014-04-18] - -### Bugfixes - -- [Issue #446](https://github.com/influxdata/influxdb/issues/446). Check for (de)serialization errors -- [Issue #456](https://github.com/influxdata/influxdb/issues/456). Continuous queries failed if one of the group by columns had null value -- [Issue #455](https://github.com/influxdata/influxdb/issues/455). Comparison operators should ignore null values - -## v0.5.8 [2014-04-17] - -- Renamed config.toml.sample to config.sample.toml - -### Bugfixes - -- [Issue #244](https://github.com/influxdata/influxdb/issues/244). Reconstruct the query from the ast -- [Issue #449](https://github.com/influxdata/influxdb/issues/449). Heartbeat timeouts can cause reading from connection to lock up -- [Issue #451](https://github.com/influxdata/influxdb/issues/451). Reduce the aggregation state that is kept in memory so that - aggregation queries over large periods of time don't take insance amount of memory - -## v0.5.7 [2014-04-15] - -### Features - -- Queries are now logged as INFO in the log file before they run - -### Bugfixes - -- [Issue #328](https://github.com/influxdata/influxdb/issues/328). Join queries with math expressions don't work -- [Issue #440](https://github.com/influxdata/influxdb/issues/440). Heartbeat timeouts in logs -- [Issue #442](https://github.com/influxdata/influxdb/issues/442). shouldQuerySequentially didn't work as expected - causing count(*) queries on large time series to use - lots of memory -- [Issue #437](https://github.com/influxdata/influxdb/issues/437). Queries with negative constants don't parse properly -- [Issue #432](https://github.com/influxdata/influxdb/issues/432). Deleted data using a delete query is resurrected after a server restart -- [Issue #439](https://github.com/influxdata/influxdb/issues/439). Report the right location of the error in the query -- Fix some bugs with the WAL recovery on startup - -## v0.5.6 [2014-04-08] - -### Features - -- [Issue #310](https://github.com/influxdata/influxdb/issues/310). Request should support multiple timeseries -- [Issue #416](https://github.com/influxdata/influxdb/issues/416). Improve the time it takes to drop database - -### Bugfixes - -- [Issue #413](https://github.com/influxdata/influxdb/issues/413). Don't assume that group by interval is greater than a second -- [Issue #415](https://github.com/influxdata/influxdb/issues/415). Include the database when sending an auth error back to the user -- [Issue #421](https://github.com/influxdata/influxdb/issues/421). Make read timeout a config option -- [Issue #392](https://github.com/influxdata/influxdb/issues/392). Different columns in different shards returns invalid results when a query spans those shards - -### Bugfixes - -## v0.5.5 [2014-04-04] - -- Upgrade leveldb 1.10 -> 1.15 - - This should be a backward compatible change, but is here for documentation only - -### Feature - -- Add a command line option to repair corrupted leveldb databases on startup -- [Issue #401](https://github.com/influxdata/influxdb/issues/401). No limit on the number of columns in the group by clause - -### Bugfixes - -- [Issue #398](https://github.com/influxdata/influxdb/issues/398). Support now() and NOW() in the query lang -- [Issue #403](https://github.com/influxdata/influxdb/issues/403). Filtering should work with join queries -- [Issue #404](https://github.com/influxdata/influxdb/issues/404). Filtering with invalid condition shouldn't crash the server -- [Issue #405](https://github.com/influxdata/influxdb/issues/405). Percentile shouldn't crash for small number of values -- [Issue #408](https://github.com/influxdata/influxdb/issues/408). Make InfluxDB recover from internal bugs and panics -- [Issue #390](https://github.com/influxdata/influxdb/issues/390). Multiple response.WriteHeader when querying as admin -- [Issue #407](https://github.com/influxdata/influxdb/issues/407). Start processing continuous queries only after the WAL is initialized -- Close leveldb databases properly if we couldn't create a new Shard. See leveldb\_shard\_datastore\_test:131 - -## v0.5.4 [2014-04-02] - -### Bugfixes - -- [Issue #386](https://github.com/influxdata/influxdb/issues/386). Drop series should work with series containing dots -- [Issue #389](https://github.com/influxdata/influxdb/issues/389). Filtering shouldn't stop prematurely -- [Issue #341](https://github.com/influxdata/influxdb/issues/341). Make the number of shards that are queried in parallel configurable -- [Issue #394](https://github.com/influxdata/influxdb/issues/394). Support count(distinct) and count(DISTINCT) -- [Issue #362](https://github.com/influxdata/influxdb/issues/362). Limit should be enforced after aggregation - -## v0.5.3 [2014-03-31] - -### Bugfixes - -- [Issue #378](https://github.com/influxdata/influxdb/issues/378). Indexing should return if there are no requests added since the last index -- [Issue #370](https://github.com/influxdata/influxdb/issues/370). Filtering and limit should be enforced on the shards -- [Issue #379](https://github.com/influxdata/influxdb/issues/379). Boolean columns should be usable in where clauses -- [Issue #381](https://github.com/influxdata/influxdb/issues/381). Should be able to do deletes as a cluster admin - -## v0.5.2 [2014-03-28] - -### Bugfixes - -- [Issue #342](https://github.com/influxdata/influxdb/issues/342). Data resurrected after a server restart -- [Issue #367](https://github.com/influxdata/influxdb/issues/367). Influxdb won't start if the api port is commented out -- [Issue #355](https://github.com/influxdata/influxdb/issues/355). Return an error on wrong time strings -- [Issue #331](https://github.com/influxdata/influxdb/issues/331). Allow negative time values in the where clause -- [Issue #371](https://github.com/influxdata/influxdb/issues/371). Seris index isn't deleted when the series is dropped -- [Issue #360](https://github.com/influxdata/influxdb/issues/360). Store and recover continuous queries - -## v0.5.1 [2014-03-24] - -### Bugfixes - -- Revert the version of goraft due to a bug found in the latest version - -## v0.5.0 [2014-03-24] - -### Features - -- [Issue #293](https://github.com/influxdata/influxdb/pull/293). Implement a Graphite listener - -### Bugfixes - -- [Issue #340](https://github.com/influxdata/influxdb/issues/340). Writing many requests while replaying seems to cause commits out of order - -## v0.5.0-rc.6 [2014-03-20] - -### Bugfixes - -- Increase raft election timeout to avoid unecessary relections -- Sort points before writing them to avoid an explosion in the request - number when the points are written randomly -- [Issue #335](https://github.com/influxdata/influxdb/issues/335). Fixes regexp for interpolating more than one column value in continuous queries -- [Issue #318](https://github.com/influxdata/influxdb/pull/318). Support EXPLAIN queries -- [Issue #333](https://github.com/influxdata/influxdb/pull/333). Fail - when the password is too short or too long instead of passing it to - the crypto library - -## v0.5.0-rc.5 [2014-03-11] - -### Bugfixes - -- [Issue #312](https://github.com/influxdata/influxdb/issues/312). WAL should wait for server id to be set before recovering -- [Issue #301](https://github.com/influxdata/influxdb/issues/301). Use ref counting to guard against race conditions in the shard cache -- [Issue #319](https://github.com/influxdata/influxdb/issues/319). Propagate engine creation error correctly to the user -- [Issue #316](https://github.com/influxdata/influxdb/issues/316). Make - sure we don't starve goroutines if we get an access denied error - from one of the shards -- [Issue #306](https://github.com/influxdata/influxdb/issues/306). Deleting/Dropping database takes a lot of memory -- [Issue #302](https://github.com/influxdata/influxdb/issues/302). Should be able to set negative timestamps on points -- [Issue #327](https://github.com/influxdata/influxdb/issues/327). Make delete queries not use WAL. This addresses #315, #317 and #314 -- [Issue #321](https://github.com/influxdata/influxdb/issues/321). Make sure we split points on shards properly - -## v0.5.0-rc.4 [2014-03-07] - -### Bugfixes - -- [Issue #298](https://github.com/influxdata/influxdb/issues/298). Fix limit when querying multiple shards -- [Issue #305](https://github.com/influxdata/influxdb/issues/305). Shard ids not unique after restart -- [Issue #309](https://github.com/influxdata/influxdb/issues/309). Don't relog the requests on the remote server -- Fix few bugs in the WAL and refactor the way it works (this requires purging the WAL from previous rc) - -## v0.5.0-rc.3 [2014-03-03] - -### Bugfixes -- [Issue #69](https://github.com/influxdata/influxdb/issues/69). Support column aliases -- [Issue #287](https://github.com/influxdata/influxdb/issues/287). Make the lru cache size configurable -- [Issue #38](https://github.com/influxdata/influxdb/issues/38). Fix a memory leak discussed in this story -- [Issue #286](https://github.com/influxdata/influxdb/issues/286). Make the number of open shards configurable -- Make LevelDB use the max open files configuration option. - -## v0.5.0-rc.2 [2014-02-27] - -### Bugfixes - -- [Issue #274](https://github.com/influxdata/influxdb/issues/274). Crash after restart -- [Issue #277](https://github.com/influxdata/influxdb/issues/277). Ensure duplicate shards won't be created -- [Issue #279](https://github.com/influxdata/influxdb/issues/279). Limits not working on regex queries -- [Issue #281](https://github.com/influxdata/influxdb/issues/281). `./influxdb -v` should print the sha when building from source -- [Issue #283](https://github.com/influxdata/influxdb/issues/283). Dropping shard and restart in cluster causes panic. -- [Issue #288](https://github.com/influxdata/influxdb/issues/288). Sequence numbers should be unique per server id - -## v0.5.0-rc.1 [2014-02-25] - -### Bugfixes - -- Ensure large deletes don't take too much memory -- [Issue #240](https://github.com/influxdata/influxdb/pull/240). Unable to query against columns with `.` in the name. -- [Issue #250](https://github.com/influxdata/influxdb/pull/250). different result between normal and continuous query with "group by" clause -- [Issue #216](https://github.com/influxdata/influxdb/pull/216). Results with no points should exclude columns and points - -### Features - -- [Issue #243](https://github.com/influxdata/influxdb/issues/243). Should have endpoint to GET a user's attributes. -- [Issue #269](https://github.com/influxdata/influxdb/pull/269), [Issue #65](https://github.com/influxdata/influxdb/issues/65) New clustering architecture (see docs), with the side effect that queries can be distributed between multiple shards -- [Issue #164](https://github.com/influxdata/influxdb/pull/269),[Issue #103](https://github.com/influxdata/influxdb/pull/269),[Issue #166](https://github.com/influxdata/influxdb/pull/269),[Issue #165](https://github.com/influxdata/influxdb/pull/269),[Issue #132](https://github.com/influxdata/influxdb/pull/269) Make request log a log file instead of leveldb with recovery on startup - -### Deprecated - -- [Issue #189](https://github.com/influxdata/influxdb/issues/189). `/cluster_admins` and `/db/:db/users` return usernames in a `name` key instead of `username` key. -- [Issue #216](https://github.com/influxdata/influxdb/pull/216). Results with no points should exclude columns and points - -## v0.4.4 [2014-02-05] - -### Features - -- Make the leveldb max open files configurable in the toml file - -## v0.4.3 [2014-01-31] - -### Bugfixes - -- [Issue #225](https://github.com/influxdata/influxdb/issues/225). Remove a hard limit on the points returned by the datastore -- [Issue #223](https://github.com/influxdata/influxdb/issues/223). Null values caused count(distinct()) to panic -- [Issue #224](https://github.com/influxdata/influxdb/issues/224). Null values broke replication due to protobuf limitation - -## v0.4.1 [2014-01-30] - -### Features - -- [Issue #193](https://github.com/influxdata/influxdb/issues/193). Allow logging to stdout. Thanks @schmurfy -- [Issue #190](https://github.com/influxdata/influxdb/pull/190). Add support for SSL. -- [Issue #194](https://github.com/influxdata/influxdb/pull/194). Should be able to disable Admin interface. - -### Bugfixes - -- [Issue #33](https://github.com/influxdata/influxdb/issues/33). Don't call WriteHeader more than once per request -- [Issue #195](https://github.com/influxdata/influxdb/issues/195). Allow the bind address to be configurable, Thanks @schmurfy. -- [Issue #199](https://github.com/influxdata/influxdb/issues/199). Make the test timeout configurable -- [Issue #200](https://github.com/influxdata/influxdb/issues/200). Selecting `time` or `sequence_number` silently fail -- [Issue #215](https://github.com/influxdata/influxdb/pull/215). Server fails to start up after Raft log compaction and restart. - -## v0.4.0 [2014-01-17] - -## Features - -- [Issue #86](https://github.com/influxdata/influxdb/issues/86). Support arithmetic expressions in select clause -- [Issue #92](https://github.com/influxdata/influxdb/issues/92). Change '==' to '=' and '!=' to '<>' -- [Issue #88](https://github.com/influxdata/influxdb/issues/88). Support datetime strings -- [Issue #64](https://github.com/influxdata/influxdb/issues/64). Shard writes and queries across cluster with replay for briefly downed nodes (< 24 hrs) -- [Issue #78](https://github.com/influxdata/influxdb/issues/78). Sequence numbers persist across restarts so they're not reused -- [Issue #102](https://github.com/influxdata/influxdb/issues/102). Support expressions in where condition -- [Issue #101](https://github.com/influxdata/influxdb/issues/101). Support expressions in aggregates -- [Issue #62](https://github.com/influxdata/influxdb/issues/62). Support updating and deleting column values -- [Issue #96](https://github.com/influxdata/influxdb/issues/96). Replicate deletes in a cluster -- [Issue #94](https://github.com/influxdata/influxdb/issues/94). delete queries -- [Issue #116](https://github.com/influxdata/influxdb/issues/116). Use proper logging -- [Issue #40](https://github.com/influxdata/influxdb/issues/40). Use TOML instead of JSON in the config file -- [Issue #99](https://github.com/influxdata/influxdb/issues/99). Support list series in the query language -- [Issue #149](https://github.com/influxdata/influxdb/issues/149). Cluster admins should be able to perform reads and writes. -- [Issue #108](https://github.com/influxdata/influxdb/issues/108). Querying one point using `time =` -- [Issue #114](https://github.com/influxdata/influxdb/issues/114). Servers should periodically check that they're consistent. -- [Issue #93](https://github.com/influxdata/influxdb/issues/93). Should be able to drop a time series -- [Issue #177](https://github.com/influxdata/influxdb/issues/177). Support drop series in the query language. -- [Issue #184](https://github.com/influxdata/influxdb/issues/184). Implement Raft log compaction. -- [Issue #153](https://github.com/influxdata/influxdb/issues/153). Implement continuous queries - -### Bugfixes - -- [Issue #90](https://github.com/influxdata/influxdb/issues/90). Group by multiple columns panic -- [Issue #89](https://github.com/influxdata/influxdb/issues/89). 'Group by' combined with 'where' not working -- [Issue #106](https://github.com/influxdata/influxdb/issues/106). Don't panic if we only see one point and can't calculate derivative -- [Issue #105](https://github.com/influxdata/influxdb/issues/105). Panic when using a where clause that reference columns with null values -- [Issue #61](https://github.com/influxdata/influxdb/issues/61). Remove default limits from queries -- [Issue #118](https://github.com/influxdata/influxdb/issues/118). Make column names starting with '_' legal -- [Issue #121](https://github.com/influxdata/influxdb/issues/121). Don't fall back to the cluster admin auth if the db user auth fails -- [Issue #127](https://github.com/influxdata/influxdb/issues/127). Return error on delete queries with where condition that don't have time -- [Issue #117](https://github.com/influxdata/influxdb/issues/117). Fill empty groups with default values -- [Issue #150](https://github.com/influxdata/influxdb/pull/150). Fix parser for when multiple divisions look like a regex. -- [Issue #158](https://github.com/influxdata/influxdb/issues/158). Logged deletes should be stored with the time range if missing. -- [Issue #136](https://github.com/influxdata/influxdb/issues/136). Make sure writes are replicated in order to avoid triggering replays -- [Issue #145](https://github.com/influxdata/influxdb/issues/145). Server fails to join cluster if all starting at same time. -- [Issue #176](https://github.com/influxdata/influxdb/issues/176). Drop database should take effect on all nodes -- [Issue #180](https://github.com/influxdata/influxdb/issues/180). Column names not returned when running multi-node cluster and writing more than one point. -- [Issue #182](https://github.com/influxdata/influxdb/issues/182). Queries with invalid limit clause crash the server - -### Deprecated - -- deprecate '==' and '!=' in favor of '=' and '<>', respectively -- deprecate `/dbs` (for listing databases) in favor of a more consistent `/db` endpoint -- deprecate `username` field for a more consistent `name` field in `/db/:db/users` and `/cluster_admins` -- deprecate endpoints `/db/:db/admins/:user` in favor of using `/db/:db/users/:user` which should - be used to update user flags, password, etc. -- Querying for column names that don't exist no longer throws an error. - -## v0.3.2 - -## Features - -- [Issue #82](https://github.com/influxdata/influxdb/issues/82). Add endpoint for listing available admin interfaces. -- [Issue #80](https://github.com/influxdata/influxdb/issues/80). Support durations when specifying start and end time -- [Issue #81](https://github.com/influxdata/influxdb/issues/81). Add support for IN - -## Bugfixes - -- [Issue #75](https://github.com/influxdata/influxdb/issues/75). Don't allow time series names that start with underscore -- [Issue #85](https://github.com/influxdata/influxdb/issues/85). Non-existing columns exist after they have been queried before - -## v0.3.0 - -## Features - -- [Issue #51](https://github.com/influxdata/influxdb/issues/51). Implement first and last aggregates -- [Issue #35](https://github.com/influxdata/influxdb/issues/35). Support table aliases in Join Queries -- [Issue #71](https://github.com/influxdata/influxdb/issues/71). Add WillReturnSingleSeries to the Query -- [Issue #61](https://github.com/influxdata/influxdb/issues/61). Limit should default to 10k -- [Issue #59](https://github.com/influxdata/influxdb/issues/59). Add histogram aggregate function - -## Bugfixes - -- Fix join and merges when the query is a descending order query -- [Issue #57](https://github.com/influxdata/influxdb/issues/57). Don't panic when type of time != float -- [Issue #63](https://github.com/influxdata/influxdb/issues/63). Aggregate queries should not have a sequence_number column - -## v0.2.0 - -### Features - -- [Issue #37](https://github.com/influxdata/influxdb/issues/37). Support the negation of the regex matcher !~ -- [Issue #47](https://github.com/influxdata/influxdb/issues/47). Spill out query and database detail at the time of bug report - -### Bugfixes - -- [Issue #36](https://github.com/influxdata/influxdb/issues/36). The regex operator should be =~ not ~= -- [Issue #39](https://github.com/influxdata/influxdb/issues/39). Return proper content types from the http api -- [Issue #42](https://github.com/influxdata/influxdb/issues/42). Make the api consistent with the docs -- [Issue #41](https://github.com/influxdata/influxdb/issues/41). Table/Points not deleted when database is dropped -- [Issue #45](https://github.com/influxdata/influxdb/issues/45). Aggregation shouldn't mess up the order of the points -- [Issue #44](https://github.com/influxdata/influxdb/issues/44). Fix crashes on RHEL 5.9 -- [Issue #34](https://github.com/influxdata/influxdb/issues/34). Ascending order always return null for columns that have a null value -- [Issue #55](https://github.com/influxdata/influxdb/issues/55). Limit should limit the points that match the Where clause -- [Issue #53](https://github.com/influxdata/influxdb/issues/53). Writing null values via HTTP API fails - -### Deprecated - -- Preparing to deprecate `/dbs` (for listing databases) in favor of a more consistent `/db` endpoint -- Preparing to deprecate `username` field for a more consistent `name` field in the `/db/:db/users` -- Preparing to deprecate endpoints `/db/:db/admins/:user` in favor of using `/db/:db/users/:user` which should - be used to update user flags, password, etc. - -## v0.1.0 - -### Features - -- [Issue #29](https://github.com/influxdata/influxdb/issues/29). Semicolon is now optional in queries -- [Issue #31](https://github.com/influxdata/influxdb/issues/31). Support Basic Auth as well as query params for authentication. - -### Bugfixes - -- Don't allow creating users with empty username -- [Issue #22](https://github.com/influxdata/influxdb/issues/22). Don't set goroot if it was set -- [Issue #25](https://github.com/influxdata/influxdb/issues/25). Fix queries that use the median aggregator -- [Issue #26](https://github.com/influxdata/influxdb/issues/26). Default log and db directories should be in /opt/influxdb/shared/data -- [Issue #27](https://github.com/influxdata/influxdb/issues/27). Group by should not blow up if the one of the columns in group by has null values -- [Issue #30](https://github.com/influxdata/influxdb/issues/30). Column indexes/names getting off somehow -- [Issue #32](https://github.com/influxdata/influxdb/issues/32). Fix many typos in the codebase. Thanks @pborreli - -## v0.0.9 - -#### Features - -- Add stddev(...) support -- Better docs, thanks @auxesis and @d-snp. - -#### Bugfixes - -- Set PYTHONPATH and CC appropriately on mac os x. -- [Issue #18](https://github.com/influxdata/influxdb/issues/18). Fix 386 debian and redhat packages -- [Issue #23](https://github.com/influxdata/influxdb/issues/23). Fix the init scripts on redhat - -## v0.0.8 - -#### Features - -- Add a way to reset the root password from the command line. -- Add distinct(..) and derivative(...) support -- Print test coverage if running go1.2 - -#### Bugfixes - -- Fix the default admin site path in the .deb and .rpm packages. -- Fix the configuration filename in the .tar.gz package. - -## v0.0.7 - -#### Features - -- include the admin site in the repo to make it easier for newcomers. - -## v0.0.6 - -#### Features - -- Add count(distinct(..)) support - -#### Bugfixes - -- Reuse levigo read/write options. - -## v0.0.5 - -#### Features - -- Cache passwords in memory to speed up password verification -- Add MERGE and INNER JOIN support - -#### Bugfixes - -- All columns should be returned if `select *` was used -- Read/Write benchmarks - -## v0.0.2 - -#### Features - -- Add an admin UI -- Deb and RPM packages - -#### Bugfixes - -- Fix some nil pointer dereferences -- Cleanup the aggregators implementation - -## v0.0.1 [2013-10-22] - - * Initial Release diff --git a/vendor/github.com/influxdata/influxdb/CODING_GUIDELINES.md b/vendor/github.com/influxdata/influxdb/CODING_GUIDELINES.md deleted file mode 100644 index 6c1d2f2e..00000000 --- a/vendor/github.com/influxdata/influxdb/CODING_GUIDELINES.md +++ /dev/null @@ -1,82 +0,0 @@ -_This document is currently in draft form._ - -# Background - -The goal of this guide is to capture some Do and Don'ts of Go code for the InfluxDB database. When it comes to Go, writing good code is often achieved with the help of tools like `go fmt` and `go vet`. However there are still some practices not enforceable by any tools. This guide lists some specific practices to follow when writing code for the database. - -*Like everything, one needs to use good judgment.* There will always be times when it doesn't make sense to follow a guideline outlined in this document. If that case arises, be ready to justify your choices. - -# The Guidelines - -## Try not to use third-party libraries - -A third-party package is defined as one that is not part of the standard Go distribution. Generally speaking we prefer to minimize our use of third-party packages, and avoid them unless absolutely necessarily. We'll often write a little bit of code rather than pull in a third-party package. Of course, we do use some third-party packages -- most importantly we use [BoltDB](https://github.com/boltdb/bolt) in some storage engines. So to maximise the chance your change will be accepted by us, use only the standard libraries, or the third-party packages we have decided to use. - -For rationale, check out the post [The Case Against Third Party Libraries](http://blog.gopheracademy.com/advent-2014/case-against-3pl/). - -## Always include a default case in a 'switch' statement -The lack of a `default` case in a `switch` statement can be a significant source of bugs. This is particularly true in the case of a type-assertions switch. So always include a `default` statement unless you have an explicit reason not to. - -## When -- and when not -- set a channel to 'nil' - -## Use defer with anonymous functions to handle complex locking -Consider a block of code like the following. -``` - mu.Lock() - if foo == "quit" { - mu.Unlock() - return - } else if foo == "continue" { - if bar == "quit" { - mu.Unlock() - return - } - bar = "still going" - } else { - qux = "here at last" - mu.Unlock() - return - } - foo = "more to do" - bar = "still more to do" - mu.Unlock() - - qux = "finished now" - return -``` -While this is obviously contrived, complex lock control like this is sometimes required, and doesn't lend itself to `defer`. But as the code evolves, it's easy to introduce new cases, and forget to release locks. One way to address this is to use an anonymous function like so: -``` - more := func() bool { - mu.Lock() - defer mu.Unlock() - if foo == "quit" { - return false - } else if foo == "continue" { - if bar == "quit" { - return false - } - bar = "still going" - } else { - qux = "here at last" - return false - } - foo = "more to do" - bar = "still more to do" - return true - }() - - if more { - qux = "finished" - } - return -``` -This allows us to use `defer` but ensures that if any new cases are added to the logic within the anonymous function, the lock will always be released. Another advantage of this approach is that `defer` will still run even in the event of a panic, ensuring the locks will be released even in that case. - -## When to call 'panic()' - -# Useful links -- [Useful techniques in Go](http://arslan.io/ten-useful-techniques-in-go) -- [Go in production](http://peter.bourgon.org/go-in-production/) -- [Principles of designing Go APIs with channels](https://inconshreveable.com/07-08-2014/principles-of-designing-go-apis-with-channels/) -- [Common mistakes in Golang](http://soryy.com/blog/2014/common-mistakes-with-go-lang/). Especially this section `Loops, Closures, and Local Variables` - diff --git a/vendor/github.com/influxdata/influxdb/CONTRIBUTING.md b/vendor/github.com/influxdata/influxdb/CONTRIBUTING.md deleted file mode 100644 index f62ebd07..00000000 --- a/vendor/github.com/influxdata/influxdb/CONTRIBUTING.md +++ /dev/null @@ -1,280 +0,0 @@ -Contributing to InfluxDB -======================== - -Bug reports ---------------- -Before you file an issue, please search existing issues in case it has already been filed, or perhaps even fixed. If you file an issue, please include the following. -* Full details of your operating system (or distribution) e.g. 64-bit Ubuntu 14.04. -* The version of InfluxDB you are running -* Whether you installed it using a pre-built package, or built it from source. -* A small test case, if applicable, that demonstrates the issues. - -Remember the golden rule of bug reports: **The easier you make it for us to reproduce the problem, the faster it will get fixed.** -If you have never written a bug report before, or if you want to brush up on your bug reporting skills, we recommend reading [Simon Tatham's essay "How to Report Bugs Effectively."](http://www.chiark.greenend.org.uk/~sgtatham/bugs.html) - -Test cases should be in the form of `curl` commands. For example: -```bash -# create database -curl -G http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb" - -# create retention policy -curl -G http://localhost:8086/query --data-urlencode "q=CREATE RETENTION POLICY myrp ON mydb DURATION 365d REPLICATION 1 DEFAULT" - -# write data -curl -X POST http://localhost:8086/write --data-urlencode "db=mydb" --data-binary "cpu,region=useast,host=server_1,service=redis value=61" - -# Delete a Measurement -curl -G http://localhost:8086/query --data-urlencode 'db=mydb' --data-urlencode 'q=DROP MEASUREMENT cpu' - -# Query the Measurement -# Bug: expected it to return no data, but data comes back. -curl -G http://localhost:8086/query --data-urlencode 'db=mydb' --data-urlencode 'q=SELECT * from cpu' -``` -**If you don't include a clear test case like this, your issue may not be investigated, and may even be closed**. If writing the data is too difficult, please zip up your data directory and include a link to it in your bug report. - -Please note that issues are *not the place to file general questions* such as "how do I use collectd with InfluxDB?" Questions of this nature should be sent to the [Google Group](https://groups.google.com/forum/#!forum/influxdb), not filed as issues. Issues like this will be closed. - -Feature requests ---------------- -We really like to receive feature requests, as it helps us prioritize our work. Please be clear about your requirements, as incomplete feature requests may simply be closed if we don't understand what you would like to see added to InfluxDB. - -Contributing to the source code ---------------- - -InfluxDB follows standard Go project structure. This means that all your Go development are done in `$GOPATH/src`. GOPATH can be any directory under which InfluxDB and all its dependencies will be cloned. For full details on the project structure, follow along below. - -You should also read our [coding guide](https://github.com/influxdata/influxdb/blob/master/CODING_GUIDELINES.md), to understand better how to write code for InfluxDB. - -Submitting a pull request ------------- -To submit a pull request you should fork the InfluxDB repository, and make your change on a feature branch of your fork. Then generate a pull request from your branch against *master* of the InfluxDB repository. Include in your pull request details of your change -- the why *and* the how -- as well as the testing your performed. Also, be sure to run the test suite with your change in place. Changes that cause tests to fail cannot be merged. - -There will usually be some back and forth as we finalize the change, but once that completes it may be merged. - -To assist in review for the PR, please add the following to your pull request comment: - -```md -- [ ] CHANGELOG.md updated -- [ ] Rebased/mergable -- [ ] Tests pass -- [ ] Sign [CLA](https://influxdata.com/community/cla/) (if not already signed) -``` - -Signing the CLA ---------------- - -If you are going to be contributing back to InfluxDB please take a -second to sign our CLA, which can be found -[on our website](https://influxdata.com/community/cla/). - -Installing Go -------------- -InfluxDB requires Go 1.7.4. - -At InfluxDB we find gvm, a Go version manager, useful for installing Go. For instructions -on how to install it see [the gvm page on github](https://github.com/moovweb/gvm). - -After installing gvm you can install and set the default go version by -running the following: - - gvm install go1.7.4 - gvm use go1.7.4 --default - -Installing GDM -------------- -InfluxDB uses [gdm](https://github.com/sparrc/gdm) to manage dependencies. Install it by running the following: - - go get github.com/sparrc/gdm - -Revision Control Systems -------------- -Go has the ability to import remote packages via revision control systems with the `go get` command. To ensure that you can retrieve any remote package, be sure to install the following rcs software to your system. -Currently the project only depends on `git` and `mercurial`. - -* [Install Git](http://git-scm.com/book/en/Getting-Started-Installing-Git) -* [Install Mercurial](http://mercurial.selenic.com/wiki/Download) - -Getting the source ------- -Setup the project structure and fetch the repo like so: - -```bash - mkdir $HOME/gocodez - export GOPATH=$HOME/gocodez - go get github.com/influxdata/influxdb -``` - -You can add the line `export GOPATH=$HOME/gocodez` to your bash/zsh file to be set for every shell instead of having to manually run it everytime. - -Cloning a fork -------------- -If you wish to work with fork of InfluxDB, your own fork for example, you must still follow the directory structure above. But instead of cloning the main repo, instead clone your fork. Follow the steps below to work with a fork: - -```bash - export GOPATH=$HOME/gocodez - mkdir -p $GOPATH/src/github.com/influxdata - cd $GOPATH/src/github.com/influxdata - git clone git@github.com:/influxdb -``` - -Retaining the directory structure `$GOPATH/src/github.com/influxdata` is necessary so that Go imports work correctly. - -Build and Test ------ - -Make sure you have Go installed and the project structure as shown above. To then get the dependencies for the project, execute the following commands: - -```bash -cd $GOPATH/src/github.com/influxdata/influxdb -gdm restore -``` - -To then build and install the binaries, run the following command. -```bash -go clean ./... -go install ./... -``` -The binaries will be located in `$GOPATH/bin`. Please note that the InfluxDB binary is named `influxd`, not `influxdb`. - -To set the version and commit flags during the build pass the following to the **install** command: - -```bash --ldflags="-X main.version=$VERSION -X main.branch=$BRANCH -X main.commit=$COMMIT" -``` - -where `$VERSION` is the version, `$BRANCH` is the branch, and `$COMMIT` is the git commit hash. - -If you want to build packages, see `build.py` usage information: - -```bash -python build.py --help - -# Or to build a package for your current system -python build.py --package -``` - -To run the tests, execute the following command: - -```bash -cd $GOPATH/src/github.com/influxdata/influxdb -go test -v ./... - -# run tests that match some pattern -go test -run=TestDatabase . -v - -# run tests and show coverage -go test -coverprofile /tmp/cover . && go tool cover -html /tmp/cover -``` - -To install go cover, run the following command: -``` -go get golang.org/x/tools/cmd/cover -``` - -Generated Google Protobuf code ------------------ -Most changes to the source do not require that the generated protocol buffer code be changed. But if you need to modify the protocol buffer code, you'll first need to install the protocol buffers toolchain. - -First install the [protocol buffer compiler](https://developers.google.com/protocol-buffers/ -) 2.6.1 or later for your OS: - -Then install the go plugins: - -```bash -go get github.com/gogo/protobuf/proto -go get github.com/gogo/protobuf/protoc-gen-gogo -go get github.com/gogo/protobuf/gogoproto -``` - -Finally run, `go generate` after updating any `*.proto` file: - -```bash -go generate ./... -``` -**Troubleshooting** - -If generating the protobuf code is failing for you, check each of the following: -* Ensure the protobuf library can be found. Make sure that `LD_LIBRRARY_PATH` includes the directory in which the library `libprotoc.so` has been installed. -* Ensure the command `protoc-gen-gogo`, found in `GOPATH/bin`, is on your path. This can be done by adding `GOPATH/bin` to `PATH`. - - -Generated Go Templates ----------------------- - -The query engine requires optimizes data structures for each data type so -instead of writing each implementation several times we use templates. _Do not -change code that ends in a `.gen.go` extension!_ Instead you must edit the -`.gen.go.tmpl` file that was used to generate it. - -Once you've edited the template file, you'll need the [`tmpl`][tmpl] utility -to generate the code: - -```sh -$ go get github.com/benbjohnson/tmpl -``` - -Then you can regenerate all templates in the project: - -```sh -$ go generate ./... -``` - -[tmpl]: https://github.com/benbjohnson/tmpl - - -Pre-commit checks -------------- - -We have a pre-commit hook to make sure code is formatted properly and vetted before you commit any changes. We strongly recommend using the pre-commit hook to guard against accidentally committing unformatted code. To use the pre-commit hook, run the following: -```bash - cd $GOPATH/src/github.com/influxdata/influxdb - cp .hooks/pre-commit .git/hooks/ -``` -In case the commit is rejected because it's not formatted you can run -the following to format the code: - -``` -go fmt ./... -go vet ./... -``` - -To install go vet, run the following command: -``` -go get golang.org/x/tools/cmd/vet -``` - -NOTE: If you have not installed mercurial, the above command will fail. See [Revision Control Systems](#revision-control-systems) above. - -For more information on `go vet`, [read the GoDoc](https://godoc.org/golang.org/x/tools/cmd/vet). - -Profiling ------ -When troubleshooting problems with CPU or memory the Go toolchain can be helpful. You can start InfluxDB with CPU and memory profiling turned on. For example: - -```sh -# start influx with profiling -./influxd -cpuprofile influxdcpu.prof -memprof influxdmem.prof -# run queries, writes, whatever you're testing -# Quit out of influxd and influxd.prof will then be written. -# open up pprof to examine the profiling data. -go tool pprof ./influxd influxd.prof -# once inside run "web", opens up browser with the CPU graph -# can also run "web " to zoom in. Or "list " to see specific lines -``` -Note that when you pass the binary to `go tool pprof` *you must specify the path to the binary*. - -If you are profiling benchmarks built with the `testing` package, you may wish -to use the [`github.com/pkg/profile`](github.com/pkg/profile) package to limit -the code being profiled: - -```go -func BenchmarkSomething(b *testing.B) { - // do something intensive like fill database with data... - defer profile.Start(profile.ProfilePath("/tmp"), profile.MemProfile).Stop() - // do something that you want to profile... -} -``` - -Continuous Integration testing ------ -InfluxDB uses CircleCI for continuous integration testing. To see how the code is built and tested, check out [this file](https://github.com/influxdata/influxdb/blob/master/circle-test.sh). It closely follows the build and test process outlined above. You can see the exact version of Go InfluxDB uses for testing by consulting that file. diff --git a/vendor/github.com/influxdata/influxdb/DOCKER.md b/vendor/github.com/influxdata/influxdb/DOCKER.md deleted file mode 100644 index 4d902e64..00000000 --- a/vendor/github.com/influxdata/influxdb/DOCKER.md +++ /dev/null @@ -1,30 +0,0 @@ -# Docker Setup -======================== - -This document describes how to build and run a minimal InfluxDB container under Docker. Currently, it has only been tested for local development and assumes that you have a working docker environment. - -## Building Image - -To build a docker image for InfluxDB from your current checkout, run the following: - -``` -$ ./build-docker.sh -``` - -This script uses the `golang:1.7.4` image to build a fully static binary of `influxd` and then adds it to a minimal `scratch` image. - -To build the image using a different version of go: - -``` -$ GO_VER=1.7.4 ./build-docker.sh -``` - -Available version can be found [here](https://hub.docker.com/_/golang/). - -## Single Node Container - -This will start an interactive, single-node, that publishes the containers port `8086` and `8088` to the hosts ports `8086` and `8088` respectively. This is identical to starting `influxd` manually. - -``` -$ docker run -it -p 8086:8086 -p 8088:8088 influxdb -``` diff --git a/vendor/github.com/influxdata/influxdb/Dockerfile b/vendor/github.com/influxdata/influxdb/Dockerfile deleted file mode 100644 index d30cd300..00000000 --- a/vendor/github.com/influxdata/influxdb/Dockerfile +++ /dev/null @@ -1,24 +0,0 @@ -FROM busybox:ubuntu-14.04 - -MAINTAINER Jason Wilder "" - -# admin, http, udp, cluster, graphite, opentsdb, collectd -EXPOSE 8083 8086 8086/udp 8088 2003 4242 25826 - -WORKDIR /app - -# copy binary into image -COPY influxd /app/ - -# Add influxd to the PATH -ENV PATH=/app:$PATH - -# Generate a default config -RUN influxd config > /etc/influxdb.toml - -# Use /data for all disk storage -RUN sed -i 's/dir = "\/.*influxdb/dir = "\/data/' /etc/influxdb.toml - -VOLUME ["/data"] - -ENTRYPOINT ["influxd", "--config", "/etc/influxdb.toml"] diff --git a/vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu32 b/vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu32 deleted file mode 100644 index 49466a0a..00000000 --- a/vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu32 +++ /dev/null @@ -1,35 +0,0 @@ -FROM ioft/i386-ubuntu:14.04 - -RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \ - python-software-properties \ - software-properties-common \ - wget \ - git \ - mercurial \ - make \ - ruby \ - ruby-dev \ - rpm \ - zip \ - python \ - python-boto - -RUN gem install fpm - -# Install go -ENV GOPATH /root/go -ENV GO_VERSION 1.7.4 -ENV GO_ARCH 386 -RUN wget https://storage.googleapis.com/golang/go${GO_VERSION}.linux-${GO_ARCH}.tar.gz; \ - tar -C /usr/local/ -xf /go${GO_VERSION}.linux-${GO_ARCH}.tar.gz ; \ - rm /go${GO_VERSION}.linux-${GO_ARCH}.tar.gz -ENV PATH /usr/local/go/bin:$PATH - -ENV PROJECT_DIR $GOPATH/src/github.com/influxdata/influxdb -ENV PATH $GOPATH/bin:$PATH -RUN mkdir -p $PROJECT_DIR -WORKDIR $PROJECT_DIR - -VOLUME $PROJECT_DIR - -ENTRYPOINT [ "/root/go/src/github.com/influxdata/influxdb/build.py" ] diff --git a/vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu64 b/vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu64 deleted file mode 100644 index b8ceba34..00000000 --- a/vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu64 +++ /dev/null @@ -1,38 +0,0 @@ -FROM ubuntu:trusty - -RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \ - python-software-properties \ - software-properties-common \ - wget \ - git \ - mercurial \ - make \ - ruby \ - ruby-dev \ - rpm \ - zip \ - python \ - python-boto \ - asciidoc \ - xmlto \ - docbook-xsl - -RUN gem install fpm - -# Install go -ENV GOPATH /root/go -ENV GO_VERSION 1.7.4 -ENV GO_ARCH amd64 -RUN wget https://storage.googleapis.com/golang/go${GO_VERSION}.linux-${GO_ARCH}.tar.gz; \ - tar -C /usr/local/ -xf /go${GO_VERSION}.linux-${GO_ARCH}.tar.gz ; \ - rm /go${GO_VERSION}.linux-${GO_ARCH}.tar.gz -ENV PATH /usr/local/go/bin:$PATH - -ENV PROJECT_DIR $GOPATH/src/github.com/influxdata/influxdb -ENV PATH $GOPATH/bin:$PATH -RUN mkdir -p $PROJECT_DIR -WORKDIR $PROJECT_DIR - -VOLUME $PROJECT_DIR - -ENTRYPOINT [ "/root/go/src/github.com/influxdata/influxdb/build.py" ] diff --git a/vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu64_git b/vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu64_git deleted file mode 100644 index 61227a34..00000000 --- a/vendor/github.com/influxdata/influxdb/Dockerfile_build_ubuntu64_git +++ /dev/null @@ -1,43 +0,0 @@ -FROM ubuntu:trusty - -RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \ - python-software-properties \ - software-properties-common \ - wget \ - git \ - mercurial \ - make \ - ruby \ - ruby-dev \ - rpm \ - zip \ - python \ - python-boto - -RUN gem install fpm - -# Setup env -ENV GOPATH /root/go -ENV PROJECT_DIR $GOPATH/src/github.com/influxdata/influxdb -ENV PATH $GOPATH/bin:$PATH -RUN mkdir -p $PROJECT_DIR - -VOLUME $PROJECT_DIR - - -# Install go -ENV GO_VERSION 1.7.4 -ENV GO_ARCH amd64 -RUN wget https://storage.googleapis.com/golang/go${GO_VERSION}.linux-${GO_ARCH}.tar.gz; \ - tar -C /usr/local/ -xf /go${GO_VERSION}.linux-${GO_ARCH}.tar.gz ; \ - rm /go${GO_VERSION}.linux-${GO_ARCH}.tar.gz - -# Clone Go tip for compilation -ENV GOROOT_BOOTSTRAP /usr/local/go -RUN git clone https://go.googlesource.com/go -ENV PATH /go/bin:$PATH - -# Add script for compiling go -ENV GO_CHECKOUT master -ADD ./gobuild.sh /gobuild.sh -ENTRYPOINT [ "/gobuild.sh" ] diff --git a/vendor/github.com/influxdata/influxdb/Dockerfile_test_ubuntu32 b/vendor/github.com/influxdata/influxdb/Dockerfile_test_ubuntu32 deleted file mode 100644 index af505b5c..00000000 --- a/vendor/github.com/influxdata/influxdb/Dockerfile_test_ubuntu32 +++ /dev/null @@ -1,12 +0,0 @@ -FROM 32bit/ubuntu:14.04 - -RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y python-software-properties software-properties-common git -RUN add-apt-repository ppa:evarlast/golang1.4 -RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-overwrite" golang-go - -ENV GOPATH=/root/go -RUN mkdir -p /root/go/src/github.com/influxdata/influxdb -RUN mkdir -p /tmp/artifacts - -VOLUME /root/go/src/github.com/influxdata/influxdb -VOLUME /tmp/artifacts diff --git a/vendor/github.com/influxdata/influxdb/Godeps b/vendor/github.com/influxdata/influxdb/Godeps deleted file mode 100644 index ed902f3b..00000000 --- a/vendor/github.com/influxdata/influxdb/Godeps +++ /dev/null @@ -1,20 +0,0 @@ -collectd.org e84e8af5356e7f47485bbc95c96da6dd7984a67e -github.com/BurntSushi/toml 99064174e013895bbd9b025c31100bd1d9b590ca -github.com/bmizerany/pat c068ca2f0aacee5ac3681d68e4d0a003b7d1fd2c -github.com/boltdb/bolt 4b1ebc1869ad66568b313d0dc410e2be72670dda -github.com/cespare/xxhash 4a94f899c20bc44d4f5f807cb14529e72aca99d6 -github.com/davecgh/go-spew 346938d642f2ec3594ed81d874461961cd0faa76 -github.com/dgrijalva/jwt-go 24c63f56522a87ec5339cc3567883f1039378fdb -github.com/dgryski/go-bits 2ad8d707cc05b1815ce6ff2543bb5e8d8f9298ef -github.com/dgryski/go-bitstream 7d46cd22db7004f0cceb6f7975824b560cf0e486 -github.com/gogo/protobuf a9cd0c35b97daf74d0ebf3514c5254814b2703b4 -github.com/golang/snappy d9eb7a3d35ec988b8585d4a0068e462c27d28380 -github.com/influxdata/usage-client 6d3895376368aa52a3a81d2a16e90f0f52371967 -github.com/jwilder/encoding 4dada27c33277820fe35c7ee71ed34fbc9477d00 -github.com/paulbellamy/ratecounter 5a11f585a31379765c190c033b6ad39956584447 -github.com/peterh/liner 8975875355a81d612fafb9f5a6037bdcc2d9b073 -github.com/rakyll/statik e383bbf6b2ec1a2fb8492dfd152d945fb88919b6 -github.com/retailnext/hllpp 38a7bb71b483e855d35010808143beaf05b67f9d -go.uber.org/atomic 74ca5ec650841aee9f289dce76e928313a37cbc6 -go.uber.org/zap 416270f07a9f696922512bb6487df46a2238f73d -golang.org/x/crypto 9477e0b78b9ac3d0b03822fd95422e2fe07627cd diff --git a/vendor/github.com/influxdata/influxdb/LICENSE b/vendor/github.com/influxdata/influxdb/LICENSE deleted file mode 100644 index 63cef79b..00000000 --- a/vendor/github.com/influxdata/influxdb/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2013-2016 Errplane Inc. - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/influxdata/influxdb/LICENSE_OF_DEPENDENCIES.md b/vendor/github.com/influxdata/influxdb/LICENSE_OF_DEPENDENCIES.md deleted file mode 100644 index 12280798..00000000 --- a/vendor/github.com/influxdata/influxdb/LICENSE_OF_DEPENDENCIES.md +++ /dev/null @@ -1,25 +0,0 @@ -# List -- bootstrap 3.3.5 [MIT LICENSE](https://github.com/twbs/bootstrap/blob/master/LICENSE) -- collectd.org [ISC LICENSE](https://github.com/collectd/go-collectd/blob/master/LICENSE) -- github.com/BurntSushi/toml [WTFPL LICENSE](https://github.com/BurntSushi/toml/blob/master/COPYING) -- github.com/bmizerany/pat [MIT LICENSE](https://github.com/bmizerany/pat#license) -- github.com/boltdb/bolt [MIT LICENSE](https://github.com/boltdb/bolt/blob/master/LICENSE) -- github.com/cespare/xxhash [MIT LICENSE](https://github.com/cespare/xxhash/blob/master/LICENSE.txt) -- github.com/davecgh/go-spew/spew [ISC LICENSE](https://github.com/davecgh/go-spew/blob/master/LICENSE) -- github.com/dgrijalva/jwt-go [MIT LICENSE](https://github.com/dgrijalva/jwt-go/blob/master/LICENSE) -- github.com/dgryski/go-bits [MIT LICENSE](https://github.com/dgryski/go-bits/blob/master/LICENSE) -- github.com/dgryski/go-bitstream [MIT LICENSE](https://github.com/dgryski/go-bitstream/blob/master/LICENSE) -- github.com/gogo/protobuf/proto [BSD LICENSE](https://github.com/gogo/protobuf/blob/master/LICENSE) -- github.com/golang/snappy [BSD LICENSE](https://github.com/golang/snappy/blob/master/LICENSE) -- github.com/influxdata/usage-client [MIT LICENSE](https://github.com/influxdata/usage-client/blob/master/LICENSE.txt) -- github.com/jwilder/encoding [MIT LICENSE](https://github.com/jwilder/encoding/blob/master/LICENSE) -- github.com/paulbellamy/ratecounter [MIT LICENSE](https://github.com/paulbellamy/ratecounter/blob/master/LICENSE) -- github.com/peterh/liner [MIT LICENSE](https://github.com/peterh/liner/blob/master/COPYING) -- github.com/rakyll/statik [APACHE LICENSE](https://github.com/rakyll/statik/blob/master/LICENSE) -- github.com/retailnext/hllpp [BSD LICENSE](https://github.com/retailnext/hllpp/blob/master/LICENSE) -- github.com/uber-go/atomic [MIT LICENSE](https://github.com/uber-go/atomic/blob/master/LICENSE.txt) -- github.com/uber-go/zap [MIT LICENSE](https://github.com/uber-go/zap/blob/master/LICENSE.txt) -- glyphicons [LICENSE](http://glyphicons.com/license/) -- golang.org/x/crypto [BSD LICENSE](https://github.com/golang/crypto/blob/master/LICENSE) -- jquery 2.1.4 [MIT LICENSE](https://github.com/jquery/jquery/blob/master/LICENSE.txt) -- react 0.13.3 [BSD LICENSE](https://github.com/facebook/react/blob/master/LICENSE) diff --git a/vendor/github.com/influxdata/influxdb/Makefile b/vendor/github.com/influxdata/influxdb/Makefile deleted file mode 100644 index 9fa9c826..00000000 --- a/vendor/github.com/influxdata/influxdb/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -PACKAGES=$(shell find . -name '*.go' -print0 | xargs -0 -n1 dirname | sort --unique) - -default: - -metalint: deadcode cyclo aligncheck defercheck structcheck lint errcheck - -deadcode: - @deadcode $(PACKAGES) 2>&1 - -cyclo: - @gocyclo -over 10 $(PACKAGES) - -aligncheck: - @aligncheck $(PACKAGES) - -defercheck: - @defercheck $(PACKAGES) - - -structcheck: - @structcheck $(PACKAGES) - -lint: - @for pkg in $(PACKAGES); do golint $$pkg; done - -errcheck: - @for pkg in $(PACKAGES); do \ - errcheck -ignorepkg=bytes,fmt -ignore=":(Rollback|Close)" $$pkg \ - done - -tools: - go get github.com/remyoudompheng/go-misc/deadcode - go get github.com/alecthomas/gocyclo - go get github.com/opennota/check/... - go get github.com/golang/lint/golint - go get github.com/kisielk/errcheck - go get github.com/sparrc/gdm - -.PHONY: default,metalint,deadcode,cyclo,aligncheck,defercheck,structcheck,lint,errcheck,tools diff --git a/vendor/github.com/influxdata/influxdb/QUERIES.md b/vendor/github.com/influxdata/influxdb/QUERIES.md deleted file mode 100644 index 46a9eb1d..00000000 --- a/vendor/github.com/influxdata/influxdb/QUERIES.md +++ /dev/null @@ -1,180 +0,0 @@ -The top level name is called a measurement. These names can contain any characters. Then there are field names, field values, tag keys and tag values, which can also contain any characters. However, if the measurement, field, or tag contains any character other than [A-Z,a-z,0-9,_], or if it starts with a digit, it must be double-quoted. Therefore anywhere a measurement name, field key, or tag key appears it should be wrapped in double quotes. - -# Databases & retention policies - -```sql --- create a database -CREATE DATABASE - --- create a retention policy -CREATE RETENTION POLICY ON DURATION REPLICATION [DEFAULT] - --- alter retention policy -ALTER RETENTION POLICY ON (DURATION | REPLICATION | DEFAULT)+ - --- drop a database -DROP DATABASE - --- drop a retention policy -DROP RETENTION POLICY ON -``` -where `` is either `INF` for infinite retention, or an integer followed by the desired unit of time: u,ms,s,m,h,d,w for microseconds, milliseconds, seconds, minutes, hours, days, or weeks, respectively. `` must be an integer. - -If present, `DEFAULT` sets the retention policy as the default retention policy for writes and reads. - -# Users and permissions - -```sql --- create user -CREATE USER WITH PASSWORD '' - --- grant privilege on a database -GRANT ON TO - --- grant cluster admin privileges -GRANT ALL [PRIVILEGES] TO - --- revoke privilege -REVOKE ON FROM - --- revoke all privileges for a DB -REVOKE ALL [PRIVILEGES] ON FROM - --- revoke all privileges including cluster admin -REVOKE ALL [PRIVILEGES] FROM - --- combine db creation with privilege assignment (user must already exist) -CREATE DATABASE GRANT TO -CREATE DATABASE REVOKE FROM - --- delete a user -DROP USER - - -``` -where ` := READ | WRITE | All `. - -Authentication must be enabled in the influxdb.conf file for user permissions to be in effect. - -By default, newly created users have no privileges to any databases. - -Cluster administration privileges automatically grant full read and write permissions to all databases, regardless of subsequent database-specific privilege revocation statements. - -# Select - -```sql -SELECT mean(value) from cpu WHERE host = 'serverA' AND time > now() - 4h GROUP BY time(5m) - -SELECT mean(value) from cpu WHERE time > now() - 4h GROUP BY time(5m), region -``` - -## Group By - -# Delete - -# Series - -## Destroy - -```sql -DROP MEASUREMENT -DROP MEASUREMENT cpu WHERE region = 'uswest' -``` - -## Show - -Show series queries are for pulling out individual series from measurement names and tag data. They're useful for discovery. - -```sql --- show all databases -SHOW DATABASES - --- show measurement names -SHOW MEASUREMENTS -SHOW MEASUREMENTS LIMIT 15 -SHOW MEASUREMENTS LIMIT 10 OFFSET 40 -SHOW MEASUREMENTS WHERE service = 'redis' --- LIMIT and OFFSET can be applied to any of the SHOW type queries - --- show all series across all measurements/tagsets -SHOW SERIES - --- get a show of all series for any measurements where tag key region = tak value 'uswest' -SHOW SERIES WHERE region = 'uswest' - -SHOW SERIES FROM cpu_load WHERE region = 'uswest' LIMIT 10 - --- returns the 100 - 109 rows in the result. In the case of SHOW SERIES, which returns --- series split into measurements. Each series counts as a row. So you could see only a --- single measurement returned, but 10 series within it. -SHOW SERIES FROM cpu_load WHERE region = 'uswest' LIMIT 10 OFFSET 100 - --- show all retention policies on a database -SHOW RETENTION POLICIES ON mydb - --- get a show of all tag keys across all measurements -SHOW TAG KEYS - --- show all the tag keys for a given measurement -SHOW TAG KEYS FROM cpu -SHOW TAG KEYS FROM temperature, wind_speed - --- show all the tag values. note that a single WHERE TAG KEY = '...' clause is required -SHOW TAG VALUES WITH TAG KEY = 'region' -SHOW TAG VALUES FROM cpu WHERE region = 'uswest' WITH TAG KEY = 'host' - --- and you can do stuff against fields -SHOW FIELD KEYS FROM cpu - --- but you can't do this -SHOW FIELD VALUES --- we don't index field values, so this query should be invalid. - --- show all users -SHOW USERS -``` - -Note that `FROM` and `WHERE` are optional clauses in most of the show series queries. - -And the show series output looks like this: - -```json -[ - { - "name": "cpu", - "columns": ["id", "region", "host"], - "values": [ - 1, "uswest", "servera", - 2, "uswest", "serverb" - ] - }, - { - "name": "reponse_time", - "columns": ["id", "application", "host"], - "values": [ - 3, "myRailsApp", "servera" - ] - } -] -``` - -# Continuous Queries - -Continuous queries are going to be inspired by MySQL `TRIGGER` syntax: - -http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html - -Instead of having automatically-assigned ids, named continuous queries allows for some level of duplication prevention, -particularly in the case where creation is scripted. - -## Create - - CREATE CONTINUOUS QUERY AS SELECT ... FROM ... - -## Destroy - - DROP CONTINUOUS QUERY - -## List - - SHOW CONTINUOUS QUERIES diff --git a/vendor/github.com/influxdata/influxdb/README.md b/vendor/github.com/influxdata/influxdb/README.md deleted file mode 100644 index 8c269ddf..00000000 --- a/vendor/github.com/influxdata/influxdb/README.md +++ /dev/null @@ -1,71 +0,0 @@ -# InfluxDB [![Circle CI](https://circleci.com/gh/influxdata/influxdb/tree/master.svg?style=svg)](https://circleci.com/gh/influxdata/influxdb/tree/master) [![Go Report Card](https://goreportcard.com/badge/github.com/influxdata/influxdb)](https://goreportcard.com/report/github.com/influxdata/influxdb) [![Docker pulls](https://img.shields.io/docker/pulls/library/influxdb.svg)](https://hub.docker.com/_/influxdb/) - -## An Open-Source Time Series Database - -InfluxDB is an open source **time series database** with -**no external dependencies**. It's useful for recording metrics, -events, and performing analytics. - -## Features - -* Built-in [HTTP API](https://docs.influxdata.com/influxdb/latest/guides/writing_data/) so you don't have to write any server side code to get up and running. -* Data can be tagged, allowing very flexible querying. -* SQL-like query language. -* Simple to install and manage, and fast to get data in and out. -* It aims to answer queries in real-time. That means every data point is - indexed as it comes in and is immediately available in queries that - should return in < 100ms. - -## Installation - -We recommend installing InfluxDB using one of the [pre-built packages](https://influxdata.com/downloads/#influxdb). Then start InfluxDB using: - -* `service influxdb start` if you have installed InfluxDB using an official Debian or RPM package. -* `systemctl start influxdb` if you have installed InfluxDB using an official Debian or RPM package, and are running a distro with `systemd`. For example, Ubuntu 15 or later. -* `$GOPATH/bin/influxd` if you have built InfluxDB from source. - -## Getting Started - -### Create your first database - -``` -curl -G 'http://localhost:8086/query' --data-urlencode "q=CREATE DATABASE mydb" -``` - -### Insert some data -``` -curl -XPOST 'http://localhost:8086/write?db=mydb' \ --d 'cpu,host=server01,region=uswest load=42 1434055562000000000' - -curl -XPOST 'http://localhost:8086/write?db=mydb' \ --d 'cpu,host=server02,region=uswest load=78 1434055562000000000' - -curl -XPOST 'http://localhost:8086/write?db=mydb' \ --d 'cpu,host=server03,region=useast load=15.4 1434055562000000000' -``` - -### Query for the data -```JSON -curl -G http://localhost:8086/query?pretty=true --data-urlencode "db=mydb" \ ---data-urlencode "q=SELECT * FROM cpu WHERE host='server01' AND time < now() - 1d" -``` - -### Analyze the data -```JSON -curl -G http://localhost:8086/query?pretty=true --data-urlencode "db=mydb" \ ---data-urlencode "q=SELECT mean(load) FROM cpu WHERE region='uswest'" -``` - -## Documentation - -* Read more about the [design goals and motivations of the project](https://docs.influxdata.com/influxdb/latest/). -* Follow the [getting started guide](https://docs.influxdata.com/influxdb/latest/introduction/getting_started/) to learn the basics in just a few minutes. -* Learn more about [InfluxDB's key concepts](https://docs.influxdata.com/influxdb/latest/guides/writing_data/). - -## Contributing - -If you're feeling adventurous and want to contribute to InfluxDB, see our [contributing doc](https://github.com/influxdata/influxdb/blob/master/CONTRIBUTING.md) for info on how to make feature requests, build from source, and run tests. - -## Looking for Support? - -InfluxDB offers a number of services to help your project succeed. We offer Developer Support for organizations in active development, Managed Hosting to make it easy to move into production, and Enterprise Support for companies requiring the best response times, SLAs, and technical fixes. Visit our [support page](https://influxdata.com/services/) or contact [sales@influxdb.com](mailto:sales@influxdb.com) to learn how we can best help you succeed. diff --git a/vendor/github.com/influxdata/influxdb/TODO.md b/vendor/github.com/influxdata/influxdb/TODO.md deleted file mode 100644 index 56b5294b..00000000 --- a/vendor/github.com/influxdata/influxdb/TODO.md +++ /dev/null @@ -1,9 +0,0 @@ -# TODO - -## v2 - -TODO list for v2. Here is a list of things we want to add to v1, but can't because they would be a breaking change. - -- [#1834](https://github.com/influxdata/influxdb/issues/1834): Disallow using time as a tag key or field key. -- [#2124](https://github.com/influxdata/influxdb/issues/2124): Prohibit writes with precision, but without an explicit timestamp. -- [#4461](https://github.com/influxdata/influxdb/issues/4461): Change default time boundaries. diff --git a/vendor/github.com/influxdata/influxdb/appveyor.yml b/vendor/github.com/influxdata/influxdb/appveyor.yml deleted file mode 100644 index 05f77efb..00000000 --- a/vendor/github.com/influxdata/influxdb/appveyor.yml +++ /dev/null @@ -1,37 +0,0 @@ -version: 0.{build} -pull_requests: - do_not_increment_build_number: true -branches: - only: - - master - -os: Windows Server 2012 R2 - -# Custom clone folder (variables are not expanded here). -clone_folder: c:\gopath\src\github.com\influxdata\influxdb - -# Environment variables -environment: - GOROOT: C:\go17 - GOPATH: C:\gopath - -# Scripts that run after cloning repository -install: - - set PATH=%GOROOT%\bin;%GOPATH%\bin;%PATH% - - rmdir c:\go /s /q - - echo %PATH% - - echo %GOPATH% - - cd C:\gopath\src\github.com\influxdata\influxdb - - go version - - go env - - go get github.com/sparrc/gdm - - cd C:\gopath\src\github.com\influxdata\influxdb - - gdm restore - -# To run your custom scripts instead of automatic MSBuild -build_script: - - go get -t -v ./... - - go test -race -v ./... - -# To disable deployment -deploy: off diff --git a/vendor/github.com/influxdata/influxdb/build-docker.sh b/vendor/github.com/influxdata/influxdb/build-docker.sh deleted file mode 100755 index 3e45beac..00000000 --- a/vendor/github.com/influxdata/influxdb/build-docker.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -set -e -x - -GO_VER=${GO_VER:-1.7.4} - -docker run -it -v "${GOPATH}":/gopath -v "$(pwd)":/app -e "GOPATH=/gopath" -w /app golang:$GO_VER sh -c 'CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o influxd ./cmd/influxd' - -docker build -t influxdb . diff --git a/vendor/github.com/influxdata/influxdb/build.py b/vendor/github.com/influxdata/influxdb/build.py deleted file mode 100755 index 46933199..00000000 --- a/vendor/github.com/influxdata/influxdb/build.py +++ /dev/null @@ -1,987 +0,0 @@ -#!/usr/bin/python2.7 -u - -import sys -import os -import subprocess -import time -from datetime import datetime -import shutil -import tempfile -import hashlib -import re -import logging -import argparse - -################ -#### InfluxDB Variables -################ - -# Packaging variables -PACKAGE_NAME = "influxdb" -INSTALL_ROOT_DIR = "/usr/bin" -LOG_DIR = "/var/log/influxdb" -DATA_DIR = "/var/lib/influxdb" -SCRIPT_DIR = "/usr/lib/influxdb/scripts" -CONFIG_DIR = "/etc/influxdb" -LOGROTATE_DIR = "/etc/logrotate.d" -MAN_DIR = "/usr/share/man" - -INIT_SCRIPT = "scripts/init.sh" -SYSTEMD_SCRIPT = "scripts/influxdb.service" -PREINST_SCRIPT = "scripts/pre-install.sh" -POSTINST_SCRIPT = "scripts/post-install.sh" -POSTUNINST_SCRIPT = "scripts/post-uninstall.sh" -LOGROTATE_SCRIPT = "scripts/logrotate" -DEFAULT_CONFIG = "etc/config.sample.toml" - -# Default AWS S3 bucket for uploads -DEFAULT_BUCKET = "dl.influxdata.com/influxdb/artifacts" - -CONFIGURATION_FILES = [ - CONFIG_DIR + '/influxdb.conf', - LOGROTATE_DIR + '/influxdb', -] - -PACKAGE_LICENSE = "MIT" -PACKAGE_URL = "https://github.com/influxdata/influxdb" -MAINTAINER = "support@influxdb.com" -VENDOR = "InfluxData" -DESCRIPTION = "Distributed time-series database." - -prereqs = [ 'git', 'go' ] -go_vet_command = "go tool vet ./" -optional_prereqs = [ 'fpm', 'rpmbuild', 'gpg' ] - -fpm_common_args = "-f -s dir --log error \ ---vendor {} \ ---url {} \ ---after-install {} \ ---before-install {} \ ---after-remove {} \ ---license {} \ ---maintainer {} \ ---directories {} \ ---directories {} \ ---directories {} \ ---description \"{}\"".format( - VENDOR, - PACKAGE_URL, - POSTINST_SCRIPT, - PREINST_SCRIPT, - POSTUNINST_SCRIPT, - PACKAGE_LICENSE, - MAINTAINER, - LOG_DIR, - DATA_DIR, - MAN_DIR, - DESCRIPTION) - -for f in CONFIGURATION_FILES: - fpm_common_args += " --config-files {}".format(f) - -targets = { - 'influx' : './cmd/influx', - 'influxd' : './cmd/influxd', - 'influx_stress' : './cmd/influx_stress', - 'influx_inspect' : './cmd/influx_inspect', - 'influx_tsm' : './cmd/influx_tsm', -} - -supported_builds = { - 'darwin': [ "amd64" ], - 'windows': [ "amd64" ], - 'linux': [ "amd64", "i386", "armhf", "arm64", "armel", "static_i386", "static_amd64" ] -} - -supported_packages = { - "darwin": [ "tar" ], - "linux": [ "deb", "rpm", "tar" ], - "windows": [ "zip" ], -} - -################ -#### InfluxDB Functions -################ - -def print_banner(): - logging.info(""" - ___ __ _ ___ ___ - |_ _|_ _ / _| |_ ___ _| \\| _ ) - | || ' \\| _| | || \\ \\ / |) | _ \\ - |___|_||_|_| |_|\\_,_/_\\_\\___/|___/ - Build Script -""") - -def create_package_fs(build_root): - """Create a filesystem structure to mimic the package filesystem. - """ - logging.debug("Creating package filesystem at location: {}".format(build_root)) - # Using [1:] for the path names due to them being absolute - # (will overwrite previous paths, per 'os.path.join' documentation) - dirs = [ INSTALL_ROOT_DIR[1:], - LOG_DIR[1:], - DATA_DIR[1:], - SCRIPT_DIR[1:], - CONFIG_DIR[1:], - LOGROTATE_DIR[1:], - MAN_DIR[1:] ] - for d in dirs: - os.makedirs(os.path.join(build_root, d)) - os.chmod(os.path.join(build_root, d), 0o755) - -def package_scripts(build_root, config_only=False, windows=False): - """Copy the necessary scripts and configuration files to the package - filesystem. - """ - if config_only: - logging.debug("Copying configuration to build directory.") - shutil.copyfile(DEFAULT_CONFIG, os.path.join(build_root, "influxdb.conf")) - os.chmod(os.path.join(build_root, "influxdb.conf"), 0o644) - else: - logging.debug("Copying scripts and sample configuration to build directory.") - shutil.copyfile(INIT_SCRIPT, os.path.join(build_root, SCRIPT_DIR[1:], INIT_SCRIPT.split('/')[1])) - os.chmod(os.path.join(build_root, SCRIPT_DIR[1:], INIT_SCRIPT.split('/')[1]), 0o644) - shutil.copyfile(SYSTEMD_SCRIPT, os.path.join(build_root, SCRIPT_DIR[1:], SYSTEMD_SCRIPT.split('/')[1])) - os.chmod(os.path.join(build_root, SCRIPT_DIR[1:], SYSTEMD_SCRIPT.split('/')[1]), 0o644) - shutil.copyfile(LOGROTATE_SCRIPT, os.path.join(build_root, LOGROTATE_DIR[1:], "influxdb")) - os.chmod(os.path.join(build_root, LOGROTATE_DIR[1:], "influxdb"), 0o644) - shutil.copyfile(DEFAULT_CONFIG, os.path.join(build_root, CONFIG_DIR[1:], "influxdb.conf")) - os.chmod(os.path.join(build_root, CONFIG_DIR[1:], "influxdb.conf"), 0o644) - -def package_man_files(build_root): - """Copy and gzip man pages to the package filesystem.""" - logging.debug("Installing man pages.") - run("make -C man/ clean install DESTDIR={}/usr".format(build_root)) - for path, dir, files in os.walk(os.path.join(build_root, MAN_DIR[1:])): - for f in files: - run("gzip -9n {}".format(os.path.join(path, f))) - -def run_generate(): - """Run 'go generate' to rebuild any static assets. - """ - logging.info("Running 'go generate'...") - if not check_path_for("statik"): - run("go install github.com/rakyll/statik") - orig_path = None - if os.path.join(os.environ.get("GOPATH"), "bin") not in os.environ["PATH"].split(os.pathsep): - orig_path = os.environ["PATH"].split(os.pathsep) - os.environ["PATH"] = os.environ["PATH"].split(os.pathsep).append(os.path.join(os.environ.get("GOPATH"), "bin")) - run("rm -f ./services/admin/statik/statik.go") - run("go generate ./services/admin") - if orig_path is not None: - os.environ["PATH"] = orig_path - return True - -def go_get(branch, update=False, no_uncommitted=False): - """Retrieve build dependencies or restore pinned dependencies. - """ - if local_changes() and no_uncommitted: - logging.error("There are uncommitted changes in the current directory.") - return False - if not check_path_for("gdm"): - logging.info("Downloading `gdm`...") - get_command = "go get github.com/sparrc/gdm" - run(get_command) - logging.info("Retrieving dependencies with `gdm`...") - sys.stdout.flush() - run("{}/bin/gdm restore -v".format(os.environ.get("GOPATH"))) - return True - -def run_tests(race, parallel, timeout, no_vet): - """Run the Go test suite on binary output. - """ - logging.info("Starting tests...") - if race: - logging.info("Race is enabled.") - if parallel is not None: - logging.info("Using parallel: {}".format(parallel)) - if timeout is not None: - logging.info("Using timeout: {}".format(timeout)) - out = run("go fmt ./...") - if len(out) > 0: - logging.error("Code not formatted. Please use 'go fmt ./...' to fix formatting errors.") - logging.error("{}".format(out)) - return False - if not no_vet: - logging.info("Running 'go vet'...") - out = run(go_vet_command) - if len(out) > 0: - logging.error("Go vet failed. Please run 'go vet ./...' and fix any errors.") - logging.error("{}".format(out)) - return False - else: - logging.info("Skipping 'go vet' call...") - test_command = "go test -v" - if race: - test_command += " -race" - if parallel is not None: - test_command += " -parallel {}".format(parallel) - if timeout is not None: - test_command += " -timeout {}".format(timeout) - test_command += " ./..." - logging.info("Running tests...") - output = run(test_command) - logging.debug("Test output:\n{}".format(output.encode('ascii', 'ignore'))) - return True - -################ -#### All InfluxDB-specific content above this line -################ - -def run(command, allow_failure=False, shell=False): - """Run shell command (convenience wrapper around subprocess). - """ - out = None - logging.debug("{}".format(command)) - try: - if shell: - out = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=shell) - else: - out = subprocess.check_output(command.split(), stderr=subprocess.STDOUT) - out = out.decode('utf-8').strip() - # logging.debug("Command output: {}".format(out)) - except subprocess.CalledProcessError as e: - if allow_failure: - logging.warn("Command '{}' failed with error: {}".format(command, e.output)) - return None - else: - logging.error("Command '{}' failed with error: {}".format(command, e.output)) - sys.exit(1) - except OSError as e: - if allow_failure: - logging.warn("Command '{}' failed with error: {}".format(command, e)) - return out - else: - logging.error("Command '{}' failed with error: {}".format(command, e)) - sys.exit(1) - else: - return out - -def create_temp_dir(prefix = None): - """ Create temporary directory with optional prefix. - """ - if prefix is None: - return tempfile.mkdtemp(prefix="{}-build.".format(PACKAGE_NAME)) - else: - return tempfile.mkdtemp(prefix=prefix) - -def increment_minor_version(version): - """Return the version with the minor version incremented and patch - version set to zero. - """ - ver_list = version.split('.') - if len(ver_list) != 3: - logging.warn("Could not determine how to increment version '{}', will just use provided version.".format(version)) - return version - ver_list[1] = str(int(ver_list[1]) + 1) - ver_list[2] = str(0) - inc_version = '.'.join(ver_list) - logging.debug("Incremented version from '{}' to '{}'.".format(version, inc_version)) - return inc_version - -def get_current_version_tag(): - """Retrieve the raw git version tag. - """ - version = run("git describe --always --tags --abbrev=0") - return version - -def get_current_version(): - """Parse version information from git tag output. - """ - version_tag = get_current_version_tag() - # Remove leading 'v' - if version_tag[0] == 'v': - version_tag = version_tag[1:] - # Replace any '-'/'_' with '~' - if '-' in version_tag: - version_tag = version_tag.replace("-","~") - if '_' in version_tag: - version_tag = version_tag.replace("_","~") - return version_tag - -def get_current_commit(short=False): - """Retrieve the current git commit. - """ - command = None - if short: - command = "git log --pretty=format:'%h' -n 1" - else: - command = "git rev-parse HEAD" - out = run(command) - return out.strip('\'\n\r ') - -def get_current_branch(): - """Retrieve the current git branch. - """ - command = "git rev-parse --abbrev-ref HEAD" - out = run(command) - return out.strip() - -def local_changes(): - """Return True if there are local un-committed changes. - """ - output = run("git diff-files --ignore-submodules --").strip() - if len(output) > 0: - return True - return False - -def get_system_arch(): - """Retrieve current system architecture. - """ - arch = os.uname()[4] - if arch == "x86_64": - arch = "amd64" - elif arch == "386": - arch = "i386" - elif arch == "aarch64": - arch = "arm64" - elif 'arm' in arch: - # Prevent uname from reporting full ARM arch (eg 'armv7l') - arch = "arm" - return arch - -def get_system_platform(): - """Retrieve current system platform. - """ - if sys.platform.startswith("linux"): - return "linux" - else: - return sys.platform - -def get_go_version(): - """Retrieve version information for Go. - """ - out = run("go version") - matches = re.search('go version go(\S+)', out) - if matches is not None: - return matches.groups()[0].strip() - return None - -def check_path_for(b): - """Check the the user's path for the provided binary. - """ - def is_exe(fpath): - return os.path.isfile(fpath) and os.access(fpath, os.X_OK) - - for path in os.environ["PATH"].split(os.pathsep): - path = path.strip('"') - full_path = os.path.join(path, b) - if os.path.isfile(full_path) and os.access(full_path, os.X_OK): - return full_path - -def check_environ(build_dir = None): - """Check environment for common Go variables. - """ - logging.info("Checking environment...") - for v in [ "GOPATH", "GOBIN", "GOROOT" ]: - logging.debug("Using '{}' for {}".format(os.environ.get(v), v)) - - cwd = os.getcwd() - if build_dir is None and os.environ.get("GOPATH") and os.environ.get("GOPATH") not in cwd: - logging.warn("Your current directory is not under your GOPATH. This may lead to build failures.") - return True - -def check_prereqs(): - """Check user path for required dependencies. - """ - logging.info("Checking for dependencies...") - for req in prereqs: - if not check_path_for(req): - logging.error("Could not find dependency: {}".format(req)) - return False - return True - -def upload_packages(packages, bucket_name=None, overwrite=False): - """Upload provided package output to AWS S3. - """ - logging.debug("Uploading files to bucket '{}': {}".format(bucket_name, packages)) - try: - import boto - from boto.s3.key import Key - from boto.s3.connection import OrdinaryCallingFormat - logging.getLogger("boto").setLevel(logging.WARNING) - except ImportError: - logging.warn("Cannot upload packages without 'boto' Python library!") - return False - logging.info("Connecting to AWS S3...") - # Up the number of attempts to 10 from default of 1 - boto.config.add_section("Boto") - boto.config.set("Boto", "metadata_service_num_attempts", "10") - c = boto.connect_s3(calling_format=OrdinaryCallingFormat()) - if bucket_name is None: - bucket_name = DEFAULT_BUCKET - bucket = c.get_bucket(bucket_name.split('/')[0]) - for p in packages: - if '/' in bucket_name: - # Allow for nested paths within the bucket name (ex: - # bucket/folder). Assuming forward-slashes as path - # delimiter. - name = os.path.join('/'.join(bucket_name.split('/')[1:]), - os.path.basename(p)) - else: - name = os.path.basename(p) - logging.debug("Using key: {}".format(name)) - if bucket.get_key(name) is None or overwrite: - logging.info("Uploading file {}".format(name)) - k = Key(bucket) - k.key = name - if overwrite: - n = k.set_contents_from_filename(p, replace=True) - else: - n = k.set_contents_from_filename(p, replace=False) - k.make_public() - else: - logging.warn("Not uploading file {}, as it already exists in the target bucket.".format(name)) - return True - -def go_list(vendor=False, relative=False): - """ - Return a list of packages - If vendor is False vendor package are not included - If relative is True the package prefix defined by PACKAGE_URL is stripped - """ - p = subprocess.Popen(["go", "list", "./..."], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - out, err = p.communicate() - packages = out.split('\n') - if packages[-1] == '': - packages = packages[:-1] - if not vendor: - non_vendor = [] - for p in packages: - if '/vendor/' not in p: - non_vendor.append(p) - packages = non_vendor - if relative: - relative_pkgs = [] - for p in packages: - r = p.replace(PACKAGE_URL, '.') - if r != '.': - relative_pkgs.append(r) - packages = relative_pkgs - return packages - -def build(version=None, - platform=None, - arch=None, - nightly=False, - race=False, - clean=False, - outdir=".", - tags=[], - static=False): - """Build each target for the specified architecture and platform. - """ - logging.info("Starting build for {}/{}...".format(platform, arch)) - logging.info("Using Go version: {}".format(get_go_version())) - logging.info("Using git branch: {}".format(get_current_branch())) - logging.info("Using git commit: {}".format(get_current_commit())) - if static: - logging.info("Using statically-compiled output.") - if race: - logging.info("Race is enabled.") - if len(tags) > 0: - logging.info("Using build tags: {}".format(','.join(tags))) - - logging.info("Sending build output to: {}".format(outdir)) - if not os.path.exists(outdir): - os.makedirs(outdir) - elif clean and outdir != '/' and outdir != ".": - logging.info("Cleaning build directory '{}' before building.".format(outdir)) - shutil.rmtree(outdir) - os.makedirs(outdir) - - logging.info("Using version '{}' for build.".format(version)) - - for target, path in targets.items(): - logging.info("Building target: {}".format(target)) - build_command = "" - - # Handle static binary output - if static is True or "static_" in arch: - if "static_" in arch: - static = True - arch = arch.replace("static_", "") - build_command += "CGO_ENABLED=0 " - - # Handle variations in architecture output - if arch == "i386" or arch == "i686": - arch = "386" - elif "arm" in arch: - arch = "arm" - build_command += "GOOS={} GOARCH={} ".format(platform, arch) - - if "arm" in arch: - if arch == "armel": - build_command += "GOARM=5 " - elif arch == "armhf" or arch == "arm": - build_command += "GOARM=6 " - elif arch == "arm64": - # TODO(rossmcdonald) - Verify this is the correct setting for arm64 - build_command += "GOARM=7 " - else: - logging.error("Invalid ARM architecture specified: {}".format(arch)) - logging.error("Please specify either 'armel', 'armhf', or 'arm64'.") - return False - if platform == 'windows': - target = target + '.exe' - build_command += "go build -o {} ".format(os.path.join(outdir, target)) - if race: - build_command += "-race " - if len(tags) > 0: - build_command += "-tags {} ".format(','.join(tags)) - if "1.4" in get_go_version(): - if static: - build_command += "-ldflags=\"-s -X main.version {} -X main.branch {} -X main.commit {}\" ".format(version, - get_current_branch(), - get_current_commit()) - else: - build_command += "-ldflags=\"-X main.version {} -X main.branch {} -X main.commit {}\" ".format(version, - get_current_branch(), - get_current_commit()) - - else: - # Starting with Go 1.5, the linker flag arguments changed to 'name=value' from 'name value' - if static: - build_command += "-ldflags=\"-s -X main.version={} -X main.branch={} -X main.commit={}\" ".format(version, - get_current_branch(), - get_current_commit()) - else: - build_command += "-ldflags=\"-X main.version={} -X main.branch={} -X main.commit={}\" ".format(version, - get_current_branch(), - get_current_commit()) - if static: - build_command += "-a -installsuffix cgo " - build_command += path - start_time = datetime.utcnow() - run(build_command, shell=True) - end_time = datetime.utcnow() - logging.info("Time taken: {}s".format((end_time - start_time).total_seconds())) - return True - -def generate_md5_from_file(path): - """Generate MD5 signature based on the contents of the file at path. - """ - m = hashlib.md5() - with open(path, 'rb') as f: - for chunk in iter(lambda: f.read(4096), b""): - m.update(chunk) - return m.hexdigest() - -def generate_sig_from_file(path): - """Generate a detached GPG signature from the file at path. - """ - logging.debug("Generating GPG signature for file: {}".format(path)) - gpg_path = check_path_for('gpg') - if gpg_path is None: - logging.warn("gpg binary not found on path! Skipping signature creation.") - return False - if os.environ.get("GNUPG_HOME") is not None: - run('gpg --homedir {} --armor --yes --detach-sign {}'.format(os.environ.get("GNUPG_HOME"), path)) - else: - run('gpg --armor --detach-sign --yes {}'.format(path)) - return True - -def package(build_output, pkg_name, version, nightly=False, iteration=1, static=False, release=False): - """Package the output of the build process. - """ - outfiles = [] - tmp_build_dir = create_temp_dir() - logging.debug("Packaging for build output: {}".format(build_output)) - logging.info("Using temporary directory: {}".format(tmp_build_dir)) - try: - for platform in build_output: - # Create top-level folder displaying which platform (linux, etc) - os.makedirs(os.path.join(tmp_build_dir, platform)) - for arch in build_output[platform]: - logging.info("Creating packages for {}/{}".format(platform, arch)) - # Create second-level directory displaying the architecture (amd64, etc) - current_location = build_output[platform][arch] - - # Create directory tree to mimic file system of package - build_root = os.path.join(tmp_build_dir, - platform, - arch, - '{}-{}-{}'.format(PACKAGE_NAME, version, iteration)) - os.makedirs(build_root) - - # Copy packaging scripts to build directory - if platform == "windows": - # For windows and static builds, just copy - # binaries to root of package (no other scripts or - # directories) - package_scripts(build_root, config_only=True, windows=True) - elif static or "static_" in arch: - package_scripts(build_root, config_only=True) - else: - create_package_fs(build_root) - package_scripts(build_root) - - if platform != "windows": - package_man_files(build_root) - - for binary in targets: - # Copy newly-built binaries to packaging directory - if platform == 'windows': - binary = binary + '.exe' - if platform == 'windows' or static or "static_" in arch: - # Where the binary should go in the package filesystem - to = os.path.join(build_root, binary) - # Where the binary currently is located - fr = os.path.join(current_location, binary) - else: - # Where the binary currently is located - fr = os.path.join(current_location, binary) - # Where the binary should go in the package filesystem - to = os.path.join(build_root, INSTALL_ROOT_DIR[1:], binary) - shutil.copy(fr, to) - - for package_type in supported_packages[platform]: - # Package the directory structure for each package type for the platform - logging.debug("Packaging directory '{}' as '{}'.".format(build_root, package_type)) - name = pkg_name - # Reset version, iteration, and current location on each run - # since they may be modified below. - package_version = version - package_iteration = iteration - if "static_" in arch: - # Remove the "static_" from the displayed arch on the package - package_arch = arch.replace("static_", "") - else: - package_arch = arch - if not release and not nightly: - # For non-release builds, just use the commit hash as the version - package_version = "{}~{}".format(version, - get_current_commit(short=True)) - package_iteration = "0" - package_build_root = build_root - current_location = build_output[platform][arch] - - if package_type in ['zip', 'tar']: - # For tars and zips, start the packaging one folder above - # the build root (to include the package name) - package_build_root = os.path.join('/', '/'.join(build_root.split('/')[:-1])) - if nightly: - if static or "static_" in arch: - name = '{}-static-nightly_{}_{}'.format(name, - platform, - package_arch) - else: - name = '{}-nightly_{}_{}'.format(name, - platform, - package_arch) - else: - if static or "static_" in arch: - name = '{}-{}-static_{}_{}'.format(name, - package_version, - platform, - package_arch) - else: - name = '{}-{}_{}_{}'.format(name, - package_version, - platform, - package_arch) - current_location = os.path.join(os.getcwd(), current_location) - if package_type == 'tar': - tar_command = "cd {} && tar -cvzf {}.tar.gz ./*".format(package_build_root, name) - run(tar_command, shell=True) - run("mv {}.tar.gz {}".format(os.path.join(package_build_root, name), current_location), shell=True) - outfile = os.path.join(current_location, name + ".tar.gz") - outfiles.append(outfile) - elif package_type == 'zip': - zip_command = "cd {} && zip -r {}.zip ./*".format(package_build_root, name) - run(zip_command, shell=True) - run("mv {}.zip {}".format(os.path.join(package_build_root, name), current_location), shell=True) - outfile = os.path.join(current_location, name + ".zip") - outfiles.append(outfile) - elif package_type not in ['zip', 'tar'] and static or "static_" in arch: - logging.info("Skipping package type '{}' for static builds.".format(package_type)) - else: - fpm_command = "fpm {} --name {} -a {} -t {} --version {} --iteration {} -C {} -p {} ".format( - fpm_common_args, - name, - package_arch, - package_type, - package_version, - package_iteration, - package_build_root, - current_location) - if package_type == "rpm": - fpm_command += "--depends coreutils --rpm-posttrans {}".format(POSTINST_SCRIPT) - out = run(fpm_command, shell=True) - matches = re.search(':path=>"(.*)"', out) - outfile = None - if matches is not None: - outfile = matches.groups()[0] - if outfile is None: - logging.warn("Could not determine output from packaging output!") - else: - if nightly: - # Strip nightly version from package name - new_outfile = outfile.replace("{}-{}".format(package_version, package_iteration), "nightly") - os.rename(outfile, new_outfile) - outfile = new_outfile - else: - if package_type == 'rpm': - # rpm's convert any dashes to underscores - package_version = package_version.replace("-", "_") - new_outfile = outfile.replace("{}-{}".format(package_version, package_iteration), package_version) - os.rename(outfile, new_outfile) - outfile = new_outfile - outfiles.append(os.path.join(os.getcwd(), outfile)) - logging.debug("Produced package files: {}".format(outfiles)) - return outfiles - finally: - # Cleanup - shutil.rmtree(tmp_build_dir) - -def main(args): - global PACKAGE_NAME - - if args.release and args.nightly: - logging.error("Cannot be both a nightly and a release.") - return 1 - - if args.nightly: - args.version = increment_minor_version(args.version) - args.version = "{}~n{}".format(args.version, - datetime.utcnow().strftime("%Y%m%d%H%M")) - args.iteration = 0 - - # Pre-build checks - check_environ() - if not check_prereqs(): - return 1 - if args.build_tags is None: - args.build_tags = [] - else: - args.build_tags = args.build_tags.split(',') - - orig_commit = get_current_commit(short=True) - orig_branch = get_current_branch() - - if args.platform not in supported_builds and args.platform != 'all': - logging.error("Invalid build platform: {}".format(target_platform)) - return 1 - - build_output = {} - - if args.branch != orig_branch and args.commit != orig_commit: - logging.error("Can only specify one branch or commit to build from.") - return 1 - elif args.branch != orig_branch: - logging.info("Moving to git branch: {}".format(args.branch)) - run("git checkout {}".format(args.branch)) - elif args.commit != orig_commit: - logging.info("Moving to git commit: {}".format(args.commit)) - run("git checkout {}".format(args.commit)) - - if not args.no_get: - if not go_get(args.branch, update=args.update, no_uncommitted=args.no_uncommitted): - return 1 - - if args.generate: - if not run_generate(): - return 1 - - if args.test: - if not run_tests(args.race, args.parallel, args.timeout, args.no_vet): - return 1 - - platforms = [] - single_build = True - if args.platform == 'all': - platforms = supported_builds.keys() - single_build = False - else: - platforms = [args.platform] - - for platform in platforms: - build_output.update( { platform : {} } ) - archs = [] - if args.arch == "all": - single_build = False - archs = supported_builds.get(platform) - else: - archs = [args.arch] - - for arch in archs: - od = args.outdir - if not single_build: - od = os.path.join(args.outdir, platform, arch) - if not build(version=args.version, - platform=platform, - arch=arch, - nightly=args.nightly, - race=args.race, - clean=args.clean, - outdir=od, - tags=args.build_tags, - static=args.static): - return 1 - build_output.get(platform).update( { arch : od } ) - - # Build packages - if args.package: - if not check_path_for("fpm"): - logging.error("FPM ruby gem required for packaging. Stopping.") - return 1 - packages = package(build_output, - args.name, - args.version, - nightly=args.nightly, - iteration=args.iteration, - static=args.static, - release=args.release) - if args.sign: - logging.debug("Generating GPG signatures for packages: {}".format(packages)) - sigs = [] # retain signatures so they can be uploaded with packages - for p in packages: - if generate_sig_from_file(p): - sigs.append(p + '.asc') - else: - logging.error("Creation of signature for package [{}] failed!".format(p)) - return 1 - packages += sigs - if args.upload: - logging.debug("Files staged for upload: {}".format(packages)) - if args.nightly: - args.upload_overwrite = True - if not upload_packages(packages, bucket_name=args.bucket, overwrite=args.upload_overwrite): - return 1 - logging.info("Packages created:") - for p in packages: - logging.info("{} (MD5={})".format(p.split('/')[-1:][0], - generate_md5_from_file(p))) - if orig_branch != get_current_branch(): - logging.info("Moving back to original git branch: {}".format(orig_branch)) - run("git checkout {}".format(orig_branch)) - - return 0 - -if __name__ == '__main__': - LOG_LEVEL = logging.INFO - if '--debug' in sys.argv[1:]: - LOG_LEVEL = logging.DEBUG - log_format = '[%(levelname)s] %(funcName)s: %(message)s' - logging.basicConfig(level=LOG_LEVEL, - format=log_format) - - parser = argparse.ArgumentParser(description='InfluxDB build and packaging script.') - parser.add_argument('--verbose','-v','--debug', - action='store_true', - help='Use debug output') - parser.add_argument('--outdir', '-o', - metavar='', - default='./build/', - type=os.path.abspath, - help='Output directory') - parser.add_argument('--name', '-n', - metavar='', - default=PACKAGE_NAME, - type=str, - help='Name to use for package name (when package is specified)') - parser.add_argument('--arch', - metavar='', - type=str, - default=get_system_arch(), - help='Target architecture for build output') - parser.add_argument('--platform', - metavar='', - type=str, - default=get_system_platform(), - help='Target platform for build output') - parser.add_argument('--branch', - metavar='', - type=str, - default=get_current_branch(), - help='Build from a specific branch') - parser.add_argument('--commit', - metavar='', - type=str, - default=get_current_commit(short=True), - help='Build from a specific commit') - parser.add_argument('--version', - metavar='', - type=str, - default=get_current_version(), - help='Version information to apply to build output (ex: 0.12.0)') - parser.add_argument('--iteration', - metavar='', - type=str, - default="1", - help='Package iteration to apply to build output (defaults to 1)') - parser.add_argument('--stats', - action='store_true', - help='Emit build metrics (requires InfluxDB Python client)') - parser.add_argument('--stats-server', - metavar='', - type=str, - help='Send build stats to InfluxDB using provided hostname and port') - parser.add_argument('--stats-db', - metavar='', - type=str, - help='Send build stats to InfluxDB using provided database name') - parser.add_argument('--nightly', - action='store_true', - help='Mark build output as nightly build (will incremement the minor version)') - parser.add_argument('--update', - action='store_true', - help='Update build dependencies prior to building') - parser.add_argument('--package', - action='store_true', - help='Package binary output') - parser.add_argument('--release', - action='store_true', - help='Mark build output as release') - parser.add_argument('--clean', - action='store_true', - help='Clean output directory before building') - parser.add_argument('--no-get', - action='store_true', - help='Do not retrieve pinned dependencies when building') - parser.add_argument('--no-uncommitted', - action='store_true', - help='Fail if uncommitted changes exist in the working directory') - parser.add_argument('--upload', - action='store_true', - help='Upload output packages to AWS S3') - parser.add_argument('--upload-overwrite','-w', - action='store_true', - help='Upload output packages to AWS S3') - parser.add_argument('--bucket', - metavar='', - type=str, - default=DEFAULT_BUCKET, - help='Destination bucket for uploads') - parser.add_argument('--generate', - action='store_true', - help='Run "go generate" before building') - parser.add_argument('--build-tags', - metavar='', - help='Optional build tags to use for compilation') - parser.add_argument('--static', - action='store_true', - help='Create statically-compiled binary output') - parser.add_argument('--sign', - action='store_true', - help='Create GPG detached signatures for packages (when package is specified)') - parser.add_argument('--test', - action='store_true', - help='Run tests (does not produce build output)') - parser.add_argument('--no-vet', - action='store_true', - help='Do not run "go vet" when running tests') - parser.add_argument('--race', - action='store_true', - help='Enable race flag for build output') - parser.add_argument('--parallel', - metavar='', - type=int, - help='Number of tests to run simultaneously') - parser.add_argument('--timeout', - metavar='', - type=str, - help='Timeout for tests before failing') - args = parser.parse_args() - print_banner() - sys.exit(main(args)) diff --git a/vendor/github.com/influxdata/influxdb/build.sh b/vendor/github.com/influxdata/influxdb/build.sh deleted file mode 100755 index 0f80ac78..00000000 --- a/vendor/github.com/influxdata/influxdb/build.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -# Run the build utility via Docker - -set -e - -# Make sure our working dir is the dir of the script -DIR=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd) -cd $DIR - - -# Build new docker image -docker build -f Dockerfile_build_ubuntu64 -t influxdb-builder $DIR -echo "Running build.py" -# Run docker -docker run --rm \ - -e AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID" \ - -e AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY" \ - -v $HOME/.aws.conf:/root/.aws.conf \ - -v $DIR:/root/go/src/github.com/influxdata/influxdb \ - influxdb-builder \ - "$@" - diff --git a/vendor/github.com/influxdata/influxdb/circle-test.sh b/vendor/github.com/influxdata/influxdb/circle-test.sh deleted file mode 100755 index 6b34043b..00000000 --- a/vendor/github.com/influxdata/influxdb/circle-test.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash -# -# This is the InfluxDB test script for CircleCI, it is a light wrapper around ./test.sh. - -# Exit if any command fails -set -e - -# Get dir of script and make it is our working directory. -DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) -cd $DIR - -export OUTPUT_DIR="$CIRCLE_ARTIFACTS" -# Don't delete the container since CircleCI doesn't have permission to do so. -export DOCKER_RM="false" - -# Get number of test environments. -count=$(./test.sh count) -# Check that we aren't wasting CircleCI nodes. -if [ $CIRCLE_NODE_TOTAL -gt $count ] -then - echo "More CircleCI nodes allocated than tests environments to run!" - exit 1 -fi - -# Map CircleCI nodes to test environments. -tests=$(seq 0 $((count - 1))) -for i in $tests -do - mine=$(( $i % $CIRCLE_NODE_TOTAL )) - if [ $mine -eq $CIRCLE_NODE_INDEX ] - then - echo "Running test env index: $i" - ./test.sh $i - fi -done diff --git a/vendor/github.com/influxdata/influxdb/circle.yml b/vendor/github.com/influxdata/influxdb/circle.yml deleted file mode 100644 index cac10049..00000000 --- a/vendor/github.com/influxdata/influxdb/circle.yml +++ /dev/null @@ -1,41 +0,0 @@ -machine: - services: - - docker - environment: - GODIST: "go1.7.4.linux-amd64.tar.gz" - post: - - mkdir -p download - - test -e download/$GODIST || curl -o download/$GODIST https://storage.googleapis.com/golang/$GODIST - - sudo rm -rf /usr/local/go - - sudo tar -C /usr/local -xzf download/$GODIST - -dependencies: - cache_directories: - - "~/docker" - - ~/download - override: - - ./test.sh save: - # building the docker images can take a long time, hence caching - timeout: 1800 - -test: - override: - - bash circle-test.sh: - parallel: true - -deployment: - release: - tag: /^v[0-9]+(\.[0-9]+)*(\S*)$/ - commands: - - > - docker run - -e "AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID" - -e "AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY" - -v $(pwd):/root/go/src/github.com/influxdata/influxdb - influxdb_build_ubuntu64 - --release - --package - --platform all - --arch all - --upload - --bucket dl.influxdata.com/influxdb/releases diff --git a/vendor/github.com/influxdata/influxdb/client/README.md b/vendor/github.com/influxdata/influxdb/client/README.md deleted file mode 100644 index 2ec8afbc..00000000 --- a/vendor/github.com/influxdata/influxdb/client/README.md +++ /dev/null @@ -1,292 +0,0 @@ -# InfluxDB Client - -[![GoDoc](https://godoc.org/github.com/influxdata/influxdb?status.svg)](http://godoc.org/github.com/influxdata/influxdb1-client/v2) - -## Description - -**NOTE:** The Go client library now has a "v2" version, with the old version -being deprecated. The new version can be imported at -`import "github.com/influxdata/influxdb1-client/v2"`. It is not backwards-compatible. - -A Go client library written and maintained by the **InfluxDB** team. -This package provides convenience functions to read and write time series data. -It uses the HTTP protocol to communicate with your **InfluxDB** cluster. - - -## Getting Started - -### Connecting To Your Database - -Connecting to an **InfluxDB** database is straightforward. You will need a host -name, a port and the cluster user credentials if applicable. The default port is -8086. You can customize these settings to your specific installation via the -**InfluxDB** configuration file. - -Though not necessary for experimentation, you may want to create a new user -and authenticate the connection to your database. - -For more information please check out the -[Admin Docs](https://docs.influxdata.com/influxdb/latest/administration/). - -For the impatient, you can create a new admin user _bubba_ by firing off the -[InfluxDB CLI](https://github.com/influxdata/influxdb/blob/master/cmd/influx/main.go). - -```shell -influx -> create user bubba with password 'bumblebeetuna' -> grant all privileges to bubba -``` - -And now for good measure set the credentials in you shell environment. -In the example below we will use $INFLUX_USER and $INFLUX_PWD - -Now with the administrivia out of the way, let's connect to our database. - -NOTE: If you've opted out of creating a user, you can omit Username and Password in -the configuration below. - -```go -package main - -import ( - "log" - "time" - - "github.com/influxdata/influxdb1-client/v2" -) - -const ( - MyDB = "square_holes" - username = "bubba" - password = "bumblebeetuna" -) - -func main() { - // Make client - c, err := client.NewHTTPClient(client.HTTPConfig{ - Addr: "http://localhost:8086", - Username: username, - Password: password, - }) - - if err != nil { - log.Fatalln("Error: ", err) - } - - // Create a new point batch - bp, err := client.NewBatchPoints(client.BatchPointsConfig{ - Database: MyDB, - Precision: "s", - }) - - if err != nil { - log.Fatalln("Error: ", err) - } - - // Create a point and add to batch - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{ - "idle": 10.1, - "system": 53.3, - "user": 46.6, - } - pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now()) - - if err != nil { - log.Fatalln("Error: ", err) - } - - bp.AddPoint(pt) - - // Write the batch - c.Write(bp) -} - -``` - -### Inserting Data - -Time series data aka *points* are written to the database using batch inserts. -The mechanism is to create one or more points and then create a batch aka -*batch points* and write these to a given database and series. A series is a -combination of a measurement (time/values) and a set of tags. - -In this sample we will create a batch of a 1,000 points. Each point has a time and -a single value as well as 2 tags indicating a shape and color. We write these points -to a database called _square_holes_ using a measurement named _shapes_. - -NOTE: You can specify a RetentionPolicy as part of the batch points. If not -provided InfluxDB will use the database _default_ retention policy. - -```go -func writePoints(clnt client.Client) { - sampleSize := 1000 - rand.Seed(42) - - bp, _ := client.NewBatchPoints(client.BatchPointsConfig{ - Database: "systemstats", - Precision: "us", - }) - - for i := 0; i < sampleSize; i++ { - regions := []string{"us-west1", "us-west2", "us-west3", "us-east1"} - tags := map[string]string{ - "cpu": "cpu-total", - "host": fmt.Sprintf("host%d", rand.Intn(1000)), - "region": regions[rand.Intn(len(regions))], - } - - idle := rand.Float64() * 100.0 - fields := map[string]interface{}{ - "idle": idle, - "busy": 100.0 - idle, - } - - bp.AddPoint(client.NewPoint( - "cpu_usage", - tags, - fields, - time.Now(), - )) - } - - err := clnt.Write(bp) - if err != nil { - log.Fatal(err) - } -} -``` - - -### Querying Data - -One nice advantage of using **InfluxDB** the ability to query your data using familiar -SQL constructs. In this example we can create a convenience function to query the database -as follows: - -```go -// queryDB convenience function to query the database -func queryDB(clnt client.Client, cmd string) (res []client.Result, err error) { - q := client.Query{ - Command: cmd, - Database: MyDB, - } - if response, err := clnt.Query(q); err == nil { - if response.Error() != nil { - return res, response.Error() - } - res = response.Results - } else { - return res, err - } - return res, nil -} -``` - -#### Creating a Database - -```go -_, err := queryDB(clnt, fmt.Sprintf("CREATE DATABASE %s", MyDB)) -if err != nil { - log.Fatal(err) -} -``` - -#### Count Records - -```go -q := fmt.Sprintf("SELECT count(%s) FROM %s", "value", MyMeasurement) -res, err := queryDB(clnt, q) -if err != nil { - log.Fatal(err) -} -count := res[0].Series[0].Values[0][1] -log.Printf("Found a total of %v records\n", count) -``` - -#### Find the last 10 _shapes_ records - -```go -q := fmt.Sprintf("SELECT * FROM %s LIMIT %d", MyMeasurement, 20) -res, err = queryDB(clnt, q) -if err != nil { - log.Fatal(err) -} - -for i, row := range res[0].Series[0].Values { - t, err := time.Parse(time.RFC3339, row[0].(string)) - if err != nil { - log.Fatal(err) - } - val := row[1].(string) - log.Printf("[%2d] %s: %s\n", i, t.Format(time.Stamp), val) -} -``` - -### Using the UDP Client - -The **InfluxDB** client also supports writing over UDP. - -```go -func WriteUDP() { - // Make client - c, err := client.NewUDPClient("localhost:8089") - if err != nil { - panic(err.Error()) - } - - // Create a new point batch - bp, _ := client.NewBatchPoints(client.BatchPointsConfig{ - Precision: "s", - }) - - // Create a point and add to batch - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{ - "idle": 10.1, - "system": 53.3, - "user": 46.6, - } - pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now()) - if err != nil { - panic(err.Error()) - } - bp.AddPoint(pt) - - // Write the batch - c.Write(bp) -} -``` - -### Point Splitting - -The UDP client now supports splitting single points that exceed the configured -payload size. The logic for processing each point is listed here, starting with -an empty payload. - -1. If adding the point to the current (non-empty) payload would exceed the - configured size, send the current payload. Otherwise, add it to the current - payload. -1. If the point is smaller than the configured size, add it to the payload. -1. If the point has no timestamp, just try to send the entire point as a single - UDP payload, and process the next point. -1. Since the point has a timestamp, re-use the existing measurement name, - tagset, and timestamp and create multiple new points by splitting up the - fields. The per-point length will be kept close to the configured size, - staying under it if possible. This does mean that one large field, maybe a - long string, could be sent as a larger-than-configured payload. - -The above logic attempts to respect configured payload sizes, but not sacrifice -any data integrity. Points without a timestamp can't be split, as that may -cause fields to have differing timestamps when processed by the server. - -## Go Docs - -Please refer to -[http://godoc.org/github.com/influxdata/influxdb1-client/v2](http://godoc.org/github.com/influxdata/influxdb1-client/v2) -for documentation. - -## See Also - -You can also examine how the client library is used by the -[InfluxDB CLI](https://github.com/influxdata/influxdb/blob/master/cmd/influx/main.go). diff --git a/vendor/github.com/influxdata/influxdb/client/example_test.go b/vendor/github.com/influxdata/influxdb/client/example_test.go deleted file mode 100644 index f3753834..00000000 --- a/vendor/github.com/influxdata/influxdb/client/example_test.go +++ /dev/null @@ -1,113 +0,0 @@ -package client_test - -import ( - "fmt" - "log" - "math/rand" - "net/url" - "os" - "strconv" - "time" - - "github.com/influxdata/influxdb/client" -) - -func ExampleNewClient() { - host, err := url.Parse(fmt.Sprintf("http://%s:%d", "localhost", 8086)) - if err != nil { - log.Fatal(err) - } - - // NOTE: this assumes you've setup a user and have setup shell env variables, - // namely INFLUX_USER/INFLUX_PWD. If not just omit Username/Password below. - conf := client.Config{ - URL: *host, - Username: os.Getenv("INFLUX_USER"), - Password: os.Getenv("INFLUX_PWD"), - } - con, err := client.NewClient(conf) - if err != nil { - log.Fatal(err) - } - log.Println("Connection", con) -} - -func ExampleClient_Ping() { - host, err := url.Parse(fmt.Sprintf("http://%s:%d", "localhost", 8086)) - if err != nil { - log.Fatal(err) - } - con, err := client.NewClient(client.Config{URL: *host}) - if err != nil { - log.Fatal(err) - } - - dur, ver, err := con.Ping() - if err != nil { - log.Fatal(err) - } - log.Printf("Happy as a hippo! %v, %s", dur, ver) -} - -func ExampleClient_Query() { - host, err := url.Parse(fmt.Sprintf("http://%s:%d", "localhost", 8086)) - if err != nil { - log.Fatal(err) - } - con, err := client.NewClient(client.Config{URL: *host}) - if err != nil { - log.Fatal(err) - } - - q := client.Query{ - Command: "select count(value) from shapes", - Database: "square_holes", - } - if response, err := con.Query(q); err == nil && response.Error() == nil { - log.Println(response.Results) - } -} - -func ExampleClient_Write() { - host, err := url.Parse(fmt.Sprintf("http://%s:%d", "localhost", 8086)) - if err != nil { - log.Fatal(err) - } - con, err := client.NewClient(client.Config{URL: *host}) - if err != nil { - log.Fatal(err) - } - - var ( - shapes = []string{"circle", "rectangle", "square", "triangle"} - colors = []string{"red", "blue", "green"} - sampleSize = 1000 - pts = make([]client.Point, sampleSize) - ) - - rand.Seed(42) - for i := 0; i < sampleSize; i++ { - pts[i] = client.Point{ - Measurement: "shapes", - Tags: map[string]string{ - "color": strconv.Itoa(rand.Intn(len(colors))), - "shape": strconv.Itoa(rand.Intn(len(shapes))), - }, - Fields: map[string]interface{}{ - "value": rand.Intn(sampleSize), - }, - Time: time.Now(), - Precision: "s", - } - } - - bps := client.BatchPoints{ - Points: pts, - Database: "BumbeBeeTuna", - RetentionPolicy: "default", - } - _, err = con.Write(bps) - if err != nil { - log.Fatal(err) - } -} diff --git a/vendor/github.com/influxdata/influxdb/client/influxdb.go b/vendor/github.com/influxdata/influxdb/client/influxdb.go deleted file mode 100644 index 909fe747..00000000 --- a/vendor/github.com/influxdata/influxdb/client/influxdb.go +++ /dev/null @@ -1,829 +0,0 @@ -package client // import "github.com/influxdata/influxdb/client" - -import ( - "bytes" - "crypto/tls" - "encoding/json" - "errors" - "fmt" - "io" - "io/ioutil" - "net" - "net/http" - "net/url" - "strconv" - "strings" - "time" - - "github.com/influxdata/influxdb/models" -) - -const ( - // DefaultHost is the default host used to connect to an InfluxDB instance - DefaultHost = "localhost" - - // DefaultPort is the default port used to connect to an InfluxDB instance - DefaultPort = 8086 - - // DefaultTimeout is the default connection timeout used to connect to an InfluxDB instance - DefaultTimeout = 0 -) - -// Query is used to send a command to the server. Both Command and Database are required. -type Query struct { - Command string - Database string - - // Chunked tells the server to send back chunked responses. This places - // less load on the server by sending back chunks of the response rather - // than waiting for the entire response all at once. - Chunked bool - - // ChunkSize sets the maximum number of rows that will be returned per - // chunk. Chunks are either divided based on their series or if they hit - // the chunk size limit. - // - // Chunked must be set to true for this option to be used. - ChunkSize int -} - -// ParseConnectionString will parse a string to create a valid connection URL -func ParseConnectionString(path string, ssl bool) (url.URL, error) { - var host string - var port int - - h, p, err := net.SplitHostPort(path) - if err != nil { - if path == "" { - host = DefaultHost - } else { - host = path - } - // If they didn't specify a port, always use the default port - port = DefaultPort - } else { - host = h - port, err = strconv.Atoi(p) - if err != nil { - return url.URL{}, fmt.Errorf("invalid port number %q: %s\n", path, err) - } - } - - u := url.URL{ - Scheme: "http", - } - if ssl { - u.Scheme = "https" - } - - u.Host = net.JoinHostPort(host, strconv.Itoa(port)) - - return u, nil -} - -// Config is used to specify what server to connect to. -// URL: The URL of the server connecting to. -// Username/Password are optional. They will be passed via basic auth if provided. -// UserAgent: If not provided, will default "InfluxDBClient", -// Timeout: If not provided, will default to 0 (no timeout) -type Config struct { - URL url.URL - UnixSocket string - Username string - Password string - UserAgent string - Timeout time.Duration - Precision string - WriteConsistency string - UnsafeSsl bool -} - -// NewConfig will create a config to be used in connecting to the client -func NewConfig() Config { - return Config{ - Timeout: DefaultTimeout, - } -} - -// Client is used to make calls to the server. -type Client struct { - url url.URL - unixSocket string - username string - password string - httpClient *http.Client - userAgent string - precision string -} - -const ( - // ConsistencyOne requires at least one data node acknowledged a write. - ConsistencyOne = "one" - - // ConsistencyAll requires all data nodes to acknowledge a write. - ConsistencyAll = "all" - - // ConsistencyQuorum requires a quorum of data nodes to acknowledge a write. - ConsistencyQuorum = "quorum" - - // ConsistencyAny allows for hinted hand off, potentially no write happened yet. - ConsistencyAny = "any" -) - -// NewClient will instantiate and return a connected client to issue commands to the server. -func NewClient(c Config) (*Client, error) { - tlsConfig := &tls.Config{ - InsecureSkipVerify: c.UnsafeSsl, - } - - tr := &http.Transport{ - TLSClientConfig: tlsConfig, - } - - if c.UnixSocket != "" { - // No need for compression in local communications. - tr.DisableCompression = true - - tr.Dial = func(_, _ string) (net.Conn, error) { - return net.Dial("unix", c.UnixSocket) - } - } - - client := Client{ - url: c.URL, - unixSocket: c.UnixSocket, - username: c.Username, - password: c.Password, - httpClient: &http.Client{Timeout: c.Timeout, Transport: tr}, - userAgent: c.UserAgent, - precision: c.Precision, - } - if client.userAgent == "" { - client.userAgent = "InfluxDBClient" - } - return &client, nil -} - -// SetAuth will update the username and passwords -func (c *Client) SetAuth(u, p string) { - c.username = u - c.password = p -} - -// SetPrecision will update the precision -func (c *Client) SetPrecision(precision string) { - c.precision = precision -} - -// Query sends a command to the server and returns the Response -func (c *Client) Query(q Query) (*Response, error) { - u := c.url - - u.Path = "query" - values := u.Query() - values.Set("q", q.Command) - values.Set("db", q.Database) - if q.Chunked { - values.Set("chunked", "true") - if q.ChunkSize > 0 { - values.Set("chunk_size", strconv.Itoa(q.ChunkSize)) - } - } - if c.precision != "" { - values.Set("epoch", c.precision) - } - u.RawQuery = values.Encode() - - req, err := http.NewRequest("POST", u.String(), nil) - if err != nil { - return nil, err - } - req.Header.Set("User-Agent", c.userAgent) - if c.username != "" { - req.SetBasicAuth(c.username, c.password) - } - - resp, err := c.httpClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - var response Response - if q.Chunked { - cr := NewChunkedResponse(resp.Body) - for { - r, err := cr.NextResponse() - if err != nil { - // If we got an error while decoding the response, send that back. - return nil, err - } - - if r == nil { - break - } - - response.Results = append(response.Results, r.Results...) - if r.Err != nil { - response.Err = r.Err - break - } - } - } else { - dec := json.NewDecoder(resp.Body) - dec.UseNumber() - if err := dec.Decode(&response); err != nil { - // Ignore EOF errors if we got an invalid status code. - if !(err == io.EOF && resp.StatusCode != http.StatusOK) { - return nil, err - } - } - } - - // If we don't have an error in our json response, and didn't get StatusOK, - // then send back an error. - if resp.StatusCode != http.StatusOK && response.Error() == nil { - return &response, fmt.Errorf("received status code %d from server", resp.StatusCode) - } - return &response, nil -} - -// Write takes BatchPoints and allows for writing of multiple points with defaults -// If successful, error is nil and Response is nil -// If an error occurs, Response may contain additional information if populated. -func (c *Client) Write(bp BatchPoints) (*Response, error) { - u := c.url - u.Path = "write" - - var b bytes.Buffer - for _, p := range bp.Points { - err := checkPointTypes(p) - if err != nil { - return nil, err - } - if p.Raw != "" { - if _, err := b.WriteString(p.Raw); err != nil { - return nil, err - } - } else { - for k, v := range bp.Tags { - if p.Tags == nil { - p.Tags = make(map[string]string, len(bp.Tags)) - } - p.Tags[k] = v - } - - if _, err := b.WriteString(p.MarshalString()); err != nil { - return nil, err - } - } - - if err := b.WriteByte('\n'); err != nil { - return nil, err - } - } - - req, err := http.NewRequest("POST", u.String(), &b) - if err != nil { - return nil, err - } - req.Header.Set("Content-Type", "") - req.Header.Set("User-Agent", c.userAgent) - if c.username != "" { - req.SetBasicAuth(c.username, c.password) - } - - precision := bp.Precision - if precision == "" { - precision = c.precision - } - - params := req.URL.Query() - params.Set("db", bp.Database) - params.Set("rp", bp.RetentionPolicy) - params.Set("precision", precision) - params.Set("consistency", bp.WriteConsistency) - req.URL.RawQuery = params.Encode() - - resp, err := c.httpClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - var response Response - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK { - var err = fmt.Errorf(string(body)) - response.Err = err - return &response, err - } - - return nil, nil -} - -// WriteLineProtocol takes a string with line returns to delimit each write -// If successful, error is nil and Response is nil -// If an error occurs, Response may contain additional information if populated. -func (c *Client) WriteLineProtocol(data, database, retentionPolicy, precision, writeConsistency string) (*Response, error) { - u := c.url - u.Path = "write" - - r := strings.NewReader(data) - - req, err := http.NewRequest("POST", u.String(), r) - if err != nil { - return nil, err - } - req.Header.Set("Content-Type", "") - req.Header.Set("User-Agent", c.userAgent) - if c.username != "" { - req.SetBasicAuth(c.username, c.password) - } - params := req.URL.Query() - params.Set("db", database) - params.Set("rp", retentionPolicy) - params.Set("precision", precision) - params.Set("consistency", writeConsistency) - req.URL.RawQuery = params.Encode() - - resp, err := c.httpClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - var response Response - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK { - err := fmt.Errorf(string(body)) - response.Err = err - return &response, err - } - - return nil, nil -} - -// Ping will check to see if the server is up -// Ping returns how long the request took, the version of the server it connected to, and an error if one occurred. -func (c *Client) Ping() (time.Duration, string, error) { - now := time.Now() - u := c.url - u.Path = "ping" - - req, err := http.NewRequest("GET", u.String(), nil) - if err != nil { - return 0, "", err - } - req.Header.Set("User-Agent", c.userAgent) - if c.username != "" { - req.SetBasicAuth(c.username, c.password) - } - - resp, err := c.httpClient.Do(req) - if err != nil { - return 0, "", err - } - defer resp.Body.Close() - - version := resp.Header.Get("X-Influxdb-Version") - return time.Since(now), version, nil -} - -// Structs - -// Message represents a user message. -type Message struct { - Level string `json:"level,omitempty"` - Text string `json:"text,omitempty"` -} - -// Result represents a resultset returned from a single statement. -type Result struct { - Series []models.Row - Messages []*Message - Err error -} - -// MarshalJSON encodes the result into JSON. -func (r *Result) MarshalJSON() ([]byte, error) { - // Define a struct that outputs "error" as a string. - var o struct { - Series []models.Row `json:"series,omitempty"` - Messages []*Message `json:"messages,omitempty"` - Err string `json:"error,omitempty"` - } - - // Copy fields to output struct. - o.Series = r.Series - o.Messages = r.Messages - if r.Err != nil { - o.Err = r.Err.Error() - } - - return json.Marshal(&o) -} - -// UnmarshalJSON decodes the data into the Result struct -func (r *Result) UnmarshalJSON(b []byte) error { - var o struct { - Series []models.Row `json:"series,omitempty"` - Messages []*Message `json:"messages,omitempty"` - Err string `json:"error,omitempty"` - } - - dec := json.NewDecoder(bytes.NewBuffer(b)) - dec.UseNumber() - err := dec.Decode(&o) - if err != nil { - return err - } - r.Series = o.Series - r.Messages = o.Messages - if o.Err != "" { - r.Err = errors.New(o.Err) - } - return nil -} - -// Response represents a list of statement results. -type Response struct { - Results []Result - Err error -} - -// MarshalJSON encodes the response into JSON. -func (r *Response) MarshalJSON() ([]byte, error) { - // Define a struct that outputs "error" as a string. - var o struct { - Results []Result `json:"results,omitempty"` - Err string `json:"error,omitempty"` - } - - // Copy fields to output struct. - o.Results = r.Results - if r.Err != nil { - o.Err = r.Err.Error() - } - - return json.Marshal(&o) -} - -// UnmarshalJSON decodes the data into the Response struct -func (r *Response) UnmarshalJSON(b []byte) error { - var o struct { - Results []Result `json:"results,omitempty"` - Err string `json:"error,omitempty"` - } - - dec := json.NewDecoder(bytes.NewBuffer(b)) - dec.UseNumber() - err := dec.Decode(&o) - if err != nil { - return err - } - r.Results = o.Results - if o.Err != "" { - r.Err = errors.New(o.Err) - } - return nil -} - -// Error returns the first error from any statement. -// Returns nil if no errors occurred on any statements. -func (r *Response) Error() error { - if r.Err != nil { - return r.Err - } - for _, result := range r.Results { - if result.Err != nil { - return result.Err - } - } - return nil -} - -// duplexReader reads responses and writes it to another writer while -// satisfying the reader interface. -type duplexReader struct { - r io.Reader - w io.Writer -} - -func (r *duplexReader) Read(p []byte) (n int, err error) { - n, err = r.r.Read(p) - if err == nil { - r.w.Write(p[:n]) - } - return n, err -} - -// ChunkedResponse represents a response from the server that -// uses chunking to stream the output. -type ChunkedResponse struct { - dec *json.Decoder - duplex *duplexReader - buf bytes.Buffer -} - -// NewChunkedResponse reads a stream and produces responses from the stream. -func NewChunkedResponse(r io.Reader) *ChunkedResponse { - resp := &ChunkedResponse{} - resp.duplex = &duplexReader{r: r, w: &resp.buf} - resp.dec = json.NewDecoder(resp.duplex) - resp.dec.UseNumber() - return resp -} - -// NextResponse reads the next line of the stream and returns a response. -func (r *ChunkedResponse) NextResponse() (*Response, error) { - var response Response - if err := r.dec.Decode(&response); err != nil { - if err == io.EOF { - return nil, nil - } - // A decoding error happened. This probably means the server crashed - // and sent a last-ditch error message to us. Ensure we have read the - // entirety of the connection to get any remaining error text. - io.Copy(ioutil.Discard, r.duplex) - return nil, errors.New(strings.TrimSpace(r.buf.String())) - } - r.buf.Reset() - return &response, nil -} - -// Point defines the fields that will be written to the database -// Measurement, Time, and Fields are required -// Precision can be specified if the time is in epoch format (integer). -// Valid values for Precision are n, u, ms, s, m, and h -type Point struct { - Measurement string - Tags map[string]string - Time time.Time - Fields map[string]interface{} - Precision string - Raw string -} - -// MarshalJSON will format the time in RFC3339Nano -// Precision is also ignored as it is only used for writing, not reading -// Or another way to say it is we always send back in nanosecond precision -func (p *Point) MarshalJSON() ([]byte, error) { - point := struct { - Measurement string `json:"measurement,omitempty"` - Tags map[string]string `json:"tags,omitempty"` - Time string `json:"time,omitempty"` - Fields map[string]interface{} `json:"fields,omitempty"` - Precision string `json:"precision,omitempty"` - }{ - Measurement: p.Measurement, - Tags: p.Tags, - Fields: p.Fields, - Precision: p.Precision, - } - // Let it omit empty if it's really zero - if !p.Time.IsZero() { - point.Time = p.Time.UTC().Format(time.RFC3339Nano) - } - return json.Marshal(&point) -} - -// MarshalString renders string representation of a Point with specified -// precision. The default precision is nanoseconds. -func (p *Point) MarshalString() string { - pt, err := models.NewPoint(p.Measurement, models.NewTags(p.Tags), p.Fields, p.Time) - if err != nil { - return "# ERROR: " + err.Error() + " " + p.Measurement - } - if p.Precision == "" || p.Precision == "ns" || p.Precision == "n" { - return pt.String() - } - return pt.PrecisionString(p.Precision) -} - -// UnmarshalJSON decodes the data into the Point struct -func (p *Point) UnmarshalJSON(b []byte) error { - var normal struct { - Measurement string `json:"measurement"` - Tags map[string]string `json:"tags"` - Time time.Time `json:"time"` - Precision string `json:"precision"` - Fields map[string]interface{} `json:"fields"` - } - var epoch struct { - Measurement string `json:"measurement"` - Tags map[string]string `json:"tags"` - Time *int64 `json:"time"` - Precision string `json:"precision"` - Fields map[string]interface{} `json:"fields"` - } - - if err := func() error { - var err error - dec := json.NewDecoder(bytes.NewBuffer(b)) - dec.UseNumber() - if err = dec.Decode(&epoch); err != nil { - return err - } - // Convert from epoch to time.Time, but only if Time - // was actually set. - var ts time.Time - if epoch.Time != nil { - ts, err = EpochToTime(*epoch.Time, epoch.Precision) - if err != nil { - return err - } - } - p.Measurement = epoch.Measurement - p.Tags = epoch.Tags - p.Time = ts - p.Precision = epoch.Precision - p.Fields = normalizeFields(epoch.Fields) - return nil - }(); err == nil { - return nil - } - - dec := json.NewDecoder(bytes.NewBuffer(b)) - dec.UseNumber() - if err := dec.Decode(&normal); err != nil { - return err - } - normal.Time = SetPrecision(normal.Time, normal.Precision) - p.Measurement = normal.Measurement - p.Tags = normal.Tags - p.Time = normal.Time - p.Precision = normal.Precision - p.Fields = normalizeFields(normal.Fields) - - return nil -} - -// Remove any notion of json.Number -func normalizeFields(fields map[string]interface{}) map[string]interface{} { - newFields := map[string]interface{}{} - - for k, v := range fields { - switch v := v.(type) { - case json.Number: - jv, e := v.Float64() - if e != nil { - panic(fmt.Sprintf("unable to convert json.Number to float64: %s", e)) - } - newFields[k] = jv - default: - newFields[k] = v - } - } - return newFields -} - -// BatchPoints is used to send batched data in a single write. -// Database and Points are required -// If no retention policy is specified, it will use the databases default retention policy. -// If tags are specified, they will be "merged" with all points. If a point already has that tag, it will be ignored. -// If time is specified, it will be applied to any point with an empty time. -// Precision can be specified if the time is in epoch format (integer). -// Valid values for Precision are n, u, ms, s, m, and h -type BatchPoints struct { - Points []Point `json:"points,omitempty"` - Database string `json:"database,omitempty"` - RetentionPolicy string `json:"retentionPolicy,omitempty"` - Tags map[string]string `json:"tags,omitempty"` - Time time.Time `json:"time,omitempty"` - Precision string `json:"precision,omitempty"` - WriteConsistency string `json:"-"` -} - -// UnmarshalJSON decodes the data into the BatchPoints struct -func (bp *BatchPoints) UnmarshalJSON(b []byte) error { - var normal struct { - Points []Point `json:"points"` - Database string `json:"database"` - RetentionPolicy string `json:"retentionPolicy"` - Tags map[string]string `json:"tags"` - Time time.Time `json:"time"` - Precision string `json:"precision"` - } - var epoch struct { - Points []Point `json:"points"` - Database string `json:"database"` - RetentionPolicy string `json:"retentionPolicy"` - Tags map[string]string `json:"tags"` - Time *int64 `json:"time"` - Precision string `json:"precision"` - } - - if err := func() error { - var err error - if err = json.Unmarshal(b, &epoch); err != nil { - return err - } - // Convert from epoch to time.Time - var ts time.Time - if epoch.Time != nil { - ts, err = EpochToTime(*epoch.Time, epoch.Precision) - if err != nil { - return err - } - } - bp.Points = epoch.Points - bp.Database = epoch.Database - bp.RetentionPolicy = epoch.RetentionPolicy - bp.Tags = epoch.Tags - bp.Time = ts - bp.Precision = epoch.Precision - return nil - }(); err == nil { - return nil - } - - if err := json.Unmarshal(b, &normal); err != nil { - return err - } - normal.Time = SetPrecision(normal.Time, normal.Precision) - bp.Points = normal.Points - bp.Database = normal.Database - bp.RetentionPolicy = normal.RetentionPolicy - bp.Tags = normal.Tags - bp.Time = normal.Time - bp.Precision = normal.Precision - - return nil -} - -// utility functions - -// Addr provides the current url as a string of the server the client is connected to. -func (c *Client) Addr() string { - if c.unixSocket != "" { - return c.unixSocket - } - return c.url.String() -} - -// checkPointTypes ensures no unsupported types are submitted to influxdb, returning error if they are found. -func checkPointTypes(p Point) error { - for _, v := range p.Fields { - switch v.(type) { - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, float32, float64, bool, string, nil: - return nil - default: - return fmt.Errorf("unsupported point type: %T", v) - } - } - return nil -} - -// helper functions - -// EpochToTime takes a unix epoch time and uses precision to return back a time.Time -func EpochToTime(epoch int64, precision string) (time.Time, error) { - if precision == "" { - precision = "s" - } - var t time.Time - switch precision { - case "h": - t = time.Unix(0, epoch*int64(time.Hour)) - case "m": - t = time.Unix(0, epoch*int64(time.Minute)) - case "s": - t = time.Unix(0, epoch*int64(time.Second)) - case "ms": - t = time.Unix(0, epoch*int64(time.Millisecond)) - case "u": - t = time.Unix(0, epoch*int64(time.Microsecond)) - case "n": - t = time.Unix(0, epoch) - default: - return time.Time{}, fmt.Errorf("Unknown precision %q", precision) - } - return t, nil -} - -// SetPrecision will round a time to the specified precision -func SetPrecision(t time.Time, precision string) time.Time { - switch precision { - case "n": - case "u": - return t.Round(time.Microsecond) - case "ms": - return t.Round(time.Millisecond) - case "s": - return t.Round(time.Second) - case "m": - return t.Round(time.Minute) - case "h": - return t.Round(time.Hour) - } - return t -} diff --git a/vendor/github.com/influxdata/influxdb/client/influxdb_test.go b/vendor/github.com/influxdata/influxdb/client/influxdb_test.go deleted file mode 100644 index 39349c19..00000000 --- a/vendor/github.com/influxdata/influxdb/client/influxdb_test.go +++ /dev/null @@ -1,831 +0,0 @@ -package client_test - -import ( - "crypto/tls" - "encoding/json" - "errors" - "fmt" - "io/ioutil" - "net/http" - "net/http/httptest" - "net/url" - "os" - "strings" - "testing" - "time" - - "github.com/influxdata/influxdb/client" -) - -func BenchmarkWrite(b *testing.B) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var data client.Response - w.WriteHeader(http.StatusNoContent) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - b.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - bp := client.BatchPoints{ - Points: []client.Point{ - {Fields: map[string]interface{}{"value": 101}}}, - } - for i := 0; i < b.N; i++ { - r, err := c.Write(bp) - if err != nil { - b.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - if r != nil { - b.Fatalf("unexpected response. expected %v, actual %v", nil, r) - } - } -} - -func BenchmarkUnmarshalJSON2Tags(b *testing.B) { - var bp client.BatchPoints - data := []byte(` -{ - "database": "foo", - "retentionPolicy": "bar", - "points": [ - { - "name": "cpu", - "tags": { - "host": "server01", - "region": "us-east1" - }, - "time": 14244733039069373, - "precision": "n", - "fields": { - "value": 4541770385657154000 - } - } - ] -} -`) - - for i := 0; i < b.N; i++ { - if err := json.Unmarshal(data, &bp); err != nil { - b.Errorf("unable to unmarshal nanosecond data: %s", err.Error()) - } - b.SetBytes(int64(len(data))) - } -} - -func BenchmarkUnmarshalJSON10Tags(b *testing.B) { - var bp client.BatchPoints - data := []byte(` -{ - "database": "foo", - "retentionPolicy": "bar", - "points": [ - { - "name": "cpu", - "tags": { - "host": "server01", - "region": "us-east1", - "tag1": "value1", - "tag2": "value2", - "tag2": "value3", - "tag4": "value4", - "tag5": "value5", - "tag6": "value6", - "tag7": "value7", - "tag8": "value8" - }, - "time": 14244733039069373, - "precision": "n", - "fields": { - "value": 4541770385657154000 - } - } - ] -} -`) - - for i := 0; i < b.N; i++ { - if err := json.Unmarshal(data, &bp); err != nil { - b.Errorf("unable to unmarshal nanosecond data: %s", err.Error()) - } - b.SetBytes(int64(len(data))) - } -} - -func TestNewClient(t *testing.T) { - config := client.Config{} - _, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestClient_Ping(t *testing.T) { - ts := emptyTestServer() - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - d, version, err := c.Ping() - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - if d.Nanoseconds() == 0 { - t.Fatalf("expected a duration greater than zero. actual %v", d.Nanoseconds()) - } - if version != "x.x" { - t.Fatalf("unexpected version. expected %s, actual %v", "x.x", version) - } -} - -func TestClient_Query(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var data client.Response - w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - query := client.Query{} - _, err = c.Query(query) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestClient_ChunkedQuery(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var data client.Response - w.WriteHeader(http.StatusOK) - enc := json.NewEncoder(w) - _ = enc.Encode(data) - _ = enc.Encode(data) - })) - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - query := client.Query{Chunked: true} - _, err = c.Query(query) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestClient_BasicAuth(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - u, p, ok := r.BasicAuth() - - if !ok { - t.Errorf("basic auth error") - } - if u != "username" { - t.Errorf("unexpected username, expected %q, actual %q", "username", u) - } - if p != "password" { - t.Errorf("unexpected password, expected %q, actual %q", "password", p) - } - w.WriteHeader(http.StatusNoContent) - })) - defer ts.Close() - - u, _ := url.Parse(ts.URL) - u.User = url.UserPassword("username", "password") - config := client.Config{URL: *u, Username: "username", Password: "password"} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - _, _, err = c.Ping() - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestClient_Write(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var data client.Response - w.WriteHeader(http.StatusNoContent) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - bp := client.BatchPoints{} - r, err := c.Write(bp) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - if r != nil { - t.Fatalf("unexpected response. expected %v, actual %v", nil, r) - } -} - -func TestClient_UserAgent(t *testing.T) { - receivedUserAgent := "" - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - receivedUserAgent = r.UserAgent() - - var data client.Response - w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - _, err := http.Get(ts.URL) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - tests := []struct { - name string - userAgent string - expected string - }{ - { - name: "Empty user agent", - userAgent: "", - expected: "InfluxDBClient", - }, - { - name: "Custom user agent", - userAgent: "Test Influx Client", - expected: "Test Influx Client", - }, - } - - for _, test := range tests { - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u, UserAgent: test.userAgent} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - receivedUserAgent = "" - query := client.Query{} - _, err = c.Query(query) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - if !strings.HasPrefix(receivedUserAgent, test.expected) { - t.Fatalf("Unexpected user agent. expected %v, actual %v", test.expected, receivedUserAgent) - } - - receivedUserAgent = "" - bp := client.BatchPoints{} - _, err = c.Write(bp) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - if !strings.HasPrefix(receivedUserAgent, test.expected) { - t.Fatalf("Unexpected user agent. expected %v, actual %v", test.expected, receivedUserAgent) - } - - receivedUserAgent = "" - _, _, err = c.Ping() - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - if receivedUserAgent != test.expected { - t.Fatalf("Unexpected user agent. expected %v, actual %v", test.expected, receivedUserAgent) - } - } -} - -func TestClient_Messages(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"results":[{"messages":[{"level":"warning","text":"deprecation test"}]}]}`)) - })) - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - query := client.Query{} - resp, err := c.Query(query) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - if got, exp := len(resp.Results), 1; got != exp { - t.Fatalf("unexpected number of results. expected %v, actual %v", exp, got) - } - - r := resp.Results[0] - if got, exp := len(r.Messages), 1; got != exp { - t.Fatalf("unexpected number of messages. expected %v, actual %v", exp, got) - } - - m := r.Messages[0] - if got, exp := m.Level, "warning"; got != exp { - t.Errorf("unexpected message level. expected %v, actual %v", exp, got) - } - if got, exp := m.Text, "deprecation test"; got != exp { - t.Errorf("unexpected message text. expected %v, actual %v", exp, got) - } -} - -func TestPoint_UnmarshalEpoch(t *testing.T) { - now := time.Now() - tests := []struct { - name string - epoch int64 - precision string - expected time.Time - }{ - { - name: "nanoseconds", - epoch: now.UnixNano(), - precision: "n", - expected: now, - }, - { - name: "microseconds", - epoch: now.Round(time.Microsecond).UnixNano() / int64(time.Microsecond), - precision: "u", - expected: now.Round(time.Microsecond), - }, - { - name: "milliseconds", - epoch: now.Round(time.Millisecond).UnixNano() / int64(time.Millisecond), - precision: "ms", - expected: now.Round(time.Millisecond), - }, - { - name: "seconds", - epoch: now.Round(time.Second).UnixNano() / int64(time.Second), - precision: "s", - expected: now.Round(time.Second), - }, - { - name: "minutes", - epoch: now.Round(time.Minute).UnixNano() / int64(time.Minute), - precision: "m", - expected: now.Round(time.Minute), - }, - { - name: "hours", - epoch: now.Round(time.Hour).UnixNano() / int64(time.Hour), - precision: "h", - expected: now.Round(time.Hour), - }, - { - name: "max int64", - epoch: 9223372036854775807, - precision: "n", - expected: time.Unix(0, 9223372036854775807), - }, - { - name: "100 years from now", - epoch: now.Add(time.Hour * 24 * 365 * 100).UnixNano(), - precision: "n", - expected: now.Add(time.Hour * 24 * 365 * 100), - }, - } - - for _, test := range tests { - t.Logf("testing %q\n", test.name) - data := []byte(fmt.Sprintf(`{"time": %d, "precision":"%s"}`, test.epoch, test.precision)) - t.Logf("json: %s", string(data)) - var p client.Point - err := json.Unmarshal(data, &p) - if err != nil { - t.Fatalf("unexpected error. exptected: %v, actual: %v", nil, err) - } - if !p.Time.Equal(test.expected) { - t.Fatalf("Unexpected time. expected: %v, actual: %v", test.expected, p.Time) - } - } -} - -func TestPoint_UnmarshalRFC(t *testing.T) { - now := time.Now().UTC() - tests := []struct { - name string - rfc string - now time.Time - expected time.Time - }{ - { - name: "RFC3339Nano", - rfc: time.RFC3339Nano, - now: now, - expected: now, - }, - { - name: "RFC3339", - rfc: time.RFC3339, - now: now.Round(time.Second), - expected: now.Round(time.Second), - }, - } - - for _, test := range tests { - t.Logf("testing %q\n", test.name) - ts := test.now.Format(test.rfc) - data := []byte(fmt.Sprintf(`{"time": %q}`, ts)) - t.Logf("json: %s", string(data)) - var p client.Point - err := json.Unmarshal(data, &p) - if err != nil { - t.Fatalf("unexpected error. exptected: %v, actual: %v", nil, err) - } - if !p.Time.Equal(test.expected) { - t.Fatalf("Unexpected time. expected: %v, actual: %v", test.expected, p.Time) - } - } -} - -func TestPoint_MarshalOmitempty(t *testing.T) { - now := time.Now().UTC() - tests := []struct { - name string - point client.Point - now time.Time - expected string - }{ - { - name: "all empty", - point: client.Point{Measurement: "cpu", Fields: map[string]interface{}{"value": 1.1}}, - now: now, - expected: `{"measurement":"cpu","fields":{"value":1.1}}`, - }, - { - name: "with time", - point: client.Point{Measurement: "cpu", Fields: map[string]interface{}{"value": 1.1}, Time: now}, - now: now, - expected: fmt.Sprintf(`{"measurement":"cpu","time":"%s","fields":{"value":1.1}}`, now.Format(time.RFC3339Nano)), - }, - { - name: "with tags", - point: client.Point{Measurement: "cpu", Fields: map[string]interface{}{"value": 1.1}, Tags: map[string]string{"foo": "bar"}}, - now: now, - expected: `{"measurement":"cpu","tags":{"foo":"bar"},"fields":{"value":1.1}}`, - }, - { - name: "with precision", - point: client.Point{Measurement: "cpu", Fields: map[string]interface{}{"value": 1.1}, Precision: "ms"}, - now: now, - expected: `{"measurement":"cpu","fields":{"value":1.1},"precision":"ms"}`, - }, - } - - for _, test := range tests { - t.Logf("testing %q\n", test.name) - b, err := json.Marshal(&test.point) - if err != nil { - t.Fatalf("unexpected error. exptected: %v, actual: %v", nil, err) - } - if test.expected != string(b) { - t.Fatalf("Unexpected result. expected: %v, actual: %v", test.expected, string(b)) - } - } -} - -func TestEpochToTime(t *testing.T) { - now := time.Now() - - tests := []struct { - name string - epoch int64 - precision string - expected time.Time - }{ - {name: "nanoseconds", epoch: now.UnixNano(), precision: "n", expected: now}, - {name: "microseconds", epoch: now.Round(time.Microsecond).UnixNano() / int64(time.Microsecond), precision: "u", expected: now.Round(time.Microsecond)}, - {name: "milliseconds", epoch: now.Round(time.Millisecond).UnixNano() / int64(time.Millisecond), precision: "ms", expected: now.Round(time.Millisecond)}, - {name: "seconds", epoch: now.Round(time.Second).UnixNano() / int64(time.Second), precision: "s", expected: now.Round(time.Second)}, - {name: "minutes", epoch: now.Round(time.Minute).UnixNano() / int64(time.Minute), precision: "m", expected: now.Round(time.Minute)}, - {name: "hours", epoch: now.Round(time.Hour).UnixNano() / int64(time.Hour), precision: "h", expected: now.Round(time.Hour)}, - } - - for _, test := range tests { - t.Logf("testing %q\n", test.name) - tm, e := client.EpochToTime(test.epoch, test.precision) - if e != nil { - t.Fatalf("unexpected error: expected %v, actual: %v", nil, e) - } - if tm != test.expected { - t.Fatalf("unexpected time: expected %v, actual %v", test.expected, tm) - } - } -} - -// helper functions - -func emptyTestServer() *httptest.Server { - return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - time.Sleep(50 * time.Millisecond) - w.Header().Set("X-Influxdb-Version", "x.x") - return - })) -} - -// Ensure that data with epoch times can be decoded. -func TestBatchPoints_Normal(t *testing.T) { - var bp client.BatchPoints - data := []byte(` -{ - "database": "foo", - "retentionPolicy": "bar", - "points": [ - { - "name": "cpu", - "tags": { - "host": "server01" - }, - "time": 14244733039069373, - "precision": "n", - "values": { - "value": 4541770385657154000 - } - }, - { - "name": "cpu", - "tags": { - "host": "server01" - }, - "time": 14244733039069380, - "precision": "n", - "values": { - "value": 7199311900554737000 - } - } - ] -} -`) - - if err := json.Unmarshal(data, &bp); err != nil { - t.Errorf("unable to unmarshal nanosecond data: %s", err.Error()) - } -} - -func TestClient_Timeout(t *testing.T) { - done := make(chan bool) - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - <-done - })) - defer ts.Close() - defer func() { done <- true }() - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u, Timeout: 500 * time.Millisecond} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - query := client.Query{} - _, err = c.Query(query) - if err == nil { - t.Fatalf("unexpected success. expected timeout error") - } else if !strings.Contains(err.Error(), "request canceled") && - !strings.Contains(err.Error(), "use of closed network connection") { - t.Fatalf("unexpected error. expected 'request canceled' error, got %v", err) - } -} - -func TestClient_NoTimeout(t *testing.T) { - if testing.Short() { - t.Skip("skipping in short mode") - } - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - time.Sleep(1 * time.Second) - var data client.Response - w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - query := client.Query{} - _, err = c.Query(query) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestClient_WriteUint64(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var data client.Response - w.WriteHeader(http.StatusNoContent) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - bp := client.BatchPoints{ - Points: []client.Point{ - { - Fields: map[string]interface{}{"value": uint64(10)}, - }, - }, - } - r, err := c.Write(bp) - if err == nil { - t.Fatalf("unexpected error. expected err, actual %v", err) - } - if r != nil { - t.Fatalf("unexpected response. expected %v, actual %v", nil, r) - } -} - -func TestClient_ParseConnectionString_IPv6(t *testing.T) { - path := "[fdf5:9ede:1875:0:a9ee:a600:8fe3:d495]:8086" - u, err := client.ParseConnectionString(path, false) - if err != nil { - t.Fatalf("unexpected error, expected %v, actual %v", nil, err) - } - if u.Host != path { - t.Fatalf("ipv6 parse failed, expected %s, actual %s", path, u.Host) - } -} - -func TestClient_CustomCertificates(t *testing.T) { - // generated with: - // openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3650 -nodes -config influx.cnf - // influx.cnf: - // [req] - // distinguished_name = req_distinguished_name - // x509_extensions = v3_req - // prompt = no - // [req_distinguished_name] - // C = US - // ST = CA - // L = San Francisco - // O = InfluxDB - // CN = github.com/influxdata - // [v3_req] - // keyUsage = keyEncipherment, dataEncipherment - // extendedKeyUsage = serverAuth - // subjectAltName = @alt_names - // [alt_names] - // IP.1 = 127.0.0.1 - // - key := ` ------BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDLswqKJLxfhBRi -4qdj7+jpBxTAi4MewrcMPp+9YlbLke3F7w2DPrZVkYVeWmg8LyTPAigrXeadK6hv -qjRr05a7sMc5+ynivGbWUySWT+u17V85x6VR5TMIkJEOqpiIU8aYk0l+3UcrzVjS -1QZCUBoxVwAVaSR6AXTA8YrVXdk/AI3f22dYiBjFmV4LJJkGjTaCnlDKu54hMU1t -WTyFcoY9TBzZ1XA+ng5RQ/QADeL2PYrTW4s/mLI3jfKKD53EI4uM2FjW37ZfuxTa -mhCR7/lxM4COg9K70y5uebfqJvuoXAwXLOzVbdfF5b9fJFbL67kaK2tiMT3Wt39m -hXzclLTDAgMBAAECggEAK8mpElkjRUUXPMqMQSdpYe5rv5g973bb8n3jyMpC7i/I -dSwWM4hfmbVWfhnhHk7kErvb9raQxGiGJLrp2eP6Gw69RPGA54SodpoY21cCzHDi -b4FDQH+MoOKyy/xQHb4kitfejK70ha320huI5OhjOQgCtJeNh8yYVIGX3pX2BVyu -36UB9tfX1S5pbiHeih3vZGd322Muj/joNzIelnYRBnoO0xqvQ0S1Dk+dLCTHO0/m -u9AZN8c2TsRWZpJPMWwBv8LuABbE0e66/TSsrfklAn86ELCo44lZURDE7uPZ4pIH -FWtmf+nW5Hy6aPhy60E40MqotlejhWwB3ktY/m3JAQKBgQDuB4nhxzJA9lH9EaCt -byvJ9wGVvI3k79hOwc/Z2R3dNe+Ma+TJy+aBppvsLF4qz83aWC+canyasbHcPNR/ -vXQGlsgKfucrmd1PfMV7uvOIkfOjK0E6mRC+jMuKtNTQrdtM1BU/Z7LY0iy0fNJ6 -aNqhFdlJmmk0g+4bR4SAWB6FkwKBgQDbE/7r1u+GdJk/mhdjTi1aegr9lXb0l7L6 -BCvOYhs/Z/pXfsaYPSXhgk2w+LiGk6BaEA2/4Sr0YS2MAAaIhBVeFBIXVpNrXB3K -Yg1jOEeLQ3qoVBeJFhJNrN9ZQx33HANC1W/Y1apMwaYqCRUGVQkrdcsN2KNea1z0 -3qeYeCCSEQKBgCKZKeuNfrp+k1BLnaVYAW9r3ekb7SwXyMM53LJ3oqWiz10D2c+T -OcAirYtYr59dcTiJlPIRcGcz6PxwQxsGOLU0eYM9CvEFfmutYS8o73ksbdOL2AFi -elKYOIXC3yQuATBbq3L56b8mXaUmd5mfYBgGCv1t2ljtzFBext248UbNAoGBAIv1 -2V24YiwnH6THf/ucfVMZNx5Mt8OJivk5YvcmLDw05HWzc5LdNe89PP871z963u3K -5c3ZP4UC9INFnOboY3JIJkqsr9/d6NZcECt8UBDDmoAhwSt+Y1EmiUZQn7s4NUkk -bKE919/Ts6GVTc5O013lkkUVS0HOG4QBH1dEH6LRAoGAStl11WA9tuKXiBl5XG/C -cq9mFPNJK3pEgd6YH874vEnYEEqENR4MFK3uWXus9Nm+VYxbUbPEzFF4kpsfukDg -/JAVqY4lUam7g6fyyaoIIPQEp7jGjbsUf46IjnUjFcaojOugA3EAfn9awREUDuJZ -cvh4WzEegcExTppINW1NB5E= ------END PRIVATE KEY----- -` - cert := ` ------BEGIN CERTIFICATE----- -MIIDdjCCAl6gAwIBAgIJAMYGAwkxUV51MA0GCSqGSIb3DQEBCwUAMFgxCzAJBgNV -BAYTAlVTMQswCQYDVQQIDAJDQTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNjbzERMA8G -A1UECgwISW5mbHV4REIxETAPBgNVBAMMCGluZmx1eGRiMB4XDTE1MTIyOTAxNTg1 -NloXDTI1MTIyNjAxNTg1NlowWDELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRYw -FAYDVQQHDA1TYW4gRnJhbmNpc2NvMREwDwYDVQQKDAhJbmZsdXhEQjERMA8GA1UE -AwwIaW5mbHV4ZGIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDLswqK -JLxfhBRi4qdj7+jpBxTAi4MewrcMPp+9YlbLke3F7w2DPrZVkYVeWmg8LyTPAigr -XeadK6hvqjRr05a7sMc5+ynivGbWUySWT+u17V85x6VR5TMIkJEOqpiIU8aYk0l+ -3UcrzVjS1QZCUBoxVwAVaSR6AXTA8YrVXdk/AI3f22dYiBjFmV4LJJkGjTaCnlDK -u54hMU1tWTyFcoY9TBzZ1XA+ng5RQ/QADeL2PYrTW4s/mLI3jfKKD53EI4uM2FjW -37ZfuxTamhCR7/lxM4COg9K70y5uebfqJvuoXAwXLOzVbdfF5b9fJFbL67kaK2ti -MT3Wt39mhXzclLTDAgMBAAGjQzBBMAwGA1UdEwQFMAMBAf8wCwYDVR0PBAQDAgQw -MBMGA1UdJQQMMAoGCCsGAQUFBwMBMA8GA1UdEQQIMAaHBH8AAAEwDQYJKoZIhvcN -AQELBQADggEBAJxgHeduV9q2BuKnrt+sjXLGn/HwbMbgGbgFK6kUKJBWtv6Pa7JJ -m4teDmTMWiaeB2g4N2bmaWTuEZzzShNKG5roFeWm1ilFMAyzkb+VifN4YuDKH62F -3e259qsytiGbbJF3F//4sjfMw8qZVEPvspG1zKsASo0PpSOOUFmxcj0oMAXhnMrk -rRcbk6fufhyq0iZGl8ZLKTCrkjk0b3qlNs6UaRD9/XBB59VlQ8I338sfjV06edwY -jn5Amab0uyoFNEp70Y4WGxrxUTS1GAC1LCA13S7EnidD440UrnWALTarjmHAK6aW -war3JNM1mGB3o2iAtuOJlFIKLpI1x+1e8pI= ------END CERTIFICATE----- -` - cer, err := tls.X509KeyPair([]byte(cert), []byte(key)) - - if err != nil { - t.Fatalf("Received error: %v", err) - } - - server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var data client.Response - w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(data) - })) - server.TLS = &tls.Config{Certificates: []tls.Certificate{cer}} - server.TLS.BuildNameToCertificate() - server.StartTLS() - defer server.Close() - - certFile, _ := ioutil.TempFile("", "influx-cert-") - certFile.WriteString(cert) - certFile.Close() - defer os.Remove(certFile.Name()) - - u, _ := url.Parse(server.URL) - - tests := []struct { - name string - unsafeSsl bool - expected error - }{ - {name: "validate certificates", unsafeSsl: false, expected: errors.New("error")}, - {name: "not validate certificates", unsafeSsl: true, expected: nil}, - } - - for _, test := range tests { - config := client.Config{URL: *u, UnsafeSsl: test.unsafeSsl} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - query := client.Query{} - _, err = c.Query(query) - - if (test.expected == nil) != (err == nil) { - t.Fatalf("%s: expected %v. got %v. unsafeSsl: %v", test.name, test.expected, err, test.unsafeSsl) - } - } -} - -func TestChunkedResponse(t *testing.T) { - s := `{"results":[{},{}]}{"results":[{}]}` - r := client.NewChunkedResponse(strings.NewReader(s)) - resp, err := r.NextResponse() - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } else if actual := len(resp.Results); actual != 2 { - t.Fatalf("unexpected number of results. expected %v, actual %v", 2, actual) - } - - resp, err = r.NextResponse() - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } else if actual := len(resp.Results); actual != 1 { - t.Fatalf("unexpected number of results. expected %v, actual %v", 1, actual) - } - - resp, err = r.NextResponse() - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } else if resp != nil { - t.Fatalf("unexpected response. expected %v, actual %v", nil, resp) - } -} diff --git a/vendor/github.com/influxdata/influxdb/client/v2/client.go b/vendor/github.com/influxdata/influxdb/client/v2/client.go deleted file mode 100644 index 01c7c465..00000000 --- a/vendor/github.com/influxdata/influxdb/client/v2/client.go +++ /dev/null @@ -1,502 +0,0 @@ -package client // import "github.com/influxdata/influxdb1-client/v2" - -import ( - "bytes" - "crypto/tls" - "encoding/json" - "errors" - "fmt" - "io/ioutil" - "net/http" - "net/url" - "path" - "time" - - "github.com/influxdata/influxdb/models" -) - -// HTTPConfig is the config data needed to create an HTTP Client -type HTTPConfig struct { - // Addr should be of the form "http://host:port" - // or "http://[ipv6-host%zone]:port". - Addr string - - // Username is the influxdb username, optional - Username string - - // Password is the influxdb password, optional - Password string - - // UserAgent is the http User Agent, defaults to "InfluxDBClient" - UserAgent string - - // Timeout for influxdb writes, defaults to no timeout - Timeout time.Duration - - // InsecureSkipVerify gets passed to the http client, if true, it will - // skip https certificate verification. Defaults to false - InsecureSkipVerify bool - - // TLSConfig allows the user to set their own TLS config for the HTTP - // Client. If set, this option overrides InsecureSkipVerify. - TLSConfig *tls.Config -} - -// BatchPointsConfig is the config data needed to create an instance of the BatchPoints struct -type BatchPointsConfig struct { - // Precision is the write precision of the points, defaults to "ns" - Precision string - - // Database is the database to write points to - Database string - - // RetentionPolicy is the retention policy of the points - RetentionPolicy string - - // Write consistency is the number of servers required to confirm write - WriteConsistency string -} - -// Client is a client interface for writing & querying the database -type Client interface { - // Ping checks that status of cluster, and will always return 0 time and no - // error for UDP clients - Ping(timeout time.Duration) (time.Duration, string, error) - - // Write takes a BatchPoints object and writes all Points to InfluxDB. - Write(bp BatchPoints) error - - // Query makes an InfluxDB Query on the database. This will fail if using - // the UDP client. - Query(q Query) (*Response, error) - - // Close releases any resources a Client may be using. - Close() error -} - -// NewHTTPClient returns a new Client from the provided config. -// Client is safe for concurrent use by multiple goroutines. -func NewHTTPClient(conf HTTPConfig) (Client, error) { - if conf.UserAgent == "" { - conf.UserAgent = "InfluxDBClient" - } - - u, err := url.Parse(conf.Addr) - if err != nil { - return nil, err - } else if u.Scheme != "http" && u.Scheme != "https" { - m := fmt.Sprintf("Unsupported protocol scheme: %s, your address"+ - " must start with http:// or https://", u.Scheme) - return nil, errors.New(m) - } - - tr := &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: conf.InsecureSkipVerify, - }, - } - if conf.TLSConfig != nil { - tr.TLSClientConfig = conf.TLSConfig - } - return &client{ - url: *u, - username: conf.Username, - password: conf.Password, - useragent: conf.UserAgent, - httpClient: &http.Client{ - Timeout: conf.Timeout, - Transport: tr, - }, - transport: tr, - }, nil -} - -// Ping will check to see if the server is up with an optional timeout on waiting for leader. -// Ping returns how long the request took, the version of the server it connected to, and an error if one occurred. -func (c *client) Ping(timeout time.Duration) (time.Duration, string, error) { - now := time.Now() - u := c.url - u.Path = "ping" - - req, err := http.NewRequest("GET", u.String(), nil) - if err != nil { - return 0, "", err - } - - req.Header.Set("User-Agent", c.useragent) - - if c.username != "" { - req.SetBasicAuth(c.username, c.password) - } - - if timeout > 0 { - params := req.URL.Query() - params.Set("wait_for_leader", fmt.Sprintf("%.0fs", timeout.Seconds())) - req.URL.RawQuery = params.Encode() - } - - resp, err := c.httpClient.Do(req) - if err != nil { - return 0, "", err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return 0, "", err - } - - if resp.StatusCode != http.StatusNoContent { - var err = fmt.Errorf(string(body)) - return 0, "", err - } - - version := resp.Header.Get("X-Influxdb-Version") - return time.Since(now), version, nil -} - -// Close releases the client's resources. -func (c *client) Close() error { - c.transport.CloseIdleConnections() - return nil -} - -// client is safe for concurrent use as the fields are all read-only -// once the client is instantiated. -type client struct { - // N.B - if url.UserInfo is accessed in future modifications to the - // methods on client, you will need to syncronise access to url. - url url.URL - username string - password string - useragent string - httpClient *http.Client - transport *http.Transport -} - -// BatchPoints is an interface into a batched grouping of points to write into -// InfluxDB together. BatchPoints is NOT thread-safe, you must create a separate -// batch for each goroutine. -type BatchPoints interface { - // AddPoint adds the given point to the Batch of points - AddPoint(p *Point) - // AddPoints adds the given points to the Batch of points - AddPoints(ps []*Point) - // Points lists the points in the Batch - Points() []*Point - - // Precision returns the currently set precision of this Batch - Precision() string - // SetPrecision sets the precision of this batch. - SetPrecision(s string) error - - // Database returns the currently set database of this Batch - Database() string - // SetDatabase sets the database of this Batch - SetDatabase(s string) - - // WriteConsistency returns the currently set write consistency of this Batch - WriteConsistency() string - // SetWriteConsistency sets the write consistency of this Batch - SetWriteConsistency(s string) - - // RetentionPolicy returns the currently set retention policy of this Batch - RetentionPolicy() string - // SetRetentionPolicy sets the retention policy of this Batch - SetRetentionPolicy(s string) -} - -// NewBatchPoints returns a BatchPoints interface based on the given config. -func NewBatchPoints(conf BatchPointsConfig) (BatchPoints, error) { - if conf.Precision == "" { - conf.Precision = "ns" - } - if _, err := time.ParseDuration("1" + conf.Precision); err != nil { - return nil, err - } - bp := &batchpoints{ - database: conf.Database, - precision: conf.Precision, - retentionPolicy: conf.RetentionPolicy, - writeConsistency: conf.WriteConsistency, - } - return bp, nil -} - -type batchpoints struct { - points []*Point - database string - precision string - retentionPolicy string - writeConsistency string -} - -func (bp *batchpoints) AddPoint(p *Point) { - bp.points = append(bp.points, p) -} - -func (bp *batchpoints) AddPoints(ps []*Point) { - bp.points = append(bp.points, ps...) -} - -func (bp *batchpoints) Points() []*Point { - return bp.points -} - -func (bp *batchpoints) Precision() string { - return bp.precision -} - -func (bp *batchpoints) Database() string { - return bp.database -} - -func (bp *batchpoints) WriteConsistency() string { - return bp.writeConsistency -} - -func (bp *batchpoints) RetentionPolicy() string { - return bp.retentionPolicy -} - -func (bp *batchpoints) SetPrecision(p string) error { - if _, err := time.ParseDuration("1" + p); err != nil { - return err - } - bp.precision = p - return nil -} - -func (bp *batchpoints) SetDatabase(db string) { - bp.database = db -} - -func (bp *batchpoints) SetWriteConsistency(wc string) { - bp.writeConsistency = wc -} - -func (bp *batchpoints) SetRetentionPolicy(rp string) { - bp.retentionPolicy = rp -} - -// Point represents a single data point -type Point struct { - pt models.Point -} - -// NewPoint returns a point with the given timestamp. If a timestamp is not -// given, then data is sent to the database without a timestamp, in which case -// the server will assign local time upon reception. NOTE: it is recommended to -// send data with a timestamp. -func NewPoint( - name string, - tags map[string]string, - fields map[string]interface{}, - t ...time.Time, -) (*Point, error) { - var T time.Time - if len(t) > 0 { - T = t[0] - } - - pt, err := models.NewPoint(name, models.NewTags(tags), fields, T) - if err != nil { - return nil, err - } - return &Point{ - pt: pt, - }, nil -} - -// String returns a line-protocol string of the Point -func (p *Point) String() string { - return p.pt.String() -} - -// PrecisionString returns a line-protocol string of the Point, at precision -func (p *Point) PrecisionString(precison string) string { - return p.pt.PrecisionString(precison) -} - -// Name returns the measurement name of the point -func (p *Point) Name() string { - return p.pt.Name() -} - -// Tags returns the tags associated with the point -func (p *Point) Tags() map[string]string { - return p.pt.Tags().Map() -} - -// Time return the timestamp for the point -func (p *Point) Time() time.Time { - return p.pt.Time() -} - -// UnixNano returns the unix nano time of the point -func (p *Point) UnixNano() int64 { - return p.pt.UnixNano() -} - -// Fields returns the fields for the point -func (p *Point) Fields() map[string]interface{} { - return p.pt.Fields() -} - -// NewPointFrom returns a point from the provided models.Point. -func NewPointFrom(pt models.Point) *Point { - return &Point{pt: pt} -} - -func (c *client) Write(bp BatchPoints) error { - var b bytes.Buffer - - for _, p := range bp.Points() { - if _, err := b.WriteString(p.pt.PrecisionString(bp.Precision())); err != nil { - return err - } - - if err := b.WriteByte('\n'); err != nil { - return err - } - } - - u := c.url - u.Path = path.Join(u.Path, "write") - req, err := http.NewRequest("POST", u.String(), &b) - if err != nil { - return err - } - req.Header.Set("Content-Type", "") - req.Header.Set("User-Agent", c.useragent) - if c.username != "" { - req.SetBasicAuth(c.username, c.password) - } - - params := req.URL.Query() - params.Set("db", bp.Database()) - params.Set("rp", bp.RetentionPolicy()) - params.Set("precision", bp.Precision()) - params.Set("consistency", bp.WriteConsistency()) - req.URL.RawQuery = params.Encode() - - resp, err := c.httpClient.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK { - var err = fmt.Errorf(string(body)) - return err - } - - return nil -} - -// Query defines a query to send to the server -type Query struct { - Command string - Database string - Precision string -} - -// NewQuery returns a query object -// database and precision strings can be empty strings if they are not needed -// for the query. -func NewQuery(command, database, precision string) Query { - return Query{ - Command: command, - Database: database, - Precision: precision, - } -} - -// Response represents a list of statement results. -type Response struct { - Results []Result - Err string `json:"error,omitempty"` -} - -// Error returns the first error from any statement. -// Returns nil if no errors occurred on any statements. -func (r *Response) Error() error { - if r.Err != "" { - return fmt.Errorf(r.Err) - } - for _, result := range r.Results { - if result.Err != "" { - return fmt.Errorf(result.Err) - } - } - return nil -} - -// Message represents a user message. -type Message struct { - Level string - Text string -} - -// Result represents a resultset returned from a single statement. -type Result struct { - Series []models.Row - Messages []*Message - Err string `json:"error,omitempty"` -} - -// Query sends a command to the server and returns the Response -func (c *client) Query(q Query) (*Response, error) { - u := c.url - u.Path = "query" - - req, err := http.NewRequest("POST", u.String(), nil) - if err != nil { - return nil, err - } - req.Header.Set("Content-Type", "") - req.Header.Set("User-Agent", c.useragent) - if c.username != "" { - req.SetBasicAuth(c.username, c.password) - } - - params := req.URL.Query() - params.Set("q", q.Command) - params.Set("db", q.Database) - if q.Precision != "" { - params.Set("epoch", q.Precision) - } - req.URL.RawQuery = params.Encode() - - resp, err := c.httpClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - var response Response - dec := json.NewDecoder(resp.Body) - dec.UseNumber() - decErr := dec.Decode(&response) - - // ignore this error if we got an invalid status code - if decErr != nil && decErr.Error() == "EOF" && resp.StatusCode != http.StatusOK { - decErr = nil - } - // If we got a valid decode error, send that back - if decErr != nil { - return nil, fmt.Errorf("unable to decode json: received status code %d err: %s", resp.StatusCode, decErr) - } - // If we don't have an error in our json response, and didn't get statusOK - // then send back an error - if resp.StatusCode != http.StatusOK && response.Error() == nil { - return &response, fmt.Errorf("received status code %d from server", - resp.StatusCode) - } - return &response, nil -} diff --git a/vendor/github.com/influxdata/influxdb/client/v2/client_test.go b/vendor/github.com/influxdata/influxdb/client/v2/client_test.go deleted file mode 100644 index 30ee09df..00000000 --- a/vendor/github.com/influxdata/influxdb/client/v2/client_test.go +++ /dev/null @@ -1,480 +0,0 @@ -package client - -import ( - "encoding/json" - "net/http" - "net/http/httptest" - "reflect" - "strings" - "sync" - "testing" - "time" -) - -func TestUDPClient_Query(t *testing.T) { - config := UDPConfig{Addr: "localhost:8089"} - c, err := NewUDPClient(config) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } - defer c.Close() - query := Query{} - _, err = c.Query(query) - if err == nil { - t.Error("Querying UDP client should fail") - } -} - -func TestUDPClient_Ping(t *testing.T) { - config := UDPConfig{Addr: "localhost:8089"} - c, err := NewUDPClient(config) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } - defer c.Close() - - rtt, version, err := c.Ping(0) - if rtt != 0 || version != "" || err != nil { - t.Errorf("unexpected error. expected (%v, '%v', %v), actual (%v, '%v', %v)", 0, "", nil, rtt, version, err) - } -} - -func TestUDPClient_Write(t *testing.T) { - config := UDPConfig{Addr: "localhost:8089"} - c, err := NewUDPClient(config) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } - defer c.Close() - - bp, err := NewBatchPoints(BatchPointsConfig{}) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } - - fields := make(map[string]interface{}) - fields["value"] = 1.0 - pt, _ := NewPoint("cpu", make(map[string]string), fields) - bp.AddPoint(pt) - - err = c.Write(bp) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestUDPClient_BadAddr(t *testing.T) { - config := UDPConfig{Addr: "foobar@wahoo"} - c, err := NewUDPClient(config) - if err == nil { - defer c.Close() - t.Error("Expected resolve error") - } -} - -func TestUDPClient_Batches(t *testing.T) { - var logger writeLogger - var cl udpclient - - cl.conn = &logger - cl.payloadSize = 20 // should allow for two points per batch - - // expected point should look like this: "cpu a=1i" - fields := map[string]interface{}{"a": 1} - - p, _ := NewPoint("cpu", nil, fields, time.Time{}) - - bp, _ := NewBatchPoints(BatchPointsConfig{}) - - for i := 0; i < 9; i++ { - bp.AddPoint(p) - } - - if err := cl.Write(bp); err != nil { - t.Fatalf("Unexpected error during Write: %v", err) - } - - if len(logger.writes) != 5 { - t.Errorf("Mismatched write count: got %v, exp %v", len(logger.writes), 5) - } -} - -func TestUDPClient_Split(t *testing.T) { - var logger writeLogger - var cl udpclient - - cl.conn = &logger - cl.payloadSize = 1 // force one field per point - - fields := map[string]interface{}{"a": 1, "b": 2, "c": 3, "d": 4} - - p, _ := NewPoint("cpu", nil, fields, time.Unix(1, 0)) - - bp, _ := NewBatchPoints(BatchPointsConfig{}) - - bp.AddPoint(p) - - if err := cl.Write(bp); err != nil { - t.Fatalf("Unexpected error during Write: %v", err) - } - - if len(logger.writes) != len(fields) { - t.Errorf("Mismatched write count: got %v, exp %v", len(logger.writes), len(fields)) - } -} - -type writeLogger struct { - writes [][]byte -} - -func (w *writeLogger) Write(b []byte) (int, error) { - w.writes = append(w.writes, append([]byte(nil), b...)) - return len(b), nil -} - -func (w *writeLogger) Close() error { return nil } - -func TestClient_Query(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var data Response - w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - config := HTTPConfig{Addr: ts.URL} - c, _ := NewHTTPClient(config) - defer c.Close() - - query := Query{} - _, err := c.Query(query) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestClient_BasicAuth(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - u, p, ok := r.BasicAuth() - - if !ok { - t.Errorf("basic auth error") - } - if u != "username" { - t.Errorf("unexpected username, expected %q, actual %q", "username", u) - } - if p != "password" { - t.Errorf("unexpected password, expected %q, actual %q", "password", p) - } - var data Response - w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - config := HTTPConfig{Addr: ts.URL, Username: "username", Password: "password"} - c, _ := NewHTTPClient(config) - defer c.Close() - - query := Query{} - _, err := c.Query(query) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestClient_Ping(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var data Response - w.WriteHeader(http.StatusNoContent) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - config := HTTPConfig{Addr: ts.URL} - c, _ := NewHTTPClient(config) - defer c.Close() - - _, _, err := c.Ping(0) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestClient_Concurrent_Use(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - w.Write([]byte(`{}`)) - })) - defer ts.Close() - - config := HTTPConfig{Addr: ts.URL} - c, _ := NewHTTPClient(config) - defer c.Close() - - var wg sync.WaitGroup - wg.Add(3) - n := 1000 - - go func() { - defer wg.Done() - bp, err := NewBatchPoints(BatchPointsConfig{}) - if err != nil { - t.Errorf("got error %v", err) - } - - for i := 0; i < n; i++ { - if err = c.Write(bp); err != nil { - t.Fatalf("got error %v", err) - } - } - }() - - go func() { - defer wg.Done() - var q Query - for i := 0; i < n; i++ { - if _, err := c.Query(q); err != nil { - t.Fatalf("got error %v", err) - } - } - }() - - go func() { - defer wg.Done() - for i := 0; i < n; i++ { - c.Ping(time.Second) - } - }() - wg.Wait() -} - -func TestClient_Write(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var data Response - w.WriteHeader(http.StatusNoContent) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - config := HTTPConfig{Addr: ts.URL} - c, _ := NewHTTPClient(config) - defer c.Close() - - bp, err := NewBatchPoints(BatchPointsConfig{}) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } - err = c.Write(bp) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } -} - -func TestClient_UserAgent(t *testing.T) { - receivedUserAgent := "" - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - receivedUserAgent = r.UserAgent() - - var data Response - w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(data) - })) - defer ts.Close() - - _, err := http.Get(ts.URL) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } - - tests := []struct { - name string - userAgent string - expected string - }{ - { - name: "Empty user agent", - userAgent: "", - expected: "InfluxDBClient", - }, - { - name: "Custom user agent", - userAgent: "Test Influx Client", - expected: "Test Influx Client", - }, - } - - for _, test := range tests { - - config := HTTPConfig{Addr: ts.URL, UserAgent: test.userAgent} - c, _ := NewHTTPClient(config) - defer c.Close() - - receivedUserAgent = "" - query := Query{} - _, err = c.Query(query) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } - if !strings.HasPrefix(receivedUserAgent, test.expected) { - t.Errorf("Unexpected user agent. expected %v, actual %v", test.expected, receivedUserAgent) - } - - receivedUserAgent = "" - bp, _ := NewBatchPoints(BatchPointsConfig{}) - err = c.Write(bp) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } - if !strings.HasPrefix(receivedUserAgent, test.expected) { - t.Errorf("Unexpected user agent. expected %v, actual %v", test.expected, receivedUserAgent) - } - - receivedUserAgent = "" - _, err := c.Query(query) - if err != nil { - t.Errorf("unexpected error. expected %v, actual %v", nil, err) - } - if receivedUserAgent != test.expected { - t.Errorf("Unexpected user agent. expected %v, actual %v", test.expected, receivedUserAgent) - } - } -} - -func TestClient_PointString(t *testing.T) { - const shortForm = "2006-Jan-02" - time1, _ := time.Parse(shortForm, "2013-Feb-03") - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{"idle": 10.1, "system": 50.9, "user": 39.0} - p, _ := NewPoint("cpu_usage", tags, fields, time1) - - s := "cpu_usage,cpu=cpu-total idle=10.1,system=50.9,user=39 1359849600000000000" - if p.String() != s { - t.Errorf("Point String Error, got %s, expected %s", p.String(), s) - } - - s = "cpu_usage,cpu=cpu-total idle=10.1,system=50.9,user=39 1359849600000" - if p.PrecisionString("ms") != s { - t.Errorf("Point String Error, got %s, expected %s", - p.PrecisionString("ms"), s) - } -} - -func TestClient_PointWithoutTimeString(t *testing.T) { - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{"idle": 10.1, "system": 50.9, "user": 39.0} - p, _ := NewPoint("cpu_usage", tags, fields) - - s := "cpu_usage,cpu=cpu-total idle=10.1,system=50.9,user=39" - if p.String() != s { - t.Errorf("Point String Error, got %s, expected %s", p.String(), s) - } - - if p.PrecisionString("ms") != s { - t.Errorf("Point String Error, got %s, expected %s", - p.PrecisionString("ms"), s) - } -} - -func TestClient_PointName(t *testing.T) { - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{"idle": 10.1, "system": 50.9, "user": 39.0} - p, _ := NewPoint("cpu_usage", tags, fields) - - exp := "cpu_usage" - if p.Name() != exp { - t.Errorf("Error, got %s, expected %s", - p.Name(), exp) - } -} - -func TestClient_PointTags(t *testing.T) { - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{"idle": 10.1, "system": 50.9, "user": 39.0} - p, _ := NewPoint("cpu_usage", tags, fields) - - if !reflect.DeepEqual(tags, p.Tags()) { - t.Errorf("Error, got %v, expected %v", - p.Tags(), tags) - } -} - -func TestClient_PointUnixNano(t *testing.T) { - const shortForm = "2006-Jan-02" - time1, _ := time.Parse(shortForm, "2013-Feb-03") - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{"idle": 10.1, "system": 50.9, "user": 39.0} - p, _ := NewPoint("cpu_usage", tags, fields, time1) - - exp := int64(1359849600000000000) - if p.UnixNano() != exp { - t.Errorf("Error, got %d, expected %d", - p.UnixNano(), exp) - } -} - -func TestClient_PointFields(t *testing.T) { - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{"idle": 10.1, "system": 50.9, "user": 39.0} - p, _ := NewPoint("cpu_usage", tags, fields) - - if !reflect.DeepEqual(fields, p.Fields()) { - t.Errorf("Error, got %v, expected %v", - p.Fields(), fields) - } -} - -func TestBatchPoints_PrecisionError(t *testing.T) { - _, err := NewBatchPoints(BatchPointsConfig{Precision: "foobar"}) - if err == nil { - t.Errorf("Precision: foobar should have errored") - } - - bp, _ := NewBatchPoints(BatchPointsConfig{Precision: "ns"}) - err = bp.SetPrecision("foobar") - if err == nil { - t.Errorf("Precision: foobar should have errored") - } -} - -func TestBatchPoints_SettersGetters(t *testing.T) { - bp, _ := NewBatchPoints(BatchPointsConfig{ - Precision: "ns", - Database: "db", - RetentionPolicy: "rp", - WriteConsistency: "wc", - }) - if bp.Precision() != "ns" { - t.Errorf("Expected: %s, got %s", bp.Precision(), "ns") - } - if bp.Database() != "db" { - t.Errorf("Expected: %s, got %s", bp.Database(), "db") - } - if bp.RetentionPolicy() != "rp" { - t.Errorf("Expected: %s, got %s", bp.RetentionPolicy(), "rp") - } - if bp.WriteConsistency() != "wc" { - t.Errorf("Expected: %s, got %s", bp.WriteConsistency(), "wc") - } - - bp.SetDatabase("db2") - bp.SetRetentionPolicy("rp2") - bp.SetWriteConsistency("wc2") - err := bp.SetPrecision("s") - if err != nil { - t.Errorf("Did not expect error: %s", err.Error()) - } - - if bp.Precision() != "s" { - t.Errorf("Expected: %s, got %s", bp.Precision(), "s") - } - if bp.Database() != "db2" { - t.Errorf("Expected: %s, got %s", bp.Database(), "db2") - } - if bp.RetentionPolicy() != "rp2" { - t.Errorf("Expected: %s, got %s", bp.RetentionPolicy(), "rp2") - } - if bp.WriteConsistency() != "wc2" { - t.Errorf("Expected: %s, got %s", bp.WriteConsistency(), "wc2") - } -} diff --git a/vendor/github.com/influxdata/influxdb/client/v2/example_test.go b/vendor/github.com/influxdata/influxdb/client/v2/example_test.go deleted file mode 100644 index 21288aa4..00000000 --- a/vendor/github.com/influxdata/influxdb/client/v2/example_test.go +++ /dev/null @@ -1,265 +0,0 @@ -package client_test - -import ( - "fmt" - "math/rand" - "os" - "time" - - "github.com/influxdata/influxdb1-client/v2" -) - -// Create a new client -func ExampleClient() { - // NOTE: this assumes you've setup a user and have setup shell env variables, - // namely INFLUX_USER/INFLUX_PWD. If not just omit Username/Password below. - _, err := client.NewHTTPClient(client.HTTPConfig{ - Addr: "http://localhost:8086", - Username: os.Getenv("INFLUX_USER"), - Password: os.Getenv("INFLUX_PWD"), - }) - if err != nil { - fmt.Println("Error creating InfluxDB Client: ", err.Error()) - } -} - -// Write a point using the UDP client -func ExampleClient_uDP() { - // Make client - config := client.UDPConfig{Addr: "localhost:8089"} - c, err := client.NewUDPClient(config) - if err != nil { - fmt.Println("Error: ", err.Error()) - } - defer c.Close() - - // Create a new point batch - bp, _ := client.NewBatchPoints(client.BatchPointsConfig{ - Precision: "s", - }) - - // Create a point and add to batch - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{ - "idle": 10.1, - "system": 53.3, - "user": 46.6, - } - pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now()) - if err != nil { - fmt.Println("Error: ", err.Error()) - } - bp.AddPoint(pt) - - // Write the batch - c.Write(bp) -} - -// Ping the cluster using the HTTP client -func ExampleClient_Ping() { - // Make client - c, err := client.NewHTTPClient(client.HTTPConfig{ - Addr: "http://localhost:8086", - }) - if err != nil { - fmt.Println("Error creating InfluxDB Client: ", err.Error()) - } - defer c.Close() - - _, _, err = c.Ping(0) - if err != nil { - fmt.Println("Error pinging InfluxDB Cluster: ", err.Error()) - } -} - -// Write a point using the HTTP client -func ExampleClient_write() { - // Make client - c, err := client.NewHTTPClient(client.HTTPConfig{ - Addr: "http://localhost:8086", - }) - if err != nil { - fmt.Println("Error creating InfluxDB Client: ", err.Error()) - } - defer c.Close() - - // Create a new point batch - bp, _ := client.NewBatchPoints(client.BatchPointsConfig{ - Database: "BumbleBeeTuna", - Precision: "s", - }) - - // Create a point and add to batch - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{ - "idle": 10.1, - "system": 53.3, - "user": 46.6, - } - pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now()) - if err != nil { - fmt.Println("Error: ", err.Error()) - } - bp.AddPoint(pt) - - // Write the batch - c.Write(bp) -} - -// Create a batch and add a point -func ExampleBatchPoints() { - // Create a new point batch - bp, _ := client.NewBatchPoints(client.BatchPointsConfig{ - Database: "BumbleBeeTuna", - Precision: "s", - }) - - // Create a point and add to batch - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{ - "idle": 10.1, - "system": 53.3, - "user": 46.6, - } - pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now()) - if err != nil { - fmt.Println("Error: ", err.Error()) - } - bp.AddPoint(pt) -} - -// Using the BatchPoints setter functions -func ExampleBatchPoints_setters() { - // Create a new point batch - bp, _ := client.NewBatchPoints(client.BatchPointsConfig{}) - bp.SetDatabase("BumbleBeeTuna") - bp.SetPrecision("ms") - - // Create a point and add to batch - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{ - "idle": 10.1, - "system": 53.3, - "user": 46.6, - } - pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now()) - if err != nil { - fmt.Println("Error: ", err.Error()) - } - bp.AddPoint(pt) -} - -// Create a new point with a timestamp -func ExamplePoint() { - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{ - "idle": 10.1, - "system": 53.3, - "user": 46.6, - } - pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now()) - if err == nil { - fmt.Println("We created a point: ", pt.String()) - } -} - -// Create a new point without a timestamp -func ExamplePoint_withoutTime() { - tags := map[string]string{"cpu": "cpu-total"} - fields := map[string]interface{}{ - "idle": 10.1, - "system": 53.3, - "user": 46.6, - } - pt, err := client.NewPoint("cpu_usage", tags, fields) - if err == nil { - fmt.Println("We created a point w/o time: ", pt.String()) - } -} - -// Write 1000 points -func ExampleClient_write1000() { - sampleSize := 1000 - - // Make client - c, err := client.NewHTTPClient(client.HTTPConfig{ - Addr: "http://localhost:8086", - }) - if err != nil { - fmt.Println("Error creating InfluxDB Client: ", err.Error()) - } - defer c.Close() - - rand.Seed(42) - - bp, _ := client.NewBatchPoints(client.BatchPointsConfig{ - Database: "systemstats", - Precision: "us", - }) - - for i := 0; i < sampleSize; i++ { - regions := []string{"us-west1", "us-west2", "us-west3", "us-east1"} - tags := map[string]string{ - "cpu": "cpu-total", - "host": fmt.Sprintf("host%d", rand.Intn(1000)), - "region": regions[rand.Intn(len(regions))], - } - - idle := rand.Float64() * 100.0 - fields := map[string]interface{}{ - "idle": idle, - "busy": 100.0 - idle, - } - - pt, err := client.NewPoint( - "cpu_usage", - tags, - fields, - time.Now(), - ) - if err != nil { - println("Error:", err.Error()) - continue - } - bp.AddPoint(pt) - } - - err = c.Write(bp) - if err != nil { - fmt.Println("Error: ", err.Error()) - } -} - -// Make a Query -func ExampleClient_query() { - // Make client - c, err := client.NewHTTPClient(client.HTTPConfig{ - Addr: "http://localhost:8086", - }) - if err != nil { - fmt.Println("Error creating InfluxDB Client: ", err.Error()) - } - defer c.Close() - - q := client.NewQuery("SELECT count(value) FROM shapes", "square_holes", "ns") - if response, err := c.Query(q); err == nil && response.Error() == nil { - fmt.Println(response.Results) - } -} - -// Create a Database with a query -func ExampleClient_createDatabase() { - // Make client - c, err := client.NewHTTPClient(client.HTTPConfig{ - Addr: "http://localhost:8086", - }) - if err != nil { - fmt.Println("Error creating InfluxDB Client: ", err.Error()) - } - defer c.Close() - - q := client.NewQuery("CREATE DATABASE telegraf", "", "") - if response, err := c.Query(q); err == nil && response.Error() == nil { - fmt.Println(response.Results) - } -} diff --git a/vendor/github.com/influxdata/influxdb/client/v2/udp.go b/vendor/github.com/influxdata/influxdb/client/v2/udp.go deleted file mode 100644 index aff07d0f..00000000 --- a/vendor/github.com/influxdata/influxdb/client/v2/udp.go +++ /dev/null @@ -1,112 +0,0 @@ -package client - -import ( - "fmt" - "io" - "net" - "time" -) - -const ( - // UDPPayloadSize is a reasonable default payload size for UDP packets that - // could be travelling over the internet. - UDPPayloadSize = 512 -) - -// UDPConfig is the config data needed to create a UDP Client -type UDPConfig struct { - // Addr should be of the form "host:port" - // or "[ipv6-host%zone]:port". - Addr string - - // PayloadSize is the maximum size of a UDP client message, optional - // Tune this based on your network. Defaults to UDPPayloadSize. - PayloadSize int -} - -// NewUDPClient returns a client interface for writing to an InfluxDB UDP -// service from the given config. -func NewUDPClient(conf UDPConfig) (Client, error) { - var udpAddr *net.UDPAddr - udpAddr, err := net.ResolveUDPAddr("udp", conf.Addr) - if err != nil { - return nil, err - } - - conn, err := net.DialUDP("udp", nil, udpAddr) - if err != nil { - return nil, err - } - - payloadSize := conf.PayloadSize - if payloadSize == 0 { - payloadSize = UDPPayloadSize - } - - return &udpclient{ - conn: conn, - payloadSize: payloadSize, - }, nil -} - -// Close releases the udpclient's resources. -func (uc *udpclient) Close() error { - return uc.conn.Close() -} - -type udpclient struct { - conn io.WriteCloser - payloadSize int -} - -func (uc *udpclient) Write(bp BatchPoints) error { - var b = make([]byte, 0, uc.payloadSize) // initial buffer size, it will grow as needed - var d, _ = time.ParseDuration("1" + bp.Precision()) - - var delayedError error - - var checkBuffer = func(n int) { - if len(b) > 0 && len(b)+n > uc.payloadSize { - if _, err := uc.conn.Write(b); err != nil { - delayedError = err - } - b = b[:0] - } - } - - for _, p := range bp.Points() { - p.pt.Round(d) - pointSize := p.pt.StringSize() + 1 // include newline in size - //point := p.pt.RoundedString(d) + "\n" - - checkBuffer(pointSize) - - if p.Time().IsZero() || pointSize <= uc.payloadSize { - b = p.pt.AppendString(b) - b = append(b, '\n') - continue - } - - points := p.pt.Split(uc.payloadSize - 1) // account for newline character - for _, sp := range points { - checkBuffer(sp.StringSize() + 1) - b = sp.AppendString(b) - b = append(b, '\n') - } - } - - if len(b) > 0 { - if _, err := uc.conn.Write(b); err != nil { - return err - } - } - return delayedError -} - -func (uc *udpclient) Query(q Query) (*Response, error) { - return nil, fmt.Errorf("Querying via UDP is not supported") -} - -func (uc *udpclient) Ping(timeout time.Duration) (time.Duration, string, error) { - return 0, "", nil -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli.go b/vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli.go deleted file mode 100644 index 90dd1a13..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli.go +++ /dev/null @@ -1,1006 +0,0 @@ -package cli // import "github.com/influxdata/influxdb/cmd/influx/cli" - -import ( - "bytes" - "encoding/csv" - "encoding/json" - "errors" - "fmt" - "io" - "io/ioutil" - "net" - "os" - "os/signal" - "path/filepath" - "sort" - "strconv" - "strings" - "syscall" - "text/tabwriter" - - "golang.org/x/crypto/ssh/terminal" - - "github.com/influxdata/influxdb/client" - "github.com/influxdata/influxdb/importer/v8" - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/models" - "github.com/peterh/liner" -) - -const ( - noTokenMsg = "Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.\n" -) - -// ErrBlankCommand is returned when a parsed command is empty. -var ErrBlankCommand = errors.New("empty input") - -// CommandLine holds CLI configuration and state -type CommandLine struct { - Line *liner.State - Host string - Port int - Database string - Ssl bool - RetentionPolicy string - ClientVersion string - ServerVersion string - Pretty bool // controls pretty print for json - Format string // controls the output format. Valid values are json, csv, or column - Execute string - ShowVersion bool - Import bool - Chunked bool - Quit chan struct{} - IgnoreSignals bool // Ignore signals normally caught by this process (used primarily for testing) - ForceTTY bool // Force the CLI to act as if it were connected to a TTY - osSignals chan os.Signal - historyFilePath string - - Client *client.Client - ClientConfig client.Config // Client config options. - ImporterConfig v8.Config // Importer configuration options. -} - -// New returns an instance of CommandLine -func New(version string) *CommandLine { - return &CommandLine{ - ClientVersion: version, - Quit: make(chan struct{}, 1), - osSignals: make(chan os.Signal, 1), - } -} - -// Run executes the CLI -func (c *CommandLine) Run() error { - hasTTY := c.ForceTTY || terminal.IsTerminal(int(os.Stdin.Fd())) - - var promptForPassword bool - // determine if they set the password flag but provided no value - for _, v := range os.Args { - v = strings.ToLower(v) - if (strings.HasPrefix(v, "-password") || strings.HasPrefix(v, "--password")) && c.ClientConfig.Password == "" { - promptForPassword = true - break - } - } - - // Check if we will be able to prompt for the password later. - if promptForPassword && !hasTTY { - return errors.New("Unable to prompt for a password with no TTY.") - } - - // Read environment variables for username/password. - if c.ClientConfig.Username == "" { - c.ClientConfig.Username = os.Getenv("INFLUX_USERNAME") - } - // If we are going to be prompted for a password, always use the entered password. - if promptForPassword { - // Open the liner (temporarily) and prompt for the password. - p, e := func() (string, error) { - l := liner.NewLiner() - defer l.Close() - return l.PasswordPrompt("password: ") - }() - if e != nil { - return errors.New("Unable to parse password") - } - c.ClientConfig.Password = p - } else if c.ClientConfig.Password == "" { - c.ClientConfig.Password = os.Getenv("INFLUX_PASSWORD") - } - - if err := c.Connect(""); err != nil { - return fmt.Errorf( - "Failed to connect to %s: %s\nPlease check your connection settings and ensure 'influxd' is running.", - c.Client.Addr(), err.Error()) - } - - // Modify precision. - c.SetPrecision(c.ClientConfig.Precision) - - if c.Execute != "" { - // Make the non-interactive mode send everything through the CLI's parser - // the same way the interactive mode works - lines := strings.Split(c.Execute, "\n") - for _, line := range lines { - if err := c.ParseCommand(line); err != nil { - return err - } - } - return nil - } - - if c.Import { - addr := net.JoinHostPort(c.Host, strconv.Itoa(c.Port)) - u, e := client.ParseConnectionString(addr, c.Ssl) - if e != nil { - return e - } - - // Copy the latest importer config and inject the latest client config - // into it. - config := c.ImporterConfig - config.Config = c.ClientConfig - config.URL = u - - i := v8.NewImporter(config) - if err := i.Import(); err != nil { - err = fmt.Errorf("ERROR: %s\n", err) - return err - } - return nil - } - - if !hasTTY { - cmd, err := ioutil.ReadAll(os.Stdin) - if err != nil { - return err - } - return c.ExecuteQuery(string(cmd)) - } - - if !c.IgnoreSignals { - // register OS signals for graceful termination - signal.Notify(c.osSignals, syscall.SIGINT, syscall.SIGTERM) - } - - c.Line = liner.NewLiner() - defer c.Line.Close() - - c.Line.SetMultiLineMode(true) - - token, err := c.DatabaseToken() - if err != nil { - return fmt.Errorf("Failed to check token: %s", err.Error()) - } - if token == "" { - fmt.Printf(noTokenMsg) - } - fmt.Printf("Connected to %s version %s\n", c.Client.Addr(), c.ServerVersion) - - c.Version() - - // Only load/write history if HOME environment variable is set. - if homeDir := os.Getenv("HOME"); homeDir != "" { - // Attempt to load the history file. - c.historyFilePath = filepath.Join(homeDir, ".influx_history") - if historyFile, err := os.Open(c.historyFilePath); err == nil { - c.Line.ReadHistory(historyFile) - historyFile.Close() - } - } - - // read from prompt until exit is run - return c.mainLoop() -} - -// mainLoop runs the main prompt loop for the CLI. -func (c *CommandLine) mainLoop() error { - for { - select { - case <-c.osSignals: - c.exit() - return nil - case <-c.Quit: - c.exit() - return nil - default: - l, e := c.Line.Prompt("> ") - if e == io.EOF { - // Instead of die, register that someone exited the program gracefully - l = "exit" - } else if e != nil { - c.exit() - return e - } - if err := c.ParseCommand(l); err != ErrBlankCommand && !strings.HasPrefix(strings.TrimSpace(l), "auth") { - c.Line.AppendHistory(l) - c.saveHistory() - } - } - } -} - -// ParseCommand parses an instruction and calls related method, if any -func (c *CommandLine) ParseCommand(cmd string) error { - lcmd := strings.TrimSpace(strings.ToLower(cmd)) - tokens := strings.Fields(lcmd) - - if len(tokens) > 0 { - switch tokens[0] { - case "exit", "quit": - close(c.Quit) - case "gopher": - c.gopher() - case "connect": - return c.Connect(cmd) - case "auth": - c.SetAuth(cmd) - case "help": - c.help() - case "history": - c.history() - case "format": - c.SetFormat(cmd) - case "precision": - c.SetPrecision(cmd) - case "consistency": - c.SetWriteConsistency(cmd) - case "settings": - c.Settings() - case "pretty": - c.Pretty = !c.Pretty - if c.Pretty { - fmt.Println("Pretty print enabled") - } else { - fmt.Println("Pretty print disabled") - } - case "use": - c.use(cmd) - case "insert": - return c.Insert(cmd) - case "clear": - c.clear(cmd) - default: - return c.ExecuteQuery(cmd) - } - - return nil - } - return ErrBlankCommand -} - -// Connect connects client to a server -func (c *CommandLine) Connect(cmd string) error { - // Remove the "connect" keyword if it exists - addr := strings.TrimSpace(strings.Replace(cmd, "connect", "", -1)) - if addr == "" { - // If they didn't provide a connection string, use the current settings - addr = net.JoinHostPort(c.Host, strconv.Itoa(c.Port)) - } - - URL, err := client.ParseConnectionString(addr, c.Ssl) - if err != nil { - return err - } - - // Create copy of the current client config and create a new client. - ClientConfig := c.ClientConfig - ClientConfig.UserAgent = "InfluxDBShell/" + c.ClientVersion - ClientConfig.URL = URL - - client, err := client.NewClient(ClientConfig) - if err != nil { - return fmt.Errorf("Could not create client %s", err) - } - c.Client = client - - _, v, err := c.Client.Ping() - if err != nil { - return fmt.Errorf("Failed to connect to %s: %v\n", c.Client.Addr(), err) - } - c.ServerVersion = v - - // Update the command with the current connection information - if host, port, err := net.SplitHostPort(ClientConfig.URL.Host); err == nil { - c.Host = host - if i, err := strconv.Atoi(port); err == nil { - c.Port = i - } - } - - return nil -} - -// SetAuth sets client authentication credentials -func (c *CommandLine) SetAuth(cmd string) { - // If they pass in the entire command, we should parse it - // auth - args := strings.Fields(cmd) - if len(args) == 3 { - args = args[1:] - } else { - args = []string{} - } - - if len(args) == 2 { - c.ClientConfig.Username = args[0] - c.ClientConfig.Password = args[1] - } else { - u, e := c.Line.Prompt("username: ") - if e != nil { - fmt.Printf("Unable to process input: %s", e) - return - } - c.ClientConfig.Username = strings.TrimSpace(u) - p, e := c.Line.PasswordPrompt("password: ") - if e != nil { - fmt.Printf("Unable to process input: %s", e) - return - } - c.ClientConfig.Password = p - } - - // Update the client as well - c.Client.SetAuth(c.ClientConfig.Username, c.ClientConfig.Password) -} - -func (c *CommandLine) clear(cmd string) { - args := strings.Split(strings.TrimSuffix(strings.TrimSpace(cmd), ";"), " ") - v := strings.ToLower(strings.Join(args[1:], " ")) - switch v { - case "database", "db": - c.Database = "" - fmt.Println("database context cleared") - return - case "retention policy", "rp": - c.RetentionPolicy = "" - fmt.Println("retention policy context cleared") - return - default: - if len(args) > 1 { - fmt.Printf("invalid command %q.\n", v) - } - fmt.Println(`Possible commands for 'clear' are: - # Clear the database context - clear database - clear db - - # Clear the retention policy context - clear retention policy - clear rp - `) - } -} - -func (c *CommandLine) use(cmd string) { - args := strings.Split(strings.TrimSuffix(strings.TrimSpace(cmd), ";"), " ") - if len(args) != 2 { - fmt.Printf("Could not parse database name from %q.\n", cmd) - return - } - - stmt := args[1] - db, rp, err := parseDatabaseAndRetentionPolicy([]byte(stmt)) - if err != nil { - fmt.Printf("Unable to parse database or retention policy from %s", stmt) - return - } - - if !c.databaseExists(db) { - return - } - - c.Database = db - fmt.Printf("Using database %s\n", db) - - if rp != "" { - if !c.retentionPolicyExists(db, rp) { - return - } - c.RetentionPolicy = rp - fmt.Printf("Using retention policy %s\n", rp) - } -} - -func (c *CommandLine) databaseExists(db string) bool { - // Validate if specified database exists - response, err := c.Client.Query(client.Query{Command: "SHOW DATABASES"}) - if err != nil { - fmt.Printf("ERR: %s\n", err) - return false - } else if err := response.Error(); err != nil { - if c.ClientConfig.Username == "" { - fmt.Printf("ERR: %s\n", err) - return false - } - // TODO(jsternberg): Fix SHOW DATABASES to be user-aware #6397. - // If we are unable to run SHOW DATABASES, display a warning and use the - // database anyway in case the person doesn't have permission to run the - // command, but does have permission to use the database. - fmt.Printf("WARN: %s\n", err) - } else { - // Verify the provided database exists - if databaseExists := func() bool { - for _, result := range response.Results { - for _, row := range result.Series { - if row.Name == "databases" { - for _, values := range row.Values { - for _, database := range values { - if database == db { - return true - } - } - } - } - } - } - return false - }(); !databaseExists { - fmt.Printf("ERR: Database %s doesn't exist. Run SHOW DATABASES for a list of existing databases.\n", db) - return false - } - } - return true -} - -func (c *CommandLine) retentionPolicyExists(db, rp string) bool { - // Validate if specified database exists - response, err := c.Client.Query(client.Query{Command: fmt.Sprintf("SHOW RETENTION POLICIES ON %q", db)}) - if err != nil { - fmt.Printf("ERR: %s\n", err) - return false - } else if err := response.Error(); err != nil { - if c.ClientConfig.Username == "" { - fmt.Printf("ERR: %s\n", err) - return false - } - fmt.Printf("WARN: %s\n", err) - } else { - // Verify the provided database exists - if retentionPolicyExists := func() bool { - for _, result := range response.Results { - for _, row := range result.Series { - for _, values := range row.Values { - for i, v := range values { - if i != 0 { - continue - } - if v == rp { - return true - } - } - } - } - } - return false - }(); !retentionPolicyExists { - fmt.Printf("ERR: RETENTION POLICY %s doesn't exist. Run SHOW RETENTION POLICIES ON %q for a list of existing retention polices.\n", rp, db) - return false - } - } - return true -} - -// SetPrecision sets client precision -func (c *CommandLine) SetPrecision(cmd string) { - // normalize cmd - cmd = strings.ToLower(cmd) - - // Remove the "precision" keyword if it exists - cmd = strings.TrimSpace(strings.Replace(cmd, "precision", "", -1)) - - switch cmd { - case "h", "m", "s", "ms", "u", "ns": - c.ClientConfig.Precision = cmd - c.Client.SetPrecision(c.ClientConfig.Precision) - case "rfc3339": - c.ClientConfig.Precision = "" - c.Client.SetPrecision(c.ClientConfig.Precision) - default: - fmt.Printf("Unknown precision %q. Please use rfc3339, h, m, s, ms, u or ns.\n", cmd) - } -} - -// SetFormat sets output format -func (c *CommandLine) SetFormat(cmd string) { - // Remove the "format" keyword if it exists - cmd = strings.TrimSpace(strings.Replace(cmd, "format", "", -1)) - // normalize cmd - cmd = strings.ToLower(cmd) - - switch cmd { - case "json", "csv", "column": - c.Format = cmd - default: - fmt.Printf("Unknown format %q. Please use json, csv, or column.\n", cmd) - } -} - -// SetWriteConsistency sets cluster consistency level -func (c *CommandLine) SetWriteConsistency(cmd string) { - // Remove the "consistency" keyword if it exists - cmd = strings.TrimSpace(strings.Replace(cmd, "consistency", "", -1)) - // normalize cmd - cmd = strings.ToLower(cmd) - - _, err := models.ParseConsistencyLevel(cmd) - if err != nil { - fmt.Printf("Unknown consistency level %q. Please use any, one, quorum, or all.\n", cmd) - return - } - c.ClientConfig.WriteConsistency = cmd -} - -// isWhitespace returns true if the rune is a space, tab, or newline. -func isWhitespace(ch rune) bool { return ch == ' ' || ch == '\t' || ch == '\n' } - -// isLetter returns true if the rune is a letter. -func isLetter(ch rune) bool { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') } - -// isDigit returns true if the rune is a digit. -func isDigit(ch rune) bool { return (ch >= '0' && ch <= '9') } - -// isIdentFirstChar returns true if the rune can be used as the first char in an unquoted identifer. -func isIdentFirstChar(ch rune) bool { return isLetter(ch) || ch == '_' } - -// isIdentChar returns true if the rune can be used in an unquoted identifier. -func isNotIdentChar(ch rune) bool { return !(isLetter(ch) || isDigit(ch) || ch == '_') } - -func parseUnquotedIdentifier(stmt string) (string, string) { - if fields := strings.FieldsFunc(stmt, isNotIdentChar); len(fields) > 0 { - return fields[0], strings.TrimPrefix(stmt, fields[0]) - } - return "", stmt -} - -func parseDoubleQuotedIdentifier(stmt string) (string, string) { - escapeNext := false - fields := strings.FieldsFunc(stmt, func(ch rune) bool { - if ch == '\\' { - escapeNext = true - } else if ch == '"' { - if !escapeNext { - return true - } - escapeNext = false - } - return false - }) - if len(fields) > 0 { - return fields[0], strings.TrimPrefix(stmt, "\""+fields[0]+"\"") - } - return "", stmt -} - -func parseNextIdentifier(stmt string) (ident, remainder string) { - if len(stmt) > 0 { - switch { - case isWhitespace(rune(stmt[0])): - return parseNextIdentifier(stmt[1:]) - case isIdentFirstChar(rune(stmt[0])): - return parseUnquotedIdentifier(stmt) - case stmt[0] == '"': - return parseDoubleQuotedIdentifier(stmt) - } - } - return "", stmt -} - -func (c *CommandLine) parseInto(stmt string) *client.BatchPoints { - ident, stmt := parseNextIdentifier(stmt) - db, rp := c.Database, c.RetentionPolicy - if strings.HasPrefix(stmt, ".") { - db = ident - ident, stmt = parseNextIdentifier(stmt[1:]) - } - if strings.HasPrefix(stmt, " ") { - rp = ident - stmt = stmt[1:] - } - - return &client.BatchPoints{ - Points: []client.Point{ - client.Point{Raw: stmt}, - }, - Database: db, - RetentionPolicy: rp, - Precision: c.ClientConfig.Precision, - WriteConsistency: c.ClientConfig.WriteConsistency, - } -} - -func (c *CommandLine) parseInsert(stmt string) (*client.BatchPoints, error) { - i, point := parseNextIdentifier(stmt) - if !strings.EqualFold(i, "insert") { - return nil, fmt.Errorf("found %s, expected INSERT\n", i) - } - if i, r := parseNextIdentifier(point); strings.EqualFold(i, "into") { - bp := c.parseInto(r) - return bp, nil - } - return &client.BatchPoints{ - Points: []client.Point{ - client.Point{Raw: point}, - }, - Database: c.Database, - RetentionPolicy: c.RetentionPolicy, - Precision: c.ClientConfig.Precision, - WriteConsistency: c.ClientConfig.WriteConsistency, - }, nil -} - -// Insert runs an INSERT statement -func (c *CommandLine) Insert(stmt string) error { - bp, err := c.parseInsert(stmt) - if err != nil { - fmt.Printf("ERR: %s\n", err) - return nil - } - if _, err := c.Client.Write(*bp); err != nil { - fmt.Printf("ERR: %s\n", err) - if c.Database == "" { - fmt.Println("Note: error may be due to not setting a database or retention policy.") - fmt.Println(`Please set a database with the command "use " or`) - fmt.Println("INSERT INTO . ") - } - } - return nil -} - -// query creates a query struct to be used with the client. -func (c *CommandLine) query(query string) client.Query { - return client.Query{ - Command: query, - Database: c.Database, - Chunked: true, - } -} - -// ExecuteQuery runs any query statement -func (c *CommandLine) ExecuteQuery(query string) error { - // If we have a retention policy, we need to rewrite the statement sources - if c.RetentionPolicy != "" { - pq, err := influxql.NewParser(strings.NewReader(query)).ParseQuery() - if err != nil { - fmt.Printf("ERR: %s\n", err) - return err - } - for _, stmt := range pq.Statements { - if selectStatement, ok := stmt.(*influxql.SelectStatement); ok { - influxql.WalkFunc(selectStatement.Sources, func(n influxql.Node) { - if t, ok := n.(*influxql.Measurement); ok { - if t.Database == "" && c.Database != "" { - t.Database = c.Database - } - if t.RetentionPolicy == "" && c.RetentionPolicy != "" { - t.RetentionPolicy = c.RetentionPolicy - } - } - }) - } - } - query = pq.String() - } - response, err := c.Client.Query(c.query(query)) - if err != nil { - fmt.Printf("ERR: %s\n", err) - return err - } - c.FormatResponse(response, os.Stdout) - if err := response.Error(); err != nil { - fmt.Printf("ERR: %s\n", response.Error()) - if c.Database == "" { - fmt.Println("Warning: It is possible this error is due to not setting a database.") - fmt.Println(`Please set a database with the command "use ".`) - } - return err - } - return nil -} - -// DatabaseToken retrieves database token -func (c *CommandLine) DatabaseToken() (string, error) { - response, err := c.Client.Query(c.query("SHOW DIAGNOSTICS for 'registration'")) - if err != nil { - return "", err - } - - if response.Error() != nil || len(response.Results) == 0 || len(response.Results[0].Series) == 0 { - return "", nil - } - - // Look for position of "token" column. - for i, s := range (*response).Results[0].Series[0].Columns { - if s == "token" { - return (*response).Results[0].Series[0].Values[0][i].(string), nil - } - } - return "", nil -} - -// FormatResponse formats output to previsouly chosen format -func (c *CommandLine) FormatResponse(response *client.Response, w io.Writer) { - switch c.Format { - case "json": - c.writeJSON(response, w) - case "csv": - c.writeCSV(response, w) - case "column": - c.writeColumns(response, w) - default: - fmt.Fprintf(w, "Unknown output format %q.\n", c.Format) - } -} - -func (c *CommandLine) writeJSON(response *client.Response, w io.Writer) { - var data []byte - var err error - if c.Pretty { - data, err = json.MarshalIndent(response, "", " ") - } else { - data, err = json.Marshal(response) - } - if err != nil { - fmt.Fprintf(w, "Unable to parse json: %s\n", err) - return - } - fmt.Fprintln(w, string(data)) -} - -func (c *CommandLine) writeCSV(response *client.Response, w io.Writer) { - csvw := csv.NewWriter(w) - for _, result := range response.Results { - // Create a tabbed writer for each result as they won't always line up - rows := c.formatResults(result, "\t") - for _, r := range rows { - csvw.Write(strings.Split(r, "\t")) - } - csvw.Flush() - } -} - -func (c *CommandLine) writeColumns(response *client.Response, w io.Writer) { - // Create a tabbed writer for each result as they won't always line up - writer := new(tabwriter.Writer) - writer.Init(w, 0, 8, 1, ' ', 0) - - for _, result := range response.Results { - // Print out all messages first - for _, m := range result.Messages { - fmt.Fprintf(w, "%s: %s.\n", m.Level, m.Text) - } - csv := c.formatResults(result, "\t") - for _, r := range csv { - fmt.Fprintln(writer, r) - } - writer.Flush() - } -} - -// formatResults will behave differently if you are formatting for columns or csv -func (c *CommandLine) formatResults(result client.Result, separator string) []string { - rows := []string{} - // Create a tabbed writer for each result as they won't always line up - for i, row := range result.Series { - // gather tags - tags := []string{} - for k, v := range row.Tags { - tags = append(tags, fmt.Sprintf("%s=%s", k, v)) - sort.Strings(tags) - } - - columnNames := []string{} - - // Only put name/tags in a column if format is csv - if c.Format == "csv" { - if len(tags) > 0 { - columnNames = append([]string{"tags"}, columnNames...) - } - - if row.Name != "" { - columnNames = append([]string{"name"}, columnNames...) - } - } - - for _, column := range row.Columns { - columnNames = append(columnNames, column) - } - - // Output a line separator if we have more than one set or results and format is column - if i > 0 && c.Format == "column" { - rows = append(rows, "") - } - - // If we are column format, we break out the name/tag to separate lines - if c.Format == "column" { - if row.Name != "" { - n := fmt.Sprintf("name: %s", row.Name) - rows = append(rows, n) - } - if len(tags) > 0 { - t := fmt.Sprintf("tags: %s", (strings.Join(tags, ", "))) - rows = append(rows, t) - } - } - - rows = append(rows, strings.Join(columnNames, separator)) - - // if format is column, write dashes under each column - if c.Format == "column" { - lines := []string{} - for _, columnName := range columnNames { - lines = append(lines, strings.Repeat("-", len(columnName))) - } - rows = append(rows, strings.Join(lines, separator)) - } - - for _, v := range row.Values { - var values []string - if c.Format == "csv" { - if row.Name != "" { - values = append(values, row.Name) - } - if len(tags) > 0 { - values = append(values, strings.Join(tags, ",")) - } - } - - for _, vv := range v { - values = append(values, interfaceToString(vv)) - } - rows = append(rows, strings.Join(values, separator)) - } - // Output a line separator if in column format - if c.Format == "column" { - rows = append(rows, "") - } - } - return rows -} - -func interfaceToString(v interface{}) string { - switch t := v.(type) { - case nil: - return "" - case bool: - return fmt.Sprintf("%v", v) - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr: - return fmt.Sprintf("%d", t) - case float32, float64: - return fmt.Sprintf("%v", t) - default: - return fmt.Sprintf("%v", t) - } -} - -// Settings prints current settings -func (c *CommandLine) Settings() { - w := new(tabwriter.Writer) - w.Init(os.Stdout, 0, 1, 1, ' ', 0) - fmt.Fprintln(w, "Setting\tValue") - fmt.Fprintln(w, "--------\t--------") - if c.Port > 0 { - fmt.Fprintf(w, "Host\t%s:%d\n", c.Host, c.Port) - } else { - fmt.Fprintf(w, "Host\t%s\n", c.Host) - } - fmt.Fprintf(w, "Username\t%s\n", c.ClientConfig.Username) - fmt.Fprintf(w, "Database\t%s\n", c.Database) - fmt.Fprintf(w, "RetentionPolicy\t%s\n", c.RetentionPolicy) - fmt.Fprintf(w, "Pretty\t%v\n", c.Pretty) - fmt.Fprintf(w, "Format\t%s\n", c.Format) - fmt.Fprintf(w, "Write Consistency\t%s\n", c.ClientConfig.WriteConsistency) - fmt.Fprintln(w) - w.Flush() -} - -func (c *CommandLine) help() { - fmt.Println(`Usage: - connect connects to another node specified by host:port - auth prompts for username and password - pretty toggles pretty print for the json format - use sets current database - format specifies the format of the server responses: json, csv, or column - precision specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns - consistency sets write consistency level: any, one, quorum, or all - history displays command history - settings outputs the current settings for the shell - clear clears settings such as database or retention policy. run 'clear' for help - exit/quit/ctrl+d quits the influx shell - - show databases show database names - show series show series information - show measurements show measurement information - show tag keys show tag key information - show field keys show field key information - - A full list of influxql commands can be found at: - https://docs.influxdata.com/influxdb/latest/query_language/spec/ -`) -} - -func (c *CommandLine) history() { - var buf bytes.Buffer - c.Line.WriteHistory(&buf) - fmt.Print(buf.String()) -} - -func (c *CommandLine) saveHistory() { - if historyFile, err := os.Create(c.historyFilePath); err != nil { - fmt.Printf("There was an error writing history file: %s\n", err) - } else { - c.Line.WriteHistory(historyFile) - historyFile.Close() - } -} - -func (c *CommandLine) gopher() { - fmt.Println(` - .-::-::://:-::- .:/++/' - '://:-''/oo+//++o+/.://o- ./+: - .:-. '++- .o/ '+yydhy' o- - .:/. .h: :osoys .smMN- :/ - -/:.' s- /MMMymh. '/y/ s' - -+s:'''' d -mMMms// '-/o: - -/++/++/////:. o: '... s- :s. - :+-+s-' ':/' 's- /+ 'o: - '+-'o: /ydhsh. '//. '-o- o- - .y. o: .MMMdm+y ':+++:::/+:.' s: - .-h/ y- 'sdmds'h -+ydds:::-.' 'h. - .//-.d' o: '.' 'dsNMMMNh:.:++' :y - +y. 'd 's. .s:mddds: ++ o/ - 'N- odd 'o/. './o-s-' .---+++' o- - 'N' yNd .://:/:::::. -s -+/s/./s' 'o/' - so' .h '''' ////s: '+. .s +y' - os/-.y' 's' 'y::+ +d' - '.:o/ -+:-:.' so.---.' - o' 'd-.''/s' - .s' :y.''.y - -s mo:::' - :: yh - // '''' /M' - o+ .s///:/. 'N: - :+ /: -s' ho - 's- -/s/:+/.+h' +h - ys' ':' '-. -d - oh .h - /o .s - s. .h - -y .d - m/ -h - +d /o - 'N- y: - h: m. - s- -d - o- s+ - +- 'm' - s/ oo--. - y- /s ':+' - s' 'od--' .d: - -+ ':o: ':+-/+ - y- .:+- ' - //o- '.:+/. - .-:+/' ''-/+/. - ./:' ''.:o+/-' - .+o:/:/+-' ''.-+ooo/-' - o: -h///++////-. - /: .o/ - //+ 'y - ./sooy. - -`) -} - -// Version prints CLI version -func (c *CommandLine) Version() { - fmt.Println("InfluxDB shell version:", c.ClientVersion) -} - -func (c *CommandLine) exit() { - // write to history file - c.saveHistory() - // release line resources - c.Line.Close() - c.Line = nil -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_internal_test.go b/vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_internal_test.go deleted file mode 100644 index a1eacd05..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_internal_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package cli - -import "testing" - -func TestParseCommand_InsertInto(t *testing.T) { - t.Parallel() - - c := CommandLine{} - - tests := []struct { - cmd, db, rp string - }{ - { - cmd: `INSERT INTO test cpu,host=serverA,region=us-west value=1.0`, - db: "", - rp: "test", - }, - { - cmd: ` INSERT INTO .test cpu,host=serverA,region=us-west value=1.0`, - db: "", - rp: "test", - }, - { - cmd: `INSERT INTO "test test" cpu,host=serverA,region=us-west value=1.0`, - db: "", - rp: "test test", - }, - { - cmd: `Insert iNTO test.test cpu,host=serverA,region=us-west value=1.0`, - db: "test", - rp: "test", - }, - { - cmd: `insert into "test test" cpu,host=serverA,region=us-west value=1.0`, - db: "", - rp: "test test", - }, - { - cmd: `insert into "d b"."test test" cpu,host=serverA,region=us-west value=1.0`, - db: "d b", - rp: "test test", - }, - } - - for _, test := range tests { - t.Logf("command: %s", test.cmd) - bp, err := c.parseInsert(test.cmd) - if err != nil { - t.Fatal(err) - } - if bp.Database != test.db { - t.Fatalf(`Command "insert into" db parsing failed, expected: %q, actual: %q`, test.db, bp.Database) - } - if bp.RetentionPolicy != test.rp { - t.Fatalf(`Command "insert into" rp parsing failed, expected: %q, actual: %q`, test.rp, bp.RetentionPolicy) - } - } -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_test.go b/vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_test.go deleted file mode 100644 index 9ec74c28..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/cli_test.go +++ /dev/null @@ -1,523 +0,0 @@ -package cli_test - -import ( - "bufio" - "bytes" - "fmt" - "io" - "net" - "net/http" - "net/http/httptest" - "net/url" - "strconv" - "strings" - "testing" - - "github.com/influxdata/influxdb/client" - "github.com/influxdata/influxdb/cmd/influx/cli" - "github.com/influxdata/influxdb/influxql" - "github.com/peterh/liner" -) - -const ( - CLIENT_VERSION = "y.y" - SERVER_VERSION = "x.x" -) - -func TestNewCLI(t *testing.T) { - t.Parallel() - c := cli.New(CLIENT_VERSION) - - if c == nil { - t.Fatal("CommandLine shouldn't be nil.") - } - - if c.ClientVersion != CLIENT_VERSION { - t.Fatalf("CommandLine version is %s but should be %s", c.ClientVersion, CLIENT_VERSION) - } -} - -func TestRunCLI(t *testing.T) { - t.Parallel() - ts := emptyTestServer() - defer ts.Close() - - u, _ := url.Parse(ts.URL) - h, p, _ := net.SplitHostPort(u.Host) - c := cli.New(CLIENT_VERSION) - c.Host = h - c.Port, _ = strconv.Atoi(p) - c.IgnoreSignals = true - c.ForceTTY = true - go func() { - close(c.Quit) - }() - if err := c.Run(); err != nil { - t.Fatalf("Run failed with error: %s", err) - } -} - -func TestRunCLI_ExecuteInsert(t *testing.T) { - t.Parallel() - ts := emptyTestServer() - defer ts.Close() - - u, _ := url.Parse(ts.URL) - h, p, _ := net.SplitHostPort(u.Host) - c := cli.New(CLIENT_VERSION) - c.Host = h - c.Port, _ = strconv.Atoi(p) - c.ClientConfig.Precision = "ms" - c.Execute = "INSERT sensor,floor=1 value=2" - c.IgnoreSignals = true - c.ForceTTY = true - if err := c.Run(); err != nil { - t.Fatalf("Run failed with error: %s", err) - } -} - -func TestSetAuth(t *testing.T) { - t.Parallel() - c := cli.New(CLIENT_VERSION) - config := client.NewConfig() - client, _ := client.NewClient(config) - c.Client = client - u := "userx" - p := "pwdy" - c.SetAuth("auth " + u + " " + p) - - // validate CLI configuration - if c.ClientConfig.Username != u { - t.Fatalf("Username is %s but should be %s", c.ClientConfig.Username, u) - } - if c.ClientConfig.Password != p { - t.Fatalf("Password is %s but should be %s", c.ClientConfig.Password, p) - } -} - -func TestSetPrecision(t *testing.T) { - t.Parallel() - c := cli.New(CLIENT_VERSION) - config := client.NewConfig() - client, _ := client.NewClient(config) - c.Client = client - - // validate set non-default precision - p := "ns" - c.SetPrecision("precision " + p) - if c.ClientConfig.Precision != p { - t.Fatalf("Precision is %s but should be %s", c.ClientConfig.Precision, p) - } - - // validate set default precision which equals empty string - p = "rfc3339" - c.SetPrecision("precision " + p) - if c.ClientConfig.Precision != "" { - t.Fatalf("Precision is %s but should be empty", c.ClientConfig.Precision) - } -} - -func TestSetFormat(t *testing.T) { - t.Parallel() - c := cli.New(CLIENT_VERSION) - config := client.NewConfig() - client, _ := client.NewClient(config) - c.Client = client - - // validate set non-default format - f := "json" - c.SetFormat("format " + f) - if c.Format != f { - t.Fatalf("Format is %s but should be %s", c.Format, f) - } -} - -func TestSetWriteConsistency(t *testing.T) { - t.Parallel() - c := cli.New(CLIENT_VERSION) - config := client.NewConfig() - client, _ := client.NewClient(config) - c.Client = client - - // set valid write consistency - consistency := "all" - c.SetWriteConsistency("consistency " + consistency) - if c.ClientConfig.WriteConsistency != consistency { - t.Fatalf("WriteConsistency is %s but should be %s", c.ClientConfig.WriteConsistency, consistency) - } - - // set different valid write consistency and validate change - consistency = "quorum" - c.SetWriteConsistency("consistency " + consistency) - if c.ClientConfig.WriteConsistency != consistency { - t.Fatalf("WriteConsistency is %s but should be %s", c.ClientConfig.WriteConsistency, consistency) - } - - // set invalid write consistency and verify there was no change - invalidConsistency := "invalid_consistency" - c.SetWriteConsistency("consistency " + invalidConsistency) - if c.ClientConfig.WriteConsistency == invalidConsistency { - t.Fatalf("WriteConsistency is %s but should be %s", c.ClientConfig.WriteConsistency, consistency) - } -} - -func TestParseCommand_CommandsExist(t *testing.T) { - t.Parallel() - c, err := client.NewClient(client.Config{}) - if err != nil { - t.Fatalf("unexpected error %v", err) - } - - m := cli.CommandLine{Client: c, Line: liner.NewLiner()} - tests := []struct { - cmd string - }{ - {cmd: "gopher"}, - {cmd: "auth"}, - {cmd: "help"}, - {cmd: "format"}, - {cmd: "precision"}, - {cmd: "settings"}, - } - for _, test := range tests { - if err := m.ParseCommand(test.cmd); err != nil { - t.Fatalf(`Got error %v for command %q, expected nil`, err, test.cmd) - } - } -} - -func TestParseCommand_Connect(t *testing.T) { - t.Parallel() - ts := emptyTestServer() - defer ts.Close() - - u, _ := url.Parse(ts.URL) - cmd := "connect " + u.Host - c := cli.CommandLine{} - - // assert connection is established - if err := c.ParseCommand(cmd); err != nil { - t.Fatalf("There was an error while connecting to %v: %v", u.Path, err) - } - - // assert server version is populated - if c.ServerVersion != SERVER_VERSION { - t.Fatalf("Server version is %s but should be %s.", c.ServerVersion, SERVER_VERSION) - } -} - -func TestParseCommand_TogglePretty(t *testing.T) { - t.Parallel() - c := cli.CommandLine{} - if c.Pretty { - t.Fatalf(`Pretty should be false.`) - } - c.ParseCommand("pretty") - if !c.Pretty { - t.Fatalf(`Pretty should be true.`) - } - c.ParseCommand("pretty") - if c.Pretty { - t.Fatalf(`Pretty should be false.`) - } -} - -func TestParseCommand_Exit(t *testing.T) { - t.Parallel() - tests := []struct { - cmd string - }{ - {cmd: "exit"}, - {cmd: " exit"}, - {cmd: "exit "}, - {cmd: "Exit "}, - } - - for _, test := range tests { - c := cli.CommandLine{Quit: make(chan struct{}, 1)} - c.ParseCommand(test.cmd) - // channel should be closed - if _, ok := <-c.Quit; ok { - t.Fatalf(`Command "exit" failed for %q.`, test.cmd) - } - } -} - -func TestParseCommand_Quit(t *testing.T) { - t.Parallel() - tests := []struct { - cmd string - }{ - {cmd: "quit"}, - {cmd: " quit"}, - {cmd: "quit "}, - {cmd: "Quit "}, - } - - for _, test := range tests { - c := cli.CommandLine{Quit: make(chan struct{}, 1)} - c.ParseCommand(test.cmd) - // channel should be closed - if _, ok := <-c.Quit; ok { - t.Fatalf(`Command "quit" failed for %q.`, test.cmd) - } - } -} - -func TestParseCommand_Use(t *testing.T) { - t.Parallel() - ts := emptyTestServer() - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - - tests := []struct { - cmd string - }{ - {cmd: "use db"}, - {cmd: " use db"}, - {cmd: "use db "}, - {cmd: "use db;"}, - {cmd: "use db; "}, - {cmd: "Use db"}, - } - - for _, test := range tests { - m := cli.CommandLine{Client: c} - if err := m.ParseCommand(test.cmd); err != nil { - t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd) - } - - if m.Database != "db" { - t.Fatalf(`Command "use" changed database to %q. Expected db`, m.Database) - } - } -} - -func TestParseCommand_UseAuth(t *testing.T) { - t.Parallel() - ts := emptyTestServer() - defer ts.Close() - - u, _ := url.Parse(ts.URL) - tests := []struct { - cmd string - user string - database string - }{ - { - cmd: "use db", - user: "admin", - database: "db", - }, - { - cmd: "use blank", - user: "admin", - database: "", - }, - { - cmd: "use db", - user: "anonymous", - database: "db", - }, - { - cmd: "use blank", - user: "anonymous", - database: "blank", - }, - } - - for i, tt := range tests { - config := client.Config{URL: *u, Username: tt.user} - fmt.Println("using auth:", tt.user) - c, err := client.NewClient(config) - if err != nil { - t.Errorf("%d. unexpected error. expected %v, actual %v", i, nil, err) - continue - } - m := cli.CommandLine{Client: c} - m.ClientConfig.Username = tt.user - - if err := m.ParseCommand(tt.cmd); err != nil { - t.Fatalf(`%d. Got error %v for command %q, expected nil.`, i, err, tt.cmd) - } - - if m.Database != tt.database { - t.Fatalf(`%d. Command "use" changed database to %q. Expected %q`, i, m.Database, tt.database) - } - } -} - -func TestParseCommand_Consistency(t *testing.T) { - t.Parallel() - c := cli.CommandLine{} - tests := []struct { - cmd string - }{ - {cmd: "consistency one"}, - {cmd: " consistency one"}, - {cmd: "consistency one "}, - {cmd: "consistency one;"}, - {cmd: "consistency one; "}, - {cmd: "Consistency one"}, - } - - for _, test := range tests { - if err := c.ParseCommand(test.cmd); err != nil { - t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd) - } - - if c.ClientConfig.WriteConsistency != "one" { - t.Fatalf(`Command "consistency" changed consistency to %q. Expected one`, c.ClientConfig.WriteConsistency) - } - } -} - -func TestParseCommand_Insert(t *testing.T) { - t.Parallel() - ts := emptyTestServer() - defer ts.Close() - - u, _ := url.Parse(ts.URL) - config := client.Config{URL: *u} - c, err := client.NewClient(config) - if err != nil { - t.Fatalf("unexpected error. expected %v, actual %v", nil, err) - } - m := cli.CommandLine{Client: c} - - tests := []struct { - cmd string - }{ - {cmd: "INSERT cpu,host=serverA,region=us-west value=1.0"}, - {cmd: " INSERT cpu,host=serverA,region=us-west value=1.0"}, - {cmd: "INSERT cpu,host=serverA,region=us-west value=1.0"}, - {cmd: "insert cpu,host=serverA,region=us-west value=1.0 "}, - {cmd: "insert"}, - {cmd: "Insert "}, - {cmd: "insert c"}, - {cmd: "insert int"}, - } - - for _, test := range tests { - if err := m.ParseCommand(test.cmd); err != nil { - t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd) - } - } -} - -func TestParseCommand_History(t *testing.T) { - t.Parallel() - c := cli.CommandLine{Line: liner.NewLiner()} - defer c.Line.Close() - - // append one entry to history - c.Line.AppendHistory("abc") - - tests := []struct { - cmd string - }{ - {cmd: "history"}, - {cmd: " history"}, - {cmd: "history "}, - {cmd: "History "}, - } - - for _, test := range tests { - if err := c.ParseCommand(test.cmd); err != nil { - t.Fatalf(`Got error %v for command %q, expected nil.`, err, test.cmd) - } - } - - // buf size should be at least 1 - var buf bytes.Buffer - c.Line.WriteHistory(&buf) - if buf.Len() < 1 { - t.Fatal("History is borked") - } -} - -func TestParseCommand_HistoryWithBlankCommand(t *testing.T) { - t.Parallel() - c := cli.CommandLine{Line: liner.NewLiner()} - defer c.Line.Close() - - // append one entry to history - c.Line.AppendHistory("x") - - tests := []struct { - cmd string - err error - }{ - {cmd: "history"}, - {cmd: " history"}, - {cmd: "history "}, - {cmd: "", err: cli.ErrBlankCommand}, // shouldn't be persisted in history - {cmd: " ", err: cli.ErrBlankCommand}, // shouldn't be persisted in history - {cmd: " ", err: cli.ErrBlankCommand}, // shouldn't be persisted in history - } - - // a blank command will return cli.ErrBlankCommand. - for _, test := range tests { - if err := c.ParseCommand(test.cmd); err != test.err { - t.Errorf(`Got error %v for command %q, expected %v`, err, test.cmd, test.err) - } - } - - // buf shall not contain empty commands - var buf bytes.Buffer - c.Line.WriteHistory(&buf) - scanner := bufio.NewScanner(&buf) - for scanner.Scan() { - if strings.TrimSpace(scanner.Text()) == "" { - t.Fatal("Empty commands should not be persisted in history.") - } - } -} - -// helper methods - -func emptyTestServer() *httptest.Server { - return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("X-Influxdb-Version", SERVER_VERSION) - - // Fake authorization entirely based on the username. - authorized := false - user, _, _ := r.BasicAuth() - switch user { - case "", "admin": - authorized = true - } - - switch r.URL.Path { - case "/query": - values := r.URL.Query() - parser := influxql.NewParser(bytes.NewBufferString(values.Get("q"))) - q, err := parser.ParseQuery() - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } - stmt := q.Statements[0] - - switch stmt.(type) { - case *influxql.ShowDatabasesStatement: - if authorized { - io.WriteString(w, `{"results":[{"series":[{"name":"databases","columns":["name"],"values":[["db"]]}]}]}`) - } else { - w.WriteHeader(http.StatusUnauthorized) - io.WriteString(w, fmt.Sprintf(`{"error":"error authorizing query: %s not authorized to execute statement 'SHOW DATABASES', requires admin privilege"}`, user)) - } - case *influxql.ShowDiagnosticsStatement: - io.WriteString(w, `{"results":[{}]}`) - } - case "/write": - w.WriteHeader(http.StatusOK) - } - })) -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser.go b/vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser.go deleted file mode 100644 index 4134c021..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser.go +++ /dev/null @@ -1,34 +0,0 @@ -package cli - -import ( - "bytes" - "fmt" -) - -func parseDatabaseAndRetentionPolicy(stmt []byte) (string, string, error) { - var db, rp []byte - var quoted bool - var seperatorCount int - - stmt = bytes.TrimSpace(stmt) - - for _, b := range stmt { - if b == '"' { - quoted = !quoted - continue - } - if b == '.' && !quoted { - seperatorCount++ - if seperatorCount > 1 { - return "", "", fmt.Errorf("unable to parse database and retention policy from %s", string(stmt)) - } - continue - } - if seperatorCount == 1 { - rp = append(rp, b) - continue - } - db = append(db, b) - } - return string(db), string(rp), nil -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser_internal_test.go b/vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser_internal_test.go deleted file mode 100644 index 5bf955c7..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx/cli/parser_internal_test.go +++ /dev/null @@ -1,90 +0,0 @@ -package cli - -import ( - "errors" - "testing" -) - -func Test_parseDatabaseAndretentionPolicy(t *testing.T) { - tests := []struct { - stmt string - db string - rp string - err error - }{ - { - stmt: `foo`, - db: "foo", - }, - { - stmt: `"foo.bar"`, - db: "foo.bar", - }, - { - stmt: `"foo.bar".`, - db: "foo.bar", - }, - { - stmt: `."foo.bar"`, - rp: "foo.bar", - }, - { - stmt: `foo.bar`, - db: "foo", - rp: "bar", - }, - { - stmt: `"foo".bar`, - db: "foo", - rp: "bar", - }, - { - stmt: `"foo"."bar"`, - db: "foo", - rp: "bar", - }, - { - stmt: `"foo.bin"."bar"`, - db: "foo.bin", - rp: "bar", - }, - { - stmt: `"foo.bin"."bar.baz...."`, - db: "foo.bin", - rp: "bar.baz....", - }, - { - stmt: ` "foo.bin"."bar.baz...." `, - db: "foo.bin", - rp: "bar.baz....", - }, - - { - stmt: `"foo.bin"."bar".boom`, - err: errors.New("foo"), - }, - { - stmt: "foo.bar.", - err: errors.New("foo"), - }, - } - - for _, test := range tests { - db, rp, err := parseDatabaseAndRetentionPolicy([]byte(test.stmt)) - if err != nil && test.err == nil { - t.Errorf("unexpected error: got %s", err) - continue - } - if test.err != nil && err == nil { - t.Errorf("expected err: got: nil, exp: %s", test.err) - continue - } - if db != test.db { - t.Errorf("unexpected database: got: %s, exp: %s", db, test.db) - } - if rp != test.rp { - t.Errorf("unexpected retention policy: got: %s, exp: %s", rp, test.rp) - } - } - -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx/main.go b/vendor/github.com/influxdata/influxdb/cmd/influx/main.go deleted file mode 100644 index 7d8bccef..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx/main.go +++ /dev/null @@ -1,119 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "os" - - "github.com/influxdata/influxdb/client" - "github.com/influxdata/influxdb/cmd/influx/cli" -) - -// These variables are populated via the Go linker. -var ( - version string -) - -const ( - // defaultFormat is the default format of the results when issuing queries - defaultFormat = "column" - - // defaultPrecision is the default timestamp format of the results when issuing queries - defaultPrecision = "ns" - - // defaultPPS is the default points per second that the import will throttle at - // by default it's 0, which means it will not throttle - defaultPPS = 0 -) - -func init() { - // If version is not set, make that clear. - if version == "" { - version = "unknown" - } -} - -func main() { - c := cli.New(version) - - fs := flag.NewFlagSet("InfluxDB shell version "+version, flag.ExitOnError) - fs.StringVar(&c.Host, "host", client.DefaultHost, "Influxdb host to connect to.") - fs.IntVar(&c.Port, "port", client.DefaultPort, "Influxdb port to connect to.") - fs.StringVar(&c.ClientConfig.UnixSocket, "socket", "", "Influxdb unix socket to connect to.") - fs.StringVar(&c.ClientConfig.Username, "username", "", "Username to connect to the server.") - fs.StringVar(&c.ClientConfig.Password, "password", "", `Password to connect to the server. Leaving blank will prompt for password (--password="").`) - fs.StringVar(&c.Database, "database", c.Database, "Database to connect to the server.") - fs.BoolVar(&c.Ssl, "ssl", false, "Use https for connecting to cluster.") - fs.BoolVar(&c.ClientConfig.UnsafeSsl, "unsafeSsl", false, "Set this when connecting to the cluster using https and not use SSL verification.") - fs.StringVar(&c.Format, "format", defaultFormat, "Format specifies the format of the server responses: json, csv, or column.") - fs.StringVar(&c.ClientConfig.Precision, "precision", defaultPrecision, "Precision specifies the format of the timestamp: rfc3339,h,m,s,ms,u or ns.") - fs.StringVar(&c.ClientConfig.WriteConsistency, "consistency", "all", "Set write consistency level: any, one, quorum, or all.") - fs.BoolVar(&c.Pretty, "pretty", false, "Turns on pretty print for the json format.") - fs.StringVar(&c.Execute, "execute", c.Execute, "Execute command and quit.") - fs.BoolVar(&c.ShowVersion, "version", false, "Displays the InfluxDB version.") - fs.BoolVar(&c.Import, "import", false, "Import a previous database.") - fs.IntVar(&c.ImporterConfig.PPS, "pps", defaultPPS, "How many points per second the import will allow. By default it is zero and will not throttle importing.") - fs.StringVar(&c.ImporterConfig.Path, "path", "", "path to the file to import") - fs.BoolVar(&c.ImporterConfig.Compressed, "compressed", false, "set to true if the import file is compressed") - - // Define our own custom usage to print - fs.Usage = func() { - fmt.Println(`Usage of influx: - -version - Display the version and exit. - -host 'host name' - Host to connect to. - -port 'port #' - Port to connect to. - -socket 'unix domain socket' - Unix socket to connect to. - -database 'database name' - Database to connect to the server. - -password 'password' - Password to connect to the server. Leaving blank will prompt for password (--password ''). - -username 'username' - Username to connect to the server. - -ssl - Use https for requests. - -unsafeSsl - Set this when connecting to the cluster using https and not use SSL verification. - -execute 'command' - Execute command and quit. - -format 'json|csv|column' - Format specifies the format of the server responses: json, csv, or column. - -precision 'rfc3339|h|m|s|ms|u|ns' - Precision specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns. - -consistency 'any|one|quorum|all' - Set write consistency level: any, one, quorum, or all - -pretty - Turns on pretty print for the json format. - -import - Import a previous database export from file - -pps - How many points per second the import will allow. By default it is zero and will not throttle importing. - -path - Path to file to import - -compressed - Set to true if the import file is compressed - -Examples: - - # Use influx in a non-interactive mode to query the database "metrics" and pretty print json: - $ influx -database 'metrics' -execute 'select * from cpu' -format 'json' -pretty - - # Connect to a specific database on startup and set database context: - $ influx -database 'metrics' -host 'localhost' -port '8086' -`) - } - fs.Parse(os.Args[1:]) - - if c.ShowVersion { - c.Version() - os.Exit(0) - } - - if err := c.Run(); err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) - os.Exit(1) - } -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/README.md b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/README.md deleted file mode 100644 index bcdf4185..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/README.md +++ /dev/null @@ -1,107 +0,0 @@ -# `influx_inspect` - -## Ways to run - -### `influx_inspect` -Will print usage for the tool. - -### `influx_inspect report` -Displays series meta-data for all shards. Default location [$HOME/.influxdb] - -### `influx_inspect dumptsm` -Dumps low-level details about tsm1 files - -#### Flags - -##### `-index` bool -Dump raw index data. - -`default` = false - -#### `-blocks` bool -Dump raw block data. - -`default` = false - -#### `-all` -Dump all data. Caution: This may print a lot of information. - -`default` = false - -#### `-filter-key` -Only display index and block data match this key substring. - -`default` = "" - - -### `influx_inspect export` -Exports all tsm files to line protocol. This output file can be imported via the [influx](https://github.com/influxdata/influxdb/tree/master/importer#running-the-import-command) command. - - -#### `-datadir` string -Data storage path. - -`default` = "$HOME/.influxdb/data" - -#### `-waldir` string -WAL storage path. - -`default` = "$HOME/.influxdb/wal" - -#### `-out` string -Destination file to export to - -`default` = "$HOME/.influxdb/export" - -#### `-database` string (optional) -Database to export. - -`default` = "" - -#### `-retention` string (optional) -Retention policy to export. - -`default` = "" - -#### `-start` string (optional) -Optional. The time range to start at. - -#### `-end` string (optional) -Optional. The time range to end at. - -#### `-compress` bool (optional) -Compress the output. - -`default` = false - -#### Sample Commands - -Export entire database and compress output: -``` -influx_inspect export --compress -``` - -Export specific retention policy: -``` -influx_inspect export --database mydb --retention autogen -``` - -##### Sample Data -This is a sample of what the output will look like. - -``` -# DDL -CREATE DATABASE MY_DB_NAME -CREATE RETENTION POLICY autogen ON MY_DB_NAME DURATION inf REPLICATION 1 - -# DML -# CONTEXT-DATABASE:MY_DB_NAME -# CONTEXT-RETENTION-POLICY:autogen -randset value=97.9296104805 1439856000000000000 -randset value=25.3849066842 1439856100000000000 -``` - -# Caveats - -The system does not have access to the meta store when exporting TSM shards. As such, it always creates the retention policy with infinite duration and replication factor of 1. -End users may want to change this prior to re-importing if they are importing to a cluster or want a different duration for retention. diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/dumptsm/dumptsm.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/dumptsm/dumptsm.go deleted file mode 100644 index 424df2a6..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/dumptsm/dumptsm.go +++ /dev/null @@ -1,334 +0,0 @@ -package dumptsm - -import ( - "encoding/binary" - "flag" - "fmt" - "io" - "os" - "strconv" - "strings" - "text/tabwriter" - "time" - - "github.com/influxdata/influxdb/tsdb/engine/tsm1" -) - -// Command represents the program execution for "influxd dumptsm". -type Command struct { - // Standard input/output, overridden for testing. - Stderr io.Writer - Stdout io.Writer - - dumpIndex bool - dumpBlocks bool - dumpAll bool - filterKey string - path string -} - -// NewCommand returns a new instance of Command. -func NewCommand() *Command { - return &Command{ - Stderr: os.Stderr, - Stdout: os.Stdout, - } -} - -// Run executes the command. -func (cmd *Command) Run(args ...string) error { - fs := flag.NewFlagSet("file", flag.ExitOnError) - fs.BoolVar(&cmd.dumpIndex, "index", false, "Dump raw index data") - fs.BoolVar(&cmd.dumpBlocks, "blocks", false, "Dump raw block data") - fs.BoolVar(&cmd.dumpAll, "all", false, "Dump all data. Caution: This may print a lot of information") - fs.StringVar(&cmd.filterKey, "filter-key", "", "Only display index and block data match this key substring") - - fs.SetOutput(cmd.Stdout) - fs.Usage = cmd.printUsage - - if err := fs.Parse(args); err != nil { - return err - } - - if fs.Arg(0) == "" { - fmt.Printf("TSM file not specified\n\n") - fs.Usage() - return nil - } - cmd.path = fs.Args()[0] - cmd.dumpBlocks = cmd.dumpBlocks || cmd.dumpAll || cmd.filterKey != "" - cmd.dumpIndex = cmd.dumpIndex || cmd.dumpAll || cmd.filterKey != "" - return cmd.dump() -} - -func (cmd *Command) dump() error { - var errors []error - - f, err := os.Open(cmd.path) - if err != nil { - return err - } - - // Get the file size - stat, err := f.Stat() - if err != nil { - return err - } - b := make([]byte, 8) - - r, err := tsm1.NewTSMReader(f) - if err != nil { - return fmt.Errorf("Error opening TSM files: %s", err.Error()) - } - defer r.Close() - - minTime, maxTime := r.TimeRange() - keyCount := r.KeyCount() - - blockStats := &blockStats{} - - println("Summary:") - fmt.Printf(" File: %s\n", cmd.path) - fmt.Printf(" Time Range: %s - %s\n", - time.Unix(0, minTime).UTC().Format(time.RFC3339Nano), - time.Unix(0, maxTime).UTC().Format(time.RFC3339Nano), - ) - fmt.Printf(" Duration: %s ", time.Unix(0, maxTime).Sub(time.Unix(0, minTime))) - fmt.Printf(" Series: %d ", keyCount) - fmt.Printf(" File Size: %d\n", stat.Size()) - println() - - tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) - - if cmd.dumpIndex { - println("Index:") - tw.Flush() - println() - - fmt.Fprintln(tw, " "+strings.Join([]string{"Pos", "Min Time", "Max Time", "Ofs", "Size", "Key", "Field"}, "\t")) - var pos int - for i := 0; i < keyCount; i++ { - key, _ := r.KeyAt(i) - for _, e := range r.Entries(string(key)) { - pos++ - split := strings.Split(string(key), "#!~#") - - // We dont' know know if we have fields so use an informative default - var measurement, field string = "UNKNOWN", "UNKNOWN" - - // Possible corruption? Try to read as much as we can and point to the problem. - measurement = split[0] - field = split[1] - - if cmd.filterKey != "" && !strings.Contains(string(key), cmd.filterKey) { - continue - } - fmt.Fprintln(tw, " "+strings.Join([]string{ - strconv.FormatInt(int64(pos), 10), - time.Unix(0, e.MinTime).UTC().Format(time.RFC3339Nano), - time.Unix(0, e.MaxTime).UTC().Format(time.RFC3339Nano), - strconv.FormatInt(int64(e.Offset), 10), - strconv.FormatInt(int64(e.Size), 10), - measurement, - field, - }, "\t")) - tw.Flush() - } - } - } - - tw = tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) - fmt.Fprintln(tw, " "+strings.Join([]string{"Blk", "Chk", "Ofs", "Len", "Type", "Min Time", "Points", "Enc [T/V]", "Len [T/V]"}, "\t")) - - // Starting at 5 because the magic number is 4 bytes + 1 byte version - i := int64(5) - var blockCount, pointCount, blockSize int64 - indexSize := r.IndexSize() - - // Start at the beginning and read every block - for j := 0; j < keyCount; j++ { - key, _ := r.KeyAt(j) - for _, e := range r.Entries(string(key)) { - - f.Seek(int64(e.Offset), 0) - f.Read(b[:4]) - - chksum := binary.BigEndian.Uint32(b[:4]) - - buf := make([]byte, e.Size-4) - f.Read(buf) - - blockSize += int64(e.Size) - - if cmd.filterKey != "" && !strings.Contains(string(key), cmd.filterKey) { - i += blockSize - blockCount++ - continue - } - - blockType := buf[0] - - encoded := buf[1:] - - var v []tsm1.Value - v, err := tsm1.DecodeBlock(buf, v) - if err != nil { - return err - } - startTime := time.Unix(0, v[0].UnixNano()) - - pointCount += int64(len(v)) - - // Length of the timestamp block - tsLen, j := binary.Uvarint(encoded) - - // Unpack the timestamp bytes - ts := encoded[int(j) : int(j)+int(tsLen)] - - // Unpack the value bytes - values := encoded[int(j)+int(tsLen):] - - tsEncoding := timeEnc[int(ts[0]>>4)] - vEncoding := encDescs[int(blockType+1)][values[0]>>4] - - typeDesc := blockTypes[blockType] - - blockStats.inc(0, ts[0]>>4) - blockStats.inc(int(blockType+1), values[0]>>4) - blockStats.size(len(buf)) - - if cmd.dumpBlocks { - fmt.Fprintln(tw, " "+strings.Join([]string{ - strconv.FormatInt(blockCount, 10), - strconv.FormatUint(uint64(chksum), 10), - strconv.FormatInt(i, 10), - strconv.FormatInt(int64(len(buf)), 10), - typeDesc, - startTime.UTC().Format(time.RFC3339Nano), - strconv.FormatInt(int64(len(v)), 10), - fmt.Sprintf("%s/%s", tsEncoding, vEncoding), - fmt.Sprintf("%d/%d", len(ts), len(values)), - }, "\t")) - } - - i += blockSize - blockCount++ - } - } - - if cmd.dumpBlocks { - println("Blocks:") - tw.Flush() - println() - } - - var blockSizeAvg int64 - if blockCount > 0 { - blockSizeAvg = blockSize / blockCount - } - fmt.Printf("Statistics\n") - fmt.Printf(" Blocks:\n") - fmt.Printf(" Total: %d Size: %d Min: %d Max: %d Avg: %d\n", - blockCount, blockSize, blockStats.min, blockStats.max, blockSizeAvg) - fmt.Printf(" Index:\n") - fmt.Printf(" Total: %d Size: %d\n", blockCount, indexSize) - fmt.Printf(" Points:\n") - fmt.Printf(" Total: %d", pointCount) - println() - - println(" Encoding:") - for i, counts := range blockStats.counts { - if len(counts) == 0 { - continue - } - fmt.Printf(" %s: ", strings.Title(fieldType[i])) - for j, v := range counts { - fmt.Printf("\t%s: %d (%d%%) ", encDescs[i][j], v, int(float64(v)/float64(blockCount)*100)) - } - println() - } - fmt.Printf(" Compression:\n") - fmt.Printf(" Per block: %0.2f bytes/point\n", float64(blockSize)/float64(pointCount)) - fmt.Printf(" Total: %0.2f bytes/point\n", float64(stat.Size())/float64(pointCount)) - - if len(errors) > 0 { - println() - fmt.Printf("Errors (%d):\n", len(errors)) - for _, err := range errors { - fmt.Printf(" * %v\n", err) - } - println() - return fmt.Errorf("error count %d", len(errors)) - } - return nil -} - -// printUsage prints the usage message to STDERR. -func (cmd *Command) printUsage() { - usage := `Dumps low-level details about tsm1 files. - -Usage: influx_inspect dumptsm [flags] - Only display index and block data match this key substring -` - - fmt.Fprintf(cmd.Stdout, usage) -} - -var ( - fieldType = []string{ - "timestamp", "float", "int", "bool", "string", - } - blockTypes = []string{ - "float64", "int64", "bool", "string", - } - timeEnc = []string{ - "none", "s8b", "rle", - } - floatEnc = []string{ - "none", "gor", - } - intEnc = []string{ - "none", "s8b", "rle", - } - boolEnc = []string{ - "none", "bp", - } - stringEnc = []string{ - "none", "snpy", - } - encDescs = [][]string{ - timeEnc, floatEnc, intEnc, boolEnc, stringEnc, - } -) - -type blockStats struct { - min, max int - counts [][]int -} - -func (b *blockStats) inc(typ int, enc byte) { - for len(b.counts) <= typ { - b.counts = append(b.counts, []int{}) - } - for len(b.counts[typ]) <= int(enc) { - b.counts[typ] = append(b.counts[typ], 0) - } - b.counts[typ][enc]++ -} - -func (b *blockStats) size(sz int) { - if b.min == 0 || sz < b.min { - b.min = sz - } - if b.min == 0 || sz > b.max { - b.max = sz - } -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/dumptsm/dumptsm_test.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/dumptsm/dumptsm_test.go deleted file mode 100644 index 6a01a65e..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/dumptsm/dumptsm_test.go +++ /dev/null @@ -1,3 +0,0 @@ -package dumptsm_test - -// TODO: write some tests diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/export/export.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/export/export.go deleted file mode 100644 index 9a928094..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/export/export.go +++ /dev/null @@ -1,407 +0,0 @@ -package export - -import ( - "bufio" - "compress/gzip" - "flag" - "fmt" - "io" - "math" - "os" - "path/filepath" - "sort" - "strconv" - "strings" - "sync" - "time" - - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/models" - "github.com/influxdata/influxdb/pkg/escape" - "github.com/influxdata/influxdb/tsdb/engine/tsm1" -) - -// Command represents the program execution for "influx_inspect export". -type Command struct { - // Standard input/output, overridden for testing. - Stderr io.Writer - Stdout io.Writer - - dataDir string - walDir string - out string - database string - retentionPolicy string - startTime int64 - endTime int64 - compress bool - - manifest map[string]struct{} - tsmFiles map[string][]string - walFiles map[string][]string -} - -// NewCommand returns a new instance of Command. -func NewCommand() *Command { - return &Command{ - Stderr: os.Stderr, - Stdout: os.Stdout, - - manifest: make(map[string]struct{}), - tsmFiles: make(map[string][]string), - walFiles: make(map[string][]string), - } -} - -// Run executes the command. -func (cmd *Command) Run(args ...string) error { - var start, end string - fs := flag.NewFlagSet("export", flag.ExitOnError) - fs.StringVar(&cmd.dataDir, "datadir", os.Getenv("HOME")+"/.influxdb/data", "Data storage path") - fs.StringVar(&cmd.walDir, "waldir", os.Getenv("HOME")+"/.influxdb/wal", "WAL storage path") - fs.StringVar(&cmd.out, "out", os.Getenv("HOME")+"/.influxdb/export", "Destination file to export to") - fs.StringVar(&cmd.database, "database", "", "Optional: the database to export") - fs.StringVar(&cmd.retentionPolicy, "retention", "", "Optional: the retention policy to export (requires -database)") - fs.StringVar(&start, "start", "", "Optional: the start time to export (RFC3339 format)") - fs.StringVar(&end, "end", "", "Optional: the end time to export (RFC3339 format)") - fs.BoolVar(&cmd.compress, "compress", false, "Compress the output") - - fs.SetOutput(cmd.Stdout) - fs.Usage = func() { - fmt.Fprintf(cmd.Stdout, "Exports TSM files into InfluxDB line protocol format.\n\n") - fmt.Fprintf(cmd.Stdout, "Usage: %s export [flags]\n\n", filepath.Base(os.Args[0])) - fs.PrintDefaults() - } - - if err := fs.Parse(args); err != nil { - return err - } - - // set defaults - if start != "" { - s, err := time.Parse(time.RFC3339, start) - if err != nil { - return err - } - cmd.startTime = s.UnixNano() - } else { - cmd.startTime = math.MinInt64 - } - if end != "" { - e, err := time.Parse(time.RFC3339, end) - if err != nil { - return err - } - cmd.endTime = e.UnixNano() - } else { - // set end time to max if it is not set. - cmd.endTime = math.MaxInt64 - } - - if err := cmd.validate(); err != nil { - return err - } - - return cmd.export() -} - -func (cmd *Command) validate() error { - if cmd.retentionPolicy != "" && cmd.database == "" { - return fmt.Errorf("must specify a db") - } - if cmd.startTime != 0 && cmd.endTime != 0 && cmd.endTime < cmd.startTime { - return fmt.Errorf("end time before start time") - } - return nil -} - -func (cmd *Command) export() error { - if err := cmd.walkTSMFiles(); err != nil { - return err - } - if err := cmd.walkWALFiles(); err != nil { - return err - } - return cmd.write() -} - -func (cmd *Command) walkTSMFiles() error { - return filepath.Walk(cmd.dataDir, func(path string, f os.FileInfo, err error) error { - if err != nil { - return err - } - - // check to see if this is a tsm file - if filepath.Ext(path) != "."+tsm1.TSMFileExtension { - return nil - } - - relPath, err := filepath.Rel(cmd.dataDir, path) - if err != nil { - return err - } - dirs := strings.Split(relPath, string(byte(os.PathSeparator))) - if len(dirs) < 2 { - return fmt.Errorf("invalid directory structure for %s", path) - } - if dirs[0] == cmd.database || cmd.database == "" { - if dirs[1] == cmd.retentionPolicy || cmd.retentionPolicy == "" { - key := filepath.Join(dirs[0], dirs[1]) - cmd.manifest[key] = struct{}{} - cmd.tsmFiles[key] = append(cmd.tsmFiles[key], path) - } - } - return nil - }) -} - -func (cmd *Command) walkWALFiles() error { - return filepath.Walk(cmd.walDir, func(path string, f os.FileInfo, err error) error { - if err != nil { - return err - } - - // check to see if this is a wal file - fileName := filepath.Base(path) - if filepath.Ext(path) != "."+tsm1.WALFileExtension || !strings.HasPrefix(fileName, tsm1.WALFilePrefix) { - return nil - } - - relPath, err := filepath.Rel(cmd.walDir, path) - if err != nil { - return err - } - dirs := strings.Split(relPath, string(byte(os.PathSeparator))) - if len(dirs) < 2 { - return fmt.Errorf("invalid directory structure for %s", path) - } - if dirs[0] == cmd.database || cmd.database == "" { - if dirs[1] == cmd.retentionPolicy || cmd.retentionPolicy == "" { - key := filepath.Join(dirs[0], dirs[1]) - cmd.manifest[key] = struct{}{} - cmd.walFiles[key] = append(cmd.walFiles[key], path) - } - } - return nil - }) -} - -func (cmd *Command) write() error { - // open our output file and create an output buffer - f, err := os.Create(cmd.out) - if err != nil { - return err - } - defer f.Close() - - // Because calling (*os.File).Write is relatively expensive, - // and we don't *need* to sync to disk on every written line of export, - // use a sized buffered writer so that we only sync the file every megabyte. - bw := bufio.NewWriterSize(f, 1024*1024) - defer bw.Flush() - - var w io.Writer = bw - - if cmd.compress { - gzw := gzip.NewWriter(w) - defer gzw.Close() - w = gzw - } - - s, e := time.Unix(0, cmd.startTime).Format(time.RFC3339), time.Unix(0, cmd.endTime).Format(time.RFC3339) - fmt.Fprintf(w, "# INFLUXDB EXPORT: %s - %s\n", s, e) - - // Write out all the DDL - fmt.Fprintln(w, "# DDL") - for key := range cmd.manifest { - keys := strings.Split(key, string(os.PathSeparator)) - db, rp := influxql.QuoteIdent(keys[0]), influxql.QuoteIdent(keys[1]) - fmt.Fprintf(w, "CREATE DATABASE %s WITH NAME %s\n", db, rp) - } - - fmt.Fprintln(w, "# DML") - for key := range cmd.manifest { - keys := strings.Split(key, string(os.PathSeparator)) - fmt.Fprintf(w, "# CONTEXT-DATABASE:%s\n", keys[0]) - fmt.Fprintf(w, "# CONTEXT-RETENTION-POLICY:%s\n", keys[1]) - if files, ok := cmd.tsmFiles[key]; ok { - fmt.Fprintf(cmd.Stdout, "writing out tsm file data for %s...", key) - if err := cmd.writeTsmFiles(w, files); err != nil { - return err - } - fmt.Fprintln(cmd.Stdout, "complete.") - } - if _, ok := cmd.walFiles[key]; ok { - fmt.Fprintf(cmd.Stdout, "writing out wal file data for %s...", key) - if err := cmd.writeWALFiles(w, cmd.walFiles[key], key); err != nil { - return err - } - fmt.Fprintln(cmd.Stdout, "complete.") - } - } - return nil -} - -func (cmd *Command) writeTsmFiles(w io.Writer, files []string) error { - fmt.Fprintln(w, "# writing tsm data") - - // we need to make sure we write the same order that the files were written - sort.Strings(files) - - for _, f := range files { - if err := cmd.exportTSMFile(f, w); err != nil { - return err - } - } - - return nil -} - -func (cmd *Command) exportTSMFile(tsmFilePath string, w io.Writer) error { - f, err := os.Open(tsmFilePath) - if err != nil { - return err - } - defer f.Close() - - r, err := tsm1.NewTSMReader(f) - if err != nil { - fmt.Fprintf(cmd.Stderr, "unable to read %s, skipping: %s\n", tsmFilePath, err.Error()) - return nil - } - defer r.Close() - - if sgStart, sgEnd := r.TimeRange(); sgStart > cmd.endTime || sgEnd < cmd.startTime { - return nil - } - - for i := 0; i < r.KeyCount(); i++ { - key, _ := r.KeyAt(i) - values, err := r.ReadAll(string(key)) - if err != nil { - fmt.Fprintf(cmd.Stderr, "unable to read key %q in %s, skipping: %s\n", string(key), tsmFilePath, err.Error()) - continue - } - measurement, field := tsm1.SeriesAndFieldFromCompositeKey(key) - field = escape.String(field) - - if err := cmd.writeValues(w, measurement, field, values); err != nil { - // An error from writeValues indicates an IO error, which should be returned. - return err - } - } - return nil -} - -func (cmd *Command) writeWALFiles(w io.Writer, files []string, key string) error { - fmt.Fprintln(w, "# writing wal data") - - // we need to make sure we write the same order that the wal received the data - sort.Strings(files) - - var once sync.Once - warnDelete := func() { - once.Do(func() { - msg := fmt.Sprintf(`WARNING: detected deletes in wal file. -Some series for %q may be brought back by replaying this data. -To resolve, you can either let the shard snapshot prior to exporting the data -or manually editing the exported file. - `, key) - fmt.Fprintln(cmd.Stderr, msg) - }) - } - - for _, f := range files { - if err := cmd.exportWALFile(f, w, warnDelete); err != nil { - return err - } - } - - return nil -} - -// exportWAL reads every WAL entry from r and exports it to w. -func (cmd *Command) exportWALFile(walFilePath string, w io.Writer, warnDelete func()) error { - f, err := os.Open(walFilePath) - if err != nil { - return err - } - defer f.Close() - - r := tsm1.NewWALSegmentReader(f) - defer r.Close() - - for r.Next() { - entry, err := r.Read() - if err != nil { - n := r.Count() - fmt.Fprintf(cmd.Stderr, "file %s corrupt at position %d", walFilePath, n) - break - } - - switch t := entry.(type) { - case *tsm1.DeleteWALEntry, *tsm1.DeleteRangeWALEntry: - warnDelete() - continue - case *tsm1.WriteWALEntry: - for key, values := range t.Values { - measurement, field := tsm1.SeriesAndFieldFromCompositeKey([]byte(key)) - // measurements are stored escaped, field names are not - field = escape.String(field) - - if err := cmd.writeValues(w, measurement, field, values); err != nil { - // An error from writeValues indicates an IO error, which should be returned. - return err - } - } - } - } - return nil -} - -// writeValues writes every value in values to w, using the given series key and field name. -// If any call to w.Write fails, that error is returned. -func (cmd *Command) writeValues(w io.Writer, seriesKey []byte, field string, values []tsm1.Value) error { - buf := []byte(string(seriesKey) + " " + field + "=") - prefixLen := len(buf) - - for _, value := range values { - ts := value.UnixNano() - if (ts < cmd.startTime) || (ts > cmd.endTime) { - continue - } - - // Re-slice buf to be " =". - buf = buf[:prefixLen] - - // Append the correct representation of the value. - switch v := value.Value().(type) { - case float64: - buf = strconv.AppendFloat(buf, v, 'g', -1, 64) - case int64: - buf = strconv.AppendInt(buf, v, 10) - buf = append(buf, 'i') - case bool: - buf = strconv.AppendBool(buf, v) - case string: - buf = append(buf, '"') - buf = append(buf, models.EscapeStringField(v)...) - buf = append(buf, '"') - default: - // This shouldn't be possible, but we'll format it anyway. - buf = append(buf, fmt.Sprintf("%v", v)...) - } - - // Now buf has " =". - // Append the timestamp and a newline, then write it. - buf = append(buf, ' ') - buf = strconv.AppendInt(buf, ts, 10) - buf = append(buf, '\n') - if _, err := w.Write(buf); err != nil { - // Underlying IO error needs to be returned. - return err - } - } - - return nil -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/export/export_test.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/export/export_test.go deleted file mode 100644 index d1a6598d..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/export/export_test.go +++ /dev/null @@ -1,337 +0,0 @@ -package export - -import ( - "bytes" - "fmt" - "io/ioutil" - "math" - "math/rand" - "os" - "sort" - "strconv" - "strings" - "testing" - - "github.com/golang/snappy" - "github.com/influxdata/influxdb/tsdb/engine/tsm1" -) - -type corpus map[string][]tsm1.Value - -var ( - basicCorpus = corpus{ - tsm1.SeriesFieldKey("floats,k=f", "f"): []tsm1.Value{ - tsm1.NewValue(1, float64(1.5)), - tsm1.NewValue(2, float64(3)), - }, - tsm1.SeriesFieldKey("ints,k=i", "i"): []tsm1.Value{ - tsm1.NewValue(10, int64(15)), - tsm1.NewValue(20, int64(30)), - }, - tsm1.SeriesFieldKey("bools,k=b", "b"): []tsm1.Value{ - tsm1.NewValue(100, true), - tsm1.NewValue(200, false), - }, - tsm1.SeriesFieldKey("strings,k=s", "s"): []tsm1.Value{ - tsm1.NewValue(1000, "1k"), - tsm1.NewValue(2000, "2k"), - }, - } - - basicCorpusExpLines = []string{ - "floats,k=f f=1.5 1", - "floats,k=f f=3 2", - "ints,k=i i=15i 10", - "ints,k=i i=30i 20", - "bools,k=b b=true 100", - "bools,k=b b=false 200", - `strings,k=s s="1k" 1000`, - `strings,k=s s="2k" 2000`, - } - - escapeStringCorpus = corpus{ - tsm1.SeriesFieldKey("t", "s"): []tsm1.Value{ - tsm1.NewValue(1, `1. "quotes"`), - tsm1.NewValue(2, `2. back\slash`), - tsm1.NewValue(3, `3. bs\q"`), - }, - } - - escCorpusExpLines = []string{ - `t s="1. \"quotes\"" 1`, - `t s="2. back\\slash" 2`, - `t s="3. bs\\q\"" 3`, - } -) - -func Test_exportWALFile(t *testing.T) { - for _, c := range []struct { - corpus corpus - lines []string - }{ - {corpus: basicCorpus, lines: basicCorpusExpLines}, - {corpus: escapeStringCorpus, lines: escCorpusExpLines}, - } { - walFile := writeCorpusToWALFile(c.corpus) - defer os.Remove(walFile.Name()) - - var out bytes.Buffer - if err := newCommand().exportWALFile(walFile.Name(), &out, func() {}); err != nil { - t.Fatal(err) - } - - lines := strings.Split(out.String(), "\n") - for _, exp := range c.lines { - found := false - for _, l := range lines { - if exp == l { - found = true - break - } - } - - if !found { - t.Fatalf("expected line %q to be in exported output:\n%s", exp, out.String()) - } - } - } -} - -func Test_exportTSMFile(t *testing.T) { - for _, c := range []struct { - corpus corpus - lines []string - }{ - {corpus: basicCorpus, lines: basicCorpusExpLines}, - {corpus: escapeStringCorpus, lines: escCorpusExpLines}, - } { - tsmFile := writeCorpusToTSMFile(c.corpus) - defer os.Remove(tsmFile.Name()) - - var out bytes.Buffer - if err := newCommand().exportTSMFile(tsmFile.Name(), &out); err != nil { - t.Fatal(err) - } - - lines := strings.Split(out.String(), "\n") - for _, exp := range c.lines { - found := false - for _, l := range lines { - if exp == l { - found = true - break - } - } - - if !found { - t.Fatalf("expected line %q to be in exported output:\n%s", exp, out.String()) - } - } - } -} - -var sink interface{} - -func benchmarkExportTSM(c corpus, b *testing.B) { - // Garbage collection is relatively likely to happen during export, so track allocations. - b.ReportAllocs() - - f := writeCorpusToTSMFile(c) - defer os.Remove(f.Name()) - - cmd := newCommand() - var out bytes.Buffer - b.ResetTimer() - b.StartTimer() - for i := 0; i < b.N; i++ { - if err := cmd.exportTSMFile(f.Name(), &out); err != nil { - b.Fatal(err) - } - - sink = out.Bytes() - out.Reset() - } -} - -func BenchmarkExportTSMFloats_100s_250vps(b *testing.B) { - benchmarkExportTSM(makeFloatsCorpus(100, 250), b) -} - -func BenchmarkExportTSMInts_100s_250vps(b *testing.B) { - benchmarkExportTSM(makeIntsCorpus(100, 250), b) -} - -func BenchmarkExportTSMBools_100s_250vps(b *testing.B) { - benchmarkExportTSM(makeBoolsCorpus(100, 250), b) -} - -func BenchmarkExportTSMStrings_100s_250vps(b *testing.B) { - benchmarkExportTSM(makeStringsCorpus(100, 250), b) -} - -func benchmarkExportWAL(c corpus, b *testing.B) { - // Garbage collection is relatively likely to happen during export, so track allocations. - b.ReportAllocs() - - f := writeCorpusToWALFile(c) - defer os.Remove(f.Name()) - - cmd := newCommand() - var out bytes.Buffer - b.ResetTimer() - b.StartTimer() - for i := 0; i < b.N; i++ { - if err := cmd.exportWALFile(f.Name(), &out, func() {}); err != nil { - b.Fatal(err) - } - - sink = out.Bytes() - out.Reset() - } -} - -func BenchmarkExportWALFloats_100s_250vps(b *testing.B) { - benchmarkExportWAL(makeFloatsCorpus(100, 250), b) -} - -func BenchmarkExportWALInts_100s_250vps(b *testing.B) { - benchmarkExportWAL(makeIntsCorpus(100, 250), b) -} - -func BenchmarkExportWALBools_100s_250vps(b *testing.B) { - benchmarkExportWAL(makeBoolsCorpus(100, 250), b) -} - -func BenchmarkExportWALStrings_100s_250vps(b *testing.B) { - benchmarkExportWAL(makeStringsCorpus(100, 250), b) -} - -// newCommand returns a command that discards its output and that accepts all timestamps. -func newCommand() *Command { - return &Command{ - Stderr: ioutil.Discard, - Stdout: ioutil.Discard, - startTime: math.MinInt64, - endTime: math.MaxInt64, - } -} - -// makeCorpus returns a new corpus filled with values generated by fn. -// The RNG passed to fn is seeded with numSeries * numValuesPerSeries, for predictable output. -func makeCorpus(numSeries, numValuesPerSeries int, fn func(*rand.Rand) interface{}) corpus { - rng := rand.New(rand.NewSource(int64(numSeries) * int64(numValuesPerSeries))) - var unixNano int64 - corpus := make(corpus, numSeries) - for i := 0; i < numSeries; i++ { - vals := make([]tsm1.Value, numValuesPerSeries) - for j := 0; j < numValuesPerSeries; j++ { - vals[j] = tsm1.NewValue(unixNano, fn(rng)) - unixNano++ - } - - k := fmt.Sprintf("m,t=%d", i) - corpus[tsm1.SeriesFieldKey(k, "x")] = vals - } - - return corpus -} - -func makeFloatsCorpus(numSeries, numFloatsPerSeries int) corpus { - return makeCorpus(numSeries, numFloatsPerSeries, func(rng *rand.Rand) interface{} { - return rng.Float64() - }) -} - -func makeIntsCorpus(numSeries, numIntsPerSeries int) corpus { - return makeCorpus(numSeries, numIntsPerSeries, func(rng *rand.Rand) interface{} { - // This will only return positive integers. That's probably okay. - return rng.Int63() - }) -} - -func makeBoolsCorpus(numSeries, numBoolsPerSeries int) corpus { - return makeCorpus(numSeries, numBoolsPerSeries, func(rng *rand.Rand) interface{} { - return rand.Int63n(2) == 1 - }) -} - -func makeStringsCorpus(numSeries, numStringsPerSeries int) corpus { - return makeCorpus(numSeries, numStringsPerSeries, func(rng *rand.Rand) interface{} { - // The string will randomly have 2-6 parts - parts := make([]string, rand.Intn(4)+2) - - for i := range parts { - // Each part is a random base36-encoded number - parts[i] = strconv.FormatInt(rand.Int63(), 36) - } - - // Join the individual parts with underscores. - return strings.Join(parts, "_") - }) -} - -// writeCorpusToWALFile writes the given corpus as a WAL file, and returns a handle to that file. -// It is the caller's responsibility to remove the returned temp file. -// writeCorpusToWALFile will panic on any error that occurs. -func writeCorpusToWALFile(c corpus) *os.File { - walFile, err := ioutil.TempFile("", "export_test_corpus_wal") - if err != nil { - panic(err) - } - - e := &tsm1.WriteWALEntry{Values: c} - b, err := e.Encode(nil) - if err != nil { - panic(err) - } - - w := tsm1.NewWALSegmentWriter(walFile) - if err := w.Write(e.Type(), snappy.Encode(nil, b)); err != nil { - panic(err) - } - - // (*tsm1.WALSegmentWriter).sync isn't exported, but it only Syncs the file anyway. - if err := walFile.Sync(); err != nil { - panic(err) - } - - return walFile -} - -// writeCorpusToTSMFile writes the given corpus as a TSM file, and returns a handle to that file. -// It is the caller's responsibility to remove the returned temp file. -// writeCorpusToTSMFile will panic on any error that occurs. -func writeCorpusToTSMFile(c corpus) *os.File { - tsmFile, err := ioutil.TempFile("", "export_test_corpus_tsm") - if err != nil { - panic(err) - } - - w, err := tsm1.NewTSMWriter(tsmFile) - if err != nil { - panic(err) - } - - // Write the series in alphabetical order so that each test run is comparable, - // given an identical corpus. - keys := make([]string, 0, len(c)) - for k := range c { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - if err := w.Write(k, c[k]); err != nil { - panic(err) - } - } - - if err := w.WriteIndex(); err != nil { - panic(err) - } - - if err := w.Close(); err != nil { - panic(err) - } - - return tsmFile -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/help/help.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/help/help.go deleted file mode 100644 index ee238c39..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/help/help.go +++ /dev/null @@ -1,42 +0,0 @@ -package help - -import ( - "fmt" - "io" - "os" - "strings" -) - -// Command displays help for command-line sub-commands. -type Command struct { - Stdout io.Writer -} - -// NewCommand returns a new instance of Command. -func NewCommand() *Command { - return &Command{ - Stdout: os.Stdout, - } -} - -// Run executes the command. -func (cmd *Command) Run(args ...string) error { - fmt.Fprintln(cmd.Stdout, strings.TrimSpace(usage)) - return nil -} - -const usage = ` -Usage: influx_inspect [[command] [arguments]] - -The commands are: - - dumptsm dumps low-level details about tsm1 files. - export exports raw data from a shard to line protocol - help display this help message - report displays a shard level report - verify verifies integrity of TSM files - -"help" is the default command. - -Use "influx_inspect [command] -help" for more information about a command. -` diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/help/help_test.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/help/help_test.go deleted file mode 100644 index 31d16324..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/help/help_test.go +++ /dev/null @@ -1,3 +0,0 @@ -package help_test - -// TODO: write some tests diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/main.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/main.go deleted file mode 100644 index 028d626b..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/main.go +++ /dev/null @@ -1,84 +0,0 @@ -package main - -import ( - "fmt" - "io" - "log" - "os" - - "github.com/influxdata/influxdb/cmd" - "github.com/influxdata/influxdb/cmd/influx_inspect/dumptsm" - "github.com/influxdata/influxdb/cmd/influx_inspect/export" - "github.com/influxdata/influxdb/cmd/influx_inspect/help" - "github.com/influxdata/influxdb/cmd/influx_inspect/report" - "github.com/influxdata/influxdb/cmd/influx_inspect/verify" - _ "github.com/influxdata/influxdb/tsdb/engine" -) - -func main() { - - m := NewMain() - if err := m.Run(os.Args[1:]...); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } -} - -// Main represents the program execution. -type Main struct { - Logger *log.Logger - - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer -} - -// NewMain return a new instance of Main. -func NewMain() *Main { - return &Main{ - Logger: log.New(os.Stderr, "[influx_inspect] ", log.LstdFlags), - Stdin: os.Stdin, - Stdout: os.Stdout, - Stderr: os.Stderr, - } -} - -// Run determines and runs the command specified by the CLI args. -func (m *Main) Run(args ...string) error { - name, args := cmd.ParseCommandName(args) - - // Extract name from args. - switch name { - case "", "help": - if err := help.NewCommand().Run(args...); err != nil { - return fmt.Errorf("help: %s", err) - } - case "dumptsmdev": - fmt.Fprintf(m.Stderr, "warning: dumptsmdev is deprecated, use dumptsm instead.\n") - fallthrough - case "dumptsm": - name := dumptsm.NewCommand() - if err := name.Run(args...); err != nil { - return fmt.Errorf("dumptsm: %s", err) - } - case "export": - name := export.NewCommand() - if err := name.Run(args...); err != nil { - return fmt.Errorf("export: %s", err) - } - case "report": - name := report.NewCommand() - if err := name.Run(args...); err != nil { - return fmt.Errorf("report: %s", err) - } - case "verify": - name := verify.NewCommand() - if err := name.Run(args...); err != nil { - return fmt.Errorf("verify: %s", err) - } - default: - return fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'influx_inspect help' for usage`+"\n\n", name) - } - - return nil -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/report/report.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/report/report.go deleted file mode 100644 index 248250c6..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/report/report.go +++ /dev/null @@ -1,184 +0,0 @@ -package report - -import ( - "flag" - "fmt" - "io" - "os" - "path/filepath" - "strconv" - "strings" - "text/tabwriter" - "time" - - "github.com/influxdata/influxdb/models" - "github.com/influxdata/influxdb/tsdb/engine/tsm1" - "github.com/retailnext/hllpp" -) - -// Command represents the program execution for "influxd report". -type Command struct { - Stderr io.Writer - Stdout io.Writer - - dir string - pattern string - detailed bool -} - -// NewCommand returns a new instance of Command. -func NewCommand() *Command { - return &Command{ - Stderr: os.Stderr, - Stdout: os.Stdout, - } -} - -// Run executes the command. -func (cmd *Command) Run(args ...string) error { - fs := flag.NewFlagSet("report", flag.ExitOnError) - fs.StringVar(&cmd.pattern, "pattern", "", "Include only files matching a pattern") - fs.BoolVar(&cmd.detailed, "detailed", false, "Report detailed cardinality estimates") - - fs.SetOutput(cmd.Stdout) - fs.Usage = cmd.printUsage - - if err := fs.Parse(args); err != nil { - return err - } - cmd.dir = fs.Arg(0) - - start := time.Now() - - files, err := filepath.Glob(filepath.Join(cmd.dir, fmt.Sprintf("*.%s", tsm1.TSMFileExtension))) - if err != nil { - return err - } - - var filtered []string - if cmd.pattern != "" { - for _, f := range files { - if strings.Contains(f, cmd.pattern) { - filtered = append(filtered, f) - } - } - files = filtered - } - - if len(files) == 0 { - return fmt.Errorf("no tsm files at %v\n", cmd.dir) - } - - tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) - fmt.Fprintln(tw, strings.Join([]string{"File", "Series", "Load Time"}, "\t")) - - totalSeries := hllpp.New() - tagCardialities := map[string]*hllpp.HLLPP{} - measCardinalities := map[string]*hllpp.HLLPP{} - fieldCardinalities := map[string]*hllpp.HLLPP{} - - ordering := make([]chan struct{}, 0, len(files)) - for range files { - ordering = append(ordering, make(chan struct{})) - } - - for _, f := range files { - file, err := os.OpenFile(f, os.O_RDONLY, 0600) - if err != nil { - fmt.Fprintf(cmd.Stderr, "error: %s: %v. Skipping.\n", f, err) - continue - } - - loadStart := time.Now() - reader, err := tsm1.NewTSMReader(file) - if err != nil { - fmt.Fprintf(cmd.Stderr, "error: %s: %v. Skipping.\n", file.Name(), err) - continue - } - loadTime := time.Since(loadStart) - - seriesCount := reader.KeyCount() - for i := 0; i < seriesCount; i++ { - key, _ := reader.KeyAt(i) - totalSeries.Add([]byte(key)) - - if cmd.detailed { - sep := strings.Index(string(key), "#!~#") - seriesKey, field := key[:sep], key[sep+4:] - measurement, tags, _ := models.ParseKey(seriesKey) - - measCount, ok := measCardinalities[measurement] - if !ok { - measCount = hllpp.New() - measCardinalities[measurement] = measCount - } - measCount.Add([]byte(key)) - - fieldCount, ok := fieldCardinalities[measurement] - if !ok { - fieldCount = hllpp.New() - fieldCardinalities[measurement] = fieldCount - } - fieldCount.Add([]byte(field)) - - for _, t := range tags { - tagCount, ok := tagCardialities[string(t.Key)] - if !ok { - tagCount = hllpp.New() - tagCardialities[string(t.Key)] = tagCount - } - tagCount.Add(t.Value) - } - } - } - reader.Close() - - fmt.Fprintln(tw, strings.Join([]string{ - filepath.Base(file.Name()), - strconv.FormatInt(int64(seriesCount), 10), - loadTime.String(), - }, "\t")) - tw.Flush() - } - - tw.Flush() - println() - fmt.Printf("Statistics\n") - fmt.Printf(" Series:\n") - fmt.Printf(" Total (est): %d\n", totalSeries.Count()) - if cmd.detailed { - fmt.Printf(" Measurements (est):\n") - for t, card := range measCardinalities { - fmt.Printf(" %v: %d (%d%%)\n", t, card.Count(), int((float64(card.Count())/float64(totalSeries.Count()))*100)) - } - - fmt.Printf(" Fields (est):\n") - for t, card := range fieldCardinalities { - fmt.Printf(" %v: %d\n", t, card.Count()) - } - - fmt.Printf(" Tags (est):\n") - for t, card := range tagCardialities { - fmt.Printf(" %v: %d\n", t, card.Count()) - } - } - - fmt.Printf("Completed in %s\n", time.Since(start)) - return nil -} - -// printUsage prints the usage message to STDERR. -func (cmd *Command) printUsage() { - usage := `Displays shard level report. - -Usage: influx_inspect report [flags] - - -pattern - Include only files matching a pattern. - -detailed - Report detailed cardinality estimates. - Defaults to "false". -` - - fmt.Fprintf(cmd.Stdout, usage) -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/report/report_test.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/report/report_test.go deleted file mode 100644 index 3a6ba2c1..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/report/report_test.go +++ /dev/null @@ -1,3 +0,0 @@ -package report_test - -// TODO: write some tests diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/verify/verify.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/verify/verify.go deleted file mode 100644 index fb9ec5c7..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/verify/verify.go +++ /dev/null @@ -1,119 +0,0 @@ -package verify - -import ( - "flag" - "fmt" - "hash/crc32" - "io" - "os" - "path/filepath" - "text/tabwriter" - "time" - - "github.com/influxdata/influxdb/tsdb/engine/tsm1" -) - -// Command represents the program execution for "influx_inspect verify". -type Command struct { - Stderr io.Writer - Stdout io.Writer -} - -// NewCommand returns a new instance of Command. -func NewCommand() *Command { - return &Command{ - Stderr: os.Stderr, - Stdout: os.Stdout, - } -} - -// Run executes the command. -func (cmd *Command) Run(args ...string) error { - var path string - fs := flag.NewFlagSet("verify", flag.ExitOnError) - fs.StringVar(&path, "dir", os.Getenv("HOME")+"/.influxdb", "Root storage path. [$HOME/.influxdb]") - - fs.SetOutput(cmd.Stdout) - fs.Usage = cmd.printUsage - - if err := fs.Parse(args); err != nil { - return err - } - - start := time.Now() - dataPath := filepath.Join(path, "data") - - brokenBlocks := 0 - totalBlocks := 0 - - // No need to do this in a loop - ext := fmt.Sprintf(".%s", tsm1.TSMFileExtension) - - // Get all TSM files by walking through the data dir - files := []string{} - err := filepath.Walk(dataPath, func(path string, f os.FileInfo, err error) error { - if err != nil { - return err - } - if filepath.Ext(path) == ext { - files = append(files, path) - } - return nil - }) - if err != nil { - panic(err) - } - - tw := tabwriter.NewWriter(cmd.Stdout, 16, 8, 0, '\t', 0) - - // Verify the checksums of every block in every file - for _, f := range files { - file, err := os.OpenFile(f, os.O_RDONLY, 0600) - if err != nil { - return err - } - - reader, err := tsm1.NewTSMReader(file) - if err != nil { - return err - } - - blockItr := reader.BlockIterator() - brokenFileBlocks := 0 - count := 0 - for blockItr.Next() { - totalBlocks++ - key, _, _, checksum, buf, err := blockItr.Read() - if err != nil { - brokenBlocks++ - fmt.Fprintf(tw, "%s: could not get checksum for key %v block %d due to error: %q\n", f, key, count, err) - } else if expected := crc32.ChecksumIEEE(buf); checksum != expected { - brokenBlocks++ - fmt.Fprintf(tw, "%s: got %d but expected %d for key %v, block %d\n", f, checksum, expected, key, count) - } - count++ - } - if brokenFileBlocks == 0 { - fmt.Fprintf(tw, "%s: healthy\n", f) - } - reader.Close() - } - - fmt.Fprintf(tw, "Broken Blocks: %d / %d, in %vs\n", brokenBlocks, totalBlocks, time.Since(start).Seconds()) - tw.Flush() - return nil -} - -// printUsage prints the usage message to STDERR. -func (cmd *Command) printUsage() { - usage := fmt.Sprintf(`Verifies the integrity of TSM files. - -Usage: influx_inspect verify [flags] - - -dir - Root storage path - Defaults to "%[1]s/.influxdb". - `, os.Getenv("HOME")) - - fmt.Fprintf(cmd.Stdout, usage) -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/verify/verify_test.go b/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/verify/verify_test.go deleted file mode 100644 index c21ecdce..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_inspect/verify/verify_test.go +++ /dev/null @@ -1,3 +0,0 @@ -package verify_test - -// TODO: write some tests diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_stress/README.md b/vendor/github.com/influxdata/influxdb/cmd/influx_stress/README.md deleted file mode 100644 index c4036331..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_stress/README.md +++ /dev/null @@ -1,43 +0,0 @@ -# `influx_stress` - -If you run into any issues with this tool please mention @jackzampolin when you create an issue. - -## Ways to run - -### `influx_stress` -This runs a basic stress test with the [default config](https://github.com/influxdata/influxdb/blob/master/stress/stress.toml) For more information on the configuration file please see the default. - -### `influx_stress -config someConfig.toml` -This runs the stress test with a valid configuration file located at `someConfig.tom` - -### `influx_stress -v2 -config someConfig.iql` -This runs the stress test with a valid `v2` configuration file. For more information about the `v2` stress test see the [v2 stress README](https://github.com/influxdata/influxdb/blob/master/stress/v2/README.md). - -## Flags - -If flags are defined they overwrite the config from any file passed in. - -### `-addr` string -IP address and port of database where response times will persist (e.g., localhost:8086) - -`default` = "http://localhost:8086" - -### `-config` string -The relative path to the stress test configuration file. - -`default` = [config](https://github.com/influxdata/influxdb/blob/master/stress/stress.toml) - -### `-cpuprofile` filename -Writes the result of Go's cpu profile to filename - -`default` = no profiling - -### `-database` string -Name of database on `-addr` that `influx_stress` will persist write and query response times - -`default` = "stress" - -### `-tags` value -A comma separated list of tags to add to write and query response times. - -`default` = "" diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_stress/examples/template.toml b/vendor/github.com/influxdata/influxdb/cmd/influx_stress/examples/template.toml deleted file mode 100644 index 08be3399..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_stress/examples/template.toml +++ /dev/null @@ -1,92 +0,0 @@ -# This section can be removed -[provision] - # The basic provisioner simply deletes and creates database. - # If `reset_database` is false, it will not attempt to delete the database - [provision.basic] - # If enabled the provisioner will actually run - enabled = true - # Address of the instance that is to be provisioned - address = "localhost:8086" - # Database the will be created/deleted - database = "stress" - # Attempt to delete database - reset_database = true - -# This section cannot be commented out -# To prevent writes set `enabled=false` -# in [write.influx_client.basic] -[write] - [write.point_generator] - # The basic point generator will generate points of the form - # `cpu,host=server-%v,location=us-west value=234 123456` - [write.point_generator.basic] - # number of points that will be written for each of the series - point_count = 100 - # number of series - series_count = 100000 - # How much time between each timestamp - tick = "10s" - # Randomize timestamp a bit (not functional) - jitter = true - # Precision of points that are being written - precision = "s" - # name of the measurement that will be written - measurement = "cpu" - # The date for the first point that is written into influx - start_date = "2006-Jan-02" - # Defines a tag for a series - [[write.point_generator.basic.tag]] - key = "host" - value = "server" - [[write.point_generator.basic.tag]] - key = "location" - value = "us-west" - # Defines a field for a series - [[write.point_generator.basic.field]] - key = "value" - value = "float64" # supported types: float64, int, bool - - - [write.influx_client] - [write.influx_client.basic] - # If enabled the writer will actually write - enabled = true - # Addresses is an array of the Influxdb instances - addresses = ["localhost:8086"] # stress_test_server runs on port 1234 - # Database that is being written to - database = "stress" - # Precision of points that are being written - precision = "s" - # Size of batches that are sent to db - batch_size = 10000 - # Interval between each batch - batch_interval = "0s" - # How many concurrent writers to the db - concurrency = 10 - # ssl enabled? - ssl = false - # format of points that are written to influxdb - format = "line_http" # line_udp (not supported yet), graphite_tcp (not supported yet), graphite_udp (not supported yet) - -# This section can be removed -[read] - [read.query_generator] - [read.query_generator.basic] - # Template of the query that will be ran against the instance - template = "SELECT count(value) FROM cpu where host='server-%v'" - # How many times the templated query will be ran - query_count = 250 - - [read.query_client] - [read.query_client.basic] - # if enabled the reader will actually read - enabled = true - # Address of the instance that will be queried - addresses = ["localhost:8086"] - # Database that will be queried - database = "stress" - # Interval bewteen queries - query_interval = "100ms" - # Number of concurrent queriers - concurrency = 1 - diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_stress/influx_stress.go b/vendor/github.com/influxdata/influxdb/cmd/influx_stress/influx_stress.go deleted file mode 100644 index 31c2b167..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_stress/influx_stress.go +++ /dev/null @@ -1,70 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "log" - "os" - "runtime/pprof" - - "github.com/influxdata/influxdb/stress" - v2 "github.com/influxdata/influxdb/stress/v2" -) - -var ( - useV2 = flag.Bool("v2", false, "Use version 2 of stress tool") - config = flag.String("config", "", "The stress test file") - cpuprofile = flag.String("cpuprofile", "", "Write the cpu profile to `filename`") - db = flag.String("db", "", "target database within test system for write and query load") -) - -func main() { - o := stress.NewOutputConfig() - flag.Parse() - - if *cpuprofile != "" { - f, err := os.Create(*cpuprofile) - if err != nil { - fmt.Println(err) - return - } - pprof.StartCPUProfile(f) - defer pprof.StopCPUProfile() - } - - if *useV2 { - if *config != "" { - v2.RunStress(*config) - } else { - v2.RunStress("stress/v2/iql/file.iql") - } - } else { - - c, err := stress.NewConfig(*config) - if err != nil { - log.Fatal(err) - return - } - - if *db != "" { - c.Provision.Basic.Database = *db - c.Write.InfluxClients.Basic.Database = *db - c.Read.QueryClients.Basic.Database = *db - } - - w := stress.NewWriter(c.Write.PointGenerators.Basic, &c.Write.InfluxClients.Basic) - r := stress.NewQuerier(&c.Read.QueryGenerators.Basic, &c.Read.QueryClients.Basic) - s := stress.NewStressTest(&c.Provision.Basic, w, r) - - bw := stress.NewBroadcastChannel() - bw.Register(c.Write.InfluxClients.Basic.BasicWriteHandler) - bw.Register(o.HTTPHandler("write")) - - br := stress.NewBroadcastChannel() - br.Register(c.Read.QueryClients.Basic.BasicReadHandler) - br.Register(o.HTTPHandler("read")) - - s.Start(bw.Handle, br.Handle) - - } -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/README.md b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/README.md deleted file mode 100644 index d63c152a..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/README.md +++ /dev/null @@ -1,152 +0,0 @@ -# Converting b1 and bz1 shards to tsm1 - -`influx_tsm` is a tool for converting b1 and bz1 shards to tsm1 -format. Converting shards to tsm1 format results in a very significant -reduction in disk usage, and significantly improved write-throughput, -when writing data into those shards. - -Conversion can be controlled on a database-by-database basis. By -default a database is backed up before it is converted, allowing you -to roll back any changes. Because of the backup process, ensure the -host system has at least as much free disk space as the disk space -consumed by the _data_ directory of your InfluxDB system. - -The tool automatically ignores tsm1 shards, and can be run -idempotently on any database. - -Conversion is an offline process, and the InfluxDB system must be -stopped during conversion. However the conversion process reads and -writes shards directly on disk and should be fast. - -## Steps - -Follow these steps to perform a conversion. - -* Identify the databases you wish to convert. You can convert one or more databases at a time. By default all databases are converted. -* Decide on parallel operation. By default the conversion operation peforms each operation in a serial manner. This minimizes load on the host system performing the conversion, but also takes the most time. If you wish to minimize the time conversion takes, enable parallel mode. Conversion will then perform as many operations as possible in parallel, but the process may place significant load on the host system (CPU, disk, and RAM, usage will all increase). -* Stop all write-traffic to your InfluxDB system. -* Restart the InfluxDB service and wait until all WAL data is flushed to disk -- this has completed when the system responds to queries. This is to ensure all data is present in shards. -* Stop the InfluxDB service. It should not be restarted until conversion is complete. -* Run conversion tool. Depending on the size of the data directory, this might be a lengthy operation. Consider running the conversion tool under a "screen" session to avoid any interruptions. -* Unless you ran the conversion tool as the same user as that which runs InfluxDB, then you may need to set the correct read-and-write permissions on the new tsm1 directories. -* Restart node and ensure data looks correct. -* If everything looks OK, you may then wish to remove or archive the backed-up databases. -* Restart write traffic. - -## Example session - -Below is an example session, showing a database being converted. - -``` -$ # Create a backup location that the `influxdb` user has full access to -$ mkdir -m 0777 /path/to/influxdb_backup -$ sudo -u influxdb influx_tsm -backup /path/to/influxdb_backup -parallel /var/lib/influxdb/data - -b1 and bz1 shard conversion. ------------------------------------ -Data directory is: /var/lib/influxdb/data -Backup directory is: /path/to/influxdb_backup -Databases specified: all -Database backups enabled: yes -Parallel mode enabled (GOMAXPROCS): yes (8) - - -Found 1 shards that will be converted. - -Database Retention Path Engine Size -_internal monitor /var/lib/influxdb/data/_internal/monitor/1 bz1 65536 - -These shards will be converted. Proceed? y/N: y -Conversion starting.... -Backing up 1 databases... -2016/01/28 12:23:43.699266 Backup of databse '_internal' started -2016/01/28 12:23:43.699883 Backing up file /var/lib/influxdb/data/_internal/monitor/1 -2016/01/28 12:23:43.700052 Database _internal backed up (851.776µs) -2016/01/28 12:23:43.700320 Starting conversion of shard: /var/lib/influxdb/data/_internal/monitor/1 -2016/01/28 12:23:43.706276 Conversion of /var/lib/influxdb/data/_internal/monitor/1 successful (6.040148ms) - -Summary statistics -======================================== -Databases converted: 1 -Shards converted: 1 -TSM files created: 1 -Points read: 369 -Points written: 369 -NaN filtered: 0 -Inf filtered: 0 -Points without fields filtered: 0 -Disk usage pre-conversion (bytes): 65536 -Disk usage post-conversion (bytes): 11000 -Reduction factor: 83% -Bytes per TSM point: 29.81 -Total conversion time: 7.330443ms - -$ # restart node, verify data -$ sudo rm -r /path/to/influxdb_backup -``` - -Note that the tool first lists the shards that will be converted, -before asking for confirmation. You can abort the conversion process -at this step if you just wish to see what would be converted, or if -the list of shards does not look correct. - -__WARNING:__ If you run the `influx_tsm` tool as a user other than the -`influxdb` user (or the user that the InfluxDB process runs under), -please make sure to verify the shard permissions are correct prior to -starting InfluxDB. If needed, shard permissions can be corrected with -the `chown` command. For example: - -``` -sudo chown -R influxdb:influxdb /var/lib/influxdb -``` - -## Rolling back a conversion - -After a successful backup (the message `Database XYZ backed up` was -logged), you have a duplicate of that database in the _backup_ -directory you provided on the command line. If, when checking your -data after a successful conversion, you notice things missing or -something just isn't right, you can "undo" the conversion: - -- Shut down your node (this is very important) -- Remove the database's directory from the influxdb `data` directory (default: `~/.influxdb/data/XYZ` for binary installations or `/var/lib/influxdb/data/XYZ` for packaged installations) -- Copy (to really make sure the shard is preserved) the database's directory from the backup directory you created into the `data` directory. - -Using the same directories as above, and assuming a database named `stats`: - -``` -$ sudo rm -r /var/lib/influxdb/data/stats -$ sudo cp -r /path/to/influxdb_backup/stats /var/lib/influxdb/data/ -$ # restart influxd node -``` - -#### How to avoid downtime when upgrading shards - -*Identify non-`tsm1` shards* - -Non-`tsm1` shards are files of the form: `data///`. - -`tsm1` shards are files of the form: `data////.tsm`. - -*Determine which `bz`/`bz1` shards are cold for writes* - -Run the `SHOW SHARDS` query to see the start and end dates for shards. -If the date range for a shard does not span the current time then the shard is said to be cold for writes. -This means that no new points are expected to be added to the shard. -The shard whose date range spans now is said to be hot for writes. -You can only safely convert cold shards without stopping the InfluxDB process. - -*Convert cold shards* - -1. Copy each of the cold shards you'd like to convert to a new directory with the structure `/tmp/data///`. -2. Run the `influx_tsm` tool on the copied files: -``` -influx_tsm -parallel /tmp/data/ -``` -3. Remove the existing cold `b1`/`bz1` shards from the production data directory. -4. Move the new `tsm1` shards into the original directory, overwriting the existing `b1`/`bz1` shards of the same name. Do this simultaneously with step 3 to avoid any query errors. -5. Wait an hour, a day, or a week (depending on your retention period) for any hot `b1`/`bz1` shards to become cold and repeat steps 1 through 4 on the newly cold shards. - -> **Note:** Any points written to the cold shards after making a copy will be lost when the `tsm1` shard overwrites the existing cold shard. -Nothing in InfluxDB will prevent writes to cold shards, they are merely unexpected, not impossible. -It is your responsibility to prevent writes to cold shards to prevent data loss. diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/b1/reader.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/b1/reader.go deleted file mode 100644 index 50e41b06..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/b1/reader.go +++ /dev/null @@ -1,269 +0,0 @@ -package b1 // import "github.com/influxdata/influxdb/cmd/influx_tsm/b1" - -import ( - "encoding/binary" - "math" - "sort" - "time" - - "github.com/boltdb/bolt" - "github.com/influxdata/influxdb/cmd/influx_tsm/stats" - "github.com/influxdata/influxdb/cmd/influx_tsm/tsdb" - "github.com/influxdata/influxdb/tsdb/engine/tsm1" -) - -// DefaultChunkSize is the size of chunks read from the b1 shard -const DefaultChunkSize int = 1000 - -var excludedBuckets = map[string]bool{ - "fields": true, - "meta": true, - "series": true, - "wal": true, -} - -// Reader is used to read all data from a b1 shard. -type Reader struct { - path string - db *bolt.DB - tx *bolt.Tx - - cursors []*cursor - currCursor int - - keyBuf string - values []tsm1.Value - valuePos int - - fields map[string]*tsdb.MeasurementFields - codecs map[string]*tsdb.FieldCodec - - stats *stats.Stats -} - -// NewReader returns a reader for the b1 shard at path. -func NewReader(path string, stats *stats.Stats, chunkSize int) *Reader { - r := &Reader{ - path: path, - fields: make(map[string]*tsdb.MeasurementFields), - codecs: make(map[string]*tsdb.FieldCodec), - stats: stats, - } - - if chunkSize <= 0 { - chunkSize = DefaultChunkSize - } - - r.values = make([]tsm1.Value, chunkSize) - - return r -} - -// Open opens the reader. -func (r *Reader) Open() error { - // Open underlying storage. - db, err := bolt.Open(r.path, 0666, &bolt.Options{Timeout: 1 * time.Second}) - if err != nil { - return err - } - r.db = db - - // Load fields. - if err := r.db.View(func(tx *bolt.Tx) error { - meta := tx.Bucket([]byte("fields")) - c := meta.Cursor() - - for k, v := c.First(); k != nil; k, v = c.Next() { - mf := &tsdb.MeasurementFields{} - if err := mf.UnmarshalBinary(v); err != nil { - return err - } - r.fields[string(k)] = mf - r.codecs[string(k)] = tsdb.NewFieldCodec(mf.Fields) - } - return nil - }); err != nil { - return err - } - - seriesSet := make(map[string]bool) - - // ignore series index and find all series in this shard - if err := r.db.View(func(tx *bolt.Tx) error { - tx.ForEach(func(name []byte, _ *bolt.Bucket) error { - key := string(name) - if !excludedBuckets[key] { - seriesSet[key] = true - } - return nil - }) - return nil - }); err != nil { - return err - } - - r.tx, err = r.db.Begin(false) - if err != nil { - return err - } - - // Create cursor for each field of each series. - for s := range seriesSet { - measurement := tsdb.MeasurementFromSeriesKey(s) - fields := r.fields[measurement] - if fields == nil { - r.stats.IncrFiltered() - continue - } - for _, f := range fields.Fields { - c := newCursor(r.tx, s, f.Name, r.codecs[measurement]) - c.SeekTo(0) - r.cursors = append(r.cursors, c) - } - } - sort.Sort(cursors(r.cursors)) - - return nil -} - -// Next returns whether any data remains to be read. It must be called before -// the next call to Read(). -func (r *Reader) Next() bool { - r.valuePos = 0 -OUTER: - for { - if r.currCursor >= len(r.cursors) { - // All cursors drained. No more data remains. - return false - } - - cc := r.cursors[r.currCursor] - r.keyBuf = tsm1.SeriesFieldKey(cc.series, cc.field) - - for { - k, v := cc.Next() - if k == -1 { - // Go to next cursor and try again. - r.currCursor++ - if r.valuePos == 0 { - // The previous cursor had no data. Instead of returning - // just go immediately to the next cursor. - continue OUTER - } - // There is some data available. Indicate that it should be read. - return true - } - - if f, ok := v.(float64); ok { - if math.IsInf(f, 0) { - r.stats.AddPointsRead(1) - r.stats.IncrInf() - continue - } - - if math.IsNaN(f) { - r.stats.AddPointsRead(1) - r.stats.IncrNaN() - continue - } - } - - r.values[r.valuePos] = tsm1.NewValue(k, v) - r.valuePos++ - - if r.valuePos >= len(r.values) { - return true - } - } - } -} - -// Read returns the next chunk of data in the shard, converted to tsm1 values. Data is -// emitted completely for every field, in every series, before the next field is processed. -// Data from Read() adheres to the requirements for writing to tsm1 shards -func (r *Reader) Read() (string, []tsm1.Value, error) { - return r.keyBuf, r.values[:r.valuePos], nil -} - -// Close closes the reader. -func (r *Reader) Close() error { - r.tx.Rollback() - return r.db.Close() -} - -// cursor provides ordered iteration across a series. -type cursor struct { - // Bolt cursor and readahead buffer. - cursor *bolt.Cursor - keyBuf int64 - valBuf interface{} - - series string - field string - dec *tsdb.FieldCodec -} - -// Cursor returns an iterator for a key over a single field. -func newCursor(tx *bolt.Tx, series string, field string, dec *tsdb.FieldCodec) *cursor { - cur := &cursor{ - keyBuf: -2, - series: series, - field: field, - dec: dec, - } - - // Retrieve series bucket. - b := tx.Bucket([]byte(series)) - if b != nil { - cur.cursor = b.Cursor() - } - - return cur -} - -// Seek moves the cursor to a position. -func (c *cursor) SeekTo(seek int64) { - var seekBytes [8]byte - binary.BigEndian.PutUint64(seekBytes[:], uint64(seek)) - k, v := c.cursor.Seek(seekBytes[:]) - c.keyBuf, c.valBuf = tsdb.DecodeKeyValue(c.field, c.dec, k, v) -} - -// Next returns the next key/value pair from the cursor. -func (c *cursor) Next() (key int64, value interface{}) { - for { - k, v := func() (int64, interface{}) { - if c.keyBuf != -2 { - k, v := c.keyBuf, c.valBuf - c.keyBuf = -2 - return k, v - } - - k, v := c.cursor.Next() - if k == nil { - return -1, nil - } - return tsdb.DecodeKeyValue(c.field, c.dec, k, v) - }() - - if k != -1 && v == nil { - // There is a point in the series at the next timestamp, - // but not for this cursor's field. Go to the next point. - continue - } - return k, v - } -} - -// Sort b1 cursors in correct order for writing to TSM files. - -type cursors []*cursor - -func (a cursors) Len() int { return len(a) } -func (a cursors) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a cursors) Less(i, j int) bool { - if a[i].series == a[j].series { - return a[i].field < a[j].field - } - return a[i].series < a[j].series -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/bz1/reader.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/bz1/reader.go deleted file mode 100644 index eb65c29b..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/bz1/reader.go +++ /dev/null @@ -1,370 +0,0 @@ -package bz1 // import "github.com/influxdata/influxdb/cmd/influx_tsm/bz1" - -import ( - "bytes" - "encoding/binary" - "encoding/json" - "fmt" - "math" - "sort" - "time" - - "github.com/boltdb/bolt" - "github.com/golang/snappy" - "github.com/influxdata/influxdb/cmd/influx_tsm/stats" - "github.com/influxdata/influxdb/cmd/influx_tsm/tsdb" - "github.com/influxdata/influxdb/tsdb/engine/tsm1" -) - -// DefaultChunkSize is the size of chunks read from the bz1 shard -const DefaultChunkSize = 1000 - -// Reader is used to read all data from a bz1 shard. -type Reader struct { - path string - db *bolt.DB - tx *bolt.Tx - - cursors []*cursor - currCursor int - - keyBuf string - values []tsm1.Value - valuePos int - - fields map[string]*tsdb.MeasurementFields - codecs map[string]*tsdb.FieldCodec - - stats *stats.Stats -} - -// NewReader returns a reader for the bz1 shard at path. -func NewReader(path string, stats *stats.Stats, chunkSize int) *Reader { - r := &Reader{ - path: path, - fields: make(map[string]*tsdb.MeasurementFields), - codecs: make(map[string]*tsdb.FieldCodec), - stats: stats, - } - - if chunkSize <= 0 { - chunkSize = DefaultChunkSize - } - - r.values = make([]tsm1.Value, chunkSize) - - return r -} - -// Open opens the reader. -func (r *Reader) Open() error { - // Open underlying storage. - db, err := bolt.Open(r.path, 0666, &bolt.Options{Timeout: 1 * time.Second}) - if err != nil { - return err - } - r.db = db - - seriesSet := make(map[string]bool) - - if err := r.db.View(func(tx *bolt.Tx) error { - var data []byte - - meta := tx.Bucket([]byte("meta")) - if meta == nil { - // No data in this shard. - return nil - } - - pointsBucket := tx.Bucket([]byte("points")) - if pointsBucket == nil { - return nil - } - - if err := pointsBucket.ForEach(func(key, _ []byte) error { - seriesSet[string(key)] = true - return nil - }); err != nil { - return err - } - - buf := meta.Get([]byte("fields")) - if buf == nil { - // No data in this shard. - return nil - } - - data, err = snappy.Decode(nil, buf) - if err != nil { - return err - } - if err := json.Unmarshal(data, &r.fields); err != nil { - return err - } - return nil - }); err != nil { - return err - } - - // Build the codec for each measurement. - for k, v := range r.fields { - r.codecs[k] = tsdb.NewFieldCodec(v.Fields) - } - - r.tx, err = r.db.Begin(false) - if err != nil { - return err - } - - // Create cursor for each field of each series. - for s := range seriesSet { - measurement := tsdb.MeasurementFromSeriesKey(s) - fields := r.fields[measurement] - if fields == nil { - r.stats.IncrFiltered() - continue - } - for _, f := range fields.Fields { - c := newCursor(r.tx, s, f.Name, r.codecs[measurement]) - if c == nil { - continue - } - c.SeekTo(0) - r.cursors = append(r.cursors, c) - } - } - sort.Sort(cursors(r.cursors)) - - return nil -} - -// Next returns whether there is any more data to be read. -func (r *Reader) Next() bool { - r.valuePos = 0 -OUTER: - for { - if r.currCursor >= len(r.cursors) { - // All cursors drained. No more data remains. - return false - } - - cc := r.cursors[r.currCursor] - r.keyBuf = tsm1.SeriesFieldKey(cc.series, cc.field) - - for { - k, v := cc.Next() - if k == -1 { - // Go to next cursor and try again. - r.currCursor++ - if r.valuePos == 0 { - // The previous cursor had no data. Instead of returning - // just go immediately to the next cursor. - continue OUTER - } - // There is some data available. Indicate that it should be read. - return true - } - - if f, ok := v.(float64); ok { - if math.IsInf(f, 0) { - r.stats.AddPointsRead(1) - r.stats.IncrInf() - continue - } - - if math.IsNaN(f) { - r.stats.AddPointsRead(1) - r.stats.IncrNaN() - continue - } - } - - r.values[r.valuePos] = tsm1.NewValue(k, v) - r.valuePos++ - - if r.valuePos >= len(r.values) { - return true - } - } - } -} - -// Read returns the next chunk of data in the shard, converted to tsm1 values. Data is -// emitted completely for every field, in every series, before the next field is processed. -// Data from Read() adheres to the requirements for writing to tsm1 shards -func (r *Reader) Read() (string, []tsm1.Value, error) { - return r.keyBuf, r.values[:r.valuePos], nil -} - -// Close closes the reader. -func (r *Reader) Close() error { - r.tx.Rollback() - return r.db.Close() -} - -// cursor provides ordered iteration across a series. -type cursor struct { - cursor *bolt.Cursor - buf []byte // uncompressed buffer - off int // buffer offset - fieldIndices []int - index int - - series string - field string - dec *tsdb.FieldCodec - - keyBuf int64 - valBuf interface{} -} - -// newCursor returns an instance of a bz1 cursor. -func newCursor(tx *bolt.Tx, series string, field string, dec *tsdb.FieldCodec) *cursor { - // Retrieve points bucket. Ignore if there is no bucket. - b := tx.Bucket([]byte("points")).Bucket([]byte(series)) - if b == nil { - return nil - } - - return &cursor{ - cursor: b.Cursor(), - series: series, - field: field, - dec: dec, - keyBuf: -2, - } -} - -// Seek moves the cursor to a position. -func (c *cursor) SeekTo(seek int64) { - var seekBytes [8]byte - binary.BigEndian.PutUint64(seekBytes[:], uint64(seek)) - - // Move cursor to appropriate block and set to buffer. - k, v := c.cursor.Seek(seekBytes[:]) - if v == nil { // get the last block, it might have this time - _, v = c.cursor.Last() - } else if seek < int64(binary.BigEndian.Uint64(k)) { // the seek key is less than this block, go back one and check - _, v = c.cursor.Prev() - - // if the previous block max time is less than the seek value, reset to where we were originally - if v == nil || seek > int64(binary.BigEndian.Uint64(v[0:8])) { - _, v = c.cursor.Seek(seekBytes[:]) - } - } - c.setBuf(v) - - // Read current block up to seek position. - c.seekBuf(seekBytes[:]) - - // Return current entry. - c.keyBuf, c.valBuf = c.read() -} - -// seekBuf moves the cursor to a position within the current buffer. -func (c *cursor) seekBuf(seek []byte) (key, value []byte) { - for { - // Slice off the current entry. - buf := c.buf[c.off:] - - // Exit if current entry's timestamp is on or after the seek. - if len(buf) == 0 { - return - } - - if bytes.Compare(buf[0:8], seek) != -1 { - return - } - - c.off += entryHeaderSize + entryDataSize(buf) - } -} - -// Next returns the next key/value pair from the cursor. If there are no values -// remaining, -1 is returned. -func (c *cursor) Next() (int64, interface{}) { - for { - k, v := func() (int64, interface{}) { - if c.keyBuf != -2 { - k, v := c.keyBuf, c.valBuf - c.keyBuf = -2 - return k, v - } - - // Ignore if there is no buffer. - if len(c.buf) == 0 { - return -1, nil - } - - // Move forward to next entry. - c.off += entryHeaderSize + entryDataSize(c.buf[c.off:]) - - // If no items left then read first item from next block. - if c.off >= len(c.buf) { - _, v := c.cursor.Next() - c.setBuf(v) - } - - return c.read() - }() - - if k != -1 && v == nil { - // There is a point in the series at the next timestamp, - // but not for this cursor's field. Go to the next point. - continue - } - return k, v - } -} - -// setBuf saves a compressed block to the buffer. -func (c *cursor) setBuf(block []byte) { - // Clear if the block is empty. - if len(block) == 0 { - c.buf, c.off, c.fieldIndices, c.index = c.buf[0:0], 0, c.fieldIndices[0:0], 0 - return - } - - // Otherwise decode block into buffer. - // Skip over the first 8 bytes since they are the max timestamp. - buf, err := snappy.Decode(nil, block[8:]) - if err != nil { - c.buf = c.buf[0:0] - fmt.Printf("block decode error: %s\n", err) - } - - c.buf, c.off = buf, 0 -} - -// read reads the current key and value from the current block. -func (c *cursor) read() (key int64, value interface{}) { - // Return nil if the offset is at the end of the buffer. - if c.off >= len(c.buf) { - return -1, nil - } - - // Otherwise read the current entry. - buf := c.buf[c.off:] - dataSize := entryDataSize(buf) - - return tsdb.DecodeKeyValue(c.field, c.dec, buf[0:8], buf[entryHeaderSize:entryHeaderSize+dataSize]) -} - -// Sort bz1 cursors in correct order for writing to TSM files. - -type cursors []*cursor - -func (a cursors) Len() int { return len(a) } -func (a cursors) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a cursors) Less(i, j int) bool { - if a[i].series == a[j].series { - return a[i].field < a[j].field - } - return a[i].series < a[j].series -} - -// entryHeaderSize is the number of bytes required for the header. -const entryHeaderSize = 8 + 4 - -// entryDataSize returns the size of an entry's data field, in bytes. -func entryDataSize(v []byte) int { return int(binary.BigEndian.Uint32(v[8:12])) } diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/converter.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/converter.go deleted file mode 100644 index 3469af62..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/converter.go +++ /dev/null @@ -1,118 +0,0 @@ -package main - -import ( - "fmt" - "os" - "path/filepath" - - "github.com/influxdata/influxdb/cmd/influx_tsm/stats" - "github.com/influxdata/influxdb/tsdb/engine/tsm1" -) - -const ( - maxBlocksPerKey = 65535 -) - -// KeyIterator is used to iterate over b* keys for conversion to tsm keys -type KeyIterator interface { - Next() bool - Read() (string, []tsm1.Value, error) -} - -// Converter encapsulates the logic for converting b*1 shards to tsm1 shards. -type Converter struct { - path string - maxTSMFileSize uint32 - sequence int - stats *stats.Stats -} - -// NewConverter returns a new instance of the Converter. -func NewConverter(path string, sz uint32, stats *stats.Stats) *Converter { - return &Converter{ - path: path, - maxTSMFileSize: sz, - stats: stats, - } -} - -// Process writes the data provided by iter to a tsm1 shard. -func (c *Converter) Process(iter KeyIterator) error { - // Ensure the tsm1 directory exists. - if err := os.MkdirAll(c.path, 0777); err != nil { - return err - } - - // Iterate until no more data remains. - var w tsm1.TSMWriter - var keyCount map[string]int - - for iter.Next() { - k, v, err := iter.Read() - if err != nil { - return err - } - - if w == nil { - w, err = c.nextTSMWriter() - if err != nil { - return err - } - keyCount = map[string]int{} - } - if err := w.Write(k, v); err != nil { - return err - } - keyCount[k]++ - - c.stats.AddPointsRead(len(v)) - c.stats.AddPointsWritten(len(v)) - - // If we have a max file size configured and we're over it, start a new TSM file. - if w.Size() > c.maxTSMFileSize || keyCount[k] == maxBlocksPerKey { - if err := w.WriteIndex(); err != nil && err != tsm1.ErrNoValues { - return err - } - - c.stats.AddTSMBytes(w.Size()) - - if err := w.Close(); err != nil { - return err - } - w = nil - } - } - - if w != nil { - if err := w.WriteIndex(); err != nil && err != tsm1.ErrNoValues { - return err - } - c.stats.AddTSMBytes(w.Size()) - - if err := w.Close(); err != nil { - return err - } - } - - return nil -} - -// nextTSMWriter returns the next TSMWriter for the Converter. -func (c *Converter) nextTSMWriter() (tsm1.TSMWriter, error) { - c.sequence++ - fileName := filepath.Join(c.path, fmt.Sprintf("%09d-%09d.%s", 1, c.sequence, tsm1.TSMFileExtension)) - - fd, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666) - if err != nil { - return nil, err - } - - // Create the writer for the new TSM file. - w, err := tsm1.NewTSMWriter(fd) - if err != nil { - return nil, err - } - - c.stats.IncrTSMFileCount() - return w, nil -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/main.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/main.go deleted file mode 100644 index 77f0d438..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/main.go +++ /dev/null @@ -1,413 +0,0 @@ -package main - -import ( - "bufio" - "errors" - "flag" - "fmt" - "io" - "io/ioutil" - "log" - "os" - "path/filepath" - "runtime" - "runtime/pprof" - "sort" - "strings" - "text/tabwriter" - "time" - - "net/http" - _ "net/http/pprof" - - "github.com/influxdata/influxdb/cmd/influx_tsm/b1" - "github.com/influxdata/influxdb/cmd/influx_tsm/bz1" - "github.com/influxdata/influxdb/cmd/influx_tsm/tsdb" -) - -// ShardReader reads b* shards and converts to tsm shards -type ShardReader interface { - KeyIterator - Open() error - Close() error -} - -const ( - tsmExt = "tsm" -) - -var description = ` -Convert a database from b1 or bz1 format to tsm1 format. - -This tool will backup the directories before conversion (if not disabled). -The backed-up files must be removed manually, generally after starting up the -node again to make sure all of data has been converted correctly. - -To restore a backup: - Shut down the node, remove the converted directory, and - copy the backed-up directory to the original location.` - -type options struct { - DataPath string - BackupPath string - DBs []string - DebugAddr string - TSMSize uint64 - Parallel bool - SkipBackup bool - UpdateInterval time.Duration - Yes bool - CPUFile string -} - -func (o *options) Parse() error { - fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError) - - var dbs string - - fs.StringVar(&dbs, "dbs", "", "Comma-delimited list of databases to convert. Default is to convert all databases.") - fs.Uint64Var(&opts.TSMSize, "sz", maxTSMSz, "Maximum size of individual TSM files.") - fs.BoolVar(&opts.Parallel, "parallel", false, "Perform parallel conversion. (up to GOMAXPROCS shards at once)") - fs.BoolVar(&opts.SkipBackup, "nobackup", false, "Disable database backups. Not recommended.") - fs.StringVar(&opts.BackupPath, "backup", "", "The location to backup up the current databases. Must not be within the data directory.") - fs.StringVar(&opts.DebugAddr, "debug", "", "If set, http debugging endpoints will be enabled on the given address") - fs.DurationVar(&opts.UpdateInterval, "interval", 5*time.Second, "How often status updates are printed.") - fs.BoolVar(&opts.Yes, "y", false, "Don't ask, just convert") - fs.StringVar(&opts.CPUFile, "profile", "", "CPU Profile location") - fs.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage: %v [options] \n", os.Args[0]) - fmt.Fprintf(os.Stderr, "%v\n\nOptions:\n", description) - fs.PrintDefaults() - fmt.Fprintf(os.Stderr, "\n") - } - - if err := fs.Parse(os.Args[1:]); err != nil { - return err - } - - if len(fs.Args()) < 1 { - return errors.New("no data directory specified") - } - var err error - if o.DataPath, err = filepath.Abs(fs.Args()[0]); err != nil { - return err - } - if o.DataPath, err = filepath.EvalSymlinks(filepath.Clean(o.DataPath)); err != nil { - return err - } - - if o.TSMSize > maxTSMSz { - return fmt.Errorf("bad TSM file size, maximum TSM file size is %d", maxTSMSz) - } - - // Check if specific databases were requested. - o.DBs = strings.Split(dbs, ",") - if len(o.DBs) == 1 && o.DBs[0] == "" { - o.DBs = nil - } - - if !o.SkipBackup { - if o.BackupPath == "" { - return errors.New("either -nobackup or -backup DIR must be set") - } - if o.BackupPath, err = filepath.Abs(o.BackupPath); err != nil { - return err - } - if o.BackupPath, err = filepath.EvalSymlinks(filepath.Clean(o.BackupPath)); err != nil { - if os.IsNotExist(err) { - return errors.New("backup directory must already exist") - } - return err - } - - if strings.HasPrefix(o.BackupPath, o.DataPath) { - fmt.Println(o.BackupPath, o.DataPath) - return errors.New("backup directory cannot be contained within data directory") - } - } - - if o.DebugAddr != "" { - log.Printf("Starting debugging server on http://%v", o.DebugAddr) - go func() { - log.Fatal(http.ListenAndServe(o.DebugAddr, nil)) - }() - } - - return nil -} - -var opts options - -const maxTSMSz uint64 = 2 * 1024 * 1024 * 1024 - -func init() { - log.SetOutput(os.Stderr) - log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds) -} - -func main() { - if err := opts.Parse(); err != nil { - log.Fatal(err) - } - - // Determine the list of databases - dbs, err := ioutil.ReadDir(opts.DataPath) - if err != nil { - log.Fatalf("failed to access data directory at %v: %v\n", opts.DataPath, err) - } - fmt.Println() // Cleanly separate output from start of program. - - if opts.Parallel { - if !isEnvSet("GOMAXPROCS") { - // Only modify GOMAXPROCS if it wasn't set in the environment - // This means 'GOMAXPROCS=1 influx_tsm -parallel' will not actually - // run in parallel - runtime.GOMAXPROCS(runtime.NumCPU()) - } - } - - var badUser string - if opts.SkipBackup { - badUser = "(NOT RECOMMENDED)" - } - - // Dump summary of what is about to happen. - fmt.Println("b1 and bz1 shard conversion.") - fmt.Println("-----------------------------------") - fmt.Println("Data directory is: ", opts.DataPath) - if !opts.SkipBackup { - fmt.Println("Backup directory is: ", opts.BackupPath) - } - fmt.Println("Databases specified: ", allDBs(opts.DBs)) - fmt.Println("Database backups enabled: ", yesno(!opts.SkipBackup), badUser) - fmt.Printf("Parallel mode enabled (GOMAXPROCS): %s (%d)\n", yesno(opts.Parallel), runtime.GOMAXPROCS(0)) - fmt.Println() - - shards := collectShards(dbs) - - // Anything to convert? - fmt.Printf("\nFound %d shards that will be converted.\n", len(shards)) - if len(shards) == 0 { - fmt.Println("Nothing to do.") - return - } - - // Display list of convertible shards. - fmt.Println() - w := new(tabwriter.Writer) - w.Init(os.Stdout, 0, 8, 1, '\t', 0) - fmt.Fprintln(w, "Database\tRetention\tPath\tEngine\tSize") - for _, si := range shards { - fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%d\n", si.Database, si.RetentionPolicy, si.FullPath(opts.DataPath), si.FormatAsString(), si.Size) - } - w.Flush() - - if !opts.Yes { - // Get confirmation from user. - fmt.Printf("\nThese shards will be converted. Proceed? y/N: ") - liner := bufio.NewReader(os.Stdin) - yn, err := liner.ReadString('\n') - if err != nil { - log.Fatalf("failed to read response: %v", err) - } - yn = strings.TrimRight(strings.ToLower(yn), "\n") - if yn != "y" { - log.Fatal("Conversion aborted.") - } - } - fmt.Println("Conversion starting....") - - if opts.CPUFile != "" { - f, err := os.Create(opts.CPUFile) - if err != nil { - log.Fatal(err) - } - if err = pprof.StartCPUProfile(f); err != nil { - log.Fatal(err) - } - defer pprof.StopCPUProfile() - } - - tr := newTracker(shards, opts) - - if err := tr.Run(); err != nil { - log.Fatalf("Error occurred preventing completion: %v\n", err) - } - - tr.PrintStats() -} - -func collectShards(dbs []os.FileInfo) tsdb.ShardInfos { - // Get the list of shards for conversion. - var shards tsdb.ShardInfos - for _, db := range dbs { - d := tsdb.NewDatabase(filepath.Join(opts.DataPath, db.Name())) - shs, err := d.Shards() - if err != nil { - log.Fatalf("Failed to access shards for database %v: %v\n", d.Name(), err) - } - shards = append(shards, shs...) - } - - sort.Sort(shards) - shards = shards.FilterFormat(tsdb.TSM1) - if len(dbs) > 0 { - shards = shards.ExclusiveDatabases(opts.DBs) - } - - return shards -} - -// backupDatabase backs up the database named db -func backupDatabase(db string) error { - copyFile := func(path string, info os.FileInfo, err error) error { - // Strip the DataPath from the path and replace with BackupPath. - toPath := strings.Replace(path, opts.DataPath, opts.BackupPath, 1) - - if info.IsDir() { - return os.MkdirAll(toPath, info.Mode()) - } - - in, err := os.Open(path) - if err != nil { - return err - } - defer in.Close() - - srcInfo, err := os.Stat(path) - if err != nil { - return err - } - - out, err := os.OpenFile(toPath, os.O_CREATE|os.O_WRONLY, info.Mode()) - if err != nil { - return err - } - defer out.Close() - - dstInfo, err := os.Stat(toPath) - if err != nil { - return err - } - - if dstInfo.Size() == srcInfo.Size() { - log.Printf("Backup file already found for %v with correct size, skipping.", path) - return nil - } - - if dstInfo.Size() > srcInfo.Size() { - log.Printf("Invalid backup file found for %v, replacing with good copy.", path) - if err := out.Truncate(0); err != nil { - return err - } - if _, err := out.Seek(0, os.SEEK_SET); err != nil { - return err - } - } - - if dstInfo.Size() > 0 { - log.Printf("Resuming backup of file %v, starting at %v bytes", path, dstInfo.Size()) - } - - off, err := out.Seek(0, os.SEEK_END) - if err != nil { - return err - } - if _, err := in.Seek(off, os.SEEK_SET); err != nil { - return err - } - - log.Printf("Backing up file %v", path) - - _, err = io.Copy(out, in) - - return err - } - - return filepath.Walk(filepath.Join(opts.DataPath, db), copyFile) -} - -// convertShard converts the shard in-place. -func convertShard(si *tsdb.ShardInfo, tr *tracker) error { - src := si.FullPath(opts.DataPath) - dst := fmt.Sprintf("%v.%v", src, tsmExt) - - var reader ShardReader - switch si.Format { - case tsdb.BZ1: - reader = bz1.NewReader(src, &tr.Stats, 0) - case tsdb.B1: - reader = b1.NewReader(src, &tr.Stats, 0) - default: - return fmt.Errorf("Unsupported shard format: %v", si.FormatAsString()) - } - - // Open the shard, and create a converter. - if err := reader.Open(); err != nil { - return fmt.Errorf("Failed to open %v for conversion: %v", src, err) - } - defer reader.Close() - converter := NewConverter(dst, uint32(opts.TSMSize), &tr.Stats) - - // Perform the conversion. - if err := converter.Process(reader); err != nil { - return fmt.Errorf("Conversion of %v failed: %v", src, err) - } - - // Delete source shard, and rename new tsm1 shard. - if err := reader.Close(); err != nil { - return fmt.Errorf("Conversion of %v failed due to close: %v", src, err) - } - - if err := os.RemoveAll(si.FullPath(opts.DataPath)); err != nil { - return fmt.Errorf("Deletion of %v failed: %v", src, err) - } - if err := os.Rename(dst, src); err != nil { - return fmt.Errorf("Rename of %v to %v failed: %v", dst, src, err) - } - - return nil -} - -// ParallelGroup allows the maximum parrallelism of a set of operations to be controlled. -type ParallelGroup chan struct{} - -// NewParallelGroup returns a group which allows n operations to run in parallel. A value of 0 -// means no operations will ever run. -func NewParallelGroup(n int) ParallelGroup { - return make(chan struct{}, n) -} - -// Do executes one operation of the ParallelGroup -func (p ParallelGroup) Do(f func()) { - p <- struct{}{} // acquire working slot - defer func() { <-p }() - - f() -} - -// yesno returns "yes" for true, "no" for false. -func yesno(b bool) string { - if b { - return "yes" - } - return "no" -} - -// allDBs returns "all" if all databases are requested for conversion. -func allDBs(dbs []string) string { - if dbs == nil { - return "all" - } - return fmt.Sprintf("%v", dbs) -} - -// isEnvSet checks to see if a variable was set in the environment -func isEnvSet(name string) bool { - for _, s := range os.Environ() { - if strings.SplitN(s, "=", 2)[0] == name { - return true - } - } - return false -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/stats/stats.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/stats/stats.go deleted file mode 100644 index 38c5a645..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/stats/stats.go +++ /dev/null @@ -1,54 +0,0 @@ -package stats - -import ( - "sync/atomic" - "time" -) - -// Stats are the statistics captured while converting non-TSM shards to TSM -type Stats struct { - NanFiltered uint64 - InfFiltered uint64 - FieldsFiltered uint64 - PointsWritten uint64 - PointsRead uint64 - TsmFilesCreated uint64 - TsmBytesWritten uint64 - CompletedShards uint64 - TotalTime time.Duration -} - -// AddPointsRead increments the number of read points. -func (s *Stats) AddPointsRead(n int) { - atomic.AddUint64(&s.PointsRead, uint64(n)) -} - -// AddPointsWritten increments the number of written points. -func (s *Stats) AddPointsWritten(n int) { - atomic.AddUint64(&s.PointsWritten, uint64(n)) -} - -// AddTSMBytes increments the number of TSM Bytes. -func (s *Stats) AddTSMBytes(n uint32) { - atomic.AddUint64(&s.TsmBytesWritten, uint64(n)) -} - -// IncrTSMFileCount increments the number of TSM files created. -func (s *Stats) IncrTSMFileCount() { - atomic.AddUint64(&s.TsmFilesCreated, 1) -} - -// IncrNaN increments the number of NaNs filtered. -func (s *Stats) IncrNaN() { - atomic.AddUint64(&s.NanFiltered, 1) -} - -// IncrInf increments the number of Infs filtered. -func (s *Stats) IncrInf() { - atomic.AddUint64(&s.InfFiltered, 1) -} - -// IncrFiltered increments the number of fields filtered. -func (s *Stats) IncrFiltered() { - atomic.AddUint64(&s.FieldsFiltered, 1) -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tracker.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tracker.go deleted file mode 100644 index 9048aa76..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tracker.go +++ /dev/null @@ -1,130 +0,0 @@ -package main - -import ( - "fmt" - "log" - "runtime" - "sync" - "sync/atomic" - "time" - - "github.com/influxdata/influxdb/cmd/influx_tsm/stats" - "github.com/influxdata/influxdb/cmd/influx_tsm/tsdb" -) - -// tracker will orchestrate and track the conversions of non-TSM shards to TSM -type tracker struct { - Stats stats.Stats - - shards tsdb.ShardInfos - opts options - - pg ParallelGroup - wg sync.WaitGroup -} - -// newTracker will setup and return a clean tracker instance -func newTracker(shards tsdb.ShardInfos, opts options) *tracker { - t := &tracker{ - shards: shards, - opts: opts, - pg: NewParallelGroup(runtime.GOMAXPROCS(0)), - } - - return t -} - -func (t *tracker) Run() error { - conversionStart := time.Now() - - // Backup each directory. - if !opts.SkipBackup { - databases := t.shards.Databases() - fmt.Printf("Backing up %d databases...\n", len(databases)) - t.wg.Add(len(databases)) - for i := range databases { - db := databases[i] - go t.pg.Do(func() { - defer t.wg.Done() - - start := time.Now() - log.Printf("Backup of database '%v' started", db) - err := backupDatabase(db) - if err != nil { - log.Fatalf("Backup of database %v failed: %v\n", db, err) - } - log.Printf("Database %v backed up (%v)\n", db, time.Now().Sub(start)) - }) - } - t.wg.Wait() - } else { - fmt.Println("Database backup disabled.") - } - - t.wg.Add(len(t.shards)) - for i := range t.shards { - si := t.shards[i] - go t.pg.Do(func() { - defer func() { - atomic.AddUint64(&t.Stats.CompletedShards, 1) - t.wg.Done() - }() - - start := time.Now() - log.Printf("Starting conversion of shard: %v", si.FullPath(opts.DataPath)) - if err := convertShard(si, t); err != nil { - log.Fatalf("Failed to convert %v: %v\n", si.FullPath(opts.DataPath), err) - } - log.Printf("Conversion of %v successful (%v)\n", si.FullPath(opts.DataPath), time.Since(start)) - }) - } - - done := make(chan struct{}) - go func() { - t.wg.Wait() - close(done) - }() - -WAIT_LOOP: - for { - select { - case <-done: - break WAIT_LOOP - case <-time.After(opts.UpdateInterval): - t.StatusUpdate() - } - } - - t.Stats.TotalTime = time.Since(conversionStart) - - return nil -} - -func (t *tracker) StatusUpdate() { - shardCount := atomic.LoadUint64(&t.Stats.CompletedShards) - pointCount := atomic.LoadUint64(&t.Stats.PointsRead) - pointWritten := atomic.LoadUint64(&t.Stats.PointsWritten) - - log.Printf("Still Working: Completed Shards: %d/%d Points read/written: %d/%d", shardCount, len(t.shards), pointCount, pointWritten) -} - -func (t *tracker) PrintStats() { - preSize := t.shards.Size() - postSize := int64(t.Stats.TsmBytesWritten) - - fmt.Printf("\nSummary statistics\n========================================\n") - fmt.Printf("Databases converted: %d\n", len(t.shards.Databases())) - fmt.Printf("Shards converted: %d\n", len(t.shards)) - fmt.Printf("TSM files created: %d\n", t.Stats.TsmFilesCreated) - fmt.Printf("Points read: %d\n", t.Stats.PointsRead) - fmt.Printf("Points written: %d\n", t.Stats.PointsWritten) - fmt.Printf("NaN filtered: %d\n", t.Stats.NanFiltered) - fmt.Printf("Inf filtered: %d\n", t.Stats.InfFiltered) - fmt.Printf("Points without fields filtered: %d\n", t.Stats.FieldsFiltered) - fmt.Printf("Disk usage pre-conversion (bytes): %d\n", preSize) - fmt.Printf("Disk usage post-conversion (bytes): %d\n", postSize) - fmt.Printf("Reduction factor: %d%%\n", 100*(preSize-postSize)/preSize) - fmt.Printf("Bytes per TSM point: %.2f\n", float64(postSize)/float64(t.Stats.PointsWritten)) - fmt.Printf("Total conversion time: %v\n", t.Stats.TotalTime) - fmt.Println() -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/codec.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/codec.go deleted file mode 100644 index 4c3a7b61..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/codec.go +++ /dev/null @@ -1,119 +0,0 @@ -package tsdb - -import ( - "encoding/binary" - "errors" - "fmt" - "math" -) - -const ( - fieldFloat = 1 - fieldInteger = 2 - fieldBoolean = 3 - fieldString = 4 -) - -var ( - // ErrFieldNotFound is returned when a field cannot be found. - ErrFieldNotFound = errors.New("field not found") - - // ErrFieldUnmappedID is returned when the system is presented, during decode, with a field ID - // there is no mapping for. - ErrFieldUnmappedID = errors.New("field ID not mapped") -) - -// FieldCodec provides encoding and decoding functionality for the fields of a given -// Measurement. -type FieldCodec struct { - fieldsByID map[uint8]*Field - fieldsByName map[string]*Field -} - -// NewFieldCodec returns a FieldCodec for the given Measurement. Must be called with -// a RLock that protects the Measurement. -func NewFieldCodec(fields map[string]*Field) *FieldCodec { - fieldsByID := make(map[uint8]*Field, len(fields)) - fieldsByName := make(map[string]*Field, len(fields)) - for _, f := range fields { - fieldsByID[f.ID] = f - fieldsByName[f.Name] = f - } - return &FieldCodec{fieldsByID: fieldsByID, fieldsByName: fieldsByName} -} - -// FieldIDByName returns the ID for the given field. -func (f *FieldCodec) FieldIDByName(s string) (uint8, error) { - fi := f.fieldsByName[s] - if fi == nil { - return 0, ErrFieldNotFound - } - return fi.ID, nil -} - -// DecodeByID scans a byte slice for a field with the given ID, converts it to its -// expected type, and return that value. -func (f *FieldCodec) DecodeByID(targetID uint8, b []byte) (interface{}, error) { - var value interface{} - for { - if len(b) == 0 { - // No more bytes. - return nil, ErrFieldNotFound - } - - field := f.fieldsByID[b[0]] - if field == nil { - // This can happen, though is very unlikely. If this node receives encoded data, to be written - // to disk, and is queried for that data before its metastore is updated, there will be no field - // mapping for the data during decode. All this can happen because data is encoded by the node - // that first received the write request, not the node that actually writes the data to disk. - // So if this happens, the read must be aborted. - return nil, ErrFieldUnmappedID - } - - switch field.Type { - case fieldFloat: - if field.ID == targetID { - value = math.Float64frombits(binary.BigEndian.Uint64(b[1:9])) - } - b = b[9:] - case fieldInteger: - if field.ID == targetID { - value = int64(binary.BigEndian.Uint64(b[1:9])) - } - b = b[9:] - case fieldBoolean: - if field.ID == targetID { - value = b[1] == 1 - } - b = b[2:] - case fieldString: - length := binary.BigEndian.Uint16(b[1:3]) - if field.ID == targetID { - value = string(b[3 : 3+length]) - } - b = b[3+length:] - default: - panic(fmt.Sprintf("unsupported value type during decode by id: %T", field.Type)) - } - - if value != nil { - return value, nil - } - } -} - -// DecodeByName scans a byte slice for a field with the given name, converts it to its -// expected type, and return that value. -func (f *FieldCodec) DecodeByName(name string, b []byte) (interface{}, error) { - fi := f.FieldByName(name) - if fi == nil { - return 0, ErrFieldNotFound - } - return f.DecodeByID(fi.ID, b) -} - -// FieldByName returns the field by its name. It will return a nil if not found -func (f *FieldCodec) FieldByName(name string) *Field { - return f.fieldsByName[name] -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/database.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/database.go deleted file mode 100644 index c276db08..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/database.go +++ /dev/null @@ -1,239 +0,0 @@ -package tsdb // import "github.com/influxdata/influxdb/cmd/influx_tsm/tsdb" - -import ( - "fmt" - "os" - "path" - "path/filepath" - "sort" - "time" - - "github.com/boltdb/bolt" - "github.com/influxdata/influxdb/pkg/slices" -) - -// Flags for differentiating between engines -const ( - B1 = iota - BZ1 - TSM1 -) - -// EngineFormat holds the flag for the engine -type EngineFormat int - -// String returns the string format of the engine. -func (e EngineFormat) String() string { - switch e { - case TSM1: - return "tsm1" - case B1: - return "b1" - case BZ1: - return "bz1" - default: - panic("unrecognized shard engine format") - } -} - -// ShardInfo is the description of a shard on disk. -type ShardInfo struct { - Database string - RetentionPolicy string - Path string - Format EngineFormat - Size int64 -} - -// FormatAsString returns the format of the shard as a string. -func (s *ShardInfo) FormatAsString() string { - return s.Format.String() -} - -// FullPath returns the full path to the shard, given the data directory root. -func (s *ShardInfo) FullPath(dataPath string) string { - return filepath.Join(dataPath, s.Database, s.RetentionPolicy, s.Path) -} - -// ShardInfos is an array of ShardInfo -type ShardInfos []*ShardInfo - -func (s ShardInfos) Len() int { return len(s) } -func (s ShardInfos) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -func (s ShardInfos) Less(i, j int) bool { - if s[i].Database == s[j].Database { - if s[i].RetentionPolicy == s[j].RetentionPolicy { - return s[i].Path < s[i].Path - } - - return s[i].RetentionPolicy < s[j].RetentionPolicy - } - - return s[i].Database < s[j].Database -} - -// Databases returns the sorted unique set of databases for the shards. -func (s ShardInfos) Databases() []string { - dbm := make(map[string]bool) - for _, ss := range s { - dbm[ss.Database] = true - } - - var dbs []string - for k := range dbm { - dbs = append(dbs, k) - } - sort.Strings(dbs) - return dbs -} - -// FilterFormat returns a copy of the ShardInfos, with shards of the given -// format removed. -func (s ShardInfos) FilterFormat(fmt EngineFormat) ShardInfos { - var a ShardInfos - for _, si := range s { - if si.Format != fmt { - a = append(a, si) - } - } - return a -} - -// Size returns the space on disk consumed by the shards. -func (s ShardInfos) Size() int64 { - var sz int64 - for _, si := range s { - sz += si.Size - } - return sz -} - -// ExclusiveDatabases returns a copy of the ShardInfo, with shards associated -// with the given databases present. If the given set is empty, all databases -// are returned. -func (s ShardInfos) ExclusiveDatabases(exc []string) ShardInfos { - var a ShardInfos - - // Empty set? Return everything. - if len(exc) == 0 { - a = make(ShardInfos, len(s)) - copy(a, s) - return a - } - - for _, si := range s { - if slices.Exists(exc, si.Database) { - a = append(a, si) - } - } - return a -} - -// Database represents an entire database on disk. -type Database struct { - path string -} - -// NewDatabase creates a database instance using data at path. -func NewDatabase(path string) *Database { - return &Database{path: path} -} - -// Name returns the name of the database. -func (d *Database) Name() string { - return path.Base(d.path) -} - -// Path returns the path to the database. -func (d *Database) Path() string { - return d.path -} - -// Shards returns information for every shard in the database. -func (d *Database) Shards() ([]*ShardInfo, error) { - fd, err := os.Open(d.path) - if err != nil { - return nil, err - } - - // Get each retention policy. - rps, err := fd.Readdirnames(-1) - if err != nil { - return nil, err - } - - // Process each retention policy. - var shardInfos []*ShardInfo - for _, rp := range rps { - rpfd, err := os.Open(filepath.Join(d.path, rp)) - if err != nil { - return nil, err - } - - // Process each shard - shards, err := rpfd.Readdirnames(-1) - for _, sh := range shards { - fmt, sz, err := shardFormat(filepath.Join(d.path, rp, sh)) - if err != nil { - return nil, err - } - - si := &ShardInfo{ - Database: d.Name(), - RetentionPolicy: path.Base(rp), - Path: sh, - Format: fmt, - Size: sz, - } - shardInfos = append(shardInfos, si) - } - } - - sort.Sort(ShardInfos(shardInfos)) - return shardInfos, nil -} - -// shardFormat returns the format and size on disk of the shard at path. -func shardFormat(path string) (EngineFormat, int64, error) { - // If it's a directory then it's a tsm1 engine - fi, err := os.Stat(path) - if err != nil { - return 0, 0, err - } - if fi.Mode().IsDir() { - return TSM1, fi.Size(), nil - } - - // It must be a BoltDB-based engine. - db, err := bolt.Open(path, 0666, &bolt.Options{Timeout: 1 * time.Second}) - if err != nil { - return 0, 0, err - } - defer db.Close() - - var format EngineFormat - err = db.View(func(tx *bolt.Tx) error { - // Retrieve the meta bucket. - b := tx.Bucket([]byte("meta")) - - // If no format is specified then it must be an original b1 database. - if b == nil { - format = B1 - return nil - } - - // There is an actual format indicator. - switch f := string(b.Get([]byte("format"))); f { - case "b1", "v1": - format = B1 - case "bz1": - format = BZ1 - default: - return fmt.Errorf("unrecognized engine format: %s", f) - } - - return nil - }) - - return format, fi.Size(), err -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/internal/meta.pb.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/internal/meta.pb.go deleted file mode 100644 index c580f4db..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/internal/meta.pb.go +++ /dev/null @@ -1,122 +0,0 @@ -// Code generated by protoc-gen-gogo. -// source: internal/meta.proto -// DO NOT EDIT! - -/* -Package internal is a generated protocol buffer package. - -It is generated from these files: - internal/meta.proto - -It has these top-level messages: - Series - Tag - MeasurementFields - Field -*/ -package internal - -import proto "github.com/gogo/protobuf/proto" -import fmt "fmt" -import math "math" - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -type Series struct { - Key *string `protobuf:"bytes,1,req,name=Key" json:"Key,omitempty"` - Tags []*Tag `protobuf:"bytes,2,rep,name=Tags" json:"Tags,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Series) Reset() { *m = Series{} } -func (m *Series) String() string { return proto.CompactTextString(m) } -func (*Series) ProtoMessage() {} - -func (m *Series) GetKey() string { - if m != nil && m.Key != nil { - return *m.Key - } - return "" -} - -func (m *Series) GetTags() []*Tag { - if m != nil { - return m.Tags - } - return nil -} - -type Tag struct { - Key *string `protobuf:"bytes,1,req,name=Key" json:"Key,omitempty"` - Value *string `protobuf:"bytes,2,req,name=Value" json:"Value,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Tag) Reset() { *m = Tag{} } -func (m *Tag) String() string { return proto.CompactTextString(m) } -func (*Tag) ProtoMessage() {} - -func (m *Tag) GetKey() string { - if m != nil && m.Key != nil { - return *m.Key - } - return "" -} - -func (m *Tag) GetValue() string { - if m != nil && m.Value != nil { - return *m.Value - } - return "" -} - -type MeasurementFields struct { - Fields []*Field `protobuf:"bytes,1,rep,name=Fields" json:"Fields,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *MeasurementFields) Reset() { *m = MeasurementFields{} } -func (m *MeasurementFields) String() string { return proto.CompactTextString(m) } -func (*MeasurementFields) ProtoMessage() {} - -func (m *MeasurementFields) GetFields() []*Field { - if m != nil { - return m.Fields - } - return nil -} - -type Field struct { - ID *int32 `protobuf:"varint,1,req,name=ID" json:"ID,omitempty"` - Name *string `protobuf:"bytes,2,req,name=Name" json:"Name,omitempty"` - Type *int32 `protobuf:"varint,3,req,name=Type" json:"Type,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Field) Reset() { *m = Field{} } -func (m *Field) String() string { return proto.CompactTextString(m) } -func (*Field) ProtoMessage() {} - -func (m *Field) GetID() int32 { - if m != nil && m.ID != nil { - return *m.ID - } - return 0 -} - -func (m *Field) GetName() string { - if m != nil && m.Name != nil { - return *m.Name - } - return "" -} - -func (m *Field) GetType() int32 { - if m != nil && m.Type != nil { - return *m.Type - } - return 0 -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/types.go b/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/types.go deleted file mode 100644 index c0d0010f..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/types.go +++ /dev/null @@ -1,60 +0,0 @@ -package tsdb - -import ( - "encoding/binary" - "strings" - - "github.com/influxdata/influxdb/cmd/influx_tsm/tsdb/internal" - "github.com/influxdata/influxdb/influxql" - - "github.com/gogo/protobuf/proto" -) - -// Field represents an encoded field. -type Field struct { - ID uint8 `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Type influxql.DataType `json:"type,omitempty"` -} - -// MeasurementFields is a mapping from measurements to its fields. -type MeasurementFields struct { - Fields map[string]*Field `json:"fields"` - Codec *FieldCodec -} - -// UnmarshalBinary decodes the object from a binary format. -func (m *MeasurementFields) UnmarshalBinary(buf []byte) error { - var pb internal.MeasurementFields - if err := proto.Unmarshal(buf, &pb); err != nil { - return err - } - m.Fields = make(map[string]*Field) - for _, f := range pb.Fields { - m.Fields[f.GetName()] = &Field{ID: uint8(f.GetID()), Name: f.GetName(), Type: influxql.DataType(f.GetType())} - } - return nil -} - -// Series represents a series in the shard. -type Series struct { - Key string - Tags map[string]string -} - -// MeasurementFromSeriesKey returns the Measurement name for a given series. -func MeasurementFromSeriesKey(key string) string { - return strings.SplitN(key, ",", 2)[0] -} - -// DecodeKeyValue decodes the key and value from bytes. -func DecodeKeyValue(field string, dec *FieldCodec, k, v []byte) (int64, interface{}) { - // Convert key to a timestamp. - key := int64(binary.BigEndian.Uint64(k[0:8])) - - decValue, err := dec.DecodeByName(field, v) - if err != nil { - return key, nil - } - return key, decValue -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/backup/backup.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/backup/backup.go deleted file mode 100644 index a3564cbd..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/backup/backup.go +++ /dev/null @@ -1,376 +0,0 @@ -package backup - -import ( - "encoding/binary" - "encoding/json" - "errors" - "flag" - "fmt" - "io" - "io/ioutil" - "log" - "os" - "path/filepath" - "strconv" - "strings" - "time" - - "github.com/influxdata/influxdb/services/snapshotter" - "github.com/influxdata/influxdb/tcp" -) - -const ( - // Suffix is a suffix added to the backup while it's in-process. - Suffix = ".pending" - - // Metafile is the base name given to the metastore backups. - Metafile = "meta" - - // BackupFilePattern is the beginning of the pattern for a backup - // file. They follow the scheme ... - BackupFilePattern = "%s.%s.%05d" -) - -// Command represents the program execution for "influxd backup". -type Command struct { - // The logger passed to the ticker during execution. - Logger *log.Logger - - // Standard input/output, overridden for testing. - Stderr io.Writer - Stdout io.Writer - - host string - path string - database string -} - -// NewCommand returns a new instance of Command with default settings. -func NewCommand() *Command { - return &Command{ - Stderr: os.Stderr, - Stdout: os.Stdout, - } -} - -// Run executes the program. -func (cmd *Command) Run(args ...string) error { - // Set up logger. - cmd.Logger = log.New(cmd.Stderr, "", log.LstdFlags) - - // Parse command line arguments. - retentionPolicy, shardID, since, err := cmd.parseFlags(args) - if err != nil { - return err - } - - // based on the arguments passed in we only backup the minimum - if shardID != "" { - // always backup the metastore - if err := cmd.backupMetastore(); err != nil { - return err - } - err = cmd.backupShard(retentionPolicy, shardID, since) - } else if retentionPolicy != "" { - err = cmd.backupRetentionPolicy(retentionPolicy, since) - } else if cmd.database != "" { - err = cmd.backupDatabase(since) - } else { - err = cmd.backupMetastore() - } - - if err != nil { - cmd.Logger.Printf("backup failed: %v", err) - return err - } - - cmd.Logger.Println("backup complete") - - return nil -} - -// parseFlags parses and validates the command line arguments into a request object. -func (cmd *Command) parseFlags(args []string) (retentionPolicy, shardID string, since time.Time, err error) { - fs := flag.NewFlagSet("", flag.ContinueOnError) - - fs.StringVar(&cmd.host, "host", "localhost:8088", "") - fs.StringVar(&cmd.database, "database", "", "") - fs.StringVar(&retentionPolicy, "retention", "", "") - fs.StringVar(&shardID, "shard", "", "") - var sinceArg string - fs.StringVar(&sinceArg, "since", "", "") - - fs.SetOutput(cmd.Stderr) - fs.Usage = cmd.printUsage - - err = fs.Parse(args) - if err != nil { - return - } - if sinceArg != "" { - since, err = time.Parse(time.RFC3339, sinceArg) - if err != nil { - return - } - } - - // Ensure that only one arg is specified. - if fs.NArg() == 0 { - return "", "", time.Unix(0, 0), errors.New("backup destination path required") - } else if fs.NArg() != 1 { - return "", "", time.Unix(0, 0), errors.New("only one backup path allowed") - } - cmd.path = fs.Arg(0) - - err = os.MkdirAll(cmd.path, 0700) - - return -} - -// backupShard will write a tar archive of the passed in shard with any TSM files that have been -// created since the time passed in -func (cmd *Command) backupShard(retentionPolicy string, shardID string, since time.Time) error { - id, err := strconv.ParseUint(shardID, 10, 64) - if err != nil { - return err - } - - shardArchivePath, err := cmd.nextPath(filepath.Join(cmd.path, fmt.Sprintf(BackupFilePattern, cmd.database, retentionPolicy, id))) - if err != nil { - return err - } - - cmd.Logger.Printf("backing up db=%v rp=%v shard=%v to %s since %s", - cmd.database, retentionPolicy, shardID, shardArchivePath, since) - - req := &snapshotter.Request{ - Type: snapshotter.RequestShardBackup, - Database: cmd.database, - RetentionPolicy: retentionPolicy, - ShardID: id, - Since: since, - } - - // TODO: verify shard backup data - return cmd.downloadAndVerify(req, shardArchivePath, nil) -} - -// backupDatabase will request the database information from the server and then backup the metastore and -// every shard in every retention policy in the database. Each shard will be written to a separate tar. -func (cmd *Command) backupDatabase(since time.Time) error { - cmd.Logger.Printf("backing up db=%s since %s", cmd.database, since) - - req := &snapshotter.Request{ - Type: snapshotter.RequestDatabaseInfo, - Database: cmd.database, - } - - response, err := cmd.requestInfo(req) - if err != nil { - return err - } - - return cmd.backupResponsePaths(response, since) -} - -// backupRetentionPolicy will request the retention policy information from the server and then backup -// the metastore and every shard in the retention policy. Each shard will be written to a separate tar. -func (cmd *Command) backupRetentionPolicy(retentionPolicy string, since time.Time) error { - cmd.Logger.Printf("backing up rp=%s since %s", retentionPolicy, since) - - req := &snapshotter.Request{ - Type: snapshotter.RequestRetentionPolicyInfo, - Database: cmd.database, - RetentionPolicy: retentionPolicy, - } - - response, err := cmd.requestInfo(req) - if err != nil { - return err - } - - return cmd.backupResponsePaths(response, since) -} - -// backupResponsePaths will backup the metastore and all shard paths in the response struct -func (cmd *Command) backupResponsePaths(response *snapshotter.Response, since time.Time) error { - if err := cmd.backupMetastore(); err != nil { - return err - } - - // loop through the returned paths and back up each shard - for _, path := range response.Paths { - rp, id, err := retentionAndShardFromPath(path) - if err != nil { - return err - } - - if err := cmd.backupShard(rp, id, since); err != nil { - return err - } - } - - return nil -} - -// backupMetastore will backup the metastore on the host to the passed in path. Database and retention policy backups -// will force a backup of the metastore as well as requesting a specific shard backup from the command line -func (cmd *Command) backupMetastore() error { - metastoreArchivePath, err := cmd.nextPath(filepath.Join(cmd.path, Metafile)) - if err != nil { - return err - } - - cmd.Logger.Printf("backing up metastore to %s", metastoreArchivePath) - - req := &snapshotter.Request{ - Type: snapshotter.RequestMetastoreBackup, - } - - return cmd.downloadAndVerify(req, metastoreArchivePath, func(file string) error { - binData, err := ioutil.ReadFile(file) - if err != nil { - return err - } - - magic := binary.BigEndian.Uint64(binData[:8]) - if magic != snapshotter.BackupMagicHeader { - cmd.Logger.Println("Invalid metadata blob, ensure the metadata service is running (default port 8088)") - return errors.New("invalid metadata received") - } - - return nil - }) -} - -// nextPath returns the next file to write to. -func (cmd *Command) nextPath(path string) (string, error) { - // Iterate through incremental files until one is available. - for i := 0; ; i++ { - s := fmt.Sprintf(path+".%02d", i) - if _, err := os.Stat(s); os.IsNotExist(err) { - return s, nil - } else if err != nil { - return "", err - } - } -} - -// downloadAndVerify will download either the metastore or shard to a temp file and then -// rename it to a good backup file name after complete -func (cmd *Command) downloadAndVerify(req *snapshotter.Request, path string, validator func(string) error) error { - tmppath := path + Suffix - if err := cmd.download(req, tmppath); err != nil { - return err - } - - if validator != nil { - if err := validator(tmppath); err != nil { - if rmErr := os.Remove(tmppath); rmErr != nil { - cmd.Logger.Printf("Error cleaning up temporary file: %v", rmErr) - } - return err - } - } - - f, err := os.Stat(tmppath) - if err != nil { - return err - } - - // There was nothing downloaded, don't create an empty backup file. - if f.Size() == 0 { - return os.Remove(tmppath) - } - - // Rename temporary file to final path. - if err := os.Rename(tmppath, path); err != nil { - return fmt.Errorf("rename: %s", err) - } - - return nil -} - -// download downloads a snapshot of either the metastore or a shard from a host to a given path. -func (cmd *Command) download(req *snapshotter.Request, path string) error { - // Create local file to write to. - f, err := os.Create(path) - if err != nil { - return fmt.Errorf("open temp file: %s", err) - } - defer f.Close() - - // Connect to snapshotter service. - conn, err := tcp.Dial("tcp", cmd.host, snapshotter.MuxHeader) - if err != nil { - return err - } - defer conn.Close() - - // Write the request - if err := json.NewEncoder(conn).Encode(req); err != nil { - return fmt.Errorf("encode snapshot request: %s", err) - } - - // Read snapshot from the connection - if _, err := io.Copy(f, conn); err != nil { - return fmt.Errorf("copy backup to file: %s", err) - } - - return nil -} - -// requestInfo will request the database or retention policy information from the host -func (cmd *Command) requestInfo(request *snapshotter.Request) (*snapshotter.Response, error) { - // Connect to snapshotter service. - conn, err := tcp.Dial("tcp", cmd.host, snapshotter.MuxHeader) - if err != nil { - return nil, err - } - defer conn.Close() - - // Write the request - if err := json.NewEncoder(conn).Encode(request); err != nil { - return nil, fmt.Errorf("encode snapshot request: %s", err) - } - - // Read the response - var r snapshotter.Response - if err := json.NewDecoder(conn).Decode(&r); err != nil { - return nil, err - } - - return &r, nil -} - -// printUsage prints the usage message to STDERR. -func (cmd *Command) printUsage() { - fmt.Fprintf(cmd.Stdout, `Downloads a snapshot of a data node and saves it to disk. - -Usage: influxd backup [flags] PATH - - -host - The host to connect to snapshot. Defaults to 127.0.0.1:8088. - -database - The database to backup. - -retention - Optional. The retention policy to backup. - -shard - Optional. The shard id to backup. If specified, retention is required. - -since <2015-12-24T08:12:23> - Optional. Do an incremental backup since the passed in RFC3339 - formatted time. - -`) -} - -// retentionAndShardFromPath will take the shard relative path and split it into the -// retention policy name and shard ID. The first part of the path should be the database name. -func retentionAndShardFromPath(path string) (retention, shard string, err error) { - a := strings.Split(path, string(filepath.Separator)) - if len(a) != 3 { - return "", "", fmt.Errorf("expected database, retention policy, and shard id in path: %s", path) - } - - return a[1], a[2], nil -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/help/help.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/help/help.go deleted file mode 100644 index 34fddeb0..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/help/help.go +++ /dev/null @@ -1,45 +0,0 @@ -package help - -import ( - "fmt" - "io" - "os" - "strings" -) - -// Command displays help for command-line sub-commands. -type Command struct { - Stdout io.Writer -} - -// NewCommand returns a new instance of Command. -func NewCommand() *Command { - return &Command{ - Stdout: os.Stdout, - } -} - -// Run executes the command. -func (cmd *Command) Run(args ...string) error { - fmt.Fprintln(cmd.Stdout, strings.TrimSpace(usage)) - return nil -} - -const usage = ` -Configure and start an InfluxDB server. - -Usage: influxd [[command] [arguments]] - -The commands are: - - backup downloads a snapshot of a data node and saves it to disk - config display the default configuration - help display this help message - restore uses a snapshot of a data node to rebuild a cluster - run run node with existing configuration - version displays the InfluxDB version - -"run" is the default command. - -Use "influxd [command] -help" for more information about a command. -` diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/main.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/main.go deleted file mode 100644 index 8859d12e..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/main.go +++ /dev/null @@ -1,180 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io" - "math/rand" - "os" - "os/signal" - "syscall" - "time" - - "github.com/influxdata/influxdb/cmd" - "github.com/influxdata/influxdb/cmd/influxd/backup" - "github.com/influxdata/influxdb/cmd/influxd/help" - "github.com/influxdata/influxdb/cmd/influxd/restore" - "github.com/influxdata/influxdb/cmd/influxd/run" - "go.uber.org/zap" -) - -// These variables are populated via the Go linker. -var ( - version string - commit string - branch string -) - -func init() { - // If commit, branch, or build time are not set, make that clear. - if version == "" { - version = "unknown" - } - if commit == "" { - commit = "unknown" - } - if branch == "" { - branch = "unknown" - } -} - -func main() { - rand.Seed(time.Now().UnixNano()) - - m := NewMain() - if err := m.Run(os.Args[1:]...); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } -} - -// Main represents the program execution. -type Main struct { - Logger zap.Logger - - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer -} - -// NewMain return a new instance of Main. -func NewMain() *Main { - return &Main{ - Logger: zap.New( - zap.NewTextEncoder(), - zap.Output(os.Stderr), - ), - Stdin: os.Stdin, - Stdout: os.Stdout, - Stderr: os.Stderr, - } -} - -// Run determines and runs the command specified by the CLI args. -func (m *Main) Run(args ...string) error { - name, args := cmd.ParseCommandName(args) - - // Extract name from args. - switch name { - case "", "run": - cmd := run.NewCommand() - - // Tell the server the build details. - cmd.Version = version - cmd.Commit = commit - cmd.Branch = branch - cmd.Logger = m.Logger - - if err := cmd.Run(args...); err != nil { - return fmt.Errorf("run: %s", err) - } - - signalCh := make(chan os.Signal, 1) - signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM) - m.Logger.Info("Listening for signals") - - // Block until one of the signals above is received - select { - case <-signalCh: - m.Logger.Info("Signal received, initializing clean shutdown...") - go func() { - cmd.Close() - }() - } - - // Block again until another signal is received, a shutdown timeout elapses, - // or the Command is gracefully closed - m.Logger.Info("Waiting for clean shutdown...") - select { - case <-signalCh: - m.Logger.Info("second signal received, initializing hard shutdown") - case <-time.After(time.Second * 30): - m.Logger.Info("time limit reached, initializing hard shutdown") - case <-cmd.Closed: - m.Logger.Info("server shutdown completed") - } - - // goodbye. - - case "backup": - name := backup.NewCommand() - if err := name.Run(args...); err != nil { - return fmt.Errorf("backup: %s", err) - } - case "restore": - name := restore.NewCommand() - if err := name.Run(args...); err != nil { - return fmt.Errorf("restore: %s", err) - } - case "config": - if err := run.NewPrintConfigCommand().Run(args...); err != nil { - return fmt.Errorf("config: %s", err) - } - case "version": - if err := NewVersionCommand().Run(args...); err != nil { - return fmt.Errorf("version: %s", err) - } - case "help": - if err := help.NewCommand().Run(args...); err != nil { - return fmt.Errorf("help: %s", err) - } - default: - return fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'influxd help' for usage`+"\n\n", name) - } - - return nil -} - -// VersionCommand represents the command executed by "influxd version". -type VersionCommand struct { - Stdout io.Writer - Stderr io.Writer -} - -// NewVersionCommand return a new instance of VersionCommand. -func NewVersionCommand() *VersionCommand { - return &VersionCommand{ - Stdout: os.Stdout, - Stderr: os.Stderr, - } -} - -// Run prints the current version and commit info. -func (cmd *VersionCommand) Run(args ...string) error { - // Parse flags in case -h is specified. - fs := flag.NewFlagSet("", flag.ContinueOnError) - fs.Usage = func() { fmt.Fprintln(cmd.Stderr, versionUsage) } - if err := fs.Parse(args); err != nil { - return err - } - - // Print version info. - fmt.Fprintf(cmd.Stdout, "InfluxDB v%s (git: %s %s)\n", version, branch, commit) - - return nil -} - -var versionUsage = `Displays the InfluxDB version, build branch and git commit hash. - -Usage: influxd version -` diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/restore/restore.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/restore/restore.go deleted file mode 100644 index 9f1bb628..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/restore/restore.go +++ /dev/null @@ -1,386 +0,0 @@ -package restore - -import ( - "archive/tar" - "bytes" - "encoding/binary" - "errors" - "flag" - "fmt" - "io" - "io/ioutil" - "net" - "os" - "path/filepath" - "strconv" - "sync" - - "github.com/influxdata/influxdb/cmd/influxd/backup" - "github.com/influxdata/influxdb/services/meta" - "github.com/influxdata/influxdb/services/snapshotter" -) - -// Command represents the program execution for "influxd restore". -type Command struct { - Stdout io.Writer - Stderr io.Writer - - backupFilesPath string - metadir string - datadir string - database string - retention string - shard string - - // TODO: when the new meta stuff is done this should not be exported or be gone - MetaConfig *meta.Config -} - -// NewCommand returns a new instance of Command with default settings. -func NewCommand() *Command { - return &Command{ - Stdout: os.Stdout, - Stderr: os.Stderr, - MetaConfig: meta.NewConfig(), - } -} - -// Run executes the program. -func (cmd *Command) Run(args ...string) error { - if err := cmd.parseFlags(args); err != nil { - return err - } - - if cmd.metadir != "" { - if err := cmd.unpackMeta(); err != nil { - return err - } - } - - if cmd.shard != "" { - return cmd.unpackShard(cmd.shard) - } else if cmd.retention != "" { - return cmd.unpackRetention() - } else if cmd.datadir != "" { - return cmd.unpackDatabase() - } - return nil -} - -// parseFlags parses and validates the command line arguments. -func (cmd *Command) parseFlags(args []string) error { - fs := flag.NewFlagSet("", flag.ContinueOnError) - fs.StringVar(&cmd.metadir, "metadir", "", "") - fs.StringVar(&cmd.datadir, "datadir", "", "") - fs.StringVar(&cmd.database, "database", "", "") - fs.StringVar(&cmd.retention, "retention", "", "") - fs.StringVar(&cmd.shard, "shard", "", "") - fs.SetOutput(cmd.Stdout) - fs.Usage = cmd.printUsage - if err := fs.Parse(args); err != nil { - return err - } - - cmd.MetaConfig = meta.NewConfig() - cmd.MetaConfig.Dir = cmd.metadir - - // Require output path. - cmd.backupFilesPath = fs.Arg(0) - if cmd.backupFilesPath == "" { - return fmt.Errorf("path with backup files required") - } - - // validate the arguments - if cmd.metadir == "" && cmd.database == "" { - return fmt.Errorf("-metadir or -database are required to restore") - } - - if cmd.database != "" && cmd.datadir == "" { - return fmt.Errorf("-datadir is required to restore") - } - - if cmd.shard != "" { - if cmd.database == "" { - return fmt.Errorf("-database is required to restore shard") - } - if cmd.retention == "" { - return fmt.Errorf("-retention is required to restore shard") - } - } else if cmd.retention != "" && cmd.database == "" { - return fmt.Errorf("-database is required to restore retention policy") - } - - return nil -} - -// unpackMeta reads the metadata from the backup directory and initializes a raft -// cluster and replaces the root metadata. -func (cmd *Command) unpackMeta() error { - // find the meta file - metaFiles, err := filepath.Glob(filepath.Join(cmd.backupFilesPath, backup.Metafile+".*")) - if err != nil { - return err - } - - if len(metaFiles) == 0 { - return fmt.Errorf("no metastore backups in %s", cmd.backupFilesPath) - } - - latest := metaFiles[len(metaFiles)-1] - - fmt.Fprintf(cmd.Stdout, "Using metastore snapshot: %v\n", latest) - // Read the metastore backup - f, err := os.Open(latest) - if err != nil { - return err - } - - var buf bytes.Buffer - if _, err := io.Copy(&buf, f); err != nil { - return fmt.Errorf("copy: %s", err) - } - - b := buf.Bytes() - var i int - - // Make sure the file is actually a meta store backup file - magic := binary.BigEndian.Uint64(b[:8]) - if magic != snapshotter.BackupMagicHeader { - return fmt.Errorf("invalid metadata file") - } - i += 8 - - // Size of the meta store bytes - length := int(binary.BigEndian.Uint64(b[i : i+8])) - i += 8 - metaBytes := b[i : i+length] - i += int(length) - - // Size of the node.json bytes - length = int(binary.BigEndian.Uint64(b[i : i+8])) - i += 8 - nodeBytes := b[i:] - - // Unpack into metadata. - var data meta.Data - if err := data.UnmarshalBinary(metaBytes); err != nil { - return fmt.Errorf("unmarshal: %s", err) - } - - // Copy meta config and remove peers so it starts in single mode. - c := cmd.MetaConfig - c.Dir = cmd.metadir - - // Create the meta dir - if os.MkdirAll(c.Dir, 0700); err != nil { - return err - } - - // Write node.json back to meta dir - if err := ioutil.WriteFile(filepath.Join(c.Dir, "node.json"), nodeBytes, 0655); err != nil { - return err - } - - client := meta.NewClient(c) - if err := client.Open(); err != nil { - return err - } - defer client.Close() - - // Force set the full metadata. - if err := client.SetData(&data); err != nil { - return fmt.Errorf("set data: %s", err) - } - - // remove the raft.db file if it exists - err = os.Remove(filepath.Join(cmd.metadir, "raft.db")) - if err != nil { - if os.IsNotExist(err) { - return nil - } - return err - } - - // remove the node.json file if it exists - err = os.Remove(filepath.Join(cmd.metadir, "node.json")) - if err != nil { - if os.IsNotExist(err) { - return nil - } - return err - } - - return nil -} - -// unpackShard will look for all backup files in the path matching this shard ID -// and restore them to the data dir -func (cmd *Command) unpackShard(shardID string) error { - // make sure the shard isn't already there so we don't clobber anything - restorePath := filepath.Join(cmd.datadir, cmd.database, cmd.retention, shardID) - if _, err := os.Stat(restorePath); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("shard already present: %s", restorePath) - } - - id, err := strconv.ParseUint(shardID, 10, 64) - if err != nil { - return err - } - - // find the shard backup files - pat := filepath.Join(cmd.backupFilesPath, fmt.Sprintf(backup.BackupFilePattern, cmd.database, cmd.retention, id)) - return cmd.unpackFiles(pat + ".*") -} - -// unpackDatabase will look for all backup files in the path matching this database -// and restore them to the data dir -func (cmd *Command) unpackDatabase() error { - // make sure the shard isn't already there so we don't clobber anything - restorePath := filepath.Join(cmd.datadir, cmd.database) - if _, err := os.Stat(restorePath); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("database already present: %s", restorePath) - } - - // find the database backup files - pat := filepath.Join(cmd.backupFilesPath, cmd.database) - return cmd.unpackFiles(pat + ".*") -} - -// unpackRetention will look for all backup files in the path matching this retention -// and restore them to the data dir -func (cmd *Command) unpackRetention() error { - // make sure the shard isn't already there so we don't clobber anything - restorePath := filepath.Join(cmd.datadir, cmd.database, cmd.retention) - if _, err := os.Stat(restorePath); err != nil && !os.IsNotExist(err) { - return fmt.Errorf("retention already present: %s", restorePath) - } - - // find the retention backup files - pat := filepath.Join(cmd.backupFilesPath, cmd.database) - return cmd.unpackFiles(fmt.Sprintf("%s.%s.*", pat, cmd.retention)) -} - -// unpackFiles will look for backup files matching the pattern and restore them to the data dir -func (cmd *Command) unpackFiles(pat string) error { - fmt.Printf("Restoring from backup %s\n", pat) - - backupFiles, err := filepath.Glob(pat) - if err != nil { - return err - } - - if len(backupFiles) == 0 { - return fmt.Errorf("no backup files for %s in %s", pat, cmd.backupFilesPath) - } - - for _, fn := range backupFiles { - if err := cmd.unpackTar(fn); err != nil { - return err - } - } - - return nil -} - -// unpackTar will restore a single tar archive to the data dir -func (cmd *Command) unpackTar(tarFile string) error { - f, err := os.Open(tarFile) - if err != nil { - return err - } - defer f.Close() - - tr := tar.NewReader(f) - - for { - hdr, err := tr.Next() - if err == io.EOF { - return nil - } else if err != nil { - return err - } - - if err := cmd.unpackFile(tr, hdr.Name); err != nil { - return err - } - } -} - -// unpackFile will copy the current file from the tar archive to the data dir -func (cmd *Command) unpackFile(tr *tar.Reader, fileName string) error { - nativeFileName := filepath.FromSlash(fileName) - fn := filepath.Join(cmd.datadir, nativeFileName) - fmt.Printf("unpacking %s\n", fn) - - if err := os.MkdirAll(filepath.Dir(fn), 0777); err != nil { - return fmt.Errorf("error making restore dir: %s", err.Error()) - } - - ff, err := os.Create(fn) - if err != nil { - return err - } - defer ff.Close() - - if _, err := io.Copy(ff, tr); err != nil { - return err - } - - return nil -} - -// printUsage prints the usage message to STDERR. -func (cmd *Command) printUsage() { - fmt.Fprintf(cmd.Stdout, `Uses backups from the PATH to restore the metastore, databases, -retention policies, or specific shards. The InfluxDB process must not be -running during a restore. - -Usage: influxd restore [flags] PATH - - -metadir - Optional. If set the metastore will be recovered to the given path. - -datadir - Optional. If set the restore process will recover the specified - database, retention policy or shard to the given directory. - -database - Optional. Required if no metadir given. Will restore the database - TSM files. - -retention - Optional. If given, database is required. Will restore the retention policy's - TSM files. - -shard - Optional. If given, database and retention are required. Will restore the shard's - TSM files. - -`) -} - -type nopListener struct { - mu sync.Mutex - closing chan struct{} -} - -func newNopListener() *nopListener { - return &nopListener{closing: make(chan struct{})} -} - -func (ln *nopListener) Accept() (net.Conn, error) { - ln.mu.Lock() - defer ln.mu.Unlock() - - <-ln.closing - return nil, errors.New("listener closing") -} - -func (ln *nopListener) Close() error { - if ln.closing != nil { - close(ln.closing) - ln.mu.Lock() - defer ln.mu.Unlock() - - ln.closing = nil - } - return nil -} - -func (ln *nopListener) Addr() net.Addr { return &net.TCPAddr{} } diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/backup_restore_test.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/backup_restore_test.go deleted file mode 100644 index c6b419d8..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/backup_restore_test.go +++ /dev/null @@ -1,106 +0,0 @@ -package run_test - -import ( - "io/ioutil" - "net" - "os" - "path/filepath" - "testing" - "time" - - "github.com/influxdata/influxdb/cmd/influxd/backup" - "github.com/influxdata/influxdb/cmd/influxd/restore" -) - -func TestServer_BackupAndRestore(t *testing.T) { - t.Skip("currently fails intermittently. See issue https://github.com/influxdata/influxdb/issues/6590") - config := NewConfig() - config.Data.Engine = "tsm1" - config.Data.Dir, _ = ioutil.TempDir("", "data_backup") - config.Meta.Dir, _ = ioutil.TempDir("", "meta_backup") - config.BindAddress = freePort() - - backupDir, _ := ioutil.TempDir("", "backup") - defer os.RemoveAll(backupDir) - - db := "mydb" - rp := "forever" - expected := `{"results":[{"series":[{"name":"myseries","columns":["time","host","value"],"values":[["1970-01-01T00:00:00.001Z","A",23]]}]}]}` - - // set the cache snapshot size low so that a single point will cause TSM file creation - config.Data.CacheSnapshotMemorySize = 1 - - func() { - s := OpenServer(config) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy(db, newRetentionPolicySpec(rp, 1, 0), true); err != nil { - t.Fatal(err) - } - - if _, err := s.Write(db, rp, "myseries,host=A value=23 1000000", nil); err != nil { - t.Fatalf("failed to write: %s", err) - } - - // wait for the snapshot to write - time.Sleep(time.Second) - - res, err := s.Query(`select * from "mydb"."forever"."myseries"`) - if err != nil { - t.Fatalf("error querying: %s", err.Error()) - } - if res != expected { - t.Fatalf("query results wrong:\n\texp: %s\n\tgot: %s", expected, res) - } - - // now backup - cmd := backup.NewCommand() - _, port, err := net.SplitHostPort(config.BindAddress) - if err != nil { - t.Fatal(err) - } - hostAddress := net.JoinHostPort("localhost", port) - if err := cmd.Run("-host", hostAddress, "-database", "mydb", backupDir); err != nil { - t.Fatalf("error backing up: %s, hostAddress: %s", err.Error(), hostAddress) - } - }() - - if _, err := os.Stat(config.Meta.Dir); err == nil || !os.IsNotExist(err) { - t.Fatalf("meta dir should be deleted") - } - - if _, err := os.Stat(config.Data.Dir); err == nil || !os.IsNotExist(err) { - t.Fatalf("meta dir should be deleted") - } - - // restore - cmd := restore.NewCommand() - - if err := cmd.Run("-metadir", config.Meta.Dir, "-datadir", config.Data.Dir, "-database", "mydb", backupDir); err != nil { - t.Fatalf("error restoring: %s", err.Error()) - } - - // Make sure node.json was restored - nodePath := filepath.Join(config.Meta.Dir, "node.json") - if _, err := os.Stat(nodePath); err != nil || os.IsNotExist(err) { - t.Fatalf("node.json should exist") - } - - // now open it up and verify we're good - s := OpenServer(config) - defer s.Close() - - res, err := s.Query(`select * from "mydb"."forever"."myseries"`) - if err != nil { - t.Fatalf("error querying: %s", err.Error()) - } - if res != expected { - t.Fatalf("query results wrong:\n\texp: %s\n\tgot: %s", expected, res) - } -} - -func freePort() string { - l, _ := net.Listen("tcp", "") - defer l.Close() - return l.Addr().String() -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/command.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/command.go deleted file mode 100644 index 6f10fb25..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/command.go +++ /dev/null @@ -1,260 +0,0 @@ -package run - -import ( - "flag" - "fmt" - "io" - "io/ioutil" - "log" - "os" - "path/filepath" - "runtime" - "strconv" - "time" - - "go.uber.org/zap" -) - -const logo = ` - 8888888 .d888 888 8888888b. 888888b. - 888 d88P" 888 888 "Y88b 888 "88b - 888 888 888 888 888 888 .88P - 888 88888b. 888888 888 888 888 888 888 888 888 8888888K. - 888 888 "88b 888 888 888 888 Y8bd8P' 888 888 888 "Y88b - 888 888 888 888 888 888 888 X88K 888 888 888 888 - 888 888 888 888 888 Y88b 888 .d8""8b. 888 .d88P 888 d88P - 8888888 888 888 888 888 "Y88888 888 888 8888888P" 8888888P" - -` - -// Command represents the command executed by "influxd run". -type Command struct { - Version string - Branch string - Commit string - BuildTime string - - closing chan struct{} - Closed chan struct{} - - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer - Logger zap.Logger - - Server *Server -} - -// NewCommand return a new instance of Command. -func NewCommand() *Command { - return &Command{ - closing: make(chan struct{}), - Closed: make(chan struct{}), - Stdin: os.Stdin, - Stdout: os.Stdout, - Stderr: os.Stderr, - Logger: zap.New(zap.NullEncoder()), - } -} - -// Run parses the config from args and runs the server. -func (cmd *Command) Run(args ...string) error { - // Parse the command line flags. - options, err := cmd.ParseFlags(args...) - if err != nil { - return err - } - - // Print sweet InfluxDB logo. - fmt.Print(logo) - - // Mark start-up in log. - cmd.Logger.Info(fmt.Sprintf("InfluxDB starting, version %s, branch %s, commit %s", - cmd.Version, cmd.Branch, cmd.Commit)) - cmd.Logger.Info(fmt.Sprintf("Go version %s, GOMAXPROCS set to %d", runtime.Version(), runtime.GOMAXPROCS(0))) - - // Write the PID file. - if err := cmd.writePIDFile(options.PIDFile); err != nil { - return fmt.Errorf("write pid file: %s", err) - } - - // Parse config - config, err := cmd.ParseConfig(options.GetConfigPath()) - if err != nil { - return fmt.Errorf("parse config: %s", err) - } - - // Apply any environment variables on top of the parsed config - if err := config.ApplyEnvOverrides(); err != nil { - return fmt.Errorf("apply env config: %v", err) - } - - // Validate the configuration. - if err := config.Validate(); err != nil { - return fmt.Errorf("%s. To generate a valid configuration file run `influxd config > influxdb.generated.conf`", err) - } - - if config.HTTPD.PprofEnabled { - // Turn on block profiling to debug stuck databases - runtime.SetBlockProfileRate(int(1 * time.Second)) - } - - // Create server from config and start it. - buildInfo := &BuildInfo{ - Version: cmd.Version, - Commit: cmd.Commit, - Branch: cmd.Branch, - Time: cmd.BuildTime, - } - s, err := NewServer(config, buildInfo) - if err != nil { - return fmt.Errorf("create server: %s", err) - } - s.Logger = cmd.Logger - s.CPUProfile = options.CPUProfile - s.MemProfile = options.MemProfile - if err := s.Open(); err != nil { - return fmt.Errorf("open server: %s", err) - } - cmd.Server = s - - // Begin monitoring the server's error channel. - go cmd.monitorServerErrors() - - return nil -} - -// Close shuts down the server. -func (cmd *Command) Close() error { - defer close(cmd.Closed) - close(cmd.closing) - if cmd.Server != nil { - return cmd.Server.Close() - } - return nil -} - -func (cmd *Command) monitorServerErrors() { - logger := log.New(cmd.Stderr, "", log.LstdFlags) - for { - select { - case err := <-cmd.Server.Err(): - logger.Println(err) - case <-cmd.closing: - return - } - } -} - -// ParseFlags parses the command line flags from args and returns an options set. -func (cmd *Command) ParseFlags(args ...string) (Options, error) { - var options Options - fs := flag.NewFlagSet("", flag.ContinueOnError) - fs.StringVar(&options.ConfigPath, "config", "", "") - fs.StringVar(&options.PIDFile, "pidfile", "", "") - // Ignore hostname option. - _ = fs.String("hostname", "", "") - fs.StringVar(&options.CPUProfile, "cpuprofile", "", "") - fs.StringVar(&options.MemProfile, "memprofile", "", "") - fs.Usage = func() { fmt.Fprintln(cmd.Stderr, usage) } - if err := fs.Parse(args); err != nil { - return Options{}, err - } - return options, nil -} - -// writePIDFile writes the process ID to path. -func (cmd *Command) writePIDFile(path string) error { - // Ignore if path is not set. - if path == "" { - return nil - } - - // Ensure the required directory structure exists. - err := os.MkdirAll(filepath.Dir(path), 0777) - if err != nil { - return fmt.Errorf("mkdir: %s", err) - } - - // Retrieve the PID and write it. - pid := strconv.Itoa(os.Getpid()) - if err := ioutil.WriteFile(path, []byte(pid), 0666); err != nil { - return fmt.Errorf("write file: %s", err) - } - - return nil -} - -// ParseConfig parses the config at path. -// Returns a demo configuration if path is blank. -func (cmd *Command) ParseConfig(path string) (*Config, error) { - // Use demo configuration if no config path is specified. - if path == "" { - cmd.Logger.Info("no configuration provided, using default settings") - return NewDemoConfig() - } - - cmd.Logger.Info(fmt.Sprintf("Using configuration at: %s\n", path)) - - config := NewConfig() - if err := config.FromTomlFile(path); err != nil { - return nil, err - } - - return config, nil -} - -var usage = `Runs the InfluxDB server. - -Usage: influxd run [flags] - - -config - Set the path to the configuration file. - This defaults to the environment variable INFLUXDB_CONFIG_PATH, - ~/.influxdb/influxdb.conf, or /etc/influxdb/influxdb.conf if a file - is present at any of these locations. - Disable the automatic loading of a configuration file using - the null device (such as /dev/null). - -pidfile - Write process ID to a file. - -cpuprofile - Write CPU profiling information to a file. - -memprofile - Write memory usage information to a file. -` - -// Options represents the command line options that can be parsed. -type Options struct { - ConfigPath string - PIDFile string - CPUProfile string - MemProfile string -} - -// GetConfigPath returns the config path from the options. -// It will return a path by searching in this order: -// 1. The CLI option in ConfigPath -// 2. The environment variable INFLUXDB_CONFIG_PATH -// 3. The first influxdb.conf file on the path: -// - ~/.influxdb -// - /etc/influxdb -func (opt *Options) GetConfigPath() string { - if opt.ConfigPath != "" { - if opt.ConfigPath == os.DevNull { - return "" - } - return opt.ConfigPath - } else if envVar := os.Getenv("INFLUXDB_CONFIG_PATH"); envVar != "" { - return envVar - } - - for _, path := range []string{ - os.ExpandEnv("${HOME}/.influxdb/influxdb.conf"), - "/etc/influxdb/influxdb.conf", - } { - if _, err := os.Stat(path); err == nil { - return path - } - } - return "" -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/config.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/config.go deleted file mode 100644 index 71e3d6a7..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/config.go +++ /dev/null @@ -1,294 +0,0 @@ -package run - -import ( - "bytes" - "fmt" - "io/ioutil" - "log" - "os" - "os/user" - "path/filepath" - "reflect" - "regexp" - "strconv" - "strings" - "time" - - "github.com/BurntSushi/toml" - "github.com/influxdata/influxdb/coordinator" - "github.com/influxdata/influxdb/monitor" - "github.com/influxdata/influxdb/services/admin" - "github.com/influxdata/influxdb/services/collectd" - "github.com/influxdata/influxdb/services/continuous_querier" - "github.com/influxdata/influxdb/services/graphite" - "github.com/influxdata/influxdb/services/httpd" - "github.com/influxdata/influxdb/services/meta" - "github.com/influxdata/influxdb/services/opentsdb" - "github.com/influxdata/influxdb/services/precreator" - "github.com/influxdata/influxdb/services/retention" - "github.com/influxdata/influxdb/services/subscriber" - "github.com/influxdata/influxdb/services/udp" - "github.com/influxdata/influxdb/tsdb" -) - -const ( - // DefaultBindAddress is the default address for various RPC services. - DefaultBindAddress = ":8088" -) - -// Config represents the configuration format for the influxd binary. -type Config struct { - Meta *meta.Config `toml:"meta"` - Data tsdb.Config `toml:"data"` - Coordinator coordinator.Config `toml:"coordinator"` - Retention retention.Config `toml:"retention"` - Precreator precreator.Config `toml:"shard-precreation"` - - Admin admin.Config `toml:"admin"` - Monitor monitor.Config `toml:"monitor"` - Subscriber subscriber.Config `toml:"subscriber"` - HTTPD httpd.Config `toml:"http"` - GraphiteInputs []graphite.Config `toml:"graphite"` - CollectdInputs []collectd.Config `toml:"collectd"` - OpenTSDBInputs []opentsdb.Config `toml:"opentsdb"` - UDPInputs []udp.Config `toml:"udp"` - - ContinuousQuery continuous_querier.Config `toml:"continuous_queries"` - - // Server reporting - ReportingDisabled bool `toml:"reporting-disabled"` - - // BindAddress is the address that all TCP services use (Raft, Snapshot, Cluster, etc.) - BindAddress string `toml:"bind-address"` -} - -// NewConfig returns an instance of Config with reasonable defaults. -func NewConfig() *Config { - c := &Config{} - c.Meta = meta.NewConfig() - c.Data = tsdb.NewConfig() - c.Coordinator = coordinator.NewConfig() - c.Precreator = precreator.NewConfig() - - c.Admin = admin.NewConfig() - c.Monitor = monitor.NewConfig() - c.Subscriber = subscriber.NewConfig() - c.HTTPD = httpd.NewConfig() - - c.GraphiteInputs = []graphite.Config{graphite.NewConfig()} - c.CollectdInputs = []collectd.Config{collectd.NewConfig()} - c.OpenTSDBInputs = []opentsdb.Config{opentsdb.NewConfig()} - c.UDPInputs = []udp.Config{udp.NewConfig()} - - c.ContinuousQuery = continuous_querier.NewConfig() - c.Retention = retention.NewConfig() - c.BindAddress = DefaultBindAddress - - return c -} - -// NewDemoConfig returns the config that runs when no config is specified. -func NewDemoConfig() (*Config, error) { - c := NewConfig() - - var homeDir string - // By default, store meta and data files in current users home directory - u, err := user.Current() - if err == nil { - homeDir = u.HomeDir - } else if os.Getenv("HOME") != "" { - homeDir = os.Getenv("HOME") - } else { - return nil, fmt.Errorf("failed to determine current user for storage") - } - - c.Meta.Dir = filepath.Join(homeDir, ".influxdb/meta") - c.Data.Dir = filepath.Join(homeDir, ".influxdb/data") - c.Data.WALDir = filepath.Join(homeDir, ".influxdb/wal") - - return c, nil -} - -// trimBOM trims the Byte-Order-Marks from the beginning of the file. -// this is for Windows compatability only. -// see https://github.com/influxdata/telegraf/issues/1378 -func trimBOM(f []byte) []byte { - return bytes.TrimPrefix(f, []byte("\xef\xbb\xbf")) -} - -// FromTomlFile loads the config from a TOML file. -func (c *Config) FromTomlFile(fpath string) error { - bs, err := ioutil.ReadFile(fpath) - if err != nil { - return err - } - bs = trimBOM(bs) - return c.FromToml(string(bs)) -} - -// FromToml loads the config from TOML. -func (c *Config) FromToml(input string) error { - // Replace deprecated [cluster] with [coordinator] - re := regexp.MustCompile(`(?m)^\s*\[cluster\]`) - input = re.ReplaceAllStringFunc(input, func(in string) string { - in = strings.TrimSpace(in) - out := "[coordinator]" - log.Printf("deprecated config option %s replaced with %s; %s will not be supported in a future release\n", in, out, in) - return out - }) - - _, err := toml.Decode(input, c) - return err -} - -// Validate returns an error if the config is invalid. -func (c *Config) Validate() error { - if err := c.Meta.Validate(); err != nil { - return err - } - - if err := c.Data.Validate(); err != nil { - return err - } - - if err := c.Monitor.Validate(); err != nil { - return err - } - - if err := c.Subscriber.Validate(); err != nil { - return err - } - - for _, graphite := range c.GraphiteInputs { - if err := graphite.Validate(); err != nil { - return fmt.Errorf("invalid graphite config: %v", err) - } - } - - for _, collectd := range c.CollectdInputs { - if err := collectd.Validate(); err != nil { - return fmt.Errorf("invalid collectd config: %v", err) - } - } - - return nil -} - -// ApplyEnvOverrides apply the environment configuration on top of the config. -func (c *Config) ApplyEnvOverrides() error { - return c.applyEnvOverrides("INFLUXDB", reflect.ValueOf(c)) -} - -func (c *Config) applyEnvOverrides(prefix string, spec reflect.Value) error { - // If we have a pointer, dereference it - s := spec - if spec.Kind() == reflect.Ptr { - s = spec.Elem() - } - - // Make sure we have struct - if s.Kind() != reflect.Struct { - return nil - } - - typeOfSpec := s.Type() - for i := 0; i < s.NumField(); i++ { - f := s.Field(i) - // Get the toml tag to determine what env var name to use - configName := typeOfSpec.Field(i).Tag.Get("toml") - // Replace hyphens with underscores to avoid issues with shells - configName = strings.Replace(configName, "-", "_", -1) - fieldKey := typeOfSpec.Field(i).Name - - // Skip any fields that we cannot set - if f.CanSet() || f.Kind() == reflect.Slice { - - // Use the upper-case prefix and toml name for the env var - key := strings.ToUpper(configName) - if prefix != "" { - key = strings.ToUpper(fmt.Sprintf("%s_%s", prefix, configName)) - } - value := os.Getenv(key) - - // If the type is s slice, apply to each using the index as a suffix - // e.g. GRAPHITE_0 - if f.Kind() == reflect.Slice || f.Kind() == reflect.Array { - for i := 0; i < f.Len(); i++ { - if err := c.applyEnvOverrides(key, f.Index(i)); err != nil { - return err - } - if err := c.applyEnvOverrides(fmt.Sprintf("%s_%d", key, i), f.Index(i)); err != nil { - return err - } - } - continue - } - - // If it's a sub-config, recursively apply - if f.Kind() == reflect.Struct || f.Kind() == reflect.Ptr { - if err := c.applyEnvOverrides(key, f); err != nil { - return err - } - continue - } - - // Skip any fields we don't have a value to set - if value == "" { - continue - } - - switch f.Kind() { - case reflect.String: - f.SetString(value) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - - var intValue int64 - - // Handle toml.Duration - if f.Type().Name() == "Duration" { - dur, err := time.ParseDuration(value) - if err != nil { - return fmt.Errorf("failed to apply %v to %v using type %v and value '%v'", key, fieldKey, f.Type().String(), value) - } - intValue = dur.Nanoseconds() - } else { - var err error - intValue, err = strconv.ParseInt(value, 0, f.Type().Bits()) - if err != nil { - return fmt.Errorf("failed to apply %v to %v using type %v and value '%v'", key, fieldKey, f.Type().String(), value) - } - } - - f.SetInt(intValue) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - var intValue uint64 - var err error - intValue, err = strconv.ParseUint(value, 0, f.Type().Bits()) - if err != nil { - return fmt.Errorf("failed to apply %v to %v using type %v and value '%v'", key, fieldKey, f.Type().String(), value) - } - - f.SetUint(intValue) - case reflect.Bool: - boolValue, err := strconv.ParseBool(value) - if err != nil { - return fmt.Errorf("failed to apply %v to %v using type %v and value '%v'", key, fieldKey, f.Type().String(), value) - - } - f.SetBool(boolValue) - case reflect.Float32, reflect.Float64: - floatValue, err := strconv.ParseFloat(value, f.Type().Bits()) - if err != nil { - return fmt.Errorf("failed to apply %v to %v using type %v and value '%v'", key, fieldKey, f.Type().String(), value) - - } - f.SetFloat(floatValue) - default: - if err := c.applyEnvOverrides(key, f); err != nil { - return err - } - } - } - } - return nil -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/config_command.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/config_command.go deleted file mode 100644 index de945c56..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/config_command.go +++ /dev/null @@ -1,92 +0,0 @@ -package run - -import ( - "flag" - "fmt" - "io" - "os" - - "github.com/BurntSushi/toml" -) - -// PrintConfigCommand represents the command executed by "influxd config". -type PrintConfigCommand struct { - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer -} - -// NewPrintConfigCommand return a new instance of PrintConfigCommand. -func NewPrintConfigCommand() *PrintConfigCommand { - return &PrintConfigCommand{ - Stdin: os.Stdin, - Stdout: os.Stdout, - Stderr: os.Stderr, - } -} - -// Run parses and prints the current config loaded. -func (cmd *PrintConfigCommand) Run(args ...string) error { - // Parse command flags. - fs := flag.NewFlagSet("", flag.ContinueOnError) - configPath := fs.String("config", "", "") - fs.Usage = func() { fmt.Fprintln(cmd.Stderr, printConfigUsage) } - if err := fs.Parse(args); err != nil { - return err - } - - // Parse config from path. - opt := Options{ConfigPath: *configPath} - config, err := cmd.parseConfig(opt.GetConfigPath()) - if err != nil { - return fmt.Errorf("parse config: %s", err) - } - - // Apply any environment variables on top of the parsed config - if err := config.ApplyEnvOverrides(); err != nil { - return fmt.Errorf("apply env config: %v", err) - } - - // Validate the configuration. - if err := config.Validate(); err != nil { - return fmt.Errorf("%s. To generate a valid configuration file run `influxd config > influxdb.generated.conf`", err) - } - - toml.NewEncoder(cmd.Stdout).Encode(config) - fmt.Fprint(cmd.Stdout, "\n") - - return nil -} - -// ParseConfig parses the config at path. -// Returns a demo configuration if path is blank. -func (cmd *PrintConfigCommand) parseConfig(path string) (*Config, error) { - config, err := NewDemoConfig() - if err != nil { - config = NewConfig() - } - - if path == "" { - return config, nil - } - - fmt.Fprintf(os.Stderr, "Merging with configuration at: %s\n", path) - - if err := config.FromTomlFile(path); err != nil { - return nil, err - } - return config, nil -} - -var printConfigUsage = `Displays the default configuration. - -Usage: influxd config [flags] - - -config - Set the path to the initial configuration file. - This defaults to the environment variable INFLUXDB_CONFIG_PATH, - ~/.influxdb/influxdb.conf, or /etc/influxdb/influxdb.conf if a file - is present at any of these locations. - Disable the automatic loading of a configuration file using - the null device (such as /dev/null). -` diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/config_test.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/config_test.go deleted file mode 100644 index ef261859..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/config_test.go +++ /dev/null @@ -1,257 +0,0 @@ -package run_test - -import ( - "os" - "testing" - - "github.com/BurntSushi/toml" - "github.com/influxdata/influxdb/cmd/influxd/run" -) - -// Ensure the configuration can be parsed. -func TestConfig_Parse(t *testing.T) { - // Parse configuration. - var c run.Config - if err := c.FromToml(` -[meta] -dir = "/tmp/meta" - -[data] -dir = "/tmp/data" - -[coordinator] - -[admin] -bind-address = ":8083" - -[http] -bind-address = ":8087" - -[[graphite]] -protocol = "udp" - -[[graphite]] -protocol = "tcp" - -[[collectd]] -bind-address = ":1000" - -[[collectd]] -bind-address = ":1010" - -[[opentsdb]] -bind-address = ":2000" - -[[opentsdb]] -bind-address = ":2010" - -[[opentsdb]] -bind-address = ":2020" - -[[udp]] -bind-address = ":4444" - -[monitoring] -enabled = true - -[subscriber] -enabled = true - -[continuous_queries] -enabled = true -`); err != nil { - t.Fatal(err) - } - - // Validate configuration. - if c.Meta.Dir != "/tmp/meta" { - t.Fatalf("unexpected meta dir: %s", c.Meta.Dir) - } else if c.Data.Dir != "/tmp/data" { - t.Fatalf("unexpected data dir: %s", c.Data.Dir) - } else if c.Admin.BindAddress != ":8083" { - t.Fatalf("unexpected admin bind address: %s", c.Admin.BindAddress) - } else if c.HTTPD.BindAddress != ":8087" { - t.Fatalf("unexpected api bind address: %s", c.HTTPD.BindAddress) - } else if len(c.GraphiteInputs) != 2 { - t.Fatalf("unexpected graphiteInputs count: %d", len(c.GraphiteInputs)) - } else if c.GraphiteInputs[0].Protocol != "udp" { - t.Fatalf("unexpected graphite protocol(0): %s", c.GraphiteInputs[0].Protocol) - } else if c.GraphiteInputs[1].Protocol != "tcp" { - t.Fatalf("unexpected graphite protocol(1): %s", c.GraphiteInputs[1].Protocol) - } else if c.CollectdInputs[0].BindAddress != ":1000" { - t.Fatalf("unexpected collectd bind address: %s", c.CollectdInputs[0].BindAddress) - } else if c.CollectdInputs[1].BindAddress != ":1010" { - t.Fatalf("unexpected collectd bind address: %s", c.CollectdInputs[1].BindAddress) - } else if c.OpenTSDBInputs[0].BindAddress != ":2000" { - t.Fatalf("unexpected opentsdb bind address: %s", c.OpenTSDBInputs[0].BindAddress) - } else if c.OpenTSDBInputs[1].BindAddress != ":2010" { - t.Fatalf("unexpected opentsdb bind address: %s", c.OpenTSDBInputs[1].BindAddress) - } else if c.OpenTSDBInputs[2].BindAddress != ":2020" { - t.Fatalf("unexpected opentsdb bind address: %s", c.OpenTSDBInputs[2].BindAddress) - } else if c.UDPInputs[0].BindAddress != ":4444" { - t.Fatalf("unexpected udp bind address: %s", c.UDPInputs[0].BindAddress) - } else if c.Subscriber.Enabled != true { - t.Fatalf("unexpected subscriber enabled: %v", c.Subscriber.Enabled) - } else if c.ContinuousQuery.Enabled != true { - t.Fatalf("unexpected continuous query enabled: %v", c.ContinuousQuery.Enabled) - } -} - -// Ensure the configuration can be parsed. -func TestConfig_Parse_EnvOverride(t *testing.T) { - // Parse configuration. - var c run.Config - if _, err := toml.Decode(` -[meta] -dir = "/tmp/meta" - -[data] -dir = "/tmp/data" - -[coordinator] - -[admin] -bind-address = ":8083" - -[http] -bind-address = ":8087" - -[[graphite]] -protocol = "udp" - -[[graphite]] -protocol = "tcp" - -[[collectd]] -bind-address = ":1000" - -[[collectd]] -bind-address = ":1010" - -[[opentsdb]] -bind-address = ":2000" - -[[opentsdb]] -bind-address = ":2010" - -[[udp]] -bind-address = ":4444" - -[[udp]] - -[monitoring] -enabled = true - -[continuous_queries] -enabled = true -`, &c); err != nil { - t.Fatal(err) - } - - if err := os.Setenv("INFLUXDB_UDP_BIND_ADDRESS", ":1234"); err != nil { - t.Fatalf("failed to set env var: %v", err) - } - - if err := os.Setenv("INFLUXDB_UDP_0_BIND_ADDRESS", ":5555"); err != nil { - t.Fatalf("failed to set env var: %v", err) - } - - if err := os.Setenv("INFLUXDB_GRAPHITE_1_PROTOCOL", "udp"); err != nil { - t.Fatalf("failed to set env var: %v", err) - } - - if err := os.Setenv("INFLUXDB_COLLECTD_1_BIND_ADDRESS", ":1020"); err != nil { - t.Fatalf("failed to set env var: %v", err) - } - - if err := os.Setenv("INFLUXDB_OPENTSDB_0_BIND_ADDRESS", ":2020"); err != nil { - t.Fatalf("failed to set env var: %v", err) - } - - // uint64 type - if err := os.Setenv("INFLUXDB_DATA_CACHE_MAX_MEMORY_SIZE", "1000"); err != nil { - t.Fatalf("failed to set env var: %v", err) - } - - if err := c.ApplyEnvOverrides(); err != nil { - t.Fatalf("failed to apply env overrides: %v", err) - } - - if c.UDPInputs[0].BindAddress != ":5555" { - t.Fatalf("unexpected udp bind address: %s", c.UDPInputs[0].BindAddress) - } - - if c.UDPInputs[1].BindAddress != ":1234" { - t.Fatalf("unexpected udp bind address: %s", c.UDPInputs[1].BindAddress) - } - - if c.GraphiteInputs[1].Protocol != "udp" { - t.Fatalf("unexpected graphite protocol: %s", c.GraphiteInputs[1].Protocol) - } - - if c.CollectdInputs[1].BindAddress != ":1020" { - t.Fatalf("unexpected collectd bind address: %s", c.CollectdInputs[1].BindAddress) - } - - if c.OpenTSDBInputs[0].BindAddress != ":2020" { - t.Fatalf("unexpected opentsdb bind address: %s", c.OpenTSDBInputs[0].BindAddress) - } - - if c.Data.CacheMaxMemorySize != 1000 { - t.Fatalf("unexpected cache max memory size: %v", c.Data.CacheMaxMemorySize) - } -} - -func TestConfig_ValidateNoServiceConfigured(t *testing.T) { - var c run.Config - if _, err := toml.Decode(` -[meta] -enabled = false - -[data] -enabled = false -`, &c); err != nil { - t.Fatal(err) - } - - if e := c.Validate(); e == nil { - t.Fatalf("got nil, expected error") - } -} - -func TestConfig_ValidateMonitorStore_MetaOnly(t *testing.T) { - c := run.NewConfig() - if _, err := toml.Decode(` -[monitor] -store-enabled = true - -[meta] -dir = "foo" - -[data] -enabled = false -`, &c); err != nil { - t.Fatal(err) - } - - if err := c.Validate(); err == nil { - t.Fatalf("got nil, expected error") - } -} - -func TestConfig_DeprecatedOptions(t *testing.T) { - // Parse configuration. - var c run.Config - if err := c.FromToml(` -[cluster] -max-select-point = 100 -`); err != nil { - t.Fatal(err) - } - - // Validate configuration. - if c.Coordinator.MaxSelectPointN != 100 { - t.Fatalf("unexpected coordinator max select points: %d", c.Coordinator.MaxSelectPointN) - - } -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server.go deleted file mode 100644 index e462f896..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server.go +++ /dev/null @@ -1,631 +0,0 @@ -package run - -import ( - "fmt" - "io" - "log" - "net" - "os" - "path/filepath" - "runtime" - "runtime/pprof" - "time" - - "github.com/influxdata/influxdb" - "github.com/influxdata/influxdb/coordinator" - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/models" - "github.com/influxdata/influxdb/monitor" - "github.com/influxdata/influxdb/services/admin" - "github.com/influxdata/influxdb/services/collectd" - "github.com/influxdata/influxdb/services/continuous_querier" - "github.com/influxdata/influxdb/services/graphite" - "github.com/influxdata/influxdb/services/httpd" - "github.com/influxdata/influxdb/services/meta" - "github.com/influxdata/influxdb/services/opentsdb" - "github.com/influxdata/influxdb/services/precreator" - "github.com/influxdata/influxdb/services/retention" - "github.com/influxdata/influxdb/services/snapshotter" - "github.com/influxdata/influxdb/services/subscriber" - "github.com/influxdata/influxdb/services/udp" - "github.com/influxdata/influxdb/tcp" - "github.com/influxdata/influxdb/tsdb" - client "github.com/influxdata/usage-client/v1" - "go.uber.org/zap" - // Initialize the engine packages - _ "github.com/influxdata/influxdb/tsdb/engine" -) - -var startTime time.Time - -func init() { - startTime = time.Now().UTC() -} - -// BuildInfo represents the build details for the server code. -type BuildInfo struct { - Version string - Commit string - Branch string - Time string -} - -// Server represents a container for the metadata and storage data and services. -// It is built using a Config and it manages the startup and shutdown of all -// services in the proper order. -type Server struct { - buildInfo BuildInfo - - err chan error - closing chan struct{} - - BindAddress string - Listener net.Listener - - Logger zap.Logger - - MetaClient *meta.Client - - TSDBStore *tsdb.Store - QueryExecutor *influxql.QueryExecutor - PointsWriter *coordinator.PointsWriter - Subscriber *subscriber.Service - - Services []Service - - // These references are required for the tcp muxer. - SnapshotterService *snapshotter.Service - - Monitor *monitor.Monitor - - // Server reporting and registration - reportingDisabled bool - - // Profiling - CPUProfile string - MemProfile string - - // httpAPIAddr is the host:port combination for the main HTTP API for querying and writing data - httpAPIAddr string - - // httpUseTLS specifies if we should use a TLS connection to the http servers - httpUseTLS bool - - // tcpAddr is the host:port combination for the TCP listener that services mux onto - tcpAddr string - - config *Config -} - -// NewServer returns a new instance of Server built from a config. -func NewServer(c *Config, buildInfo *BuildInfo) (*Server, error) { - // We need to ensure that a meta directory always exists even if - // we don't start the meta store. node.json is always stored under - // the meta directory. - if err := os.MkdirAll(c.Meta.Dir, 0777); err != nil { - return nil, fmt.Errorf("mkdir all: %s", err) - } - - // 0.10-rc1 and prior would sometimes put the node.json at the root - // dir which breaks backup/restore and restarting nodes. This moves - // the file from the root so it's always under the meta dir. - oldPath := filepath.Join(filepath.Dir(c.Meta.Dir), "node.json") - newPath := filepath.Join(c.Meta.Dir, "node.json") - - if _, err := os.Stat(oldPath); err == nil { - if err := os.Rename(oldPath, newPath); err != nil { - return nil, err - } - } - - _, err := influxdb.LoadNode(c.Meta.Dir) - if err != nil { - if !os.IsNotExist(err) { - return nil, err - } - } - - if err := raftDBExists(c.Meta.Dir); err != nil { - return nil, err - } - - // In 0.10.0 bind-address got moved to the top level. Check - // The old location to keep things backwards compatible - bind := c.BindAddress - - s := &Server{ - buildInfo: *buildInfo, - err: make(chan error), - closing: make(chan struct{}), - - BindAddress: bind, - - Logger: zap.New( - zap.NewTextEncoder(), - zap.Output(os.Stderr), - ), - - MetaClient: meta.NewClient(c.Meta), - - reportingDisabled: c.ReportingDisabled, - - httpAPIAddr: c.HTTPD.BindAddress, - httpUseTLS: c.HTTPD.HTTPSEnabled, - tcpAddr: bind, - - config: c, - } - s.Monitor = monitor.New(s, c.Monitor) - - if err := s.MetaClient.Open(); err != nil { - return nil, err - } - - s.TSDBStore = tsdb.NewStore(c.Data.Dir) - s.TSDBStore.EngineOptions.Config = c.Data - - // Copy TSDB configuration. - s.TSDBStore.EngineOptions.EngineVersion = c.Data.Engine - - // Create the Subscriber service - s.Subscriber = subscriber.NewService(c.Subscriber) - - // Initialize points writer. - s.PointsWriter = coordinator.NewPointsWriter() - s.PointsWriter.WriteTimeout = time.Duration(c.Coordinator.WriteTimeout) - s.PointsWriter.TSDBStore = s.TSDBStore - s.PointsWriter.Subscriber = s.Subscriber - - // Initialize query executor. - s.QueryExecutor = influxql.NewQueryExecutor() - s.QueryExecutor.StatementExecutor = &coordinator.StatementExecutor{ - MetaClient: s.MetaClient, - TaskManager: s.QueryExecutor.TaskManager, - TSDBStore: coordinator.LocalTSDBStore{Store: s.TSDBStore}, - Monitor: s.Monitor, - PointsWriter: s.PointsWriter, - MaxSelectPointN: c.Coordinator.MaxSelectPointN, - MaxSelectSeriesN: c.Coordinator.MaxSelectSeriesN, - MaxSelectBucketsN: c.Coordinator.MaxSelectBucketsN, - } - s.QueryExecutor.TaskManager.QueryTimeout = time.Duration(c.Coordinator.QueryTimeout) - s.QueryExecutor.TaskManager.LogQueriesAfter = time.Duration(c.Coordinator.LogQueriesAfter) - s.QueryExecutor.TaskManager.MaxConcurrentQueries = c.Coordinator.MaxConcurrentQueries - - // Initialize the monitor - s.Monitor.Version = s.buildInfo.Version - s.Monitor.Commit = s.buildInfo.Commit - s.Monitor.Branch = s.buildInfo.Branch - s.Monitor.BuildTime = s.buildInfo.Time - s.Monitor.PointsWriter = (*monitorPointsWriter)(s.PointsWriter) - return s, nil -} - -func (s *Server) Statistics(tags map[string]string) []models.Statistic { - var statistics []models.Statistic - statistics = append(statistics, s.QueryExecutor.Statistics(tags)...) - statistics = append(statistics, s.TSDBStore.Statistics(tags)...) - statistics = append(statistics, s.PointsWriter.Statistics(tags)...) - statistics = append(statistics, s.Subscriber.Statistics(tags)...) - for _, srv := range s.Services { - if m, ok := srv.(monitor.Reporter); ok { - statistics = append(statistics, m.Statistics(tags)...) - } - } - return statistics -} - -func (s *Server) appendSnapshotterService() { - srv := snapshotter.NewService() - srv.TSDBStore = s.TSDBStore - srv.MetaClient = s.MetaClient - s.Services = append(s.Services, srv) - s.SnapshotterService = srv -} - -// SetLogOutput sets the logger used for all messages. It must not be called -// after the Open method has been called. -func (s *Server) SetLogOutput(w io.Writer) { - s.Logger = zap.New(zap.NewTextEncoder(), zap.Output(zap.AddSync(w))) -} - -func (s *Server) appendMonitorService() { - s.Services = append(s.Services, s.Monitor) -} - -func (s *Server) appendRetentionPolicyService(c retention.Config) { - if !c.Enabled { - return - } - srv := retention.NewService(c) - srv.MetaClient = s.MetaClient - srv.TSDBStore = s.TSDBStore - s.Services = append(s.Services, srv) -} - -func (s *Server) appendAdminService(c admin.Config) { - if !c.Enabled { - return - } - c.Version = s.buildInfo.Version - srv := admin.NewService(c) - s.Services = append(s.Services, srv) -} - -func (s *Server) appendHTTPDService(c httpd.Config) { - if !c.Enabled { - return - } - srv := httpd.NewService(c) - srv.Handler.MetaClient = s.MetaClient - srv.Handler.QueryAuthorizer = meta.NewQueryAuthorizer(s.MetaClient) - srv.Handler.WriteAuthorizer = meta.NewWriteAuthorizer(s.MetaClient) - srv.Handler.QueryExecutor = s.QueryExecutor - srv.Handler.Monitor = s.Monitor - srv.Handler.PointsWriter = s.PointsWriter - srv.Handler.Version = s.buildInfo.Version - - s.Services = append(s.Services, srv) -} - -func (s *Server) appendCollectdService(c collectd.Config) { - if !c.Enabled { - return - } - srv := collectd.NewService(c) - srv.MetaClient = s.MetaClient - srv.PointsWriter = s.PointsWriter - s.Services = append(s.Services, srv) -} - -func (s *Server) appendOpenTSDBService(c opentsdb.Config) error { - if !c.Enabled { - return nil - } - srv, err := opentsdb.NewService(c) - if err != nil { - return err - } - srv.PointsWriter = s.PointsWriter - srv.MetaClient = s.MetaClient - s.Services = append(s.Services, srv) - return nil -} - -func (s *Server) appendGraphiteService(c graphite.Config) error { - if !c.Enabled { - return nil - } - srv, err := graphite.NewService(c) - if err != nil { - return err - } - - srv.PointsWriter = s.PointsWriter - srv.MetaClient = s.MetaClient - srv.Monitor = s.Monitor - s.Services = append(s.Services, srv) - return nil -} - -func (s *Server) appendPrecreatorService(c precreator.Config) error { - if !c.Enabled { - return nil - } - srv, err := precreator.NewService(c) - if err != nil { - return err - } - - srv.MetaClient = s.MetaClient - s.Services = append(s.Services, srv) - return nil -} - -func (s *Server) appendUDPService(c udp.Config) { - if !c.Enabled { - return - } - srv := udp.NewService(c) - srv.PointsWriter = s.PointsWriter - srv.MetaClient = s.MetaClient - s.Services = append(s.Services, srv) -} - -func (s *Server) appendContinuousQueryService(c continuous_querier.Config) { - if !c.Enabled { - return - } - srv := continuous_querier.NewService(c) - srv.MetaClient = s.MetaClient - srv.QueryExecutor = s.QueryExecutor - s.Services = append(s.Services, srv) -} - -// Err returns an error channel that multiplexes all out of band errors received from all services. -func (s *Server) Err() <-chan error { return s.err } - -// Open opens the meta and data store and all services. -func (s *Server) Open() error { - // Start profiling, if set. - startProfile(s.CPUProfile, s.MemProfile) - - // Open shared TCP connection. - ln, err := net.Listen("tcp", s.BindAddress) - if err != nil { - return fmt.Errorf("listen: %s", err) - } - s.Listener = ln - - // Multiplex listener. - mux := tcp.NewMux() - go mux.Serve(ln) - - // Append services. - s.appendMonitorService() - s.appendPrecreatorService(s.config.Precreator) - s.appendSnapshotterService() - s.appendAdminService(s.config.Admin) - s.appendContinuousQueryService(s.config.ContinuousQuery) - s.appendHTTPDService(s.config.HTTPD) - s.appendRetentionPolicyService(s.config.Retention) - for _, i := range s.config.GraphiteInputs { - if err := s.appendGraphiteService(i); err != nil { - return err - } - } - for _, i := range s.config.CollectdInputs { - s.appendCollectdService(i) - } - for _, i := range s.config.OpenTSDBInputs { - if err := s.appendOpenTSDBService(i); err != nil { - return err - } - } - for _, i := range s.config.UDPInputs { - s.appendUDPService(i) - } - - s.Subscriber.MetaClient = s.MetaClient - s.Subscriber.MetaClient = s.MetaClient - s.PointsWriter.MetaClient = s.MetaClient - s.Monitor.MetaClient = s.MetaClient - - s.SnapshotterService.Listener = mux.Listen(snapshotter.MuxHeader) - - // Configure logging for all services and clients. - if s.config.Meta.LoggingEnabled { - s.MetaClient.WithLogger(s.Logger) - } - s.TSDBStore.WithLogger(s.Logger) - if s.config.Data.QueryLogEnabled { - s.QueryExecutor.WithLogger(s.Logger) - } - s.PointsWriter.WithLogger(s.Logger) - s.Subscriber.WithLogger(s.Logger) - for _, svc := range s.Services { - svc.WithLogger(s.Logger) - } - s.SnapshotterService.WithLogger(s.Logger) - s.Monitor.WithLogger(s.Logger) - - // Open TSDB store. - if err := s.TSDBStore.Open(); err != nil { - return fmt.Errorf("open tsdb store: %s", err) - } - - // Open the subcriber service - if err := s.Subscriber.Open(); err != nil { - return fmt.Errorf("open subscriber: %s", err) - } - - // Open the points writer service - if err := s.PointsWriter.Open(); err != nil { - return fmt.Errorf("open points writer: %s", err) - } - - for _, service := range s.Services { - if err := service.Open(); err != nil { - return fmt.Errorf("open service: %s", err) - } - } - - // Start the reporting service, if not disabled. - if !s.reportingDisabled { - go s.startServerReporting() - } - - return nil -} - -// Close shuts down the meta and data stores and all services. -func (s *Server) Close() error { - stopProfile() - - // Close the listener first to stop any new connections - if s.Listener != nil { - s.Listener.Close() - } - - // Close services to allow any inflight requests to complete - // and prevent new requests from being accepted. - for _, service := range s.Services { - service.Close() - } - - if s.PointsWriter != nil { - s.PointsWriter.Close() - } - - if s.QueryExecutor != nil { - s.QueryExecutor.Close() - } - - // Close the TSDBStore, no more reads or writes at this point - if s.TSDBStore != nil { - s.TSDBStore.Close() - } - - if s.Subscriber != nil { - s.Subscriber.Close() - } - - if s.MetaClient != nil { - s.MetaClient.Close() - } - - close(s.closing) - return nil -} - -// startServerReporting starts periodic server reporting. -func (s *Server) startServerReporting() { - s.reportServer() - - ticker := time.NewTicker(24 * time.Hour) - defer ticker.Stop() - for { - select { - case <-s.closing: - return - case <-ticker.C: - s.reportServer() - } - } -} - -// reportServer reports usage statistics about the system. -func (s *Server) reportServer() { - dis := s.MetaClient.Databases() - numDatabases := len(dis) - - numMeasurements := 0 - numSeries := 0 - - // Only needed in the case of a data node - if s.TSDBStore != nil { - for _, di := range dis { - d := s.TSDBStore.DatabaseIndex(di.Name) - if d == nil { - // No data in this store for this database. - continue - } - m, s := d.MeasurementSeriesCounts() - numMeasurements += m - numSeries += s - } - } - - clusterID := s.MetaClient.ClusterID() - cl := client.New("") - usage := client.Usage{ - Product: "influxdb", - Data: []client.UsageData{ - { - Values: client.Values{ - "os": runtime.GOOS, - "arch": runtime.GOARCH, - "version": s.buildInfo.Version, - "cluster_id": fmt.Sprintf("%v", clusterID), - "num_series": numSeries, - "num_measurements": numMeasurements, - "num_databases": numDatabases, - "uptime": time.Since(startTime).Seconds(), - }, - }, - }, - } - - s.Logger.Info("Sending usage statistics to usage.influxdata.com") - - go cl.Save(usage) -} - -// monitorErrorChan reads an error channel and resends it through the server. -func (s *Server) monitorErrorChan(ch <-chan error) { - for { - select { - case err, ok := <-ch: - if !ok { - return - } - s.err <- err - case <-s.closing: - return - } - } -} - -// Service represents a service attached to the server. -type Service interface { - WithLogger(log zap.Logger) - Open() error - Close() error -} - -// prof stores the file locations of active profiles. -var prof struct { - cpu *os.File - mem *os.File -} - -// StartProfile initializes the cpu and memory profile, if specified. -func startProfile(cpuprofile, memprofile string) { - if cpuprofile != "" { - f, err := os.Create(cpuprofile) - if err != nil { - log.Fatalf("cpuprofile: %v", err) - } - log.Printf("writing CPU profile to: %s\n", cpuprofile) - prof.cpu = f - pprof.StartCPUProfile(prof.cpu) - } - - if memprofile != "" { - f, err := os.Create(memprofile) - if err != nil { - log.Fatalf("memprofile: %v", err) - } - log.Printf("writing mem profile to: %s\n", memprofile) - prof.mem = f - runtime.MemProfileRate = 4096 - } - -} - -// StopProfile closes the cpu and memory profiles if they are running. -func stopProfile() { - if prof.cpu != nil { - pprof.StopCPUProfile() - prof.cpu.Close() - log.Println("CPU profile stopped") - } - if prof.mem != nil { - pprof.Lookup("heap").WriteTo(prof.mem, 0) - prof.mem.Close() - log.Println("mem profile stopped") - } -} - -type tcpaddr struct{ host string } - -func (a *tcpaddr) Network() string { return "tcp" } -func (a *tcpaddr) String() string { return a.host } - -// monitorPointsWriter is a wrapper around `coordinator.PointsWriter` that helps -// to prevent a circular dependency between the `cluster` and `monitor` packages. -type monitorPointsWriter coordinator.PointsWriter - -func (pw *monitorPointsWriter) WritePoints(database, retentionPolicy string, points models.Points) error { - return (*coordinator.PointsWriter)(pw).WritePoints(database, retentionPolicy, models.ConsistencyLevelAny, points) -} - -func raftDBExists(dir string) error { - // Check to see if there is a raft db, if so, error out with a message - // to downgrade, export, and then import the meta data - raftFile := filepath.Join(dir, "raft.db") - if _, err := os.Stat(raftFile); err == nil { - return fmt.Errorf("detected %s. To proceed, you'll need to either 1) downgrade to v0.11.x, export your metadata, upgrade to the current version again, and then import the metadata or 2) delete the file, which will effectively reset your database. For more assistance with the upgrade, see: https://docs.influxdata.com/influxdb/v0.12/administration/upgrading/", raftFile) - } - return nil -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_bench_test.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_bench_test.go deleted file mode 100644 index 7004dd6a..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_bench_test.go +++ /dev/null @@ -1,136 +0,0 @@ -package run_test - -import ( - "bytes" - "fmt" - "net/url" - "testing" -) - -var strResult string - -func BenchmarkServer_Query_Count_1(b *testing.B) { benchmarkServerQueryCount(b, 1) } -func BenchmarkServer_Query_Count_1K(b *testing.B) { benchmarkServerQueryCount(b, 1000) } -func BenchmarkServer_Query_Count_100K(b *testing.B) { benchmarkServerQueryCount(b, 100000) } -func BenchmarkServer_Query_Count_1M(b *testing.B) { benchmarkServerQueryCount(b, 1000000) } - -func benchmarkServerQueryCount(b *testing.B, pointN int) { - if _, err := benchServer.Query(`DROP MEASUREMENT cpu`); err != nil { - b.Fatal(err) - } - - // Write data into server. - var buf bytes.Buffer - for i := 0; i < pointN; i++ { - fmt.Fprintf(&buf, `cpu value=100 %d`, i+1) - if i != pointN-1 { - fmt.Fprint(&buf, "\n") - } - } - benchServer.MustWrite("db0", "rp0", buf.String(), nil) - - // Query simple count from server. - b.ResetTimer() - b.ReportAllocs() - var err error - for i := 0; i < b.N; i++ { - if strResult, err = benchServer.Query(`SELECT count(value) FROM db0.rp0.cpu`); err != nil { - b.Fatal(err) - } else if strResult != fmt.Sprintf(`{"results":[{"series":[{"name":"cpu","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",%d]]}]}]}`, pointN) { - b.Fatalf("unexpected result: %s", strResult) - } - } -} - -func BenchmarkServer_Query_Count_Where_500(b *testing.B) { - benchmarkServerQueryCountWhere(b, false, 500) -} -func BenchmarkServer_Query_Count_Where_1K(b *testing.B) { - benchmarkServerQueryCountWhere(b, false, 1000) -} -func BenchmarkServer_Query_Count_Where_10K(b *testing.B) { - benchmarkServerQueryCountWhere(b, false, 10000) -} -func BenchmarkServer_Query_Count_Where_100K(b *testing.B) { - benchmarkServerQueryCountWhere(b, false, 100000) -} - -func BenchmarkServer_Query_Count_Where_Regex_500(b *testing.B) { - benchmarkServerQueryCountWhere(b, true, 500) -} -func BenchmarkServer_Query_Count_Where_Regex_1K(b *testing.B) { - benchmarkServerQueryCountWhere(b, true, 1000) -} -func BenchmarkServer_Query_Count_Where_Regex_10K(b *testing.B) { - benchmarkServerQueryCountWhere(b, true, 10000) -} -func BenchmarkServer_Query_Count_Where_Regex_100K(b *testing.B) { - benchmarkServerQueryCountWhere(b, true, 100000) -} - -func benchmarkServerQueryCountWhere(b *testing.B, useRegex bool, pointN int) { - if _, err := benchServer.Query(`DROP MEASUREMENT cpu`); err != nil { - b.Fatal(err) - } - - // Write data into server. - var buf bytes.Buffer - for i := 0; i < pointN; i++ { - fmt.Fprintf(&buf, `cpu,host=server-%d value=100 %d`, i, i) - if i != pointN-1 { - fmt.Fprint(&buf, "\n") - } - } - benchServer.MustWrite("db0", "rp0", buf.String(), nil) - - // Query count from server with WHERE - var ( - err error - condition = `host = 'server-487'` - ) - - if useRegex { - condition = `host =~ /^server-487$/` - } - - b.ResetTimer() - b.ReportAllocs() - for i := 0; i < b.N; i++ { - if strResult, err = benchServer.Query(fmt.Sprintf(`SELECT count(value) FROM db0.rp0.cpu WHERE %s`, condition)); err != nil { - b.Fatal(err) - } else if strResult == `{"results":[{}]}` { - b.Fatal("no results") - } - } -} - -func BenchmarkServer_ShowSeries_1(b *testing.B) { benchmarkServerShowSeries(b, 1) } -func BenchmarkServer_ShowSeries_1K(b *testing.B) { benchmarkServerShowSeries(b, 1000) } -func BenchmarkServer_ShowSeries_100K(b *testing.B) { benchmarkServerShowSeries(b, 100000) } -func BenchmarkServer_ShowSeries_1M(b *testing.B) { benchmarkServerShowSeries(b, 1000000) } - -func benchmarkServerShowSeries(b *testing.B, pointN int) { - if _, err := benchServer.Query(`DROP MEASUREMENT cpu`); err != nil { - b.Fatal(err) - } - - // Write data into server. - var buf bytes.Buffer - for i := 0; i < pointN; i++ { - fmt.Fprintf(&buf, `cpu,host=server%d value=100 %d`, i, i+1) - if i != pointN-1 { - fmt.Fprint(&buf, "\n") - } - } - benchServer.MustWrite("db0", "rp0", buf.String(), nil) - - // Query simple count from server. - b.ResetTimer() - b.ReportAllocs() - var err error - for i := 0; i < b.N; i++ { - if strResult, err = benchServer.QueryWithParams(`SHOW SERIES`, url.Values{"db": {"db0"}}); err != nil { - b.Fatal(err) - } - } -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_helpers_test.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_helpers_test.go deleted file mode 100644 index bd95501e..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_helpers_test.go +++ /dev/null @@ -1,473 +0,0 @@ -// This package is a set of convenience helpers and structs to make integration testing easier -package run_test - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "io/ioutil" - "math" - "net/http" - "net/url" - "os" - "regexp" - "strings" - "testing" - "time" - - "github.com/influxdata/influxdb/cmd/influxd/run" - "github.com/influxdata/influxdb/services/httpd" - "github.com/influxdata/influxdb/services/meta" - "github.com/influxdata/influxdb/toml" -) - -const emptyResults = `{"results":[{}]}` - -// Server represents a test wrapper for run.Server. -type Server struct { - *run.Server - Config *run.Config -} - -// NewServer returns a new instance of Server. -func NewServer(c *run.Config) *Server { - buildInfo := &run.BuildInfo{ - Version: "testServer", - Commit: "testCommit", - Branch: "testBranch", - } - srv, _ := run.NewServer(c, buildInfo) - s := Server{ - Server: srv, - Config: c, - } - return &s -} - -// OpenServer opens a test server. -func OpenServer(c *run.Config) *Server { - s := NewServer(c) - configureLogging(s) - if err := s.Open(); err != nil { - panic(err.Error()) - } - return s -} - -// OpenServerWithVersion opens a test server with a specific version. -func OpenServerWithVersion(c *run.Config, version string) *Server { - buildInfo := &run.BuildInfo{ - Version: version, - Commit: "", - Branch: "", - } - srv, _ := run.NewServer(c, buildInfo) - s := Server{ - Server: srv, - Config: c, - } - if err := s.Open(); err != nil { - panic(err.Error()) - } - configureLogging(&s) - - return &s -} - -// OpenDefaultServer opens a test server with a default database & retention policy. -func OpenDefaultServer(c *run.Config) *Server { - s := OpenServer(c) - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - panic(err) - } - return s -} - -// Close shuts down the server and removes all temporary paths. -func (s *Server) Close() { - if err := s.Server.Close(); err != nil { - panic(err.Error()) - } - if err := os.RemoveAll(s.Config.Meta.Dir); err != nil { - panic(err.Error()) - } - if err := os.RemoveAll(s.Config.Data.Dir); err != nil { - panic(err.Error()) - } -} - -// URL returns the base URL for the httpd endpoint. -func (s *Server) URL() string { - for _, service := range s.Services { - if service, ok := service.(*httpd.Service); ok { - return "http://" + service.Addr().String() - } - } - panic("httpd server not found in services") -} - -// CreateDatabaseAndRetentionPolicy will create the database and retention policy. -func (s *Server) CreateDatabaseAndRetentionPolicy(db string, rp *meta.RetentionPolicySpec, makeDefault bool) error { - if _, err := s.MetaClient.CreateDatabase(db); err != nil { - return err - } else if _, err := s.MetaClient.CreateRetentionPolicy(db, rp, makeDefault); err != nil { - return err - } - return nil -} - -// Query executes a query against the server and returns the results. -func (s *Server) Query(query string) (results string, err error) { - return s.QueryWithParams(query, nil) -} - -// MustQuery executes a query against the server and returns the results. -func (s *Server) MustQuery(query string) string { - results, err := s.Query(query) - if err != nil { - panic(err) - } - return results -} - -// Query executes a query against the server and returns the results. -func (s *Server) QueryWithParams(query string, values url.Values) (results string, err error) { - var v url.Values - if values == nil { - v = url.Values{} - } else { - v, _ = url.ParseQuery(values.Encode()) - } - v.Set("q", query) - return s.HTTPPost(s.URL()+"/query?"+v.Encode(), nil) -} - -// MustQueryWithParams executes a query against the server and returns the results. -func (s *Server) MustQueryWithParams(query string, values url.Values) string { - results, err := s.QueryWithParams(query, values) - if err != nil { - panic(err) - } - return results -} - -// HTTPGet makes an HTTP GET request to the server and returns the response. -func (s *Server) HTTPGet(url string) (results string, err error) { - resp, err := http.Get(url) - if err != nil { - return "", err - } - body := strings.TrimSpace(string(MustReadAll(resp.Body))) - switch resp.StatusCode { - case http.StatusBadRequest: - if !expectPattern(".*error parsing query*.", body) { - return "", fmt.Errorf("unexpected status code: code=%d, body=%s", resp.StatusCode, body) - } - return body, nil - case http.StatusOK: - return body, nil - default: - return "", fmt.Errorf("unexpected status code: code=%d, body=%s", resp.StatusCode, body) - } -} - -// HTTPPost makes an HTTP POST request to the server and returns the response. -func (s *Server) HTTPPost(url string, content []byte) (results string, err error) { - buf := bytes.NewBuffer(content) - resp, err := http.Post(url, "application/json", buf) - if err != nil { - return "", err - } - body := strings.TrimSpace(string(MustReadAll(resp.Body))) - switch resp.StatusCode { - case http.StatusBadRequest: - if !expectPattern(".*error parsing query*.", body) { - return "", fmt.Errorf("unexpected status code: code=%d, body=%s", resp.StatusCode, body) - } - return body, nil - case http.StatusOK, http.StatusNoContent: - return body, nil - default: - return "", fmt.Errorf("unexpected status code: code=%d, body=%s", resp.StatusCode, body) - } -} - -// Write executes a write against the server and returns the results. -func (s *Server) Write(db, rp, body string, params url.Values) (results string, err error) { - if params == nil { - params = url.Values{} - } - if params.Get("db") == "" { - params.Set("db", db) - } - if params.Get("rp") == "" { - params.Set("rp", rp) - } - resp, err := http.Post(s.URL()+"/write?"+params.Encode(), "", strings.NewReader(body)) - if err != nil { - return "", err - } else if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { - return "", fmt.Errorf("invalid status code: code=%d, body=%s", resp.StatusCode, MustReadAll(resp.Body)) - } - return string(MustReadAll(resp.Body)), nil -} - -// MustWrite executes a write to the server. Panic on error. -func (s *Server) MustWrite(db, rp, body string, params url.Values) string { - results, err := s.Write(db, rp, body, params) - if err != nil { - panic(err) - } - return results -} - -// NewConfig returns the default config with temporary paths. -func NewConfig() *run.Config { - c := run.NewConfig() - c.BindAddress = "127.0.0.1:0" - c.ReportingDisabled = true - c.Coordinator.WriteTimeout = toml.Duration(30 * time.Second) - c.Meta.Dir = MustTempFile() - - if !testing.Verbose() { - c.Meta.LoggingEnabled = false - } - - c.Data.Dir = MustTempFile() - c.Data.WALDir = MustTempFile() - - c.HTTPD.Enabled = true - c.HTTPD.BindAddress = "127.0.0.1:0" - c.HTTPD.LogEnabled = testing.Verbose() - - c.Monitor.StoreEnabled = false - - return c -} - -func newRetentionPolicySpec(name string, rf int, duration time.Duration) *meta.RetentionPolicySpec { - return &meta.RetentionPolicySpec{Name: name, ReplicaN: &rf, Duration: &duration} -} - -func maxFloat64() string { - maxFloat64, _ := json.Marshal(math.MaxFloat64) - return string(maxFloat64) -} - -func maxInt64() string { - maxInt64, _ := json.Marshal(^int64(0)) - return string(maxInt64) -} - -func now() time.Time { - return time.Now().UTC() -} - -func yesterday() time.Time { - return now().Add(-1 * time.Hour * 24) -} - -func mustParseTime(layout, value string) time.Time { - tm, err := time.Parse(layout, value) - if err != nil { - panic(err) - } - return tm -} - -// MustReadAll reads r. Panic on error. -func MustReadAll(r io.Reader) []byte { - b, err := ioutil.ReadAll(r) - if err != nil { - panic(err) - } - return b -} - -// MustTempFile returns a path to a temporary file. -func MustTempFile() string { - f, err := ioutil.TempFile("", "influxd-") - if err != nil { - panic(err) - } - f.Close() - os.Remove(f.Name()) - return f.Name() -} - -func expectPattern(exp, act string) bool { - re := regexp.MustCompile(exp) - if !re.MatchString(act) { - return false - } - return true -} - -type Query struct { - name string - command string - params url.Values - exp, act string - pattern bool - skip bool - repeat int - once bool -} - -// Execute runs the command and returns an err if it fails -func (q *Query) Execute(s *Server) (err error) { - if q.params == nil { - q.act, err = s.Query(q.command) - return - } - q.act, err = s.QueryWithParams(q.command, q.params) - return -} - -func (q *Query) success() bool { - if q.pattern { - return expectPattern(q.exp, q.act) - } - return q.exp == q.act -} - -func (q *Query) Error(err error) string { - return fmt.Sprintf("%s: %v", q.name, err) -} - -func (q *Query) failureMessage() string { - return fmt.Sprintf("%s: unexpected results\nquery: %s\nparams: %v\nexp: %s\nactual: %s\n", q.name, q.command, q.params, q.exp, q.act) -} - -type Write struct { - db string - rp string - data string -} - -func (w *Write) duplicate() *Write { - return &Write{ - db: w.db, - rp: w.rp, - data: w.data, - } -} - -type Writes []*Write - -func (a Writes) duplicate() Writes { - writes := make(Writes, 0, len(a)) - for _, w := range a { - writes = append(writes, w.duplicate()) - } - return writes -} - -type Tests map[string]Test - -type Test struct { - initialized bool - writes Writes - params url.Values - db string - rp string - exp string - queries []*Query -} - -func NewTest(db, rp string) Test { - return Test{ - db: db, - rp: rp, - } -} - -func (t Test) duplicate() Test { - test := Test{ - initialized: t.initialized, - writes: t.writes.duplicate(), - db: t.db, - rp: t.rp, - exp: t.exp, - queries: make([]*Query, len(t.queries)), - } - - if t.params != nil { - t.params = url.Values{} - for k, a := range t.params { - vals := make([]string, len(a)) - copy(vals, a) - test.params[k] = vals - } - } - copy(test.queries, t.queries) - return test -} - -func (t *Test) addQueries(q ...*Query) { - t.queries = append(t.queries, q...) -} - -func (t *Test) database() string { - if t.db != "" { - return t.db - } - return "db0" -} - -func (t *Test) retentionPolicy() string { - if t.rp != "" { - return t.rp - } - return "default" -} - -func (t *Test) init(s *Server) error { - if len(t.writes) == 0 || t.initialized { - return nil - } - if t.db == "" { - t.db = "db0" - } - if t.rp == "" { - t.rp = "rp0" - } - - if err := writeTestData(s, t); err != nil { - return err - } - - t.initialized = true - - return nil -} - -func writeTestData(s *Server, t *Test) error { - for i, w := range t.writes { - if w.db == "" { - w.db = t.database() - } - if w.rp == "" { - w.rp = t.retentionPolicy() - } - - if err := s.CreateDatabaseAndRetentionPolicy(w.db, newRetentionPolicySpec(w.rp, 1, 0), true); err != nil { - return err - } - if res, err := s.Write(w.db, w.rp, w.data, t.params); err != nil { - return fmt.Errorf("write #%d: %s", i, err) - } else if t.exp != res { - return fmt.Errorf("unexpected results\nexp: %s\ngot: %s\n", t.exp, res) - } - } - - return nil -} - -func configureLogging(s *Server) { - // Set the logger to discard unless verbose is on - if !testing.Verbose() { - s.SetLogOutput(ioutil.Discard) - } -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_suite_test.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_suite_test.go deleted file mode 100644 index 6e8c68b4..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_suite_test.go +++ /dev/null @@ -1,518 +0,0 @@ -package run_test - -import ( - "fmt" - "net/url" - "strings" - "testing" - "time" -) - -var tests Tests - -// Load all shared tests -func init() { - tests = make(map[string]Test) - - tests["database_commands"] = Test{ - queries: []*Query{ - &Query{ - name: "create database should succeed", - command: `CREATE DATABASE db0`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "create database with retention duration should succeed", - command: `CREATE DATABASE db0_r WITH DURATION 24h REPLICATION 2 NAME db0_r_policy`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "create database should error with bad name", - command: `CREATE DATABASE 0xdb0`, - exp: `{"error":"error parsing query: found 0xdb0, expected identifier at line 1, char 17"}`, - }, - &Query{ - name: "create database with retention duration should error with bad retention duration", - command: `CREATE DATABASE db0 WITH DURATION xyz`, - exp: `{"error":"error parsing query: found xyz, expected duration at line 1, char 35"}`, - }, - &Query{ - name: "create database with retention replication should error with bad retention replication number", - command: `CREATE DATABASE db0 WITH REPLICATION xyz`, - exp: `{"error":"error parsing query: found xyz, expected integer at line 1, char 38"}`, - }, - &Query{ - name: "create database with retention name should error with missing retention name", - command: `CREATE DATABASE db0 WITH NAME`, - exp: `{"error":"error parsing query: found EOF, expected identifier at line 1, char 31"}`, - }, - &Query{ - name: "show database should succeed", - command: `SHOW DATABASES`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["db0"],["db0_r"]]}]}]}`, - }, - &Query{ - name: "create database should not error with existing database", - command: `CREATE DATABASE db0`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "create database should create non-existing database", - command: `CREATE DATABASE db1`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "create database with retention duration should error if retention policy is different", - command: `CREATE DATABASE db1 WITH DURATION 24h`, - exp: `{"results":[{"statement_id":0,"error":"retention policy conflicts with an existing policy"}]}`, - }, - &Query{ - name: "create database should error with bad retention duration", - command: `CREATE DATABASE db1 WITH DURATION xyz`, - exp: `{"error":"error parsing query: found xyz, expected duration at line 1, char 35"}`, - }, - &Query{ - name: "show database should succeed", - command: `SHOW DATABASES`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["db0"],["db0_r"],["db1"]]}]}]}`, - }, - &Query{ - name: "drop database db0 should succeed", - command: `DROP DATABASE db0`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "drop database db0_r should succeed", - command: `DROP DATABASE db0_r`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "drop database db1 should succeed", - command: `DROP DATABASE db1`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "drop database should not error if it does not exists", - command: `DROP DATABASE db1`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "drop database should not error with non-existing database db1", - command: `DROP DATABASE db1`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "show database should have no results", - command: `SHOW DATABASES`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"]}]}]}`, - }, - &Query{ - name: "create database with shard group duration should succeed", - command: `CREATE DATABASE db0 WITH SHARD DURATION 61m`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "create database with shard group duration and duration should succeed", - command: `CREATE DATABASE db1 WITH DURATION 60m SHARD DURATION 30m`, - exp: `{"results":[{"statement_id":0}]}`, - }, - }, - } - - tests["drop_and_recreate_database"] = Test{ - db: "db0", - rp: "rp0", - writes: Writes{ - &Write{data: fmt.Sprintf(`cpu,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano())}, - }, - queries: []*Query{ - &Query{ - name: "Drop database after data write", - command: `DROP DATABASE db0`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "Recreate database", - command: `CREATE DATABASE db0`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "Recreate retention policy", - command: `CREATE RETENTION POLICY rp0 ON db0 DURATION 365d REPLICATION 1 DEFAULT`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "Show measurements after recreate", - command: `SHOW MEASUREMENTS`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Query data after recreate", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }, - } - - tests["drop_database_isolated"] = Test{ - db: "db0", - rp: "rp0", - writes: Writes{ - &Write{data: fmt.Sprintf(`cpu,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano())}, - }, - queries: []*Query{ - &Query{ - name: "Query data from 1st database", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","region","val"],"values":[["2000-01-01T00:00:00Z","serverA","uswest",23.2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Query data from 1st database with GROUP BY *", - command: `SELECT * FROM cpu GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"serverA","region":"uswest"},"columns":["time","val"],"values":[["2000-01-01T00:00:00Z",23.2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Drop other database", - command: `DROP DATABASE db1`, - once: true, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "Query data from 1st database and ensure it's still there", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","region","val"],"values":[["2000-01-01T00:00:00Z","serverA","uswest",23.2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Query data from 1st database and ensure it's still there with GROUP BY *", - command: `SELECT * FROM cpu GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"serverA","region":"uswest"},"columns":["time","val"],"values":[["2000-01-01T00:00:00Z",23.2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }, - } - - tests["delete_series"] = Test{ - db: "db0", - rp: "rp0", - writes: Writes{ - &Write{data: fmt.Sprintf(`cpu,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano())}, - &Write{data: fmt.Sprintf(`cpu,host=serverA,region=uswest val=100 %d`, mustParseTime(time.RFC3339Nano, "2000-01-02T00:00:00Z").UnixNano())}, - &Write{data: fmt.Sprintf(`cpu,host=serverA,region=uswest val=200 %d`, mustParseTime(time.RFC3339Nano, "2000-01-03T00:00:00Z").UnixNano())}, - &Write{db: "db1", data: fmt.Sprintf(`cpu,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano())}, - }, - queries: []*Query{ - &Query{ - name: "Show series is present", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=serverA,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Delete series", - command: `DELETE FROM cpu WHERE time < '2000-01-03T00:00:00Z'`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - once: true, - }, - &Query{ - name: "Show series still exists", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=serverA,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Make sure last point still exists", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","region","val"],"values":[["2000-01-03T00:00:00Z","serverA","uswest",200]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Make sure data wasn't deleted from other database.", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","region","val"],"values":[["2000-01-01T00:00:00Z","serverA","uswest",23.2]]}]}]}`, - params: url.Values{"db": []string{"db1"}}, - }, - }, - } - - tests["drop_and_recreate_series"] = Test{ - db: "db0", - rp: "rp0", - writes: Writes{ - &Write{data: fmt.Sprintf(`cpu,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano())}, - &Write{db: "db1", data: fmt.Sprintf(`cpu,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano())}, - }, - queries: []*Query{ - &Query{ - name: "Show series is present", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=serverA,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Drop series after data write", - command: `DROP SERIES FROM cpu`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - once: true, - }, - &Query{ - name: "Show series is gone", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Make sure data wasn't deleted from other database.", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","region","val"],"values":[["2000-01-01T00:00:00Z","serverA","uswest",23.2]]}]}]}`, - params: url.Values{"db": []string{"db1"}}, - }, - }, - } - tests["drop_and_recreate_series_retest"] = Test{ - db: "db0", - rp: "rp0", - writes: Writes{ - &Write{data: fmt.Sprintf(`cpu,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano())}, - }, - queries: []*Query{ - &Query{ - name: "Show series is present again after re-write", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=serverA,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }, - } - - tests["drop_series_from_regex"] = Test{ - db: "db0", - rp: "rp0", - writes: Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`a,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`aa,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`b,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`c,host=serverA,region=uswest val=30.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - }, "\n")}, - }, - queries: []*Query{ - &Query{ - name: "Show series is present", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["a,host=serverA,region=uswest"],["aa,host=serverA,region=uswest"],["b,host=serverA,region=uswest"],["c,host=serverA,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Drop series after data write", - command: `DROP SERIES FROM /a.*/`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - once: true, - }, - &Query{ - name: "Show series is gone", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["b,host=serverA,region=uswest"],["c,host=serverA,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Drop series from regex that matches no measurements", - command: `DROP SERIES FROM /a.*/`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - once: true, - }, - &Query{ - name: "make sure DROP SERIES doesn't delete anything when regex doesn't match", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["b,host=serverA,region=uswest"],["c,host=serverA,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Drop series with WHERE field should error", - command: `DROP SERIES FROM c WHERE val > 50.0`, - exp: `{"results":[{"statement_id":0,"error":"fields not supported in WHERE clause during deletion"}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "make sure DROP SERIES with field in WHERE didn't delete data", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["b,host=serverA,region=uswest"],["c,host=serverA,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Drop series with WHERE time should error", - command: `DROP SERIES FROM c WHERE time > now() - 1d`, - exp: `{"results":[{"statement_id":0,"error":"DROP SERIES doesn't support time in WHERE clause"}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }, - } - - tests["retention_policy_commands"] = Test{ - db: "db0", - queries: []*Query{ - &Query{ - name: "create retention policy should succeed", - command: `CREATE RETENTION POLICY rp0 ON db0 DURATION 1h REPLICATION 1`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "show retention policy should succeed", - command: `SHOW RETENTION POLICIES ON db0`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["name","duration","shardGroupDuration","replicaN","default"],"values":[["rp0","1h0m0s","1h0m0s",1,false]]}]}]}`, - }, - &Query{ - name: "alter retention policy should succeed", - command: `ALTER RETENTION POLICY rp0 ON db0 DURATION 2h REPLICATION 3 DEFAULT`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "show retention policy should have new altered information", - command: `SHOW RETENTION POLICIES ON db0`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["name","duration","shardGroupDuration","replicaN","default"],"values":[["rp0","2h0m0s","1h0m0s",3,true]]}]}]}`, - }, - &Query{ - name: "show retention policy should still show policy", - command: `SHOW RETENTION POLICIES ON db0`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["name","duration","shardGroupDuration","replicaN","default"],"values":[["rp0","2h0m0s","1h0m0s",3,true]]}]}]}`, - }, - &Query{ - name: "create a second non-default retention policy", - command: `CREATE RETENTION POLICY rp2 ON db0 DURATION 1h REPLICATION 1`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "show retention policy should show both", - command: `SHOW RETENTION POLICIES ON db0`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["name","duration","shardGroupDuration","replicaN","default"],"values":[["rp0","2h0m0s","1h0m0s",3,true],["rp2","1h0m0s","1h0m0s",1,false]]}]}]}`, - }, - &Query{ - name: "dropping non-default retention policy succeed", - command: `DROP RETENTION POLICY rp2 ON db0`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "create a third non-default retention policy", - command: `CREATE RETENTION POLICY rp3 ON db0 DURATION 1h REPLICATION 1 SHARD DURATION 30m`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "create retention policy with default on", - command: `CREATE RETENTION POLICY rp3 ON db0 DURATION 1h REPLICATION 1 SHARD DURATION 30m DEFAULT`, - exp: `{"results":[{"statement_id":0,"error":"retention policy conflicts with an existing policy"}]}`, - once: true, - }, - &Query{ - name: "show retention policy should show both with custom shard", - command: `SHOW RETENTION POLICIES ON db0`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["name","duration","shardGroupDuration","replicaN","default"],"values":[["rp0","2h0m0s","1h0m0s",3,true],["rp3","1h0m0s","1h0m0s",1,false]]}]}]}`, - }, - &Query{ - name: "dropping non-default custom shard retention policy succeed", - command: `DROP RETENTION POLICY rp3 ON db0`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "show retention policy should show just default", - command: `SHOW RETENTION POLICIES ON db0`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["name","duration","shardGroupDuration","replicaN","default"],"values":[["rp0","2h0m0s","1h0m0s",3,true]]}]}]}`, - }, - &Query{ - name: "Ensure retention policy with unacceptable retention cannot be created", - command: `CREATE RETENTION POLICY rp4 ON db0 DURATION 1s REPLICATION 1`, - exp: `{"results":[{"statement_id":0,"error":"retention policy duration must be at least 1h0m0s"}]}`, - once: true, - }, - &Query{ - name: "Check error when deleting retention policy on non-existent database", - command: `DROP RETENTION POLICY rp1 ON mydatabase`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "Ensure retention policy for non existing db is not created", - command: `CREATE RETENTION POLICY rp0 ON nodb DURATION 1h REPLICATION 1`, - exp: `{"results":[{"statement_id":0,"error":"database not found: nodb"}]}`, - once: true, - }, - &Query{ - name: "drop rp0", - command: `DROP RETENTION POLICY rp0 ON db0`, - exp: `{"results":[{"statement_id":0}]}`, - }, - // INF Shard Group Duration will normalize to the Retention Policy Duration Default - &Query{ - name: "create retention policy with inf shard group duration", - command: `CREATE RETENTION POLICY rpinf ON db0 DURATION INF REPLICATION 1 SHARD DURATION 0s`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - // 0s Shard Group Duration will normalize to the Replication Policy Duration - &Query{ - name: "create retention policy with 0s shard group duration", - command: `CREATE RETENTION POLICY rpzero ON db0 DURATION 1h REPLICATION 1 SHARD DURATION 0s`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - // 1s Shard Group Duration will normalize to the MinDefaultRetentionPolicyDuration - &Query{ - name: "create retention policy with 1s shard group duration", - command: `CREATE RETENTION POLICY rponesecond ON db0 DURATION 2h REPLICATION 1 SHARD DURATION 1s`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "show retention policy: validate normalized shard group durations are working", - command: `SHOW RETENTION POLICIES ON db0`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["name","duration","shardGroupDuration","replicaN","default"],"values":[["rpinf","0s","168h0m0s",1,false],["rpzero","1h0m0s","1h0m0s",1,false],["rponesecond","2h0m0s","1h0m0s",1,false]]}]}]}`, - }, - }, - } - - tests["retention_policy_auto_create"] = Test{ - queries: []*Query{ - &Query{ - name: "create database should succeed", - command: `CREATE DATABASE db0`, - exp: `{"results":[{"statement_id":0}]}`, - once: true, - }, - &Query{ - name: "show retention policies should return auto-created policy", - command: `SHOW RETENTION POLICIES ON db0`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["name","duration","shardGroupDuration","replicaN","default"],"values":[["autogen","0s","168h0m0s",1,true]]}]}]}`, - }, - }, - } - -} - -func (tests Tests) load(t *testing.T, key string) Test { - test, ok := tests[key] - if !ok { - t.Fatalf("no test %q", key) - } - - return test.duplicate() -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_test.go b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_test.go deleted file mode 100644 index a3fa82c0..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_test.go +++ /dev/null @@ -1,7090 +0,0 @@ -package run_test - -import ( - "flag" - "fmt" - "net/http" - "net/url" - "os" - "strconv" - "strings" - "testing" - "time" - - "github.com/influxdata/influxdb/coordinator" - "github.com/influxdata/influxdb/models" -) - -// Global server used by benchmarks -var benchServer *Server - -func TestMain(m *testing.M) { - flag.Parse() - - // Setup - c := NewConfig() - c.Retention.Enabled = false - c.Monitor.StoreEnabled = false - c.Meta.LoggingEnabled = false - c.Admin.Enabled = false - c.Subscriber.Enabled = false - c.ContinuousQuery.Enabled = false - c.Data.MaxSeriesPerDatabase = 10000000 // 10M - c.Data.MaxValuesPerTag = 1000000 // 1M - benchServer = OpenDefaultServer(c) - - // Run suite. - r := m.Run() - - // Cleanup - benchServer.Close() - - os.Exit(r) -} - -// Ensure that HTTP responses include the InfluxDB version. -func TestServer_HTTPResponseVersion(t *testing.T) { - version := "v1234" - s := OpenServerWithVersion(NewConfig(), version) - defer s.Close() - - resp, _ := http.Get(s.URL() + "/query") - got := resp.Header.Get("X-Influxdb-Version") - if got != version { - t.Errorf("Server responded with incorrect version, exp %s, got %s", version, got) - } -} - -// Ensure the database commands work. -func TestServer_DatabaseCommands(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := tests.load(t, "database_commands") - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_DropAndRecreateDatabase(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := tests.load(t, "drop_and_recreate_database") - - if err := s.CreateDatabaseAndRetentionPolicy(test.database(), newRetentionPolicySpec(test.retentionPolicy(), 1, 0), true); err != nil { - t.Fatal(err) - } - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_DropDatabaseIsolated(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := tests.load(t, "drop_database_isolated") - - if err := s.CreateDatabaseAndRetentionPolicy(test.database(), newRetentionPolicySpec(test.retentionPolicy(), 1, 0), true); err != nil { - t.Fatal(err) - } - if err := s.CreateDatabaseAndRetentionPolicy("db1", newRetentionPolicySpec("rp1", 1, 0), true); err != nil { - t.Fatal(err) - } - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_DeleteSeries(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := tests.load(t, "delete_series") - - if err := s.CreateDatabaseAndRetentionPolicy(test.database(), newRetentionPolicySpec(test.retentionPolicy(), 1, 0), true); err != nil { - t.Fatal(err) - } - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_DropAndRecreateSeries(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := tests.load(t, "drop_and_recreate_series") - - if err := s.CreateDatabaseAndRetentionPolicy(test.database(), newRetentionPolicySpec(test.retentionPolicy(), 1, 0), true); err != nil { - t.Fatal(err) - } - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } - - // Re-write data and test again. - retest := tests.load(t, "drop_and_recreate_series_retest") - - for i, query := range retest.queries { - if i == 0 { - if err := retest.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_DropSeriesFromRegex(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := tests.load(t, "drop_series_from_regex") - - if err := s.CreateDatabaseAndRetentionPolicy(test.database(), newRetentionPolicySpec(test.retentionPolicy(), 1, 0), true); err != nil { - t.Fatal(err) - } - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure retention policy commands work. -func TestServer_RetentionPolicyCommands(t *testing.T) { - t.Parallel() - c := NewConfig() - c.Meta.RetentionAutoCreate = false - s := OpenServer(c) - defer s.Close() - - test := tests.load(t, "retention_policy_commands") - - // Create a database. - if _, err := s.MetaClient.CreateDatabase(test.database()); err != nil { - t.Fatal(err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the autocreation of retention policy works. -func TestServer_DatabaseRetentionPolicyAutoCreate(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := tests.load(t, "retention_policy_auto_create") - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure user commands work. -func TestServer_UserCommands(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - // Create a database. - if _, err := s.MetaClient.CreateDatabase("db0"); err != nil { - t.Fatal(err) - } - - test := Test{ - queries: []*Query{ - &Query{ - name: "show users, no actual users", - command: `SHOW USERS`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["user","admin"]}]}]}`, - }, - &Query{ - name: `create user`, - command: "CREATE USER jdoe WITH PASSWORD '1337'", - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "show users, 1 existing user", - command: `SHOW USERS`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["user","admin"],"values":[["jdoe",false]]}]}]}`, - }, - &Query{ - name: "grant all priviledges to jdoe", - command: `GRANT ALL PRIVILEGES TO jdoe`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "show users, existing user as admin", - command: `SHOW USERS`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["user","admin"],"values":[["jdoe",true]]}]}]}`, - }, - &Query{ - name: "grant DB privileges to user", - command: `GRANT READ ON db0 TO jdoe`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "revoke all privileges", - command: `REVOKE ALL PRIVILEGES FROM jdoe`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "bad create user request", - command: `CREATE USER 0xBAD WITH PASSWORD pwd1337`, - exp: `{"error":"error parsing query: found 0xBAD, expected identifier at line 1, char 13"}`, - }, - &Query{ - name: "bad create user request, no name", - command: `CREATE USER WITH PASSWORD pwd1337`, - exp: `{"error":"error parsing query: found WITH, expected identifier at line 1, char 13"}`, - }, - &Query{ - name: "bad create user request, no password", - command: `CREATE USER jdoe`, - exp: `{"error":"error parsing query: found EOF, expected WITH at line 1, char 18"}`, - }, - &Query{ - name: "drop user", - command: `DROP USER jdoe`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "make sure user was dropped", - command: `SHOW USERS`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["user","admin"]}]}]}`, - }, - &Query{ - name: "delete non existing user", - command: `DROP USER noone`, - exp: `{"results":[{"statement_id":0,"error":"user not found"}]}`, - }, - }, - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(fmt.Sprintf("command: %s - err: %s", query.command, query.Error(err))) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can create a single point via line protocol with float type and read it back. -func TestServer_Write_LineProtocol_Float(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 1*time.Hour), true); err != nil { - t.Fatal(err) - } - - now := now() - if res, err := s.Write("db0", "rp0", `cpu,host=server01 value=1.0 `+strconv.FormatInt(now.UnixNano(), 10), nil); err != nil { - t.Fatal(err) - } else if exp := ``; exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } - - // Verify the data was written. - if res, err := s.Query(`SELECT * FROM db0.rp0.cpu GROUP BY *`); err != nil { - t.Fatal(err) - } else if exp := fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["%s",1]]}]}]}`, now.Format(time.RFC3339Nano)); exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } -} - -// Ensure the server can create a single point via line protocol with bool type and read it back. -func TestServer_Write_LineProtocol_Bool(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 1*time.Hour), true); err != nil { - t.Fatal(err) - } - - now := now() - if res, err := s.Write("db0", "rp0", `cpu,host=server01 value=true `+strconv.FormatInt(now.UnixNano(), 10), nil); err != nil { - t.Fatal(err) - } else if exp := ``; exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } - - // Verify the data was written. - if res, err := s.Query(`SELECT * FROM db0.rp0.cpu GROUP BY *`); err != nil { - t.Fatal(err) - } else if exp := fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["%s",true]]}]}]}`, now.Format(time.RFC3339Nano)); exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } -} - -// Ensure the server can create a single point via line protocol with string type and read it back. -func TestServer_Write_LineProtocol_String(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 1*time.Hour), true); err != nil { - t.Fatal(err) - } - - now := now() - if res, err := s.Write("db0", "rp0", `cpu,host=server01 value="disk full" `+strconv.FormatInt(now.UnixNano(), 10), nil); err != nil { - t.Fatal(err) - } else if exp := ``; exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } - - // Verify the data was written. - if res, err := s.Query(`SELECT * FROM db0.rp0.cpu GROUP BY *`); err != nil { - t.Fatal(err) - } else if exp := fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["%s","disk full"]]}]}]}`, now.Format(time.RFC3339Nano)); exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } -} - -// Ensure the server can create a single point via line protocol with integer type and read it back. -func TestServer_Write_LineProtocol_Integer(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 1*time.Hour), true); err != nil { - t.Fatal(err) - } - - now := now() - if res, err := s.Write("db0", "rp0", `cpu,host=server01 value=100 `+strconv.FormatInt(now.UnixNano(), 10), nil); err != nil { - t.Fatal(err) - } else if exp := ``; exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } - - // Verify the data was written. - if res, err := s.Query(`SELECT * FROM db0.rp0.cpu GROUP BY *`); err != nil { - t.Fatal(err) - } else if exp := fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["%s",100]]}]}]}`, now.Format(time.RFC3339Nano)); exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } -} - -// Ensure the server returns a partial write response when some points fail to parse. Also validate that -// the successfully parsed points can be queried. -func TestServer_Write_LineProtocol_Partial(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 1*time.Hour), true); err != nil { - t.Fatal(err) - } - - now := now() - points := []string{ - "cpu,host=server01 value=100 " + strconv.FormatInt(now.UnixNano(), 10), - "cpu,host=server01 value=NaN " + strconv.FormatInt(now.UnixNano(), 20), - "cpu,host=server01 value=NaN " + strconv.FormatInt(now.UnixNano(), 30), - } - if res, err := s.Write("db0", "rp0", strings.Join(points, "\n"), nil); err == nil { - t.Fatal("expected error. got nil", err) - } else if exp := ``; exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } else if exp := "partial write"; !strings.Contains(err.Error(), exp) { - t.Fatalf("unexpected error: exp\nexp: %v\ngot: %v", exp, err) - } - - // Verify the data was written. - if res, err := s.Query(`SELECT * FROM db0.rp0.cpu GROUP BY *`); err != nil { - t.Fatal(err) - } else if exp := fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["%s",100]]}]}]}`, now.Format(time.RFC3339Nano)); exp != res { - t.Fatalf("unexpected results\nexp: %s\ngot: %s\n", exp, res) - } -} - -// Ensure the server can query with default databases (via param) and default retention policy -func TestServer_Query_DefaultDBAndRP(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=1.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T01:00:00Z").UnixNano())}, - } - - test.addQueries([]*Query{ - &Query{ - name: "default db and rp", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM cpu GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T01:00:00Z",1]]}]}]}`, - }, - &Query{ - name: "default rp exists", - command: `show retention policies ON db0`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["name","duration","shardGroupDuration","replicaN","default"],"values":[["autogen","0s","168h0m0s",1,false],["rp0","0s","168h0m0s",1,true]]}]}]}`, - }, - &Query{ - name: "default rp", - command: `SELECT * FROM db0..cpu GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T01:00:00Z",1]]}]}]}`, - }, - &Query{ - name: "default dp", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM rp0.cpu GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T01:00:00Z",1]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can have a database with multiple measurements. -func TestServer_Query_Multiple_Measurements(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - // Make sure we do writes for measurements that will span across shards - writes := []string{ - fmt.Sprintf("cpu,host=server01 value=100,core=4 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf("cpu1,host=server02 value=50,core=2 %d", mustParseTime(time.RFC3339Nano, "2015-01-01T00:00:00Z").UnixNano()), - } - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "measurement in one shard but not another shouldn't panic server", - command: `SELECT host,value FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","value"],"values":[["2000-01-01T00:00:00Z","server01",100]]}]}]}`, - }, - &Query{ - name: "measurement in one shard but not another shouldn't panic server", - command: `SELECT host,value FROM db0.rp0.cpu GROUP BY host`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","host","value"],"values":[["2000-01-01T00:00:00Z","server01",100]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server correctly supports data with identical tag values. -func TestServer_Query_IdenticalTagValues(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - writes := []string{ - fmt.Sprintf("cpu,t1=val1 value=1 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf("cpu,t2=val2 value=2 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:00Z").UnixNano()), - fmt.Sprintf("cpu,t1=val2 value=3 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T00:02:00Z").UnixNano()), - } - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "measurements with identical tag values - SELECT *, no GROUP BY", - command: `SELECT * FROM db0.rp0.cpu GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"t1":"","t2":"val2"},"columns":["time","value"],"values":[["2000-01-01T00:01:00Z",2]]},{"name":"cpu","tags":{"t1":"val1","t2":""},"columns":["time","value"],"values":[["2000-01-01T00:00:00Z",1]]},{"name":"cpu","tags":{"t1":"val2","t2":""},"columns":["time","value"],"values":[["2000-01-01T00:02:00Z",3]]}]}]}`, - }, - &Query{ - name: "measurements with identical tag values - SELECT *, with GROUP BY", - command: `SELECT value FROM db0.rp0.cpu GROUP BY t1,t2`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"t1":"","t2":"val2"},"columns":["time","value"],"values":[["2000-01-01T00:01:00Z",2]]},{"name":"cpu","tags":{"t1":"val1","t2":""},"columns":["time","value"],"values":[["2000-01-01T00:00:00Z",1]]},{"name":"cpu","tags":{"t1":"val2","t2":""},"columns":["time","value"],"values":[["2000-01-01T00:02:00Z",3]]}]}]}`, - }, - &Query{ - name: "measurements with identical tag values - SELECT value no GROUP BY", - command: `SELECT value FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:00Z",1],["2000-01-01T00:01:00Z",2],["2000-01-01T00:02:00Z",3]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle a query that involves accessing no shards. -func TestServer_Query_NoShards(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: `cpu,host=server01 value=1 ` + strconv.FormatInt(now.UnixNano(), 10)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "selecting value should succeed", - command: `SELECT value FROM db0.rp0.cpu WHERE time < now() - 1d`, - exp: `{"results":[{"statement_id":0}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can query a non-existent field -func TestServer_Query_NonExistent(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: `cpu,host=server01 value=1 ` + strconv.FormatInt(now.UnixNano(), 10)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "selecting value should succeed", - command: `SELECT value FROM db0.rp0.cpu`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["%s",1]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "selecting non-existent should succeed", - command: `SELECT foo FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can perform basic math -func TestServer_Query_Math(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - writes := []string{ - "float value=42 " + strconv.FormatInt(now.UnixNano(), 10), - "integer value=42i " + strconv.FormatInt(now.UnixNano(), 10), - } - - test := NewTest("db", "rp") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "SELECT multiple of float value", - command: `SELECT value * 2 from db.rp.float`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"float","columns":["time","value"],"values":[["%s",84]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "SELECT multiple of float value", - command: `SELECT 2 * value from db.rp.float`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"float","columns":["time","value"],"values":[["%s",84]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "SELECT multiple of integer value", - command: `SELECT value * 2 from db.rp.integer`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"integer","columns":["time","value"],"values":[["%s",84]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "SELECT float multiple of integer value", - command: `SELECT value * 2.0 from db.rp.integer`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"integer","columns":["time","value"],"values":[["%s",84]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "SELECT square of float value", - command: `SELECT value * value from db.rp.float`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"float","columns":["time","value_value"],"values":[["%s",1764]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "SELECT square of integer value", - command: `SELECT value * value from db.rp.integer`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"integer","columns":["time","value_value"],"values":[["%s",1764]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "SELECT square of integer, float value", - command: `SELECT value * value,float from db.rp.integer`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"integer","columns":["time","value_value","float"],"values":[["%s",1764,null]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "SELECT square of integer value with alias", - command: `SELECT value * value as square from db.rp.integer`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"integer","columns":["time","square"],"values":[["%s",1764]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "SELECT sum of aggregates", - command: `SELECT max(value) + min(value) from db.rp.integer`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"integer","columns":["time","max_min"],"values":[["1970-01-01T00:00:00Z",84]]}]}]}`), - }, - &Query{ - name: "SELECT square of enclosed integer value", - command: `SELECT ((value) * (value)) from db.rp.integer`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"integer","columns":["time","value_value"],"values":[["%s",1764]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "SELECT square of enclosed integer value", - command: `SELECT (value * value) from db.rp.integer`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"integer","columns":["time","value_value"],"values":[["%s",1764]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can query with the count aggregate function -func TestServer_Query_Count(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - - test := NewTest("db0", "rp0") - writes := []string{ - `cpu,host=server01 value=1.0 ` + strconv.FormatInt(now.UnixNano(), 10), - `ram value1=1.0,value2=2.0 ` + strconv.FormatInt(now.UnixNano(), 10), - } - - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - hour_ago := now.Add(-time.Hour).UTC() - - test.addQueries([]*Query{ - &Query{ - name: "selecting count(value) should succeed", - command: `SELECT count(value) FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",1]]}]}]}`, - }, - &Query{ - name: "selecting count(value) with where time should return result", - command: fmt.Sprintf(`SELECT count(value) FROM db0.rp0.cpu WHERE time >= '%s'`, hour_ago.Format(time.RFC3339Nano)), - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","count"],"values":[["%s",1]]}]}]}`, hour_ago.Format(time.RFC3339Nano)), - }, - &Query{ - name: "selecting count(value) with filter that excludes all results should return 0", - command: fmt.Sprintf(`SELECT count(value) FROM db0.rp0.cpu WHERE value=100 AND time >= '%s'`, hour_ago.Format(time.RFC3339Nano)), - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "selecting count(value1) with matching filter against value2 should return correct result", - command: fmt.Sprintf(`SELECT count(value1) FROM db0.rp0.ram WHERE value2=2 AND time >= '%s'`, hour_ago.Format(time.RFC3339Nano)), - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"ram","columns":["time","count"],"values":[["%s",1]]}]}]}`, hour_ago.Format(time.RFC3339Nano)), - }, - &Query{ - name: "selecting count(value1) with non-matching filter against value2 should return correct result", - command: fmt.Sprintf(`SELECT count(value1) FROM db0.rp0.ram WHERE value2=3 AND time >= '%s'`, hour_ago.Format(time.RFC3339Nano)), - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "selecting count(*) should expand the wildcard", - command: `SELECT count(*) FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","count_value"],"values":[["1970-01-01T00:00:00Z",1]]}]}]}`, - }, - &Query{ - name: "selecting count(2) should error", - command: `SELECT count(2) FROM db0.rp0.cpu`, - exp: `{"error":"error parsing query: expected field argument in count()"}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can limit concurrent series. -func TestServer_Query_MaxSelectSeriesN(t *testing.T) { - t.Parallel() - config := NewConfig() - config.Coordinator.MaxSelectSeriesN = 3 - s := OpenServer(config) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: `cpu,host=server01 value=1.0 0`}, - &Write{data: `cpu,host=server02 value=1.0 0`}, - &Write{data: `cpu,host=server03 value=1.0 0`}, - &Write{data: `cpu,host=server04 value=1.0 0`}, - } - - test.addQueries([]*Query{ - &Query{ - name: "exceeed max series", - command: `SELECT COUNT(value) FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"error":"max-select-series limit exceeded: (4/3)"}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can query with Now(). -func TestServer_Query_Now(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: `cpu,host=server01 value=1.0 ` + strconv.FormatInt(now.UnixNano(), 10)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "where with time < now() should work", - command: `SELECT * FROM db0.rp0.cpu where time < now()`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","value"],"values":[["%s","server01",1]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "where with time < now() and GROUP BY * should work", - command: `SELECT * FROM db0.rp0.cpu where time < now() GROUP BY *`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["%s",1]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "where with time > now() should return an empty result", - command: `SELECT * FROM db0.rp0.cpu where time > now()`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "where with time > now() with GROUP BY * should return an empty result", - command: `SELECT * FROM db0.rp0.cpu where time > now() GROUP BY *`, - exp: `{"results":[{"statement_id":0}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can query with epoch precisions. -func TestServer_Query_EpochPrecision(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: `cpu,host=server01 value=1.0 ` + strconv.FormatInt(now.UnixNano(), 10)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "nanosecond precision", - command: `SELECT * FROM db0.rp0.cpu GROUP BY *`, - params: url.Values{"epoch": []string{"n"}}, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[[%d,1]]}]}]}`, now.UnixNano()), - }, - &Query{ - name: "microsecond precision", - command: `SELECT * FROM db0.rp0.cpu GROUP BY *`, - params: url.Values{"epoch": []string{"u"}}, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[[%d,1]]}]}]}`, now.UnixNano()/int64(time.Microsecond)), - }, - &Query{ - name: "millisecond precision", - command: `SELECT * FROM db0.rp0.cpu GROUP BY *`, - params: url.Values{"epoch": []string{"ms"}}, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[[%d,1]]}]}]}`, now.UnixNano()/int64(time.Millisecond)), - }, - &Query{ - name: "second precision", - command: `SELECT * FROM db0.rp0.cpu GROUP BY *`, - params: url.Values{"epoch": []string{"s"}}, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[[%d,1]]}]}]}`, now.UnixNano()/int64(time.Second)), - }, - &Query{ - name: "minute precision", - command: `SELECT * FROM db0.rp0.cpu GROUP BY *`, - params: url.Values{"epoch": []string{"m"}}, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[[%d,1]]}]}]}`, now.UnixNano()/int64(time.Minute)), - }, - &Query{ - name: "hour precision", - command: `SELECT * FROM db0.rp0.cpu GROUP BY *`, - params: url.Values{"epoch": []string{"h"}}, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[[%d,1]]}]}]}`, now.UnixNano()/int64(time.Hour)), - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server works with tag queries. -func TestServer_Query_Tags(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - - writes := []string{ - fmt.Sprintf("cpu,host=server01 value=100,core=4 %d", now.UnixNano()), - fmt.Sprintf("cpu,host=server02 value=50,core=2 %d", now.Add(1).UnixNano()), - - fmt.Sprintf("cpu1,host=server01,region=us-west value=100 %d", mustParseTime(time.RFC3339Nano, "2015-02-28T01:03:36.703820946Z").UnixNano()), - fmt.Sprintf("cpu1,host=server02 value=200 %d", mustParseTime(time.RFC3339Nano, "2010-02-28T01:03:37.703820946Z").UnixNano()), - fmt.Sprintf("cpu1,host=server03 value=300 %d", mustParseTime(time.RFC3339Nano, "2012-02-28T01:03:38.703820946Z").UnixNano()), - - fmt.Sprintf("cpu2,host=server01 value=100 %d", mustParseTime(time.RFC3339Nano, "2015-02-28T01:03:36.703820946Z").UnixNano()), - fmt.Sprintf("cpu2 value=200 %d", mustParseTime(time.RFC3339Nano, "2012-02-28T01:03:38.703820946Z").UnixNano()), - - fmt.Sprintf("cpu3,company=acme01 value=100 %d", mustParseTime(time.RFC3339Nano, "2015-02-28T01:03:36.703820946Z").UnixNano()), - fmt.Sprintf("cpu3 value=200 %d", mustParseTime(time.RFC3339Nano, "2012-02-28T01:03:38.703820946Z").UnixNano()), - - fmt.Sprintf("status_code,url=http://www.example.com value=404 %d", mustParseTime(time.RFC3339Nano, "2015-07-22T08:13:54.929026672Z").UnixNano()), - fmt.Sprintf("status_code,url=https://influxdb.com value=418 %d", mustParseTime(time.RFC3339Nano, "2015-07-22T09:52:24.914395083Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "tag without field should return error", - command: `SELECT host FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"error":"statement must have at least one field in select clause"}]}`, - skip: true, // FIXME(benbjohnson): tags should stream as values - }, - &Query{ - name: "field with tag should succeed", - command: `SELECT host, value FROM db0.rp0.cpu`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","value"],"values":[["%s","server01",100],["%s","server02",50]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }, - &Query{ - name: "field with tag and GROUP BY should succeed", - command: `SELECT host, value FROM db0.rp0.cpu GROUP BY host`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","host","value"],"values":[["%s","server01",100]]},{"name":"cpu","tags":{"host":"server02"},"columns":["time","host","value"],"values":[["%s","server02",50]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }, - &Query{ - name: "field with two tags should succeed", - command: `SELECT host, value, core FROM db0.rp0.cpu`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","value","core"],"values":[["%s","server01",100,4],["%s","server02",50,2]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }, - &Query{ - name: "field with two tags and GROUP BY should succeed", - command: `SELECT host, value, core FROM db0.rp0.cpu GROUP BY host`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","host","value","core"],"values":[["%s","server01",100,4]]},{"name":"cpu","tags":{"host":"server02"},"columns":["time","host","value","core"],"values":[["%s","server02",50,2]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }, - &Query{ - name: "select * with tags should succeed", - command: `SELECT * FROM db0.rp0.cpu`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core","host","value"],"values":[["%s",4,"server01",100],["%s",2,"server02",50]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }, - &Query{ - name: "select * with tags with GROUP BY * should succeed", - command: `SELECT * FROM db0.rp0.cpu GROUP BY *`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","core","value"],"values":[["%s",4,100]]},{"name":"cpu","tags":{"host":"server02"},"columns":["time","core","value"],"values":[["%s",2,50]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }, - &Query{ - name: "group by tag", - command: `SELECT value FROM db0.rp0.cpu GROUP by host`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["%s",100]]},{"name":"cpu","tags":{"host":"server02"},"columns":["time","value"],"values":[["%s",50]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }, - &Query{ - name: "single field (EQ tag value1)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host = 'server01'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",100]]}]}]}`, - }, - &Query{ - name: "single field (2 EQ tags)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host = 'server01' AND region = 'us-west'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",100]]}]}]}`, - }, - &Query{ - name: "single field (OR different tags)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host = 'server03' OR region = 'us-west'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","value"],"values":[["2012-02-28T01:03:38.703820946Z",300],["2015-02-28T01:03:36.703820946Z",100]]}]}]}`, - }, - &Query{ - name: "single field (OR with non-existent tag value)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host = 'server01' OR host = 'server66'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",100]]}]}]}`, - }, - &Query{ - name: "single field (OR with all tag values)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host = 'server01' OR host = 'server02' OR host = 'server03'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","value"],"values":[["2010-02-28T01:03:37.703820946Z",200],["2012-02-28T01:03:38.703820946Z",300],["2015-02-28T01:03:36.703820946Z",100]]}]}]}`, - }, - &Query{ - name: "single field (1 EQ and 1 NEQ tag)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host = 'server01' AND region != 'us-west'`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "single field (EQ tag value2)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host = 'server02'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","value"],"values":[["2010-02-28T01:03:37.703820946Z",200]]}]}]}`, - }, - &Query{ - name: "single field (NEQ tag value1)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host != 'server01'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","value"],"values":[["2010-02-28T01:03:37.703820946Z",200],["2012-02-28T01:03:38.703820946Z",300]]}]}]}`, - }, - &Query{ - name: "single field (NEQ tag value1 AND NEQ tag value2)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host != 'server01' AND host != 'server02'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","value"],"values":[["2012-02-28T01:03:38.703820946Z",300]]}]}]}`, - }, - &Query{ - name: "single field (NEQ tag value1 OR NEQ tag value2)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host != 'server01' OR host != 'server02'`, // Yes, this is always true, but that's the point. - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","value"],"values":[["2010-02-28T01:03:37.703820946Z",200],["2012-02-28T01:03:38.703820946Z",300],["2015-02-28T01:03:36.703820946Z",100]]}]}]}`, - }, - &Query{ - name: "single field (NEQ tag value1 AND NEQ tag value2 AND NEQ tag value3)", - command: `SELECT value FROM db0.rp0.cpu1 WHERE host != 'server01' AND host != 'server02' AND host != 'server03'`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "single field (NEQ tag value1, point without any tags)", - command: `SELECT value FROM db0.rp0.cpu2 WHERE host != 'server01'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu2","columns":["time","value"],"values":[["2012-02-28T01:03:38.703820946Z",200]]}]}]}`, - }, - &Query{ - name: "single field (NEQ tag value1, point without any tags)", - command: `SELECT value FROM db0.rp0.cpu3 WHERE company !~ /acme01/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu3","columns":["time","value"],"values":[["2012-02-28T01:03:38.703820946Z",200]]}]}]}`, - }, - &Query{ - name: "single field (regex tag match)", - command: `SELECT value FROM db0.rp0.cpu3 WHERE company =~ /acme01/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu3","columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",100]]}]}]}`, - }, - &Query{ - name: "single field (regex tag match)", - command: `SELECT value FROM db0.rp0.cpu3 WHERE company !~ /acme[23]/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu3","columns":["time","value"],"values":[["2012-02-28T01:03:38.703820946Z",200],["2015-02-28T01:03:36.703820946Z",100]]}]}]}`, - }, - &Query{ - name: "single field (regex tag match with escaping)", - command: `SELECT value FROM db0.rp0.status_code WHERE url !~ /https\:\/\/influxdb\.com/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"status_code","columns":["time","value"],"values":[["2015-07-22T08:13:54.929026672Z",404]]}]}]}`, - }, - &Query{ - name: "single field (regex tag match with escaping)", - command: `SELECT value FROM db0.rp0.status_code WHERE url =~ /https\:\/\/influxdb\.com/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"status_code","columns":["time","value"],"values":[["2015-07-22T09:52:24.914395083Z",418]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server correctly queries with an alias. -func TestServer_Query_Alias(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - writes := []string{ - fmt.Sprintf("cpu value=1i,steps=3i %d", mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf("cpu value=2i,steps=4i %d", mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:00Z").UnixNano()), - } - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "baseline query - SELECT * FROM db0.rp0.cpu", - command: `SELECT * FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","steps","value"],"values":[["2000-01-01T00:00:00Z",3,1],["2000-01-01T00:01:00Z",4,2]]}]}]}`, - }, - &Query{ - name: "basic query with alias - SELECT steps, value as v FROM db0.rp0.cpu", - command: `SELECT steps, value as v FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","steps","v"],"values":[["2000-01-01T00:00:00Z",3,1],["2000-01-01T00:01:00Z",4,2]]}]}]}`, - }, - &Query{ - name: "double aggregate sum - SELECT sum(value), sum(steps) FROM db0.rp0.cpu", - command: `SELECT sum(value), sum(steps) FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum","sum_1"],"values":[["1970-01-01T00:00:00Z",3,7]]}]}]}`, - }, - &Query{ - name: "double aggregate sum reverse order - SELECT sum(steps), sum(value) FROM db0.rp0.cpu", - command: `SELECT sum(steps), sum(value) FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum","sum_1"],"values":[["1970-01-01T00:00:00Z",7,3]]}]}]}`, - }, - &Query{ - name: "double aggregate sum with alias - SELECT sum(value) as sumv, sum(steps) as sums FROM db0.rp0.cpu", - command: `SELECT sum(value) as sumv, sum(steps) as sums FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sumv","sums"],"values":[["1970-01-01T00:00:00Z",3,7]]}]}]}`, - }, - &Query{ - name: "double aggregate with same value - SELECT sum(value), mean(value) FROM db0.rp0.cpu", - command: `SELECT sum(value), mean(value) FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum","mean"],"values":[["1970-01-01T00:00:00Z",3,1.5]]}]}]}`, - }, - &Query{ - name: "double aggregate with same value and same alias - SELECT mean(value) as mv, max(value) as mv FROM db0.rp0.cpu", - command: `SELECT mean(value) as mv, max(value) as mv FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","mv","mv"],"values":[["1970-01-01T00:00:00Z",1.5,2]]}]}]}`, - }, - &Query{ - name: "double aggregate with non-existent field - SELECT mean(value), max(foo) FROM db0.rp0.cpu", - command: `SELECT mean(value), max(foo) FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","mean","max"],"values":[["1970-01-01T00:00:00Z",1.5,null]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server will succeed and error for common scenarios. -func TestServer_Query_Common(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf("cpu,host=server01 value=1 %s", strconv.FormatInt(now.UnixNano(), 10))}, - } - - test.addQueries([]*Query{ - &Query{ - name: "selecting a from a non-existent database should error", - command: `SELECT value FROM db1.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"error":"database not found: db1"}]}`, - }, - &Query{ - name: "selecting a from a non-existent retention policy should error", - command: `SELECT value FROM db0.rp1.cpu`, - exp: `{"results":[{"statement_id":0,"error":"retention policy not found: rp1"}]}`, - }, - &Query{ - name: "selecting a valid measurement and field should succeed", - command: `SELECT value FROM db0.rp0.cpu`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["%s",1]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "explicitly selecting time and a valid measurement and field should succeed", - command: `SELECT time,value FROM db0.rp0.cpu`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["%s",1]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "selecting a measurement that doesn't exist should result in empty set", - command: `SELECT value FROM db0.rp0.idontexist`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "selecting a field that doesn't exist should result in empty set", - command: `SELECT idontexist FROM db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "selecting wildcard without specifying a database should error", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0,"error":"database name required"}]}`, - }, - &Query{ - name: "selecting explicit field without specifying a database should error", - command: `SELECT value FROM cpu`, - exp: `{"results":[{"statement_id":0,"error":"database name required"}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can query two points. -func TestServer_Query_SelectTwoPoints(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf("cpu value=100 %s\ncpu value=200 %s", strconv.FormatInt(now.UnixNano(), 10), strconv.FormatInt(now.Add(1).UnixNano(), 10))}, - } - - test.addQueries( - &Query{ - name: "selecting two points should result in two points", - command: `SELECT * FROM db0.rp0.cpu`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["%s",100],["%s",200]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }, - &Query{ - name: "selecting two points with GROUP BY * should result in two points", - command: `SELECT * FROM db0.rp0.cpu GROUP BY *`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["%s",100],["%s",200]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }, - ) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can query two negative points. -func TestServer_Query_SelectTwoNegativePoints(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf("cpu value=-100 %s\ncpu value=-200 %s", strconv.FormatInt(now.UnixNano(), 10), strconv.FormatInt(now.Add(1).UnixNano(), 10))}, - } - - test.addQueries(&Query{ - name: "selecting two negative points should succeed", - command: `SELECT * FROM db0.rp0.cpu`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["%s",-100],["%s",-200]]}]}]}`, now.Format(time.RFC3339Nano), now.Add(1).Format(time.RFC3339Nano)), - }) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can query with relative time. -func TestServer_Query_SelectRelativeTime(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - now := now() - yesterday := yesterday() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf("cpu,host=server01 value=100 %s\ncpu,host=server01 value=200 %s", strconv.FormatInt(yesterday.UnixNano(), 10), strconv.FormatInt(now.UnixNano(), 10))}, - } - - test.addQueries([]*Query{ - &Query{ - name: "single point with time pre-calculated for past time queries yesterday", - command: `SELECT * FROM db0.rp0.cpu where time >= '` + yesterday.Add(-1*time.Minute).Format(time.RFC3339Nano) + `' GROUP BY *`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["%s",100],["%s",200]]}]}]}`, yesterday.Format(time.RFC3339Nano), now.Format(time.RFC3339Nano)), - }, - &Query{ - name: "single point with time pre-calculated for relative time queries now", - command: `SELECT * FROM db0.rp0.cpu where time >= now() - 1m GROUP BY *`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["%s",200]]}]}]}`, now.Format(time.RFC3339Nano)), - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various simple derivative queries. -func TestServer_Query_SelectRawDerivative(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf("cpu value=210 1278010021000000000\ncpu value=10 1278010022000000000")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate single derivate", - command: `SELECT derivative(value) from db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",-200]]}]}]}`, - }, - &Query{ - name: "calculate derivate with unit", - command: `SELECT derivative(value, 10s) from db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",-2000]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various simple non_negative_derivative queries. -func TestServer_Query_SelectRawNonNegativeDerivative(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=10 1278010021000000000 -cpu value=15 1278010022000000000 -cpu value=10 1278010023000000000 -cpu value=20 1278010024000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate single non_negative_derivative", - command: `SELECT non_negative_derivative(value) from db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","non_negative_derivative"],"values":[["2010-07-01T18:47:02Z",5],["2010-07-01T18:47:04Z",10]]}]}]}`, - }, - &Query{ - name: "calculate single non_negative_derivative", - command: `SELECT non_negative_derivative(value, 10s) from db0.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","non_negative_derivative"],"values":[["2010-07-01T18:47:02Z",50],["2010-07-01T18:47:04Z",100]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various group by time derivative queries. -func TestServer_Query_SelectGroupByTimeDerivative(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=10 1278010020000000000 -cpu value=15 1278010021000000000 -cpu value=20 1278010022000000000 -cpu value=25 1278010023000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate derivative of count with unit default (2s) group by time", - command: `SELECT derivative(count(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",2],["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of count with unit 4s group by time", - command: `SELECT derivative(count(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",4],["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mean with unit default (2s) group by time", - command: `SELECT derivative(mean(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mean with unit 4s group by time", - command: `SELECT derivative(mean(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of median with unit default (2s) group by time", - command: `SELECT derivative(median(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of median with unit 4s group by time", - command: `SELECT derivative(median(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mode with unit default (2s) group by time", - command: `SELECT derivative(mode(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mode with unit 4s group by time", - command: `SELECT derivative(mode(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - - &Query{ - name: "calculate derivative of sum with unit default (2s) group by time", - command: `SELECT derivative(sum(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of sum with unit 4s group by time", - command: `SELECT derivative(sum(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",40]]}]}]}`, - }, - &Query{ - name: "calculate derivative of first with unit default (2s) group by time", - command: `SELECT derivative(first(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of first with unit 4s group by time", - command: `SELECT derivative(first(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of last with unit default (2s) group by time", - command: `SELECT derivative(last(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of last with unit 4s group by time", - command: `SELECT derivative(last(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of min with unit default (2s) group by time", - command: `SELECT derivative(min(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of min with unit 4s group by time", - command: `SELECT derivative(min(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of max with unit default (2s) group by time", - command: `SELECT derivative(max(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of max with unit 4s group by time", - command: `SELECT derivative(max(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of percentile with unit default (2s) group by time", - command: `SELECT derivative(percentile(value, 50)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of percentile with unit 4s group by time", - command: `SELECT derivative(percentile(value, 50), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various group by time derivative queries. -func TestServer_Query_SelectGroupByTimeDerivativeWithFill(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=10 1278010020000000000 -cpu value=20 1278010021000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate derivative of count with unit default (2s) group by time with fill 0", - command: `SELECT derivative(count(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",2],["2010-07-01T18:47:02Z",-2]]}]}]}`, - }, - &Query{ - name: "calculate derivative of count with unit 4s group by time with fill 0", - command: `SELECT derivative(count(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",4],["2010-07-01T18:47:02Z",-4]]}]}]}`, - }, - &Query{ - name: "calculate derivative of count with unit default (2s) group by time with fill previous", - command: `SELECT derivative(count(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of count with unit 4s group by time with fill previous", - command: `SELECT derivative(count(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mean with unit default (2s) group by time with fill 0", - command: `SELECT derivative(mean(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",-15]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mean with unit 4s group by time with fill 0", - command: `SELECT derivative(mean(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",30],["2010-07-01T18:47:02Z",-30]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mean with unit default (2s) group by time with fill previous", - command: `SELECT derivative(mean(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mean with unit 4s group by time with fill previous", - command: `SELECT derivative(mean(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of median with unit default (2s) group by time with fill 0", - command: `SELECT derivative(median(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",-15]]}]}]}`, - }, - &Query{ - name: "calculate derivative of median with unit 4s group by time with fill 0", - command: `SELECT derivative(median(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",30],["2010-07-01T18:47:02Z",-30]]}]}]}`, - }, - &Query{ - name: "calculate derivative of median with unit default (2s) group by time with fill previous", - command: `SELECT derivative(median(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of median with unit 4s group by time with fill previous", - command: `SELECT derivative(median(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mode with unit default (2s) group by time with fill 0", - command: `SELECT derivative(mode(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",-10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mode with unit 4s group by time with fill 0", - command: `SELECT derivative(mode(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",-20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mode with unit default (2s) group by time with fill previous", - command: `SELECT derivative(mode(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of mode with unit 4s group by time with fill previous", - command: `SELECT derivative(mode(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of sum with unit default (2s) group by time with fill 0", - command: `SELECT derivative(sum(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",30],["2010-07-01T18:47:02Z",-30]]}]}]}`, - }, - &Query{ - name: "calculate derivative of sum with unit 4s group by time with fill 0", - command: `SELECT derivative(sum(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",60],["2010-07-01T18:47:02Z",-60]]}]}]}`, - }, - &Query{ - name: "calculate derivative of sum with unit default (2s) group by time with fill previous", - command: `SELECT derivative(sum(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of sum with unit 4s group by time with fill previous", - command: `SELECT derivative(sum(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of first with unit default (2s) group by time with fill 0", - command: `SELECT derivative(first(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",-10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of first with unit 4s group by time with fill 0", - command: `SELECT derivative(first(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",-20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of first with unit default (2s) group by time with fill previous", - command: `SELECT derivative(first(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of first with unit 4s group by time with fill previous", - command: `SELECT derivative(first(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of last with unit default (2s) group by time with fill 0", - command: `SELECT derivative(last(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",-20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of last with unit 4s group by time with fill 0", - command: `SELECT derivative(last(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",40],["2010-07-01T18:47:02Z",-40]]}]}]}`, - }, - &Query{ - name: "calculate derivative of last with unit default (2s) group by time with fill previous", - command: `SELECT derivative(last(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of last with unit 4s group by time with fill previous", - command: `SELECT derivative(last(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of min with unit default (2s) group by time with fill 0", - command: `SELECT derivative(min(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",-10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of min with unit 4s group by time with fill 0", - command: `SELECT derivative(min(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",-20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of min with unit default (2s) group by time with fill previous", - command: `SELECT derivative(min(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of min with unit 4s group by time with fill previous", - command: `SELECT derivative(min(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of max with unit default (2s) group by time with fill 0", - command: `SELECT derivative(max(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",-20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of max with unit 4s group by time with fill 0", - command: `SELECT derivative(max(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",40],["2010-07-01T18:47:02Z",-40]]}]}]}`, - }, - &Query{ - name: "calculate derivative of max with unit default (2s) group by time with fill previous", - command: `SELECT derivative(max(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of max with unit 4s group by time with fill previous", - command: `SELECT derivative(max(value), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of percentile with unit default (2s) group by time with fill 0", - command: `SELECT derivative(percentile(value, 50)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",-10]]}]}]}`, - }, - &Query{ - name: "calculate derivative of percentile with unit 4s group by time with fill 0", - command: `SELECT derivative(percentile(value, 50), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",-20]]}]}]}`, - }, - &Query{ - name: "calculate derivative of percentile with unit default (2s) group by time with fill previous", - command: `SELECT derivative(percentile(value, 50)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate derivative of percentile with unit 4s group by time with fill previous", - command: `SELECT derivative(percentile(value, 50), 4s) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","derivative"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various group by time difference queries. -func TestServer_Query_SelectGroupByTimeDifference(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=10 1278010020000000000 -cpu value=15 1278010021000000000 -cpu value=20 1278010022000000000 -cpu value=25 1278010023000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate difference of count", - command: `SELECT difference(count(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",2],["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of mean", - command: `SELECT difference(mean(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate difference of median", - command: `SELECT difference(median(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate difference of mode", - command: `SELECT difference(mode(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate difference of sum", - command: `SELECT difference(sum(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate difference of first", - command: `SELECT difference(first(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate difference of last", - command: `SELECT difference(last(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate difference of min", - command: `SELECT difference(min(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate difference of max", - command: `SELECT difference(max(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate difference of percentile", - command: `SELECT difference(percentile(value, 50)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various group by time difference queries with fill. -func TestServer_Query_SelectGroupByTimeDifferenceWithFill(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=10 1278010020000000000 -cpu value=20 1278010021000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate difference of count with fill 0", - command: `SELECT difference(count(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",2],["2010-07-01T18:47:02Z",-2]]}]}]}`, - }, - &Query{ - name: "calculate difference of count with fill previous", - command: `SELECT difference(count(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of mean with fill 0", - command: `SELECT difference(mean(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",-15]]}]}]}`, - }, - &Query{ - name: "calculate difference of mean with fill previous", - command: `SELECT difference(mean(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of median with fill 0", - command: `SELECT difference(median(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",-15]]}]}]}`, - }, - &Query{ - name: "calculate difference of median with fill previous", - command: `SELECT difference(median(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of mode with fill 0", - command: `SELECT difference(mode(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",-10]]}]}]}`, - }, - &Query{ - name: "calculate difference of mode with fill previous", - command: `SELECT difference(mode(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of sum with fill 0", - command: `SELECT difference(sum(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",30],["2010-07-01T18:47:02Z",-30]]}]}]}`, - }, - &Query{ - name: "calculate difference of sum with fill previous", - command: `SELECT difference(sum(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of first with fill 0", - command: `SELECT difference(first(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",-10]]}]}]}`, - }, - &Query{ - name: "calculate difference of first with fill previous", - command: `SELECT difference(first(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of last with fill 0", - command: `SELECT difference(last(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",-20]]}]}]}`, - }, - &Query{ - name: "calculate difference of last with fill previous", - command: `SELECT difference(last(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of min with fill 0", - command: `SELECT difference(min(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",-10]]}]}]}`, - }, - &Query{ - name: "calculate difference of min with fill previous", - command: `SELECT difference(min(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of max with fill 0", - command: `SELECT difference(max(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",-20]]}]}]}`, - }, - &Query{ - name: "calculate difference of max with fill previous", - command: `SELECT difference(max(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - &Query{ - name: "calculate difference of percentile with fill 0", - command: `SELECT difference(percentile(value, 50)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",-10]]}]}]}`, - }, - &Query{ - name: "calculate difference of percentile with fill previous", - command: `SELECT difference(percentile(value, 50)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","difference"],"values":[["2010-07-01T18:47:02Z",0]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various group by time moving average queries. -func TestServer_Query_SelectGroupByTimeMovingAverage(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=10 1278010020000000000 -cpu value=15 1278010021000000000 -cpu value=20 1278010022000000000 -cpu value=25 1278010023000000000 -cpu value=30 1278010024000000000 -cpu value=35 1278010025000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate moving average of count", - command: `SELECT moving_average(count(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",1],["2010-07-01T18:47:02Z",2],["2010-07-01T18:47:04Z",2]]}]}]}`, - }, - &Query{ - name: "calculate moving average of mean", - command: `SELECT moving_average(mean(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",17.5],["2010-07-01T18:47:04Z",27.5]]}]}]}`, - }, - &Query{ - name: "calculate moving average of median", - command: `SELECT moving_average(median(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",17.5],["2010-07-01T18:47:04Z",27.5]]}]}]}`, - }, - &Query{ - name: "calculate moving average of mode", - command: `SELECT moving_average(mode(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",15],["2010-07-01T18:47:04Z",25]]}]}]}`, - }, - &Query{ - name: "calculate moving average of sum", - command: `SELECT moving_average(sum(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",35],["2010-07-01T18:47:04Z",55]]}]}]}`, - }, - &Query{ - name: "calculate moving average of first", - command: `SELECT moving_average(first(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",15],["2010-07-01T18:47:04Z",25]]}]}]}`, - }, - &Query{ - name: "calculate moving average of last", - command: `SELECT moving_average(last(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",20],["2010-07-01T18:47:04Z",30]]}]}]}`, - }, - &Query{ - name: "calculate moving average of min", - command: `SELECT moving_average(min(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",15],["2010-07-01T18:47:04Z",25]]}]}]}`, - }, - &Query{ - name: "calculate moving average of max", - command: `SELECT moving_average(max(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",20],["2010-07-01T18:47:04Z",30]]}]}]}`, - }, - &Query{ - name: "calculate moving average of percentile", - command: `SELECT moving_average(percentile(value, 50), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",15],["2010-07-01T18:47:04Z",25]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various group by time moving average queries. -func TestServer_Query_SelectGroupByTimeMovingAverageWithFill(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=10 1278010020000000000 -cpu value=15 1278010021000000000 -cpu value=30 1278010024000000000 -cpu value=35 1278010025000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate moving average of count with fill 0", - command: `SELECT moving_average(count(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",1],["2010-07-01T18:47:02Z",1],["2010-07-01T18:47:04Z",1]]}]}]}`, - }, - &Query{ - name: "calculate moving average of count with fill previous", - command: `SELECT moving_average(count(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",2],["2010-07-01T18:47:04Z",2]]}]}]}`, - }, - &Query{ - name: "calculate moving average of mean with fill 0", - command: `SELECT moving_average(mean(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",6.25],["2010-07-01T18:47:02Z",6.25],["2010-07-01T18:47:04Z",16.25]]}]}]}`, - }, - &Query{ - name: "calculate moving average of mean with fill previous", - command: `SELECT moving_average(mean(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",12.5],["2010-07-01T18:47:04Z",22.5]]}]}]}`, - }, - &Query{ - name: "calculate moving average of median with fill 0", - command: `SELECT moving_average(median(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",6.25],["2010-07-01T18:47:02Z",6.25],["2010-07-01T18:47:04Z",16.25]]}]}]}`, - }, - &Query{ - name: "calculate moving average of median with fill previous", - command: `SELECT moving_average(median(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",12.5],["2010-07-01T18:47:04Z",22.5]]}]}]}`, - }, - &Query{ - name: "calculate moving average of mode with fill 0", - command: `SELECT moving_average(mode(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",5],["2010-07-01T18:47:02Z",5],["2010-07-01T18:47:04Z",15]]}]}]}`, - }, - &Query{ - name: "calculate moving average of mode with fill previous", - command: `SELECT moving_average(mode(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",10],["2010-07-01T18:47:04Z",20]]}]}]}`, - }, - &Query{ - name: "calculate moving average of sum with fill 0", - command: `SELECT moving_average(sum(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",12.5],["2010-07-01T18:47:02Z",12.5],["2010-07-01T18:47:04Z",32.5]]}]}]}`, - }, - &Query{ - name: "calculate moving average of sum with fill previous", - command: `SELECT moving_average(sum(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",25],["2010-07-01T18:47:04Z",45]]}]}]}`, - }, - &Query{ - name: "calculate moving average of first with fill 0", - command: `SELECT moving_average(first(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",5],["2010-07-01T18:47:02Z",5],["2010-07-01T18:47:04Z",15]]}]}]}`, - }, - &Query{ - name: "calculate moving average of first with fill previous", - command: `SELECT moving_average(first(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",10],["2010-07-01T18:47:04Z",20]]}]}]}`, - }, - &Query{ - name: "calculate moving average of last with fill 0", - command: `SELECT moving_average(last(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",7.5],["2010-07-01T18:47:02Z",7.5],["2010-07-01T18:47:04Z",17.5]]}]}]}`, - }, - &Query{ - name: "calculate moving average of last with fill previous", - command: `SELECT moving_average(last(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",15],["2010-07-01T18:47:04Z",25]]}]}]}`, - }, - &Query{ - name: "calculate moving average of min with fill 0", - command: `SELECT moving_average(min(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",5],["2010-07-01T18:47:02Z",5],["2010-07-01T18:47:04Z",15]]}]}]}`, - }, - &Query{ - name: "calculate moving average of min with fill previous", - command: `SELECT moving_average(min(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",10],["2010-07-01T18:47:04Z",20]]}]}]}`, - }, - &Query{ - name: "calculate moving average of max with fill 0", - command: `SELECT moving_average(max(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",7.5],["2010-07-01T18:47:02Z",7.5],["2010-07-01T18:47:04Z",17.5]]}]}]}`, - }, - &Query{ - name: "calculate moving average of max with fill previous", - command: `SELECT moving_average(max(value), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",15],["2010-07-01T18:47:04Z",25]]}]}]}`, - }, - &Query{ - name: "calculate moving average of percentile with fill 0", - command: `SELECT moving_average(percentile(value, 50), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:00Z",5],["2010-07-01T18:47:02Z",5],["2010-07-01T18:47:04Z",15]]}]}]}`, - }, - &Query{ - name: "calculate moving average of percentile with fill previous", - command: `SELECT moving_average(percentile(value, 50), 2) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:05' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","moving_average"],"values":[["2010-07-01T18:47:02Z",10],["2010-07-01T18:47:04Z",20]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various group by time cumulative sum queries. -func TestServer_Query_SelectGroupByTimeCumulativeSum(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=10 1278010020000000000 -cpu value=15 1278010021000000000 -cpu value=20 1278010022000000000 -cpu value=25 1278010023000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate cumulative sum of count", - command: `SELECT cumulative_sum(count(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",2],["2010-07-01T18:47:02Z",4]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of mean", - command: `SELECT cumulative_sum(mean(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",12.5],["2010-07-01T18:47:02Z",35]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of median", - command: `SELECT cumulative_sum(median(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",12.5],["2010-07-01T18:47:02Z",35]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of mode", - command: `SELECT cumulative_sum(mode(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",30]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of sum", - command: `SELECT cumulative_sum(sum(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",25],["2010-07-01T18:47:02Z",70]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of first", - command: `SELECT cumulative_sum(first(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",30]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of last", - command: `SELECT cumulative_sum(last(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",40]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of min", - command: `SELECT cumulative_sum(min(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",30]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of max", - command: `SELECT cumulative_sum(max(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",40]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of percentile", - command: `SELECT cumulative_sum(percentile(value, 50)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",30]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure the server can handle various group by time cumulative sum queries with fill. -func TestServer_Query_SelectGroupByTimeCumulativeSumWithFill(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=10 1278010020000000000 -cpu value=20 1278010021000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "calculate cumulative sum of count with fill 0", - command: `SELECT cumulative_sum(count(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",2],["2010-07-01T18:47:02Z",2]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of count with fill previous", - command: `SELECT cumulative_sum(count(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",2],["2010-07-01T18:47:02Z",4]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of mean with fill 0", - command: `SELECT cumulative_sum(mean(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",15]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of mean with fill previous", - command: `SELECT cumulative_sum(mean(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",30]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of median with fill 0", - command: `SELECT cumulative_sum(median(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",15]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of median with fill previous", - command: `SELECT cumulative_sum(median(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",15],["2010-07-01T18:47:02Z",30]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of mode with fill 0", - command: `SELECT cumulative_sum(mode(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of mode with fill previous", - command: `SELECT cumulative_sum(mode(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of sum with fill 0", - command: `SELECT cumulative_sum(sum(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",30],["2010-07-01T18:47:02Z",30]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of sum with fill previous", - command: `SELECT cumulative_sum(sum(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",30],["2010-07-01T18:47:02Z",60]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of first with fill 0", - command: `SELECT cumulative_sum(first(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of first with fill previous", - command: `SELECT cumulative_sum(first(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of last with fill 0", - command: `SELECT cumulative_sum(last(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of last with fill previous", - command: `SELECT cumulative_sum(last(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",40]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of min with fill 0", - command: `SELECT cumulative_sum(min(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of min with fill previous", - command: `SELECT cumulative_sum(min(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of max with fill 0", - command: `SELECT cumulative_sum(max(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of max with fill previous", - command: `SELECT cumulative_sum(max(value)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",20],["2010-07-01T18:47:02Z",40]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of percentile with fill 0", - command: `SELECT cumulative_sum(percentile(value, 50)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",10]]}]}]}`, - }, - &Query{ - name: "calculate cumulative sum of percentile with fill previous", - command: `SELECT cumulative_sum(percentile(value, 50)) from db0.rp0.cpu where time >= '2010-07-01 18:47:00' and time <= '2010-07-01 18:47:03' group by time(2s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","cumulative_sum"],"values":[["2010-07-01T18:47:00Z",10],["2010-07-01T18:47:02Z",20]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_SelectGroupByTime_MultipleAggregates(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`test,t=a x=1i 1000000000 -test,t=b y=1i 1000000000 -test,t=a x=2i 2000000000 -test,t=b y=2i 2000000000 -test,t=a x=3i 3000000000 -test,t=b y=3i 3000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "two aggregates with a group by host", - command: `SELECT mean(x) as x, mean(y) as y from db0.rp0.test where time >= 1s and time < 4s group by t, time(1s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"test","tags":{"t":"a"},"columns":["time","x","y"],"values":[["1970-01-01T00:00:01Z",1,null],["1970-01-01T00:00:02Z",2,null],["1970-01-01T00:00:03Z",3,null]]},{"name":"test","tags":{"t":"b"},"columns":["time","x","y"],"values":[["1970-01-01T00:00:01Z",null,1],["1970-01-01T00:00:02Z",null,2],["1970-01-01T00:00:03Z",null,3]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_MathWithFill(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=15 1278010020000000000 -`)}, - } - - test.addQueries([]*Query{ - &Query{ - name: "multiplication with fill previous", - command: `SELECT 4*mean(value) FROM db0.rp0.cpu WHERE time >= '2010-07-01 18:47:00' AND time < '2010-07-01 18:48:30' GROUP BY time(30s) FILL(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","mean"],"values":[["2010-07-01T18:47:00Z",60],["2010-07-01T18:47:30Z",60],["2010-07-01T18:48:00Z",60]]}]}]}`, - }, - &Query{ - name: "multiplication of mode value with fill previous", - command: `SELECT 4*mode(value) FROM db0.rp0.cpu WHERE time >= '2010-07-01 18:47:00' AND time < '2010-07-01 18:48:30' GROUP BY time(30s) FILL(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","mode"],"values":[["2010-07-01T18:47:00Z",60],["2010-07-01T18:47:30Z",60],["2010-07-01T18:48:00Z",60]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// mergeMany ensures that when merging many series together and some of them have a different number -// of points than others in a group by interval the results are correct -func TestServer_Query_MergeMany(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - // set infinite retention policy as we are inserting data in the past and don't want retention policy enforcement to make this test racy - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - test := NewTest("db0", "rp0") - - writes := []string{} - for i := 1; i < 11; i++ { - for j := 1; j < 5+i%3; j++ { - data := fmt.Sprintf(`cpu,host=server_%d value=22 %d`, i, time.Unix(int64(j), int64(0)).UTC().UnixNano()) - writes = append(writes, data) - } - } - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "GROUP by time", - command: `SELECT count(value) FROM db0.rp0.cpu WHERE time >= '1970-01-01T00:00:01Z' AND time <= '1970-01-01T00:00:06Z' GROUP BY time(1s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","count"],"values":[["1970-01-01T00:00:01Z",10],["1970-01-01T00:00:02Z",10],["1970-01-01T00:00:03Z",10],["1970-01-01T00:00:04Z",10],["1970-01-01T00:00:05Z",7],["1970-01-01T00:00:06Z",3]]}]}]}`, - }, - &Query{ - skip: true, - name: "GROUP by tag - FIXME issue #2875", - command: `SELECT count(value) FROM db0.rp0.cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T02:00:00Z' group by host`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","count"],"values":[["2000-01-01T00:00:00Z",1]]},{"name":"cpu","tags":{"host":"server02"},"columns":["time","count"],"values":[["2000-01-01T00:00:00Z",1]]},{"name":"cpu","tags":{"host":"server03"},"columns":["time","count"],"values":[["2000-01-01T00:00:00Z",1]]}]}]}`, - }, - &Query{ - name: "GROUP by field", - command: `SELECT count(value) FROM db0.rp0.cpu group by value`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"value":""},"columns":["time","count"],"values":[["1970-01-01T00:00:00Z",50]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_SLimitAndSOffset(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - // set infinite retention policy as we are inserting data in the past and don't want retention policy enforcement to make this test racy - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - test := NewTest("db0", "rp0") - - writes := []string{} - for i := 1; i < 10; i++ { - data := fmt.Sprintf(`cpu,region=us-east,host=server-%d value=%d %d`, i, i, time.Unix(int64(i), int64(0)).UnixNano()) - writes = append(writes, data) - } - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "SLIMIT 2 SOFFSET 1", - command: `SELECT count(value) FROM db0.rp0.cpu GROUP BY * SLIMIT 2 SOFFSET 1`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server-2","region":"us-east"},"columns":["time","count"],"values":[["1970-01-01T00:00:00Z",1]]},{"name":"cpu","tags":{"host":"server-3","region":"us-east"},"columns":["time","count"],"values":[["1970-01-01T00:00:00Z",1]]}]}]}`, - }, - &Query{ - name: "SLIMIT 2 SOFFSET 3", - command: `SELECT count(value) FROM db0.rp0.cpu GROUP BY * SLIMIT 2 SOFFSET 3`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server-4","region":"us-east"},"columns":["time","count"],"values":[["1970-01-01T00:00:00Z",1]]},{"name":"cpu","tags":{"host":"server-5","region":"us-east"},"columns":["time","count"],"values":[["1970-01-01T00:00:00Z",1]]}]}]}`, - }, - &Query{ - name: "SLIMIT 3 SOFFSET 8", - command: `SELECT count(value) FROM db0.rp0.cpu GROUP BY * SLIMIT 3 SOFFSET 8`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"server-9","region":"us-east"},"columns":["time","count"],"values":[["1970-01-01T00:00:00Z",1]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Regex(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu1,host=server01 value=10 %d`, mustParseTime(time.RFC3339Nano, "2015-02-28T01:03:36.703820946Z").UnixNano()), - fmt.Sprintf(`cpu2,host=server01 value=20 %d`, mustParseTime(time.RFC3339Nano, "2015-02-28T01:03:36.703820946Z").UnixNano()), - fmt.Sprintf(`cpu3,host=server01 value=30 %d`, mustParseTime(time.RFC3339Nano, "2015-02-28T01:03:36.703820946Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "default db and rp", - command: `SELECT * FROM /cpu[13]/`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","columns":["time","host","value"],"values":[["2015-02-28T01:03:36.703820946Z","server01",10]]},{"name":"cpu3","columns":["time","host","value"],"values":[["2015-02-28T01:03:36.703820946Z","server01",30]]}]}]}`, - }, - &Query{ - name: "default db and rp with GROUP BY *", - command: `SELECT * FROM /cpu[13]/ GROUP BY *`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","tags":{"host":"server01"},"columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",10]]},{"name":"cpu3","tags":{"host":"server01"},"columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",30]]}]}]}`, - }, - &Query{ - name: "specifying db and rp", - command: `SELECT * FROM db0.rp0./cpu[13]/ GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","tags":{"host":"server01"},"columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",10]]},{"name":"cpu3","tags":{"host":"server01"},"columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",30]]}]}]}`, - }, - &Query{ - name: "default db and specified rp", - command: `SELECT * FROM rp0./cpu[13]/ GROUP BY *`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","tags":{"host":"server01"},"columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",10]]},{"name":"cpu3","tags":{"host":"server01"},"columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",30]]}]}]}`, - }, - &Query{ - name: "specified db and default rp", - command: `SELECT * FROM db0../cpu[13]/ GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu1","tags":{"host":"server01"},"columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",10]]},{"name":"cpu3","tags":{"host":"server01"},"columns":["time","value"],"values":[["2015-02-28T01:03:36.703820946Z",30]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_Int(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`int value=45 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - // int64 - &Query{ - name: "stddev with just one point - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT STDDEV(value) FROM int`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"int","columns":["time","stddev"],"values":[["1970-01-01T00:00:00Z",null]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_IntMax(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`intmax value=%s %d`, maxInt64(), mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`intmax value=%s %d`, maxInt64(), mustParseTime(time.RFC3339Nano, "2000-01-01T01:00:00Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "large mean and stddev - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MEAN(value), STDDEV(value) FROM intmax`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmax","columns":["time","mean","stddev"],"values":[["1970-01-01T00:00:00Z",` + maxInt64() + `,0]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_IntMany(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`intmany,host=server01 value=2.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`intmany,host=server02 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`intmany,host=server03 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - fmt.Sprintf(`intmany,host=server04 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:30Z").UnixNano()), - fmt.Sprintf(`intmany,host=server05 value=5.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:40Z").UnixNano()), - fmt.Sprintf(`intmany,host=server06 value=5.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:50Z").UnixNano()), - fmt.Sprintf(`intmany,host=server07 value=7.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:00Z").UnixNano()), - fmt.Sprintf(`intmany,host=server08 value=9.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:10Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "mean and stddev - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MEAN(value), STDDEV(value) FROM intmany WHERE time >= '2000-01-01' AND time < '2000-01-01T00:02:00Z' GROUP BY time(10m)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","mean","stddev"],"values":[["2000-01-01T00:00:00Z",5,2.138089935299395]]}]}]}`, - }, - &Query{ - name: "first - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT FIRST(value) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","first"],"values":[["2000-01-01T00:00:00Z",2]]}]}]}`, - }, - &Query{ - name: "first - int - epoch ms", - params: url.Values{"db": []string{"db0"}, "epoch": []string{"ms"}}, - command: `SELECT FIRST(value) FROM intmany`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","first"],"values":[[%d,2]]}]}]}`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()/int64(time.Millisecond)), - }, - &Query{ - name: "last - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT LAST(value) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","last"],"values":[["2000-01-01T00:01:10Z",9]]}]}]}`, - }, - &Query{ - name: "spread - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SPREAD(value) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","spread"],"values":[["1970-01-01T00:00:00Z",7]]}]}]}`, - }, - &Query{ - name: "median - even count - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MEDIAN(value) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","median"],"values":[["1970-01-01T00:00:00Z",4.5]]}]}]}`, - }, - &Query{ - name: "median - odd count - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MEDIAN(value) FROM intmany where time < '2000-01-01T00:01:10Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","median"],"values":[["1970-01-01T00:00:00Z",4]]}]}]}`, - }, - &Query{ - name: "mode - single - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MODE(value) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","mode"],"values":[["1970-01-01T00:00:00Z",4]]}]}]}`, - }, - &Query{ - name: "mode - multiple - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MODE(value) FROM intmany where time < '2000-01-01T00:01:10Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","mode"],"values":[["1970-01-01T00:00:00Z",4]]}]}]}`, - }, - &Query{ - name: "distinct as call - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT DISTINCT(value) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","distinct"],"values":[["1970-01-01T00:00:00Z",2],["1970-01-01T00:00:00Z",4],["1970-01-01T00:00:00Z",5],["1970-01-01T00:00:00Z",7],["1970-01-01T00:00:00Z",9]]}]}]}`, - }, - &Query{ - name: "distinct alt syntax - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT DISTINCT value FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","distinct"],"values":[["1970-01-01T00:00:00Z",2],["1970-01-01T00:00:00Z",4],["1970-01-01T00:00:00Z",5],["1970-01-01T00:00:00Z",7],["1970-01-01T00:00:00Z",9]]}]}]}`, - }, - &Query{ - name: "distinct select tag - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT DISTINCT(host) FROM intmany`, - exp: `{"results":[{"statement_id":0,"error":"statement must have at least one field in select clause"}]}`, - skip: true, // FIXME(benbjohnson): should be allowed, need to stream tag values - }, - &Query{ - name: "distinct alt select tag - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT DISTINCT host FROM intmany`, - exp: `{"results":[{"statement_id":0,"error":"statement must have at least one field in select clause"}]}`, - skip: true, // FIXME(benbjohnson): should be allowed, need to stream tag values - }, - &Query{ - name: "count distinct - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT COUNT(DISTINCT value) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",5]]}]}]}`, - }, - &Query{ - name: "count distinct as call - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT COUNT(DISTINCT(value)) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",5]]}]}]}`, - }, - &Query{ - name: "count distinct select tag - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT COUNT(DISTINCT host) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",0]]}]}]}`, - skip: true, // FIXME(benbjohnson): stream tag values - }, - &Query{ - name: "count distinct as call select tag - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT COUNT(DISTINCT host) FROM intmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",0]]}]}]}`, - skip: true, // FIXME(benbjohnson): stream tag values - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_IntMany_GroupBy(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`intmany,host=server01 value=2.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`intmany,host=server02 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`intmany,host=server03 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - fmt.Sprintf(`intmany,host=server04 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:30Z").UnixNano()), - fmt.Sprintf(`intmany,host=server05 value=5.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:40Z").UnixNano()), - fmt.Sprintf(`intmany,host=server06 value=5.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:50Z").UnixNano()), - fmt.Sprintf(`intmany,host=server07 value=7.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:00Z").UnixNano()), - fmt.Sprintf(`intmany,host=server08 value=9.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:10Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "max order by time with time specified group by 10s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, max(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:14Z' group by time(10s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","max"],"values":[["2000-01-01T00:00:00Z",2],["2000-01-01T00:00:10Z",4],["2000-01-01T00:00:20Z",4],["2000-01-01T00:00:30Z",4],["2000-01-01T00:00:40Z",5],["2000-01-01T00:00:50Z",5],["2000-01-01T00:01:00Z",7],["2000-01-01T00:01:10Z",9]]}]}]}`, - }, - &Query{ - name: "max order by time without time specified group by 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT max(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:14Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","max"],"values":[["2000-01-01T00:00:00Z",4],["2000-01-01T00:00:30Z",5],["2000-01-01T00:01:00Z",9]]}]}]}`, - }, - &Query{ - name: "max order by time with time specified group by 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, max(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:14Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","max"],"values":[["2000-01-01T00:00:00Z",4],["2000-01-01T00:00:30Z",5],["2000-01-01T00:01:00Z",9]]}]}]}`, - }, - &Query{ - name: "min order by time without time specified group by 15s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT min(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:14Z' group by time(15s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","min"],"values":[["2000-01-01T00:00:00Z",2],["2000-01-01T00:00:15Z",4],["2000-01-01T00:00:30Z",4],["2000-01-01T00:00:45Z",5],["2000-01-01T00:01:00Z",7]]}]}]}`, - }, - &Query{ - name: "min order by time with time specified group by 15s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, min(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:14Z' group by time(15s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","min"],"values":[["2000-01-01T00:00:00Z",2],["2000-01-01T00:00:15Z",4],["2000-01-01T00:00:30Z",4],["2000-01-01T00:00:45Z",5],["2000-01-01T00:01:00Z",7]]}]}]}`, - }, - &Query{ - name: "first order by time without time specified group by 15s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT first(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:14Z' group by time(15s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","first"],"values":[["2000-01-01T00:00:00Z",2],["2000-01-01T00:00:15Z",4],["2000-01-01T00:00:30Z",4],["2000-01-01T00:00:45Z",5],["2000-01-01T00:01:00Z",7]]}]}]}`, - }, - &Query{ - name: "first order by time with time specified group by 15s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, first(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:14Z' group by time(15s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","first"],"values":[["2000-01-01T00:00:00Z",2],["2000-01-01T00:00:15Z",4],["2000-01-01T00:00:30Z",4],["2000-01-01T00:00:45Z",5],["2000-01-01T00:01:00Z",7]]}]}]}`, - }, - &Query{ - name: "last order by time without time specified group by 15s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT last(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:14Z' group by time(15s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","last"],"values":[["2000-01-01T00:00:00Z",4],["2000-01-01T00:00:15Z",4],["2000-01-01T00:00:30Z",5],["2000-01-01T00:00:45Z",5],["2000-01-01T00:01:00Z",9]]}]}]}`, - }, - &Query{ - name: "last order by time with time specified group by 15s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, last(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:14Z' group by time(15s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","last"],"values":[["2000-01-01T00:00:00Z",4],["2000-01-01T00:00:15Z",4],["2000-01-01T00:00:30Z",5],["2000-01-01T00:00:45Z",5],["2000-01-01T00:01:00Z",9]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_IntMany_OrderByDesc(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`intmany,host=server01 value=2.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`intmany,host=server02 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`intmany,host=server03 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - fmt.Sprintf(`intmany,host=server04 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:30Z").UnixNano()), - fmt.Sprintf(`intmany,host=server05 value=5.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:40Z").UnixNano()), - fmt.Sprintf(`intmany,host=server06 value=5.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:50Z").UnixNano()), - fmt.Sprintf(`intmany,host=server07 value=7.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:00Z").UnixNano()), - fmt.Sprintf(`intmany,host=server08 value=9.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:10Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "aggregate order by time desc", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT max(value) FROM intmany where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:00Z' group by time(10s) order by time desc`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intmany","columns":["time","max"],"values":[["2000-01-01T00:01:00Z",7],["2000-01-01T00:00:50Z",5],["2000-01-01T00:00:40Z",5],["2000-01-01T00:00:30Z",4],["2000-01-01T00:00:20Z",4],["2000-01-01T00:00:10Z",4],["2000-01-01T00:00:00Z",2]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_IntOverlap(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`intoverlap,region=us-east value=20 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`intoverlap,region=us-east value=30 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`intoverlap,region=us-west value=100 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`intoverlap,region=us-east otherVal=20 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:03Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - /* &Query{ - name: "aggregation with no interval - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT count(value) FROM intoverlap WHERE time = '2000-01-01 00:00:00'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intoverlap","columns":["time","count"],"values":[["2000-01-01T00:00:00Z",2]]}]}]}`, - }, - &Query{ - name: "sum - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM intoverlap WHERE time >= '2000-01-01 00:00:05' AND time <= '2000-01-01T00:00:10Z' GROUP BY time(10s), region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intoverlap","tags":{"region":"us-east"},"columns":["time","sum"],"values":[["2000-01-01T00:00:10Z",30]]}]}]}`, - }, - */&Query{ - name: "aggregation with a null field value - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM intoverlap GROUP BY region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intoverlap","tags":{"region":"us-east"},"columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",50]]},{"name":"intoverlap","tags":{"region":"us-west"},"columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",100]]}]}]}`, - }, - &Query{ - name: "multiple aggregations - int", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value), MEAN(value) FROM intoverlap GROUP BY region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intoverlap","tags":{"region":"us-east"},"columns":["time","sum","mean"],"values":[["1970-01-01T00:00:00Z",50,25]]},{"name":"intoverlap","tags":{"region":"us-west"},"columns":["time","sum","mean"],"values":[["1970-01-01T00:00:00Z",100,100]]}]}]}`, - }, - &Query{ - skip: true, - name: "multiple aggregations with division - int FIXME issue #2879", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value), mean(value), sum(value) / mean(value) as div FROM intoverlap GROUP BY region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"intoverlap","tags":{"region":"us-east"},"columns":["time","sum","mean","div"],"values":[["1970-01-01T00:00:00Z",50,25,2]]},{"name":"intoverlap","tags":{"region":"us-west"},"columns":["time","div"],"values":[["1970-01-01T00:00:00Z",100,100,1]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_FloatSingle(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`floatsingle value=45.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "stddev with just one point - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT STDDEV(value) FROM floatsingle`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatsingle","columns":["time","stddev"],"values":[["1970-01-01T00:00:00Z",null]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_FloatMany(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`floatmany,host=server01 value=2.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`floatmany,host=server02 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`floatmany,host=server03 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - fmt.Sprintf(`floatmany,host=server04 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:30Z").UnixNano()), - fmt.Sprintf(`floatmany,host=server05 value=5.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:40Z").UnixNano()), - fmt.Sprintf(`floatmany,host=server06 value=5.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:50Z").UnixNano()), - fmt.Sprintf(`floatmany,host=server07 value=7.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:00Z").UnixNano()), - fmt.Sprintf(`floatmany,host=server08 value=9.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:10Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "mean and stddev - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MEAN(value), STDDEV(value) FROM floatmany WHERE time >= '2000-01-01' AND time < '2000-01-01T00:02:00Z' GROUP BY time(10m)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","mean","stddev"],"values":[["2000-01-01T00:00:00Z",5,2.138089935299395]]}]}]}`, - }, - &Query{ - name: "first - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT FIRST(value) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","first"],"values":[["2000-01-01T00:00:00Z",2]]}]}]}`, - }, - &Query{ - name: "last - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT LAST(value) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","last"],"values":[["2000-01-01T00:01:10Z",9]]}]}]}`, - }, - &Query{ - name: "spread - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SPREAD(value) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","spread"],"values":[["1970-01-01T00:00:00Z",7]]}]}]}`, - }, - &Query{ - name: "median - even count - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MEDIAN(value) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","median"],"values":[["1970-01-01T00:00:00Z",4.5]]}]}]}`, - }, - &Query{ - name: "median - odd count - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MEDIAN(value) FROM floatmany where time < '2000-01-01T00:01:10Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","median"],"values":[["1970-01-01T00:00:00Z",4]]}]}]}`, - }, - &Query{ - name: "mode - single - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MODE(value) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","mode"],"values":[["1970-01-01T00:00:00Z",4]]}]}]}`, - }, - &Query{ - name: "mode - multiple - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MODE(value) FROM floatmany where time < '2000-01-01T00:00:10Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","mode"],"values":[["1970-01-01T00:00:00Z",2]]}]}]}`, - }, - &Query{ - name: "distinct as call - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT DISTINCT(value) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","distinct"],"values":[["1970-01-01T00:00:00Z",2],["1970-01-01T00:00:00Z",4],["1970-01-01T00:00:00Z",5],["1970-01-01T00:00:00Z",7],["1970-01-01T00:00:00Z",9]]}]}]}`, - }, - &Query{ - name: "distinct alt syntax - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT DISTINCT value FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","distinct"],"values":[["1970-01-01T00:00:00Z",2],["1970-01-01T00:00:00Z",4],["1970-01-01T00:00:00Z",5],["1970-01-01T00:00:00Z",7],["1970-01-01T00:00:00Z",9]]}]}]}`, - }, - &Query{ - name: "distinct select tag - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT DISTINCT(host) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"error":"statement must have at least one field in select clause"}]}`, - skip: true, // FIXME(benbjohnson): show be allowed, stream tag values - }, - &Query{ - name: "distinct alt select tag - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT DISTINCT host FROM floatmany`, - exp: `{"results":[{"statement_id":0,"error":"statement must have at least one field in select clause"}]}`, - skip: true, // FIXME(benbjohnson): show be allowed, stream tag values - }, - &Query{ - name: "count distinct - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT COUNT(DISTINCT value) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",5]]}]}]}`, - }, - &Query{ - name: "count distinct as call - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT COUNT(DISTINCT(value)) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",5]]}]}]}`, - }, - &Query{ - name: "count distinct select tag - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT COUNT(DISTINCT host) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",0]]}]}]}`, - skip: true, // FIXME(benbjohnson): stream tag values - }, - &Query{ - name: "count distinct as call select tag - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT COUNT(DISTINCT host) FROM floatmany`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatmany","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",0]]}]}]}`, - skip: true, // FIXME(benbjohnson): stream tag values - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_FloatOverlap(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`floatoverlap,region=us-east value=20.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`floatoverlap,region=us-east value=30.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`floatoverlap,region=us-west value=100.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`floatoverlap,region=us-east otherVal=20.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:03Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "aggregation with no interval - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT count(value) FROM floatoverlap WHERE time = '2000-01-01 00:00:00'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatoverlap","columns":["time","count"],"values":[["2000-01-01T00:00:00Z",2]]}]}]}`, - }, - &Query{ - name: "sum - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM floatoverlap WHERE time >= '2000-01-01 00:00:05' AND time <= '2000-01-01T00:00:10Z' GROUP BY time(10s), region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatoverlap","tags":{"region":"us-east"},"columns":["time","sum"],"values":[["2000-01-01T00:00:00Z",null],["2000-01-01T00:00:10Z",30]]}]}]}`, - }, - &Query{ - name: "aggregation with a null field value - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM floatoverlap GROUP BY region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatoverlap","tags":{"region":"us-east"},"columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",50]]},{"name":"floatoverlap","tags":{"region":"us-west"},"columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",100]]}]}]}`, - }, - &Query{ - name: "multiple aggregations - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value), MEAN(value) FROM floatoverlap GROUP BY region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatoverlap","tags":{"region":"us-east"},"columns":["time","sum","mean"],"values":[["1970-01-01T00:00:00Z",50,25]]},{"name":"floatoverlap","tags":{"region":"us-west"},"columns":["time","sum","mean"],"values":[["1970-01-01T00:00:00Z",100,100]]}]}]}`, - }, - &Query{ - name: "multiple aggregations with division - float", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value) / mean(value) as div FROM floatoverlap GROUP BY region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"floatoverlap","tags":{"region":"us-east"},"columns":["time","div"],"values":[["1970-01-01T00:00:00Z",2]]},{"name":"floatoverlap","tags":{"region":"us-west"},"columns":["time","div"],"values":[["1970-01-01T00:00:00Z",1]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_GroupByOffset(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`offset,region=us-east,host=serverA value=20.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`offset,region=us-east,host=serverB value=30.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`offset,region=us-west,host=serverC value=100.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "group by offset - standard", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value) FROM "offset" WHERE time >= '1999-12-31T23:59:55Z' AND time < '2000-01-01T00:00:15Z' GROUP BY time(10s, 5s) FILL(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"offset","columns":["time","sum"],"values":[["1999-12-31T23:59:55Z",120],["2000-01-01T00:00:05Z",30]]}]}]}`, - }, - &Query{ - name: "group by offset - misaligned time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value) FROM "offset" WHERE time >= '2000-01-01T00:00:00Z' AND time < '2000-01-01T00:00:20Z' GROUP BY time(10s, 5s) FILL(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"offset","columns":["time","sum"],"values":[["1999-12-31T23:59:55Z",120],["2000-01-01T00:00:05Z",30],["2000-01-01T00:00:15Z",0]]}]}]}`, - }, - &Query{ - name: "group by offset - negative time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value) FROM "offset" WHERE time >= '1999-12-31T23:59:55Z' AND time < '2000-01-01T00:00:15Z' GROUP BY time(10s, -5s) FILL(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"offset","columns":["time","sum"],"values":[["1999-12-31T23:59:55Z",120],["2000-01-01T00:00:05Z",30]]}]}]}`, - }, - &Query{ - name: "group by offset - modulo", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value) FROM "offset" WHERE time >= '1999-12-31T23:59:55Z' AND time < '2000-01-01T00:00:15Z' GROUP BY time(10s, 35s) FILL(0)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"offset","columns":["time","sum"],"values":[["1999-12-31T23:59:55Z",120],["2000-01-01T00:00:05Z",30]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_Load(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`load,region=us-east,host=serverA value=20.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`load,region=us-east,host=serverB value=30.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`load,region=us-west,host=serverC value=100.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "group by multiple dimensions", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value) FROM load GROUP BY region, host`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"load","tags":{"host":"serverA","region":"us-east"},"columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",20]]},{"name":"load","tags":{"host":"serverB","region":"us-east"},"columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",30]]},{"name":"load","tags":{"host":"serverC","region":"us-west"},"columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",100]]}]}]}`, - }, - &Query{ - name: "group by multiple dimensions", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value)*2 FROM load`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"load","columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",300]]}]}]}`, - }, - &Query{ - name: "group by multiple dimensions", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value)/2 FROM load`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"load","columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",75]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_CPU(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`cpu,region=uk,host=serverZ,service=redis value=20.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:03Z").UnixNano()), - fmt.Sprintf(`cpu,region=uk,host=serverZ,service=mysql value=30.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:03Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "aggregation with WHERE and AND", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(value) FROM cpu WHERE region='uk' AND host='serverZ'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",50]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Aggregates_String(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join([]string{ - fmt.Sprintf(`stringdata value="first" %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:03Z").UnixNano()), - fmt.Sprintf(`stringdata value="last" %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:04Z").UnixNano()), - }, "\n")}, - } - - test.addQueries([]*Query{ - // strings - &Query{ - name: "STDDEV on string data - string", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT STDDEV(value) FROM stringdata`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"stringdata","columns":["time","stddev"],"values":[["1970-01-01T00:00:00Z",null]]}]}]}`, - skip: true, // FIXME(benbjohnson): allow non-float var ref expr in cursor iterator - }, - &Query{ - name: "MEAN on string data - string", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MEAN(value) FROM stringdata`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"stringdata","columns":["time","mean"],"values":[["1970-01-01T00:00:00Z",0]]}]}]}`, - skip: true, // FIXME(benbjohnson): allow non-float var ref expr in cursor iterator - }, - &Query{ - name: "MEDIAN on string data - string", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT MEDIAN(value) FROM stringdata`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"stringdata","columns":["time","median"],"values":[["1970-01-01T00:00:00Z",null]]}]}]}`, - skip: true, // FIXME(benbjohnson): allow non-float var ref expr in cursor iterator - }, - &Query{ - name: "COUNT on string data - string", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT COUNT(value) FROM stringdata`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"stringdata","columns":["time","count"],"values":[["1970-01-01T00:00:00Z",2]]}]}]}`, - skip: true, // FIXME(benbjohnson): allow non-float var ref expr in cursor iterator - }, - &Query{ - name: "FIRST on string data - string", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT FIRST(value) FROM stringdata`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"stringdata","columns":["time","first"],"values":[["2000-01-01T00:00:03Z","first"]]}]}]}`, - skip: true, // FIXME(benbjohnson): allow non-float var ref expr in cursor iterator - }, - &Query{ - name: "LAST on string data - string", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT LAST(value) FROM stringdata`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"stringdata","columns":["time","last"],"values":[["2000-01-01T00:00:04Z","last"]]}]}]}`, - skip: true, // FIXME(benbjohnson): allow non-float var ref expr in cursor iterator - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_AggregateSelectors(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`network,host=server01,region=west,core=1 rx=10i,tx=20i,core=2i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`network,host=server02,region=west,core=2 rx=40i,tx=50i,core=3i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`network,host=server03,region=east,core=3 rx=40i,tx=55i,core=4i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - fmt.Sprintf(`network,host=server04,region=east,core=4 rx=40i,tx=60i,core=1i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:30Z").UnixNano()), - fmt.Sprintf(`network,host=server05,region=west,core=1 rx=50i,tx=70i,core=2i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:40Z").UnixNano()), - fmt.Sprintf(`network,host=server06,region=east,core=2 rx=50i,tx=40i,core=3i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:50Z").UnixNano()), - fmt.Sprintf(`network,host=server07,region=west,core=3 rx=70i,tx=30i,core=4i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:00Z").UnixNano()), - fmt.Sprintf(`network,host=server08,region=east,core=4 rx=90i,tx=10i,core=1i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:10Z").UnixNano()), - fmt.Sprintf(`network,host=server09,region=east,core=1 rx=5i,tx=4i,core=2i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:20Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "baseline", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM network`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","core","core_1","host","region","rx","tx"],"values":[["2000-01-01T00:00:00Z",2,"1","server01","west",10,20],["2000-01-01T00:00:10Z",3,"2","server02","west",40,50],["2000-01-01T00:00:20Z",4,"3","server03","east",40,55],["2000-01-01T00:00:30Z",1,"4","server04","east",40,60],["2000-01-01T00:00:40Z",2,"1","server05","west",50,70],["2000-01-01T00:00:50Z",3,"2","server06","east",50,40],["2000-01-01T00:01:00Z",4,"3","server07","west",70,30],["2000-01-01T00:01:10Z",1,"4","server08","east",90,10],["2000-01-01T00:01:20Z",2,"1","server09","east",5,4]]}]}]}`, - }, - &Query{ - name: "max - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT max(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","max"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",90]]}]}]}`, - }, - &Query{ - name: "max - baseline 30s - epoch ms", - params: url.Values{"db": []string{"db0"}, "epoch": []string{"ms"}}, - command: `SELECT max(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: fmt.Sprintf( - `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","max"],"values":[[%d,40],[%d,50],[%d,90]]}]}]}`, - mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()/int64(time.Millisecond), - mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:30Z").UnixNano()/int64(time.Millisecond), - mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:00Z").UnixNano()/int64(time.Millisecond), - ), - }, - &Query{ - name: "max - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, max(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","tx","max"],"values":[["2000-01-01T00:00:00Z",50,40],["2000-01-01T00:00:30Z",70,50],["2000-01-01T00:01:00Z",10,90]]}]}]}`, - }, - &Query{ - name: "max - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, max(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","max"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",90]]}]}]}`, - }, - &Query{ - name: "max - time and tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, tx, max(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","tx","max"],"values":[["2000-01-01T00:00:00Z",50,40],["2000-01-01T00:00:30Z",70,50],["2000-01-01T00:01:00Z",10,90]]}]}]}`, - }, - &Query{ - name: "min - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT min(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","min"],"values":[["2000-01-01T00:00:00Z",10],["2000-01-01T00:00:30Z",40],["2000-01-01T00:01:00Z",5]]}]}]}`, - }, - &Query{ - name: "min - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, min(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","tx","min"],"values":[["2000-01-01T00:00:00Z",20,10],["2000-01-01T00:00:30Z",60,40],["2000-01-01T00:01:00Z",4,5]]}]}]}`, - }, - &Query{ - name: "min - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, min(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","min"],"values":[["2000-01-01T00:00:00Z",10],["2000-01-01T00:00:30Z",40],["2000-01-01T00:01:00Z",5]]}]}]}`, - }, - &Query{ - name: "min - time and tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, tx, min(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","tx","min"],"values":[["2000-01-01T00:00:00Z",20,10],["2000-01-01T00:00:30Z",60,40],["2000-01-01T00:01:00Z",4,5]]}]}]}`, - }, - &Query{ - name: "max,min - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT max(rx), min(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","max","min"],"values":[["2000-01-01T00:00:00Z",40,10],["2000-01-01T00:00:30Z",50,40],["2000-01-01T00:01:00Z",90,5]]}]}]}`, - }, - &Query{ - name: "first - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT first(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","first"],"values":[["2000-01-01T00:00:00Z",10],["2000-01-01T00:00:30Z",40],["2000-01-01T00:01:00Z",70]]}]}]}`, - }, - &Query{ - name: "first - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, tx, first(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","tx","first"],"values":[["2000-01-01T00:00:00Z",20,10],["2000-01-01T00:00:30Z",60,40],["2000-01-01T00:01:00Z",30,70]]}]}]}`, - }, - &Query{ - name: "first - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, first(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","first"],"values":[["2000-01-01T00:00:00Z",10],["2000-01-01T00:00:30Z",40],["2000-01-01T00:01:00Z",70]]}]}]}`, - }, - &Query{ - name: "first - time and tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, tx, first(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","tx","first"],"values":[["2000-01-01T00:00:00Z",20,10],["2000-01-01T00:00:30Z",60,40],["2000-01-01T00:01:00Z",30,70]]}]}]}`, - }, - &Query{ - name: "last - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT last(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","last"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",5]]}]}]}`, - }, - &Query{ - name: "last - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, last(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","tx","last"],"values":[["2000-01-01T00:00:00Z",55,40],["2000-01-01T00:00:30Z",40,50],["2000-01-01T00:01:00Z",4,5]]}]}]}`, - }, - &Query{ - name: "last - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, last(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","last"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",5]]}]}]}`, - }, - &Query{ - name: "last - time and tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, tx, last(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","tx","last"],"values":[["2000-01-01T00:00:00Z",55,40],["2000-01-01T00:00:30Z",40,50],["2000-01-01T00:01:00Z",4,5]]}]}]}`, - }, - &Query{ - name: "count - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT count(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","count"],"values":[["2000-01-01T00:00:00Z",3],["2000-01-01T00:00:30Z",3],["2000-01-01T00:01:00Z",3]]}]}]}`, - }, - &Query{ - name: "count - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, count(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "count - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, count(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "distinct - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT distinct(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","distinct"],"values":[["2000-01-01T00:00:00Z",10],["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",70],["2000-01-01T00:01:00Z",90],["2000-01-01T00:01:00Z",5]]}]}]}`, - }, - &Query{ - name: "distinct - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, distinct(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: aggregate function distinct() can not be combined with other functions or fields"}`, - }, - &Query{ - name: "distinct - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, distinct(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: aggregate function distinct() can not be combined with other functions or fields"}`, - }, - &Query{ - name: "mean - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT mean(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","mean"],"values":[["2000-01-01T00:00:00Z",30],["2000-01-01T00:00:30Z",46.666666666666664],["2000-01-01T00:01:00Z",55]]}]}]}`, - }, - &Query{ - name: "mean - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, mean(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "mean - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, mean(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "median - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT median(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","median"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",70]]}]}]}`, - }, - &Query{ - name: "median - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, median(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "median - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, median(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "mode - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT mode(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","mode"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",5]]}]}]}`, - }, - &Query{ - name: "mode - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, mode(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "mode - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, mode(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "mode - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT mode(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","mode"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",5]]}]}]}`, - }, - &Query{ - name: "mode - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, mode(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "mode - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, mode(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "spread - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT spread(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","spread"],"values":[["2000-01-01T00:00:00Z",30],["2000-01-01T00:00:30Z",10],["2000-01-01T00:01:00Z",85]]}]}]}`, - }, - &Query{ - name: "spread - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, spread(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "spread - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, spread(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "stddev - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT stddev(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","stddev"],"values":[["2000-01-01T00:00:00Z",17.320508075688775],["2000-01-01T00:00:30Z",5.773502691896258],["2000-01-01T00:01:00Z",44.44097208657794]]}]}]}`, - }, - &Query{ - name: "stddev - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, stddev(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "stddev - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, stddev(rx) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"error":"error parsing query: mixing aggregate and non-aggregate queries is not supported"}`, - }, - &Query{ - name: "percentile - baseline 30s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT percentile(rx, 75) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","percentile"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",70]]}]}]}`, - }, - &Query{ - name: "percentile - time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT time, percentile(rx, 75) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","percentile"],"values":[["2000-01-01T00:00:00Z",40],["2000-01-01T00:00:30Z",50],["2000-01-01T00:01:00Z",70]]}]}]}`, - }, - &Query{ - name: "percentile - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT tx, percentile(rx, 75) FROM network where time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T00:01:29Z' group by time(30s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","tx","percentile"],"values":[["2000-01-01T00:00:00Z",50,40],["2000-01-01T00:00:30Z",70,50],["2000-01-01T00:01:00Z",30,70]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Selectors(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`network,host=server01,region=west,core=1 rx=10i,tx=20i,core=2i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`network,host=server02,region=west,core=2 rx=40i,tx=50i,core=3i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`network,host=server03,region=east,core=3 rx=40i,tx=55i,core=4i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - fmt.Sprintf(`network,host=server04,region=east,core=4 rx=40i,tx=60i,core=1i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:30Z").UnixNano()), - fmt.Sprintf(`network,host=server05,region=west,core=1 rx=50i,tx=70i,core=2i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:40Z").UnixNano()), - fmt.Sprintf(`network,host=server06,region=east,core=2 rx=50i,tx=40i,core=3i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:50Z").UnixNano()), - fmt.Sprintf(`network,host=server07,region=west,core=3 rx=70i,tx=30i,core=4i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:00Z").UnixNano()), - fmt.Sprintf(`network,host=server08,region=east,core=4 rx=90i,tx=10i,core=1i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:10Z").UnixNano()), - fmt.Sprintf(`network,host=server09,region=east,core=1 rx=5i,tx=4i,core=2i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:01:20Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "max - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT max(tx) FROM network`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","max"],"values":[["2000-01-01T00:00:40Z",70]]}]}]}`, - }, - &Query{ - name: "min - tx", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT min(tx) FROM network`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","min"],"values":[["2000-01-01T00:01:20Z",4]]}]}]}`, - }, - &Query{ - name: "first", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT first(tx) FROM network`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","first"],"values":[["2000-01-01T00:00:00Z",20]]}]}]}`, - }, - &Query{ - name: "last", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT last(tx) FROM network`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","last"],"values":[["2000-01-01T00:01:20Z",4]]}]}]}`, - }, - &Query{ - name: "percentile", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT percentile(tx, 50) FROM network`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"network","columns":["time","percentile"],"values":[["2000-01-01T00:00:50Z",40]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_TopInt(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - // cpu data with overlapping duplicate values - // hour 0 - fmt.Sprintf(`cpu,host=server01 value=2.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server02 value=3.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`cpu,host=server03 value=4.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - // hour 1 - fmt.Sprintf(`cpu,host=server04 value=5.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T01:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server05 value=7.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T01:00:10Z").UnixNano()), - fmt.Sprintf(`cpu,host=server06 value=6.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T01:00:20Z").UnixNano()), - // hour 2 - fmt.Sprintf(`cpu,host=server07 value=7.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T02:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server08 value=9.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T02:00:10Z").UnixNano()), - - // memory data - // hour 0 - fmt.Sprintf(`memory,host=a,service=redis value=1000i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`memory,host=b,service=mysql value=2000i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`memory,host=b,service=redis value=1500i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - // hour 1 - fmt.Sprintf(`memory,host=a,service=redis value=1001i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T01:00:00Z").UnixNano()), - fmt.Sprintf(`memory,host=b,service=mysql value=2001i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T01:00:00Z").UnixNano()), - fmt.Sprintf(`memory,host=b,service=redis value=1501i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T01:00:00Z").UnixNano()), - // hour 2 - fmt.Sprintf(`memory,host=a,service=redis value=1002i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T02:00:00Z").UnixNano()), - fmt.Sprintf(`memory,host=b,service=mysql value=2002i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T02:00:00Z").UnixNano()), - fmt.Sprintf(`memory,host=b,service=redis value=1502i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T02:00:00Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "top - cpu", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, 1) FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","top"],"values":[["2000-01-01T02:00:10Z",9]]}]}]}`, - }, - &Query{ - name: "top - cpu - 2 values", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, 2) FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","top"],"values":[["2000-01-01T01:00:10Z",7],["2000-01-01T02:00:10Z",9]]}]}]}`, - }, - &Query{ - name: "top - cpu - 3 values - sorts on tie properly", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, 3) FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","top"],"values":[["2000-01-01T01:00:10Z",7],["2000-01-01T02:00:00Z",7],["2000-01-01T02:00:10Z",9]]}]}]}`, - }, - &Query{ - name: "top - cpu - with tag", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, host, 2) FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","top","host"],"values":[["2000-01-01T01:00:10Z",7,"server05"],["2000-01-01T02:00:10Z",9,"server08"]]}]}]}`, - }, - &Query{ - name: "top - cpu - 3 values with limit 2", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, 3) FROM cpu limit 2`, - exp: `{"error":"error parsing query: limit (3) in top function can not be larger than the LIMIT (2) in the select statement"}`, - }, - &Query{ - name: "top - cpu - hourly", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, 1) FROM cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T02:00:10Z' group by time(1h)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","top"],"values":[["2000-01-01T00:00:00Z",4],["2000-01-01T01:00:00Z",7],["2000-01-01T02:00:00Z",9]]}]}]}`, - }, - &Query{ - name: "top - cpu - 2 values hourly", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, 2) FROM cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T02:00:10Z' group by time(1h)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","top"],"values":[["2000-01-01T00:00:00Z",4],["2000-01-01T00:00:00Z",3],["2000-01-01T01:00:00Z",7],["2000-01-01T01:00:00Z",6],["2000-01-01T02:00:00Z",9],["2000-01-01T02:00:00Z",7]]}]}]}`, - }, - &Query{ - name: "top - cpu - 3 values hourly - validates that a bucket can have less than limit if no values exist in that time bucket", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, 3) FROM cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T02:00:10Z' group by time(1h)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","top"],"values":[["2000-01-01T00:00:00Z",4],["2000-01-01T00:00:00Z",3],["2000-01-01T00:00:00Z",2],["2000-01-01T01:00:00Z",7],["2000-01-01T01:00:00Z",6],["2000-01-01T01:00:00Z",5],["2000-01-01T02:00:00Z",9],["2000-01-01T02:00:00Z",7]]}]}]}`, - }, - &Query{ - name: "top - memory - 2 values, two tags", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, 2), host, service FROM memory`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","columns":["time","top","host","service"],"values":[["2000-01-01T01:00:00Z",2001,"b","mysql"],["2000-01-01T02:00:00Z",2002,"b","mysql"]]}]}]}`, - }, - &Query{ - name: "top - memory - host tag with limit 2", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, host, 2) FROM memory`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","columns":["time","top","host"],"values":[["2000-01-01T02:00:00Z",2002,"b"],["2000-01-01T02:00:00Z",1002,"a"]]}]}]}`, - }, - &Query{ - name: "top - memory - host tag with limit 2, service tag in select", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, host, 2), service FROM memory`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","columns":["time","top","host","service"],"values":[["2000-01-01T02:00:00Z",2002,"b","mysql"],["2000-01-01T02:00:00Z",1002,"a","redis"]]}]}]}`, - }, - &Query{ - name: "top - memory - service tag with limit 2, host tag in select", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, service, 2), host FROM memory`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","columns":["time","top","service","host"],"values":[["2000-01-01T02:00:00Z",2002,"mysql","b"],["2000-01-01T02:00:00Z",1502,"redis","b"]]}]}]}`, - }, - &Query{ - name: "top - memory - host and service tag with limit 2", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, host, service, 2) FROM memory`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","columns":["time","top","host","service"],"values":[["2000-01-01T02:00:00Z",2002,"b","mysql"],["2000-01-01T02:00:00Z",1502,"b","redis"]]}]}]}`, - }, - &Query{ - name: "top - memory - host tag with limit 2 with service tag in select", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, host, 2), service FROM memory`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","columns":["time","top","host","service"],"values":[["2000-01-01T02:00:00Z",2002,"b","mysql"],["2000-01-01T02:00:00Z",1002,"a","redis"]]}]}]}`, - }, - &Query{ - name: "top - memory - host and service tag with limit 3", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT TOP(value, host, service, 3) FROM memory`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","columns":["time","top","host","service"],"values":[["2000-01-01T02:00:00Z",2002,"b","mysql"],["2000-01-01T02:00:00Z",1502,"b","redis"],["2000-01-01T02:00:00Z",1002,"a","redis"]]}]}]}`, - }, - - // TODO - // - Test that specifiying fields or tags in the function will rewrite the query to expand them to the fields - // - Test that a field can be used in the top function - // - Test that asking for a field will come back before a tag if they have the same name for a tag and a field - // - Test that `select top(value, host, 2)` when there is only one value for `host` it will only bring back one value - // - Test that `select top(value, host, 4) from foo where time > now() - 1d and time < now() group by time(1h)` and host is unique in some time buckets that it returns only the unique ones, and not always 4 values - - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP: %s", query.name) - continue - } - - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Test various aggregates when different series only have data for the same timestamp. -func TestServer_Query_Aggregates_IdenticalTime(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`series,host=a value=1 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`series,host=b value=2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`series,host=c value=3 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`series,host=d value=4 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`series,host=e value=5 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`series,host=f value=5 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`series,host=g value=5 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`series,host=h value=5 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`series,host=i value=5 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "last from multiple series with identical timestamp", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT last(value) FROM "series"`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"series","columns":["time","last"],"values":[["2000-01-01T00:00:00Z",5]]}]}]}`, - repeat: 100, - }, - &Query{ - name: "first from multiple series with identical timestamp", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT first(value) FROM "series"`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"series","columns":["time","first"],"values":[["2000-01-01T00:00:00Z",5]]}]}]}`, - repeat: 100, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - for n := 0; n <= query.repeat; n++ { - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } - } -} - -// This will test that when using a group by, that it observes the time you asked for -// but will only put the values in the bucket that match the time range -func TestServer_Query_GroupByTimeCutoffs(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu value=1i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`cpu value=2i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:01Z").UnixNano()), - fmt.Sprintf(`cpu value=3i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:05Z").UnixNano()), - fmt.Sprintf(`cpu value=4i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:08Z").UnixNano()), - fmt.Sprintf(`cpu value=5i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:09Z").UnixNano()), - fmt.Sprintf(`cpu value=6i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - } - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "sum all time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum"],"values":[["1970-01-01T00:00:00Z",21]]}]}]}`, - }, - &Query{ - name: "sum all time grouped by time 5s", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T00:00:10Z' group by time(5s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum"],"values":[["2000-01-01T00:00:00Z",3],["2000-01-01T00:00:05Z",12],["2000-01-01T00:00:10Z",6]]}]}]}`, - }, - &Query{ - name: "sum all time grouped by time 5s missing first point", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM cpu where time >= '2000-01-01T00:00:01Z' and time <= '2000-01-01T00:00:10Z' group by time(5s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum"],"values":[["2000-01-01T00:00:00Z",2],["2000-01-01T00:00:05Z",12],["2000-01-01T00:00:10Z",6]]}]}]}`, - }, - &Query{ - name: "sum all time grouped by time 5s missing first points (null for bucket)", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM cpu where time >= '2000-01-01T00:00:02Z' and time <= '2000-01-01T00:00:10Z' group by time(5s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum"],"values":[["2000-01-01T00:00:00Z",null],["2000-01-01T00:00:05Z",12],["2000-01-01T00:00:10Z",6]]}]}]}`, - }, - &Query{ - name: "sum all time grouped by time 5s missing last point - 2 time intervals", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T00:00:09Z' group by time(5s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum"],"values":[["2000-01-01T00:00:00Z",3],["2000-01-01T00:00:05Z",12]]}]}]}`, - }, - &Query{ - name: "sum all time grouped by time 5s missing last 2 points - 2 time intervals", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT SUM(value) FROM cpu where time >= '2000-01-01T00:00:00Z' and time <= '2000-01-01T00:00:08Z' group by time(5s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sum"],"values":[["2000-01-01T00:00:00Z",3],["2000-01-01T00:00:05Z",7]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Write_Precision(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []struct { - write string - params url.Values - }{ - { - write: fmt.Sprintf("cpu_n0_precision value=1 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z").UnixNano()), - }, - { - write: fmt.Sprintf("cpu_n1_precision value=1.1 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z").UnixNano()), - params: url.Values{"precision": []string{"n"}}, - }, - { - write: fmt.Sprintf("cpu_u_precision value=100 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z").Truncate(time.Microsecond).UnixNano()/int64(time.Microsecond)), - params: url.Values{"precision": []string{"u"}}, - }, - { - write: fmt.Sprintf("cpu_ms_precision value=200 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z").Truncate(time.Millisecond).UnixNano()/int64(time.Millisecond)), - params: url.Values{"precision": []string{"ms"}}, - }, - { - write: fmt.Sprintf("cpu_s_precision value=300 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z").Truncate(time.Second).UnixNano()/int64(time.Second)), - params: url.Values{"precision": []string{"s"}}, - }, - { - write: fmt.Sprintf("cpu_m_precision value=400 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z").Truncate(time.Minute).UnixNano()/int64(time.Minute)), - params: url.Values{"precision": []string{"m"}}, - }, - { - write: fmt.Sprintf("cpu_h_precision value=500 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z").Truncate(time.Hour).UnixNano()/int64(time.Hour)), - params: url.Values{"precision": []string{"h"}}, - }, - } - - test := NewTest("db0", "rp0") - - test.addQueries([]*Query{ - &Query{ - name: "point with nanosecond precision time - no precision specified on write", - command: `SELECT * FROM cpu_n0_precision`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu_n0_precision","columns":["time","value"],"values":[["2000-01-01T12:34:56.789012345Z",1]]}]}]}`, - }, - &Query{ - name: "point with nanosecond precision time", - command: `SELECT * FROM cpu_n1_precision`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu_n1_precision","columns":["time","value"],"values":[["2000-01-01T12:34:56.789012345Z",1.1]]}]}]}`, - }, - &Query{ - name: "point with microsecond precision time", - command: `SELECT * FROM cpu_u_precision`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu_u_precision","columns":["time","value"],"values":[["2000-01-01T12:34:56.789012Z",100]]}]}]}`, - }, - &Query{ - name: "point with millisecond precision time", - command: `SELECT * FROM cpu_ms_precision`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu_ms_precision","columns":["time","value"],"values":[["2000-01-01T12:34:56.789Z",200]]}]}]}`, - }, - &Query{ - name: "point with second precision time", - command: `SELECT * FROM cpu_s_precision`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu_s_precision","columns":["time","value"],"values":[["2000-01-01T12:34:56Z",300]]}]}]}`, - }, - &Query{ - name: "point with minute precision time", - command: `SELECT * FROM cpu_m_precision`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu_m_precision","columns":["time","value"],"values":[["2000-01-01T12:34:00Z",400]]}]}]}`, - }, - &Query{ - name: "point with hour precision time", - command: `SELECT * FROM cpu_h_precision`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu_h_precision","columns":["time","value"],"values":[["2000-01-01T12:00:00Z",500]]}]}]}`, - }, - }...) - - // we are doing writes that require parameter changes, so we are fighting the test harness a little to make this happen properly - for _, w := range writes { - test.writes = Writes{ - &Write{data: w.write}, - } - test.params = w.params - test.initialized = false - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Wildcards(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`wildcard,region=us-east value=10 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`wildcard,region=us-east valx=20 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`wildcard,region=us-east value=30,valx=40 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - - fmt.Sprintf(`wgroup,region=us-east value=10.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`wgroup,region=us-east value=20.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`wgroup,region=us-west value=30.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - - fmt.Sprintf(`m1,region=us-east value=10.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`m2,host=server01 field=20.0 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:01Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "wildcard", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM wildcard`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","columns":["time","region","value","valx"],"values":[["2000-01-01T00:00:00Z","us-east",10,null],["2000-01-01T00:00:10Z","us-east",null,20],["2000-01-01T00:00:20Z","us-east",30,40]]}]}]}`, - }, - &Query{ - name: "wildcard with group by", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM wildcard GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","tags":{"region":"us-east"},"columns":["time","value","valx"],"values":[["2000-01-01T00:00:00Z",10,null],["2000-01-01T00:00:10Z",null,20],["2000-01-01T00:00:20Z",30,40]]}]}]}`, - }, - &Query{ - name: "GROUP BY queries", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT mean(value) FROM wgroup GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wgroup","tags":{"region":"us-east"},"columns":["time","mean"],"values":[["1970-01-01T00:00:00Z",15]]},{"name":"wgroup","tags":{"region":"us-west"},"columns":["time","mean"],"values":[["1970-01-01T00:00:00Z",30]]}]}]}`, - }, - &Query{ - name: "GROUP BY queries with time", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT mean(value) FROM wgroup WHERE time >= '2000-01-01T00:00:00Z' AND time < '2000-01-01T00:01:00Z' GROUP BY *,TIME(1m)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wgroup","tags":{"region":"us-east"},"columns":["time","mean"],"values":[["2000-01-01T00:00:00Z",15]]},{"name":"wgroup","tags":{"region":"us-west"},"columns":["time","mean"],"values":[["2000-01-01T00:00:00Z",30]]}]}]}`, - }, - &Query{ - name: "wildcard and field in select", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT value, * FROM wildcard`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","columns":["time","value","region","value_1","valx"],"values":[["2000-01-01T00:00:00Z",10,"us-east",10,null],["2000-01-01T00:00:10Z",null,"us-east",null,20],["2000-01-01T00:00:20Z",30,"us-east",30,40]]}]}]}`, - }, - &Query{ - name: "field and wildcard in select", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT value, * FROM wildcard`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","columns":["time","value","region","value_1","valx"],"values":[["2000-01-01T00:00:00Z",10,"us-east",10,null],["2000-01-01T00:00:10Z",null,"us-east",null,20],["2000-01-01T00:00:20Z",30,"us-east",30,40]]}]}]}`, - }, - &Query{ - name: "field and wildcard in group by", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM wildcard GROUP BY region, *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","tags":{"region":"us-east"},"columns":["time","value","valx"],"values":[["2000-01-01T00:00:00Z",10,null],["2000-01-01T00:00:10Z",null,20],["2000-01-01T00:00:20Z",30,40]]}]}]}`, - }, - &Query{ - name: "wildcard and field in group by", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM wildcard GROUP BY *, region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","tags":{"region":"us-east"},"columns":["time","value","valx"],"values":[["2000-01-01T00:00:00Z",10,null],["2000-01-01T00:00:10Z",null,20],["2000-01-01T00:00:20Z",30,40]]}]}]}`, - }, - &Query{ - name: "wildcard with multiple measurements", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM m1, m2`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"m1","columns":["time","field","host","region","value"],"values":[["2000-01-01T00:00:00Z",null,null,"us-east",10]]},{"name":"m2","columns":["time","field","host","region","value"],"values":[["2000-01-01T00:00:01Z",20,"server01",null,null]]}]}]}`, - }, - &Query{ - name: "wildcard with multiple measurements via regex", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM /^m.*/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"m1","columns":["time","field","host","region","value"],"values":[["2000-01-01T00:00:00Z",null,null,"us-east",10]]},{"name":"m2","columns":["time","field","host","region","value"],"values":[["2000-01-01T00:00:01Z",20,"server01",null,null]]}]}]}`, - }, - &Query{ - name: "wildcard with multiple measurements via regex and limit", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM db0../^m.*/ LIMIT 2`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"m1","columns":["time","field","host","region","value"],"values":[["2000-01-01T00:00:00Z",null,null,"us-east",10]]},{"name":"m2","columns":["time","field","host","region","value"],"values":[["2000-01-01T00:00:01Z",20,"server01",null,null]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_WildcardExpansion(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`wildcard,region=us-east,host=A value=10,cpu=80 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`wildcard,region=us-east,host=B value=20,cpu=90 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`wildcard,region=us-west,host=B value=30,cpu=70 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - fmt.Sprintf(`wildcard,region=us-east,host=A value=40,cpu=60 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:30Z").UnixNano()), - - fmt.Sprintf(`dupnames,region=us-east,day=1 value=10,day=3i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`dupnames,region=us-east,day=2 value=20,day=2i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`dupnames,region=us-west,day=3 value=30,day=1i %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "wildcard", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM wildcard`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","columns":["time","cpu","host","region","value"],"values":[["2000-01-01T00:00:00Z",80,"A","us-east",10],["2000-01-01T00:00:10Z",90,"B","us-east",20],["2000-01-01T00:00:20Z",70,"B","us-west",30],["2000-01-01T00:00:30Z",60,"A","us-east",40]]}]}]}`, - }, - &Query{ - name: "no wildcard in select", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT cpu, host, region, value FROM wildcard`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","columns":["time","cpu","host","region","value"],"values":[["2000-01-01T00:00:00Z",80,"A","us-east",10],["2000-01-01T00:00:10Z",90,"B","us-east",20],["2000-01-01T00:00:20Z",70,"B","us-west",30],["2000-01-01T00:00:30Z",60,"A","us-east",40]]}]}]}`, - }, - &Query{ - name: "no wildcard in select, preserve column order", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT host, cpu, region, value FROM wildcard`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","columns":["time","host","cpu","region","value"],"values":[["2000-01-01T00:00:00Z","A",80,"us-east",10],["2000-01-01T00:00:10Z","B",90,"us-east",20],["2000-01-01T00:00:20Z","B",70,"us-west",30],["2000-01-01T00:00:30Z","A",60,"us-east",40]]}]}]}`, - }, - - &Query{ - name: "no wildcard with alias", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT cpu as c, host as h, region, value FROM wildcard`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"wildcard","columns":["time","c","h","region","value"],"values":[["2000-01-01T00:00:00Z",80,"A","us-east",10],["2000-01-01T00:00:10Z",90,"B","us-east",20],["2000-01-01T00:00:20Z",70,"B","us-west",30],["2000-01-01T00:00:30Z",60,"A","us-east",40]]}]}]}`, - }, - &Query{ - name: "duplicate tag and field key", - command: `SELECT * FROM dupnames`, - params: url.Values{"db": []string{"db0"}}, - exp: `{"results":[{"statement_id":0,"series":[{"name":"dupnames","columns":["time","day","day_1","region","value"],"values":[["2000-01-01T00:00:00Z",3,"1","us-east",10],["2000-01-01T00:00:10Z",2,"2","us-east",20],["2000-01-01T00:00:20Z",1,"3","us-west",30]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_AcrossShardsAndFields(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu load=100 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`cpu load=200 %d`, mustParseTime(time.RFC3339Nano, "2010-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`cpu core=4 %d`, mustParseTime(time.RFC3339Nano, "2015-01-01T00:00:00Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "two results for cpu", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT load FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","load"],"values":[["2000-01-01T00:00:00Z",100],["2010-01-01T00:00:00Z",200]]}]}]}`, - }, - &Query{ - name: "two results for cpu, multi-select", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT core,load FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core","load"],"values":[["2000-01-01T00:00:00Z",null,100],["2010-01-01T00:00:00Z",null,200],["2015-01-01T00:00:00Z",4,null]]}]}]}`, - }, - &Query{ - name: "two results for cpu, wildcard select", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core","load"],"values":[["2000-01-01T00:00:00Z",null,100],["2010-01-01T00:00:00Z",null,200],["2015-01-01T00:00:00Z",4,null]]}]}]}`, - }, - &Query{ - name: "one result for core", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT core FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core"],"values":[["2015-01-01T00:00:00Z",4]]}]}]}`, - }, - &Query{ - name: "empty result set from non-existent field", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT foo FROM cpu`, - exp: `{"results":[{"statement_id":0}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Where_Fields(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu alert_id="alert",tenant_id="tenant",_cust="johnson brothers" %d`, mustParseTime(time.RFC3339Nano, "2015-02-28T01:03:36.703820946Z").UnixNano()), - fmt.Sprintf(`cpu alert_id="alert",tenant_id="tenant",_cust="johnson brothers" %d`, mustParseTime(time.RFC3339Nano, "2015-02-28T01:03:36.703820946Z").UnixNano()), - - fmt.Sprintf(`cpu load=100.0,core=4 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:02Z").UnixNano()), - fmt.Sprintf(`cpu load=80.0,core=2 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:01:02Z").UnixNano()), - - fmt.Sprintf(`clicks local=true %d`, mustParseTime(time.RFC3339Nano, "2014-11-10T23:00:01Z").UnixNano()), - fmt.Sprintf(`clicks local=false %d`, mustParseTime(time.RFC3339Nano, "2014-11-10T23:00:02Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - // non type specific - &Query{ - name: "missing measurement with group by", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT load from missing group by *`, - exp: `{"results":[{"statement_id":0}]}`, - }, - - // string - &Query{ - name: "single string field", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT alert_id FROM cpu WHERE alert_id='alert'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","alert_id"],"values":[["2015-02-28T01:03:36.703820946Z","alert"]]}]}]}`, - }, - &Query{ - name: "string AND query, all fields in SELECT", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT alert_id,tenant_id,_cust FROM cpu WHERE alert_id='alert' AND tenant_id='tenant'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","alert_id","tenant_id","_cust"],"values":[["2015-02-28T01:03:36.703820946Z","alert","tenant","johnson brothers"]]}]}]}`, - }, - &Query{ - name: "string AND query, all fields in SELECT, one in parenthesis", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT alert_id,tenant_id FROM cpu WHERE alert_id='alert' AND (tenant_id='tenant')`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","alert_id","tenant_id"],"values":[["2015-02-28T01:03:36.703820946Z","alert","tenant"]]}]}]}`, - }, - &Query{ - name: "string underscored field", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT alert_id FROM cpu WHERE _cust='johnson brothers'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","alert_id"],"values":[["2015-02-28T01:03:36.703820946Z","alert"]]}]}]}`, - }, - &Query{ - name: "string no match", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT alert_id FROM cpu WHERE _cust='acme'`, - exp: `{"results":[{"statement_id":0}]}`, - }, - - // float64 - &Query{ - name: "float64 GT no match", - params: url.Values{"db": []string{"db0"}}, - command: `select load from cpu where load > 100`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "float64 GTE match one", - params: url.Values{"db": []string{"db0"}}, - command: `select load from cpu where load >= 100`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","load"],"values":[["2009-11-10T23:00:02Z",100]]}]}]}`, - }, - &Query{ - name: "float64 EQ match upper bound", - params: url.Values{"db": []string{"db0"}}, - command: `select load from cpu where load = 100`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","load"],"values":[["2009-11-10T23:00:02Z",100]]}]}]}`, - }, - &Query{ - name: "float64 LTE match two", - params: url.Values{"db": []string{"db0"}}, - command: `select load from cpu where load <= 100`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","load"],"values":[["2009-11-10T23:00:02Z",100],["2009-11-10T23:01:02Z",80]]}]}]}`, - }, - &Query{ - name: "float64 GT match one", - params: url.Values{"db": []string{"db0"}}, - command: `select load from cpu where load > 99`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","load"],"values":[["2009-11-10T23:00:02Z",100]]}]}]}`, - }, - &Query{ - name: "float64 EQ no match", - params: url.Values{"db": []string{"db0"}}, - command: `select load from cpu where load = 99`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "float64 LT match one", - params: url.Values{"db": []string{"db0"}}, - command: `select load from cpu where load < 99`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","load"],"values":[["2009-11-10T23:01:02Z",80]]}]}]}`, - }, - &Query{ - name: "float64 LT no match", - params: url.Values{"db": []string{"db0"}}, - command: `select load from cpu where load < 80`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "float64 NE match one", - params: url.Values{"db": []string{"db0"}}, - command: `select load from cpu where load != 100`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","load"],"values":[["2009-11-10T23:01:02Z",80]]}]}]}`, - }, - - // int64 - &Query{ - name: "int64 GT no match", - params: url.Values{"db": []string{"db0"}}, - command: `select core from cpu where core > 4`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "int64 GTE match one", - params: url.Values{"db": []string{"db0"}}, - command: `select core from cpu where core >= 4`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core"],"values":[["2009-11-10T23:00:02Z",4]]}]}]}`, - }, - &Query{ - name: "int64 EQ match upper bound", - params: url.Values{"db": []string{"db0"}}, - command: `select core from cpu where core = 4`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core"],"values":[["2009-11-10T23:00:02Z",4]]}]}]}`, - }, - &Query{ - name: "int64 LTE match two ", - params: url.Values{"db": []string{"db0"}}, - command: `select core from cpu where core <= 4`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core"],"values":[["2009-11-10T23:00:02Z",4],["2009-11-10T23:01:02Z",2]]}]}]}`, - }, - &Query{ - name: "int64 GT match one", - params: url.Values{"db": []string{"db0"}}, - command: `select core from cpu where core > 3`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core"],"values":[["2009-11-10T23:00:02Z",4]]}]}]}`, - }, - &Query{ - name: "int64 EQ no match", - params: url.Values{"db": []string{"db0"}}, - command: `select core from cpu where core = 3`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "int64 LT match one", - params: url.Values{"db": []string{"db0"}}, - command: `select core from cpu where core < 3`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core"],"values":[["2009-11-10T23:01:02Z",2]]}]}]}`, - }, - &Query{ - name: "int64 LT no match", - params: url.Values{"db": []string{"db0"}}, - command: `select core from cpu where core < 2`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "int64 NE match one", - params: url.Values{"db": []string{"db0"}}, - command: `select core from cpu where core != 4`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","core"],"values":[["2009-11-10T23:01:02Z",2]]}]}]}`, - }, - - // bool - &Query{ - name: "bool EQ match true", - params: url.Values{"db": []string{"db0"}}, - command: `select local from clicks where local = true`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"clicks","columns":["time","local"],"values":[["2014-11-10T23:00:01Z",true]]}]}]}`, - }, - &Query{ - name: "bool EQ match false", - params: url.Values{"db": []string{"db0"}}, - command: `select local from clicks where local = false`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"clicks","columns":["time","local"],"values":[["2014-11-10T23:00:02Z",false]]}]}]}`, - }, - - &Query{ - name: "bool NE match one", - params: url.Values{"db": []string{"db0"}}, - command: `select local from clicks where local != true`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"clicks","columns":["time","local"],"values":[["2014-11-10T23:00:02Z",false]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Where_With_Tags(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`where_events,tennant=paul foo="bar" %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:02Z").UnixNano()), - fmt.Sprintf(`where_events,tennant=paul foo="baz" %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:03Z").UnixNano()), - fmt.Sprintf(`where_events,tennant=paul foo="bat" %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:04Z").UnixNano()), - fmt.Sprintf(`where_events,tennant=todd foo="bar" %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:05Z").UnixNano()), - fmt.Sprintf(`where_events,tennant=david foo="bap" %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:06Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "tag field and time", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from where_events where (tennant = 'paul' OR tennant = 'david') AND time > 1s AND (foo = 'bar' OR foo = 'baz' OR foo = 'bap')`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"where_events","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z","bar"],["2009-11-10T23:00:03Z","baz"],["2009-11-10T23:00:06Z","bap"]]}]}]}`, - }, - &Query{ - name: "tag or field", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from where_events where tennant = 'paul' OR foo = 'bar'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"where_events","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z","bar"],["2009-11-10T23:00:03Z","baz"],["2009-11-10T23:00:04Z","bat"],["2009-11-10T23:00:05Z","bar"]]}]}]}`, - }, - &Query{ - name: "non-existant tag and field", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from where_events where tenant != 'paul' AND foo = 'bar'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"where_events","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z","bar"],["2009-11-10T23:00:05Z","bar"]]}]}]}`, - }, - &Query{ - name: "non-existant tag or field", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from where_events where tenant != 'paul' OR foo = 'bar'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"where_events","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z","bar"],["2009-11-10T23:00:03Z","baz"],["2009-11-10T23:00:04Z","bat"],["2009-11-10T23:00:05Z","bar"],["2009-11-10T23:00:06Z","bap"]]}]}]}`, - }, - &Query{ - name: "where on tag that should be double quoted but isn't", - params: url.Values{"db": []string{"db0"}}, - command: `show series where data-center = 'foo'`, - exp: `{"results":[{"statement_id":0,"error":"invalid tag comparison operator"}]}`, - }, - &Query{ - name: "where comparing tag and field", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from where_events where tennant != foo`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"where_events","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z","bar"],["2009-11-10T23:00:03Z","baz"],["2009-11-10T23:00:04Z","bat"],["2009-11-10T23:00:05Z","bar"],["2009-11-10T23:00:06Z","bap"]]}]}]}`, - }, - &Query{ - name: "where comparing tag and tag", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from where_events where tennant = tennant`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"where_events","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z","bar"],["2009-11-10T23:00:03Z","baz"],["2009-11-10T23:00:04Z","bat"],["2009-11-10T23:00:05Z","bar"],["2009-11-10T23:00:06Z","bap"]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_With_EmptyTags(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu value=1 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:02Z").UnixNano()), - fmt.Sprintf(`cpu,host=server01 value=2 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:03Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "where empty tag", - params: url.Values{"db": []string{"db0"}}, - command: `select value from cpu where host = ''`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2009-11-10T23:00:02Z",1]]}]}]}`, - }, - &Query{ - name: "where not empty tag", - params: url.Values{"db": []string{"db0"}}, - command: `select value from cpu where host != ''`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2009-11-10T23:00:03Z",2]]}]}]}`, - }, - &Query{ - name: "where regex none", - params: url.Values{"db": []string{"db0"}}, - command: `select value from cpu where host !~ /.*/`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "where regex exact", - params: url.Values{"db": []string{"db0"}}, - command: `select value from cpu where host =~ /^server01$/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2009-11-10T23:00:03Z",2]]}]}]}`, - }, - &Query{ - name: "where regex exact (not)", - params: url.Values{"db": []string{"db0"}}, - command: `select value from cpu where host !~ /^server01$/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2009-11-10T23:00:02Z",1]]}]}]}`, - }, - &Query{ - name: "where regex at least one char", - params: url.Values{"db": []string{"db0"}}, - command: `select value from cpu where host =~ /.+/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2009-11-10T23:00:03Z",2]]}]}]}`, - }, - &Query{ - name: "where regex not at least one char", - params: url.Values{"db": []string{"db0"}}, - command: `select value from cpu where host !~ /.+/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2009-11-10T23:00:02Z",1]]}]}]}`, - }, - &Query{ - name: "group by empty tag", - params: url.Values{"db": []string{"db0"}}, - command: `select value from cpu group by host`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":""},"columns":["time","value"],"values":[["2009-11-10T23:00:02Z",1]]},{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["2009-11-10T23:00:03Z",2]]}]}]}`, - }, - &Query{ - name: "group by missing tag", - params: url.Values{"db": []string{"db0"}}, - command: `select value from cpu group by region`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"region":""},"columns":["time","value"],"values":[["2009-11-10T23:00:02Z",1],["2009-11-10T23:00:03Z",2]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_LimitAndOffset(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`limited,tennant=paul foo=2 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:02Z").UnixNano()), - fmt.Sprintf(`limited,tennant=paul foo=3 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:03Z").UnixNano()), - fmt.Sprintf(`limited,tennant=paul foo=4 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:04Z").UnixNano()), - fmt.Sprintf(`limited,tennant=todd foo=5 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:05Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "limit on points", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from "limited" LIMIT 2`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z",2],["2009-11-10T23:00:03Z",3]]}]}]}`, - }, - &Query{ - name: "limit higher than the number of data points", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from "limited" LIMIT 20`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z",2],["2009-11-10T23:00:03Z",3],["2009-11-10T23:00:04Z",4],["2009-11-10T23:00:05Z",5]]}]}]}`, - }, - &Query{ - name: "limit and offset", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from "limited" LIMIT 2 OFFSET 1`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","columns":["time","foo"],"values":[["2009-11-10T23:00:03Z",3],["2009-11-10T23:00:04Z",4]]}]}]}`, - }, - &Query{ - name: "limit + offset equal to total number of points", - params: url.Values{"db": []string{"db0"}}, - command: `select foo from "limited" LIMIT 3 OFFSET 3`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","columns":["time","foo"],"values":[["2009-11-10T23:00:05Z",5]]}]}]}`, - }, - &Query{ - name: "limit - offset higher than number of points", - command: `select foo from "limited" LIMIT 2 OFFSET 20`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "limit on points with group by time", - command: `select mean(foo) from "limited" WHERE time >= '2009-11-10T23:00:02Z' AND time < '2009-11-10T23:00:06Z' GROUP BY TIME(1s) LIMIT 2`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","columns":["time","mean"],"values":[["2009-11-10T23:00:02Z",2],["2009-11-10T23:00:03Z",3]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "limit higher than the number of data points with group by time", - command: `select mean(foo) from "limited" WHERE time >= '2009-11-10T23:00:02Z' AND time < '2009-11-10T23:00:06Z' GROUP BY TIME(1s) LIMIT 20`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","columns":["time","mean"],"values":[["2009-11-10T23:00:02Z",2],["2009-11-10T23:00:03Z",3],["2009-11-10T23:00:04Z",4],["2009-11-10T23:00:05Z",5]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "limit and offset with group by time", - command: `select mean(foo) from "limited" WHERE time >= '2009-11-10T23:00:02Z' AND time < '2009-11-10T23:00:06Z' GROUP BY TIME(1s) LIMIT 2 OFFSET 1`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","columns":["time","mean"],"values":[["2009-11-10T23:00:03Z",3],["2009-11-10T23:00:04Z",4]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "limit + offset equal to the number of points with group by time", - command: `select mean(foo) from "limited" WHERE time >= '2009-11-10T23:00:02Z' AND time < '2009-11-10T23:00:06Z' GROUP BY TIME(1s) LIMIT 3 OFFSET 3`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","columns":["time","mean"],"values":[["2009-11-10T23:00:05Z",5]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "limit - offset higher than number of points with group by time", - command: `select mean(foo) from "limited" WHERE time >= '2009-11-10T23:00:02Z' AND time < '2009-11-10T23:00:06Z' GROUP BY TIME(1s) LIMIT 2 OFFSET 20`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "limit - group by tennant", - command: `select foo from "limited" group by tennant limit 1`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","tags":{"tennant":"paul"},"columns":["time","foo"],"values":[["2009-11-10T23:00:02Z",2]]},{"name":"limited","tags":{"tennant":"todd"},"columns":["time","foo"],"values":[["2009-11-10T23:00:05Z",5]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "limit and offset - group by tennant", - command: `select foo from "limited" group by tennant limit 1 offset 1`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"limited","tags":{"tennant":"paul"},"columns":["time","foo"],"values":[["2009-11-10T23:00:03Z",3]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Fill(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`fills val=3 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:02Z").UnixNano()), - fmt.Sprintf(`fills val=5 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:03Z").UnixNano()), - fmt.Sprintf(`fills val=4 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:06Z").UnixNano()), - fmt.Sprintf(`fills val=10 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:16Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "fill with value", - command: `select mean(val) from fills where time >= '2009-11-10T23:00:00Z' and time < '2009-11-10T23:00:20Z' group by time(5s) FILL(1)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"fills","columns":["time","mean"],"values":[["2009-11-10T23:00:00Z",4],["2009-11-10T23:00:05Z",4],["2009-11-10T23:00:10Z",1],["2009-11-10T23:00:15Z",10]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "fill with value, WHERE all values match condition", - command: `select mean(val) from fills where time >= '2009-11-10T23:00:00Z' and time < '2009-11-10T23:00:20Z' and val < 50 group by time(5s) FILL(1)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"fills","columns":["time","mean"],"values":[["2009-11-10T23:00:00Z",4],["2009-11-10T23:00:05Z",4],["2009-11-10T23:00:10Z",1],["2009-11-10T23:00:15Z",10]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "fill with value, WHERE no values match condition", - command: `select mean(val) from fills where time >= '2009-11-10T23:00:00Z' and time < '2009-11-10T23:00:20Z' and val > 50 group by time(5s) FILL(1)`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "fill with previous", - command: `select mean(val) from fills where time >= '2009-11-10T23:00:00Z' and time < '2009-11-10T23:00:20Z' group by time(5s) FILL(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"fills","columns":["time","mean"],"values":[["2009-11-10T23:00:00Z",4],["2009-11-10T23:00:05Z",4],["2009-11-10T23:00:10Z",4],["2009-11-10T23:00:15Z",10]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "fill with none, i.e. clear out nulls", - command: `select mean(val) from fills where time >= '2009-11-10T23:00:00Z' and time < '2009-11-10T23:00:20Z' group by time(5s) FILL(none)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"fills","columns":["time","mean"],"values":[["2009-11-10T23:00:00Z",4],["2009-11-10T23:00:05Z",4],["2009-11-10T23:00:15Z",10]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "fill defaults to null", - command: `select mean(val) from fills where time >= '2009-11-10T23:00:00Z' and time < '2009-11-10T23:00:20Z' group by time(5s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"fills","columns":["time","mean"],"values":[["2009-11-10T23:00:00Z",4],["2009-11-10T23:00:05Z",4],["2009-11-10T23:00:10Z",null],["2009-11-10T23:00:15Z",10]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "fill defaults to 0 for count", - command: `select count(val) from fills where time >= '2009-11-10T23:00:00Z' and time < '2009-11-10T23:00:20Z' group by time(5s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"fills","columns":["time","count"],"values":[["2009-11-10T23:00:00Z",2],["2009-11-10T23:00:05Z",1],["2009-11-10T23:00:10Z",0],["2009-11-10T23:00:15Z",1]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "fill none drops 0s for count", - command: `select count(val) from fills where time >= '2009-11-10T23:00:00Z' and time < '2009-11-10T23:00:20Z' group by time(5s) fill(none)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"fills","columns":["time","count"],"values":[["2009-11-10T23:00:00Z",2],["2009-11-10T23:00:05Z",1],["2009-11-10T23:00:15Z",1]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "fill previous overwrites 0s for count", - command: `select count(val) from fills where time >= '2009-11-10T23:00:00Z' and time < '2009-11-10T23:00:20Z' group by time(5s) fill(previous)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"fills","columns":["time","count"],"values":[["2009-11-10T23:00:00Z",2],["2009-11-10T23:00:05Z",1],["2009-11-10T23:00:10Z",1],["2009-11-10T23:00:15Z",1]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Chunk(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := make([]string, 10001) // 10,000 is the default chunking size, even when no chunking requested. - expectedValues := make([]string, 10000) - for i := 0; i < len(writes); i++ { - writes[i] = fmt.Sprintf(`cpu value=%d %d`, i, time.Unix(0, int64(i)).UnixNano()) - if i < len(expectedValues) { - expectedValues[i] = fmt.Sprintf(`["%s",%d]`, time.Unix(0, int64(i)).UTC().Format(time.RFC3339Nano), i) - } - } - expected := fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[%s],"partial":true}]}]}`, strings.Join(expectedValues, ",")) - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "SELECT all values, no chunking", - command: `SELECT value FROM cpu`, - exp: expected, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_DropAndRecreateMeasurement(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - if err := s.CreateDatabaseAndRetentionPolicy("db1", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := strings.Join([]string{ - fmt.Sprintf(`cpu,host=serverA,region=uswest val=23.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`memory,host=serverB,region=uswest val=33.2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:01Z").UnixNano()), - }, "\n") - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: writes}, - &Write{db: "db1", data: writes}, - } - - test.addQueries([]*Query{ - &Query{ - name: "verify cpu measurement exists in db1", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","region","val"],"values":[["2000-01-01T00:00:00Z","serverA","uswest",23.2]]}]}]}`, - params: url.Values{"db": []string{"db1"}}, - }, - &Query{ - name: "Drop Measurement, series tags preserved tests", - command: `SHOW MEASUREMENTS`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["cpu"],["memory"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "show series", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=serverA,region=uswest"],["memory,host=serverB,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "ensure we can query for memory with both tags", - command: `SELECT * FROM memory where region='uswest' and host='serverB' GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","tags":{"host":"serverB","region":"uswest"},"columns":["time","val"],"values":[["2000-01-01T00:00:01Z",33.2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "drop measurement cpu", - command: `DROP MEASUREMENT cpu`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "verify measurements in DB that we deleted a measurement from", - command: `SHOW MEASUREMENTS`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["memory"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "verify series", - command: `SHOW SERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["memory,host=serverB,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "verify cpu measurement is gone", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "verify cpu measurement is NOT gone from other DB", - command: `SELECT * FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","region","val"],"values":[["2000-01-01T00:00:00Z","serverA","uswest",23.2]]}]}]}`, - params: url.Values{"db": []string{"db1"}}, - }, - &Query{ - name: "verify selecting from a tag 'host' still works", - command: `SELECT * FROM memory where host='serverB' GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","tags":{"host":"serverB","region":"uswest"},"columns":["time","val"],"values":[["2000-01-01T00:00:01Z",33.2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "verify selecting from a tag 'region' still works", - command: `SELECT * FROM memory where region='uswest' GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","tags":{"host":"serverB","region":"uswest"},"columns":["time","val"],"values":[["2000-01-01T00:00:01Z",33.2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "verify selecting from a tag 'host' and 'region' still works", - command: `SELECT * FROM memory where region='uswest' and host='serverB' GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"memory","tags":{"host":"serverB","region":"uswest"},"columns":["time","val"],"values":[["2000-01-01T00:00:01Z",33.2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "Drop non-existant measurement", - command: `DROP MEASUREMENT doesntexist`, - exp: `{"results":[{"statement_id":0,"error":"measurement not found: doesntexist"}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - // Test that re-inserting the measurement works fine. - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } - - test = NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: writes}, - } - - test.addQueries([]*Query{ - &Query{ - name: "verify measurements after recreation", - command: `SHOW MEASUREMENTS`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["cpu"],["memory"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "verify cpu measurement has been re-inserted", - command: `SELECT * FROM cpu GROUP BY *`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","tags":{"host":"serverA","region":"uswest"},"columns":["time","val"],"values":[["2000-01-01T00:00:00Z",23.2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_ShowQueries_Future(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu,host=server01 value=100 %d`, models.MaxNanoTime), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: `show measurements`, - command: "SHOW MEASUREMENTS", - exp: `{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["cpu"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show series`, - command: "SHOW SERIES", - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=server01"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag keys`, - command: "SHOW TAG KEYS FROM cpu", - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["tagKey"],"values":[["host"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values`, - command: "SHOW TAG VALUES WITH KEY = \"host\"", - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show field keys`, - command: "SHOW FIELD KEYS", - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["fieldKey","fieldType"],"values":[["value","float"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_ShowSeries(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu,host=server01 value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:01Z").UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=uswest value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:02Z").UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=useast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:03Z").UnixNano()), - fmt.Sprintf(`cpu,host=server02,region=useast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:04Z").UnixNano()), - fmt.Sprintf(`gpu,host=server02,region=useast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:05Z").UnixNano()), - fmt.Sprintf(`gpu,host=server03,region=caeast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:06Z").UnixNano()), - fmt.Sprintf(`disk,host=server03,region=caeast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:07Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: `show series`, - command: "SHOW SERIES", - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=server01"],["cpu,host=server01,region=useast"],["cpu,host=server01,region=uswest"],["cpu,host=server02,region=useast"],["disk,host=server03,region=caeast"],["gpu,host=server02,region=useast"],["gpu,host=server03,region=caeast"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show series from measurement`, - command: "SHOW SERIES FROM cpu", - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=server01"],["cpu,host=server01,region=useast"],["cpu,host=server01,region=uswest"],["cpu,host=server02,region=useast"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show series from regular expression`, - command: "SHOW SERIES FROM /[cg]pu/", - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=server01"],["cpu,host=server01,region=useast"],["cpu,host=server01,region=uswest"],["cpu,host=server02,region=useast"],["gpu,host=server02,region=useast"],["gpu,host=server03,region=caeast"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show series with where tag`, - command: "SHOW SERIES WHERE region = 'uswest'", - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=server01,region=uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show series where tag matches regular expression`, - command: "SHOW SERIES WHERE region =~ /ca.*/", - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["disk,host=server03,region=caeast"],["gpu,host=server03,region=caeast"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show series`, - command: "SHOW SERIES WHERE host !~ /server0[12]/", - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["disk,host=server03,region=caeast"],["gpu,host=server03,region=caeast"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show series with from and where`, - command: "SHOW SERIES FROM cpu WHERE region = 'useast'", - exp: `{"results":[{"statement_id":0,"series":[{"columns":["key"],"values":[["cpu,host=server01,region=useast"],["cpu,host=server02,region=useast"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show series with WHERE time should fail`, - command: "SHOW SERIES WHERE time > now() - 1h", - exp: `{"results":[{"statement_id":0,"error":"SHOW SERIES doesn't support time in WHERE clause"}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show series with WHERE field should fail`, - command: "SHOW SERIES WHERE value > 10.0", - exp: `{"results":[{"statement_id":0,"error":"invalid tag comparison operator"}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_ShowStats(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - if err := s.MetaClient.CreateSubscription("db0", "rp0", "foo", "ALL", []string{"udp://localhost:9000"}); err != nil { - t.Fatal(err) - } - - test := NewTest("db0", "rp0") - test.addQueries([]*Query{ - &Query{ - name: `show shots`, - command: "SHOW STATS", - exp: "subscriber", // Should see a subscriber stat in the json - pattern: true, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_ShowMeasurements(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu,host=server01 value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=uswest value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=useast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server02,region=useast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`gpu,host=server02,region=useast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`gpu,host=server02,region=caeast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`other,host=server03,region=caeast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: `show measurements with limit 2`, - command: "SHOW MEASUREMENTS LIMIT 2", - exp: `{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["cpu"],["gpu"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show measurements using WITH`, - command: "SHOW MEASUREMENTS WITH MEASUREMENT = cpu", - exp: `{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["cpu"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show measurements using WITH and regex`, - command: "SHOW MEASUREMENTS WITH MEASUREMENT =~ /[cg]pu/", - exp: `{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["cpu"],["gpu"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show measurements using WITH and regex - no matches`, - command: "SHOW MEASUREMENTS WITH MEASUREMENT =~ /.*zzzzz.*/", - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show measurements where tag matches regular expression`, - command: "SHOW MEASUREMENTS WHERE region =~ /ca.*/", - exp: `{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["gpu"],["other"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show measurements where tag does not match a regular expression`, - command: "SHOW MEASUREMENTS WHERE region !~ /ca.*/", - exp: `{"results":[{"statement_id":0,"series":[{"name":"measurements","columns":["name"],"values":[["cpu"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show measurements with time in WHERE clauses errors`, - command: `SHOW MEASUREMENTS WHERE time > now() - 1h`, - exp: `{"results":[{"statement_id":0,"error":"SHOW MEASUREMENTS doesn't support time in WHERE clause"}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_ShowTagKeys(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu,host=server01 value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=uswest value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=useast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server02,region=useast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`gpu,host=server02,region=useast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`gpu,host=server03,region=caeast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`disk,host=server03,region=caeast value=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: `show tag keys`, - command: "SHOW TAG KEYS", - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["tagKey"],"values":[["host"],["region"]]},{"name":"disk","columns":["tagKey"],"values":[["host"],["region"]]},{"name":"gpu","columns":["tagKey"],"values":[["host"],["region"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "show tag keys from", - command: "SHOW TAG KEYS FROM cpu", - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["tagKey"],"values":[["host"],["region"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "show tag keys from regex", - command: "SHOW TAG KEYS FROM /[cg]pu/", - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["tagKey"],"values":[["host"],["region"]]},{"name":"gpu","columns":["tagKey"],"values":[["host"],["region"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "show tag keys measurement not found", - command: "SHOW TAG KEYS FROM doesntexist", - exp: `{"results":[{"statement_id":0}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "show tag keys with time in WHERE clause errors", - command: "SHOW TAG KEYS FROM cpu WHERE time > now() - 1h", - exp: `{"results":[{"statement_id":0,"error":"SHOW TAG KEYS doesn't support time in WHERE clause"}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "show tag values with key", - command: "SHOW TAG VALUES WITH KEY = host", - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"],["host","server02"]]},{"name":"disk","columns":["key","value"],"values":[["host","server03"]]},{"name":"gpu","columns":["key","value"],"values":[["host","server02"],["host","server03"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: "show tag values with key regex", - command: "SHOW TAG VALUES WITH KEY =~ /ho/", - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"],["host","server02"]]},{"name":"disk","columns":["key","value"],"values":[["host","server03"]]},{"name":"gpu","columns":["key","value"],"values":[["host","server02"],["host","server03"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key and where`, - command: `SHOW TAG VALUES FROM cpu WITH KEY = host WHERE region = 'uswest'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key regex and where`, - command: `SHOW TAG VALUES FROM cpu WITH KEY =~ /ho/ WHERE region = 'uswest'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key and where matches the regular expression`, - command: `SHOW TAG VALUES WITH KEY = host WHERE region =~ /ca.*/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"disk","columns":["key","value"],"values":[["host","server03"]]},{"name":"gpu","columns":["key","value"],"values":[["host","server03"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key and where does not match the regular expression`, - command: `SHOW TAG VALUES WITH KEY = region WHERE host !~ /server0[12]/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"disk","columns":["key","value"],"values":[["region","caeast"]]},{"name":"gpu","columns":["key","value"],"values":[["region","caeast"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key and where partially matches the regular expression`, - command: `SHOW TAG VALUES WITH KEY = host WHERE region =~ /us/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"],["host","server02"]]},{"name":"gpu","columns":["key","value"],"values":[["host","server02"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key and where partially does not match the regular expression`, - command: `SHOW TAG VALUES WITH KEY = host WHERE region !~ /us/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"]]},{"name":"disk","columns":["key","value"],"values":[["host","server03"]]},{"name":"gpu","columns":["key","value"],"values":[["host","server03"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key in and where does not match the regular expression`, - command: `SHOW TAG VALUES FROM cpu WITH KEY IN (host, region) WHERE region = 'uswest'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"],["region","uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key regex and where does not match the regular expression`, - command: `SHOW TAG VALUES FROM cpu WITH KEY =~ /(host|region)/ WHERE region = 'uswest'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"],["region","uswest"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key and measurement matches regular expression`, - command: `SHOW TAG VALUES FROM /[cg]pu/ WITH KEY = host`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["key","value"],"values":[["host","server01"],["host","server02"]]},{"name":"gpu","columns":["key","value"],"values":[["host","server02"],["host","server03"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show tag values with key and time in WHERE clause should error`, - command: `SHOW TAG VALUES WITH KEY = host WHERE time > now() - 1h`, - exp: `{"results":[{"statement_id":0,"error":"SHOW TAG VALUES doesn't support time in WHERE clause"}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_ShowFieldKeys(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu,host=server01 field1=100 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=uswest field1=200,field2=300,field3=400 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=useast field1=200,field2=300,field3=400 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`cpu,host=server02,region=useast field1=200,field2=300,field3=400 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`gpu,host=server01,region=useast field4=200,field5=300 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`gpu,host=server03,region=caeast field6=200,field7=300 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - fmt.Sprintf(`disk,host=server03,region=caeast field8=200,field9=300 %d`, mustParseTime(time.RFC3339Nano, "2009-11-10T23:00:00Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: `show field keys`, - command: `SHOW FIELD KEYS`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["fieldKey","fieldType"],"values":[["field1","float"],["field2","float"],["field3","float"]]},{"name":"disk","columns":["fieldKey","fieldType"],"values":[["field8","float"],["field9","float"]]},{"name":"gpu","columns":["fieldKey","fieldType"],"values":[["field4","float"],["field5","float"],["field6","float"],["field7","float"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show field keys from measurement`, - command: `SHOW FIELD KEYS FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["fieldKey","fieldType"],"values":[["field1","float"],["field2","float"],["field3","float"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - &Query{ - name: `show field keys measurement with regex`, - command: `SHOW FIELD KEYS FROM /[cg]pu/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["fieldKey","fieldType"],"values":[["field1","float"],["field2","float"],["field3","float"]]},{"name":"gpu","columns":["fieldKey","fieldType"],"values":[["field4","float"],["field5","float"],["field6","float"],["field7","float"]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_ContinuousQuery(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - runTest := func(test *Test, t *testing.T) { - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } - } - - // Start times of CQ intervals. - interval0 := time.Now().Add(-time.Second).Round(time.Second * 5) - interval1 := interval0.Add(-time.Second * 5) - interval2 := interval0.Add(-time.Second * 10) - interval3 := interval0.Add(-time.Second * 15) - - writes := []string{ - // Point too far in the past for CQ to pick up. - fmt.Sprintf(`cpu,host=server01,region=uswest value=100 %d`, interval3.Add(time.Second).UnixNano()), - - // Points two intervals ago. - fmt.Sprintf(`cpu,host=server01 value=100 %d`, interval2.Add(time.Second).UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=uswest value=100 %d`, interval2.Add(time.Second*2).UnixNano()), - fmt.Sprintf(`cpu,host=server01,region=useast value=100 %d`, interval2.Add(time.Second*3).UnixNano()), - - // Points one interval ago. - fmt.Sprintf(`gpu,host=server02,region=useast value=100 %d`, interval1.Add(time.Second).UnixNano()), - fmt.Sprintf(`gpu,host=server03,region=caeast value=100 %d`, interval1.Add(time.Second*2).UnixNano()), - - // Points in the current interval. - fmt.Sprintf(`gpu,host=server03,region=caeast value=100 %d`, interval0.Add(time.Second).UnixNano()), - fmt.Sprintf(`disk,host=server03,region=caeast value=100 %d`, interval0.Add(time.Second*2).UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - test.addQueries([]*Query{ - &Query{ - name: `create another retention policy for CQ to write into`, - command: `CREATE RETENTION POLICY rp1 ON db0 DURATION 1h REPLICATION 1`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "create continuous query with backreference", - command: `CREATE CONTINUOUS QUERY "cq1" ON db0 BEGIN SELECT count(value) INTO "rp1".:MEASUREMENT FROM /[cg]pu/ GROUP BY time(5s) END`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: `create another retention policy for CQ to write into`, - command: `CREATE RETENTION POLICY rp2 ON db0 DURATION 1h REPLICATION 1`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "create continuous query with backreference and group by time", - command: `CREATE CONTINUOUS QUERY "cq2" ON db0 BEGIN SELECT count(value) INTO "rp2".:MEASUREMENT FROM /[cg]pu/ GROUP BY time(5s), * END`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: `show continuous queries`, - command: `SHOW CONTINUOUS QUERIES`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"db0","columns":["name","query"],"values":[["cq1","CREATE CONTINUOUS QUERY cq1 ON db0 BEGIN SELECT count(value) INTO db0.rp1.:MEASUREMENT FROM db0.rp0./[cg]pu/ GROUP BY time(5s) END"],["cq2","CREATE CONTINUOUS QUERY cq2 ON db0 BEGIN SELECT count(value) INTO db0.rp2.:MEASUREMENT FROM db0.rp0./[cg]pu/ GROUP BY time(5s), * END"]]}]}]}`, - }, - }...) - - // Run first test to create CQs. - runTest(&test, t) - - // Setup tests to check the CQ results. - test2 := NewTest("db0", "rp1") - test2.addQueries([]*Query{ - &Query{ - skip: true, - name: "check results of cq1", - command: `SELECT * FROM "rp1"./[cg]pu/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","count","host","region","value"],"values":[["` + interval2.UTC().Format(time.RFC3339Nano) + `",3,null,null,null]]},{"name":"gpu","columns":["time","count","host","region","value"],"values":[["` + interval1.UTC().Format(time.RFC3339Nano) + `",2,null,null,null],["` + interval0.UTC().Format(time.RFC3339Nano) + `",1,null,null,null]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - // TODO: restore this test once this is fixed: https://github.com/influxdata/influxdb/issues/3968 - &Query{ - skip: true, - name: "check results of cq2", - command: `SELECT * FROM "rp2"./[cg]pu/`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","count","host","region","value"],"values":[["` + interval2.UTC().Format(time.RFC3339Nano) + `",1,"server01","uswest",null],["` + interval2.UTC().Format(time.RFC3339Nano) + `",1,"server01","",null],["` + interval2.UTC().Format(time.RFC3339Nano) + `",1,"server01","useast",null]]},{"name":"gpu","columns":["time","count","host","region","value"],"values":[["` + interval1.UTC().Format(time.RFC3339Nano) + `",1,"server02","useast",null],["` + interval1.UTC().Format(time.RFC3339Nano) + `",1,"server03","caeast",null],["` + interval0.UTC().Format(time.RFC3339Nano) + `",1,"server03","caeast",null]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - // Run second test to check CQ results. - runTest(&test2, t) -} - -// Tests that a known CQ query with concurrent writes does not deadlock the server -func TestServer_ContinuousQuery_Deadlock(t *testing.T) { - - // Skip until #3517 & #3522 are merged - t.Skip("Skipping CQ deadlock test") - if testing.Short() { - t.Skip("skipping CQ deadlock test") - } - t.Parallel() - s := OpenServer(NewConfig()) - defer func() { - s.Close() - // Nil the server so our deadlock detector goroutine can determine if we completed writes - // without timing out - s.Server = nil - }() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - test := NewTest("db0", "rp0") - - test.addQueries([]*Query{ - &Query{ - name: "create continuous query", - command: `CREATE CONTINUOUS QUERY "my.query" ON db0 BEGIN SELECT sum(visits) as visits INTO test_1m FROM myseries GROUP BY time(1m), host END`, - exp: `{"results":[{"statement_id":0}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } - - // Deadlock detector. If the deadlock is fixed, this test should complete all the writes in ~2.5s seconds (with artifical delays - // added). After 10 seconds, if the server has not been closed then we hit the deadlock bug. - iterations := 0 - go func(s *Server) { - <-time.After(10 * time.Second) - - // If the server is not nil then the test is still running and stuck. We panic to avoid - // having the whole test suite hang indefinitely. - if s.Server != nil { - panic("possible deadlock. writes did not complete in time") - } - }(s) - - for { - - // After the second write, if the deadlock exists, we'll get a write timeout and - // all subsequent writes will timeout - if iterations > 5 { - break - } - writes := []string{} - for i := 0; i < 1000; i++ { - writes = append(writes, fmt.Sprintf(`myseries,host=host-%d visits=1i`, i)) - } - write := strings.Join(writes, "\n") - - if _, err := s.Write(test.db, test.rp, write, test.params); err != nil { - t.Fatal(err) - } - iterations += 1 - time.Sleep(500 * time.Millisecond) - } -} - -func TestServer_Query_EvilIdentifiers(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf("cpu select=1,in-bytes=2 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano())}, - } - - test.addQueries([]*Query{ - &Query{ - name: `query evil identifiers`, - command: `SELECT "select", "in-bytes" FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","select","in-bytes"],"values":[["2000-01-01T00:00:00Z",1,2]]}]}]}`, - params: url.Values{"db": []string{"db0"}}, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_OrderByTime(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu,host=server1 value=1 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:01Z").UnixNano()), - fmt.Sprintf(`cpu,host=server1 value=2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:02Z").UnixNano()), - fmt.Sprintf(`cpu,host=server1 value=3 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:03Z").UnixNano()), - - fmt.Sprintf(`power,presence=true value=1 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:01Z").UnixNano()), - fmt.Sprintf(`power,presence=true value=2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:02Z").UnixNano()), - fmt.Sprintf(`power,presence=true value=3 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:03Z").UnixNano()), - fmt.Sprintf(`power,presence=false value=4 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:04Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "order on points", - params: url.Values{"db": []string{"db0"}}, - command: `select value from "cpu" ORDER BY time DESC`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:03Z",3],["2000-01-01T00:00:02Z",2],["2000-01-01T00:00:01Z",1]]}]}]}`, - }, - - &Query{ - name: "order desc with tags", - params: url.Values{"db": []string{"db0"}}, - command: `select value from "power" ORDER BY time DESC`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"power","columns":["time","value"],"values":[["2000-01-01T00:00:04Z",4],["2000-01-01T00:00:03Z",3],["2000-01-01T00:00:02Z",2],["2000-01-01T00:00:01Z",1]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_FieldWithMultiplePeriods(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu foo.bar.baz=1 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "baseline", - params: url.Values{"db": []string{"db0"}}, - command: `select * from cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","foo.bar.baz"],"values":[["2000-01-01T00:00:00Z",1]]}]}]}`, - }, - &Query{ - name: "select field with periods", - params: url.Values{"db": []string{"db0"}}, - command: `select "foo.bar.baz" from cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","foo.bar.baz"],"values":[["2000-01-01T00:00:00Z",1]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_FieldWithMultiplePeriodsMeasurementPrefixMatch(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`foo foo.bar.baz=1 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "baseline", - params: url.Values{"db": []string{"db0"}}, - command: `select * from foo`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"foo","columns":["time","foo.bar.baz"],"values":[["2000-01-01T00:00:00Z",1]]}]}]}`, - }, - &Query{ - name: "select field with periods", - params: url.Values{"db": []string{"db0"}}, - command: `select "foo.bar.baz" from foo`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"foo","columns":["time","foo.bar.baz"],"values":[["2000-01-01T00:00:00Z",1]]}]}]}`, - }, - }...) - - for i, query := range test.queries { - if i == 0 { - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - } - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_IntoTarget(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`foo value=1 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf(`foo value=2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano()), - fmt.Sprintf(`foo value=3 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:20Z").UnixNano()), - fmt.Sprintf(`foo value=4 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:30Z").UnixNano()), - fmt.Sprintf(`foo value=4,foobar=3 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:40Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "into", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * INTO baz FROM foo`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"result","columns":["time","written"],"values":[["1970-01-01T00:00:00Z",5]]}]}]}`, - }, - &Query{ - name: "confirm results", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM baz`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"baz","columns":["time","foobar","value"],"values":[["2000-01-01T00:00:00Z",null,1],["2000-01-01T00:00:10Z",null,2],["2000-01-01T00:00:20Z",null,3],["2000-01-01T00:00:30Z",null,4],["2000-01-01T00:00:40Z",3,4]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// Ensure that binary operators of aggregates of separate fields, when a field is sometimes missing and sometimes present, -// result in values that are still properly time-aligned. -func TestServer_Query_IntoTarget_Sparse(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - // All points have fields n and a. Field b is not present in all intervals. - // First 10s interval is missing field b. Result a_n should be (2+5)*(3+7) = 70, b_n is null. - fmt.Sprintf(`foo a=2,n=3 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:01Z").UnixNano()), - fmt.Sprintf(`foo a=5,n=7 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:02Z").UnixNano()), - // Second 10s interval has field b. Result a_n = 11*17 = 187, b_n = 13*17 = 221. - fmt.Sprintf(`foo a=11,b=13,n=17 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:11Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "into", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sum(a) * sum(n) as a_n, sum(b) * sum(n) as b_n INTO baz FROM foo WHERE time >= '2000-01-01T00:00:00Z' AND time < '2000-01-01T00:01:00Z' GROUP BY time(10s)`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"result","columns":["time","written"],"values":[["1970-01-01T00:00:00Z",2]]}]}]}`, - }, - &Query{ - name: "confirm results", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM baz`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"baz","columns":["time","a_n","b_n"],"values":[["2000-01-01T00:00:00Z",70,null],["2000-01-01T00:00:10Z",187,221]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// This test ensures that data is not duplicated with measurements -// of the same name. -func TestServer_Query_DuplicateMeasurements(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - // Create a second database. - if err := s.CreateDatabaseAndRetentionPolicy("db1", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=1 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano())}, - } - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - test = NewTest("db1", "rp0") - test.writes = Writes{ - &Write{data: fmt.Sprintf(`cpu value=2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:10Z").UnixNano())}, - } - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - test.addQueries([]*Query{ - &Query{ - name: "select from both databases", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT value FROM db0.rp0.cpu, db1.rp0.cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:00Z",1],["2000-01-01T00:00:10Z",2]]}]}]}`, - }, - }...) - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_LargeTimestamp(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - writes := []string{ - fmt.Sprintf(`cpu value=100 %d`, models.MaxNanoTime), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - test.addQueries([]*Query{ - &Query{ - name: `select value at max nano time`, - params: url.Values{"db": []string{"db0"}}, - command: fmt.Sprintf(`SELECT value FROM cpu WHERE time <= %d`, models.MaxNanoTime), - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["` + time.Unix(0, models.MaxNanoTime).UTC().Format(time.RFC3339Nano) + `",100]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - // Open a new server with the same configuration file. - // This is to ensure the meta data was marshaled correctly. - s2 := OpenServer(s.Config) - defer s2.Close() - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -// This test reproduced a data race with closing the -// Subscriber points channel while writes were in-flight in the PointsWriter. -func TestServer_ConcurrentPointsWriter_Subscriber(t *testing.T) { - t.Parallel() - s := OpenDefaultServer(NewConfig()) - defer s.Close() - - // goroutine to write points - done := make(chan struct{}) - go func() { - for { - select { - case <-done: - return - default: - wpr := &coordinator.WritePointsRequest{ - Database: "db0", - RetentionPolicy: "rp0", - } - s.PointsWriter.WritePoints(wpr.Database, wpr.RetentionPolicy, models.ConsistencyLevelAny, wpr.Points) - } - } - }() - - time.Sleep(10 * time.Millisecond) - - close(done) - // Race occurs on s.Close() -} - -// Ensure time in where clause is inclusive -func TestServer_WhereTimeInclusive(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu value=1 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:01Z").UnixNano()), - fmt.Sprintf(`cpu value=2 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:02Z").UnixNano()), - fmt.Sprintf(`cpu value=3 %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:03Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "all GTE/LTE", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time >= '2000-01-01T00:00:01Z' and time <= '2000-01-01T00:00:03Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:01Z",1],["2000-01-01T00:00:02Z",2],["2000-01-01T00:00:03Z",3]]}]}]}`, - }, - &Query{ - name: "all GTE", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time >= '2000-01-01T00:00:01Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:01Z",1],["2000-01-01T00:00:02Z",2],["2000-01-01T00:00:03Z",3]]}]}]}`, - }, - &Query{ - name: "all LTE", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time <= '2000-01-01T00:00:03Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:01Z",1],["2000-01-01T00:00:02Z",2],["2000-01-01T00:00:03Z",3]]}]}]}`, - }, - &Query{ - name: "first GTE/LTE", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time >= '2000-01-01T00:00:01Z' and time <= '2000-01-01T00:00:01Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:01Z",1]]}]}]}`, - }, - &Query{ - name: "last GTE/LTE", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time >= '2000-01-01T00:00:03Z' and time <= '2000-01-01T00:00:03Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:03Z",3]]}]}]}`, - }, - &Query{ - name: "before GTE/LTE", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time <= '2000-01-01T00:00:00Z'`, - exp: `{"results":[{"statement_id":0}]}`, - }, - &Query{ - name: "all GT/LT", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time > '2000-01-01T00:00:00Z' and time < '2000-01-01T00:00:04Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:01Z",1],["2000-01-01T00:00:02Z",2],["2000-01-01T00:00:03Z",3]]}]}]}`, - }, - &Query{ - name: "first GT/LT", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time > '2000-01-01T00:00:00Z' and time < '2000-01-01T00:00:02Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:01Z",1]]}]}]}`, - }, - &Query{ - name: "last GT/LT", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time > '2000-01-01T00:00:02Z' and time < '2000-01-01T00:00:04Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:03Z",3]]}]}]}`, - }, - &Query{ - name: "all GT", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time > '2000-01-01T00:00:00Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:01Z",1],["2000-01-01T00:00:02Z",2],["2000-01-01T00:00:03Z",3]]}]}]}`, - }, - &Query{ - name: "all LT", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * from cpu where time < '2000-01-01T00:00:04Z'`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["2000-01-01T00:00:01Z",1],["2000-01-01T00:00:02Z",2],["2000-01-01T00:00:03Z",3]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_ImplicitEndTime(t *testing.T) { - t.Skip("flaky test") - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - now := time.Now().UTC().Truncate(time.Second) - past := now.Add(-10 * time.Second) - future := now.Add(10 * time.Minute) - writes := []string{ - fmt.Sprintf(`cpu value=1 %d`, past.UnixNano()), - fmt.Sprintf(`cpu value=2 %d`, future.UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "raw query", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT * FROM cpu`, - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","value"],"values":[["%s",1],["%s",2]]}]}]}`, past.Format(time.RFC3339Nano), future.Format(time.RFC3339Nano)), - }, - &Query{ - name: "aggregate query", - params: url.Values{"db": []string{"db0"}}, - command: fmt.Sprintf(`SELECT mean(value) FROM cpu WHERE time > '%s' - 1m GROUP BY time(1m) FILL(none)`, now.Truncate(time.Minute).Format(time.RFC3339Nano)), - exp: fmt.Sprintf(`{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","mean"],"values":[["%s",1]]}]}]}`, now.Truncate(time.Minute).Format(time.RFC3339Nano)), - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} - -func TestServer_Query_Sample_Wildcard(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig()) - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0), true); err != nil { - t.Fatal(err) - } - - writes := []string{ - fmt.Sprintf(`cpu float=1,int=1i,string="hello, world",bool=true %d`, mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - } - - test := NewTest("db0", "rp0") - test.writes = Writes{ - &Write{data: strings.Join(writes, "\n")}, - } - - test.addQueries([]*Query{ - &Query{ - name: "sample() with wildcard", - params: url.Values{"db": []string{"db0"}}, - command: `SELECT sample(*, 1) FROM cpu`, - exp: `{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","sample_bool","sample_float","sample_int","sample_string"],"values":[["2000-01-01T00:00:00Z",true,1,1,"hello, world"]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} diff --git a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_test.md b/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_test.md deleted file mode 100644 index 8df37e33..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/influxd/run/server_test.md +++ /dev/null @@ -1,150 +0,0 @@ -# Server Integration Tests - -Currently, the file `server_test.go` has integration tests for single node scenarios. -At some point we'll need to add cluster tests, and may add them in a different file, or -rename `server_test.go` to `server_single_node_test.go` or something like that. - -## What is in a test? - -Each test is broken apart effectively into the following areas: - -- Write sample data -- Use cases for table driven test, that include a command (typically a query) and an expected result. - -When each test runs it does the following: - -- init: determines if there are any writes and if so, writes them to the in-memory database -- queries: iterate through each query, executing the command, and comparing the results to the expected result. - -## Idempotent - Allows for parallel tests - -Each test should be `idempotent`, meaning that its data will not be affected by other tests, or use cases within the table tests themselves. -This allows for parallel testing, keeping the test suite total execution time very low. - -### Basic sample test - -```go -// Ensure the server can have a database with multiple measurements. -func TestServer_Query_Multiple_Measurements(t *testing.T) { - t.Parallel() - s := OpenServer(NewConfig(), "") - defer s.Close() - - if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicyInfo("rp0", 1, 1*time.Hour)); err != nil { - t.Fatal(err) - } - - // Make sure we do writes for measurements that will span across shards - writes := []string{ - fmt.Sprintf("cpu,host=server01 value=100,core=4 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf("cpu1,host=server02 value=50,core=2 %d", mustParseTime(time.RFC3339Nano, "2015-01-01T00:00:00Z").UnixNano()), - } - test := NewTest("db0", "rp0") - test.write = strings.Join(writes, "\n") - - test.addQueries([]*Query{ - &Query{ - name: "measurement in one shard but not another shouldn't panic server", - command: `SELECT host,value FROM db0.rp0.cpu`, - exp: `{"results":[{"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["2000-01-01T00:00:00Z",100]]}]}]}`, - }, - }...) - - if err := test.init(s); err != nil { - t.Fatalf("test init failed: %s", err) - } - - for _, query := range test.queries { - if query.skip { - t.Logf("SKIP:: %s", query.name) - continue - } - if err := query.Execute(s); err != nil { - t.Error(query.Error(err)) - } else if !query.success() { - t.Error(query.failureMessage()) - } - } -} -``` - -Let's break this down: - -In this test, we first tell it to run in parallel with the `t.Parallel()` call. - -We then open a new server with: - -```go -s := OpenServer(NewConfig(), "") -defer s.Close() -``` - -If needed, we create a database and default retention policy. This is usually needed -when inserting and querying data. This is not needed if you are testing commands like `CREATE DATABASE`, `SHOW DIAGNOSTICS`, etc. - -```go -if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicyInfo("rp0", 1, 1*time.Hour)); err != nil { - t.Fatal(err) -} -``` - -Next, set up the write data you need: - -```go -writes := []string{ - fmt.Sprintf("cpu,host=server01 value=100,core=4 %d", mustParseTime(time.RFC3339Nano, "2000-01-01T00:00:00Z").UnixNano()), - fmt.Sprintf("cpu1,host=server02 value=50,core=2 %d", mustParseTime(time.RFC3339Nano, "2015-01-01T00:00:00Z").UnixNano()), -} -``` -Create a new test with the database and retention policy: - -```go -test := NewTest("db0", "rp0") -``` - -Send in the writes: -```go -test.write = strings.Join(writes, "\n") -``` - -Add some queries (the second one is mocked out to show how to add more than one): - -```go -test.addQueries([]*Query{ - &Query{ - name: "measurement in one shard but not another shouldn't panic server", - command: `SELECT host,value FROM db0.rp0.cpu`, - exp: `{"results":[{"series":[{"name":"cpu","tags":{"host":"server01"},"columns":["time","value"],"values":[["2000-01-01T00:00:00Z",100]]}]}]}`, - }, - &Query{ - name: "another test here...", - command: `Some query command`, - exp: `the expected results`, - }, -}...) -``` - -The rest of the code is boilerplate execution code. It is purposefully not refactored out to a helper -to make sure the test failure reports the proper lines for debugging purposes. - -#### Running the tests - -To run the tests: - -```sh -go test ./cmd/influxd/run -parallel 500 -timeout 10s -``` - -#### Running a specific test - -```sh -go test ./cmd/influxd/run -parallel 500 -timeout 10s -run TestServer_Query_Fill -``` - -#### Verbose feedback - -By default, all logs are silenced when testing. If you pass in the `-v` flag, the test suite becomes verbose, and enables all logging in the system - -```sh -go test ./cmd/influxd/run -parallel 500 -timeout 10s -run TestServer_Query_Fill -v -``` diff --git a/vendor/github.com/influxdata/influxdb/cmd/parse.go b/vendor/github.com/influxdata/influxdb/cmd/parse.go deleted file mode 100644 index 688f378b..00000000 --- a/vendor/github.com/influxdata/influxdb/cmd/parse.go +++ /dev/null @@ -1,28 +0,0 @@ -package cmd - -import "strings" - -// ParseCommandName extracts the command name and args from the args list. -func ParseCommandName(args []string) (string, []string) { - // Retrieve command name as first argument. - var name string - if len(args) > 0 { - if !strings.HasPrefix(args[0], "-") { - name = args[0] - } else if args[0] == "-h" || args[0] == "-help" || args[0] == "--help" { - // Special case -h immediately following binary name - name = "help" - } - } - - // If command is "help" and has an argument then rewrite args to use "-h". - if name == "help" && len(args) > 2 && !strings.HasPrefix(args[1], "-") { - return args[1], []string{"-h"} - } - - // If a named command is specified then return it with its arguments. - if name != "" { - return name, args[1:] - } - return "", args -} diff --git a/vendor/github.com/influxdata/influxdb/coordinator/config.go b/vendor/github.com/influxdata/influxdb/coordinator/config.go deleted file mode 100644 index add06d2e..00000000 --- a/vendor/github.com/influxdata/influxdb/coordinator/config.go +++ /dev/null @@ -1,47 +0,0 @@ -package coordinator - -import ( - "time" - - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/toml" -) - -const ( - // DefaultWriteTimeout is the default timeout for a complete write to succeed. - DefaultWriteTimeout = 10 * time.Second - - // DefaultMaxConcurrentQueries is the maximum number of running queries. - // A value of zero will make the maximum query limit unlimited. - DefaultMaxConcurrentQueries = 0 - - // DefaultMaxSelectPointN is the maximum number of points a SELECT can process. - // A value of zero will make the maximum point count unlimited. - DefaultMaxSelectPointN = 0 - - // DefaultMaxSelectSeriesN is the maximum number of series a SELECT can run. - // A value of zero will make the maximum series count unlimited. - DefaultMaxSelectSeriesN = 0 -) - -// Config represents the configuration for the clustering service. -type Config struct { - WriteTimeout toml.Duration `toml:"write-timeout"` - MaxConcurrentQueries int `toml:"max-concurrent-queries"` - QueryTimeout toml.Duration `toml:"query-timeout"` - LogQueriesAfter toml.Duration `toml:"log-queries-after"` - MaxSelectPointN int `toml:"max-select-point"` - MaxSelectSeriesN int `toml:"max-select-series"` - MaxSelectBucketsN int `toml:"max-select-buckets"` -} - -// NewConfig returns an instance of Config with defaults. -func NewConfig() Config { - return Config{ - WriteTimeout: toml.Duration(DefaultWriteTimeout), - QueryTimeout: toml.Duration(influxql.DefaultQueryTimeout), - MaxConcurrentQueries: DefaultMaxConcurrentQueries, - MaxSelectPointN: DefaultMaxSelectPointN, - MaxSelectSeriesN: DefaultMaxSelectSeriesN, - } -} diff --git a/vendor/github.com/influxdata/influxdb/coordinator/config_test.go b/vendor/github.com/influxdata/influxdb/coordinator/config_test.go deleted file mode 100644 index 2f214367..00000000 --- a/vendor/github.com/influxdata/influxdb/coordinator/config_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package coordinator_test - -import ( - "testing" - "time" - - "github.com/BurntSushi/toml" - "github.com/influxdata/influxdb/coordinator" -) - -func TestConfig_Parse(t *testing.T) { - // Parse configuration. - var c coordinator.Config - if _, err := toml.Decode(` -write-timeout = "20s" -`, &c); err != nil { - t.Fatal(err) - } - - // Validate configuration. - if time.Duration(c.WriteTimeout) != 20*time.Second { - t.Fatalf("unexpected write timeout s: %s", c.WriteTimeout) - } -} diff --git a/vendor/github.com/influxdata/influxdb/coordinator/meta_client.go b/vendor/github.com/influxdata/influxdb/coordinator/meta_client.go deleted file mode 100644 index 040f4a10..00000000 --- a/vendor/github.com/influxdata/influxdb/coordinator/meta_client.go +++ /dev/null @@ -1,35 +0,0 @@ -package coordinator - -import ( - "time" - - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/services/meta" -) - -// MetaClient is an interface for accessing meta data. -type MetaClient interface { - CreateContinuousQuery(database, name, query string) error - CreateDatabase(name string) (*meta.DatabaseInfo, error) - CreateDatabaseWithRetentionPolicy(name string, spec *meta.RetentionPolicySpec) (*meta.DatabaseInfo, error) - CreateRetentionPolicy(database string, spec *meta.RetentionPolicySpec, makeDefault bool) (*meta.RetentionPolicyInfo, error) - CreateSubscription(database, rp, name, mode string, destinations []string) error - CreateUser(name, password string, admin bool) (*meta.UserInfo, error) - Database(name string) *meta.DatabaseInfo - Databases() []meta.DatabaseInfo - DropShard(id uint64) error - DropContinuousQuery(database, name string) error - DropDatabase(name string) error - DropRetentionPolicy(database, name string) error - DropSubscription(database, rp, name string) error - DropUser(name string) error - RetentionPolicy(database, name string) (rpi *meta.RetentionPolicyInfo, err error) - SetAdminPrivilege(username string, admin bool) error - SetPrivilege(username, database string, p influxql.Privilege) error - ShardsByTimeRange(sources influxql.Sources, tmin, tmax time.Time) (a []meta.ShardInfo, err error) - UpdateRetentionPolicy(database, name string, rpu *meta.RetentionPolicyUpdate, makeDefault bool) error - UpdateUser(name, password string) error - UserPrivilege(username, database string) (*influxql.Privilege, error) - UserPrivileges(username string) (map[string]influxql.Privilege, error) - Users() []meta.UserInfo -} diff --git a/vendor/github.com/influxdata/influxdb/coordinator/meta_client_test.go b/vendor/github.com/influxdata/influxdb/coordinator/meta_client_test.go deleted file mode 100644 index 03a009f9..00000000 --- a/vendor/github.com/influxdata/influxdb/coordinator/meta_client_test.go +++ /dev/null @@ -1,160 +0,0 @@ -package coordinator_test - -import ( - "time" - - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/services/meta" -) - -// MetaClient is a mockable implementation of cluster.MetaClient. -type MetaClient struct { - CreateContinuousQueryFn func(database, name, query string) error - CreateDatabaseFn func(name string) (*meta.DatabaseInfo, error) - CreateDatabaseWithRetentionPolicyFn func(name string, spec *meta.RetentionPolicySpec) (*meta.DatabaseInfo, error) - CreateRetentionPolicyFn func(database string, spec *meta.RetentionPolicySpec, makeDefault bool) (*meta.RetentionPolicyInfo, error) - CreateSubscriptionFn func(database, rp, name, mode string, destinations []string) error - CreateUserFn func(name, password string, admin bool) (*meta.UserInfo, error) - DatabaseFn func(name string) *meta.DatabaseInfo - DatabasesFn func() []meta.DatabaseInfo - DataNodeFn func(id uint64) (*meta.NodeInfo, error) - DataNodesFn func() ([]meta.NodeInfo, error) - DeleteDataNodeFn func(id uint64) error - DeleteMetaNodeFn func(id uint64) error - DropContinuousQueryFn func(database, name string) error - DropDatabaseFn func(name string) error - DropRetentionPolicyFn func(database, name string) error - DropSubscriptionFn func(database, rp, name string) error - DropShardFn func(id uint64) error - DropUserFn func(name string) error - MetaNodesFn func() ([]meta.NodeInfo, error) - RetentionPolicyFn func(database, name string) (rpi *meta.RetentionPolicyInfo, err error) - SetAdminPrivilegeFn func(username string, admin bool) error - SetPrivilegeFn func(username, database string, p influxql.Privilege) error - ShardsByTimeRangeFn func(sources influxql.Sources, tmin, tmax time.Time) (a []meta.ShardInfo, err error) - UpdateRetentionPolicyFn func(database, name string, rpu *meta.RetentionPolicyUpdate, makeDefault bool) error - UpdateUserFn func(name, password string) error - UserPrivilegeFn func(username, database string) (*influxql.Privilege, error) - UserPrivilegesFn func(username string) (map[string]influxql.Privilege, error) - UsersFn func() []meta.UserInfo -} - -func (c *MetaClient) CreateContinuousQuery(database, name, query string) error { - return c.CreateContinuousQueryFn(database, name, query) -} - -func (c *MetaClient) CreateDatabase(name string) (*meta.DatabaseInfo, error) { - return c.CreateDatabaseFn(name) -} - -func (c *MetaClient) CreateDatabaseWithRetentionPolicy(name string, spec *meta.RetentionPolicySpec) (*meta.DatabaseInfo, error) { - return c.CreateDatabaseWithRetentionPolicyFn(name, spec) -} - -func (c *MetaClient) CreateRetentionPolicy(database string, spec *meta.RetentionPolicySpec, makeDefault bool) (*meta.RetentionPolicyInfo, error) { - return c.CreateRetentionPolicyFn(database, spec, makeDefault) -} - -func (c *MetaClient) DropShard(id uint64) error { - return c.DropShardFn(id) -} - -func (c *MetaClient) CreateSubscription(database, rp, name, mode string, destinations []string) error { - return c.CreateSubscriptionFn(database, rp, name, mode, destinations) -} - -func (c *MetaClient) CreateUser(name, password string, admin bool) (*meta.UserInfo, error) { - return c.CreateUserFn(name, password, admin) -} - -func (c *MetaClient) Database(name string) *meta.DatabaseInfo { - return c.DatabaseFn(name) -} - -func (c *MetaClient) Databases() []meta.DatabaseInfo { - return c.DatabasesFn() -} - -func (c *MetaClient) DataNode(id uint64) (*meta.NodeInfo, error) { - return c.DataNodeFn(id) -} - -func (c *MetaClient) DataNodes() ([]meta.NodeInfo, error) { - return c.DataNodesFn() -} - -func (c *MetaClient) DeleteDataNode(id uint64) error { - return c.DeleteDataNodeFn(id) -} - -func (c *MetaClient) DeleteMetaNode(id uint64) error { - return c.DeleteMetaNodeFn(id) -} - -func (c *MetaClient) DropContinuousQuery(database, name string) error { - return c.DropContinuousQueryFn(database, name) -} - -func (c *MetaClient) DropDatabase(name string) error { - return c.DropDatabaseFn(name) -} - -func (c *MetaClient) DropRetentionPolicy(database, name string) error { - return c.DropRetentionPolicyFn(database, name) -} - -func (c *MetaClient) DropSubscription(database, rp, name string) error { - return c.DropSubscriptionFn(database, rp, name) -} - -func (c *MetaClient) DropUser(name string) error { - return c.DropUserFn(name) -} - -func (c *MetaClient) MetaNodes() ([]meta.NodeInfo, error) { - return c.MetaNodesFn() -} - -func (c *MetaClient) RetentionPolicy(database, name string) (rpi *meta.RetentionPolicyInfo, err error) { - return c.RetentionPolicyFn(database, name) -} - -func (c *MetaClient) SetAdminPrivilege(username string, admin bool) error { - return c.SetAdminPrivilegeFn(username, admin) -} - -func (c *MetaClient) SetPrivilege(username, database string, p influxql.Privilege) error { - return c.SetPrivilegeFn(username, database, p) -} - -func (c *MetaClient) ShardsByTimeRange(sources influxql.Sources, tmin, tmax time.Time) (a []meta.ShardInfo, err error) { - return c.ShardsByTimeRangeFn(sources, tmin, tmax) -} - -func (c *MetaClient) UpdateRetentionPolicy(database, name string, rpu *meta.RetentionPolicyUpdate, makeDefault bool) error { - return c.UpdateRetentionPolicyFn(database, name, rpu, makeDefault) -} - -func (c *MetaClient) UpdateUser(name, password string) error { - return c.UpdateUserFn(name, password) -} - -func (c *MetaClient) UserPrivilege(username, database string) (*influxql.Privilege, error) { - return c.UserPrivilegeFn(username, database) -} - -func (c *MetaClient) UserPrivileges(username string) (map[string]influxql.Privilege, error) { - return c.UserPrivilegesFn(username) -} - -func (c *MetaClient) Users() []meta.UserInfo { - return c.UsersFn() -} - -// DefaultMetaClientDatabaseFn returns a single database (db0) with a retention policy. -func DefaultMetaClientDatabaseFn(name string) *meta.DatabaseInfo { - return &meta.DatabaseInfo{ - Name: DefaultDatabase, - DefaultRetentionPolicy: DefaultRetentionPolicy, - } -} diff --git a/vendor/github.com/influxdata/influxdb/coordinator/points_writer.go b/vendor/github.com/influxdata/influxdb/coordinator/points_writer.go deleted file mode 100644 index 1fbb5adf..00000000 --- a/vendor/github.com/influxdata/influxdb/coordinator/points_writer.go +++ /dev/null @@ -1,374 +0,0 @@ -package coordinator - -import ( - "errors" - "fmt" - "sort" - "sync" - "sync/atomic" - "time" - - "github.com/influxdata/influxdb" - "github.com/influxdata/influxdb/models" - "github.com/influxdata/influxdb/services/meta" - "github.com/influxdata/influxdb/tsdb" - "go.uber.org/zap" -) - -// The statistics generated by the "write" mdoule -const ( - statWriteReq = "req" - statPointWriteReq = "pointReq" - statPointWriteReqLocal = "pointReqLocal" - statWriteOK = "writeOk" - statWriteDrop = "writeDrop" - statWriteTimeout = "writeTimeout" - statWriteErr = "writeError" - statSubWriteOK = "subWriteOk" - statSubWriteDrop = "subWriteDrop" -) - -var ( - // ErrTimeout is returned when a write times out. - ErrTimeout = errors.New("timeout") - - // ErrPartialWrite is returned when a write partially succeeds but does - // not meet the requested consistency level. - ErrPartialWrite = errors.New("partial write") - - // ErrWriteFailed is returned when no writes succeeded. - ErrWriteFailed = errors.New("write failed") -) - -// PointsWriter handles writes across multiple local and remote data nodes. -type PointsWriter struct { - mu sync.RWMutex - closing chan struct{} - WriteTimeout time.Duration - Logger zap.Logger - - Node *influxdb.Node - - MetaClient interface { - Database(name string) (di *meta.DatabaseInfo) - RetentionPolicy(database, policy string) (*meta.RetentionPolicyInfo, error) - CreateShardGroup(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) - ShardOwner(shardID uint64) (string, string, *meta.ShardGroupInfo) - } - - TSDBStore interface { - CreateShard(database, retentionPolicy string, shardID uint64, enabled bool) error - WriteToShard(shardID uint64, points []models.Point) error - } - - ShardWriter interface { - WriteShard(shardID, ownerID uint64, points []models.Point) error - } - - Subscriber interface { - Points() chan<- *WritePointsRequest - } - subPoints chan<- *WritePointsRequest - - stats *WriteStatistics -} - -// WritePointsRequest represents a request to write point data to the cluster -type WritePointsRequest struct { - Database string - RetentionPolicy string - Points []models.Point -} - -// AddPoint adds a point to the WritePointRequest with field key 'value' -func (w *WritePointsRequest) AddPoint(name string, value interface{}, timestamp time.Time, tags map[string]string) { - pt, err := models.NewPoint( - name, models.NewTags(tags), map[string]interface{}{"value": value}, timestamp, - ) - if err != nil { - return - } - w.Points = append(w.Points, pt) -} - -// NewPointsWriter returns a new instance of PointsWriter for a node. -func NewPointsWriter() *PointsWriter { - return &PointsWriter{ - closing: make(chan struct{}), - WriteTimeout: DefaultWriteTimeout, - Logger: zap.New(zap.NullEncoder()), - stats: &WriteStatistics{}, - } -} - -// ShardMapping contains a mapping of a shards to a points. -type ShardMapping struct { - Points map[uint64][]models.Point // The points associated with a shard ID - Shards map[uint64]*meta.ShardInfo // The shards that have been mapped, keyed by shard ID -} - -// NewShardMapping creates an empty ShardMapping -func NewShardMapping() *ShardMapping { - return &ShardMapping{ - Points: map[uint64][]models.Point{}, - Shards: map[uint64]*meta.ShardInfo{}, - } -} - -// MapPoint maps a point to shard -func (s *ShardMapping) MapPoint(shardInfo *meta.ShardInfo, p models.Point) { - s.Points[shardInfo.ID] = append(s.Points[shardInfo.ID], p) - s.Shards[shardInfo.ID] = shardInfo -} - -// Open opens the communication channel with the point writer -func (w *PointsWriter) Open() error { - w.mu.Lock() - defer w.mu.Unlock() - w.closing = make(chan struct{}) - if w.Subscriber != nil { - w.subPoints = w.Subscriber.Points() - } - return nil -} - -// Close closes the communication channel with the point writer -func (w *PointsWriter) Close() error { - w.mu.Lock() - defer w.mu.Unlock() - if w.closing != nil { - close(w.closing) - } - if w.subPoints != nil { - // 'nil' channels always block so this makes the - // select statement in WritePoints hit its default case - // dropping any in-flight writes. - w.subPoints = nil - } - return nil -} - -func (w *PointsWriter) WithLogger(log zap.Logger) { - w.Logger = log.With(zap.String("service", "write")) -} - -// WriteStatistics keeps statistics related to the PointsWriter. -type WriteStatistics struct { - WriteReq int64 - PointWriteReq int64 - PointWriteReqLocal int64 - WriteOK int64 - WriteDropped int64 - WriteTimeout int64 - WriteErr int64 - SubWriteOK int64 - SubWriteDrop int64 -} - -// Statistics returns statistics for periodic monitoring. -func (w *PointsWriter) Statistics(tags map[string]string) []models.Statistic { - return []models.Statistic{{ - Name: "write", - Tags: tags, - Values: map[string]interface{}{ - statWriteReq: atomic.LoadInt64(&w.stats.WriteReq), - statPointWriteReq: atomic.LoadInt64(&w.stats.PointWriteReq), - statPointWriteReqLocal: atomic.LoadInt64(&w.stats.PointWriteReqLocal), - statWriteOK: atomic.LoadInt64(&w.stats.WriteOK), - statWriteDrop: atomic.LoadInt64(&w.stats.WriteDropped), - statWriteTimeout: atomic.LoadInt64(&w.stats.WriteTimeout), - statWriteErr: atomic.LoadInt64(&w.stats.WriteErr), - statSubWriteOK: atomic.LoadInt64(&w.stats.SubWriteOK), - statSubWriteDrop: atomic.LoadInt64(&w.stats.SubWriteDrop), - }, - }} -} - -// MapShards maps the points contained in wp to a ShardMapping. If a point -// maps to a shard group or shard that does not currently exist, it will be -// created before returning the mapping. -func (w *PointsWriter) MapShards(wp *WritePointsRequest) (*ShardMapping, error) { - rp, err := w.MetaClient.RetentionPolicy(wp.Database, wp.RetentionPolicy) - if err != nil { - return nil, err - } else if rp == nil { - return nil, influxdb.ErrRetentionPolicyNotFound(wp.RetentionPolicy) - } - - // Holds all the shard groups and shards that are required for writes. - list := make(sgList, 0, 8) - min := time.Unix(0, models.MinNanoTime) - if rp.Duration > 0 { - min = time.Now().Add(-rp.Duration) - } - - for _, p := range wp.Points { - // Either the point is outside the scope of the RP, or we already have - // a suitable shard group for the point. - if p.Time().Before(min) || list.Covers(p.Time()) { - continue - } - - // No shard groups overlap with the point's time, so we will create - // a new shard group for this point. - sg, err := w.MetaClient.CreateShardGroup(wp.Database, wp.RetentionPolicy, p.Time()) - if err != nil { - return nil, err - } - - if sg == nil { - return nil, errors.New("nil shard group") - } - list = list.Append(*sg) - } - - mapping := NewShardMapping() - for _, p := range wp.Points { - sg := list.ShardGroupAt(p.Time()) - if sg == nil { - // We didn't create a shard group because the point was outside the - // scope of the RP. - atomic.AddInt64(&w.stats.WriteDropped, 1) - continue - } - - sh := sg.ShardFor(p.HashID()) - mapping.MapPoint(&sh, p) - } - return mapping, nil -} - -// sgList is a wrapper around a meta.ShardGroupInfos where we can also check -// if a given time is covered by any of the shard groups in the list. -type sgList meta.ShardGroupInfos - -func (l sgList) Covers(t time.Time) bool { - if len(l) == 0 { - return false - } - return l.ShardGroupAt(t) != nil -} - -func (l sgList) ShardGroupAt(t time.Time) *meta.ShardGroupInfo { - // Attempt to find a shard group that could contain this point. - // Shard groups are sorted first according to end time, and then according - // to start time. Therefore, if there are multiple shard groups that match - // this point's time they will be preferred in this order: - // - // - a shard group with the earliest end time; - // - (assuming identical end times) the shard group with the earliest start - // time. - idx := sort.Search(len(l), func(i int) bool { return l[i].EndTime.After(t) }) - - // We couldn't find a shard group the point falls into. - if idx == len(l) || t.Before(l[idx].StartTime) { - return nil - } - return &l[idx] -} - -// Append appends a shard group to the list, and returns a sorted list. -func (l sgList) Append(sgi meta.ShardGroupInfo) sgList { - next := append(l, sgi) - sort.Sort(meta.ShardGroupInfos(next)) - return next -} - -// WritePointsInto is a copy of WritePoints that uses a tsdb structure instead of -// a cluster structure for information. This is to avoid a circular dependency -func (w *PointsWriter) WritePointsInto(p *IntoWriteRequest) error { - return w.WritePoints(p.Database, p.RetentionPolicy, models.ConsistencyLevelOne, p.Points) -} - -// WritePoints writes across multiple local and remote data nodes according the consistency level. -func (w *PointsWriter) WritePoints(database, retentionPolicy string, consistencyLevel models.ConsistencyLevel, points []models.Point) error { - atomic.AddInt64(&w.stats.WriteReq, 1) - atomic.AddInt64(&w.stats.PointWriteReq, int64(len(points))) - - if retentionPolicy == "" { - db := w.MetaClient.Database(database) - if db == nil { - return influxdb.ErrDatabaseNotFound(database) - } - retentionPolicy = db.DefaultRetentionPolicy - } - - shardMappings, err := w.MapShards(&WritePointsRequest{Database: database, RetentionPolicy: retentionPolicy, Points: points}) - if err != nil { - return err - } - - // Write each shard in it's own goroutine and return as soon as one fails. - ch := make(chan error, len(shardMappings.Points)) - for shardID, points := range shardMappings.Points { - go func(shard *meta.ShardInfo, database, retentionPolicy string, points []models.Point) { - ch <- w.writeToShard(shard, database, retentionPolicy, points) - }(shardMappings.Shards[shardID], database, retentionPolicy, points) - } - - // Send points to subscriptions if possible. - ok := false - // We need to lock just in case the channel is about to be nil'ed - w.mu.RLock() - select { - case w.subPoints <- &WritePointsRequest{Database: database, RetentionPolicy: retentionPolicy, Points: points}: - ok = true - default: - } - w.mu.RUnlock() - if ok { - atomic.AddInt64(&w.stats.SubWriteOK, 1) - } else { - atomic.AddInt64(&w.stats.SubWriteDrop, 1) - } - - timeout := time.NewTimer(w.WriteTimeout) - defer timeout.Stop() - for range shardMappings.Points { - select { - case <-w.closing: - return ErrWriteFailed - case <-timeout.C: - atomic.AddInt64(&w.stats.WriteTimeout, 1) - // return timeout error to caller - return ErrTimeout - case err := <-ch: - if err != nil { - return err - } - } - } - return nil -} - -// writeToShards writes points to a shard. -func (w *PointsWriter) writeToShard(shard *meta.ShardInfo, database, retentionPolicy string, points []models.Point) error { - atomic.AddInt64(&w.stats.PointWriteReqLocal, int64(len(points))) - - err := w.TSDBStore.WriteToShard(shard.ID, points) - if err == nil { - atomic.AddInt64(&w.stats.WriteOK, 1) - return nil - } - - // If we've written to shard that should exist on the current node, but the store has - // not actually created this shard, tell it to create it and retry the write - if err == tsdb.ErrShardNotFound { - err = w.TSDBStore.CreateShard(database, retentionPolicy, shard.ID, true) - if err != nil { - w.Logger.Info(fmt.Sprintf("write failed for shard %d: %v", shard.ID, err)) - - atomic.AddInt64(&w.stats.WriteErr, 1) - return err - } - } - err = w.TSDBStore.WriteToShard(shard.ID, points) - if err != nil { - w.Logger.Info(fmt.Sprintf("write failed for shard %d: %v", shard.ID, err)) - atomic.AddInt64(&w.stats.WriteErr, 1) - return err - } - - atomic.AddInt64(&w.stats.WriteOK, 1) - return nil -} diff --git a/vendor/github.com/influxdata/influxdb/coordinator/points_writer_internal_test.go b/vendor/github.com/influxdata/influxdb/coordinator/points_writer_internal_test.go deleted file mode 100644 index ec6a6cac..00000000 --- a/vendor/github.com/influxdata/influxdb/coordinator/points_writer_internal_test.go +++ /dev/null @@ -1,46 +0,0 @@ -package coordinator - -import ( - "testing" - "time" -) - -func TestSgList_ShardGroupAt(t *testing.T) { - base := time.Date(2016, 10, 19, 0, 0, 0, 0, time.UTC) - day := func(n int) time.Time { - return base.Add(time.Duration(24*n) * time.Hour) - } - - list := sgList{ - {ID: 1, StartTime: day(0), EndTime: day(1)}, - {ID: 2, StartTime: day(1), EndTime: day(2)}, - {ID: 3, StartTime: day(2), EndTime: day(3)}, - // SG day 3 to day 4 missing... - {ID: 4, StartTime: day(4), EndTime: day(5)}, - {ID: 5, StartTime: day(5), EndTime: day(6)}, - } - - examples := []struct { - T time.Time - ShardGroupID uint64 // 0 will indicate we don't expect a shard group - }{ - {T: base.Add(-time.Minute), ShardGroupID: 0}, // Before any SG - {T: day(0), ShardGroupID: 1}, - {T: day(0).Add(time.Minute), ShardGroupID: 1}, - {T: day(1), ShardGroupID: 2}, - {T: day(3).Add(time.Minute), ShardGroupID: 0}, // No matching SG - {T: day(5).Add(time.Hour), ShardGroupID: 5}, - } - - for i, example := range examples { - sg := list.ShardGroupAt(example.T) - var id uint64 - if sg != nil { - id = sg.ID - } - - if got, exp := id, example.ShardGroupID; got != exp { - t.Errorf("[Example %d] got %v, expected %v", i+1, got, exp) - } - } -} diff --git a/vendor/github.com/influxdata/influxdb/coordinator/points_writer_test.go b/vendor/github.com/influxdata/influxdb/coordinator/points_writer_test.go deleted file mode 100644 index 86752e9d..00000000 --- a/vendor/github.com/influxdata/influxdb/coordinator/points_writer_test.go +++ /dev/null @@ -1,636 +0,0 @@ -package coordinator_test - -import ( - "fmt" - "reflect" - "sync" - "sync/atomic" - "testing" - "time" - - "github.com/influxdata/influxdb" - "github.com/influxdata/influxdb/coordinator" - "github.com/influxdata/influxdb/models" - "github.com/influxdata/influxdb/services/meta" -) - -// TODO(benbjohnson): Rewrite tests to use cluster_test.MetaClient. - -// Ensures the points writer maps a single point to a single shard. -func TestPointsWriter_MapShards_One(t *testing.T) { - ms := PointsWriterMetaClient{} - rp := NewRetentionPolicy("myp", time.Hour, 3) - - ms.NodeIDFn = func() uint64 { return 1 } - ms.RetentionPolicyFn = func(db, retentionPolicy string) (*meta.RetentionPolicyInfo, error) { - return rp, nil - } - - ms.CreateShardGroupIfNotExistsFn = func(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) { - return &rp.ShardGroups[0], nil - } - - c := coordinator.PointsWriter{MetaClient: ms} - pr := &coordinator.WritePointsRequest{ - Database: "mydb", - RetentionPolicy: "myrp", - } - pr.AddPoint("cpu", 1.0, time.Now(), nil) - - var ( - shardMappings *coordinator.ShardMapping - err error - ) - if shardMappings, err = c.MapShards(pr); err != nil { - t.Fatalf("unexpected an error: %v", err) - } - - if exp := 1; len(shardMappings.Points) != exp { - t.Errorf("MapShards() len mismatch. got %v, exp %v", len(shardMappings.Points), exp) - } -} - -// Ensures the points writer maps to a new shard group when the shard duration -// is changed. -func TestPointsWriter_MapShards_AlterShardDuration(t *testing.T) { - ms := PointsWriterMetaClient{} - rp := NewRetentionPolicy("myp", time.Hour, 3) - - ms.NodeIDFn = func() uint64 { return 1 } - ms.RetentionPolicyFn = func(db, retentionPolicy string) (*meta.RetentionPolicyInfo, error) { - return rp, nil - } - - var ( - i int - now = time.Now() - ) - - ms.CreateShardGroupIfNotExistsFn = func(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) { - sg := []meta.ShardGroupInfo{ - meta.ShardGroupInfo{ - Shards: make([]meta.ShardInfo, 1), - StartTime: now, EndTime: now.Add(rp.Duration).Add(-1), - }, - meta.ShardGroupInfo{ - Shards: make([]meta.ShardInfo, 1), - StartTime: now.Add(time.Hour), EndTime: now.Add(3 * time.Hour).Add(rp.Duration).Add(-1), - }, - }[i] - i++ - return &sg, nil - } - - c := coordinator.NewPointsWriter() - c.MetaClient = ms - - pr := &coordinator.WritePointsRequest{ - Database: "mydb", - RetentionPolicy: "myrp", - } - pr.AddPoint("cpu", 1.0, now, nil) - pr.AddPoint("cpu", 2.0, now.Add(2*time.Second), nil) - - var ( - shardMappings *coordinator.ShardMapping - err error - ) - if shardMappings, err = c.MapShards(pr); err != nil { - t.Fatalf("unexpected an error: %v", err) - } - - if got, exp := len(shardMappings.Points[0]), 2; got != exp { - t.Fatalf("got %d point(s), expected %d", got, exp) - } - - if got, exp := len(shardMappings.Shards), 1; got != exp { - t.Errorf("got %d shard(s), expected %d", got, exp) - } - - // Now we alter the retention policy duration. - rp.ShardGroupDuration = 3 * time.Hour - - pr = &coordinator.WritePointsRequest{ - Database: "mydb", - RetentionPolicy: "myrp", - } - pr.AddPoint("cpu", 1.0, now.Add(2*time.Hour), nil) - - // Point is beyond previous shard group so a new shard group should be - // created. - if shardMappings, err = c.MapShards(pr); err != nil { - t.Fatalf("unexpected an error: %v", err) - } - - // We can check value of i since it's only incremeneted when a shard group - // is created. - if got, exp := i, 2; got != exp { - t.Fatal("new shard group was not created, expected it to be") - } -} - -// Ensures the points writer maps a multiple points across shard group boundaries. -func TestPointsWriter_MapShards_Multiple(t *testing.T) { - ms := PointsWriterMetaClient{} - rp := NewRetentionPolicy("myp", time.Hour, 3) - rp.ShardGroupDuration = time.Hour - AttachShardGroupInfo(rp, []meta.ShardOwner{ - {NodeID: 1}, - {NodeID: 2}, - {NodeID: 3}, - }) - AttachShardGroupInfo(rp, []meta.ShardOwner{ - {NodeID: 1}, - {NodeID: 2}, - {NodeID: 3}, - }) - - ms.NodeIDFn = func() uint64 { return 1 } - ms.RetentionPolicyFn = func(db, retentionPolicy string) (*meta.RetentionPolicyInfo, error) { - return rp, nil - } - - ms.CreateShardGroupIfNotExistsFn = func(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) { - for i, sg := range rp.ShardGroups { - if timestamp.Equal(sg.StartTime) || timestamp.After(sg.StartTime) && timestamp.Before(sg.EndTime) { - return &rp.ShardGroups[i], nil - } - } - panic("should not get here") - } - - c := coordinator.NewPointsWriter() - c.MetaClient = ms - defer c.Close() - pr := &coordinator.WritePointsRequest{ - Database: "mydb", - RetentionPolicy: "myrp", - } - - // Three points that range over the shardGroup duration (1h) and should map to two - // distinct shards - pr.AddPoint("cpu", 1.0, time.Now(), nil) - pr.AddPoint("cpu", 2.0, time.Now().Add(time.Hour), nil) - pr.AddPoint("cpu", 3.0, time.Now().Add(time.Hour+time.Second), nil) - - var ( - shardMappings *coordinator.ShardMapping - err error - ) - if shardMappings, err = c.MapShards(pr); err != nil { - t.Fatalf("unexpected an error: %v", err) - } - - if exp := 2; len(shardMappings.Points) != exp { - t.Errorf("MapShards() len mismatch. got %v, exp %v", len(shardMappings.Points), exp) - } - - for _, points := range shardMappings.Points { - // First shard should have 1 point w/ first point added - if len(points) == 1 && points[0].Time() != pr.Points[0].Time() { - t.Fatalf("MapShards() value mismatch. got %v, exp %v", points[0].Time(), pr.Points[0].Time()) - } - - // Second shard should have the last two points added - if len(points) == 2 && points[0].Time() != pr.Points[1].Time() { - t.Fatalf("MapShards() value mismatch. got %v, exp %v", points[0].Time(), pr.Points[1].Time()) - } - - if len(points) == 2 && points[1].Time() != pr.Points[2].Time() { - t.Fatalf("MapShards() value mismatch. got %v, exp %v", points[1].Time(), pr.Points[2].Time()) - } - } -} - -// Ensures the points writer does not map points beyond the retention policy. -func TestPointsWriter_MapShards_Invalid(t *testing.T) { - ms := PointsWriterMetaClient{} - rp := NewRetentionPolicy("myp", time.Hour, 3) - - ms.RetentionPolicyFn = func(db, retentionPolicy string) (*meta.RetentionPolicyInfo, error) { - return rp, nil - } - - ms.CreateShardGroupIfNotExistsFn = func(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) { - return &rp.ShardGroups[0], nil - } - - c := coordinator.NewPointsWriter() - c.MetaClient = ms - defer c.Close() - pr := &coordinator.WritePointsRequest{ - Database: "mydb", - RetentionPolicy: "myrp", - } - - // Add a point that goes beyond the current retention policy. - pr.AddPoint("cpu", 1.0, time.Now().Add(-2*time.Hour), nil) - - var ( - shardMappings *coordinator.ShardMapping - err error - ) - if shardMappings, err = c.MapShards(pr); err != nil { - t.Fatalf("unexpected an error: %v", err) - } - - if exp := 0; len(shardMappings.Points) != exp { - t.Errorf("MapShards() len mismatch. got %v, exp %v", len(shardMappings.Points), exp) - } -} - -func TestPointsWriter_WritePoints(t *testing.T) { - tests := []struct { - name string - database string - retentionPolicy string - - // the responses returned by each shard write call. node ID 1 = pos 0 - err []error - expErr error - }{ - { - name: "write one success", - database: "mydb", - retentionPolicy: "myrp", - err: []error{nil, nil, nil}, - expErr: nil, - }, - - // Write to non-existent database - { - name: "write to non-existent database", - database: "doesnt_exist", - retentionPolicy: "", - err: []error{nil, nil, nil}, - expErr: fmt.Errorf("database not found: doesnt_exist"), - }, - } - - for _, test := range tests { - - pr := &coordinator.WritePointsRequest{ - Database: test.database, - RetentionPolicy: test.retentionPolicy, - } - - // Ensure that the test shard groups are created before the points - // are created. - ms := NewPointsWriterMetaClient() - - // Three points that range over the shardGroup duration (1h) and should map to two - // distinct shards - pr.AddPoint("cpu", 1.0, time.Now(), nil) - pr.AddPoint("cpu", 2.0, time.Now().Add(time.Hour), nil) - pr.AddPoint("cpu", 3.0, time.Now().Add(time.Hour+time.Second), nil) - - // copy to prevent data race - theTest := test - sm := coordinator.NewShardMapping() - sm.MapPoint( - &meta.ShardInfo{ID: uint64(1), Owners: []meta.ShardOwner{ - {NodeID: 1}, - {NodeID: 2}, - {NodeID: 3}, - }}, - pr.Points[0]) - sm.MapPoint( - &meta.ShardInfo{ID: uint64(2), Owners: []meta.ShardOwner{ - {NodeID: 1}, - {NodeID: 2}, - {NodeID: 3}, - }}, - pr.Points[1]) - sm.MapPoint( - &meta.ShardInfo{ID: uint64(2), Owners: []meta.ShardOwner{ - {NodeID: 1}, - {NodeID: 2}, - {NodeID: 3}, - }}, - pr.Points[2]) - - // Local coordinator.Node ShardWriter - // lock on the write increment since these functions get called in parallel - var mu sync.Mutex - sw := &fakeShardWriter{ - ShardWriteFn: func(shardID, nodeID uint64, points []models.Point) error { - mu.Lock() - defer mu.Unlock() - return theTest.err[int(nodeID)-1] - }, - } - - store := &fakeStore{ - WriteFn: func(shardID uint64, points []models.Point) error { - mu.Lock() - defer mu.Unlock() - return theTest.err[0] - }, - } - - ms.DatabaseFn = func(database string) *meta.DatabaseInfo { - return nil - } - ms.NodeIDFn = func() uint64 { return 1 } - - subPoints := make(chan *coordinator.WritePointsRequest, 1) - sub := Subscriber{} - sub.PointsFn = func() chan<- *coordinator.WritePointsRequest { - return subPoints - } - - c := coordinator.NewPointsWriter() - c.MetaClient = ms - c.ShardWriter = sw - c.TSDBStore = store - c.Subscriber = sub - c.Node = &influxdb.Node{ID: 1} - - c.Open() - defer c.Close() - - err := c.WritePoints(pr.Database, pr.RetentionPolicy, models.ConsistencyLevelOne, pr.Points) - if err == nil && test.expErr != nil { - t.Errorf("PointsWriter.WritePoints(): '%s' error: got %v, exp %v", test.name, err, test.expErr) - } - - if err != nil && test.expErr == nil { - t.Errorf("PointsWriter.WritePoints(): '%s' error: got %v, exp %v", test.name, err, test.expErr) - } - if err != nil && test.expErr != nil && err.Error() != test.expErr.Error() { - t.Errorf("PointsWriter.WritePoints(): '%s' error: got %v, exp %v", test.name, err, test.expErr) - } - if test.expErr == nil { - select { - case p := <-subPoints: - if !reflect.DeepEqual(p, pr) { - t.Errorf("PointsWriter.WritePoints(): '%s' error: unexpected WritePointsRequest got %v, exp %v", test.name, p, pr) - } - default: - t.Errorf("PointsWriter.WritePoints(): '%s' error: Subscriber.Points not called", test.name) - } - } - } -} - -type fakePointsWriter struct { - WritePointsIntoFn func(*coordinator.IntoWriteRequest) error -} - -func (f *fakePointsWriter) WritePointsInto(req *coordinator.IntoWriteRequest) error { - return f.WritePointsIntoFn(req) -} - -func TestBufferedPointsWriter(t *testing.T) { - db := "db0" - rp := "rp0" - capacity := 10000 - - writePointsIntoCnt := 0 - pointsWritten := []models.Point{} - - reset := func() { - writePointsIntoCnt = 0 - pointsWritten = pointsWritten[:0] - } - - fakeWriter := &fakePointsWriter{ - WritePointsIntoFn: func(req *coordinator.IntoWriteRequest) error { - writePointsIntoCnt++ - pointsWritten = append(pointsWritten, req.Points...) - return nil - }, - } - - w := coordinator.NewBufferedPointsWriter(fakeWriter, db, rp, capacity) - - // Test that capacity and length are correct for new buffered writer. - if w.Cap() != capacity { - t.Fatalf("exp %d, got %d", capacity, w.Cap()) - } else if w.Len() != 0 { - t.Fatalf("exp %d, got %d", 0, w.Len()) - } - - // Test flushing an empty buffer. - if err := w.Flush(); err != nil { - t.Fatal(err) - } else if writePointsIntoCnt > 0 { - t.Fatalf("exp 0, got %d", writePointsIntoCnt) - } - - // Test writing zero points. - if err := w.WritePointsInto(&coordinator.IntoWriteRequest{ - Database: db, - RetentionPolicy: rp, - Points: []models.Point{}, - }); err != nil { - t.Fatal(err) - } else if writePointsIntoCnt > 0 { - t.Fatalf("exp 0, got %d", writePointsIntoCnt) - } else if w.Len() > 0 { - t.Fatalf("exp 0, got %d", w.Len()) - } - - // Test writing single large bunch of points points. - req := coordinator.WritePointsRequest{ - Database: db, - RetentionPolicy: rp, - } - - numPoints := int(float64(capacity) * 5.5) - for i := 0; i < numPoints; i++ { - req.AddPoint("cpu", float64(i), time.Now().Add(time.Duration(i)*time.Second), nil) - } - - r := coordinator.IntoWriteRequest(req) - if err := w.WritePointsInto(&r); err != nil { - t.Fatal(err) - } else if writePointsIntoCnt != 5 { - t.Fatalf("exp 5, got %d", writePointsIntoCnt) - } else if w.Len() != capacity/2 { - t.Fatalf("exp %d, got %d", capacity/2, w.Len()) - } else if len(pointsWritten) != numPoints-capacity/2 { - t.Fatalf("exp %d, got %d", numPoints-capacity/2, len(pointsWritten)) - } - - if err := w.Flush(); err != nil { - t.Fatal(err) - } else if writePointsIntoCnt != 6 { - t.Fatalf("exp 6, got %d", writePointsIntoCnt) - } else if w.Len() != 0 { - t.Fatalf("exp 0, got %d", w.Len()) - } else if len(pointsWritten) != numPoints { - t.Fatalf("exp %d, got %d", numPoints, len(pointsWritten)) - } else if !reflect.DeepEqual(r.Points, pointsWritten) { - t.Fatal("points don't match") - } - - reset() - - // Test writing points one at a time. - for i, _ := range r.Points { - if err := w.WritePointsInto(&coordinator.IntoWriteRequest{ - Database: db, - RetentionPolicy: rp, - Points: r.Points[i : i+1], - }); err != nil { - t.Fatal(err) - } - } - - if err := w.Flush(); err != nil { - t.Fatal(err) - } else if writePointsIntoCnt != 6 { - t.Fatalf("exp 6, got %d", writePointsIntoCnt) - } else if w.Len() != 0 { - t.Fatalf("exp 0, got %d", w.Len()) - } else if len(pointsWritten) != numPoints { - t.Fatalf("exp %d, got %d", numPoints, len(pointsWritten)) - } else if !reflect.DeepEqual(r.Points, pointsWritten) { - t.Fatal("points don't match") - } -} - -var shardID uint64 - -type fakeShardWriter struct { - ShardWriteFn func(shardID, nodeID uint64, points []models.Point) error -} - -func (f *fakeShardWriter) WriteShard(shardID, nodeID uint64, points []models.Point) error { - return f.ShardWriteFn(shardID, nodeID, points) -} - -type fakeStore struct { - WriteFn func(shardID uint64, points []models.Point) error - CreateShardfn func(database, retentionPolicy string, shardID uint64, enabled bool) error -} - -func (f *fakeStore) WriteToShard(shardID uint64, points []models.Point) error { - return f.WriteFn(shardID, points) -} - -func (f *fakeStore) CreateShard(database, retentionPolicy string, shardID uint64, enabled bool) error { - return f.CreateShardfn(database, retentionPolicy, shardID, enabled) -} - -func NewPointsWriterMetaClient() *PointsWriterMetaClient { - ms := &PointsWriterMetaClient{} - rp := NewRetentionPolicy("myp", time.Hour, 3) - AttachShardGroupInfo(rp, []meta.ShardOwner{ - {NodeID: 1}, - {NodeID: 2}, - {NodeID: 3}, - }) - AttachShardGroupInfo(rp, []meta.ShardOwner{ - {NodeID: 1}, - {NodeID: 2}, - {NodeID: 3}, - }) - - ms.RetentionPolicyFn = func(db, retentionPolicy string) (*meta.RetentionPolicyInfo, error) { - return rp, nil - } - - ms.CreateShardGroupIfNotExistsFn = func(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) { - for i, sg := range rp.ShardGroups { - if timestamp.Equal(sg.StartTime) || timestamp.After(sg.StartTime) && timestamp.Before(sg.EndTime) { - return &rp.ShardGroups[i], nil - } - } - panic("should not get here") - } - return ms -} - -type PointsWriterMetaClient struct { - NodeIDFn func() uint64 - RetentionPolicyFn func(database, name string) (*meta.RetentionPolicyInfo, error) - CreateShardGroupIfNotExistsFn func(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) - DatabaseFn func(database string) *meta.DatabaseInfo - ShardOwnerFn func(shardID uint64) (string, string, *meta.ShardGroupInfo) -} - -func (m PointsWriterMetaClient) NodeID() uint64 { return m.NodeIDFn() } - -func (m PointsWriterMetaClient) RetentionPolicy(database, name string) (*meta.RetentionPolicyInfo, error) { - return m.RetentionPolicyFn(database, name) -} - -func (m PointsWriterMetaClient) CreateShardGroup(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) { - return m.CreateShardGroupIfNotExistsFn(database, policy, timestamp) -} - -func (m PointsWriterMetaClient) Database(database string) *meta.DatabaseInfo { - return m.DatabaseFn(database) -} - -func (m PointsWriterMetaClient) ShardOwner(shardID uint64) (string, string, *meta.ShardGroupInfo) { - return m.ShardOwnerFn(shardID) -} - -type Subscriber struct { - PointsFn func() chan<- *coordinator.WritePointsRequest -} - -func (s Subscriber) Points() chan<- *coordinator.WritePointsRequest { - return s.PointsFn() -} - -func NewRetentionPolicy(name string, duration time.Duration, nodeCount int) *meta.RetentionPolicyInfo { - shards := []meta.ShardInfo{} - owners := []meta.ShardOwner{} - for i := 1; i <= nodeCount; i++ { - owners = append(owners, meta.ShardOwner{NodeID: uint64(i)}) - } - - // each node is fully replicated with each other - shards = append(shards, meta.ShardInfo{ - ID: nextShardID(), - Owners: owners, - }) - - start := time.Now() - rp := &meta.RetentionPolicyInfo{ - Name: "myrp", - ReplicaN: nodeCount, - Duration: duration, - ShardGroupDuration: duration, - ShardGroups: []meta.ShardGroupInfo{ - meta.ShardGroupInfo{ - ID: nextShardID(), - StartTime: start, - EndTime: start.Add(duration).Add(-1), - Shards: shards, - }, - }, - } - return rp -} - -func AttachShardGroupInfo(rp *meta.RetentionPolicyInfo, owners []meta.ShardOwner) { - var startTime, endTime time.Time - if len(rp.ShardGroups) == 0 { - startTime = time.Now() - } else { - startTime = rp.ShardGroups[len(rp.ShardGroups)-1].StartTime.Add(rp.ShardGroupDuration) - } - endTime = startTime.Add(rp.ShardGroupDuration).Add(-1) - - sh := meta.ShardGroupInfo{ - ID: uint64(len(rp.ShardGroups) + 1), - StartTime: startTime, - EndTime: endTime, - Shards: []meta.ShardInfo{ - meta.ShardInfo{ - ID: nextShardID(), - Owners: owners, - }, - }, - } - rp.ShardGroups = append(rp.ShardGroups, sh) -} - -func nextShardID() uint64 { - return atomic.AddUint64(&shardID, 1) -} diff --git a/vendor/github.com/influxdata/influxdb/coordinator/statement_executor.go b/vendor/github.com/influxdata/influxdb/coordinator/statement_executor.go deleted file mode 100644 index fc211295..00000000 --- a/vendor/github.com/influxdata/influxdb/coordinator/statement_executor.go +++ /dev/null @@ -1,1236 +0,0 @@ -package coordinator - -import ( - "bytes" - "errors" - "fmt" - "io" - "sort" - "strconv" - "time" - - "github.com/influxdata/influxdb" - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/models" - "github.com/influxdata/influxdb/monitor" - "github.com/influxdata/influxdb/services/meta" - "github.com/influxdata/influxdb/tsdb" -) - -var ErrDatabaseNameRequired = errors.New("database name required") - -type pointsWriter interface { - WritePointsInto(*IntoWriteRequest) error -} - -// StatementExecutor executes a statement in the query. -type StatementExecutor struct { - MetaClient MetaClient - - // TaskManager holds the StatementExecutor that handles task-related commands. - TaskManager influxql.StatementExecutor - - // TSDB storage for local node. - TSDBStore TSDBStore - - // Holds monitoring data for SHOW STATS and SHOW DIAGNOSTICS. - Monitor *monitor.Monitor - - // Used for rewriting points back into system for SELECT INTO statements. - PointsWriter pointsWriter - - // Select statement limits - MaxSelectPointN int - MaxSelectSeriesN int - MaxSelectBucketsN int -} - -func (e *StatementExecutor) ExecuteStatement(stmt influxql.Statement, ctx influxql.ExecutionContext) error { - // Select statements are handled separately so that they can be streamed. - if stmt, ok := stmt.(*influxql.SelectStatement); ok { - return e.executeSelectStatement(stmt, &ctx) - } - - var rows models.Rows - var messages []*influxql.Message - var err error - switch stmt := stmt.(type) { - case *influxql.AlterRetentionPolicyStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeAlterRetentionPolicyStatement(stmt) - case *influxql.CreateContinuousQueryStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeCreateContinuousQueryStatement(stmt) - case *influxql.CreateDatabaseStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeCreateDatabaseStatement(stmt) - case *influxql.CreateRetentionPolicyStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeCreateRetentionPolicyStatement(stmt) - case *influxql.CreateSubscriptionStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeCreateSubscriptionStatement(stmt) - case *influxql.CreateUserStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeCreateUserStatement(stmt) - case *influxql.DeleteSeriesStatement: - err = e.executeDeleteSeriesStatement(stmt, ctx.Database) - case *influxql.DropContinuousQueryStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeDropContinuousQueryStatement(stmt) - case *influxql.DropDatabaseStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeDropDatabaseStatement(stmt) - case *influxql.DropMeasurementStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeDropMeasurementStatement(stmt, ctx.Database) - case *influxql.DropSeriesStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeDropSeriesStatement(stmt, ctx.Database) - case *influxql.DropRetentionPolicyStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeDropRetentionPolicyStatement(stmt) - case *influxql.DropShardStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeDropShardStatement(stmt) - case *influxql.DropSubscriptionStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeDropSubscriptionStatement(stmt) - case *influxql.DropUserStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeDropUserStatement(stmt) - case *influxql.GrantStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeGrantStatement(stmt) - case *influxql.GrantAdminStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeGrantAdminStatement(stmt) - case *influxql.RevokeStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeRevokeStatement(stmt) - case *influxql.RevokeAdminStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeRevokeAdminStatement(stmt) - case *influxql.ShowContinuousQueriesStatement: - rows, err = e.executeShowContinuousQueriesStatement(stmt) - case *influxql.ShowDatabasesStatement: - rows, err = e.executeShowDatabasesStatement(stmt) - case *influxql.ShowDiagnosticsStatement: - rows, err = e.executeShowDiagnosticsStatement(stmt) - case *influxql.ShowGrantsForUserStatement: - rows, err = e.executeShowGrantsForUserStatement(stmt) - case *influxql.ShowMeasurementsStatement: - return e.executeShowMeasurementsStatement(stmt, &ctx) - case *influxql.ShowRetentionPoliciesStatement: - rows, err = e.executeShowRetentionPoliciesStatement(stmt) - case *influxql.ShowShardsStatement: - rows, err = e.executeShowShardsStatement(stmt) - case *influxql.ShowShardGroupsStatement: - rows, err = e.executeShowShardGroupsStatement(stmt) - case *influxql.ShowStatsStatement: - rows, err = e.executeShowStatsStatement(stmt) - case *influxql.ShowSubscriptionsStatement: - rows, err = e.executeShowSubscriptionsStatement(stmt) - case *influxql.ShowTagValuesStatement: - return e.executeShowTagValues(stmt, &ctx) - case *influxql.ShowUsersStatement: - rows, err = e.executeShowUsersStatement(stmt) - case *influxql.SetPasswordUserStatement: - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - err = e.executeSetPasswordUserStatement(stmt) - case *influxql.ShowQueriesStatement, *influxql.KillQueryStatement: - // Send query related statements to the task manager. - return e.TaskManager.ExecuteStatement(stmt, ctx) - default: - return influxql.ErrInvalidQuery - } - - if err != nil { - return err - } - - return ctx.Send(&influxql.Result{ - StatementID: ctx.StatementID, - Series: rows, - Messages: messages, - }) -} - -func (e *StatementExecutor) executeAlterRetentionPolicyStatement(stmt *influxql.AlterRetentionPolicyStatement) error { - rpu := &meta.RetentionPolicyUpdate{ - Duration: stmt.Duration, - ReplicaN: stmt.Replication, - ShardGroupDuration: stmt.ShardGroupDuration, - } - - // Update the retention policy. - if err := e.MetaClient.UpdateRetentionPolicy(stmt.Database, stmt.Name, rpu, stmt.Default); err != nil { - return err - } - return nil -} - -func (e *StatementExecutor) executeCreateContinuousQueryStatement(q *influxql.CreateContinuousQueryStatement) error { - // Verify that retention policies exist. - var err error - verifyRPFn := func(n influxql.Node) { - if err != nil { - return - } - switch m := n.(type) { - case *influxql.Measurement: - var rp *meta.RetentionPolicyInfo - if rp, err = e.MetaClient.RetentionPolicy(m.Database, m.RetentionPolicy); err != nil { - return - } else if rp == nil { - err = fmt.Errorf("%s: %s.%s", meta.ErrRetentionPolicyNotFound, m.Database, m.RetentionPolicy) - } - default: - return - } - } - - influxql.WalkFunc(q, verifyRPFn) - - if err != nil { - return err - } - - return e.MetaClient.CreateContinuousQuery(q.Database, q.Name, q.String()) -} - -func (e *StatementExecutor) executeCreateDatabaseStatement(stmt *influxql.CreateDatabaseStatement) error { - if !stmt.RetentionPolicyCreate { - _, err := e.MetaClient.CreateDatabase(stmt.Name) - return err - } - - spec := meta.RetentionPolicySpec{ - Name: stmt.RetentionPolicyName, - Duration: stmt.RetentionPolicyDuration, - ReplicaN: stmt.RetentionPolicyReplication, - ShardGroupDuration: stmt.RetentionPolicyShardGroupDuration, - } - _, err := e.MetaClient.CreateDatabaseWithRetentionPolicy(stmt.Name, &spec) - return err -} - -func (e *StatementExecutor) executeCreateRetentionPolicyStatement(stmt *influxql.CreateRetentionPolicyStatement) error { - spec := meta.RetentionPolicySpec{ - Name: stmt.Name, - Duration: &stmt.Duration, - ReplicaN: &stmt.Replication, - ShardGroupDuration: stmt.ShardGroupDuration, - } - - // Create new retention policy. - _, err := e.MetaClient.CreateRetentionPolicy(stmt.Database, &spec, stmt.Default) - if err != nil { - return err - } - - return nil -} - -func (e *StatementExecutor) executeCreateSubscriptionStatement(q *influxql.CreateSubscriptionStatement) error { - return e.MetaClient.CreateSubscription(q.Database, q.RetentionPolicy, q.Name, q.Mode, q.Destinations) -} - -func (e *StatementExecutor) executeCreateUserStatement(q *influxql.CreateUserStatement) error { - _, err := e.MetaClient.CreateUser(q.Name, q.Password, q.Admin) - return err -} - -func (e *StatementExecutor) executeDeleteSeriesStatement(stmt *influxql.DeleteSeriesStatement, database string) error { - if dbi := e.MetaClient.Database(database); dbi == nil { - return influxql.ErrDatabaseNotFound(database) - } - - // Convert "now()" to current time. - stmt.Condition = influxql.Reduce(stmt.Condition, &influxql.NowValuer{Now: time.Now().UTC()}) - - // Locally delete the series. - return e.TSDBStore.DeleteSeries(database, stmt.Sources, stmt.Condition) -} - -func (e *StatementExecutor) executeDropContinuousQueryStatement(q *influxql.DropContinuousQueryStatement) error { - return e.MetaClient.DropContinuousQuery(q.Database, q.Name) -} - -// executeDropDatabaseStatement drops a database from the cluster. -// It does not return an error if the database was not found on any of -// the nodes, or in the Meta store. -func (e *StatementExecutor) executeDropDatabaseStatement(stmt *influxql.DropDatabaseStatement) error { - // Locally delete the datababse. - if err := e.TSDBStore.DeleteDatabase(stmt.Name); err != nil { - return err - } - - // Remove the database from the Meta Store. - return e.MetaClient.DropDatabase(stmt.Name) -} - -func (e *StatementExecutor) executeDropMeasurementStatement(stmt *influxql.DropMeasurementStatement, database string) error { - if dbi := e.MetaClient.Database(database); dbi == nil { - return influxql.ErrDatabaseNotFound(database) - } - - // Locally drop the measurement - return e.TSDBStore.DeleteMeasurement(database, stmt.Name) -} - -func (e *StatementExecutor) executeDropSeriesStatement(stmt *influxql.DropSeriesStatement, database string) error { - if dbi := e.MetaClient.Database(database); dbi == nil { - return influxql.ErrDatabaseNotFound(database) - } - - // Check for time in WHERE clause (not supported). - if influxql.HasTimeExpr(stmt.Condition) { - return errors.New("DROP SERIES doesn't support time in WHERE clause") - } - - // Locally drop the series. - return e.TSDBStore.DeleteSeries(database, stmt.Sources, stmt.Condition) -} - -func (e *StatementExecutor) executeDropShardStatement(stmt *influxql.DropShardStatement) error { - // Locally delete the shard. - if err := e.TSDBStore.DeleteShard(stmt.ID); err != nil { - return err - } - - // Remove the shard reference from the Meta Store. - return e.MetaClient.DropShard(stmt.ID) -} - -func (e *StatementExecutor) executeDropRetentionPolicyStatement(stmt *influxql.DropRetentionPolicyStatement) error { - // Locally drop the retention policy. - if err := e.TSDBStore.DeleteRetentionPolicy(stmt.Database, stmt.Name); err != nil { - return err - } - - return e.MetaClient.DropRetentionPolicy(stmt.Database, stmt.Name) -} - -func (e *StatementExecutor) executeDropSubscriptionStatement(q *influxql.DropSubscriptionStatement) error { - return e.MetaClient.DropSubscription(q.Database, q.RetentionPolicy, q.Name) -} - -func (e *StatementExecutor) executeDropUserStatement(q *influxql.DropUserStatement) error { - return e.MetaClient.DropUser(q.Name) -} - -func (e *StatementExecutor) executeGrantStatement(stmt *influxql.GrantStatement) error { - return e.MetaClient.SetPrivilege(stmt.User, stmt.On, stmt.Privilege) -} - -func (e *StatementExecutor) executeGrantAdminStatement(stmt *influxql.GrantAdminStatement) error { - return e.MetaClient.SetAdminPrivilege(stmt.User, true) -} - -func (e *StatementExecutor) executeRevokeStatement(stmt *influxql.RevokeStatement) error { - priv := influxql.NoPrivileges - - // Revoking all privileges means there's no need to look at existing user privileges. - if stmt.Privilege != influxql.AllPrivileges { - p, err := e.MetaClient.UserPrivilege(stmt.User, stmt.On) - if err != nil { - return err - } - // Bit clear (AND NOT) the user's privilege with the revoked privilege. - priv = *p &^ stmt.Privilege - } - - return e.MetaClient.SetPrivilege(stmt.User, stmt.On, priv) -} - -func (e *StatementExecutor) executeRevokeAdminStatement(stmt *influxql.RevokeAdminStatement) error { - return e.MetaClient.SetAdminPrivilege(stmt.User, false) -} - -func (e *StatementExecutor) executeSetPasswordUserStatement(q *influxql.SetPasswordUserStatement) error { - return e.MetaClient.UpdateUser(q.Name, q.Password) -} - -func (e *StatementExecutor) executeSelectStatement(stmt *influxql.SelectStatement, ctx *influxql.ExecutionContext) error { - itrs, stmt, err := e.createIterators(stmt, ctx) - if err != nil { - return err - } - - // Generate a row emitter from the iterator set. - em := influxql.NewEmitter(itrs, stmt.TimeAscending(), ctx.ChunkSize) - em.Columns = stmt.ColumnNames() - em.OmitTime = stmt.OmitTime - defer em.Close() - - // Emit rows to the results channel. - var writeN int64 - var emitted bool - - var pointsWriter *BufferedPointsWriter - if stmt.Target != nil { - pointsWriter = NewBufferedPointsWriter(e.PointsWriter, stmt.Target.Measurement.Database, stmt.Target.Measurement.RetentionPolicy, 10000) - } - - for { - row, partial, err := em.Emit() - if err != nil { - return err - } else if row == nil { - // Check if the query was interrupted while emitting. - select { - case <-ctx.InterruptCh: - return influxql.ErrQueryInterrupted - default: - } - break - } - - // Write points back into system for INTO statements. - if stmt.Target != nil { - if err := e.writeInto(pointsWriter, stmt, row); err != nil { - return err - } - writeN += int64(len(row.Values)) - continue - } - - result := &influxql.Result{ - StatementID: ctx.StatementID, - Series: []*models.Row{row}, - Partial: partial, - } - - // Send results or exit if closing. - if err := ctx.Send(result); err != nil { - return err - } - - emitted = true - } - - // Flush remaining points and emit write count if an INTO statement. - if stmt.Target != nil { - if err := pointsWriter.Flush(); err != nil { - return err - } - - var messages []*influxql.Message - if ctx.ReadOnly { - messages = append(messages, influxql.ReadOnlyWarning(stmt.String())) - } - - return ctx.Send(&influxql.Result{ - StatementID: ctx.StatementID, - Messages: messages, - Series: []*models.Row{{ - Name: "result", - Columns: []string{"time", "written"}, - Values: [][]interface{}{{time.Unix(0, 0).UTC(), writeN}}, - }}, - }) - } - - // Always emit at least one result. - if !emitted { - return ctx.Send(&influxql.Result{ - StatementID: ctx.StatementID, - Series: make([]*models.Row, 0), - }) - } - - return nil -} - -func (e *StatementExecutor) createIterators(stmt *influxql.SelectStatement, ctx *influxql.ExecutionContext) ([]influxql.Iterator, *influxql.SelectStatement, error) { - // It is important to "stamp" this time so that everywhere we evaluate `now()` in the statement is EXACTLY the same `now` - now := time.Now().UTC() - opt := influxql.SelectOptions{ - InterruptCh: ctx.InterruptCh, - NodeID: ctx.ExecutionOptions.NodeID, - MaxSeriesN: e.MaxSelectSeriesN, - } - - // Replace instances of "now()" with the current time, and check the resultant times. - nowValuer := influxql.NowValuer{Now: now} - stmt.Condition = influxql.Reduce(stmt.Condition, &nowValuer) - // Replace instances of "now()" with the current time in the dimensions. - for _, d := range stmt.Dimensions { - d.Expr = influxql.Reduce(d.Expr, &nowValuer) - } - - var err error - opt.MinTime, opt.MaxTime, err = influxql.TimeRange(stmt.Condition) - if err != nil { - return nil, stmt, err - } - - if opt.MaxTime.IsZero() { - // In the case that we're executing a meta query where the user cannot - // specify a time condition, then we expand the default max time - // to the maximum possible value, to ensure that data where all points - // are in the future are returned. - if influxql.Sources(stmt.Sources).HasSystemSource() { - opt.MaxTime = time.Unix(0, influxql.MaxTime).UTC() - } else { - if interval, err := stmt.GroupByInterval(); err != nil { - return nil, stmt, err - } else if interval > 0 { - opt.MaxTime = now - } else { - opt.MaxTime = time.Unix(0, influxql.MaxTime).UTC() - } - } - } - if opt.MinTime.IsZero() { - opt.MinTime = time.Unix(0, influxql.MinTime).UTC() - } - - // Convert DISTINCT into a call. - stmt.RewriteDistinct() - - // Remove "time" from fields list. - stmt.RewriteTimeFields() - - // Rewrite any regex conditions that could make use of the index. - stmt.RewriteRegexConditions() - - // Create an iterator creator based on the shards in the cluster. - ic, err := e.iteratorCreator(stmt, &opt) - if err != nil { - return nil, stmt, err - } - - // Expand regex sources to their actual source names. - if stmt.Sources.HasRegex() { - sources, err := ic.ExpandSources(stmt.Sources) - if err != nil { - return nil, stmt, err - } - stmt.Sources = sources - } - - // Rewrite wildcards, if any exist. - tmp, err := stmt.RewriteFields(ic) - if err != nil { - return nil, stmt, err - } - stmt = tmp - - if e.MaxSelectBucketsN > 0 && !stmt.IsRawQuery { - interval, err := stmt.GroupByInterval() - if err != nil { - return nil, stmt, err - } - - if interval > 0 { - // Determine the start and end time matched to the interval (may not match the actual times). - min := opt.MinTime.Truncate(interval) - max := opt.MaxTime.Truncate(interval).Add(interval) - - // Determine the number of buckets by finding the time span and dividing by the interval. - buckets := int64(max.Sub(min)) / int64(interval) - if int(buckets) > e.MaxSelectBucketsN { - return nil, stmt, fmt.Errorf("max-select-buckets limit exceeded: (%d/%d)", buckets, e.MaxSelectBucketsN) - } - } - } - - // Create a set of iterators from a selection. - itrs, err := influxql.Select(stmt, ic, &opt) - if err != nil { - return nil, stmt, err - } - - if e.MaxSelectPointN > 0 { - monitor := influxql.PointLimitMonitor(itrs, influxql.DefaultStatsInterval, e.MaxSelectPointN) - ctx.Query.Monitor(monitor) - } - return itrs, stmt, nil -} - -// iteratorCreator returns a new instance of IteratorCreator based on stmt. -func (e *StatementExecutor) iteratorCreator(stmt *influxql.SelectStatement, opt *influxql.SelectOptions) (influxql.IteratorCreator, error) { - // Retrieve a list of shard IDs. - shards, err := e.MetaClient.ShardsByTimeRange(stmt.Sources, opt.MinTime, opt.MaxTime) - if err != nil { - return nil, err - } - return e.TSDBStore.IteratorCreator(shards, opt) -} - -func (e *StatementExecutor) executeShowContinuousQueriesStatement(stmt *influxql.ShowContinuousQueriesStatement) (models.Rows, error) { - dis := e.MetaClient.Databases() - - rows := []*models.Row{} - for _, di := range dis { - row := &models.Row{Columns: []string{"name", "query"}, Name: di.Name} - for _, cqi := range di.ContinuousQueries { - row.Values = append(row.Values, []interface{}{cqi.Name, cqi.Query}) - } - rows = append(rows, row) - } - return rows, nil -} - -func (e *StatementExecutor) executeShowDatabasesStatement(q *influxql.ShowDatabasesStatement) (models.Rows, error) { - dis := e.MetaClient.Databases() - - row := &models.Row{Name: "databases", Columns: []string{"name"}} - for _, di := range dis { - row.Values = append(row.Values, []interface{}{di.Name}) - } - return []*models.Row{row}, nil -} - -func (e *StatementExecutor) executeShowDiagnosticsStatement(stmt *influxql.ShowDiagnosticsStatement) (models.Rows, error) { - diags, err := e.Monitor.Diagnostics() - if err != nil { - return nil, err - } - - // Get a sorted list of diagnostics keys. - sortedKeys := make([]string, 0, len(diags)) - for k := range diags { - sortedKeys = append(sortedKeys, k) - } - sort.Strings(sortedKeys) - - rows := make([]*models.Row, 0, len(diags)) - for _, k := range sortedKeys { - if stmt.Module != "" && k != stmt.Module { - continue - } - - row := &models.Row{Name: k} - - row.Columns = diags[k].Columns - row.Values = diags[k].Rows - rows = append(rows, row) - } - return rows, nil -} - -func (e *StatementExecutor) executeShowGrantsForUserStatement(q *influxql.ShowGrantsForUserStatement) (models.Rows, error) { - priv, err := e.MetaClient.UserPrivileges(q.Name) - if err != nil { - return nil, err - } - - row := &models.Row{Columns: []string{"database", "privilege"}} - for d, p := range priv { - row.Values = append(row.Values, []interface{}{d, p.String()}) - } - return []*models.Row{row}, nil -} - -func (e *StatementExecutor) executeShowMeasurementsStatement(q *influxql.ShowMeasurementsStatement, ctx *influxql.ExecutionContext) error { - if q.Database == "" { - return ErrDatabaseNameRequired - } - - measurements, err := e.TSDBStore.Measurements(q.Database, q.Condition) - if err != nil || len(measurements) == 0 { - return ctx.Send(&influxql.Result{ - StatementID: ctx.StatementID, - Err: err, - }) - } - - if q.Offset > 0 { - if q.Offset >= len(measurements) { - measurements = nil - } else { - measurements = measurements[q.Offset:] - } - } - - if q.Limit > 0 { - if q.Limit < len(measurements) { - measurements = measurements[:q.Limit] - } - } - - values := make([][]interface{}, len(measurements)) - for i, m := range measurements { - values[i] = []interface{}{m} - } - - if len(values) == 0 { - return ctx.Send(&influxql.Result{ - StatementID: ctx.StatementID, - }) - } - - return ctx.Send(&influxql.Result{ - StatementID: ctx.StatementID, - Series: []*models.Row{{ - Name: "measurements", - Columns: []string{"name"}, - Values: values, - }}, - }) -} - -func (e *StatementExecutor) executeShowRetentionPoliciesStatement(q *influxql.ShowRetentionPoliciesStatement) (models.Rows, error) { - if q.Database == "" { - return nil, ErrDatabaseNameRequired - } - - di := e.MetaClient.Database(q.Database) - if di == nil { - return nil, influxdb.ErrDatabaseNotFound(q.Database) - } - - row := &models.Row{Columns: []string{"name", "duration", "shardGroupDuration", "replicaN", "default"}} - for _, rpi := range di.RetentionPolicies { - row.Values = append(row.Values, []interface{}{rpi.Name, rpi.Duration.String(), rpi.ShardGroupDuration.String(), rpi.ReplicaN, di.DefaultRetentionPolicy == rpi.Name}) - } - return []*models.Row{row}, nil -} - -func (e *StatementExecutor) executeShowShardsStatement(stmt *influxql.ShowShardsStatement) (models.Rows, error) { - dis := e.MetaClient.Databases() - - rows := []*models.Row{} - for _, di := range dis { - row := &models.Row{Columns: []string{"id", "database", "retention_policy", "shard_group", "start_time", "end_time", "expiry_time", "owners"}, Name: di.Name} - for _, rpi := range di.RetentionPolicies { - for _, sgi := range rpi.ShardGroups { - // Shards associated with deleted shard groups are effectively deleted. - // Don't list them. - if sgi.Deleted() { - continue - } - - for _, si := range sgi.Shards { - ownerIDs := make([]uint64, len(si.Owners)) - for i, owner := range si.Owners { - ownerIDs[i] = owner.NodeID - } - - row.Values = append(row.Values, []interface{}{ - si.ID, - di.Name, - rpi.Name, - sgi.ID, - sgi.StartTime.UTC().Format(time.RFC3339), - sgi.EndTime.UTC().Format(time.RFC3339), - sgi.EndTime.Add(rpi.Duration).UTC().Format(time.RFC3339), - joinUint64(ownerIDs), - }) - } - } - } - rows = append(rows, row) - } - return rows, nil -} - -func (e *StatementExecutor) executeShowShardGroupsStatement(stmt *influxql.ShowShardGroupsStatement) (models.Rows, error) { - dis := e.MetaClient.Databases() - - row := &models.Row{Columns: []string{"id", "database", "retention_policy", "start_time", "end_time", "expiry_time"}, Name: "shard groups"} - for _, di := range dis { - for _, rpi := range di.RetentionPolicies { - for _, sgi := range rpi.ShardGroups { - // Shards associated with deleted shard groups are effectively deleted. - // Don't list them. - if sgi.Deleted() { - continue - } - - row.Values = append(row.Values, []interface{}{ - sgi.ID, - di.Name, - rpi.Name, - sgi.StartTime.UTC().Format(time.RFC3339), - sgi.EndTime.UTC().Format(time.RFC3339), - sgi.EndTime.Add(rpi.Duration).UTC().Format(time.RFC3339), - }) - } - } - } - - return []*models.Row{row}, nil -} - -func (e *StatementExecutor) executeShowStatsStatement(stmt *influxql.ShowStatsStatement) (models.Rows, error) { - stats, err := e.Monitor.Statistics(nil) - if err != nil { - return nil, err - } - - var rows []*models.Row - for _, stat := range stats { - if stmt.Module != "" && stat.Name != stmt.Module { - continue - } - row := &models.Row{Name: stat.Name, Tags: stat.Tags} - - values := make([]interface{}, 0, len(stat.Values)) - for _, k := range stat.ValueNames() { - row.Columns = append(row.Columns, k) - values = append(values, stat.Values[k]) - } - row.Values = [][]interface{}{values} - rows = append(rows, row) - } - return rows, nil -} - -func (e *StatementExecutor) executeShowSubscriptionsStatement(stmt *influxql.ShowSubscriptionsStatement) (models.Rows, error) { - dis := e.MetaClient.Databases() - - rows := []*models.Row{} - for _, di := range dis { - row := &models.Row{Columns: []string{"retention_policy", "name", "mode", "destinations"}, Name: di.Name} - for _, rpi := range di.RetentionPolicies { - for _, si := range rpi.Subscriptions { - row.Values = append(row.Values, []interface{}{rpi.Name, si.Name, si.Mode, si.Destinations}) - } - } - if len(row.Values) > 0 { - rows = append(rows, row) - } - } - return rows, nil -} - -func (e *StatementExecutor) executeShowTagValues(q *influxql.ShowTagValuesStatement, ctx *influxql.ExecutionContext) error { - if q.Database == "" { - return ErrDatabaseNameRequired - } - - tagValues, err := e.TSDBStore.TagValues(q.Database, q.Condition) - if err != nil { - return ctx.Send(&influxql.Result{ - StatementID: ctx.StatementID, - Err: err, - }) - } - - emitted := false - for _, m := range tagValues { - values := m.Values - - if q.Offset > 0 { - if q.Offset >= len(values) { - values = nil - } else { - values = values[q.Offset:] - } - } - - if q.Limit > 0 { - if q.Limit < len(values) { - values = values[:q.Limit] - } - } - - if len(values) == 0 { - continue - } - - row := &models.Row{ - Name: m.Measurement, - Columns: []string{"key", "value"}, - Values: make([][]interface{}, len(values)), - } - for i, v := range values { - row.Values[i] = []interface{}{v.Key, v.Value} - } - - if err := ctx.Send(&influxql.Result{ - StatementID: ctx.StatementID, - Series: []*models.Row{row}, - }); err != nil { - return err - } - emitted = true - } - - // Ensure at least one result is emitted. - if !emitted { - return ctx.Send(&influxql.Result{ - StatementID: ctx.StatementID, - }) - } - return nil -} - -func (e *StatementExecutor) executeShowUsersStatement(q *influxql.ShowUsersStatement) (models.Rows, error) { - row := &models.Row{Columns: []string{"user", "admin"}} - for _, ui := range e.MetaClient.Users() { - row.Values = append(row.Values, []interface{}{ui.Name, ui.Admin}) - } - return []*models.Row{row}, nil -} - -type BufferedPointsWriter struct { - w pointsWriter - buf []models.Point - database string - retentionPolicy string -} - -func NewBufferedPointsWriter(w pointsWriter, database, retentionPolicy string, capacity int) *BufferedPointsWriter { - return &BufferedPointsWriter{ - w: w, - buf: make([]models.Point, 0, capacity), - database: database, - retentionPolicy: retentionPolicy, - } -} - -func (w *BufferedPointsWriter) WritePointsInto(req *IntoWriteRequest) error { - // Make sure we're buffering points only for the expected destination. - if req.Database != w.database || req.RetentionPolicy != w.retentionPolicy { - return fmt.Errorf("writer for %s.%s can't write into %s.%s", w.database, w.retentionPolicy, req.Database, req.RetentionPolicy) - } - - for i := 0; i < len(req.Points); { - // Get the available space in the buffer. - avail := cap(w.buf) - len(w.buf) - - // Calculate number of points to copy into the buffer. - n := len(req.Points[i:]) - if n > avail { - n = avail - } - - // Copy points into buffer. - w.buf = append(w.buf, req.Points[i:n+i]...) - - // Advance the index by number of points copied. - i += n - - // If buffer is full, flush points to underlying writer. - if len(w.buf) == cap(w.buf) { - if err := w.Flush(); err != nil { - return err - } - } - } - - return nil -} - -// Flush writes all buffered points to the underlying writer. -func (w *BufferedPointsWriter) Flush() error { - if len(w.buf) == 0 { - return nil - } - - if err := w.w.WritePointsInto(&IntoWriteRequest{ - Database: w.database, - RetentionPolicy: w.retentionPolicy, - Points: w.buf, - }); err != nil { - return err - } - - // Clear the buffer. - w.buf = w.buf[:0] - - return nil -} - -// Len returns the number of points buffered. -func (w *BufferedPointsWriter) Len() int { return len(w.buf) } - -// Cap returns the capacity (in points) of the buffer. -func (w *BufferedPointsWriter) Cap() int { return cap(w.buf) } - -func (e *StatementExecutor) writeInto(w pointsWriter, stmt *influxql.SelectStatement, row *models.Row) error { - if stmt.Target.Measurement.Database == "" { - return errNoDatabaseInTarget - } - - // It might seem a bit weird that this is where we do this, since we will have to - // convert rows back to points. The Executors (both aggregate and raw) are complex - // enough that changing them to write back to the DB is going to be clumsy - // - // it might seem weird to have the write be in the QueryExecutor, but the interweaving of - // limitedRowWriter and ExecuteAggregate/Raw makes it ridiculously hard to make sure that the - // results will be the same as when queried normally. - name := stmt.Target.Measurement.Name - if name == "" { - name = row.Name - } - - points, err := convertRowToPoints(name, row) - if err != nil { - return err - } - - if err := w.WritePointsInto(&IntoWriteRequest{ - Database: stmt.Target.Measurement.Database, - RetentionPolicy: stmt.Target.Measurement.RetentionPolicy, - Points: points, - }); err != nil { - return err - } - - return nil -} - -var errNoDatabaseInTarget = errors.New("no database in target") - -// convertRowToPoints will convert a query result Row into Points that can be written back in. -func convertRowToPoints(measurementName string, row *models.Row) ([]models.Point, error) { - // figure out which parts of the result are the time and which are the fields - timeIndex := -1 - fieldIndexes := make(map[string]int) - for i, c := range row.Columns { - if c == "time" { - timeIndex = i - } else { - fieldIndexes[c] = i - } - } - - if timeIndex == -1 { - return nil, errors.New("error finding time index in result") - } - - points := make([]models.Point, 0, len(row.Values)) - for _, v := range row.Values { - vals := make(map[string]interface{}) - for fieldName, fieldIndex := range fieldIndexes { - val := v[fieldIndex] - if val != nil { - vals[fieldName] = v[fieldIndex] - } - } - - p, err := models.NewPoint(measurementName, models.NewTags(row.Tags), vals, v[timeIndex].(time.Time)) - if err != nil { - // Drop points that can't be stored - continue - } - - points = append(points, p) - } - - return points, nil -} - -// NormalizeStatement adds a default database and policy to the measurements in statement. -func (e *StatementExecutor) NormalizeStatement(stmt influxql.Statement, defaultDatabase string) (err error) { - influxql.WalkFunc(stmt, func(node influxql.Node) { - if err != nil { - return - } - switch node := node.(type) { - case *influxql.ShowRetentionPoliciesStatement: - if node.Database == "" { - node.Database = defaultDatabase - } - case *influxql.ShowMeasurementsStatement: - if node.Database == "" { - node.Database = defaultDatabase - } - case *influxql.ShowTagValuesStatement: - if node.Database == "" { - node.Database = defaultDatabase - } - case *influxql.Measurement: - switch stmt.(type) { - case *influxql.DropSeriesStatement, *influxql.DeleteSeriesStatement: - // DB and RP not supported by these statements so don't rewrite into invalid - // statements - default: - err = e.normalizeMeasurement(node, defaultDatabase) - } - } - }) - return -} - -func (e *StatementExecutor) normalizeMeasurement(m *influxql.Measurement, defaultDatabase string) error { - // Targets (measurements in an INTO clause) can have blank names, which means it will be - // the same as the measurement name it came from in the FROM clause. - if !m.IsTarget && m.Name == "" && m.Regex == nil { - return errors.New("invalid measurement") - } - - // Measurement does not have an explicit database? Insert default. - if m.Database == "" { - m.Database = defaultDatabase - } - - // The database must now be specified by this point. - if m.Database == "" { - return ErrDatabaseNameRequired - } - - // Find database. - di := e.MetaClient.Database(m.Database) - if di == nil { - return influxdb.ErrDatabaseNotFound(m.Database) - } - - // If no retention policy was specified, use the default. - if m.RetentionPolicy == "" { - if di.DefaultRetentionPolicy == "" { - return fmt.Errorf("default retention policy not set for: %s", di.Name) - } - m.RetentionPolicy = di.DefaultRetentionPolicy - } - - return nil -} - -// IntoWriteRequest is a partial copy of cluster.WriteRequest -type IntoWriteRequest struct { - Database string - RetentionPolicy string - Points []models.Point -} - -// TSDBStore is an interface for accessing the time series data store. -type TSDBStore interface { - CreateShard(database, policy string, shardID uint64, enabled bool) error - WriteToShard(shardID uint64, points []models.Point) error - - RestoreShard(id uint64, r io.Reader) error - BackupShard(id uint64, since time.Time, w io.Writer) error - - DeleteDatabase(name string) error - DeleteMeasurement(database, name string) error - DeleteRetentionPolicy(database, name string) error - DeleteSeries(database string, sources []influxql.Source, condition influxql.Expr) error - DeleteShard(id uint64) error - IteratorCreator(shards []meta.ShardInfo, opt *influxql.SelectOptions) (influxql.IteratorCreator, error) - - Measurements(database string, cond influxql.Expr) ([]string, error) - TagValues(database string, cond influxql.Expr) ([]tsdb.TagValues, error) -} - -type LocalTSDBStore struct { - *tsdb.Store -} - -func (s LocalTSDBStore) IteratorCreator(shards []meta.ShardInfo, opt *influxql.SelectOptions) (influxql.IteratorCreator, error) { - shardIDs := make([]uint64, len(shards)) - for i, sh := range shards { - shardIDs[i] = sh.ID - } - return s.Store.IteratorCreator(shardIDs, opt) -} - -// ShardIteratorCreator is an interface for creating an IteratorCreator to access a specific shard. -type ShardIteratorCreator interface { - ShardIteratorCreator(id uint64) influxql.IteratorCreator -} - -// joinUint64 returns a comma-delimited string of uint64 numbers. -func joinUint64(a []uint64) string { - var buf bytes.Buffer - for i, x := range a { - buf.WriteString(strconv.FormatUint(x, 10)) - if i < len(a)-1 { - buf.WriteRune(',') - } - } - return buf.String() -} - -// stringSet represents a set of strings. -type stringSet map[string]struct{} - -// newStringSet returns an empty stringSet. -func newStringSet() stringSet { - return make(map[string]struct{}) -} - -// add adds strings to the set. -func (s stringSet) add(ss ...string) { - for _, n := range ss { - s[n] = struct{}{} - } -} - -// contains returns whether the set contains the given string. -func (s stringSet) contains(ss string) bool { - _, ok := s[ss] - return ok -} - -// list returns the current elements in the set, in sorted order. -func (s stringSet) list() []string { - l := make([]string, 0, len(s)) - for k := range s { - l = append(l, k) - } - sort.Strings(l) - return l -} - -// union returns the union of this set and another. -func (s stringSet) union(o stringSet) stringSet { - ns := newStringSet() - for k := range s { - ns[k] = struct{}{} - } - for k := range o { - ns[k] = struct{}{} - } - return ns -} - -// intersect returns the intersection of this set and another. -func (s stringSet) intersect(o stringSet) stringSet { - shorter, longer := s, o - if len(longer) < len(shorter) { - shorter, longer = longer, shorter - } - - ns := newStringSet() - for k := range shorter { - if _, ok := longer[k]; ok { - ns[k] = struct{}{} - } - } - return ns -} diff --git a/vendor/github.com/influxdata/influxdb/coordinator/statement_executor_test.go b/vendor/github.com/influxdata/influxdb/coordinator/statement_executor_test.go deleted file mode 100644 index f41cdee5..00000000 --- a/vendor/github.com/influxdata/influxdb/coordinator/statement_executor_test.go +++ /dev/null @@ -1,374 +0,0 @@ -package coordinator_test - -import ( - "bytes" - "errors" - "io" - "os" - "reflect" - "testing" - "time" - - "github.com/davecgh/go-spew/spew" - "github.com/influxdata/influxdb/coordinator" - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/internal" - "github.com/influxdata/influxdb/models" - "github.com/influxdata/influxdb/services/meta" - "github.com/influxdata/influxdb/tsdb" - "go.uber.org/zap" -) - -const ( - // DefaultDatabase is the default database name used in tests. - DefaultDatabase = "db0" - - // DefaultRetentionPolicy is the default retention policy name used in tests. - DefaultRetentionPolicy = "rp0" -) - -// Ensure query executor can execute a simple SELECT statement. -func TestQueryExecutor_ExecuteQuery_SelectStatement(t *testing.T) { - e := DefaultQueryExecutor() - - // The meta client should return a single shard owned by the local node. - e.MetaClient.ShardsByTimeRangeFn = func(sources influxql.Sources, tmin, tmax time.Time) (a []meta.ShardInfo, err error) { - return []meta.ShardInfo{{ID: 100, Owners: []meta.ShardOwner{{NodeID: 0}}}}, nil - } - - // The TSDB store should return an IteratorCreator for shard. - // This IteratorCreator returns a single iterator with "value" in the aux fields. - e.TSDBStore.ShardIteratorCreatorFn = func(id uint64) influxql.IteratorCreator { - if id != 100 { - t.Fatalf("unexpected shard id: %d", id) - } - - var ic IteratorCreator - ic.CreateIteratorFn = func(opt influxql.IteratorOptions) (influxql.Iterator, error) { - return &FloatIterator{Points: []influxql.FloatPoint{ - {Name: "cpu", Time: int64(0 * time.Second), Aux: []interface{}{float64(100)}}, - {Name: "cpu", Time: int64(1 * time.Second), Aux: []interface{}{float64(200)}}, - }}, nil - } - ic.FieldDimensionsFn = func(sources influxql.Sources) (fields map[string]influxql.DataType, dimensions map[string]struct{}, err error) { - return map[string]influxql.DataType{"value": influxql.Float}, nil, nil - } - return &ic - } - - // Verify all results from the query. - if a := ReadAllResults(e.ExecuteQuery(`SELECT * FROM cpu`, "db0", 0)); !reflect.DeepEqual(a, []*influxql.Result{ - { - StatementID: 0, - Series: []*models.Row{{ - Name: "cpu", - Columns: []string{"time", "value"}, - Values: [][]interface{}{ - {time.Unix(0, 0).UTC(), float64(100)}, - {time.Unix(1, 0).UTC(), float64(200)}, - }, - }}, - }, - }) { - t.Fatalf("unexpected results: %s", spew.Sdump(a)) - } -} - -// Ensure query executor can enforce a maximum bucket selection count. -func TestQueryExecutor_ExecuteQuery_MaxSelectBucketsN(t *testing.T) { - e := DefaultQueryExecutor() - e.StatementExecutor.MaxSelectBucketsN = 3 - - // The meta client should return a single shards on the local node. - e.MetaClient.ShardsByTimeRangeFn = func(sources influxql.Sources, tmin, tmax time.Time) (a []meta.ShardInfo, err error) { - return []meta.ShardInfo{ - {ID: 100, Owners: []meta.ShardOwner{{NodeID: 0}}}, - }, nil - } - - var ic IteratorCreator - ic.CreateIteratorFn = func(opt influxql.IteratorOptions) (influxql.Iterator, error) { - return &FloatIterator{ - Points: []influxql.FloatPoint{{Name: "cpu", Time: int64(0 * time.Second), Aux: []interface{}{float64(100)}}}, - }, nil - } - ic.FieldDimensionsFn = func(sources influxql.Sources) (fields map[string]influxql.DataType, dimensions map[string]struct{}, err error) { - return map[string]influxql.DataType{"value": influxql.Float}, nil, nil - } - e.TSDBStore.ShardIteratorCreatorFn = func(id uint64) influxql.IteratorCreator { return &ic } - - // Verify all results from the query. - if a := ReadAllResults(e.ExecuteQuery(`SELECT count(value) FROM cpu WHERE time >= '2000-01-01T00:00:05Z' AND time < '2000-01-01T00:00:35Z' GROUP BY time(10s)`, "db0", 0)); !reflect.DeepEqual(a, []*influxql.Result{ - { - StatementID: 0, - Err: errors.New("max-select-buckets limit exceeded: (4/3)"), - }, - }) { - t.Fatalf("unexpected results: %s", spew.Sdump(a)) - } -} - -func TestStatementExecutor_NormalizeDropSeries(t *testing.T) { - q, err := influxql.ParseQuery("DROP SERIES FROM cpu") - if err != nil { - t.Fatalf("unexpected error parsing query: %v", err) - } - - stmt := q.Statements[0].(*influxql.DropSeriesStatement) - - s := &coordinator.StatementExecutor{ - MetaClient: &internal.MetaClientMock{ - DatabaseFn: func(name string) *meta.DatabaseInfo { - t.Fatal("meta client should not be called") - return nil - }, - }, - } - if err := s.NormalizeStatement(stmt, "foo"); err != nil { - t.Fatalf("unexpected error normalizing statement: %v", err) - } - - m := stmt.Sources[0].(*influxql.Measurement) - if m.Database != "" { - t.Fatalf("database rewritten when not supposed to: %v", m.Database) - } - if m.RetentionPolicy != "" { - t.Fatalf("database rewritten when not supposed to: %v", m.RetentionPolicy) - } - - if exp, got := "DROP SERIES FROM cpu", q.String(); exp != got { - t.Fatalf("generated query does match parsed: exp %v, got %v", exp, got) - } -} - -func TestStatementExecutor_NormalizeDeleteSeries(t *testing.T) { - q, err := influxql.ParseQuery("DELETE FROM cpu") - if err != nil { - t.Fatalf("unexpected error parsing query: %v", err) - } - - stmt := q.Statements[0].(*influxql.DeleteSeriesStatement) - - s := &coordinator.StatementExecutor{ - MetaClient: &internal.MetaClientMock{ - DatabaseFn: func(name string) *meta.DatabaseInfo { - t.Fatal("meta client should not be called") - return nil - }, - }, - } - if err := s.NormalizeStatement(stmt, "foo"); err != nil { - t.Fatalf("unexpected error normalizing statement: %v", err) - } - - m := stmt.Sources[0].(*influxql.Measurement) - if m.Database != "" { - t.Fatalf("database rewritten when not supposed to: %v", m.Database) - } - if m.RetentionPolicy != "" { - t.Fatalf("database rewritten when not supposed to: %v", m.RetentionPolicy) - } - - if exp, got := "DELETE FROM cpu", q.String(); exp != got { - t.Fatalf("generated query does match parsed: exp %v, got %v", exp, got) - } -} - -// QueryExecutor is a test wrapper for coordinator.QueryExecutor. -type QueryExecutor struct { - *influxql.QueryExecutor - - MetaClient MetaClient - TSDBStore TSDBStore - StatementExecutor *coordinator.StatementExecutor - LogOutput bytes.Buffer -} - -// NewQueryExecutor returns a new instance of QueryExecutor. -// This query executor always has a node id of 0. -func NewQueryExecutor() *QueryExecutor { - e := &QueryExecutor{ - QueryExecutor: influxql.NewQueryExecutor(), - } - e.StatementExecutor = &coordinator.StatementExecutor{ - MetaClient: &e.MetaClient, - TSDBStore: &e.TSDBStore, - } - e.QueryExecutor.StatementExecutor = e.StatementExecutor - - var out io.Writer = &e.LogOutput - if testing.Verbose() { - out = io.MultiWriter(out, os.Stderr) - } - e.QueryExecutor.WithLogger(zap.New( - zap.NewTextEncoder(), - zap.Output(os.Stderr), - )) - - return e -} - -// DefaultQueryExecutor returns a QueryExecutor with a database (db0) and retention policy (rp0). -func DefaultQueryExecutor() *QueryExecutor { - e := NewQueryExecutor() - e.MetaClient.DatabaseFn = DefaultMetaClientDatabaseFn - return e -} - -// ExecuteQuery parses query and executes against the database. -func (e *QueryExecutor) ExecuteQuery(query, database string, chunkSize int) <-chan *influxql.Result { - return e.QueryExecutor.ExecuteQuery(MustParseQuery(query), influxql.ExecutionOptions{ - Database: database, - ChunkSize: chunkSize, - }, make(chan struct{})) -} - -// TSDBStore is a mockable implementation of coordinator.TSDBStore. -type TSDBStore struct { - CreateShardFn func(database, policy string, shardID uint64, enabled bool) error - WriteToShardFn func(shardID uint64, points []models.Point) error - - RestoreShardFn func(id uint64, r io.Reader) error - BackupShardFn func(id uint64, since time.Time, w io.Writer) error - - DeleteDatabaseFn func(name string) error - DeleteMeasurementFn func(database, name string) error - DeleteRetentionPolicyFn func(database, name string) error - DeleteShardFn func(id uint64) error - DeleteSeriesFn func(database string, sources []influxql.Source, condition influxql.Expr) error - DatabaseIndexFn func(name string) *tsdb.DatabaseIndex - ShardIteratorCreatorFn func(id uint64) influxql.IteratorCreator -} - -func (s *TSDBStore) CreateShard(database, policy string, shardID uint64, enabled bool) error { - if s.CreateShardFn == nil { - return nil - } - return s.CreateShardFn(database, policy, shardID, enabled) -} - -func (s *TSDBStore) WriteToShard(shardID uint64, points []models.Point) error { - return s.WriteToShardFn(shardID, points) -} - -func (s *TSDBStore) RestoreShard(id uint64, r io.Reader) error { - return s.RestoreShardFn(id, r) -} - -func (s *TSDBStore) BackupShard(id uint64, since time.Time, w io.Writer) error { - return s.BackupShardFn(id, since, w) -} - -func (s *TSDBStore) DeleteDatabase(name string) error { - return s.DeleteDatabaseFn(name) -} - -func (s *TSDBStore) DeleteMeasurement(database, name string) error { - return s.DeleteMeasurementFn(database, name) -} - -func (s *TSDBStore) DeleteRetentionPolicy(database, name string) error { - return s.DeleteRetentionPolicyFn(database, name) -} - -func (s *TSDBStore) DeleteShard(id uint64) error { - return s.DeleteShardFn(id) -} - -func (s *TSDBStore) DeleteSeries(database string, sources []influxql.Source, condition influxql.Expr) error { - return s.DeleteSeriesFn(database, sources, condition) -} - -func (s *TSDBStore) IteratorCreator(shards []meta.ShardInfo, opt *influxql.SelectOptions) (influxql.IteratorCreator, error) { - // Generate iterators for each node. - ics := make([]influxql.IteratorCreator, 0) - if err := func() error { - for _, shard := range shards { - ic := s.ShardIteratorCreator(shard.ID) - if ic == nil { - continue - } - ics = append(ics, ic) - } - - return nil - }(); err != nil { - influxql.IteratorCreators(ics).Close() - return nil, err - } - - return influxql.IteratorCreators(ics), nil -} - -func (s *TSDBStore) ShardIteratorCreator(id uint64) influxql.IteratorCreator { - return s.ShardIteratorCreatorFn(id) -} - -func (s *TSDBStore) DatabaseIndex(name string) *tsdb.DatabaseIndex { - return s.DatabaseIndexFn(name) -} - -func (s *TSDBStore) Measurements(database string, cond influxql.Expr) ([]string, error) { - return nil, nil -} - -func (s *TSDBStore) TagValues(database string, cond influxql.Expr) ([]tsdb.TagValues, error) { - return nil, nil -} - -// MustParseQuery parses s into a query. Panic on error. -func MustParseQuery(s string) *influxql.Query { - q, err := influxql.ParseQuery(s) - if err != nil { - panic(err) - } - return q -} - -// ReadAllResults reads all results from c and returns as a slice. -func ReadAllResults(c <-chan *influxql.Result) []*influxql.Result { - var a []*influxql.Result - for result := range c { - a = append(a, result) - } - return a -} - -// IteratorCreator is a mockable implementation of IteratorCreator. -type IteratorCreator struct { - CreateIteratorFn func(opt influxql.IteratorOptions) (influxql.Iterator, error) - FieldDimensionsFn func(sources influxql.Sources) (fields map[string]influxql.DataType, dimensions map[string]struct{}, err error) - ExpandSourcesFn func(sources influxql.Sources) (influxql.Sources, error) -} - -func (ic *IteratorCreator) CreateIterator(opt influxql.IteratorOptions) (influxql.Iterator, error) { - return ic.CreateIteratorFn(opt) -} - -func (ic *IteratorCreator) FieldDimensions(sources influxql.Sources) (fields map[string]influxql.DataType, dimensions map[string]struct{}, err error) { - return ic.FieldDimensionsFn(sources) -} - -func (ic *IteratorCreator) ExpandSources(sources influxql.Sources) (influxql.Sources, error) { - return ic.ExpandSourcesFn(sources) -} - -// FloatIterator is a represents an iterator that reads from a slice. -type FloatIterator struct { - Points []influxql.FloatPoint - stats influxql.IteratorStats -} - -func (itr *FloatIterator) Stats() influxql.IteratorStats { return itr.stats } -func (itr *FloatIterator) Close() error { return nil } - -// Next returns the next value and shifts it off the beginning of the points slice. -func (itr *FloatIterator) Next() (*influxql.FloatPoint, error) { - if len(itr.Points) == 0 { - return nil, nil - } - - v := &itr.Points[0] - itr.Points = itr.Points[1:] - return v, nil -} diff --git a/vendor/github.com/influxdata/influxdb/errors.go b/vendor/github.com/influxdata/influxdb/errors.go deleted file mode 100644 index 13c782ad..00000000 --- a/vendor/github.com/influxdata/influxdb/errors.go +++ /dev/null @@ -1,44 +0,0 @@ -package influxdb - -import ( - "errors" - "fmt" - "strings" -) - -// ErrFieldTypeConflict is returned when a new field already exists with a -// different type. -var ErrFieldTypeConflict = errors.New("field type conflict") - -// ErrDatabaseNotFound indicates that a database operation failed on the -// specified database because the specified database does not exist. -func ErrDatabaseNotFound(name string) error { return fmt.Errorf("database not found: %s", name) } - -// ErrRetentionPolicyNotFound indicates that the named retention policy could -// not be found in the database. -func ErrRetentionPolicyNotFound(name string) error { - return fmt.Errorf("retention policy not found: %s", name) -} - -// IsClientError indicates whether an error is a known client error. -func IsClientError(err error) bool { - if err == nil { - return false - } - - if strings.HasPrefix(err.Error(), ErrFieldTypeConflict.Error()) { - return true - } - - return false -} - -const upgradeMessage = `******************************************************************* - UNSUPPORTED SHARD FORMAT DETECTED - -As of version 0.11, only tsm shards are supported. Please use the -influx_tsm tool to convert non-tsm shards. - -More information can be found at the documentation site: -https://docs.influxdata.com/influxdb/v0.10/administration/upgrading -*******************************************************************` diff --git a/vendor/github.com/influxdata/influxdb/etc/burn-in/.rvmrc b/vendor/github.com/influxdata/influxdb/etc/burn-in/.rvmrc deleted file mode 100644 index a9c1a9ca..00000000 --- a/vendor/github.com/influxdata/influxdb/etc/burn-in/.rvmrc +++ /dev/null @@ -1 +0,0 @@ -rvm use ruby-2.1.0@burn-in --create diff --git a/vendor/github.com/influxdata/influxdb/etc/burn-in/Gemfile b/vendor/github.com/influxdata/influxdb/etc/burn-in/Gemfile deleted file mode 100644 index b1816e8b..00000000 --- a/vendor/github.com/influxdata/influxdb/etc/burn-in/Gemfile +++ /dev/null @@ -1,4 +0,0 @@ -source 'https://rubygems.org' - -gem "colorize" -gem "influxdb" diff --git a/vendor/github.com/influxdata/influxdb/etc/burn-in/Gemfile.lock b/vendor/github.com/influxdata/influxdb/etc/burn-in/Gemfile.lock deleted file mode 100644 index 9e721c3a..00000000 --- a/vendor/github.com/influxdata/influxdb/etc/burn-in/Gemfile.lock +++ /dev/null @@ -1,14 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - colorize (0.6.0) - influxdb (0.0.16) - json - json (1.8.1) - -PLATFORMS - ruby - -DEPENDENCIES - colorize - influxdb diff --git a/vendor/github.com/influxdata/influxdb/etc/burn-in/burn-in.rb b/vendor/github.com/influxdata/influxdb/etc/burn-in/burn-in.rb deleted file mode 100644 index 1d44bc2c..00000000 --- a/vendor/github.com/influxdata/influxdb/etc/burn-in/burn-in.rb +++ /dev/null @@ -1,79 +0,0 @@ -require "influxdb" -require "colorize" -require "benchmark" - -require_relative "log" -require_relative "random_gaussian" - -BATCH_SIZE = 10_000 - -Log.info "Starting burn-in suite" -master = InfluxDB::Client.new -master.delete_database("burn-in") rescue nil -master.create_database("burn-in") -master.create_database_user("burn-in", "user", "pass") - -master.database = "burn-in" -# master.query "select * from test1 into test2;" -# master.query "select count(value) from test1 group by time(1m) into test2;" - -influxdb = InfluxDB::Client.new "burn-in", username: "user", password: "pass" - -Log.success "Connected to server #{influxdb.host}:#{influxdb.port}" - -Log.log "Creating RandomGaussian(500, 25)" -gaussian = RandomGaussian.new(500, 25) -point_count = 0 - -while true - Log.log "Generating 10,000 points.." - points = [] - BATCH_SIZE.times do |n| - points << {value: gaussian.rand.to_i.abs} - end - point_count += points.length - - Log.info "Sending points to server.." - begin - st = Time.now - foo = influxdb.write_point("test1", points) - et = Time.now - Log.log foo.inspect - Log.log "#{et-st} seconds elapsed" - Log.success "Write successful." - rescue => e - Log.failure "Write failed:" - Log.log e - end - sleep 0.5 - - Log.info "Checking regular points" - st = Time.now - response = influxdb.query("select count(value) from test1;") - et = Time.now - - Log.log "#{et-st} seconds elapsed" - - response_count = response["test1"].first["count"] - if point_count == response_count - Log.success "Point counts match: #{point_count} == #{response_count}" - else - Log.failure "Point counts don't match: #{point_count} != #{response_count}" - end - - # Log.info "Checking continuous query points for test2" - # st = Time.now - # response = influxdb.query("select count(value) from test2;") - # et = Time.now - - # Log.log "#{et-st} seconds elapsed" - - # response_count = response["test2"].first["count"] - # if point_count == response_count - # Log.success "Point counts match: #{point_count} == #{response_count}" - # else - # Log.failure "Point counts don't match: #{point_count} != #{response_count}" - # end -end - - diff --git a/vendor/github.com/influxdata/influxdb/etc/burn-in/log.rb b/vendor/github.com/influxdata/influxdb/etc/burn-in/log.rb deleted file mode 100644 index 0f70d763..00000000 --- a/vendor/github.com/influxdata/influxdb/etc/burn-in/log.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Log - def self.info(msg) - print Time.now.strftime("%r") + " | " - puts msg.to_s.colorize(:yellow) - end - - def self.success(msg) - print Time.now.strftime("%r") + " | " - puts msg.to_s.colorize(:green) - end - - def self.failure(msg) - print Time.now.strftime("%r") + " | " - puts msg.to_s.colorize(:red) - end - - def self.log(msg) - print Time.now.strftime("%r") + " | " - puts msg.to_s - end -end - - diff --git a/vendor/github.com/influxdata/influxdb/etc/burn-in/random_gaussian.rb b/vendor/github.com/influxdata/influxdb/etc/burn-in/random_gaussian.rb deleted file mode 100644 index 51d6c3c0..00000000 --- a/vendor/github.com/influxdata/influxdb/etc/burn-in/random_gaussian.rb +++ /dev/null @@ -1,31 +0,0 @@ -class RandomGaussian - def initialize(mean, stddev, rand_helper = lambda { Kernel.rand }) - @rand_helper = rand_helper - @mean = mean - @stddev = stddev - @valid = false - @next = 0 - end - - def rand - if @valid then - @valid = false - return @next - else - @valid = true - x, y = self.class.gaussian(@mean, @stddev, @rand_helper) - @next = y - return x - end - end - - private - def self.gaussian(mean, stddev, rand) - theta = 2 * Math::PI * rand.call - rho = Math.sqrt(-2 * Math.log(1 - rand.call)) - scale = stddev * rho - x = mean + scale * Math.cos(theta) - y = mean + scale * Math.sin(theta) - return x, y - end -end diff --git a/vendor/github.com/influxdata/influxdb/etc/burn-in/random_points.rb b/vendor/github.com/influxdata/influxdb/etc/burn-in/random_points.rb deleted file mode 100644 index 93bc8314..00000000 --- a/vendor/github.com/influxdata/influxdb/etc/burn-in/random_points.rb +++ /dev/null @@ -1,29 +0,0 @@ -require "influxdb" - -ONE_WEEK_IN_SECONDS = 7*24*60*60 -NUM_POINTS = 10_000 -BATCHES = 100 - -master = InfluxDB::Client.new -master.delete_database("ctx") rescue nil -master.create_database("ctx") - -influxdb = InfluxDB::Client.new "ctx" -influxdb.time_precision = "s" - -names = ["foo", "bar", "baz", "quu", "qux"] - -st = Time.now -BATCHES.times do |m| - points = [] - - puts "Writing #{NUM_POINTS} points, time ##{m}.." - NUM_POINTS.times do |n| - timestamp = Time.now.to_i - rand(ONE_WEEK_IN_SECONDS) - points << {value: names.sample, time: timestamp} - end - - influxdb.write_point("ct1", points) -end -puts st -puts Time.now diff --git a/vendor/github.com/influxdata/influxdb/etc/config.sample.toml b/vendor/github.com/influxdata/influxdb/etc/config.sample.toml deleted file mode 100644 index c55bc5a4..00000000 --- a/vendor/github.com/influxdata/influxdb/etc/config.sample.toml +++ /dev/null @@ -1,439 +0,0 @@ -### Welcome to the InfluxDB configuration file. - -# The values in this file override the default values used by the system if -# a config option is not specified. The commented out lines are the the configuration -# field and the default value used. Uncommentting a line and changing the value -# will change the value used at runtime when the process is restarted. - -# Once every 24 hours InfluxDB will report usage data to usage.influxdata.com -# The data includes a random ID, os, arch, version, the number of series and other -# usage data. No data from user databases is ever transmitted. -# Change this option to true to disable reporting. -# reporting-disabled = false - -# we'll try to get the hostname automatically, but if it the os returns something -# that isn't resolvable by other servers in the cluster, use this option to -# manually set the hostname -# hostname = "localhost" - -### -### [meta] -### -### Controls the parameters for the Raft consensus group that stores metadata -### about the InfluxDB cluster. -### - -[meta] - # Where the metadata/raft database is stored - dir = "/var/lib/influxdb/meta" - - # Automatically create a default retention policy when creating a database. - # retention-autocreate = true - - # If log messages are printed for the meta service - # logging-enabled = true - -### -### [data] -### -### Controls where the actual shard data for InfluxDB lives and how it is -### flushed from the WAL. "dir" may need to be changed to a suitable place -### for your system, but the WAL settings are an advanced configuration. The -### defaults should work for most systems. -### - -[data] - # The directory where the TSM storage engine stores TSM files. - dir = "/var/lib/influxdb/data" - - # The directory where the TSM storage engine stores WAL files. - wal-dir = "/var/lib/influxdb/wal" - - # Trace logging provides more verbose output around the tsm engine. Turning - # this on can provide more useful output for debugging tsm engine issues. - # trace-logging-enabled = false - - # Whether queries should be logged before execution. Very useful for troubleshooting, but will - # log any sensitive data contained within a query. - # query-log-enabled = true - - # Settings for the TSM engine - - # CacheMaxMemorySize is the maximum size a shard's cache can - # reach before it starts rejecting writes. - # cache-max-memory-size = 1048576000 - - # CacheSnapshotMemorySize is the size at which the engine will - # snapshot the cache and write it to a TSM file, freeing up memory - # cache-snapshot-memory-size = 26214400 - - # CacheSnapshotWriteColdDuration is the length of time at - # which the engine will snapshot the cache and write it to - # a new TSM file if the shard hasn't received writes or deletes - # cache-snapshot-write-cold-duration = "10m" - - # CompactFullWriteColdDuration is the duration at which the engine - # will compact all TSM files in a shard if it hasn't received a - # write or delete - # compact-full-write-cold-duration = "4h" - - # The maximum series allowed per database before writes are dropped. This limit can prevent - # high cardinality issues at the database level. This limit can be disabled by setting it to - # 0. - # max-series-per-database = 1000000 - - # The maximum number of tag values per tag that are allowed before writes are dropped. This limit - # can prevent high cardinality tag values from being written to a measurement. This limit can be - # disabled by setting it to 0. - # max-values-per-tag = 100000 - -### -### [coordinator] -### -### Controls the clustering service configuration. -### - -[coordinator] - # The default time a write request will wait until a "timeout" error is returned to the caller. - # write-timeout = "10s" - - # The maximum number of concurrent queries allowed to be executing at one time. If a query is - # executed and exceeds this limit, an error is returned to the caller. This limit can be disabled - # by setting it to 0. - # max-concurrent-queries = 0 - - # The maximum time a query will is allowed to execute before being killed by the system. This limit - # can help prevent run away queries. Setting the value to 0 disables the limit. - # query-timeout = "0s" - - # The the time threshold when a query will be logged as a slow query. This limit can be set to help - # discover slow or resource intensive queries. Setting the value to 0 disables the slow query logging. - # log-queries-after = "0s" - - # The maximum number of points a SELECT can process. A value of 0 will make the maximum - # point count unlimited. - # max-select-point = 0 - - # The maximum number of series a SELECT can run. A value of 0 will make the maximum series - # count unlimited. - - # The maximum number of series a SELECT can run. A value of zero will make the maximum series - # count unlimited. - # max-select-series = 0 - - # The maxium number of group by time bucket a SELECt can create. A value of zero will max the maximum - # number of buckets unlimited. - # max-select-buckets = 0 - -### -### [retention] -### -### Controls the enforcement of retention policies for evicting old data. -### - -[retention] - # Determines whether retention policy enforcment enabled. - # enabled = true - - # The interval of time when retention policy enforcement checks run. - # check-interval = "30m" - -### -### [shard-precreation] -### -### Controls the precreation of shards, so they are available before data arrives. -### Only shards that, after creation, will have both a start- and end-time in the -### future, will ever be created. Shards are never precreated that would be wholly -### or partially in the past. - -[shard-precreation] - # Determines whether shard pre-creation service is enabled. - # enabled = true - - # The interval of time when the check to pre-create new shards runs. - # check-interval = "10m" - - # The default period ahead of the endtime of a shard group that its successor - # group is created. - # advance-period = "30m" - -### -### Controls the system self-monitoring, statistics and diagnostics. -### -### The internal database for monitoring data is created automatically if -### if it does not already exist. The target retention within this database -### is called 'monitor' and is also created with a retention period of 7 days -### and a replication factor of 1, if it does not exist. In all cases the -### this retention policy is configured as the default for the database. - -[monitor] - # Whether to record statistics internally. - # store-enabled = true - - # The destination database for recorded statistics - # store-database = "_internal" - - # The interval at which to record statistics - # store-interval = "10s" - -### -### [admin] -### -### Controls the availability of the built-in, web-based admin interface. If HTTPS is -### enabled for the admin interface, HTTPS must also be enabled on the [http] service. -### -### NOTE: This interface is deprecated as of 1.1.0 and will be removed in a future release. - -[admin] - # Determines whether the admin service is enabled. - # enabled = false - - # The default bind address used by the admin service. - # bind-address = ":8083" - - # Whether the admin service should use HTTPS. - # https-enabled = false - - # The SSL certificate used when HTTPS is enabled. - # https-certificate = "/etc/ssl/influxdb.pem" - -### -### [http] -### -### Controls how the HTTP endpoints are configured. These are the primary -### mechanism for getting data into and out of InfluxDB. -### - -[http] - # Determines whether HTTP endpoint is enabled. - # enabled = true - - # The bind address used by the HTTP service. - # bind-address = ":8086" - - # Determines whether HTTP authentication is enabled. - # auth-enabled = false - - # The default realm sent back when issuing a basic auth challenge. - # realm = "InfluxDB" - - # Determines whether HTTP request logging is enable.d - # log-enabled = true - - # Determines whether detailed write logging is enabled. - # write-tracing = false - - # Determines whether the pprof endpoint is enabled. This endpoint is used for - # troubleshooting and monitoring. - # pprof-enabled = true - - # Determines whether HTTPS is enabled. - # https-enabled = false - - # The SSL certificate to use when HTTPS is enabled. - # https-certificate = "/etc/ssl/influxdb.pem" - - # Use a separate private key location. - # https-private-key = "" - - # The JWT auth shared secret to validate requests using JSON web tokens. - # shared-sercret = "" - - # The default chunk size for result sets that should be chunked. - # max-row-limit = 10000 - - # The maximum number of HTTP connections that may be open at once. New connections that - # would exceed this limit are dropped. Setting this value to 0 disables the limit. - # max-connection-limit = 0 - - # Enable http service over unix domain socket - # unix-socket-enabled = false - - # The path of the unix domain socket. - # bind-socket = "/var/run/influxdb.sock" - -### -### [subscriber] -### -### Controls the subscriptions, which can be used to fork a copy of all data -### received by the InfluxDB host. -### - -[subscriber] - # Determines whether the subscriber service is enabled. - # enabled = true - - # The default timeout for HTTP writes to subscribers. - # http-timeout = "30s" - - # Allows insecure HTTPS connections to subscribers. This is useful when testing with self- - # signed certificates. - # insecure-skip-verify = false - - # The path to the PEM encoded CA certs file. If the empty string, the default system certs will be used - # ca-certs = "" - - # The number of writer goroutines processing the write channel. - # write-concurrency = 40 - - # The number of in-flight writes buffered in the write channel. - # write-buffer-size = 1000 - - -### -### [[graphite]] -### -### Controls one or many listeners for Graphite data. -### - -[[graphite]] - # Determines whether the graphite endpoint is enabled. - # enabled = false - # database = "graphite" - # retention-policy = "" - # bind-address = ":2003" - # protocol = "tcp" - # consistency-level = "one" - - # These next lines control how batching works. You should have this enabled - # otherwise you could get dropped metrics or poor performance. Batching - # will buffer points in memory if you have many coming in. - - # Flush if this many points get buffered - # batch-size = 5000 - - # number of batches that may be pending in memory - # batch-pending = 10 - - # Flush at least this often even if we haven't hit buffer limit - # batch-timeout = "1s" - - # UDP Read buffer size, 0 means OS default. UDP listener will fail if set above OS max. - # udp-read-buffer = 0 - - ### This string joins multiple matching 'measurement' values providing more control over the final measurement name. - # separator = "." - - ### Default tags that will be added to all metrics. These can be overridden at the template level - ### or by tags extracted from metric - # tags = ["region=us-east", "zone=1c"] - - ### Each template line requires a template pattern. It can have an optional - ### filter before the template and separated by spaces. It can also have optional extra - ### tags following the template. Multiple tags should be separated by commas and no spaces - ### similar to the line protocol format. There can be only one default template. - # templates = [ - # "*.app env.service.resource.measurement", - # # Default template - # "server.*", - # ] - -### -### [collectd] -### -### Controls one or many listeners for collectd data. -### - -[[collectd]] - # enabled = false - # bind-address = ":25826" - # database = "collectd" - # retention-policy = "" - # - # The collectd service supports either scanning a directory for multiple types - # db files, or specifying a single db file. - # typesdb = "/usr/local/share/collectd" - # - # security-level = "none" - # auth-file = "/etc/collectd/auth_file" - - # These next lines control how batching works. You should have this enabled - # otherwise you could get dropped metrics or poor performance. Batching - # will buffer points in memory if you have many coming in. - - # Flush if this many points get buffered - # batch-size = 5000 - - # Number of batches that may be pending in memory - # batch-pending = 10 - - # Flush at least this often even if we haven't hit buffer limit - # batch-timeout = "10s" - - # UDP Read buffer size, 0 means OS default. UDP listener will fail if set above OS max. - # read-buffer = 0 - -### -### [opentsdb] -### -### Controls one or many listeners for OpenTSDB data. -### - -[[opentsdb]] - # enabled = false - # bind-address = ":4242" - # database = "opentsdb" - # retention-policy = "" - # consistency-level = "one" - # tls-enabled = false - # certificate= "/etc/ssl/influxdb.pem" - - # Log an error for every malformed point. - # log-point-errors = true - - # These next lines control how batching works. You should have this enabled - # otherwise you could get dropped metrics or poor performance. Only points - # metrics received over the telnet protocol undergo batching. - - # Flush if this many points get buffered - # batch-size = 1000 - - # Number of batches that may be pending in memory - # batch-pending = 5 - - # Flush at least this often even if we haven't hit buffer limit - # batch-timeout = "1s" - -### -### [[udp]] -### -### Controls the listeners for InfluxDB line protocol data via UDP. -### - -[[udp]] - # enabled = false - # bind-address = ":8089" - # database = "udp" - # retention-policy = "" - - # These next lines control how batching works. You should have this enabled - # otherwise you could get dropped metrics or poor performance. Batching - # will buffer points in memory if you have many coming in. - - # Flush if this many points get buffered - # batch-size = 5000 - - # Number of batches that may be pending in memory - # batch-pending = 10 - - # Will flush at least this often even if we haven't hit buffer limit - # batch-timeout = "1s" - - # UDP Read buffer size, 0 means OS default. UDP listener will fail if set above OS max. - # read-buffer = 0 - -### -### [continuous_queries] -### -### Controls how continuous queries are run within InfluxDB. -### - -[continuous_queries] - # Determiens whether the continuous query service is enabled. - # enabled = true - - # Controls whether queries are logged when executed by the CQ service. - # log-enabled = true - - # interval for how often continuous queries will be checked if they need to run - # run-interval = "1s" diff --git a/vendor/github.com/influxdata/influxdb/gobuild.sh b/vendor/github.com/influxdata/influxdb/gobuild.sh deleted file mode 100755 index 9a96e7e9..00000000 --- a/vendor/github.com/influxdata/influxdb/gobuild.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash -# This script run inside the Dockerfile_build_ubuntu64_git container and -# gets the latests Go source code and compiles it. -# Then passes control over to the normal build.py script - -set -e - -cd /go/src -git fetch --all -git checkout $GO_CHECKOUT -# Merge in recent changes if we are on a branch -# if we checked out a tag just ignore the error -git pull || true -./make.bash - -# Run normal build.py -cd "$PROJECT_DIR" -exec ./build.py "$@" diff --git a/vendor/github.com/influxdata/influxdb/importer/README.md b/vendor/github.com/influxdata/influxdb/importer/README.md deleted file mode 100644 index 7b0dd875..00000000 --- a/vendor/github.com/influxdata/influxdb/importer/README.md +++ /dev/null @@ -1,214 +0,0 @@ -# Import/Export - -## Exporting from 0.8.9 - -Version `0.8.9` of InfluxDB adds support to export your data to a format that can be imported into `0.9.3` and later. - -Note that `0.8.9` can be found here: - -``` -http://get.influxdb.org.s3.amazonaws.com/influxdb_0.8.9_amd64.deb -http://get.influxdb.org.s3.amazonaws.com/influxdb-0.8.9-1.x86_64.rpm -``` - -### Design - -`0.8.9` exports raw data to a flat file that includes two sections, `DDL` and `DML`. You can choose to export them independently (see below). - -The `DDL` section contains the sql commands to create databases and retention policies. the `DML` section is [line protocol](https://github.com/influxdata/influxdb/blob/master/tsdb/README.md) and can be directly posted to the [http endpoint](https://docs.influxdata.com/influxdb/v0.10/guides/writing_data) in `0.10`. Remember that batching is important and we don't recommend batch sizes over 5k without further testing. - -Example export file: -``` -# DDL -CREATE DATABASE db0 -CREATE DATABASE db1 -CREATE RETENTION POLICY rp1 ON db1 DURATION 1h REPLICATION 1 - -# DML -# CONTEXT-DATABASE:db0 -# CONTEXT-RETENTION-POLICY:autogen -cpu,host=server1 value=33.3 1464026335000000000 -cpu,host=server1 value=43.3 1464026395000000000 -cpu,host=server1 value=63.3 1464026575000000000 - -# CONTEXT-DATABASE:db1 -# CONTEXT-RETENTION-POLICY:rp1 -cpu,host=server1 value=73.3 1464026335000000000 -cpu,host=server1 value=83.3 1464026395000000000 -cpu,host=server1 value=93.3 1464026575000000000 -``` - -You need to specify a database and shard group when you export. - -To list out your shards, use the following http endpoint: - -`/cluster/shard_spaces` - -example: -```sh -http://username:password@localhost:8086/cluster/shard_spaces -``` - -Then, to export a database with then name "metrics" and a shard space with the name "default", issue the following curl command: - -```sh -curl -o export http://username:password@localhost:8086/export/metrics/default -``` - -Compression is supported, and will result in a significantly smaller file size. - -Use the following command for compression: -```sh -curl -o export.gz --compressed http://username:password@localhost:8086/export/metrics/default -``` - -You can also export just the `DDL` with this option: - -```sh -curl -o export.ddl http://username:password@localhost:8086/export/metrics/default?l=ddl -``` - -Or just the `DML` with this option: - -```sh -curl -o export.dml.gz --compressed http://username:password@localhost:8086/export/metrics/default?l=dml -``` - -### Assumptions - -- Series name mapping follows these [guidelines](https://docs.influxdata.com/influxdb/v0.8/advanced_topics/schema_design/) -- Database name will map directly from `0.8` to `0.10` -- Shard Spaces map to Retention Policies -- Shard Space Duration is ignored, as in `0.10` we determine shard size automatically -- Regex is used to match the correct series names and only exports that data for the database -- Duration becomes the new Retention Policy duration - -- Users are not migrated due to inability to get passwords. Anyone using users will need to manually set these back up in `0.10` - -### Upgrade Recommendations - -It's recommended that you upgrade to `0.9.3` or later first and have all your writes going there. Then, on the `0.8.X` instances, upgrade to `0.8.9`. - -It is important that when exporting you change your config to allow for the http endpoints not timing out. To do so, make this change in your config: - -```toml -# Configure the http api -[api] -read-timeout = "0s" -``` - -### Exceptions - -If a series can't be exported to tags based on the guidelines mentioned above, -we will insert the entire series name as the measurement name. You can either -allow that to import into the new InfluxDB instance, or you can do your own -data massage on it prior to importing it. - -For example, if you have the following series name: - -``` -metric.disk.c.host.server01.single -``` - -It will export as exactly thta as the measurement name and no tags: - -``` -metric.disk.c.host.server01.single -``` - -### Export Metrics - -When you export, you will now get comments inline in the `DML`: - -`# Found 999 Series for export` - -As well as count totals for each series exported: - -`# Series FOO - Points Exported: 999` - -With a total at the bottom: - -`# Points Exported: 999` - -You can grep the file that was exported at the end to get all the export metrics: - -`cat myexport | grep Exported` - -## Importing - -Version `0.9.3` of InfluxDB adds support to import your data from version `0.8.9`. - -## Caveats - -For the export/import to work, all requisites have to be met. For export, all series names in `0.8` should be in the following format: - -``` -.... -``` -for example: -``` -az.us-west-1.host.serverA.cpu -``` -or any number of tags -``` -building.2.temperature -``` - -Additionally, the fields need to have a consistent type (all float64, int64, etc) for every write in `0.8`. Otherwise they have the potential to fail writes in the import. -See below for more information. - -## Running the import command - - To import via the cli, you can specify the following command: - - ```sh - influx -import -path=metrics-default.gz -compressed - ``` - - If the file is not compressed you can issue it without the `-compressed` flag: - - ```sh - influx -import -path=metrics-default - ``` - - To redirect failed import lines to another file, run this command: - - ```sh - influx -import -path=metrics-default.gz -compressed > failures - ``` - - The import will use the line protocol in batches of 5,000 lines per batch when sending data to the server. - -### Throttiling the import - - If you need to throttle the import so the database has time to ingest, you can use the `-pps` flag. This will limit the points per second that will be sent to the server. - - ```sh - influx -import -path=metrics-default.gz -compressed -pps 50000 > failures - ``` - - Which is stating that you don't want MORE than 50,000 points per second to write to the database. Due to the processing that is taking place however, you will likely never get exactly 50,000 pps, more like 35,000 pps, etc. - -## Understanding the results of the import - -During the import, a status message will write out for every 100,000 points imported and report stats on the progress of the import: - -``` -2015/08/21 14:48:01 Processed 3100000 lines. Time elapsed: 56.740578415s. Points per second (PPS): 54634 -``` - - The batch will give some basic stats when finished: - - ```sh - 2015/07/29 23:15:20 Processed 2 commands - 2015/07/29 23:15:20 Processed 70207923 inserts - 2015/07/29 23:15:20 Failed 29785000 inserts - ``` - - Most inserts fail due to the following types of error: - - ```sh - 2015/07/29 22:18:28 error writing batch: write failed: field type conflict: input field "value" on measurement "metric" is type float64, already exists as type integer - ``` - - This is due to the fact that in `0.8` a field could get created and saved as int or float types for independent writes. In `0.9` and greater the field has to have a consistent type. diff --git a/vendor/github.com/influxdata/influxdb/importer/v8/importer.go b/vendor/github.com/influxdata/influxdb/importer/v8/importer.go deleted file mode 100644 index 5dfa5f52..00000000 --- a/vendor/github.com/influxdata/influxdb/importer/v8/importer.go +++ /dev/null @@ -1,240 +0,0 @@ -package v8 // import "github.com/influxdata/influxdb/importer/v8" - -import ( - "bufio" - "compress/gzip" - "fmt" - "io" - "log" - "os" - "strings" - "time" - - "github.com/influxdata/influxdb/client" -) - -const batchSize = 5000 - -// Config is the config used to initialize a Importer importer -type Config struct { - Path string // Path to import data. - Version string - Compressed bool // Whether import data is gzipped. - PPS int // points per second importer imports with. - - client.Config -} - -// NewConfig returns an initialized *Config -func NewConfig() Config { - return Config{Config: client.NewConfig()} -} - -// Importer is the importer used for importing 0.8 data -type Importer struct { - client *client.Client - database string - retentionPolicy string - config Config - batch []string - totalInserts int - failedInserts int - totalCommands int - throttlePointsWritten int - lastWrite time.Time - throttle *time.Ticker -} - -// NewImporter will return an intialized Importer struct -func NewImporter(config Config) *Importer { - config.UserAgent = fmt.Sprintf("influxDB importer/%s", config.Version) - return &Importer{ - config: config, - batch: make([]string, 0, batchSize), - } -} - -// Import processes the specified file in the Config and writes the data to the databases in chunks specified by batchSize -func (i *Importer) Import() error { - // Create a client and try to connect. - cl, err := client.NewClient(i.config.Config) - if err != nil { - return fmt.Errorf("could not create client %s", err) - } - i.client = cl - if _, _, e := i.client.Ping(); e != nil { - return fmt.Errorf("failed to connect to %s\n", i.client.Addr()) - } - - // Validate args - if i.config.Path == "" { - return fmt.Errorf("file argument required") - } - - defer func() { - if i.totalInserts > 0 { - log.Printf("Processed %d commands\n", i.totalCommands) - log.Printf("Processed %d inserts\n", i.totalInserts) - log.Printf("Failed %d inserts\n", i.failedInserts) - } - }() - - // Open the file - f, err := os.Open(i.config.Path) - if err != nil { - return err - } - defer f.Close() - - var r io.Reader - - // If gzipped, wrap in a gzip reader - if i.config.Compressed { - gr, err := gzip.NewReader(f) - if err != nil { - return err - } - defer gr.Close() - // Set the reader to the gzip reader - r = gr - } else { - // Standard text file so our reader can just be the file - r = f - } - - // Get our reader - scanner := bufio.NewScanner(r) - - // Process the DDL - i.processDDL(scanner) - - // Set up our throttle channel. Since there is effectively no other activity at this point - // the smaller resolution gets us much closer to the requested PPS - i.throttle = time.NewTicker(time.Microsecond) - defer i.throttle.Stop() - - // Prime the last write - i.lastWrite = time.Now() - - // Process the DML - i.processDML(scanner) - - // Check if we had any errors scanning the file - if err := scanner.Err(); err != nil { - return fmt.Errorf("reading standard input: %s", err) - } - - return nil -} - -func (i *Importer) processDDL(scanner *bufio.Scanner) { - for scanner.Scan() { - line := scanner.Text() - // If we find the DML token, we are done with DDL - if strings.HasPrefix(line, "# DML") { - return - } - if strings.HasPrefix(line, "#") { - continue - } - // Skip blank lines - if strings.TrimSpace(line) == "" { - continue - } - i.queryExecutor(line) - } -} - -func (i *Importer) processDML(scanner *bufio.Scanner) { - start := time.Now() - for scanner.Scan() { - line := scanner.Text() - if strings.HasPrefix(line, "# CONTEXT-DATABASE:") { - i.database = strings.TrimSpace(strings.Split(line, ":")[1]) - } - if strings.HasPrefix(line, "# CONTEXT-RETENTION-POLICY:") { - i.retentionPolicy = strings.TrimSpace(strings.Split(line, ":")[1]) - } - if strings.HasPrefix(line, "#") { - continue - } - // Skip blank lines - if strings.TrimSpace(line) == "" { - continue - } - i.batchAccumulator(line, start) - } - // Call batchWrite one last time to flush anything out in the batch - i.batchWrite() -} - -func (i *Importer) execute(command string) { - response, err := i.client.Query(client.Query{Command: command, Database: i.database}) - if err != nil { - log.Printf("error: %s\n", err) - return - } - if err := response.Error(); err != nil { - log.Printf("error: %s\n", response.Error()) - } -} - -func (i *Importer) queryExecutor(command string) { - i.totalCommands++ - i.execute(command) -} - -func (i *Importer) batchAccumulator(line string, start time.Time) { - i.batch = append(i.batch, line) - if len(i.batch) == batchSize { - i.batchWrite() - i.batch = i.batch[:0] - // Give some status feedback every 100000 lines processed - processed := i.totalInserts + i.failedInserts - if processed%100000 == 0 { - since := time.Since(start) - pps := float64(processed) / since.Seconds() - log.Printf("Processed %d lines. Time elapsed: %s. Points per second (PPS): %d", processed, since.String(), int64(pps)) - } - } -} - -func (i *Importer) batchWrite() { - // Accumulate the batch size to see how many points we have written this second - i.throttlePointsWritten += len(i.batch) - - // Find out when we last wrote data - since := time.Since(i.lastWrite) - - // Check to see if we've exceeded our points per second for the current timeframe - var currentPPS int - if since.Seconds() > 0 { - currentPPS = int(float64(i.throttlePointsWritten) / since.Seconds()) - } else { - currentPPS = i.throttlePointsWritten - } - - // If our currentPPS is greater than the PPS specified, then we wait and retry - if int(currentPPS) > i.config.PPS && i.config.PPS != 0 { - // Wait for the next tick - <-i.throttle.C - - // Decrement the batch size back out as it is going to get called again - i.throttlePointsWritten -= len(i.batch) - i.batchWrite() - return - } - - _, e := i.client.WriteLineProtocol(strings.Join(i.batch, "\n"), i.database, i.retentionPolicy, i.config.Precision, i.config.WriteConsistency) - if e != nil { - log.Println("error writing batch: ", e) - // Output failed lines to STDOUT so users can capture lines that failed to import - fmt.Println(strings.Join(i.batch, "\n")) - i.failedInserts += len(i.batch) - } else { - i.totalInserts += len(i.batch) - } - i.throttlePointsWritten = 0 - i.lastWrite = time.Now() - return -} diff --git a/vendor/github.com/influxdata/influxdb/influxdb.go b/vendor/github.com/influxdata/influxdb/influxdb.go deleted file mode 100644 index 0befd299..00000000 --- a/vendor/github.com/influxdata/influxdb/influxdb.go +++ /dev/null @@ -1 +0,0 @@ -package influxdb // import "github.com/influxdata/influxdb" diff --git a/vendor/github.com/influxdata/influxdb/influxql/README.md b/vendor/github.com/influxdata/influxdb/influxql/README.md deleted file mode 100644 index dfed3c93..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/README.md +++ /dev/null @@ -1,1048 +0,0 @@ -# The Influx Query Language Specification - -## Introduction - -This is a reference for the Influx Query Language ("InfluxQL"). - -InfluxQL is a SQL-like query language for interacting with InfluxDB. It has -been lovingly crafted to feel familiar to those coming from other SQL or -SQL-like environments while providing features specific to storing and analyzing -time series data. - - -## Notation - -The syntax is specified using Extended Backus-Naur Form ("EBNF"). EBNF is the -same notation used in the [Go](http://golang.org) programming language -specification, which can be found [here](https://golang.org/ref/spec). Not so -coincidentally, InfluxDB is written in Go. - -``` -Production = production_name "=" [ Expression ] "." . -Expression = Alternative { "|" Alternative } . -Alternative = Term { Term } . -Term = production_name | token [ "…" token ] | Group | Option | Repetition . -Group = "(" Expression ")" . -Option = "[" Expression "]" . -Repetition = "{" Expression "}" . -``` - -Notation operators in order of increasing precedence: - -``` -| alternation -() grouping -[] option (0 or 1 times) -{} repetition (0 to n times) -``` - -## Query representation - -### Characters - -InfluxQL is Unicode text encoded in [UTF-8](http://en.wikipedia.org/wiki/UTF-8). - -``` -newline = /* the Unicode code point U+000A */ . -unicode_char = /* an arbitrary Unicode code point except newline */ . -``` - -## Letters and digits - -Letters are the set of ASCII characters plus the underscore character _ (U+005F) -is considered a letter. - -Only decimal digits are supported. - -``` -letter = ascii_letter | "_" . -ascii_letter = "A" … "Z" | "a" … "z" . -digit = "0" … "9" . -``` - -## Identifiers - -Identifiers are tokens which refer to database names, retention policy names, -user names, measurement names, tag keys, and field keys. - -The rules: - -- double quoted identifiers can contain any unicode character other than a new line -- double quoted identifiers can contain escaped `"` characters (i.e., `\"`) -- double quoted identifiers can contain InfluxQL keywords -- unquoted identifiers must start with an upper or lowercase ASCII character or "_" -- unquoted identifiers may contain only ASCII letters, decimal digits, and "_" - -``` -identifier = unquoted_identifier | quoted_identifier . -unquoted_identifier = ( letter ) { letter | digit } . -quoted_identifier = `"` unicode_char { unicode_char } `"` . -``` - -#### Examples: - -``` -cpu -_cpu_stats -"1h" -"anything really" -"1_Crazy-1337.identifier>NAME👍" -``` - -## Keywords - -``` -ALL ALTER ANY AS ASC BEGIN -BY CREATE CONTINUOUS DATABASE DATABASES DEFAULT -DELETE DESC DESTINATIONS DIAGNOSTICS DISTINCT DROP -DURATION END EVERY EXPLAIN FIELD FOR -FROM GRANT GRANTS GROUP GROUPS IN -INF INSERT INTO KEY KEYS KILL -LIMIT SHOW MEASUREMENT MEASUREMENTS NAME OFFSET -ON ORDER PASSWORD POLICY POLICIES PRIVILEGES -QUERIES QUERY READ REPLICATION RESAMPLE RETENTION -REVOKE SELECT SERIES SET SHARD SHARDS -SLIMIT SOFFSET STATS SUBSCRIPTION SUBSCRIPTIONS TAG -TO USER USERS VALUES WHERE WITH -WRITE -``` - -## Literals - -### Integers - -InfluxQL supports decimal integer literals. Hexadecimal and octal literals are -not currently supported. - -``` -int_lit = ( "1" … "9" ) { digit } . -``` - -### Floats - -InfluxQL supports floating-point literals. Exponents are not currently supported. - -``` -float_lit = int_lit "." int_lit . -``` - -### Strings - -String literals must be surrounded by single quotes. Strings may contain `'` -characters as long as they are escaped (i.e., `\'`). - -``` -string_lit = `'` { unicode_char } `'` . -``` - -### Durations - -Duration literals specify a length of time. An integer literal followed -immediately (with no spaces) by a duration unit listed below is interpreted as -a duration literal. - -### Duration units -| Units | Meaning | -|--------|-----------------------------------------| -| u or µ | microseconds (1 millionth of a second) | -| ms | milliseconds (1 thousandth of a second) | -| s | second | -| m | minute | -| h | hour | -| d | day | -| w | week | - -``` -duration_lit = int_lit duration_unit . -duration_unit = "u" | "µ" | "ms" | "s" | "m" | "h" | "d" | "w" . -``` - -### Dates & Times - -The date and time literal format is not specified in EBNF like the rest of this document. It is specified using Go's date / time parsing format, which is a reference date written in the format required by InfluxQL. The reference date time is: - -InfluxQL reference date time: January 2nd, 2006 at 3:04:05 PM - -``` -time_lit = "2006-01-02 15:04:05.999999" | "2006-01-02" . -``` - -### Booleans - -``` -bool_lit = TRUE | FALSE . -``` - -### Regular Expressions - -``` -regex_lit = "/" { unicode_char } "/" . -``` - -**Comparators:** -`=~` matches against -`!~` doesn't match against - -> **Note:** Use regular expressions to match measurements and tags. -You cannot use regular expressions to match databases, retention policies, or fields. - -## Queries - -A query is composed of one or more statements separated by a semicolon. - -``` -query = statement { ";" statement } . - -statement = alter_retention_policy_stmt | - create_continuous_query_stmt | - create_database_stmt | - create_retention_policy_stmt | - create_subscription_stmt | - create_user_stmt | - delete_stmt | - drop_continuous_query_stmt | - drop_database_stmt | - drop_measurement_stmt | - drop_retention_policy_stmt | - drop_series_stmt | - drop_shard_stmt | - drop_subscription_stmt | - drop_user_stmt | - grant_stmt | - kill_query_statement | - show_continuous_queries_stmt | - show_databases_stmt | - show_field_keys_stmt | - show_grants_stmt | - show_measurements_stmt | - show_queries_stmt | - show_retention_policies | - show_series_stmt | - show_shard_groups_stmt | - show_shards_stmt | - show_subscriptions_stmt| - show_tag_keys_stmt | - show_tag_values_stmt | - show_users_stmt | - revoke_stmt | - select_stmt . -``` - -## Statements - -### ALTER RETENTION POLICY - -``` -alter_retention_policy_stmt = "ALTER RETENTION POLICY" policy_name on_clause - retention_policy_option - [ retention_policy_option ] - [ retention_policy_option ] - [ retention_policy_option ] . -``` - -> Replication factors do not serve a purpose with single node instances. - -#### Examples: - -```sql --- Set default retention policy for mydb to 1h.cpu. -ALTER RETENTION POLICY "1h.cpu" ON "mydb" DEFAULT - --- Change duration and replication factor. -ALTER RETENTION POLICY "policy1" ON "somedb" DURATION 1h REPLICATION 4 -``` - -### CREATE CONTINUOUS QUERY - -``` -create_continuous_query_stmt = "CREATE CONTINUOUS QUERY" query_name on_clause - [ "RESAMPLE" resample_opts ] - "BEGIN" select_stmt "END" . - -query_name = identifier . - -resample_opts = (every_stmt for_stmt | every_stmt | for_stmt) . -every_stmt = "EVERY" duration_lit -for_stmt = "FOR" duration_lit -``` - -#### Examples: - -```sql --- selects from DEFAULT retention policy and writes into 6_months retention policy -CREATE CONTINUOUS QUERY "10m_event_count" -ON "db_name" -BEGIN - SELECT count("value") - INTO "6_months"."events" - FROM "events" - GROUP BY time(10m) -END; - --- this selects from the output of one continuous query in one retention policy and outputs to another series in another retention policy -CREATE CONTINUOUS QUERY "1h_event_count" -ON "db_name" -BEGIN - SELECT sum("count") as "count" - INTO "2_years"."events" - FROM "6_months"."events" - GROUP BY time(1h) -END; - --- this customizes the resample interval so the interval is queried every 10s and intervals are resampled until 2m after their start time --- when resample is used, at least one of "EVERY" or "FOR" must be used -CREATE CONTINUOUS QUERY "cpu_mean" -ON "db_name" -RESAMPLE EVERY 10s FOR 2m -BEGIN - SELECT mean("value") - INTO "cpu_mean" - FROM "cpu" - GROUP BY time(1m) -END; -``` - -### CREATE DATABASE - -``` -create_database_stmt = "CREATE DATABASE" db_name - [ WITH - [ retention_policy_duration ] - [ retention_policy_replication ] - [ retention_policy_shard_group_duration ] - [ retention_policy_name ] - ] . -``` - -> Replication factors do not serve a purpose with single node instances. - -#### Examples: - -```sql --- Create a database called foo -CREATE DATABASE "foo" - --- Create a database called bar with a new DEFAULT retention policy and specify the duration, replication, shard group duration, and name of that retention policy -CREATE DATABASE "bar" WITH DURATION 1d REPLICATION 1 SHARD DURATION 30m NAME "myrp" - --- Create a database called mydb with a new DEFAULT retention policy and specify the name of that retention policy -CREATE DATABASE "mydb" WITH NAME "myrp" -``` - -### CREATE RETENTION POLICY - -``` -create_retention_policy_stmt = "CREATE RETENTION POLICY" policy_name on_clause - retention_policy_duration - retention_policy_replication - [ retention_policy_shard_group_duration ] - [ "DEFAULT" ] . -``` - -> Replication factors do not serve a purpose with single node instances. - -#### Examples - -```sql --- Create a retention policy. -CREATE RETENTION POLICY "10m.events" ON "somedb" DURATION 60m REPLICATION 2 - --- Create a retention policy and set it as the DEFAULT. -CREATE RETENTION POLICY "10m.events" ON "somedb" DURATION 60m REPLICATION 2 DEFAULT - --- Create a retention policy and specify the shard group duration. -CREATE RETENTION POLICY "10m.events" ON "somedb" DURATION 60m REPLICATION 2 SHARD DURATION 30m -``` - -### CREATE SUBSCRIPTION - -Subscriptions tell InfluxDB to send all the data it receives to Kapacitor or other third parties. - -``` -create_subscription_stmt = "CREATE SUBSCRIPTION" subscription_name "ON" db_name "." retention_policy "DESTINATIONS" ("ANY"|"ALL") host { "," host} . -``` - -#### Examples: - -```sql --- Create a SUBSCRIPTION on database 'mydb' and retention policy 'autogen' that send data to 'example.com:9090' via UDP. -CREATE SUBSCRIPTION "sub0" ON "mydb"."autogen" DESTINATIONS ALL 'udp://example.com:9090' - --- Create a SUBSCRIPTION on database 'mydb' and retention policy 'autogen' that round robins the data to 'h1.example.com:9090' and 'h2.example.com:9090'. -CREATE SUBSCRIPTION "sub0" ON "mydb"."autogen" DESTINATIONS ANY 'udp://h1.example.com:9090', 'udp://h2.example.com:9090' -``` - -### CREATE USER - -``` -create_user_stmt = "CREATE USER" user_name "WITH PASSWORD" password - [ "WITH ALL PRIVILEGES" ] . -``` - -#### Examples: - -```sql --- Create a normal database user. -CREATE USER "jdoe" WITH PASSWORD '1337password' - --- Create an admin user. --- Note: Unlike the GRANT statement, the "PRIVILEGES" keyword is required here. -CREATE USER "jdoe" WITH PASSWORD '1337password' WITH ALL PRIVILEGES -``` - -> **Note:** The password string must be wrapped in single quotes. - -### DELETE - -``` -delete_stmt = "DELETE" ( from_clause | where_clause | from_clause where_clause ) . -``` - -#### Examples: - -```sql -DELETE FROM "cpu" -DELETE FROM "cpu" WHERE time < '2000-01-01T00:00:00Z' -DELETE WHERE time < '2000-01-01T00:00:00Z' -``` - -### DROP CONTINUOUS QUERY - -``` -drop_continuous_query_stmt = "DROP CONTINUOUS QUERY" query_name on_clause . -``` - -#### Example: - -```sql -DROP CONTINUOUS QUERY "myquery" ON "mydb" -``` - -### DROP DATABASE - -``` -drop_database_stmt = "DROP DATABASE" db_name . -``` - -#### Example: - -```sql -DROP DATABASE "mydb" -``` - -### DROP MEASUREMENT - -``` -drop_measurement_stmt = "DROP MEASUREMENT" measurement . -``` - -#### Examples: - -```sql --- drop the cpu measurement -DROP MEASUREMENT "cpu" -``` - -### DROP RETENTION POLICY - -``` -drop_retention_policy_stmt = "DROP RETENTION POLICY" policy_name on_clause . -``` - -#### Example: - -```sql --- drop the retention policy named 1h.cpu from mydb -DROP RETENTION POLICY "1h.cpu" ON "mydb" -``` - -### DROP SERIES - -``` -drop_series_stmt = "DROP SERIES" ( from_clause | where_clause | from_clause where_clause ) . -``` - -#### Example: - -```sql -DROP SERIES FROM "telegraf"."autogen"."cpu" WHERE cpu = 'cpu8' - -``` - -### DROP SHARD - -``` -drop_shard_stmt = "DROP SHARD" ( shard_id ) . -``` - -#### Example: - -``` -DROP SHARD 1 -``` - -### DROP SUBSCRIPTION - -``` -drop_subscription_stmt = "DROP SUBSCRIPTION" subscription_name "ON" db_name "." retention_policy . -``` - -#### Example: - -```sql -DROP SUBSCRIPTION "sub0" ON "mydb"."autogen" -``` - -### DROP USER - -``` -drop_user_stmt = "DROP USER" user_name . -``` - -#### Example: - -```sql -DROP USER "jdoe" -``` - -### GRANT - -> **NOTE:** Users can be granted privileges on databases that do not exist. - -``` -grant_stmt = "GRANT" privilege [ on_clause ] to_clause . -``` - -#### Examples: - -```sql --- grant admin privileges -GRANT ALL TO "jdoe" - --- grant read access to a database -GRANT READ ON "mydb" TO "jdoe" -``` - -### KILL QUERY - -``` -kill_query_statement = "KILL QUERY" query_id . -``` - -#### Examples: - -``` ---- kill a query with the query_id 36 -KILL QUERY 36 -``` - -> **NOTE:** Identify the `query_id` from the `SHOW QUERIES` output. - -### SHOW CONTINUOUS QUERIES - -``` -show_continuous_queries_stmt = "SHOW CONTINUOUS QUERIES" . -``` - -#### Example: - -```sql --- show all continuous queries -SHOW CONTINUOUS QUERIES -``` - -### SHOW DATABASES - -``` -show_databases_stmt = "SHOW DATABASES" . -``` - -#### Example: - -```sql --- show all databases -SHOW DATABASES -``` - -### SHOW FIELD KEYS - -``` -show_field_keys_stmt = "SHOW FIELD KEYS" [ from_clause ] . -``` - -#### Examples: - -```sql --- show field keys and field value data types from all measurements -SHOW FIELD KEYS - --- show field keys and field value data types from specified measurement -SHOW FIELD KEYS FROM "cpu" -``` - -### SHOW GRANTS - -``` -show_grants_stmt = "SHOW GRANTS FOR" user_name . -``` - -#### Example: - -```sql --- show grants for jdoe -SHOW GRANTS FOR "jdoe" -``` - -### SHOW MEASUREMENTS - -``` -show_measurements_stmt = "SHOW MEASUREMENTS" [ with_measurement_clause ] [ where_clause ] [ limit_clause ] [ offset_clause ] . -``` - -#### Examples: - -```sql --- show all measurements -SHOW MEASUREMENTS - --- show measurements where region tag = 'uswest' AND host tag = 'serverA' -SHOW MEASUREMENTS WHERE "region" = 'uswest' AND "host" = 'serverA' - --- show measurements that start with 'h2o' -SHOW MEASUREMENTS WITH MEASUREMENT =~ /h2o.*/ -``` - -### SHOW QUERIES - -``` -show_queries_stmt = "SHOW QUERIES" . -``` - -#### Example: - -```sql --- show all currently-running queries -SHOW QUERIES -``` - -### SHOW RETENTION POLICIES - -``` -show_retention_policies = "SHOW RETENTION POLICIES" on_clause . -``` - -#### Example: - -```sql --- show all retention policies on a database -SHOW RETENTION POLICIES ON "mydb" -``` - -### SHOW SERIES - -``` -show_series_stmt = "SHOW SERIES" [ from_clause ] [ where_clause ] [ limit_clause ] [ offset_clause ] . -``` - -#### Example: - -```sql -SHOW SERIES FROM "telegraf"."autogen"."cpu" WHERE cpu = 'cpu8' -``` - -### SHOW SHARD GROUPS - -``` -show_shard_groups_stmt = "SHOW SHARD GROUPS" . -``` - -#### Example: - -```sql -SHOW SHARD GROUPS -``` - -### SHOW SHARDS - -``` -show_shards_stmt = "SHOW SHARDS" . -``` - -#### Example: - -```sql -SHOW SHARDS -``` - -### SHOW SUBSCRIPTIONS - -``` -show_subscriptions_stmt = "SHOW SUBSCRIPTIONS" . -``` - -#### Example: - -```sql -SHOW SUBSCRIPTIONS -``` - -### SHOW TAG KEYS - -``` -show_tag_keys_stmt = "SHOW TAG KEYS" [ from_clause ] [ where_clause ] [ group_by_clause ] - [ limit_clause ] [ offset_clause ] . -``` - -#### Examples: - -```sql --- show all tag keys -SHOW TAG KEYS - --- show all tag keys from the cpu measurement -SHOW TAG KEYS FROM "cpu" - --- show all tag keys from the cpu measurement where the region key = 'uswest' -SHOW TAG KEYS FROM "cpu" WHERE "region" = 'uswest' - --- show all tag keys where the host key = 'serverA' -SHOW TAG KEYS WHERE "host" = 'serverA' -``` - -### SHOW TAG VALUES - -``` -show_tag_values_stmt = "SHOW TAG VALUES" [ from_clause ] with_tag_clause [ where_clause ] - [ group_by_clause ] [ limit_clause ] [ offset_clause ] . -``` - -#### Examples: - -```sql --- show all tag values across all measurements for the region tag -SHOW TAG VALUES WITH KEY = "region" - --- show tag values from the cpu measurement for the region tag -SHOW TAG VALUES FROM "cpu" WITH KEY = "region" - --- show tag values across all measurements for all tag keys that do not include the letter c -SHOW TAG VALUES WITH KEY !~ /.*c.*/ - --- show tag values from the cpu measurement for region & host tag keys where service = 'redis' -SHOW TAG VALUES FROM "cpu" WITH KEY IN ("region", "host") WHERE "service" = 'redis' -``` - -### SHOW USERS - -``` -show_users_stmt = "SHOW USERS" . -``` - -#### Example: - -```sql --- show all users -SHOW USERS -``` - -### REVOKE - -``` -revoke_stmt = "REVOKE" privilege [ on_clause ] "FROM" user_name . -``` - -#### Examples: - -```sql --- revoke admin privileges from jdoe -REVOKE ALL PRIVILEGES FROM "jdoe" - --- revoke read privileges from jdoe on mydb -REVOKE READ ON "mydb" FROM "jdoe" -``` - -### SELECT - -``` -select_stmt = "SELECT" fields from_clause [ into_clause ] [ where_clause ] - [ group_by_clause ] [ order_by_clause ] [ limit_clause ] - [ offset_clause ] [ slimit_clause ] [ soffset_clause ] . -``` - -#### Examples: - -```sql --- select mean value from the cpu measurement where region = 'uswest' grouped by 10 minute intervals -SELECT mean("value") FROM "cpu" WHERE "region" = 'uswest' GROUP BY time(10m) fill(0) - --- select from all measurements beginning with cpu into the same measurement name in the cpu_1h retention policy -SELECT mean("value") INTO "cpu_1h".:MEASUREMENT FROM /cpu.*/ -``` - -## Clauses - -``` -from_clause = "FROM" measurements . - -group_by_clause = "GROUP BY" dimensions fill(fill_option). - -into_clause = "INTO" ( measurement | back_ref ). - -limit_clause = "LIMIT" int_lit . - -offset_clause = "OFFSET" int_lit . - -slimit_clause = "SLIMIT" int_lit . - -soffset_clause = "SOFFSET" int_lit . - -on_clause = "ON" db_name . - -order_by_clause = "ORDER BY" sort_fields . - -to_clause = "TO" user_name . - -where_clause = "WHERE" expr . - -with_measurement_clause = "WITH MEASUREMENT" ( "=" measurement | "=~" regex_lit ) . - -with_tag_clause = "WITH KEY" ( "=" tag_key | "!=" tag_key | "=~" regex_lit | "IN (" tag_keys ")" ) . -``` - -## Expressions - -``` -binary_op = "+" | "-" | "*" | "/" | "AND" | "OR" | "=" | "!=" | "<>" | "<" | - "<=" | ">" | ">=" . - -expr = unary_expr { binary_op unary_expr } . - -unary_expr = "(" expr ")" | var_ref | time_lit | string_lit | int_lit | - float_lit | bool_lit | duration_lit | regex_lit . -``` - -## Other - -``` -alias = "AS" identifier . - -back_ref = ( policy_name ".:MEASUREMENT" ) | - ( db_name "." [ policy_name ] ".:MEASUREMENT" ) . - -db_name = identifier . - -dimension = expr . - -dimensions = dimension { "," dimension } . - -field_key = identifier . - -field = expr [ alias ] . - -fields = field { "," field } . - -fill_option = "null" | "none" | "previous" | "linear" | int_lit | float_lit . - -host = string_lit . - -measurement = measurement_name | - ( policy_name "." measurement_name ) | - ( db_name "." [ policy_name ] "." measurement_name ) . - -measurements = measurement { "," measurement } . - -measurement_name = identifier | regex_lit . - -password = string_lit . - -policy_name = identifier . - -privilege = "ALL" [ "PRIVILEGES" ] | "READ" | "WRITE" . - -query_id = int_lit . - -query_name = identifier . - -retention_policy = identifier . - -retention_policy_option = retention_policy_duration | - retention_policy_replication | - retention_policy_shard_group_duration | - "DEFAULT" . - -retention_policy_duration = "DURATION" duration_lit . - -retention_policy_replication = "REPLICATION" int_lit . - -retention_policy_shard_group_duration = "SHARD DURATION" duration_lit . - -retention_policy_name = "NAME" identifier . - -series_id = int_lit . - -shard_id = int_lit . - -sort_field = field_key [ ASC | DESC ] . - -sort_fields = sort_field { "," sort_field } . - -subscription_name = identifier . - -tag_key = identifier . - -tag_keys = tag_key { "," tag_key } . - -user_name = identifier . - -var_ref = measurement . -``` - -## Query Engine Internals - -Once you understand the language itself, it's important to know how these -language constructs are implemented in the query engine. This gives you an -intuitive sense for how results will be processed and how to create efficient -queries. - -The life cycle of a query looks like this: - -1. InfluxQL query string is tokenized and then parsed into an abstract syntax - tree (AST). This is the code representation of the query itself. - -2. The AST is passed to the `QueryExecutor` which directs queries to the - appropriate handlers. For example, queries related to meta data are executed - by the meta service and `SELECT` statements are executed by the shards - themselves. - -3. The query engine then determines the shards that match the `SELECT` - statement's time range. From these shards, iterators are created for each - field in the statement. - -4. Iterators are passed to the emitter which drains them and joins the resulting - points. The emitter's job is to convert simple time/value points into the - more complex result objects that are returned to the client. - - -### Understanding Iterators - -Iterators are at the heart of the query engine. They provide a simple interface -for looping over a set of points. For example, this is an iterator over Float -points: - -``` -type FloatIterator interface { - Next() *FloatPoint -} -``` - -These iterators are created through the `IteratorCreator` interface: - -``` -type IteratorCreator interface { - CreateIterator(opt *IteratorOptions) (Iterator, error) -} -``` - -The `IteratorOptions` provide arguments about field selection, time ranges, -and dimensions that the iterator creator can use when planning an iterator. -The `IteratorCreator` interface is used at many levels such as the `Shards`, -`Shard`, and `Engine`. This allows optimizations to be performed when applicable -such as returning a precomputed `COUNT()`. - -Iterators aren't just for reading raw data from storage though. Iterators can be -composed so that they provided additional functionality around an input -iterator. For example, a `DistinctIterator` can compute the distinct values for -each time window for an input iterator. Or a `FillIterator` can generate -additional points that are missing from an input iterator. - -This composition also lends itself well to aggregation. For example, a statement -such as this: - -``` -SELECT MEAN(value) FROM cpu GROUP BY time(10m) -``` - -In this case, `MEAN(value)` is a `MeanIterator` wrapping an iterator from the -underlying shards. However, if we can add an additional iterator to determine -the derivative of the mean: - -``` -SELECT DERIVATIVE(MEAN(value), 20m) FROM cpu GROUP BY time(10m) -``` - - -### Understanding Auxiliary Fields - -Because InfluxQL allows users to use selector functions such as `FIRST()`, -`LAST()`, `MIN()`, and `MAX()`, the engine must provide a way to return related -data at the same time with the selected point. - -For example, in this query: - -``` -SELECT FIRST(value), host FROM cpu GROUP BY time(1h) -``` - -We are selecting the first `value` that occurs every hour but we also want to -retrieve the `host` associated with that point. Since the `Point` types only -specify a single typed `Value` for efficiency, we push the `host` into the -auxiliary fields of the point. These auxiliary fields are attached to the point -until it is passed to the emitter where the fields get split off to their own -iterator. - - -### Built-in Iterators - -There are many helper iterators that let us build queries: - -* Merge Iterator - This iterator combines one or more iterators into a single - new iterator of the same type. This iterator guarantees that all points - within a window will be output before starting the next window but does not - provide ordering guarantees within the window. This allows for fast access - for aggregate queries which do not need stronger sorting guarantees. - -* Sorted Merge Iterator - This iterator also combines one or more iterators - into a new iterator of the same type. However, this iterator guarantees - time ordering of every point. This makes it slower than the `MergeIterator` - but this ordering guarantee is required for non-aggregate queries which - return the raw data points. - -* Limit Iterator - This iterator limits the number of points per name/tag - group. This is the implementation of the `LIMIT` & `OFFSET` syntax. - -* Fill Iterator - This iterator injects extra points if they are missing from - the input iterator. It can provide `null` points, points with the previous - value, or points with a specific value. - -* Buffered Iterator - This iterator provides the ability to "unread" a point - back onto a buffer so it can be read again next time. This is used extensively - to provide lookahead for windowing. - -* Reduce Iterator - This iterator calls a reduction function for each point in - a window. When the window is complete then all points for that window are - output. This is used for simple aggregate functions such as `COUNT()`. - -* Reduce Slice Iterator - This iterator collects all points for a window first - and then passes them all to a reduction function at once. The results are - returned from the iterator. This is used for aggregate functions such as - `DERIVATIVE()`. - -* Transform Iterator - This iterator calls a transform function for each point - from an input iterator. This is used for executing binary expressions. - -* Dedupe Iterator - This iterator only outputs unique points. It is resource - intensive so it is only used for small queries such as meta query statements. - - -### Call Iterators - -Function calls in InfluxQL are implemented at two levels. Some calls can be -wrapped at multiple layers to improve efficiency. For example, a `COUNT()` can -be performed at the shard level and then multiple `CountIterator`s can be -wrapped with another `CountIterator` to compute the count of all shards. These -iterators can be created using `NewCallIterator()`. - -Some iterators are more complex or need to be implemented at a higher level. -For example, the `DERIVATIVE()` needs to retrieve all points for a window first -before performing the calculation. This iterator is created by the engine itself -and is never requested to be created by the lower levels. diff --git a/vendor/github.com/influxdata/influxdb/influxql/ast.go b/vendor/github.com/influxdata/influxdb/influxql/ast.go deleted file mode 100644 index d7f3573f..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/ast.go +++ /dev/null @@ -1,4754 +0,0 @@ -package influxql - -import ( - "bytes" - "errors" - "fmt" - "regexp" - "regexp/syntax" - "sort" - "strconv" - "strings" - "time" - - "github.com/gogo/protobuf/proto" - internal "github.com/influxdata/influxdb/influxql/internal" -) - -// DataType represents the primitive data types available in InfluxQL. -type DataType int - -const ( - // Unknown primitive data type. - Unknown DataType = 0 - // Float means the data type is a float - Float = 1 - // Integer means the data type is a integer - Integer = 2 - // String means the data type is a string of text. - String = 3 - // Boolean means the data type is a boolean. - Boolean = 4 - // Time means the data type is a time. - Time = 5 - // Duration means the data type is a duration of time. - Duration = 6 - // Tag means the data type is a tag. - Tag = 7 - // AnyField means the data type is any field. - AnyField = 8 -) - -var ( - // ErrInvalidTime is returned when the timestamp string used to - // compare against time field is invalid. - ErrInvalidTime = errors.New("invalid timestamp string") -) - -// InspectDataType returns the data type of a given value. -func InspectDataType(v interface{}) DataType { - switch v.(type) { - case float64: - return Float - case int64, int32, int: - return Integer - case string: - return String - case bool: - return Boolean - case time.Time: - return Time - case time.Duration: - return Duration - default: - return Unknown - } -} - -// InspectDataTypes returns all of the data types for an interface slice. -func InspectDataTypes(a []interface{}) []DataType { - dta := make([]DataType, len(a)) - for i, v := range a { - dta[i] = InspectDataType(v) - } - return dta -} - -func (d DataType) String() string { - switch d { - case Float: - return "float" - case Integer: - return "integer" - case String: - return "string" - case Boolean: - return "boolean" - case Time: - return "time" - case Duration: - return "duration" - case Tag: - return "tag" - case AnyField: - return "field" - } - return "unknown" -} - -// Node represents a node in the InfluxDB abstract syntax tree. -type Node interface { - node() - String() string -} - -func (*Query) node() {} -func (Statements) node() {} - -func (*AlterRetentionPolicyStatement) node() {} -func (*CreateContinuousQueryStatement) node() {} -func (*CreateDatabaseStatement) node() {} -func (*CreateRetentionPolicyStatement) node() {} -func (*CreateSubscriptionStatement) node() {} -func (*CreateUserStatement) node() {} -func (*Distinct) node() {} -func (*DeleteSeriesStatement) node() {} -func (*DeleteStatement) node() {} -func (*DropContinuousQueryStatement) node() {} -func (*DropDatabaseStatement) node() {} -func (*DropMeasurementStatement) node() {} -func (*DropRetentionPolicyStatement) node() {} -func (*DropSeriesStatement) node() {} -func (*DropShardStatement) node() {} -func (*DropSubscriptionStatement) node() {} -func (*DropUserStatement) node() {} -func (*GrantStatement) node() {} -func (*GrantAdminStatement) node() {} -func (*KillQueryStatement) node() {} -func (*RevokeStatement) node() {} -func (*RevokeAdminStatement) node() {} -func (*SelectStatement) node() {} -func (*SetPasswordUserStatement) node() {} -func (*ShowContinuousQueriesStatement) node() {} -func (*ShowGrantsForUserStatement) node() {} -func (*ShowDatabasesStatement) node() {} -func (*ShowFieldKeysStatement) node() {} -func (*ShowRetentionPoliciesStatement) node() {} -func (*ShowMeasurementsStatement) node() {} -func (*ShowQueriesStatement) node() {} -func (*ShowSeriesStatement) node() {} -func (*ShowShardGroupsStatement) node() {} -func (*ShowShardsStatement) node() {} -func (*ShowStatsStatement) node() {} -func (*ShowSubscriptionsStatement) node() {} -func (*ShowDiagnosticsStatement) node() {} -func (*ShowTagKeysStatement) node() {} -func (*ShowTagValuesStatement) node() {} -func (*ShowUsersStatement) node() {} - -func (*BinaryExpr) node() {} -func (*BooleanLiteral) node() {} -func (*Call) node() {} -func (*Dimension) node() {} -func (Dimensions) node() {} -func (*DurationLiteral) node() {} -func (*IntegerLiteral) node() {} -func (*Field) node() {} -func (Fields) node() {} -func (*Measurement) node() {} -func (Measurements) node() {} -func (*nilLiteral) node() {} -func (*NumberLiteral) node() {} -func (*ParenExpr) node() {} -func (*RegexLiteral) node() {} -func (*ListLiteral) node() {} -func (*SortField) node() {} -func (SortFields) node() {} -func (Sources) node() {} -func (*StringLiteral) node() {} -func (*Target) node() {} -func (*TimeLiteral) node() {} -func (*VarRef) node() {} -func (*Wildcard) node() {} - -// Query represents a collection of ordered statements. -type Query struct { - Statements Statements -} - -// String returns a string representation of the query. -func (q *Query) String() string { return q.Statements.String() } - -// Statements represents a list of statements. -type Statements []Statement - -// String returns a string representation of the statements. -func (a Statements) String() string { - var str []string - for _, stmt := range a { - str = append(str, stmt.String()) - } - return strings.Join(str, ";\n") -} - -// Statement represents a single command in InfluxQL. -type Statement interface { - Node - stmt() - RequiredPrivileges() (ExecutionPrivileges, error) -} - -// HasDefaultDatabase provides an interface to get the default database from a Statement. -type HasDefaultDatabase interface { - Node - stmt() - DefaultDatabase() string -} - -// ExecutionPrivilege is a privilege required for a user to execute -// a statement on a database or resource. -type ExecutionPrivilege struct { - // Admin privilege required. - Admin bool - - // Name of the database. - Name string - - // Database privilege required. - Privilege Privilege -} - -// ExecutionPrivileges is a list of privileges required to execute a statement. -type ExecutionPrivileges []ExecutionPrivilege - -func (*AlterRetentionPolicyStatement) stmt() {} -func (*CreateContinuousQueryStatement) stmt() {} -func (*CreateDatabaseStatement) stmt() {} -func (*CreateRetentionPolicyStatement) stmt() {} -func (*CreateSubscriptionStatement) stmt() {} -func (*CreateUserStatement) stmt() {} -func (*DeleteSeriesStatement) stmt() {} -func (*DeleteStatement) stmt() {} -func (*DropContinuousQueryStatement) stmt() {} -func (*DropDatabaseStatement) stmt() {} -func (*DropMeasurementStatement) stmt() {} -func (*DropRetentionPolicyStatement) stmt() {} -func (*DropSeriesStatement) stmt() {} -func (*DropSubscriptionStatement) stmt() {} -func (*DropUserStatement) stmt() {} -func (*GrantStatement) stmt() {} -func (*GrantAdminStatement) stmt() {} -func (*KillQueryStatement) stmt() {} -func (*ShowContinuousQueriesStatement) stmt() {} -func (*ShowGrantsForUserStatement) stmt() {} -func (*ShowDatabasesStatement) stmt() {} -func (*ShowFieldKeysStatement) stmt() {} -func (*ShowMeasurementsStatement) stmt() {} -func (*ShowQueriesStatement) stmt() {} -func (*ShowRetentionPoliciesStatement) stmt() {} -func (*ShowSeriesStatement) stmt() {} -func (*ShowShardGroupsStatement) stmt() {} -func (*ShowShardsStatement) stmt() {} -func (*ShowStatsStatement) stmt() {} -func (*DropShardStatement) stmt() {} -func (*ShowSubscriptionsStatement) stmt() {} -func (*ShowDiagnosticsStatement) stmt() {} -func (*ShowTagKeysStatement) stmt() {} -func (*ShowTagValuesStatement) stmt() {} -func (*ShowUsersStatement) stmt() {} -func (*RevokeStatement) stmt() {} -func (*RevokeAdminStatement) stmt() {} -func (*SelectStatement) stmt() {} -func (*SetPasswordUserStatement) stmt() {} - -// Expr represents an expression that can be evaluated to a value. -type Expr interface { - Node - expr() -} - -func (*BinaryExpr) expr() {} -func (*BooleanLiteral) expr() {} -func (*Call) expr() {} -func (*Distinct) expr() {} -func (*DurationLiteral) expr() {} -func (*IntegerLiteral) expr() {} -func (*nilLiteral) expr() {} -func (*NumberLiteral) expr() {} -func (*ParenExpr) expr() {} -func (*RegexLiteral) expr() {} -func (*ListLiteral) expr() {} -func (*StringLiteral) expr() {} -func (*TimeLiteral) expr() {} -func (*VarRef) expr() {} -func (*Wildcard) expr() {} - -// Literal represents a static literal. -type Literal interface { - Expr - literal() -} - -func (*BooleanLiteral) literal() {} -func (*DurationLiteral) literal() {} -func (*IntegerLiteral) literal() {} -func (*nilLiteral) literal() {} -func (*NumberLiteral) literal() {} -func (*RegexLiteral) literal() {} -func (*ListLiteral) literal() {} -func (*StringLiteral) literal() {} -func (*TimeLiteral) literal() {} - -// Source represents a source of data for a statement. -type Source interface { - Node - source() -} - -func (*Measurement) source() {} - -// Sources represents a list of sources. -type Sources []Source - -// Names returns a list of source names. -func (a Sources) Names() []string { - names := make([]string, 0, len(a)) - for _, s := range a { - switch s := s.(type) { - case *Measurement: - names = append(names, s.Name) - } - } - return names -} - -// Filter returns a list of source names filtered by the database/retention policy. -func (a Sources) Filter(database, retentionPolicy string) []Source { - sources := make([]Source, 0, len(a)) - for _, s := range a { - switch s := s.(type) { - case *Measurement: - if s.Database == database && s.RetentionPolicy == retentionPolicy { - sources = append(sources, s) - } - } - } - return sources -} - -// HasSystemSource returns true if any of the sources are internal, system sources. -func (a Sources) HasSystemSource() bool { - for _, s := range a { - switch s := s.(type) { - case *Measurement: - if IsSystemName(s.Name) { - return true - } - } - } - return false -} - -// HasRegex returns true if any of the sources are regex measurements. -func (a Sources) HasRegex() bool { - for _, s := range a { - switch s := s.(type) { - case *Measurement: - if s.Regex != nil { - return true - } - } - } - return false -} - -// String returns a string representation of a Sources array. -func (a Sources) String() string { - var buf bytes.Buffer - - ubound := len(a) - 1 - for i, src := range a { - _, _ = buf.WriteString(src.String()) - if i < ubound { - _, _ = buf.WriteString(", ") - } - } - - return buf.String() -} - -// MarshalBinary encodes a list of sources to a binary format. -func (a Sources) MarshalBinary() ([]byte, error) { - var pb internal.Measurements - pb.Items = make([]*internal.Measurement, len(a)) - for i, source := range a { - pb.Items[i] = encodeMeasurement(source.(*Measurement)) - } - return proto.Marshal(&pb) -} - -// UnmarshalBinary decodes binary data into a list of sources. -func (a *Sources) UnmarshalBinary(buf []byte) error { - var pb internal.Measurements - if err := proto.Unmarshal(buf, &pb); err != nil { - return err - } - *a = make(Sources, len(pb.GetItems())) - for i := range pb.GetItems() { - mm, err := decodeMeasurement(pb.GetItems()[i]) - if err != nil { - return err - } - (*a)[i] = mm - } - return nil -} - -// IsSystemName returns true if name is an internal system name. -func IsSystemName(name string) bool { - switch name { - case "_fieldKeys", - "_measurements", - "_series", - "_tagKeys", - "_tags": - return true - default: - return false - } -} - -// SortField represents a field to sort results by. -type SortField struct { - // Name of the field - Name string - - // Sort order. - Ascending bool -} - -// String returns a string representation of a sort field -func (field *SortField) String() string { - var buf bytes.Buffer - if field.Name != "" { - _, _ = buf.WriteString(field.Name) - _, _ = buf.WriteString(" ") - } - if field.Ascending { - _, _ = buf.WriteString("ASC") - } else { - _, _ = buf.WriteString("DESC") - } - return buf.String() -} - -// SortFields represents an ordered list of ORDER BY fields -type SortFields []*SortField - -// String returns a string representation of sort fields -func (a SortFields) String() string { - fields := make([]string, 0, len(a)) - for _, field := range a { - fields = append(fields, field.String()) - } - return strings.Join(fields, ", ") -} - -// CreateDatabaseStatement represents a command for creating a new database. -type CreateDatabaseStatement struct { - // Name of the database to be created. - Name string - - // RetentionPolicyCreate indicates whether the user explicitly wants to create a retention policy - RetentionPolicyCreate bool - - // RetentionPolicyDuration indicates retention duration for the new database - RetentionPolicyDuration *time.Duration - - // RetentionPolicyReplication indicates retention replication for the new database - RetentionPolicyReplication *int - - // RetentionPolicyName indicates retention name for the new database - RetentionPolicyName string - - // RetentionPolicyShardGroupDuration indicates shard group duration for the new database - RetentionPolicyShardGroupDuration time.Duration -} - -// String returns a string representation of the create database statement. -func (s *CreateDatabaseStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("CREATE DATABASE ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - if s.RetentionPolicyCreate { - _, _ = buf.WriteString(" WITH") - if s.RetentionPolicyDuration != nil { - _, _ = buf.WriteString(" DURATION ") - _, _ = buf.WriteString(s.RetentionPolicyDuration.String()) - } - if s.RetentionPolicyReplication != nil { - _, _ = buf.WriteString(" REPLICATION ") - _, _ = buf.WriteString(strconv.Itoa(*s.RetentionPolicyReplication)) - } - if s.RetentionPolicyShardGroupDuration > 0 { - _, _ = buf.WriteString(" SHARD DURATION ") - _, _ = buf.WriteString(s.RetentionPolicyShardGroupDuration.String()) - } - if s.RetentionPolicyName != "" { - _, _ = buf.WriteString(" NAME ") - _, _ = buf.WriteString(QuoteIdent(s.RetentionPolicyName)) - } - } - - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a CreateDatabaseStatement. -func (s *CreateDatabaseStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// DropDatabaseStatement represents a command to drop a database. -type DropDatabaseStatement struct { - // Name of the database to be dropped. - Name string -} - -// String returns a string representation of the drop database statement. -func (s *DropDatabaseStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("DROP DATABASE ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a DropDatabaseStatement. -func (s *DropDatabaseStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// DropRetentionPolicyStatement represents a command to drop a retention policy from a database. -type DropRetentionPolicyStatement struct { - // Name of the policy to drop. - Name string - - // Name of the database to drop the policy from. - Database string -} - -// String returns a string representation of the drop retention policy statement. -func (s *DropRetentionPolicyStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("DROP RETENTION POLICY ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Database)) - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a DropRetentionPolicyStatement. -func (s *DropRetentionPolicyStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: WritePrivilege}}, nil -} - -// CreateUserStatement represents a command for creating a new user. -type CreateUserStatement struct { - // Name of the user to be created. - Name string - - // User's password. - Password string - - // User's admin privilege. - Admin bool -} - -// String returns a string representation of the create user statement. -func (s *CreateUserStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("CREATE USER ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - _, _ = buf.WriteString(" WITH PASSWORD ") - _, _ = buf.WriteString("[REDACTED]") - if s.Admin { - _, _ = buf.WriteString(" WITH ALL PRIVILEGES") - } - return buf.String() -} - -// RequiredPrivileges returns the privilege(s) required to execute a CreateUserStatement. -func (s *CreateUserStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// DropUserStatement represents a command for dropping a user. -type DropUserStatement struct { - // Name of the user to drop. - Name string -} - -// String returns a string representation of the drop user statement. -func (s *DropUserStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("DROP USER ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - return buf.String() -} - -// RequiredPrivileges returns the privilege(s) required to execute a DropUserStatement. -func (s *DropUserStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// Privilege is a type of action a user can be granted the right to use. -type Privilege int - -const ( - // NoPrivileges means no privileges required / granted / revoked. - NoPrivileges Privilege = iota - // ReadPrivilege means read privilege required / granted / revoked. - ReadPrivilege - // WritePrivilege means write privilege required / granted / revoked. - WritePrivilege - // AllPrivileges means all privileges required / granted / revoked. - AllPrivileges -) - -// NewPrivilege returns an initialized *Privilege. -func NewPrivilege(p Privilege) *Privilege { return &p } - -// String returns a string representation of a Privilege. -func (p Privilege) String() string { - switch p { - case NoPrivileges: - return "NO PRIVILEGES" - case ReadPrivilege: - return "READ" - case WritePrivilege: - return "WRITE" - case AllPrivileges: - return "ALL PRIVILEGES" - } - return "" -} - -// GrantStatement represents a command for granting a privilege. -type GrantStatement struct { - // The privilege to be granted. - Privilege Privilege - - // Database to grant the privilege to. - On string - - // Who to grant the privilege to. - User string -} - -// String returns a string representation of the grant statement. -func (s *GrantStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("GRANT ") - _, _ = buf.WriteString(s.Privilege.String()) - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.On)) - _, _ = buf.WriteString(" TO ") - _, _ = buf.WriteString(QuoteIdent(s.User)) - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a GrantStatement. -func (s *GrantStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// GrantAdminStatement represents a command for granting admin privilege. -type GrantAdminStatement struct { - // Who to grant the privilege to. - User string -} - -// String returns a string representation of the grant admin statement. -func (s *GrantAdminStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("GRANT ALL PRIVILEGES TO ") - _, _ = buf.WriteString(QuoteIdent(s.User)) - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a GrantAdminStatement. -func (s *GrantAdminStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// KillQueryStatement represents a command for killing a query. -type KillQueryStatement struct { - // The query to kill. - QueryID uint64 - - // The host to delegate the kill to. - Host string -} - -// String returns a string representation of the kill query statement. -func (s *KillQueryStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("KILL QUERY ") - _, _ = buf.WriteString(strconv.FormatUint(s.QueryID, 10)) - if s.Host != "" { - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Host)) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a KillQueryStatement. -func (s *KillQueryStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// SetPasswordUserStatement represents a command for changing user password. -type SetPasswordUserStatement struct { - // Plain Password - Password string - - // Who to grant the privilege to. - Name string -} - -// String returns a string representation of the set password statement. -func (s *SetPasswordUserStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SET PASSWORD FOR ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - _, _ = buf.WriteString(" = ") - _, _ = buf.WriteString("[REDACTED]") - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a SetPasswordUserStatement. -func (s *SetPasswordUserStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// RevokeStatement represents a command to revoke a privilege from a user. -type RevokeStatement struct { - // The privilege to be revoked. - Privilege Privilege - - // Database to revoke the privilege from. - On string - - // Who to revoke privilege from. - User string -} - -// String returns a string representation of the revoke statement. -func (s *RevokeStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("REVOKE ") - _, _ = buf.WriteString(s.Privilege.String()) - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.On)) - _, _ = buf.WriteString(" FROM ") - _, _ = buf.WriteString(QuoteIdent(s.User)) - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a RevokeStatement. -func (s *RevokeStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// RevokeAdminStatement represents a command to revoke admin privilege from a user. -type RevokeAdminStatement struct { - // Who to revoke admin privilege from. - User string -} - -// String returns a string representation of the revoke admin statement. -func (s *RevokeAdminStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("REVOKE ALL PRIVILEGES FROM ") - _, _ = buf.WriteString(QuoteIdent(s.User)) - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a RevokeAdminStatement. -func (s *RevokeAdminStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// CreateRetentionPolicyStatement represents a command to create a retention policy. -type CreateRetentionPolicyStatement struct { - // Name of policy to create. - Name string - - // Name of database this policy belongs to. - Database string - - // Duration data written to this policy will be retained. - Duration time.Duration - - // Replication factor for data written to this policy. - Replication int - - // Should this policy be set as default for the database? - Default bool - - // Shard Duration - ShardGroupDuration time.Duration -} - -// String returns a string representation of the create retention policy. -func (s *CreateRetentionPolicyStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("CREATE RETENTION POLICY ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Database)) - _, _ = buf.WriteString(" DURATION ") - _, _ = buf.WriteString(FormatDuration(s.Duration)) - _, _ = buf.WriteString(" REPLICATION ") - _, _ = buf.WriteString(strconv.Itoa(s.Replication)) - if s.ShardGroupDuration > 0 { - _, _ = buf.WriteString(" SHARD DURATION ") - _, _ = buf.WriteString(FormatDuration(s.ShardGroupDuration)) - } - if s.Default { - _, _ = buf.WriteString(" DEFAULT") - } - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a CreateRetentionPolicyStatement. -func (s *CreateRetentionPolicyStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// AlterRetentionPolicyStatement represents a command to alter an existing retention policy. -type AlterRetentionPolicyStatement struct { - // Name of policy to alter. - Name string - - // Name of the database this policy belongs to. - Database string - - // Duration data written to this policy will be retained. - Duration *time.Duration - - // Replication factor for data written to this policy. - Replication *int - - // Should this policy be set as defalut for the database? - Default bool - - // Duration of the Shard - ShardGroupDuration *time.Duration -} - -// String returns a string representation of the alter retention policy statement. -func (s *AlterRetentionPolicyStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("ALTER RETENTION POLICY ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Database)) - - if s.Duration != nil { - _, _ = buf.WriteString(" DURATION ") - _, _ = buf.WriteString(FormatDuration(*s.Duration)) - } - - if s.Replication != nil { - _, _ = buf.WriteString(" REPLICATION ") - _, _ = buf.WriteString(strconv.Itoa(*s.Replication)) - } - - if s.ShardGroupDuration != nil { - _, _ = buf.WriteString(" SHARD DURATION ") - _, _ = buf.WriteString(FormatDuration(*s.ShardGroupDuration)) - } - - if s.Default { - _, _ = buf.WriteString(" DEFAULT") - } - - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute an AlterRetentionPolicyStatement. -func (s *AlterRetentionPolicyStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// FillOption represents different options for aggregate windows. -type FillOption int - -const ( - // NullFill means that empty aggregate windows will just have null values. - NullFill FillOption = iota - // NoFill means that empty aggregate windows will be purged from the result. - NoFill - // NumberFill means that empty aggregate windows will be filled with the given number - NumberFill - // PreviousFill means that empty aggregate windows will be filled with whatever the previous aggregate window had - PreviousFill - // LinearFill means that empty aggregate windows will be filled with whatever a linear value between non null windows - LinearFill -) - -// SelectStatement represents a command for extracting data from the database. -type SelectStatement struct { - // Expressions returned from the selection. - Fields Fields - - // Target (destination) for the result of the select. - Target *Target - - // Expressions used for grouping the selection. - Dimensions Dimensions - - // Data sources that fields are extracted from. - Sources Sources - - // An expression evaluated on data point. - Condition Expr - - // Fields to sort results by - SortFields SortFields - - // Maximum number of rows to be returned. Unlimited if zero. - Limit int - - // Returns rows starting at an offset from the first row. - Offset int - - // Maxiumum number of series to be returned. Unlimited if zero. - SLimit int - - // Returns series starting at an offset from the first one. - SOffset int - - // memoize the group by interval - groupByInterval time.Duration - - // if it's a query for raw data values (i.e. not an aggregate) - IsRawQuery bool - - // What fill option the select statement uses, if any - Fill FillOption - - // The value to fill empty aggregate buckets with, if any - FillValue interface{} - - // Renames the implicit time field name. - TimeAlias string - - // Removes the "time" column from the output. - OmitTime bool - - // Removes duplicate rows from raw queries. - Dedupe bool -} - -// HasDerivative returns true if one of the function calls in the statement is a -// derivative aggregate -func (s *SelectStatement) HasDerivative() bool { - for _, f := range s.FunctionCalls() { - if f.Name == "derivative" || f.Name == "non_negative_derivative" { - return true - } - } - return false -} - -// IsSimpleDerivative return true if one of the function call is a derivative function with a -// variable ref as the first arg -func (s *SelectStatement) IsSimpleDerivative() bool { - for _, f := range s.FunctionCalls() { - if f.Name == "derivative" || f.Name == "non_negative_derivative" { - // it's nested if the first argument is an aggregate function - if _, ok := f.Args[0].(*VarRef); ok { - return true - } - } - } - return false -} - -// TimeAscending returns true if the time field is sorted in chronological order. -func (s *SelectStatement) TimeAscending() bool { - return len(s.SortFields) == 0 || s.SortFields[0].Ascending -} - -// TimeFieldName returns the name of the time field. -func (s *SelectStatement) TimeFieldName() string { - if s.TimeAlias != "" { - return s.TimeAlias - } - return "time" -} - -// Clone returns a deep copy of the statement. -func (s *SelectStatement) Clone() *SelectStatement { - clone := *s - clone.Fields = make(Fields, 0, len(s.Fields)) - clone.Dimensions = make(Dimensions, 0, len(s.Dimensions)) - clone.Sources = cloneSources(s.Sources) - clone.SortFields = make(SortFields, 0, len(s.SortFields)) - clone.Condition = CloneExpr(s.Condition) - - if s.Target != nil { - clone.Target = &Target{ - Measurement: &Measurement{ - Database: s.Target.Measurement.Database, - RetentionPolicy: s.Target.Measurement.RetentionPolicy, - Name: s.Target.Measurement.Name, - Regex: CloneRegexLiteral(s.Target.Measurement.Regex), - }, - } - } - for _, f := range s.Fields { - clone.Fields = append(clone.Fields, &Field{Expr: CloneExpr(f.Expr), Alias: f.Alias}) - } - for _, d := range s.Dimensions { - clone.Dimensions = append(clone.Dimensions, &Dimension{Expr: CloneExpr(d.Expr)}) - } - for _, f := range s.SortFields { - clone.SortFields = append(clone.SortFields, &SortField{Name: f.Name, Ascending: f.Ascending}) - } - return &clone -} - -func cloneSources(sources Sources) Sources { - clone := make(Sources, 0, len(sources)) - for _, s := range sources { - clone = append(clone, cloneSource(s)) - } - return clone -} - -func cloneSource(s Source) Source { - if s == nil { - return nil - } - - switch s := s.(type) { - case *Measurement: - m := &Measurement{Database: s.Database, RetentionPolicy: s.RetentionPolicy, Name: s.Name} - if s.Regex != nil { - m.Regex = &RegexLiteral{Val: regexp.MustCompile(s.Regex.Val.String())} - } - return m - default: - panic("unreachable") - } -} - -// RewriteFields returns the re-written form of the select statement. Any wildcard query -// fields are replaced with the supplied fields, and any wildcard GROUP BY fields are replaced -// with the supplied dimensions. Any fields with no type specifier are rewritten with the -// appropriate type. -func (s *SelectStatement) RewriteFields(ic IteratorCreator) (*SelectStatement, error) { - // Retrieve a list of unique field and dimensions. - fieldSet, dimensionSet, err := ic.FieldDimensions(s.Sources) - if err != nil { - return s, err - } - - // Rewrite all variable references in the fields with their types if one - // hasn't been specified. - rewrite := func(n Node) { - ref, ok := n.(*VarRef) - if !ok || (ref.Type != Unknown && ref.Type != AnyField) { - return - } - - if typ, ok := fieldSet[ref.Val]; ok { - ref.Type = typ - } else if ref.Type != AnyField { - if _, ok := dimensionSet[ref.Val]; ok { - ref.Type = Tag - } - } - } - WalkFunc(s.Fields, rewrite) - WalkFunc(s.Condition, rewrite) - - // Ignore if there are no wildcards. - hasFieldWildcard := s.HasFieldWildcard() - hasDimensionWildcard := s.HasDimensionWildcard() - if !hasFieldWildcard && !hasDimensionWildcard { - return s, nil - } - - // If there are no dimension wildcards then merge dimensions to fields. - if !hasDimensionWildcard { - // Remove the dimensions present in the group by so they don't get added as fields. - for _, d := range s.Dimensions { - switch expr := d.Expr.(type) { - case *VarRef: - if _, ok := dimensionSet[expr.Val]; ok { - delete(dimensionSet, expr.Val) - } - } - } - } - - // Sort the field and dimension names for wildcard expansion. - var fields []VarRef - if len(fieldSet) > 0 { - fields = make([]VarRef, 0, len(fieldSet)) - for name, typ := range fieldSet { - fields = append(fields, VarRef{Val: name, Type: typ}) - } - if !hasDimensionWildcard { - for name := range dimensionSet { - fields = append(fields, VarRef{Val: name, Type: Tag}) - } - dimensionSet = nil - } - sort.Sort(VarRefs(fields)) - } - dimensions := stringSetSlice(dimensionSet) - - other := s.Clone() - - // Rewrite all wildcard query fields - if hasFieldWildcard { - // Allocate a slice assuming there is exactly one wildcard for efficiency. - rwFields := make(Fields, 0, len(s.Fields)+len(fields)-1) - for _, f := range s.Fields { - switch expr := f.Expr.(type) { - case *Wildcard: - for _, ref := range fields { - if expr.Type == FIELD && ref.Type == Tag { - continue - } else if expr.Type == TAG && ref.Type != Tag { - continue - } - rwFields = append(rwFields, &Field{Expr: &VarRef{Val: ref.Val, Type: ref.Type}}) - } - case *RegexLiteral: - for _, ref := range fields { - if expr.Val.MatchString(ref.Val) { - rwFields = append(rwFields, &Field{Expr: &VarRef{Val: ref.Val, Type: ref.Type}}) - } - } - case *Call: - // Clone a template that we can modify and use for new fields. - template := CloneExpr(expr).(*Call) - - // Search for the call with a wildcard by continuously descending until - // we no longer have a call. - call := template - for len(call.Args) > 0 { - arg, ok := call.Args[0].(*Call) - if !ok { - break - } - call = arg - } - - // Check if this field value is a wildcard. - if len(call.Args) == 0 { - rwFields = append(rwFields, f) - continue - } - - // Retrieve if this is a wildcard or a regular expression. - var re *regexp.Regexp - switch expr := call.Args[0].(type) { - case *Wildcard: - if expr.Type == TAG { - return s, fmt.Errorf("unable to use tag wildcard in %s()", call.Name) - } - case *RegexLiteral: - re = expr.Val - default: - rwFields = append(rwFields, f) - continue - } - - // All types that can expand wildcards support float and integer. - supportedTypes := map[DataType]struct{}{ - Float: struct{}{}, - Integer: struct{}{}, - } - - // Add additional types for certain functions. - switch call.Name { - case "count", "first", "last", "distinct", "elapsed", "mode", "sample": - supportedTypes[String] = struct{}{} - fallthrough - case "min", "max": - supportedTypes[Boolean] = struct{}{} - } - - for _, ref := range fields { - // Do not expand tags within a function call. It likely won't do anything - // anyway and will be the wrong thing in 99% of cases. - if ref.Type == Tag { - continue - } else if _, ok := supportedTypes[ref.Type]; !ok { - continue - } else if re != nil && !re.MatchString(ref.Val) { - continue - } - - // Make a new expression and replace the wildcard within this cloned expression. - call.Args[0] = &VarRef{Val: ref.Val, Type: ref.Type} - rwFields = append(rwFields, &Field{ - Expr: CloneExpr(template), - Alias: fmt.Sprintf("%s_%s", f.Name(), ref.Val), - }) - } - default: - rwFields = append(rwFields, f) - } - } - other.Fields = rwFields - } - - // Rewrite all wildcard GROUP BY fields - if hasDimensionWildcard { - // Allocate a slice assuming there is exactly one wildcard for efficiency. - rwDimensions := make(Dimensions, 0, len(s.Dimensions)+len(dimensions)-1) - for _, d := range s.Dimensions { - switch expr := d.Expr.(type) { - case *Wildcard: - for _, name := range dimensions { - rwDimensions = append(rwDimensions, &Dimension{Expr: &VarRef{Val: name}}) - } - case *RegexLiteral: - for _, name := range dimensions { - if expr.Val.MatchString(name) { - rwDimensions = append(rwDimensions, &Dimension{Expr: &VarRef{Val: name}}) - } - } - default: - rwDimensions = append(rwDimensions, d) - } - } - other.Dimensions = rwDimensions - } - - return other, nil -} - -// RewriteRegexExprs rewrites regex conditions to make better use of the -// database index. -// -// Conditions that can currently be simplified are: -// -// - host =~ /^foo$/ becomes host = 'foo' -// - host !~ /^foo$/ becomes host != 'foo' -// -// Note: if the regex contains groups, character classes, repetition or -// similar, it's likely it won't be rewritten. In order to support rewriting -// regexes with these characters would be a lot more work. -func (s *SelectStatement) RewriteRegexConditions() { - s.Condition = RewriteExpr(s.Condition, func(e Expr) Expr { - be, ok := e.(*BinaryExpr) - if !ok || (be.Op != EQREGEX && be.Op != NEQREGEX) { - // This expression is not a binary condition or doesn't have a - // regex based operator. - return e - } - - // Handle regex-based condition. - rhs := be.RHS.(*RegexLiteral) // This must be a regex. - - val, ok := matchExactRegex(rhs.Val.String()) - if !ok { - // Regex didn't match. - return e - } - - // Remove leading and trailing ^ and $. - be.RHS = &StringLiteral{Val: val} - - // Update the condition operator. - if be.Op == EQREGEX { - be.Op = EQ - } else { - be.Op = NEQ - } - return be - }) -} - -// matchExactRegex matches regexes that have the following form: /^foo$/. It -// considers /^$/ to be a matching regex. -func matchExactRegex(v string) (string, bool) { - re, err := syntax.Parse(v, syntax.Perl) - if err != nil { - // Nothing we can do or log. - return "", false - } - - if re.Op != syntax.OpConcat { - return "", false - } - - if len(re.Sub) < 2 || len(re.Sub) > 3 { - // Regex has too few or too many subexpressions. - return "", false - } - - start := re.Sub[0] - if !(start.Op == syntax.OpBeginLine || start.Op == syntax.OpBeginText) { - // Regex does not begin with ^ - return "", false - } - - end := re.Sub[len(re.Sub)-1] - if !(end.Op == syntax.OpEndLine || end.Op == syntax.OpEndText) { - // Regex does not end with $ - return "", false - } - - if len(re.Sub) == 3 { - middle := re.Sub[1] - if middle.Op != syntax.OpLiteral { - // Regex does not contain a literal op. - return "", false - } - - // We can rewrite this regex. - return string(middle.Rune), true - } - - // The regex /^$/ - return "", true -} - -// RewriteDistinct rewrites the expression to be a call for map/reduce to work correctly -// This method assumes all validation has passed -func (s *SelectStatement) RewriteDistinct() { - WalkFunc(s.Fields, func(n Node) { - switch n := n.(type) { - case *Field: - if expr, ok := n.Expr.(*Distinct); ok { - n.Expr = expr.NewCall() - s.IsRawQuery = false - } - case *Call: - for i, arg := range n.Args { - if arg, ok := arg.(*Distinct); ok { - n.Args[i] = arg.NewCall() - } - } - } - }) -} - -// RewriteTimeFields removes any "time" field references. -func (s *SelectStatement) RewriteTimeFields() { - for i := 0; i < len(s.Fields); i++ { - switch expr := s.Fields[i].Expr.(type) { - case *VarRef: - if expr.Val == "time" { - s.TimeAlias = s.Fields[i].Alias - s.Fields = append(s.Fields[:i], s.Fields[i+1:]...) - } - } - } -} - -// ColumnNames will walk all fields and functions and return the appropriate field names for the select statement -// while maintaining order of the field names -func (s *SelectStatement) ColumnNames() []string { - // First walk each field to determine the number of columns. - columnFields := Fields{} - for _, field := range s.Fields { - columnFields = append(columnFields, field) - - switch f := field.Expr.(type) { - case *Call: - if f.Name == "top" || f.Name == "bottom" { - for _, arg := range f.Args[1:] { - ref, ok := arg.(*VarRef) - if ok { - columnFields = append(columnFields, &Field{Expr: ref}) - } - } - } - } - } - - // Determine if we should add an extra column for an implicit time. - offset := 0 - if !s.OmitTime { - offset++ - } - - columnNames := make([]string, len(columnFields)+offset) - if !s.OmitTime { - // Add the implicit time if requested. - columnNames[0] = s.TimeFieldName() - } - - // Keep track of the encountered column names. - names := make(map[string]int) - - // Resolve aliases first. - for i, col := range columnFields { - if col.Alias != "" { - columnNames[i+offset] = col.Alias - names[col.Alias] = 1 - } - } - - // Resolve any generated names and resolve conflicts. - for i, col := range columnFields { - if columnNames[i+offset] != "" { - continue - } - - name := col.Name() - count, conflict := names[name] - if conflict { - for { - resolvedName := fmt.Sprintf("%s_%d", name, count) - _, conflict = names[resolvedName] - if !conflict { - names[name] = count + 1 - name = resolvedName - break - } - count++ - } - } - names[name]++ - columnNames[i+offset] = name - } - return columnNames -} - -// HasTimeFieldSpecified will walk all fields and determine if the user explicitly asked for time -// This is needed to determine re-write behaviors for functions like TOP and BOTTOM -func (s *SelectStatement) HasTimeFieldSpecified() bool { - for _, f := range s.Fields { - if f.Name() == "time" { - return true - } - } - return false -} - -// String returns a string representation of the select statement. -func (s *SelectStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SELECT ") - _, _ = buf.WriteString(s.Fields.String()) - - if s.Target != nil { - _, _ = buf.WriteString(" ") - _, _ = buf.WriteString(s.Target.String()) - } - if len(s.Sources) > 0 { - _, _ = buf.WriteString(" FROM ") - _, _ = buf.WriteString(s.Sources.String()) - } - if s.Condition != nil { - _, _ = buf.WriteString(" WHERE ") - _, _ = buf.WriteString(s.Condition.String()) - } - if len(s.Dimensions) > 0 { - _, _ = buf.WriteString(" GROUP BY ") - _, _ = buf.WriteString(s.Dimensions.String()) - } - switch s.Fill { - case NoFill: - _, _ = buf.WriteString(" fill(none)") - case NumberFill: - _, _ = buf.WriteString(fmt.Sprintf(" fill(%v)", s.FillValue)) - case LinearFill: - _, _ = buf.WriteString(" fill(linear)") - case PreviousFill: - _, _ = buf.WriteString(" fill(previous)") - } - if len(s.SortFields) > 0 { - _, _ = buf.WriteString(" ORDER BY ") - _, _ = buf.WriteString(s.SortFields.String()) - } - if s.Limit > 0 { - _, _ = fmt.Fprintf(&buf, " LIMIT %d", s.Limit) - } - if s.Offset > 0 { - _, _ = buf.WriteString(" OFFSET ") - _, _ = buf.WriteString(strconv.Itoa(s.Offset)) - } - if s.SLimit > 0 { - _, _ = fmt.Fprintf(&buf, " SLIMIT %d", s.SLimit) - } - if s.SOffset > 0 { - _, _ = fmt.Fprintf(&buf, " SOFFSET %d", s.SOffset) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute the SelectStatement. -// NOTE: Statement should be normalized first (database name(s) in Sources and -// Target should be populated). If the statement has not been normalized, an -// empty string will be returned for the database name and it is up to the caller -// to interpret that as the default database. -func (s *SelectStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - ep := ExecutionPrivileges{} - for _, source := range s.Sources { - measurement, ok := source.(*Measurement) - if !ok { - return nil, fmt.Errorf("invalid measurement: %s", source) - } - - ep = append(ep, ExecutionPrivilege{ - Name: measurement.Database, - Privilege: ReadPrivilege, - }) - } - - if s.Target != nil { - p := ExecutionPrivilege{Admin: false, Name: s.Target.Measurement.Database, Privilege: WritePrivilege} - ep = append(ep, p) - } - return ep, nil -} - -// HasWildcard returns whether or not the select statement has at least 1 wildcard -func (s *SelectStatement) HasWildcard() bool { - return s.HasFieldWildcard() || s.HasDimensionWildcard() -} - -// HasFieldWildcard returns whether or not the select statement has at least 1 wildcard in the fields -func (s *SelectStatement) HasFieldWildcard() (hasWildcard bool) { - WalkFunc(s.Fields, func(n Node) { - if hasWildcard { - return - } - switch n.(type) { - case *Wildcard, *RegexLiteral: - hasWildcard = true - } - }) - return hasWildcard -} - -// HasDimensionWildcard returns whether or not the select statement has -// at least 1 wildcard in the dimensions aka `GROUP BY` -func (s *SelectStatement) HasDimensionWildcard() bool { - for _, d := range s.Dimensions { - switch d.Expr.(type) { - case *Wildcard, *RegexLiteral: - return true - } - } - - return false -} - -func (s *SelectStatement) validate(tr targetRequirement) error { - if err := s.validateFields(); err != nil { - return err - } - - if err := s.validateDimensions(); err != nil { - return err - } - - if err := s.validateDistinct(); err != nil { - return err - } - - if err := s.validateAggregates(tr); err != nil { - return err - } - - return nil -} - -func (s *SelectStatement) validateFields() error { - ns := s.NamesInSelect() - if len(ns) == 1 && ns[0] == "time" { - return fmt.Errorf("at least 1 non-time field must be queried") - } - - for _, f := range s.Fields { - switch expr := f.Expr.(type) { - case *BinaryExpr: - if err := expr.validate(); err != nil { - return err - } - } - } - return nil -} - -func (s *SelectStatement) validateDimensions() error { - var dur time.Duration - for _, dim := range s.Dimensions { - switch expr := dim.Expr.(type) { - case *Call: - // Ensure the call is time() and it has one or two duration arguments. - // If we already have a duration - if expr.Name != "time" { - return errors.New("only time() calls allowed in dimensions") - } else if got := len(expr.Args); got < 1 || got > 2 { - return errors.New("time dimension expected 1 or 2 arguments") - } else if lit, ok := expr.Args[0].(*DurationLiteral); !ok { - return errors.New("time dimension must have duration argument") - } else if dur != 0 { - return errors.New("multiple time dimensions not allowed") - } else { - dur = lit.Val - if len(expr.Args) == 2 { - switch lit := expr.Args[1].(type) { - case *DurationLiteral: - // noop - case *Call: - if lit.Name != "now" { - return errors.New("time dimension offset function must be now()") - } else if len(lit.Args) != 0 { - return errors.New("time dimension offset now() function requires no arguments") - } - default: - return errors.New("time dimension offset must be duration or now()") - } - } - } - case *VarRef: - if strings.ToLower(expr.Val) == "time" { - return errors.New("time() is a function and expects at least one argument") - } - case *Wildcard: - case *RegexLiteral: - default: - return errors.New("only time and tag dimensions allowed") - } - } - return nil -} - -// validSelectWithAggregate determines if a SELECT statement has the correct -// combination of aggregate functions combined with selected fields and tags -// Currently we don't have support for all aggregates, but aggregates that -// can be combined with fields/tags are: -// TOP, BOTTOM, MAX, MIN, FIRST, LAST -func (s *SelectStatement) validSelectWithAggregate() error { - calls := map[string]struct{}{} - numAggregates := 0 - for _, f := range s.Fields { - fieldCalls := walkFunctionCalls(f.Expr) - for _, c := range fieldCalls { - calls[c.Name] = struct{}{} - } - if len(fieldCalls) != 0 { - numAggregates++ - } - } - // For TOP, BOTTOM, MAX, MIN, FIRST, LAST, PERCENTILE (selector functions) it is ok to ask for fields and tags - // but only if one function is specified. Combining multiple functions and fields and tags is not currently supported - onlySelectors := true - for k := range calls { - switch k { - case "top", "bottom", "max", "min", "first", "last", "percentile", "sample": - default: - onlySelectors = false - break - } - } - if onlySelectors { - // If they only have one selector, they can have as many fields or tags as they want - if numAggregates == 1 { - return nil - } - // If they have multiple selectors, they are not allowed to have any other fields or tags specified - if numAggregates > 1 && len(s.Fields) != numAggregates { - return fmt.Errorf("mixing multiple selector functions with tags or fields is not supported") - } - } - - if numAggregates != 0 && numAggregates != len(s.Fields) { - return fmt.Errorf("mixing aggregate and non-aggregate queries is not supported") - } - return nil -} - -// validTopBottomAggr determines if TOP or BOTTOM aggregates have valid arguments. -func (s *SelectStatement) validTopBottomAggr(expr *Call) error { - if exp, got := 2, len(expr.Args); got < exp { - return fmt.Errorf("invalid number of arguments for %s, expected at least %d, got %d", expr.Name, exp, got) - } - if len(expr.Args) > 1 { - callLimit, ok := expr.Args[len(expr.Args)-1].(*IntegerLiteral) - if !ok { - return fmt.Errorf("expected integer as last argument in %s(), found %s", expr.Name, expr.Args[len(expr.Args)-1]) - } - // Check if they asked for a limit smaller than what they passed into the call - if int64(callLimit.Val) > int64(s.Limit) && s.Limit != 0 { - return fmt.Errorf("limit (%d) in %s function can not be larger than the LIMIT (%d) in the select statement", int64(callLimit.Val), expr.Name, int64(s.Limit)) - } - - for _, v := range expr.Args[:len(expr.Args)-1] { - if _, ok := v.(*VarRef); !ok { - return fmt.Errorf("only fields or tags are allowed in %s(), found %s", expr.Name, v) - } - } - } - return nil -} - -// validPercentileAggr determines if PERCENTILE have valid arguments. -func (s *SelectStatement) validPercentileAggr(expr *Call) error { - if err := s.validSelectWithAggregate(); err != nil { - return err - } - if exp, got := 2, len(expr.Args); got != exp { - return fmt.Errorf("invalid number of arguments for %s, expected %d, got %d", expr.Name, exp, got) - } - - switch expr.Args[0].(type) { - case *VarRef, *RegexLiteral, *Wildcard: - // do nothing - default: - return fmt.Errorf("expected field argument in percentile()") - } - - switch expr.Args[1].(type) { - case *IntegerLiteral, *NumberLiteral: - return nil - default: - return fmt.Errorf("expected float argument in percentile()") - } -} - -// validPercentileAggr determines if PERCENTILE have valid arguments. -func (s *SelectStatement) validSampleAggr(expr *Call) error { - if err := s.validSelectWithAggregate(); err != nil { - return err - } - if exp, got := 2, len(expr.Args); got != exp { - return fmt.Errorf("invalid number of arguments for %s, expected %d, got %d", expr.Name, exp, got) - } - - switch expr.Args[0].(type) { - case *VarRef, *RegexLiteral, *Wildcard: - // do nothing - default: - return fmt.Errorf("expected field argument in sample()") - } - - switch expr.Args[1].(type) { - case *IntegerLiteral: - return nil - default: - return fmt.Errorf("expected integer argument in sample()") - } -} - -func (s *SelectStatement) validateAggregates(tr targetRequirement) error { - for _, f := range s.Fields { - for _, expr := range walkFunctionCalls(f.Expr) { - switch expr.Name { - case "derivative", "non_negative_derivative", "difference", "moving_average", "cumulative_sum", "elapsed": - if err := s.validSelectWithAggregate(); err != nil { - return err - } - switch expr.Name { - case "derivative", "non_negative_derivative", "elapsed": - if min, max, got := 1, 2, len(expr.Args); got > max || got < min { - return fmt.Errorf("invalid number of arguments for %s, expected at least %d but no more than %d, got %d", expr.Name, min, max, got) - } - // If a duration arg is passed, make sure it's a duration - if len(expr.Args) == 2 { - // Second must be a duration .e.g (1h) - if _, ok := expr.Args[1].(*DurationLiteral); !ok { - return fmt.Errorf("second argument to %s must be a duration, got %T", expr.Name, expr.Args[1]) - } - } - case "difference", "cumulative_sum": - if got := len(expr.Args); got != 1 { - return fmt.Errorf("invalid number of arguments for %s, expected 1, got %d", expr.Name, got) - } - case "moving_average": - if got := len(expr.Args); got != 2 { - return fmt.Errorf("invalid number of arguments for moving_average, expected 2, got %d", got) - } - - if lit, ok := expr.Args[1].(*IntegerLiteral); !ok { - return fmt.Errorf("second argument for moving_average must be an integer, got %T", expr.Args[1]) - } else if lit.Val <= 1 { - return fmt.Errorf("moving_average window must be greater than 1, got %d", lit.Val) - } else if int64(int(lit.Val)) != lit.Val { - return fmt.Errorf("moving_average window too large, got %d", lit.Val) - } - } - // Validate that if they have grouping by time, they need a sub-call like min/max, etc. - groupByInterval, err := s.GroupByInterval() - if err != nil { - return fmt.Errorf("invalid group interval: %v", err) - } - - if c, ok := expr.Args[0].(*Call); ok && groupByInterval == 0 { - return fmt.Errorf("%s aggregate requires a GROUP BY interval", expr.Name) - } else if !ok && groupByInterval > 0 { - return fmt.Errorf("aggregate function required inside the call to %s", expr.Name) - } else if ok { - switch c.Name { - case "top", "bottom": - if err := s.validTopBottomAggr(c); err != nil { - return err - } - case "percentile": - if err := s.validPercentileAggr(c); err != nil { - return err - } - default: - if exp, got := 1, len(c.Args); got != exp { - return fmt.Errorf("invalid number of arguments for %s, expected %d, got %d", c.Name, exp, got) - } - - switch fc := c.Args[0].(type) { - case *VarRef, *Wildcard, *RegexLiteral: - // do nothing - case *Call: - if fc.Name != "distinct" || expr.Name != "count" { - return fmt.Errorf("expected field argument in %s()", c.Name) - } else if exp, got := 1, len(fc.Args); got != exp { - return fmt.Errorf("count(distinct %s) can only have %d argument(s), got %d", fc.Name, exp, got) - } else if _, ok := fc.Args[0].(*VarRef); !ok { - return fmt.Errorf("expected field argument in distinct()") - } - case *Distinct: - if expr.Name != "count" { - return fmt.Errorf("expected field argument in %s()", c.Name) - } - default: - return fmt.Errorf("expected field argument in %s()", c.Name) - } - } - } - case "top", "bottom": - if err := s.validTopBottomAggr(expr); err != nil { - return err - } - case "percentile": - if err := s.validPercentileAggr(expr); err != nil { - return err - } - case "sample": - if err := s.validSampleAggr(expr); err != nil { - return err - } - case "holt_winters", "holt_winters_with_fit": - if exp, got := 3, len(expr.Args); got != exp { - return fmt.Errorf("invalid number of arguments for %s, expected %d, got %d", expr.Name, exp, got) - } - // Validate that if they have grouping by time, they need a sub-call like min/max, etc. - groupByInterval, err := s.GroupByInterval() - if err != nil { - return fmt.Errorf("invalid group interval: %v", err) - } - - if _, ok := expr.Args[0].(*Call); ok && groupByInterval == 0 { - return fmt.Errorf("%s aggregate requires a GROUP BY interval", expr.Name) - } else if !ok { - return fmt.Errorf("must use aggregate function with %s", expr.Name) - } - if arg, ok := expr.Args[1].(*IntegerLiteral); !ok { - return fmt.Errorf("expected integer argument as second arg in %s", expr.Name) - } else if arg.Val <= 0 { - return fmt.Errorf("second arg to %s must be greater than 0, got %d", expr.Name, arg.Val) - } - if _, ok := expr.Args[2].(*IntegerLiteral); !ok { - return fmt.Errorf("expected integer argument as third arg in %s", expr.Name) - } - default: - if err := s.validSelectWithAggregate(); err != nil { - return err - } - if exp, got := 1, len(expr.Args); got != exp { - // Special error message if distinct was used as the argument. - if expr.Name == "count" && got >= 1 { - if _, ok := expr.Args[0].(*Distinct); ok { - return fmt.Errorf("count(distinct ) can only have one argument") - } - } - return fmt.Errorf("invalid number of arguments for %s, expected %d, got %d", expr.Name, exp, got) - } - switch fc := expr.Args[0].(type) { - case *VarRef, *Wildcard, *RegexLiteral: - // do nothing - case *Call: - if fc.Name != "distinct" || expr.Name != "count" { - return fmt.Errorf("expected field argument in %s()", expr.Name) - } else if exp, got := 1, len(fc.Args); got != exp { - return fmt.Errorf("count(distinct ) can only have one argument") - } else if _, ok := fc.Args[0].(*VarRef); !ok { - return fmt.Errorf("expected field argument in distinct()") - } - case *Distinct: - if expr.Name != "count" { - return fmt.Errorf("expected field argument in %s()", expr.Name) - } - default: - return fmt.Errorf("expected field argument in %s()", expr.Name) - } - } - } - } - - // Check that we have valid duration and where clauses for aggregates - - // fetch the group by duration - groupByDuration, _ := s.GroupByInterval() - - // If we have a group by interval, but no aggregate function, it's an invalid statement - if s.IsRawQuery && groupByDuration > 0 { - return fmt.Errorf("GROUP BY requires at least one aggregate function") - } - - // If we have an aggregate function with a group by time without a where clause, it's an invalid statement - if tr == targetNotRequired { // ignore create continuous query statements - if !s.IsRawQuery && groupByDuration > 0 && !HasTimeExpr(s.Condition) { - return fmt.Errorf("aggregate functions with GROUP BY time require a WHERE time clause") - } - } - return nil -} - -// HasDistinct checks if a select statement contains DISTINCT -func (s *SelectStatement) HasDistinct() bool { - // determine if we have a call named distinct - for _, f := range s.Fields { - switch c := f.Expr.(type) { - case *Call: - if c.Name == "distinct" { - return true - } - case *Distinct: - return true - } - } - return false -} - -func (s *SelectStatement) validateDistinct() error { - if !s.HasDistinct() { - return nil - } - - if len(s.Fields) > 1 { - return fmt.Errorf("aggregate function distinct() can not be combined with other functions or fields") - } - - switch c := s.Fields[0].Expr.(type) { - case *Call: - if len(c.Args) == 0 { - return fmt.Errorf("distinct function requires at least one argument") - } - - if len(c.Args) != 1 { - return fmt.Errorf("distinct function can only have one argument") - } - } - return nil -} - -// GroupByInterval extracts the time interval, if specified. -func (s *SelectStatement) GroupByInterval() (time.Duration, error) { - // return if we've already pulled it out - if s.groupByInterval != 0 { - return s.groupByInterval, nil - } - - // Ignore if there are no dimensions. - if len(s.Dimensions) == 0 { - return 0, nil - } - - for _, d := range s.Dimensions { - if call, ok := d.Expr.(*Call); ok && call.Name == "time" { - // Make sure there is exactly one argument. - if got := len(call.Args); got < 1 || got > 2 { - return 0, errors.New("time dimension expected 1 or 2 arguments") - } - - // Ensure the argument is a duration. - lit, ok := call.Args[0].(*DurationLiteral) - if !ok { - return 0, errors.New("time dimension must have duration argument") - } - s.groupByInterval = lit.Val - return lit.Val, nil - } - } - return 0, nil -} - -// GroupByOffset extracts the time interval offset, if specified. -func (s *SelectStatement) GroupByOffset() (time.Duration, error) { - interval, err := s.GroupByInterval() - if err != nil { - return 0, err - } - - // Ignore if there are no dimensions. - if len(s.Dimensions) == 0 { - return 0, nil - } - - for _, d := range s.Dimensions { - if call, ok := d.Expr.(*Call); ok && call.Name == "time" { - if len(call.Args) == 2 { - switch expr := call.Args[1].(type) { - case *DurationLiteral: - return expr.Val % interval, nil - case *TimeLiteral: - return expr.Val.Sub(expr.Val.Truncate(interval)), nil - default: - return 0, fmt.Errorf("invalid time dimension offset: %s", expr) - } - } - return 0, nil - } - } - return 0, nil -} - -// SetTimeRange sets the start and end time of the select statement to [start, end). i.e. start inclusive, end exclusive. -// This is used commonly for continuous queries so the start and end are in buckets. -func (s *SelectStatement) SetTimeRange(start, end time.Time) error { - cond := fmt.Sprintf("time >= '%s' AND time < '%s'", start.UTC().Format(time.RFC3339Nano), end.UTC().Format(time.RFC3339Nano)) - if s.Condition != nil { - cond = fmt.Sprintf("%s AND %s", s.rewriteWithoutTimeDimensions(), cond) - } - - expr, err := NewParser(strings.NewReader(cond)).ParseExpr() - if err != nil { - return err - } - - // fold out any previously replaced time dimensios and set the condition - s.Condition = Reduce(expr, nil) - - return nil -} - -// rewriteWithoutTimeDimensions will remove any WHERE time... clauses from the select statement -// This is necessary when setting an explicit time range to override any that previously existed. -func (s *SelectStatement) rewriteWithoutTimeDimensions() string { - n := RewriteFunc(s.Condition, func(n Node) Node { - switch n := n.(type) { - case *BinaryExpr: - if n.LHS.String() == "time" { - return &BooleanLiteral{Val: true} - } - return n - case *Call: - return &BooleanLiteral{Val: true} - default: - return n - } - }) - - return n.String() -} - -// NamesInWhere returns the field and tag names (idents) referenced in the where clause -func (s *SelectStatement) NamesInWhere() []string { - var a []string - if s.Condition != nil { - a = walkNames(s.Condition) - } - return a -} - -// NamesInSelect returns the field and tag names (idents) in the select clause -func (s *SelectStatement) NamesInSelect() []string { - var a []string - - for _, f := range s.Fields { - a = append(a, walkNames(f.Expr)...) - } - - return a -} - -// NamesInDimension returns the field and tag names (idents) in the group by -func (s *SelectStatement) NamesInDimension() []string { - var a []string - - for _, d := range s.Dimensions { - a = append(a, walkNames(d.Expr)...) - } - - return a -} - -// LimitTagSets returns a tag set list with SLIMIT and SOFFSET applied. -func LimitTagSets(a []*TagSet, slimit, soffset int) []*TagSet { - // Ignore if no limit or offset is specified. - if slimit == 0 && soffset == 0 { - return a - } - - // If offset is beyond the number of tag sets then return nil. - if soffset > len(a) { - return nil - } - - // Clamp limit to the max number of tag sets. - if soffset+slimit > len(a) { - slimit = len(a) - soffset - } - return a[soffset : soffset+slimit] -} - -// walkNames will walk the Expr and return the database fields -func walkNames(exp Expr) []string { - switch expr := exp.(type) { - case *VarRef: - return []string{expr.Val} - case *Call: - var a []string - for _, expr := range expr.Args { - if ref, ok := expr.(*VarRef); ok { - a = append(a, ref.Val) - } - } - return a - case *BinaryExpr: - var ret []string - ret = append(ret, walkNames(expr.LHS)...) - ret = append(ret, walkNames(expr.RHS)...) - return ret - case *ParenExpr: - return walkNames(expr.Expr) - } - - return nil -} - -// walkRefs will walk the Expr and return the database fields -func walkRefs(exp Expr) []VarRef { - switch expr := exp.(type) { - case *VarRef: - return []VarRef{*expr} - case *Call: - a := make([]VarRef, 0, len(expr.Args)) - for _, expr := range expr.Args { - if ref, ok := expr.(*VarRef); ok { - a = append(a, *ref) - } - } - return a - case *BinaryExpr: - lhs := walkRefs(expr.LHS) - rhs := walkRefs(expr.RHS) - ret := make([]VarRef, 0, len(lhs)+len(rhs)) - ret = append(ret, lhs...) - ret = append(ret, rhs...) - return ret - case *ParenExpr: - return walkRefs(expr.Expr) - } - - return nil -} - -// ExprNames returns a list of non-"time" field names from an expression. -func ExprNames(expr Expr) []VarRef { - m := make(map[VarRef]struct{}) - for _, ref := range walkRefs(expr) { - if ref.Val == "time" { - continue - } - m[ref] = struct{}{} - } - - a := make([]VarRef, 0, len(m)) - for k := range m { - a = append(a, k) - } - sort.Sort(VarRefs(a)) - - return a -} - -// FunctionCalls returns the Call objects from the query -func (s *SelectStatement) FunctionCalls() []*Call { - var a []*Call - for _, f := range s.Fields { - a = append(a, walkFunctionCalls(f.Expr)...) - } - return a -} - -// FunctionCallsByPosition returns the Call objects from the query in the order they appear in the select statement -func (s *SelectStatement) FunctionCallsByPosition() [][]*Call { - var a [][]*Call - for _, f := range s.Fields { - a = append(a, walkFunctionCalls(f.Expr)) - } - return a -} - -// walkFunctionCalls walks the Field of a query for any function calls made -func walkFunctionCalls(exp Expr) []*Call { - switch expr := exp.(type) { - case *VarRef: - return nil - case *Call: - return []*Call{expr} - case *BinaryExpr: - var ret []*Call - ret = append(ret, walkFunctionCalls(expr.LHS)...) - ret = append(ret, walkFunctionCalls(expr.RHS)...) - return ret - case *ParenExpr: - return walkFunctionCalls(expr.Expr) - } - - return nil -} - -// filters an expression to exclude expressions unrelated to a source. -func filterExprBySource(name string, expr Expr) Expr { - switch expr := expr.(type) { - case *VarRef: - if !strings.HasPrefix(expr.Val, name) { - return nil - } - - case *BinaryExpr: - lhs := filterExprBySource(name, expr.LHS) - rhs := filterExprBySource(name, expr.RHS) - - // If an expr is logical then return either LHS/RHS or both. - // If an expr is arithmetic or comparative then require both sides. - if expr.Op == AND || expr.Op == OR { - if lhs == nil && rhs == nil { - return nil - } else if lhs != nil && rhs == nil { - return lhs - } else if lhs == nil && rhs != nil { - return rhs - } - } else { - if lhs == nil || rhs == nil { - return nil - } - } - return &BinaryExpr{Op: expr.Op, LHS: lhs, RHS: rhs} - - case *ParenExpr: - exp := filterExprBySource(name, expr.Expr) - if exp == nil { - return nil - } - return &ParenExpr{Expr: exp} - } - return expr -} - -// MatchSource returns the source name that matches a field name. -// Returns a blank string if no sources match. -func MatchSource(sources Sources, name string) string { - for _, src := range sources { - switch src := src.(type) { - case *Measurement: - if strings.HasPrefix(name, src.Name) { - return src.Name - } - } - } - return "" -} - -// Target represents a target (destination) policy, measurement, and DB. -type Target struct { - // Measurement to write into. - Measurement *Measurement -} - -// String returns a string representation of the Target. -func (t *Target) String() string { - if t == nil { - return "" - } - - var buf bytes.Buffer - _, _ = buf.WriteString("INTO ") - _, _ = buf.WriteString(t.Measurement.String()) - if t.Measurement.Name == "" { - _, _ = buf.WriteString(":MEASUREMENT") - } - - return buf.String() -} - -// DeleteStatement represents a command for removing data from the database. -type DeleteStatement struct { - // Data source that values are removed from. - Source Source - - // An expression evaluated on data point. - Condition Expr -} - -// String returns a string representation of the delete statement. -func (s *DeleteStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("DELETE FROM ") - _, _ = buf.WriteString(s.Source.String()) - if s.Condition != nil { - _, _ = buf.WriteString(" WHERE ") - _, _ = buf.WriteString(s.Condition.String()) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a DeleteStatement. -func (s *DeleteStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: WritePrivilege}}, nil -} - -// ShowSeriesStatement represents a command for listing series in the database. -type ShowSeriesStatement struct { - // Database to query. If blank, use the default database. - // The database can also be specified per source in the Sources. - Database string - - // Measurement(s) the series are listed for. - Sources Sources - - // An expression evaluated on a series name or tag. - Condition Expr - - // Fields to sort results by - SortFields SortFields - - // Maximum number of rows to be returned. - // Unlimited if zero. - Limit int - - // Returns rows starting at an offset from the first row. - Offset int -} - -// String returns a string representation of the list series statement. -func (s *ShowSeriesStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SHOW SERIES") - - if s.Database != "" { - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Database)) - } - if s.Sources != nil { - _, _ = buf.WriteString(" FROM ") - _, _ = buf.WriteString(s.Sources.String()) - } - - if s.Condition != nil { - _, _ = buf.WriteString(" WHERE ") - _, _ = buf.WriteString(s.Condition.String()) - } - if len(s.SortFields) > 0 { - _, _ = buf.WriteString(" ORDER BY ") - _, _ = buf.WriteString(s.SortFields.String()) - } - if s.Limit > 0 { - _, _ = buf.WriteString(" LIMIT ") - _, _ = buf.WriteString(strconv.Itoa(s.Limit)) - } - if s.Offset > 0 { - _, _ = buf.WriteString(" OFFSET ") - _, _ = buf.WriteString(strconv.Itoa(s.Offset)) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a ShowSeriesStatement. -func (s *ShowSeriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil -} - -// DropSeriesStatement represents a command for removing a series from the database. -type DropSeriesStatement struct { - // Data source that fields are extracted from (optional) - Sources Sources - - // An expression evaluated on data point (optional) - Condition Expr -} - -// String returns a string representation of the drop series statement. -func (s *DropSeriesStatement) String() string { - var buf bytes.Buffer - buf.WriteString("DROP SERIES") - - if s.Sources != nil { - buf.WriteString(" FROM ") - buf.WriteString(s.Sources.String()) - } - if s.Condition != nil { - buf.WriteString(" WHERE ") - buf.WriteString(s.Condition.String()) - } - - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a DropSeriesStatement. -func (s DropSeriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: WritePrivilege}}, nil -} - -// DeleteSeriesStatement represents a command for deleting all or part of a series from a database. -type DeleteSeriesStatement struct { - // Data source that fields are extracted from (optional) - Sources Sources - - // An expression evaluated on data point (optional) - Condition Expr -} - -// String returns a string representation of the delete series statement. -func (s *DeleteSeriesStatement) String() string { - var buf bytes.Buffer - buf.WriteString("DELETE") - - if s.Sources != nil { - buf.WriteString(" FROM ") - buf.WriteString(s.Sources.String()) - } - if s.Condition != nil { - buf.WriteString(" WHERE ") - buf.WriteString(s.Condition.String()) - } - - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a DeleteSeriesStatement. -func (s DeleteSeriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: WritePrivilege}}, nil -} - -// DropShardStatement represents a command for removing a shard from -// the node. -type DropShardStatement struct { - // ID of the shard to be dropped. - ID uint64 -} - -// String returns a string representation of the drop series statement. -func (s *DropShardStatement) String() string { - var buf bytes.Buffer - buf.WriteString("DROP SHARD ") - buf.WriteString(strconv.FormatUint(s.ID, 10)) - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a -// DropShardStatement. -func (s *DropShardStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// ShowContinuousQueriesStatement represents a command for listing continuous queries. -type ShowContinuousQueriesStatement struct{} - -// String returns a string representation of the list continuous queries statement. -func (s *ShowContinuousQueriesStatement) String() string { return "SHOW CONTINUOUS QUERIES" } - -// RequiredPrivileges returns the privilege required to execute a ShowContinuousQueriesStatement. -func (s *ShowContinuousQueriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil -} - -// ShowGrantsForUserStatement represents a command for listing user privileges. -type ShowGrantsForUserStatement struct { - // Name of the user to display privileges. - Name string -} - -// String returns a string representation of the show grants for user. -func (s *ShowGrantsForUserStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SHOW GRANTS FOR ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a ShowGrantsForUserStatement -func (s *ShowGrantsForUserStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// ShowDatabasesStatement represents a command for listing all databases in the cluster. -type ShowDatabasesStatement struct{} - -// String returns a string representation of the list databases command. -func (s *ShowDatabasesStatement) String() string { return "SHOW DATABASES" } - -// RequiredPrivileges returns the privilege required to execute a ShowDatabasesStatement -func (s *ShowDatabasesStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// CreateContinuousQueryStatement represents a command for creating a continuous query. -type CreateContinuousQueryStatement struct { - // Name of the continuous query to be created. - Name string - - // Name of the database to create the continuous query on. - Database string - - // Source of data (SELECT statement). - Source *SelectStatement - - // Interval to resample previous queries - ResampleEvery time.Duration - - // Maximum duration to resample previous queries - ResampleFor time.Duration -} - -// String returns a string representation of the statement. -func (s *CreateContinuousQueryStatement) String() string { - var buf bytes.Buffer - fmt.Fprintf(&buf, "CREATE CONTINUOUS QUERY %s ON %s ", QuoteIdent(s.Name), QuoteIdent(s.Database)) - - if s.ResampleEvery > 0 || s.ResampleFor > 0 { - buf.WriteString("RESAMPLE ") - if s.ResampleEvery > 0 { - fmt.Fprintf(&buf, "EVERY %s ", FormatDuration(s.ResampleEvery)) - } - if s.ResampleFor > 0 { - fmt.Fprintf(&buf, "FOR %s ", FormatDuration(s.ResampleFor)) - } - } - fmt.Fprintf(&buf, "BEGIN %s END", s.Source.String()) - return buf.String() -} - -// DefaultDatabase returns the default database from the statement. -func (s *CreateContinuousQueryStatement) DefaultDatabase() string { - return s.Database -} - -// RequiredPrivileges returns the privilege required to execute a CreateContinuousQueryStatement. -func (s *CreateContinuousQueryStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - ep := ExecutionPrivileges{{Admin: false, Name: s.Database, Privilege: ReadPrivilege}} - - // Selecting into a database that's different from the source? - if s.Source.Target.Measurement.Database != "" { - // Change source database privilege requirement to read. - ep[0].Privilege = ReadPrivilege - - // Add destination database privilege requirement and set it to write. - p := ExecutionPrivilege{ - Admin: false, - Name: s.Source.Target.Measurement.Database, - Privilege: WritePrivilege, - } - ep = append(ep, p) - } - - return ep, nil -} - -func (s *CreateContinuousQueryStatement) validate() error { - interval, err := s.Source.GroupByInterval() - if err != nil { - return err - } - - if s.ResampleFor != 0 { - if s.ResampleEvery != 0 && s.ResampleEvery > interval { - interval = s.ResampleEvery - } - if interval > s.ResampleFor { - return fmt.Errorf("FOR duration must be >= GROUP BY time duration: must be a minimum of %s, got %s", FormatDuration(interval), FormatDuration(s.ResampleFor)) - } - } - return nil -} - -// DropContinuousQueryStatement represents a command for removing a continuous query. -type DropContinuousQueryStatement struct { - Name string - Database string -} - -// String returns a string representation of the statement. -func (s *DropContinuousQueryStatement) String() string { - return fmt.Sprintf("DROP CONTINUOUS QUERY %s ON %s", QuoteIdent(s.Name), QuoteIdent(s.Database)) -} - -// RequiredPrivileges returns the privilege(s) required to execute a DropContinuousQueryStatement -func (s *DropContinuousQueryStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: WritePrivilege}}, nil -} - -// ShowMeasurementsStatement represents a command for listing measurements. -type ShowMeasurementsStatement struct { - // Database to query. If blank, use the default database. - Database string - - // Measurement name or regex. - Source Source - - // An expression evaluated on data point. - Condition Expr - - // Fields to sort results by - SortFields SortFields - - // Maximum number of rows to be returned. - // Unlimited if zero. - Limit int - - // Returns rows starting at an offset from the first row. - Offset int -} - -// String returns a string representation of the statement. -func (s *ShowMeasurementsStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SHOW MEASUREMENTS") - - if s.Database != "" { - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(s.Database) - } - if s.Source != nil { - _, _ = buf.WriteString(" WITH MEASUREMENT ") - if m, ok := s.Source.(*Measurement); ok && m.Regex != nil { - _, _ = buf.WriteString("=~ ") - } else { - _, _ = buf.WriteString("= ") - } - _, _ = buf.WriteString(s.Source.String()) - } - if s.Condition != nil { - _, _ = buf.WriteString(" WHERE ") - _, _ = buf.WriteString(s.Condition.String()) - } - if len(s.SortFields) > 0 { - _, _ = buf.WriteString(" ORDER BY ") - _, _ = buf.WriteString(s.SortFields.String()) - } - if s.Limit > 0 { - _, _ = buf.WriteString(" LIMIT ") - _, _ = buf.WriteString(strconv.Itoa(s.Limit)) - } - if s.Offset > 0 { - _, _ = buf.WriteString(" OFFSET ") - _, _ = buf.WriteString(strconv.Itoa(s.Offset)) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege(s) required to execute a ShowMeasurementsStatement -func (s *ShowMeasurementsStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil -} - -// DropMeasurementStatement represents a command to drop a measurement. -type DropMeasurementStatement struct { - // Name of the measurement to be dropped. - Name string -} - -// String returns a string representation of the drop measurement statement. -func (s *DropMeasurementStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("DROP MEASUREMENT ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - return buf.String() -} - -// RequiredPrivileges returns the privilege(s) required to execute a DropMeasurementStatement -func (s *DropMeasurementStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// ShowQueriesStatement represents a command for listing all running queries. -type ShowQueriesStatement struct{} - -// String returns a string representation of the show queries statement. -func (s *ShowQueriesStatement) String() string { - return "SHOW QUERIES" -} - -// RequiredPrivileges returns the privilege required to execute a ShowQueriesStatement. -func (s *ShowQueriesStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil -} - -// ShowRetentionPoliciesStatement represents a command for listing retention policies. -type ShowRetentionPoliciesStatement struct { - // Name of the database to list policies for. - Database string -} - -// String returns a string representation of a ShowRetentionPoliciesStatement. -func (s *ShowRetentionPoliciesStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SHOW RETENTION POLICIES") - if s.Database != "" { - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Database)) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege(s) required to execute a ShowRetentionPoliciesStatement -func (s *ShowRetentionPoliciesStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil -} - -// ShowStatsStatement displays statistics for a given module. -type ShowStatsStatement struct { - // Module - Module string -} - -// String returns a string representation of a ShowStatsStatement. -func (s *ShowStatsStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SHOW STATS") - if s.Module != "" { - _, _ = buf.WriteString(" FOR ") - _, _ = buf.WriteString(QuoteString(s.Module)) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege(s) required to execute a ShowStatsStatement -func (s *ShowStatsStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// ShowShardGroupsStatement represents a command for displaying shard groups in the cluster. -type ShowShardGroupsStatement struct{} - -// String returns a string representation of the SHOW SHARD GROUPS command. -func (s *ShowShardGroupsStatement) String() string { return "SHOW SHARD GROUPS" } - -// RequiredPrivileges returns the privileges required to execute the statement. -func (s *ShowShardGroupsStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// ShowShardsStatement represents a command for displaying shards in the cluster. -type ShowShardsStatement struct{} - -// String returns a string representation. -func (s *ShowShardsStatement) String() string { return "SHOW SHARDS" } - -// RequiredPrivileges returns the privileges required to execute the statement. -func (s *ShowShardsStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// ShowDiagnosticsStatement represents a command for show node diagnostics. -type ShowDiagnosticsStatement struct { - // Module - Module string -} - -// String returns a string representation of the ShowDiagnosticsStatement. -func (s *ShowDiagnosticsStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SHOW DIAGNOSTICS") - if s.Module != "" { - _, _ = buf.WriteString(" FOR ") - _, _ = buf.WriteString(QuoteString(s.Module)) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a ShowDiagnosticsStatement -func (s *ShowDiagnosticsStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// CreateSubscriptionStatement represents a command to add a subscription to the incoming data stream -type CreateSubscriptionStatement struct { - Name string - Database string - RetentionPolicy string - Destinations []string - Mode string -} - -// String returns a string representation of the CreateSubscriptionStatement. -func (s *CreateSubscriptionStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("CREATE SUBSCRIPTION ") - _, _ = buf.WriteString(QuoteIdent(s.Name)) - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Database)) - _, _ = buf.WriteString(".") - _, _ = buf.WriteString(QuoteIdent(s.RetentionPolicy)) - _, _ = buf.WriteString(" DESTINATIONS ") - _, _ = buf.WriteString(s.Mode) - _, _ = buf.WriteString(" ") - for i, dest := range s.Destinations { - if i != 0 { - _, _ = buf.WriteString(", ") - } - _, _ = buf.WriteString(QuoteString(dest)) - } - - return buf.String() -} - -// RequiredPrivileges returns the privilege required to execute a CreateSubscriptionStatement -func (s *CreateSubscriptionStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// DropSubscriptionStatement represents a command to drop a subscription to the incoming data stream. -type DropSubscriptionStatement struct { - Name string - Database string - RetentionPolicy string -} - -// String returns a string representation of the DropSubscriptionStatement. -func (s *DropSubscriptionStatement) String() string { - return fmt.Sprintf(`DROP SUBSCRIPTION %s ON %s.%s`, QuoteIdent(s.Name), QuoteIdent(s.Database), QuoteIdent(s.RetentionPolicy)) -} - -// RequiredPrivileges returns the privilege required to execute a DropSubscriptionStatement -func (s *DropSubscriptionStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// ShowSubscriptionsStatement represents a command to show a list of subscriptions. -type ShowSubscriptionsStatement struct { -} - -// String returns a string representation of the ShowSubscriptionStatement. -func (s *ShowSubscriptionsStatement) String() string { - return "SHOW SUBSCRIPTIONS" -} - -// RequiredPrivileges returns the privilege required to execute a ShowSubscriptionStatement -func (s *ShowSubscriptionsStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// ShowTagKeysStatement represents a command for listing tag keys. -type ShowTagKeysStatement struct { - // Database to query. If blank, use the default database. - // The database can also be specified per source in the Sources. - Database string - - // Data sources that fields are extracted from. - Sources Sources - - // An expression evaluated on data point. - Condition Expr - - // Fields to sort results by - SortFields SortFields - - // Maximum number of tag keys per measurement. Unlimited if zero. - Limit int - - // Returns tag keys starting at an offset from the first row. - Offset int - - // Maxiumum number of series to be returned. Unlimited if zero. - SLimit int - - // Returns series starting at an offset from the first one. - SOffset int -} - -// String returns a string representation of the statement. -func (s *ShowTagKeysStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SHOW TAG KEYS") - - if s.Database != "" { - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Database)) - } - if s.Sources != nil { - _, _ = buf.WriteString(" FROM ") - _, _ = buf.WriteString(s.Sources.String()) - } - if s.Condition != nil { - _, _ = buf.WriteString(" WHERE ") - _, _ = buf.WriteString(s.Condition.String()) - } - if len(s.SortFields) > 0 { - _, _ = buf.WriteString(" ORDER BY ") - _, _ = buf.WriteString(s.SortFields.String()) - } - if s.Limit > 0 { - _, _ = buf.WriteString(" LIMIT ") - _, _ = buf.WriteString(strconv.Itoa(s.Limit)) - } - if s.Offset > 0 { - _, _ = buf.WriteString(" OFFSET ") - _, _ = buf.WriteString(strconv.Itoa(s.Offset)) - } - if s.SLimit > 0 { - _, _ = buf.WriteString(" SLIMIT ") - _, _ = buf.WriteString(strconv.Itoa(s.SLimit)) - } - if s.SOffset > 0 { - _, _ = buf.WriteString(" SOFFSET ") - _, _ = buf.WriteString(strconv.Itoa(s.SOffset)) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege(s) required to execute a ShowTagKeysStatement -func (s *ShowTagKeysStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil -} - -// ShowTagValuesStatement represents a command for listing tag values. -type ShowTagValuesStatement struct { - // Database to query. If blank, use the default database. - // The database can also be specified per source in the Sources. - Database string - - // Data source that fields are extracted from. - Sources Sources - - // Operation to use when selecting tag key(s). - Op Token - - // Literal to compare the tag key(s) with. - TagKeyExpr Literal - - // An expression evaluated on data point. - Condition Expr - - // Fields to sort results by - SortFields SortFields - - // Maximum number of rows to be returned. - // Unlimited if zero. - Limit int - - // Returns rows starting at an offset from the first row. - Offset int -} - -// String returns a string representation of the statement. -func (s *ShowTagValuesStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SHOW TAG VALUES") - - if s.Database != "" { - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Database)) - } - if s.Sources != nil { - _, _ = buf.WriteString(" FROM ") - _, _ = buf.WriteString(s.Sources.String()) - } - _, _ = buf.WriteString(" WITH KEY ") - _, _ = buf.WriteString(s.Op.String()) - _, _ = buf.WriteString(" ") - if lit, ok := s.TagKeyExpr.(*StringLiteral); ok { - _, _ = buf.WriteString(QuoteIdent(lit.Val)) - } else { - _, _ = buf.WriteString(s.TagKeyExpr.String()) - } - if s.Condition != nil { - _, _ = buf.WriteString(" WHERE ") - _, _ = buf.WriteString(s.Condition.String()) - } - if len(s.SortFields) > 0 { - _, _ = buf.WriteString(" ORDER BY ") - _, _ = buf.WriteString(s.SortFields.String()) - } - if s.Limit > 0 { - _, _ = buf.WriteString(" LIMIT ") - _, _ = buf.WriteString(strconv.Itoa(s.Limit)) - } - if s.Offset > 0 { - _, _ = buf.WriteString(" OFFSET ") - _, _ = buf.WriteString(strconv.Itoa(s.Offset)) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege(s) required to execute a ShowTagValuesStatement -func (s *ShowTagValuesStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil -} - -// ShowUsersStatement represents a command for listing users. -type ShowUsersStatement struct{} - -// String returns a string representation of the ShowUsersStatement. -func (s *ShowUsersStatement) String() string { - return "SHOW USERS" -} - -// RequiredPrivileges returns the privilege(s) required to execute a ShowUsersStatement -func (s *ShowUsersStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: true, Name: "", Privilege: AllPrivileges}}, nil -} - -// ShowFieldKeysStatement represents a command for listing field keys. -type ShowFieldKeysStatement struct { - // Database to query. If blank, use the default database. - // The database can also be specified per source in the Sources. - Database string - - // Data sources that fields are extracted from. - Sources Sources - - // Fields to sort results by - SortFields SortFields - - // Maximum number of rows to be returned. - // Unlimited if zero. - Limit int - - // Returns rows starting at an offset from the first row. - Offset int -} - -// String returns a string representation of the statement. -func (s *ShowFieldKeysStatement) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("SHOW FIELD KEYS") - - if s.Database != "" { - _, _ = buf.WriteString(" ON ") - _, _ = buf.WriteString(QuoteIdent(s.Database)) - } - if s.Sources != nil { - _, _ = buf.WriteString(" FROM ") - _, _ = buf.WriteString(s.Sources.String()) - } - if len(s.SortFields) > 0 { - _, _ = buf.WriteString(" ORDER BY ") - _, _ = buf.WriteString(s.SortFields.String()) - } - if s.Limit > 0 { - _, _ = buf.WriteString(" LIMIT ") - _, _ = buf.WriteString(strconv.Itoa(s.Limit)) - } - if s.Offset > 0 { - _, _ = buf.WriteString(" OFFSET ") - _, _ = buf.WriteString(strconv.Itoa(s.Offset)) - } - return buf.String() -} - -// RequiredPrivileges returns the privilege(s) required to execute a ShowFieldKeysStatement -func (s *ShowFieldKeysStatement) RequiredPrivileges() (ExecutionPrivileges, error) { - return ExecutionPrivileges{{Admin: false, Name: "", Privilege: ReadPrivilege}}, nil -} - -// Fields represents a list of fields. -type Fields []*Field - -// AliasNames returns a list of calculated field names in -// order of alias, function name, then field. -func (a Fields) AliasNames() []string { - names := []string{} - for _, f := range a { - names = append(names, f.Name()) - } - return names -} - -// Names returns a list of field names. -func (a Fields) Names() []string { - names := []string{} - for _, f := range a { - switch expr := f.Expr.(type) { - case *Call: - names = append(names, expr.Name) - case *VarRef: - names = append(names, expr.Val) - case *BinaryExpr: - names = append(names, walkNames(expr)...) - case *ParenExpr: - names = append(names, walkNames(expr)...) - } - } - return names -} - -// String returns a string representation of the fields. -func (a Fields) String() string { - var str []string - for _, f := range a { - str = append(str, f.String()) - } - return strings.Join(str, ", ") -} - -// Field represents an expression retrieved from a select statement. -type Field struct { - Expr Expr - Alias string -} - -// Name returns the name of the field. Returns alias, if set. -// Otherwise uses the function name or variable name. -func (f *Field) Name() string { - // Return alias, if set. - if f.Alias != "" { - return f.Alias - } - - // Return the function name or variable name, if available. - switch expr := f.Expr.(type) { - case *Call: - return expr.Name - case *BinaryExpr: - return BinaryExprName(expr) - case *ParenExpr: - f := Field{Expr: expr.Expr} - return f.Name() - case *VarRef: - return expr.Val - } - - // Otherwise return a blank name. - return "" -} - -// String returns a string representation of the field. -func (f *Field) String() string { - str := f.Expr.String() - - if f.Alias == "" { - return str - } - return fmt.Sprintf("%s AS %s", str, QuoteIdent(f.Alias)) -} - -// Sort Interface for Fields -func (a Fields) Len() int { return len(a) } -func (a Fields) Less(i, j int) bool { return a[i].Name() < a[j].Name() } -func (a Fields) Swap(i, j int) { a[i], a[j] = a[j], a[i] } - -// Dimensions represents a list of dimensions. -type Dimensions []*Dimension - -// String returns a string representation of the dimensions. -func (a Dimensions) String() string { - var str []string - for _, d := range a { - str = append(str, d.String()) - } - return strings.Join(str, ", ") -} - -// Normalize returns the interval and tag dimensions separately. -// Returns 0 if no time interval is specified. -func (a Dimensions) Normalize() (time.Duration, []string) { - var dur time.Duration - var tags []string - - for _, dim := range a { - switch expr := dim.Expr.(type) { - case *Call: - lit, _ := expr.Args[0].(*DurationLiteral) - dur = lit.Val - case *VarRef: - tags = append(tags, expr.Val) - } - } - - return dur, tags -} - -// Dimension represents an expression that a select statement is grouped by. -type Dimension struct { - Expr Expr -} - -// String returns a string representation of the dimension. -func (d *Dimension) String() string { return d.Expr.String() } - -// Measurements represents a list of measurements. -type Measurements []*Measurement - -// String returns a string representation of the measurements. -func (a Measurements) String() string { - var str []string - for _, m := range a { - str = append(str, m.String()) - } - return strings.Join(str, ", ") -} - -// Measurement represents a single measurement used as a datasource. -type Measurement struct { - Database string - RetentionPolicy string - Name string - Regex *RegexLiteral - IsTarget bool -} - -// String returns a string representation of the measurement. -func (m *Measurement) String() string { - var buf bytes.Buffer - if m.Database != "" { - _, _ = buf.WriteString(QuoteIdent(m.Database)) - _, _ = buf.WriteString(".") - } - - if m.RetentionPolicy != "" { - _, _ = buf.WriteString(QuoteIdent(m.RetentionPolicy)) - } - - if m.Database != "" || m.RetentionPolicy != "" { - _, _ = buf.WriteString(`.`) - } - - if m.Name != "" { - _, _ = buf.WriteString(QuoteIdent(m.Name)) - } else if m.Regex != nil { - _, _ = buf.WriteString(m.Regex.String()) - } - - return buf.String() -} - -func encodeMeasurement(mm *Measurement) *internal.Measurement { - pb := &internal.Measurement{ - Database: proto.String(mm.Database), - RetentionPolicy: proto.String(mm.RetentionPolicy), - Name: proto.String(mm.Name), - IsTarget: proto.Bool(mm.IsTarget), - } - if mm.Regex != nil { - pb.Regex = proto.String(mm.Regex.Val.String()) - } - return pb -} - -func decodeMeasurement(pb *internal.Measurement) (*Measurement, error) { - mm := &Measurement{ - Database: pb.GetDatabase(), - RetentionPolicy: pb.GetRetentionPolicy(), - Name: pb.GetName(), - IsTarget: pb.GetIsTarget(), - } - - if pb.Regex != nil { - regex, err := regexp.Compile(pb.GetRegex()) - if err != nil { - return nil, fmt.Errorf("invalid binary measurement regex: value=%q, err=%s", pb.GetRegex(), err) - } - mm.Regex = &RegexLiteral{Val: regex} - } - - return mm, nil -} - -// VarRef represents a reference to a variable. -type VarRef struct { - Val string - Type DataType -} - -// String returns a string representation of the variable reference. -func (r *VarRef) String() string { - buf := bytes.NewBufferString(QuoteIdent(r.Val)) - if r.Type != Unknown { - buf.WriteString("::") - buf.WriteString(r.Type.String()) - } - return buf.String() -} - -// VarRefs represents a slice of VarRef types. -type VarRefs []VarRef - -func (a VarRefs) Len() int { return len(a) } -func (a VarRefs) Less(i, j int) bool { - if a[i].Val != a[j].Val { - return a[i].Val < a[j].Val - } - return a[i].Type < a[j].Type -} -func (a VarRefs) Swap(i, j int) { a[i], a[j] = a[j], a[i] } - -// Strings returns a slice of the variable names. -func (a VarRefs) Strings() []string { - s := make([]string, len(a)) - for i, ref := range a { - s[i] = ref.Val - } - return s -} - -// Call represents a function call. -type Call struct { - Name string - Args []Expr -} - -// String returns a string representation of the call. -func (c *Call) String() string { - // Join arguments. - var str []string - for _, arg := range c.Args { - str = append(str, arg.String()) - } - - // Write function name and args. - return fmt.Sprintf("%s(%s)", c.Name, strings.Join(str, ", ")) -} - -// Distinct represents a DISTINCT expression. -type Distinct struct { - // Identifier following DISTINCT - Val string -} - -// String returns a string representation of the expression. -func (d *Distinct) String() string { - return fmt.Sprintf("DISTINCT %s", d.Val) -} - -// NewCall returns a new call expression from this expressions. -func (d *Distinct) NewCall() *Call { - return &Call{ - Name: "distinct", - Args: []Expr{ - &VarRef{Val: d.Val}, - }, - } -} - -// NumberLiteral represents a numeric literal. -type NumberLiteral struct { - Val float64 -} - -// String returns a string representation of the literal. -func (l *NumberLiteral) String() string { return strconv.FormatFloat(l.Val, 'f', 3, 64) } - -// IntegerLiteral represents an integer literal. -type IntegerLiteral struct { - Val int64 -} - -// String returns a string representation of the literal. -func (l *IntegerLiteral) String() string { return fmt.Sprintf("%d", l.Val) } - -// BooleanLiteral represents a boolean literal. -type BooleanLiteral struct { - Val bool -} - -// String returns a string representation of the literal. -func (l *BooleanLiteral) String() string { - if l.Val { - return "true" - } - return "false" -} - -// isTrueLiteral returns true if the expression is a literal "true" value. -func isTrueLiteral(expr Expr) bool { - if expr, ok := expr.(*BooleanLiteral); ok { - return expr.Val == true - } - return false -} - -// isFalseLiteral returns true if the expression is a literal "false" value. -func isFalseLiteral(expr Expr) bool { - if expr, ok := expr.(*BooleanLiteral); ok { - return expr.Val == false - } - return false -} - -// ListLiteral represents a list of strings literal. -type ListLiteral struct { - Vals []string -} - -// String returns a string representation of the literal. -func (s *ListLiteral) String() string { - var buf bytes.Buffer - _, _ = buf.WriteString("(") - for idx, tagKey := range s.Vals { - if idx != 0 { - _, _ = buf.WriteString(", ") - } - _, _ = buf.WriteString(QuoteIdent(tagKey)) - } - _, _ = buf.WriteString(")") - return buf.String() -} - -// StringLiteral represents a string literal. -type StringLiteral struct { - Val string -} - -// String returns a string representation of the literal. -func (l *StringLiteral) String() string { return QuoteString(l.Val) } - -// IsTimeLiteral returns if this string can be interpreted as a time literal. -func (l *StringLiteral) IsTimeLiteral() bool { - return isDateTimeString(l.Val) || isDateString(l.Val) -} - -// ToTimeLiteral returns a time literal if this string can be converted to a time literal. -func (l *StringLiteral) ToTimeLiteral() (*TimeLiteral, error) { - if isDateTimeString(l.Val) { - t, err := time.Parse(DateTimeFormat, l.Val) - if err != nil { - // try to parse it as an RFCNano time - t, err = time.Parse(time.RFC3339Nano, l.Val) - if err != nil { - return nil, ErrInvalidTime - } - } - return &TimeLiteral{Val: t}, nil - } else if isDateString(l.Val) { - t, err := time.Parse(DateFormat, l.Val) - if err != nil { - return nil, ErrInvalidTime - } - return &TimeLiteral{Val: t}, nil - } - return nil, ErrInvalidTime -} - -// TimeLiteral represents a point-in-time literal. -type TimeLiteral struct { - Val time.Time -} - -// String returns a string representation of the literal. -func (l *TimeLiteral) String() string { - return `'` + l.Val.UTC().Format(time.RFC3339Nano) + `'` -} - -// DurationLiteral represents a duration literal. -type DurationLiteral struct { - Val time.Duration -} - -// String returns a string representation of the literal. -func (l *DurationLiteral) String() string { return FormatDuration(l.Val) } - -// nilLiteral represents a nil literal. -// This is not available to the query language itself. It's only used internally. -type nilLiteral struct{} - -// String returns a string representation of the literal. -func (l *nilLiteral) String() string { return `nil` } - -// BinaryExpr represents an operation between two expressions. -type BinaryExpr struct { - Op Token - LHS Expr - RHS Expr -} - -// String returns a string representation of the binary expression. -func (e *BinaryExpr) String() string { - return fmt.Sprintf("%s %s %s", e.LHS.String(), e.Op.String(), e.RHS.String()) -} - -func (e *BinaryExpr) validate() error { - v := binaryExprValidator{} - Walk(&v, e) - if v.err != nil { - return v.err - } else if v.calls && v.refs { - return errors.New("binary expressions cannot mix aggregates and raw fields") - } - return nil -} - -type binaryExprValidator struct { - calls bool - refs bool - err error -} - -func (v *binaryExprValidator) Visit(n Node) Visitor { - if v.err != nil { - return nil - } - - switch n := n.(type) { - case *Call: - v.calls = true - - if n.Name == "top" || n.Name == "bottom" { - v.err = fmt.Errorf("cannot use %s() inside of a binary expression", n.Name) - return nil - } - - for _, expr := range n.Args { - switch e := expr.(type) { - case *BinaryExpr: - v.err = e.validate() - return nil - } - } - return nil - case *VarRef: - v.refs = true - return nil - } - return v -} - -// BinaryExprName returns the name of a binary expression by concatenating -// the variables in the binary expression with underscores. -func BinaryExprName(expr *BinaryExpr) string { - v := binaryExprNameVisitor{} - Walk(&v, expr) - return strings.Join(v.names, "_") -} - -type binaryExprNameVisitor struct { - names []string -} - -func (v *binaryExprNameVisitor) Visit(n Node) Visitor { - switch n := n.(type) { - case *VarRef: - v.names = append(v.names, n.Val) - case *Call: - v.names = append(v.names, n.Name) - return nil - } - return v -} - -// ParenExpr represents a parenthesized expression. -type ParenExpr struct { - Expr Expr -} - -// String returns a string representation of the parenthesized expression. -func (e *ParenExpr) String() string { return fmt.Sprintf("(%s)", e.Expr.String()) } - -// RegexLiteral represents a regular expression. -type RegexLiteral struct { - Val *regexp.Regexp -} - -// String returns a string representation of the literal. -func (r *RegexLiteral) String() string { - if r.Val != nil { - return fmt.Sprintf("/%s/", strings.Replace(r.Val.String(), `/`, `\/`, -1)) - } - return "" -} - -// CloneRegexLiteral returns a clone of the RegexLiteral. -func CloneRegexLiteral(r *RegexLiteral) *RegexLiteral { - if r == nil { - return nil - } - - clone := &RegexLiteral{} - if r.Val != nil { - clone.Val = regexp.MustCompile(r.Val.String()) - } - - return clone -} - -// Wildcard represents a wild card expression. -type Wildcard struct { - Type Token -} - -// String returns a string representation of the wildcard. -func (e *Wildcard) String() string { - switch e.Type { - case FIELD: - return "*::field" - case TAG: - return "*::tag" - default: - return "*" - } -} - -// CloneExpr returns a deep copy of the expression. -func CloneExpr(expr Expr) Expr { - if expr == nil { - return nil - } - switch expr := expr.(type) { - case *BinaryExpr: - return &BinaryExpr{Op: expr.Op, LHS: CloneExpr(expr.LHS), RHS: CloneExpr(expr.RHS)} - case *BooleanLiteral: - return &BooleanLiteral{Val: expr.Val} - case *Call: - args := make([]Expr, len(expr.Args)) - for i, arg := range expr.Args { - args[i] = CloneExpr(arg) - } - return &Call{Name: expr.Name, Args: args} - case *Distinct: - return &Distinct{Val: expr.Val} - case *DurationLiteral: - return &DurationLiteral{Val: expr.Val} - case *IntegerLiteral: - return &IntegerLiteral{Val: expr.Val} - case *NumberLiteral: - return &NumberLiteral{Val: expr.Val} - case *ParenExpr: - return &ParenExpr{Expr: CloneExpr(expr.Expr)} - case *RegexLiteral: - return &RegexLiteral{Val: expr.Val} - case *StringLiteral: - return &StringLiteral{Val: expr.Val} - case *TimeLiteral: - return &TimeLiteral{Val: expr.Val} - case *VarRef: - return &VarRef{Val: expr.Val, Type: expr.Type} - case *Wildcard: - return &Wildcard{Type: expr.Type} - } - panic("unreachable") -} - -// HasTimeExpr returns true if the expression has a time term. -func HasTimeExpr(expr Expr) bool { - switch n := expr.(type) { - case *BinaryExpr: - if n.Op == AND || n.Op == OR { - return HasTimeExpr(n.LHS) || HasTimeExpr(n.RHS) - } - if ref, ok := n.LHS.(*VarRef); ok && strings.ToLower(ref.Val) == "time" { - return true - } - return false - case *ParenExpr: - // walk down the tree - return HasTimeExpr(n.Expr) - default: - return false - } -} - -// OnlyTimeExpr returns true if the expression only has time constraints. -func OnlyTimeExpr(expr Expr) bool { - if expr == nil { - return false - } - switch n := expr.(type) { - case *BinaryExpr: - if n.Op == AND || n.Op == OR { - return OnlyTimeExpr(n.LHS) && OnlyTimeExpr(n.RHS) - } - if ref, ok := n.LHS.(*VarRef); ok && strings.ToLower(ref.Val) == "time" { - return true - } - return false - case *ParenExpr: - // walk down the tree - return OnlyTimeExpr(n.Expr) - default: - return false - } -} - -// TimeRange returns the minimum and maximum times specified by an expression. -// Returns zero times if there is no bound. -func TimeRange(expr Expr) (min, max time.Time, err error) { - WalkFunc(expr, func(n Node) { - if err != nil { - return - } - - if n, ok := n.(*BinaryExpr); ok { - // Extract literal expression & operator on LHS. - // Check for "time" on the left-hand side first. - // Otherwise check for for the right-hand side and flip the operator. - op := n.Op - var value time.Time - value, err = timeExprValue(n.LHS, n.RHS) - if err != nil { - return - } else if value.IsZero() { - if value, err = timeExprValue(n.RHS, n.LHS); value.IsZero() || err != nil { - return - } else if op == LT { - op = GT - } else if op == LTE { - op = GTE - } else if op == GT { - op = LT - } else if op == GTE { - op = LTE - } - } - - // Update the min/max depending on the operator. - // The GT & LT update the value by +/- 1ns not make them "not equal". - switch op { - case GT: - if min.IsZero() || value.After(min) { - min = value.Add(time.Nanosecond) - } - case GTE: - if min.IsZero() || value.After(min) { - min = value - } - case LT: - if max.IsZero() || value.Before(max) { - max = value.Add(-time.Nanosecond) - } - case LTE: - if max.IsZero() || value.Before(max) { - max = value - } - case EQ: - if min.IsZero() || value.After(min) { - min = value - } - if max.IsZero() || value.Add(1*time.Nanosecond).Before(max) { - max = value.Add(1 * time.Nanosecond) - } - } - } - }) - return -} - -// TimeRangeAsEpochNano returns the minimum and maximum times, as epoch nano, specified by -// an expression. If there is no lower bound, the minimum time is returned -// for minimum. If there is no higher bound, now is returned for maximum. -func TimeRangeAsEpochNano(expr Expr) (min, max int64, err error) { - tmin, tmax, err := TimeRange(expr) - if err != nil { - return 0, 0, err - } - - if tmin.IsZero() { - min = time.Unix(0, MinTime).UnixNano() - } else { - min = tmin.UnixNano() - } - if tmax.IsZero() { - max = time.Now().UnixNano() - } else { - max = tmax.UnixNano() - } - return -} - -// timeExprValue returns the time literal value of a "time == " expression. -// Returns zero time if the expression is not a time expression. -func timeExprValue(ref Expr, lit Expr) (t time.Time, err error) { - if ref, ok := ref.(*VarRef); ok && strings.ToLower(ref.Val) == "time" { - // If literal looks like a date time then parse it as a time literal. - if strlit, ok := lit.(*StringLiteral); ok { - if strlit.IsTimeLiteral() { - t, err := strlit.ToTimeLiteral() - if err != nil { - return time.Time{}, err - } - lit = t - } - } - - switch lit := lit.(type) { - case *TimeLiteral: - if lit.Val.After(time.Unix(0, MaxTime)) { - return time.Time{}, fmt.Errorf("time %s overflows time literal", lit.Val.Format(time.RFC3339)) - } else if lit.Val.Before(time.Unix(0, MinTime+1)) { - // The minimum allowable time literal is one greater than the minimum time because the minimum time - // is a sentinel value only used internally. - return time.Time{}, fmt.Errorf("time %s underflows time literal", lit.Val.Format(time.RFC3339)) - } - return lit.Val, nil - case *DurationLiteral: - return time.Unix(0, int64(lit.Val)).UTC(), nil - case *NumberLiteral: - return time.Unix(0, int64(lit.Val)).UTC(), nil - case *IntegerLiteral: - return time.Unix(0, lit.Val).UTC(), nil - default: - return time.Time{}, fmt.Errorf("invalid operation: time and %T are not compatible", lit) - } - } - return time.Time{}, nil -} - -// Visitor can be called by Walk to traverse an AST hierarchy. -// The Visit() function is called once per node. -type Visitor interface { - Visit(Node) Visitor -} - -// Walk traverses a node hierarchy in depth-first order. -func Walk(v Visitor, node Node) { - if node == nil { - return - } - - if v = v.Visit(node); v == nil { - return - } - - switch n := node.(type) { - case *BinaryExpr: - Walk(v, n.LHS) - Walk(v, n.RHS) - - case *Call: - for _, expr := range n.Args { - Walk(v, expr) - } - - case *CreateContinuousQueryStatement: - Walk(v, n.Source) - - case *Dimension: - Walk(v, n.Expr) - - case Dimensions: - for _, c := range n { - Walk(v, c) - } - - case *DeleteSeriesStatement: - Walk(v, n.Sources) - Walk(v, n.Condition) - - case *DropSeriesStatement: - Walk(v, n.Sources) - Walk(v, n.Condition) - - case *Field: - Walk(v, n.Expr) - - case Fields: - for _, c := range n { - Walk(v, c) - } - - case *ParenExpr: - Walk(v, n.Expr) - - case *Query: - Walk(v, n.Statements) - - case *SelectStatement: - Walk(v, n.Fields) - Walk(v, n.Target) - Walk(v, n.Dimensions) - Walk(v, n.Sources) - Walk(v, n.Condition) - Walk(v, n.SortFields) - - case *ShowSeriesStatement: - Walk(v, n.Sources) - Walk(v, n.Condition) - - case *ShowTagKeysStatement: - Walk(v, n.Sources) - Walk(v, n.Condition) - Walk(v, n.SortFields) - - case *ShowTagValuesStatement: - Walk(v, n.Sources) - Walk(v, n.Condition) - Walk(v, n.SortFields) - - case *ShowFieldKeysStatement: - Walk(v, n.Sources) - Walk(v, n.SortFields) - - case SortFields: - for _, sf := range n { - Walk(v, sf) - } - - case Sources: - for _, s := range n { - Walk(v, s) - } - - case Statements: - for _, s := range n { - Walk(v, s) - } - - case *Target: - if n != nil { - Walk(v, n.Measurement) - } - } -} - -// WalkFunc traverses a node hierarchy in depth-first order. -func WalkFunc(node Node, fn func(Node)) { - Walk(walkFuncVisitor(fn), node) -} - -type walkFuncVisitor func(Node) - -func (fn walkFuncVisitor) Visit(n Node) Visitor { fn(n); return fn } - -// Rewriter can be called by Rewrite to replace nodes in the AST hierarchy. -// The Rewrite() function is called once per node. -type Rewriter interface { - Rewrite(Node) Node -} - -// Rewrite recursively invokes the rewriter to replace each node. -// Nodes are traversed depth-first and rewritten from leaf to root. -func Rewrite(r Rewriter, node Node) Node { - switch n := node.(type) { - case *Query: - n.Statements = Rewrite(r, n.Statements).(Statements) - - case Statements: - for i, s := range n { - n[i] = Rewrite(r, s).(Statement) - } - - case *SelectStatement: - n.Fields = Rewrite(r, n.Fields).(Fields) - n.Dimensions = Rewrite(r, n.Dimensions).(Dimensions) - n.Sources = Rewrite(r, n.Sources).(Sources) - n.Condition = Rewrite(r, n.Condition).(Expr) - - case Fields: - for i, f := range n { - n[i] = Rewrite(r, f).(*Field) - } - - case *Field: - n.Expr = Rewrite(r, n.Expr).(Expr) - - case Dimensions: - for i, d := range n { - n[i] = Rewrite(r, d).(*Dimension) - } - - case *Dimension: - n.Expr = Rewrite(r, n.Expr).(Expr) - - case *BinaryExpr: - n.LHS = Rewrite(r, n.LHS).(Expr) - n.RHS = Rewrite(r, n.RHS).(Expr) - - case *ParenExpr: - n.Expr = Rewrite(r, n.Expr).(Expr) - - case *Call: - for i, expr := range n.Args { - n.Args[i] = Rewrite(r, expr).(Expr) - } - } - - return r.Rewrite(node) -} - -// RewriteFunc rewrites a node hierarchy. -func RewriteFunc(node Node, fn func(Node) Node) Node { - return Rewrite(rewriterFunc(fn), node) -} - -type rewriterFunc func(Node) Node - -func (fn rewriterFunc) Rewrite(n Node) Node { return fn(n) } - -// RewriteExpr recursively invokes the function to replace each expr. -// Nodes are traversed depth-first and rewritten from leaf to root. -func RewriteExpr(expr Expr, fn func(Expr) Expr) Expr { - switch e := expr.(type) { - case *BinaryExpr: - e.LHS = RewriteExpr(e.LHS, fn) - e.RHS = RewriteExpr(e.RHS, fn) - if e.LHS != nil && e.RHS == nil { - expr = e.LHS - } else if e.RHS != nil && e.LHS == nil { - expr = e.RHS - } else if e.LHS == nil && e.RHS == nil { - return nil - } - - case *ParenExpr: - e.Expr = RewriteExpr(e.Expr, fn) - if e.Expr == nil { - return nil - } - - case *Call: - for i, expr := range e.Args { - e.Args[i] = RewriteExpr(expr, fn) - } - } - - return fn(expr) -} - -// Eval evaluates expr against a map. -func Eval(expr Expr, m map[string]interface{}) interface{} { - if expr == nil { - return nil - } - - switch expr := expr.(type) { - case *BinaryExpr: - return evalBinaryExpr(expr, m) - case *BooleanLiteral: - return expr.Val - case *IntegerLiteral: - return expr.Val - case *NumberLiteral: - return expr.Val - case *ParenExpr: - return Eval(expr.Expr, m) - case *RegexLiteral: - return expr.Val - case *StringLiteral: - return expr.Val - case *VarRef: - return m[expr.Val] - default: - return nil - } -} - -func evalBinaryExpr(expr *BinaryExpr, m map[string]interface{}) interface{} { - lhs := Eval(expr.LHS, m) - rhs := Eval(expr.RHS, m) - - // Evaluate if both sides are simple types. - switch lhs := lhs.(type) { - case bool: - rhs, ok := rhs.(bool) - switch expr.Op { - case AND: - return ok && (lhs && rhs) - case OR: - return ok && (lhs || rhs) - case EQ: - return ok && (lhs == rhs) - case NEQ: - return ok && (lhs != rhs) - } - case float64: - // Try the rhs as a float64 or int64 - rhsf, ok := rhs.(float64) - if !ok { - var rhsi int64 - if rhsi, ok = rhs.(int64); ok { - rhsf = float64(rhsi) - } - } - - rhs := rhsf - switch expr.Op { - case EQ: - return ok && (lhs == rhs) - case NEQ: - return ok && (lhs != rhs) - case LT: - return ok && (lhs < rhs) - case LTE: - return ok && (lhs <= rhs) - case GT: - return ok && (lhs > rhs) - case GTE: - return ok && (lhs >= rhs) - case ADD: - if !ok { - return nil - } - return lhs + rhs - case SUB: - if !ok { - return nil - } - return lhs - rhs - case MUL: - if !ok { - return nil - } - return lhs * rhs - case DIV: - if !ok { - return nil - } else if rhs == 0 { - return float64(0) - } - return lhs / rhs - } - case int64: - // Try as a float64 to see if a float cast is required. - rhsf, ok := rhs.(float64) - if ok { - lhs := float64(lhs) - rhs := rhsf - switch expr.Op { - case EQ: - return lhs == rhs - case NEQ: - return lhs != rhs - case LT: - return lhs < rhs - case LTE: - return lhs <= rhs - case GT: - return lhs > rhs - case GTE: - return lhs >= rhs - case ADD: - return lhs + rhs - case SUB: - return lhs - rhs - case MUL: - return lhs * rhs - case DIV: - if rhs == 0 { - return float64(0) - } - return lhs / rhs - } - } else { - rhs, ok := rhs.(int64) - switch expr.Op { - case EQ: - return ok && (lhs == rhs) - case NEQ: - return ok && (lhs != rhs) - case LT: - return ok && (lhs < rhs) - case LTE: - return ok && (lhs <= rhs) - case GT: - return ok && (lhs > rhs) - case GTE: - return ok && (lhs >= rhs) - case ADD: - if !ok { - return nil - } - return lhs + rhs - case SUB: - if !ok { - return nil - } - return lhs - rhs - case MUL: - if !ok { - return nil - } - return lhs * rhs - case DIV: - if !ok { - return nil - } else if rhs == 0 { - return float64(0) - } - return lhs / rhs - } - } - case string: - switch expr.Op { - case EQ: - rhs, ok := rhs.(string) - return ok && lhs == rhs - case NEQ: - rhs, ok := rhs.(string) - return ok && lhs != rhs - case EQREGEX: - rhs, ok := rhs.(*regexp.Regexp) - return ok && rhs.MatchString(lhs) - case NEQREGEX: - rhs, ok := rhs.(*regexp.Regexp) - return ok && !rhs.MatchString(lhs) - } - } - return nil -} - -// EvalBool evaluates expr and returns true if result is a boolean true. -// Otherwise returns false. -func EvalBool(expr Expr, m map[string]interface{}) bool { - v, _ := Eval(expr, m).(bool) - return v -} - -// Reduce evaluates expr using the available values in valuer. -// References that don't exist in valuer are ignored. -func Reduce(expr Expr, valuer Valuer) Expr { - expr = reduce(expr, valuer) - - // Unwrap parens at top level. - if expr, ok := expr.(*ParenExpr); ok { - return expr.Expr - } - return expr -} - -func reduce(expr Expr, valuer Valuer) Expr { - if expr == nil { - return nil - } - - switch expr := expr.(type) { - case *BinaryExpr: - return reduceBinaryExpr(expr, valuer) - case *Call: - return reduceCall(expr, valuer) - case *ParenExpr: - return reduceParenExpr(expr, valuer) - case *VarRef: - return reduceVarRef(expr, valuer) - default: - return CloneExpr(expr) - } -} - -func reduceBinaryExpr(expr *BinaryExpr, valuer Valuer) Expr { - // Reduce both sides first. - op := expr.Op - lhs := reduce(expr.LHS, valuer) - rhs := reduce(expr.RHS, valuer) - - // Do not evaluate if one side is nil. - if lhs == nil || rhs == nil { - return &BinaryExpr{LHS: lhs, RHS: rhs, Op: expr.Op} - } - - // If we have a logical operator (AND, OR) and one side is a boolean literal - // then we need to have special handling. - if op == AND { - if isFalseLiteral(lhs) || isFalseLiteral(rhs) { - return &BooleanLiteral{Val: false} - } else if isTrueLiteral(lhs) { - return rhs - } else if isTrueLiteral(rhs) { - return lhs - } - } else if op == OR { - if isTrueLiteral(lhs) || isTrueLiteral(rhs) { - return &BooleanLiteral{Val: true} - } else if isFalseLiteral(lhs) { - return rhs - } else if isFalseLiteral(rhs) { - return lhs - } - } - - // Evaluate if both sides are simple types. - switch lhs := lhs.(type) { - case *BooleanLiteral: - return reduceBinaryExprBooleanLHS(op, lhs, rhs) - case *DurationLiteral: - return reduceBinaryExprDurationLHS(op, lhs, rhs) - case *IntegerLiteral: - return reduceBinaryExprIntegerLHS(op, lhs, rhs) - case *nilLiteral: - return reduceBinaryExprNilLHS(op, lhs, rhs) - case *NumberLiteral: - return reduceBinaryExprNumberLHS(op, lhs, rhs) - case *StringLiteral: - return reduceBinaryExprStringLHS(op, lhs, rhs) - case *TimeLiteral: - return reduceBinaryExprTimeLHS(op, lhs, rhs) - default: - return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs} - } -} - -func reduceBinaryExprBooleanLHS(op Token, lhs *BooleanLiteral, rhs Expr) Expr { - switch rhs := rhs.(type) { - case *BooleanLiteral: - switch op { - case EQ: - return &BooleanLiteral{Val: lhs.Val == rhs.Val} - case NEQ: - return &BooleanLiteral{Val: lhs.Val != rhs.Val} - case AND: - return &BooleanLiteral{Val: lhs.Val && rhs.Val} - case OR: - return &BooleanLiteral{Val: lhs.Val || rhs.Val} - } - case *nilLiteral: - return &BooleanLiteral{Val: false} - } - return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs} -} - -func reduceBinaryExprDurationLHS(op Token, lhs *DurationLiteral, rhs Expr) Expr { - switch rhs := rhs.(type) { - case *DurationLiteral: - switch op { - case ADD: - return &DurationLiteral{Val: lhs.Val + rhs.Val} - case SUB: - return &DurationLiteral{Val: lhs.Val - rhs.Val} - case EQ: - return &BooleanLiteral{Val: lhs.Val == rhs.Val} - case NEQ: - return &BooleanLiteral{Val: lhs.Val != rhs.Val} - case GT: - return &BooleanLiteral{Val: lhs.Val > rhs.Val} - case GTE: - return &BooleanLiteral{Val: lhs.Val >= rhs.Val} - case LT: - return &BooleanLiteral{Val: lhs.Val < rhs.Val} - case LTE: - return &BooleanLiteral{Val: lhs.Val <= rhs.Val} - } - case *NumberLiteral: - switch op { - case MUL: - return &DurationLiteral{Val: lhs.Val * time.Duration(rhs.Val)} - case DIV: - if rhs.Val == 0 { - return &DurationLiteral{Val: 0} - } - return &DurationLiteral{Val: lhs.Val / time.Duration(rhs.Val)} - } - case *IntegerLiteral: - switch op { - case MUL: - return &DurationLiteral{Val: lhs.Val * time.Duration(rhs.Val)} - case DIV: - if rhs.Val == 0 { - return &DurationLiteral{Val: 0} - } - return &DurationLiteral{Val: lhs.Val / time.Duration(rhs.Val)} - } - case *TimeLiteral: - switch op { - case ADD: - return &TimeLiteral{Val: rhs.Val.Add(lhs.Val)} - } - case *StringLiteral: - t, err := rhs.ToTimeLiteral() - if err != nil { - break - } - expr := reduceBinaryExprDurationLHS(op, lhs, t) - - // If the returned expression is still a binary expr, that means - // we couldn't reduce it so this wasn't used in a time literal context. - if _, ok := expr.(*BinaryExpr); !ok { - return expr - } - case *nilLiteral: - return &BooleanLiteral{Val: false} - } - return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs} -} - -func reduceBinaryExprIntegerLHS(op Token, lhs *IntegerLiteral, rhs Expr) Expr { - switch rhs := rhs.(type) { - case *NumberLiteral: - return reduceBinaryExprNumberLHS(op, &NumberLiteral{Val: float64(lhs.Val)}, rhs) - case *IntegerLiteral: - switch op { - case ADD: - return &IntegerLiteral{Val: lhs.Val + rhs.Val} - case SUB: - return &IntegerLiteral{Val: lhs.Val - rhs.Val} - case MUL: - return &IntegerLiteral{Val: lhs.Val * rhs.Val} - case DIV: - if rhs.Val == 0 { - return &NumberLiteral{Val: 0} - } - return &NumberLiteral{Val: float64(lhs.Val) / float64(rhs.Val)} - case EQ: - return &BooleanLiteral{Val: lhs.Val == rhs.Val} - case NEQ: - return &BooleanLiteral{Val: lhs.Val != rhs.Val} - case GT: - return &BooleanLiteral{Val: lhs.Val > rhs.Val} - case GTE: - return &BooleanLiteral{Val: lhs.Val >= rhs.Val} - case LT: - return &BooleanLiteral{Val: lhs.Val < rhs.Val} - case LTE: - return &BooleanLiteral{Val: lhs.Val <= rhs.Val} - } - case *DurationLiteral: - // Treat the integer as a timestamp. - switch op { - case ADD: - return &TimeLiteral{Val: time.Unix(0, lhs.Val).Add(rhs.Val)} - case SUB: - return &TimeLiteral{Val: time.Unix(0, lhs.Val).Add(-rhs.Val)} - } - case *TimeLiteral: - d := &DurationLiteral{Val: time.Duration(lhs.Val)} - expr := reduceBinaryExprDurationLHS(op, d, rhs) - if _, ok := expr.(*BinaryExpr); !ok { - return expr - } - case *StringLiteral: - t, err := rhs.ToTimeLiteral() - if err != nil { - break - } - d := &DurationLiteral{Val: time.Duration(lhs.Val)} - expr := reduceBinaryExprDurationLHS(op, d, t) - if _, ok := expr.(*BinaryExpr); !ok { - return expr - } - case *nilLiteral: - return &BooleanLiteral{Val: false} - } - return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs} -} - -func reduceBinaryExprNilLHS(op Token, lhs *nilLiteral, rhs Expr) Expr { - switch op { - case EQ, NEQ: - return &BooleanLiteral{Val: false} - } - return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs} -} - -func reduceBinaryExprNumberLHS(op Token, lhs *NumberLiteral, rhs Expr) Expr { - switch rhs := rhs.(type) { - case *NumberLiteral: - switch op { - case ADD: - return &NumberLiteral{Val: lhs.Val + rhs.Val} - case SUB: - return &NumberLiteral{Val: lhs.Val - rhs.Val} - case MUL: - return &NumberLiteral{Val: lhs.Val * rhs.Val} - case DIV: - if rhs.Val == 0 { - return &NumberLiteral{Val: 0} - } - return &NumberLiteral{Val: lhs.Val / rhs.Val} - case EQ: - return &BooleanLiteral{Val: lhs.Val == rhs.Val} - case NEQ: - return &BooleanLiteral{Val: lhs.Val != rhs.Val} - case GT: - return &BooleanLiteral{Val: lhs.Val > rhs.Val} - case GTE: - return &BooleanLiteral{Val: lhs.Val >= rhs.Val} - case LT: - return &BooleanLiteral{Val: lhs.Val < rhs.Val} - case LTE: - return &BooleanLiteral{Val: lhs.Val <= rhs.Val} - } - case *IntegerLiteral: - switch op { - case ADD: - return &NumberLiteral{Val: lhs.Val + float64(rhs.Val)} - case SUB: - return &NumberLiteral{Val: lhs.Val - float64(rhs.Val)} - case MUL: - return &NumberLiteral{Val: lhs.Val * float64(rhs.Val)} - case DIV: - if float64(rhs.Val) == 0 { - return &NumberLiteral{Val: 0} - } - return &NumberLiteral{Val: lhs.Val / float64(rhs.Val)} - case EQ: - return &BooleanLiteral{Val: lhs.Val == float64(rhs.Val)} - case NEQ: - return &BooleanLiteral{Val: lhs.Val != float64(rhs.Val)} - case GT: - return &BooleanLiteral{Val: lhs.Val > float64(rhs.Val)} - case GTE: - return &BooleanLiteral{Val: lhs.Val >= float64(rhs.Val)} - case LT: - return &BooleanLiteral{Val: lhs.Val < float64(rhs.Val)} - case LTE: - return &BooleanLiteral{Val: lhs.Val <= float64(rhs.Val)} - } - case *nilLiteral: - return &BooleanLiteral{Val: false} - } - return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs} -} - -func reduceBinaryExprStringLHS(op Token, lhs *StringLiteral, rhs Expr) Expr { - switch rhs := rhs.(type) { - case *StringLiteral: - switch op { - case EQ: - var expr Expr = &BooleanLiteral{Val: lhs.Val == rhs.Val} - // This might be a comparison between time literals. - // If it is, parse the time literals and then compare since it - // could be a different result if they use different formats - // for the same time. - if lhs.IsTimeLiteral() && rhs.IsTimeLiteral() { - tlhs, err := lhs.ToTimeLiteral() - if err != nil { - return expr - } - - trhs, err := rhs.ToTimeLiteral() - if err != nil { - return expr - } - - t := reduceBinaryExprTimeLHS(op, tlhs, trhs) - if _, ok := t.(*BinaryExpr); !ok { - expr = t - } - } - return expr - case NEQ: - var expr Expr = &BooleanLiteral{Val: lhs.Val != rhs.Val} - // This might be a comparison between time literals. - // If it is, parse the time literals and then compare since it - // could be a different result if they use different formats - // for the same time. - if lhs.IsTimeLiteral() && rhs.IsTimeLiteral() { - tlhs, err := lhs.ToTimeLiteral() - if err != nil { - return expr - } - - trhs, err := rhs.ToTimeLiteral() - if err != nil { - return expr - } - - t := reduceBinaryExprTimeLHS(op, tlhs, trhs) - if _, ok := t.(*BinaryExpr); !ok { - expr = t - } - } - return expr - case ADD: - return &StringLiteral{Val: lhs.Val + rhs.Val} - default: - // Attempt to convert the string literal to a time literal. - t, err := lhs.ToTimeLiteral() - if err != nil { - break - } - expr := reduceBinaryExprTimeLHS(op, t, rhs) - - // If the returned expression is still a binary expr, that means - // we couldn't reduce it so this wasn't used in a time literal context. - if _, ok := expr.(*BinaryExpr); !ok { - return expr - } - } - case *DurationLiteral: - // Attempt to convert the string literal to a time literal. - t, err := lhs.ToTimeLiteral() - if err != nil { - break - } - expr := reduceBinaryExprTimeLHS(op, t, rhs) - - // If the returned expression is still a binary expr, that means - // we couldn't reduce it so this wasn't used in a time literal context. - if _, ok := expr.(*BinaryExpr); !ok { - return expr - } - case *TimeLiteral: - // Attempt to convert the string literal to a time literal. - t, err := lhs.ToTimeLiteral() - if err != nil { - break - } - expr := reduceBinaryExprTimeLHS(op, t, rhs) - - // If the returned expression is still a binary expr, that means - // we couldn't reduce it so this wasn't used in a time literal context. - if _, ok := expr.(*BinaryExpr); !ok { - return expr - } - case *IntegerLiteral: - // Attempt to convert the string literal to a time literal. - t, err := lhs.ToTimeLiteral() - if err != nil { - break - } - expr := reduceBinaryExprTimeLHS(op, t, rhs) - - // If the returned expression is still a binary expr, that means - // we couldn't reduce it so this wasn't used in a time literal context. - if _, ok := expr.(*BinaryExpr); !ok { - return expr - } - case *nilLiteral: - switch op { - case EQ, NEQ: - return &BooleanLiteral{Val: false} - } - } - return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs} -} - -func reduceBinaryExprTimeLHS(op Token, lhs *TimeLiteral, rhs Expr) Expr { - switch rhs := rhs.(type) { - case *DurationLiteral: - switch op { - case ADD: - return &TimeLiteral{Val: lhs.Val.Add(rhs.Val)} - case SUB: - return &TimeLiteral{Val: lhs.Val.Add(-rhs.Val)} - } - case *IntegerLiteral: - d := &DurationLiteral{Val: time.Duration(rhs.Val)} - expr := reduceBinaryExprTimeLHS(op, lhs, d) - if _, ok := expr.(*BinaryExpr); !ok { - return expr - } - case *TimeLiteral: - switch op { - case SUB: - return &DurationLiteral{Val: lhs.Val.Sub(rhs.Val)} - case EQ: - return &BooleanLiteral{Val: lhs.Val.Equal(rhs.Val)} - case NEQ: - return &BooleanLiteral{Val: !lhs.Val.Equal(rhs.Val)} - case GT: - return &BooleanLiteral{Val: lhs.Val.After(rhs.Val)} - case GTE: - return &BooleanLiteral{Val: lhs.Val.After(rhs.Val) || lhs.Val.Equal(rhs.Val)} - case LT: - return &BooleanLiteral{Val: lhs.Val.Before(rhs.Val)} - case LTE: - return &BooleanLiteral{Val: lhs.Val.Before(rhs.Val) || lhs.Val.Equal(rhs.Val)} - } - case *StringLiteral: - t, err := rhs.ToTimeLiteral() - if err != nil { - break - } - expr := reduceBinaryExprTimeLHS(op, lhs, t) - - // If the returned expression is still a binary expr, that means - // we couldn't reduce it so this wasn't used in a time literal context. - if _, ok := expr.(*BinaryExpr); !ok { - return expr - } - case *nilLiteral: - return &BooleanLiteral{Val: false} - } - return &BinaryExpr{Op: op, LHS: lhs, RHS: rhs} -} - -func reduceCall(expr *Call, valuer Valuer) Expr { - // Evaluate "now()" if valuer is set. - if expr.Name == "now" && len(expr.Args) == 0 && valuer != nil { - if v, ok := valuer.Value("now()"); ok { - v, _ := v.(time.Time) - return &TimeLiteral{Val: v} - } - } - - // Otherwise reduce arguments. - args := make([]Expr, len(expr.Args)) - for i, arg := range expr.Args { - args[i] = reduce(arg, valuer) - } - return &Call{Name: expr.Name, Args: args} -} - -func reduceParenExpr(expr *ParenExpr, valuer Valuer) Expr { - subexpr := reduce(expr.Expr, valuer) - if subexpr, ok := subexpr.(*BinaryExpr); ok { - return &ParenExpr{Expr: subexpr} - } - return subexpr -} - -func reduceVarRef(expr *VarRef, valuer Valuer) Expr { - // Ignore if there is no valuer. - if valuer == nil { - return &VarRef{Val: expr.Val, Type: expr.Type} - } - - // Retrieve the value of the ref. - // Ignore if the value doesn't exist. - v, ok := valuer.Value(expr.Val) - if !ok { - return &VarRef{Val: expr.Val, Type: expr.Type} - } - - // Return the value as a literal. - switch v := v.(type) { - case bool: - return &BooleanLiteral{Val: v} - case time.Duration: - return &DurationLiteral{Val: v} - case float64: - return &NumberLiteral{Val: v} - case string: - return &StringLiteral{Val: v} - case time.Time: - return &TimeLiteral{Val: v} - default: - return &nilLiteral{} - } -} - -// Valuer is the interface that wraps the Value() method. -// -// Value returns the value and existence flag for a given key. -type Valuer interface { - Value(key string) (interface{}, bool) -} - -// NowValuer returns only the value for "now()". -type NowValuer struct { - Now time.Time -} - -// Value is a method that returns the value and existence flag for a given key. -func (v *NowValuer) Value(key string) (interface{}, bool) { - if key == "now()" { - return v.Now, true - } - return nil, false -} - -// ContainsVarRef returns true if expr is a VarRef or contains one. -func ContainsVarRef(expr Expr) bool { - var v containsVarRefVisitor - Walk(&v, expr) - return v.contains -} - -type containsVarRefVisitor struct { - contains bool -} - -func (v *containsVarRefVisitor) Visit(n Node) Visitor { - switch n.(type) { - case *Call: - return nil - case *VarRef: - v.contains = true - } - return v -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/ast_test.go b/vendor/github.com/influxdata/influxdb/influxql/ast_test.go deleted file mode 100644 index f1d3eab5..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/ast_test.go +++ /dev/null @@ -1,1433 +0,0 @@ -package influxql_test - -import ( - "fmt" - "reflect" - "strings" - "testing" - "time" - - "github.com/influxdata/influxdb/influxql" -) - -func BenchmarkQuery_String(b *testing.B) { - p := influxql.NewParser(strings.NewReader(`SELECT foo AS zoo, a AS b FROM bar WHERE value > 10 AND q = 'hello'`)) - q, _ := p.ParseStatement() - for i := 0; i < b.N; i++ { - _ = q.String() - } -} - -// Ensure a value's data type can be retrieved. -func TestInspectDataType(t *testing.T) { - for i, tt := range []struct { - v interface{} - typ influxql.DataType - }{ - {float64(100), influxql.Float}, - {int64(100), influxql.Integer}, - {int32(100), influxql.Integer}, - {100, influxql.Integer}, - {true, influxql.Boolean}, - {"string", influxql.String}, - {time.Now(), influxql.Time}, - {time.Second, influxql.Duration}, - {nil, influxql.Unknown}, - } { - if typ := influxql.InspectDataType(tt.v); tt.typ != typ { - t.Errorf("%d. %v (%s): unexpected type: %s", i, tt.v, tt.typ, typ) - continue - } - } -} - -func TestDataType_String(t *testing.T) { - for i, tt := range []struct { - typ influxql.DataType - v string - }{ - {influxql.Float, "float"}, - {influxql.Integer, "integer"}, - {influxql.Boolean, "boolean"}, - {influxql.String, "string"}, - {influxql.Time, "time"}, - {influxql.Duration, "duration"}, - {influxql.Tag, "tag"}, - {influxql.Unknown, "unknown"}, - } { - if v := tt.typ.String(); tt.v != v { - t.Errorf("%d. %v (%s): unexpected string: %s", i, tt.typ, tt.v, v) - } - } -} - -// Ensure the SELECT statement can extract GROUP BY interval. -func TestSelectStatement_GroupByInterval(t *testing.T) { - q := "SELECT sum(value) from foo where time < now() GROUP BY time(10m)" - stmt, err := influxql.NewParser(strings.NewReader(q)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", stmt, err) - } - - s := stmt.(*influxql.SelectStatement) - d, err := s.GroupByInterval() - if d != 10*time.Minute { - t.Fatalf("group by interval not equal:\nexp=%s\ngot=%s", 10*time.Minute, d) - } - if err != nil { - t.Fatalf("error parsing group by interval: %s", err.Error()) - } -} - -// Ensure the SELECT statement can have its start and end time set -func TestSelectStatement_SetTimeRange(t *testing.T) { - q := "SELECT sum(value) from foo where time < now() GROUP BY time(10m)" - stmt, err := influxql.NewParser(strings.NewReader(q)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", stmt, err) - } - - s := stmt.(*influxql.SelectStatement) - start := time.Now().Add(-20 * time.Hour).Round(time.Second).UTC() - end := time.Now().Add(10 * time.Hour).Round(time.Second).UTC() - s.SetTimeRange(start, end) - min, max := MustTimeRange(s.Condition) - - if min != start { - t.Fatalf("start time wasn't set properly.\n exp: %s\n got: %s", start, min) - } - // the end range is actually one nanosecond before the given one since end is exclusive - end = end.Add(-time.Nanosecond) - if max != end { - t.Fatalf("end time wasn't set properly.\n exp: %s\n got: %s", end, max) - } - - // ensure we can set a time on a select that already has one set - start = time.Now().Add(-20 * time.Hour).Round(time.Second).UTC() - end = time.Now().Add(10 * time.Hour).Round(time.Second).UTC() - q = fmt.Sprintf("SELECT sum(value) from foo WHERE time >= %ds and time <= %ds GROUP BY time(10m)", start.Unix(), end.Unix()) - stmt, err = influxql.NewParser(strings.NewReader(q)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", stmt, err) - } - - s = stmt.(*influxql.SelectStatement) - min, max = MustTimeRange(s.Condition) - if start != min || end != max { - t.Fatalf("start and end times weren't equal:\n exp: %s\n got: %s\n exp: %s\n got:%s\n", start, min, end, max) - } - - // update and ensure it saves it - start = time.Now().Add(-40 * time.Hour).Round(time.Second).UTC() - end = time.Now().Add(20 * time.Hour).Round(time.Second).UTC() - s.SetTimeRange(start, end) - min, max = MustTimeRange(s.Condition) - - // TODO: right now the SetTimeRange can't override the start time if it's more recent than what they're trying to set it to. - // shouldn't matter for our purposes with continuous queries, but fix this later - - if min != start { - t.Fatalf("start time wasn't set properly.\n exp: %s\n got: %s", start, min) - } - // the end range is actually one nanosecond before the given one since end is exclusive - end = end.Add(-time.Nanosecond) - if max != end { - t.Fatalf("end time wasn't set properly.\n exp: %s\n got: %s", end, max) - } - - // ensure that when we set a time range other where clause conditions are still there - q = "SELECT sum(value) from foo WHERE foo = 'bar' and time < now() GROUP BY time(10m)" - stmt, err = influxql.NewParser(strings.NewReader(q)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", stmt, err) - } - - s = stmt.(*influxql.SelectStatement) - - // update and ensure it saves it - start = time.Now().Add(-40 * time.Hour).Round(time.Second).UTC() - end = time.Now().Add(20 * time.Hour).Round(time.Second).UTC() - s.SetTimeRange(start, end) - min, max = MustTimeRange(s.Condition) - - if min != start { - t.Fatalf("start time wasn't set properly.\n exp: %s\n got: %s", start, min) - } - // the end range is actually one nanosecond before the given one since end is exclusive - end = end.Add(-time.Nanosecond) - if max != end { - t.Fatalf("end time wasn't set properly.\n exp: %s\n got: %s", end, max) - } - - // ensure the where clause is there - hasWhere := false - influxql.WalkFunc(s.Condition, func(n influxql.Node) { - if ex, ok := n.(*influxql.BinaryExpr); ok { - if lhs, ok := ex.LHS.(*influxql.VarRef); ok { - if lhs.Val == "foo" { - if rhs, ok := ex.RHS.(*influxql.StringLiteral); ok { - if rhs.Val == "bar" { - hasWhere = true - } - } - } - } - } - }) - if !hasWhere { - t.Fatal("set time range cleared out the where clause") - } -} - -// Ensure the idents from the select clause can come out -func TestSelect_NamesInSelect(t *testing.T) { - s := MustParseSelectStatement("select count(asdf), count(bar) from cpu") - a := s.NamesInSelect() - if !reflect.DeepEqual(a, []string{"asdf", "bar"}) { - t.Fatal("expected names asdf and bar") - } -} - -// Ensure the idents from the where clause can come out -func TestSelect_NamesInWhere(t *testing.T) { - s := MustParseSelectStatement("select * from cpu where time > 23s AND (asdf = 'jkl' OR (foo = 'bar' AND baz = 'bar'))") - a := s.NamesInWhere() - if !reflect.DeepEqual(a, []string{"time", "asdf", "foo", "baz"}) { - t.Fatalf("exp: time,asdf,foo,baz\ngot: %s\n", strings.Join(a, ",")) - } -} - -func TestSelectStatement_HasWildcard(t *testing.T) { - var tests = []struct { - stmt string - wildcard bool - }{ - // No wildcards - { - stmt: `SELECT value FROM cpu`, - wildcard: false, - }, - - // Query wildcard - { - stmt: `SELECT * FROM cpu`, - wildcard: true, - }, - - // No GROUP BY wildcards - { - stmt: `SELECT value FROM cpu GROUP BY host`, - wildcard: false, - }, - - // No GROUP BY wildcards, time only - { - stmt: `SELECT mean(value) FROM cpu where time < now() GROUP BY time(5ms)`, - wildcard: false, - }, - - // GROUP BY wildcard - { - stmt: `SELECT value FROM cpu GROUP BY *`, - wildcard: true, - }, - - // GROUP BY wildcard with time - { - stmt: `SELECT mean(value) FROM cpu where time < now() GROUP BY *,time(1m)`, - wildcard: true, - }, - - // GROUP BY wildcard with explicit - { - stmt: `SELECT value FROM cpu GROUP BY *,host`, - wildcard: true, - }, - - // GROUP BY multiple wildcards - { - stmt: `SELECT value FROM cpu GROUP BY *,*`, - wildcard: true, - }, - - // Combo - { - stmt: `SELECT * FROM cpu GROUP BY *`, - wildcard: true, - }, - } - - for i, tt := range tests { - // Parse statement. - stmt, err := influxql.NewParser(strings.NewReader(tt.stmt)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", tt.stmt, err) - } - - // Test wildcard detection. - if w := stmt.(*influxql.SelectStatement).HasWildcard(); tt.wildcard != w { - t.Errorf("%d. %q: unexpected wildcard detection:\n\nexp=%v\n\ngot=%v\n\n", i, tt.stmt, tt.wildcard, w) - continue - } - } -} - -// Test SELECT statement field rewrite. -func TestSelectStatement_RewriteFields(t *testing.T) { - var tests = []struct { - stmt string - rewrite string - }{ - // No wildcards - { - stmt: `SELECT value FROM cpu`, - rewrite: `SELECT value FROM cpu`, - }, - - // Query wildcard - { - stmt: `SELECT * FROM cpu`, - rewrite: `SELECT host::tag, region::tag, value1::float, value2::integer FROM cpu`, - }, - - // Parser fundamentally prohibits multiple query sources - - // Query wildcard with explicit - { - stmt: `SELECT *,value1 FROM cpu`, - rewrite: `SELECT host::tag, region::tag, value1::float, value2::integer, value1::float FROM cpu`, - }, - - // Query multiple wildcards - { - stmt: `SELECT *,* FROM cpu`, - rewrite: `SELECT host::tag, region::tag, value1::float, value2::integer, host::tag, region::tag, value1::float, value2::integer FROM cpu`, - }, - - // Query wildcards with group by - { - stmt: `SELECT * FROM cpu GROUP BY host`, - rewrite: `SELECT region::tag, value1::float, value2::integer FROM cpu GROUP BY host`, - }, - - // No GROUP BY wildcards - { - stmt: `SELECT value FROM cpu GROUP BY host`, - rewrite: `SELECT value FROM cpu GROUP BY host`, - }, - - // No GROUP BY wildcards, time only - { - stmt: `SELECT mean(value) FROM cpu where time < now() GROUP BY time(5ms)`, - rewrite: `SELECT mean(value) FROM cpu WHERE time < now() GROUP BY time(5ms)`, - }, - - // GROUP BY wildcard - { - stmt: `SELECT value FROM cpu GROUP BY *`, - rewrite: `SELECT value FROM cpu GROUP BY host, region`, - }, - - // GROUP BY wildcard with time - { - stmt: `SELECT mean(value) FROM cpu where time < now() GROUP BY *,time(1m)`, - rewrite: `SELECT mean(value) FROM cpu WHERE time < now() GROUP BY host, region, time(1m)`, - }, - - // GROUP BY wildcard with fill - { - stmt: `SELECT mean(value) FROM cpu where time < now() GROUP BY *,time(1m) fill(0)`, - rewrite: `SELECT mean(value) FROM cpu WHERE time < now() GROUP BY host, region, time(1m) fill(0)`, - }, - - // GROUP BY wildcard with explicit - { - stmt: `SELECT value FROM cpu GROUP BY *,host`, - rewrite: `SELECT value FROM cpu GROUP BY host, region, host`, - }, - - // GROUP BY multiple wildcards - { - stmt: `SELECT value FROM cpu GROUP BY *,*`, - rewrite: `SELECT value FROM cpu GROUP BY host, region, host, region`, - }, - - // Combo - { - stmt: `SELECT * FROM cpu GROUP BY *`, - rewrite: `SELECT value1::float, value2::integer FROM cpu GROUP BY host, region`, - }, - - // Wildcard function with all fields. - { - stmt: `SELECT mean(*) FROM cpu`, - rewrite: `SELECT mean(value1::float) AS mean_value1, mean(value2::integer) AS mean_value2 FROM cpu`, - }, - - { - stmt: `SELECT distinct(*) FROM strings`, - rewrite: `SELECT distinct(string::string) AS distinct_string, distinct(value::float) AS distinct_value FROM strings`, - }, - - { - stmt: `SELECT distinct(*) FROM bools`, - rewrite: `SELECT distinct(bool::boolean) AS distinct_bool, distinct(value::float) AS distinct_value FROM bools`, - }, - - // Wildcard function with some fields excluded. - { - stmt: `SELECT mean(*) FROM strings`, - rewrite: `SELECT mean(value::float) AS mean_value FROM strings`, - }, - - { - stmt: `SELECT mean(*) FROM bools`, - rewrite: `SELECT mean(value::float) AS mean_value FROM bools`, - }, - - // Wildcard function with an alias. - { - stmt: `SELECT mean(*) AS alias FROM cpu`, - rewrite: `SELECT mean(value1::float) AS alias_value1, mean(value2::integer) AS alias_value2 FROM cpu`, - }, - - // Query regex - { - stmt: `SELECT /1/ FROM cpu`, - rewrite: `SELECT value1::float FROM cpu`, - }, - - { - stmt: `SELECT value1 FROM cpu GROUP BY /h/`, - rewrite: `SELECT value1::float FROM cpu GROUP BY host`, - }, - - // Query regex - { - stmt: `SELECT mean(/1/) FROM cpu`, - rewrite: `SELECT mean(value1::float) AS mean_value1 FROM cpu`, - }, - } - - for i, tt := range tests { - // Parse statement. - stmt, err := influxql.NewParser(strings.NewReader(tt.stmt)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", tt.stmt, err) - } - - var ic IteratorCreator - ic.FieldDimensionsFn = func(sources influxql.Sources) (fields map[string]influxql.DataType, dimensions map[string]struct{}, err error) { - source := sources[0].(*influxql.Measurement) - switch source.Name { - case "cpu": - fields = map[string]influxql.DataType{ - "value1": influxql.Float, - "value2": influxql.Integer, - } - case "strings": - fields = map[string]influxql.DataType{ - "value": influxql.Float, - "string": influxql.String, - } - case "bools": - fields = map[string]influxql.DataType{ - "value": influxql.Float, - "bool": influxql.Boolean, - } - } - dimensions = map[string]struct{}{"host": struct{}{}, "region": struct{}{}} - return - } - - // Rewrite statement. - rw, err := stmt.(*influxql.SelectStatement).RewriteFields(&ic) - if err != nil { - t.Errorf("%d. %q: error: %s", i, tt.stmt, err) - } else if rw == nil { - t.Errorf("%d. %q: unexpected nil statement", i, tt.stmt) - } else if rw := rw.String(); tt.rewrite != rw { - t.Errorf("%d. %q: unexpected rewrite:\n\nexp=%s\n\ngot=%s\n\n", i, tt.stmt, tt.rewrite, rw) - } - } -} - -// Test SELECT statement regex conditions rewrite. -func TestSelectStatement_RewriteRegexConditions(t *testing.T) { - var tests = []struct { - in string - out string - }{ - {in: `SELECT value FROM cpu`, out: `SELECT value FROM cpu`}, - {in: `SELECT value FROM cpu WHERE host='server-1'`, out: `SELECT value FROM cpu WHERE host='server-1'`}, - {in: `SELECT value FROM cpu WHERE host = 'server-1'`, out: `SELECT value FROM cpu WHERE host = 'server-1'`}, - {in: `SELECT value FROM cpu WHERE host != 'server-1'`, out: `SELECT value FROM cpu WHERE host != 'server-1'`}, - - // Non matching regex - {in: `SELECT value FROM cpu WHERE host =~ /server-1|server-2|server-3/`, out: `SELECT value FROM cpu WHERE host =~ /server-1|server-2|server-3/`}, - {in: `SELECT value FROM cpu WHERE host =~ /server-1/`, out: `SELECT value FROM cpu WHERE host =~ /server-1/`}, - {in: `SELECT value FROM cpu WHERE host !~ /server-1/`, out: `SELECT value FROM cpu WHERE host !~ /server-1/`}, - {in: `SELECT value FROM cpu WHERE host =~ /^server-1/`, out: `SELECT value FROM cpu WHERE host =~ /^server-1/`}, - {in: `SELECT value FROM cpu WHERE host =~ /server-1$/`, out: `SELECT value FROM cpu WHERE host =~ /server-1$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /\^server-1$/`, out: `SELECT value FROM cpu WHERE host !~ /\^server-1$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /\^$/`, out: `SELECT value FROM cpu WHERE host !~ /\^$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /^server-1\$/`, out: `SELECT value FROM cpu WHERE host !~ /^server-1\$/`}, - {in: `SELECT value FROM cpu WHERE host =~ /^\$/`, out: `SELECT value FROM cpu WHERE host =~ /^\$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /^a/`, out: `SELECT value FROM cpu WHERE host !~ /^a/`}, - - // These regexes are not supported due to the presence of escaped or meta characters. - {in: `SELECT value FROM cpu WHERE host !~ /^(foo|bar)$/`, out: `SELECT value FROM cpu WHERE host !~ /^(foo|bar)$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /^?a$/`, out: `SELECT value FROM cpu WHERE host !~ /^?a$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /^[a-z]$/`, out: `SELECT value FROM cpu WHERE host !~ /^[a-z]$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /^\d$/`, out: `SELECT value FROM cpu WHERE host !~ /^\d$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /^a*$/`, out: `SELECT value FROM cpu WHERE host !~ /^a*$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /^a.b$/`, out: `SELECT value FROM cpu WHERE host !~ /^a.b$/`}, - {in: `SELECT value FROM cpu WHERE host !~ /^ab+$/`, out: `SELECT value FROM cpu WHERE host !~ /^ab+$/`}, - {in: `SELECT value FROM cpu WHERE host =~ /^hello\world$/`, out: `SELECT value FROM cpu WHERE host =~ /^hello\world$/`}, - - // These regexes all match and will be rewritten. - {in: `SELECT value FROM cpu WHERE host !~ /^a[2]$/`, out: `SELECT value FROM cpu WHERE host != 'a2'`}, - {in: `SELECT value FROM cpu WHERE host =~ /^server-1$/`, out: `SELECT value FROM cpu WHERE host = 'server-1'`}, - {in: `SELECT value FROM cpu WHERE host !~ /^server-1$/`, out: `SELECT value FROM cpu WHERE host != 'server-1'`}, - {in: `SELECT value FROM cpu WHERE host =~ /^server 1$/`, out: `SELECT value FROM cpu WHERE host = 'server 1'`}, - {in: `SELECT value FROM cpu WHERE host =~ /^$/`, out: `SELECT value FROM cpu WHERE host = ''`}, - {in: `SELECT value FROM cpu WHERE host !~ /^$/`, out: `SELECT value FROM cpu WHERE host != ''`}, - {in: `SELECT value FROM cpu WHERE host =~ /^server-1$/ OR host =~ /^server-2$/`, out: `SELECT value FROM cpu WHERE host = 'server-1' OR host = 'server-2'`}, - {in: `SELECT value FROM cpu WHERE host =~ /^server-1$/ OR host =~ /^server]a$/`, out: `SELECT value FROM cpu WHERE host = 'server-1' OR host = 'server]a'`}, - {in: `SELECT value FROM cpu WHERE host =~ /^hello\?$/`, out: `SELECT value FROM cpu WHERE host = 'hello?'`}, - {in: `SELECT value FROM cpu WHERE host !~ /^\\$/`, out: `SELECT value FROM cpu WHERE host != '\\'`}, - {in: `SELECT value FROM cpu WHERE host !~ /^\\\$$/`, out: `SELECT value FROM cpu WHERE host != '\\$'`}, - } - - for i, test := range tests { - stmt, err := influxql.NewParser(strings.NewReader(test.in)).ParseStatement() - if err != nil { - t.Fatalf("[Example %d], %v", i, err) - } - - // Rewrite any supported regex conditions. - stmt.(*influxql.SelectStatement).RewriteRegexConditions() - - // Get the expected rewritten statement. - expStmt, err := influxql.NewParser(strings.NewReader(test.out)).ParseStatement() - if err != nil { - t.Fatalf("[Example %d], %v", i, err) - } - - // Compare the (potentially) rewritten AST to the expected AST. - if got, exp := stmt, expStmt; !reflect.DeepEqual(got, exp) { - t.Errorf("[Example %d]\nattempting %v\ngot %v\n%s\n\nexpected %v\n%s\n", i+1, test.in, got, mustMarshalJSON(got), exp, mustMarshalJSON(exp)) - } - } -} - -// Test SELECT statement time field rewrite. -func TestSelectStatement_RewriteTimeFields(t *testing.T) { - var tests = []struct { - s string - stmt influxql.Statement - }{ - { - s: `SELECT time, field1 FROM cpu`, - stmt: &influxql.SelectStatement{ - IsRawQuery: true, - Fields: []*influxql.Field{ - {Expr: &influxql.VarRef{Val: "field1"}}, - }, - Sources: []influxql.Source{ - &influxql.Measurement{Name: "cpu"}, - }, - }, - }, - { - s: `SELECT time AS timestamp, field1 FROM cpu`, - stmt: &influxql.SelectStatement{ - IsRawQuery: true, - Fields: []*influxql.Field{ - {Expr: &influxql.VarRef{Val: "field1"}}, - }, - Sources: []influxql.Source{ - &influxql.Measurement{Name: "cpu"}, - }, - TimeAlias: "timestamp", - }, - }, - } - - for i, tt := range tests { - // Parse statement. - stmt, err := influxql.NewParser(strings.NewReader(tt.s)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", tt.s, err) - } - - // Rewrite statement. - stmt.(*influxql.SelectStatement).RewriteTimeFields() - if !reflect.DeepEqual(tt.stmt, stmt) { - t.Logf("\n# %s\nexp=%s\ngot=%s\n", tt.s, mustMarshalJSON(tt.stmt), mustMarshalJSON(stmt)) - t.Logf("\nSQL exp=%s\nSQL got=%s\n", tt.stmt.String(), stmt.String()) - t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt) - } - } -} - -// Ensure that the IsRawQuery flag gets set properly -func TestSelectStatement_IsRawQuerySet(t *testing.T) { - var tests = []struct { - stmt string - isRaw bool - }{ - { - stmt: "select * from foo", - isRaw: true, - }, - { - stmt: "select value1,value2 from foo", - isRaw: true, - }, - { - stmt: "select value1,value2 from foo, time(10m)", - isRaw: true, - }, - { - stmt: "select mean(value) from foo where time < now() group by time(5m)", - isRaw: false, - }, - { - stmt: "select mean(value) from foo group by bar", - isRaw: false, - }, - { - stmt: "select mean(value) from foo group by *", - isRaw: false, - }, - { - stmt: "select mean(value) from foo group by *", - isRaw: false, - }, - } - - for _, tt := range tests { - s := MustParseSelectStatement(tt.stmt) - if s.IsRawQuery != tt.isRaw { - t.Errorf("'%s', IsRawQuery should be %v", tt.stmt, tt.isRaw) - } - } -} - -func TestSelectStatement_HasDerivative(t *testing.T) { - var tests = []struct { - stmt string - derivative bool - }{ - // No derivatives - { - stmt: `SELECT value FROM cpu`, - derivative: false, - }, - - // Query derivative - { - stmt: `SELECT derivative(value) FROM cpu`, - derivative: true, - }, - - // No GROUP BY time only - { - stmt: `SELECT mean(value) FROM cpu where time < now() GROUP BY time(5ms)`, - derivative: false, - }, - - // No GROUP BY derivatives, time only - { - stmt: `SELECT derivative(mean(value)) FROM cpu where time < now() GROUP BY time(5ms)`, - derivative: true, - }, - - { - stmt: `SELECT value FROM cpu`, - derivative: false, - }, - - // Query derivative - { - stmt: `SELECT non_negative_derivative(value) FROM cpu`, - derivative: true, - }, - - // No GROUP BY derivatives, time only - { - stmt: `SELECT non_negative_derivative(mean(value)) FROM cpu where time < now() GROUP BY time(5ms)`, - derivative: true, - }, - - // Invalid derivative function name - { - stmt: `SELECT typoDerivative(value) FROM cpu where time < now()`, - derivative: false, - }, - } - - for i, tt := range tests { - // Parse statement. - t.Logf("index: %d, statement: %s", i, tt.stmt) - stmt, err := influxql.NewParser(strings.NewReader(tt.stmt)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", tt.stmt, err) - } - - // Test derivative detection. - if d := stmt.(*influxql.SelectStatement).HasDerivative(); tt.derivative != d { - t.Errorf("%d. %q: unexpected derivative detection:\n\nexp=%v\n\ngot=%v\n\n", i, tt.stmt, tt.derivative, d) - continue - } - } -} - -func TestSelectStatement_IsSimpleDerivative(t *testing.T) { - var tests = []struct { - stmt string - derivative bool - }{ - // No derivatives - { - stmt: `SELECT value FROM cpu`, - derivative: false, - }, - - // Query derivative - { - stmt: `SELECT derivative(value) FROM cpu`, - derivative: true, - }, - - // Query derivative - { - stmt: `SELECT non_negative_derivative(value) FROM cpu`, - derivative: true, - }, - - // No GROUP BY time only - { - stmt: `SELECT mean(value) FROM cpu where time < now() GROUP BY time(5ms)`, - derivative: false, - }, - - // No GROUP BY derivatives, time only - { - stmt: `SELECT non_negative_derivative(mean(value)) FROM cpu where time < now() GROUP BY time(5ms)`, - derivative: false, - }, - - // Invalid derivative function name - { - stmt: `SELECT typoDerivative(value) FROM cpu where time < now()`, - derivative: false, - }, - } - - for i, tt := range tests { - // Parse statement. - t.Logf("index: %d, statement: %s", i, tt.stmt) - stmt, err := influxql.NewParser(strings.NewReader(tt.stmt)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", tt.stmt, err) - } - - // Test derivative detection. - if d := stmt.(*influxql.SelectStatement).IsSimpleDerivative(); tt.derivative != d { - t.Errorf("%d. %q: unexpected derivative detection:\n\nexp=%v\n\ngot=%v\n\n", i, tt.stmt, tt.derivative, d) - continue - } - } -} - -// Ensure binary expression names can be evaluated. -func TestBinaryExprName(t *testing.T) { - for i, tt := range []struct { - expr string - name string - }{ - {expr: `value + 1`, name: `value`}, - {expr: `"user" / total`, name: `user_total`}, - {expr: `("user" + total) / total`, name: `user_total_total`}, - } { - expr := influxql.MustParseExpr(tt.expr) - switch expr := expr.(type) { - case *influxql.BinaryExpr: - name := influxql.BinaryExprName(expr) - if name != tt.name { - t.Errorf("%d. unexpected name %s, got %s", i, name, tt.name) - } - default: - t.Errorf("%d. unexpected expr type: %T", i, expr) - } - } -} - -// Ensure the time range of an expression can be extracted. -func TestTimeRange(t *testing.T) { - for i, tt := range []struct { - expr string - min, max, err string - }{ - // LHS VarRef - {expr: `time > '2000-01-01 00:00:00'`, min: `2000-01-01T00:00:00.000000001Z`, max: `0001-01-01T00:00:00Z`}, - {expr: `time >= '2000-01-01 00:00:00'`, min: `2000-01-01T00:00:00Z`, max: `0001-01-01T00:00:00Z`}, - {expr: `time < '2000-01-01 00:00:00'`, min: `0001-01-01T00:00:00Z`, max: `1999-12-31T23:59:59.999999999Z`}, - {expr: `time <= '2000-01-01 00:00:00'`, min: `0001-01-01T00:00:00Z`, max: `2000-01-01T00:00:00Z`}, - - // RHS VarRef - {expr: `'2000-01-01 00:00:00' > time`, min: `0001-01-01T00:00:00Z`, max: `1999-12-31T23:59:59.999999999Z`}, - {expr: `'2000-01-01 00:00:00' >= time`, min: `0001-01-01T00:00:00Z`, max: `2000-01-01T00:00:00Z`}, - {expr: `'2000-01-01 00:00:00' < time`, min: `2000-01-01T00:00:00.000000001Z`, max: `0001-01-01T00:00:00Z`}, - {expr: `'2000-01-01 00:00:00' <= time`, min: `2000-01-01T00:00:00Z`, max: `0001-01-01T00:00:00Z`}, - - // number literal - {expr: `time < 10`, min: `0001-01-01T00:00:00Z`, max: `1970-01-01T00:00:00.000000009Z`}, - - // Equality - {expr: `time = '2000-01-01 00:00:00'`, min: `2000-01-01T00:00:00Z`, max: `2000-01-01T00:00:00.000000001Z`}, - - // Multiple time expressions. - {expr: `time >= '2000-01-01 00:00:00' AND time < '2000-01-02 00:00:00'`, min: `2000-01-01T00:00:00Z`, max: `2000-01-01T23:59:59.999999999Z`}, - - // Min/max crossover - {expr: `time >= '2000-01-01 00:00:00' AND time <= '1999-01-01 00:00:00'`, min: `2000-01-01T00:00:00Z`, max: `1999-01-01T00:00:00Z`}, - - // Absolute time - {expr: `time = 1388534400s`, min: `2014-01-01T00:00:00Z`, max: `2014-01-01T00:00:00.000000001Z`}, - - // Non-comparative expressions. - {expr: `time`, min: `0001-01-01T00:00:00Z`, max: `0001-01-01T00:00:00Z`}, - {expr: `time + 2`, min: `0001-01-01T00:00:00Z`, max: `0001-01-01T00:00:00Z`}, - {expr: `time - '2000-01-01 00:00:00'`, min: `0001-01-01T00:00:00Z`, max: `0001-01-01T00:00:00Z`}, - {expr: `time AND '2000-01-01 00:00:00'`, min: `0001-01-01T00:00:00Z`, max: `0001-01-01T00:00:00Z`}, - - // Invalid time expressions. - {expr: `time > "2000-01-01 00:00:00"`, min: `0001-01-01T00:00:00Z`, max: `0001-01-01T00:00:00Z`, err: `invalid operation: time and *influxql.VarRef are not compatible`}, - {expr: `time > '2262-04-11 23:47:17'`, min: `0001-01-01T00:00:00Z`, max: `0001-01-01T00:00:00Z`, err: `time 2262-04-11T23:47:17Z overflows time literal`}, - {expr: `time > '1677-09-20 19:12:43'`, min: `0001-01-01T00:00:00Z`, max: `0001-01-01T00:00:00Z`, err: `time 1677-09-20T19:12:43Z underflows time literal`}, - } { - // Extract time range. - expr := MustParseExpr(tt.expr) - min, max, err := influxql.TimeRange(expr) - - // Compare with expected min/max. - if min := min.Format(time.RFC3339Nano); tt.min != min { - t.Errorf("%d. %s: unexpected min:\n\nexp=%s\n\ngot=%s\n\n", i, tt.expr, tt.min, min) - continue - } - if max := max.Format(time.RFC3339Nano); tt.max != max { - t.Errorf("%d. %s: unexpected max:\n\nexp=%s\n\ngot=%s\n\n", i, tt.expr, tt.max, max) - continue - } - if (err != nil && err.Error() != tt.err) || (err == nil && tt.err != "") { - t.Errorf("%d. %s: unexpected error:\n\nexp=%s\n\ngot=%s\n\n", i, tt.expr, tt.err, err) - } - } -} - -// Ensure that we see if a where clause has only time limitations -func TestOnlyTimeExpr(t *testing.T) { - var tests = []struct { - stmt string - exp bool - }{ - { - stmt: `SELECT value FROM myseries WHERE value > 1`, - exp: false, - }, - { - stmt: `SELECT value FROM foo WHERE time >= '2000-01-01T00:00:05Z'`, - exp: true, - }, - { - stmt: `SELECT value FROM foo WHERE time >= '2000-01-01T00:00:05Z' AND time < '2000-01-01T00:00:05Z'`, - exp: true, - }, - { - stmt: `SELECT value FROM foo WHERE time >= '2000-01-01T00:00:05Z' AND asdf = 'bar'`, - exp: false, - }, - { - stmt: `SELECT value FROM foo WHERE asdf = 'jkl' AND (time >= '2000-01-01T00:00:05Z' AND time < '2000-01-01T00:00:05Z')`, - exp: false, - }, - } - - for i, tt := range tests { - // Parse statement. - stmt, err := influxql.NewParser(strings.NewReader(tt.stmt)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", tt.stmt, err) - } - if influxql.OnlyTimeExpr(stmt.(*influxql.SelectStatement).Condition) != tt.exp { - t.Fatalf("%d. expected statement to return only time dimension to be %t: %s", i, tt.exp, tt.stmt) - } - } -} - -// Ensure an AST node can be rewritten. -func TestRewrite(t *testing.T) { - expr := MustParseExpr(`time > 1 OR foo = 2`) - - // Flip LHS & RHS in all binary expressions. - act := influxql.RewriteFunc(expr, func(n influxql.Node) influxql.Node { - switch n := n.(type) { - case *influxql.BinaryExpr: - return &influxql.BinaryExpr{Op: n.Op, LHS: n.RHS, RHS: n.LHS} - default: - return n - } - }) - - // Verify that everything is flipped. - if act := act.String(); act != `2 = foo OR 1 > time` { - t.Fatalf("unexpected result: %s", act) - } -} - -// Ensure an Expr can be rewritten handling nils. -func TestRewriteExpr(t *testing.T) { - expr := MustParseExpr(`(time > 1 AND time < 10) OR foo = 2`) - - // Remove all time expressions. - act := influxql.RewriteExpr(expr, func(e influxql.Expr) influxql.Expr { - switch e := e.(type) { - case *influxql.BinaryExpr: - if lhs, ok := e.LHS.(*influxql.VarRef); ok && lhs.Val == "time" { - return nil - } - } - return e - }) - - // Verify that everything is flipped. - if act := act.String(); act != `foo = 2` { - t.Fatalf("unexpected result: %s", act) - } -} - -// Ensure that the String() value of a statement is parseable -func TestParseString(t *testing.T) { - var tests = []struct { - stmt string - }{ - { - stmt: `SELECT "cpu load" FROM myseries`, - }, - { - stmt: `SELECT "cpu load" FROM "my series"`, - }, - { - stmt: `SELECT "cpu\"load" FROM myseries`, - }, - { - stmt: `SELECT "cpu'load" FROM myseries`, - }, - { - stmt: `SELECT "cpu load" FROM "my\"series"`, - }, - { - stmt: `SELECT "field with spaces" FROM "\"ugly\" db"."\"ugly\" rp"."\"ugly\" measurement"`, - }, - { - stmt: `SELECT * FROM myseries`, - }, - { - stmt: `DROP DATABASE "!"`, - }, - { - stmt: `DROP RETENTION POLICY "my rp" ON "a database"`, - }, - { - stmt: `CREATE RETENTION POLICY "my rp" ON "a database" DURATION 1d REPLICATION 1`, - }, - { - stmt: `ALTER RETENTION POLICY "my rp" ON "a database" DEFAULT`, - }, - { - stmt: `SHOW RETENTION POLICIES ON "a database"`, - }, - { - stmt: `SHOW TAG VALUES WITH KEY IN ("a long name", short)`, - }, - { - stmt: `DROP CONTINUOUS QUERY "my query" ON "my database"`, - }, - // See issues https://github.com/influxdata/influxdb/issues/1647 - // and https://github.com/influxdata/influxdb/issues/4404 - //{ - // stmt: `DELETE FROM "my db"."my rp"."my measurement"`, - //}, - { - stmt: `DROP SUBSCRIPTION "ugly \"subscription\" name" ON "\"my\" db"."\"my\" rp"`, - }, - { - stmt: `CREATE SUBSCRIPTION "ugly \"subscription\" name" ON "\"my\" db"."\"my\" rp" DESTINATIONS ALL 'my host', 'my other host'`, - }, - { - stmt: `SHOW MEASUREMENTS WITH MEASUREMENT =~ /foo/`, - }, - { - stmt: `SHOW MEASUREMENTS WITH MEASUREMENT = "and/or"`, - }, - { - stmt: `DROP USER "user with spaces"`, - }, - { - stmt: `GRANT ALL PRIVILEGES ON "db with spaces" TO "user with spaces"`, - }, - { - stmt: `GRANT ALL PRIVILEGES TO "user with spaces"`, - }, - { - stmt: `SHOW GRANTS FOR "user with spaces"`, - }, - { - stmt: `REVOKE ALL PRIVILEGES ON "db with spaces" FROM "user with spaces"`, - }, - { - stmt: `REVOKE ALL PRIVILEGES FROM "user with spaces"`, - }, - { - stmt: `CREATE DATABASE "db with spaces"`, - }, - } - - for _, tt := range tests { - // Parse statement. - stmt, err := influxql.NewParser(strings.NewReader(tt.stmt)).ParseStatement() - if err != nil { - t.Fatalf("invalid statement: %q: %s", tt.stmt, err) - } - - stmtCopy, err := influxql.NewParser(strings.NewReader(stmt.String())).ParseStatement() - if err != nil { - t.Fatalf("failed to parse string: %v\norig: %v\ngot: %v", err, tt.stmt, stmt.String()) - } - - if !reflect.DeepEqual(stmt, stmtCopy) { - t.Fatalf("statement changed after stringifying and re-parsing:\noriginal : %v\nre-parsed: %v\n", tt.stmt, stmtCopy.String()) - } - } -} - -// Ensure an expression can be reduced. -func TestEval(t *testing.T) { - for i, tt := range []struct { - in string - out interface{} - data map[string]interface{} - }{ - // Number literals. - {in: `1 + 2`, out: int64(3)}, - {in: `(foo*2) + ( (4/2) + (3 * 5) - 0.5 )`, out: float64(26.5), data: map[string]interface{}{"foo": float64(5)}}, - {in: `foo / 2`, out: float64(2), data: map[string]interface{}{"foo": float64(4)}}, - {in: `4 = 4`, out: true}, - {in: `4 <> 4`, out: false}, - {in: `6 > 4`, out: true}, - {in: `4 >= 4`, out: true}, - {in: `4 < 6`, out: true}, - {in: `4 <= 4`, out: true}, - {in: `4 AND 5`, out: nil}, - {in: `0 = 'test'`, out: false}, - {in: `1.0 = 1`, out: true}, - {in: `1.2 = 1`, out: false}, - - // Boolean literals. - {in: `true AND false`, out: false}, - {in: `true OR false`, out: true}, - {in: `false = 4`, out: false}, - - // String literals. - {in: `'foo' = 'bar'`, out: false}, - {in: `'foo' = 'foo'`, out: true}, - {in: `'' = 4`, out: false}, - - // Regex literals. - {in: `'foo' =~ /f.*/`, out: true}, - {in: `'foo' =~ /b.*/`, out: false}, - {in: `'foo' !~ /f.*/`, out: false}, - {in: `'foo' !~ /b.*/`, out: true}, - - // Variable references. - {in: `foo`, out: "bar", data: map[string]interface{}{"foo": "bar"}}, - {in: `foo = 'bar'`, out: true, data: map[string]interface{}{"foo": "bar"}}, - {in: `foo = 'bar'`, out: nil, data: map[string]interface{}{"foo": nil}}, - {in: `foo <> 'bar'`, out: true, data: map[string]interface{}{"foo": "xxx"}}, - {in: `foo =~ /b.*/`, out: true, data: map[string]interface{}{"foo": "bar"}}, - {in: `foo !~ /b.*/`, out: false, data: map[string]interface{}{"foo": "bar"}}, - } { - // Evaluate expression. - out := influxql.Eval(MustParseExpr(tt.in), tt.data) - - // Compare with expected output. - if !reflect.DeepEqual(tt.out, out) { - t.Errorf("%d. %s: unexpected output:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.in, tt.out, out) - continue - } - } -} - -// Ensure an expression can be reduced. -func TestReduce(t *testing.T) { - now := mustParseTime("2000-01-01T00:00:00Z") - - for i, tt := range []struct { - in string - out string - data Valuer - }{ - // Number literals. - {in: `1 + 2`, out: `3`}, - {in: `(foo*2) + ( (4/2) + (3 * 5) - 0.5 )`, out: `(foo * 2) + 16.500`}, - {in: `foo(bar(2 + 3), 4)`, out: `foo(bar(5), 4)`}, - {in: `4 / 0`, out: `0.000`}, - {in: `1 / 2`, out: `0.500`}, - {in: `4 = 4`, out: `true`}, - {in: `4 <> 4`, out: `false`}, - {in: `6 > 4`, out: `true`}, - {in: `4 >= 4`, out: `true`}, - {in: `4 < 6`, out: `true`}, - {in: `4 <= 4`, out: `true`}, - {in: `4 AND 5`, out: `4 AND 5`}, - - // Boolean literals. - {in: `true AND false`, out: `false`}, - {in: `true OR false`, out: `true`}, - {in: `true OR (foo = bar AND 1 > 2)`, out: `true`}, - {in: `(foo = bar AND 1 > 2) OR true`, out: `true`}, - {in: `false OR (foo = bar AND 1 > 2)`, out: `false`}, - {in: `(foo = bar AND 1 > 2) OR false`, out: `false`}, - {in: `true = false`, out: `false`}, - {in: `true <> false`, out: `true`}, - {in: `true + false`, out: `true + false`}, - - // Time literals with now(). - {in: `now() + 2h`, out: `'2000-01-01T02:00:00Z'`, data: map[string]interface{}{"now()": now}}, - {in: `now() / 2h`, out: `'2000-01-01T00:00:00Z' / 2h`, data: map[string]interface{}{"now()": now}}, - {in: `4µ + now()`, out: `'2000-01-01T00:00:00.000004Z'`, data: map[string]interface{}{"now()": now}}, - {in: `now() + 2000000000`, out: `'2000-01-01T00:00:02Z'`, data: map[string]interface{}{"now()": now}}, - {in: `2000000000 + now()`, out: `'2000-01-01T00:00:02Z'`, data: map[string]interface{}{"now()": now}}, - {in: `now() - 2000000000`, out: `'1999-12-31T23:59:58Z'`, data: map[string]interface{}{"now()": now}}, - {in: `now() = now()`, out: `true`, data: map[string]interface{}{"now()": now}}, - {in: `now() <> now()`, out: `false`, data: map[string]interface{}{"now()": now}}, - {in: `now() < now() + 1h`, out: `true`, data: map[string]interface{}{"now()": now}}, - {in: `now() <= now() + 1h`, out: `true`, data: map[string]interface{}{"now()": now}}, - {in: `now() >= now() - 1h`, out: `true`, data: map[string]interface{}{"now()": now}}, - {in: `now() > now() - 1h`, out: `true`, data: map[string]interface{}{"now()": now}}, - {in: `now() - (now() - 60s)`, out: `1m`, data: map[string]interface{}{"now()": now}}, - {in: `now() AND now()`, out: `'2000-01-01T00:00:00Z' AND '2000-01-01T00:00:00Z'`, data: map[string]interface{}{"now()": now}}, - {in: `now()`, out: `now()`}, - {in: `946684800000000000 + 2h`, out: `'2000-01-01T02:00:00Z'`}, - - // Time literals. - {in: `'2000-01-01T00:00:00Z' + 2h`, out: `'2000-01-01T02:00:00Z'`}, - {in: `'2000-01-01T00:00:00Z' / 2h`, out: `'2000-01-01T00:00:00Z' / 2h`}, - {in: `4µ + '2000-01-01T00:00:00Z'`, out: `'2000-01-01T00:00:00.000004Z'`}, - {in: `'2000-01-01T00:00:00Z' + 2000000000`, out: `'2000-01-01T00:00:02Z'`}, - {in: `2000000000 + '2000-01-01T00:00:00Z'`, out: `'2000-01-01T00:00:02Z'`}, - {in: `'2000-01-01T00:00:00Z' - 2000000000`, out: `'1999-12-31T23:59:58Z'`}, - {in: `'2000-01-01T00:00:00Z' = '2000-01-01T00:00:00Z'`, out: `true`}, - {in: `'2000-01-01T00:00:00.000000000Z' = '2000-01-01T00:00:00Z'`, out: `true`}, - {in: `'2000-01-01T00:00:00Z' <> '2000-01-01T00:00:00Z'`, out: `false`}, - {in: `'2000-01-01T00:00:00.000000000Z' <> '2000-01-01T00:00:00Z'`, out: `false`}, - {in: `'2000-01-01T00:00:00Z' < '2000-01-01T00:00:00Z' + 1h`, out: `true`}, - {in: `'2000-01-01T00:00:00.000000000Z' < '2000-01-01T00:00:00Z' + 1h`, out: `true`}, - {in: `'2000-01-01T00:00:00Z' <= '2000-01-01T00:00:00Z' + 1h`, out: `true`}, - {in: `'2000-01-01T00:00:00.000000000Z' <= '2000-01-01T00:00:00Z' + 1h`, out: `true`}, - {in: `'2000-01-01T00:00:00Z' > '2000-01-01T00:00:00Z' - 1h`, out: `true`}, - {in: `'2000-01-01T00:00:00.000000000Z' > '2000-01-01T00:00:00Z' - 1h`, out: `true`}, - {in: `'2000-01-01T00:00:00Z' >= '2000-01-01T00:00:00Z' - 1h`, out: `true`}, - {in: `'2000-01-01T00:00:00.000000000Z' >= '2000-01-01T00:00:00Z' - 1h`, out: `true`}, - {in: `'2000-01-01T00:00:00Z' - ('2000-01-01T00:00:00Z' - 60s)`, out: `1m`}, - {in: `'2000-01-01T00:00:00Z' AND '2000-01-01T00:00:00Z'`, out: `'2000-01-01T00:00:00Z' AND '2000-01-01T00:00:00Z'`}, - - // Duration literals. - {in: `10m + 1h - 60s`, out: `69m`}, - {in: `(10m / 2) * 5`, out: `25m`}, - {in: `60s = 1m`, out: `true`}, - {in: `60s <> 1m`, out: `false`}, - {in: `60s < 1h`, out: `true`}, - {in: `60s <= 1h`, out: `true`}, - {in: `60s > 12s`, out: `true`}, - {in: `60s >= 1m`, out: `true`}, - {in: `60s AND 1m`, out: `1m AND 1m`}, - {in: `60m / 0`, out: `0s`}, - {in: `60m + 50`, out: `1h + 50`}, - - // String literals. - {in: `'foo' + 'bar'`, out: `'foobar'`}, - - // Variable references. - {in: `foo`, out: `'bar'`, data: map[string]interface{}{"foo": "bar"}}, - {in: `foo = 'bar'`, out: `true`, data: map[string]interface{}{"foo": "bar"}}, - {in: `foo = 'bar'`, out: `false`, data: map[string]interface{}{"foo": nil}}, - {in: `foo <> 'bar'`, out: `false`, data: map[string]interface{}{"foo": nil}}, - } { - // Fold expression. - expr := influxql.Reduce(MustParseExpr(tt.in), tt.data) - - // Compare with expected output. - if out := expr.String(); tt.out != out { - t.Errorf("%d. %s: unexpected expr:\n\nexp=%s\n\ngot=%s\n\n", i, tt.in, tt.out, out) - continue - } - } -} - -func Test_fieldsNames(t *testing.T) { - for _, test := range []struct { - in []string - out []string - alias []string - }{ - { //case: binary expr(valRef) - in: []string{"value+value"}, - out: []string{"value", "value"}, - alias: []string{"value_value"}, - }, - { //case: binary expr + valRef - in: []string{"value+value", "temperature"}, - out: []string{"value", "value", "temperature"}, - alias: []string{"value_value", "temperature"}, - }, - { //case: aggregate expr - in: []string{"mean(value)"}, - out: []string{"mean"}, - alias: []string{"mean"}, - }, - { //case: binary expr(aggregate expr) - in: []string{"mean(value) + max(value)"}, - out: []string{"value", "value"}, - alias: []string{"mean_max"}, - }, - { //case: binary expr(aggregate expr) + valRef - in: []string{"mean(value) + max(value)", "temperature"}, - out: []string{"value", "value", "temperature"}, - alias: []string{"mean_max", "temperature"}, - }, - { //case: mixed aggregate and varRef - in: []string{"mean(value) + temperature"}, - out: []string{"value", "temperature"}, - alias: []string{"mean_temperature"}, - }, - { //case: ParenExpr(varRef) - in: []string{"(value)"}, - out: []string{"value"}, - alias: []string{"value"}, - }, - { //case: ParenExpr(varRef + varRef) - in: []string{"(value + value)"}, - out: []string{"value", "value"}, - alias: []string{"value_value"}, - }, - { //case: ParenExpr(aggregate) - in: []string{"(mean(value))"}, - out: []string{"value"}, - alias: []string{"mean"}, - }, - { //case: ParenExpr(aggregate + aggregate) - in: []string{"(mean(value) + max(value))"}, - out: []string{"value", "value"}, - alias: []string{"mean_max"}, - }, - } { - fields := influxql.Fields{} - for _, s := range test.in { - expr := MustParseExpr(s) - fields = append(fields, &influxql.Field{Expr: expr}) - } - got := fields.Names() - if !reflect.DeepEqual(got, test.out) { - t.Errorf("get fields name:\nexp=%v\ngot=%v\n", test.out, got) - } - alias := fields.AliasNames() - if !reflect.DeepEqual(alias, test.alias) { - t.Errorf("get fields alias name:\nexp=%v\ngot=%v\n", test.alias, alias) - } - } - -} - -func TestSelect_ColumnNames(t *testing.T) { - for i, tt := range []struct { - stmt *influxql.SelectStatement - columns []string - }{ - { - stmt: &influxql.SelectStatement{ - Fields: influxql.Fields([]*influxql.Field{ - {Expr: &influxql.VarRef{Val: "value"}}, - }), - }, - columns: []string{"time", "value"}, - }, - { - stmt: &influxql.SelectStatement{ - Fields: influxql.Fields([]*influxql.Field{ - {Expr: &influxql.VarRef{Val: "value"}}, - {Expr: &influxql.VarRef{Val: "value"}}, - {Expr: &influxql.VarRef{Val: "value_1"}}, - }), - }, - columns: []string{"time", "value", "value_1", "value_1_1"}, - }, - { - stmt: &influxql.SelectStatement{ - Fields: influxql.Fields([]*influxql.Field{ - {Expr: &influxql.VarRef{Val: "value"}}, - {Expr: &influxql.VarRef{Val: "value_1"}}, - {Expr: &influxql.VarRef{Val: "value"}}, - }), - }, - columns: []string{"time", "value", "value_1", "value_2"}, - }, - { - stmt: &influxql.SelectStatement{ - Fields: influxql.Fields([]*influxql.Field{ - {Expr: &influxql.VarRef{Val: "value"}}, - {Expr: &influxql.VarRef{Val: "total"}, Alias: "value"}, - {Expr: &influxql.VarRef{Val: "value"}}, - }), - }, - columns: []string{"time", "value_1", "value", "value_2"}, - }, - { - stmt: &influxql.SelectStatement{ - Fields: influxql.Fields([]*influxql.Field{ - {Expr: &influxql.VarRef{Val: "value"}}, - }), - TimeAlias: "timestamp", - }, - columns: []string{"timestamp", "value"}, - }, - } { - columns := tt.stmt.ColumnNames() - if !reflect.DeepEqual(columns, tt.columns) { - t.Errorf("%d. expected %s, got %s", i, tt.columns, columns) - } - } -} - -func TestSelect_Privileges(t *testing.T) { - stmt := &influxql.SelectStatement{ - Target: &influxql.Target{ - Measurement: &influxql.Measurement{Database: "db2"}, - }, - Sources: []influxql.Source{ - &influxql.Measurement{Database: "db0"}, - &influxql.Measurement{Database: "db1"}, - }, - } - - exp := influxql.ExecutionPrivileges{ - influxql.ExecutionPrivilege{Name: "db0", Privilege: influxql.ReadPrivilege}, - influxql.ExecutionPrivilege{Name: "db1", Privilege: influxql.ReadPrivilege}, - influxql.ExecutionPrivilege{Name: "db2", Privilege: influxql.WritePrivilege}, - } - - got, err := stmt.RequiredPrivileges() - if err != nil { - t.Fatal(err) - } - - if !reflect.DeepEqual(exp, got) { - t.Errorf("exp: %v, got: %v", exp, got) - } -} - -func TestSources_Names(t *testing.T) { - sources := influxql.Sources([]influxql.Source{ - &influxql.Measurement{ - Name: "cpu", - }, - &influxql.Measurement{ - Name: "mem", - }, - }) - - names := sources.Names() - if names[0] != "cpu" { - t.Errorf("expected cpu, got %s", names[0]) - } - if names[1] != "mem" { - t.Errorf("expected mem, got %s", names[1]) - } -} - -func TestSources_HasSystemSource(t *testing.T) { - sources := influxql.Sources([]influxql.Source{ - &influxql.Measurement{ - Name: "_measurements", - }, - }) - - ok := sources.HasSystemSource() - if !ok { - t.Errorf("expected to find a system source, found none") - } - - sources = influxql.Sources([]influxql.Source{ - &influxql.Measurement{ - Name: "cpu", - }, - }) - - ok = sources.HasSystemSource() - if ok { - t.Errorf("expected to find no system source, found one") - } -} - -// Parse statements that might appear valid but should return an error. -// If allowed to execute, at least some of these statements would result in a panic. -func TestParse_Errors(t *testing.T) { - for _, tt := range []struct { - tmpl string - good string - bad string - }{ - // Second argument to derivative must be duration - {tmpl: `SELECT derivative(f, %s) FROM m`, good: "1h", bad: "true"}, - } { - good := fmt.Sprintf(tt.tmpl, tt.good) - if _, err := influxql.ParseStatement(good); err != nil { - t.Fatalf("statement %q should have parsed correctly but returned error: %s", good, err) - } - - bad := fmt.Sprintf(tt.tmpl, tt.bad) - if _, err := influxql.ParseStatement(bad); err == nil { - t.Fatalf("statement %q should have resulted in a parse error but did not", bad) - } - } -} - -// Valuer represents a simple wrapper around a map to implement the influxql.Valuer interface. -type Valuer map[string]interface{} - -// Value returns the value and existence of a key. -func (o Valuer) Value(key string) (v interface{}, ok bool) { - v, ok = o[key] - return -} - -// MustTimeRange will parse a time range. Panic on error. -func MustTimeRange(expr influxql.Expr) (min, max time.Time) { - min, max, err := influxql.TimeRange(expr) - if err != nil { - panic(err) - } - return min, max -} - -// mustParseTime parses an IS0-8601 string. Panic on error. -func mustParseTime(s string) time.Time { - t, err := time.Parse(time.RFC3339, s) - if err != nil { - panic(err.Error()) - } - return t -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/call_iterator.go b/vendor/github.com/influxdata/influxdb/influxql/call_iterator.go deleted file mode 100644 index c78b5fda..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/call_iterator.go +++ /dev/null @@ -1,1294 +0,0 @@ -package influxql - -import ( - "bytes" - "container/heap" - "fmt" - "math" - "sort" - "time" -) - -/* -This file contains iterator implementations for each function call available -in InfluxQL. Call iterators are separated into two groups: - -1. Map/reduce-style iterators - these are passed to IteratorCreator so that - processing can be at the low-level storage and aggregates are returned. - -2. Raw aggregate iterators - these require the full set of data for a window. - These are handled by the select() function and raw points are streamed in - from the low-level storage. - -There are helpers to aid in building aggregate iterators. For simple map/reduce -iterators, you can use the reduceIterator types and pass a reduce function. This -reduce function is passed a previous and current value and the new timestamp, -value, and auxilary fields are returned from it. - -For raw aggregate iterators, you can use the reduceSliceIterators which pass -in a slice of all points to the function and return a point. For more complex -iterator types, you may need to create your own iterators by hand. - -Once your iterator is complete, you'll need to add it to the NewCallIterator() -function if it is to be available to IteratorCreators and add it to the select() -function to allow it to be included during planning. -*/ - -// NewCallIterator returns a new iterator for a Call. -func NewCallIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - name := opt.Expr.(*Call).Name - switch name { - case "count": - return newCountIterator(input, opt) - case "min": - return newMinIterator(input, opt) - case "max": - return newMaxIterator(input, opt) - case "sum": - return newSumIterator(input, opt) - case "first": - return newFirstIterator(input, opt) - case "last": - return newLastIterator(input, opt) - case "mean": - return newMeanIterator(input, opt) - default: - return nil, fmt.Errorf("unsupported function call: %s", name) - } -} - -// newCountIterator returns an iterator for operating on a count() call. -func newCountIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - // FIXME: Wrap iterator in int-type iterator and always output int value. - - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, IntegerPointEmitter) { - fn := NewFloatFuncIntegerReducer(FloatCountReduce, &IntegerPoint{Value: 0, Time: ZeroTime}) - return fn, fn - } - return &floatReduceIntegerIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerFuncReducer(IntegerCountReduce, &IntegerPoint{Value: 0, Time: ZeroTime}) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - case StringIterator: - createFn := func() (StringPointAggregator, IntegerPointEmitter) { - fn := NewStringFuncIntegerReducer(StringCountReduce, &IntegerPoint{Value: 0, Time: ZeroTime}) - return fn, fn - } - return &stringReduceIntegerIterator{input: newBufStringIterator(input), opt: opt, create: createFn}, nil - case BooleanIterator: - createFn := func() (BooleanPointAggregator, IntegerPointEmitter) { - fn := NewBooleanFuncIntegerReducer(BooleanCountReduce, &IntegerPoint{Value: 0, Time: ZeroTime}) - return fn, fn - } - return &booleanReduceIntegerIterator{input: newBufBooleanIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported count iterator type: %T", input) - } -} - -// FloatCountReduce returns the count of points. -func FloatCountReduce(prev *IntegerPoint, curr *FloatPoint) (int64, int64, []interface{}) { - if prev == nil { - return ZeroTime, 1, nil - } - return ZeroTime, prev.Value + 1, nil -} - -// IntegerCountReduce returns the count of points. -func IntegerCountReduce(prev, curr *IntegerPoint) (int64, int64, []interface{}) { - if prev == nil { - return ZeroTime, 1, nil - } - return ZeroTime, prev.Value + 1, nil -} - -// StringCountReduce returns the count of points. -func StringCountReduce(prev *IntegerPoint, curr *StringPoint) (int64, int64, []interface{}) { - if prev == nil { - return ZeroTime, 1, nil - } - return ZeroTime, prev.Value + 1, nil -} - -// BooleanCountReduce returns the count of points. -func BooleanCountReduce(prev *IntegerPoint, curr *BooleanPoint) (int64, int64, []interface{}) { - if prev == nil { - return ZeroTime, 1, nil - } - return ZeroTime, prev.Value + 1, nil -} - -// newMinIterator returns an iterator for operating on a min() call. -func newMinIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatFuncReducer(FloatMinReduce, nil) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerFuncReducer(IntegerMinReduce, nil) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - case BooleanIterator: - createFn := func() (BooleanPointAggregator, BooleanPointEmitter) { - fn := NewBooleanFuncReducer(BooleanMinReduce, nil) - return fn, fn - } - return &booleanReduceBooleanIterator{input: newBufBooleanIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported min iterator type: %T", input) - } -} - -// FloatMinReduce returns the minimum value between prev & curr. -func FloatMinReduce(prev, curr *FloatPoint) (int64, float64, []interface{}) { - if prev == nil || curr.Value < prev.Value || (curr.Value == prev.Value && curr.Time < prev.Time) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// IntegerMinReduce returns the minimum value between prev & curr. -func IntegerMinReduce(prev, curr *IntegerPoint) (int64, int64, []interface{}) { - if prev == nil || curr.Value < prev.Value || (curr.Value == prev.Value && curr.Time < prev.Time) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// BooleanMinReduce returns the minimum value between prev & curr. -func BooleanMinReduce(prev, curr *BooleanPoint) (int64, bool, []interface{}) { - if prev == nil || (curr.Value != prev.Value && !curr.Value) || (curr.Value == prev.Value && curr.Time < prev.Time) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// newMaxIterator returns an iterator for operating on a max() call. -func newMaxIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatFuncReducer(FloatMaxReduce, nil) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerFuncReducer(IntegerMaxReduce, nil) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - case BooleanIterator: - createFn := func() (BooleanPointAggregator, BooleanPointEmitter) { - fn := NewBooleanFuncReducer(BooleanMaxReduce, nil) - return fn, fn - } - return &booleanReduceBooleanIterator{input: newBufBooleanIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported max iterator type: %T", input) - } -} - -// FloatMaxReduce returns the maximum value between prev & curr. -func FloatMaxReduce(prev, curr *FloatPoint) (int64, float64, []interface{}) { - if prev == nil || curr.Value > prev.Value || (curr.Value == prev.Value && curr.Time < prev.Time) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// IntegerMaxReduce returns the maximum value between prev & curr. -func IntegerMaxReduce(prev, curr *IntegerPoint) (int64, int64, []interface{}) { - if prev == nil || curr.Value > prev.Value || (curr.Value == prev.Value && curr.Time < prev.Time) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// BooleanMaxReduce returns the minimum value between prev & curr. -func BooleanMaxReduce(prev, curr *BooleanPoint) (int64, bool, []interface{}) { - if prev == nil || (curr.Value != prev.Value && curr.Value) || (curr.Value == prev.Value && curr.Time < prev.Time) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// newSumIterator returns an iterator for operating on a sum() call. -func newSumIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatFuncReducer(FloatSumReduce, &FloatPoint{Value: 0, Time: ZeroTime}) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerFuncReducer(IntegerSumReduce, &IntegerPoint{Value: 0, Time: ZeroTime}) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported sum iterator type: %T", input) - } -} - -// FloatSumReduce returns the sum prev value & curr value. -func FloatSumReduce(prev, curr *FloatPoint) (int64, float64, []interface{}) { - if prev == nil { - return ZeroTime, curr.Value, nil - } - return prev.Time, prev.Value + curr.Value, nil -} - -// IntegerSumReduce returns the sum prev value & curr value. -func IntegerSumReduce(prev, curr *IntegerPoint) (int64, int64, []interface{}) { - if prev == nil { - return ZeroTime, curr.Value, nil - } - return prev.Time, prev.Value + curr.Value, nil -} - -// newFirstIterator returns an iterator for operating on a first() call. -func newFirstIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatFuncReducer(FloatFirstReduce, nil) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerFuncReducer(IntegerFirstReduce, nil) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - case StringIterator: - createFn := func() (StringPointAggregator, StringPointEmitter) { - fn := NewStringFuncReducer(StringFirstReduce, nil) - return fn, fn - } - return &stringReduceStringIterator{input: newBufStringIterator(input), opt: opt, create: createFn}, nil - case BooleanIterator: - createFn := func() (BooleanPointAggregator, BooleanPointEmitter) { - fn := NewBooleanFuncReducer(BooleanFirstReduce, nil) - return fn, fn - } - return &booleanReduceBooleanIterator{input: newBufBooleanIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported first iterator type: %T", input) - } -} - -// FloatFirstReduce returns the first point sorted by time. -func FloatFirstReduce(prev, curr *FloatPoint) (int64, float64, []interface{}) { - if prev == nil || curr.Time < prev.Time || (curr.Time == prev.Time && curr.Value > prev.Value) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// IntegerFirstReduce returns the first point sorted by time. -func IntegerFirstReduce(prev, curr *IntegerPoint) (int64, int64, []interface{}) { - if prev == nil || curr.Time < prev.Time || (curr.Time == prev.Time && curr.Value > prev.Value) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// StringFirstReduce returns the first point sorted by time. -func StringFirstReduce(prev, curr *StringPoint) (int64, string, []interface{}) { - if prev == nil || curr.Time < prev.Time || (curr.Time == prev.Time && curr.Value > prev.Value) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// BooleanFirstReduce returns the first point sorted by time. -func BooleanFirstReduce(prev, curr *BooleanPoint) (int64, bool, []interface{}) { - if prev == nil || curr.Time < prev.Time || (curr.Time == prev.Time && !curr.Value && prev.Value) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// newLastIterator returns an iterator for operating on a last() call. -func newLastIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatFuncReducer(FloatLastReduce, nil) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerFuncReducer(IntegerLastReduce, nil) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - case StringIterator: - createFn := func() (StringPointAggregator, StringPointEmitter) { - fn := NewStringFuncReducer(StringLastReduce, nil) - return fn, fn - } - return &stringReduceStringIterator{input: newBufStringIterator(input), opt: opt, create: createFn}, nil - case BooleanIterator: - createFn := func() (BooleanPointAggregator, BooleanPointEmitter) { - fn := NewBooleanFuncReducer(BooleanLastReduce, nil) - return fn, fn - } - return &booleanReduceBooleanIterator{input: newBufBooleanIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported last iterator type: %T", input) - } -} - -// FloatLastReduce returns the last point sorted by time. -func FloatLastReduce(prev, curr *FloatPoint) (int64, float64, []interface{}) { - if prev == nil || curr.Time > prev.Time || (curr.Time == prev.Time && curr.Value > prev.Value) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// IntegerLastReduce returns the last point sorted by time. -func IntegerLastReduce(prev, curr *IntegerPoint) (int64, int64, []interface{}) { - if prev == nil || curr.Time > prev.Time || (curr.Time == prev.Time && curr.Value > prev.Value) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// StringLastReduce returns the first point sorted by time. -func StringLastReduce(prev, curr *StringPoint) (int64, string, []interface{}) { - if prev == nil || curr.Time > prev.Time || (curr.Time == prev.Time && curr.Value > prev.Value) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// BooleanLastReduce returns the first point sorted by time. -func BooleanLastReduce(prev, curr *BooleanPoint) (int64, bool, []interface{}) { - if prev == nil || curr.Time > prev.Time || (curr.Time == prev.Time && curr.Value && !prev.Value) { - return curr.Time, curr.Value, curr.Aux - } - return prev.Time, prev.Value, prev.Aux -} - -// NewDistinctIterator returns an iterator for operating on a distinct() call. -func NewDistinctIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatDistinctReducer() - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerDistinctReducer() - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - case StringIterator: - createFn := func() (StringPointAggregator, StringPointEmitter) { - fn := NewStringDistinctReducer() - return fn, fn - } - return &stringReduceStringIterator{input: newBufStringIterator(input), opt: opt, create: createFn}, nil - case BooleanIterator: - createFn := func() (BooleanPointAggregator, BooleanPointEmitter) { - fn := NewBooleanDistinctReducer() - return fn, fn - } - return &booleanReduceBooleanIterator{input: newBufBooleanIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported distinct iterator type: %T", input) - } -} - -// newMeanIterator returns an iterator for operating on a mean() call. -func newMeanIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatMeanReducer() - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, FloatPointEmitter) { - fn := NewIntegerMeanReducer() - return fn, fn - } - return &integerReduceFloatIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported mean iterator type: %T", input) - } -} - -// NewMedianIterator returns an iterator for operating on a median() call. -func NewMedianIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - return newMedianIterator(input, opt) -} - -// newMedianIterator returns an iterator for operating on a median() call. -func newMedianIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatSliceFuncReducer(FloatMedianReduceSlice) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, FloatPointEmitter) { - fn := NewIntegerSliceFuncFloatReducer(IntegerMedianReduceSlice) - return fn, fn - } - return &integerReduceFloatIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported median iterator type: %T", input) - } -} - -// FloatMedianReduceSlice returns the median value within a window. -func FloatMedianReduceSlice(a []FloatPoint) []FloatPoint { - if len(a) == 1 { - return a - } - - // OPTIMIZE(benbjohnson): Use getSortedRange() from v0.9.5.1. - - // Return the middle value from the points. - // If there are an even number of points then return the mean of the two middle points. - sort.Sort(floatPointsByValue(a)) - if len(a)%2 == 0 { - lo, hi := a[len(a)/2-1], a[(len(a)/2)] - return []FloatPoint{{Time: ZeroTime, Value: lo.Value + (hi.Value-lo.Value)/2}} - } - return []FloatPoint{{Time: ZeroTime, Value: a[len(a)/2].Value}} -} - -// IntegerMedianReduceSlice returns the median value within a window. -func IntegerMedianReduceSlice(a []IntegerPoint) []FloatPoint { - if len(a) == 1 { - return []FloatPoint{{Time: ZeroTime, Value: float64(a[0].Value)}} - } - - // OPTIMIZE(benbjohnson): Use getSortedRange() from v0.9.5.1. - - // Return the middle value from the points. - // If there are an even number of points then return the mean of the two middle points. - sort.Sort(integerPointsByValue(a)) - if len(a)%2 == 0 { - lo, hi := a[len(a)/2-1], a[(len(a)/2)] - return []FloatPoint{{Time: ZeroTime, Value: float64(lo.Value) + float64(hi.Value-lo.Value)/2}} - } - return []FloatPoint{{Time: ZeroTime, Value: float64(a[len(a)/2].Value)}} -} - -// newModeIterator returns an iterator for operating on a mode() call. -func NewModeIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatSliceFuncReducer(FloatModeReduceSlice) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerSliceFuncReducer(IntegerModeReduceSlice) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - case StringIterator: - createFn := func() (StringPointAggregator, StringPointEmitter) { - fn := NewStringSliceFuncReducer(StringModeReduceSlice) - return fn, fn - } - return &stringReduceStringIterator{input: newBufStringIterator(input), opt: opt, create: createFn}, nil - case BooleanIterator: - createFn := func() (BooleanPointAggregator, BooleanPointEmitter) { - fn := NewBooleanSliceFuncReducer(BooleanModeReduceSlice) - return fn, fn - } - return &booleanReduceBooleanIterator{input: newBufBooleanIterator(input), opt: opt, create: createFn}, nil - - default: - return nil, fmt.Errorf("unsupported median iterator type: %T", input) - } -} - -// FloatModeReduceSlice returns the mode value within a window. -func FloatModeReduceSlice(a []FloatPoint) []FloatPoint { - if len(a) == 1 { - return a - } - - // fmt.Println(a[0]) - sort.Sort(floatPointsByValue(a)) - - mostFreq := 0 - currFreq := 0 - currMode := a[0].Value - mostMode := a[0].Value - mostTime := a[0].Time - currTime := a[0].Time - - for _, p := range a { - if p.Value != currMode { - currFreq = 1 - currMode = p.Value - currTime = p.Time - continue - } - currFreq++ - if mostFreq > currFreq || (mostFreq == currFreq && currTime > mostTime) { - continue - } - mostFreq = currFreq - mostMode = p.Value - mostTime = p.Time - } - - return []FloatPoint{{Time: ZeroTime, Value: mostMode}} -} - -// IntegerModeReduceSlice returns the mode value within a window. -func IntegerModeReduceSlice(a []IntegerPoint) []IntegerPoint { - if len(a) == 1 { - return a - } - sort.Sort(integerPointsByValue(a)) - - mostFreq := 0 - currFreq := 0 - currMode := a[0].Value - mostMode := a[0].Value - mostTime := a[0].Time - currTime := a[0].Time - - for _, p := range a { - if p.Value != currMode { - currFreq = 1 - currMode = p.Value - currTime = p.Time - continue - } - currFreq++ - if mostFreq > currFreq || (mostFreq == currFreq && currTime > mostTime) { - continue - } - mostFreq = currFreq - mostMode = p.Value - mostTime = p.Time - } - - return []IntegerPoint{{Time: ZeroTime, Value: mostMode}} -} - -// StringModeReduceSlice returns the mode value within a window. -func StringModeReduceSlice(a []StringPoint) []StringPoint { - if len(a) == 1 { - return a - } - - sort.Sort(stringPointsByValue(a)) - - mostFreq := 0 - currFreq := 0 - currMode := a[0].Value - mostMode := a[0].Value - mostTime := a[0].Time - currTime := a[0].Time - - for _, p := range a { - if p.Value != currMode { - currFreq = 1 - currMode = p.Value - currTime = p.Time - continue - } - currFreq++ - if mostFreq > currFreq || (mostFreq == currFreq && currTime > mostTime) { - continue - } - mostFreq = currFreq - mostMode = p.Value - mostTime = p.Time - } - - return []StringPoint{{Time: ZeroTime, Value: mostMode}} -} - -// BooleanModeReduceSlice returns the mode value within a window. -func BooleanModeReduceSlice(a []BooleanPoint) []BooleanPoint { - if len(a) == 1 { - return a - } - - trueFreq := 0 - falsFreq := 0 - mostMode := false - - for _, p := range a { - if p.Value { - trueFreq++ - } else { - falsFreq++ - } - } - // In case either of true or false are mode then retuned mode value wont be - // of metric with oldest timestamp - if trueFreq >= falsFreq { - mostMode = true - } - - return []BooleanPoint{{Time: ZeroTime, Value: mostMode}} -} - -// newStddevIterator returns an iterator for operating on a stddev() call. -func newStddevIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatSliceFuncReducer(FloatStddevReduceSlice) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, FloatPointEmitter) { - fn := NewIntegerSliceFuncFloatReducer(IntegerStddevReduceSlice) - return fn, fn - } - return &integerReduceFloatIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported stddev iterator type: %T", input) - } -} - -// FloatStddevReduceSlice returns the stddev value within a window. -func FloatStddevReduceSlice(a []FloatPoint) []FloatPoint { - // If there is only one point then return 0. - if len(a) < 2 { - return []FloatPoint{{Time: ZeroTime, Nil: true}} - } - - // Calculate the mean. - var mean float64 - var count int - for _, p := range a { - if math.IsNaN(p.Value) { - continue - } - count++ - mean += (p.Value - mean) / float64(count) - } - - // Calculate the variance. - var variance float64 - for _, p := range a { - if math.IsNaN(p.Value) { - continue - } - variance += math.Pow(p.Value-mean, 2) - } - return []FloatPoint{{ - Time: ZeroTime, - Value: math.Sqrt(variance / float64(count-1)), - }} -} - -// IntegerStddevReduceSlice returns the stddev value within a window. -func IntegerStddevReduceSlice(a []IntegerPoint) []FloatPoint { - // If there is only one point then return 0. - if len(a) < 2 { - return []FloatPoint{{Time: ZeroTime, Nil: true}} - } - - // Calculate the mean. - var mean float64 - var count int - for _, p := range a { - count++ - mean += (float64(p.Value) - mean) / float64(count) - } - - // Calculate the variance. - var variance float64 - for _, p := range a { - variance += math.Pow(float64(p.Value)-mean, 2) - } - return []FloatPoint{{ - Time: ZeroTime, - Value: math.Sqrt(variance / float64(count-1)), - }} -} - -// newSpreadIterator returns an iterator for operating on a spread() call. -func newSpreadIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatSliceFuncReducer(FloatSpreadReduceSlice) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerSliceFuncReducer(IntegerSpreadReduceSlice) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported spread iterator type: %T", input) - } -} - -// FloatSpreadReduceSlice returns the spread value within a window. -func FloatSpreadReduceSlice(a []FloatPoint) []FloatPoint { - // Find min & max values. - min, max := a[0].Value, a[0].Value - for _, p := range a[1:] { - min = math.Min(min, p.Value) - max = math.Max(max, p.Value) - } - return []FloatPoint{{Time: ZeroTime, Value: max - min}} -} - -// IntegerSpreadReduceSlice returns the spread value within a window. -func IntegerSpreadReduceSlice(a []IntegerPoint) []IntegerPoint { - // Find min & max values. - min, max := a[0].Value, a[0].Value - for _, p := range a[1:] { - if p.Value < min { - min = p.Value - } - if p.Value > max { - max = p.Value - } - } - return []IntegerPoint{{Time: ZeroTime, Value: max - min}} -} - -func newTopIterator(input Iterator, opt IteratorOptions, n *IntegerLiteral, tags []int) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - aggregateFn := NewFloatTopReduceSliceFunc(int(n.Val), tags, opt.Interval) - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatSliceFuncReducer(aggregateFn) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - aggregateFn := NewIntegerTopReduceSliceFunc(int(n.Val), tags, opt.Interval) - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerSliceFuncReducer(aggregateFn) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported top iterator type: %T", input) - } -} - -// NewFloatTopReduceSliceFunc returns the top values within a window. -func NewFloatTopReduceSliceFunc(n int, tags []int, interval Interval) FloatReduceSliceFunc { - return func(a []FloatPoint) []FloatPoint { - // Filter by tags if they exist. - if len(tags) > 0 { - a = filterFloatByUniqueTags(a, tags, func(cur, p *FloatPoint) bool { - return p.Value > cur.Value || (p.Value == cur.Value && p.Time < cur.Time) - }) - } - - // If we ask for more elements than exist, restrict n to be the length of the array. - size := n - if size > len(a) { - size = len(a) - } - - // Construct a heap preferring higher values and breaking ties - // based on the earliest time for a point. - h := floatPointsSortBy(a, func(a, b *FloatPoint) bool { - if a.Value != b.Value { - return a.Value > b.Value - } - return a.Time < b.Time - }) - heap.Init(h) - - // Pop the first n elements and then sort by time. - points := make([]FloatPoint, 0, size) - for i := 0; i < size; i++ { - p := heap.Pop(h).(FloatPoint) - points = append(points, p) - } - - // Either zero out all values or sort the points by time - // depending on if a time interval was given or not. - if !interval.IsZero() { - for i := range points { - points[i].Time = ZeroTime - } - } else { - sort.Stable(floatPointsByTime(points)) - } - return points - } -} - -// NewIntegerTopReduceSliceFunc returns the top values within a window. -func NewIntegerTopReduceSliceFunc(n int, tags []int, interval Interval) IntegerReduceSliceFunc { - return func(a []IntegerPoint) []IntegerPoint { - // Filter by tags if they exist. - if len(tags) > 0 { - a = filterIntegerByUniqueTags(a, tags, func(cur, p *IntegerPoint) bool { - return p.Value > cur.Value || (p.Value == cur.Value && p.Time < cur.Time) - }) - } - - // If we ask for more elements than exist, restrict n to be the length of the array. - size := n - if size > len(a) { - size = len(a) - } - - // Construct a heap preferring higher values and breaking ties - // based on the earliest time for a point. - h := integerPointsSortBy(a, func(a, b *IntegerPoint) bool { - if a.Value != b.Value { - return a.Value > b.Value - } - return a.Time < b.Time - }) - heap.Init(h) - - // Pop the first n elements and then sort by time. - points := make([]IntegerPoint, 0, size) - for i := 0; i < size; i++ { - p := heap.Pop(h).(IntegerPoint) - points = append(points, p) - } - - // Either zero out all values or sort the points by time - // depending on if a time interval was given or not. - if !interval.IsZero() { - for i := range points { - points[i].Time = ZeroTime - } - } else { - sort.Stable(integerPointsByTime(points)) - } - return points - } -} - -func newBottomIterator(input Iterator, opt IteratorOptions, n *IntegerLiteral, tags []int) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - aggregateFn := NewFloatBottomReduceSliceFunc(int(n.Val), tags, opt.Interval) - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatSliceFuncReducer(aggregateFn) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - aggregateFn := NewIntegerBottomReduceSliceFunc(int(n.Val), tags, opt.Interval) - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerSliceFuncReducer(aggregateFn) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported bottom iterator type: %T", input) - } -} - -// NewFloatBottomReduceSliceFunc returns the bottom values within a window. -func NewFloatBottomReduceSliceFunc(n int, tags []int, interval Interval) FloatReduceSliceFunc { - return func(a []FloatPoint) []FloatPoint { - // Filter by tags if they exist. - if len(tags) > 0 { - a = filterFloatByUniqueTags(a, tags, func(cur, p *FloatPoint) bool { - return p.Value < cur.Value || (p.Value == cur.Value && p.Time < cur.Time) - }) - } - - // If we ask for more elements than exist, restrict n to be the length of the array. - size := n - if size > len(a) { - size = len(a) - } - - // Construct a heap preferring lower values and breaking ties - // based on the earliest time for a point. - h := floatPointsSortBy(a, func(a, b *FloatPoint) bool { - if a.Value != b.Value { - return a.Value < b.Value - } - return a.Time < b.Time - }) - heap.Init(h) - - // Pop the first n elements and then sort by time. - points := make([]FloatPoint, 0, size) - for i := 0; i < size; i++ { - p := heap.Pop(h).(FloatPoint) - points = append(points, p) - } - - // Either zero out all values or sort the points by time - // depending on if a time interval was given or not. - if !interval.IsZero() { - for i := range points { - points[i].Time = ZeroTime - } - } else { - sort.Stable(floatPointsByTime(points)) - } - return points - } -} - -// NewIntegerBottomReduceSliceFunc returns the bottom values within a window. -func NewIntegerBottomReduceSliceFunc(n int, tags []int, interval Interval) IntegerReduceSliceFunc { - return func(a []IntegerPoint) []IntegerPoint { - // Filter by tags if they exist. - if len(tags) > 0 { - a = filterIntegerByUniqueTags(a, tags, func(cur, p *IntegerPoint) bool { - return p.Value < cur.Value || (p.Value == cur.Value && p.Time < cur.Time) - }) - } - - // If we ask for more elements than exist, restrict n to be the length of the array. - size := n - if size > len(a) { - size = len(a) - } - - // Construct a heap preferring lower values and breaking ties - // based on the earliest time for a point. - h := integerPointsSortBy(a, func(a, b *IntegerPoint) bool { - if a.Value != b.Value { - return a.Value < b.Value - } - return a.Time < b.Time - }) - heap.Init(h) - - // Pop the first n elements and then sort by time. - points := make([]IntegerPoint, 0, size) - for i := 0; i < size; i++ { - p := heap.Pop(h).(IntegerPoint) - points = append(points, p) - } - - // Either zero out all values or sort the points by time - // depending on if a time interval was given or not. - if !interval.IsZero() { - for i := range points { - points[i].Time = ZeroTime - } - } else { - sort.Stable(integerPointsByTime(points)) - } - return points - } -} - -func filterFloatByUniqueTags(a []FloatPoint, tags []int, cmpFunc func(cur, p *FloatPoint) bool) []FloatPoint { - pointMap := make(map[string]FloatPoint) - for _, p := range a { - keyBuf := bytes.NewBuffer(nil) - for i, index := range tags { - if i > 0 { - keyBuf.WriteString(",") - } - fmt.Fprintf(keyBuf, "%s", p.Aux[index]) - } - key := keyBuf.String() - - cur, ok := pointMap[key] - if ok { - if cmpFunc(&cur, &p) { - pointMap[key] = p - } - } else { - pointMap[key] = p - } - } - - // Recreate the original array with our new filtered list. - points := make([]FloatPoint, 0, len(pointMap)) - for _, p := range pointMap { - points = append(points, p) - } - return points -} - -func filterIntegerByUniqueTags(a []IntegerPoint, tags []int, cmpFunc func(cur, p *IntegerPoint) bool) []IntegerPoint { - pointMap := make(map[string]IntegerPoint) - for _, p := range a { - keyBuf := bytes.NewBuffer(nil) - for i, index := range tags { - if i > 0 { - keyBuf.WriteString(",") - } - fmt.Fprintf(keyBuf, "%s", p.Aux[index]) - } - key := keyBuf.String() - - cur, ok := pointMap[key] - if ok { - if cmpFunc(&cur, &p) { - pointMap[key] = p - } - } else { - pointMap[key] = p - } - } - - // Recreate the original array with our new filtered list. - points := make([]IntegerPoint, 0, len(pointMap)) - for _, p := range pointMap { - points = append(points, p) - } - return points -} - -// newPercentileIterator returns an iterator for operating on a percentile() call. -func newPercentileIterator(input Iterator, opt IteratorOptions, percentile float64) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - floatPercentileReduceSlice := NewFloatPercentileReduceSliceFunc(percentile) - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatSliceFuncReducer(floatPercentileReduceSlice) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - integerPercentileReduceSlice := NewIntegerPercentileReduceSliceFunc(percentile) - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerSliceFuncReducer(integerPercentileReduceSlice) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported percentile iterator type: %T", input) - } -} - -// NewFloatPercentileReduceSliceFunc returns the percentile value within a window. -func NewFloatPercentileReduceSliceFunc(percentile float64) FloatReduceSliceFunc { - return func(a []FloatPoint) []FloatPoint { - length := len(a) - i := int(math.Floor(float64(length)*percentile/100.0+0.5)) - 1 - - if i < 0 || i >= length { - return nil - } - - sort.Sort(floatPointsByValue(a)) - return []FloatPoint{{Time: a[i].Time, Value: a[i].Value, Aux: a[i].Aux}} - } -} - -// NewIntegerPercentileReduceSliceFunc returns the percentile value within a window. -func NewIntegerPercentileReduceSliceFunc(percentile float64) IntegerReduceSliceFunc { - return func(a []IntegerPoint) []IntegerPoint { - length := len(a) - i := int(math.Floor(float64(length)*percentile/100.0+0.5)) - 1 - - if i < 0 || i >= length { - return nil - } - - sort.Sort(integerPointsByValue(a)) - return []IntegerPoint{{Time: a[i].Time, Value: a[i].Value, Aux: a[i].Aux}} - } -} - -// newDerivativeIterator returns an iterator for operating on a derivative() call. -func newDerivativeIterator(input Iterator, opt IteratorOptions, interval Interval, isNonNegative bool) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatDerivativeReducer(interval, isNonNegative, opt.Ascending) - return fn, fn - } - return newFloatStreamFloatIterator(input, createFn, opt), nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, FloatPointEmitter) { - fn := NewIntegerDerivativeReducer(interval, isNonNegative, opt.Ascending) - return fn, fn - } - return newIntegerStreamFloatIterator(input, createFn, opt), nil - default: - return nil, fmt.Errorf("unsupported derivative iterator type: %T", input) - } -} - -// newDifferenceIterator returns an iterator for operating on a difference() call. -func newDifferenceIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatDifferenceReducer() - return fn, fn - } - return newFloatStreamFloatIterator(input, createFn, opt), nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerDifferenceReducer() - return fn, fn - } - return newIntegerStreamIntegerIterator(input, createFn, opt), nil - default: - return nil, fmt.Errorf("unsupported difference iterator type: %T", input) - } -} - -// newElapsedIterator returns an iterator for operating on a elapsed() call. -func newElapsedIterator(input Iterator, opt IteratorOptions, interval Interval) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, IntegerPointEmitter) { - fn := NewFloatElapsedReducer(interval) - return fn, fn - } - return newFloatStreamIntegerIterator(input, createFn, opt), nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerElapsedReducer(interval) - return fn, fn - } - return newIntegerStreamIntegerIterator(input, createFn, opt), nil - case BooleanIterator: - createFn := func() (BooleanPointAggregator, IntegerPointEmitter) { - fn := NewBooleanElapsedReducer(interval) - return fn, fn - } - return newBooleanStreamIntegerIterator(input, createFn, opt), nil - case StringIterator: - createFn := func() (StringPointAggregator, IntegerPointEmitter) { - fn := NewStringElapsedReducer(interval) - return fn, fn - } - return newStringStreamIntegerIterator(input, createFn, opt), nil - default: - return nil, fmt.Errorf("unsupported elapsed iterator type: %T", input) - } -} - -// newMovingAverageIterator returns an iterator for operating on a moving_average() call. -func newMovingAverageIterator(input Iterator, n int, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatMovingAverageReducer(n) - return fn, fn - } - return newFloatStreamFloatIterator(input, createFn, opt), nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, FloatPointEmitter) { - fn := NewIntegerMovingAverageReducer(n) - return fn, fn - } - return newIntegerStreamFloatIterator(input, createFn, opt), nil - default: - return nil, fmt.Errorf("unsupported moving average iterator type: %T", input) - } -} - -// newCumulativeSumIterator returns an iterator for operating on a cumulative_sum() call. -func newCumulativeSumIterator(input Iterator, opt IteratorOptions) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatCumulativeSumReducer() - return fn, fn - } - return newFloatStreamFloatIterator(input, createFn, opt), nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerCumulativeSumReducer() - return fn, fn - } - return newIntegerStreamIntegerIterator(input, createFn, opt), nil - default: - return nil, fmt.Errorf("unsupported cumulative sum iterator type: %T", input) - } -} - -// newHoltWintersIterator returns an iterator for operating on a elapsed() call. -func newHoltWintersIterator(input Iterator, opt IteratorOptions, h, m int, includeFitData bool, interval time.Duration) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatHoltWintersReducer(h, m, includeFitData, interval) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, FloatPointEmitter) { - fn := NewFloatHoltWintersReducer(h, m, includeFitData, interval) - return fn, fn - } - return &integerReduceFloatIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported elapsed iterator type: %T", input) - } -} - -// NewSampleIterator returns an iterator -func NewSampleIterator(input Iterator, opt IteratorOptions, size int) (Iterator, error) { - return newSampleIterator(input, opt, size) -} - -// newSampleIterator returns an iterator -func newSampleIterator(input Iterator, opt IteratorOptions, size int) (Iterator, error) { - switch input := input.(type) { - case FloatIterator: - createFn := func() (FloatPointAggregator, FloatPointEmitter) { - fn := NewFloatSampleReducer(size) - return fn, fn - } - return &floatReduceFloatIterator{input: newBufFloatIterator(input), opt: opt, create: createFn}, nil - case IntegerIterator: - createFn := func() (IntegerPointAggregator, IntegerPointEmitter) { - fn := NewIntegerSampleReducer(size) - return fn, fn - } - return &integerReduceIntegerIterator{input: newBufIntegerIterator(input), opt: opt, create: createFn}, nil - case StringIterator: - createFn := func() (StringPointAggregator, StringPointEmitter) { - fn := NewStringSampleReducer(size) - return fn, fn - } - return &stringReduceStringIterator{input: newBufStringIterator(input), opt: opt, create: createFn}, nil - case BooleanIterator: - createFn := func() (BooleanPointAggregator, BooleanPointEmitter) { - fn := NewBooleanSampleReducer(size) - return fn, fn - } - return &booleanReduceBooleanIterator{input: newBufBooleanIterator(input), opt: opt, create: createFn}, nil - default: - return nil, fmt.Errorf("unsupported elapsed iterator type: %T", input) - } -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/call_iterator_test.go b/vendor/github.com/influxdata/influxdb/influxql/call_iterator_test.go deleted file mode 100644 index 30b34e5c..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/call_iterator_test.go +++ /dev/null @@ -1,944 +0,0 @@ -package influxql_test - -import ( - "testing" - "time" - - "github.com/davecgh/go-spew/spew" - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/pkg/deep" -) - -// Ensure that a float iterator can be created for a count() call. -func TestCallIterator_Count_Float(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &FloatIterator{Points: []influxql.FloatPoint{ - {Name: "cpu", Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Name: "cpu", Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Name: "cpu", Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Name: "cpu", Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Name: "cpu", Time: 5, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Name: "cpu", Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - {Name: "mem", Time: 23, Value: 10, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`count("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Name: "cpu", Time: 0, Value: 3, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 0, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 5, Value: 1, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 20, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "mem", Time: 20, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that an integer iterator can be created for a count() call. -func TestCallIterator_Count_Integer(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &IntegerIterator{Points: []influxql.IntegerPoint{ - {Name: "cpu", Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Name: "cpu", Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Name: "cpu", Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Name: "cpu", Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Name: "cpu", Time: 5, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Name: "cpu", Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - {Name: "mem", Time: 23, Value: 10, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`count("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Name: "cpu", Time: 0, Value: 3, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 0, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 5, Value: 1, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 20, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "mem", Time: 20, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a string iterator can be created for a count() call. -func TestCallIterator_Count_String(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &StringIterator{Points: []influxql.StringPoint{ - {Name: "cpu", Time: 0, Value: "d", Tags: ParseTags("region=us-east,host=hostA")}, - {Name: "cpu", Time: 1, Value: "c", Tags: ParseTags("region=us-west,host=hostB")}, - {Name: "cpu", Time: 2, Value: "b", Tags: ParseTags("region=us-east,host=hostA")}, - {Name: "cpu", Time: 1, Value: "b", Tags: ParseTags("region=us-west,host=hostA")}, - - {Name: "cpu", Time: 5, Value: "e", Tags: ParseTags("region=us-east,host=hostA")}, - - {Name: "cpu", Time: 23, Value: "a", Tags: ParseTags("region=us-west,host=hostB")}, - {Name: "mem", Time: 23, Value: "b", Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`count("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Name: "cpu", Time: 0, Value: 3, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 0, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 5, Value: 1, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 20, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "mem", Time: 20, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a boolean iterator can be created for a count() call. -func TestCallIterator_Count_Boolean(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &BooleanIterator{Points: []influxql.BooleanPoint{ - {Name: "cpu", Time: 0, Value: true, Tags: ParseTags("region=us-east,host=hostA")}, - {Name: "cpu", Time: 1, Value: true, Tags: ParseTags("region=us-west,host=hostB")}, - {Name: "cpu", Time: 2, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - {Name: "cpu", Time: 1, Value: true, Tags: ParseTags("region=us-west,host=hostA")}, - - {Name: "cpu", Time: 5, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - - {Name: "cpu", Time: 23, Value: false, Tags: ParseTags("region=us-west,host=hostB")}, - {Name: "mem", Time: 23, Value: true, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`count("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Name: "cpu", Time: 0, Value: 3, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 0, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 5, Value: 1, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Time: 20, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Name: "mem", Time: 20, Value: 1, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a float iterator can be created for a min() call. -func TestCallIterator_Min_Float(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &FloatIterator{Points: []influxql.FloatPoint{ - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 4, Value: 12, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 5, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`min("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Time: 1, Value: 10, Tags: ParseTags("host=hostA"), Aggregated: 4}}, - {&influxql.FloatPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 5, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 23, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a integer iterator can be created for a min() call. -func TestCallIterator_Min_Integer(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &IntegerIterator{Points: []influxql.IntegerPoint{ - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 4, Value: 12, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 5, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`min("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Time: 1, Value: 10, Tags: ParseTags("host=hostA"), Aggregated: 4}}, - {&influxql.IntegerPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 5, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 23, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a boolean iterator can be created for a min() call. -func TestCallIterator_Min_Boolean(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &BooleanIterator{Points: []influxql.BooleanPoint{ - {Time: 0, Value: true, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: false, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: true, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 5, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: true, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`min("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.BooleanPoint{Time: 2, Value: false, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.BooleanPoint{Time: 1, Value: false, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.BooleanPoint{Time: 5, Value: false, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.BooleanPoint{Time: 23, Value: true, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a float iterator can be created for a max() call. -func TestCallIterator_Max_Float(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &FloatIterator{Points: []influxql.FloatPoint{ - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 5, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`max("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Time: 0, Value: 15, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.FloatPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 5, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 23, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a integer iterator can be created for a max() call. -func TestCallIterator_Max_Integer(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &IntegerIterator{Points: []influxql.IntegerPoint{ - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 5, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`max("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Time: 0, Value: 15, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.IntegerPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 5, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 23, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a boolean iterator can be created for a max() call. -func TestCallIterator_Max_Boolean(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &BooleanIterator{Points: []influxql.BooleanPoint{ - {Time: 0, Value: true, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: false, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: true, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 5, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: true, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`max("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.BooleanPoint{Time: 0, Value: true, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.BooleanPoint{Time: 1, Value: false, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.BooleanPoint{Time: 5, Value: false, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.BooleanPoint{Time: 23, Value: true, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a float iterator can be created for a sum() call. -func TestCallIterator_Sum_Float(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &FloatIterator{Points: []influxql.FloatPoint{ - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 5, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`sum("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Time: 0, Value: 35, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.FloatPoint{Time: 0, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 5, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 20, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that an integer iterator can be created for a sum() call. -func TestCallIterator_Sum_Integer(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &IntegerIterator{Points: []influxql.IntegerPoint{ - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 5, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`sum("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Time: 0, Value: 35, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.IntegerPoint{Time: 0, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 5, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 20, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a float iterator can be created for a first() call. -func TestCallIterator_First_Float(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &FloatIterator{Points: []influxql.FloatPoint{ - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 6, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`first("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Time: 0, Value: 15, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.FloatPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 6, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 23, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that an integer iterator can be created for a first() call. -func TestCallIterator_First_Integer(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &IntegerIterator{Points: []influxql.IntegerPoint{ - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 6, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`first("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Time: 0, Value: 15, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.IntegerPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 6, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 23, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a string iterator can be created for a first() call. -func TestCallIterator_First_String(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &StringIterator{Points: []influxql.StringPoint{ - {Time: 1, Value: "c", Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: "b", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 0, Value: "d", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: "b", Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 6, Value: "e", Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: "a", Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`first("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.StringPoint{Time: 0, Value: "d", Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.StringPoint{Time: 1, Value: "c", Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.StringPoint{Time: 6, Value: "e", Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.StringPoint{Time: 23, Value: "a", Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a boolean iterator can be created for a first() call. -func TestCallIterator_First_Boolean(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &BooleanIterator{Points: []influxql.BooleanPoint{ - {Time: 1, Value: true, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 0, Value: true, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: false, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 6, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: false, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`first("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.BooleanPoint{Time: 0, Value: true, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.BooleanPoint{Time: 1, Value: true, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.BooleanPoint{Time: 6, Value: false, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.BooleanPoint{Time: 23, Value: false, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a float iterator can be created for a last() call. -func TestCallIterator_Last_Float(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &FloatIterator{Points: []influxql.FloatPoint{ - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 6, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`last("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Time: 2, Value: 10, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.FloatPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 6, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.FloatPoint{Time: 23, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that an integer iterator can be created for a last() call. -func TestCallIterator_Last_Integer(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &IntegerIterator{Points: []influxql.IntegerPoint{ - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 6, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`last("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Time: 2, Value: 10, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.IntegerPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 6, Value: 20, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.IntegerPoint{Time: 23, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a string iterator can be created for a last() call. -func TestCallIterator_Last_String(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &StringIterator{Points: []influxql.StringPoint{ - {Time: 1, Value: "c", Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: "b", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 0, Value: "d", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: "b", Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 6, Value: "e", Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: "a", Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`last("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.StringPoint{Time: 2, Value: "b", Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.StringPoint{Time: 1, Value: "c", Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.StringPoint{Time: 6, Value: "e", Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.StringPoint{Time: 23, Value: "a", Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a boolean iterator can be created for a last() call. -func TestCallIterator_Last_Boolean(t *testing.T) { - itr, _ := influxql.NewCallIterator( - &BooleanIterator{Points: []influxql.BooleanPoint{ - {Time: 1, Value: true, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 2, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 0, Value: true, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: false, Tags: ParseTags("region=us-west,host=hostA")}, - - {Time: 6, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - - {Time: 23, Value: false, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`last("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.BooleanPoint{Time: 2, Value: false, Tags: ParseTags("host=hostA"), Aggregated: 3}}, - {&influxql.BooleanPoint{Time: 1, Value: true, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - {&influxql.BooleanPoint{Time: 6, Value: false, Tags: ParseTags("host=hostA"), Aggregated: 1}}, - {&influxql.BooleanPoint{Time: 23, Value: false, Tags: ParseTags("host=hostB"), Aggregated: 1}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a float iterator can be created for a mode() call. -func TestCallIterator_Mode_Float(t *testing.T) { - itr, _ := influxql.NewModeIterator(&FloatIterator{Points: []influxql.FloatPoint{ - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 3, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 4, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 6, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 7, Value: 21, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 8, Value: 21, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 22, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 24, Value: 25, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`mode("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Time: 0, Value: 10, Tags: ParseTags("host=hostA"), Aggregated: 0}}, - {&influxql.FloatPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB"), Aggregated: 0}}, - {&influxql.FloatPoint{Time: 5, Value: 21, Tags: ParseTags("host=hostA"), Aggregated: 0}}, - {&influxql.FloatPoint{Time: 20, Value: 8, Tags: ParseTags("host=hostB"), Aggregated: 0}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a integer iterator can be created for a mode() call. -func TestCallIterator_Mode_Integer(t *testing.T) { - itr, _ := influxql.NewModeIterator(&IntegerIterator{Points: []influxql.IntegerPoint{ - {Time: 0, Value: 15, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: 11, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 1, Value: 10, Tags: ParseTags("region=us-west,host=hostA")}, - {Time: 2, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 3, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 4, Value: 10, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 6, Value: 20, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 7, Value: 21, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 8, Value: 21, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 22, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 23, Value: 8, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 24, Value: 25, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`mode("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Time: 0, Value: 10, Tags: ParseTags("host=hostA")}}, - {&influxql.IntegerPoint{Time: 1, Value: 11, Tags: ParseTags("host=hostB")}}, - {&influxql.IntegerPoint{Time: 5, Value: 21, Tags: ParseTags("host=hostA")}}, - {&influxql.IntegerPoint{Time: 20, Value: 8, Tags: ParseTags("host=hostB")}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a string iterator can be created for a mode() call. -func TestCallIterator_Mode_String(t *testing.T) { - itr, _ := influxql.NewModeIterator(&StringIterator{Points: []influxql.StringPoint{ - {Time: 0, Value: "15", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: "11", Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 1, Value: "10", Tags: ParseTags("region=us-west,host=hostA")}, - {Time: 2, Value: "10", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 3, Value: "10", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 4, Value: "10", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 6, Value: "20", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 7, Value: "21", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 7, Value: "21", Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 22, Value: "8", Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 23, Value: "8", Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 24, Value: "25", Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`mode("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.StringPoint{Time: 0, Value: "10", Tags: ParseTags("host=hostA")}}, - {&influxql.StringPoint{Time: 1, Value: "11", Tags: ParseTags("host=hostB")}}, - {&influxql.StringPoint{Time: 5, Value: "21", Tags: ParseTags("host=hostA")}}, - {&influxql.StringPoint{Time: 20, Value: "8", Tags: ParseTags("host=hostB")}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure that a boolean iterator can be created for a modBooleanl. -func TestCallIterator_Mode_Boolean(t *testing.T) { - itr, _ := influxql.NewModeIterator(&BooleanIterator{Points: []influxql.BooleanPoint{ - {Time: 0, Value: true, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 1, Value: false, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 1, Value: true, Tags: ParseTags("region=us-west,host=hostA")}, - {Time: 2, Value: true, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 3, Value: true, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 4, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 6, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 7, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 8, Value: false, Tags: ParseTags("region=us-east,host=hostA")}, - {Time: 22, Value: false, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 23, Value: true, Tags: ParseTags("region=us-west,host=hostB")}, - {Time: 24, Value: true, Tags: ParseTags("region=us-west,host=hostB")}, - }}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`mode("value")`), - Dimensions: []string{"host"}, - Interval: influxql.Interval{Duration: 5 * time.Nanosecond}, - }, - ) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.BooleanPoint{Time: 0, Value: true, Tags: ParseTags("host=hostA")}}, - {&influxql.BooleanPoint{Time: 1, Value: false, Tags: ParseTags("host=hostB")}}, - {&influxql.BooleanPoint{Time: 5, Value: false, Tags: ParseTags("host=hostA")}}, - {&influxql.BooleanPoint{Time: 20, Value: true, Tags: ParseTags("host=hostB")}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -func TestNewCallIterator_UnsupportedExprName(t *testing.T) { - _, err := influxql.NewCallIterator( - &FloatIterator{}, - influxql.IteratorOptions{ - Expr: MustParseExpr(`foobar("value")`), - }, - ) - - if err == nil || err.Error() != "unsupported function call: foobar" { - t.Errorf("unexpected error: %s", err) - } -} - -func BenchmarkCountIterator_1K(b *testing.B) { benchmarkCountIterator(b, 1000) } -func BenchmarkCountIterator_100K(b *testing.B) { benchmarkCountIterator(b, 100000) } -func BenchmarkCountIterator_1M(b *testing.B) { benchmarkCountIterator(b, 1000000) } - -func benchmarkCountIterator(b *testing.B, pointN int) { - benchmarkCallIterator(b, influxql.IteratorOptions{ - Expr: MustParseExpr("count(value)"), - StartTime: influxql.MinTime, - EndTime: influxql.MaxTime, - }, pointN) -} - -func benchmarkCallIterator(b *testing.B, opt influxql.IteratorOptions, pointN int) { - b.ReportAllocs() - - for i := 0; i < b.N; i++ { - // Create a lightweight point generator. - p := influxql.FloatPoint{Name: "cpu", Value: 100} - input := FloatPointGenerator{ - N: pointN, - Fn: func(i int) *influxql.FloatPoint { return &p }, - } - - // Execute call against input. - itr, err := influxql.NewCallIterator(&input, opt) - if err != nil { - b.Fatal(err) - } - influxql.DrainIterator(itr) - } -} - -func BenchmarkSampleIterator_1k(b *testing.B) { benchmarkSampleIterator(b, 1000) } -func BenchmarkSampleIterator_100k(b *testing.B) { benchmarkSampleIterator(b, 100000) } -func BenchmarkSampleIterator_1M(b *testing.B) { benchmarkSampleIterator(b, 1000000) } - -func benchmarkSampleIterator(b *testing.B, pointN int) { - b.ReportAllocs() - - // Create a lightweight point generator. - p := influxql.FloatPoint{Name: "cpu"} - input := FloatPointGenerator{ - N: pointN, - Fn: func(i int) *influxql.FloatPoint { - p.Value = float64(i) - return &p - }, - } - - for i := 0; i < b.N; i++ { - // Execute call against input. - itr, err := influxql.NewSampleIterator(&input, influxql.IteratorOptions{}, 100) - if err != nil { - b.Fatal(err) - } - influxql.DrainIterator(itr) - } -} - -func BenchmarkDistinctIterator_1K(b *testing.B) { benchmarkDistinctIterator(b, 1000) } -func BenchmarkDistinctIterator_100K(b *testing.B) { benchmarkDistinctIterator(b, 100000) } -func BenchmarkDistinctIterator_1M(b *testing.B) { benchmarkDistinctIterator(b, 1000000) } - -func benchmarkDistinctIterator(b *testing.B, pointN int) { - b.ReportAllocs() - - for i := 0; i < b.N; i++ { - // Create a lightweight point generator. - p := influxql.FloatPoint{Name: "cpu"} - input := FloatPointGenerator{ - N: pointN, - Fn: func(i int) *influxql.FloatPoint { - p.Value = float64(i % 10) - return &p - }, - } - - // Execute call against input. - itr, err := influxql.NewDistinctIterator(&input, influxql.IteratorOptions{}) - if err != nil { - b.Fatal(err) - } - influxql.DrainIterator(itr) - } -} - -func BenchmarkModeIterator_1K(b *testing.B) { benchmarkModeIterator(b, 1000) } -func BenchmarkModeIterator_100K(b *testing.B) { benchmarkModeIterator(b, 100000) } -func BenchmarkModeIterator_1M(b *testing.B) { benchmarkModeIterator(b, 1000000) } - -func benchmarkModeIterator(b *testing.B, pointN int) { - b.ReportAllocs() - - for i := 0; i < b.N; i++ { - // Create a lightweight point generator. - p := influxql.FloatPoint{Name: "cpu"} - input := FloatPointGenerator{ - N: pointN, - Fn: func(i int) *influxql.FloatPoint { - p.Value = float64(10) - return &p - }, - } - - // Execute call against input. - itr, err := influxql.NewModeIterator(&input, influxql.IteratorOptions{}) - if err != nil { - b.Fatal(err) - } - influxql.DrainIterator(itr) - } -} - -type FloatPointGenerator struct { - i int - N int - Fn func(i int) *influxql.FloatPoint -} - -func (g *FloatPointGenerator) Close() error { return nil } -func (g *FloatPointGenerator) Stats() influxql.IteratorStats { return influxql.IteratorStats{} } - -func (g *FloatPointGenerator) Next() (*influxql.FloatPoint, error) { - if g.i == g.N { - return nil, nil - } - p := g.Fn(g.i) - g.i++ - return p, nil -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/cast.go b/vendor/github.com/influxdata/influxdb/influxql/cast.go deleted file mode 100644 index b993a17d..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/cast.go +++ /dev/null @@ -1,41 +0,0 @@ -package influxql - -func castToFloat(v interface{}) float64 { - switch v := v.(type) { - case float64: - return v - case int64: - return float64(v) - default: - return float64(0) - } -} - -func castToInteger(v interface{}) int64 { - switch v := v.(type) { - case float64: - return int64(v) - case int64: - return v - default: - return int64(0) - } -} - -func castToString(v interface{}) string { - switch v := v.(type) { - case string: - return v - default: - return "" - } -} - -func castToBoolean(v interface{}) bool { - switch v := v.(type) { - case bool: - return v - default: - return false - } -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/doc.go b/vendor/github.com/influxdata/influxdb/influxql/doc.go deleted file mode 100644 index c26b9a56..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/doc.go +++ /dev/null @@ -1,59 +0,0 @@ -/* -Package influxql implements a parser for the InfluxDB query language. - -InfluxQL is a DML and DDL language for the InfluxDB time series database. -It provides the ability to query for aggregate statistics as well as create -and configure the InfluxDB server. - -Selecting data - -The SELECT query is used for retrieving data from one or more series. It allows -for a list of columns followed by a list of series to select from. - - SELECT value FROM cpu_load - -You can also add a a conditional expression to limit the results of the query: - - SELECT value FROM cpu_load WHERE host = 'influxdb.com' - -Limits and ordering can be set on selection queries as well: - - SELECT value FROM cpu_load LIMIT 100 ORDER DESC; - - -Removing data - -The DELETE query is available to remove time series data points from the -database. This query will delete "cpu_load" values older than an hour: - - DELETE FROM cpu_load WHERE time < now() - 1h - - -Continuous Queries - -Queries can be run indefinitely on the server in order to generate new series. -This is done by running a "SELECT INTO" query. For example, this query computes -the hourly mean for cpu_load and stores it into a "cpu_load" series in the -"daily" shard space. - - SELECT mean(value) AS value FROM cpu_load GROUP BY 1h - INTO daily.cpu_load - -If there is existing data on the source series then this query will be run for -all historic data. To only execute the query on new incoming data you can append -"NO BACKFILL" to the end of the query: - - SELECT mean(value) AS value FROM cpu_load GROUP BY 1h - INTO daily.cpu_load NO BACKFILL - -Continuous queries will return an id that can be used to remove them in the -future. To remove a continous query, use the DROP CONTINUOUS QUERY statement: - - DROP CONTINUOUS QUERY 12 - -You can also list all continuous queries by running: - - LIST CONTINUOUS QUERIES - -*/ -package influxql diff --git a/vendor/github.com/influxdata/influxdb/influxql/emitter.go b/vendor/github.com/influxdata/influxdb/influxql/emitter.go deleted file mode 100644 index 0f55e242..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/emitter.go +++ /dev/null @@ -1,221 +0,0 @@ -package influxql - -import ( - "fmt" - "time" - - "github.com/influxdata/influxdb/models" -) - -// Emitter groups values together by name, -type Emitter struct { - buf []Point - itrs []Iterator - ascending bool - chunkSize int - - tags Tags - row *models.Row - - // The columns to attach to each row. - Columns []string - - // Removes the "time" column from output. - // Used for meta queries where time does not apply. - OmitTime bool -} - -// NewEmitter returns a new instance of Emitter that pulls from itrs. -func NewEmitter(itrs []Iterator, ascending bool, chunkSize int) *Emitter { - return &Emitter{ - buf: make([]Point, len(itrs)), - itrs: itrs, - ascending: ascending, - chunkSize: chunkSize, - } -} - -// Close closes the underlying iterators. -func (e *Emitter) Close() error { - return Iterators(e.itrs).Close() -} - -// Emit returns the next row from the iterators. -func (e *Emitter) Emit() (*models.Row, bool, error) { - // Immediately end emission if there are no iterators. - if len(e.itrs) == 0 { - return nil, false, nil - } - - // Continually read from iterators until they are exhausted. - for { - // Fill buffer. Return row if no more points remain. - t, name, tags, err := e.loadBuf() - if err != nil { - return nil, false, err - } else if t == ZeroTime { - row := e.row - e.row = nil - return row, false, nil - } - - // Read next set of values from all iterators at a given time/name/tags. - // If no values are returned then return row. - values := e.readAt(t, name, tags) - if values == nil { - row := e.row - e.row = nil - return row, false, nil - } - - // If there's no row yet then create one. - // If the name and tags match the existing row, append to that row if - // the number of values doesn't exceed the chunk size. - // Otherwise return existing row and add values to next emitted row. - if e.row == nil { - e.createRow(name, tags, values) - } else if e.row.Name == name && e.tags.Equals(&tags) { - if e.chunkSize > 0 && len(e.row.Values) >= e.chunkSize { - row := e.row - row.Partial = true - e.createRow(name, tags, values) - return row, true, nil - } - e.row.Values = append(e.row.Values, values) - } else { - row := e.row - e.createRow(name, tags, values) - return row, true, nil - } - } -} - -// loadBuf reads in points into empty buffer slots. -// Returns the next time/name/tags to emit for. -func (e *Emitter) loadBuf() (t int64, name string, tags Tags, err error) { - t = ZeroTime - - for i := range e.itrs { - // Load buffer, if empty. - if e.buf[i] == nil { - e.buf[i], err = e.readIterator(e.itrs[i]) - if err != nil { - break - } - } - - // Skip if buffer is empty. - p := e.buf[i] - if p == nil { - continue - } - itrTime, itrName, itrTags := p.time(), p.name(), p.tags() - - // Initialize range values if not set. - if t == ZeroTime { - t, name, tags = itrTime, itrName, itrTags - continue - } - - // Update range values if lower and emitter is in time ascending order. - if e.ascending { - if (itrName < name) || (itrName == name && itrTags.ID() < tags.ID()) || (itrName == name && itrTags.ID() == tags.ID() && itrTime < t) { - t, name, tags = itrTime, itrName, itrTags - } - continue - } - - // Update range values if higher and emitter is in time descending order. - if (itrName < name) || (itrName == name && itrTags.ID() < tags.ID()) || (itrName == name && itrTags.ID() == tags.ID() && itrTime < t) { - t, name, tags = itrTime, itrName, itrTags - } - } - - return -} - -// createRow creates a new row attached to the emitter. -func (e *Emitter) createRow(name string, tags Tags, values []interface{}) { - e.tags = tags - e.row = &models.Row{ - Name: name, - Tags: tags.KeyValues(), - Columns: e.Columns, - Values: [][]interface{}{values}, - } -} - -// readAt returns the next slice of values from the iterators at time/name/tags. -// Returns nil values once the iterators are exhausted. -func (e *Emitter) readAt(t int64, name string, tags Tags) []interface{} { - // If time is included then move colums over by one. - offset := 1 - if e.OmitTime { - offset = 0 - } - - values := make([]interface{}, len(e.itrs)+offset) - if !e.OmitTime { - values[0] = time.Unix(0, t).UTC() - } - - for i, p := range e.buf { - // Skip if buffer is empty. - if p == nil { - values[i+offset] = nil - continue - } - - // Skip point if it doesn't match time/name/tags. - pTags := p.tags() - if p.time() != t || p.name() != name || !pTags.Equals(&tags) { - values[i+offset] = nil - continue - } - - // Read point value. - values[i+offset] = p.value() - - // Clear buffer. - e.buf[i] = nil - } - - return values -} - -// readIterator reads the next point from itr. -func (e *Emitter) readIterator(itr Iterator) (Point, error) { - if itr == nil { - return nil, nil - } - - switch itr := itr.(type) { - case FloatIterator: - if p, err := itr.Next(); err != nil { - return nil, err - } else if p != nil { - return p, nil - } - case IntegerIterator: - if p, err := itr.Next(); err != nil { - return nil, err - } else if p != nil { - return p, nil - } - case StringIterator: - if p, err := itr.Next(); err != nil { - return nil, err - } else if p != nil { - return p, nil - } - case BooleanIterator: - if p, err := itr.Next(); err != nil { - return nil, err - } else if p != nil { - return p, nil - } - default: - panic(fmt.Sprintf("unsupported iterator: %T", itr)) - } - return nil, nil -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/emitter_test.go b/vendor/github.com/influxdata/influxdb/influxql/emitter_test.go deleted file mode 100644 index fdf8be0a..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/emitter_test.go +++ /dev/null @@ -1,125 +0,0 @@ -package influxql_test - -import ( - "testing" - "time" - - "github.com/davecgh/go-spew/spew" - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/models" - "github.com/influxdata/influxdb/pkg/deep" -) - -// Ensure the emitter can group iterators together into rows. -func TestEmitter_Emit(t *testing.T) { - // Build an emitter that pulls from two iterators. - e := influxql.NewEmitter([]influxql.Iterator{ - &FloatIterator{Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("region=west"), Time: 0, Value: 1}, - {Name: "cpu", Tags: ParseTags("region=west"), Time: 1, Value: 2}, - }}, - &FloatIterator{Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("region=west"), Time: 1, Value: 4}, - {Name: "cpu", Tags: ParseTags("region=north"), Time: 0, Value: 4}, - {Name: "mem", Time: 4, Value: 5}, - }}, - }, true, 0) - e.Columns = []string{"col1", "col2"} - - // Verify the cpu region=west is emitted first. - if row, _, err := e.Emit(); err != nil { - t.Fatalf("unexpected error(0): %s", err) - } else if !deep.Equal(row, &models.Row{ - Name: "cpu", - Tags: map[string]string{"region": "west"}, - Columns: []string{"col1", "col2"}, - Values: [][]interface{}{ - {time.Unix(0, 0).UTC(), float64(1), nil}, - {time.Unix(0, 1).UTC(), float64(2), float64(4)}, - }, - }) { - t.Fatalf("unexpected row(0): %s", spew.Sdump(row)) - } - - // Verify the cpu region=north is emitted next. - if row, _, err := e.Emit(); err != nil { - t.Fatalf("unexpected error(1): %s", err) - } else if !deep.Equal(row, &models.Row{ - Name: "cpu", - Tags: map[string]string{"region": "north"}, - Columns: []string{"col1", "col2"}, - Values: [][]interface{}{ - {time.Unix(0, 0).UTC(), nil, float64(4)}, - }, - }) { - t.Fatalf("unexpected row(1): %s", spew.Sdump(row)) - } - - // Verify the mem series is emitted last. - if row, _, err := e.Emit(); err != nil { - t.Fatalf("unexpected error(2): %s", err) - } else if !deep.Equal(row, &models.Row{ - Name: "mem", - Columns: []string{"col1", "col2"}, - Values: [][]interface{}{ - {time.Unix(0, 4).UTC(), nil, float64(5)}, - }, - }) { - t.Fatalf("unexpected row(2): %s", spew.Sdump(row)) - } - - // Verify EOF. - if row, _, err := e.Emit(); err != nil { - t.Fatalf("unexpected error(eof): %s", err) - } else if row != nil { - t.Fatalf("unexpected eof: %s", spew.Sdump(row)) - } -} - -// Ensure the emitter will limit the chunked output from a series. -func TestEmitter_ChunkSize(t *testing.T) { - // Build an emitter that pulls from one iterator with multiple points in the same series. - e := influxql.NewEmitter([]influxql.Iterator{ - &FloatIterator{Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("region=west"), Time: 0, Value: 1}, - {Name: "cpu", Tags: ParseTags("region=west"), Time: 1, Value: 2}, - }}, - }, true, 1) - e.Columns = []string{"col1"} - - // Verify the cpu region=west is emitted first. - if row, _, err := e.Emit(); err != nil { - t.Fatalf("unexpected error(0): %s", err) - } else if !deep.Equal(row, &models.Row{ - Name: "cpu", - Tags: map[string]string{"region": "west"}, - Columns: []string{"col1"}, - Values: [][]interface{}{ - {time.Unix(0, 0).UTC(), float64(1)}, - }, - Partial: true, - }) { - t.Fatalf("unexpected row(0): %s", spew.Sdump(row)) - } - - // Verify the cpu region=north is emitted next. - if row, _, err := e.Emit(); err != nil { - t.Fatalf("unexpected error(1): %s", err) - } else if !deep.Equal(row, &models.Row{ - Name: "cpu", - Tags: map[string]string{"region": "west"}, - Columns: []string{"col1"}, - Values: [][]interface{}{ - {time.Unix(0, 1).UTC(), float64(2)}, - }, - }) { - t.Fatalf("unexpected row(1): %s", spew.Sdump(row)) - } - - // Verify EOF. - if row, _, err := e.Emit(); err != nil { - t.Fatalf("unexpected error(eof): %s", err) - } else if row != nil { - t.Fatalf("unexpected eof: %s", spew.Sdump(row)) - } -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/functions.gen.go b/vendor/github.com/influxdata/influxdb/influxql/functions.gen.go deleted file mode 100644 index 85e92f39..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/functions.gen.go +++ /dev/null @@ -1,1669 +0,0 @@ -// Generated by tmpl -// https://github.com/benbjohnson/tmpl -// -// DO NOT EDIT! -// Source: functions.gen.go.tmpl - -package influxql - -import ( - "math/rand" - "sort" - "time" -) - -// FloatPointAggregator aggregates points to produce a single point. -type FloatPointAggregator interface { - AggregateFloat(p *FloatPoint) -} - -// FloatBulkPointAggregator aggregates multiple points at a time. -type FloatBulkPointAggregator interface { - AggregateFloatBulk(points []FloatPoint) -} - -// AggregateFloatPoints feeds a slice of FloatPoint into an -// aggregator. If the aggregator is a FloatBulkPointAggregator, it will -// use the AggregateBulk method. -func AggregateFloatPoints(a FloatPointAggregator, points []FloatPoint) { - switch a := a.(type) { - case FloatBulkPointAggregator: - a.AggregateFloatBulk(points) - default: - for _, p := range points { - a.AggregateFloat(&p) - } - } -} - -// FloatPointEmitter produces a single point from an aggregate. -type FloatPointEmitter interface { - Emit() []FloatPoint -} - -// FloatReduceFunc is the function called by a FloatPoint reducer. -type FloatReduceFunc func(prev *FloatPoint, curr *FloatPoint) (t int64, v float64, aux []interface{}) - -// FloatFuncReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type FloatFuncReducer struct { - prev *FloatPoint - fn FloatReduceFunc -} - -// NewFloatFuncReducer creates a new FloatFuncFloatReducer. -func NewFloatFuncReducer(fn FloatReduceFunc, prev *FloatPoint) *FloatFuncReducer { - return &FloatFuncReducer{fn: fn, prev: prev} -} - -// AggregateFloat takes a FloatPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *FloatFuncReducer) AggregateFloat(p *FloatPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &FloatPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateFloat. -func (r *FloatFuncReducer) Emit() []FloatPoint { - return []FloatPoint{*r.prev} -} - -// FloatReduceSliceFunc is the function called by a FloatPoint reducer. -type FloatReduceSliceFunc func(a []FloatPoint) []FloatPoint - -// FloatSliceFuncReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type FloatSliceFuncReducer struct { - points []FloatPoint - fn FloatReduceSliceFunc -} - -// NewFloatSliceFuncReducer creates a new FloatSliceFuncReducer. -func NewFloatSliceFuncReducer(fn FloatReduceSliceFunc) *FloatSliceFuncReducer { - return &FloatSliceFuncReducer{fn: fn} -} - -// AggregateFloat copies the FloatPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *FloatSliceFuncReducer) AggregateFloat(p *FloatPoint) { - r.points = append(r.points, *p) -} - -// AggregateFloatBulk performs a bulk copy of FloatPoints into the internal slice. -// This is a more efficient version of calling AggregateFloat on each point. -func (r *FloatSliceFuncReducer) AggregateFloatBulk(points []FloatPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *FloatSliceFuncReducer) Emit() []FloatPoint { - return r.fn(r.points) -} - -// FloatReduceIntegerFunc is the function called by a FloatPoint reducer. -type FloatReduceIntegerFunc func(prev *IntegerPoint, curr *FloatPoint) (t int64, v int64, aux []interface{}) - -// FloatFuncIntegerReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type FloatFuncIntegerReducer struct { - prev *IntegerPoint - fn FloatReduceIntegerFunc -} - -// NewFloatFuncIntegerReducer creates a new FloatFuncIntegerReducer. -func NewFloatFuncIntegerReducer(fn FloatReduceIntegerFunc, prev *IntegerPoint) *FloatFuncIntegerReducer { - return &FloatFuncIntegerReducer{fn: fn, prev: prev} -} - -// AggregateFloat takes a FloatPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *FloatFuncIntegerReducer) AggregateFloat(p *FloatPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &IntegerPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateFloat. -func (r *FloatFuncIntegerReducer) Emit() []IntegerPoint { - return []IntegerPoint{*r.prev} -} - -// FloatReduceIntegerSliceFunc is the function called by a FloatPoint reducer. -type FloatReduceIntegerSliceFunc func(a []FloatPoint) []IntegerPoint - -// FloatSliceFuncIntegerReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type FloatSliceFuncIntegerReducer struct { - points []FloatPoint - fn FloatReduceIntegerSliceFunc -} - -// NewFloatSliceFuncIntegerReducer creates a new FloatSliceFuncIntegerReducer. -func NewFloatSliceFuncIntegerReducer(fn FloatReduceIntegerSliceFunc) *FloatSliceFuncIntegerReducer { - return &FloatSliceFuncIntegerReducer{fn: fn} -} - -// AggregateFloat copies the FloatPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *FloatSliceFuncIntegerReducer) AggregateFloat(p *FloatPoint) { - r.points = append(r.points, *p) -} - -// AggregateFloatBulk performs a bulk copy of FloatPoints into the internal slice. -// This is a more efficient version of calling AggregateFloat on each point. -func (r *FloatSliceFuncIntegerReducer) AggregateFloatBulk(points []FloatPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *FloatSliceFuncIntegerReducer) Emit() []IntegerPoint { - return r.fn(r.points) -} - -// FloatReduceStringFunc is the function called by a FloatPoint reducer. -type FloatReduceStringFunc func(prev *StringPoint, curr *FloatPoint) (t int64, v string, aux []interface{}) - -// FloatFuncStringReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type FloatFuncStringReducer struct { - prev *StringPoint - fn FloatReduceStringFunc -} - -// NewFloatFuncStringReducer creates a new FloatFuncStringReducer. -func NewFloatFuncStringReducer(fn FloatReduceStringFunc, prev *StringPoint) *FloatFuncStringReducer { - return &FloatFuncStringReducer{fn: fn, prev: prev} -} - -// AggregateFloat takes a FloatPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *FloatFuncStringReducer) AggregateFloat(p *FloatPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &StringPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateFloat. -func (r *FloatFuncStringReducer) Emit() []StringPoint { - return []StringPoint{*r.prev} -} - -// FloatReduceStringSliceFunc is the function called by a FloatPoint reducer. -type FloatReduceStringSliceFunc func(a []FloatPoint) []StringPoint - -// FloatSliceFuncStringReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type FloatSliceFuncStringReducer struct { - points []FloatPoint - fn FloatReduceStringSliceFunc -} - -// NewFloatSliceFuncStringReducer creates a new FloatSliceFuncStringReducer. -func NewFloatSliceFuncStringReducer(fn FloatReduceStringSliceFunc) *FloatSliceFuncStringReducer { - return &FloatSliceFuncStringReducer{fn: fn} -} - -// AggregateFloat copies the FloatPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *FloatSliceFuncStringReducer) AggregateFloat(p *FloatPoint) { - r.points = append(r.points, *p) -} - -// AggregateFloatBulk performs a bulk copy of FloatPoints into the internal slice. -// This is a more efficient version of calling AggregateFloat on each point. -func (r *FloatSliceFuncStringReducer) AggregateFloatBulk(points []FloatPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *FloatSliceFuncStringReducer) Emit() []StringPoint { - return r.fn(r.points) -} - -// FloatReduceBooleanFunc is the function called by a FloatPoint reducer. -type FloatReduceBooleanFunc func(prev *BooleanPoint, curr *FloatPoint) (t int64, v bool, aux []interface{}) - -// FloatFuncBooleanReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type FloatFuncBooleanReducer struct { - prev *BooleanPoint - fn FloatReduceBooleanFunc -} - -// NewFloatFuncBooleanReducer creates a new FloatFuncBooleanReducer. -func NewFloatFuncBooleanReducer(fn FloatReduceBooleanFunc, prev *BooleanPoint) *FloatFuncBooleanReducer { - return &FloatFuncBooleanReducer{fn: fn, prev: prev} -} - -// AggregateFloat takes a FloatPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *FloatFuncBooleanReducer) AggregateFloat(p *FloatPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &BooleanPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateFloat. -func (r *FloatFuncBooleanReducer) Emit() []BooleanPoint { - return []BooleanPoint{*r.prev} -} - -// FloatReduceBooleanSliceFunc is the function called by a FloatPoint reducer. -type FloatReduceBooleanSliceFunc func(a []FloatPoint) []BooleanPoint - -// FloatSliceFuncBooleanReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type FloatSliceFuncBooleanReducer struct { - points []FloatPoint - fn FloatReduceBooleanSliceFunc -} - -// NewFloatSliceFuncBooleanReducer creates a new FloatSliceFuncBooleanReducer. -func NewFloatSliceFuncBooleanReducer(fn FloatReduceBooleanSliceFunc) *FloatSliceFuncBooleanReducer { - return &FloatSliceFuncBooleanReducer{fn: fn} -} - -// AggregateFloat copies the FloatPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *FloatSliceFuncBooleanReducer) AggregateFloat(p *FloatPoint) { - r.points = append(r.points, *p) -} - -// AggregateFloatBulk performs a bulk copy of FloatPoints into the internal slice. -// This is a more efficient version of calling AggregateFloat on each point. -func (r *FloatSliceFuncBooleanReducer) AggregateFloatBulk(points []FloatPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *FloatSliceFuncBooleanReducer) Emit() []BooleanPoint { - return r.fn(r.points) -} - -// FloatDistinctReducer returns the distinct points in a series. -type FloatDistinctReducer struct { - m map[float64]FloatPoint -} - -// NewFloatDistinctReducer creates a new FloatDistinctReducer. -func NewFloatDistinctReducer() *FloatDistinctReducer { - return &FloatDistinctReducer{m: make(map[float64]FloatPoint)} -} - -// AggregateFloat aggregates a point into the reducer. -func (r *FloatDistinctReducer) AggregateFloat(p *FloatPoint) { - if _, ok := r.m[p.Value]; !ok { - r.m[p.Value] = *p - } -} - -// Emit emits the distinct points that have been aggregated into the reducer. -func (r *FloatDistinctReducer) Emit() []FloatPoint { - points := make([]FloatPoint, 0, len(r.m)) - for _, p := range r.m { - points = append(points, FloatPoint{Time: p.Time, Value: p.Value}) - } - sort.Sort(floatPoints(points)) - return points -} - -// FloatElapsedReducer calculates the elapsed of the aggregated points. -type FloatElapsedReducer struct { - unitConversion int64 - prev FloatPoint - curr FloatPoint -} - -// NewFloatElapsedReducer creates a new FloatElapsedReducer. -func NewFloatElapsedReducer(interval Interval) *FloatElapsedReducer { - return &FloatElapsedReducer{ - unitConversion: int64(interval.Duration), - prev: FloatPoint{Nil: true}, - curr: FloatPoint{Nil: true}, - } -} - -// AggregateFloat aggregates a point into the reducer and updates the current window. -func (r *FloatElapsedReducer) AggregateFloat(p *FloatPoint) { - r.prev = r.curr - r.curr = *p -} - -// Emit emits the elapsed of the reducer at the current point. -func (r *FloatElapsedReducer) Emit() []IntegerPoint { - if !r.prev.Nil { - elapsed := (r.curr.Time - r.prev.Time) / r.unitConversion - return []IntegerPoint{ - {Time: r.curr.Time, Value: elapsed}, - } - } - return nil -} - -// FloatSampleReduces implements a reservoir sampling to calculate a random subset of points -type FloatSampleReducer struct { - count int // how many points we've iterated over - rng *rand.Rand // random number generator for each reducer - - points floatPoints // the reservoir -} - -// NewFloatSampleReducer creates a new FloatSampleReducer -func NewFloatSampleReducer(size int) *FloatSampleReducer { - return &FloatSampleReducer{ - rng: rand.New(rand.NewSource(time.Now().UnixNano())), // seed with current time as suggested by https://golang.org/pkg/math/rand/ - points: make(floatPoints, size), - } -} - -// AggregateFloat aggregates a point into the reducer. -func (r *FloatSampleReducer) AggregateFloat(p *FloatPoint) { - r.count++ - // Fill the reservoir with the first n points - if r.count-1 < len(r.points) { - r.points[r.count-1] = *p - return - } - - // Generate a random integer between 1 and the count and - // if that number is less than the length of the slice - // replace the point at that index rnd with p. - rnd := r.rng.Intn(r.count) - if rnd < len(r.points) { - r.points[rnd] = *p - } -} - -// Emit emits the reservoir sample as many points. -func (r *FloatSampleReducer) Emit() []FloatPoint { - min := len(r.points) - if r.count < min { - min = r.count - } - pts := r.points[:min] - sort.Sort(pts) - return pts -} - -// IntegerPointAggregator aggregates points to produce a single point. -type IntegerPointAggregator interface { - AggregateInteger(p *IntegerPoint) -} - -// IntegerBulkPointAggregator aggregates multiple points at a time. -type IntegerBulkPointAggregator interface { - AggregateIntegerBulk(points []IntegerPoint) -} - -// AggregateIntegerPoints feeds a slice of IntegerPoint into an -// aggregator. If the aggregator is a IntegerBulkPointAggregator, it will -// use the AggregateBulk method. -func AggregateIntegerPoints(a IntegerPointAggregator, points []IntegerPoint) { - switch a := a.(type) { - case IntegerBulkPointAggregator: - a.AggregateIntegerBulk(points) - default: - for _, p := range points { - a.AggregateInteger(&p) - } - } -} - -// IntegerPointEmitter produces a single point from an aggregate. -type IntegerPointEmitter interface { - Emit() []IntegerPoint -} - -// IntegerReduceFloatFunc is the function called by a IntegerPoint reducer. -type IntegerReduceFloatFunc func(prev *FloatPoint, curr *IntegerPoint) (t int64, v float64, aux []interface{}) - -// IntegerFuncFloatReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type IntegerFuncFloatReducer struct { - prev *FloatPoint - fn IntegerReduceFloatFunc -} - -// NewIntegerFuncFloatReducer creates a new IntegerFuncFloatReducer. -func NewIntegerFuncFloatReducer(fn IntegerReduceFloatFunc, prev *FloatPoint) *IntegerFuncFloatReducer { - return &IntegerFuncFloatReducer{fn: fn, prev: prev} -} - -// AggregateInteger takes a IntegerPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *IntegerFuncFloatReducer) AggregateInteger(p *IntegerPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &FloatPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateInteger. -func (r *IntegerFuncFloatReducer) Emit() []FloatPoint { - return []FloatPoint{*r.prev} -} - -// IntegerReduceFloatSliceFunc is the function called by a IntegerPoint reducer. -type IntegerReduceFloatSliceFunc func(a []IntegerPoint) []FloatPoint - -// IntegerSliceFuncFloatReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type IntegerSliceFuncFloatReducer struct { - points []IntegerPoint - fn IntegerReduceFloatSliceFunc -} - -// NewIntegerSliceFuncFloatReducer creates a new IntegerSliceFuncFloatReducer. -func NewIntegerSliceFuncFloatReducer(fn IntegerReduceFloatSliceFunc) *IntegerSliceFuncFloatReducer { - return &IntegerSliceFuncFloatReducer{fn: fn} -} - -// AggregateInteger copies the IntegerPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *IntegerSliceFuncFloatReducer) AggregateInteger(p *IntegerPoint) { - r.points = append(r.points, *p) -} - -// AggregateIntegerBulk performs a bulk copy of IntegerPoints into the internal slice. -// This is a more efficient version of calling AggregateInteger on each point. -func (r *IntegerSliceFuncFloatReducer) AggregateIntegerBulk(points []IntegerPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *IntegerSliceFuncFloatReducer) Emit() []FloatPoint { - return r.fn(r.points) -} - -// IntegerReduceFunc is the function called by a IntegerPoint reducer. -type IntegerReduceFunc func(prev *IntegerPoint, curr *IntegerPoint) (t int64, v int64, aux []interface{}) - -// IntegerFuncReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type IntegerFuncReducer struct { - prev *IntegerPoint - fn IntegerReduceFunc -} - -// NewIntegerFuncReducer creates a new IntegerFuncIntegerReducer. -func NewIntegerFuncReducer(fn IntegerReduceFunc, prev *IntegerPoint) *IntegerFuncReducer { - return &IntegerFuncReducer{fn: fn, prev: prev} -} - -// AggregateInteger takes a IntegerPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *IntegerFuncReducer) AggregateInteger(p *IntegerPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &IntegerPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateInteger. -func (r *IntegerFuncReducer) Emit() []IntegerPoint { - return []IntegerPoint{*r.prev} -} - -// IntegerReduceSliceFunc is the function called by a IntegerPoint reducer. -type IntegerReduceSliceFunc func(a []IntegerPoint) []IntegerPoint - -// IntegerSliceFuncReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type IntegerSliceFuncReducer struct { - points []IntegerPoint - fn IntegerReduceSliceFunc -} - -// NewIntegerSliceFuncReducer creates a new IntegerSliceFuncReducer. -func NewIntegerSliceFuncReducer(fn IntegerReduceSliceFunc) *IntegerSliceFuncReducer { - return &IntegerSliceFuncReducer{fn: fn} -} - -// AggregateInteger copies the IntegerPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *IntegerSliceFuncReducer) AggregateInteger(p *IntegerPoint) { - r.points = append(r.points, *p) -} - -// AggregateIntegerBulk performs a bulk copy of IntegerPoints into the internal slice. -// This is a more efficient version of calling AggregateInteger on each point. -func (r *IntegerSliceFuncReducer) AggregateIntegerBulk(points []IntegerPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *IntegerSliceFuncReducer) Emit() []IntegerPoint { - return r.fn(r.points) -} - -// IntegerReduceStringFunc is the function called by a IntegerPoint reducer. -type IntegerReduceStringFunc func(prev *StringPoint, curr *IntegerPoint) (t int64, v string, aux []interface{}) - -// IntegerFuncStringReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type IntegerFuncStringReducer struct { - prev *StringPoint - fn IntegerReduceStringFunc -} - -// NewIntegerFuncStringReducer creates a new IntegerFuncStringReducer. -func NewIntegerFuncStringReducer(fn IntegerReduceStringFunc, prev *StringPoint) *IntegerFuncStringReducer { - return &IntegerFuncStringReducer{fn: fn, prev: prev} -} - -// AggregateInteger takes a IntegerPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *IntegerFuncStringReducer) AggregateInteger(p *IntegerPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &StringPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateInteger. -func (r *IntegerFuncStringReducer) Emit() []StringPoint { - return []StringPoint{*r.prev} -} - -// IntegerReduceStringSliceFunc is the function called by a IntegerPoint reducer. -type IntegerReduceStringSliceFunc func(a []IntegerPoint) []StringPoint - -// IntegerSliceFuncStringReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type IntegerSliceFuncStringReducer struct { - points []IntegerPoint - fn IntegerReduceStringSliceFunc -} - -// NewIntegerSliceFuncStringReducer creates a new IntegerSliceFuncStringReducer. -func NewIntegerSliceFuncStringReducer(fn IntegerReduceStringSliceFunc) *IntegerSliceFuncStringReducer { - return &IntegerSliceFuncStringReducer{fn: fn} -} - -// AggregateInteger copies the IntegerPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *IntegerSliceFuncStringReducer) AggregateInteger(p *IntegerPoint) { - r.points = append(r.points, *p) -} - -// AggregateIntegerBulk performs a bulk copy of IntegerPoints into the internal slice. -// This is a more efficient version of calling AggregateInteger on each point. -func (r *IntegerSliceFuncStringReducer) AggregateIntegerBulk(points []IntegerPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *IntegerSliceFuncStringReducer) Emit() []StringPoint { - return r.fn(r.points) -} - -// IntegerReduceBooleanFunc is the function called by a IntegerPoint reducer. -type IntegerReduceBooleanFunc func(prev *BooleanPoint, curr *IntegerPoint) (t int64, v bool, aux []interface{}) - -// IntegerFuncBooleanReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type IntegerFuncBooleanReducer struct { - prev *BooleanPoint - fn IntegerReduceBooleanFunc -} - -// NewIntegerFuncBooleanReducer creates a new IntegerFuncBooleanReducer. -func NewIntegerFuncBooleanReducer(fn IntegerReduceBooleanFunc, prev *BooleanPoint) *IntegerFuncBooleanReducer { - return &IntegerFuncBooleanReducer{fn: fn, prev: prev} -} - -// AggregateInteger takes a IntegerPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *IntegerFuncBooleanReducer) AggregateInteger(p *IntegerPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &BooleanPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateInteger. -func (r *IntegerFuncBooleanReducer) Emit() []BooleanPoint { - return []BooleanPoint{*r.prev} -} - -// IntegerReduceBooleanSliceFunc is the function called by a IntegerPoint reducer. -type IntegerReduceBooleanSliceFunc func(a []IntegerPoint) []BooleanPoint - -// IntegerSliceFuncBooleanReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type IntegerSliceFuncBooleanReducer struct { - points []IntegerPoint - fn IntegerReduceBooleanSliceFunc -} - -// NewIntegerSliceFuncBooleanReducer creates a new IntegerSliceFuncBooleanReducer. -func NewIntegerSliceFuncBooleanReducer(fn IntegerReduceBooleanSliceFunc) *IntegerSliceFuncBooleanReducer { - return &IntegerSliceFuncBooleanReducer{fn: fn} -} - -// AggregateInteger copies the IntegerPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *IntegerSliceFuncBooleanReducer) AggregateInteger(p *IntegerPoint) { - r.points = append(r.points, *p) -} - -// AggregateIntegerBulk performs a bulk copy of IntegerPoints into the internal slice. -// This is a more efficient version of calling AggregateInteger on each point. -func (r *IntegerSliceFuncBooleanReducer) AggregateIntegerBulk(points []IntegerPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *IntegerSliceFuncBooleanReducer) Emit() []BooleanPoint { - return r.fn(r.points) -} - -// IntegerDistinctReducer returns the distinct points in a series. -type IntegerDistinctReducer struct { - m map[int64]IntegerPoint -} - -// NewIntegerDistinctReducer creates a new IntegerDistinctReducer. -func NewIntegerDistinctReducer() *IntegerDistinctReducer { - return &IntegerDistinctReducer{m: make(map[int64]IntegerPoint)} -} - -// AggregateInteger aggregates a point into the reducer. -func (r *IntegerDistinctReducer) AggregateInteger(p *IntegerPoint) { - if _, ok := r.m[p.Value]; !ok { - r.m[p.Value] = *p - } -} - -// Emit emits the distinct points that have been aggregated into the reducer. -func (r *IntegerDistinctReducer) Emit() []IntegerPoint { - points := make([]IntegerPoint, 0, len(r.m)) - for _, p := range r.m { - points = append(points, IntegerPoint{Time: p.Time, Value: p.Value}) - } - sort.Sort(integerPoints(points)) - return points -} - -// IntegerElapsedReducer calculates the elapsed of the aggregated points. -type IntegerElapsedReducer struct { - unitConversion int64 - prev IntegerPoint - curr IntegerPoint -} - -// NewIntegerElapsedReducer creates a new IntegerElapsedReducer. -func NewIntegerElapsedReducer(interval Interval) *IntegerElapsedReducer { - return &IntegerElapsedReducer{ - unitConversion: int64(interval.Duration), - prev: IntegerPoint{Nil: true}, - curr: IntegerPoint{Nil: true}, - } -} - -// AggregateInteger aggregates a point into the reducer and updates the current window. -func (r *IntegerElapsedReducer) AggregateInteger(p *IntegerPoint) { - r.prev = r.curr - r.curr = *p -} - -// Emit emits the elapsed of the reducer at the current point. -func (r *IntegerElapsedReducer) Emit() []IntegerPoint { - if !r.prev.Nil { - elapsed := (r.curr.Time - r.prev.Time) / r.unitConversion - return []IntegerPoint{ - {Time: r.curr.Time, Value: elapsed}, - } - } - return nil -} - -// IntegerSampleReduces implements a reservoir sampling to calculate a random subset of points -type IntegerSampleReducer struct { - count int // how many points we've iterated over - rng *rand.Rand // random number generator for each reducer - - points integerPoints // the reservoir -} - -// NewIntegerSampleReducer creates a new IntegerSampleReducer -func NewIntegerSampleReducer(size int) *IntegerSampleReducer { - return &IntegerSampleReducer{ - rng: rand.New(rand.NewSource(time.Now().UnixNano())), // seed with current time as suggested by https://golang.org/pkg/math/rand/ - points: make(integerPoints, size), - } -} - -// AggregateInteger aggregates a point into the reducer. -func (r *IntegerSampleReducer) AggregateInteger(p *IntegerPoint) { - r.count++ - // Fill the reservoir with the first n points - if r.count-1 < len(r.points) { - r.points[r.count-1] = *p - return - } - - // Generate a random integer between 1 and the count and - // if that number is less than the length of the slice - // replace the point at that index rnd with p. - rnd := r.rng.Intn(r.count) - if rnd < len(r.points) { - r.points[rnd] = *p - } -} - -// Emit emits the reservoir sample as many points. -func (r *IntegerSampleReducer) Emit() []IntegerPoint { - min := len(r.points) - if r.count < min { - min = r.count - } - pts := r.points[:min] - sort.Sort(pts) - return pts -} - -// StringPointAggregator aggregates points to produce a single point. -type StringPointAggregator interface { - AggregateString(p *StringPoint) -} - -// StringBulkPointAggregator aggregates multiple points at a time. -type StringBulkPointAggregator interface { - AggregateStringBulk(points []StringPoint) -} - -// AggregateStringPoints feeds a slice of StringPoint into an -// aggregator. If the aggregator is a StringBulkPointAggregator, it will -// use the AggregateBulk method. -func AggregateStringPoints(a StringPointAggregator, points []StringPoint) { - switch a := a.(type) { - case StringBulkPointAggregator: - a.AggregateStringBulk(points) - default: - for _, p := range points { - a.AggregateString(&p) - } - } -} - -// StringPointEmitter produces a single point from an aggregate. -type StringPointEmitter interface { - Emit() []StringPoint -} - -// StringReduceFloatFunc is the function called by a StringPoint reducer. -type StringReduceFloatFunc func(prev *FloatPoint, curr *StringPoint) (t int64, v float64, aux []interface{}) - -// StringFuncFloatReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type StringFuncFloatReducer struct { - prev *FloatPoint - fn StringReduceFloatFunc -} - -// NewStringFuncFloatReducer creates a new StringFuncFloatReducer. -func NewStringFuncFloatReducer(fn StringReduceFloatFunc, prev *FloatPoint) *StringFuncFloatReducer { - return &StringFuncFloatReducer{fn: fn, prev: prev} -} - -// AggregateString takes a StringPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *StringFuncFloatReducer) AggregateString(p *StringPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &FloatPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateString. -func (r *StringFuncFloatReducer) Emit() []FloatPoint { - return []FloatPoint{*r.prev} -} - -// StringReduceFloatSliceFunc is the function called by a StringPoint reducer. -type StringReduceFloatSliceFunc func(a []StringPoint) []FloatPoint - -// StringSliceFuncFloatReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type StringSliceFuncFloatReducer struct { - points []StringPoint - fn StringReduceFloatSliceFunc -} - -// NewStringSliceFuncFloatReducer creates a new StringSliceFuncFloatReducer. -func NewStringSliceFuncFloatReducer(fn StringReduceFloatSliceFunc) *StringSliceFuncFloatReducer { - return &StringSliceFuncFloatReducer{fn: fn} -} - -// AggregateString copies the StringPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *StringSliceFuncFloatReducer) AggregateString(p *StringPoint) { - r.points = append(r.points, *p) -} - -// AggregateStringBulk performs a bulk copy of StringPoints into the internal slice. -// This is a more efficient version of calling AggregateString on each point. -func (r *StringSliceFuncFloatReducer) AggregateStringBulk(points []StringPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *StringSliceFuncFloatReducer) Emit() []FloatPoint { - return r.fn(r.points) -} - -// StringReduceIntegerFunc is the function called by a StringPoint reducer. -type StringReduceIntegerFunc func(prev *IntegerPoint, curr *StringPoint) (t int64, v int64, aux []interface{}) - -// StringFuncIntegerReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type StringFuncIntegerReducer struct { - prev *IntegerPoint - fn StringReduceIntegerFunc -} - -// NewStringFuncIntegerReducer creates a new StringFuncIntegerReducer. -func NewStringFuncIntegerReducer(fn StringReduceIntegerFunc, prev *IntegerPoint) *StringFuncIntegerReducer { - return &StringFuncIntegerReducer{fn: fn, prev: prev} -} - -// AggregateString takes a StringPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *StringFuncIntegerReducer) AggregateString(p *StringPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &IntegerPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateString. -func (r *StringFuncIntegerReducer) Emit() []IntegerPoint { - return []IntegerPoint{*r.prev} -} - -// StringReduceIntegerSliceFunc is the function called by a StringPoint reducer. -type StringReduceIntegerSliceFunc func(a []StringPoint) []IntegerPoint - -// StringSliceFuncIntegerReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type StringSliceFuncIntegerReducer struct { - points []StringPoint - fn StringReduceIntegerSliceFunc -} - -// NewStringSliceFuncIntegerReducer creates a new StringSliceFuncIntegerReducer. -func NewStringSliceFuncIntegerReducer(fn StringReduceIntegerSliceFunc) *StringSliceFuncIntegerReducer { - return &StringSliceFuncIntegerReducer{fn: fn} -} - -// AggregateString copies the StringPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *StringSliceFuncIntegerReducer) AggregateString(p *StringPoint) { - r.points = append(r.points, *p) -} - -// AggregateStringBulk performs a bulk copy of StringPoints into the internal slice. -// This is a more efficient version of calling AggregateString on each point. -func (r *StringSliceFuncIntegerReducer) AggregateStringBulk(points []StringPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *StringSliceFuncIntegerReducer) Emit() []IntegerPoint { - return r.fn(r.points) -} - -// StringReduceFunc is the function called by a StringPoint reducer. -type StringReduceFunc func(prev *StringPoint, curr *StringPoint) (t int64, v string, aux []interface{}) - -// StringFuncReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type StringFuncReducer struct { - prev *StringPoint - fn StringReduceFunc -} - -// NewStringFuncReducer creates a new StringFuncStringReducer. -func NewStringFuncReducer(fn StringReduceFunc, prev *StringPoint) *StringFuncReducer { - return &StringFuncReducer{fn: fn, prev: prev} -} - -// AggregateString takes a StringPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *StringFuncReducer) AggregateString(p *StringPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &StringPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateString. -func (r *StringFuncReducer) Emit() []StringPoint { - return []StringPoint{*r.prev} -} - -// StringReduceSliceFunc is the function called by a StringPoint reducer. -type StringReduceSliceFunc func(a []StringPoint) []StringPoint - -// StringSliceFuncReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type StringSliceFuncReducer struct { - points []StringPoint - fn StringReduceSliceFunc -} - -// NewStringSliceFuncReducer creates a new StringSliceFuncReducer. -func NewStringSliceFuncReducer(fn StringReduceSliceFunc) *StringSliceFuncReducer { - return &StringSliceFuncReducer{fn: fn} -} - -// AggregateString copies the StringPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *StringSliceFuncReducer) AggregateString(p *StringPoint) { - r.points = append(r.points, *p) -} - -// AggregateStringBulk performs a bulk copy of StringPoints into the internal slice. -// This is a more efficient version of calling AggregateString on each point. -func (r *StringSliceFuncReducer) AggregateStringBulk(points []StringPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *StringSliceFuncReducer) Emit() []StringPoint { - return r.fn(r.points) -} - -// StringReduceBooleanFunc is the function called by a StringPoint reducer. -type StringReduceBooleanFunc func(prev *BooleanPoint, curr *StringPoint) (t int64, v bool, aux []interface{}) - -// StringFuncBooleanReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type StringFuncBooleanReducer struct { - prev *BooleanPoint - fn StringReduceBooleanFunc -} - -// NewStringFuncBooleanReducer creates a new StringFuncBooleanReducer. -func NewStringFuncBooleanReducer(fn StringReduceBooleanFunc, prev *BooleanPoint) *StringFuncBooleanReducer { - return &StringFuncBooleanReducer{fn: fn, prev: prev} -} - -// AggregateString takes a StringPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *StringFuncBooleanReducer) AggregateString(p *StringPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &BooleanPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateString. -func (r *StringFuncBooleanReducer) Emit() []BooleanPoint { - return []BooleanPoint{*r.prev} -} - -// StringReduceBooleanSliceFunc is the function called by a StringPoint reducer. -type StringReduceBooleanSliceFunc func(a []StringPoint) []BooleanPoint - -// StringSliceFuncBooleanReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type StringSliceFuncBooleanReducer struct { - points []StringPoint - fn StringReduceBooleanSliceFunc -} - -// NewStringSliceFuncBooleanReducer creates a new StringSliceFuncBooleanReducer. -func NewStringSliceFuncBooleanReducer(fn StringReduceBooleanSliceFunc) *StringSliceFuncBooleanReducer { - return &StringSliceFuncBooleanReducer{fn: fn} -} - -// AggregateString copies the StringPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *StringSliceFuncBooleanReducer) AggregateString(p *StringPoint) { - r.points = append(r.points, *p) -} - -// AggregateStringBulk performs a bulk copy of StringPoints into the internal slice. -// This is a more efficient version of calling AggregateString on each point. -func (r *StringSliceFuncBooleanReducer) AggregateStringBulk(points []StringPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *StringSliceFuncBooleanReducer) Emit() []BooleanPoint { - return r.fn(r.points) -} - -// StringDistinctReducer returns the distinct points in a series. -type StringDistinctReducer struct { - m map[string]StringPoint -} - -// NewStringDistinctReducer creates a new StringDistinctReducer. -func NewStringDistinctReducer() *StringDistinctReducer { - return &StringDistinctReducer{m: make(map[string]StringPoint)} -} - -// AggregateString aggregates a point into the reducer. -func (r *StringDistinctReducer) AggregateString(p *StringPoint) { - if _, ok := r.m[p.Value]; !ok { - r.m[p.Value] = *p - } -} - -// Emit emits the distinct points that have been aggregated into the reducer. -func (r *StringDistinctReducer) Emit() []StringPoint { - points := make([]StringPoint, 0, len(r.m)) - for _, p := range r.m { - points = append(points, StringPoint{Time: p.Time, Value: p.Value}) - } - sort.Sort(stringPoints(points)) - return points -} - -// StringElapsedReducer calculates the elapsed of the aggregated points. -type StringElapsedReducer struct { - unitConversion int64 - prev StringPoint - curr StringPoint -} - -// NewStringElapsedReducer creates a new StringElapsedReducer. -func NewStringElapsedReducer(interval Interval) *StringElapsedReducer { - return &StringElapsedReducer{ - unitConversion: int64(interval.Duration), - prev: StringPoint{Nil: true}, - curr: StringPoint{Nil: true}, - } -} - -// AggregateString aggregates a point into the reducer and updates the current window. -func (r *StringElapsedReducer) AggregateString(p *StringPoint) { - r.prev = r.curr - r.curr = *p -} - -// Emit emits the elapsed of the reducer at the current point. -func (r *StringElapsedReducer) Emit() []IntegerPoint { - if !r.prev.Nil { - elapsed := (r.curr.Time - r.prev.Time) / r.unitConversion - return []IntegerPoint{ - {Time: r.curr.Time, Value: elapsed}, - } - } - return nil -} - -// StringSampleReduces implements a reservoir sampling to calculate a random subset of points -type StringSampleReducer struct { - count int // how many points we've iterated over - rng *rand.Rand // random number generator for each reducer - - points stringPoints // the reservoir -} - -// NewStringSampleReducer creates a new StringSampleReducer -func NewStringSampleReducer(size int) *StringSampleReducer { - return &StringSampleReducer{ - rng: rand.New(rand.NewSource(time.Now().UnixNano())), // seed with current time as suggested by https://golang.org/pkg/math/rand/ - points: make(stringPoints, size), - } -} - -// AggregateString aggregates a point into the reducer. -func (r *StringSampleReducer) AggregateString(p *StringPoint) { - r.count++ - // Fill the reservoir with the first n points - if r.count-1 < len(r.points) { - r.points[r.count-1] = *p - return - } - - // Generate a random integer between 1 and the count and - // if that number is less than the length of the slice - // replace the point at that index rnd with p. - rnd := r.rng.Intn(r.count) - if rnd < len(r.points) { - r.points[rnd] = *p - } -} - -// Emit emits the reservoir sample as many points. -func (r *StringSampleReducer) Emit() []StringPoint { - min := len(r.points) - if r.count < min { - min = r.count - } - pts := r.points[:min] - sort.Sort(pts) - return pts -} - -// BooleanPointAggregator aggregates points to produce a single point. -type BooleanPointAggregator interface { - AggregateBoolean(p *BooleanPoint) -} - -// BooleanBulkPointAggregator aggregates multiple points at a time. -type BooleanBulkPointAggregator interface { - AggregateBooleanBulk(points []BooleanPoint) -} - -// AggregateBooleanPoints feeds a slice of BooleanPoint into an -// aggregator. If the aggregator is a BooleanBulkPointAggregator, it will -// use the AggregateBulk method. -func AggregateBooleanPoints(a BooleanPointAggregator, points []BooleanPoint) { - switch a := a.(type) { - case BooleanBulkPointAggregator: - a.AggregateBooleanBulk(points) - default: - for _, p := range points { - a.AggregateBoolean(&p) - } - } -} - -// BooleanPointEmitter produces a single point from an aggregate. -type BooleanPointEmitter interface { - Emit() []BooleanPoint -} - -// BooleanReduceFloatFunc is the function called by a BooleanPoint reducer. -type BooleanReduceFloatFunc func(prev *FloatPoint, curr *BooleanPoint) (t int64, v float64, aux []interface{}) - -// BooleanFuncFloatReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type BooleanFuncFloatReducer struct { - prev *FloatPoint - fn BooleanReduceFloatFunc -} - -// NewBooleanFuncFloatReducer creates a new BooleanFuncFloatReducer. -func NewBooleanFuncFloatReducer(fn BooleanReduceFloatFunc, prev *FloatPoint) *BooleanFuncFloatReducer { - return &BooleanFuncFloatReducer{fn: fn, prev: prev} -} - -// AggregateBoolean takes a BooleanPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *BooleanFuncFloatReducer) AggregateBoolean(p *BooleanPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &FloatPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateBoolean. -func (r *BooleanFuncFloatReducer) Emit() []FloatPoint { - return []FloatPoint{*r.prev} -} - -// BooleanReduceFloatSliceFunc is the function called by a BooleanPoint reducer. -type BooleanReduceFloatSliceFunc func(a []BooleanPoint) []FloatPoint - -// BooleanSliceFuncFloatReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type BooleanSliceFuncFloatReducer struct { - points []BooleanPoint - fn BooleanReduceFloatSliceFunc -} - -// NewBooleanSliceFuncFloatReducer creates a new BooleanSliceFuncFloatReducer. -func NewBooleanSliceFuncFloatReducer(fn BooleanReduceFloatSliceFunc) *BooleanSliceFuncFloatReducer { - return &BooleanSliceFuncFloatReducer{fn: fn} -} - -// AggregateBoolean copies the BooleanPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *BooleanSliceFuncFloatReducer) AggregateBoolean(p *BooleanPoint) { - r.points = append(r.points, *p) -} - -// AggregateBooleanBulk performs a bulk copy of BooleanPoints into the internal slice. -// This is a more efficient version of calling AggregateBoolean on each point. -func (r *BooleanSliceFuncFloatReducer) AggregateBooleanBulk(points []BooleanPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *BooleanSliceFuncFloatReducer) Emit() []FloatPoint { - return r.fn(r.points) -} - -// BooleanReduceIntegerFunc is the function called by a BooleanPoint reducer. -type BooleanReduceIntegerFunc func(prev *IntegerPoint, curr *BooleanPoint) (t int64, v int64, aux []interface{}) - -// BooleanFuncIntegerReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type BooleanFuncIntegerReducer struct { - prev *IntegerPoint - fn BooleanReduceIntegerFunc -} - -// NewBooleanFuncIntegerReducer creates a new BooleanFuncIntegerReducer. -func NewBooleanFuncIntegerReducer(fn BooleanReduceIntegerFunc, prev *IntegerPoint) *BooleanFuncIntegerReducer { - return &BooleanFuncIntegerReducer{fn: fn, prev: prev} -} - -// AggregateBoolean takes a BooleanPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *BooleanFuncIntegerReducer) AggregateBoolean(p *BooleanPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &IntegerPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateBoolean. -func (r *BooleanFuncIntegerReducer) Emit() []IntegerPoint { - return []IntegerPoint{*r.prev} -} - -// BooleanReduceIntegerSliceFunc is the function called by a BooleanPoint reducer. -type BooleanReduceIntegerSliceFunc func(a []BooleanPoint) []IntegerPoint - -// BooleanSliceFuncIntegerReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type BooleanSliceFuncIntegerReducer struct { - points []BooleanPoint - fn BooleanReduceIntegerSliceFunc -} - -// NewBooleanSliceFuncIntegerReducer creates a new BooleanSliceFuncIntegerReducer. -func NewBooleanSliceFuncIntegerReducer(fn BooleanReduceIntegerSliceFunc) *BooleanSliceFuncIntegerReducer { - return &BooleanSliceFuncIntegerReducer{fn: fn} -} - -// AggregateBoolean copies the BooleanPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *BooleanSliceFuncIntegerReducer) AggregateBoolean(p *BooleanPoint) { - r.points = append(r.points, *p) -} - -// AggregateBooleanBulk performs a bulk copy of BooleanPoints into the internal slice. -// This is a more efficient version of calling AggregateBoolean on each point. -func (r *BooleanSliceFuncIntegerReducer) AggregateBooleanBulk(points []BooleanPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *BooleanSliceFuncIntegerReducer) Emit() []IntegerPoint { - return r.fn(r.points) -} - -// BooleanReduceStringFunc is the function called by a BooleanPoint reducer. -type BooleanReduceStringFunc func(prev *StringPoint, curr *BooleanPoint) (t int64, v string, aux []interface{}) - -// BooleanFuncStringReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type BooleanFuncStringReducer struct { - prev *StringPoint - fn BooleanReduceStringFunc -} - -// NewBooleanFuncStringReducer creates a new BooleanFuncStringReducer. -func NewBooleanFuncStringReducer(fn BooleanReduceStringFunc, prev *StringPoint) *BooleanFuncStringReducer { - return &BooleanFuncStringReducer{fn: fn, prev: prev} -} - -// AggregateBoolean takes a BooleanPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *BooleanFuncStringReducer) AggregateBoolean(p *BooleanPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &StringPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateBoolean. -func (r *BooleanFuncStringReducer) Emit() []StringPoint { - return []StringPoint{*r.prev} -} - -// BooleanReduceStringSliceFunc is the function called by a BooleanPoint reducer. -type BooleanReduceStringSliceFunc func(a []BooleanPoint) []StringPoint - -// BooleanSliceFuncStringReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type BooleanSliceFuncStringReducer struct { - points []BooleanPoint - fn BooleanReduceStringSliceFunc -} - -// NewBooleanSliceFuncStringReducer creates a new BooleanSliceFuncStringReducer. -func NewBooleanSliceFuncStringReducer(fn BooleanReduceStringSliceFunc) *BooleanSliceFuncStringReducer { - return &BooleanSliceFuncStringReducer{fn: fn} -} - -// AggregateBoolean copies the BooleanPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *BooleanSliceFuncStringReducer) AggregateBoolean(p *BooleanPoint) { - r.points = append(r.points, *p) -} - -// AggregateBooleanBulk performs a bulk copy of BooleanPoints into the internal slice. -// This is a more efficient version of calling AggregateBoolean on each point. -func (r *BooleanSliceFuncStringReducer) AggregateBooleanBulk(points []BooleanPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *BooleanSliceFuncStringReducer) Emit() []StringPoint { - return r.fn(r.points) -} - -// BooleanReduceFunc is the function called by a BooleanPoint reducer. -type BooleanReduceFunc func(prev *BooleanPoint, curr *BooleanPoint) (t int64, v bool, aux []interface{}) - -// BooleanFuncReducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type BooleanFuncReducer struct { - prev *BooleanPoint - fn BooleanReduceFunc -} - -// NewBooleanFuncReducer creates a new BooleanFuncBooleanReducer. -func NewBooleanFuncReducer(fn BooleanReduceFunc, prev *BooleanPoint) *BooleanFuncReducer { - return &BooleanFuncReducer{fn: fn, prev: prev} -} - -// AggregateBoolean takes a BooleanPoint and invokes the reduce function with the -// current and new point to modify the current point. -func (r *BooleanFuncReducer) AggregateBoolean(p *BooleanPoint) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &BooleanPoint{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with AggregateBoolean. -func (r *BooleanFuncReducer) Emit() []BooleanPoint { - return []BooleanPoint{*r.prev} -} - -// BooleanReduceSliceFunc is the function called by a BooleanPoint reducer. -type BooleanReduceSliceFunc func(a []BooleanPoint) []BooleanPoint - -// BooleanSliceFuncReducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type BooleanSliceFuncReducer struct { - points []BooleanPoint - fn BooleanReduceSliceFunc -} - -// NewBooleanSliceFuncReducer creates a new BooleanSliceFuncReducer. -func NewBooleanSliceFuncReducer(fn BooleanReduceSliceFunc) *BooleanSliceFuncReducer { - return &BooleanSliceFuncReducer{fn: fn} -} - -// AggregateBoolean copies the BooleanPoint into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *BooleanSliceFuncReducer) AggregateBoolean(p *BooleanPoint) { - r.points = append(r.points, *p) -} - -// AggregateBooleanBulk performs a bulk copy of BooleanPoints into the internal slice. -// This is a more efficient version of calling AggregateBoolean on each point. -func (r *BooleanSliceFuncReducer) AggregateBooleanBulk(points []BooleanPoint) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *BooleanSliceFuncReducer) Emit() []BooleanPoint { - return r.fn(r.points) -} - -// BooleanDistinctReducer returns the distinct points in a series. -type BooleanDistinctReducer struct { - m map[bool]BooleanPoint -} - -// NewBooleanDistinctReducer creates a new BooleanDistinctReducer. -func NewBooleanDistinctReducer() *BooleanDistinctReducer { - return &BooleanDistinctReducer{m: make(map[bool]BooleanPoint)} -} - -// AggregateBoolean aggregates a point into the reducer. -func (r *BooleanDistinctReducer) AggregateBoolean(p *BooleanPoint) { - if _, ok := r.m[p.Value]; !ok { - r.m[p.Value] = *p - } -} - -// Emit emits the distinct points that have been aggregated into the reducer. -func (r *BooleanDistinctReducer) Emit() []BooleanPoint { - points := make([]BooleanPoint, 0, len(r.m)) - for _, p := range r.m { - points = append(points, BooleanPoint{Time: p.Time, Value: p.Value}) - } - sort.Sort(booleanPoints(points)) - return points -} - -// BooleanElapsedReducer calculates the elapsed of the aggregated points. -type BooleanElapsedReducer struct { - unitConversion int64 - prev BooleanPoint - curr BooleanPoint -} - -// NewBooleanElapsedReducer creates a new BooleanElapsedReducer. -func NewBooleanElapsedReducer(interval Interval) *BooleanElapsedReducer { - return &BooleanElapsedReducer{ - unitConversion: int64(interval.Duration), - prev: BooleanPoint{Nil: true}, - curr: BooleanPoint{Nil: true}, - } -} - -// AggregateBoolean aggregates a point into the reducer and updates the current window. -func (r *BooleanElapsedReducer) AggregateBoolean(p *BooleanPoint) { - r.prev = r.curr - r.curr = *p -} - -// Emit emits the elapsed of the reducer at the current point. -func (r *BooleanElapsedReducer) Emit() []IntegerPoint { - if !r.prev.Nil { - elapsed := (r.curr.Time - r.prev.Time) / r.unitConversion - return []IntegerPoint{ - {Time: r.curr.Time, Value: elapsed}, - } - } - return nil -} - -// BooleanSampleReduces implements a reservoir sampling to calculate a random subset of points -type BooleanSampleReducer struct { - count int // how many points we've iterated over - rng *rand.Rand // random number generator for each reducer - - points booleanPoints // the reservoir -} - -// NewBooleanSampleReducer creates a new BooleanSampleReducer -func NewBooleanSampleReducer(size int) *BooleanSampleReducer { - return &BooleanSampleReducer{ - rng: rand.New(rand.NewSource(time.Now().UnixNano())), // seed with current time as suggested by https://golang.org/pkg/math/rand/ - points: make(booleanPoints, size), - } -} - -// AggregateBoolean aggregates a point into the reducer. -func (r *BooleanSampleReducer) AggregateBoolean(p *BooleanPoint) { - r.count++ - // Fill the reservoir with the first n points - if r.count-1 < len(r.points) { - r.points[r.count-1] = *p - return - } - - // Generate a random integer between 1 and the count and - // if that number is less than the length of the slice - // replace the point at that index rnd with p. - rnd := r.rng.Intn(r.count) - if rnd < len(r.points) { - r.points[rnd] = *p - } -} - -// Emit emits the reservoir sample as many points. -func (r *BooleanSampleReducer) Emit() []BooleanPoint { - min := len(r.points) - if r.count < min { - min = r.count - } - pts := r.points[:min] - sort.Sort(pts) - return pts -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/functions.gen.go.tmpl b/vendor/github.com/influxdata/influxdb/influxql/functions.gen.go.tmpl deleted file mode 100644 index 2b9a2448..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/functions.gen.go.tmpl +++ /dev/null @@ -1,219 +0,0 @@ -package influxql - -import ( -"sort" -"time" -"math/rand" -) - -{{with $types := .}}{{range $k := $types}} - -// {{$k.Name}}PointAggregator aggregates points to produce a single point. -type {{$k.Name}}PointAggregator interface { - Aggregate{{$k.Name}}(p *{{$k.Name}}Point) -} - -// {{$k.Name}}BulkPointAggregator aggregates multiple points at a time. -type {{$k.Name}}BulkPointAggregator interface { - Aggregate{{$k.Name}}Bulk(points []{{$k.Name}}Point) -} - -// Aggregate{{$k.Name}}Points feeds a slice of {{$k.Name}}Point into an -// aggregator. If the aggregator is a {{$k.Name}}BulkPointAggregator, it will -// use the AggregateBulk method. -func Aggregate{{$k.Name}}Points(a {{$k.Name}}PointAggregator, points []{{$k.Name}}Point) { - switch a := a.(type) { - case {{$k.Name}}BulkPointAggregator: - a.Aggregate{{$k.Name}}Bulk(points) - default: - for _, p := range points { - a.Aggregate{{$k.Name}}(&p) - } - } -} - -// {{$k.Name}}PointEmitter produces a single point from an aggregate. -type {{$k.Name}}PointEmitter interface { - Emit() []{{$k.Name}}Point -} - -{{range $v := $types}} - -// {{$k.Name}}Reduce{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Func is the function called by a {{$k.Name}}Point reducer. -type {{$k.Name}}Reduce{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Func func(prev *{{$v.Name}}Point, curr *{{$k.Name}}Point) (t int64, v {{$v.Type}}, aux []interface{}) - -// {{$k.Name}}Func{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer is a reducer that reduces -// the passed in points to a single point using a reduce function. -type {{$k.Name}}Func{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer struct { - prev *{{$v.Name}}Point - fn {{$k.Name}}Reduce{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Func -} - -// New{{$k.Name}}Func{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer creates a new {{$k.Name}}Func{{$v.Name}}Reducer. -func New{{$k.Name}}Func{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer(fn {{$k.Name}}Reduce{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Func, prev *{{$v.Name}}Point) *{{$k.Name}}Func{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer { - return &{{$k.Name}}Func{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer{fn: fn, prev: prev} -} - -// Aggregate{{$k.Name}} takes a {{$k.Name}}Point and invokes the reduce function with the -// current and new point to modify the current point. -func (r *{{$k.Name}}Func{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer) Aggregate{{$k.Name}}(p *{{$k.Name}}Point) { - t, v, aux := r.fn(r.prev, p) - if r.prev == nil { - r.prev = &{{$v.Name}}Point{} - } - r.prev.Time = t - r.prev.Value = v - r.prev.Aux = aux - if p.Aggregated > 1 { - r.prev.Aggregated += p.Aggregated - } else { - r.prev.Aggregated++ - } -} - -// Emit emits the point that was generated when reducing the points fed in with Aggregate{{$k.Name}}. -func (r *{{$k.Name}}Func{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer) Emit() []{{$v.Name}}Point { - return []{{$v.Name}}Point{*r.prev} -} - -// {{$k.Name}}Reduce{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}SliceFunc is the function called by a {{$k.Name}}Point reducer. -type {{$k.Name}}Reduce{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}SliceFunc func(a []{{$k.Name}}Point) []{{$v.Name}}Point - -// {{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer is a reducer that aggregates -// the passed in points and then invokes the function to reduce the points when they are emitted. -type {{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer struct { - points []{{$k.Name}}Point - fn {{$k.Name}}Reduce{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}SliceFunc -} - -// New{{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer creates a new {{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer. -func New{{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer(fn {{$k.Name}}Reduce{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}SliceFunc) *{{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer { - return &{{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer{fn: fn} -} - -// Aggregate{{$k.Name}} copies the {{$k.Name}}Point into the internal slice to be passed -// to the reduce function when Emit is called. -func (r *{{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer) Aggregate{{$k.Name}}(p *{{$k.Name}}Point) { - r.points = append(r.points, *p) -} - -// Aggregate{{$k.Name}}Bulk performs a bulk copy of {{$k.Name}}Points into the internal slice. -// This is a more efficient version of calling Aggregate{{$k.Name}} on each point. -func (r *{{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer) Aggregate{{$k.Name}}Bulk(points []{{$k.Name}}Point) { - r.points = append(r.points, points...) -} - -// Emit invokes the reduce function on the aggregated points to generate the aggregated points. -// This method does not clear the points from the internal slice. -func (r *{{$k.Name}}SliceFunc{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}Reducer) Emit() []{{$v.Name}}Point { - return r.fn(r.points) -} -{{end}} - -// {{$k.Name}}DistinctReducer returns the distinct points in a series. -type {{$k.Name}}DistinctReducer struct { - m map[{{$k.Type}}]{{$k.Name}}Point -} - -// New{{$k.Name}}DistinctReducer creates a new {{$k.Name}}DistinctReducer. -func New{{$k.Name}}DistinctReducer() *{{$k.Name}}DistinctReducer { - return &{{$k.Name}}DistinctReducer{m: make(map[{{$k.Type}}]{{$k.Name}}Point)} -} - -// Aggregate{{$k.Name}} aggregates a point into the reducer. -func (r *{{$k.Name}}DistinctReducer) Aggregate{{$k.Name}}(p *{{$k.Name}}Point) { - if _, ok := r.m[p.Value]; !ok { - r.m[p.Value] = *p - } -} - -// Emit emits the distinct points that have been aggregated into the reducer. -func (r *{{$k.Name}}DistinctReducer) Emit() []{{$k.Name}}Point { - points := make([]{{$k.Name}}Point, 0, len(r.m)) - for _, p := range r.m { - points = append(points, {{$k.Name}}Point{Time: p.Time, Value: p.Value}) - } - sort.Sort({{$k.name}}Points(points)) - return points -} - -// {{$k.Name}}ElapsedReducer calculates the elapsed of the aggregated points. -type {{$k.Name}}ElapsedReducer struct { - unitConversion int64 - prev {{$k.Name}}Point - curr {{$k.Name}}Point -} - -// New{{$k.Name}}ElapsedReducer creates a new {{$k.Name}}ElapsedReducer. -func New{{$k.Name}}ElapsedReducer(interval Interval) *{{$k.Name}}ElapsedReducer { - return &{{$k.Name}}ElapsedReducer{ - unitConversion: int64(interval.Duration), - prev: {{$k.Name}}Point{Nil: true}, - curr: {{$k.Name}}Point{Nil: true}, - } -} - -// Aggregate{{$k.Name}} aggregates a point into the reducer and updates the current window. -func (r *{{$k.Name}}ElapsedReducer) Aggregate{{$k.Name}}(p *{{$k.Name}}Point) { - r.prev = r.curr - r.curr = *p -} - -// Emit emits the elapsed of the reducer at the current point. -func (r *{{$k.Name}}ElapsedReducer) Emit() []IntegerPoint { - if !r.prev.Nil { - elapsed := (r.curr.Time - r.prev.Time) / r.unitConversion - return []IntegerPoint{ - {Time: r.curr.Time, Value: elapsed}, - } - } - return nil -} - -// {{$k.Name}}SampleReduces implements a reservoir sampling to calculate a random subset of points -type {{$k.Name}}SampleReducer struct { - count int // how many points we've iterated over - rng *rand.Rand // random number generator for each reducer - - points {{$k.name}}Points // the reservoir -} - -// New{{$k.Name}}SampleReducer creates a new {{$k.Name}}SampleReducer -func New{{$k.Name}}SampleReducer(size int) *{{$k.Name}}SampleReducer { - return &{{$k.Name}}SampleReducer{ - rng: rand.New(rand.NewSource(time.Now().UnixNano())), // seed with current time as suggested by https://golang.org/pkg/math/rand/ - points: make({{$k.name}}Points, size), - } -} - -// Aggregate{{$k.Name}} aggregates a point into the reducer. -func (r *{{$k.Name}}SampleReducer) Aggregate{{$k.Name}}(p *{{$k.Name}}Point) { - r.count++ - // Fill the reservoir with the first n points - if r.count-1 < len(r.points) { - r.points[r.count-1] = *p - return - } - - // Generate a random integer between 1 and the count and - // if that number is less than the length of the slice - // replace the point at that index rnd with p. - rnd := r.rng.Intn(r.count) - if rnd < len(r.points) { - r.points[rnd] = *p - } -} - -// Emit emits the reservoir sample as many points. -func (r *{{$k.Name}}SampleReducer) Emit() []{{$k.Name}}Point { - min := len(r.points) - if r.count < min { - min = r.count - } - pts := r.points[:min] - sort.Sort(pts) - return pts -} - - -{{end}}{{end}} diff --git a/vendor/github.com/influxdata/influxdb/influxql/functions.go b/vendor/github.com/influxdata/influxdb/influxql/functions.go deleted file mode 100644 index f48a400a..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/functions.go +++ /dev/null @@ -1,759 +0,0 @@ -package influxql - -import ( - "math" - "time" - - "github.com/influxdata/influxdb/influxql/neldermead" -) - -// FloatMeanReducer calculates the mean of the aggregated points. -type FloatMeanReducer struct { - sum float64 - count uint32 -} - -// NewFloatMeanReducer creates a new FloatMeanReducer. -func NewFloatMeanReducer() *FloatMeanReducer { - return &FloatMeanReducer{} -} - -// AggregateFloat aggregates a point into the reducer. -func (r *FloatMeanReducer) AggregateFloat(p *FloatPoint) { - if p.Aggregated >= 2 { - r.sum += p.Value * float64(p.Aggregated) - r.count += p.Aggregated - } else { - r.sum += p.Value - r.count++ - } -} - -// Emit emits the mean of the aggregated points as a single point. -func (r *FloatMeanReducer) Emit() []FloatPoint { - return []FloatPoint{{ - Time: ZeroTime, - Value: r.sum / float64(r.count), - Aggregated: r.count, - }} -} - -// IntegerMeanReducer calculates the mean of the aggregated points. -type IntegerMeanReducer struct { - sum int64 - count uint32 -} - -// NewIntegerMeanReducer creates a new IntegerMeanReducer. -func NewIntegerMeanReducer() *IntegerMeanReducer { - return &IntegerMeanReducer{} -} - -// AggregateInteger aggregates a point into the reducer. -func (r *IntegerMeanReducer) AggregateInteger(p *IntegerPoint) { - if p.Aggregated >= 2 { - r.sum += p.Value * int64(p.Aggregated) - r.count += p.Aggregated - } else { - r.sum += p.Value - r.count++ - } -} - -// Emit emits the mean of the aggregated points as a single point. -func (r *IntegerMeanReducer) Emit() []FloatPoint { - return []FloatPoint{{ - Time: ZeroTime, - Value: float64(r.sum) / float64(r.count), - Aggregated: r.count, - }} -} - -// FloatDerivativeReducer calculates the derivative of the aggregated points. -type FloatDerivativeReducer struct { - interval Interval - prev FloatPoint - curr FloatPoint - isNonNegative bool - ascending bool -} - -// NewFloatDerivativeReducer creates a new FloatDerivativeReducer. -func NewFloatDerivativeReducer(interval Interval, isNonNegative, ascending bool) *FloatDerivativeReducer { - return &FloatDerivativeReducer{ - interval: interval, - isNonNegative: isNonNegative, - ascending: ascending, - prev: FloatPoint{Nil: true}, - curr: FloatPoint{Nil: true}, - } -} - -// AggregateFloat aggregates a point into the reducer and updates the current window. -func (r *FloatDerivativeReducer) AggregateFloat(p *FloatPoint) { - // Skip past a point when it does not advance the stream. A joined series - // may have multiple points at the same time so we will discard anything - // except the first point we encounter. - if !r.curr.Nil && r.curr.Time == p.Time { - return - } - - r.prev = r.curr - r.curr = *p -} - -// Emit emits the derivative of the reducer at the current point. -func (r *FloatDerivativeReducer) Emit() []FloatPoint { - if !r.prev.Nil { - // Calculate the derivative of successive points by dividing the - // difference of each value by the elapsed time normalized to the interval. - diff := r.curr.Value - r.prev.Value - elapsed := r.curr.Time - r.prev.Time - if !r.ascending { - elapsed = -elapsed - } - value := diff / (float64(elapsed) / float64(r.interval.Duration)) - - // Mark this point as read by changing the previous point to nil. - r.prev.Nil = true - - // Drop negative values for non-negative derivatives. - if r.isNonNegative && diff < 0 { - return nil - } - return []FloatPoint{{Time: r.curr.Time, Value: value}} - } - return nil -} - -// IntegerDerivativeReducer calculates the derivative of the aggregated points. -type IntegerDerivativeReducer struct { - interval Interval - prev IntegerPoint - curr IntegerPoint - isNonNegative bool - ascending bool -} - -// NewIntegerDerivativeReducer creates a new IntegerDerivativeReducer. -func NewIntegerDerivativeReducer(interval Interval, isNonNegative, ascending bool) *IntegerDerivativeReducer { - return &IntegerDerivativeReducer{ - interval: interval, - isNonNegative: isNonNegative, - ascending: ascending, - prev: IntegerPoint{Nil: true}, - curr: IntegerPoint{Nil: true}, - } -} - -// AggregateInteger aggregates a point into the reducer and updates the current window. -func (r *IntegerDerivativeReducer) AggregateInteger(p *IntegerPoint) { - // Skip past a point when it does not advance the stream. A joined series - // may have multiple points at the same time so we will discard anything - // except the first point we encounter. - if !r.curr.Nil && r.curr.Time == p.Time { - return - } - - r.prev = r.curr - r.curr = *p -} - -// Emit emits the derivative of the reducer at the current point. -func (r *IntegerDerivativeReducer) Emit() []FloatPoint { - if !r.prev.Nil { - // Calculate the derivative of successive points by dividing the - // difference of each value by the elapsed time normalized to the interval. - diff := float64(r.curr.Value - r.prev.Value) - elapsed := r.curr.Time - r.prev.Time - if !r.ascending { - elapsed = -elapsed - } - value := diff / (float64(elapsed) / float64(r.interval.Duration)) - - // Mark this point as read by changing the previous point to nil. - r.prev.Nil = true - - // Drop negative values for non-negative derivatives. - if r.isNonNegative && diff < 0 { - return nil - } - return []FloatPoint{{Time: r.curr.Time, Value: value}} - } - return nil -} - -// FloatDifferenceReducer calculates the derivative of the aggregated points. -type FloatDifferenceReducer struct { - prev FloatPoint - curr FloatPoint -} - -// NewFloatDifferenceReducer creates a new FloatDifferenceReducer. -func NewFloatDifferenceReducer() *FloatDifferenceReducer { - return &FloatDifferenceReducer{ - prev: FloatPoint{Nil: true}, - curr: FloatPoint{Nil: true}, - } -} - -// AggregateFloat aggregates a point into the reducer and updates the current window. -func (r *FloatDifferenceReducer) AggregateFloat(p *FloatPoint) { - // Skip past a point when it does not advance the stream. A joined series - // may have multiple points at the same time so we will discard anything - // except the first point we encounter. - if !r.curr.Nil && r.curr.Time == p.Time { - return - } - - r.prev = r.curr - r.curr = *p -} - -// Emit emits the difference of the reducer at the current point. -func (r *FloatDifferenceReducer) Emit() []FloatPoint { - if !r.prev.Nil { - // Calculate the difference of successive points. - value := r.curr.Value - r.prev.Value - - // Mark this point as read by changing the previous point to nil. - r.prev.Nil = true - return []FloatPoint{{Time: r.curr.Time, Value: value}} - } - return nil -} - -// IntegerDifferenceReducer calculates the derivative of the aggregated points. -type IntegerDifferenceReducer struct { - prev IntegerPoint - curr IntegerPoint -} - -// NewIntegerDifferenceReducer creates a new IntegerDifferenceReducer. -func NewIntegerDifferenceReducer() *IntegerDifferenceReducer { - return &IntegerDifferenceReducer{ - prev: IntegerPoint{Nil: true}, - curr: IntegerPoint{Nil: true}, - } -} - -// AggregateInteger aggregates a point into the reducer and updates the current window. -func (r *IntegerDifferenceReducer) AggregateInteger(p *IntegerPoint) { - // Skip past a point when it does not advance the stream. A joined series - // may have multiple points at the same time so we will discard anything - // except the first point we encounter. - if !r.curr.Nil && r.curr.Time == p.Time { - return - } - - r.prev = r.curr - r.curr = *p -} - -// Emit emits the difference of the reducer at the current point. -func (r *IntegerDifferenceReducer) Emit() []IntegerPoint { - if !r.prev.Nil { - // Calculate the difference of successive points. - value := r.curr.Value - r.prev.Value - - // Mark this point as read by changing the previous point to nil. - r.prev.Nil = true - return []IntegerPoint{{Time: r.curr.Time, Value: value}} - } - return nil -} - -// FloatMovingAverageReducer calculates the moving average of the aggregated points. -type FloatMovingAverageReducer struct { - pos int - sum float64 - time int64 - buf []float64 -} - -// NewFloatMovingAverageReducer creates a new FloatMovingAverageReducer. -func NewFloatMovingAverageReducer(n int) *FloatMovingAverageReducer { - return &FloatMovingAverageReducer{ - buf: make([]float64, 0, n), - } -} - -// AggregateFloat aggregates a point into the reducer and updates the current window. -func (r *FloatMovingAverageReducer) AggregateFloat(p *FloatPoint) { - if len(r.buf) != cap(r.buf) { - r.buf = append(r.buf, p.Value) - } else { - r.sum -= r.buf[r.pos] - r.buf[r.pos] = p.Value - } - r.sum += p.Value - r.time = p.Time - r.pos++ - if r.pos >= cap(r.buf) { - r.pos = 0 - } -} - -// Emit emits the moving average of the current window. Emit should be called -// after every call to AggregateFloat and it will produce one point if there -// is enough data to fill a window, otherwise it will produce zero points. -func (r *FloatMovingAverageReducer) Emit() []FloatPoint { - if len(r.buf) != cap(r.buf) { - return []FloatPoint{} - } - return []FloatPoint{ - { - Value: r.sum / float64(len(r.buf)), - Time: r.time, - Aggregated: uint32(len(r.buf)), - }, - } -} - -// IntegerMovingAverageReducer calculates the moving average of the aggregated points. -type IntegerMovingAverageReducer struct { - pos int - sum int64 - time int64 - buf []int64 -} - -// NewIntegerMovingAverageReducer creates a new IntegerMovingAverageReducer. -func NewIntegerMovingAverageReducer(n int) *IntegerMovingAverageReducer { - return &IntegerMovingAverageReducer{ - buf: make([]int64, 0, n), - } -} - -// AggregateInteger aggregates a point into the reducer and updates the current window. -func (r *IntegerMovingAverageReducer) AggregateInteger(p *IntegerPoint) { - if len(r.buf) != cap(r.buf) { - r.buf = append(r.buf, p.Value) - } else { - r.sum -= r.buf[r.pos] - r.buf[r.pos] = p.Value - } - r.sum += p.Value - r.time = p.Time - r.pos++ - if r.pos >= cap(r.buf) { - r.pos = 0 - } -} - -// Emit emits the moving average of the current window. Emit should be called -// after every call to AggregateInteger and it will produce one point if there -// is enough data to fill a window, otherwise it will produce zero points. -func (r *IntegerMovingAverageReducer) Emit() []FloatPoint { - if len(r.buf) != cap(r.buf) { - return []FloatPoint{} - } - return []FloatPoint{ - { - Value: float64(r.sum) / float64(len(r.buf)), - Time: r.time, - Aggregated: uint32(len(r.buf)), - }, - } -} - -// FloatCumulativeSumReducer cumulates the values from each point. -type FloatCumulativeSumReducer struct { - curr FloatPoint -} - -// NewFloatCumulativeSumReducer creates a new FloatCumulativeSumReducer. -func NewFloatCumulativeSumReducer() *FloatCumulativeSumReducer { - return &FloatCumulativeSumReducer{ - curr: FloatPoint{Nil: true}, - } -} - -func (r *FloatCumulativeSumReducer) AggregateFloat(p *FloatPoint) { - r.curr.Value += p.Value - r.curr.Time = p.Time - r.curr.Nil = false -} - -func (r *FloatCumulativeSumReducer) Emit() []FloatPoint { - var pts []FloatPoint - if !r.curr.Nil { - pts = []FloatPoint{r.curr} - } - return pts -} - -// IntegerCumulativeSumReducer cumulates the values from each point. -type IntegerCumulativeSumReducer struct { - curr IntegerPoint -} - -// NewIntegerCumulativeSumReducer creates a new IntegerCumulativeSumReducer. -func NewIntegerCumulativeSumReducer() *IntegerCumulativeSumReducer { - return &IntegerCumulativeSumReducer{ - curr: IntegerPoint{Nil: true}, - } -} - -func (r *IntegerCumulativeSumReducer) AggregateInteger(p *IntegerPoint) { - r.curr.Value += p.Value - r.curr.Time = p.Time - r.curr.Nil = false -} - -func (r *IntegerCumulativeSumReducer) Emit() []IntegerPoint { - var pts []IntegerPoint - if !r.curr.Nil { - pts = []IntegerPoint{r.curr} - } - return pts -} - -// FloatHoltWintersReducer forecasts a series into the future. -// This is done using the Holt-Winters damped method. -// 1. Using the series the initial values are calculated using a SSE. -// 2. The series is forecasted into the future using the iterative relations. -type FloatHoltWintersReducer struct { - // Season period - m int - seasonal bool - - // Horizon - h int - - // Interval between points - interval int64 - // interval / 2 -- used to perform rounding - halfInterval int64 - - // Whether to include all data or only future values - includeFitData bool - - // NelderMead optimizer - optim *neldermead.Optimizer - // Small difference bound for the optimizer - epsilon float64 - - y []float64 - points []FloatPoint -} - -const ( - // Arbitrary weight for initializing some intial guesses. - // This should be in the range [0,1] - hwWeight = 0.5 - // Epsilon value for the minimization process - hwDefaultEpsilon = 1.0e-4 - // Define a grid of initial guesses for the parameters: alpha, beta, gamma, and phi. - // Keep in mind that this grid is N^4 so we should keep N small - // The starting lower guess - hwGuessLower = 0.3 - // The upper bound on the grid - hwGuessUpper = 1.0 - // The step between guesses - hwGuessStep = 0.4 -) - -// NewFloatHoltWintersReducer creates a new FloatHoltWintersReducer. -func NewFloatHoltWintersReducer(h, m int, includeFitData bool, interval time.Duration) *FloatHoltWintersReducer { - seasonal := true - if m < 2 { - seasonal = false - } - return &FloatHoltWintersReducer{ - h: h, - m: m, - seasonal: seasonal, - includeFitData: includeFitData, - interval: int64(interval), - halfInterval: int64(interval) / 2, - optim: neldermead.New(), - epsilon: hwDefaultEpsilon, - } -} - -func (r *FloatHoltWintersReducer) aggregate(time int64, value float64) { - r.points = append(r.points, FloatPoint{ - Time: time, - Value: value, - }) -} - -// AggregateFloat aggregates a point into the reducer and updates the current window. -func (r *FloatHoltWintersReducer) AggregateFloat(p *FloatPoint) { - r.aggregate(p.Time, p.Value) -} - -// AggregateInteger aggregates a point into the reducer and updates the current window. -func (r *FloatHoltWintersReducer) AggregateInteger(p *IntegerPoint) { - r.aggregate(p.Time, float64(p.Value)) -} - -func (r *FloatHoltWintersReducer) roundTime(t int64) int64 { - // Overflow safe round function - remainder := t % r.interval - if remainder > r.halfInterval { - // Round up - return (t/r.interval + 1) * r.interval - } - // Round down - return (t / r.interval) * r.interval -} - -// Emit returns the points generated by the HoltWinters algorithm. -func (r *FloatHoltWintersReducer) Emit() []FloatPoint { - if l := len(r.points); l < 2 || r.seasonal && l < r.m || r.h <= 0 { - return nil - } - // First fill in r.y with values and NaNs for missing values - start, stop := r.roundTime(r.points[0].Time), r.roundTime(r.points[len(r.points)-1].Time) - count := (stop - start) / r.interval - if count <= 0 { - return nil - } - r.y = make([]float64, 1, count) - r.y[0] = r.points[0].Value - t := r.roundTime(r.points[0].Time) - for _, p := range r.points[1:] { - rounded := r.roundTime(p.Time) - if rounded <= t { - // Drop values that occur for the same time bucket - continue - } - t += r.interval - // Add any missing values before the next point - for rounded != t { - // Add in a NaN so we can skip it later. - r.y = append(r.y, math.NaN()) - t += r.interval - } - r.y = append(r.y, p.Value) - } - - // Seasonality - m := r.m - - // Starting guesses - // NOTE: Since these values are guesses - // in the cases where we were missing data, - // we can just skip the value and call it good. - - l0 := 0.0 - if r.seasonal { - for i := 0; i < m; i++ { - if !math.IsNaN(r.y[i]) { - l0 += (1 / float64(m)) * r.y[i] - } - } - } else { - l0 += hwWeight * r.y[0] - } - - b0 := 0.0 - if r.seasonal { - for i := 0; i < m && m+i < len(r.y); i++ { - if !math.IsNaN(r.y[i]) && !math.IsNaN(r.y[m+i]) { - b0 += 1 / float64(m*m) * (r.y[m+i] - r.y[i]) - } - } - } else { - if !math.IsNaN(r.y[1]) { - b0 = hwWeight * (r.y[1] - r.y[0]) - } - } - - var s []float64 - if r.seasonal { - s = make([]float64, m) - for i := 0; i < m; i++ { - if !math.IsNaN(r.y[i]) { - s[i] = r.y[i] / l0 - } else { - s[i] = 0 - } - } - } - - parameters := make([]float64, 6+len(s)) - parameters[4] = l0 - parameters[5] = b0 - o := len(parameters) - len(s) - for i := range s { - parameters[i+o] = s[i] - } - - // Determine best fit for the various parameters - minSSE := math.Inf(1) - var bestParams []float64 - for alpha := hwGuessLower; alpha < hwGuessUpper; alpha += hwGuessStep { - for beta := hwGuessLower; beta < hwGuessUpper; beta += hwGuessStep { - for gamma := hwGuessLower; gamma < hwGuessUpper; gamma += hwGuessStep { - for phi := hwGuessLower; phi < hwGuessUpper; phi += hwGuessStep { - parameters[0] = alpha - parameters[1] = beta - parameters[2] = gamma - parameters[3] = phi - sse, params := r.optim.Optimize(r.sse, parameters, r.epsilon, 1) - if sse < minSSE || bestParams == nil { - minSSE = sse - bestParams = params - } - } - } - } - } - - // Forecast - forecasted := r.forecast(r.h, bestParams) - var points []FloatPoint - if r.includeFitData { - start := r.points[0].Time - points = make([]FloatPoint, 0, len(forecasted)) - for i, v := range forecasted { - if !math.IsNaN(v) { - t := start + r.interval*(int64(i)) - points = append(points, FloatPoint{ - Value: v, - Time: t, - }) - } - } - } else { - stop := r.points[len(r.points)-1].Time - points = make([]FloatPoint, 0, r.h) - for i, v := range forecasted[len(r.y):] { - if !math.IsNaN(v) { - t := stop + r.interval*(int64(i)+1) - points = append(points, FloatPoint{ - Value: v, - Time: t, - }) - } - } - } - // Clear data set - r.y = r.y[0:0] - return points -} - -// Using the recursive relations compute the next values -func (r *FloatHoltWintersReducer) next(alpha, beta, gamma, phi, phiH, yT, lTp, bTp, sTm, sTmh float64) (yTh, lT, bT, sT float64) { - lT = alpha*(yT/sTm) + (1-alpha)*(lTp+phi*bTp) - bT = beta*(lT-lTp) + (1-beta)*phi*bTp - sT = gamma*(yT/(lTp+phi*bTp)) + (1-gamma)*sTm - yTh = (lT + phiH*bT) * sTmh - return -} - -// Forecast the data h points into the future. -func (r *FloatHoltWintersReducer) forecast(h int, params []float64) []float64 { - // Constrain parameters - r.constrain(params) - - yT := r.y[0] - - phi := params[3] - phiH := phi - - lT := params[4] - bT := params[5] - sT := 0.0 - - // seasonals is a ring buffer of past sT values - var seasonals []float64 - var m, so int - if r.seasonal { - seasonals = params[6:] - m = len(params[6:]) - if m == 1 { - seasonals[0] = 1 - } - // Season index offset - so = m - 1 - } - - forecasted := make([]float64, len(r.y)+h) - forecasted[0] = yT - l := len(r.y) - var hm int - stm, stmh := 1.0, 1.0 - for t := 1; t < l+h; t++ { - if r.seasonal { - hm = t % m - stm = seasonals[(t-m+so)%m] - stmh = seasonals[(t-m+hm+so)%m] - } - yT, lT, bT, sT = r.next( - params[0], // alpha - params[1], // beta - params[2], // gamma - phi, - phiH, - yT, - lT, - bT, - stm, - stmh, - ) - phiH += math.Pow(phi, float64(t)) - - if r.seasonal { - seasonals[(t+so)%m] = sT - so++ - } - - forecasted[t] = yT - } - return forecasted -} - -// Compute sum squared error for the given parameters. -func (r *FloatHoltWintersReducer) sse(params []float64) float64 { - sse := 0.0 - forecasted := r.forecast(0, params) - for i := range forecasted { - // Skip missing values since we cannot use them to compute an error. - if !math.IsNaN(r.y[i]) { - // Compute error - if math.IsNaN(forecasted[i]) { - // Penalize forecasted NaNs - return math.Inf(1) - } - diff := forecasted[i] - r.y[i] - sse += diff * diff - } - } - return sse -} - -// Constrain alpha, beta, gamma, phi in the range [0, 1] -func (r *FloatHoltWintersReducer) constrain(x []float64) { - // alpha - if x[0] > 1 { - x[0] = 1 - } - if x[0] < 0 { - x[0] = 0 - } - // beta - if x[1] > 1 { - x[1] = 1 - } - if x[1] < 0 { - x[1] = 0 - } - // gamma - if x[2] > 1 { - x[2] = 1 - } - if x[2] < 0 { - x[2] = 0 - } - // phi - if x[3] > 1 { - x[3] = 1 - } - if x[3] < 0 { - x[3] = 0 - } -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/functions_test.go b/vendor/github.com/influxdata/influxdb/influxql/functions_test.go deleted file mode 100644 index d843cc8e..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/functions_test.go +++ /dev/null @@ -1,498 +0,0 @@ -package influxql_test - -import ( - "math" - "testing" - "time" - - "github.com/davecgh/go-spew/spew" - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/pkg/deep" -) - -func almostEqual(got, exp float64) bool { - return math.Abs(got-exp) < 1e-5 && !math.IsNaN(got) -} - -func TestHoltWinters_AusTourists(t *testing.T) { - hw := influxql.NewFloatHoltWintersReducer(10, 4, false, 1) - // Dataset from http://www.inside-r.org/packages/cran/fpp/docs/austourists - austourists := []influxql.FloatPoint{ - {Time: 1, Value: 30.052513}, - {Time: 2, Value: 19.148496}, - {Time: 3, Value: 25.317692}, - {Time: 4, Value: 27.591437}, - {Time: 5, Value: 32.076456}, - {Time: 6, Value: 23.487961}, - {Time: 7, Value: 28.47594}, - {Time: 8, Value: 35.123753}, - {Time: 9, Value: 36.838485}, - {Time: 10, Value: 25.007017}, - {Time: 11, Value: 30.72223}, - {Time: 12, Value: 28.693759}, - {Time: 13, Value: 36.640986}, - {Time: 14, Value: 23.824609}, - {Time: 15, Value: 29.311683}, - {Time: 16, Value: 31.770309}, - {Time: 17, Value: 35.177877}, - {Time: 18, Value: 19.775244}, - {Time: 19, Value: 29.60175}, - {Time: 20, Value: 34.538842}, - {Time: 21, Value: 41.273599}, - {Time: 22, Value: 26.655862}, - {Time: 23, Value: 28.279859}, - {Time: 24, Value: 35.191153}, - {Time: 25, Value: 41.727458}, - {Time: 26, Value: 24.04185}, - {Time: 27, Value: 32.328103}, - {Time: 28, Value: 37.328708}, - {Time: 29, Value: 46.213153}, - {Time: 30, Value: 29.346326}, - {Time: 31, Value: 36.48291}, - {Time: 32, Value: 42.977719}, - {Time: 33, Value: 48.901525}, - {Time: 34, Value: 31.180221}, - {Time: 35, Value: 37.717881}, - {Time: 36, Value: 40.420211}, - {Time: 37, Value: 51.206863}, - {Time: 38, Value: 31.887228}, - {Time: 39, Value: 40.978263}, - {Time: 40, Value: 43.772491}, - {Time: 41, Value: 55.558567}, - {Time: 42, Value: 33.850915}, - {Time: 43, Value: 42.076383}, - {Time: 44, Value: 45.642292}, - {Time: 45, Value: 59.76678}, - {Time: 46, Value: 35.191877}, - {Time: 47, Value: 44.319737}, - {Time: 48, Value: 47.913736}, - } - - for _, p := range austourists { - hw.AggregateFloat(&p) - } - points := hw.Emit() - - forecasted := []influxql.FloatPoint{ - {Time: 49, Value: 51.85064132137853}, - {Time: 50, Value: 43.26055282315273}, - {Time: 51, Value: 41.827258044814464}, - {Time: 52, Value: 54.3990354591749}, - {Time: 53, Value: 54.62334472770803}, - {Time: 54, Value: 45.57155693625209}, - {Time: 55, Value: 44.06051240252263}, - {Time: 56, Value: 57.30029870759433}, - {Time: 57, Value: 57.53591513519172}, - {Time: 58, Value: 47.999008139396096}, - } - - if exp, got := len(forecasted), len(points); exp != got { - t.Fatalf("unexpected number of points emitted: got %d exp %d", got, exp) - } - - for i := range forecasted { - if exp, got := forecasted[i].Time, points[i].Time; got != exp { - t.Errorf("unexpected time on points[%d] got %v exp %v", i, got, exp) - } - if exp, got := forecasted[i].Value, points[i].Value; !almostEqual(got, exp) { - t.Errorf("unexpected value on points[%d] got %v exp %v", i, got, exp) - } - } -} - -func TestHoltWinters_AusTourists_Missing(t *testing.T) { - hw := influxql.NewFloatHoltWintersReducer(10, 4, false, 1) - // Dataset from http://www.inside-r.org/packages/cran/fpp/docs/austourists - austourists := []influxql.FloatPoint{ - {Time: 1, Value: 30.052513}, - {Time: 3, Value: 25.317692}, - {Time: 4, Value: 27.591437}, - {Time: 5, Value: 32.076456}, - {Time: 6, Value: 23.487961}, - {Time: 7, Value: 28.47594}, - {Time: 9, Value: 36.838485}, - {Time: 10, Value: 25.007017}, - {Time: 11, Value: 30.72223}, - {Time: 12, Value: 28.693759}, - {Time: 13, Value: 36.640986}, - {Time: 14, Value: 23.824609}, - {Time: 15, Value: 29.311683}, - {Time: 16, Value: 31.770309}, - {Time: 17, Value: 35.177877}, - {Time: 19, Value: 29.60175}, - {Time: 20, Value: 34.538842}, - {Time: 21, Value: 41.273599}, - {Time: 22, Value: 26.655862}, - {Time: 23, Value: 28.279859}, - {Time: 24, Value: 35.191153}, - {Time: 25, Value: 41.727458}, - {Time: 26, Value: 24.04185}, - {Time: 27, Value: 32.328103}, - {Time: 28, Value: 37.328708}, - {Time: 30, Value: 29.346326}, - {Time: 31, Value: 36.48291}, - {Time: 32, Value: 42.977719}, - {Time: 34, Value: 31.180221}, - {Time: 35, Value: 37.717881}, - {Time: 36, Value: 40.420211}, - {Time: 37, Value: 51.206863}, - {Time: 38, Value: 31.887228}, - {Time: 41, Value: 55.558567}, - {Time: 42, Value: 33.850915}, - {Time: 43, Value: 42.076383}, - {Time: 44, Value: 45.642292}, - {Time: 45, Value: 59.76678}, - {Time: 46, Value: 35.191877}, - {Time: 47, Value: 44.319737}, - {Time: 48, Value: 47.913736}, - } - - for _, p := range austourists { - hw.AggregateFloat(&p) - } - points := hw.Emit() - - forecasted := []influxql.FloatPoint{ - {Time: 49, Value: 54.84533610387743}, - {Time: 50, Value: 41.19329421863249}, - {Time: 51, Value: 45.71673175112451}, - {Time: 52, Value: 56.05759298805955}, - {Time: 53, Value: 59.32337460282217}, - {Time: 54, Value: 44.75280096850461}, - {Time: 55, Value: 49.98865098113751}, - {Time: 56, Value: 61.86084934967605}, - {Time: 57, Value: 65.95805633454883}, - {Time: 58, Value: 50.1502170480547}, - } - - if exp, got := len(forecasted), len(points); exp != got { - t.Fatalf("unexpected number of points emitted: got %d exp %d", got, exp) - } - - for i := range forecasted { - if exp, got := forecasted[i].Time, points[i].Time; got != exp { - t.Errorf("unexpected time on points[%d] got %v exp %v", i, got, exp) - } - if exp, got := forecasted[i].Value, points[i].Value; !almostEqual(got, exp) { - t.Errorf("unexpected value on points[%d] got %v exp %v", i, got, exp) - } - } -} - -func TestHoltWinters_USPopulation(t *testing.T) { - series := []influxql.FloatPoint{ - {Time: 1, Value: 3.93}, - {Time: 2, Value: 5.31}, - {Time: 3, Value: 7.24}, - {Time: 4, Value: 9.64}, - {Time: 5, Value: 12.90}, - {Time: 6, Value: 17.10}, - {Time: 7, Value: 23.20}, - {Time: 8, Value: 31.40}, - {Time: 9, Value: 39.80}, - {Time: 10, Value: 50.20}, - {Time: 11, Value: 62.90}, - {Time: 12, Value: 76.00}, - {Time: 13, Value: 92.00}, - {Time: 14, Value: 105.70}, - {Time: 15, Value: 122.80}, - {Time: 16, Value: 131.70}, - {Time: 17, Value: 151.30}, - {Time: 18, Value: 179.30}, - {Time: 19, Value: 203.20}, - } - hw := influxql.NewFloatHoltWintersReducer(10, 0, true, 1) - for _, p := range series { - hw.AggregateFloat(&p) - } - points := hw.Emit() - - forecasted := []influxql.FloatPoint{ - {Time: 1, Value: 3.93}, - {Time: 2, Value: 4.957405463559748}, - {Time: 3, Value: 7.012210102535647}, - {Time: 4, Value: 10.099589257439924}, - {Time: 5, Value: 14.229926188104242}, - {Time: 6, Value: 19.418878968703797}, - {Time: 7, Value: 25.68749172281409}, - {Time: 8, Value: 33.062351305731305}, - {Time: 9, Value: 41.575791076125206}, - {Time: 10, Value: 51.26614395589263}, - {Time: 11, Value: 62.178047564264595}, - {Time: 12, Value: 74.36280483872488}, - {Time: 13, Value: 87.87880423073163}, - {Time: 14, Value: 102.79200429905801}, - {Time: 15, Value: 119.17648832929542}, - {Time: 16, Value: 137.11509549747296}, - {Time: 17, Value: 156.70013608313175}, - {Time: 18, Value: 178.03419933863566}, - {Time: 19, Value: 201.23106385518594}, - {Time: 20, Value: 226.4167216525905}, - {Time: 21, Value: 253.73052878285205}, - {Time: 22, Value: 283.32649700397553}, - {Time: 23, Value: 315.37474308085984}, - {Time: 24, Value: 350.06311454009256}, - {Time: 25, Value: 387.59901328556873}, - {Time: 26, Value: 428.21144141893404}, - {Time: 27, Value: 472.1532969569147}, - {Time: 28, Value: 519.7039509590035}, - {Time: 29, Value: 571.1721419458248}, - } - - if exp, got := len(forecasted), len(points); exp != got { - t.Fatalf("unexpected number of points emitted: got %d exp %d", got, exp) - } - for i := range forecasted { - if exp, got := forecasted[i].Time, points[i].Time; got != exp { - t.Errorf("unexpected time on points[%d] got %v exp %v", i, got, exp) - } - if exp, got := forecasted[i].Value, points[i].Value; !almostEqual(got, exp) { - t.Errorf("unexpected value on points[%d] got %v exp %v", i, got, exp) - } - } -} - -func TestHoltWinters_USPopulation_Missing(t *testing.T) { - series := []influxql.FloatPoint{ - {Time: 1, Value: 3.93}, - {Time: 2, Value: 5.31}, - {Time: 3, Value: 7.24}, - {Time: 4, Value: 9.64}, - {Time: 5, Value: 12.90}, - {Time: 6, Value: 17.10}, - {Time: 7, Value: 23.20}, - {Time: 8, Value: 31.40}, - {Time: 10, Value: 50.20}, - {Time: 11, Value: 62.90}, - {Time: 12, Value: 76.00}, - {Time: 13, Value: 92.00}, - {Time: 15, Value: 122.80}, - {Time: 16, Value: 131.70}, - {Time: 17, Value: 151.30}, - {Time: 19, Value: 203.20}, - } - hw := influxql.NewFloatHoltWintersReducer(10, 0, true, 1) - for _, p := range series { - hw.AggregateFloat(&p) - } - points := hw.Emit() - - forecasted := []influxql.FloatPoint{ - {Time: 1, Value: 3.93}, - {Time: 2, Value: 4.8931364428135105}, - {Time: 3, Value: 6.962653629047061}, - {Time: 4, Value: 10.056207765903274}, - {Time: 5, Value: 14.18435088129532}, - {Time: 6, Value: 19.362939306110846}, - {Time: 7, Value: 25.613247940326584}, - {Time: 8, Value: 32.96213087008264}, - {Time: 9, Value: 41.442230043017204}, - {Time: 10, Value: 51.09223428526052}, - {Time: 11, Value: 61.95719155158485}, - {Time: 12, Value: 74.08887794968567}, - {Time: 13, Value: 87.54622778052787}, - {Time: 14, Value: 102.39582960014131}, - {Time: 15, Value: 118.7124941463221}, - {Time: 16, Value: 136.57990089987464}, - {Time: 17, Value: 156.09133107941278}, - {Time: 18, Value: 177.35049601833734}, - {Time: 19, Value: 200.472471161683}, - {Time: 20, Value: 225.58474737097785}, - {Time: 21, Value: 252.82841286206823}, - {Time: 22, Value: 282.35948095261017}, - {Time: 23, Value: 314.3503808953992}, - {Time: 24, Value: 348.99163145856954}, - {Time: 25, Value: 386.49371962730555}, - {Time: 26, Value: 427.08920989407727}, - {Time: 27, Value: 471.0351131332573}, - {Time: 28, Value: 518.615548088049}, - {Time: 29, Value: 570.1447331101863}, - } - - if exp, got := len(forecasted), len(points); exp != got { - t.Fatalf("unexpected number of points emitted: got %d exp %d", got, exp) - } - for i := range forecasted { - if exp, got := forecasted[i].Time, points[i].Time; got != exp { - t.Errorf("unexpected time on points[%d] got %v exp %v", i, got, exp) - } - if exp, got := forecasted[i].Value, points[i].Value; !almostEqual(got, exp) { - t.Errorf("unexpected value on points[%d] got %v exp %v", i, got, exp) - } - } -} -func TestHoltWinters_RoundTime(t *testing.T) { - maxTime := time.Unix(0, influxql.MaxTime).Round(time.Second).UnixNano() - data := []influxql.FloatPoint{ - {Time: maxTime - int64(5*time.Second), Value: 1}, - {Time: maxTime - int64(4*time.Second+103*time.Millisecond), Value: 10}, - {Time: maxTime - int64(3*time.Second+223*time.Millisecond), Value: 2}, - {Time: maxTime - int64(2*time.Second+481*time.Millisecond), Value: 11}, - } - hw := influxql.NewFloatHoltWintersReducer(2, 2, true, time.Second) - for _, p := range data { - hw.AggregateFloat(&p) - } - points := hw.Emit() - - forecasted := []influxql.FloatPoint{ - {Time: maxTime - int64(5*time.Second), Value: 1}, - {Time: maxTime - int64(4*time.Second), Value: 10.006729104838234}, - {Time: maxTime - int64(3*time.Second), Value: 1.998341814469269}, - {Time: maxTime - int64(2*time.Second), Value: 10.997858830631172}, - {Time: maxTime - int64(1*time.Second), Value: 4.085860238030013}, - {Time: maxTime - int64(0*time.Second), Value: 11.35713604403339}, - } - - if exp, got := len(forecasted), len(points); exp != got { - t.Fatalf("unexpected number of points emitted: got %d exp %d", got, exp) - } - for i := range forecasted { - if exp, got := forecasted[i].Time, points[i].Time; got != exp { - t.Errorf("unexpected time on points[%d] got %v exp %v", i, got, exp) - } - if exp, got := forecasted[i].Value, points[i].Value; !almostEqual(got, exp) { - t.Errorf("unexpected value on points[%d] got %v exp %v", i, got, exp) - } - } -} - -func TestHoltWinters_MaxTime(t *testing.T) { - data := []influxql.FloatPoint{ - {Time: influxql.MaxTime - 1, Value: 1}, - {Time: influxql.MaxTime, Value: 2}, - } - hw := influxql.NewFloatHoltWintersReducer(1, 0, true, 1) - for _, p := range data { - hw.AggregateFloat(&p) - } - points := hw.Emit() - - forecasted := []influxql.FloatPoint{ - {Time: influxql.MaxTime - 1, Value: 1}, - {Time: influxql.MaxTime, Value: 2.001516944066403}, - {Time: influxql.MaxTime + 1, Value: 2.5365248972488343}, - } - - if exp, got := len(forecasted), len(points); exp != got { - t.Fatalf("unexpected number of points emitted: got %d exp %d", got, exp) - } - for i := range forecasted { - if exp, got := forecasted[i].Time, points[i].Time; got != exp { - t.Errorf("unexpected time on points[%d] got %v exp %v", i, got, exp) - } - if exp, got := forecasted[i].Value, points[i].Value; !almostEqual(got, exp) { - t.Errorf("unexpected value on points[%d] got %v exp %v", i, got, exp) - } - } -} - -// TestSample_AllSamplesSeen attempts to verify that it is possible -// to get every subsample in a reasonable number of iterations. -// -// The idea here is that 30 iterations should be enough to hit every possible -// sequence at least once. -func TestSample_AllSamplesSeen(t *testing.T) { - ps := []influxql.FloatPoint{ - {Time: 1, Value: 1}, - {Time: 2, Value: 2}, - {Time: 3, Value: 3}, - } - - // List of all the possible subsamples - samples := [][]influxql.FloatPoint{ - { - {Time: 1, Value: 1}, - {Time: 2, Value: 2}, - }, - { - {Time: 1, Value: 1}, - {Time: 3, Value: 3}, - }, - { - {Time: 2, Value: 2}, - {Time: 3, Value: 3}, - }, - } - - // 30 iterations should be sufficient to guarantee that - // we hit every possible subsample. - for i := 0; i < 30; i++ { - s := influxql.NewFloatSampleReducer(2) - for _, p := range ps { - s.AggregateFloat(&p) - } - - points := s.Emit() - - for i, sample := range samples { - // if we find a sample that it matches, remove it from - // this list of possible samples - if deep.Equal(sample, points) { - samples = append(samples[:i], samples[i+1:]...) - break - } - } - - // if samples is empty we've seen every sample, so we're done - if len(samples) == 0 { - return - } - - // The FloatSampleReducer is seeded with time.Now().UnixNano(), and without this sleep, - // this test will fail on machines where UnixNano doesn't return full resolution. - // Specifically, some Windows machines will only return timestamps accurate to 100ns. - // While iterating through this test without an explicit sleep, - // we would only see one or two unique seeds across all the calls to NewFloatSampleReducer. - time.Sleep(time.Millisecond) - } - - // If we missed a sample, report the error - if len(samples) != 0 { - t.Fatalf("expected all samples to be seen; unseen samples: %#v", samples) - } -} - -func TestSample_SampleSizeLessThanNumPoints(t *testing.T) { - s := influxql.NewFloatSampleReducer(2) - - ps := []influxql.FloatPoint{ - {Time: 1, Value: 1}, - {Time: 2, Value: 2}, - {Time: 3, Value: 3}, - } - - for _, p := range ps { - s.AggregateFloat(&p) - } - - points := s.Emit() - - if exp, got := 2, len(points); exp != got { - t.Fatalf("unexpected number of points emitted: got %d exp %d", got, exp) - } -} - -func TestSample_SampleSizeGreaterThanNumPoints(t *testing.T) { - s := influxql.NewFloatSampleReducer(4) - - ps := []influxql.FloatPoint{ - {Time: 1, Value: 1}, - {Time: 2, Value: 2}, - {Time: 3, Value: 3}, - } - - for _, p := range ps { - s.AggregateFloat(&p) - } - - points := s.Emit() - - if exp, got := len(ps), len(points); exp != got { - t.Fatalf("unexpected number of points emitted: got %d exp %d", got, exp) - } - - if !deep.Equal(ps, points) { - t.Fatalf("unexpected points: %s", spew.Sdump(points)) - } -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/influxql.go b/vendor/github.com/influxdata/influxdb/influxql/influxql.go deleted file mode 100644 index 324e399d..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/influxql.go +++ /dev/null @@ -1,7 +0,0 @@ -package influxql // import "github.com/influxdata/influxdb/influxql" - -//go:generate tmpl -data=@tmpldata iterator.gen.go.tmpl -//go:generate tmpl -data=@tmpldata point.gen.go.tmpl -//go:generate tmpl -data=@tmpldata functions.gen.go.tmpl - -//go:generate protoc --gogo_out=. internal/internal.proto diff --git a/vendor/github.com/influxdata/influxdb/influxql/internal/internal.pb.go b/vendor/github.com/influxdata/influxdb/influxql/internal/internal.pb.go deleted file mode 100644 index cfafbf43..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/internal/internal.pb.go +++ /dev/null @@ -1,528 +0,0 @@ -// Code generated by protoc-gen-gogo. -// source: internal/internal.proto -// DO NOT EDIT! - -/* -Package influxql is a generated protocol buffer package. - -It is generated from these files: - internal/internal.proto - -It has these top-level messages: - Point - Aux - IteratorOptions - Measurements - Measurement - Interval - IteratorStats - VarRef -*/ -package influxql - -import proto "github.com/gogo/protobuf/proto" -import fmt "fmt" -import math "math" - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package - -type Point struct { - Name *string `protobuf:"bytes,1,req,name=Name" json:"Name,omitempty"` - Tags *string `protobuf:"bytes,2,req,name=Tags" json:"Tags,omitempty"` - Time *int64 `protobuf:"varint,3,req,name=Time" json:"Time,omitempty"` - Nil *bool `protobuf:"varint,4,req,name=Nil" json:"Nil,omitempty"` - Aux []*Aux `protobuf:"bytes,5,rep,name=Aux" json:"Aux,omitempty"` - Aggregated *uint32 `protobuf:"varint,6,opt,name=Aggregated" json:"Aggregated,omitempty"` - FloatValue *float64 `protobuf:"fixed64,7,opt,name=FloatValue" json:"FloatValue,omitempty"` - IntegerValue *int64 `protobuf:"varint,8,opt,name=IntegerValue" json:"IntegerValue,omitempty"` - StringValue *string `protobuf:"bytes,9,opt,name=StringValue" json:"StringValue,omitempty"` - BooleanValue *bool `protobuf:"varint,10,opt,name=BooleanValue" json:"BooleanValue,omitempty"` - Stats *IteratorStats `protobuf:"bytes,11,opt,name=Stats" json:"Stats,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Point) Reset() { *m = Point{} } -func (m *Point) String() string { return proto.CompactTextString(m) } -func (*Point) ProtoMessage() {} -func (*Point) Descriptor() ([]byte, []int) { return fileDescriptorInternal, []int{0} } - -func (m *Point) GetName() string { - if m != nil && m.Name != nil { - return *m.Name - } - return "" -} - -func (m *Point) GetTags() string { - if m != nil && m.Tags != nil { - return *m.Tags - } - return "" -} - -func (m *Point) GetTime() int64 { - if m != nil && m.Time != nil { - return *m.Time - } - return 0 -} - -func (m *Point) GetNil() bool { - if m != nil && m.Nil != nil { - return *m.Nil - } - return false -} - -func (m *Point) GetAux() []*Aux { - if m != nil { - return m.Aux - } - return nil -} - -func (m *Point) GetAggregated() uint32 { - if m != nil && m.Aggregated != nil { - return *m.Aggregated - } - return 0 -} - -func (m *Point) GetFloatValue() float64 { - if m != nil && m.FloatValue != nil { - return *m.FloatValue - } - return 0 -} - -func (m *Point) GetIntegerValue() int64 { - if m != nil && m.IntegerValue != nil { - return *m.IntegerValue - } - return 0 -} - -func (m *Point) GetStringValue() string { - if m != nil && m.StringValue != nil { - return *m.StringValue - } - return "" -} - -func (m *Point) GetBooleanValue() bool { - if m != nil && m.BooleanValue != nil { - return *m.BooleanValue - } - return false -} - -func (m *Point) GetStats() *IteratorStats { - if m != nil { - return m.Stats - } - return nil -} - -type Aux struct { - DataType *int32 `protobuf:"varint,1,req,name=DataType" json:"DataType,omitempty"` - FloatValue *float64 `protobuf:"fixed64,2,opt,name=FloatValue" json:"FloatValue,omitempty"` - IntegerValue *int64 `protobuf:"varint,3,opt,name=IntegerValue" json:"IntegerValue,omitempty"` - StringValue *string `protobuf:"bytes,4,opt,name=StringValue" json:"StringValue,omitempty"` - BooleanValue *bool `protobuf:"varint,5,opt,name=BooleanValue" json:"BooleanValue,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Aux) Reset() { *m = Aux{} } -func (m *Aux) String() string { return proto.CompactTextString(m) } -func (*Aux) ProtoMessage() {} -func (*Aux) Descriptor() ([]byte, []int) { return fileDescriptorInternal, []int{1} } - -func (m *Aux) GetDataType() int32 { - if m != nil && m.DataType != nil { - return *m.DataType - } - return 0 -} - -func (m *Aux) GetFloatValue() float64 { - if m != nil && m.FloatValue != nil { - return *m.FloatValue - } - return 0 -} - -func (m *Aux) GetIntegerValue() int64 { - if m != nil && m.IntegerValue != nil { - return *m.IntegerValue - } - return 0 -} - -func (m *Aux) GetStringValue() string { - if m != nil && m.StringValue != nil { - return *m.StringValue - } - return "" -} - -func (m *Aux) GetBooleanValue() bool { - if m != nil && m.BooleanValue != nil { - return *m.BooleanValue - } - return false -} - -type IteratorOptions struct { - Expr *string `protobuf:"bytes,1,opt,name=Expr" json:"Expr,omitempty"` - Aux []string `protobuf:"bytes,2,rep,name=Aux" json:"Aux,omitempty"` - Fields []*VarRef `protobuf:"bytes,17,rep,name=Fields" json:"Fields,omitempty"` - Sources []*Measurement `protobuf:"bytes,3,rep,name=Sources" json:"Sources,omitempty"` - Interval *Interval `protobuf:"bytes,4,opt,name=Interval" json:"Interval,omitempty"` - Dimensions []string `protobuf:"bytes,5,rep,name=Dimensions" json:"Dimensions,omitempty"` - Fill *int32 `protobuf:"varint,6,opt,name=Fill" json:"Fill,omitempty"` - FillValue *float64 `protobuf:"fixed64,7,opt,name=FillValue" json:"FillValue,omitempty"` - Condition *string `protobuf:"bytes,8,opt,name=Condition" json:"Condition,omitempty"` - StartTime *int64 `protobuf:"varint,9,opt,name=StartTime" json:"StartTime,omitempty"` - EndTime *int64 `protobuf:"varint,10,opt,name=EndTime" json:"EndTime,omitempty"` - Ascending *bool `protobuf:"varint,11,opt,name=Ascending" json:"Ascending,omitempty"` - Limit *int64 `protobuf:"varint,12,opt,name=Limit" json:"Limit,omitempty"` - Offset *int64 `protobuf:"varint,13,opt,name=Offset" json:"Offset,omitempty"` - SLimit *int64 `protobuf:"varint,14,opt,name=SLimit" json:"SLimit,omitempty"` - SOffset *int64 `protobuf:"varint,15,opt,name=SOffset" json:"SOffset,omitempty"` - Dedupe *bool `protobuf:"varint,16,opt,name=Dedupe" json:"Dedupe,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *IteratorOptions) Reset() { *m = IteratorOptions{} } -func (m *IteratorOptions) String() string { return proto.CompactTextString(m) } -func (*IteratorOptions) ProtoMessage() {} -func (*IteratorOptions) Descriptor() ([]byte, []int) { return fileDescriptorInternal, []int{2} } - -func (m *IteratorOptions) GetExpr() string { - if m != nil && m.Expr != nil { - return *m.Expr - } - return "" -} - -func (m *IteratorOptions) GetAux() []string { - if m != nil { - return m.Aux - } - return nil -} - -func (m *IteratorOptions) GetFields() []*VarRef { - if m != nil { - return m.Fields - } - return nil -} - -func (m *IteratorOptions) GetSources() []*Measurement { - if m != nil { - return m.Sources - } - return nil -} - -func (m *IteratorOptions) GetInterval() *Interval { - if m != nil { - return m.Interval - } - return nil -} - -func (m *IteratorOptions) GetDimensions() []string { - if m != nil { - return m.Dimensions - } - return nil -} - -func (m *IteratorOptions) GetFill() int32 { - if m != nil && m.Fill != nil { - return *m.Fill - } - return 0 -} - -func (m *IteratorOptions) GetFillValue() float64 { - if m != nil && m.FillValue != nil { - return *m.FillValue - } - return 0 -} - -func (m *IteratorOptions) GetCondition() string { - if m != nil && m.Condition != nil { - return *m.Condition - } - return "" -} - -func (m *IteratorOptions) GetStartTime() int64 { - if m != nil && m.StartTime != nil { - return *m.StartTime - } - return 0 -} - -func (m *IteratorOptions) GetEndTime() int64 { - if m != nil && m.EndTime != nil { - return *m.EndTime - } - return 0 -} - -func (m *IteratorOptions) GetAscending() bool { - if m != nil && m.Ascending != nil { - return *m.Ascending - } - return false -} - -func (m *IteratorOptions) GetLimit() int64 { - if m != nil && m.Limit != nil { - return *m.Limit - } - return 0 -} - -func (m *IteratorOptions) GetOffset() int64 { - if m != nil && m.Offset != nil { - return *m.Offset - } - return 0 -} - -func (m *IteratorOptions) GetSLimit() int64 { - if m != nil && m.SLimit != nil { - return *m.SLimit - } - return 0 -} - -func (m *IteratorOptions) GetSOffset() int64 { - if m != nil && m.SOffset != nil { - return *m.SOffset - } - return 0 -} - -func (m *IteratorOptions) GetDedupe() bool { - if m != nil && m.Dedupe != nil { - return *m.Dedupe - } - return false -} - -type Measurements struct { - Items []*Measurement `protobuf:"bytes,1,rep,name=Items" json:"Items,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Measurements) Reset() { *m = Measurements{} } -func (m *Measurements) String() string { return proto.CompactTextString(m) } -func (*Measurements) ProtoMessage() {} -func (*Measurements) Descriptor() ([]byte, []int) { return fileDescriptorInternal, []int{3} } - -func (m *Measurements) GetItems() []*Measurement { - if m != nil { - return m.Items - } - return nil -} - -type Measurement struct { - Database *string `protobuf:"bytes,1,opt,name=Database" json:"Database,omitempty"` - RetentionPolicy *string `protobuf:"bytes,2,opt,name=RetentionPolicy" json:"RetentionPolicy,omitempty"` - Name *string `protobuf:"bytes,3,opt,name=Name" json:"Name,omitempty"` - Regex *string `protobuf:"bytes,4,opt,name=Regex" json:"Regex,omitempty"` - IsTarget *bool `protobuf:"varint,5,opt,name=IsTarget" json:"IsTarget,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Measurement) Reset() { *m = Measurement{} } -func (m *Measurement) String() string { return proto.CompactTextString(m) } -func (*Measurement) ProtoMessage() {} -func (*Measurement) Descriptor() ([]byte, []int) { return fileDescriptorInternal, []int{4} } - -func (m *Measurement) GetDatabase() string { - if m != nil && m.Database != nil { - return *m.Database - } - return "" -} - -func (m *Measurement) GetRetentionPolicy() string { - if m != nil && m.RetentionPolicy != nil { - return *m.RetentionPolicy - } - return "" -} - -func (m *Measurement) GetName() string { - if m != nil && m.Name != nil { - return *m.Name - } - return "" -} - -func (m *Measurement) GetRegex() string { - if m != nil && m.Regex != nil { - return *m.Regex - } - return "" -} - -func (m *Measurement) GetIsTarget() bool { - if m != nil && m.IsTarget != nil { - return *m.IsTarget - } - return false -} - -type Interval struct { - Duration *int64 `protobuf:"varint,1,opt,name=Duration" json:"Duration,omitempty"` - Offset *int64 `protobuf:"varint,2,opt,name=Offset" json:"Offset,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *Interval) Reset() { *m = Interval{} } -func (m *Interval) String() string { return proto.CompactTextString(m) } -func (*Interval) ProtoMessage() {} -func (*Interval) Descriptor() ([]byte, []int) { return fileDescriptorInternal, []int{5} } - -func (m *Interval) GetDuration() int64 { - if m != nil && m.Duration != nil { - return *m.Duration - } - return 0 -} - -func (m *Interval) GetOffset() int64 { - if m != nil && m.Offset != nil { - return *m.Offset - } - return 0 -} - -type IteratorStats struct { - SeriesN *int64 `protobuf:"varint,1,opt,name=SeriesN" json:"SeriesN,omitempty"` - PointN *int64 `protobuf:"varint,2,opt,name=PointN" json:"PointN,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *IteratorStats) Reset() { *m = IteratorStats{} } -func (m *IteratorStats) String() string { return proto.CompactTextString(m) } -func (*IteratorStats) ProtoMessage() {} -func (*IteratorStats) Descriptor() ([]byte, []int) { return fileDescriptorInternal, []int{6} } - -func (m *IteratorStats) GetSeriesN() int64 { - if m != nil && m.SeriesN != nil { - return *m.SeriesN - } - return 0 -} - -func (m *IteratorStats) GetPointN() int64 { - if m != nil && m.PointN != nil { - return *m.PointN - } - return 0 -} - -type VarRef struct { - Val *string `protobuf:"bytes,1,req,name=Val" json:"Val,omitempty"` - Type *int32 `protobuf:"varint,2,opt,name=Type" json:"Type,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *VarRef) Reset() { *m = VarRef{} } -func (m *VarRef) String() string { return proto.CompactTextString(m) } -func (*VarRef) ProtoMessage() {} -func (*VarRef) Descriptor() ([]byte, []int) { return fileDescriptorInternal, []int{7} } - -func (m *VarRef) GetVal() string { - if m != nil && m.Val != nil { - return *m.Val - } - return "" -} - -func (m *VarRef) GetType() int32 { - if m != nil && m.Type != nil { - return *m.Type - } - return 0 -} - -func init() { - proto.RegisterType((*Point)(nil), "influxql.Point") - proto.RegisterType((*Aux)(nil), "influxql.Aux") - proto.RegisterType((*IteratorOptions)(nil), "influxql.IteratorOptions") - proto.RegisterType((*Measurements)(nil), "influxql.Measurements") - proto.RegisterType((*Measurement)(nil), "influxql.Measurement") - proto.RegisterType((*Interval)(nil), "influxql.Interval") - proto.RegisterType((*IteratorStats)(nil), "influxql.IteratorStats") - proto.RegisterType((*VarRef)(nil), "influxql.VarRef") -} - -func init() { proto.RegisterFile("internal/internal.proto", fileDescriptorInternal) } - -var fileDescriptorInternal = []byte{ - // 685 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x84, 0x54, 0xd1, 0x6a, 0xdc, 0x3a, - 0x10, 0xc5, 0xf6, 0x7a, 0x63, 0x6b, 0xb3, 0x37, 0xb9, 0x22, 0xf7, 0x46, 0x94, 0xd2, 0x1a, 0x3f, - 0x19, 0x4a, 0x37, 0x90, 0xd7, 0x42, 0x61, 0xdb, 0x24, 0xb0, 0xd0, 0x6e, 0x82, 0x1c, 0xf2, 0xae, - 0x66, 0x67, 0x8d, 0xc0, 0x2b, 0x6f, 0x65, 0xb9, 0x6c, 0xde, 0xfa, 0x1b, 0xfd, 0x86, 0x7e, 0x4c, - 0x7f, 0xa9, 0x68, 0x64, 0xaf, 0x9d, 0x14, 0x9a, 0x27, 0xcf, 0x39, 0x33, 0x92, 0x7c, 0x66, 0x8e, - 0x44, 0x4e, 0xa5, 0x32, 0xa0, 0x95, 0x28, 0xcf, 0xba, 0x60, 0xb6, 0xd5, 0x95, 0xa9, 0x68, 0x24, - 0xd5, 0xba, 0x6c, 0x76, 0x5f, 0xcb, 0xf4, 0x97, 0x4f, 0xc2, 0x9b, 0x4a, 0x2a, 0x43, 0x29, 0x19, - 0x2d, 0xc5, 0x06, 0x98, 0x97, 0xf8, 0x59, 0xcc, 0x31, 0xb6, 0xdc, 0xad, 0x28, 0x6a, 0xe6, 0x3b, - 0xce, 0xc6, 0xc8, 0xc9, 0x0d, 0xb0, 0x20, 0xf1, 0xb3, 0x80, 0x63, 0x4c, 0x8f, 0x49, 0xb0, 0x94, - 0x25, 0x1b, 0x25, 0x7e, 0x16, 0x71, 0x1b, 0xd2, 0xd7, 0x24, 0x98, 0x37, 0x3b, 0x16, 0x26, 0x41, - 0x36, 0x39, 0x9f, 0xce, 0xba, 0xf3, 0x66, 0xf3, 0x66, 0xc7, 0x6d, 0x86, 0xbe, 0x22, 0x64, 0x5e, - 0x14, 0x1a, 0x0a, 0x61, 0x60, 0xc5, 0xc6, 0x89, 0x97, 0x4d, 0xf9, 0x80, 0xb1, 0xf9, 0xab, 0xb2, - 0x12, 0xe6, 0x4e, 0x94, 0x0d, 0xb0, 0x83, 0xc4, 0xcb, 0x3c, 0x3e, 0x60, 0x68, 0x4a, 0x0e, 0x17, - 0xca, 0x40, 0x01, 0xda, 0x55, 0x44, 0x89, 0x97, 0x05, 0xfc, 0x11, 0x47, 0x13, 0x32, 0xc9, 0x8d, - 0x96, 0xaa, 0x70, 0x25, 0x71, 0xe2, 0x65, 0x31, 0x1f, 0x52, 0x76, 0x97, 0x0f, 0x55, 0x55, 0x82, - 0x50, 0xae, 0x84, 0x24, 0x5e, 0x16, 0xf1, 0x47, 0x1c, 0x7d, 0x4b, 0xc2, 0xdc, 0x08, 0x53, 0xb3, - 0x49, 0xe2, 0x65, 0x93, 0xf3, 0xd3, 0x5e, 0xcc, 0xc2, 0x80, 0x16, 0xa6, 0xd2, 0x98, 0xe6, 0xae, - 0x2a, 0xfd, 0xe9, 0xa1, 0x74, 0xfa, 0x82, 0x44, 0x17, 0xc2, 0x88, 0xdb, 0x87, 0xad, 0xeb, 0x69, - 0xc8, 0xf7, 0xf8, 0x89, 0x38, 0xff, 0x59, 0x71, 0xc1, 0xf3, 0xe2, 0x46, 0xcf, 0x8b, 0x0b, 0xff, - 0x14, 0x97, 0x7e, 0x1f, 0x91, 0xa3, 0x4e, 0xc6, 0xf5, 0xd6, 0xc8, 0x4a, 0xe1, 0x84, 0x2f, 0x77, - 0x5b, 0xcd, 0x3c, 0xdc, 0x12, 0x63, 0x3b, 0x61, 0x3b, 0x4f, 0x3f, 0x09, 0xb2, 0xd8, 0x0d, 0x30, - 0x23, 0xe3, 0x2b, 0x09, 0xe5, 0xaa, 0x66, 0xff, 0xe2, 0x90, 0x8f, 0xfb, 0xbe, 0xdc, 0x09, 0xcd, - 0x61, 0xcd, 0xdb, 0x3c, 0x3d, 0x23, 0x07, 0x79, 0xd5, 0xe8, 0x7b, 0xa8, 0x59, 0x80, 0xa5, 0xff, - 0xf5, 0xa5, 0x9f, 0x41, 0xd4, 0x8d, 0x86, 0x0d, 0x28, 0xc3, 0xbb, 0x2a, 0x3a, 0x23, 0x91, 0x95, - 0xaa, 0xbf, 0x89, 0x12, 0x75, 0x4d, 0xce, 0xe9, 0xa0, 0xe9, 0x6d, 0x86, 0xef, 0x6b, 0x6c, 0x3b, - 0x2f, 0xe4, 0x06, 0x54, 0x6d, 0x7f, 0x1f, 0x3d, 0x17, 0xf3, 0x01, 0x63, 0x05, 0x5d, 0xc9, 0xb2, - 0x44, 0x97, 0x85, 0x1c, 0x63, 0xfa, 0x92, 0xc4, 0xf6, 0x3b, 0xb4, 0x57, 0x4f, 0xd8, 0xec, 0xc7, - 0x4a, 0xad, 0xa4, 0x6d, 0x08, 0x5a, 0x2b, 0xe6, 0x3d, 0x61, 0xb3, 0xb9, 0x11, 0xda, 0xe0, 0x3d, - 0x88, 0x71, 0x36, 0x3d, 0x41, 0x19, 0x39, 0xb8, 0x54, 0x2b, 0xcc, 0x11, 0xcc, 0x75, 0xd0, 0xae, - 0x9b, 0xd7, 0xf7, 0xa0, 0x56, 0x52, 0x15, 0xe8, 0xa6, 0x88, 0xf7, 0x04, 0x3d, 0x21, 0xe1, 0x27, - 0xb9, 0x91, 0x86, 0x1d, 0xe2, 0x2a, 0x07, 0xe8, 0xff, 0x64, 0x7c, 0xbd, 0x5e, 0xd7, 0x60, 0xd8, - 0x14, 0xe9, 0x16, 0x59, 0x3e, 0x77, 0xe5, 0xff, 0x38, 0xde, 0x21, 0x7b, 0x7a, 0xde, 0x2e, 0x38, - 0x72, 0xa7, 0xe7, 0xfd, 0x8a, 0x0b, 0x58, 0x35, 0x5b, 0x60, 0xc7, 0x78, 0x74, 0x8b, 0xd2, 0x77, - 0xe4, 0x70, 0x30, 0x85, 0x9a, 0xbe, 0x21, 0xe1, 0xc2, 0xc0, 0xa6, 0x66, 0xde, 0xdf, 0x86, 0xe5, - 0x6a, 0xd2, 0x1f, 0x1e, 0x99, 0x0c, 0xe8, 0xce, 0xf5, 0x5f, 0x44, 0x0d, 0xad, 0x7f, 0xf6, 0x98, - 0x66, 0xe4, 0x88, 0x83, 0x01, 0x65, 0x7b, 0x78, 0x53, 0x95, 0xf2, 0xfe, 0x01, 0xad, 0x1f, 0xf3, - 0xa7, 0xf4, 0xfe, 0x2d, 0x0a, 0x9c, 0x03, 0xf1, 0x2d, 0x3a, 0x21, 0x21, 0x87, 0x02, 0x76, 0xad, - 0xd3, 0x1d, 0xb0, 0xe7, 0x2d, 0xea, 0x5b, 0xa1, 0x0b, 0x30, 0xad, 0xbf, 0xf7, 0x38, 0x7d, 0xdf, - 0xdb, 0x08, 0xff, 0xab, 0xd1, 0x02, 0xe7, 0xe9, 0x61, 0x5f, 0xf6, 0x78, 0xd0, 0x62, 0x7f, 0xd8, - 0xe2, 0x74, 0x4e, 0xa6, 0x8f, 0x6e, 0x38, 0xf6, 0x16, 0xb4, 0x84, 0x7a, 0xd9, 0xee, 0xd1, 0x41, - 0xbb, 0x05, 0xbe, 0xa2, 0xcb, 0x6e, 0x0b, 0x87, 0xd2, 0x19, 0x19, 0xbb, 0xcb, 0x60, 0x2f, 0xd0, - 0x9d, 0x28, 0xdb, 0xd7, 0xd5, 0x86, 0xf8, 0x90, 0xda, 0xc7, 0xc1, 0x77, 0xae, 0xb4, 0xf1, 0xef, - 0x00, 0x00, 0x00, 0xff, 0xff, 0x8c, 0x01, 0x5d, 0x11, 0xb2, 0x05, 0x00, 0x00, -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/internal/internal.proto b/vendor/github.com/influxdata/influxdb/influxql/internal/internal.proto deleted file mode 100644 index 5f15c935..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/internal/internal.proto +++ /dev/null @@ -1,72 +0,0 @@ -package influxql; - -message Point { - required string Name = 1; - required string Tags = 2; - required int64 Time = 3; - required bool Nil = 4; - repeated Aux Aux = 5; - optional uint32 Aggregated = 6; - - optional double FloatValue = 7; - optional int64 IntegerValue = 8; - optional string StringValue = 9; - optional bool BooleanValue = 10; - - optional IteratorStats Stats = 11; -} - -message Aux { - required int32 DataType = 1; - optional double FloatValue = 2; - optional int64 IntegerValue = 3; - optional string StringValue = 4; - optional bool BooleanValue = 5; -} - -message IteratorOptions { - optional string Expr = 1; - repeated string Aux = 2; - repeated VarRef Fields = 17; - repeated Measurement Sources = 3; - optional Interval Interval = 4; - repeated string Dimensions = 5; - optional int32 Fill = 6; - optional double FillValue = 7; - optional string Condition = 8; - optional int64 StartTime = 9; - optional int64 EndTime = 10; - optional bool Ascending = 11; - optional int64 Limit = 12; - optional int64 Offset = 13; - optional int64 SLimit = 14; - optional int64 SOffset = 15; - optional bool Dedupe = 16; -} - -message Measurements { - repeated Measurement Items = 1; -} - -message Measurement { - optional string Database = 1; - optional string RetentionPolicy = 2; - optional string Name = 3; - optional string Regex = 4; - optional bool IsTarget = 5; -} - -message Interval { - optional int64 Duration = 1; - optional int64 Offset = 2; -} - -message IteratorStats { - optional int64 SeriesN = 1; - optional int64 PointN = 2; -} - -message VarRef { - required string Val = 1; - optional int32 Type = 2; -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/iterator.gen.go b/vendor/github.com/influxdata/influxdb/influxql/iterator.gen.go deleted file mode 100644 index f76a80e1..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/iterator.gen.go +++ /dev/null @@ -1,9664 +0,0 @@ -// Generated by tmpl -// https://github.com/benbjohnson/tmpl -// -// DO NOT EDIT! -// Source: iterator.gen.go.tmpl - -package influxql - -import ( - "container/heap" - "encoding/binary" - "errors" - "fmt" - "io" - "sort" - "sync" - "time" - - "github.com/gogo/protobuf/proto" - internal "github.com/influxdata/influxdb/influxql/internal" -) - -// DefaultStatsInterval is the default value for IteratorEncoder.StatsInterval. -const DefaultStatsInterval = 10 * time.Second - -// FloatIterator represents a stream of float points. -type FloatIterator interface { - Iterator - Next() (*FloatPoint, error) -} - -// newFloatIterators converts a slice of Iterator to a slice of FloatIterator. -// Drop and closes any iterator in itrs that is not a FloatIterator and cannot -// be cast to a FloatIterator. -func newFloatIterators(itrs []Iterator) []FloatIterator { - a := make([]FloatIterator, 0, len(itrs)) - for _, itr := range itrs { - switch itr := itr.(type) { - case FloatIterator: - a = append(a, itr) - - case IntegerIterator: - a = append(a, &integerFloatCastIterator{input: itr}) - - default: - itr.Close() - } - } - return a -} - -// bufFloatIterator represents a buffered FloatIterator. -type bufFloatIterator struct { - itr FloatIterator - buf *FloatPoint -} - -// newBufFloatIterator returns a buffered FloatIterator. -func newBufFloatIterator(itr FloatIterator) *bufFloatIterator { - return &bufFloatIterator{itr: itr} -} - -// Stats returns statistics from the input iterator. -func (itr *bufFloatIterator) Stats() IteratorStats { return itr.itr.Stats() } - -// Close closes the underlying iterator. -func (itr *bufFloatIterator) Close() error { return itr.itr.Close() } - -// peek returns the next point without removing it from the iterator. -func (itr *bufFloatIterator) peek() (*FloatPoint, error) { - p, err := itr.Next() - if err != nil { - return nil, err - } - itr.unread(p) - return p, nil -} - -// peekTime returns the time of the next point. -// Returns zero time if no more points available. -func (itr *bufFloatIterator) peekTime() (int64, error) { - p, err := itr.peek() - if p == nil || err != nil { - return ZeroTime, err - } - return p.Time, nil -} - -// Next returns the current buffer, if exists, or calls the underlying iterator. -func (itr *bufFloatIterator) Next() (*FloatPoint, error) { - buf := itr.buf - if buf != nil { - itr.buf = nil - return buf, nil - } - return itr.itr.Next() -} - -// NextInWindow returns the next value if it is between [startTime, endTime). -// If the next value is outside the range then it is moved to the buffer. -func (itr *bufFloatIterator) NextInWindow(startTime, endTime int64) (*FloatPoint, error) { - v, err := itr.Next() - if v == nil || err != nil { - return nil, err - } else if t := v.Time; t >= endTime || t < startTime { - itr.unread(v) - return nil, nil - } - return v, nil -} - -// unread sets v to the buffer. It is read on the next call to Next(). -func (itr *bufFloatIterator) unread(v *FloatPoint) { itr.buf = v } - -// floatMergeIterator represents an iterator that combines multiple float iterators. -type floatMergeIterator struct { - inputs []FloatIterator - heap *floatMergeHeap - init bool - - // Current iterator and window. - curr *floatMergeHeapItem - window struct { - name string - tags string - startTime int64 - endTime int64 - } -} - -// newFloatMergeIterator returns a new instance of floatMergeIterator. -func newFloatMergeIterator(inputs []FloatIterator, opt IteratorOptions) *floatMergeIterator { - itr := &floatMergeIterator{ - inputs: inputs, - heap: &floatMergeHeap{ - items: make([]*floatMergeHeapItem, 0, len(inputs)), - opt: opt, - }, - } - - // Initialize heap items. - for _, input := range inputs { - // Wrap in buffer, ignore any inputs without anymore points. - bufInput := newBufFloatIterator(input) - - // Append to the heap. - itr.heap.items = append(itr.heap.items, &floatMergeHeapItem{itr: bufInput}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *floatMergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *floatMergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - itr.curr = nil - itr.inputs = nil - itr.heap.items = nil - return nil -} - -// Next returns the next point from the iterator. -func (itr *floatMergeIterator) Next() (*FloatPoint, error) { - // Initialize the heap. This needs to be done lazily on the first call to this iterator - // so that iterator initialization done through the Select() call returns quickly. - // Queries can only be interrupted after the Select() call completes so any operations - // done during iterator creation cannot be interrupted, which is why we do it here - // instead so an interrupt can happen while initializing the heap. - if !itr.init { - items := itr.heap.items - itr.heap.items = make([]*floatMergeHeapItem, 0, len(items)) - for _, item := range items { - if p, err := item.itr.peek(); err != nil { - return nil, err - } else if p == nil { - continue - } - itr.heap.items = append(itr.heap.items, item) - } - heap.Init(itr.heap) - itr.init = true - } - - for { - // Retrieve the next iterator if we don't have one. - if itr.curr == nil { - if len(itr.heap.items) == 0 { - return nil, nil - } - itr.curr = heap.Pop(itr.heap).(*floatMergeHeapItem) - - // Read point and set current window. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags.ID() - itr.window.startTime, itr.window.endTime = itr.heap.opt.Window(p.Time) - return p, nil - } - - // Read the next point from the current iterator. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - - // If there are no more points then remove iterator from heap and find next. - if p == nil { - itr.curr = nil - continue - } - - // Check if the point is inside of our current window. - inWindow := true - if window := itr.window; window.name != p.Name { - inWindow = false - } else if window.tags != p.Tags.ID() { - inWindow = false - } else if opt := itr.heap.opt; opt.Ascending && p.Time >= window.endTime { - inWindow = false - } else if !opt.Ascending && p.Time < window.startTime { - inWindow = false - } - - // If it's outside our window then push iterator back on the heap and find new iterator. - if !inWindow { - itr.curr.itr.unread(p) - heap.Push(itr.heap, itr.curr) - itr.curr = nil - continue - } - - return p, nil - } -} - -// floatMergeHeap represents a heap of floatMergeHeapItems. -// Items are sorted by their next window and then by name/tags. -type floatMergeHeap struct { - opt IteratorOptions - items []*floatMergeHeapItem -} - -func (h floatMergeHeap) Len() int { return len(h.items) } -func (h floatMergeHeap) Swap(i, j int) { h.items[i], h.items[j] = h.items[j], h.items[i] } -func (h floatMergeHeap) Less(i, j int) bool { - x, err := h.items[i].itr.peek() - if err != nil { - return true - } - y, err := h.items[j].itr.peek() - if err != nil { - return false - } - - if h.opt.Ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() < y.Tags.ID() - } - } else { - if x.Name != y.Name { - return x.Name > y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() > y.Tags.ID() - } - } - - xt, _ := h.opt.Window(x.Time) - yt, _ := h.opt.Window(y.Time) - - if h.opt.Ascending { - return xt < yt - } - return xt > yt -} - -func (h *floatMergeHeap) Push(x interface{}) { - h.items = append(h.items, x.(*floatMergeHeapItem)) -} - -func (h *floatMergeHeap) Pop() interface{} { - old := h.items - n := len(old) - item := old[n-1] - h.items = old[0 : n-1] - return item -} - -type floatMergeHeapItem struct { - itr *bufFloatIterator -} - -// floatSortedMergeIterator is an iterator that sorts and merges multiple iterators into one. -type floatSortedMergeIterator struct { - inputs []FloatIterator - opt IteratorOptions - heap floatSortedMergeHeap - init bool -} - -// newFloatSortedMergeIterator returns an instance of floatSortedMergeIterator. -func newFloatSortedMergeIterator(inputs []FloatIterator, opt IteratorOptions) Iterator { - itr := &floatSortedMergeIterator{ - inputs: inputs, - heap: make(floatSortedMergeHeap, 0, len(inputs)), - opt: opt, - } - - // Initialize heap items. - for _, input := range inputs { - // Append to the heap. - itr.heap = append(itr.heap, &floatSortedMergeHeapItem{itr: input, ascending: opt.Ascending}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *floatSortedMergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *floatSortedMergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - return nil -} - -// Next returns the next points from the iterator. -func (itr *floatSortedMergeIterator) Next() (*FloatPoint, error) { return itr.pop() } - -// pop returns the next point from the heap. -// Reads the next point from item's cursor and puts it back on the heap. -func (itr *floatSortedMergeIterator) pop() (*FloatPoint, error) { - // Initialize the heap. See the MergeIterator to see why this has to be done lazily. - if !itr.init { - items := itr.heap - itr.heap = make([]*floatSortedMergeHeapItem, 0, len(items)) - for _, item := range items { - var err error - if item.point, err = item.itr.Next(); err != nil { - return nil, err - } else if item.point == nil { - continue - } - itr.heap = append(itr.heap, item) - } - heap.Init(&itr.heap) - itr.init = true - } - - if len(itr.heap) == 0 { - return nil, nil - } - - // Read the next item from the heap. - item := heap.Pop(&itr.heap).(*floatSortedMergeHeapItem) - if item.err != nil { - return nil, item.err - } else if item.point == nil { - return nil, nil - } - - // Copy the point for return. - p := item.point.Clone() - - // Read the next item from the cursor. Push back to heap if one exists. - if item.point, item.err = item.itr.Next(); item.point != nil { - heap.Push(&itr.heap, item) - } - - return p, nil -} - -// floatSortedMergeHeap represents a heap of floatSortedMergeHeapItems. -type floatSortedMergeHeap []*floatSortedMergeHeapItem - -func (h floatSortedMergeHeap) Len() int { return len(h) } -func (h floatSortedMergeHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h floatSortedMergeHeap) Less(i, j int) bool { - x, y := h[i].point, h[j].point - - if h[i].ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() < y.Tags.ID() - } - return x.Time < y.Time - } - - if x.Name != y.Name { - return x.Name > y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() > y.Tags.ID() - } - return x.Time > y.Time -} - -func (h *floatSortedMergeHeap) Push(x interface{}) { - *h = append(*h, x.(*floatSortedMergeHeapItem)) -} - -func (h *floatSortedMergeHeap) Pop() interface{} { - old := *h - n := len(old) - item := old[n-1] - *h = old[0 : n-1] - return item -} - -type floatSortedMergeHeapItem struct { - point *FloatPoint - err error - itr FloatIterator - ascending bool -} - -// floatParallelIterator represents an iterator that pulls data in a separate goroutine. -type floatParallelIterator struct { - input FloatIterator - ch chan floatPointError - - once sync.Once - closing chan struct{} - wg sync.WaitGroup -} - -// newFloatParallelIterator returns a new instance of floatParallelIterator. -func newFloatParallelIterator(input FloatIterator) *floatParallelIterator { - itr := &floatParallelIterator{ - input: input, - ch: make(chan floatPointError, 1), - closing: make(chan struct{}), - } - itr.wg.Add(1) - go itr.monitor() - return itr -} - -// Stats returns stats from the underlying iterator. -func (itr *floatParallelIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *floatParallelIterator) Close() error { - itr.once.Do(func() { close(itr.closing) }) - itr.wg.Wait() - return itr.input.Close() -} - -// Next returns the next point from the iterator. -func (itr *floatParallelIterator) Next() (*FloatPoint, error) { - v, ok := <-itr.ch - if !ok { - return nil, io.EOF - } - return v.point, v.err -} - -// monitor runs in a separate goroutine and actively pulls the next point. -func (itr *floatParallelIterator) monitor() { - defer close(itr.ch) - defer itr.wg.Done() - - for { - // Read next point. - p, err := itr.input.Next() - - select { - case <-itr.closing: - return - case itr.ch <- floatPointError{point: p, err: err}: - } - } -} - -type floatPointError struct { - point *FloatPoint - err error -} - -// floatLimitIterator represents an iterator that limits points per group. -type floatLimitIterator struct { - input FloatIterator - opt IteratorOptions - n int - - prev struct { - name string - tags Tags - } -} - -// newFloatLimitIterator returns a new instance of floatLimitIterator. -func newFloatLimitIterator(input FloatIterator, opt IteratorOptions) *floatLimitIterator { - return &floatLimitIterator{ - input: input, - opt: opt, - } -} - -// Stats returns stats from the underlying iterator. -func (itr *floatLimitIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *floatLimitIterator) Close() error { return itr.input.Close() } - -// Next returns the next point from the iterator. -func (itr *floatLimitIterator) Next() (*FloatPoint, error) { - for { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Reset window and counter if a new window is encountered. - if p.Name != itr.prev.name || !p.Tags.Equals(&itr.prev.tags) { - itr.prev.name = p.Name - itr.prev.tags = p.Tags - itr.n = 0 - } - - // Increment counter. - itr.n++ - - // Read next point if not beyond the offset. - if itr.n <= itr.opt.Offset { - continue - } - - // Read next point if we're beyond the limit. - if itr.opt.Limit > 0 && (itr.n-itr.opt.Offset) > itr.opt.Limit { - // If there's no interval, no groups, and a single source then simply exit. - if itr.opt.Interval.IsZero() && len(itr.opt.Dimensions) == 0 && len(itr.opt.Sources) == 1 { - return nil, nil - } - continue - } - - return p, nil - } -} - -type floatFillIterator struct { - input *bufFloatIterator - prev FloatPoint - startTime int64 - endTime int64 - auxFields []interface{} - init bool - opt IteratorOptions - - window struct { - name string - tags Tags - time int64 - } -} - -func newFloatFillIterator(input FloatIterator, expr Expr, opt IteratorOptions) *floatFillIterator { - if opt.Fill == NullFill { - if expr, ok := expr.(*Call); ok && expr.Name == "count" { - opt.Fill = NumberFill - opt.FillValue = float64(0) - } - } - - var startTime, endTime int64 - if opt.Ascending { - startTime, _ = opt.Window(opt.StartTime) - endTime, _ = opt.Window(opt.EndTime) - } else { - startTime, _ = opt.Window(opt.EndTime) - endTime, _ = opt.Window(opt.StartTime) - } - - var auxFields []interface{} - if len(opt.Aux) > 0 { - auxFields = make([]interface{}, len(opt.Aux)) - } - - return &floatFillIterator{ - input: newBufFloatIterator(input), - prev: FloatPoint{Nil: true}, - startTime: startTime, - endTime: endTime, - auxFields: auxFields, - opt: opt, - } -} - -func (itr *floatFillIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *floatFillIterator) Close() error { return itr.input.Close() } - -func (itr *floatFillIterator) Next() (*FloatPoint, error) { - if !itr.init { - p, err := itr.input.peek() - if p == nil || err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.init = true - } - - p, err := itr.input.Next() - if err != nil { - return nil, err - } - - // Check if the next point is outside of our window or is nil. - for p == nil || p.Name != itr.window.name || p.Tags.ID() != itr.window.tags.ID() { - // If we are inside of an interval, unread the point and continue below to - // constructing a new point. - if itr.opt.Ascending { - if itr.window.time <= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } else { - if itr.window.time >= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } - - // We are *not* in a current interval. If there is no next point, - // we are at the end of all intervals. - if p == nil { - return nil, nil - } - - // Set the new interval. - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.prev = FloatPoint{Nil: true} - break - } - - // Check if the point is our next expected point. - if p == nil || (itr.opt.Ascending && p.Time > itr.window.time) || (!itr.opt.Ascending && p.Time < itr.window.time) { - if p != nil { - itr.input.unread(p) - } - - p = &FloatPoint{ - Name: itr.window.name, - Tags: itr.window.tags, - Time: itr.window.time, - Aux: itr.auxFields, - } - - switch itr.opt.Fill { - case LinearFill: - if !itr.prev.Nil { - next, err := itr.input.peek() - if err != nil { - return nil, err - } - if next != nil { - interval := int64(itr.opt.Interval.Duration) - start := itr.window.time / interval - p.Value = linearFloat(start, itr.prev.Time/interval, next.Time/interval, itr.prev.Value, next.Value) - } else { - p.Nil = true - } - } else { - p.Nil = true - } - - case NullFill: - p.Nil = true - case NumberFill: - p.Value = castToFloat(itr.opt.FillValue) - case PreviousFill: - if !itr.prev.Nil { - p.Value = itr.prev.Value - p.Nil = itr.prev.Nil - } else { - p.Nil = true - } - } - } else { - itr.prev = *p - } - - // Advance the expected time. Do not advance to a new window here - // as there may be lingering points with the same timestamp in the previous - // window. - if itr.opt.Ascending { - itr.window.time = p.Time + int64(itr.opt.Interval.Duration) - } else { - itr.window.time = p.Time - int64(itr.opt.Interval.Duration) - } - return p, nil -} - -// floatIntervalIterator represents a float implementation of IntervalIterator. -type floatIntervalIterator struct { - input FloatIterator - opt IteratorOptions -} - -func newFloatIntervalIterator(input FloatIterator, opt IteratorOptions) *floatIntervalIterator { - return &floatIntervalIterator{input: input, opt: opt} -} - -func (itr *floatIntervalIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *floatIntervalIterator) Close() error { return itr.input.Close() } - -func (itr *floatIntervalIterator) Next() (*FloatPoint, error) { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - p.Time, _ = itr.opt.Window(p.Time) - // If we see the minimum allowable time, set the time to zero so we don't - // break the default returned time for aggregate queries without times. - if p.Time == MinTime { - p.Time = 0 - } - return p, nil -} - -// floatInterruptIterator represents a float implementation of InterruptIterator. -type floatInterruptIterator struct { - input FloatIterator - closing <-chan struct{} - count int -} - -func newFloatInterruptIterator(input FloatIterator, closing <-chan struct{}) *floatInterruptIterator { - return &floatInterruptIterator{input: input, closing: closing} -} - -func (itr *floatInterruptIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *floatInterruptIterator) Close() error { return itr.input.Close() } - -func (itr *floatInterruptIterator) Next() (*FloatPoint, error) { - // Only check if the channel is closed every N points. This - // intentionally checks on both 0 and N so that if the iterator - // has been interrupted before the first point is emitted it will - // not emit any points. - if itr.count&0xFF == 0xFF { - select { - case <-itr.closing: - return nil, nil - default: - // Reset iterator count to zero and fall through to emit the next point. - itr.count = 0 - } - } - - // Increment the counter for every point read. - itr.count++ - return itr.input.Next() -} - -// floatCloseInterruptIterator represents a float implementation of CloseInterruptIterator. -type floatCloseInterruptIterator struct { - input FloatIterator - closing <-chan struct{} - done chan struct{} - once sync.Once -} - -func newFloatCloseInterruptIterator(input FloatIterator, closing <-chan struct{}) *floatCloseInterruptIterator { - itr := &floatCloseInterruptIterator{ - input: input, - closing: closing, - done: make(chan struct{}), - } - go itr.monitor() - return itr -} - -func (itr *floatCloseInterruptIterator) monitor() { - select { - case <-itr.closing: - itr.Close() - case <-itr.done: - } -} - -func (itr *floatCloseInterruptIterator) Stats() IteratorStats { - return itr.input.Stats() -} - -func (itr *floatCloseInterruptIterator) Close() error { - itr.once.Do(func() { - close(itr.done) - itr.input.Close() - }) - return nil -} - -func (itr *floatCloseInterruptIterator) Next() (*FloatPoint, error) { - p, err := itr.input.Next() - if err != nil { - // Check if the iterator was closed. - select { - case <-itr.done: - return nil, nil - default: - return nil, err - } - } - return p, nil -} - -// auxFloatPoint represents a combination of a point and an error for the AuxIterator. -type auxFloatPoint struct { - point *FloatPoint - err error -} - -// floatAuxIterator represents a float implementation of AuxIterator. -type floatAuxIterator struct { - input *bufFloatIterator - output chan auxFloatPoint - fields auxIteratorFields - background bool -} - -func newFloatAuxIterator(input FloatIterator, opt IteratorOptions) *floatAuxIterator { - return &floatAuxIterator{ - input: newBufFloatIterator(input), - output: make(chan auxFloatPoint, 1), - fields: newAuxIteratorFields(opt), - } -} - -func (itr *floatAuxIterator) Background() { - itr.background = true - itr.Start() - go DrainIterator(itr) -} - -func (itr *floatAuxIterator) Start() { go itr.stream() } -func (itr *floatAuxIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *floatAuxIterator) Close() error { return itr.input.Close() } -func (itr *floatAuxIterator) Next() (*FloatPoint, error) { - p := <-itr.output - return p.point, p.err -} -func (itr *floatAuxIterator) Iterator(name string, typ DataType) Iterator { - return itr.fields.iterator(name, typ) -} - -func (itr *floatAuxIterator) CreateIterator(opt IteratorOptions) (Iterator, error) { - expr := opt.Expr - if expr == nil { - panic("unable to create an iterator with no expression from an aux iterator") - } - - switch expr := expr.(type) { - case *VarRef: - return itr.Iterator(expr.Val, expr.Type), nil - default: - panic(fmt.Sprintf("invalid expression type for an aux iterator: %T", expr)) - } -} - -func (itr *floatAuxIterator) FieldDimensions(sources Sources) (fields map[string]DataType, dimensions map[string]struct{}, err error) { - return nil, nil, errors.New("not implemented") -} - -func (itr *floatAuxIterator) ExpandSources(sources Sources) (Sources, error) { - return nil, errors.New("not implemented") -} - -func (itr *floatAuxIterator) stream() { - for { - // Read next point. - p, err := itr.input.Next() - if err != nil { - itr.output <- auxFloatPoint{err: err} - itr.fields.sendError(err) - break - } else if p == nil { - break - } - - // Send point to output and to each field iterator. - itr.output <- auxFloatPoint{point: p} - if ok := itr.fields.send(p); !ok && itr.background { - break - } - } - - close(itr.output) - itr.fields.close() -} - -// floatChanIterator represents a new instance of floatChanIterator. -type floatChanIterator struct { - buf struct { - i int - filled bool - points [2]FloatPoint - } - err error - cond *sync.Cond - done bool -} - -func (itr *floatChanIterator) Stats() IteratorStats { return IteratorStats{} } - -func (itr *floatChanIterator) Close() error { - itr.cond.L.Lock() - // Mark the channel iterator as done and signal all waiting goroutines to start again. - itr.done = true - itr.cond.Broadcast() - // Do not defer the unlock so we don't create an unnecessary allocation. - itr.cond.L.Unlock() - return nil -} - -func (itr *floatChanIterator) setBuf(name string, tags Tags, time int64, value interface{}) bool { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Wait for either the iterator to be done (so we don't have to set the value) - // or for the buffer to have been read and ready for another write. - for !itr.done && itr.buf.filled { - itr.cond.Wait() - } - - // Do not set the value and return false to signal that the iterator is closed. - // Do this after the above wait as the above for loop may have exited because - // the iterator was closed. - if itr.done { - return false - } - - switch v := value.(type) { - case float64: - itr.buf.points[itr.buf.i] = FloatPoint{Name: name, Tags: tags, Time: time, Value: v} - - case int64: - itr.buf.points[itr.buf.i] = FloatPoint{Name: name, Tags: tags, Time: time, Value: float64(v)} - - default: - itr.buf.points[itr.buf.i] = FloatPoint{Name: name, Tags: tags, Time: time, Nil: true} - } - itr.buf.filled = true - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() - return true -} - -func (itr *floatChanIterator) setErr(err error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - itr.err = err - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() -} - -func (itr *floatChanIterator) Next() (*FloatPoint, error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Check for an error and return one if there. - if itr.err != nil { - return nil, itr.err - } - - // Wait until either a value is available in the buffer or - // the iterator is closed. - for !itr.done && !itr.buf.filled { - itr.cond.Wait() - } - - // Return nil once the channel is done and the buffer is empty. - if itr.done && !itr.buf.filled { - return nil, nil - } - - // Always read from the buffer if it exists, even if the iterator - // is closed. This prevents the last value from being truncated by - // the parent iterator. - p := &itr.buf.points[itr.buf.i] - itr.buf.i = (itr.buf.i + 1) % len(itr.buf.points) - itr.buf.filled = false - itr.cond.Signal() - return p, nil -} - -// floatReduceFloatIterator executes a reducer for every interval and buffers the result. -type floatReduceFloatIterator struct { - input *bufFloatIterator - create func() (FloatPointAggregator, FloatPointEmitter) - opt IteratorOptions - points []FloatPoint -} - -// Stats returns stats from the input iterator. -func (itr *floatReduceFloatIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatReduceFloatIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *floatReduceFloatIterator) Next() (*FloatPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// floatReduceFloatPoint stores the reduced data for a name/tag combination. -type floatReduceFloatPoint struct { - Name string - Tags Tags - Aggregator FloatPointAggregator - Emitter FloatPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *floatReduceFloatIterator) reduce() ([]FloatPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*floatReduceFloatPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &floatReduceFloatPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateFloat(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]FloatPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// floatStreamFloatIterator streams inputs into the iterator and emits points gradually. -type floatStreamFloatIterator struct { - input *bufFloatIterator - create func() (FloatPointAggregator, FloatPointEmitter) - opt IteratorOptions - m map[string]*floatReduceFloatPoint - points []FloatPoint -} - -// newFloatStreamFloatIterator returns a new instance of floatStreamFloatIterator. -func newFloatStreamFloatIterator(input FloatIterator, createFn func() (FloatPointAggregator, FloatPointEmitter), opt IteratorOptions) *floatStreamFloatIterator { - return &floatStreamFloatIterator{ - input: newBufFloatIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*floatReduceFloatPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *floatStreamFloatIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatStreamFloatIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *floatStreamFloatIterator) Next() (*FloatPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *floatStreamFloatIterator) reduce() ([]FloatPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &floatReduceFloatPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateFloat(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// floatExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type floatExprIterator struct { - left *bufFloatIterator - right *bufFloatIterator - fn floatExprFunc - points []FloatPoint // must be size 2 - storePrev bool -} - -func newFloatExprIterator(left, right FloatIterator, opt IteratorOptions, fn func(a, b float64) float64) *floatExprIterator { - var points []FloatPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []FloatPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToFloat(opt.FillValue) - points = []FloatPoint{{Value: value}, {Value: value}} - } - return &floatExprIterator{ - left: newBufFloatIterator(left), - right: newBufFloatIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *floatExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *floatExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *floatExprIterator) Next() (*FloatPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - if a.Nil { - return a, nil - } else if b.Nil { - return b, nil - } - a.Value = itr.fn(a.Value, b.Value) - return a, nil - - } -} - -// floatExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type floatExprFunc func(a, b float64) float64 - -// floatReduceIntegerIterator executes a reducer for every interval and buffers the result. -type floatReduceIntegerIterator struct { - input *bufFloatIterator - create func() (FloatPointAggregator, IntegerPointEmitter) - opt IteratorOptions - points []IntegerPoint -} - -// Stats returns stats from the input iterator. -func (itr *floatReduceIntegerIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatReduceIntegerIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *floatReduceIntegerIterator) Next() (*IntegerPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// floatReduceIntegerPoint stores the reduced data for a name/tag combination. -type floatReduceIntegerPoint struct { - Name string - Tags Tags - Aggregator FloatPointAggregator - Emitter IntegerPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *floatReduceIntegerIterator) reduce() ([]IntegerPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*floatReduceIntegerPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &floatReduceIntegerPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateFloat(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]IntegerPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// floatStreamIntegerIterator streams inputs into the iterator and emits points gradually. -type floatStreamIntegerIterator struct { - input *bufFloatIterator - create func() (FloatPointAggregator, IntegerPointEmitter) - opt IteratorOptions - m map[string]*floatReduceIntegerPoint - points []IntegerPoint -} - -// newFloatStreamIntegerIterator returns a new instance of floatStreamIntegerIterator. -func newFloatStreamIntegerIterator(input FloatIterator, createFn func() (FloatPointAggregator, IntegerPointEmitter), opt IteratorOptions) *floatStreamIntegerIterator { - return &floatStreamIntegerIterator{ - input: newBufFloatIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*floatReduceIntegerPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *floatStreamIntegerIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatStreamIntegerIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *floatStreamIntegerIterator) Next() (*IntegerPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *floatStreamIntegerIterator) reduce() ([]IntegerPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &floatReduceIntegerPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateFloat(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// floatIntegerExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type floatIntegerExprIterator struct { - left *bufFloatIterator - right *bufFloatIterator - fn floatIntegerExprFunc - points []FloatPoint // must be size 2 - storePrev bool -} - -func newFloatIntegerExprIterator(left, right FloatIterator, opt IteratorOptions, fn func(a, b float64) int64) *floatIntegerExprIterator { - var points []FloatPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []FloatPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToFloat(opt.FillValue) - points = []FloatPoint{{Value: value}, {Value: value}} - } - return &floatIntegerExprIterator{ - left: newBufFloatIterator(left), - right: newBufFloatIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *floatIntegerExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *floatIntegerExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *floatIntegerExprIterator) Next() (*IntegerPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &IntegerPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// floatIntegerExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type floatIntegerExprFunc func(a, b float64) int64 - -// floatReduceStringIterator executes a reducer for every interval and buffers the result. -type floatReduceStringIterator struct { - input *bufFloatIterator - create func() (FloatPointAggregator, StringPointEmitter) - opt IteratorOptions - points []StringPoint -} - -// Stats returns stats from the input iterator. -func (itr *floatReduceStringIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatReduceStringIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *floatReduceStringIterator) Next() (*StringPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// floatReduceStringPoint stores the reduced data for a name/tag combination. -type floatReduceStringPoint struct { - Name string - Tags Tags - Aggregator FloatPointAggregator - Emitter StringPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *floatReduceStringIterator) reduce() ([]StringPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*floatReduceStringPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &floatReduceStringPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateFloat(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]StringPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// floatStreamStringIterator streams inputs into the iterator and emits points gradually. -type floatStreamStringIterator struct { - input *bufFloatIterator - create func() (FloatPointAggregator, StringPointEmitter) - opt IteratorOptions - m map[string]*floatReduceStringPoint - points []StringPoint -} - -// newFloatStreamStringIterator returns a new instance of floatStreamStringIterator. -func newFloatStreamStringIterator(input FloatIterator, createFn func() (FloatPointAggregator, StringPointEmitter), opt IteratorOptions) *floatStreamStringIterator { - return &floatStreamStringIterator{ - input: newBufFloatIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*floatReduceStringPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *floatStreamStringIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatStreamStringIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *floatStreamStringIterator) Next() (*StringPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *floatStreamStringIterator) reduce() ([]StringPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &floatReduceStringPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateFloat(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// floatStringExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type floatStringExprIterator struct { - left *bufFloatIterator - right *bufFloatIterator - fn floatStringExprFunc - points []FloatPoint // must be size 2 - storePrev bool -} - -func newFloatStringExprIterator(left, right FloatIterator, opt IteratorOptions, fn func(a, b float64) string) *floatStringExprIterator { - var points []FloatPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []FloatPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToFloat(opt.FillValue) - points = []FloatPoint{{Value: value}, {Value: value}} - } - return &floatStringExprIterator{ - left: newBufFloatIterator(left), - right: newBufFloatIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *floatStringExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *floatStringExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *floatStringExprIterator) Next() (*StringPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &StringPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// floatStringExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type floatStringExprFunc func(a, b float64) string - -// floatReduceBooleanIterator executes a reducer for every interval and buffers the result. -type floatReduceBooleanIterator struct { - input *bufFloatIterator - create func() (FloatPointAggregator, BooleanPointEmitter) - opt IteratorOptions - points []BooleanPoint -} - -// Stats returns stats from the input iterator. -func (itr *floatReduceBooleanIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatReduceBooleanIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *floatReduceBooleanIterator) Next() (*BooleanPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// floatReduceBooleanPoint stores the reduced data for a name/tag combination. -type floatReduceBooleanPoint struct { - Name string - Tags Tags - Aggregator FloatPointAggregator - Emitter BooleanPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *floatReduceBooleanIterator) reduce() ([]BooleanPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*floatReduceBooleanPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &floatReduceBooleanPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateFloat(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]BooleanPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// floatStreamBooleanIterator streams inputs into the iterator and emits points gradually. -type floatStreamBooleanIterator struct { - input *bufFloatIterator - create func() (FloatPointAggregator, BooleanPointEmitter) - opt IteratorOptions - m map[string]*floatReduceBooleanPoint - points []BooleanPoint -} - -// newFloatStreamBooleanIterator returns a new instance of floatStreamBooleanIterator. -func newFloatStreamBooleanIterator(input FloatIterator, createFn func() (FloatPointAggregator, BooleanPointEmitter), opt IteratorOptions) *floatStreamBooleanIterator { - return &floatStreamBooleanIterator{ - input: newBufFloatIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*floatReduceBooleanPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *floatStreamBooleanIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatStreamBooleanIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *floatStreamBooleanIterator) Next() (*BooleanPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *floatStreamBooleanIterator) reduce() ([]BooleanPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &floatReduceBooleanPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateFloat(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// floatBooleanExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type floatBooleanExprIterator struct { - left *bufFloatIterator - right *bufFloatIterator - fn floatBooleanExprFunc - points []FloatPoint // must be size 2 - storePrev bool -} - -func newFloatBooleanExprIterator(left, right FloatIterator, opt IteratorOptions, fn func(a, b float64) bool) *floatBooleanExprIterator { - var points []FloatPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []FloatPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToFloat(opt.FillValue) - points = []FloatPoint{{Value: value}, {Value: value}} - } - return &floatBooleanExprIterator{ - left: newBufFloatIterator(left), - right: newBufFloatIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *floatBooleanExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *floatBooleanExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *floatBooleanExprIterator) Next() (*BooleanPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &BooleanPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// floatBooleanExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type floatBooleanExprFunc func(a, b float64) bool - -// floatTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type floatTransformIterator struct { - input FloatIterator - fn floatTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *floatTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *floatTransformIterator) Next() (*FloatPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - p = itr.fn(p) - } - return p, nil -} - -// floatTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type floatTransformFunc func(p *FloatPoint) *FloatPoint - -// floatBoolTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type floatBoolTransformIterator struct { - input FloatIterator - fn floatBoolTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *floatBoolTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatBoolTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *floatBoolTransformIterator) Next() (*BooleanPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - return itr.fn(p), nil - } - return nil, nil -} - -// floatBoolTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type floatBoolTransformFunc func(p *FloatPoint) *BooleanPoint - -// floatDedupeIterator only outputs unique points. -// This differs from the DistinctIterator in that it compares all aux fields too. -// This iterator is relatively inefficient and should only be used on small -// datasets such as meta query results. -type floatDedupeIterator struct { - input FloatIterator - m map[string]struct{} // lookup of points already sent -} - -// newFloatDedupeIterator returns a new instance of floatDedupeIterator. -func newFloatDedupeIterator(input FloatIterator) *floatDedupeIterator { - return &floatDedupeIterator{ - input: input, - m: make(map[string]struct{}), - } -} - -// Stats returns stats from the input iterator. -func (itr *floatDedupeIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatDedupeIterator) Close() error { return itr.input.Close() } - -// Next returns the next unique point from the input iterator. -func (itr *floatDedupeIterator) Next() (*FloatPoint, error) { - for { - // Read next point. - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Serialize to bytes to store in lookup. - buf, err := proto.Marshal(encodeFloatPoint(p)) - if err != nil { - return nil, err - } - - // If the point has already been output then move to the next point. - if _, ok := itr.m[string(buf)]; ok { - continue - } - - // Otherwise mark it as emitted and return point. - itr.m[string(buf)] = struct{}{} - return p, nil - } -} - -// floatReaderIterator represents an iterator that streams from a reader. -type floatReaderIterator struct { - r io.Reader - dec *FloatPointDecoder -} - -// newFloatReaderIterator returns a new instance of floatReaderIterator. -func newFloatReaderIterator(r io.Reader, stats IteratorStats) *floatReaderIterator { - dec := NewFloatPointDecoder(r) - dec.stats = stats - - return &floatReaderIterator{ - r: r, - dec: dec, - } -} - -// Stats returns stats about points processed. -func (itr *floatReaderIterator) Stats() IteratorStats { return itr.dec.stats } - -// Close closes the underlying reader, if applicable. -func (itr *floatReaderIterator) Close() error { - if r, ok := itr.r.(io.ReadCloser); ok { - return r.Close() - } - return nil -} - -// Next returns the next point from the iterator. -func (itr *floatReaderIterator) Next() (*FloatPoint, error) { - // OPTIMIZE(benbjohnson): Reuse point on iterator. - - // Unmarshal next point. - p := &FloatPoint{} - if err := itr.dec.DecodeFloatPoint(p); err == io.EOF { - return nil, nil - } else if err != nil { - return nil, err - } - return p, nil -} - -// IntegerIterator represents a stream of integer points. -type IntegerIterator interface { - Iterator - Next() (*IntegerPoint, error) -} - -// newIntegerIterators converts a slice of Iterator to a slice of IntegerIterator. -// Drop and closes any iterator in itrs that is not a IntegerIterator and cannot -// be cast to a IntegerIterator. -func newIntegerIterators(itrs []Iterator) []IntegerIterator { - a := make([]IntegerIterator, 0, len(itrs)) - for _, itr := range itrs { - switch itr := itr.(type) { - case IntegerIterator: - a = append(a, itr) - - default: - itr.Close() - } - } - return a -} - -// bufIntegerIterator represents a buffered IntegerIterator. -type bufIntegerIterator struct { - itr IntegerIterator - buf *IntegerPoint -} - -// newBufIntegerIterator returns a buffered IntegerIterator. -func newBufIntegerIterator(itr IntegerIterator) *bufIntegerIterator { - return &bufIntegerIterator{itr: itr} -} - -// Stats returns statistics from the input iterator. -func (itr *bufIntegerIterator) Stats() IteratorStats { return itr.itr.Stats() } - -// Close closes the underlying iterator. -func (itr *bufIntegerIterator) Close() error { return itr.itr.Close() } - -// peek returns the next point without removing it from the iterator. -func (itr *bufIntegerIterator) peek() (*IntegerPoint, error) { - p, err := itr.Next() - if err != nil { - return nil, err - } - itr.unread(p) - return p, nil -} - -// peekTime returns the time of the next point. -// Returns zero time if no more points available. -func (itr *bufIntegerIterator) peekTime() (int64, error) { - p, err := itr.peek() - if p == nil || err != nil { - return ZeroTime, err - } - return p.Time, nil -} - -// Next returns the current buffer, if exists, or calls the underlying iterator. -func (itr *bufIntegerIterator) Next() (*IntegerPoint, error) { - buf := itr.buf - if buf != nil { - itr.buf = nil - return buf, nil - } - return itr.itr.Next() -} - -// NextInWindow returns the next value if it is between [startTime, endTime). -// If the next value is outside the range then it is moved to the buffer. -func (itr *bufIntegerIterator) NextInWindow(startTime, endTime int64) (*IntegerPoint, error) { - v, err := itr.Next() - if v == nil || err != nil { - return nil, err - } else if t := v.Time; t >= endTime || t < startTime { - itr.unread(v) - return nil, nil - } - return v, nil -} - -// unread sets v to the buffer. It is read on the next call to Next(). -func (itr *bufIntegerIterator) unread(v *IntegerPoint) { itr.buf = v } - -// integerMergeIterator represents an iterator that combines multiple integer iterators. -type integerMergeIterator struct { - inputs []IntegerIterator - heap *integerMergeHeap - init bool - - // Current iterator and window. - curr *integerMergeHeapItem - window struct { - name string - tags string - startTime int64 - endTime int64 - } -} - -// newIntegerMergeIterator returns a new instance of integerMergeIterator. -func newIntegerMergeIterator(inputs []IntegerIterator, opt IteratorOptions) *integerMergeIterator { - itr := &integerMergeIterator{ - inputs: inputs, - heap: &integerMergeHeap{ - items: make([]*integerMergeHeapItem, 0, len(inputs)), - opt: opt, - }, - } - - // Initialize heap items. - for _, input := range inputs { - // Wrap in buffer, ignore any inputs without anymore points. - bufInput := newBufIntegerIterator(input) - - // Append to the heap. - itr.heap.items = append(itr.heap.items, &integerMergeHeapItem{itr: bufInput}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *integerMergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *integerMergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - itr.curr = nil - itr.inputs = nil - itr.heap.items = nil - return nil -} - -// Next returns the next point from the iterator. -func (itr *integerMergeIterator) Next() (*IntegerPoint, error) { - // Initialize the heap. This needs to be done lazily on the first call to this iterator - // so that iterator initialization done through the Select() call returns quickly. - // Queries can only be interrupted after the Select() call completes so any operations - // done during iterator creation cannot be interrupted, which is why we do it here - // instead so an interrupt can happen while initializing the heap. - if !itr.init { - items := itr.heap.items - itr.heap.items = make([]*integerMergeHeapItem, 0, len(items)) - for _, item := range items { - if p, err := item.itr.peek(); err != nil { - return nil, err - } else if p == nil { - continue - } - itr.heap.items = append(itr.heap.items, item) - } - heap.Init(itr.heap) - itr.init = true - } - - for { - // Retrieve the next iterator if we don't have one. - if itr.curr == nil { - if len(itr.heap.items) == 0 { - return nil, nil - } - itr.curr = heap.Pop(itr.heap).(*integerMergeHeapItem) - - // Read point and set current window. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags.ID() - itr.window.startTime, itr.window.endTime = itr.heap.opt.Window(p.Time) - return p, nil - } - - // Read the next point from the current iterator. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - - // If there are no more points then remove iterator from heap and find next. - if p == nil { - itr.curr = nil - continue - } - - // Check if the point is inside of our current window. - inWindow := true - if window := itr.window; window.name != p.Name { - inWindow = false - } else if window.tags != p.Tags.ID() { - inWindow = false - } else if opt := itr.heap.opt; opt.Ascending && p.Time >= window.endTime { - inWindow = false - } else if !opt.Ascending && p.Time < window.startTime { - inWindow = false - } - - // If it's outside our window then push iterator back on the heap and find new iterator. - if !inWindow { - itr.curr.itr.unread(p) - heap.Push(itr.heap, itr.curr) - itr.curr = nil - continue - } - - return p, nil - } -} - -// integerMergeHeap represents a heap of integerMergeHeapItems. -// Items are sorted by their next window and then by name/tags. -type integerMergeHeap struct { - opt IteratorOptions - items []*integerMergeHeapItem -} - -func (h integerMergeHeap) Len() int { return len(h.items) } -func (h integerMergeHeap) Swap(i, j int) { h.items[i], h.items[j] = h.items[j], h.items[i] } -func (h integerMergeHeap) Less(i, j int) bool { - x, err := h.items[i].itr.peek() - if err != nil { - return true - } - y, err := h.items[j].itr.peek() - if err != nil { - return false - } - - if h.opt.Ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() < y.Tags.ID() - } - } else { - if x.Name != y.Name { - return x.Name > y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() > y.Tags.ID() - } - } - - xt, _ := h.opt.Window(x.Time) - yt, _ := h.opt.Window(y.Time) - - if h.opt.Ascending { - return xt < yt - } - return xt > yt -} - -func (h *integerMergeHeap) Push(x interface{}) { - h.items = append(h.items, x.(*integerMergeHeapItem)) -} - -func (h *integerMergeHeap) Pop() interface{} { - old := h.items - n := len(old) - item := old[n-1] - h.items = old[0 : n-1] - return item -} - -type integerMergeHeapItem struct { - itr *bufIntegerIterator -} - -// integerSortedMergeIterator is an iterator that sorts and merges multiple iterators into one. -type integerSortedMergeIterator struct { - inputs []IntegerIterator - opt IteratorOptions - heap integerSortedMergeHeap - init bool -} - -// newIntegerSortedMergeIterator returns an instance of integerSortedMergeIterator. -func newIntegerSortedMergeIterator(inputs []IntegerIterator, opt IteratorOptions) Iterator { - itr := &integerSortedMergeIterator{ - inputs: inputs, - heap: make(integerSortedMergeHeap, 0, len(inputs)), - opt: opt, - } - - // Initialize heap items. - for _, input := range inputs { - // Append to the heap. - itr.heap = append(itr.heap, &integerSortedMergeHeapItem{itr: input, ascending: opt.Ascending}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *integerSortedMergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *integerSortedMergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - return nil -} - -// Next returns the next points from the iterator. -func (itr *integerSortedMergeIterator) Next() (*IntegerPoint, error) { return itr.pop() } - -// pop returns the next point from the heap. -// Reads the next point from item's cursor and puts it back on the heap. -func (itr *integerSortedMergeIterator) pop() (*IntegerPoint, error) { - // Initialize the heap. See the MergeIterator to see why this has to be done lazily. - if !itr.init { - items := itr.heap - itr.heap = make([]*integerSortedMergeHeapItem, 0, len(items)) - for _, item := range items { - var err error - if item.point, err = item.itr.Next(); err != nil { - return nil, err - } else if item.point == nil { - continue - } - itr.heap = append(itr.heap, item) - } - heap.Init(&itr.heap) - itr.init = true - } - - if len(itr.heap) == 0 { - return nil, nil - } - - // Read the next item from the heap. - item := heap.Pop(&itr.heap).(*integerSortedMergeHeapItem) - if item.err != nil { - return nil, item.err - } else if item.point == nil { - return nil, nil - } - - // Copy the point for return. - p := item.point.Clone() - - // Read the next item from the cursor. Push back to heap if one exists. - if item.point, item.err = item.itr.Next(); item.point != nil { - heap.Push(&itr.heap, item) - } - - return p, nil -} - -// integerSortedMergeHeap represents a heap of integerSortedMergeHeapItems. -type integerSortedMergeHeap []*integerSortedMergeHeapItem - -func (h integerSortedMergeHeap) Len() int { return len(h) } -func (h integerSortedMergeHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h integerSortedMergeHeap) Less(i, j int) bool { - x, y := h[i].point, h[j].point - - if h[i].ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() < y.Tags.ID() - } - return x.Time < y.Time - } - - if x.Name != y.Name { - return x.Name > y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() > y.Tags.ID() - } - return x.Time > y.Time -} - -func (h *integerSortedMergeHeap) Push(x interface{}) { - *h = append(*h, x.(*integerSortedMergeHeapItem)) -} - -func (h *integerSortedMergeHeap) Pop() interface{} { - old := *h - n := len(old) - item := old[n-1] - *h = old[0 : n-1] - return item -} - -type integerSortedMergeHeapItem struct { - point *IntegerPoint - err error - itr IntegerIterator - ascending bool -} - -// integerParallelIterator represents an iterator that pulls data in a separate goroutine. -type integerParallelIterator struct { - input IntegerIterator - ch chan integerPointError - - once sync.Once - closing chan struct{} - wg sync.WaitGroup -} - -// newIntegerParallelIterator returns a new instance of integerParallelIterator. -func newIntegerParallelIterator(input IntegerIterator) *integerParallelIterator { - itr := &integerParallelIterator{ - input: input, - ch: make(chan integerPointError, 1), - closing: make(chan struct{}), - } - itr.wg.Add(1) - go itr.monitor() - return itr -} - -// Stats returns stats from the underlying iterator. -func (itr *integerParallelIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *integerParallelIterator) Close() error { - itr.once.Do(func() { close(itr.closing) }) - itr.wg.Wait() - return itr.input.Close() -} - -// Next returns the next point from the iterator. -func (itr *integerParallelIterator) Next() (*IntegerPoint, error) { - v, ok := <-itr.ch - if !ok { - return nil, io.EOF - } - return v.point, v.err -} - -// monitor runs in a separate goroutine and actively pulls the next point. -func (itr *integerParallelIterator) monitor() { - defer close(itr.ch) - defer itr.wg.Done() - - for { - // Read next point. - p, err := itr.input.Next() - - select { - case <-itr.closing: - return - case itr.ch <- integerPointError{point: p, err: err}: - } - } -} - -type integerPointError struct { - point *IntegerPoint - err error -} - -// integerLimitIterator represents an iterator that limits points per group. -type integerLimitIterator struct { - input IntegerIterator - opt IteratorOptions - n int - - prev struct { - name string - tags Tags - } -} - -// newIntegerLimitIterator returns a new instance of integerLimitIterator. -func newIntegerLimitIterator(input IntegerIterator, opt IteratorOptions) *integerLimitIterator { - return &integerLimitIterator{ - input: input, - opt: opt, - } -} - -// Stats returns stats from the underlying iterator. -func (itr *integerLimitIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *integerLimitIterator) Close() error { return itr.input.Close() } - -// Next returns the next point from the iterator. -func (itr *integerLimitIterator) Next() (*IntegerPoint, error) { - for { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Reset window and counter if a new window is encountered. - if p.Name != itr.prev.name || !p.Tags.Equals(&itr.prev.tags) { - itr.prev.name = p.Name - itr.prev.tags = p.Tags - itr.n = 0 - } - - // Increment counter. - itr.n++ - - // Read next point if not beyond the offset. - if itr.n <= itr.opt.Offset { - continue - } - - // Read next point if we're beyond the limit. - if itr.opt.Limit > 0 && (itr.n-itr.opt.Offset) > itr.opt.Limit { - // If there's no interval, no groups, and a single source then simply exit. - if itr.opt.Interval.IsZero() && len(itr.opt.Dimensions) == 0 && len(itr.opt.Sources) == 1 { - return nil, nil - } - continue - } - - return p, nil - } -} - -type integerFillIterator struct { - input *bufIntegerIterator - prev IntegerPoint - startTime int64 - endTime int64 - auxFields []interface{} - init bool - opt IteratorOptions - - window struct { - name string - tags Tags - time int64 - } -} - -func newIntegerFillIterator(input IntegerIterator, expr Expr, opt IteratorOptions) *integerFillIterator { - if opt.Fill == NullFill { - if expr, ok := expr.(*Call); ok && expr.Name == "count" { - opt.Fill = NumberFill - opt.FillValue = int64(0) - } - } - - var startTime, endTime int64 - if opt.Ascending { - startTime, _ = opt.Window(opt.StartTime) - endTime, _ = opt.Window(opt.EndTime) - } else { - startTime, _ = opt.Window(opt.EndTime) - endTime, _ = opt.Window(opt.StartTime) - } - - var auxFields []interface{} - if len(opt.Aux) > 0 { - auxFields = make([]interface{}, len(opt.Aux)) - } - - return &integerFillIterator{ - input: newBufIntegerIterator(input), - prev: IntegerPoint{Nil: true}, - startTime: startTime, - endTime: endTime, - auxFields: auxFields, - opt: opt, - } -} - -func (itr *integerFillIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *integerFillIterator) Close() error { return itr.input.Close() } - -func (itr *integerFillIterator) Next() (*IntegerPoint, error) { - if !itr.init { - p, err := itr.input.peek() - if p == nil || err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.init = true - } - - p, err := itr.input.Next() - if err != nil { - return nil, err - } - - // Check if the next point is outside of our window or is nil. - for p == nil || p.Name != itr.window.name || p.Tags.ID() != itr.window.tags.ID() { - // If we are inside of an interval, unread the point and continue below to - // constructing a new point. - if itr.opt.Ascending { - if itr.window.time <= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } else { - if itr.window.time >= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } - - // We are *not* in a current interval. If there is no next point, - // we are at the end of all intervals. - if p == nil { - return nil, nil - } - - // Set the new interval. - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.prev = IntegerPoint{Nil: true} - break - } - - // Check if the point is our next expected point. - if p == nil || (itr.opt.Ascending && p.Time > itr.window.time) || (!itr.opt.Ascending && p.Time < itr.window.time) { - if p != nil { - itr.input.unread(p) - } - - p = &IntegerPoint{ - Name: itr.window.name, - Tags: itr.window.tags, - Time: itr.window.time, - Aux: itr.auxFields, - } - - switch itr.opt.Fill { - case LinearFill: - if !itr.prev.Nil { - next, err := itr.input.peek() - if err != nil { - return nil, err - } - if next != nil { - interval := int64(itr.opt.Interval.Duration) - start := itr.window.time / interval - p.Value = linearInteger(start, itr.prev.Time/interval, next.Time/interval, itr.prev.Value, next.Value) - } else { - p.Nil = true - } - } else { - p.Nil = true - } - - case NullFill: - p.Nil = true - case NumberFill: - p.Value = castToInteger(itr.opt.FillValue) - case PreviousFill: - if !itr.prev.Nil { - p.Value = itr.prev.Value - p.Nil = itr.prev.Nil - } else { - p.Nil = true - } - } - } else { - itr.prev = *p - } - - // Advance the expected time. Do not advance to a new window here - // as there may be lingering points with the same timestamp in the previous - // window. - if itr.opt.Ascending { - itr.window.time = p.Time + int64(itr.opt.Interval.Duration) - } else { - itr.window.time = p.Time - int64(itr.opt.Interval.Duration) - } - return p, nil -} - -// integerIntervalIterator represents a integer implementation of IntervalIterator. -type integerIntervalIterator struct { - input IntegerIterator - opt IteratorOptions -} - -func newIntegerIntervalIterator(input IntegerIterator, opt IteratorOptions) *integerIntervalIterator { - return &integerIntervalIterator{input: input, opt: opt} -} - -func (itr *integerIntervalIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *integerIntervalIterator) Close() error { return itr.input.Close() } - -func (itr *integerIntervalIterator) Next() (*IntegerPoint, error) { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - p.Time, _ = itr.opt.Window(p.Time) - // If we see the minimum allowable time, set the time to zero so we don't - // break the default returned time for aggregate queries without times. - if p.Time == MinTime { - p.Time = 0 - } - return p, nil -} - -// integerInterruptIterator represents a integer implementation of InterruptIterator. -type integerInterruptIterator struct { - input IntegerIterator - closing <-chan struct{} - count int -} - -func newIntegerInterruptIterator(input IntegerIterator, closing <-chan struct{}) *integerInterruptIterator { - return &integerInterruptIterator{input: input, closing: closing} -} - -func (itr *integerInterruptIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *integerInterruptIterator) Close() error { return itr.input.Close() } - -func (itr *integerInterruptIterator) Next() (*IntegerPoint, error) { - // Only check if the channel is closed every N points. This - // intentionally checks on both 0 and N so that if the iterator - // has been interrupted before the first point is emitted it will - // not emit any points. - if itr.count&0xFF == 0xFF { - select { - case <-itr.closing: - return nil, nil - default: - // Reset iterator count to zero and fall through to emit the next point. - itr.count = 0 - } - } - - // Increment the counter for every point read. - itr.count++ - return itr.input.Next() -} - -// integerCloseInterruptIterator represents a integer implementation of CloseInterruptIterator. -type integerCloseInterruptIterator struct { - input IntegerIterator - closing <-chan struct{} - done chan struct{} - once sync.Once -} - -func newIntegerCloseInterruptIterator(input IntegerIterator, closing <-chan struct{}) *integerCloseInterruptIterator { - itr := &integerCloseInterruptIterator{ - input: input, - closing: closing, - done: make(chan struct{}), - } - go itr.monitor() - return itr -} - -func (itr *integerCloseInterruptIterator) monitor() { - select { - case <-itr.closing: - itr.Close() - case <-itr.done: - } -} - -func (itr *integerCloseInterruptIterator) Stats() IteratorStats { - return itr.input.Stats() -} - -func (itr *integerCloseInterruptIterator) Close() error { - itr.once.Do(func() { - close(itr.done) - itr.input.Close() - }) - return nil -} - -func (itr *integerCloseInterruptIterator) Next() (*IntegerPoint, error) { - p, err := itr.input.Next() - if err != nil { - // Check if the iterator was closed. - select { - case <-itr.done: - return nil, nil - default: - return nil, err - } - } - return p, nil -} - -// auxIntegerPoint represents a combination of a point and an error for the AuxIterator. -type auxIntegerPoint struct { - point *IntegerPoint - err error -} - -// integerAuxIterator represents a integer implementation of AuxIterator. -type integerAuxIterator struct { - input *bufIntegerIterator - output chan auxIntegerPoint - fields auxIteratorFields - background bool -} - -func newIntegerAuxIterator(input IntegerIterator, opt IteratorOptions) *integerAuxIterator { - return &integerAuxIterator{ - input: newBufIntegerIterator(input), - output: make(chan auxIntegerPoint, 1), - fields: newAuxIteratorFields(opt), - } -} - -func (itr *integerAuxIterator) Background() { - itr.background = true - itr.Start() - go DrainIterator(itr) -} - -func (itr *integerAuxIterator) Start() { go itr.stream() } -func (itr *integerAuxIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *integerAuxIterator) Close() error { return itr.input.Close() } -func (itr *integerAuxIterator) Next() (*IntegerPoint, error) { - p := <-itr.output - return p.point, p.err -} -func (itr *integerAuxIterator) Iterator(name string, typ DataType) Iterator { - return itr.fields.iterator(name, typ) -} - -func (itr *integerAuxIterator) CreateIterator(opt IteratorOptions) (Iterator, error) { - expr := opt.Expr - if expr == nil { - panic("unable to create an iterator with no expression from an aux iterator") - } - - switch expr := expr.(type) { - case *VarRef: - return itr.Iterator(expr.Val, expr.Type), nil - default: - panic(fmt.Sprintf("invalid expression type for an aux iterator: %T", expr)) - } -} - -func (itr *integerAuxIterator) FieldDimensions(sources Sources) (fields map[string]DataType, dimensions map[string]struct{}, err error) { - return nil, nil, errors.New("not implemented") -} - -func (itr *integerAuxIterator) ExpandSources(sources Sources) (Sources, error) { - return nil, errors.New("not implemented") -} - -func (itr *integerAuxIterator) stream() { - for { - // Read next point. - p, err := itr.input.Next() - if err != nil { - itr.output <- auxIntegerPoint{err: err} - itr.fields.sendError(err) - break - } else if p == nil { - break - } - - // Send point to output and to each field iterator. - itr.output <- auxIntegerPoint{point: p} - if ok := itr.fields.send(p); !ok && itr.background { - break - } - } - - close(itr.output) - itr.fields.close() -} - -// integerChanIterator represents a new instance of integerChanIterator. -type integerChanIterator struct { - buf struct { - i int - filled bool - points [2]IntegerPoint - } - err error - cond *sync.Cond - done bool -} - -func (itr *integerChanIterator) Stats() IteratorStats { return IteratorStats{} } - -func (itr *integerChanIterator) Close() error { - itr.cond.L.Lock() - // Mark the channel iterator as done and signal all waiting goroutines to start again. - itr.done = true - itr.cond.Broadcast() - // Do not defer the unlock so we don't create an unnecessary allocation. - itr.cond.L.Unlock() - return nil -} - -func (itr *integerChanIterator) setBuf(name string, tags Tags, time int64, value interface{}) bool { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Wait for either the iterator to be done (so we don't have to set the value) - // or for the buffer to have been read and ready for another write. - for !itr.done && itr.buf.filled { - itr.cond.Wait() - } - - // Do not set the value and return false to signal that the iterator is closed. - // Do this after the above wait as the above for loop may have exited because - // the iterator was closed. - if itr.done { - return false - } - - switch v := value.(type) { - case int64: - itr.buf.points[itr.buf.i] = IntegerPoint{Name: name, Tags: tags, Time: time, Value: v} - - default: - itr.buf.points[itr.buf.i] = IntegerPoint{Name: name, Tags: tags, Time: time, Nil: true} - } - itr.buf.filled = true - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() - return true -} - -func (itr *integerChanIterator) setErr(err error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - itr.err = err - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() -} - -func (itr *integerChanIterator) Next() (*IntegerPoint, error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Check for an error and return one if there. - if itr.err != nil { - return nil, itr.err - } - - // Wait until either a value is available in the buffer or - // the iterator is closed. - for !itr.done && !itr.buf.filled { - itr.cond.Wait() - } - - // Return nil once the channel is done and the buffer is empty. - if itr.done && !itr.buf.filled { - return nil, nil - } - - // Always read from the buffer if it exists, even if the iterator - // is closed. This prevents the last value from being truncated by - // the parent iterator. - p := &itr.buf.points[itr.buf.i] - itr.buf.i = (itr.buf.i + 1) % len(itr.buf.points) - itr.buf.filled = false - itr.cond.Signal() - return p, nil -} - -// integerReduceFloatIterator executes a reducer for every interval and buffers the result. -type integerReduceFloatIterator struct { - input *bufIntegerIterator - create func() (IntegerPointAggregator, FloatPointEmitter) - opt IteratorOptions - points []FloatPoint -} - -// Stats returns stats from the input iterator. -func (itr *integerReduceFloatIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerReduceFloatIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *integerReduceFloatIterator) Next() (*FloatPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// integerReduceFloatPoint stores the reduced data for a name/tag combination. -type integerReduceFloatPoint struct { - Name string - Tags Tags - Aggregator IntegerPointAggregator - Emitter FloatPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *integerReduceFloatIterator) reduce() ([]FloatPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*integerReduceFloatPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &integerReduceFloatPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateInteger(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]FloatPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// integerStreamFloatIterator streams inputs into the iterator and emits points gradually. -type integerStreamFloatIterator struct { - input *bufIntegerIterator - create func() (IntegerPointAggregator, FloatPointEmitter) - opt IteratorOptions - m map[string]*integerReduceFloatPoint - points []FloatPoint -} - -// newIntegerStreamFloatIterator returns a new instance of integerStreamFloatIterator. -func newIntegerStreamFloatIterator(input IntegerIterator, createFn func() (IntegerPointAggregator, FloatPointEmitter), opt IteratorOptions) *integerStreamFloatIterator { - return &integerStreamFloatIterator{ - input: newBufIntegerIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*integerReduceFloatPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *integerStreamFloatIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerStreamFloatIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *integerStreamFloatIterator) Next() (*FloatPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *integerStreamFloatIterator) reduce() ([]FloatPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &integerReduceFloatPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateInteger(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// integerFloatExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type integerFloatExprIterator struct { - left *bufIntegerIterator - right *bufIntegerIterator - fn integerFloatExprFunc - points []IntegerPoint // must be size 2 - storePrev bool -} - -func newIntegerFloatExprIterator(left, right IntegerIterator, opt IteratorOptions, fn func(a, b int64) float64) *integerFloatExprIterator { - var points []IntegerPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []IntegerPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToInteger(opt.FillValue) - points = []IntegerPoint{{Value: value}, {Value: value}} - } - return &integerFloatExprIterator{ - left: newBufIntegerIterator(left), - right: newBufIntegerIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *integerFloatExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *integerFloatExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *integerFloatExprIterator) Next() (*FloatPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &FloatPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// integerFloatExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type integerFloatExprFunc func(a, b int64) float64 - -// integerReduceIntegerIterator executes a reducer for every interval and buffers the result. -type integerReduceIntegerIterator struct { - input *bufIntegerIterator - create func() (IntegerPointAggregator, IntegerPointEmitter) - opt IteratorOptions - points []IntegerPoint -} - -// Stats returns stats from the input iterator. -func (itr *integerReduceIntegerIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerReduceIntegerIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *integerReduceIntegerIterator) Next() (*IntegerPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// integerReduceIntegerPoint stores the reduced data for a name/tag combination. -type integerReduceIntegerPoint struct { - Name string - Tags Tags - Aggregator IntegerPointAggregator - Emitter IntegerPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *integerReduceIntegerIterator) reduce() ([]IntegerPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*integerReduceIntegerPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &integerReduceIntegerPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateInteger(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]IntegerPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// integerStreamIntegerIterator streams inputs into the iterator and emits points gradually. -type integerStreamIntegerIterator struct { - input *bufIntegerIterator - create func() (IntegerPointAggregator, IntegerPointEmitter) - opt IteratorOptions - m map[string]*integerReduceIntegerPoint - points []IntegerPoint -} - -// newIntegerStreamIntegerIterator returns a new instance of integerStreamIntegerIterator. -func newIntegerStreamIntegerIterator(input IntegerIterator, createFn func() (IntegerPointAggregator, IntegerPointEmitter), opt IteratorOptions) *integerStreamIntegerIterator { - return &integerStreamIntegerIterator{ - input: newBufIntegerIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*integerReduceIntegerPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *integerStreamIntegerIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerStreamIntegerIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *integerStreamIntegerIterator) Next() (*IntegerPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *integerStreamIntegerIterator) reduce() ([]IntegerPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &integerReduceIntegerPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateInteger(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// integerExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type integerExprIterator struct { - left *bufIntegerIterator - right *bufIntegerIterator - fn integerExprFunc - points []IntegerPoint // must be size 2 - storePrev bool -} - -func newIntegerExprIterator(left, right IntegerIterator, opt IteratorOptions, fn func(a, b int64) int64) *integerExprIterator { - var points []IntegerPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []IntegerPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToInteger(opt.FillValue) - points = []IntegerPoint{{Value: value}, {Value: value}} - } - return &integerExprIterator{ - left: newBufIntegerIterator(left), - right: newBufIntegerIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *integerExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *integerExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *integerExprIterator) Next() (*IntegerPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - if a.Nil { - return a, nil - } else if b.Nil { - return b, nil - } - a.Value = itr.fn(a.Value, b.Value) - return a, nil - - } -} - -// integerExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type integerExprFunc func(a, b int64) int64 - -// integerReduceStringIterator executes a reducer for every interval and buffers the result. -type integerReduceStringIterator struct { - input *bufIntegerIterator - create func() (IntegerPointAggregator, StringPointEmitter) - opt IteratorOptions - points []StringPoint -} - -// Stats returns stats from the input iterator. -func (itr *integerReduceStringIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerReduceStringIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *integerReduceStringIterator) Next() (*StringPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// integerReduceStringPoint stores the reduced data for a name/tag combination. -type integerReduceStringPoint struct { - Name string - Tags Tags - Aggregator IntegerPointAggregator - Emitter StringPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *integerReduceStringIterator) reduce() ([]StringPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*integerReduceStringPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &integerReduceStringPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateInteger(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]StringPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// integerStreamStringIterator streams inputs into the iterator and emits points gradually. -type integerStreamStringIterator struct { - input *bufIntegerIterator - create func() (IntegerPointAggregator, StringPointEmitter) - opt IteratorOptions - m map[string]*integerReduceStringPoint - points []StringPoint -} - -// newIntegerStreamStringIterator returns a new instance of integerStreamStringIterator. -func newIntegerStreamStringIterator(input IntegerIterator, createFn func() (IntegerPointAggregator, StringPointEmitter), opt IteratorOptions) *integerStreamStringIterator { - return &integerStreamStringIterator{ - input: newBufIntegerIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*integerReduceStringPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *integerStreamStringIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerStreamStringIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *integerStreamStringIterator) Next() (*StringPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *integerStreamStringIterator) reduce() ([]StringPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &integerReduceStringPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateInteger(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// integerStringExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type integerStringExprIterator struct { - left *bufIntegerIterator - right *bufIntegerIterator - fn integerStringExprFunc - points []IntegerPoint // must be size 2 - storePrev bool -} - -func newIntegerStringExprIterator(left, right IntegerIterator, opt IteratorOptions, fn func(a, b int64) string) *integerStringExprIterator { - var points []IntegerPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []IntegerPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToInteger(opt.FillValue) - points = []IntegerPoint{{Value: value}, {Value: value}} - } - return &integerStringExprIterator{ - left: newBufIntegerIterator(left), - right: newBufIntegerIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *integerStringExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *integerStringExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *integerStringExprIterator) Next() (*StringPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &StringPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// integerStringExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type integerStringExprFunc func(a, b int64) string - -// integerReduceBooleanIterator executes a reducer for every interval and buffers the result. -type integerReduceBooleanIterator struct { - input *bufIntegerIterator - create func() (IntegerPointAggregator, BooleanPointEmitter) - opt IteratorOptions - points []BooleanPoint -} - -// Stats returns stats from the input iterator. -func (itr *integerReduceBooleanIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerReduceBooleanIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *integerReduceBooleanIterator) Next() (*BooleanPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// integerReduceBooleanPoint stores the reduced data for a name/tag combination. -type integerReduceBooleanPoint struct { - Name string - Tags Tags - Aggregator IntegerPointAggregator - Emitter BooleanPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *integerReduceBooleanIterator) reduce() ([]BooleanPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*integerReduceBooleanPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &integerReduceBooleanPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateInteger(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]BooleanPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// integerStreamBooleanIterator streams inputs into the iterator and emits points gradually. -type integerStreamBooleanIterator struct { - input *bufIntegerIterator - create func() (IntegerPointAggregator, BooleanPointEmitter) - opt IteratorOptions - m map[string]*integerReduceBooleanPoint - points []BooleanPoint -} - -// newIntegerStreamBooleanIterator returns a new instance of integerStreamBooleanIterator. -func newIntegerStreamBooleanIterator(input IntegerIterator, createFn func() (IntegerPointAggregator, BooleanPointEmitter), opt IteratorOptions) *integerStreamBooleanIterator { - return &integerStreamBooleanIterator{ - input: newBufIntegerIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*integerReduceBooleanPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *integerStreamBooleanIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerStreamBooleanIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *integerStreamBooleanIterator) Next() (*BooleanPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *integerStreamBooleanIterator) reduce() ([]BooleanPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &integerReduceBooleanPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateInteger(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// integerBooleanExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type integerBooleanExprIterator struct { - left *bufIntegerIterator - right *bufIntegerIterator - fn integerBooleanExprFunc - points []IntegerPoint // must be size 2 - storePrev bool -} - -func newIntegerBooleanExprIterator(left, right IntegerIterator, opt IteratorOptions, fn func(a, b int64) bool) *integerBooleanExprIterator { - var points []IntegerPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []IntegerPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToInteger(opt.FillValue) - points = []IntegerPoint{{Value: value}, {Value: value}} - } - return &integerBooleanExprIterator{ - left: newBufIntegerIterator(left), - right: newBufIntegerIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *integerBooleanExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *integerBooleanExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *integerBooleanExprIterator) Next() (*BooleanPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &BooleanPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// integerBooleanExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type integerBooleanExprFunc func(a, b int64) bool - -// integerTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type integerTransformIterator struct { - input IntegerIterator - fn integerTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *integerTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *integerTransformIterator) Next() (*IntegerPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - p = itr.fn(p) - } - return p, nil -} - -// integerTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type integerTransformFunc func(p *IntegerPoint) *IntegerPoint - -// integerBoolTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type integerBoolTransformIterator struct { - input IntegerIterator - fn integerBoolTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *integerBoolTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerBoolTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *integerBoolTransformIterator) Next() (*BooleanPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - return itr.fn(p), nil - } - return nil, nil -} - -// integerBoolTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type integerBoolTransformFunc func(p *IntegerPoint) *BooleanPoint - -// integerDedupeIterator only outputs unique points. -// This differs from the DistinctIterator in that it compares all aux fields too. -// This iterator is relatively inefficient and should only be used on small -// datasets such as meta query results. -type integerDedupeIterator struct { - input IntegerIterator - m map[string]struct{} // lookup of points already sent -} - -// newIntegerDedupeIterator returns a new instance of integerDedupeIterator. -func newIntegerDedupeIterator(input IntegerIterator) *integerDedupeIterator { - return &integerDedupeIterator{ - input: input, - m: make(map[string]struct{}), - } -} - -// Stats returns stats from the input iterator. -func (itr *integerDedupeIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerDedupeIterator) Close() error { return itr.input.Close() } - -// Next returns the next unique point from the input iterator. -func (itr *integerDedupeIterator) Next() (*IntegerPoint, error) { - for { - // Read next point. - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Serialize to bytes to store in lookup. - buf, err := proto.Marshal(encodeIntegerPoint(p)) - if err != nil { - return nil, err - } - - // If the point has already been output then move to the next point. - if _, ok := itr.m[string(buf)]; ok { - continue - } - - // Otherwise mark it as emitted and return point. - itr.m[string(buf)] = struct{}{} - return p, nil - } -} - -// integerReaderIterator represents an iterator that streams from a reader. -type integerReaderIterator struct { - r io.Reader - dec *IntegerPointDecoder -} - -// newIntegerReaderIterator returns a new instance of integerReaderIterator. -func newIntegerReaderIterator(r io.Reader, stats IteratorStats) *integerReaderIterator { - dec := NewIntegerPointDecoder(r) - dec.stats = stats - - return &integerReaderIterator{ - r: r, - dec: dec, - } -} - -// Stats returns stats about points processed. -func (itr *integerReaderIterator) Stats() IteratorStats { return itr.dec.stats } - -// Close closes the underlying reader, if applicable. -func (itr *integerReaderIterator) Close() error { - if r, ok := itr.r.(io.ReadCloser); ok { - return r.Close() - } - return nil -} - -// Next returns the next point from the iterator. -func (itr *integerReaderIterator) Next() (*IntegerPoint, error) { - // OPTIMIZE(benbjohnson): Reuse point on iterator. - - // Unmarshal next point. - p := &IntegerPoint{} - if err := itr.dec.DecodeIntegerPoint(p); err == io.EOF { - return nil, nil - } else if err != nil { - return nil, err - } - return p, nil -} - -// StringIterator represents a stream of string points. -type StringIterator interface { - Iterator - Next() (*StringPoint, error) -} - -// newStringIterators converts a slice of Iterator to a slice of StringIterator. -// Drop and closes any iterator in itrs that is not a StringIterator and cannot -// be cast to a StringIterator. -func newStringIterators(itrs []Iterator) []StringIterator { - a := make([]StringIterator, 0, len(itrs)) - for _, itr := range itrs { - switch itr := itr.(type) { - case StringIterator: - a = append(a, itr) - - default: - itr.Close() - } - } - return a -} - -// bufStringIterator represents a buffered StringIterator. -type bufStringIterator struct { - itr StringIterator - buf *StringPoint -} - -// newBufStringIterator returns a buffered StringIterator. -func newBufStringIterator(itr StringIterator) *bufStringIterator { - return &bufStringIterator{itr: itr} -} - -// Stats returns statistics from the input iterator. -func (itr *bufStringIterator) Stats() IteratorStats { return itr.itr.Stats() } - -// Close closes the underlying iterator. -func (itr *bufStringIterator) Close() error { return itr.itr.Close() } - -// peek returns the next point without removing it from the iterator. -func (itr *bufStringIterator) peek() (*StringPoint, error) { - p, err := itr.Next() - if err != nil { - return nil, err - } - itr.unread(p) - return p, nil -} - -// peekTime returns the time of the next point. -// Returns zero time if no more points available. -func (itr *bufStringIterator) peekTime() (int64, error) { - p, err := itr.peek() - if p == nil || err != nil { - return ZeroTime, err - } - return p.Time, nil -} - -// Next returns the current buffer, if exists, or calls the underlying iterator. -func (itr *bufStringIterator) Next() (*StringPoint, error) { - buf := itr.buf - if buf != nil { - itr.buf = nil - return buf, nil - } - return itr.itr.Next() -} - -// NextInWindow returns the next value if it is between [startTime, endTime). -// If the next value is outside the range then it is moved to the buffer. -func (itr *bufStringIterator) NextInWindow(startTime, endTime int64) (*StringPoint, error) { - v, err := itr.Next() - if v == nil || err != nil { - return nil, err - } else if t := v.Time; t >= endTime || t < startTime { - itr.unread(v) - return nil, nil - } - return v, nil -} - -// unread sets v to the buffer. It is read on the next call to Next(). -func (itr *bufStringIterator) unread(v *StringPoint) { itr.buf = v } - -// stringMergeIterator represents an iterator that combines multiple string iterators. -type stringMergeIterator struct { - inputs []StringIterator - heap *stringMergeHeap - init bool - - // Current iterator and window. - curr *stringMergeHeapItem - window struct { - name string - tags string - startTime int64 - endTime int64 - } -} - -// newStringMergeIterator returns a new instance of stringMergeIterator. -func newStringMergeIterator(inputs []StringIterator, opt IteratorOptions) *stringMergeIterator { - itr := &stringMergeIterator{ - inputs: inputs, - heap: &stringMergeHeap{ - items: make([]*stringMergeHeapItem, 0, len(inputs)), - opt: opt, - }, - } - - // Initialize heap items. - for _, input := range inputs { - // Wrap in buffer, ignore any inputs without anymore points. - bufInput := newBufStringIterator(input) - - // Append to the heap. - itr.heap.items = append(itr.heap.items, &stringMergeHeapItem{itr: bufInput}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *stringMergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *stringMergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - itr.curr = nil - itr.inputs = nil - itr.heap.items = nil - return nil -} - -// Next returns the next point from the iterator. -func (itr *stringMergeIterator) Next() (*StringPoint, error) { - // Initialize the heap. This needs to be done lazily on the first call to this iterator - // so that iterator initialization done through the Select() call returns quickly. - // Queries can only be interrupted after the Select() call completes so any operations - // done during iterator creation cannot be interrupted, which is why we do it here - // instead so an interrupt can happen while initializing the heap. - if !itr.init { - items := itr.heap.items - itr.heap.items = make([]*stringMergeHeapItem, 0, len(items)) - for _, item := range items { - if p, err := item.itr.peek(); err != nil { - return nil, err - } else if p == nil { - continue - } - itr.heap.items = append(itr.heap.items, item) - } - heap.Init(itr.heap) - itr.init = true - } - - for { - // Retrieve the next iterator if we don't have one. - if itr.curr == nil { - if len(itr.heap.items) == 0 { - return nil, nil - } - itr.curr = heap.Pop(itr.heap).(*stringMergeHeapItem) - - // Read point and set current window. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags.ID() - itr.window.startTime, itr.window.endTime = itr.heap.opt.Window(p.Time) - return p, nil - } - - // Read the next point from the current iterator. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - - // If there are no more points then remove iterator from heap and find next. - if p == nil { - itr.curr = nil - continue - } - - // Check if the point is inside of our current window. - inWindow := true - if window := itr.window; window.name != p.Name { - inWindow = false - } else if window.tags != p.Tags.ID() { - inWindow = false - } else if opt := itr.heap.opt; opt.Ascending && p.Time >= window.endTime { - inWindow = false - } else if !opt.Ascending && p.Time < window.startTime { - inWindow = false - } - - // If it's outside our window then push iterator back on the heap and find new iterator. - if !inWindow { - itr.curr.itr.unread(p) - heap.Push(itr.heap, itr.curr) - itr.curr = nil - continue - } - - return p, nil - } -} - -// stringMergeHeap represents a heap of stringMergeHeapItems. -// Items are sorted by their next window and then by name/tags. -type stringMergeHeap struct { - opt IteratorOptions - items []*stringMergeHeapItem -} - -func (h stringMergeHeap) Len() int { return len(h.items) } -func (h stringMergeHeap) Swap(i, j int) { h.items[i], h.items[j] = h.items[j], h.items[i] } -func (h stringMergeHeap) Less(i, j int) bool { - x, err := h.items[i].itr.peek() - if err != nil { - return true - } - y, err := h.items[j].itr.peek() - if err != nil { - return false - } - - if h.opt.Ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() < y.Tags.ID() - } - } else { - if x.Name != y.Name { - return x.Name > y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() > y.Tags.ID() - } - } - - xt, _ := h.opt.Window(x.Time) - yt, _ := h.opt.Window(y.Time) - - if h.opt.Ascending { - return xt < yt - } - return xt > yt -} - -func (h *stringMergeHeap) Push(x interface{}) { - h.items = append(h.items, x.(*stringMergeHeapItem)) -} - -func (h *stringMergeHeap) Pop() interface{} { - old := h.items - n := len(old) - item := old[n-1] - h.items = old[0 : n-1] - return item -} - -type stringMergeHeapItem struct { - itr *bufStringIterator -} - -// stringSortedMergeIterator is an iterator that sorts and merges multiple iterators into one. -type stringSortedMergeIterator struct { - inputs []StringIterator - opt IteratorOptions - heap stringSortedMergeHeap - init bool -} - -// newStringSortedMergeIterator returns an instance of stringSortedMergeIterator. -func newStringSortedMergeIterator(inputs []StringIterator, opt IteratorOptions) Iterator { - itr := &stringSortedMergeIterator{ - inputs: inputs, - heap: make(stringSortedMergeHeap, 0, len(inputs)), - opt: opt, - } - - // Initialize heap items. - for _, input := range inputs { - // Append to the heap. - itr.heap = append(itr.heap, &stringSortedMergeHeapItem{itr: input, ascending: opt.Ascending}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *stringSortedMergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *stringSortedMergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - return nil -} - -// Next returns the next points from the iterator. -func (itr *stringSortedMergeIterator) Next() (*StringPoint, error) { return itr.pop() } - -// pop returns the next point from the heap. -// Reads the next point from item's cursor and puts it back on the heap. -func (itr *stringSortedMergeIterator) pop() (*StringPoint, error) { - // Initialize the heap. See the MergeIterator to see why this has to be done lazily. - if !itr.init { - items := itr.heap - itr.heap = make([]*stringSortedMergeHeapItem, 0, len(items)) - for _, item := range items { - var err error - if item.point, err = item.itr.Next(); err != nil { - return nil, err - } else if item.point == nil { - continue - } - itr.heap = append(itr.heap, item) - } - heap.Init(&itr.heap) - itr.init = true - } - - if len(itr.heap) == 0 { - return nil, nil - } - - // Read the next item from the heap. - item := heap.Pop(&itr.heap).(*stringSortedMergeHeapItem) - if item.err != nil { - return nil, item.err - } else if item.point == nil { - return nil, nil - } - - // Copy the point for return. - p := item.point.Clone() - - // Read the next item from the cursor. Push back to heap if one exists. - if item.point, item.err = item.itr.Next(); item.point != nil { - heap.Push(&itr.heap, item) - } - - return p, nil -} - -// stringSortedMergeHeap represents a heap of stringSortedMergeHeapItems. -type stringSortedMergeHeap []*stringSortedMergeHeapItem - -func (h stringSortedMergeHeap) Len() int { return len(h) } -func (h stringSortedMergeHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h stringSortedMergeHeap) Less(i, j int) bool { - x, y := h[i].point, h[j].point - - if h[i].ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() < y.Tags.ID() - } - return x.Time < y.Time - } - - if x.Name != y.Name { - return x.Name > y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() > y.Tags.ID() - } - return x.Time > y.Time -} - -func (h *stringSortedMergeHeap) Push(x interface{}) { - *h = append(*h, x.(*stringSortedMergeHeapItem)) -} - -func (h *stringSortedMergeHeap) Pop() interface{} { - old := *h - n := len(old) - item := old[n-1] - *h = old[0 : n-1] - return item -} - -type stringSortedMergeHeapItem struct { - point *StringPoint - err error - itr StringIterator - ascending bool -} - -// stringParallelIterator represents an iterator that pulls data in a separate goroutine. -type stringParallelIterator struct { - input StringIterator - ch chan stringPointError - - once sync.Once - closing chan struct{} - wg sync.WaitGroup -} - -// newStringParallelIterator returns a new instance of stringParallelIterator. -func newStringParallelIterator(input StringIterator) *stringParallelIterator { - itr := &stringParallelIterator{ - input: input, - ch: make(chan stringPointError, 1), - closing: make(chan struct{}), - } - itr.wg.Add(1) - go itr.monitor() - return itr -} - -// Stats returns stats from the underlying iterator. -func (itr *stringParallelIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *stringParallelIterator) Close() error { - itr.once.Do(func() { close(itr.closing) }) - itr.wg.Wait() - return itr.input.Close() -} - -// Next returns the next point from the iterator. -func (itr *stringParallelIterator) Next() (*StringPoint, error) { - v, ok := <-itr.ch - if !ok { - return nil, io.EOF - } - return v.point, v.err -} - -// monitor runs in a separate goroutine and actively pulls the next point. -func (itr *stringParallelIterator) monitor() { - defer close(itr.ch) - defer itr.wg.Done() - - for { - // Read next point. - p, err := itr.input.Next() - - select { - case <-itr.closing: - return - case itr.ch <- stringPointError{point: p, err: err}: - } - } -} - -type stringPointError struct { - point *StringPoint - err error -} - -// stringLimitIterator represents an iterator that limits points per group. -type stringLimitIterator struct { - input StringIterator - opt IteratorOptions - n int - - prev struct { - name string - tags Tags - } -} - -// newStringLimitIterator returns a new instance of stringLimitIterator. -func newStringLimitIterator(input StringIterator, opt IteratorOptions) *stringLimitIterator { - return &stringLimitIterator{ - input: input, - opt: opt, - } -} - -// Stats returns stats from the underlying iterator. -func (itr *stringLimitIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *stringLimitIterator) Close() error { return itr.input.Close() } - -// Next returns the next point from the iterator. -func (itr *stringLimitIterator) Next() (*StringPoint, error) { - for { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Reset window and counter if a new window is encountered. - if p.Name != itr.prev.name || !p.Tags.Equals(&itr.prev.tags) { - itr.prev.name = p.Name - itr.prev.tags = p.Tags - itr.n = 0 - } - - // Increment counter. - itr.n++ - - // Read next point if not beyond the offset. - if itr.n <= itr.opt.Offset { - continue - } - - // Read next point if we're beyond the limit. - if itr.opt.Limit > 0 && (itr.n-itr.opt.Offset) > itr.opt.Limit { - // If there's no interval, no groups, and a single source then simply exit. - if itr.opt.Interval.IsZero() && len(itr.opt.Dimensions) == 0 && len(itr.opt.Sources) == 1 { - return nil, nil - } - continue - } - - return p, nil - } -} - -type stringFillIterator struct { - input *bufStringIterator - prev StringPoint - startTime int64 - endTime int64 - auxFields []interface{} - init bool - opt IteratorOptions - - window struct { - name string - tags Tags - time int64 - } -} - -func newStringFillIterator(input StringIterator, expr Expr, opt IteratorOptions) *stringFillIterator { - if opt.Fill == NullFill { - if expr, ok := expr.(*Call); ok && expr.Name == "count" { - opt.Fill = NumberFill - opt.FillValue = "" - } - } - - var startTime, endTime int64 - if opt.Ascending { - startTime, _ = opt.Window(opt.StartTime) - endTime, _ = opt.Window(opt.EndTime) - } else { - startTime, _ = opt.Window(opt.EndTime) - endTime, _ = opt.Window(opt.StartTime) - } - - var auxFields []interface{} - if len(opt.Aux) > 0 { - auxFields = make([]interface{}, len(opt.Aux)) - } - - return &stringFillIterator{ - input: newBufStringIterator(input), - prev: StringPoint{Nil: true}, - startTime: startTime, - endTime: endTime, - auxFields: auxFields, - opt: opt, - } -} - -func (itr *stringFillIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *stringFillIterator) Close() error { return itr.input.Close() } - -func (itr *stringFillIterator) Next() (*StringPoint, error) { - if !itr.init { - p, err := itr.input.peek() - if p == nil || err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.init = true - } - - p, err := itr.input.Next() - if err != nil { - return nil, err - } - - // Check if the next point is outside of our window or is nil. - for p == nil || p.Name != itr.window.name || p.Tags.ID() != itr.window.tags.ID() { - // If we are inside of an interval, unread the point and continue below to - // constructing a new point. - if itr.opt.Ascending { - if itr.window.time <= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } else { - if itr.window.time >= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } - - // We are *not* in a current interval. If there is no next point, - // we are at the end of all intervals. - if p == nil { - return nil, nil - } - - // Set the new interval. - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.prev = StringPoint{Nil: true} - break - } - - // Check if the point is our next expected point. - if p == nil || (itr.opt.Ascending && p.Time > itr.window.time) || (!itr.opt.Ascending && p.Time < itr.window.time) { - if p != nil { - itr.input.unread(p) - } - - p = &StringPoint{ - Name: itr.window.name, - Tags: itr.window.tags, - Time: itr.window.time, - Aux: itr.auxFields, - } - - switch itr.opt.Fill { - case LinearFill: - fallthrough - case NullFill: - p.Nil = true - case NumberFill: - p.Value = castToString(itr.opt.FillValue) - case PreviousFill: - if !itr.prev.Nil { - p.Value = itr.prev.Value - p.Nil = itr.prev.Nil - } else { - p.Nil = true - } - } - } else { - itr.prev = *p - } - - // Advance the expected time. Do not advance to a new window here - // as there may be lingering points with the same timestamp in the previous - // window. - if itr.opt.Ascending { - itr.window.time = p.Time + int64(itr.opt.Interval.Duration) - } else { - itr.window.time = p.Time - int64(itr.opt.Interval.Duration) - } - return p, nil -} - -// stringIntervalIterator represents a string implementation of IntervalIterator. -type stringIntervalIterator struct { - input StringIterator - opt IteratorOptions -} - -func newStringIntervalIterator(input StringIterator, opt IteratorOptions) *stringIntervalIterator { - return &stringIntervalIterator{input: input, opt: opt} -} - -func (itr *stringIntervalIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *stringIntervalIterator) Close() error { return itr.input.Close() } - -func (itr *stringIntervalIterator) Next() (*StringPoint, error) { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - p.Time, _ = itr.opt.Window(p.Time) - // If we see the minimum allowable time, set the time to zero so we don't - // break the default returned time for aggregate queries without times. - if p.Time == MinTime { - p.Time = 0 - } - return p, nil -} - -// stringInterruptIterator represents a string implementation of InterruptIterator. -type stringInterruptIterator struct { - input StringIterator - closing <-chan struct{} - count int -} - -func newStringInterruptIterator(input StringIterator, closing <-chan struct{}) *stringInterruptIterator { - return &stringInterruptIterator{input: input, closing: closing} -} - -func (itr *stringInterruptIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *stringInterruptIterator) Close() error { return itr.input.Close() } - -func (itr *stringInterruptIterator) Next() (*StringPoint, error) { - // Only check if the channel is closed every N points. This - // intentionally checks on both 0 and N so that if the iterator - // has been interrupted before the first point is emitted it will - // not emit any points. - if itr.count&0xFF == 0xFF { - select { - case <-itr.closing: - return nil, nil - default: - // Reset iterator count to zero and fall through to emit the next point. - itr.count = 0 - } - } - - // Increment the counter for every point read. - itr.count++ - return itr.input.Next() -} - -// stringCloseInterruptIterator represents a string implementation of CloseInterruptIterator. -type stringCloseInterruptIterator struct { - input StringIterator - closing <-chan struct{} - done chan struct{} - once sync.Once -} - -func newStringCloseInterruptIterator(input StringIterator, closing <-chan struct{}) *stringCloseInterruptIterator { - itr := &stringCloseInterruptIterator{ - input: input, - closing: closing, - done: make(chan struct{}), - } - go itr.monitor() - return itr -} - -func (itr *stringCloseInterruptIterator) monitor() { - select { - case <-itr.closing: - itr.Close() - case <-itr.done: - } -} - -func (itr *stringCloseInterruptIterator) Stats() IteratorStats { - return itr.input.Stats() -} - -func (itr *stringCloseInterruptIterator) Close() error { - itr.once.Do(func() { - close(itr.done) - itr.input.Close() - }) - return nil -} - -func (itr *stringCloseInterruptIterator) Next() (*StringPoint, error) { - p, err := itr.input.Next() - if err != nil { - // Check if the iterator was closed. - select { - case <-itr.done: - return nil, nil - default: - return nil, err - } - } - return p, nil -} - -// auxStringPoint represents a combination of a point and an error for the AuxIterator. -type auxStringPoint struct { - point *StringPoint - err error -} - -// stringAuxIterator represents a string implementation of AuxIterator. -type stringAuxIterator struct { - input *bufStringIterator - output chan auxStringPoint - fields auxIteratorFields - background bool -} - -func newStringAuxIterator(input StringIterator, opt IteratorOptions) *stringAuxIterator { - return &stringAuxIterator{ - input: newBufStringIterator(input), - output: make(chan auxStringPoint, 1), - fields: newAuxIteratorFields(opt), - } -} - -func (itr *stringAuxIterator) Background() { - itr.background = true - itr.Start() - go DrainIterator(itr) -} - -func (itr *stringAuxIterator) Start() { go itr.stream() } -func (itr *stringAuxIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *stringAuxIterator) Close() error { return itr.input.Close() } -func (itr *stringAuxIterator) Next() (*StringPoint, error) { - p := <-itr.output - return p.point, p.err -} -func (itr *stringAuxIterator) Iterator(name string, typ DataType) Iterator { - return itr.fields.iterator(name, typ) -} - -func (itr *stringAuxIterator) CreateIterator(opt IteratorOptions) (Iterator, error) { - expr := opt.Expr - if expr == nil { - panic("unable to create an iterator with no expression from an aux iterator") - } - - switch expr := expr.(type) { - case *VarRef: - return itr.Iterator(expr.Val, expr.Type), nil - default: - panic(fmt.Sprintf("invalid expression type for an aux iterator: %T", expr)) - } -} - -func (itr *stringAuxIterator) FieldDimensions(sources Sources) (fields map[string]DataType, dimensions map[string]struct{}, err error) { - return nil, nil, errors.New("not implemented") -} - -func (itr *stringAuxIterator) ExpandSources(sources Sources) (Sources, error) { - return nil, errors.New("not implemented") -} - -func (itr *stringAuxIterator) stream() { - for { - // Read next point. - p, err := itr.input.Next() - if err != nil { - itr.output <- auxStringPoint{err: err} - itr.fields.sendError(err) - break - } else if p == nil { - break - } - - // Send point to output and to each field iterator. - itr.output <- auxStringPoint{point: p} - if ok := itr.fields.send(p); !ok && itr.background { - break - } - } - - close(itr.output) - itr.fields.close() -} - -// stringChanIterator represents a new instance of stringChanIterator. -type stringChanIterator struct { - buf struct { - i int - filled bool - points [2]StringPoint - } - err error - cond *sync.Cond - done bool -} - -func (itr *stringChanIterator) Stats() IteratorStats { return IteratorStats{} } - -func (itr *stringChanIterator) Close() error { - itr.cond.L.Lock() - // Mark the channel iterator as done and signal all waiting goroutines to start again. - itr.done = true - itr.cond.Broadcast() - // Do not defer the unlock so we don't create an unnecessary allocation. - itr.cond.L.Unlock() - return nil -} - -func (itr *stringChanIterator) setBuf(name string, tags Tags, time int64, value interface{}) bool { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Wait for either the iterator to be done (so we don't have to set the value) - // or for the buffer to have been read and ready for another write. - for !itr.done && itr.buf.filled { - itr.cond.Wait() - } - - // Do not set the value and return false to signal that the iterator is closed. - // Do this after the above wait as the above for loop may have exited because - // the iterator was closed. - if itr.done { - return false - } - - switch v := value.(type) { - case string: - itr.buf.points[itr.buf.i] = StringPoint{Name: name, Tags: tags, Time: time, Value: v} - - default: - itr.buf.points[itr.buf.i] = StringPoint{Name: name, Tags: tags, Time: time, Nil: true} - } - itr.buf.filled = true - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() - return true -} - -func (itr *stringChanIterator) setErr(err error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - itr.err = err - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() -} - -func (itr *stringChanIterator) Next() (*StringPoint, error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Check for an error and return one if there. - if itr.err != nil { - return nil, itr.err - } - - // Wait until either a value is available in the buffer or - // the iterator is closed. - for !itr.done && !itr.buf.filled { - itr.cond.Wait() - } - - // Return nil once the channel is done and the buffer is empty. - if itr.done && !itr.buf.filled { - return nil, nil - } - - // Always read from the buffer if it exists, even if the iterator - // is closed. This prevents the last value from being truncated by - // the parent iterator. - p := &itr.buf.points[itr.buf.i] - itr.buf.i = (itr.buf.i + 1) % len(itr.buf.points) - itr.buf.filled = false - itr.cond.Signal() - return p, nil -} - -// stringReduceFloatIterator executes a reducer for every interval and buffers the result. -type stringReduceFloatIterator struct { - input *bufStringIterator - create func() (StringPointAggregator, FloatPointEmitter) - opt IteratorOptions - points []FloatPoint -} - -// Stats returns stats from the input iterator. -func (itr *stringReduceFloatIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringReduceFloatIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *stringReduceFloatIterator) Next() (*FloatPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// stringReduceFloatPoint stores the reduced data for a name/tag combination. -type stringReduceFloatPoint struct { - Name string - Tags Tags - Aggregator StringPointAggregator - Emitter FloatPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *stringReduceFloatIterator) reduce() ([]FloatPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*stringReduceFloatPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &stringReduceFloatPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateString(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]FloatPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// stringStreamFloatIterator streams inputs into the iterator and emits points gradually. -type stringStreamFloatIterator struct { - input *bufStringIterator - create func() (StringPointAggregator, FloatPointEmitter) - opt IteratorOptions - m map[string]*stringReduceFloatPoint - points []FloatPoint -} - -// newStringStreamFloatIterator returns a new instance of stringStreamFloatIterator. -func newStringStreamFloatIterator(input StringIterator, createFn func() (StringPointAggregator, FloatPointEmitter), opt IteratorOptions) *stringStreamFloatIterator { - return &stringStreamFloatIterator{ - input: newBufStringIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*stringReduceFloatPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *stringStreamFloatIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringStreamFloatIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *stringStreamFloatIterator) Next() (*FloatPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *stringStreamFloatIterator) reduce() ([]FloatPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &stringReduceFloatPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateString(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// stringFloatExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type stringFloatExprIterator struct { - left *bufStringIterator - right *bufStringIterator - fn stringFloatExprFunc - points []StringPoint // must be size 2 - storePrev bool -} - -func newStringFloatExprIterator(left, right StringIterator, opt IteratorOptions, fn func(a, b string) float64) *stringFloatExprIterator { - var points []StringPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []StringPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToString(opt.FillValue) - points = []StringPoint{{Value: value}, {Value: value}} - } - return &stringFloatExprIterator{ - left: newBufStringIterator(left), - right: newBufStringIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *stringFloatExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *stringFloatExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *stringFloatExprIterator) Next() (*FloatPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &FloatPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// stringFloatExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type stringFloatExprFunc func(a, b string) float64 - -// stringReduceIntegerIterator executes a reducer for every interval and buffers the result. -type stringReduceIntegerIterator struct { - input *bufStringIterator - create func() (StringPointAggregator, IntegerPointEmitter) - opt IteratorOptions - points []IntegerPoint -} - -// Stats returns stats from the input iterator. -func (itr *stringReduceIntegerIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringReduceIntegerIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *stringReduceIntegerIterator) Next() (*IntegerPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// stringReduceIntegerPoint stores the reduced data for a name/tag combination. -type stringReduceIntegerPoint struct { - Name string - Tags Tags - Aggregator StringPointAggregator - Emitter IntegerPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *stringReduceIntegerIterator) reduce() ([]IntegerPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*stringReduceIntegerPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &stringReduceIntegerPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateString(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]IntegerPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// stringStreamIntegerIterator streams inputs into the iterator and emits points gradually. -type stringStreamIntegerIterator struct { - input *bufStringIterator - create func() (StringPointAggregator, IntegerPointEmitter) - opt IteratorOptions - m map[string]*stringReduceIntegerPoint - points []IntegerPoint -} - -// newStringStreamIntegerIterator returns a new instance of stringStreamIntegerIterator. -func newStringStreamIntegerIterator(input StringIterator, createFn func() (StringPointAggregator, IntegerPointEmitter), opt IteratorOptions) *stringStreamIntegerIterator { - return &stringStreamIntegerIterator{ - input: newBufStringIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*stringReduceIntegerPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *stringStreamIntegerIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringStreamIntegerIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *stringStreamIntegerIterator) Next() (*IntegerPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *stringStreamIntegerIterator) reduce() ([]IntegerPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &stringReduceIntegerPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateString(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// stringIntegerExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type stringIntegerExprIterator struct { - left *bufStringIterator - right *bufStringIterator - fn stringIntegerExprFunc - points []StringPoint // must be size 2 - storePrev bool -} - -func newStringIntegerExprIterator(left, right StringIterator, opt IteratorOptions, fn func(a, b string) int64) *stringIntegerExprIterator { - var points []StringPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []StringPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToString(opt.FillValue) - points = []StringPoint{{Value: value}, {Value: value}} - } - return &stringIntegerExprIterator{ - left: newBufStringIterator(left), - right: newBufStringIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *stringIntegerExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *stringIntegerExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *stringIntegerExprIterator) Next() (*IntegerPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &IntegerPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// stringIntegerExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type stringIntegerExprFunc func(a, b string) int64 - -// stringReduceStringIterator executes a reducer for every interval and buffers the result. -type stringReduceStringIterator struct { - input *bufStringIterator - create func() (StringPointAggregator, StringPointEmitter) - opt IteratorOptions - points []StringPoint -} - -// Stats returns stats from the input iterator. -func (itr *stringReduceStringIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringReduceStringIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *stringReduceStringIterator) Next() (*StringPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// stringReduceStringPoint stores the reduced data for a name/tag combination. -type stringReduceStringPoint struct { - Name string - Tags Tags - Aggregator StringPointAggregator - Emitter StringPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *stringReduceStringIterator) reduce() ([]StringPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*stringReduceStringPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &stringReduceStringPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateString(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]StringPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// stringStreamStringIterator streams inputs into the iterator and emits points gradually. -type stringStreamStringIterator struct { - input *bufStringIterator - create func() (StringPointAggregator, StringPointEmitter) - opt IteratorOptions - m map[string]*stringReduceStringPoint - points []StringPoint -} - -// newStringStreamStringIterator returns a new instance of stringStreamStringIterator. -func newStringStreamStringIterator(input StringIterator, createFn func() (StringPointAggregator, StringPointEmitter), opt IteratorOptions) *stringStreamStringIterator { - return &stringStreamStringIterator{ - input: newBufStringIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*stringReduceStringPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *stringStreamStringIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringStreamStringIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *stringStreamStringIterator) Next() (*StringPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *stringStreamStringIterator) reduce() ([]StringPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &stringReduceStringPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateString(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// stringExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type stringExprIterator struct { - left *bufStringIterator - right *bufStringIterator - fn stringExprFunc - points []StringPoint // must be size 2 - storePrev bool -} - -func newStringExprIterator(left, right StringIterator, opt IteratorOptions, fn func(a, b string) string) *stringExprIterator { - var points []StringPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []StringPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToString(opt.FillValue) - points = []StringPoint{{Value: value}, {Value: value}} - } - return &stringExprIterator{ - left: newBufStringIterator(left), - right: newBufStringIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *stringExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *stringExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *stringExprIterator) Next() (*StringPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - if a.Nil { - return a, nil - } else if b.Nil { - return b, nil - } - a.Value = itr.fn(a.Value, b.Value) - return a, nil - - } -} - -// stringExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type stringExprFunc func(a, b string) string - -// stringReduceBooleanIterator executes a reducer for every interval and buffers the result. -type stringReduceBooleanIterator struct { - input *bufStringIterator - create func() (StringPointAggregator, BooleanPointEmitter) - opt IteratorOptions - points []BooleanPoint -} - -// Stats returns stats from the input iterator. -func (itr *stringReduceBooleanIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringReduceBooleanIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *stringReduceBooleanIterator) Next() (*BooleanPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// stringReduceBooleanPoint stores the reduced data for a name/tag combination. -type stringReduceBooleanPoint struct { - Name string - Tags Tags - Aggregator StringPointAggregator - Emitter BooleanPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *stringReduceBooleanIterator) reduce() ([]BooleanPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*stringReduceBooleanPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &stringReduceBooleanPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateString(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]BooleanPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// stringStreamBooleanIterator streams inputs into the iterator and emits points gradually. -type stringStreamBooleanIterator struct { - input *bufStringIterator - create func() (StringPointAggregator, BooleanPointEmitter) - opt IteratorOptions - m map[string]*stringReduceBooleanPoint - points []BooleanPoint -} - -// newStringStreamBooleanIterator returns a new instance of stringStreamBooleanIterator. -func newStringStreamBooleanIterator(input StringIterator, createFn func() (StringPointAggregator, BooleanPointEmitter), opt IteratorOptions) *stringStreamBooleanIterator { - return &stringStreamBooleanIterator{ - input: newBufStringIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*stringReduceBooleanPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *stringStreamBooleanIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringStreamBooleanIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *stringStreamBooleanIterator) Next() (*BooleanPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *stringStreamBooleanIterator) reduce() ([]BooleanPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &stringReduceBooleanPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateString(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// stringBooleanExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type stringBooleanExprIterator struct { - left *bufStringIterator - right *bufStringIterator - fn stringBooleanExprFunc - points []StringPoint // must be size 2 - storePrev bool -} - -func newStringBooleanExprIterator(left, right StringIterator, opt IteratorOptions, fn func(a, b string) bool) *stringBooleanExprIterator { - var points []StringPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []StringPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToString(opt.FillValue) - points = []StringPoint{{Value: value}, {Value: value}} - } - return &stringBooleanExprIterator{ - left: newBufStringIterator(left), - right: newBufStringIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *stringBooleanExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *stringBooleanExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *stringBooleanExprIterator) Next() (*BooleanPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &BooleanPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// stringBooleanExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type stringBooleanExprFunc func(a, b string) bool - -// stringTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type stringTransformIterator struct { - input StringIterator - fn stringTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *stringTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *stringTransformIterator) Next() (*StringPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - p = itr.fn(p) - } - return p, nil -} - -// stringTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type stringTransformFunc func(p *StringPoint) *StringPoint - -// stringBoolTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type stringBoolTransformIterator struct { - input StringIterator - fn stringBoolTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *stringBoolTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringBoolTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *stringBoolTransformIterator) Next() (*BooleanPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - return itr.fn(p), nil - } - return nil, nil -} - -// stringBoolTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type stringBoolTransformFunc func(p *StringPoint) *BooleanPoint - -// stringDedupeIterator only outputs unique points. -// This differs from the DistinctIterator in that it compares all aux fields too. -// This iterator is relatively inefficient and should only be used on small -// datasets such as meta query results. -type stringDedupeIterator struct { - input StringIterator - m map[string]struct{} // lookup of points already sent -} - -// newStringDedupeIterator returns a new instance of stringDedupeIterator. -func newStringDedupeIterator(input StringIterator) *stringDedupeIterator { - return &stringDedupeIterator{ - input: input, - m: make(map[string]struct{}), - } -} - -// Stats returns stats from the input iterator. -func (itr *stringDedupeIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *stringDedupeIterator) Close() error { return itr.input.Close() } - -// Next returns the next unique point from the input iterator. -func (itr *stringDedupeIterator) Next() (*StringPoint, error) { - for { - // Read next point. - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Serialize to bytes to store in lookup. - buf, err := proto.Marshal(encodeStringPoint(p)) - if err != nil { - return nil, err - } - - // If the point has already been output then move to the next point. - if _, ok := itr.m[string(buf)]; ok { - continue - } - - // Otherwise mark it as emitted and return point. - itr.m[string(buf)] = struct{}{} - return p, nil - } -} - -// stringReaderIterator represents an iterator that streams from a reader. -type stringReaderIterator struct { - r io.Reader - dec *StringPointDecoder -} - -// newStringReaderIterator returns a new instance of stringReaderIterator. -func newStringReaderIterator(r io.Reader, stats IteratorStats) *stringReaderIterator { - dec := NewStringPointDecoder(r) - dec.stats = stats - - return &stringReaderIterator{ - r: r, - dec: dec, - } -} - -// Stats returns stats about points processed. -func (itr *stringReaderIterator) Stats() IteratorStats { return itr.dec.stats } - -// Close closes the underlying reader, if applicable. -func (itr *stringReaderIterator) Close() error { - if r, ok := itr.r.(io.ReadCloser); ok { - return r.Close() - } - return nil -} - -// Next returns the next point from the iterator. -func (itr *stringReaderIterator) Next() (*StringPoint, error) { - // OPTIMIZE(benbjohnson): Reuse point on iterator. - - // Unmarshal next point. - p := &StringPoint{} - if err := itr.dec.DecodeStringPoint(p); err == io.EOF { - return nil, nil - } else if err != nil { - return nil, err - } - return p, nil -} - -// BooleanIterator represents a stream of boolean points. -type BooleanIterator interface { - Iterator - Next() (*BooleanPoint, error) -} - -// newBooleanIterators converts a slice of Iterator to a slice of BooleanIterator. -// Drop and closes any iterator in itrs that is not a BooleanIterator and cannot -// be cast to a BooleanIterator. -func newBooleanIterators(itrs []Iterator) []BooleanIterator { - a := make([]BooleanIterator, 0, len(itrs)) - for _, itr := range itrs { - switch itr := itr.(type) { - case BooleanIterator: - a = append(a, itr) - - default: - itr.Close() - } - } - return a -} - -// bufBooleanIterator represents a buffered BooleanIterator. -type bufBooleanIterator struct { - itr BooleanIterator - buf *BooleanPoint -} - -// newBufBooleanIterator returns a buffered BooleanIterator. -func newBufBooleanIterator(itr BooleanIterator) *bufBooleanIterator { - return &bufBooleanIterator{itr: itr} -} - -// Stats returns statistics from the input iterator. -func (itr *bufBooleanIterator) Stats() IteratorStats { return itr.itr.Stats() } - -// Close closes the underlying iterator. -func (itr *bufBooleanIterator) Close() error { return itr.itr.Close() } - -// peek returns the next point without removing it from the iterator. -func (itr *bufBooleanIterator) peek() (*BooleanPoint, error) { - p, err := itr.Next() - if err != nil { - return nil, err - } - itr.unread(p) - return p, nil -} - -// peekTime returns the time of the next point. -// Returns zero time if no more points available. -func (itr *bufBooleanIterator) peekTime() (int64, error) { - p, err := itr.peek() - if p == nil || err != nil { - return ZeroTime, err - } - return p.Time, nil -} - -// Next returns the current buffer, if exists, or calls the underlying iterator. -func (itr *bufBooleanIterator) Next() (*BooleanPoint, error) { - buf := itr.buf - if buf != nil { - itr.buf = nil - return buf, nil - } - return itr.itr.Next() -} - -// NextInWindow returns the next value if it is between [startTime, endTime). -// If the next value is outside the range then it is moved to the buffer. -func (itr *bufBooleanIterator) NextInWindow(startTime, endTime int64) (*BooleanPoint, error) { - v, err := itr.Next() - if v == nil || err != nil { - return nil, err - } else if t := v.Time; t >= endTime || t < startTime { - itr.unread(v) - return nil, nil - } - return v, nil -} - -// unread sets v to the buffer. It is read on the next call to Next(). -func (itr *bufBooleanIterator) unread(v *BooleanPoint) { itr.buf = v } - -// booleanMergeIterator represents an iterator that combines multiple boolean iterators. -type booleanMergeIterator struct { - inputs []BooleanIterator - heap *booleanMergeHeap - init bool - - // Current iterator and window. - curr *booleanMergeHeapItem - window struct { - name string - tags string - startTime int64 - endTime int64 - } -} - -// newBooleanMergeIterator returns a new instance of booleanMergeIterator. -func newBooleanMergeIterator(inputs []BooleanIterator, opt IteratorOptions) *booleanMergeIterator { - itr := &booleanMergeIterator{ - inputs: inputs, - heap: &booleanMergeHeap{ - items: make([]*booleanMergeHeapItem, 0, len(inputs)), - opt: opt, - }, - } - - // Initialize heap items. - for _, input := range inputs { - // Wrap in buffer, ignore any inputs without anymore points. - bufInput := newBufBooleanIterator(input) - - // Append to the heap. - itr.heap.items = append(itr.heap.items, &booleanMergeHeapItem{itr: bufInput}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *booleanMergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *booleanMergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - itr.curr = nil - itr.inputs = nil - itr.heap.items = nil - return nil -} - -// Next returns the next point from the iterator. -func (itr *booleanMergeIterator) Next() (*BooleanPoint, error) { - // Initialize the heap. This needs to be done lazily on the first call to this iterator - // so that iterator initialization done through the Select() call returns quickly. - // Queries can only be interrupted after the Select() call completes so any operations - // done during iterator creation cannot be interrupted, which is why we do it here - // instead so an interrupt can happen while initializing the heap. - if !itr.init { - items := itr.heap.items - itr.heap.items = make([]*booleanMergeHeapItem, 0, len(items)) - for _, item := range items { - if p, err := item.itr.peek(); err != nil { - return nil, err - } else if p == nil { - continue - } - itr.heap.items = append(itr.heap.items, item) - } - heap.Init(itr.heap) - itr.init = true - } - - for { - // Retrieve the next iterator if we don't have one. - if itr.curr == nil { - if len(itr.heap.items) == 0 { - return nil, nil - } - itr.curr = heap.Pop(itr.heap).(*booleanMergeHeapItem) - - // Read point and set current window. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags.ID() - itr.window.startTime, itr.window.endTime = itr.heap.opt.Window(p.Time) - return p, nil - } - - // Read the next point from the current iterator. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - - // If there are no more points then remove iterator from heap and find next. - if p == nil { - itr.curr = nil - continue - } - - // Check if the point is inside of our current window. - inWindow := true - if window := itr.window; window.name != p.Name { - inWindow = false - } else if window.tags != p.Tags.ID() { - inWindow = false - } else if opt := itr.heap.opt; opt.Ascending && p.Time >= window.endTime { - inWindow = false - } else if !opt.Ascending && p.Time < window.startTime { - inWindow = false - } - - // If it's outside our window then push iterator back on the heap and find new iterator. - if !inWindow { - itr.curr.itr.unread(p) - heap.Push(itr.heap, itr.curr) - itr.curr = nil - continue - } - - return p, nil - } -} - -// booleanMergeHeap represents a heap of booleanMergeHeapItems. -// Items are sorted by their next window and then by name/tags. -type booleanMergeHeap struct { - opt IteratorOptions - items []*booleanMergeHeapItem -} - -func (h booleanMergeHeap) Len() int { return len(h.items) } -func (h booleanMergeHeap) Swap(i, j int) { h.items[i], h.items[j] = h.items[j], h.items[i] } -func (h booleanMergeHeap) Less(i, j int) bool { - x, err := h.items[i].itr.peek() - if err != nil { - return true - } - y, err := h.items[j].itr.peek() - if err != nil { - return false - } - - if h.opt.Ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() < y.Tags.ID() - } - } else { - if x.Name != y.Name { - return x.Name > y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() > y.Tags.ID() - } - } - - xt, _ := h.opt.Window(x.Time) - yt, _ := h.opt.Window(y.Time) - - if h.opt.Ascending { - return xt < yt - } - return xt > yt -} - -func (h *booleanMergeHeap) Push(x interface{}) { - h.items = append(h.items, x.(*booleanMergeHeapItem)) -} - -func (h *booleanMergeHeap) Pop() interface{} { - old := h.items - n := len(old) - item := old[n-1] - h.items = old[0 : n-1] - return item -} - -type booleanMergeHeapItem struct { - itr *bufBooleanIterator -} - -// booleanSortedMergeIterator is an iterator that sorts and merges multiple iterators into one. -type booleanSortedMergeIterator struct { - inputs []BooleanIterator - opt IteratorOptions - heap booleanSortedMergeHeap - init bool -} - -// newBooleanSortedMergeIterator returns an instance of booleanSortedMergeIterator. -func newBooleanSortedMergeIterator(inputs []BooleanIterator, opt IteratorOptions) Iterator { - itr := &booleanSortedMergeIterator{ - inputs: inputs, - heap: make(booleanSortedMergeHeap, 0, len(inputs)), - opt: opt, - } - - // Initialize heap items. - for _, input := range inputs { - // Append to the heap. - itr.heap = append(itr.heap, &booleanSortedMergeHeapItem{itr: input, ascending: opt.Ascending}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *booleanSortedMergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *booleanSortedMergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - return nil -} - -// Next returns the next points from the iterator. -func (itr *booleanSortedMergeIterator) Next() (*BooleanPoint, error) { return itr.pop() } - -// pop returns the next point from the heap. -// Reads the next point from item's cursor and puts it back on the heap. -func (itr *booleanSortedMergeIterator) pop() (*BooleanPoint, error) { - // Initialize the heap. See the MergeIterator to see why this has to be done lazily. - if !itr.init { - items := itr.heap - itr.heap = make([]*booleanSortedMergeHeapItem, 0, len(items)) - for _, item := range items { - var err error - if item.point, err = item.itr.Next(); err != nil { - return nil, err - } else if item.point == nil { - continue - } - itr.heap = append(itr.heap, item) - } - heap.Init(&itr.heap) - itr.init = true - } - - if len(itr.heap) == 0 { - return nil, nil - } - - // Read the next item from the heap. - item := heap.Pop(&itr.heap).(*booleanSortedMergeHeapItem) - if item.err != nil { - return nil, item.err - } else if item.point == nil { - return nil, nil - } - - // Copy the point for return. - p := item.point.Clone() - - // Read the next item from the cursor. Push back to heap if one exists. - if item.point, item.err = item.itr.Next(); item.point != nil { - heap.Push(&itr.heap, item) - } - - return p, nil -} - -// booleanSortedMergeHeap represents a heap of booleanSortedMergeHeapItems. -type booleanSortedMergeHeap []*booleanSortedMergeHeapItem - -func (h booleanSortedMergeHeap) Len() int { return len(h) } -func (h booleanSortedMergeHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h booleanSortedMergeHeap) Less(i, j int) bool { - x, y := h[i].point, h[j].point - - if h[i].ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() < y.Tags.ID() - } - return x.Time < y.Time - } - - if x.Name != y.Name { - return x.Name > y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() > y.Tags.ID() - } - return x.Time > y.Time -} - -func (h *booleanSortedMergeHeap) Push(x interface{}) { - *h = append(*h, x.(*booleanSortedMergeHeapItem)) -} - -func (h *booleanSortedMergeHeap) Pop() interface{} { - old := *h - n := len(old) - item := old[n-1] - *h = old[0 : n-1] - return item -} - -type booleanSortedMergeHeapItem struct { - point *BooleanPoint - err error - itr BooleanIterator - ascending bool -} - -// booleanParallelIterator represents an iterator that pulls data in a separate goroutine. -type booleanParallelIterator struct { - input BooleanIterator - ch chan booleanPointError - - once sync.Once - closing chan struct{} - wg sync.WaitGroup -} - -// newBooleanParallelIterator returns a new instance of booleanParallelIterator. -func newBooleanParallelIterator(input BooleanIterator) *booleanParallelIterator { - itr := &booleanParallelIterator{ - input: input, - ch: make(chan booleanPointError, 1), - closing: make(chan struct{}), - } - itr.wg.Add(1) - go itr.monitor() - return itr -} - -// Stats returns stats from the underlying iterator. -func (itr *booleanParallelIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *booleanParallelIterator) Close() error { - itr.once.Do(func() { close(itr.closing) }) - itr.wg.Wait() - return itr.input.Close() -} - -// Next returns the next point from the iterator. -func (itr *booleanParallelIterator) Next() (*BooleanPoint, error) { - v, ok := <-itr.ch - if !ok { - return nil, io.EOF - } - return v.point, v.err -} - -// monitor runs in a separate goroutine and actively pulls the next point. -func (itr *booleanParallelIterator) monitor() { - defer close(itr.ch) - defer itr.wg.Done() - - for { - // Read next point. - p, err := itr.input.Next() - - select { - case <-itr.closing: - return - case itr.ch <- booleanPointError{point: p, err: err}: - } - } -} - -type booleanPointError struct { - point *BooleanPoint - err error -} - -// booleanLimitIterator represents an iterator that limits points per group. -type booleanLimitIterator struct { - input BooleanIterator - opt IteratorOptions - n int - - prev struct { - name string - tags Tags - } -} - -// newBooleanLimitIterator returns a new instance of booleanLimitIterator. -func newBooleanLimitIterator(input BooleanIterator, opt IteratorOptions) *booleanLimitIterator { - return &booleanLimitIterator{ - input: input, - opt: opt, - } -} - -// Stats returns stats from the underlying iterator. -func (itr *booleanLimitIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *booleanLimitIterator) Close() error { return itr.input.Close() } - -// Next returns the next point from the iterator. -func (itr *booleanLimitIterator) Next() (*BooleanPoint, error) { - for { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Reset window and counter if a new window is encountered. - if p.Name != itr.prev.name || !p.Tags.Equals(&itr.prev.tags) { - itr.prev.name = p.Name - itr.prev.tags = p.Tags - itr.n = 0 - } - - // Increment counter. - itr.n++ - - // Read next point if not beyond the offset. - if itr.n <= itr.opt.Offset { - continue - } - - // Read next point if we're beyond the limit. - if itr.opt.Limit > 0 && (itr.n-itr.opt.Offset) > itr.opt.Limit { - // If there's no interval, no groups, and a single source then simply exit. - if itr.opt.Interval.IsZero() && len(itr.opt.Dimensions) == 0 && len(itr.opt.Sources) == 1 { - return nil, nil - } - continue - } - - return p, nil - } -} - -type booleanFillIterator struct { - input *bufBooleanIterator - prev BooleanPoint - startTime int64 - endTime int64 - auxFields []interface{} - init bool - opt IteratorOptions - - window struct { - name string - tags Tags - time int64 - } -} - -func newBooleanFillIterator(input BooleanIterator, expr Expr, opt IteratorOptions) *booleanFillIterator { - if opt.Fill == NullFill { - if expr, ok := expr.(*Call); ok && expr.Name == "count" { - opt.Fill = NumberFill - opt.FillValue = false - } - } - - var startTime, endTime int64 - if opt.Ascending { - startTime, _ = opt.Window(opt.StartTime) - endTime, _ = opt.Window(opt.EndTime) - } else { - startTime, _ = opt.Window(opt.EndTime) - endTime, _ = opt.Window(opt.StartTime) - } - - var auxFields []interface{} - if len(opt.Aux) > 0 { - auxFields = make([]interface{}, len(opt.Aux)) - } - - return &booleanFillIterator{ - input: newBufBooleanIterator(input), - prev: BooleanPoint{Nil: true}, - startTime: startTime, - endTime: endTime, - auxFields: auxFields, - opt: opt, - } -} - -func (itr *booleanFillIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *booleanFillIterator) Close() error { return itr.input.Close() } - -func (itr *booleanFillIterator) Next() (*BooleanPoint, error) { - if !itr.init { - p, err := itr.input.peek() - if p == nil || err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.init = true - } - - p, err := itr.input.Next() - if err != nil { - return nil, err - } - - // Check if the next point is outside of our window or is nil. - for p == nil || p.Name != itr.window.name || p.Tags.ID() != itr.window.tags.ID() { - // If we are inside of an interval, unread the point and continue below to - // constructing a new point. - if itr.opt.Ascending { - if itr.window.time <= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } else { - if itr.window.time >= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } - - // We are *not* in a current interval. If there is no next point, - // we are at the end of all intervals. - if p == nil { - return nil, nil - } - - // Set the new interval. - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.prev = BooleanPoint{Nil: true} - break - } - - // Check if the point is our next expected point. - if p == nil || (itr.opt.Ascending && p.Time > itr.window.time) || (!itr.opt.Ascending && p.Time < itr.window.time) { - if p != nil { - itr.input.unread(p) - } - - p = &BooleanPoint{ - Name: itr.window.name, - Tags: itr.window.tags, - Time: itr.window.time, - Aux: itr.auxFields, - } - - switch itr.opt.Fill { - case LinearFill: - fallthrough - case NullFill: - p.Nil = true - case NumberFill: - p.Value = castToBoolean(itr.opt.FillValue) - case PreviousFill: - if !itr.prev.Nil { - p.Value = itr.prev.Value - p.Nil = itr.prev.Nil - } else { - p.Nil = true - } - } - } else { - itr.prev = *p - } - - // Advance the expected time. Do not advance to a new window here - // as there may be lingering points with the same timestamp in the previous - // window. - if itr.opt.Ascending { - itr.window.time = p.Time + int64(itr.opt.Interval.Duration) - } else { - itr.window.time = p.Time - int64(itr.opt.Interval.Duration) - } - return p, nil -} - -// booleanIntervalIterator represents a boolean implementation of IntervalIterator. -type booleanIntervalIterator struct { - input BooleanIterator - opt IteratorOptions -} - -func newBooleanIntervalIterator(input BooleanIterator, opt IteratorOptions) *booleanIntervalIterator { - return &booleanIntervalIterator{input: input, opt: opt} -} - -func (itr *booleanIntervalIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *booleanIntervalIterator) Close() error { return itr.input.Close() } - -func (itr *booleanIntervalIterator) Next() (*BooleanPoint, error) { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - p.Time, _ = itr.opt.Window(p.Time) - // If we see the minimum allowable time, set the time to zero so we don't - // break the default returned time for aggregate queries without times. - if p.Time == MinTime { - p.Time = 0 - } - return p, nil -} - -// booleanInterruptIterator represents a boolean implementation of InterruptIterator. -type booleanInterruptIterator struct { - input BooleanIterator - closing <-chan struct{} - count int -} - -func newBooleanInterruptIterator(input BooleanIterator, closing <-chan struct{}) *booleanInterruptIterator { - return &booleanInterruptIterator{input: input, closing: closing} -} - -func (itr *booleanInterruptIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *booleanInterruptIterator) Close() error { return itr.input.Close() } - -func (itr *booleanInterruptIterator) Next() (*BooleanPoint, error) { - // Only check if the channel is closed every N points. This - // intentionally checks on both 0 and N so that if the iterator - // has been interrupted before the first point is emitted it will - // not emit any points. - if itr.count&0xFF == 0xFF { - select { - case <-itr.closing: - return nil, nil - default: - // Reset iterator count to zero and fall through to emit the next point. - itr.count = 0 - } - } - - // Increment the counter for every point read. - itr.count++ - return itr.input.Next() -} - -// booleanCloseInterruptIterator represents a boolean implementation of CloseInterruptIterator. -type booleanCloseInterruptIterator struct { - input BooleanIterator - closing <-chan struct{} - done chan struct{} - once sync.Once -} - -func newBooleanCloseInterruptIterator(input BooleanIterator, closing <-chan struct{}) *booleanCloseInterruptIterator { - itr := &booleanCloseInterruptIterator{ - input: input, - closing: closing, - done: make(chan struct{}), - } - go itr.monitor() - return itr -} - -func (itr *booleanCloseInterruptIterator) monitor() { - select { - case <-itr.closing: - itr.Close() - case <-itr.done: - } -} - -func (itr *booleanCloseInterruptIterator) Stats() IteratorStats { - return itr.input.Stats() -} - -func (itr *booleanCloseInterruptIterator) Close() error { - itr.once.Do(func() { - close(itr.done) - itr.input.Close() - }) - return nil -} - -func (itr *booleanCloseInterruptIterator) Next() (*BooleanPoint, error) { - p, err := itr.input.Next() - if err != nil { - // Check if the iterator was closed. - select { - case <-itr.done: - return nil, nil - default: - return nil, err - } - } - return p, nil -} - -// auxBooleanPoint represents a combination of a point and an error for the AuxIterator. -type auxBooleanPoint struct { - point *BooleanPoint - err error -} - -// booleanAuxIterator represents a boolean implementation of AuxIterator. -type booleanAuxIterator struct { - input *bufBooleanIterator - output chan auxBooleanPoint - fields auxIteratorFields - background bool -} - -func newBooleanAuxIterator(input BooleanIterator, opt IteratorOptions) *booleanAuxIterator { - return &booleanAuxIterator{ - input: newBufBooleanIterator(input), - output: make(chan auxBooleanPoint, 1), - fields: newAuxIteratorFields(opt), - } -} - -func (itr *booleanAuxIterator) Background() { - itr.background = true - itr.Start() - go DrainIterator(itr) -} - -func (itr *booleanAuxIterator) Start() { go itr.stream() } -func (itr *booleanAuxIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *booleanAuxIterator) Close() error { return itr.input.Close() } -func (itr *booleanAuxIterator) Next() (*BooleanPoint, error) { - p := <-itr.output - return p.point, p.err -} -func (itr *booleanAuxIterator) Iterator(name string, typ DataType) Iterator { - return itr.fields.iterator(name, typ) -} - -func (itr *booleanAuxIterator) CreateIterator(opt IteratorOptions) (Iterator, error) { - expr := opt.Expr - if expr == nil { - panic("unable to create an iterator with no expression from an aux iterator") - } - - switch expr := expr.(type) { - case *VarRef: - return itr.Iterator(expr.Val, expr.Type), nil - default: - panic(fmt.Sprintf("invalid expression type for an aux iterator: %T", expr)) - } -} - -func (itr *booleanAuxIterator) FieldDimensions(sources Sources) (fields map[string]DataType, dimensions map[string]struct{}, err error) { - return nil, nil, errors.New("not implemented") -} - -func (itr *booleanAuxIterator) ExpandSources(sources Sources) (Sources, error) { - return nil, errors.New("not implemented") -} - -func (itr *booleanAuxIterator) stream() { - for { - // Read next point. - p, err := itr.input.Next() - if err != nil { - itr.output <- auxBooleanPoint{err: err} - itr.fields.sendError(err) - break - } else if p == nil { - break - } - - // Send point to output and to each field iterator. - itr.output <- auxBooleanPoint{point: p} - if ok := itr.fields.send(p); !ok && itr.background { - break - } - } - - close(itr.output) - itr.fields.close() -} - -// booleanChanIterator represents a new instance of booleanChanIterator. -type booleanChanIterator struct { - buf struct { - i int - filled bool - points [2]BooleanPoint - } - err error - cond *sync.Cond - done bool -} - -func (itr *booleanChanIterator) Stats() IteratorStats { return IteratorStats{} } - -func (itr *booleanChanIterator) Close() error { - itr.cond.L.Lock() - // Mark the channel iterator as done and signal all waiting goroutines to start again. - itr.done = true - itr.cond.Broadcast() - // Do not defer the unlock so we don't create an unnecessary allocation. - itr.cond.L.Unlock() - return nil -} - -func (itr *booleanChanIterator) setBuf(name string, tags Tags, time int64, value interface{}) bool { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Wait for either the iterator to be done (so we don't have to set the value) - // or for the buffer to have been read and ready for another write. - for !itr.done && itr.buf.filled { - itr.cond.Wait() - } - - // Do not set the value and return false to signal that the iterator is closed. - // Do this after the above wait as the above for loop may have exited because - // the iterator was closed. - if itr.done { - return false - } - - switch v := value.(type) { - case bool: - itr.buf.points[itr.buf.i] = BooleanPoint{Name: name, Tags: tags, Time: time, Value: v} - - default: - itr.buf.points[itr.buf.i] = BooleanPoint{Name: name, Tags: tags, Time: time, Nil: true} - } - itr.buf.filled = true - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() - return true -} - -func (itr *booleanChanIterator) setErr(err error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - itr.err = err - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() -} - -func (itr *booleanChanIterator) Next() (*BooleanPoint, error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Check for an error and return one if there. - if itr.err != nil { - return nil, itr.err - } - - // Wait until either a value is available in the buffer or - // the iterator is closed. - for !itr.done && !itr.buf.filled { - itr.cond.Wait() - } - - // Return nil once the channel is done and the buffer is empty. - if itr.done && !itr.buf.filled { - return nil, nil - } - - // Always read from the buffer if it exists, even if the iterator - // is closed. This prevents the last value from being truncated by - // the parent iterator. - p := &itr.buf.points[itr.buf.i] - itr.buf.i = (itr.buf.i + 1) % len(itr.buf.points) - itr.buf.filled = false - itr.cond.Signal() - return p, nil -} - -// booleanReduceFloatIterator executes a reducer for every interval and buffers the result. -type booleanReduceFloatIterator struct { - input *bufBooleanIterator - create func() (BooleanPointAggregator, FloatPointEmitter) - opt IteratorOptions - points []FloatPoint -} - -// Stats returns stats from the input iterator. -func (itr *booleanReduceFloatIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanReduceFloatIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *booleanReduceFloatIterator) Next() (*FloatPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// booleanReduceFloatPoint stores the reduced data for a name/tag combination. -type booleanReduceFloatPoint struct { - Name string - Tags Tags - Aggregator BooleanPointAggregator - Emitter FloatPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *booleanReduceFloatIterator) reduce() ([]FloatPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*booleanReduceFloatPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &booleanReduceFloatPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateBoolean(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]FloatPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// booleanStreamFloatIterator streams inputs into the iterator and emits points gradually. -type booleanStreamFloatIterator struct { - input *bufBooleanIterator - create func() (BooleanPointAggregator, FloatPointEmitter) - opt IteratorOptions - m map[string]*booleanReduceFloatPoint - points []FloatPoint -} - -// newBooleanStreamFloatIterator returns a new instance of booleanStreamFloatIterator. -func newBooleanStreamFloatIterator(input BooleanIterator, createFn func() (BooleanPointAggregator, FloatPointEmitter), opt IteratorOptions) *booleanStreamFloatIterator { - return &booleanStreamFloatIterator{ - input: newBufBooleanIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*booleanReduceFloatPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *booleanStreamFloatIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanStreamFloatIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *booleanStreamFloatIterator) Next() (*FloatPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *booleanStreamFloatIterator) reduce() ([]FloatPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &booleanReduceFloatPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateBoolean(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// booleanFloatExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type booleanFloatExprIterator struct { - left *bufBooleanIterator - right *bufBooleanIterator - fn booleanFloatExprFunc - points []BooleanPoint // must be size 2 - storePrev bool -} - -func newBooleanFloatExprIterator(left, right BooleanIterator, opt IteratorOptions, fn func(a, b bool) float64) *booleanFloatExprIterator { - var points []BooleanPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []BooleanPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToBoolean(opt.FillValue) - points = []BooleanPoint{{Value: value}, {Value: value}} - } - return &booleanFloatExprIterator{ - left: newBufBooleanIterator(left), - right: newBufBooleanIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *booleanFloatExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *booleanFloatExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *booleanFloatExprIterator) Next() (*FloatPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &FloatPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// booleanFloatExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type booleanFloatExprFunc func(a, b bool) float64 - -// booleanReduceIntegerIterator executes a reducer for every interval and buffers the result. -type booleanReduceIntegerIterator struct { - input *bufBooleanIterator - create func() (BooleanPointAggregator, IntegerPointEmitter) - opt IteratorOptions - points []IntegerPoint -} - -// Stats returns stats from the input iterator. -func (itr *booleanReduceIntegerIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanReduceIntegerIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *booleanReduceIntegerIterator) Next() (*IntegerPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// booleanReduceIntegerPoint stores the reduced data for a name/tag combination. -type booleanReduceIntegerPoint struct { - Name string - Tags Tags - Aggregator BooleanPointAggregator - Emitter IntegerPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *booleanReduceIntegerIterator) reduce() ([]IntegerPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*booleanReduceIntegerPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &booleanReduceIntegerPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateBoolean(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]IntegerPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// booleanStreamIntegerIterator streams inputs into the iterator and emits points gradually. -type booleanStreamIntegerIterator struct { - input *bufBooleanIterator - create func() (BooleanPointAggregator, IntegerPointEmitter) - opt IteratorOptions - m map[string]*booleanReduceIntegerPoint - points []IntegerPoint -} - -// newBooleanStreamIntegerIterator returns a new instance of booleanStreamIntegerIterator. -func newBooleanStreamIntegerIterator(input BooleanIterator, createFn func() (BooleanPointAggregator, IntegerPointEmitter), opt IteratorOptions) *booleanStreamIntegerIterator { - return &booleanStreamIntegerIterator{ - input: newBufBooleanIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*booleanReduceIntegerPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *booleanStreamIntegerIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanStreamIntegerIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *booleanStreamIntegerIterator) Next() (*IntegerPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *booleanStreamIntegerIterator) reduce() ([]IntegerPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &booleanReduceIntegerPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateBoolean(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// booleanIntegerExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type booleanIntegerExprIterator struct { - left *bufBooleanIterator - right *bufBooleanIterator - fn booleanIntegerExprFunc - points []BooleanPoint // must be size 2 - storePrev bool -} - -func newBooleanIntegerExprIterator(left, right BooleanIterator, opt IteratorOptions, fn func(a, b bool) int64) *booleanIntegerExprIterator { - var points []BooleanPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []BooleanPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToBoolean(opt.FillValue) - points = []BooleanPoint{{Value: value}, {Value: value}} - } - return &booleanIntegerExprIterator{ - left: newBufBooleanIterator(left), - right: newBufBooleanIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *booleanIntegerExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *booleanIntegerExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *booleanIntegerExprIterator) Next() (*IntegerPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &IntegerPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// booleanIntegerExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type booleanIntegerExprFunc func(a, b bool) int64 - -// booleanReduceStringIterator executes a reducer for every interval and buffers the result. -type booleanReduceStringIterator struct { - input *bufBooleanIterator - create func() (BooleanPointAggregator, StringPointEmitter) - opt IteratorOptions - points []StringPoint -} - -// Stats returns stats from the input iterator. -func (itr *booleanReduceStringIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanReduceStringIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *booleanReduceStringIterator) Next() (*StringPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// booleanReduceStringPoint stores the reduced data for a name/tag combination. -type booleanReduceStringPoint struct { - Name string - Tags Tags - Aggregator BooleanPointAggregator - Emitter StringPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *booleanReduceStringIterator) reduce() ([]StringPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*booleanReduceStringPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &booleanReduceStringPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateBoolean(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]StringPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// booleanStreamStringIterator streams inputs into the iterator and emits points gradually. -type booleanStreamStringIterator struct { - input *bufBooleanIterator - create func() (BooleanPointAggregator, StringPointEmitter) - opt IteratorOptions - m map[string]*booleanReduceStringPoint - points []StringPoint -} - -// newBooleanStreamStringIterator returns a new instance of booleanStreamStringIterator. -func newBooleanStreamStringIterator(input BooleanIterator, createFn func() (BooleanPointAggregator, StringPointEmitter), opt IteratorOptions) *booleanStreamStringIterator { - return &booleanStreamStringIterator{ - input: newBufBooleanIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*booleanReduceStringPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *booleanStreamStringIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanStreamStringIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *booleanStreamStringIterator) Next() (*StringPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *booleanStreamStringIterator) reduce() ([]StringPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &booleanReduceStringPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateBoolean(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// booleanStringExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type booleanStringExprIterator struct { - left *bufBooleanIterator - right *bufBooleanIterator - fn booleanStringExprFunc - points []BooleanPoint // must be size 2 - storePrev bool -} - -func newBooleanStringExprIterator(left, right BooleanIterator, opt IteratorOptions, fn func(a, b bool) string) *booleanStringExprIterator { - var points []BooleanPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []BooleanPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToBoolean(opt.FillValue) - points = []BooleanPoint{{Value: value}, {Value: value}} - } - return &booleanStringExprIterator{ - left: newBufBooleanIterator(left), - right: newBufBooleanIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *booleanStringExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *booleanStringExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *booleanStringExprIterator) Next() (*StringPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - p := &StringPoint{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil - - } -} - -// booleanStringExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type booleanStringExprFunc func(a, b bool) string - -// booleanReduceBooleanIterator executes a reducer for every interval and buffers the result. -type booleanReduceBooleanIterator struct { - input *bufBooleanIterator - create func() (BooleanPointAggregator, BooleanPointEmitter) - opt IteratorOptions - points []BooleanPoint -} - -// Stats returns stats from the input iterator. -func (itr *booleanReduceBooleanIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanReduceBooleanIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *booleanReduceBooleanIterator) Next() (*BooleanPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// booleanReduceBooleanPoint stores the reduced data for a name/tag combination. -type booleanReduceBooleanPoint struct { - Name string - Tags Tags - Aggregator BooleanPointAggregator - Emitter BooleanPointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *booleanReduceBooleanIterator) reduce() ([]BooleanPoint, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*booleanReduceBooleanPoint) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &booleanReduceBooleanPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.AggregateBoolean(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]BooleanPoint, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points) - 1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// booleanStreamBooleanIterator streams inputs into the iterator and emits points gradually. -type booleanStreamBooleanIterator struct { - input *bufBooleanIterator - create func() (BooleanPointAggregator, BooleanPointEmitter) - opt IteratorOptions - m map[string]*booleanReduceBooleanPoint - points []BooleanPoint -} - -// newBooleanStreamBooleanIterator returns a new instance of booleanStreamBooleanIterator. -func newBooleanStreamBooleanIterator(input BooleanIterator, createFn func() (BooleanPointAggregator, BooleanPointEmitter), opt IteratorOptions) *booleanStreamBooleanIterator { - return &booleanStreamBooleanIterator{ - input: newBufBooleanIterator(input), - create: createFn, - opt: opt, - m: make(map[string]*booleanReduceBooleanPoint), - } -} - -// Stats returns stats from the input iterator. -func (itr *booleanStreamBooleanIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanStreamBooleanIterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *booleanStreamBooleanIterator) Next() (*BooleanPoint, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *booleanStreamBooleanIterator) reduce() ([]BooleanPoint, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &booleanReduceBooleanPoint{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.AggregateBoolean(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// booleanExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type booleanExprIterator struct { - left *bufBooleanIterator - right *bufBooleanIterator - fn booleanExprFunc - points []BooleanPoint // must be size 2 - storePrev bool -} - -func newBooleanExprIterator(left, right BooleanIterator, opt IteratorOptions, fn func(a, b bool) bool) *booleanExprIterator { - var points []BooleanPoint - switch opt.Fill { - case NullFill, PreviousFill: - points = []BooleanPoint{{Nil: true}, {Nil: true}} - case NumberFill: - value := castToBoolean(opt.FillValue) - points = []BooleanPoint{{Value: value}, {Value: value}} - } - return &booleanExprIterator{ - left: newBufBooleanIterator(left), - right: newBufBooleanIterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *booleanExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *booleanExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *booleanExprIterator) Next() (*BooleanPoint, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - - if a.Nil { - return a, nil - } else if b.Nil { - return b, nil - } - a.Value = itr.fn(a.Value, b.Value) - return a, nil - - } -} - -// booleanExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type booleanExprFunc func(a, b bool) bool - -// booleanTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type booleanTransformIterator struct { - input BooleanIterator - fn booleanTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *booleanTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *booleanTransformIterator) Next() (*BooleanPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - p = itr.fn(p) - } - return p, nil -} - -// booleanTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type booleanTransformFunc func(p *BooleanPoint) *BooleanPoint - -// booleanBoolTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type booleanBoolTransformIterator struct { - input BooleanIterator - fn booleanBoolTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *booleanBoolTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanBoolTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *booleanBoolTransformIterator) Next() (*BooleanPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - return itr.fn(p), nil - } - return nil, nil -} - -// booleanBoolTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type booleanBoolTransformFunc func(p *BooleanPoint) *BooleanPoint - -// booleanDedupeIterator only outputs unique points. -// This differs from the DistinctIterator in that it compares all aux fields too. -// This iterator is relatively inefficient and should only be used on small -// datasets such as meta query results. -type booleanDedupeIterator struct { - input BooleanIterator - m map[string]struct{} // lookup of points already sent -} - -// newBooleanDedupeIterator returns a new instance of booleanDedupeIterator. -func newBooleanDedupeIterator(input BooleanIterator) *booleanDedupeIterator { - return &booleanDedupeIterator{ - input: input, - m: make(map[string]struct{}), - } -} - -// Stats returns stats from the input iterator. -func (itr *booleanDedupeIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *booleanDedupeIterator) Close() error { return itr.input.Close() } - -// Next returns the next unique point from the input iterator. -func (itr *booleanDedupeIterator) Next() (*BooleanPoint, error) { - for { - // Read next point. - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Serialize to bytes to store in lookup. - buf, err := proto.Marshal(encodeBooleanPoint(p)) - if err != nil { - return nil, err - } - - // If the point has already been output then move to the next point. - if _, ok := itr.m[string(buf)]; ok { - continue - } - - // Otherwise mark it as emitted and return point. - itr.m[string(buf)] = struct{}{} - return p, nil - } -} - -// booleanReaderIterator represents an iterator that streams from a reader. -type booleanReaderIterator struct { - r io.Reader - dec *BooleanPointDecoder -} - -// newBooleanReaderIterator returns a new instance of booleanReaderIterator. -func newBooleanReaderIterator(r io.Reader, stats IteratorStats) *booleanReaderIterator { - dec := NewBooleanPointDecoder(r) - dec.stats = stats - - return &booleanReaderIterator{ - r: r, - dec: dec, - } -} - -// Stats returns stats about points processed. -func (itr *booleanReaderIterator) Stats() IteratorStats { return itr.dec.stats } - -// Close closes the underlying reader, if applicable. -func (itr *booleanReaderIterator) Close() error { - if r, ok := itr.r.(io.ReadCloser); ok { - return r.Close() - } - return nil -} - -// Next returns the next point from the iterator. -func (itr *booleanReaderIterator) Next() (*BooleanPoint, error) { - // OPTIMIZE(benbjohnson): Reuse point on iterator. - - // Unmarshal next point. - p := &BooleanPoint{} - if err := itr.dec.DecodeBooleanPoint(p); err == io.EOF { - return nil, nil - } else if err != nil { - return nil, err - } - return p, nil -} - -// IteratorEncoder is an encoder for encoding an iterator's points to w. -type IteratorEncoder struct { - w io.Writer - - // Frequency with which stats are emitted. - StatsInterval time.Duration -} - -// NewIteratorEncoder encodes an iterator's points to w. -func NewIteratorEncoder(w io.Writer) *IteratorEncoder { - return &IteratorEncoder{ - w: w, - - StatsInterval: DefaultStatsInterval, - } -} - -// EncodeIterator encodes and writes all of itr's points to the underlying writer. -func (enc *IteratorEncoder) EncodeIterator(itr Iterator) error { - switch itr := itr.(type) { - case FloatIterator: - return enc.encodeFloatIterator(itr) - case IntegerIterator: - return enc.encodeIntegerIterator(itr) - case StringIterator: - return enc.encodeStringIterator(itr) - case BooleanIterator: - return enc.encodeBooleanIterator(itr) - default: - panic(fmt.Sprintf("unsupported iterator for encoder: %T", itr)) - } -} - -// encodeFloatIterator encodes all points from itr to the underlying writer. -func (enc *IteratorEncoder) encodeFloatIterator(itr FloatIterator) error { - ticker := time.NewTicker(enc.StatsInterval) - defer ticker.Stop() - - // Emit initial stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - - // Continually stream points from the iterator into the encoder. - penc := NewFloatPointEncoder(enc.w) - for { - // Emit stats periodically. - select { - case <-ticker.C: - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - default: - } - - // Retrieve the next point from the iterator. - p, err := itr.Next() - if err != nil { - return err - } else if p == nil { - break - } - - // Write the point to the point encoder. - if err := penc.EncodeFloatPoint(p); err != nil { - return err - } - } - - // Emit final stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - return nil -} - -// encodeIntegerIterator encodes all points from itr to the underlying writer. -func (enc *IteratorEncoder) encodeIntegerIterator(itr IntegerIterator) error { - ticker := time.NewTicker(enc.StatsInterval) - defer ticker.Stop() - - // Emit initial stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - - // Continually stream points from the iterator into the encoder. - penc := NewIntegerPointEncoder(enc.w) - for { - // Emit stats periodically. - select { - case <-ticker.C: - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - default: - } - - // Retrieve the next point from the iterator. - p, err := itr.Next() - if err != nil { - return err - } else if p == nil { - break - } - - // Write the point to the point encoder. - if err := penc.EncodeIntegerPoint(p); err != nil { - return err - } - } - - // Emit final stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - return nil -} - -// encodeStringIterator encodes all points from itr to the underlying writer. -func (enc *IteratorEncoder) encodeStringIterator(itr StringIterator) error { - ticker := time.NewTicker(enc.StatsInterval) - defer ticker.Stop() - - // Emit initial stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - - // Continually stream points from the iterator into the encoder. - penc := NewStringPointEncoder(enc.w) - for { - // Emit stats periodically. - select { - case <-ticker.C: - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - default: - } - - // Retrieve the next point from the iterator. - p, err := itr.Next() - if err != nil { - return err - } else if p == nil { - break - } - - // Write the point to the point encoder. - if err := penc.EncodeStringPoint(p); err != nil { - return err - } - } - - // Emit final stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - return nil -} - -// encodeBooleanIterator encodes all points from itr to the underlying writer. -func (enc *IteratorEncoder) encodeBooleanIterator(itr BooleanIterator) error { - ticker := time.NewTicker(enc.StatsInterval) - defer ticker.Stop() - - // Emit initial stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - - // Continually stream points from the iterator into the encoder. - penc := NewBooleanPointEncoder(enc.w) - for { - // Emit stats periodically. - select { - case <-ticker.C: - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - default: - } - - // Retrieve the next point from the iterator. - p, err := itr.Next() - if err != nil { - return err - } else if p == nil { - break - } - - // Write the point to the point encoder. - if err := penc.EncodeBooleanPoint(p); err != nil { - return err - } - } - - // Emit final stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - return nil -} - -// encode a stats object in the point stream. -func (enc *IteratorEncoder) encodeStats(stats IteratorStats) error { - buf, err := proto.Marshal(&internal.Point{ - Name: proto.String(""), - Tags: proto.String(""), - Time: proto.Int64(0), - Nil: proto.Bool(false), - - Stats: encodeIteratorStats(&stats), - }) - if err != nil { - return err - } - - if err := binary.Write(enc.w, binary.BigEndian, uint32(len(buf))); err != nil { - return err - } - if _, err := enc.w.Write(buf); err != nil { - return err - } - return nil -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/iterator.gen.go.tmpl b/vendor/github.com/influxdata/influxdb/influxql/iterator.gen.go.tmpl deleted file mode 100644 index 09dcbb58..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/iterator.gen.go.tmpl +++ /dev/null @@ -1,1586 +0,0 @@ -package influxql - -import ( - "container/heap" - "errors" - "encoding/binary" - "fmt" - "io" - "sort" - "sync" - "time" - - "github.com/gogo/protobuf/proto" - internal "github.com/influxdata/influxdb/influxql/internal" -) - -// DefaultStatsInterval is the default value for IteratorEncoder.StatsInterval. -const DefaultStatsInterval = 10 * time.Second - -{{with $types := .}}{{range $k := $types}} - -// {{$k.Name}}Iterator represents a stream of {{$k.name}} points. -type {{$k.Name}}Iterator interface { - Iterator - Next() (*{{$k.Name}}Point, error) -} - -// new{{$k.Name}}Iterators converts a slice of Iterator to a slice of {{$k.Name}}Iterator. -// Drop and closes any iterator in itrs that is not a {{$k.Name}}Iterator and cannot -// be cast to a {{$k.Name}}Iterator. -func new{{$k.Name}}Iterators(itrs []Iterator) []{{$k.Name}}Iterator { - a := make([]{{$k.Name}}Iterator, 0, len(itrs)) - for _, itr := range itrs { - switch itr := itr.(type) { - case {{$k.Name}}Iterator: - a = append(a, itr) -{{if eq .Name "Float"}} - case IntegerIterator: - a = append(a, &integerFloatCastIterator{input: itr}) -{{end}} - default: - itr.Close() - } - } - return a -} - - -// buf{{$k.Name}}Iterator represents a buffered {{$k.Name}}Iterator. -type buf{{$k.Name}}Iterator struct { - itr {{$k.Name}}Iterator - buf *{{$k.Name}}Point -} - -// newBuf{{$k.Name}}Iterator returns a buffered {{$k.Name}}Iterator. -func newBuf{{$k.Name}}Iterator(itr {{$k.Name}}Iterator) *buf{{$k.Name}}Iterator { - return &buf{{$k.Name}}Iterator{itr: itr} -} - -// Stats returns statistics from the input iterator. -func (itr *buf{{$k.Name}}Iterator) Stats() IteratorStats { return itr.itr.Stats() } - -// Close closes the underlying iterator. -func (itr *buf{{$k.Name}}Iterator) Close() error { return itr.itr.Close() } - -// peek returns the next point without removing it from the iterator. -func (itr *buf{{$k.Name}}Iterator) peek() (*{{$k.Name}}Point, error) { - p, err := itr.Next() - if err != nil { - return nil, err - } - itr.unread(p) - return p, nil -} - -// peekTime returns the time of the next point. -// Returns zero time if no more points available. -func (itr *buf{{$k.Name}}Iterator) peekTime() (int64, error) { - p, err := itr.peek() - if p == nil || err != nil { - return ZeroTime, err - } - return p.Time, nil -} - -// Next returns the current buffer, if exists, or calls the underlying iterator. -func (itr *buf{{$k.Name}}Iterator) Next() (*{{$k.Name}}Point, error) { - buf := itr.buf - if buf != nil { - itr.buf = nil - return buf, nil - } - return itr.itr.Next() -} - -// NextInWindow returns the next value if it is between [startTime, endTime). -// If the next value is outside the range then it is moved to the buffer. -func (itr *buf{{$k.Name}}Iterator) NextInWindow(startTime, endTime int64) (*{{$k.Name}}Point, error) { - v, err := itr.Next() - if v == nil || err != nil { - return nil, err - } else if t := v.Time; t >= endTime || t < startTime { - itr.unread(v) - return nil, nil - } - return v, nil -} - -// unread sets v to the buffer. It is read on the next call to Next(). -func (itr *buf{{$k.Name}}Iterator) unread(v *{{$k.Name}}Point) { itr.buf = v } - -// {{$k.name}}MergeIterator represents an iterator that combines multiple {{$k.name}} iterators. -type {{$k.name}}MergeIterator struct { - inputs []{{$k.Name}}Iterator - heap *{{$k.name}}MergeHeap - init bool - - // Current iterator and window. - curr *{{$k.name}}MergeHeapItem - window struct { - name string - tags string - startTime int64 - endTime int64 - } -} - -// new{{$k.Name}}MergeIterator returns a new instance of {{$k.name}}MergeIterator. -func new{{$k.Name}}MergeIterator(inputs []{{$k.Name}}Iterator, opt IteratorOptions) *{{$k.name}}MergeIterator { - itr := &{{$k.name}}MergeIterator{ - inputs: inputs, - heap: &{{$k.name}}MergeHeap{ - items: make([]*{{$k.name}}MergeHeapItem, 0, len(inputs)), - opt: opt, - }, - } - - // Initialize heap items. - for _, input := range inputs { - // Wrap in buffer, ignore any inputs without anymore points. - bufInput := newBuf{{$k.Name}}Iterator(input) - - // Append to the heap. - itr.heap.items = append(itr.heap.items, &{{$k.name}}MergeHeapItem{itr: bufInput}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *{{$k.name}}MergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *{{$k.name}}MergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - itr.curr = nil - itr.inputs = nil - itr.heap.items = nil - return nil -} - -// Next returns the next point from the iterator. -func (itr *{{$k.name}}MergeIterator) Next() (*{{$k.Name}}Point, error) { - // Initialize the heap. This needs to be done lazily on the first call to this iterator - // so that iterator initialization done through the Select() call returns quickly. - // Queries can only be interrupted after the Select() call completes so any operations - // done during iterator creation cannot be interrupted, which is why we do it here - // instead so an interrupt can happen while initializing the heap. - if !itr.init { - items := itr.heap.items - itr.heap.items = make([]*{{$k.name}}MergeHeapItem, 0, len(items)) - for _, item := range items { - if p, err := item.itr.peek(); err != nil { - return nil, err - } else if p == nil { - continue - } - itr.heap.items = append(itr.heap.items, item) - } - heap.Init(itr.heap) - itr.init = true - } - - for { - // Retrieve the next iterator if we don't have one. - if itr.curr == nil { - if len(itr.heap.items) == 0 { - return nil, nil - } - itr.curr = heap.Pop(itr.heap).(*{{$k.name}}MergeHeapItem) - - // Read point and set current window. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags.ID() - itr.window.startTime, itr.window.endTime = itr.heap.opt.Window(p.Time) - return p, nil - } - - // Read the next point from the current iterator. - p, err := itr.curr.itr.Next() - if err != nil { - return nil, err - } - - // If there are no more points then remove iterator from heap and find next. - if p == nil { - itr.curr = nil - continue - } - - // Check if the point is inside of our current window. - inWindow := true - if window := itr.window; window.name != p.Name { - inWindow = false - } else if window.tags != p.Tags.ID() { - inWindow = false - } else if opt := itr.heap.opt; opt.Ascending && p.Time >= window.endTime { - inWindow = false - } else if !opt.Ascending && p.Time < window.startTime { - inWindow = false - } - - // If it's outside our window then push iterator back on the heap and find new iterator. - if !inWindow { - itr.curr.itr.unread(p) - heap.Push(itr.heap, itr.curr) - itr.curr = nil - continue - } - - return p, nil - } -} - -// {{$k.name}}MergeHeap represents a heap of {{$k.name}}MergeHeapItems. -// Items are sorted by their next window and then by name/tags. -type {{$k.name}}MergeHeap struct { - opt IteratorOptions - items []*{{$k.name}}MergeHeapItem -} - -func (h {{$k.name}}MergeHeap) Len() int { return len(h.items) } -func (h {{$k.name}}MergeHeap) Swap(i, j int) { h.items[i], h.items[j] = h.items[j], h.items[i] } -func (h {{$k.name}}MergeHeap) Less(i, j int) bool { - x, err := h.items[i].itr.peek() - if err != nil { - return true - } - y, err := h.items[j].itr.peek() - if err != nil { - return false - } - - if h.opt.Ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() < y.Tags.ID() - } - } else { - if x.Name != y.Name { - return x.Name > y.Name - } else if x.Tags.ID() != y.Tags.ID() { - return x.Tags.ID() > y.Tags.ID() - } - } - - xt, _ := h.opt.Window(x.Time) - yt, _ := h.opt.Window(y.Time) - - if h.opt.Ascending { - return xt < yt - } - return xt > yt -} - - -func (h *{{$k.name}}MergeHeap) Push(x interface{}) { - h.items = append(h.items, x.(*{{$k.name}}MergeHeapItem)) -} - -func (h *{{$k.name}}MergeHeap) Pop() interface{} { - old := h.items - n := len(old) - item := old[n-1] - h.items = old[0 : n-1] - return item -} - -type {{$k.name}}MergeHeapItem struct { - itr *buf{{$k.Name}}Iterator -} - -// {{$k.name}}SortedMergeIterator is an iterator that sorts and merges multiple iterators into one. -type {{$k.name}}SortedMergeIterator struct { - inputs []{{$k.Name}}Iterator - opt IteratorOptions - heap {{$k.name}}SortedMergeHeap - init bool -} - -// new{{$k.Name}}SortedMergeIterator returns an instance of {{$k.name}}SortedMergeIterator. -func new{{$k.Name}}SortedMergeIterator(inputs []{{$k.Name}}Iterator, opt IteratorOptions) Iterator { - itr := &{{$k.name}}SortedMergeIterator{ - inputs: inputs, - heap: make({{$k.name}}SortedMergeHeap, 0, len(inputs)), - opt: opt, - } - - // Initialize heap items. - for _, input := range inputs { - // Append to the heap. - itr.heap = append(itr.heap, &{{$k.name}}SortedMergeHeapItem{itr: input, ascending: opt.Ascending}) - } - - return itr -} - -// Stats returns an aggregation of stats from the underlying iterators. -func (itr *{{$k.name}}SortedMergeIterator) Stats() IteratorStats { - var stats IteratorStats - for _, input := range itr.inputs { - stats.Add(input.Stats()) - } - return stats -} - -// Close closes the underlying iterators. -func (itr *{{$k.name}}SortedMergeIterator) Close() error { - for _, input := range itr.inputs { - input.Close() - } - return nil -} - -// Next returns the next points from the iterator. -func (itr *{{$k.name}}SortedMergeIterator) Next() (*{{$k.Name}}Point, error) { return itr.pop() } - -// pop returns the next point from the heap. -// Reads the next point from item's cursor and puts it back on the heap. -func (itr *{{$k.name}}SortedMergeIterator) pop() (*{{$k.Name}}Point, error) { - // Initialize the heap. See the MergeIterator to see why this has to be done lazily. - if !itr.init { - items := itr.heap - itr.heap = make([]*{{$k.name}}SortedMergeHeapItem, 0, len(items)) - for _, item := range items { - var err error - if item.point, err = item.itr.Next(); err != nil { - return nil, err - } else if item.point == nil { - continue - } - itr.heap = append(itr.heap, item) - } - heap.Init(&itr.heap) - itr.init = true - } - - if len(itr.heap) == 0 { - return nil, nil - } - - // Read the next item from the heap. - item := heap.Pop(&itr.heap).(*{{$k.name}}SortedMergeHeapItem) - if item.err != nil { - return nil, item.err - } else if item.point == nil { - return nil, nil - } - - // Copy the point for return. - p := item.point.Clone() - - // Read the next item from the cursor. Push back to heap if one exists. - if item.point, item.err = item.itr.Next(); item.point != nil { - heap.Push(&itr.heap, item) - } - - return p, nil -} - -// {{$k.name}}SortedMergeHeap represents a heap of {{$k.name}}SortedMergeHeapItems. -type {{$k.name}}SortedMergeHeap []*{{$k.name}}SortedMergeHeapItem - -func (h {{$k.name}}SortedMergeHeap) Len() int { return len(h) } -func (h {{$k.name}}SortedMergeHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h {{$k.name}}SortedMergeHeap) Less(i, j int) bool { - x, y := h[i].point, h[j].point - - if h[i].ascending { - if x.Name != y.Name { - return x.Name < y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() < y.Tags.ID() - } - return x.Time < y.Time - } - - if x.Name != y.Name { - return x.Name > y.Name - } else if !x.Tags.Equals(&y.Tags) { - return x.Tags.ID() > y.Tags.ID() - } - return x.Time > y.Time -} - -func (h *{{$k.name}}SortedMergeHeap) Push(x interface{}) { - *h = append(*h, x.(*{{$k.name}}SortedMergeHeapItem)) -} - -func (h *{{$k.name}}SortedMergeHeap) Pop() interface{} { - old := *h - n := len(old) - item := old[n-1] - *h = old[0 : n-1] - return item -} - -type {{$k.name}}SortedMergeHeapItem struct { - point *{{$k.Name}}Point - err error - itr {{$k.Name}}Iterator - ascending bool -} - -// {{$k.name}}ParallelIterator represents an iterator that pulls data in a separate goroutine. -type {{$k.name}}ParallelIterator struct { - input {{$k.Name}}Iterator - ch chan {{$k.name}}PointError - - once sync.Once - closing chan struct{} - wg sync.WaitGroup -} - -// new{{$k.Name}}ParallelIterator returns a new instance of {{$k.name}}ParallelIterator. -func new{{$k.Name}}ParallelIterator(input {{$k.Name}}Iterator) *{{$k.name}}ParallelIterator { - itr := &{{$k.name}}ParallelIterator{ - input: input, - ch: make(chan {{$k.name}}PointError, 1), - closing: make(chan struct{}), - } - itr.wg.Add(1) - go itr.monitor() - return itr -} - -// Stats returns stats from the underlying iterator. -func (itr *{{$k.name}}ParallelIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *{{$k.name}}ParallelIterator) Close() error { - itr.once.Do(func() { close(itr.closing) }) - itr.wg.Wait() - return itr.input.Close() -} - -// Next returns the next point from the iterator. -func (itr *{{$k.name}}ParallelIterator) Next() (*{{$k.Name}}Point, error) { - v, ok := <-itr.ch - if !ok { - return nil, io.EOF - } - return v.point, v.err -} - -// monitor runs in a separate goroutine and actively pulls the next point. -func (itr *{{$k.name}}ParallelIterator) monitor() { - defer close(itr.ch) - defer itr.wg.Done() - - for { - // Read next point. - p, err := itr.input.Next() - - select { - case <-itr.closing: - return - case itr.ch <- {{$k.name}}PointError{point: p, err: err}: - } - } -} - -type {{$k.name}}PointError struct { - point *{{$k.Name}}Point - err error -} - -// {{$k.name}}LimitIterator represents an iterator that limits points per group. -type {{$k.name}}LimitIterator struct { - input {{$k.Name}}Iterator - opt IteratorOptions - n int - - prev struct { - name string - tags Tags - } -} - -// new{{$k.Name}}LimitIterator returns a new instance of {{$k.name}}LimitIterator. -func new{{$k.Name}}LimitIterator(input {{$k.Name}}Iterator, opt IteratorOptions) *{{$k.name}}LimitIterator { - return &{{$k.name}}LimitIterator{ - input: input, - opt: opt, - } -} - -// Stats returns stats from the underlying iterator. -func (itr *{{$k.name}}LimitIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the underlying iterators. -func (itr *{{$k.name}}LimitIterator) Close() error { return itr.input.Close() } - -// Next returns the next point from the iterator. -func (itr *{{$k.name}}LimitIterator) Next() (*{{$k.Name}}Point, error) { - for { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Reset window and counter if a new window is encountered. - if p.Name != itr.prev.name || !p.Tags.Equals(&itr.prev.tags) { - itr.prev.name = p.Name - itr.prev.tags = p.Tags - itr.n = 0 - } - - // Increment counter. - itr.n++ - - // Read next point if not beyond the offset. - if itr.n <= itr.opt.Offset { - continue - } - - // Read next point if we're beyond the limit. - if itr.opt.Limit > 0 && (itr.n-itr.opt.Offset) > itr.opt.Limit { - // If there's no interval, no groups, and a single source then simply exit. - if itr.opt.Interval.IsZero() && len(itr.opt.Dimensions) == 0 && len(itr.opt.Sources) == 1 { - return nil, nil - } - continue - } - - return p, nil - } -} - -type {{$k.name}}FillIterator struct { - input *buf{{$k.Name}}Iterator - prev {{$k.Name}}Point - startTime int64 - endTime int64 - auxFields []interface{} - init bool - opt IteratorOptions - - window struct { - name string - tags Tags - time int64 - } -} - -func new{{$k.Name}}FillIterator(input {{$k.Name}}Iterator, expr Expr, opt IteratorOptions) *{{$k.name}}FillIterator { - if opt.Fill == NullFill { - if expr, ok := expr.(*Call); ok && expr.Name == "count" { - opt.Fill = NumberFill - opt.FillValue = {{$k.Zero}} - } - } - - var startTime, endTime int64 - if opt.Ascending { - startTime, _ = opt.Window(opt.StartTime) - endTime, _ = opt.Window(opt.EndTime) - } else { - startTime, _ = opt.Window(opt.EndTime) - endTime, _ = opt.Window(opt.StartTime) - } - - var auxFields []interface{} - if len(opt.Aux) > 0 { - auxFields = make([]interface{}, len(opt.Aux)) - } - - return &{{$k.name}}FillIterator{ - input: newBuf{{$k.Name}}Iterator(input), - prev: {{$k.Name}}Point{Nil: true}, - startTime: startTime, - endTime: endTime, - auxFields: auxFields, - opt: opt, - } -} - -func (itr *{{$k.name}}FillIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *{{$k.name}}FillIterator) Close() error { return itr.input.Close() } - -func (itr *{{$k.name}}FillIterator) Next() (*{{$k.Name}}Point, error) { - if !itr.init { - p, err := itr.input.peek() - if p == nil || err != nil { - return nil, err - } - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.init = true - } - - p, err := itr.input.Next() - if err != nil { - return nil, err - } - - // Check if the next point is outside of our window or is nil. - for p == nil || p.Name != itr.window.name || p.Tags.ID() != itr.window.tags.ID() { - // If we are inside of an interval, unread the point and continue below to - // constructing a new point. - if itr.opt.Ascending { - if itr.window.time <= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } else { - if itr.window.time >= itr.endTime { - itr.input.unread(p) - p = nil - break - } - } - - // We are *not* in a current interval. If there is no next point, - // we are at the end of all intervals. - if p == nil { - return nil, nil - } - - // Set the new interval. - itr.window.name, itr.window.tags = p.Name, p.Tags - itr.window.time = itr.startTime - itr.prev = {{$k.Name}}Point{Nil: true} - break - } - - // Check if the point is our next expected point. - if p == nil || (itr.opt.Ascending && p.Time > itr.window.time) || (!itr.opt.Ascending && p.Time < itr.window.time) { - if p != nil { - itr.input.unread(p) - } - - p = &{{$k.Name}}Point{ - Name: itr.window.name, - Tags: itr.window.tags, - Time: itr.window.time, - Aux: itr.auxFields, - } - - switch itr.opt.Fill { - case LinearFill: - {{- if or (eq $k.Name "Float") (eq $k.Name "Integer")}} - if !itr.prev.Nil { - next, err := itr.input.peek() - if err != nil { - return nil, err - } - if next != nil { - interval := int64(itr.opt.Interval.Duration) - start := itr.window.time / interval - p.Value = linear{{$k.Name}}(start, itr.prev.Time/interval, next.Time/interval, itr.prev.Value, next.Value) - } else { - p.Nil = true - } - } else { - p.Nil = true - } - {{else}} - fallthrough - {{- end}} - case NullFill: - p.Nil = true - case NumberFill: - p.Value = castTo{{$k.Name}}(itr.opt.FillValue) - case PreviousFill: - if !itr.prev.Nil { - p.Value = itr.prev.Value - p.Nil = itr.prev.Nil - } else { - p.Nil = true - } - } - } else { - itr.prev = *p - } - - // Advance the expected time. Do not advance to a new window here - // as there may be lingering points with the same timestamp in the previous - // window. - if itr.opt.Ascending { - itr.window.time = p.Time + int64(itr.opt.Interval.Duration) - } else { - itr.window.time = p.Time - int64(itr.opt.Interval.Duration) - } - return p, nil -} - -// {{$k.name}}IntervalIterator represents a {{$k.name}} implementation of IntervalIterator. -type {{$k.name}}IntervalIterator struct { - input {{$k.Name}}Iterator - opt IteratorOptions -} - -func new{{$k.Name}}IntervalIterator(input {{$k.Name}}Iterator, opt IteratorOptions) *{{$k.name}}IntervalIterator { - return &{{$k.name}}IntervalIterator{input: input, opt: opt} -} - -func (itr *{{$k.name}}IntervalIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *{{$k.name}}IntervalIterator) Close() error { return itr.input.Close() } - -func (itr *{{$k.name}}IntervalIterator) Next() (*{{$k.Name}}Point, error) { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - p.Time, _ = itr.opt.Window(p.Time) - // If we see the minimum allowable time, set the time to zero so we don't - // break the default returned time for aggregate queries without times. - if p.Time == MinTime { - p.Time = 0 - } - return p, nil -} - -// {{$k.name}}InterruptIterator represents a {{$k.name}} implementation of InterruptIterator. -type {{$k.name}}InterruptIterator struct { - input {{$k.Name}}Iterator - closing <-chan struct{} - count int -} - -func new{{$k.Name}}InterruptIterator(input {{$k.Name}}Iterator, closing <-chan struct{}) *{{$k.name}}InterruptIterator { - return &{{$k.name}}InterruptIterator{input: input, closing: closing} -} - -func (itr *{{$k.name}}InterruptIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *{{$k.name}}InterruptIterator) Close() error { return itr.input.Close() } - -func (itr *{{$k.name}}InterruptIterator) Next() (*{{$k.Name}}Point, error) { - // Only check if the channel is closed every N points. This - // intentionally checks on both 0 and N so that if the iterator - // has been interrupted before the first point is emitted it will - // not emit any points. - if itr.count & 0xFF == 0xFF { - select { - case <-itr.closing: - return nil, nil - default: - // Reset iterator count to zero and fall through to emit the next point. - itr.count = 0 - } - } - - // Increment the counter for every point read. - itr.count++ - return itr.input.Next() -} - -// {{$k.name}}CloseInterruptIterator represents a {{$k.name}} implementation of CloseInterruptIterator. -type {{$k.name}}CloseInterruptIterator struct { - input {{$k.Name}}Iterator - closing <-chan struct{} - done chan struct{} - once sync.Once -} - -func new{{$k.Name}}CloseInterruptIterator(input {{$k.Name}}Iterator, closing <-chan struct{}) *{{$k.name}}CloseInterruptIterator { - itr := &{{$k.name}}CloseInterruptIterator{ - input: input, - closing: closing, - done: make(chan struct{}), - } - go itr.monitor() - return itr -} - -func (itr *{{$k.name}}CloseInterruptIterator) monitor() { - select { - case <-itr.closing: - itr.Close() - case <-itr.done: - } -} - -func (itr *{{$k.name}}CloseInterruptIterator) Stats() IteratorStats { - return itr.input.Stats() -} - -func (itr *{{$k.name}}CloseInterruptIterator) Close() error { - itr.once.Do(func() { - close(itr.done) - itr.input.Close() - }) - return nil -} - -func (itr *{{$k.name}}CloseInterruptIterator) Next() (*{{$k.Name}}Point, error) { - p, err := itr.input.Next() - if err != nil { - // Check if the iterator was closed. - select { - case <-itr.done: - return nil, nil - default: - return nil, err - } - } - return p, nil -} - -// aux{{$k.Name}}Point represents a combination of a point and an error for the AuxIterator. -type aux{{$k.Name}}Point struct { - point *{{$k.Name}}Point - err error -} - -// {{$k.name}}AuxIterator represents a {{$k.name}} implementation of AuxIterator. -type {{$k.name}}AuxIterator struct { - input *buf{{$k.Name}}Iterator - output chan aux{{$k.Name}}Point - fields auxIteratorFields - background bool -} - -func new{{$k.Name}}AuxIterator(input {{$k.Name}}Iterator, opt IteratorOptions) *{{$k.name}}AuxIterator { - return &{{$k.name}}AuxIterator{ - input: newBuf{{$k.Name}}Iterator(input), - output: make(chan aux{{$k.Name}}Point, 1), - fields: newAuxIteratorFields(opt), - } -} - -func (itr *{{$k.name}}AuxIterator) Background() { - itr.background = true - itr.Start() - go DrainIterator(itr) -} - -func (itr *{{$k.name}}AuxIterator) Start() { go itr.stream() } -func (itr *{{$k.name}}AuxIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *{{$k.name}}AuxIterator) Close() error { return itr.input.Close() } -func (itr *{{$k.name}}AuxIterator) Next() (*{{$k.Name}}Point, error) { - p := <-itr.output - return p.point, p.err -} -func (itr *{{$k.name}}AuxIterator) Iterator(name string, typ DataType) Iterator { return itr.fields.iterator(name, typ) } - -func (itr *{{$k.name}}AuxIterator) CreateIterator(opt IteratorOptions) (Iterator, error) { - expr := opt.Expr - if expr == nil { - panic("unable to create an iterator with no expression from an aux iterator") - } - - switch expr := expr.(type) { - case *VarRef: - return itr.Iterator(expr.Val, expr.Type), nil - default: - panic(fmt.Sprintf("invalid expression type for an aux iterator: %T", expr)) - } -} - -func (itr *{{$k.name}}AuxIterator) FieldDimensions(sources Sources) (fields map[string]DataType, dimensions map[string]struct{}, err error) { - return nil, nil, errors.New("not implemented") -} - -func (itr *{{$k.name}}AuxIterator) ExpandSources(sources Sources) (Sources, error) { - return nil, errors.New("not implemented") -} - -func (itr *{{.name}}AuxIterator) stream() { - for { - // Read next point. - p, err := itr.input.Next() - if err != nil { - itr.output <- aux{{$k.Name}}Point{err: err} - itr.fields.sendError(err) - break - } else if p == nil { - break - } - - // Send point to output and to each field iterator. - itr.output <- aux{{$k.Name}}Point{point: p} - if ok := itr.fields.send(p); !ok && itr.background { - break - } - } - - close(itr.output) - itr.fields.close() -} - -// {{$k.name}}ChanIterator represents a new instance of {{$k.name}}ChanIterator. -type {{$k.name}}ChanIterator struct { - buf struct { - i int - filled bool - points [2]{{$k.Name}}Point - } - err error - cond *sync.Cond - done bool -} - -func (itr *{{$k.name}}ChanIterator) Stats() IteratorStats { return IteratorStats{} } - -func (itr *{{$k.name}}ChanIterator) Close() error { - itr.cond.L.Lock() - // Mark the channel iterator as done and signal all waiting goroutines to start again. - itr.done = true - itr.cond.Broadcast() - // Do not defer the unlock so we don't create an unnecessary allocation. - itr.cond.L.Unlock() - return nil -} - -func (itr *{{$k.name}}ChanIterator) setBuf(name string, tags Tags, time int64, value interface{}) bool { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Wait for either the iterator to be done (so we don't have to set the value) - // or for the buffer to have been read and ready for another write. - for !itr.done && itr.buf.filled { - itr.cond.Wait() - } - - // Do not set the value and return false to signal that the iterator is closed. - // Do this after the above wait as the above for loop may have exited because - // the iterator was closed. - if itr.done { - return false - } - - switch v := value.(type) { - case {{$k.Type}}: - itr.buf.points[itr.buf.i] = {{$k.Name}}Point{Name: name, Tags: tags, Time: time, Value: v} -{{if eq $k.Name "Float"}} - case int64: - itr.buf.points[itr.buf.i] = {{$k.Name}}Point{Name: name, Tags: tags, Time: time, Value: float64(v)} -{{end}} - default: - itr.buf.points[itr.buf.i] = {{$k.Name}}Point{Name: name, Tags: tags, Time: time, Nil: true} - } - itr.buf.filled = true - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() - return true -} - -func (itr *{{$k.name}}ChanIterator) setErr(err error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - itr.err = err - - // Signal to all waiting goroutines that a new value is ready to read. - itr.cond.Signal() -} - -func (itr *{{$k.name}}ChanIterator) Next() (*{{$k.Name}}Point, error) { - itr.cond.L.Lock() - defer itr.cond.L.Unlock() - - // Check for an error and return one if there. - if itr.err != nil { - return nil, itr.err - } - - // Wait until either a value is available in the buffer or - // the iterator is closed. - for !itr.done && !itr.buf.filled { - itr.cond.Wait() - } - - // Return nil once the channel is done and the buffer is empty. - if itr.done && !itr.buf.filled { - return nil, nil - } - - // Always read from the buffer if it exists, even if the iterator - // is closed. This prevents the last value from being truncated by - // the parent iterator. - p := &itr.buf.points[itr.buf.i] - itr.buf.i = (itr.buf.i + 1) % len(itr.buf.points) - itr.buf.filled = false - itr.cond.Signal() - return p, nil -} - -{{range $v := $types}} - -// {{$k.name}}Reduce{{$v.Name}}Iterator executes a reducer for every interval and buffers the result. -type {{$k.name}}Reduce{{$v.Name}}Iterator struct { - input *buf{{$k.Name}}Iterator - create func() ({{$k.Name}}PointAggregator, {{$v.Name}}PointEmitter) - opt IteratorOptions - points []{{$v.Name}}Point -} - -// Stats returns stats from the input iterator. -func (itr *{{$k.name}}Reduce{{$v.Name}}Iterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *{{$k.name}}Reduce{{$v.Name}}Iterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *{{$k.name}}Reduce{{$v.Name}}Iterator) Next() (*{{$v.Name}}Point, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// {{$k.name}}Reduce{{$v.Name}}Point stores the reduced data for a name/tag combination. -type {{$k.name}}Reduce{{$v.Name}}Point struct { - Name string - Tags Tags - Aggregator {{$k.Name}}PointAggregator - Emitter {{$v.Name}}PointEmitter -} - -// reduce executes fn once for every point in the next window. -// The previous value for the dimension is passed to fn. -func (itr *{{$k.name}}Reduce{{$v.Name}}Iterator) reduce() ([]{{$v.Name}}Point, error) { - // Calculate next window. - t, err := itr.input.peekTime() - if err != nil { - return nil, err - } - startTime, endTime := itr.opt.Window(t) - - // Create points by tags. - m := make(map[string]*{{$k.name}}Reduce{{$v.Name}}Point) - for { - // Read next point. - curr, err := itr.input.NextInWindow(startTime, endTime) - if err != nil { - return nil, err - } else if curr == nil { - break - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &{{$k.name}}Reduce{{.Name}}Point{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - m[id] = rp - } - rp.Aggregator.Aggregate{{$k.Name}}(curr) - } - - // Reverse sort points by name & tag. - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - if len(keys) > 1 { - sort.Sort(reverseStringSlice(keys)) - } - - a := make([]{{$v.Name}}Point, 0, len(m)) - for _, k := range keys { - rp := m[k] - points := rp.Emitter.Emit() - for i := len(points)-1; i >= 0; i-- { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - // Set the points time to the interval time if the reducer didn't provide one. - if points[i].Time == ZeroTime { - points[i].Time = startTime - } - a = append(a, points[i]) - } - } - - return a, nil -} - -// {{$k.name}}Stream{{$v.Name}}Iterator streams inputs into the iterator and emits points gradually. -type {{$k.name}}Stream{{$v.Name}}Iterator struct { - input *buf{{$k.Name}}Iterator - create func() ({{$k.Name}}PointAggregator, {{$v.Name}}PointEmitter) - opt IteratorOptions - m map[string]*{{$k.name}}Reduce{{$v.Name}}Point - points []{{$v.Name}}Point -} - -// new{{$k.Name}}Stream{{$v.Name}}Iterator returns a new instance of {{$k.name}}Stream{{$v.Name}}Iterator. -func new{{$k.Name}}Stream{{$v.Name}}Iterator(input {{$k.Name}}Iterator, createFn func() ({{$k.Name}}PointAggregator, {{$v.Name}}PointEmitter), opt IteratorOptions) *{{$k.name}}Stream{{$v.Name}}Iterator { - return &{{$k.name}}Stream{{$v.Name}}Iterator{ - input: newBuf{{$k.Name}}Iterator(input), - create: createFn, - opt: opt, - m: make(map[string]*{{$k.name}}Reduce{{$v.Name}}Point), - } -} - -// Stats returns stats from the input iterator. -func (itr *{{$k.name}}Stream{{$v.Name}}Iterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *{{$k.name}}Stream{{$v.Name}}Iterator) Close() error { return itr.input.Close() } - -// Next returns the next value for the stream iterator. -func (itr *{{$k.name}}Stream{{$v.Name}}Iterator) Next() (*{{$v.Name}}Point, error) { - // Calculate next window if we have no more points. - if len(itr.points) == 0 { - var err error - itr.points, err = itr.reduce() - if len(itr.points) == 0 { - return nil, err - } - } - - // Pop next point off the stack. - p := &itr.points[len(itr.points)-1] - itr.points = itr.points[:len(itr.points)-1] - return p, nil -} - -// reduce creates and manages aggregators for every point from the input. -// After aggregating a point, it always tries to emit a value using the emitter. -func (itr *{{$k.name}}Stream{{$v.Name}}Iterator) reduce() ([]{{$v.Name}}Point, error) { - for { - // Read next point. - curr, err := itr.input.Next() - if curr == nil || err != nil { - return nil, err - } else if curr.Nil { - continue - } - tags := curr.Tags.Subset(itr.opt.Dimensions) - - id := curr.Name - if len(tags.m) > 0 { - id += "\x00" + tags.ID() - } - - // Retrieve the aggregator for this name/tag combination or create one. - rp := itr.m[id] - if rp == nil { - aggregator, emitter := itr.create() - rp = &{{$k.name}}Reduce{{.Name}}Point{ - Name: curr.Name, - Tags: tags, - Aggregator: aggregator, - Emitter: emitter, - } - itr.m[id] = rp - } - rp.Aggregator.Aggregate{{$k.Name}}(curr) - - // Attempt to emit points from the aggregator. - points := rp.Emitter.Emit() - if len(points) == 0 { - continue - } - - for i := range points { - points[i].Name = rp.Name - points[i].Tags = rp.Tags - } - return points, nil - } -} - -// {{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprIterator executes a function to modify an existing point -// for every output of the input iterator. -type {{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprIterator struct { - left *buf{{$k.Name}}Iterator - right *buf{{$k.Name}}Iterator - fn {{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprFunc - points []{{$k.Name}}Point // must be size 2 - storePrev bool -} - -func new{{$k.Name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprIterator(left, right {{$k.Name}}Iterator, opt IteratorOptions, fn func(a, b {{$k.Type}}) {{$v.Type}}) *{{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprIterator { - var points []{{$k.Name}}Point - switch opt.Fill { - case NullFill, PreviousFill: - points = []{{$k.Name}}Point{ {Nil: true}, {Nil: true} } - case NumberFill: - value := castTo{{$k.Name}}(opt.FillValue) - points = []{{$k.Name}}Point{ {Value: value}, {Value: value} } - } - return &{{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprIterator{ - left: newBuf{{$k.Name}}Iterator(left), - right: newBuf{{$k.Name}}Iterator(right), - points: points, - fn: fn, - storePrev: opt.Fill == PreviousFill, - } -} - -func (itr *{{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprIterator) Stats() IteratorStats { - stats := itr.left.Stats() - stats.Add(itr.right.Stats()) - return stats -} - -func (itr *{{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprIterator) Close() error { - itr.left.Close() - itr.right.Close() - return nil -} - -func (itr *{{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprIterator) Next() (*{{$v.Name}}Point, error) { - for { - a, err := itr.left.Next() - if err != nil { - return nil, err - } - b, err := itr.right.Next() - if err != nil { - return nil, err - } - - if a == nil && b == nil { - return nil, nil - } else if itr.points == nil && (a == nil || b == nil ) { - return nil, nil - } - - if a != nil && b != nil { - if a.Time > b.Time { - itr.left.unread(a) - a = nil - } else if a.Time < b.Time { - itr.right.unread(b) - b = nil - } - } - - if a == nil || a.Nil { - if itr.points == nil { - continue - } - p := *b - p.Value = itr.points[0].Value - p.Nil = itr.points[0].Nil - a = &p - } else if b == nil || b.Nil { - if itr.points == nil { - continue - } - p := *a - p.Value = itr.points[1].Value - p.Nil = itr.points[1].Nil - b = &p - } - - if itr.storePrev { - itr.points[0], itr.points[1] = *a, *b - } - -{{if eq $k.Name $v.Name}} - if a.Nil { - return a, nil - } else if b.Nil { - return b, nil - } - a.Value = itr.fn(a.Value, b.Value) - return a, nil -{{else}} - p := &{{$v.Name}}Point{ - Name: a.Name, - Tags: a.Tags, - Time: a.Time, - Nil: a.Nil || b.Nil, - Aggregated: a.Aggregated, - } - if !p.Nil { - p.Value = itr.fn(a.Value, b.Value) - } - return p, nil -{{end}} - } -} - -// {{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprFunc creates or modifies a point by combining two -// points. The point passed in may be modified and returned rather than -// allocating a new point if possible. One of the points may be nil, but at -// least one of the points will be non-nil. -type {{$k.name}}{{if ne $k.Name $v.Name}}{{$v.Name}}{{end}}ExprFunc func(a, b {{$k.Type}}) {{$v.Type}} -{{end}} - -// {{$k.name}}TransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type {{$k.name}}TransformIterator struct { - input {{$k.Name}}Iterator - fn {{$k.name}}TransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *{{$k.name}}TransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *{{$k.name}}TransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *{{$k.name}}TransformIterator) Next() (*{{$k.Name}}Point, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - p = itr.fn(p) - } - return p, nil -} - -// {{$k.name}}TransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type {{$k.name}}TransformFunc func(p *{{$k.Name}}Point) *{{$k.Name}}Point - -// {{$k.name}}BoolTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type {{$k.name}}BoolTransformIterator struct { - input {{$k.Name}}Iterator - fn {{$k.name}}BoolTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *{{$k.name}}BoolTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *{{$k.name}}BoolTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *{{$k.name}}BoolTransformIterator) Next() (*BooleanPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - return itr.fn(p), nil - } - return nil, nil -} - -// {{$k.name}}BoolTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type {{$k.name}}BoolTransformFunc func(p *{{$k.Name}}Point) *BooleanPoint - -// {{$k.name}}DedupeIterator only outputs unique points. -// This differs from the DistinctIterator in that it compares all aux fields too. -// This iterator is relatively inefficient and should only be used on small -// datasets such as meta query results. -type {{$k.name}}DedupeIterator struct { - input {{$k.Name}}Iterator - m map[string]struct{} // lookup of points already sent -} - -// new{{$k.Name}}DedupeIterator returns a new instance of {{$k.name}}DedupeIterator. -func new{{$k.Name}}DedupeIterator(input {{$k.Name}}Iterator) *{{$k.name}}DedupeIterator { - return &{{$k.name}}DedupeIterator{ - input: input, - m: make(map[string]struct{}), - } -} - -// Stats returns stats from the input iterator. -func (itr *{{$k.name}}DedupeIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *{{$k.name}}DedupeIterator) Close() error { return itr.input.Close() } - -// Next returns the next unique point from the input iterator. -func (itr *{{$k.name}}DedupeIterator) Next() (*{{$k.Name}}Point, error) { - for { - // Read next point. - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - // Serialize to bytes to store in lookup. - buf, err := proto.Marshal(encode{{$k.Name}}Point(p)) - if err != nil { - return nil, err - } - - // If the point has already been output then move to the next point. - if _, ok := itr.m[string(buf)]; ok { - continue - } - - // Otherwise mark it as emitted and return point. - itr.m[string(buf)] = struct{}{} - return p, nil - } -} - -// {{$k.name}}ReaderIterator represents an iterator that streams from a reader. -type {{$k.name}}ReaderIterator struct { - r io.Reader - dec *{{$k.Name}}PointDecoder -} - -// new{{$k.Name}}ReaderIterator returns a new instance of {{$k.name}}ReaderIterator. -func new{{$k.Name}}ReaderIterator(r io.Reader, stats IteratorStats) *{{$k.name}}ReaderIterator { - dec := New{{$k.Name}}PointDecoder(r) - dec.stats = stats - - return &{{$k.name}}ReaderIterator{ - r: r, - dec: dec, - } -} - -// Stats returns stats about points processed. -func (itr *{{$k.name}}ReaderIterator) Stats() IteratorStats { return itr.dec.stats } - -// Close closes the underlying reader, if applicable. -func (itr *{{$k.name}}ReaderIterator) Close() error { - if r, ok := itr.r.(io.ReadCloser); ok { - return r.Close() - } - return nil -} - -// Next returns the next point from the iterator. -func (itr *{{$k.name}}ReaderIterator) Next() (*{{$k.Name}}Point, error) { - // OPTIMIZE(benbjohnson): Reuse point on iterator. - - // Unmarshal next point. - p := &{{$k.Name}}Point{} - if err := itr.dec.Decode{{$k.Name}}Point(p); err == io.EOF { - return nil, nil - } else if err != nil { - return nil, err - } - return p, nil -} -{{end}} - - -// IteratorEncoder is an encoder for encoding an iterator's points to w. -type IteratorEncoder struct { - w io.Writer - - // Frequency with which stats are emitted. - StatsInterval time.Duration -} - -// NewIteratorEncoder encodes an iterator's points to w. -func NewIteratorEncoder(w io.Writer) *IteratorEncoder { - return &IteratorEncoder{ - w: w, - - StatsInterval: DefaultStatsInterval, - } -} - -// EncodeIterator encodes and writes all of itr's points to the underlying writer. -func (enc *IteratorEncoder) EncodeIterator(itr Iterator) error { - switch itr := itr.(type) { - case FloatIterator: - return enc.encodeFloatIterator(itr) - case IntegerIterator: - return enc.encodeIntegerIterator(itr) - case StringIterator: - return enc.encodeStringIterator(itr) - case BooleanIterator: - return enc.encodeBooleanIterator(itr) - default: - panic(fmt.Sprintf("unsupported iterator for encoder: %T", itr)) - } -} - -{{range .}} -// encode{{.Name}}Iterator encodes all points from itr to the underlying writer. -func (enc *IteratorEncoder) encode{{.Name}}Iterator(itr {{.Name}}Iterator) error { - ticker := time.NewTicker(enc.StatsInterval) - defer ticker.Stop() - - // Emit initial stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - - // Continually stream points from the iterator into the encoder. - penc := New{{.Name}}PointEncoder(enc.w) - for { - // Emit stats periodically. - select { - case <-ticker.C: - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - default: - } - - // Retrieve the next point from the iterator. - p, err := itr.Next() - if err != nil { - return err - } else if p == nil { - break - } - - // Write the point to the point encoder. - if err := penc.Encode{{.Name}}Point(p); err != nil { - return err - } - } - - // Emit final stats. - if err := enc.encodeStats(itr.Stats()); err != nil { - return err - } - return nil -} - -{{end}} - -// encode a stats object in the point stream. -func (enc *IteratorEncoder) encodeStats(stats IteratorStats) error { - buf, err := proto.Marshal(&internal.Point{ - Name: proto.String(""), - Tags: proto.String(""), - Time: proto.Int64(0), - Nil: proto.Bool(false), - - Stats: encodeIteratorStats(&stats), - }) - if err != nil { - return err - } - - if err := binary.Write(enc.w, binary.BigEndian, uint32(len(buf))); err != nil { - return err - } - if _, err := enc.w.Write(buf); err != nil { - return err - } - return nil -} - -{{end}} diff --git a/vendor/github.com/influxdata/influxdb/influxql/iterator.go b/vendor/github.com/influxdata/influxdb/influxql/iterator.go deleted file mode 100644 index 3f1cea8f..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/iterator.go +++ /dev/null @@ -1,1223 +0,0 @@ -package influxql - -import ( - "errors" - "fmt" - "io" - "sort" - "sync" - "time" - - "github.com/influxdata/influxdb/models" - - "github.com/gogo/protobuf/proto" - internal "github.com/influxdata/influxdb/influxql/internal" -) - -// ErrUnknownCall is returned when operating on an unknown function call. -var ErrUnknownCall = errors.New("unknown call") - -const ( - // MinTime is used as the minimum time value when computing an unbounded range. - // This time is one less than the MinNanoTime so that the first minimum - // time can be used as a sentinel value to signify that it is the default - // value rather than explicitly set by the user. - MinTime = models.MinNanoTime - 1 - - // MaxTime is used as the maximum time value when computing an unbounded range. - // This time is 2262-04-11 23:47:16.854775806 +0000 UTC - MaxTime = models.MaxNanoTime -) - -// Iterator represents a generic interface for all Iterators. -// Most iterator operations are done on the typed sub-interfaces. -type Iterator interface { - Stats() IteratorStats - Close() error -} - -// Iterators represents a list of iterators. -type Iterators []Iterator - -// Stats returns the aggregation of all iterator stats. -func (a Iterators) Stats() IteratorStats { - var stats IteratorStats - for _, itr := range a { - stats.Add(itr.Stats()) - } - return stats -} - -// Close closes all iterators. -func (a Iterators) Close() error { - for _, itr := range a { - itr.Close() - } - return nil -} - -// filterNonNil returns a slice of iterators that removes all nil iterators. -func (a Iterators) filterNonNil() []Iterator { - other := make([]Iterator, 0, len(a)) - for _, itr := range a { - if itr == nil { - continue - } - other = append(other, itr) - } - return other -} - -// castType determines what type to cast the set of iterators to. -// An iterator type is chosen using this hierarchy: -// float > integer > string > boolean -func (a Iterators) castType() DataType { - if len(a) == 0 { - return Unknown - } - - typ := DataType(Boolean) - for _, input := range a { - switch input.(type) { - case FloatIterator: - // Once a float iterator is found, short circuit the end. - return Float - case IntegerIterator: - if typ > Integer { - typ = Integer - } - case StringIterator: - if typ > String { - typ = String - } - case BooleanIterator: - // Boolean is the lowest type. - } - } - return typ -} - -// cast casts an array of iterators to a single type. -// Iterators that are not compatible or cannot be cast to the -// chosen iterator type are closed and dropped. -func (a Iterators) cast() interface{} { - typ := a.castType() - switch typ { - case Float: - return newFloatIterators(a) - case Integer: - return newIntegerIterators(a) - case String: - return newStringIterators(a) - case Boolean: - return newBooleanIterators(a) - } - return a -} - -// Merge combines all iterators into a single iterator. -// A sorted merge iterator or a merge iterator can be used based on opt. -func (a Iterators) Merge(opt IteratorOptions) (Iterator, error) { - // Merge into a single iterator. - if opt.MergeSorted() { - itr := NewSortedMergeIterator(a, opt) - if itr != nil && opt.InterruptCh != nil { - itr = NewInterruptIterator(itr, opt.InterruptCh) - } - return itr, nil - } - - itr := NewMergeIterator(a, opt) - if itr == nil { - return nil, nil - } - - if opt.Expr != nil { - if expr, ok := opt.Expr.(*Call); ok && expr.Name == "count" { - opt.Expr = &Call{ - Name: "sum", - Args: expr.Args, - } - } - } - - if opt.InterruptCh != nil { - itr = NewInterruptIterator(itr, opt.InterruptCh) - } - return NewCallIterator(itr, opt) -} - -// NewMergeIterator returns an iterator to merge itrs into one. -// Inputs must either be merge iterators or only contain a single name/tag in -// sorted order. The iterator will output all points by window, name/tag, then -// time. This iterator is useful when you need all of the points for an -// interval. -func NewMergeIterator(inputs []Iterator, opt IteratorOptions) Iterator { - inputs = Iterators(inputs).filterNonNil() - if n := len(inputs); n == 0 { - return nil - } else if n == 1 { - return inputs[0] - } - - // Aggregate functions can use a more relaxed sorting so that points - // within a window are grouped. This is much more efficient. - switch inputs := Iterators(inputs).cast().(type) { - case []FloatIterator: - return newFloatMergeIterator(inputs, opt) - case []IntegerIterator: - return newIntegerMergeIterator(inputs, opt) - case []StringIterator: - return newStringMergeIterator(inputs, opt) - case []BooleanIterator: - return newBooleanMergeIterator(inputs, opt) - default: - panic(fmt.Sprintf("unsupported merge iterator type: %T", inputs)) - } -} - -// NewParallelMergeIterator returns an iterator that breaks input iterators -// into groups and processes them in parallel. -func NewParallelMergeIterator(inputs []Iterator, opt IteratorOptions, parallelism int) Iterator { - inputs = Iterators(inputs).filterNonNil() - if len(inputs) == 0 { - return nil - } else if len(inputs) == 1 { - return inputs[0] - } - - // Limit parallelism to the number of inputs. - if len(inputs) < parallelism { - parallelism = len(inputs) - } - - // Determine the number of inputs per output iterator. - n := len(inputs) / parallelism - - // Group iterators together. - outputs := make([]Iterator, parallelism) - for i := range outputs { - var slice []Iterator - if i < len(outputs)-1 { - slice = inputs[i*n : (i+1)*n] - } else { - slice = inputs[i*n:] - } - - outputs[i] = newParallelIterator(NewMergeIterator(slice, opt)) - } - - // Merge all groups together. - return NewMergeIterator(outputs, opt) -} - -// NewSortedMergeIterator returns an iterator to merge itrs into one. -// Inputs must either be sorted merge iterators or only contain a single -// name/tag in sorted order. The iterator will output all points by name/tag, -// then time. This iterator is useful when you need all points for a name/tag -// to be in order. -func NewSortedMergeIterator(inputs []Iterator, opt IteratorOptions) Iterator { - inputs = Iterators(inputs).filterNonNil() - if len(inputs) == 0 { - return nil - } - - switch inputs := Iterators(inputs).cast().(type) { - case []FloatIterator: - return newFloatSortedMergeIterator(inputs, opt) - case []IntegerIterator: - return newIntegerSortedMergeIterator(inputs, opt) - case []StringIterator: - return newStringSortedMergeIterator(inputs, opt) - case []BooleanIterator: - return newBooleanSortedMergeIterator(inputs, opt) - default: - panic(fmt.Sprintf("unsupported sorted merge iterator type: %T", inputs)) - } -} - -// newParallelIterator returns an iterator that runs in a separate goroutine. -func newParallelIterator(input Iterator) Iterator { - if input == nil { - return nil - } - - switch itr := input.(type) { - case FloatIterator: - return newFloatParallelIterator(itr) - case IntegerIterator: - return newIntegerParallelIterator(itr) - case StringIterator: - return newStringParallelIterator(itr) - case BooleanIterator: - return newBooleanParallelIterator(itr) - default: - panic(fmt.Sprintf("unsupported parallel iterator type: %T", itr)) - } -} - -// NewLimitIterator returns an iterator that limits the number of points per grouping. -func NewLimitIterator(input Iterator, opt IteratorOptions) Iterator { - switch input := input.(type) { - case FloatIterator: - return newFloatLimitIterator(input, opt) - case IntegerIterator: - return newIntegerLimitIterator(input, opt) - case StringIterator: - return newStringLimitIterator(input, opt) - case BooleanIterator: - return newBooleanLimitIterator(input, opt) - default: - panic(fmt.Sprintf("unsupported limit iterator type: %T", input)) - } -} - -// NewDedupeIterator returns an iterator that only outputs unique points. -// This iterator maintains a serialized copy of each row so it is inefficient -// to use on large datasets. It is intended for small datasets such as meta queries. -func NewDedupeIterator(input Iterator) Iterator { - if input == nil { - return nil - } - - switch input := input.(type) { - case FloatIterator: - return newFloatDedupeIterator(input) - case IntegerIterator: - return newIntegerDedupeIterator(input) - case StringIterator: - return newStringDedupeIterator(input) - case BooleanIterator: - return newBooleanDedupeIterator(input) - default: - panic(fmt.Sprintf("unsupported dedupe iterator type: %T", input)) - } -} - -// NewFillIterator returns an iterator that fills in missing points in an aggregate. -func NewFillIterator(input Iterator, expr Expr, opt IteratorOptions) Iterator { - switch input := input.(type) { - case FloatIterator: - return newFloatFillIterator(input, expr, opt) - case IntegerIterator: - return newIntegerFillIterator(input, expr, opt) - case StringIterator: - return newStringFillIterator(input, expr, opt) - case BooleanIterator: - return newBooleanFillIterator(input, expr, opt) - default: - panic(fmt.Sprintf("unsupported fill iterator type: %T", input)) - } -} - -// NewIntervalIterator returns an iterator that sets the time on each point to the interval. -func NewIntervalIterator(input Iterator, opt IteratorOptions) Iterator { - switch input := input.(type) { - case FloatIterator: - return newFloatIntervalIterator(input, opt) - case IntegerIterator: - return newIntegerIntervalIterator(input, opt) - case StringIterator: - return newStringIntervalIterator(input, opt) - case BooleanIterator: - return newBooleanIntervalIterator(input, opt) - default: - panic(fmt.Sprintf("unsupported fill iterator type: %T", input)) - } -} - -// NewInterruptIterator returns an iterator that will stop producing output when a channel -// has been closed on the passed in channel. -func NewInterruptIterator(input Iterator, closing <-chan struct{}) Iterator { - switch input := input.(type) { - case FloatIterator: - return newFloatInterruptIterator(input, closing) - case IntegerIterator: - return newIntegerInterruptIterator(input, closing) - case StringIterator: - return newStringInterruptIterator(input, closing) - case BooleanIterator: - return newBooleanInterruptIterator(input, closing) - default: - panic(fmt.Sprintf("unsupported interrupt iterator type: %T", input)) - } -} - -// NewCloseInterruptIterator returns an iterator that will invoke the Close() method on an -// iterator when a channel has been closed. -func NewCloseInterruptIterator(input Iterator, closing <-chan struct{}) Iterator { - switch input := input.(type) { - case FloatIterator: - return newFloatCloseInterruptIterator(input, closing) - case IntegerIterator: - return newIntegerCloseInterruptIterator(input, closing) - case StringIterator: - return newStringCloseInterruptIterator(input, closing) - case BooleanIterator: - return newBooleanCloseInterruptIterator(input, closing) - default: - panic(fmt.Sprintf("unsupported close iterator iterator type: %T", input)) - } -} - -// AuxIterator represents an iterator that can split off separate auxilary iterators. -type AuxIterator interface { - Iterator - IteratorCreator - - // Auxilary iterator - Iterator(name string, typ DataType) Iterator - - // Start starts writing to the created iterators. - Start() - - // Backgrounds the iterator so that, when start is called, it will - // continuously read from the iterator. - Background() -} - -// NewAuxIterator returns a new instance of AuxIterator. -func NewAuxIterator(input Iterator, opt IteratorOptions) AuxIterator { - switch input := input.(type) { - case FloatIterator: - return newFloatAuxIterator(input, opt) - case IntegerIterator: - return newIntegerAuxIterator(input, opt) - case StringIterator: - return newStringAuxIterator(input, opt) - case BooleanIterator: - return newBooleanAuxIterator(input, opt) - default: - panic(fmt.Sprintf("unsupported aux iterator type: %T", input)) - } -} - -// auxIteratorField represents an auxilary field within an AuxIterator. -type auxIteratorField struct { - name string // field name - typ DataType // detected data type - itrs []Iterator // auxillary iterators - mu sync.Mutex - opt IteratorOptions -} - -func (f *auxIteratorField) append(itr Iterator) { - f.mu.Lock() - defer f.mu.Unlock() - f.itrs = append(f.itrs, itr) -} - -func (f *auxIteratorField) close() { - f.mu.Lock() - defer f.mu.Unlock() - for _, itr := range f.itrs { - itr.Close() - } -} - -type auxIteratorFields []*auxIteratorField - -// newAuxIteratorFields returns a new instance of auxIteratorFields from a list of field names. -func newAuxIteratorFields(opt IteratorOptions) auxIteratorFields { - fields := make(auxIteratorFields, len(opt.Aux)) - for i, ref := range opt.Aux { - fields[i] = &auxIteratorField{name: ref.Val, typ: ref.Type, opt: opt} - } - return fields -} - -func (a auxIteratorFields) close() { - for _, f := range a { - f.close() - } -} - -// iterator creates a new iterator for a named auxilary field. -func (a auxIteratorFields) iterator(name string, typ DataType) Iterator { - for _, f := range a { - // Skip field if it's name doesn't match. - // Exit if no points were received by the iterator. - if f.name != name || (typ != Unknown && f.typ != typ) { - continue - } - - // Create channel iterator by data type. - switch f.typ { - case Float: - itr := &floatChanIterator{cond: sync.NewCond(&sync.Mutex{})} - f.append(itr) - return itr - case Integer: - itr := &integerChanIterator{cond: sync.NewCond(&sync.Mutex{})} - f.append(itr) - return itr - case String, Tag: - itr := &stringChanIterator{cond: sync.NewCond(&sync.Mutex{})} - f.append(itr) - return itr - case Boolean: - itr := &booleanChanIterator{cond: sync.NewCond(&sync.Mutex{})} - f.append(itr) - return itr - default: - break - } - } - - return &nilFloatIterator{} -} - -// send sends a point to all field iterators. -func (a auxIteratorFields) send(p Point) (ok bool) { - values := p.aux() - for i, f := range a { - v := values[i] - - tags := p.tags() - tags = tags.Subset(f.opt.Dimensions) - - // Send new point for each aux iterator. - // Primitive pointers represent nil values. - for _, itr := range f.itrs { - switch itr := itr.(type) { - case *floatChanIterator: - ok = itr.setBuf(p.name(), tags, p.time(), v) || ok - case *integerChanIterator: - ok = itr.setBuf(p.name(), tags, p.time(), v) || ok - case *stringChanIterator: - ok = itr.setBuf(p.name(), tags, p.time(), v) || ok - case *booleanChanIterator: - ok = itr.setBuf(p.name(), tags, p.time(), v) || ok - default: - panic(fmt.Sprintf("invalid aux itr type: %T", itr)) - } - } - } - return ok -} - -func (a auxIteratorFields) sendError(err error) { - for _, f := range a { - for _, itr := range f.itrs { - switch itr := itr.(type) { - case *floatChanIterator: - itr.setErr(err) - case *integerChanIterator: - itr.setErr(err) - case *stringChanIterator: - itr.setErr(err) - case *booleanChanIterator: - itr.setErr(err) - default: - panic(fmt.Sprintf("invalid aux itr type: %T", itr)) - } - } - } -} - -// DrainIterator reads all points from an iterator. -func DrainIterator(itr Iterator) { - defer itr.Close() - switch itr := itr.(type) { - case FloatIterator: - for p, _ := itr.Next(); p != nil; p, _ = itr.Next() { - } - case IntegerIterator: - for p, _ := itr.Next(); p != nil; p, _ = itr.Next() { - } - case StringIterator: - for p, _ := itr.Next(); p != nil; p, _ = itr.Next() { - } - case BooleanIterator: - for p, _ := itr.Next(); p != nil; p, _ = itr.Next() { - } - default: - panic(fmt.Sprintf("unsupported iterator type for draining: %T", itr)) - } -} - -// DrainIterators reads all points from all iterators. -func DrainIterators(itrs []Iterator) { - defer Iterators(itrs).Close() - for { - var hasData bool - - for _, itr := range itrs { - switch itr := itr.(type) { - case FloatIterator: - if p, _ := itr.Next(); p != nil { - hasData = true - } - case IntegerIterator: - if p, _ := itr.Next(); p != nil { - hasData = true - } - case StringIterator: - if p, _ := itr.Next(); p != nil { - hasData = true - } - case BooleanIterator: - if p, _ := itr.Next(); p != nil { - hasData = true - } - default: - panic(fmt.Sprintf("unsupported iterator type for draining: %T", itr)) - } - } - - // Exit once all iterators return a nil point. - if !hasData { - break - } - } -} - -// NewReaderIterator returns an iterator that streams from a reader. -func NewReaderIterator(r io.Reader, typ DataType, stats IteratorStats) Iterator { - switch typ { - case Float: - return newFloatReaderIterator(r, stats) - case Integer: - return newIntegerReaderIterator(r, stats) - case String: - return newStringReaderIterator(r, stats) - case Boolean: - return newBooleanReaderIterator(r, stats) - default: - return &nilFloatIterator{} - } -} - -// IteratorCreator represents an interface for objects that can create Iterators. -type IteratorCreator interface { - // Creates a simple iterator for use in an InfluxQL query. - CreateIterator(opt IteratorOptions) (Iterator, error) - - // Returns the unique fields and dimensions across a list of sources. - FieldDimensions(sources Sources) (fields map[string]DataType, dimensions map[string]struct{}, err error) - - // Expands regex sources to all matching sources. - ExpandSources(sources Sources) (Sources, error) -} - -// IteratorCreators represents a list of iterator creators. -type IteratorCreators []IteratorCreator - -// Close closes all iterator creators that implement io.Closer. -func (a IteratorCreators) Close() error { - for _, ic := range a { - if ic, ok := ic.(io.Closer); ok { - ic.Close() - } - } - return nil -} - -// CreateIterator returns a single combined iterator from multiple iterator creators. -func (a IteratorCreators) CreateIterator(opt IteratorOptions) (Iterator, error) { - // Create iterators for each shard. - // Ensure that they are closed if an error occurs. - itrs := make([]Iterator, 0, len(a)) - if err := func() error { - for _, ic := range a { - itr, err := ic.CreateIterator(opt) - if err != nil { - return err - } else if itr == nil { - continue - } - itrs = append(itrs, itr) - } - return nil - }(); err != nil { - Iterators(itrs).Close() - return nil, err - } - - if len(itrs) == 0 { - return nil, nil - } - - return Iterators(itrs).Merge(opt) -} - -// FieldDimensions returns unique fields and dimensions from multiple iterator creators. -func (a IteratorCreators) FieldDimensions(sources Sources) (fields map[string]DataType, dimensions map[string]struct{}, err error) { - fields = make(map[string]DataType) - dimensions = make(map[string]struct{}) - - for _, ic := range a { - f, d, err := ic.FieldDimensions(sources) - if err != nil { - return nil, nil, err - } - for k, typ := range f { - if _, ok := fields[k]; typ != Unknown && (!ok || typ < fields[k]) { - fields[k] = typ - } - } - for k := range d { - dimensions[k] = struct{}{} - } - } - return -} - -// ExpandSources expands sources across all iterator creators and returns a unique result. -func (a IteratorCreators) ExpandSources(sources Sources) (Sources, error) { - m := make(map[string]Source) - - for _, ic := range a { - expanded, err := ic.ExpandSources(sources) - if err != nil { - return nil, err - } - - for _, src := range expanded { - switch src := src.(type) { - case *Measurement: - m[src.String()] = src - default: - return nil, fmt.Errorf("IteratorCreators.ExpandSources: unsupported source type: %T", src) - } - } - } - - // Convert set to sorted slice. - names := make([]string, 0, len(m)) - for name := range m { - names = append(names, name) - } - sort.Strings(names) - - // Convert set to a list of Sources. - sorted := make(Sources, 0, len(m)) - for _, name := range names { - sorted = append(sorted, m[name]) - } - - return sorted, nil -} - -// IteratorOptions is an object passed to CreateIterator to specify creation options. -type IteratorOptions struct { - // Expression to iterate for. - // This can be VarRef or a Call. - Expr Expr - - // Auxilary tags or values to also retrieve for the point. - Aux []VarRef - - // Data sources from which to retrieve data. - Sources []Source - - // Group by interval and tags. - Interval Interval - Dimensions []string - - // Fill options. - Fill FillOption - FillValue interface{} - - // Condition to filter by. - Condition Expr - - // Time range for the iterator. - StartTime int64 - EndTime int64 - - // Sorted in time ascending order if true. - Ascending bool - - // Limits the number of points per series. - Limit, Offset int - - // Limits the number of series. - SLimit, SOffset int - - // Removes duplicate rows from raw queries. - Dedupe bool - - // If this channel is set and is closed, the iterator should try to exit - // and close as soon as possible. - InterruptCh <-chan struct{} -} - -// newIteratorOptionsStmt creates the iterator options from stmt. -func newIteratorOptionsStmt(stmt *SelectStatement, sopt *SelectOptions) (opt IteratorOptions, err error) { - // Determine time range from the condition. - startTime, endTime, err := TimeRange(stmt.Condition) - if err != nil { - return IteratorOptions{}, err - } - - if !startTime.IsZero() { - opt.StartTime = startTime.UnixNano() - } else { - if sopt != nil { - opt.StartTime = sopt.MinTime.UnixNano() - } else { - opt.StartTime = MinTime - } - } - if !endTime.IsZero() { - opt.EndTime = endTime.UnixNano() - } else { - if sopt != nil { - opt.EndTime = sopt.MaxTime.UnixNano() - } else { - opt.EndTime = MaxTime - } - } - - // Determine group by interval. - interval, err := stmt.GroupByInterval() - if err != nil { - return opt, err - } - // Set duration to zero if a negative interval has been used. - if interval < 0 { - interval = 0 - } else if interval > 0 { - opt.Interval.Offset, err = stmt.GroupByOffset() - if err != nil { - return opt, err - } - } - opt.Interval.Duration = interval - - // Determine dimensions. - for _, d := range stmt.Dimensions { - if d, ok := d.Expr.(*VarRef); ok { - opt.Dimensions = append(opt.Dimensions, d.Val) - } - } - - opt.Sources = stmt.Sources - opt.Condition = stmt.Condition - opt.Ascending = stmt.TimeAscending() - opt.Dedupe = stmt.Dedupe - - opt.Fill, opt.FillValue = stmt.Fill, stmt.FillValue - if opt.Fill == NullFill && stmt.Target != nil { - // Set the fill option to none if a target has been given. - // Null values will get ignored when being written to the target - // so fill(null) wouldn't write any null values to begin with. - opt.Fill = NoFill - } - opt.Limit, opt.Offset = stmt.Limit, stmt.Offset - opt.SLimit, opt.SOffset = stmt.SLimit, stmt.SOffset - if sopt != nil { - opt.InterruptCh = sopt.InterruptCh - } - - return opt, nil -} - -// MergeSorted returns true if the options require a sorted merge. -// This is only needed when the expression is a variable reference or there is no expr. -func (opt IteratorOptions) MergeSorted() bool { - if opt.Expr == nil { - return true - } - _, ok := opt.Expr.(*VarRef) - return ok -} - -// SeekTime returns the time the iterator should start from. -// For ascending iterators this is the start time, for descending iterators it's the end time. -func (opt IteratorOptions) SeekTime() int64 { - if opt.Ascending { - return opt.StartTime - } - return opt.EndTime -} - -// Window returns the time window [start,end) that t falls within. -func (opt IteratorOptions) Window(t int64) (start, end int64) { - if opt.Interval.IsZero() { - return opt.StartTime, opt.EndTime + 1 - } - - // Subtract the offset to the time so we calculate the correct base interval. - t -= int64(opt.Interval.Offset) - - // Truncate time by duration. - dt := t % int64(opt.Interval.Duration) - if dt < 0 { - // Negative modulo rounds up instead of down, so offset - // with the duration. - dt += int64(opt.Interval.Duration) - } - t -= dt - - // Apply the offset. - start = t + int64(opt.Interval.Offset) - end = start + int64(opt.Interval.Duration) - return -} - -// DerivativeInterval returns the time interval for the derivative function. -func (opt IteratorOptions) DerivativeInterval() Interval { - // Use the interval on the derivative() call, if specified. - if expr, ok := opt.Expr.(*Call); ok && len(expr.Args) == 2 { - return Interval{Duration: expr.Args[1].(*DurationLiteral).Val} - } - - // Otherwise use the group by interval, if specified. - if opt.Interval.Duration > 0 { - return Interval{Duration: opt.Interval.Duration} - } - - return Interval{Duration: time.Second} -} - -// ElapsedInterval returns the time interval for the elapsed function. -func (opt IteratorOptions) ElapsedInterval() Interval { - // Use the interval on the elapsed() call, if specified. - if expr, ok := opt.Expr.(*Call); ok && len(expr.Args) == 2 { - return Interval{Duration: expr.Args[1].(*DurationLiteral).Val} - } - - return Interval{Duration: time.Nanosecond} -} - -// MarshalBinary encodes opt into a binary format. -func (opt *IteratorOptions) MarshalBinary() ([]byte, error) { - return proto.Marshal(encodeIteratorOptions(opt)) -} - -// UnmarshalBinary decodes from a binary format in to opt. -func (opt *IteratorOptions) UnmarshalBinary(buf []byte) error { - var pb internal.IteratorOptions - if err := proto.Unmarshal(buf, &pb); err != nil { - return err - } - - other, err := decodeIteratorOptions(&pb) - if err != nil { - return err - } - *opt = *other - - return nil -} - -func encodeIteratorOptions(opt *IteratorOptions) *internal.IteratorOptions { - pb := &internal.IteratorOptions{ - Interval: encodeInterval(opt.Interval), - Dimensions: opt.Dimensions, - Fill: proto.Int32(int32(opt.Fill)), - StartTime: proto.Int64(opt.StartTime), - EndTime: proto.Int64(opt.EndTime), - Ascending: proto.Bool(opt.Ascending), - Limit: proto.Int64(int64(opt.Limit)), - Offset: proto.Int64(int64(opt.Offset)), - SLimit: proto.Int64(int64(opt.SLimit)), - SOffset: proto.Int64(int64(opt.SOffset)), - Dedupe: proto.Bool(opt.Dedupe), - } - - // Set expression, if set. - if opt.Expr != nil { - pb.Expr = proto.String(opt.Expr.String()) - } - - // Convert and encode aux fields as variable references. - pb.Fields = make([]*internal.VarRef, len(opt.Aux)) - pb.Aux = make([]string, len(opt.Aux)) - for i, ref := range opt.Aux { - pb.Fields[i] = encodeVarRef(ref) - pb.Aux[i] = ref.Val - } - - // Convert and encode sources to measurements. - sources := make([]*internal.Measurement, len(opt.Sources)) - for i, source := range opt.Sources { - mm := source.(*Measurement) - sources[i] = encodeMeasurement(mm) - } - pb.Sources = sources - - // Fill value can only be a number. Set it if available. - if v, ok := opt.FillValue.(float64); ok { - pb.FillValue = proto.Float64(v) - } - - // Set condition, if set. - if opt.Condition != nil { - pb.Condition = proto.String(opt.Condition.String()) - } - - return pb -} - -func decodeIteratorOptions(pb *internal.IteratorOptions) (*IteratorOptions, error) { - opt := &IteratorOptions{ - Interval: decodeInterval(pb.GetInterval()), - Dimensions: pb.GetDimensions(), - Fill: FillOption(pb.GetFill()), - FillValue: pb.GetFillValue(), - StartTime: pb.GetStartTime(), - EndTime: pb.GetEndTime(), - Ascending: pb.GetAscending(), - Limit: int(pb.GetLimit()), - Offset: int(pb.GetOffset()), - SLimit: int(pb.GetSLimit()), - SOffset: int(pb.GetSOffset()), - Dedupe: pb.GetDedupe(), - } - - // Set expression, if set. - if pb.Expr != nil { - expr, err := ParseExpr(pb.GetExpr()) - if err != nil { - return nil, err - } - opt.Expr = expr - } - - // Convert and decode variable references. - if fields := pb.GetFields(); fields != nil { - opt.Aux = make([]VarRef, len(fields)) - for i, ref := range fields { - opt.Aux[i] = decodeVarRef(ref) - } - } else { - opt.Aux = make([]VarRef, len(pb.GetAux())) - for i, name := range pb.GetAux() { - opt.Aux[i] = VarRef{Val: name} - } - } - - // Convert and dencode sources to measurements. - sources := make([]Source, len(pb.GetSources())) - for i, source := range pb.GetSources() { - mm, err := decodeMeasurement(source) - if err != nil { - return nil, err - } - sources[i] = mm - } - opt.Sources = sources - - // Set condition, if set. - if pb.Condition != nil { - expr, err := ParseExpr(pb.GetCondition()) - if err != nil { - return nil, err - } - opt.Condition = expr - } - - return opt, nil -} - -// selectInfo represents an object that stores info about select fields. -type selectInfo struct { - calls map[*Call]struct{} - refs map[*VarRef]struct{} -} - -// newSelectInfo creates a object with call and var ref info from stmt. -func newSelectInfo(stmt *SelectStatement) *selectInfo { - info := &selectInfo{ - calls: make(map[*Call]struct{}), - refs: make(map[*VarRef]struct{}), - } - Walk(info, stmt.Fields) - return info -} - -func (v *selectInfo) Visit(n Node) Visitor { - switch n := n.(type) { - case *Call: - v.calls[n] = struct{}{} - return nil - case *VarRef: - v.refs[n] = struct{}{} - return nil - } - return v -} - -// Interval represents a repeating interval for a query. -type Interval struct { - Duration time.Duration - Offset time.Duration -} - -// IsZero returns true if the interval has no duration. -func (i Interval) IsZero() bool { return i.Duration == 0 } - -func encodeInterval(i Interval) *internal.Interval { - return &internal.Interval{ - Duration: proto.Int64(i.Duration.Nanoseconds()), - Offset: proto.Int64(i.Offset.Nanoseconds()), - } -} - -func decodeInterval(pb *internal.Interval) Interval { - return Interval{ - Duration: time.Duration(pb.GetDuration()), - Offset: time.Duration(pb.GetOffset()), - } -} - -func encodeVarRef(ref VarRef) *internal.VarRef { - return &internal.VarRef{ - Val: proto.String(ref.Val), - Type: proto.Int32(int32(ref.Type)), - } -} - -func decodeVarRef(pb *internal.VarRef) VarRef { - return VarRef{ - Val: pb.GetVal(), - Type: DataType(pb.GetType()), - } -} - -type nilFloatIterator struct{} - -func (*nilFloatIterator) Stats() IteratorStats { return IteratorStats{} } -func (*nilFloatIterator) Close() error { return nil } -func (*nilFloatIterator) Next() (*FloatPoint, error) { return nil, nil } - -// integerFloatTransformIterator executes a function to modify an existing point for every -// output of the input iterator. -type integerFloatTransformIterator struct { - input IntegerIterator - fn integerFloatTransformFunc -} - -// Stats returns stats from the input iterator. -func (itr *integerFloatTransformIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *integerFloatTransformIterator) Close() error { return itr.input.Close() } - -// Next returns the minimum value for the next available interval. -func (itr *integerFloatTransformIterator) Next() (*FloatPoint, error) { - p, err := itr.input.Next() - if err != nil { - return nil, err - } else if p != nil { - return itr.fn(p), nil - } - return nil, nil -} - -// integerFloatTransformFunc creates or modifies a point. -// The point passed in may be modified and returned rather than allocating a -// new point if possible. -type integerFloatTransformFunc func(p *IntegerPoint) *FloatPoint - -type integerFloatCastIterator struct { - input IntegerIterator -} - -func (itr *integerFloatCastIterator) Stats() IteratorStats { return itr.input.Stats() } -func (itr *integerFloatCastIterator) Close() error { return itr.input.Close() } -func (itr *integerFloatCastIterator) Next() (*FloatPoint, error) { - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } - - return &FloatPoint{ - Name: p.Name, - Tags: p.Tags, - Time: p.Time, - Nil: p.Nil, - Value: float64(p.Value), - Aux: p.Aux, - }, nil -} - -// IteratorStats represents statistics about an iterator. -// Some statistics are available immediately upon iterator creation while -// some are derived as the iterator processes data. -type IteratorStats struct { - SeriesN int // series represented - PointN int // points returned -} - -// Add aggregates fields from s and other together. Overwrites s. -func (s *IteratorStats) Add(other IteratorStats) { - s.SeriesN += other.SeriesN - s.PointN += other.PointN -} - -func encodeIteratorStats(stats *IteratorStats) *internal.IteratorStats { - return &internal.IteratorStats{ - SeriesN: proto.Int64(int64(stats.SeriesN)), - PointN: proto.Int64(int64(stats.PointN)), - } -} - -func decodeIteratorStats(pb *internal.IteratorStats) IteratorStats { - return IteratorStats{ - SeriesN: int(pb.GetSeriesN()), - PointN: int(pb.GetPointN()), - } -} - -// floatFastDedupeIterator outputs unique points where the point has a single aux field. -type floatFastDedupeIterator struct { - input FloatIterator - m map[fastDedupeKey]struct{} // lookup of points already sent -} - -// newFloatFastDedupeIterator returns a new instance of floatFastDedupeIterator. -func newFloatFastDedupeIterator(input FloatIterator) *floatFastDedupeIterator { - return &floatFastDedupeIterator{ - input: input, - m: make(map[fastDedupeKey]struct{}), - } -} - -// Stats returns stats from the input iterator. -func (itr *floatFastDedupeIterator) Stats() IteratorStats { return itr.input.Stats() } - -// Close closes the iterator and all child iterators. -func (itr *floatFastDedupeIterator) Close() error { return itr.input.Close() } - -// Next returns the next unique point from the input iterator. -func (itr *floatFastDedupeIterator) Next() (*FloatPoint, error) { - for { - // Read next point. - // Skip if there are not any aux fields. - p, err := itr.input.Next() - if p == nil || err != nil { - return nil, err - } else if len(p.Aux) == 0 { - continue - } - - // If the point has already been output then move to the next point. - key := fastDedupeKey{name: p.Name} - key.values[0] = p.Aux[0] - if len(p.Aux) > 1 { - key.values[1] = p.Aux[1] - } - if _, ok := itr.m[key]; ok { - continue - } - - // Otherwise mark it as emitted and return point. - itr.m[key] = struct{}{} - return p, nil - } -} - -type fastDedupeKey struct { - name string - values [2]interface{} -} - -type reverseStringSlice []string - -func (p reverseStringSlice) Len() int { return len(p) } -func (p reverseStringSlice) Less(i, j int) bool { return p[i] > p[j] } -func (p reverseStringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } diff --git a/vendor/github.com/influxdata/influxdb/influxql/iterator_test.go b/vendor/github.com/influxdata/influxdb/influxql/iterator_test.go deleted file mode 100644 index 910efbc0..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/iterator_test.go +++ /dev/null @@ -1,1213 +0,0 @@ -package influxql_test - -import ( - "bytes" - "fmt" - "math" - "math/rand" - "reflect" - "regexp" - "testing" - "time" - - "github.com/davecgh/go-spew/spew" - "github.com/influxdata/influxdb/influxql" - "github.com/influxdata/influxdb/pkg/deep" -) - -// Ensure that a set of iterators can be merged together, sorted by window and name/tag. -func TestMergeIterator_Float(t *testing.T) { - inputs := []*FloatIterator{ - {Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: 8}, - }}, - {Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}, - }}, - {Points: []influxql.FloatPoint{}}, - {Points: []influxql.FloatPoint{}}, - } - - itr := influxql.NewMergeIterator(FloatIterators(inputs), influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}}, - {&influxql.FloatPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}}, - {&influxql.FloatPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: 8}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } -} - -// Ensure that a set of iterators can be merged together, sorted by window and name/tag. -func TestMergeIterator_Integer(t *testing.T) { - inputs := []*IntegerIterator{ - {Points: []influxql.IntegerPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: 8}, - }}, - {Points: []influxql.IntegerPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}, - }}, - {Points: []influxql.IntegerPoint{}}, - } - itr := influxql.NewMergeIterator(IntegerIterators(inputs), influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}}, - {&influxql.IntegerPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}}, - {&influxql.IntegerPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: 8}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } -} - -// Ensure that a set of iterators can be merged together, sorted by window and name/tag. -func TestMergeIterator_String(t *testing.T) { - inputs := []*StringIterator{ - {Points: []influxql.StringPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: "a"}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: "c"}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: "d"}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: "b"}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: "h"}, - }}, - {Points: []influxql.StringPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: "g"}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: "e"}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: "f"}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: "i"}, - }}, - {Points: []influxql.StringPoint{}}, - } - itr := influxql.NewMergeIterator(StringIterators(inputs), influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: "a"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: "c"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: "g"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: "d"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: "b"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: "e"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: "f"}}, - {&influxql.StringPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: "i"}}, - {&influxql.StringPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: "h"}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } -} - -// Ensure that a set of iterators can be merged together, sorted by window and name/tag. -func TestMergeIterator_Boolean(t *testing.T) { - inputs := []*BooleanIterator{ - {Points: []influxql.BooleanPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: true}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: true}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: false}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: false}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: true}, - }}, - {Points: []influxql.BooleanPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: true}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: true}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: false}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: false}, - }}, - {Points: []influxql.BooleanPoint{}}, - } - itr := influxql.NewMergeIterator(BooleanIterators(inputs), influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: true}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: true}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: true}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: false}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: false}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: true}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: false}}, - {&influxql.BooleanPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: false}}, - {&influxql.BooleanPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: true}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } -} - -func TestMergeIterator_Nil(t *testing.T) { - itr := influxql.NewMergeIterator([]influxql.Iterator{nil}, influxql.IteratorOptions{}) - if itr != nil { - t.Fatalf("unexpected iterator: %#v", itr) - } -} - -func TestMergeIterator_Cast_Float(t *testing.T) { - inputs := []influxql.Iterator{ - &IntegerIterator{Points: []influxql.IntegerPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: 8}, - }}, - &FloatIterator{Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}, - }}, - } - - itr := influxql.NewMergeIterator(inputs, influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}}, - {&influxql.FloatPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}}, - {&influxql.FloatPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 11, Value: 8}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - switch input := input.(type) { - case *FloatIterator: - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - case *IntegerIterator: - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } - } -} - -// Ensure that a set of iterators can be merged together, sorted by name/tag. -func TestSortedMergeIterator_Float(t *testing.T) { - inputs := []*FloatIterator{ - {Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: 8}, - }}, - {Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}, - }}, - {Points: []influxql.FloatPoint{}}, - } - itr := influxql.NewSortedMergeIterator(FloatIterators(inputs), influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}}, - {&influxql.FloatPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}}, - {&influxql.FloatPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: 8}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } -} - -// Ensure that a set of iterators can be merged together, sorted by name/tag. -func TestSortedMergeIterator_Integer(t *testing.T) { - inputs := []*IntegerIterator{ - {Points: []influxql.IntegerPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: 8}, - }}, - {Points: []influxql.IntegerPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}, - }}, - {Points: []influxql.IntegerPoint{}}, - } - itr := influxql.NewSortedMergeIterator(IntegerIterators(inputs), influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}}, - {&influxql.IntegerPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}}, - {&influxql.IntegerPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}}, - {&influxql.IntegerPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: 8}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } -} - -// Ensure that a set of iterators can be merged together, sorted by name/tag. -func TestSortedMergeIterator_String(t *testing.T) { - inputs := []*StringIterator{ - {Points: []influxql.StringPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: "a"}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: "c"}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: "d"}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: "b"}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: "h"}, - }}, - {Points: []influxql.StringPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: "g"}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: "e"}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: "f"}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: "i"}, - }}, - {Points: []influxql.StringPoint{}}, - } - itr := influxql.NewSortedMergeIterator(StringIterators(inputs), influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: "a"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: "c"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: "g"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: "d"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: "b"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: "e"}}, - {&influxql.StringPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: "f"}}, - {&influxql.StringPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: "i"}}, - {&influxql.StringPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: "h"}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } -} - -// Ensure that a set of iterators can be merged together, sorted by name/tag. -func TestSortedMergeIterator_Boolean(t *testing.T) { - inputs := []*BooleanIterator{ - {Points: []influxql.BooleanPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: true}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: true}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: false}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: false}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: true}, - }}, - {Points: []influxql.BooleanPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: true}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: true}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: false}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: true}, - }}, - {Points: []influxql.BooleanPoint{}}, - } - itr := influxql.NewSortedMergeIterator(BooleanIterators(inputs), influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: true}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: true}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: true}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: false}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: false}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: true}}, - {&influxql.BooleanPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: false}}, - {&influxql.BooleanPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: true}}, - {&influxql.BooleanPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: true}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } -} - -func TestSortedMergeIterator_Nil(t *testing.T) { - itr := influxql.NewSortedMergeIterator([]influxql.Iterator{nil}, influxql.IteratorOptions{}) - if itr != nil { - t.Fatalf("unexpected iterator: %#v", itr) - } -} - -func TestSortedMergeIterator_Cast_Float(t *testing.T) { - inputs := []influxql.Iterator{ - &IntegerIterator{Points: []influxql.IntegerPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}, - {Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: 8}, - }}, - &FloatIterator{Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}, - {Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}, - {Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}, - }}, - } - - itr := influxql.NewSortedMergeIterator(inputs, influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10 * time.Nanosecond, - }, - Ascending: true, - }) - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 1}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 12, Value: 3}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 20, Value: 7}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 30, Value: 4}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 1, Value: 2}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 11, Value: 5}}, - {&influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=B"), Time: 13, Value: 6}}, - {&influxql.FloatPoint{Name: "mem", Tags: ParseTags("host=A"), Time: 25, Value: 9}}, - {&influxql.FloatPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 4, Value: 8}}, - }) { - t.Errorf("unexpected points: %s", spew.Sdump(a)) - } - - for i, input := range inputs { - switch input := input.(type) { - case *FloatIterator: - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - case *IntegerIterator: - if !input.Closed { - t.Errorf("iterator %d not closed", i) - } - } - } -} - -// Ensure limit iterators work with limit and offset. -func TestLimitIterator_Float(t *testing.T) { - input := &FloatIterator{Points: []influxql.FloatPoint{ - {Name: "cpu", Time: 0, Value: 1}, - {Name: "cpu", Time: 5, Value: 3}, - {Name: "cpu", Time: 10, Value: 5}, - {Name: "mem", Time: 5, Value: 3}, - {Name: "mem", Time: 7, Value: 8}, - }} - - itr := influxql.NewLimitIterator(input, influxql.IteratorOptions{ - Limit: 1, - Offset: 1, - }) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Name: "cpu", Time: 5, Value: 3}}, - {&influxql.FloatPoint{Name: "mem", Time: 7, Value: 8}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } - - if !input.Closed { - t.Error("iterator not closed") - } -} - -// Ensure limit iterators work with limit and offset. -func TestLimitIterator_Integer(t *testing.T) { - input := &IntegerIterator{Points: []influxql.IntegerPoint{ - {Name: "cpu", Time: 0, Value: 1}, - {Name: "cpu", Time: 5, Value: 3}, - {Name: "cpu", Time: 10, Value: 5}, - {Name: "mem", Time: 5, Value: 3}, - {Name: "mem", Time: 7, Value: 8}, - }} - - itr := influxql.NewLimitIterator(input, influxql.IteratorOptions{ - Limit: 1, - Offset: 1, - }) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.IntegerPoint{Name: "cpu", Time: 5, Value: 3}}, - {&influxql.IntegerPoint{Name: "mem", Time: 7, Value: 8}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } - - if !input.Closed { - t.Error("iterator not closed") - } -} - -// Ensure limit iterators work with limit and offset. -func TestLimitIterator_String(t *testing.T) { - input := &StringIterator{Points: []influxql.StringPoint{ - {Name: "cpu", Time: 0, Value: "a"}, - {Name: "cpu", Time: 5, Value: "b"}, - {Name: "cpu", Time: 10, Value: "c"}, - {Name: "mem", Time: 5, Value: "d"}, - {Name: "mem", Time: 7, Value: "e"}, - }} - - itr := influxql.NewLimitIterator(input, influxql.IteratorOptions{ - Limit: 1, - Offset: 1, - }) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.StringPoint{Name: "cpu", Time: 5, Value: "b"}}, - {&influxql.StringPoint{Name: "mem", Time: 7, Value: "e"}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } - - if !input.Closed { - t.Error("iterator not closed") - } -} - -// Ensure limit iterators work with limit and offset. -func TestLimitIterator_Boolean(t *testing.T) { - input := &BooleanIterator{Points: []influxql.BooleanPoint{ - {Name: "cpu", Time: 0, Value: true}, - {Name: "cpu", Time: 5, Value: false}, - {Name: "cpu", Time: 10, Value: true}, - {Name: "mem", Time: 5, Value: false}, - {Name: "mem", Time: 7, Value: true}, - }} - - itr := influxql.NewLimitIterator(input, influxql.IteratorOptions{ - Limit: 1, - Offset: 1, - }) - - if a, err := Iterators([]influxql.Iterator{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.BooleanPoint{Name: "cpu", Time: 5, Value: false}}, - {&influxql.BooleanPoint{Name: "mem", Time: 7, Value: true}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } - - if !input.Closed { - t.Error("iterator not closed") - } -} - -// Ensure auxilary iterators can be created for auxilary fields. -func TestFloatAuxIterator(t *testing.T) { - itr := influxql.NewAuxIterator( - &FloatIterator{Points: []influxql.FloatPoint{ - {Time: 0, Value: 1, Aux: []interface{}{float64(100), float64(200)}}, - {Time: 1, Value: 2, Aux: []interface{}{float64(500), math.NaN()}}, - }}, - influxql.IteratorOptions{Aux: []influxql.VarRef{{Val: "f0", Type: influxql.Float}, {Val: "f1", Type: influxql.Float}}}, - ) - - itrs := []influxql.Iterator{ - itr, - itr.Iterator("f0", influxql.Unknown), - itr.Iterator("f1", influxql.Unknown), - itr.Iterator("f0", influxql.Unknown), - } - itr.Start() - - if a, err := Iterators(itrs).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - { - &influxql.FloatPoint{Time: 0, Value: 1, Aux: []interface{}{float64(100), float64(200)}}, - &influxql.FloatPoint{Time: 0, Value: float64(100)}, - &influxql.FloatPoint{Time: 0, Value: float64(200)}, - &influxql.FloatPoint{Time: 0, Value: float64(100)}, - }, - { - &influxql.FloatPoint{Time: 1, Value: 2, Aux: []interface{}{float64(500), math.NaN()}}, - &influxql.FloatPoint{Time: 1, Value: float64(500)}, - &influxql.FloatPoint{Time: 1, Value: math.NaN()}, - &influxql.FloatPoint{Time: 1, Value: float64(500)}, - }, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Ensure limit iterator returns a subset of points. -func TestLimitIterator(t *testing.T) { - itr := influxql.NewLimitIterator( - &FloatIterator{Points: []influxql.FloatPoint{ - {Time: 0, Value: 0}, - {Time: 1, Value: 1}, - {Time: 2, Value: 2}, - {Time: 3, Value: 3}, - }}, - influxql.IteratorOptions{ - Limit: 2, - Offset: 1, - StartTime: influxql.MinTime, - EndTime: influxql.MaxTime, - }, - ) - - if a, err := (Iterators{itr}).ReadAll(); err != nil { - t.Fatalf("unexpected error: %s", err) - } else if !deep.Equal(a, [][]influxql.Point{ - {&influxql.FloatPoint{Time: 1, Value: 1}}, - {&influxql.FloatPoint{Time: 2, Value: 2}}, - }) { - t.Fatalf("unexpected points: %s", spew.Sdump(a)) - } -} - -// Iterators is a test wrapper for iterators. -type Iterators []influxql.Iterator - -// Next returns the next value from each iterator. -// Returns nil if any iterator returns a nil. -func (itrs Iterators) Next() ([]influxql.Point, error) { - a := make([]influxql.Point, len(itrs)) - for i, itr := range itrs { - switch itr := itr.(type) { - case influxql.FloatIterator: - fp, err := itr.Next() - if fp == nil || err != nil { - return nil, err - } - a[i] = fp - case influxql.IntegerIterator: - ip, err := itr.Next() - if ip == nil || err != nil { - return nil, err - } - a[i] = ip - case influxql.StringIterator: - sp, err := itr.Next() - if sp == nil || err != nil { - return nil, err - } - a[i] = sp - case influxql.BooleanIterator: - bp, err := itr.Next() - if bp == nil || err != nil { - return nil, err - } - a[i] = bp - default: - panic(fmt.Sprintf("iterator type not supported: %T", itr)) - } - } - return a, nil -} - -// ReadAll reads all points from all iterators. -func (itrs Iterators) ReadAll() ([][]influxql.Point, error) { - var a [][]influxql.Point - - // Read from every iterator until a nil is encountered. - for { - points, err := itrs.Next() - if err != nil { - return nil, err - } else if points == nil { - break - } - a = append(a, influxql.Points(points).Clone()) - } - - // Close all iterators. - influxql.Iterators(itrs).Close() - - return a, nil -} - -func TestIteratorOptions_Window_Interval(t *testing.T) { - opt := influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10, - }, - } - - start, end := opt.Window(4) - if start != 0 { - t.Errorf("expected start to be 0, got %d", start) - } - if end != 10 { - t.Errorf("expected end to be 10, got %d", end) - } -} - -func TestIteratorOptions_Window_Offset(t *testing.T) { - opt := influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10, - Offset: 8, - }, - } - - start, end := opt.Window(14) - if start != 8 { - t.Errorf("expected start to be 8, got %d", start) - } - if end != 18 { - t.Errorf("expected end to be 18, got %d", end) - } -} - -func TestIteratorOptions_Window_Default(t *testing.T) { - opt := influxql.IteratorOptions{ - StartTime: 0, - EndTime: 60, - } - - start, end := opt.Window(34) - if start != 0 { - t.Errorf("expected start to be 0, got %d", start) - } - if end != 61 { - t.Errorf("expected end to be 61, got %d", end) - } -} - -func TestIteratorOptions_SeekTime_Ascending(t *testing.T) { - opt := influxql.IteratorOptions{ - StartTime: 30, - EndTime: 60, - Ascending: true, - } - - time := opt.SeekTime() - if time != 30 { - t.Errorf("expected time to be 30, got %d", time) - } -} - -func TestIteratorOptions_SeekTime_Descending(t *testing.T) { - opt := influxql.IteratorOptions{ - StartTime: 30, - EndTime: 60, - Ascending: false, - } - - time := opt.SeekTime() - if time != 60 { - t.Errorf("expected time to be 60, got %d", time) - } -} - -func TestIteratorOptions_MergeSorted(t *testing.T) { - opt := influxql.IteratorOptions{} - sorted := opt.MergeSorted() - if !sorted { - t.Error("expected no expression to be sorted, got unsorted") - } - - opt.Expr = &influxql.VarRef{} - sorted = opt.MergeSorted() - if !sorted { - t.Error("expected expression with varref to be sorted, got unsorted") - } - - opt.Expr = &influxql.Call{} - sorted = opt.MergeSorted() - if sorted { - t.Error("expected expression without varref to be unsorted, got sorted") - } -} - -func TestIteratorOptions_DerivativeInterval_Default(t *testing.T) { - opt := influxql.IteratorOptions{} - expected := influxql.Interval{Duration: time.Second} - actual := opt.DerivativeInterval() - if actual != expected { - t.Errorf("expected derivative interval to be %v, got %v", expected, actual) - } -} - -func TestIteratorOptions_DerivativeInterval_GroupBy(t *testing.T) { - opt := influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10, - Offset: 2, - }, - } - expected := influxql.Interval{Duration: 10} - actual := opt.DerivativeInterval() - if actual != expected { - t.Errorf("expected derivative interval to be %v, got %v", expected, actual) - } -} - -func TestIteratorOptions_DerivativeInterval_Call(t *testing.T) { - opt := influxql.IteratorOptions{ - Expr: &influxql.Call{ - Name: "mean", - Args: []influxql.Expr{ - &influxql.VarRef{Val: "value"}, - &influxql.DurationLiteral{Val: 2 * time.Second}, - }, - }, - Interval: influxql.Interval{ - Duration: 10, - Offset: 2, - }, - } - expected := influxql.Interval{Duration: 2 * time.Second} - actual := opt.DerivativeInterval() - if actual != expected { - t.Errorf("expected derivative interval to be %v, got %v", expected, actual) - } -} - -func TestIteratorOptions_ElapsedInterval_Default(t *testing.T) { - opt := influxql.IteratorOptions{} - expected := influxql.Interval{Duration: time.Nanosecond} - actual := opt.ElapsedInterval() - if actual != expected { - t.Errorf("expected elapsed interval to be %v, got %v", expected, actual) - } -} - -func TestIteratorOptions_ElapsedInterval_GroupBy(t *testing.T) { - opt := influxql.IteratorOptions{ - Interval: influxql.Interval{ - Duration: 10, - Offset: 2, - }, - } - expected := influxql.Interval{Duration: time.Nanosecond} - actual := opt.ElapsedInterval() - if actual != expected { - t.Errorf("expected elapsed interval to be %v, got %v", expected, actual) - } -} - -func TestIteratorOptions_ElapsedInterval_Call(t *testing.T) { - opt := influxql.IteratorOptions{ - Expr: &influxql.Call{ - Name: "mean", - Args: []influxql.Expr{ - &influxql.VarRef{Val: "value"}, - &influxql.DurationLiteral{Val: 2 * time.Second}, - }, - }, - Interval: influxql.Interval{ - Duration: 10, - Offset: 2, - }, - } - expected := influxql.Interval{Duration: 2 * time.Second} - actual := opt.ElapsedInterval() - if actual != expected { - t.Errorf("expected elapsed interval to be %v, got %v", expected, actual) - } -} - -// Ensure iterator options can be marshaled to and from a binary format. -func TestIteratorOptions_MarshalBinary(t *testing.T) { - opt := &influxql.IteratorOptions{ - Expr: MustParseExpr("count(value)"), - Aux: []influxql.VarRef{{Val: "a"}, {Val: "b"}, {Val: "c"}}, - Sources: []influxql.Source{ - &influxql.Measurement{Database: "db0", RetentionPolicy: "rp0", Name: "mm0"}, - }, - Interval: influxql.Interval{ - Duration: 1 * time.Hour, - Offset: 20 * time.Minute, - }, - Dimensions: []string{"region", "host"}, - Fill: influxql.NumberFill, - FillValue: float64(100), - Condition: MustParseExpr(`foo = 'bar'`), - StartTime: 1000, - EndTime: 2000, - Ascending: true, - Limit: 100, - Offset: 200, - SLimit: 300, - SOffset: 400, - Dedupe: true, - } - - // Marshal to binary. - buf, err := opt.MarshalBinary() - if err != nil { - t.Fatal(err) - } - - // Unmarshal back to an object. - var other influxql.IteratorOptions - if err := other.UnmarshalBinary(buf); err != nil { - t.Fatal(err) - } else if !reflect.DeepEqual(&other, opt) { - t.Fatalf("unexpected options: %s", spew.Sdump(other)) - } -} - -// Ensure iterator options with a regex measurement can be marshaled. -func TestIteratorOptions_MarshalBinary_Measurement_Regex(t *testing.T) { - opt := &influxql.IteratorOptions{ - Sources: []influxql.Source{ - &influxql.Measurement{Database: "db1", RetentionPolicy: "rp2", Regex: &influxql.RegexLiteral{Val: regexp.MustCompile(`series.+`)}}, - }, - } - - // Marshal to binary. - buf, err := opt.MarshalBinary() - if err != nil { - t.Fatal(err) - } - - // Unmarshal back to an object. - var other influxql.IteratorOptions - if err := other.UnmarshalBinary(buf); err != nil { - t.Fatal(err) - } else if v := other.Sources[0].(*influxql.Measurement).Regex.Val.String(); v != `series.+` { - t.Fatalf("unexpected measurement regex: %s", v) - } -} - -// Ensure iterator can be encoded and decoded over a byte stream. -func TestIterator_EncodeDecode(t *testing.T) { - var buf bytes.Buffer - - // Create an iterator with several points & stats. - itr := &FloatIterator{ - Points: []influxql.FloatPoint{ - {Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 0}, - {Name: "mem", Tags: ParseTags("host=B"), Time: 1, Value: 10}, - }, - stats: influxql.IteratorStats{ - SeriesN: 2, - PointN: 0, - }, - } - - // Encode to the buffer. - enc := influxql.NewIteratorEncoder(&buf) - enc.StatsInterval = 100 * time.Millisecond - if err := enc.EncodeIterator(itr); err != nil { - t.Fatal(err) - } - - // Decode from the buffer. - dec := influxql.NewReaderIterator(&buf, influxql.Float, itr.Stats()) - - // Initial stats should exist immediately. - fdec := dec.(influxql.FloatIterator) - if stats := fdec.Stats(); !reflect.DeepEqual(stats, influxql.IteratorStats{SeriesN: 2, PointN: 0}) { - t.Fatalf("unexpected stats(initial): %#v", stats) - } - - // Read both points. - if p, err := fdec.Next(); err != nil { - t.Fatalf("unexpected error(0): %#v", err) - } else if !reflect.DeepEqual(p, &influxql.FloatPoint{Name: "cpu", Tags: ParseTags("host=A"), Time: 0, Value: 0}) { - t.Fatalf("unexpected point(0); %#v", p) - } - if p, err := fdec.Next(); err != nil { - t.Fatalf("unexpected error(1): %#v", err) - } else if !reflect.DeepEqual(p, &influxql.FloatPoint{Name: "mem", Tags: ParseTags("host=B"), Time: 1, Value: 10}) { - t.Fatalf("unexpected point(1); %#v", p) - } - if p, err := fdec.Next(); err != nil { - t.Fatalf("unexpected error(eof): %#v", err) - } else if p != nil { - t.Fatalf("unexpected point(eof); %#v", p) - } -} - -// IteratorCreator is a mockable implementation of SelectStatementExecutor.IteratorCreator. -type IteratorCreator struct { - CreateIteratorFn func(opt influxql.IteratorOptions) (influxql.Iterator, error) - FieldDimensionsFn func(sources influxql.Sources) (fields map[string]influxql.DataType, dimensions map[string]struct{}, err error) - ExpandSourcesFn func(sources influxql.Sources) (influxql.Sources, error) -} - -func (ic *IteratorCreator) CreateIterator(opt influxql.IteratorOptions) (influxql.Iterator, error) { - return ic.CreateIteratorFn(opt) -} - -func (ic *IteratorCreator) FieldDimensions(sources influxql.Sources) (fields map[string]influxql.DataType, dimensions map[string]struct{}, err error) { - return ic.FieldDimensionsFn(sources) -} - -func (ic *IteratorCreator) ExpandSources(sources influxql.Sources) (influxql.Sources, error) { - return ic.ExpandSourcesFn(sources) -} - -// Test implementation of influxql.FloatIterator -type FloatIterator struct { - Points []influxql.FloatPoint - Closed bool - stats influxql.IteratorStats -} - -func (itr *FloatIterator) Stats() influxql.IteratorStats { return itr.stats } -func (itr *FloatIterator) Close() error { itr.Closed = true; return nil } - -// Next returns the next value and shifts it off the beginning of the points slice. -func (itr *FloatIterator) Next() (*influxql.FloatPoint, error) { - if len(itr.Points) == 0 || itr.Closed { - return nil, nil - } - - v := &itr.Points[0] - itr.Points = itr.Points[1:] - return v, nil -} - -func FloatIterators(inputs []*FloatIterator) []influxql.Iterator { - itrs := make([]influxql.Iterator, len(inputs)) - for i := range itrs { - itrs[i] = influxql.Iterator(inputs[i]) - } - return itrs -} - -// GenerateFloatIterator creates a FloatIterator with random data. -func GenerateFloatIterator(rand *rand.Rand, valueN int) *FloatIterator { - const interval = 10 * time.Second - - itr := &FloatIterator{ - Points: make([]influxql.FloatPoint, valueN), - } - - for i := 0; i < valueN; i++ { - // Generate incrementing timestamp with some jitter (1s). - jitter := (rand.Int63n(2) * int64(time.Second)) - timestamp := int64(i)*int64(10*time.Second) + jitter - - itr.Points[i] = influxql.FloatPoint{ - Name: "cpu", - Time: timestamp, - Value: rand.Float64(), - } - } - - return itr -} - -// Test implementation of influxql.IntegerIterator -type IntegerIterator struct { - Points []influxql.IntegerPoint - Closed bool - stats influxql.IteratorStats -} - -func (itr *IntegerIterator) Stats() influxql.IteratorStats { return itr.stats } -func (itr *IntegerIterator) Close() error { itr.Closed = true; return nil } - -// Next returns the next value and shifts it off the beginning of the points slice. -func (itr *IntegerIterator) Next() (*influxql.IntegerPoint, error) { - if len(itr.Points) == 0 || itr.Closed { - return nil, nil - } - - v := &itr.Points[0] - itr.Points = itr.Points[1:] - return v, nil -} - -func IntegerIterators(inputs []*IntegerIterator) []influxql.Iterator { - itrs := make([]influxql.Iterator, len(inputs)) - for i := range itrs { - itrs[i] = influxql.Iterator(inputs[i]) - } - return itrs -} - -// Test implementation of influxql.StringIterator -type StringIterator struct { - Points []influxql.StringPoint - Closed bool - stats influxql.IteratorStats -} - -func (itr *StringIterator) Stats() influxql.IteratorStats { return itr.stats } -func (itr *StringIterator) Close() error { itr.Closed = true; return nil } - -// Next returns the next value and shifts it off the beginning of the points slice. -func (itr *StringIterator) Next() (*influxql.StringPoint, error) { - if len(itr.Points) == 0 || itr.Closed { - return nil, nil - } - - v := &itr.Points[0] - itr.Points = itr.Points[1:] - return v, nil -} - -func StringIterators(inputs []*StringIterator) []influxql.Iterator { - itrs := make([]influxql.Iterator, len(inputs)) - for i := range itrs { - itrs[i] = influxql.Iterator(inputs[i]) - } - return itrs -} - -// Test implementation of influxql.BooleanIterator -type BooleanIterator struct { - Points []influxql.BooleanPoint - Closed bool - stats influxql.IteratorStats -} - -func (itr *BooleanIterator) Stats() influxql.IteratorStats { return itr.stats } -func (itr *BooleanIterator) Close() error { itr.Closed = true; return nil } - -// Next returns the next value and shifts it off the beginning of the points slice. -func (itr *BooleanIterator) Next() (*influxql.BooleanPoint, error) { - if len(itr.Points) == 0 || itr.Closed { - return nil, nil - } - - v := &itr.Points[0] - itr.Points = itr.Points[1:] - return v, nil -} - -func BooleanIterators(inputs []*BooleanIterator) []influxql.Iterator { - itrs := make([]influxql.Iterator, len(inputs)) - for i := range itrs { - itrs[i] = influxql.Iterator(inputs[i]) - } - return itrs -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/linear.go b/vendor/github.com/influxdata/influxdb/influxql/linear.go deleted file mode 100644 index 910063c9..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/linear.go +++ /dev/null @@ -1,21 +0,0 @@ -package influxql - -// linearFloat computes the the slope of the line between the points (previousTime, previousValue) and (nextTime, nextValue) -// and returns the value of the point on the line with time windowTime -// y = mx + b -func linearFloat(windowTime, previousTime, nextTime int64, previousValue, nextValue float64) float64 { - m := (nextValue - previousValue) / float64(nextTime-previousTime) // the slope of the line - x := float64(windowTime - previousTime) // how far into the interval we are - b := previousValue - return m*x + b -} - -// linearInteger computes the the slope of the line between the points (previousTime, previousValue) and (nextTime, nextValue) -// and returns the value of the point on the line with time windowTime -// y = mx + b -func linearInteger(windowTime, previousTime, nextTime int64, previousValue, nextValue int64) int64 { - m := float64(nextValue-previousValue) / float64(nextTime-previousTime) // the slope of the line - x := float64(windowTime - previousTime) // how far into the interval we are - b := float64(previousValue) - return int64(m*x + b) -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/monitor.go b/vendor/github.com/influxdata/influxdb/influxql/monitor.go deleted file mode 100644 index 3ea4618f..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/monitor.go +++ /dev/null @@ -1,23 +0,0 @@ -package influxql - -import "time" - -// PointLimitMonitor is a query monitor that exits when the number of points -// emitted exceeds a threshold. -func PointLimitMonitor(itrs Iterators, interval time.Duration, limit int) QueryMonitorFunc { - return func(closing <-chan struct{}) error { - ticker := time.NewTicker(interval) - defer ticker.Stop() - for { - select { - case <-ticker.C: - stats := itrs.Stats() - if stats.PointN >= limit { - return ErrMaxSelectPointsLimitExceeded(stats.PointN, limit) - } - case <-closing: - return nil - } - } - } -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/neldermead/neldermead.go b/vendor/github.com/influxdata/influxdb/influxql/neldermead/neldermead.go deleted file mode 100644 index 6b682928..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/neldermead/neldermead.go +++ /dev/null @@ -1,235 +0,0 @@ -// This is an implementation of the Nelder-Mead optimization method -// Based on work by Michael F. Hutt http://www.mikehutt.com/neldermead.html -package neldermead - -import "math" - -const ( - defaultMaxIterations = 1000 - // reflection coefficient - defaultAlpha = 1.0 - // contraction coefficient - defaultBeta = 0.5 - // expansion coefficient - defaultGamma = 2.0 -) - -type Optimizer struct { - MaxIterations int - // reflection coefficient - Alpha, - // contraction coefficient - Beta, - // expansion coefficient - Gamma float64 -} - -func New() *Optimizer { - return &Optimizer{ - MaxIterations: defaultMaxIterations, - Alpha: defaultAlpha, - Beta: defaultBeta, - Gamma: defaultGamma, - } -} - -func (o *Optimizer) Optimize( - objfunc func([]float64) float64, - start []float64, - epsilon, - scale float64, -) (float64, []float64) { - n := len(start) - - //holds vertices of simplex - v := make([][]float64, n+1) - for i := range v { - v[i] = make([]float64, n) - } - - //value of function at each vertex - f := make([]float64, n+1) - - //reflection - coordinates - vr := make([]float64, n) - - //expansion - coordinates - ve := make([]float64, n) - - //contraction - coordinates - vc := make([]float64, n) - - //centroid - coordinates - vm := make([]float64, n) - - // create the initial simplex - // assume one of the vertices is 0,0 - - pn := scale * (math.Sqrt(float64(n+1)) - 1 + float64(n)) / (float64(n) * math.Sqrt(2)) - qn := scale * (math.Sqrt(float64(n+1)) - 1) / (float64(n) * math.Sqrt(2)) - - for i := 0; i < n; i++ { - v[0][i] = start[i] - } - - for i := 1; i <= n; i++ { - for j := 0; j < n; j++ { - if i-1 == j { - v[i][j] = pn + start[j] - } else { - v[i][j] = qn + start[j] - } - } - } - - // find the initial function values - for j := 0; j <= n; j++ { - f[j] = objfunc(v[j]) - } - - // begin the main loop of the minimization - for itr := 1; itr <= o.MaxIterations; itr++ { - - // find the indexes of the largest and smallest values - vg := 0 - vs := 0 - for i := 0; i <= n; i++ { - if f[i] > f[vg] { - vg = i - } - if f[i] < f[vs] { - vs = i - } - } - // find the index of the second largest value - vh := vs - for i := 0; i <= n; i++ { - if f[i] > f[vh] && f[i] < f[vg] { - vh = i - } - } - - // calculate the centroid - for i := 0; i <= n-1; i++ { - cent := 0.0 - for m := 0; m <= n; m++ { - if m != vg { - cent += v[m][i] - } - } - vm[i] = cent / float64(n) - } - - // reflect vg to new vertex vr - for i := 0; i <= n-1; i++ { - vr[i] = vm[i] + o.Alpha*(vm[i]-v[vg][i]) - } - - // value of function at reflection point - fr := objfunc(vr) - - if fr < f[vh] && fr >= f[vs] { - for i := 0; i <= n-1; i++ { - v[vg][i] = vr[i] - } - f[vg] = fr - } - - // investigate a step further in this direction - if fr < f[vs] { - for i := 0; i <= n-1; i++ { - ve[i] = vm[i] + o.Gamma*(vr[i]-vm[i]) - } - - // value of function at expansion point - fe := objfunc(ve) - - // by making fe < fr as opposed to fe < f[vs], - // Rosenbrocks function takes 63 iterations as opposed - // to 64 when using double variables. - - if fe < fr { - for i := 0; i <= n-1; i++ { - v[vg][i] = ve[i] - } - f[vg] = fe - } else { - for i := 0; i <= n-1; i++ { - v[vg][i] = vr[i] - } - f[vg] = fr - } - } - - // check to see if a contraction is necessary - if fr >= f[vh] { - if fr < f[vg] && fr >= f[vh] { - // perform outside contraction - for i := 0; i <= n-1; i++ { - vc[i] = vm[i] + o.Beta*(vr[i]-vm[i]) - } - } else { - // perform inside contraction - for i := 0; i <= n-1; i++ { - vc[i] = vm[i] - o.Beta*(vm[i]-v[vg][i]) - } - } - - // value of function at contraction point - fc := objfunc(vc) - - if fc < f[vg] { - for i := 0; i <= n-1; i++ { - v[vg][i] = vc[i] - } - f[vg] = fc - } else { - // at this point the contraction is not successful, - // we must halve the distance from vs to all the - // vertices of the simplex and then continue. - - for row := 0; row <= n; row++ { - if row != vs { - for i := 0; i <= n-1; i++ { - v[row][i] = v[vs][i] + (v[row][i]-v[vs][i])/2.0 - } - } - } - f[vg] = objfunc(v[vg]) - f[vh] = objfunc(v[vh]) - } - } - - // test for convergence - fsum := 0.0 - for i := 0; i <= n; i++ { - fsum += f[i] - } - favg := fsum / float64(n+1) - s := 0.0 - for i := 0; i <= n; i++ { - s += math.Pow((f[i]-favg), 2.0) / float64(n) - } - s = math.Sqrt(s) - if s < epsilon { - break - } - } - - // find the index of the smallest value - vs := 0 - for i := 0; i <= n; i++ { - if f[i] < f[vs] { - vs = i - } - } - - parameters := make([]float64, n) - for i := 0; i < n; i++ { - parameters[i] = v[vs][i] - } - - min := objfunc(v[vs]) - - return min, parameters -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/neldermead/neldermead_test.go b/vendor/github.com/influxdata/influxdb/influxql/neldermead/neldermead_test.go deleted file mode 100644 index 12d2681c..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/neldermead/neldermead_test.go +++ /dev/null @@ -1,64 +0,0 @@ -package neldermead_test - -import ( - "math" - "testing" - - "github.com/influxdata/influxdb/influxql/neldermead" -) - -func round(num float64, precision float64) float64 { - rnum := num * math.Pow(10, precision) - var tnum float64 - if rnum < 0 { - tnum = math.Floor(rnum - 0.5) - } else { - tnum = math.Floor(rnum + 0.5) - } - rnum = tnum / math.Pow(10, precision) - return rnum -} - -func almostEqual(a, b, e float64) bool { - return math.Abs(a-b) < e -} - -func Test_Optimize(t *testing.T) { - - constraints := func(x []float64) { - for i := range x { - x[i] = round(x[i], 5) - } - } - // 100*(b-a^2)^2 + (1-a)^2 - // - // Obvious global minimum at (a,b) = (1,1) - // - // Useful visualization: - // https://www.wolframalpha.com/input/?i=minimize(100*(b-a%5E2)%5E2+%2B+(1-a)%5E2) - f := func(x []float64) float64 { - constraints(x) - // a = x[0] - // b = x[1] - return 100*(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0]) + (1.0-x[0])*(1.0-x[0]) - } - - start := []float64{-1.2, 1.0} - - opt := neldermead.New() - epsilon := 1e-5 - min, parameters := opt.Optimize(f, start, epsilon, 1) - - if !almostEqual(min, 0, epsilon) { - t.Errorf("unexpected min: got %f exp 0", min) - } - - if !almostEqual(parameters[0], 1, 1e-2) { - t.Errorf("unexpected parameters[0]: got %f exp 1", parameters[0]) - } - - if !almostEqual(parameters[1], 1, 1e-2) { - t.Errorf("unexpected parameters[1]: got %f exp 1", parameters[1]) - } - -} diff --git a/vendor/github.com/influxdata/influxdb/influxql/parser.go b/vendor/github.com/influxdata/influxdb/influxql/parser.go deleted file mode 100644 index 53aba4f2..00000000 --- a/vendor/github.com/influxdata/influxdb/influxql/parser.go +++ /dev/null @@ -1,2953 +0,0 @@ -package influxql - -import ( - "bytes" - "errors" - "fmt" - "io" - "math" - "regexp" - "sort" - "strconv" - "strings" - "time" -) - -const ( - // DateFormat represents the format for date literals. - DateFormat = "2006-01-02" - - // DateTimeFormat represents the format for date time literals. - DateTimeFormat = "2006-01-02 15:04:05.999999" -) - -// Parser represents an InfluxQL parser. -type Parser struct { - s *bufScanner - params map[string]interface{} -} - -// NewParser returns a new instance of Parser. -func NewParser(r io.Reader) *Parser { - return &Parser{s: newBufScanner(r)} -} - -// SetParams sets the parameters that will be used for any bound parameter substitutions. -func (p *Parser) SetParams(params map[string]interface{}) { - p.params = params -} - -// ParseQuery parses a query string and returns its AST representation. -func ParseQuery(s string) (*Query, error) { return NewParser(strings.NewReader(s)).ParseQuery() } - -// ParseStatement parses a statement string and returns its AST representation. -func ParseStatement(s string) (Statement, error) { - return NewParser(strings.NewReader(s)).ParseStatement() -} - -// MustParseStatement parses a statement string and returns its AST. Panic on error. -func MustParseStatement(s string) Statement { - stmt, err := ParseStatement(s) - if err != nil { - panic(err.Error()) - } - return stmt -} - -// ParseExpr parses an expression string and returns its AST representation. -func ParseExpr(s string) (Expr, error) { return NewParser(strings.NewReader(s)).ParseExpr() } - -// MustParseExpr parses an expression string and returns its AST. Panic on error. -func MustParseExpr(s string) Expr { - expr, err := ParseExpr(s) - if err != nil { - panic(err.Error()) - } - return expr -} - -// ParseQuery parses an InfluxQL string and returns a Query AST object. -func (p *Parser) ParseQuery() (*Query, error) { - var statements Statements - semi := true - - for { - if tok, pos, lit := p.scanIgnoreWhitespace(); tok == EOF { - return &Query{Statements: statements}, nil - } else if tok == SEMICOLON { - semi = true - } else { - if !semi { - return nil, newParseError(tokstr(tok, lit), []string{";"}, pos) - } - p.unscan() - s, err := p.ParseStatement() - if err != nil { - return nil, err - } - statements = append(statements, s) - semi = false - } - } -} - -// ParseStatement parses an InfluxQL string and returns a Statement AST object. -func (p *Parser) ParseStatement() (Statement, error) { - // Inspect the first token. - tok, pos, lit := p.scanIgnoreWhitespace() - switch tok { - case SELECT: - return p.parseSelectStatement(targetNotRequired) - case DELETE: - return p.parseDeleteStatement() - case SHOW: - return p.parseShowStatement() - case CREATE: - return p.parseCreateStatement() - case DROP: - return p.parseDropStatement() - case GRANT: - return p.parseGrantStatement() - case REVOKE: - return p.parseRevokeStatement() - case ALTER: - return p.parseAlterStatement() - case SET: - return p.parseSetPasswordUserStatement() - case KILL: - return p.parseKillQueryStatement() - default: - return nil, newParseError(tokstr(tok, lit), []string{"SELECT", "DELETE", "SHOW", "CREATE", "DROP", "GRANT", "REVOKE", "ALTER", "SET", "KILL"}, pos) - } -} - -// parseShowStatement parses a string and returns a list statement. -// This function assumes the SHOW token has already been consumed. -func (p *Parser) parseShowStatement() (Statement, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - switch tok { - case CONTINUOUS: - return p.parseShowContinuousQueriesStatement() - case GRANTS: - return p.parseGrantsForUserStatement() - case DATABASES: - return p.parseShowDatabasesStatement() - case FIELD: - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == KEYS { - return p.parseShowFieldKeysStatement() - } - return nil, newParseError(tokstr(tok, lit), []string{"KEYS"}, pos) - case MEASUREMENTS: - return p.parseShowMeasurementsStatement() - case QUERIES: - return p.parseShowQueriesStatement() - case RETENTION: - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == POLICIES { - return p.parseShowRetentionPoliciesStatement() - } - return nil, newParseError(tokstr(tok, lit), []string{"POLICIES"}, pos) - case SERIES: - return p.parseShowSeriesStatement() - case SHARD: - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == GROUPS { - return p.parseShowShardGroupsStatement() - } - return nil, newParseError(tokstr(tok, lit), []string{"GROUPS"}, pos) - case SHARDS: - return p.parseShowShardsStatement() - case STATS: - return p.parseShowStatsStatement() - case DIAGNOSTICS: - return p.parseShowDiagnosticsStatement() - case TAG: - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == KEYS { - return p.parseShowTagKeysStatement() - } else if tok == VALUES { - return p.parseShowTagValuesStatement() - } - return nil, newParseError(tokstr(tok, lit), []string{"KEYS", "VALUES"}, pos) - case USERS: - return p.parseShowUsersStatement() - case SUBSCRIPTIONS: - return p.parseShowSubscriptionsStatement() - } - - showQueryKeywords := []string{ - "CONTINUOUS", - "DATABASES", - "FIELD", - "GRANTS", - "MEASUREMENTS", - "QUERIES", - "RETENTION", - "SERIES", - "TAG", - "USERS", - "STATS", - "DIAGNOSTICS", - "SHARD", - "SHARDS", - "SUBSCRIPTIONS", - } - sort.Strings(showQueryKeywords) - - return nil, newParseError(tokstr(tok, lit), showQueryKeywords, pos) -} - -// parseCreateStatement parses a string and returns a create statement. -// This function assumes the CREATE token has already been consumed. -func (p *Parser) parseCreateStatement() (Statement, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == CONTINUOUS { - return p.parseCreateContinuousQueryStatement() - } else if tok == DATABASE { - return p.parseCreateDatabaseStatement() - } else if tok == USER { - return p.parseCreateUserStatement() - } else if tok == RETENTION { - tok, pos, lit = p.scanIgnoreWhitespace() - if tok != POLICY { - return nil, newParseError(tokstr(tok, lit), []string{"POLICY"}, pos) - } - return p.parseCreateRetentionPolicyStatement() - } else if tok == SUBSCRIPTION { - return p.parseCreateSubscriptionStatement() - } - - return nil, newParseError(tokstr(tok, lit), []string{"CONTINUOUS", "DATABASE", "USER", "RETENTION", "SUBSCRIPTION"}, pos) -} - -// parseDropStatement parses a string and returns a drop statement. -// This function assumes the DROP token has already been consumed. -func (p *Parser) parseDropStatement() (Statement, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - switch tok { - case CONTINUOUS: - return p.parseDropContinuousQueryStatement() - case DATABASE: - return p.parseDropDatabaseStatement() - case MEASUREMENT: - return p.parseDropMeasurementStatement() - case RETENTION: - if tok, pos, lit := p.scanIgnoreWhitespace(); tok != POLICY { - return nil, newParseError(tokstr(tok, lit), []string{"POLICY"}, pos) - } - return p.parseDropRetentionPolicyStatement() - case SERIES: - return p.parseDropSeriesStatement() - case SHARD: - return p.parseDropShardStatement() - case SUBSCRIPTION: - return p.parseDropSubscriptionStatement() - case USER: - return p.parseDropUserStatement() - default: - return nil, newParseError(tokstr(tok, lit), []string{"CONTINUOUS", "MEASUREMENT", "RETENTION", "SERIES", "SHARD", "SUBSCRIPTION", "USER"}, pos) - } -} - -// parseAlterStatement parses a string and returns an alter statement. -// This function assumes the ALTER token has already been consumed. -func (p *Parser) parseAlterStatement() (Statement, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == RETENTION { - if tok, pos, lit = p.scanIgnoreWhitespace(); tok != POLICY { - return nil, newParseError(tokstr(tok, lit), []string{"POLICY"}, pos) - } - return p.parseAlterRetentionPolicyStatement() - } - - return nil, newParseError(tokstr(tok, lit), []string{"RETENTION"}, pos) -} - -// parseSetPasswordUserStatement parses a string and returns a set statement. -// This function assumes the SET token has already been consumed. -func (p *Parser) parseSetPasswordUserStatement() (*SetPasswordUserStatement, error) { - stmt := &SetPasswordUserStatement{} - - // Consume the required PASSWORD FOR tokens. - if err := p.parseTokens([]Token{PASSWORD, FOR}); err != nil { - return nil, err - } - - // Parse username - ident, err := p.parseIdent() - - if err != nil { - return nil, err - } - stmt.Name = ident - - // Consume the required = token. - if tok, pos, lit := p.scanIgnoreWhitespace(); tok != EQ { - return nil, newParseError(tokstr(tok, lit), []string{"="}, pos) - } - - // Parse new user's password - if ident, err = p.parseString(); err != nil { - return nil, err - } - stmt.Password = ident - - return stmt, nil -} - -// parseKillQueryStatement parses a string and returns a kill statement. -// This function assumes the KILL token has already been consumed. -func (p *Parser) parseKillQueryStatement() (*KillQueryStatement, error) { - if err := p.parseTokens([]Token{QUERY}); err != nil { - return nil, err - } - - qid, err := p.parseUInt64() - if err != nil { - return nil, err - } - - var host string - if tok, _, _ := p.scanIgnoreWhitespace(); tok == ON { - host, err = p.parseIdent() - if err != nil { - return nil, err - } - } else { - p.unscan() - } - return &KillQueryStatement{QueryID: qid, Host: host}, nil -} - -// parseCreateSubscriptionStatement parses a string and returns a CreatesubScriptionStatement. -// This function assumes the "CREATE SUBSCRIPTION" tokens have already been consumed. -func (p *Parser) parseCreateSubscriptionStatement() (*CreateSubscriptionStatement, error) { - stmt := &CreateSubscriptionStatement{} - - // Read the id of the subscription to create. - ident, err := p.parseIdent() - if err != nil { - return nil, err - } - stmt.Name = ident - - // Expect an "ON" keyword. - if tok, pos, lit := p.scanIgnoreWhitespace(); tok != ON { - return nil, newParseError(tokstr(tok, lit), []string{"ON"}, pos) - } - - // Read the name of the database. - if ident, err = p.parseIdent(); err != nil { - return nil, err - } - stmt.Database = ident - - if tok, pos, lit := p.scan(); tok != DOT { - return nil, newParseError(tokstr(tok, lit), []string{"."}, pos) - } - - // Read the name of the retention policy. - if ident, err = p.parseIdent(); err != nil { - return nil, err - } - stmt.RetentionPolicy = ident - - // Expect a "DESTINATIONS" keyword. - if tok, pos, lit := p.scanIgnoreWhitespace(); tok != DESTINATIONS { - return nil, newParseError(tokstr(tok, lit), []string{"DESTINATIONS"}, pos) - } - - // Expect one of "ANY ALL" keywords. - if tok, pos, lit := p.scanIgnoreWhitespace(); tok == ALL || tok == ANY { - stmt.Mode = tokens[tok] - } else { - return nil, newParseError(tokstr(tok, lit), []string{"ALL", "ANY"}, pos) - } - - // Read list of destinations. - var destinations []string - if destinations, err = p.parseStringList(); err != nil { - return nil, err - } - stmt.Destinations = destinations - - return stmt, nil -} - -// parseCreateRetentionPolicyStatement parses a string and returns a create retention policy statement. -// This function assumes the CREATE RETENTION POLICY tokens have already been consumed. -func (p *Parser) parseCreateRetentionPolicyStatement() (*CreateRetentionPolicyStatement, error) { - stmt := &CreateRetentionPolicyStatement{} - - // Parse the retention policy name. - ident, err := p.parseIdent() - if err != nil { - return nil, err - } - stmt.Name = ident - - // Consume the required ON token. - if tok, pos, lit := p.scanIgnoreWhitespace(); tok != ON { - return nil, newParseError(tokstr(tok, lit), []string{"ON"}, pos) - } - - // Parse the database name. - ident, err = p.parseIdent() - if err != nil { - return nil, err - } - stmt.Database = ident - - // Parse required DURATION token. - if tok, pos, lit := p.scanIgnoreWhitespace(); tok != DURATION { - return nil, newParseError(tokstr(tok, lit), []string{"DURATION"}, pos) - } - - // Parse duration value - d, err := p.parseDuration() - if err != nil { - return nil, err - } - stmt.Duration = d - - // Parse required REPLICATION token. - if tok, pos, lit := p.scanIgnoreWhitespace(); tok != REPLICATION { - return nil, newParseError(tokstr(tok, lit), []string{"REPLICATION"}, pos) - } - - // Parse replication value. - n, err := p.parseInt(1, math.MaxInt32) - if err != nil { - return nil, err - } - stmt.Replication = n - - // Parse optional SHARD token. - if tok, _, _ := p.scanIgnoreWhitespace(); tok == SHARD { - if tok, pos, lit := p.scanIgnoreWhitespace(); tok != DURATION { - return nil, newParseError(tokstr(tok, lit), []string{"DURATION"}, pos) - } - - // Check to see if they used the INF keyword - tok, pos, _ := p.scanIgnoreWhitespace() - if tok == INF { - return nil, &ParseError{ - Message: "invalid duration INF for shard duration", - Pos: pos, - } - } - p.unscan() - - d, err := p.parseDuration() - if err != nil { - return nil, err - } - stmt.ShardGroupDuration = d - } else { - p.unscan() - } - - // Parse optional DEFAULT token. - if tok, _, _ := p.scanIgnoreWhitespace(); tok == DEFAULT { - stmt.Default = true - } else { - p.unscan() - } - - return stmt, nil -} - -// parseAlterRetentionPolicyStatement parses a string and returns an alter retention policy statement. -// This function assumes the ALTER RETENTION POLICY tokens have already been consumed. -func (p *Parser) parseAlterRetentionPolicyStatement() (*AlterRetentionPolicyStatement, error) { - stmt := &AlterRetentionPolicyStatement{} - - // Parse the retention policy name. - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == DEFAULT { - stmt.Name = "default" - } else if tok == IDENT { - stmt.Name = lit - } else { - return nil, newParseError(tokstr(tok, lit), []string{"identifier"}, pos) - } - - // Consume the required ON token. - if tok, pos, lit = p.scanIgnoreWhitespace(); tok != ON { - return nil, newParseError(tokstr(tok, lit), []string{"ON"}, pos) - } - - // Parse the database name. - ident, err := p.parseIdent() - if err != nil { - return nil, err - } - stmt.Database = ident - - // Loop through option tokens (DURATION, REPLICATION, SHARD DURATION, DEFAULT, etc.). - found := make(map[Token]struct{}) -Loop: - for { - tok, pos, lit := p.scanIgnoreWhitespace() - if _, ok := found[tok]; ok { - return nil, &ParseError{ - Message: fmt.Sprintf("found duplicate %s option", tok), - Pos: pos, - } - } - - switch tok { - case DURATION: - d, err := p.parseDuration() - if err != nil { - return nil, err - } - stmt.Duration = &d - case REPLICATION: - n, err := p.parseInt(1, math.MaxInt32) - if err != nil { - return nil, err - } - stmt.Replication = &n - case SHARD: - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == DURATION { - // Check to see if they used the INF keyword - tok, pos, _ := p.scanIgnoreWhitespace() - if tok == INF { - return nil, &ParseError{ - Message: "invalid duration INF for shard duration", - Pos: pos, - } - } - p.unscan() - - d, err := p.parseDuration() - if err != nil { - return nil, err - } - stmt.ShardGroupDuration = &d - } else { - return nil, newParseError(tokstr(tok, lit), []string{"DURATION"}, pos) - } - case DEFAULT: - stmt.Default = true - default: - if len(found) == 0 { - return nil, newParseError(tokstr(tok, lit), []string{"DURATION", "REPLICATION", "SHARD", "DEFAULT"}, pos) - } - p.unscan() - break Loop - } - found[tok] = struct{}{} - } - - return stmt, nil -} - -// parseInt parses a string and returns an integer literal. -func (p *Parser) parseInt(min, max int) (int, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - if tok != INTEGER { - return 0, newParseError(tokstr(tok, lit), []string{"integer"}, pos) - } - - // Convert string to int. - n, err := strconv.Atoi(lit) - if err != nil { - return 0, &ParseError{Message: err.Error(), Pos: pos} - } else if min > n || n > max { - return 0, &ParseError{ - Message: fmt.Sprintf("invalid value %d: must be %d <= n <= %d", n, min, max), - Pos: pos, - } - } - - return n, nil -} - -// parseUInt32 parses a string and returns a 32-bit unsigned integer literal. -func (p *Parser) parseUInt32() (uint32, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - if tok != INTEGER { - return 0, newParseError(tokstr(tok, lit), []string{"integer"}, pos) - } - - // Convert string to unsigned 32-bit integer - n, err := strconv.ParseUint(lit, 10, 32) - if err != nil { - return 0, &ParseError{Message: err.Error(), Pos: pos} - } - - return uint32(n), nil -} - -// parseUInt64 parses a string and returns a 64-bit unsigned integer literal. -func (p *Parser) parseUInt64() (uint64, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - if tok != INTEGER { - return 0, newParseError(tokstr(tok, lit), []string{"integer"}, pos) - } - - // Convert string to unsigned 64-bit integer - n, err := strconv.ParseUint(lit, 10, 64) - if err != nil { - return 0, &ParseError{Message: err.Error(), Pos: pos} - } - - return uint64(n), nil -} - -// parseDuration parses a string and returns a duration literal. -// This function assumes the DURATION token has already been consumed. -func (p *Parser) parseDuration() (time.Duration, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - if tok != DURATIONVAL && tok != INF { - return 0, newParseError(tokstr(tok, lit), []string{"duration"}, pos) - } - - if tok == INF { - return 0, nil - } - - d, err := ParseDuration(lit) - if err != nil { - return 0, &ParseError{Message: err.Error(), Pos: pos} - } - - return d, nil -} - -// parseIdent parses an identifier. -func (p *Parser) parseIdent() (string, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - if tok != IDENT { - return "", newParseError(tokstr(tok, lit), []string{"identifier"}, pos) - } - return lit, nil -} - -// parseIdentList parses a comma delimited list of identifiers. -func (p *Parser) parseIdentList() ([]string, error) { - // Parse first (required) identifier. - ident, err := p.parseIdent() - if err != nil { - return nil, err - } - idents := []string{ident} - - // Parse remaining (optional) identifiers. - for { - if tok, _, _ := p.scanIgnoreWhitespace(); tok != COMMA { - p.unscan() - return idents, nil - } - - if ident, err = p.parseIdent(); err != nil { - return nil, err - } - - idents = append(idents, ident) - } -} - -// parseSegmentedIdents parses a segmented identifiers. -// e.g., "db"."rp".measurement or "db"..measurement -func (p *Parser) parseSegmentedIdents() ([]string, error) { - ident, err := p.parseIdent() - if err != nil { - return nil, err - } - idents := []string{ident} - - // Parse remaining (optional) identifiers. - for { - if tok, _, _ := p.scan(); tok != DOT { - // No more segments so we're done. - p.unscan() - break - } - - if ch := p.peekRune(); ch == '/' { - // Next segment is a regex so we're done. - break - } else if ch == ':' { - // Next segment is context-specific so let caller handle it. - break - } else if ch == '.' { - // Add an empty identifier. - idents = append(idents, "") - continue - } - - // Parse the next identifier. - if ident, err = p.parseIdent(); err != nil { - return nil, err - } - - idents = append(idents, ident) - } - - if len(idents) > 3 { - msg := fmt.Sprintf("too many segments in %s", QuoteIdent(idents...)) - return nil, &ParseError{Message: msg} - } - - return idents, nil -} - -// parserString parses a string. -func (p *Parser) parseString() (string, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - if tok != STRING { - return "", newParseError(tokstr(tok, lit), []string{"string"}, pos) - } - return lit, nil -} - -// parserString parses a string. -func (p *Parser) parseStringList() ([]string, error) { - // Parse first (required) string. - str, err := p.parseString() - if err != nil { - return nil, err - } - strs := []string{str} - - // Parse remaining (optional) strings. - for { - if tok, _, _ := p.scanIgnoreWhitespace(); tok != COMMA { - p.unscan() - return strs, nil - } - - if str, err = p.parseString(); err != nil { - return nil, err - } - - strs = append(strs, str) - } -} - -// parseRevokeStatement parses a string and returns a revoke statement. -// This function assumes the REVOKE token has already been consumed. -func (p *Parser) parseRevokeStatement() (Statement, error) { - // Parse the privilege to be revoked. - priv, err := p.parsePrivilege() - if err != nil { - return nil, err - } - - // Check for ON or FROM clauses. - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == ON { - stmt, err := p.parseRevokeOnStatement() - if err != nil { - return nil, err - } - stmt.Privilege = priv - return stmt, nil - } else if tok == FROM { - // Admin privilege is only revoked on ALL PRIVILEGES. - if priv != AllPrivileges { - return nil, newParseError(tokstr(tok, lit), []string{"ON"}, pos) - } - return p.parseRevokeAdminStatement() - } - - // Only ON or FROM clauses are allowed after privilege. - if priv == AllPrivileges { - return nil, newParseError(tokstr(tok, lit), []string{"ON", "FROM"}, pos) - } - return nil, newParseError(tokstr(tok, lit), []string{"ON"}, pos) -} - -// parseRevokeOnStatement parses a string and returns a revoke statement. -// This function assumes the [PRIVILEGE] ON tokens have already been consumed. -func (p *Parser) parseRevokeOnStatement() (*RevokeStatement, error) { - stmt := &RevokeStatement{} - - // Parse the name of the database. - lit, err := p.parseIdent() - if err != nil { - return nil, err - } - stmt.On = lit - - // Parse FROM clause. - tok, pos, lit := p.scanIgnoreWhitespace() - - // Check for required FROM token. - if tok != FROM { - return nil, newParseError(tokstr(tok, lit), []string{"FROM"}, pos) - } - - // Parse the name of the user. - lit, err = p.parseIdent() - if err != nil { - return nil, err - } - stmt.User = lit - - return stmt, nil -} - -// parseRevokeAdminStatement parses a string and returns a revoke admin statement. -// This function assumes the ALL [PRVILEGES] FROM token has already been consumed. -func (p *Parser) parseRevokeAdminStatement() (*RevokeAdminStatement, error) { - // Admin privilege is always false when revoke admin clause is called. - stmt := &RevokeAdminStatement{} - - // Parse the name of the user. - lit, err := p.parseIdent() - if err != nil { - return nil, err - } - stmt.User = lit - - return stmt, nil -} - -// parseGrantStatement parses a string and returns a grant statement. -// This function assumes the GRANT token has already been consumed. -func (p *Parser) parseGrantStatement() (Statement, error) { - // Parse the privilege to be granted. - priv, err := p.parsePrivilege() - if err != nil { - return nil, err - } - - // Check for ON or TO clauses. - tok, pos, lit := p.scanIgnoreWhitespace() - if tok == ON { - stmt, err := p.parseGrantOnStatement() - if err != nil { - return nil, err - } - stmt.Privilege = priv - return stmt, nil - } else if tok == TO { - // Admin privilege is only granted on ALL PRIVILEGES. - if priv != AllPrivileges { - return nil, newParseError(tokstr(tok, lit), []string{"ON"}, pos) - } - return p.parseGrantAdminStatement() - } - - // Only ON or TO clauses are allowed after privilege. - if priv == AllPrivileges { - return nil, newParseError(tokstr(tok, lit), []string{"ON", "TO"}, pos) - } - return nil, newParseError(tokstr(tok, lit), []string{"ON"}, pos) -} - -// parseGrantOnStatement parses a string and returns a grant statement. -// This function assumes the [PRIVILEGE] ON tokens have already been consumed. -func (p *Parser) parseGrantOnStatement() (*GrantStatement, error) { - stmt := &GrantStatement{} - - // Parse the name of the database. - lit, err := p.parseIdent() - if err != nil { - return nil, err - } - stmt.On = lit - - // Parse TO clause. - tok, pos, lit := p.scanIgnoreWhitespace() - - // Check for required TO token. - if tok != TO { - return nil, newParseError(tokstr(tok, lit), []string{"TO"}, pos) - } - - // Parse the name of the user. - lit, err = p.parseIdent() - if err != nil { - return nil, err - } - stmt.User = lit - - return stmt, nil -} - -// parseGrantAdminStatement parses a string and returns a grant admin statement. -// This function assumes the ALL [PRVILEGES] TO tokens have already been consumed. -func (p *Parser) parseGrantAdminStatement() (*GrantAdminStatement, error) { - // Admin privilege is always true when grant admin clause is called. - stmt := &GrantAdminStatement{} - - // Parse the name of the user. - lit, err := p.parseIdent() - if err != nil { - return nil, err - } - stmt.User = lit - - return stmt, nil -} - -// parsePrivilege parses a string and returns a Privilege -func (p *Parser) parsePrivilege() (Privilege, error) { - tok, pos, lit := p.scanIgnoreWhitespace() - switch tok { - case READ: - return ReadPrivilege, nil - case WRITE: - return WritePrivilege, nil - case ALL: - // Consume optional PRIVILEGES token - tok, pos, lit = p.scanIgnoreWhitespace() - if tok != PRIVILEGES { - p.unscan() - } - return AllPrivileges, nil - } - return 0, newParseError(tokstr(tok, lit), []string{"READ", "WRITE", "ALL [PRIVILEGES]"}, pos) -} - -// parseSelectStatement parses a select string and returns a Statement AST object. -// This function assumes the SELECT token has already been consumed. -func (p *Parser) parseSelectStatement(tr targetRequirement) (*SelectStatement, error) { - stmt := &SelectStatement{} - var err error - - // Parse fields: "FIELD+". - if stmt.Fields, err = p.parseFields(); err != nil { - return nil, err - } - - // Parse target: "INTO" - if stmt.Target, err = p.parseTarget(tr); err != nil { - return nil, err - } - - // Parse source: "FROM". - if tok, pos, lit := p.scanIgnoreWhitespace(); tok != FROM { - return nil, newParseError(tokstr(tok, lit), []string{"FROM"}, pos) - } - if stmt.Sources, err = p.parseSources(); err != nil { - return nil, err - } - - // Parse condition: "WHERE EXPR". - if stmt.Condition, err = p.parseCondition(); err != nil { - return nil, err - } - - // Parse dimensions: "GROUP BY DIMENSION+". - if stmt.Dimensions, err = p.parseDimensions(); err != nil { - return nil, err - } - - // Parse fill options: "fill(