redis

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

redisutil

Documentation

Overview

Copyright 2021 kubernetes-app Solutions.

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.

Copyright 2021 kubernetes-app Solutions.

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.

Copyright 2021 kubernetes-app Solutions.

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.

Copyright 2021 kubernetes-app Solutions.

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.

Copyright 2021 kubernetes-app Solutions.

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 (

	// ResetHard HARD mode for RESET command
	ResetHard = "HARD"
	// ResetSoft SOFT mode for RESET command
	ResetSoft = "SOFT"
)
View Source
const (
	// RedisLinkStateConnected redis connection status connected
	RedisLinkStateConnected = "connected"
	// RedisLinkStateDisconnected redis connection status disconnected
	RedisLinkStateDisconnected = "disconnected"
)
View Source
const (
	// NodeStatusPFail Node is in PFAIL state. Not reachable for the node you are contacting, but still logically reachable
	NodeStatusPFail = "fail?"
	// NodeStatusFail Node is in FAIL state. It was not reachable for multiple nodes that promoted the PFAIL state to FAIL
	NodeStatusFail = "fail"
	// NodeStatusHandshake Untrusted node, we are handshaking.
	NodeStatusHandshake = "handshake"
	// NodeStatusNoAddr No address known for this node
	NodeStatusNoAddr = "noaddr"
	// NodeStatusNoFlags no flags at all
	NodeStatusNoFlags = "noflags"
)
View Source
const (
	// DefaultRedisPort define the default Redis Port
	DefaultRedisPort = "6379"
	// RedisMasterRole redis role master
	RedisMasterRole string = "master"
	// RedisSlaveRole redis role slave
	RedisSlaveRole string = "slave"
	// RedisStandaloneRole redis role standalone
	RedisStandaloneRole string = "standalone"
	// RedisNoneRole redis none role
	RedisNoneRole string = "none"
)
View Source
const (
	// HashMaxSlots Numbers or Redis slots used for Key hashing
	HashMaxSlots = 16383
)

Variables

View Source
var IsMasterWithNoSlot = func(n *Node) bool {
	if (n.GetRole() == RedisMasterRole) && (n.TotalSlots() == 0) {
		return true
	}
	return false
}

IsMasterWithNoSlot anonymous function for searching Master Node with no slot

View Source
var IsMasterWithSlot = func(n *Node) bool {
	if (n.GetRole() == RedisMasterRole) && (n.TotalSlots() > 0) {
		return true
	}
	return false
}

IsMasterWithSlot anonymous function for searching Master Node withslot

View Source
var IsSlave = func(n *Node) bool {
	return n.GetRole() == RedisSlaveRole
}

IsSlave anonymous function for searching Slave Node

Functions

func Contains

func Contains(s []Slot, e Slot) bool

Contains returns true if a node slice contains a node

func DecodeClusterInfos

func DecodeClusterInfos(input *string) *map[string]string

DecodeClusterInfos decode from the cmd output the Redis nodes info. Second argument is the node on which we are connected to request info

func DecodeSlotRange

func DecodeSlotRange(str string) ([]Slot, *ImportingSlot, *MigratingSlot, error)

DecodeSlotRange decode from a string a RangeSlot

each entry can have 4 representations:
     * single slot: ex: 42
     * slot range: ex: 42-52
     * migrating slot: ex: [42->-67ed2db8d677e59ec4a4cefb06858cf2a1a89fa1]
     * importing slot: ex: [42-<-67ed2db8d677e59ec4a4cefb06858cf2a1a89fa1]

func IsInconsistentError

func IsInconsistentError(err error) bool

IsInconsistentError eturns true if the error is due to cluster inconsistencies

func IsNodeNotFoundedError

func IsNodeNotFoundedError(err error) bool

IsNodeNotFoundedError returns true if the current error is a NodeNotFoundedError

func IsPartialError

func IsPartialError(err error) bool

IsPartialError returns true if the error is due to partial data recovery

func LessByID

func LessByID(n1, n2 *Node) bool

LessByID compare 2 Nodes with there ID

func MoreByID

func MoreByID(n1, n2 *Node) bool

MoreByID compare 2 Nodes with there ID

func NewClient

func NewClient(addr, password string) *redis.Client

func NewClusterClient

func NewClusterClient(addrs []string, password string) *redis.ClusterClient

func SetRedisConfig

func SetRedisConfig(ctx context.Context, rc *redis.Client, newConfig map[string]string) error

Types

type Admin

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

Admin wraps redis cluster admin logic

func (*Admin) CloseClient

func (a *Admin) CloseClient()

Close used to close all possible resources instantiate by the Admin

func (*Admin) CloseClusterClient

func (a *Admin) CloseClusterClient()

CloseClusterClient used to close all possible resources instantiate by the Admin

func (*Admin) GetClusterInfos

func (a *Admin) GetClusterInfos() (*map[string]string, error)

GetClusterInfos return the Nodes infos for all nodes

func (*Admin) GetClusterNodes

func (a *Admin) GetClusterNodes() (*Nodes, error)

func (*Admin) GetHashMaxSlot

func (a *Admin) GetHashMaxSlot() Slot

GetHashMaxSlot get the max slot value

func (*Admin) SetConfigIfNeed

func (a *Admin) SetConfigIfNeed(newConfig map[string]string) error

SetConfigIfNeed set redis config

func (*Admin) UpdateMasterConfig

func (a *Admin) UpdateMasterConfig(newConfig map[string]string) error

UpdateMasterConfig set redis master config

func (*Admin) UpdateSlaveConfig

func (a *Admin) UpdateSlaveConfig(newConfig map[string]string) error

SetConfigIfNeed set redis config

type AdminInterface

type AdminInterface interface {
	// Connections returns the connection map of all clients
	// Connections() *redis.Client
	// CloseClient the admin connections
	CloseClient()
	// CloseClusterClient the admin connections
	CloseClusterClient()
	// GetClusterInfos get node infos for all nodes
	GetClusterInfos() (*map[string]string, error)
	// GetClusterNodes get node infos for all nodes
	GetClusterNodes() (*Nodes, error)
	// SetConfigIfNeed set redis config
	SetConfigIfNeed(newConfig map[string]string) error
	// GetHashMaxSlot get the max slot value
	GetHashMaxSlot() Slot
}

AdminInterface redis cluster admin interface

func NewAdmin

func NewAdmin(addrs []string, password string) AdminInterface

NewAdmin returns new AdminInterface instance at the same time it connects to all Redis Nodes thanks to the addrs list

type ClusterInfosError

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

ClusterInfosError error type for redis cluster infos access

func NewClusterInfosError

func NewClusterInfosError() ClusterInfosError

NewClusterInfosError returns an instance of cluster infos error

func (ClusterInfosError) Error

func (e ClusterInfosError) Error() string

Error error string

func (ClusterInfosError) Inconsistent

func (e ClusterInfosError) Inconsistent() bool

Inconsistent true if the nodes do not agree with each other

func (ClusterInfosError) Partial

func (e ClusterInfosError) Partial() bool

Partial true if the some nodes of the cluster didn't answer

type ClusterStatus

type ClusterStatus string

ClusterStatus Redis Cluster status

const (
	// ClusterStatusOK ClusterStatus OK
	ClusterStatusOK ClusterStatus = "OK"
	// ClusterStatusKO ClusterStatus KO
	ClusterStatusKO ClusterStatus = "KO"
	// ClusterStatusScaling ClusterStatus Scaling
	ClusterStatusScaling ClusterStatus = "Scaling"
	// ClusterStatusCalculatingRebalancing ClusterStatus Rebalancing
	ClusterStatusCalculatingRebalancing ClusterStatus = "Calculating Rebalancing"
	// ClusterStatusRebalancing ClusterStatus Rebalancing
	ClusterStatusRebalancing ClusterStatus = "Rebalancing"
	// ClusterStatusRollingUpdate ClusterStatus RollingUpdate
	ClusterStatusRollingUpdate ClusterStatus = "RollingUpdate"
)

type Error

type Error string

Error used to represent an error

func (Error) Error

func (e Error) Error() string

type FindNodeFunc

type FindNodeFunc func(node *Node) bool

FindNodeFunc function for finding a Node it is use as input for GetNodeByFunc and GetNodesByFunc

type ImportingSlot

type ImportingSlot struct {
	SlotID     Slot   `json:"slot"`
	FromNodeID string `json:"fromNodeId"`
}

ImportingSlot represents an importing slot (slot + importing from node id)

func (ImportingSlot) String

func (s ImportingSlot) String() string

String string representation of an importing slot

type MigratingSlot

type MigratingSlot struct {
	SlotID   Slot   `json:"slot"`
	ToNodeID string `json:"toNodeId"`
}

MigratingSlot represents a migrating slot (slot + migrating to node id)

func (MigratingSlot) String

func (s MigratingSlot) String() string

String string representation of a migratting slot

type Node

type Node struct {
	ID              string
	IP              string
	Port            string
	Role            string
	LinkState       string
	MasterReferent  string
	FailStatus      []string
	PingSent        int64
	PongRecv        int64
	ConfigEpoch     int64
	Slots           []Slot
	MigratingSlots  map[Slot]string
	ImportingSlots  map[Slot]string
	ServerStartTime time.Time

	Pod *corev1.Pod
}

Node Represent a Redis Node

func NewDefaultNode

func NewDefaultNode() *Node

NewDefaultNode builds and returns new defaultNode instance

func NewNode

func NewNode(id, ip string, pod *corev1.Pod) *Node

NewNode builds and returns new Node instance

func (*Node) Clear

func (n *Node) Clear()

Clear used to clear possible ressources attach to the current Node

func (*Node) GetRole

func (n *Node) GetRole() string

GetRole return the Redis role

func (*Node) HasStatus

func (n *Node) HasStatus(flag string) bool

HasStatus returns true if the node has the provided fail status flag

func (*Node) IPPort

func (n *Node) IPPort() string

IPPort returns join Ip Port string

func (*Node) SetFailureStatus

func (n *Node) SetFailureStatus(flags string)

SetFailureStatus set from inputs flags the possible failure status

func (*Node) SetLinkStatus

func (n *Node) SetLinkStatus(status string)

SetLinkStatus set the Node link status

func (*Node) SetReferentMaster

func (n *Node) SetReferentMaster(ref string)

SetReferentMaster set the redis node parent referent

func (*Node) SetRole

func (n *Node) SetRole(flags string)

SetRole from a flags string list set the Node's role

func (*Node) String

func (n *Node) String() string

String string representation of a Instance

func (*Node) TotalSlots

func (n *Node) TotalSlots() int

TotalSlots return the total number of slot

type Nodes

type Nodes []*Node

Nodes represent a Node slice

func DecodeNodeInfos

func DecodeNodeInfos(input *string) *Nodes

DecodeNodeInfos decode from the cmd output the Redis nodes info. Second argument is the node on which we are connected to request info

func (Nodes) CountByFunc

func (n Nodes) CountByFunc(fn func(*Node) bool) (result int)

CountByFunc gives the number elements of NodeSlice that return true for the passed func.

func (Nodes) FilterByFunc

func (n Nodes) FilterByFunc(fn func(*Node) bool) Nodes

FilterByFunc remove a node from a slice by node ID and returns the slice. If not found, fail silently. Value must be unique

func (Nodes) GetNodeByAddr

func (n Nodes) GetNodeByAddr(addr string) (*Node, error)

GetNodeByAddr returns a Redis Node by its ID if not present in the Nodes slice return an error

func (Nodes) GetNodeByID

func (n Nodes) GetNodeByID(id string) (*Node, error)

GetNodeByID returns a Redis Node by its ID if not present in the Nodes slice return an error

func (Nodes) GetNodeByMasterID

func (n Nodes) GetNodeByMasterID(id string) (*Node, error)

GetNodeByMasterID returns a Redis Node by its ID if not present in the Nodes slice return an error

func (Nodes) GetNodesByFunc

func (n Nodes) GetNodesByFunc(f FindNodeFunc) (Nodes, error)

GetNodesByFunc returns first node found by the FindNodeFunc

func (Nodes) Len

func (n Nodes) Len() int

Len is the number of elements in the collection.

func (Nodes) Less

func (n Nodes) Less(i, j int) bool

Less reports whether the element with index i should sort before the element with index j.

func (Nodes) SortByFunc

func (n Nodes) SortByFunc(less func(*Node, *Node) bool) Nodes

SortByFunc returns a new ordered NodeSlice, determined by a func defining ‘less’.

func (Nodes) SortNodes

func (n Nodes) SortNodes() Nodes

SortNodes sort Nodes and return the sorted Nodes

func (Nodes) String

func (n Nodes) String() string

func (Nodes) Swap

func (n Nodes) Swap(i, j int)

Swap swaps the elements with indexes i and j.

type NodesPlacementInfo

type NodesPlacementInfo string

NodesPlacementInfo Redis Nodes placement mode information

const (
	// NodesPlacementInfoBestEffort the cluster nodes placement is in best effort,
	// it means you can have 2 masters (or more) on the same VM.
	NodesPlacementInfoBestEffort NodesPlacementInfo = "BestEffort"
	// NodesPlacementInfoOptimal the cluster nodes placement is optimal,
	// it means on master by VM
	NodesPlacementInfoOptimal NodesPlacementInfo = "Optimal"
)

type Slot

type Slot uint64

Slot represent a Redis Cluster slot

func AddSlots

func AddSlots(slots []Slot, addedSlots []Slot) []Slot

AddSlots return a new list of slots after adding some slots in it, duplicates are removed

func BuildSlotSlice

func BuildSlotSlice(min, max Slot) []Slot

BuildSlotSlice return a slice of all slots between this range

func DecodeSlot

func DecodeSlot(s string) (Slot, error)

DecodeSlot parse a string representation of a slot slot

func RemoveSlots

func RemoveSlots(slots []Slot, removedSlots []Slot) []Slot

RemoveSlots return a new list of slot where a list of slots have been removed, doesn't work if duplicates

func (Slot) String

func (s Slot) String() string

String string representation of a slot

type SlotRange

type SlotRange struct {
	Min Slot `json:"min"`
	Max Slot `json:"max"`
}

SlotRange represent a Range of slots

func SlotRangesFromSlots

func SlotRangesFromSlots(slots []Slot) []SlotRange

SlotRangesFromSlots return a slice of slot ranges from a slice of slots

func (SlotRange) String

func (s SlotRange) String() string

String string representation of a slotrange

func (SlotRange) Total

func (s SlotRange) Total() int

Total returns total slot present in the range

type SlotSlice

type SlotSlice []Slot

SlotSlice attaches the methods of sort.Interface to []string, sorting in increasing order.

func (SlotSlice) Len

func (s SlotSlice) Len() int

func (SlotSlice) Less

func (s SlotSlice) Less(i, j int) bool

func (SlotSlice) String

func (s SlotSlice) String() string

func (SlotSlice) Swap

func (s SlotSlice) Swap(i, j int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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