-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added member pkg for all etcd member manipulation functions
- Loading branch information
Showing
3 changed files
with
146 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package member | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gardener/etcd-backup-restore/pkg/etcdutil" | ||
brtypes "github.com/gardener/etcd-backup-restore/pkg/types" | ||
"github.com/sirupsen/logrus" | ||
"go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes" | ||
) | ||
|
||
func AddMemberAsLearner(logger *logrus.Logger) error { | ||
//Add member as learner to cluster | ||
// memAddCtx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) | ||
// defer cancel() | ||
memberURL := getMemberURL() | ||
if memberURL == "" { | ||
logger.Warn("Could not fetch member URL") | ||
} | ||
for { | ||
//Create etcd client | ||
clientFactory := etcdutil.NewFactory(brtypes.EtcdConnectionConfig{ | ||
Endpoints: []string{"http://etcd-main-peer.default.svc:2380"}, //TODO: use ETCD_ENDPOINT env var passed by druid | ||
InsecureTransport: true, | ||
}) | ||
cli, _ := clientFactory.NewCluster() | ||
memAddCtx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) | ||
_, err2 := cli.MemberAddAsLearner(memAddCtx, []string{memberURL}) | ||
cancel() | ||
if err2 != nil { | ||
logger.Warn("Error adding member as a learner: ", err2) | ||
} | ||
if err2 == nil || strings.Contains(rpctypes.ErrGRPCPeerURLExist.Error(), err2.Error()) { | ||
break | ||
//TODO: why not just return here? | ||
} | ||
logger.Info("Could not as member as learner due to: ", err2) | ||
logger.Info("Trying again in 5 seconds") | ||
cli.Close() | ||
timer := time.NewTimer(5 * time.Second) | ||
<-timer.C | ||
timer.Stop() | ||
} | ||
|
||
logger.Info("Adding member to cluster as learner") | ||
|
||
return nil | ||
} | ||
|
||
func IsMemberInCluster(logger *logrus.Logger) bool { | ||
//Create etcd client | ||
clientFactory := etcdutil.NewFactory(brtypes.EtcdConnectionConfig{ | ||
Endpoints: []string{"http://etcd-main-peer.default.svc:2380"}, //TODO: use ETCD_ENDPOINT env var passed by druid | ||
InsecureTransport: true, //TODO: is it right to use insecure transport? | ||
}) | ||
cli, _ := clientFactory.NewCluster() | ||
logger.Info("Etcd client created") | ||
|
||
// List members in cluster | ||
memListCtx, cancel := context.WithTimeout(context.TODO(), brtypes.DefaultEtcdConnectionTimeout) | ||
etcdMemberList, err := cli.MemberList(memListCtx) | ||
cancel() | ||
if err != nil { | ||
logger.Warn("Could not list any etcd members", err) | ||
return true | ||
} | ||
|
||
for _, y := range etcdMemberList.Members { | ||
if y.Name == os.Getenv("POD_NAME") { | ||
return true | ||
} | ||
} | ||
|
||
cli.Close() | ||
|
||
return false | ||
} | ||
|
||
func getMemberURL() string { | ||
//end := strings.Split(os.Getenv("ETCD_ENDPOINT"), "//") //TODO: use ETCD_ENDPOINT env var passed by druid | ||
memberURL := "http://" + os.Getenv("POD_NAME") + ".etcd-main-peer.default.svc:2380" | ||
//memberURL := end[0] + "//" + os.Getenv("POD_NAME") + "." + end[1] | ||
return memberURL | ||
} | ||
|
||
func PromoteMember(ctx context.Context, logger *logrus.Entry) { | ||
for { //TODO: wise to have an infinite loop? Is there a risk of getting stuck unnecessarily here? | ||
clientFactory := etcdutil.NewFactory(brtypes.EtcdConnectionConfig{ | ||
Endpoints: []string{"http://etcd-main-peer.default.svc:2380"}, //[]string{os.Getenv("ETCD_ENDPOINT")}, //TODO: Pass this via env var | ||
InsecureTransport: true, | ||
}) | ||
cli, _ := clientFactory.NewCluster() | ||
|
||
//List all members in the etcd cluster | ||
//Member URL will appear in the memberlist call response as soon as the member has been added to the cluster | ||
//However, the name of the member will appear only if the member has started | ||
ctx2, cancel := context.WithTimeout(context.TODO(), brtypes.DefaultEtcdConnectionTimeout) | ||
etcdList, memListErr := cli.MemberList(ctx2) | ||
cancel() | ||
|
||
if memListErr != nil { | ||
logger.Info("error listing members: ", memListErr) | ||
continue | ||
} | ||
|
||
//TODO: Simplify logic below | ||
var promoted bool | ||
promoted = false | ||
for _, y := range etcdList.Members { | ||
logger.Info("CHECK: ", y.Name, " : ", os.Getenv("POD_NAME")) | ||
if y.Name == os.Getenv("POD_NAME") { | ||
logger.Info("Promoting member ", y.Name) | ||
ctx2, cancel := context.WithTimeout(context.TODO(), 15*time.Second) | ||
defer cancel() | ||
//Member promote call will succeed only if member is in sync with leader, and will error out otherwise | ||
_, err3 := cli.MemberPromote(ctx2, y.ID) | ||
if err3 == nil || strings.Contains(rpctypes.ErrGRPCMemberNotLearner.Error(), err3.Error()) { | ||
//Exit if member is successfully promoted or if member is not a learner | ||
promoted = true | ||
logger.Info("Member promoted ", y.Name, " : ", y.ID) | ||
break | ||
} | ||
cancel() | ||
} | ||
} | ||
if promoted { | ||
break | ||
} | ||
|
||
//Timer here so that the member promote loop doesn't execute too frequently | ||
timer := time.NewTimer(5 * time.Second) | ||
<-timer.C | ||
timer.Stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters