Move setPeers to raft state

pull/3372/head
Jason Wilder 2015-07-17 12:03:04 -06:00
parent 9e4339753f
commit b86fecfd80
2 changed files with 20 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
"net"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -23,6 +24,7 @@ type raftState interface {
leaderCh() <-chan bool leaderCh() <-chan bool
raftEnabled() bool raftEnabled() bool
sync(index uint64, timeout time.Duration) error sync(index uint64, timeout time.Duration) error
setPeers(addrs []string) error
invalidate() error invalidate() error
close() error close() error
} }
@ -156,6 +158,19 @@ func (r *localRaft) sync(index uint64, timeout time.Duration) error {
} }
} }
// setPeers sets a list of peers in the cluster.
func (r *localRaft) setPeers(addrs []string) error {
a := make([]string, len(addrs))
for i, s := range addrs {
addr, err := net.ResolveTCPAddr("tcp", s)
if err != nil {
return fmt.Errorf("cannot resolve addr: %s, err=%s", s, err)
}
a[i] = addr.String()
}
return r.store.raft.SetPeers(a).Error()
}
func (r *localRaft) leader() string { func (r *localRaft) leader() string {
if r.store.raft == nil { if r.store.raft == nil {
return "" return ""
@ -221,6 +236,10 @@ func (r *remoteRaft) invalidate() error {
return nil return nil
} }
func (r *remoteRaft) setPeers(addrs []string) error {
return nil
}
func (r *remoteRaft) openRaft() error { func (r *remoteRaft) openRaft() error {
go func() { go func() {
for { for {

View File

@ -448,15 +448,7 @@ func (s *Store) LeaderCh() <-chan bool {
// SetPeers sets a list of peers in the cluster. // SetPeers sets a list of peers in the cluster.
func (s *Store) SetPeers(addrs []string) error { func (s *Store) SetPeers(addrs []string) error {
a := make([]string, len(addrs)) return s.raftState.setPeers(addrs)
for i, s := range addrs {
addr, err := net.ResolveTCPAddr("tcp", s)
if err != nil {
return fmt.Errorf("cannot resolve addr: %s, err=%s", s, err)
}
a[i] = addr.String()
}
return s.raft.SetPeers(a).Error()
} }
// AddPeer adds addr to the list of peers in the cluster. // AddPeer adds addr to the list of peers in the cluster.