cluster

package
v0.0.0-...-c9c6fa8 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 39 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrResultNotReady = fmt.Errorf("remote call result not ready")

Functions

func CallFuncAs

func CallFuncAs[T any](c ICluster, fc *FuncSpec) (T, error)

func GetResultAs

func GetResultAs[T any](fs *FuncSpec) (T, error)

Types

type Caller

type Caller interface {
	OnStartedLeading()
	OnStoppedLeading()
	OnStartedFollowing(leaderName string)
	OnStoppedFollowing()
	MasterCall()
	SlaverCall(leaderName string)
}

type Cluster

type Cluster struct {
	Redis redisv2.RedisClient `autowired:"" conditional_on_property:"default.cluster.mode=redis"` // redis
	// contains filtered or unexported fields
}

func NewCluster

func NewCluster(config Config, options ...ClusterOption) (*Cluster, error)

func NewClusterWithArgs

func NewClusterWithArgs(config Config, options ...ClusterOption) (*Cluster, error)

func (*Cluster) AddJobTracker

func (c *Cluster) AddJobTracker(v JobTracker) error

func (*Cluster) CallFunc deprecated

func (c *Cluster) CallFunc(f *FuncSpec) (interface{}, error)

Deprecated: Use cluster.CallFuncAs[T] instead to avoid deserialization failures with remote calls.

func (*Cluster) Close

func (c *Cluster) Close()

func (*Cluster) GetAliveNodeCount

func (c *Cluster) GetAliveNodeCount() int

func (*Cluster) GetAliveNodeNames

func (c *Cluster) GetAliveNodeNames() (aliveNames []string)

func (*Cluster) GetAllNodeCount

func (c *Cluster) GetAllNodeCount() int

func (*Cluster) GetAllNodeNames

func (c *Cluster) GetAllNodeNames() (allNames []string)

func (*Cluster) GetGRPCClient

func (c *Cluster) GetGRPCClient(nodeName string) (grpc.ClientConnInterface, error)

func (*Cluster) GetLeaderName

func (c *Cluster) GetLeaderName() string

func (*Cluster) GetLeaderNode

func (c *Cluster) GetLeaderNode() *Node

func (*Cluster) GetLostNodeNames

func (c *Cluster) GetLostNodeNames() (lostNames []string)

func (*Cluster) GetMyAddress

func (c *Cluster) GetMyAddress() string

func (*Cluster) GetMyName

func (c *Cluster) GetMyName() string

func (*Cluster) GetMyNode

func (c *Cluster) GetMyNode() *Node

func (*Cluster) GetMyTerm

func (c *Cluster) GetMyTerm() int

func (*Cluster) GetNodeByName

func (c *Cluster) GetNodeByName(name string) (node *Node)

func (*Cluster) IsCandidate

func (c *Cluster) IsCandidate() bool

func (*Cluster) IsClose

func (c *Cluster) IsClose() bool

func (*Cluster) IsClosed

func (c *Cluster) IsClosed() bool

func (*Cluster) IsEnable

func (c *Cluster) IsEnable() bool

func (*Cluster) IsFollower

func (c *Cluster) IsFollower() bool

func (*Cluster) IsLeader

func (c *Cluster) IsLeader() bool

func (*Cluster) IsReady

func (c *Cluster) IsReady() bool

func (*Cluster) Name

func (c *Cluster) Name() string

func (*Cluster) RegisterFunc

func (c *Cluster) RegisterFunc(funcName string, fn func(data interface{}) (interface{}, error))

func (*Cluster) RegisterGRPCService

func (c *Cluster) RegisterGRPCService(sd *grpc.ServiceDesc, ss interface{}) error

func (*Cluster) RemoveJobTracker

func (c *Cluster) RemoveJobTracker(v JobTracker)

func (*Cluster) Start

func (c *Cluster) Start() error

type ClusterOption

type ClusterOption func(*Cluster)

func WithClientStreamInterceptors

func WithClientStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) ClusterOption

WithClientStreamInterceptors appends user gRPC stream interceptors to cluster node clients, after the built-in traceId interceptor.

func WithClientUnaryInterceptors

func WithClientUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) ClusterOption

WithClientUnaryInterceptors appends user gRPC unary interceptors to cluster node clients, after the built-in traceId interceptor.

func WithLogger

func WithLogger(logger logger.ILog) ClusterOption

WithLogger sets the logger used by the cluster.

func WithServerStreamInterceptors

func WithServerStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) ClusterOption

WithServerStreamInterceptors appends user gRPC stream interceptors to the cluster server, after the built-in traceId interceptor.

func WithServerUnaryInterceptors

func WithServerUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) ClusterOption

WithServerUnaryInterceptors appends user gRPC unary interceptors to the cluster server, after the built-in traceId interceptor.

type Config

type Config struct {
	Mode       string        `yaml:"mode" default:"cluster" json:"mode"`
	Timeout    time.Duration `yaml:"timeout" default:"5s" json:"timeout"`
	Enable     string        `yaml:"enable" default:"true" json:"enable"`
	TraceIDKey string        `yaml:"traceIdKey" default:"X-Request-ID" json:"traceIdKey"`
	TLS        TLSConfig     `yaml:"tls" json:"tls"`
	Nodes      []*struct {
		Name  string
		IP    string
		Port  int
		Local bool
	} `yaml:"nodes" json:"nodes"`
	RedisDiscovery    RedisDiscovery    `yaml:"redisDiscovery" json:"redisDiscovery"`
	ReplicasDiscovery ReplicasDiscovery `yaml:"replicasDiscovery" json:"replicasDiscovery"`
}

type DefaultCaller

type DefaultCaller struct {
}

func (*DefaultCaller) MasterCall

func (dc *DefaultCaller) MasterCall()

func (*DefaultCaller) OnStartedFollowing

func (dc *DefaultCaller) OnStartedFollowing(leaderName string)

func (*DefaultCaller) OnStartedLeading

func (dc *DefaultCaller) OnStartedLeading()

func (*DefaultCaller) OnStoppedFollowing

func (dc *DefaultCaller) OnStoppedFollowing()

func (*DefaultCaller) OnStoppedLeading

func (dc *DefaultCaller) OnStoppedLeading()

func (*DefaultCaller) SlaverCall

func (dc *DefaultCaller) SlaverCall(leaderName string)

type DefaultJobTracker

type DefaultJobTracker struct {
	Interval int
	Blocking bool
	// contains filtered or unexported fields
}

func NewDefaultJobTracker

func NewDefaultJobTracker(interval int, caller ...Caller) *DefaultJobTracker

func NewJobTracker

func NewJobTracker(config JobTrackerConfig, caller ...Caller) *DefaultJobTracker

NewJobTracker creates a tracker with explicit scheduling options.

func (*DefaultJobTracker) Close

func (t *DefaultJobTracker) Close()

func (*DefaultJobTracker) Name

func (t *DefaultJobTracker) Name() string

func (*DefaultJobTracker) OnStartedFollowing

func (t *DefaultJobTracker) OnStartedFollowing(leaderName string)

func (*DefaultJobTracker) OnStartedLeading

func (t *DefaultJobTracker) OnStartedLeading()

func (*DefaultJobTracker) OnStoppedFollowing

func (t *DefaultJobTracker) OnStoppedFollowing()

func (*DefaultJobTracker) OnStoppedLeading

func (t *DefaultJobTracker) OnStoppedLeading()

type FuncSpec

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

func NewAsyncFuncSpec

func NewAsyncFuncSpec(nodeName, funcName string, param interface{}, timeout time.Duration) *FuncSpec

NewAsyncFuncSpec 异步调用,timeout为超时时间

func NewFuncSpec

func NewFuncSpec(nodeName, funcName string, param interface{}, timeout time.Duration) *FuncSpec

NewFuncSpec 同步调用,timeout是同步超时时间

func (*FuncSpec) GetResult deprecated

func (fs *FuncSpec) GetResult() (interface{}, error)

Deprecated: Use GetResultAs[T] instead to avoid deserialization failures with remote calls.

func (*FuncSpec) GetTraceId

func (fs *FuncSpec) GetTraceId() string

func (*FuncSpec) IgnoreNotReady

func (fs *FuncSpec) IgnoreNotReady() *FuncSpec

func (*FuncSpec) SetTraceId

func (fs *FuncSpec) SetTraceId(traceId string) *FuncSpec

type ICluster

type ICluster interface {
	Name() string                                                                 // 名称
	Start() error                                                                 // 启动
	Close()                                                                       // 关闭
	IsClose() bool                                                                // 集群是否关闭
	IsReady() bool                                                                // 集群是否就绪
	IsLeader() bool                                                               // 当前节点是否是领导人
	IsCandidate() bool                                                            // 当前节点是否是候选人
	IsFollower() bool                                                             // 当前节点是否是群众
	GetLeaderNode() *Node                                                         // 获取当前的主节点
	GetLeaderName() string                                                        // 获取leader名称
	GetMyNode() *Node                                                             // 获取当前本节点
	GetNodeByName(name string) *Node                                              // 根据名称获取节点
	GetMyAddress() string                                                         // 获取当前节点通信地址
	GetMyName() string                                                            // 获得当前本节点名称
	GetMyTerm() int                                                               // 获取当前本节点任期
	GetAllNodeNames() []string                                                    // 获取所有节点名称
	GetAllNodeCount() int                                                         // 获取所有节点的数量
	GetAliveNodeNames() []string                                                  // 获取所有活着的节点名称
	GetAliveNodeCount() int                                                       // 获取在线节点数量
	GetLostNodeNames() []string                                                   // 获取所有失联节点名称
	AddJobTracker(v JobTracker) error                                             // add scheduler
	RemoveJobTracker(v JobTracker)                                                // remove scheduler
	RegisterFunc(funcName string, fn func(data interface{}) (interface{}, error)) // registerFunc
	RegisterGRPCService(sd *grpc.ServiceDesc, ss interface{}) error               // registerGRPCService
	GetGRPCClient(nodeName string) (grpc.ClientConnInterface, error)              // getGRPCClient
	// Deprecated: Use cluster.CallFuncAs[T] instead to avoid deserialization failures with remote calls.
	CallFunc(fc *FuncSpec) (interface{}, error) // callFunc
}

type JobTracker

type JobTracker interface {
	Name() string
	// OnStartedLeading is called when a LeaderElector client starts leading
	OnStartedLeading()
	// OnStoppedLeading is called when a LeaderElector client stops leading
	OnStoppedLeading()
	// OnStartedFollowing is called when a LeaderElector client starts following
	// leaderName is the name of leader
	OnStartedFollowing(leaderName string)
	// OnStartedFollowing is called when a LeaderElector client stops following
	OnStoppedFollowing()
}

type JobTrackerConfig

type JobTrackerConfig struct {
	Interval int
	// Blocking runs MasterCall and SlaverCall synchronously in the scheduler
	// goroutine. A slow call delays the next tick. The default is false, which
	// keeps the existing asynchronous behavior.
	Blocking bool
}

JobTrackerConfig configures DefaultJobTracker.

type Node

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

type NodeInfo

type NodeInfo struct {
	Name      string `json:"name"`
	Address   string `json:"address"`
	Timestamp int64  `json:"timestamp"`
}

NodeInfo 节点信息结构

type RedisDiscovery

type RedisDiscovery struct {
	BeanName            string        `yaml:"beanName"`                          // 如果为空,则Ioc分配
	DataPath            string        `yaml:"dataPath"`                          // redis key 前缀
	Port                int           `yaml:"port" default:"8081"`               // 节点通信端口
	ElectionInterval    time.Duration `yaml:"electionInterval" default:"15s"`    //多久进行一次选主/续约
	ElectionPeriod      time.Duration `yaml:"electionPeriod" default:"30s"`      //选主/续约后有效租期时间
	SyncLeaderInterval  time.Duration `yaml:"syncLeaderInterval" default:"10s"`  //多久同步一次leader
	NodeRegisterTTL     time.Duration `yaml:"nodeRegisterTTL" default:"60s"`     // 节点注册信息的过期时间
	NodeSyncInterval    time.Duration `yaml:"nodeSyncInterval" default:"30s"`    // 多久同步一次节点信息
	NodeHeartbeatPeriod time.Duration `yaml:"nodeHeartbeatPeriod" default:"20s"` // 节点心跳续约周期
}

type ReplicasDiscovery

type ReplicasDiscovery struct {
	DomainPatten string        `yaml:"domainPatten"`
	Port         int           `yaml:"port" default:"8081"`
	PollInterval time.Duration `yaml:"pollInterval" default:"30s"` // HPA 支持:轮询 headless service DNS 的间隔
}

type TLSConfig

type TLSConfig struct {
	Enabled  bool   `yaml:"enabled" json:"enabled"`
	CertFile string `yaml:"certFile" json:"certFile"`
	KeyFile  string `yaml:"keyFile" json:"keyFile"`
	CAFile   string `yaml:"caFile" json:"caFile"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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