From 13e1515f7daabd08e2206f6abc742bb72e973ceb Mon Sep 17 00:00:00 2001 From: kaiyou Date: Thu, 7 May 2020 12:26:55 +0200 Subject: [PATCH] Split cluster into multiple files --- cluster/cluster.go | 61 --------------------------------------------- cluster/delegate.go | 25 +++++++++++++++++++ cluster/state.go | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 cluster/delegate.go create mode 100644 cluster/state.go diff --git a/cluster/cluster.go b/cluster/cluster.go index 2d4e78f..67be2f8 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -3,11 +3,8 @@ package cluster import ( "crypto/rand" "encoding/base64" - "encoding/json" "fmt" - "io/ioutil" "os" - "path" "time" "github.com/costela/wesher/common" @@ -19,12 +16,6 @@ import ( const KeyLen = 32 -// State keeps track of information needed to rejoin the cluster -type State struct { - ClusterKey []byte - Nodes []common.Node -} - type Cluster struct { LocalName string // used to avoid LocalNode(); should not change ml *memberlist.Memberlist @@ -33,8 +24,6 @@ type Cluster struct { events chan memberlist.NodeEvent } -const statePath = "/var/lib/wesher/state.json" - func New(init bool, clusterKey []byte, bindAddr string, bindPort int, useIPAsName bool) (*Cluster, error) { state := &State{} if !init { @@ -76,25 +65,6 @@ func New(init bool, clusterKey []byte, bindAddr string, bindPort int, useIPAsNam return &cluster, nil } -func (c *Cluster) NotifyConflict(node, other *memberlist.Node) { - logrus.Errorf("node name conflict detected: %s", other.Name) -} - -func (c *Cluster) NodeMeta(limit int) []byte { - encoded, err := c.localNode.Encode(limit) - if err != nil { - logrus.Errorf("failed to encode local node: %s", err) - return nil - } - return encoded -} - -// none of these are used -func (c *Cluster) NotifyMsg([]byte) {} -func (c *Cluster) GetBroadcasts(overhead, limit int) [][]byte { return nil } -func (c *Cluster) LocalState(join bool) []byte { return nil } -func (c *Cluster) MergeRemoteState(buf []byte, join bool) {} - func (c *Cluster) Join(addrs []string) error { if len(addrs) == 0 { for _, n := range c.state.Nodes { @@ -176,34 +146,3 @@ func computeClusterKey(state *State, clusterKey []byte) ([]byte, error) { state.ClusterKey = clusterKey return clusterKey, nil } - -func (c *Cluster) saveState() error { - if err := os.MkdirAll(path.Dir(statePath), 0700); err != nil { - return err - } - - stateOut, err := json.MarshalIndent(c.state, "", " ") - if err != nil { - return err - } - - return ioutil.WriteFile(statePath, stateOut, 0600) -} - -func loadState(cs *State) { - content, err := ioutil.ReadFile(statePath) - if err != nil { - if !os.IsNotExist(err) { - logrus.Warnf("could not open state in %s: %s", statePath, err) - } - return - } - - // avoid partially unmarshalled content by using a temp var - csTmp := &State{} - if err := json.Unmarshal(content, csTmp); err != nil { - logrus.Warnf("could not decode state: %s", err) - } else { - *cs = *csTmp - } -} diff --git a/cluster/delegate.go b/cluster/delegate.go new file mode 100644 index 0000000..b7da044 --- /dev/null +++ b/cluster/delegate.go @@ -0,0 +1,25 @@ +package cluster + +import ( + "github.com/hashicorp/memberlist" + "github.com/sirupsen/logrus" +) + +func (c *Cluster) NotifyConflict(node, other *memberlist.Node) { + logrus.Errorf("node name conflict detected: %s", other.Name) +} + +func (c *Cluster) NodeMeta(limit int) []byte { + encoded, err := c.localNode.Encode(limit) + if err != nil { + logrus.Errorf("failed to encode local node: %s", err) + return nil + } + return encoded +} + +// none of these are used +func (c *Cluster) NotifyMsg([]byte) {} +func (c *Cluster) GetBroadcasts(overhead, limit int) [][]byte { return nil } +func (c *Cluster) LocalState(join bool) []byte { return nil } +func (c *Cluster) MergeRemoteState(buf []byte, join bool) {} diff --git a/cluster/state.go b/cluster/state.go new file mode 100644 index 0000000..df1f2ec --- /dev/null +++ b/cluster/state.go @@ -0,0 +1,50 @@ +package cluster + +import ( + "encoding/json" + "io/ioutil" + "os" + "path" + + "github.com/costela/wesher/common" + "github.com/sirupsen/logrus" +) + +// State keeps track of information needed to rejoin the cluster +type State struct { + ClusterKey []byte + Nodes []common.Node +} + +const statePath = "/var/lib/wesher/state.json" + +func (c *Cluster) saveState() error { + if err := os.MkdirAll(path.Dir(statePath), 0700); err != nil { + return err + } + + stateOut, err := json.MarshalIndent(c.state, "", " ") + if err != nil { + return err + } + + return ioutil.WriteFile(statePath, stateOut, 0600) +} + +func loadState(cs *State) { + content, err := ioutil.ReadFile(statePath) + if err != nil { + if !os.IsNotExist(err) { + logrus.Warnf("could not open state in %s: %s", statePath, err) + } + return + } + + // avoid partially unmarshalled content by using a temp var + csTmp := &State{} + if err := json.Unmarshal(content, csTmp); err != nil { + logrus.Warnf("could not decode state: %s", err) + } else { + *cs = *csTmp + } +}