2015-06-08 19:07:05 +00:00
|
|
|
package snapshotter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-12-25 13:23:22 +00:00
|
|
|
"time"
|
2015-06-08 19:07:05 +00:00
|
|
|
|
2015-12-25 13:23:22 +00:00
|
|
|
"github.com/influxdb/influxdb"
|
2016-01-20 18:23:17 +00:00
|
|
|
"github.com/influxdb/influxdb/services/meta"
|
2015-06-08 19:07:05 +00:00
|
|
|
"github.com/influxdb/influxdb/tsdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MuxHeader is the header byte used for the TCP muxer.
|
|
|
|
const MuxHeader = 3
|
|
|
|
|
|
|
|
// Service manages the listener for the snapshot endpoint.
|
|
|
|
type Service struct {
|
|
|
|
wg sync.WaitGroup
|
|
|
|
err chan error
|
|
|
|
|
2015-12-23 15:48:25 +00:00
|
|
|
MetaClient interface {
|
2015-06-08 19:07:05 +00:00
|
|
|
encoding.BinaryMarshaler
|
2015-12-25 13:23:22 +00:00
|
|
|
Database(name string) (*meta.DatabaseInfo, error)
|
2015-06-08 19:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TSDBStore *tsdb.Store
|
|
|
|
|
|
|
|
Listener net.Listener
|
|
|
|
Logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns a new instance of Service.
|
|
|
|
func NewService() *Service {
|
|
|
|
return &Service{
|
|
|
|
err: make(chan error),
|
|
|
|
Logger: log.New(os.Stderr, "[snapshot] ", log.LstdFlags),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open starts the service.
|
|
|
|
func (s *Service) Open() error {
|
2015-08-12 22:07:12 +00:00
|
|
|
s.Logger.Println("Starting snapshot service")
|
|
|
|
|
2015-06-08 19:07:05 +00:00
|
|
|
s.wg.Add(1)
|
|
|
|
go s.serve()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements the Service interface.
|
|
|
|
func (s *Service) Close() error {
|
|
|
|
if s.Listener != nil {
|
|
|
|
s.Listener.Close()
|
|
|
|
}
|
|
|
|
s.wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:53:40 +00:00
|
|
|
// SetLogger sets the internal logger to the logger passed in.
|
|
|
|
func (s *Service) SetLogger(l *log.Logger) {
|
|
|
|
s.Logger = l
|
|
|
|
}
|
|
|
|
|
2015-06-08 19:07:05 +00:00
|
|
|
// Err returns a channel for fatal out-of-band errors.
|
|
|
|
func (s *Service) Err() <-chan error { return s.err }
|
|
|
|
|
|
|
|
// serve serves snapshot requests from the listener.
|
|
|
|
func (s *Service) serve() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Wait for next connection.
|
|
|
|
conn, err := s.Listener.Accept()
|
|
|
|
if err != nil && strings.Contains(err.Error(), "connection closed") {
|
|
|
|
s.Logger.Println("snapshot listener closed")
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
s.Logger.Println("error accepting snapshot request: ", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle connection in separate goroutine.
|
|
|
|
s.wg.Add(1)
|
|
|
|
go func(conn net.Conn) {
|
|
|
|
defer s.wg.Done()
|
|
|
|
defer conn.Close()
|
|
|
|
if err := s.handleConn(conn); err != nil {
|
|
|
|
s.Logger.Println(err)
|
|
|
|
}
|
|
|
|
}(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleConn processes conn. This is run in a separate goroutine.
|
|
|
|
func (s *Service) handleConn(conn net.Conn) error {
|
2015-12-25 13:23:22 +00:00
|
|
|
r, err := s.readRequest(conn)
|
2015-06-08 19:07:05 +00:00
|
|
|
if err != nil {
|
2015-12-25 13:23:22 +00:00
|
|
|
return fmt.Errorf("read request: %s", err)
|
2015-06-08 19:07:05 +00:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:23:22 +00:00
|
|
|
switch r.Type {
|
|
|
|
case RequestShardBackup:
|
|
|
|
if err := s.TSDBStore.BackupShard(r.ShardID, r.Since, conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case RequestMetastoreBackup:
|
|
|
|
// Retrieve and serialize the current meta data.
|
2016-01-20 18:23:17 +00:00
|
|
|
buf, err := s.MetaClient.MarshalBinary()
|
2015-12-25 13:23:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshal meta: %s", err)
|
|
|
|
}
|
|
|
|
if _, err := conn.Write(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case RequestDatabaseInfo:
|
|
|
|
return s.writeDatabaseInfo(conn, r.Database)
|
|
|
|
case RequestRetentionPolicyInfo:
|
|
|
|
return s.writeRetentionPolicyInfo(conn, r.Database, r.RetentionPolicy)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("request type unknown: %v", r.Type)
|
2015-06-08 19:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:23:22 +00:00
|
|
|
// writeDatabaseInfo will write the relative paths of all shards in the database on
|
|
|
|
// this server into the connection
|
|
|
|
func (s *Service) writeDatabaseInfo(conn net.Conn, database string) error {
|
|
|
|
res := Response{}
|
2016-01-20 18:23:17 +00:00
|
|
|
db, err := s.MetaClient.Database(database)
|
2015-12-25 13:23:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if db == nil {
|
|
|
|
return influxdb.ErrDatabaseNotFound(database)
|
2015-06-08 19:07:05 +00:00
|
|
|
}
|
2015-12-25 13:23:22 +00:00
|
|
|
|
|
|
|
for _, rp := range db.RetentionPolicies {
|
|
|
|
for _, sg := range rp.ShardGroups {
|
|
|
|
for _, sh := range sg.Shards {
|
|
|
|
// ignore if the shard isn't on the server
|
|
|
|
if s.TSDBStore.Shard(sh.ID) == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := s.TSDBStore.ShardRelativePath(sh.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Paths = append(res.Paths, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(conn).Encode(res); err != nil {
|
|
|
|
return fmt.Errorf("encode resonse: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-06-08 19:07:05 +00:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:23:22 +00:00
|
|
|
// writeDatabaseInfo will write the relative paths of all shards in the retention policy on
|
|
|
|
// this server into the connection
|
|
|
|
func (s *Service) writeRetentionPolicyInfo(conn net.Conn, database, retentionPolicy string) error {
|
|
|
|
res := Response{}
|
2016-01-20 18:23:17 +00:00
|
|
|
db, err := s.MetaClient.Database(database)
|
2015-06-08 19:07:05 +00:00
|
|
|
if err != nil {
|
2015-12-25 13:23:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if db == nil {
|
|
|
|
return influxdb.ErrDatabaseNotFound(database)
|
2015-06-08 19:07:05 +00:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:23:22 +00:00
|
|
|
var ret *meta.RetentionPolicyInfo
|
|
|
|
|
|
|
|
for _, rp := range db.RetentionPolicies {
|
|
|
|
if rp.Name == retentionPolicy {
|
|
|
|
ret = &rp
|
|
|
|
break
|
|
|
|
}
|
2015-06-08 19:07:05 +00:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:23:22 +00:00
|
|
|
if ret == nil {
|
|
|
|
return influxdb.ErrRetentionPolicyNotFound(retentionPolicy)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sg := range ret.ShardGroups {
|
|
|
|
for _, sh := range sg.Shards {
|
|
|
|
// ignore if the shard isn't on the server
|
|
|
|
if s.TSDBStore.Shard(sh.ID) == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := s.TSDBStore.ShardRelativePath(sh.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-08 19:07:05 +00:00
|
|
|
|
2015-12-25 13:23:22 +00:00
|
|
|
res.Paths = append(res.Paths, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(conn).Encode(res); err != nil {
|
|
|
|
return fmt.Errorf("encode resonse: %s", err.Error())
|
2015-06-08 19:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-12-25 13:23:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// readRequest Unmarshals a request object from the conn
|
|
|
|
func (s *Service) readRequest(conn net.Conn) (Request, error) {
|
|
|
|
var r Request
|
|
|
|
if err := json.NewDecoder(conn).Decode(&r); err != nil {
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
RequestShardBackup RequestType = iota
|
|
|
|
RequestMetastoreBackup
|
|
|
|
RequestDatabaseInfo
|
|
|
|
RequestRetentionPolicyInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
// Request represents a request for a specific backup or for information
|
|
|
|
// about the shards on this server for a database or retention policy
|
|
|
|
type Request struct {
|
|
|
|
Type RequestType
|
|
|
|
Database string
|
|
|
|
RetentionPolicy string
|
|
|
|
ShardID uint64
|
|
|
|
Since time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response contains the relative paths for all the shards on this server
|
|
|
|
// that are in the requested database or retention policy
|
|
|
|
type Response struct {
|
|
|
|
Paths []string
|
2015-06-08 19:07:05 +00:00
|
|
|
}
|