Skip to content

Commit

Permalink
remove etcd binding
Browse files Browse the repository at this point in the history
  • Loading branch information
xuriwuyun committed Oct 24, 2023
1 parent fb99104 commit 6e5d0e2
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 214 deletions.
166 changes: 0 additions & 166 deletions lorry/binding/etcd/etcd.go

This file was deleted.

43 changes: 43 additions & 0 deletions lorry/engines/etcd/get_replica_role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package etcd

import (
"context"

"github.com/apecloud/kubeblocks/lorry/dcs"
)

func (mgr *Manager) GetReplicaRole(ctx context.Context, cluster *dcs.Cluster) (string, error) {
etcdResp, err := mgr.etcd.Status(ctx, mgr.endpoint)
if err != nil {
return "", err
}

role := "follower"
switch {
case etcdResp.Leader == etcdResp.Header.MemberId:
role = "leader"
case etcdResp.IsLearner:
role = "learner"
}

return role, nil
}
108 changes: 108 additions & 0 deletions lorry/engines/etcd/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package etcd

import (
"context"
"strconv"
"strings"
"time"

v3 "go.etcd.io/etcd/client/v3"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/apecloud/kubeblocks/lorry/engines"
)

const (
endpoint = "endpoint"

defaultPort = 2379
defaultDialTimeout = 600 * time.Millisecond
)

type Manager struct {
engines.DBManagerBase
etcd *v3.Client
endpoint string
}

var _ engines.DBManager = &Manager{}

func NewManager(properties engines.Properties) (engines.DBManager, error) {
logger := ctrl.Log.WithName("ETCD")

managerBase, err := engines.NewDBManagerBase(logger)
if err != nil {
return nil, err
}

mgr := &Manager{
DBManagerBase: *managerBase,
}

var endpoints []string
endpoint, ok := properties[endpoint]
if ok {
mgr.endpoint = endpoint
endpoints = []string{endpoint}
}

cli, err := v3.New(v3.Config{
Endpoints: endpoints,
DialTimeout: defaultDialTimeout,
})
if err != nil {
return nil, err
}

mgr.etcd = cli
return mgr, nil
}

func (mgr *Manager) IsDBStartupReady() bool {
if mgr.DBStartupReady {
return true
}

ctx, cancel := context.WithTimeout(context.Background(), defaultDialTimeout)
status, err := mgr.etcd.Status(ctx, mgr.endpoint)
cancel()
if err != nil {
mgr.Logger.Info("get etcd status failed", "error", err, "status", status)
}

mgr.DBStartupReady = true
mgr.Logger.Info("DB startup ready")
return true
}

func (mgr *Manager) GetRunningPort() int {
index := strings.Index(mgr.endpoint, ":")
if index < 0 {
return defaultPort
}
port, err := strconv.Atoi(mgr.endpoint[index+1:])
if err != nil {
return defaultPort
}

return port
}
Loading

0 comments on commit 6e5d0e2

Please sign in to comment.