forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
77 lines (63 loc) · 1.6 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package snapshotter
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"github.com/influxdata/influxdb/services/meta"
"github.com/influxdata/influxdb/tcp"
)
// Client provides an API for the snapshotter service.
type Client struct {
host string
}
// NewClient returns a new *Client.
func NewClient(host string) *Client {
return &Client{host: host}
}
// MetastoreBackup returns a snapshot of the meta store.
func (c *Client) MetastoreBackup() (*meta.Data, error) {
req := &Request{
Type: RequestMetastoreBackup,
}
b, err := c.doRequest(req)
if err != nil {
return nil, err
}
// Check the magic.
magic := binary.BigEndian.Uint64(b[:8])
if magic != BackupMagicHeader {
return nil, errors.New("invalid metadata received")
}
i := 8
// Size of the meta store bytes.
length := int(binary.BigEndian.Uint64(b[i : i+8]))
i += 8
metaBytes := b[i : i+length]
i += int(length)
// Unpack meta data.
var data meta.Data
if err := data.UnmarshalBinary(metaBytes); err != nil {
return nil, fmt.Errorf("unmarshal: %s", err)
}
return &data, nil
}
// doRequest sends a request to the snapshotter service and returns the result.
func (c *Client) doRequest(req *Request) ([]byte, error) {
// Connect to snapshotter service.
conn, err := tcp.Dial("tcp", c.host, MuxHeader)
if err != nil {
return nil, err
}
defer conn.Close()
// Write the request
if err := json.NewEncoder(conn).Encode(req); err != nil {
return nil, fmt.Errorf("encode snapshot request: %s", err)
}
// Read snapshot from the connection
var buf bytes.Buffer
_, err = io.Copy(&buf, conn)
return buf.Bytes(), err
}