2015-12-23 15:48:25 +00:00
|
|
|
package influxdb
|
|
|
|
|
2015-12-30 13:15:00 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-01-27 19:50:32 +00:00
|
|
|
"fmt"
|
2016-01-25 23:13:06 +00:00
|
|
|
"io/ioutil"
|
2015-12-30 13:15:00 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-01-25 23:13:06 +00:00
|
|
|
"strconv"
|
2015-12-30 13:15:00 +00:00
|
|
|
)
|
|
|
|
|
2016-01-27 19:50:32 +00:00
|
|
|
const (
|
|
|
|
nodeFile = "node.json"
|
|
|
|
oldNodeFile = "id"
|
|
|
|
peersFilename = "peers.json"
|
|
|
|
)
|
2015-12-23 15:48:25 +00:00
|
|
|
|
|
|
|
type Node struct {
|
2015-12-30 13:15:00 +00:00
|
|
|
path string
|
|
|
|
ID uint64
|
|
|
|
MetaServers []string
|
2015-12-23 15:48:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 23:13:06 +00:00
|
|
|
// LoadNode will load the node information from disk if present
|
2016-02-02 19:10:29 +00:00
|
|
|
func LoadNode(path string, addrs []string) (*Node, error) {
|
2016-01-25 23:13:06 +00:00
|
|
|
// Always check to see if we are upgrading first
|
2016-02-02 19:10:29 +00:00
|
|
|
if err := upgradeNodeFile(path, addrs); err != nil {
|
2016-01-25 23:13:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-30 13:15:00 +00:00
|
|
|
n := &Node{
|
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(filepath.Join(path, nodeFile))
|
2016-01-06 22:34:34 +00:00
|
|
|
if err != nil {
|
2016-01-25 23:13:06 +00:00
|
|
|
return nil, err
|
2015-12-30 13:15:00 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := json.NewDecoder(f).Decode(n); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
2015-12-23 15:48:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 03:08:23 +00:00
|
|
|
// NewNode will return a new node
|
2016-02-02 19:10:29 +00:00
|
|
|
func NewNode(path string, addrs []string) *Node {
|
2016-01-26 03:08:23 +00:00
|
|
|
return &Node{
|
|
|
|
path: path,
|
2016-02-02 19:10:29 +00:00
|
|
|
MetaServers: addrs,
|
2016-01-26 03:08:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 13:15:00 +00:00
|
|
|
// Save will save the node file to disk and replace the existing one if present
|
|
|
|
func (n *Node) Save() error {
|
2016-01-07 20:29:24 +00:00
|
|
|
file := filepath.Join(n.path, nodeFile)
|
|
|
|
tmpFile := file + "tmp"
|
2015-12-30 13:15:00 +00:00
|
|
|
|
2016-01-07 20:29:24 +00:00
|
|
|
f, err := os.Create(tmpFile)
|
2015-12-30 13:15:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-31 05:49:58 +00:00
|
|
|
if err = json.NewEncoder(f).Encode(n); err != nil {
|
|
|
|
f.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = f.Close(); nil != err {
|
2016-01-07 20:29:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Rename(tmpFile, file)
|
2015-12-23 15:48:25 +00:00
|
|
|
}
|
2016-01-25 23:13:06 +00:00
|
|
|
|
2016-02-02 19:10:29 +00:00
|
|
|
// AddMetaServers adds the addrs to the set of MetaServers known to this node.
|
|
|
|
// If an addr already exists, it will not be re-added.
|
|
|
|
func (n *Node) AddMetaServers(addrs []string) {
|
|
|
|
unique := map[string]struct{}{}
|
|
|
|
for _, addr := range n.MetaServers {
|
|
|
|
unique[addr] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
unique[addr] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
metaServers := []string{}
|
|
|
|
for addr := range unique {
|
|
|
|
metaServers = append(metaServers, addr)
|
|
|
|
}
|
|
|
|
n.MetaServers = metaServers
|
|
|
|
}
|
|
|
|
|
|
|
|
func upgradeNodeFile(path string, addrs []string) error {
|
2016-01-25 23:13:06 +00:00
|
|
|
oldFile := filepath.Join(path, oldNodeFile)
|
|
|
|
b, err := ioutil.ReadFile(oldFile)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// We shouldn't have an empty ID file, but if we do, ignore it
|
|
|
|
if len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:50:32 +00:00
|
|
|
peers := []string{}
|
|
|
|
pb, err := ioutil.ReadFile(filepath.Join(path, peersFilename))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(pb, &peers)
|
|
|
|
if len(peers) > 1 {
|
|
|
|
return fmt.Errorf("to upgrade a cluster, please contact support at influxdata")
|
|
|
|
}
|
|
|
|
|
2016-01-25 23:13:06 +00:00
|
|
|
n := &Node{
|
|
|
|
path: path,
|
2016-02-02 19:10:29 +00:00
|
|
|
MetaServers: addrs,
|
2016-01-25 23:13:06 +00:00
|
|
|
}
|
|
|
|
if n.ID, err = strconv.ParseUint(string(b), 10, 64); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := n.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Remove(oldFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|