1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-05 12:45:38 +01:00

Check aliveness of all nodes in a single etcd query

Signed-off-by: Oshank Kumar <okumar@redhat.com>
This commit is contained in:
Oshank Kumar
2019-03-05 16:57:29 +05:30
committed by Kaushal M
parent 994aaa0489
commit 73f5bbdeef
2 changed files with 49 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package store
import (
"context"
"os"
"path"
"strconv"
"time"
@@ -56,6 +57,52 @@ func (s *GDStore) IsNodeAlive(peerID interface{}) (int, bool) {
return 0, false
}
// GetAliveNodes will return a map of all alive nodes. It will contain
// peerID and corresponding process id. It uses single etcd query
// to get all alive nodes.
func (s *GDStore) GetAliveNodes(ctx context.Context) map[string]int {
peerStatus := map[string]int{}
resp, err := s.Get(ctx, LivenessKeyPrefix, clientv3.WithPrefix())
if err != nil {
return peerStatus
}
for _, kv := range resp.Kvs {
peerID := path.Base(string(kv.Key))
pid, err := strconv.Atoi(string(kv.Value))
if err == nil {
peerStatus[peerID] = pid
}
}
return peerStatus
}
// AreNodesAlive returns true if all given nodes are alive.
func (s *GDStore) AreNodesAlive(ctx context.Context, peerIDs ...uuid.UUID) bool {
var nodeIDs []string
for _, peerID := range peerIDs {
nodeIDs = append(nodeIDs, peerID.String())
}
peerStatus := s.GetAliveNodes(ctx)
if len(nodeIDs) > len(peerStatus) {
return false
}
for _, nodeID := range nodeIDs {
if _, ok := peerStatus[nodeID]; !ok {
return false
}
}
return true
}
func (s *GDStore) publishLiveness() error {
// publish liveness of this instance into the store
key := LivenessKeyPrefix + gdctx.MyUUID.String()

View File

@@ -124,11 +124,8 @@ func (t *Txn) removeContextData() {
}
func (t *Txn) checkAlive() error {
for _, node := range t.Nodes {
// TODO: Using prefixed query, get all alive nodes in a single etcd query
if _, online := store.Store.IsNodeAlive(node); !online {
return fmt.Errorf("node %s is probably down", node.String())
}
if online := store.Store.AreNodesAlive(context.Background(), t.Nodes...); !online {
return errors.New("probably some of the nodes are down")
}
return nil
}