Skip to content

Commit

Permalink
Update load database config with validations and tests
Browse files Browse the repository at this point in the history
Update the client struct so the Database is public and can be set by library users.
Move the db_config API to specify the database name in the URL.
Make load config test also test loading continuous queries.
  • Loading branch information
pauldix committed Aug 1, 2014
1 parent ab24da4 commit 87361d1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
32 changes: 20 additions & 12 deletions api/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (self *HttpServer) Serve(listener net.Listener) {
self.registerEndpoint(p, "get", "/cluster/shard_spaces", self.getShardSpaces)
self.registerEndpoint(p, "post", "/cluster/shard_spaces", self.createShardSpace)
self.registerEndpoint(p, "del", "/cluster/shard_spaces/:db/:name", self.dropShardSpace)
self.registerEndpoint(p, "post", "/cluster/db_config", self.configureDatabase)
self.registerEndpoint(p, "post", "/cluster/database_configs/:db", self.configureDatabase)

// return whether the cluster is in sync or not
self.registerEndpoint(p, "get", "/sync", self.isInSync)
Expand Down Expand Up @@ -1210,6 +1210,21 @@ func (self *HttpServer) configureDatabase(w libhttp.ResponseWriter, r *libhttp.R
if err != nil {
return libhttp.StatusInternalServerError, err.Error()
}
databaseConfig.Database = r.URL.Query().Get(":db")

This comment has been minimized.

Copy link
@jvshahid

jvshahid Aug 1, 2014

Contributor

Do we need to keep the Database field ? I think we can just use a variable to store the value of :db and get rid of Database from DatabaseConfig


// validate before creating anything
for _, queryString := range databaseConfig.ContinuousQueries {
q, err := parser.ParseQuery(queryString)
if err != nil {
return libhttp.StatusBadRequest, err.Error()
}
for _, query := range q {
if !query.SelectQuery.IsContinuousQuery() {
return libhttp.StatusBadRequest, fmt.Errorf("This query isn't a continuous query. Use 'into'. %s", query.QueryString)
}
}
}

err = self.coordinator.CreateDatabase(u, databaseConfig.Database)
if err != nil {
return libhttp.StatusBadRequest, err.Error()
Expand All @@ -1222,18 +1237,11 @@ func (self *HttpServer) configureDatabase(w libhttp.ResponseWriter, r *libhttp.R
}
}
for _, queryString := range databaseConfig.ContinuousQueries {
q, err := parser.ParseQuery(queryString)
if err != nil {
return libhttp.StatusInternalServerError, err.Error()
}
q, _ := parser.ParseQuery(queryString)

This comment has been minimized.

Copy link
@jvshahid

jvshahid Aug 1, 2014

Contributor

Do we still need to parse the query again ?

for _, query := range q {
selectQuery := query.SelectQuery

if selectQuery.IsContinuousQuery() {
err := self.coordinator.CreateContinuousQuery(u, databaseConfig.Database, query.QueryString)
if err != nil {
return libhttp.StatusInternalServerError, err.Error()
}
err := self.coordinator.CreateContinuousQuery(u, databaseConfig.Database, query.QueryString)
if err != nil {
return libhttp.StatusInternalServerError, err.Error()
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions client/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Client struct {
host string
username string
password string
database string
Database string

This comment has been minimized.

Copy link
@jvshahid

jvshahid Aug 1, 2014

Contributor

Why is database capitalized ?

httpClient *http.Client
udpConn *net.UDPConn
schema string
Expand Down Expand Up @@ -394,7 +394,7 @@ func (self *Client) writeSeriesCommon(series []*Series, options map[string]strin
if err != nil {
return err
}
url := self.getUrl("/db/" + self.database + "/series")
url := self.getUrl("/db/" + self.Database + "/series")
for name, value := range options {
url += fmt.Sprintf("&%s=%s", name, value)
}
Expand Down Expand Up @@ -431,7 +431,7 @@ func (self *Client) QueryWithNumbers(query string, precision ...TimePrecision) (

func (self *Client) queryCommon(query string, useNumber bool, precision ...TimePrecision) ([]*Series, error) {
escapedQuery := url.QueryEscape(query)
url := self.getUrl("/db/" + self.database + "/series")
url := self.getUrl("/db/" + self.Database + "/series")
if len(precision) > 0 {
url += "&time_precision=" + string(precision[0])
}
Expand Down Expand Up @@ -481,12 +481,12 @@ func (self *Client) AuthenticateClusterAdmin(username, password string) error {
}

func (self *Client) GetContinuousQueries() ([]map[string]interface{}, error) {
url := self.getUrlWithUserAndPass(fmt.Sprintf("/db/%s/continuous_queries", self.database), self.username, self.password)
url := self.getUrlWithUserAndPass(fmt.Sprintf("/db/%s/continuous_queries", self.Database), self.username, self.password)
return self.listSomething(url)
}

func (self *Client) DeleteContinuousQueries(id int) error {
url := self.getUrlWithUserAndPass(fmt.Sprintf("/db/%s/continuous_queries/%d", self.database, id), self.username, self.password)
url := self.getUrlWithUserAndPass(fmt.Sprintf("/db/%s/continuous_queries/%d", self.Database, id), self.username, self.password)
resp, err := self.del(url)
return responseToError(resp, err, true)
}
Expand Down
5 changes: 4 additions & 1 deletion integration/database_conf.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"database": "test_db_conf_db",
"spaces": [
{
"name": "everything",
Expand All @@ -25,5 +24,9 @@
"replicationFactor": 2,
"split": 3
}
],
"continuousQueries": [
"select * from events into events.[id]",
"select count(value) from events group by time(5m) into 5m.count.events"
]
}
10 changes: 9 additions & 1 deletion integration/single_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ func (self *SingleServerSuite) TestDropShardSpace(c *C) {
func (self *SingleServerSuite) TestLoadDatabaseConfig(c *C) {
contents, err := ioutil.ReadFile("database_conf.json")
c.Assert(err, IsNil)
resp, err := http.Post("http://localhost:8086/cluster/db_config?u=root&p=root", "application/json", bytes.NewBuffer(contents))
resp, err := http.Post("http://localhost:8086/cluster/database_configs/test_db_conf_db?u=root&p=root", "application/json", bytes.NewBuffer(contents))
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusCreated)

Expand All @@ -849,4 +849,12 @@ func (self *SingleServerSuite) TestLoadDatabaseConfig(c *C) {
c.Assert(space, NotNil)
c.Assert(space.Split, Equals, uint32(3))
c.Assert(space.ReplicationFactor, Equals, uint32(2))

client.Database = "test_db_conf_db"
series, err := client.Query("list continuous queries;")
c.Assert(err, IsNil)
queries := series[0].Points
c.Assert(queries, HasLen, 2)
c.Assert(queries[0][2], Equals, "select * from events into events.[id]")
c.Assert(queries[1][2], Equals, "select count(value) from events group by time(5m) into 5m.count.events")
}

0 comments on commit 87361d1

Please sign in to comment.