2019-05-22 23:16:55 +02:00
|
|
|
package opentsdb
|
|
|
|
|
|
|
|
import (
|
2020-06-30 23:15:34 +02:00
|
|
|
"errors"
|
2020-02-23 12:35:47 +01:00
|
|
|
"io"
|
2019-05-22 23:16:55 +02:00
|
|
|
"net"
|
2020-02-23 12:35:47 +01:00
|
|
|
"net/http"
|
2019-05-22 23:16:55 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2020-12-08 19:49:32 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2020-02-23 12:35:47 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/ingestserver/opentsdbhttp"
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2019-07-15 21:58:00 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-02-25 18:09:46 +01:00
|
|
|
writeRequestsTCP = metrics.NewCounter(`vm_ingestserver_requests_total{type="opentsdb", name="write", net="tcp"}`)
|
|
|
|
writeErrorsTCP = metrics.NewCounter(`vm_ingestserver_request_errors_total{type="opentsdb", name="write", net="tcp"}`)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2020-02-25 18:09:46 +01:00
|
|
|
writeRequestsUDP = metrics.NewCounter(`vm_ingestserver_requests_total{type="opentsdb", name="write", net="udp"}`)
|
|
|
|
writeErrorsUDP = metrics.NewCounter(`vm_ingestserver_request_errors_total{type="opentsdb", name="write", net="udp"}`)
|
2019-05-22 23:16:55 +02:00
|
|
|
)
|
|
|
|
|
2019-12-13 23:29:14 +01:00
|
|
|
// Server is a server for collecting OpenTSDB TCP and UDP metrics.
|
|
|
|
//
|
|
|
|
// It accepts simultaneously Telnet put requests and HTTP put requests over TCP.
|
|
|
|
type Server struct {
|
|
|
|
addr string
|
|
|
|
ls *listenerSwitch
|
|
|
|
httpServer *opentsdbhttp.Server
|
|
|
|
lnUDP net.PacketConn
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustStart starts OpenTSDB collector on the given addr.
|
|
|
|
//
|
|
|
|
// MustStop must be called on the returned server when it is no longer needed.
|
2020-02-23 12:35:47 +01:00
|
|
|
func MustStart(addr string, telnetInsertHandler func(r io.Reader) error, httpInsertHandler func(req *http.Request) error) *Server {
|
2019-05-22 23:16:55 +02:00
|
|
|
logger.Infof("starting TCP OpenTSDB collector at %q", addr)
|
2019-07-15 21:58:00 +02:00
|
|
|
lnTCP, err := netutil.NewTCPListener("opentsdb", addr)
|
2019-05-22 23:16:55 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("cannot start TCP OpenTSDB collector at %q: %s", addr, err)
|
|
|
|
}
|
2019-12-13 23:29:14 +01:00
|
|
|
ls := newListenerSwitch(lnTCP)
|
|
|
|
lnHTTP := ls.newHTTPListener()
|
|
|
|
lnTelnet := ls.newTelnetListener()
|
2020-02-23 12:35:47 +01:00
|
|
|
httpServer := opentsdbhttp.MustServe(lnHTTP, httpInsertHandler)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
logger.Infof("starting UDP OpenTSDB collector at %q", addr)
|
2021-03-16 23:03:06 +01:00
|
|
|
lnUDP, err := net.ListenPacket(netutil.GetUDPNetwork(), addr)
|
2019-05-22 23:16:55 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("cannot start UDP OpenTSDB collector at %q: %s", addr, err)
|
|
|
|
}
|
|
|
|
|
2019-12-13 23:29:14 +01:00
|
|
|
s := &Server{
|
|
|
|
addr: addr,
|
|
|
|
ls: ls,
|
|
|
|
httpServer: httpServer,
|
|
|
|
lnUDP: lnUDP,
|
|
|
|
}
|
|
|
|
s.wg.Add(1)
|
2019-05-22 23:16:55 +02:00
|
|
|
go func() {
|
2019-12-13 23:29:14 +01:00
|
|
|
defer s.wg.Done()
|
2020-02-23 12:35:47 +01:00
|
|
|
serveTelnet(lnTelnet, telnetInsertHandler)
|
2019-12-13 23:29:14 +01:00
|
|
|
logger.Infof("stopped TCP telnet OpenTSDB server at %q", addr)
|
2019-05-22 23:16:55 +02:00
|
|
|
}()
|
2019-12-13 23:29:14 +01:00
|
|
|
s.wg.Add(1)
|
2019-05-22 23:16:55 +02:00
|
|
|
go func() {
|
2019-12-13 23:29:14 +01:00
|
|
|
defer s.wg.Done()
|
|
|
|
httpServer.Wait()
|
|
|
|
// Do not log when httpServer is stopped, since this is logged by the server itself.
|
2019-05-22 23:16:55 +02:00
|
|
|
}()
|
2019-12-13 23:29:14 +01:00
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer s.wg.Done()
|
2020-02-23 12:35:47 +01:00
|
|
|
serveUDP(lnUDP, telnetInsertHandler)
|
2019-12-13 23:29:14 +01:00
|
|
|
logger.Infof("stopped UDP OpenTSDB server at %q", addr)
|
|
|
|
}()
|
|
|
|
return s
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2019-12-13 23:29:14 +01:00
|
|
|
// MustStop stops the server.
|
|
|
|
func (s *Server) MustStop() {
|
|
|
|
// Stop HTTP server. Do not emit log message, since it is emitted by the httpServer.
|
|
|
|
s.httpServer.MustStop()
|
|
|
|
|
|
|
|
logger.Infof("stopping TCP telnet OpenTSDB server at %q...", s.addr)
|
|
|
|
if err := s.ls.stop(); err != nil {
|
|
|
|
logger.Errorf("cannot stop TCP telnet OpenTSDB server: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Infof("stopping UDP OpenTSDB server at %q...", s.addr)
|
|
|
|
if err := s.lnUDP.Close(); err != nil {
|
|
|
|
logger.Errorf("cannot stop UDP OpenTSDB server: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until all the servers are stopped.
|
|
|
|
s.wg.Wait()
|
|
|
|
logger.Infof("TCP and UDP OpenTSDB servers at %q have been stopped", s.addr)
|
|
|
|
}
|
|
|
|
|
2020-02-23 12:35:47 +01:00
|
|
|
func serveTelnet(ln net.Listener, insertHandler func(r io.Reader) error) {
|
2019-05-22 23:16:55 +02:00
|
|
|
for {
|
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
2020-06-30 23:15:34 +02:00
|
|
|
var ne net.Error
|
|
|
|
if errors.As(err, &ne) {
|
2019-05-22 23:16:55 +02:00
|
|
|
if ne.Temporary() {
|
2019-12-13 23:29:14 +01:00
|
|
|
logger.Errorf("opentsdb: temporary error when listening for TCP addr %q: %s", ln.Addr(), err)
|
2019-05-22 23:16:55 +02:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
logger.Fatalf("unrecoverable error when accepting TCP OpenTSDB connections: %s", err)
|
|
|
|
}
|
|
|
|
logger.Fatalf("unexpected error when accepting TCP OpenTSDB connections: %s", err)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
writeRequestsTCP.Inc()
|
|
|
|
if err := insertHandler(c); err != nil {
|
|
|
|
writeErrorsTCP.Inc()
|
|
|
|
logger.Errorf("error in TCP OpenTSDB conn %q<->%q: %s", c.LocalAddr(), c.RemoteAddr(), err)
|
|
|
|
}
|
|
|
|
_ = c.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 12:35:47 +01:00
|
|
|
func serveUDP(ln net.PacketConn, insertHandler func(r io.Reader) error) {
|
2020-12-08 19:49:32 +01:00
|
|
|
gomaxprocs := cgroup.AvailableCPUs()
|
2019-05-22 23:16:55 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < gomaxprocs; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
var bb bytesutil.ByteBuffer
|
|
|
|
bb.B = bytesutil.Resize(bb.B, 64*1024)
|
|
|
|
for {
|
|
|
|
bb.Reset()
|
|
|
|
bb.B = bb.B[:cap(bb.B)]
|
|
|
|
n, addr, err := ln.ReadFrom(bb.B)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorsUDP.Inc()
|
2020-06-30 23:15:34 +02:00
|
|
|
var ne net.Error
|
|
|
|
if errors.As(err, &ne) {
|
2019-05-22 23:16:55 +02:00
|
|
|
if ne.Temporary() {
|
2019-12-13 23:29:14 +01:00
|
|
|
logger.Errorf("opentsdb: temporary error when listening for UDP addr %q: %s", ln.LocalAddr(), err)
|
2019-05-22 23:16:55 +02:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.Errorf("cannot read OpenTSDB UDP data: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bb.B = bb.B[:n]
|
|
|
|
writeRequestsUDP.Inc()
|
|
|
|
if err := insertHandler(bb.NewReader()); err != nil {
|
|
|
|
writeErrorsUDP.Inc()
|
|
|
|
logger.Errorf("error in UDP OpenTSDB conn %q<->%q: %s", ln.LocalAddr(), addr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|