redisclusterutil

package
v0.9.8 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package redisclusterutil implements some protocol level details of cluster specification.

It could be used independently from rediscluster package.

Index

Constants

View Source
const MasterOnlyKey = "CLUSTER_SELF:MASTER_ONLY"

MasterOnlyKey is a key of redis's SET which contains slots number. Cluster connector's main loop will switch these slots to MasterOnly mode on configuration refreshing. When slot migrates, slave redis instance behaves stupidly: they do not know about migration, and therefore they doesn't response with "ASKING" error for migrated key, and doesn't response to "ASKING" command on new shard. MasterOnlyKey is used within custom cluster migration utility to correctly migrate slot.

View Source
const NumSlots = 16384

NumSlots is the number of slots keys are sharded into in a redis cluster

Variables

This section is empty.

Functions

func BatchKey

func BatchKey(reqs []redis.Request) (string, bool)

BatchKey returns first key from a batch that is targeted to common slot.

func BatchSlot

func BatchSlot(reqs []redis.Request) (uint16, bool)

BatchSlot returns slot common for all requests in batch (if there is such common slot).

func CRC16

func CRC16(buf []byte) uint16

CRC16 returns checksum for a given set of bytes based on the crc algorithm defined for hashing redis keys in a cluster setup

func ParseMasterOnly

func ParseMasterOnly(resp interface{}) (set map[uint16]struct{}, valid bool, err error)

ParseMasterOnly parses content of MasterOnlyKey.

func ReqSlot

func ReqSlot(req redis.Request) (uint16, bool)

ReqSlot returns slot number targeted by this command.

func RequestMasterOnly

func RequestMasterOnly(c redis.Sender, key string) (set map[uint16]struct{}, valid bool, err error)

RequestMasterOnly fetches content of key as a int set, and returns it as a map. If key is empty (""), then MasterOnlyKey is used.

func Resolve

func Resolve(addr string) (string, error)

Resolve just resolves hostname:port to ipaddr:port

func SetMasterOnly

func SetMasterOnly(c redis.Sender, key string, slots []uint16) error

SetMasterOnly sets MasterOnlyKey to contain specified slots. It is used before slot migration.

func Slot

func Slot(key string) uint16

Slot returns the cluster slot the given key will fall into, taking into account curly braces within the key as per the spec.

func UnsetMasterOnly

func UnsetMasterOnly(c redis.Sender, key string, slots []uint16) error

UnsetMasterOnly unsets slots from MasterOnlyKey. It is used after slot migration.

Types

type InstanceInfo

type InstanceInfo struct {
	Uuid   string
	Addr   string
	IP     string
	Port   int
	Port2  int
	Fail   bool
	MySelf bool
	// NoAddr means that node were missed due to misconfiguration.
	// More probably, redis instance with other UUID were started on the same port.
	NoAddr    bool
	SlaveOf   string
	Slots     [][2]uint16
	Migrating []SlotMigration
}

InstanceInfo represents line of CLUSTER NODES result.

func (*InstanceInfo) AddrValid added in v0.9.3

func (ii *InstanceInfo) AddrValid() bool

AddrValid returns true if instance is successfully configure. Note that it could differ from HasAddr in some corner cases.

func (*InstanceInfo) HasAddr

func (ii *InstanceInfo) HasAddr() bool

HasAddr returns true if it is addressless instance (replaced with instance with other UUID), it will have no port

func (*InstanceInfo) IsMaster

func (ii *InstanceInfo) IsMaster() bool

IsMaster returns if this instance is master

type InstanceInfos

type InstanceInfos []InstanceInfo

InstanceInfos represents CLUSTER NODES result

func ParseClusterNodes

func ParseClusterNodes(res interface{}) (InstanceInfos, error)

ParseClusterNodes parses result of CLUSTER NODES command.

func (InstanceInfos) CollectAddressesAndMigrations

func (iis InstanceInfos) CollectAddressesAndMigrations(addrs map[string]struct{}, migrating map[uint16]struct{})

CollectAddressesAndMigrations collects all node's addresses and all slot migrations.

func (InstanceInfos) HashSum

func (iis InstanceInfos) HashSum() uint64

HashSum calculates signature of cluster configuration. It assumes, configuration were sorted in some way. If configuration fetched from all hosts has same signature, then cluster is in stable state.

func (InstanceInfos) Hosts

func (iis InstanceInfos) Hosts() []string

Hosts returns set of instance addresses.

func (InstanceInfos) MergeWith

func (iis InstanceInfos) MergeWith(other InstanceInfos) InstanceInfos

MergeWith merges sorted cluster information, giving preference to myself lines. It could be used to obtain "union of all cluster configuration visions" in custom tools managing cluster.

func (InstanceInfos) MySelf

func (iis InstanceInfos) MySelf() *InstanceInfo

MySelf returns info line for the host information were collected from.

func (InstanceInfos) SlotsRanges

func (iis InstanceInfos) SlotsRanges() []SlotsRange

SlotsRanges returns sorted SlotsRange-s made from slots information of cluster configuration.

type SlotMigration

type SlotMigration struct {
	Number uint16
	Moving SlotMoving
	Peer   string
}

SlotMigration represents one migrating slot.

type SlotMoving

type SlotMoving byte

SlotMoving is a flag about direction of slot migration.

const (
	// SlotMigrating indicates slot is migrating from this instance.
	SlotMigrating SlotMoving = 1
	// SlotImporting indicates slot is importing into this instance.
	SlotImporting SlotMoving = 2
)

type SlotsRange

type SlotsRange struct {
	From  int
	To    int
	Addrs []string // addresses of hosts hosting this range of slots. First address is a master, and other are slaves.
}

SlotsRange represents slice of slots

func ParseSlotsInfo

func ParseSlotsInfo(res interface{}) ([]SlotsRange, error)

ParseSlotsInfo parses result of CLUSTER SLOTS command

Jump to

Keyboard shortcuts

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