Skip to content

Commit

Permalink
Merge pull request #2897 from influxdb/graphite_db
Browse files Browse the repository at this point in the history
Ensure target Graphite database exists
  • Loading branch information
otoolep committed Jun 11, 2015
2 parents 95500d7 + dddaf46 commit f60c816
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- [#2869](https://github.com/influxdb/influxdb/issues/2869): Adding field to existing measurement causes panic
- [#2849](https://github.com/influxdb/influxdb/issues/2849): RC32: Frequent write errors
- [#2700](https://github.com/influxdb/influxdb/issues/2700): Incorrect error message in database EncodeFields

- [#2897](https://github.com/influxdb/influxdb/pull/2897): Ensure target Graphite database exists

## v0.9.0-rc33 [2015-06-09]

Expand Down
1 change: 1 addition & 0 deletions cmd/influxd/run/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (s *Server) appendGraphiteService(c graphite.Config) error {
}

srv.PointsWriter = s.PointsWriter
srv.MetaStore = s.MetaStore
s.Services = append(s.Services, srv)
return nil
}
Expand Down
11 changes: 10 additions & 1 deletion services/graphite/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/influxdb/influxdb/cluster"
"github.com/influxdb/influxdb/meta"
"github.com/influxdb/influxdb/tsdb"
)

Expand Down Expand Up @@ -41,6 +42,9 @@ type Service struct {
PointsWriter interface {
WritePoints(p *cluster.WritePointsRequest) error
}
MetaStore interface {
CreateDatabaseIfNotExists(name string) (*meta.DatabaseInfo, error)
}
}

// NewService returns an instance of the Graphite service.
Expand Down Expand Up @@ -73,14 +77,19 @@ func NewService(c Config) (*Service, error) {
func (s *Service) Open() error {
var err error

if _, err := s.MetaStore.CreateDatabaseIfNotExists(s.database); err != nil {
s.logger.Printf("failed to ensure target database %s exists: %s", s.database, err.Error())
return err
}
s.logger.Printf("ensured target database %s exists", s.database)

if strings.ToLower(s.protocol) == "tcp" {
s.addr, err = s.openTCPServer()
} else if strings.ToLower(s.protocol) == "udp" {
s.addr, err = s.openUDPServer()
} else {
return fmt.Errorf("unrecognized Graphite input protocol %s", s.protocol)
}

if err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions services/graphite/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/influxdb/influxdb/cluster"
"github.com/influxdb/influxdb/meta"
"github.com/influxdb/influxdb/services/graphite"
"github.com/influxdb/influxdb/toml"
"github.com/influxdb/influxdb/tsdb"
Expand Down Expand Up @@ -272,11 +273,17 @@ func Test_ServerGraphiteTCP(t *testing.T) {
},
}
service.PointsWriter = &pointsWriter
dbCreator := DatabaseCreator{}
service.MetaStore = &dbCreator

if err := service.Open(); err != nil {
t.Fatalf("failed to open Graphite service: %s", err.Error())
}

if !dbCreator.Created {
t.Fatalf("failed to create target database")
}

// Connect to the graphite endpoint we just spun up
_, port, _ := net.SplitHostPort(service.Addr().String())
conn, err := net.Dial("tcp", "127.0.0.1:"+port)
Expand Down Expand Up @@ -339,11 +346,17 @@ func Test_ServerGraphiteUDP(t *testing.T) {
},
}
service.PointsWriter = &pointsWriter
dbCreator := DatabaseCreator{}
service.MetaStore = &dbCreator

if err := service.Open(); err != nil {
t.Fatalf("failed to open Graphite service: %s", err.Error())
}

if !dbCreator.Created {
t.Fatalf("failed to create target database")
}

// Connect to the graphite endpoint we just spun up
_, port, _ := net.SplitHostPort(service.Addr().String())
conn, err := net.Dial("udp", "127.0.0.1:"+port)
Expand Down Expand Up @@ -371,6 +384,15 @@ func (w *PointsWriter) WritePoints(p *cluster.WritePointsRequest) error {
return w.WritePointsFn(p)
}

type DatabaseCreator struct {
Created bool
}

func (d *DatabaseCreator) CreateDatabaseIfNotExists(name string) (*meta.DatabaseInfo, error) {
d.Created = true
return nil, nil
}

// Test Helpers
func errstr(err error) string {
if err != nil {
Expand Down

0 comments on commit f60c816

Please sign in to comment.