etcdutil

package
v0.0.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

Copyright © 2020 Marvin

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	// DefaultLeaderElectionTTLSecond is the duration that non-leader candidates will
	// wait to force acquire leadership
	DefaultLeaderElectionTTLSecond = 30
	// DefaultMasterLeaderPrefixKey is the dbms-master leader election key prefix
	DefaultMasterLeaderPrefixKey = "/dbms-master/leader"
)
View Source
const (
	// DefaultDialTimeout is the maximum amount of time a dial will wait for a
	// connection to setup. 30s is long enough for most of the network conditions.
	DefaultDialTimeout = 30 * time.Second

	// DefaultRequestTimeout 10s is long enough for most of etcd clusters.
	DefaultRequestTimeout = 60 * time.Second

	// DefaultAutoSyncIntervalDuration is the auto sync interval duration for etcd
	DefaultAutoSyncIntervalDuration = 30 * time.Second
)

Variables

View Source
var NotFoundLeader = errors.New("not found leader")

Functions

func AddMember

func AddMember(client *clientv3.Client, peerAddrs []string) (*clientv3.MemberAddResponse, error)

AddMember adds an etcd member.

func CreateClient

func CreateClient(ctx context.Context, endpoints []string, tlsCfg *tls.Config) (*clientv3.Client, error)

CreateClient creates an etcd client with some default config items.

func DeleteKey

func DeleteKey(client *clientv3.Client, key string, opts ...clientv3.OpOption) (*clientv3.DeleteResponse, error)

DeleteKey delete key-value in the etcd server

func GetClusterLeader added in v0.0.14

func GetClusterLeader(client *clientv3.Client) (string, error)

GetClusterLeader get an etcd leader addr

func GetKey

func GetKey(client *clientv3.Client, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error)

GetKey gets key-value in the etcd server

func ListMembers

func ListMembers(client *clientv3.Client) (*clientv3.MemberListResponse, error)

ListMembers returns a list of internal etcd members.

func MoveLeader added in v0.0.13

func MoveLeader(client *clientv3.Client, id uint64) (*clientv3.MoveLeaderResponse, error)

MoveLeader transfer an etcd member leader by the given id.

func PutKey

func PutKey(client *clientv3.Client, key, value string, opts ...clientv3.OpOption) (*clientv3.PutResponse, error)

PutKey puts key-value in the etcd server

func RemoveMember

func RemoveMember(client *clientv3.Client, id uint64) (*clientv3.MemberRemoveResponse, error)

RemoveMember removes an etcd member by the given id.

func StatusMember added in v0.0.14

func StatusMember(client *clientv3.Client) (*clientv3.StatusResponse, error)

StatusMember get an etcd member status

func TxnKey

func TxnKey(client *clientv3.Client, ops ...clientv3.Op) (*clientv3.TxnResponse, error)

TxnKey put key txn

func WatchKey

func WatchKey(client *clientv3.Client, key string, opts ...clientv3.OpOption) clientv3.WatchChan

WatchKey watch key in the etcd server

Types

type Callbacks

type Callbacks struct {
	// OnStartedLeading is called when a LeaderElector client starts leading
	OnStartedLeading func(ctx context.Context) error
	// OnStoppedLeading is called when a LeaderElector client stops leading
	OnStoppedLeading func(ctx context.Context) error
	// OnNewLeader is called when the client observes a leader that is
	// not the previously observed leader. This includes the first observed
	// leader when the client starts.
	OnNewLeader func(identity string)
}

type Connect

type Connect struct {
	// contains filtered or unexported fields
}

Connect used for watch meta database conn if the database conn information occur change, then modify database conn

func NewServiceConnect

func NewServiceConnect(etcdCli *clientv3.Client, addrRole, logLevel string, dbConnReady *atomic.Bool) *Connect

func (*Connect) Close

func (c *Connect) Close() error

Close used for close service

func (*Connect) Connect

func (c *Connect) Connect(prefixKey string) error

func (*Connect) Watch

func (c *Connect) Watch(prefixKey string) error

Watch used for watch database key change

type Discovery

type Discovery struct {
	// contains filtered or unexported fields
}

func NewServiceDiscovery

func NewServiceDiscovery(etcdCli *clientv3.Client) *Discovery

func (*Discovery) Assign added in v0.0.8

func (d *Discovery) Assign(taskName, hostIP string) (string, error)

Assign used for get free status service instance addr

func (*Discovery) Close

func (d *Discovery) Close() error

Close used for close service

func (*Discovery) Del

func (d *Discovery) Del(key string)

Del used for del service instance

func (*Discovery) Discovery

func (d *Discovery) Discovery(prefixKey string) error

func (*Discovery) Set

func (d *Discovery) Set(key string, val string)

Set used for add service machine

func (*Discovery) Watch

func (d *Discovery) Watch(prefixKey string)

Watch used for 1, watch service register, and initial instance list 2, watch prefix, modify the occurring change's instance

type Election

type Election struct {
	// Lock is the resource that will be used for locking
	EtcdClient *clientv3.Client

	// LeaseDuration is the duration that non-leader candidates will
	// wait to force acquire leadership. This is measured against time of
	// last observed ack.
	//
	// A client needs to wait a full LeaseDuration without observing a change to
	// the record before it can attempt to take over. When all clients are
	// shutdown and a new set of clients are started with different names against
	// the same leader record, they must wait the full LeaseDuration before
	// attempting to acquire the lease. Thes LeaseDuration should be as short as
	// possible (within your tolerance for clock skew rate) to avoid a possible
	// long waits in the scenario.
	//
	// Core clients default this value to 15 seconds.
	LeaseTTL int

	// Callbacks are callbacks that are triggered during certain lifecycle
	// events of the LeaderElector
	Callbacks Callbacks

	// Prefix is the election leader key prefix
	Prefix string

	// Identity is the election instance identity
	Identity string
	// contains filtered or unexported fields
}

Election implements the leader election based on etcd

func NewElection

func NewElection(e *Election) (*Election, error)

func (*Election) Close

func (e *Election) Close()

func (*Election) CurrentIsLeader

func (e *Election) CurrentIsLeader(ctx context.Context) (bool, error)

CurrentIsLeader judge current node whether is leader

func (*Election) Leader

func (e *Election) Leader(ctx context.Context) (string, error)

func (*Election) Run

func (e *Election) Run(ctx context.Context) (errs []error)

type Embed

type Embed interface {
	Init(opts ...configutil.MasterOption) (err error)
	Run() (err error)
	Join() (err error)
	GetConfig() *configutil.MasterOptions
	Close()
}

Embed etcd server interface

func NewETCDServer

func NewETCDServer() Embed

type Etcd

type Etcd struct {
	MasterOptions *configutil.MasterOptions
	Srv           *embed.Etcd
	Config        *embed.Config
}

func (*Etcd) Close

func (e *Etcd) Close()

func (*Etcd) GetConfig

func (e *Etcd) GetConfig() *configutil.MasterOptions

GetConfig used for return master finally config

func (*Etcd) Init

func (e *Etcd) Init(opts ...configutil.MasterOption) (err error)

Init inits embed etcd start config

func (*Etcd) Join

func (e *Etcd) Join() (err error)

Join joins an embedded etcd server node prepareJoinEtcd prepares config needed to join an existing cluster. learn from https://github.com/pingcap/pd/blob/37efcb05f397f26c70cda8dd44acaa3061c92159/server/join/join.go#L44.

when setting `initial-cluster` explicitly to bootstrap a new cluster: - if local persistent data exist, just restart the previous cluster (in fact, it's not bootstrapping). - if local persistent data not exist, just bootstrap the cluster as a new cluster.

when setting `join` to join an existing cluster (without `initial-cluster` set): - if local persistent data exists (in fact, it's not join):

  • just restart if `member` already exists (already joined before)
  • read `initial-cluster` back from local persistent data to restart (just like bootstrapping)

- if local persistent data not exist:

  1. fetch member list from the cluster to check if we can join now.
  2. call `member add` to add the member info into the cluster.
  3. generate config for join (`initial-cluster` and `initial-cluster-state`).
  4. save `initial-cluster` in local persistent data for later restarting.

NOTE: A member can't join to another cluster after it has joined a previous one.

func (*Etcd) Run

func (e *Etcd) Run() (err error)

Run starts an embedded etcd server.

type Instance added in v0.0.8

type Instance struct {
	Addr string
	// the Role column represent the node role
	Role string
	// the State column represent the node state, options: BOUND、FREE
	State string
	// the TaskName column represent the node bound task
	TaskName string
}

Instance register service information, used for etcd key -> value

func (*Instance) String added in v0.0.8

func (i *Instance) String() string

type Register

type Register struct {
	// contains filtered or unexported fields
}

func NewServiceRegister

func NewServiceRegister(etcdCli *clientv3.Client, addr, key, value string, keepaliveTTL int64) *Register

func (*Register) Register

func (r *Register) Register(ctx context.Context) (err error)

func (*Register) Revoke

func (r *Register) Revoke() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL