Feature/410/expose raft cluster api (#501)

* Added an endpoint to expose raft cluster details
This commit is contained in:
apratt3377 2018-09-11 15:05:42 -04:00 committed by Samer Falah
parent f13b6aad4f
commit 0af7ad0664
5 changed files with 32 additions and 22 deletions

View File

@ -628,7 +628,11 @@ web3._extend({
new web3._extend.Property({
name: 'leader',
getter: 'raft_leader'
})
}),
new web3._extend.Property({
name: 'cluster',
getter: 'raft_cluster'
}),
]
})
`

View File

@ -35,5 +35,10 @@ func (s *PublicRaftAPI) Leader() (string, error) {
if nil != err {
return "", err
}
return addr.nodeId.String(), nil
return addr.NodeId.String(), nil
}
func (s *PublicRaftAPI) Cluster() []*Address {
nodeInfo := s.raftService.raftProtocolManager.NodeInfo()
return append(nodeInfo.PeerAddresses, nodeInfo.Address)
}

View File

@ -624,17 +624,17 @@ func (pm *ProtocolManager) entriesToApply(allEntries []raftpb.Entry) (entriesToA
}
func raftUrl(address *Address) string {
return fmt.Sprintf("http://%s:%d", address.ip, address.raftPort)
return fmt.Sprintf("http://%s:%d", address.Ip, address.RaftPort)
}
func (pm *ProtocolManager) addPeer(address *Address) {
pm.mu.Lock()
defer pm.mu.Unlock()
raftId := address.raftId
raftId := address.RaftId
// Add P2P connection:
p2pNode := discover.NewNode(address.nodeId, address.ip, 0, uint16(address.p2pPort))
p2pNode := discover.NewNode(address.NodeId, address.Ip, 0, uint16(address.P2pPort))
pm.p2pServer.AddPeer(p2pNode)
// Add raft transport connection:

View File

@ -5,28 +5,29 @@ import (
"net"
"fmt"
"log"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/rlp"
"log"
)
// Serializable information about a Peer. Sufficient to build `etcdRaft.Peer`
// or `discover.Node`.
type Address struct {
raftId uint16
nodeId discover.NodeID
ip net.IP
p2pPort uint16
raftPort uint16
RaftId uint16 `json:"raftId"`
NodeId discover.NodeID `json:"nodeId"`
Ip net.IP `json:"ip"`
P2pPort uint16 `json:"p2pPort"`
RaftPort uint16 `json:"raftPort"`
}
func newAddress(raftId uint16, raftPort uint16, node *discover.Node) *Address {
return &Address{
raftId: raftId,
nodeId: node.ID,
ip: node.IP,
p2pPort: node.TCP,
raftPort: raftPort,
RaftId: raftId,
NodeId: node.ID,
Ip: node.IP,
P2pPort: node.TCP,
RaftPort: raftPort,
}
}
@ -37,7 +38,7 @@ type Peer struct {
}
func (addr *Address) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, []interface{}{addr.raftId, addr.nodeId, addr.ip, addr.p2pPort, addr.raftPort})
return rlp.Encode(w, []interface{}{addr.RaftId, addr.NodeId, addr.Ip, addr.P2pPort, addr.RaftPort})
}
func (addr *Address) DecodeRLP(s *rlp.Stream) error {
@ -53,7 +54,7 @@ func (addr *Address) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode(&temp); err != nil {
return err
} else {
addr.raftId, addr.nodeId, addr.ip, addr.p2pPort, addr.raftPort = temp.RaftId, temp.NodeId, temp.Ip, temp.P2pPort, temp.RaftPort
addr.RaftId, addr.NodeId, addr.Ip, addr.P2pPort, addr.RaftPort = temp.RaftId, temp.NodeId, temp.Ip, temp.P2pPort, temp.RaftPort
return nil
}
}

View File

@ -30,7 +30,7 @@ type ByRaftId []Address
func (a ByRaftId) Len() int { return len(a) }
func (a ByRaftId) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRaftId) Less(i, j int) bool { return a[i].raftId < a[j].raftId }
func (a ByRaftId) Less(i, j int) bool { return a[i].RaftId < a[j].RaftId }
func (pm *ProtocolManager) buildSnapshot() *Snapshot {
pm.mu.RLock()
@ -140,17 +140,17 @@ func (pm *ProtocolManager) updateClusterMembership(newConfState raftpb.ConfState
for _, tempAddress := range addresses {
address := tempAddress // Allocate separately on the heap for each iteration.
if address.raftId == pm.raftId {
if address.RaftId == pm.raftId {
// If we're a newcomer to an existing cluster, this is where we learn
// our own Address.
pm.setLocalAddress(&address)
} else {
pm.mu.RLock()
existingPeer := pm.peers[address.raftId]
existingPeer := pm.peers[address.RaftId]
pm.mu.RUnlock()
if existingPeer == nil {
log.Info("adding new raft peer", "raft id", address.raftId)
log.Info("adding new raft peer", "raft id", address.RaftId)
pm.addPeer(&address)
}
}