utils

package
v0.0.0-...-d7cb029 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2020 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ReplayString = "string"
	ReplayInt64s = "int64s"
)
View Source
const (
	GolangSecurityTime = "2006-01-02T15:04:05Z"
	// GolangSecurityTime = "2006-01-02 15:04:05"
	ReaderBufferSize = bytesize.MB * 32
	WriterBufferSize = bytesize.MB * 8

	LogLevelNone  = "none"
	LogLevelError = "error"
	LogLevelWarn  = "warn"
	LogLevelInfo  = "info"
	LogLevelDebug = "debug"

	TencentCluster = "tencent_cluster"
	AliyunCluster  = "aliyun_cluster"
	UCloudCluster  = "ucloud_cluster"
	CodisCluster   = "codis_cluster"

	CoidsErrMsg = "ERR backend server 'server' not found"

	CheckpointKey     = "redis-shake-checkpoint"
	CheckpointOffset  = "offset"
	CheckpointRunId   = "runid"
	CheckpointVersion = "version"
)
View Source
const (
	KB = 1024
	MB = 1024 * KB
	GB = 1024 * MB
	TB = 1024 * GB
	PB = 1024 * TB
)
View Source
const (
	AddressSplitter        = "@"
	AddressHeaderSplitter  = ":"
	AddressClusterSplitter = ";"
)

Variables

View Source
var (
	Version          = "$"
	LogRotater       *logRotate.Logger
	StartTime        string
	TargetRoundRobin int
	RDBVersion       uint = 9 // 9 for 5.0
)
View Source
var (
	FcvCheckpoint = Checkpoint{
		CurrentVersion:           1,
		FeatureCompatibleVersion: 1,
	}
	FcvConfiguration = Configuration{
		CurrentVersion:           1,
		FeatureCompatibleVersion: 0,
	}

	LowestCheckpointVersion = map[int]string{
		0: "1.0.0",
		1: "2.0.0",
	}
	LowestConfigurationVersion = map[int]string{
		0: "1.0.0",
		1: "2.0.0",
	}
)
View Source
var ErrNil = errors.New("redigo: nil returned")

ErrNil indicates that a reply value is nil.

View Source
var (
	HttpApi *nimo.HttpRestProvider
)
View Source
var (
	RecvChanSize = 4096
)

Functions

func AuthPassword

func AuthPassword(c net.Conn, authType, passwd string)

func Bool

func Bool(reply interface{}, err error) (bool, error)

Bool is a helper that converts a command reply to a boolean. If err is not equal to nil, then Bool returns false, err. Otherwise Bool converts the reply to boolean as follows:

Reply type      Result
integer         value != 0, nil
bulk string     strconv.ParseBool(reply)
nil             false, ErrNil
other           false, error

func Bytes

func Bytes(reply interface{}, err error) ([]byte, error)

Bytes is a helper that converts a command reply to a slice of bytes. If err is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts the reply to a slice of bytes as follows:

Reply type      Result
bulk string     reply, nil
simple string   []byte(reply), nil
nil             nil, ErrNil
other           nil, error

func Bytes2String

func Bytes2String(b []byte) string

func CheckFcv

func CheckFcv(file string, fcv int) (int, error)

--------------------------------------------------

func CheckHandleNetError

func CheckHandleNetError(err error) bool

func CheckSlotDistributionEqual

func CheckSlotDistributionEqual(src, dst []SlotOwner) bool

func CheckVersionChecksum

func CheckVersionChecksum(d []byte) (uint, uint64, error)

func ChoseSlotInRange

func ChoseSlotInRange(prefix string, left, right int) string

chose key(${prefix}-${suffix}) to let it hash in the given slot boundary: [left, right]. return suffix

func CompareUnorderedList

func CompareUnorderedList(a, b []string) bool

compare two unordered list. return true means equal.

func CompareVersion

func CompareVersion(a, b string, level int) int

* compare the version with given level. e.g., * 2.0.1, 2.0.3, level = 2 => equal: 0 * 2.0.1, 2.0.3, level = 3 => smaller: 1 * 3.1.1, 2.1 level = 2 => bigger: 2 * 3, 3.2, level = 2 => smaller: 1 * 3.a, 3.2, level = 2 => unknown: 3

func CutRedisInfoSegment

func CutRedisInfoSegment(content []byte, field string) []byte

cut segment

func Float64

func Float64(reply interface{}, err error) (float64, error)

Float64 is a helper that converts a command reply to 64 bit float. If err is not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts the reply to an int as follows:

Reply type    Result
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Float64ToByte

func Float64ToByte(float float64) string

func FlushWriter

func FlushWriter(w *bufio.Writer)

func GetAllClusterNode

func GetAllClusterNode(client redigo.Conn, role string, choose string) ([]string, error)

return id list if choose == "id", otherwise address

func GetFakeSlaveOffset

func GetFakeSlaveOffset(c redigo.Conn) (string, error)

func GetLocalIp

func GetLocalIp(preferdInterfaces []string) (ip string, interfaceName string, err error)

func GetMetric

func GetMetric(input int64) string

func GetRDBChecksum

func GetRDBChecksum(target, authType, auth string, tlsEnable bool) (string, error)

func GetReadableRedisAddressThroughSentinel

func GetReadableRedisAddressThroughSentinel(sentinelAddrs []string, sentinelMasterName string, fromMaster bool) (string, error)

GetReadableRedisAddressThroughSentinel gets readable redis address First, the function will pick one from available slaves randomly. If there is no available slave, it will pick master.

func GetRedisVersion

func GetRedisVersion(target, authType, auth string, tlsEnable bool) (string, error)

func GetSlotBoundary

func GetSlotBoundary(shardList []SlotOwner, address string) (int, int)

return -1, -1 means not found

func GetTotalLink() int

func GetWritableRedisAddressThroughSentinel

func GetWritableRedisAddressThroughSentinel(sentinelAddrs []string, sentinelMasterName string) (string, error)

getWritableRedisAddressThroughSentinel gets writable redis address The function will return redis master address.

func Goodbye

func Goodbye()

func InitHttpApi

func InitHttpApi(port int)

func Int

func Int(reply interface{}, err error) (int, error)

Int is a helper that converts a command reply to an integer. If err is not equal to nil, then Int returns 0, err. Otherwise, Int converts the reply to an int as follows:

Reply type    Result
integer       int(reply), nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Int64

func Int64(reply interface{}, err error) (int64, error)

Int64 is a helper that converts a command reply to 64 bit integer. If err is not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the reply to an int64 as follows:

Reply type    Result
integer       reply, nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Iocopy

func Iocopy(r io.Reader, w io.Writer, p []byte, max int) int

func KeyToSlot

func KeyToSlot(key string) uint16

func MultiBulk deprecated

func MultiBulk(reply interface{}, err error) ([]interface{}, error)

MultiBulk is a helper that converts an array command reply to a []interface{}.

Deprecated: Use Values instead.

func NewClusterConn

func NewClusterConn(clusterClient *redigoCluster.Cluster, recvChanSize int) redigo.Conn

func NewRDBLoader

func NewRDBLoader(reader *bufio.Reader, rbytes *atomic2.Int64, size int) chan *rdb.BinEntry

func OpenNetConn

func OpenNetConn(target, authType, passwd string, tlsEnable bool) net.Conn

func OpenNetConnSoft

func OpenNetConnSoft(target, authType, passwd string, tlsEnable bool) net.Conn

func OpenReadFile

func OpenReadFile(name string) (*os.File, int64)

func OpenReadWriteFile

func OpenReadWriteFile(name string) *os.File

func OpenRedisConn

func OpenRedisConn(target []string, authType, passwd string, isCluster bool, tlsEnable bool) redigo.Conn

func OpenRedisConnWithTimeout

func OpenRedisConnWithTimeout(target []string, authType, passwd string, readTimeout, writeTimeout time.Duration,
	isCluster bool, tlsEnable bool) redigo.Conn

func OpenSyncConn

func OpenSyncConn(target string, authType, passwd string, tlsEnable bool) (net.Conn, <-chan int64)

func OpenWriteFile

func OpenWriteFile(name string) *os.File

func ParseAddress

func ParseAddress(tp string) error

parse source address and target address

func ParseInfo

func ParseInfo(content []byte) map[string]string

func ParseKeyspace

func ParseKeyspace(content []byte) (map[int32]int64, error)

func ParseRedisInfo

func ParseRedisInfo(content []byte) map[string]string

parse single info field: "info server", "info keyspace"

func PickTargetRoundRobin

func PickTargetRoundRobin(n int) int

func ReadRESPEnd

func ReadRESPEnd(c net.Conn) (string, error)

read until hit the end of RESP: "\r\n"

func RemoveRESPEnd

func RemoveRESPEnd(input string) string

func RestoreBigkey

func RestoreBigkey(client redigo.Conn, key string, value string, pttl int64, db int, preDb *int)

func RestoreRdbEntry

func RestoreRdbEntry(c redigo.Conn, e *rdb.BinEntry)

func SelectDB

func SelectDB(c redigo.Conn, db uint32)

func SendPSyncAck

func SendPSyncAck(bw *bufio.Writer, offset int64) error

func SendPSyncContinue

func SendPSyncContinue(br *bufio.Reader, bw *bufio.Writer, runid string, offset int64) (string, int64, <-chan int64)

func SendPSyncFullsync

func SendPSyncFullsync(br *bufio.Reader, bw *bufio.Writer) (string, int64, <-chan int64)

func SendPSyncListeningPort

func SendPSyncListeningPort(c net.Conn, port int)

func StartQoS

func StartQoS(limit int) chan struct{}

func String

func String(reply interface{}, err error) (string, error)

String is a helper that converts a command reply to a string. If err is not equal to nil, then String returns "", err. Otherwise String converts the reply to a string as follows:

Reply type      Result
bulk string     string(reply), nil
simple string   reply, nil
nil             "",  ErrNil
other           "",  error

func String2Bytes

func String2Bytes(s string) []byte

func Uint64

func Uint64(reply interface{}, err error) (uint64, error)

Uint64 is a helper that converts a command reply to 64 bit integer. If err is not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the reply to an int64 as follows:

Reply type    Result
integer       reply, nil
bulk string   parsed reply, nil
nil           0, ErrNil
other         0, error

func Values

func Values(reply interface{}, err error) ([]interface{}, error)

Values is a helper that converts an array command reply to a []interface{}. If err is not equal to nil, then Values returns nil, err. Otherwise, Values converts the reply as follows:

Reply type      Result
array           reply, nil
nil             nil, ErrNil
other           nil, error

func Welcome

func Welcome()

func WritePid

func WritePid(id string) (err error)

func WritePidById

func WritePidById(id string, path string) error

Types

type Checkpoint

type Checkpoint struct {
	/*
	 * version: 0(or set not), MongoShake < 2.4, fcv == 0
	 * version: 1, MongoShake == 2.4, 0 < fcv <= 1
	 */
	CurrentVersion           int
	FeatureCompatibleVersion int
}

for checkpoint

func (Checkpoint) IsCompatible

func (c Checkpoint) IsCompatible(v int) bool

type ClusterConn

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

implement redigo.Conn(https://github.com/garyburd/redigo) * Embed redis-go-cluster(https://github.com/chasex/redis-go-cluster) * The reason I create this struct is that redis-go-cluster isn't fulfill redigo.Conn * interface. So I implement "Err", "Send", "Flush" and "Receive" interfaces.

func (*ClusterConn) Close

func (cc *ClusterConn) Close() error

func (*ClusterConn) Do

func (cc *ClusterConn) Do(commandName string, args ...interface{}) (reply interface{}, err error)

func (*ClusterConn) Err

func (cc *ClusterConn) Err() error

func (*ClusterConn) Flush

func (cc *ClusterConn) Flush() error

send batcher and put the return into recvChan

func (*ClusterConn) Receive

func (cc *ClusterConn) Receive() (reply interface{}, err error)

read recvChan

func (*ClusterConn) Send

func (cc *ClusterConn) Send(commandName string, args ...interface{}) error

just add into batcher

type ClusterNodeInfo

type ClusterNodeInfo struct {
	Id          string
	Address     string
	Flags       string
	Master      string
	PingSent    string
	PongRecv    string
	ConfigEpoch string
	LinkStat    string
	Slot        string
}

func ClusterNodeChoose

func ClusterNodeChoose(input []*ClusterNodeInfo, role string) []*ClusterNodeInfo

needMaster: true(master), false(slave)

func ParseClusterNode

func ParseClusterNode(content []byte) []*ClusterNodeInfo

* 10.1.1.1:21331> cluster nodes * d49a4c7b516b8da222d46a0a589b77f381285977 10.1.1.1:21333@31333 master - 0 1557996786000 3 connected 10923-16383 * f23ba7be501b2dcd4d6eeabd2d25551513e5c186 10.1.1.1:21336@31336 slave d49a4c7b516b8da222d46a0a589b77f381285977 0 1557996785000 6 connected * 75fffcd521738606a919607a7ddd52bcd6d65aa8 10.1.1.1:21331@31331 myself,master - 0 1557996784000 1 connected 0-5460 * da3dd51bb9cb5803d99942e0f875bc5f36dc3d10 10.1.1.1:21332@31332 master - 0 1557996786260 2 connected 5461-10922 * eff4e654d3cc361a8ec63640812e394a8deac3d6 10.1.1.1:21335@31335 slave da3dd51bb9cb5803d99942e0f875bc5f36dc3d10 0 1557996787261 5 connected * 486e081f8d47968df6a7e43ef9d3ba93b77d03b2 10.1.1.1:21334@31334 slave 75fffcd521738606a919607a7ddd52bcd6d65aa8 0 1557996785258 4 connected

type Configuration

type Configuration struct {
	/*
	 * version: 0(or set not), MongoShake < 2.4.0, fcv == 0
	 * version: 1, MongoShake == 2.4.0, 0 <= fcv <= 1
	 */
	CurrentVersion           int
	FeatureCompatibleVersion int
}

for configuration

func (Configuration) IsCompatible

func (c Configuration) IsCompatible(v int) bool

type Error

type Error string

func (Error) Error

func (err Error) Error() string

type Fcv

type Fcv interface {
	IsCompatible(int) bool
}

type SlotOwner

type SlotOwner struct {
	Master            string
	Slave             []string
	SlotLeftBoundary  int
	SlotRightBoundary int
}

*************************************

func GetSlotDistribution

func GetSlotDistribution(target, authType, auth string, tlsEnable bool) ([]SlotOwner, error)

Jump to

Keyboard shortcuts

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