influxdb/services/udp/service.go

258 lines
6.2 KiB
Go
Raw Normal View History

2016-02-10 18:30:52 +00:00
package udp // import "github.com/influxdata/influxdb/services/udp"
2015-06-03 13:06:36 +00:00
import (
2015-06-04 23:07:34 +00:00
"errors"
"io"
2015-06-04 23:07:34 +00:00
"log"
2015-06-03 13:06:36 +00:00
"net"
2015-06-04 23:07:34 +00:00
"os"
"sync"
"sync/atomic"
2015-06-03 13:06:36 +00:00
"time"
2015-06-04 23:07:34 +00:00
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/services/meta"
"github.com/influxdata/influxdb/tsdb"
2015-06-04 23:07:34 +00:00
)
const (
// Arbitrary, testing indicated that this doesn't typically get over 10
parserChanLen = 1000
MAX_UDP_PAYLOAD = 64 * 1024
2015-06-03 13:06:36 +00:00
)
2015-09-09 01:47:41 +00:00
// statistics gathered by the UDP package.
const (
statPointsReceived = "pointsRx"
statBytesReceived = "bytesRx"
statPointsParseFail = "pointsParseFail"
statReadFail = "readFail"
statBatchesTransmitted = "batchesTx"
statPointsTransmitted = "pointsTx"
statBatchesTransmitFail = "batchesTxFail"
2015-09-09 01:47:41 +00:00
)
2015-06-04 23:07:34 +00:00
//
// Service represents here an UDP service
// that will listen for incoming packets
// formatted with the inline protocol
//
2015-06-03 13:06:36 +00:00
type Service struct {
2015-06-04 23:07:34 +00:00
conn *net.UDPConn
addr *net.UDPAddr
wg sync.WaitGroup
done chan struct{}
2015-06-03 13:06:36 +00:00
parserChan chan []byte
batcher *tsdb.PointBatcher
config Config
2015-06-04 23:07:34 +00:00
PointsWriter interface {
WritePoints(database, retentionPolicy string, consistencyLevel models.ConsistencyLevel, points []models.Point) error
2015-06-04 23:07:34 +00:00
}
2015-12-23 15:48:25 +00:00
MetaClient interface {
CreateDatabase(name string) (*meta.DatabaseInfo, error)
}
Logger *log.Logger
stats *Statistics
statTags map[string]string
2015-06-03 13:06:36 +00:00
}
// NewService returns a new instance of Service.
2015-06-03 13:06:36 +00:00
func NewService(c Config) *Service {
2015-09-08 22:25:42 +00:00
d := *c.WithDefaults()
2015-06-03 13:06:36 +00:00
return &Service{
config: d,
done: make(chan struct{}),
parserChan: make(chan []byte, parserChanLen),
batcher: tsdb.NewPointBatcher(d.BatchSize, d.BatchPending, time.Duration(d.BatchTimeout)),
Logger: log.New(os.Stderr, "[udp] ", log.LstdFlags),
stats: &Statistics{},
statTags: map[string]string{"bind": d.BindAddress},
2015-06-04 23:07:34 +00:00
}
}
// Open starts the service
2015-06-04 23:07:34 +00:00
func (s *Service) Open() (err error) {
if s.config.BindAddress == "" {
return errors.New("bind address has to be specified in config")
}
if s.config.Database == "" {
return errors.New("database has to be specified in config")
}
if _, err := s.MetaClient.CreateDatabase(s.config.Database); err != nil {
return errors.New("Failed to ensure target database exists")
}
2015-06-04 23:07:34 +00:00
s.addr, err = net.ResolveUDPAddr("udp", s.config.BindAddress)
if err != nil {
s.Logger.Printf("Failed to resolve UDP address %s: %s", s.config.BindAddress, err)
return err
}
s.conn, err = net.ListenUDP("udp", s.addr)
if err != nil {
s.Logger.Printf("Failed to set up UDP listener at address %s: %s", s.addr, err)
return err
}
if s.config.ReadBuffer != 0 {
err = s.conn.SetReadBuffer(s.config.ReadBuffer)
if err != nil {
s.Logger.Printf("Failed to set UDP read buffer to %d: %s",
s.config.ReadBuffer, err)
return err
}
}
2015-06-04 23:07:34 +00:00
s.Logger.Printf("Started listening on UDP: %s", s.config.BindAddress)
2015-06-04 23:07:34 +00:00
s.wg.Add(3)
2015-06-04 23:07:34 +00:00
go s.serve()
go s.parser()
go s.writer()
2015-06-04 23:07:34 +00:00
return nil
}
// Statistics maintains statistics for the UDP service.
type Statistics struct {
PointsReceived int64
BytesReceived int64
PointsParseFail int64
ReadFail int64
BatchesTransmitted int64
PointsTransmitted int64
BatchesTransmitFail int64
}
// Statistics returns statistics for periodic monitoring.
func (s *Service) Statistics(tags map[string]string) []models.Statistic {
// Insert any missing deault tag values.
for k, v := range s.statTags {
if _, ok := tags[k]; !ok {
tags[k] = v
}
}
return []models.Statistic{{
Name: "udp",
Tags: tags,
Values: map[string]interface{}{
statPointsReceived: atomic.LoadInt64(&s.stats.PointsReceived),
statBytesReceived: atomic.LoadInt64(&s.stats.BytesReceived),
statPointsParseFail: atomic.LoadInt64(&s.stats.PointsParseFail),
statReadFail: atomic.LoadInt64(&s.stats.ReadFail),
statBatchesTransmitted: atomic.LoadInt64(&s.stats.BatchesTransmitted),
statPointsTransmitted: atomic.LoadInt64(&s.stats.PointsTransmitted),
statBatchesTransmitFail: atomic.LoadInt64(&s.stats.BatchesTransmitFail),
},
}}
}
func (s *Service) writer() {
2015-06-04 23:07:34 +00:00
defer s.wg.Done()
for {
select {
case batch := <-s.batcher.Out():
if err := s.PointsWriter.WritePoints(s.config.Database, s.config.RetentionPolicy, models.ConsistencyLevelAny, batch); err == nil {
atomic.AddInt64(&s.stats.BatchesTransmitted, 1)
atomic.AddInt64(&s.stats.PointsTransmitted, int64(len(batch)))
2015-09-09 01:47:41 +00:00
} else {
s.Logger.Printf("failed to write point batch to database %q: %s", s.config.Database, err)
atomic.AddInt64(&s.stats.BatchesTransmitFail, 1)
2015-06-04 23:07:34 +00:00
}
2015-06-04 23:07:34 +00:00
case <-s.done:
return
}
2015-06-03 13:06:36 +00:00
}
}
2015-06-04 23:07:34 +00:00
func (s *Service) serve() {
defer s.wg.Done()
buf := make([]byte, MAX_UDP_PAYLOAD)
2015-06-04 23:07:34 +00:00
s.batcher.Start()
for {
select {
case <-s.done:
// We closed the connection, time to go.
return
default:
// Keep processing.
n, _, err := s.conn.ReadFromUDP(buf)
if err != nil {
atomic.AddInt64(&s.stats.ReadFail, 1)
s.Logger.Printf("Failed to read UDP message: %s", err)
continue
}
atomic.AddInt64(&s.stats.BytesReceived, int64(n))
bufCopy := make([]byte, n)
copy(bufCopy, buf[:n])
s.parserChan <- bufCopy
2015-06-04 23:07:34 +00:00
}
}
}
2015-06-04 23:07:34 +00:00
func (s *Service) parser() {
defer s.wg.Done()
2015-06-04 23:07:34 +00:00
for {
select {
case <-s.done:
return
case buf := <-s.parserChan:
points, err := models.ParsePointsWithPrecision(buf, time.Now().UTC(), s.config.Precision)
if err != nil {
atomic.AddInt64(&s.stats.PointsParseFail, 1)
s.Logger.Printf("Failed to parse points: %s", err)
continue
}
2015-06-04 23:07:34 +00:00
for _, point := range points {
s.batcher.In() <- point
}
atomic.AddInt64(&s.stats.PointsReceived, int64(len(points)))
2015-06-04 23:07:34 +00:00
}
}
2015-06-03 13:06:36 +00:00
}
// Close closes the underlying listener.
2015-06-03 13:06:36 +00:00
func (s *Service) Close() error {
2015-06-04 23:07:34 +00:00
if s.conn == nil {
return errors.New("Service already closed")
}
s.conn.Close()
s.batcher.Flush()
close(s.done)
s.wg.Wait()
// Release all remaining resources.
s.done = nil
s.conn = nil
s.Logger.Print("Service closed")
return nil
2015-06-03 13:06:36 +00:00
}
// SetLogOutput sets the writer to which all logs are written. It must not be
// called after Open is called.
func (s *Service) SetLogOutput(w io.Writer) {
s.Logger = log.New(w, "[udp] ", log.LstdFlags)
}
// Addr returns the listener's address
2015-06-03 13:06:36 +00:00
func (s *Service) Addr() net.Addr {
2015-06-04 23:07:34 +00:00
return s.addr
2015-06-03 13:06:36 +00:00
}