recover master slave relationship after restart

This commit is contained in:
finley
2025-06-01 22:31:58 +08:00
parent 2562ad10a0
commit 325ec10326
6 changed files with 65 additions and 42 deletions

View File

@@ -1,10 +1,14 @@
package core
import (
"errors"
"hash/crc32"
"net"
"strings"
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/connection"
"github.com/hdt3213/godis/redis/protocol"
)
@@ -44,6 +48,19 @@ func (cluster *Cluster) LocalExecWithinLock(c redis.Connection, cmdLine [][]byte
return cluster.db.ExecWithLock(c, cmdLine)
}
func (cluster *Cluster) SlaveOf(master string) error {
host, port, err := net.SplitHostPort(master)
if err != nil {
return errors.New("invalid master address")
}
c := connection.NewFakeConn()
reply := cluster.db.Exec(c, utils.ToCmdLine("slaveof", host, port))
if err := protocol.Try2ErrorReply(reply); err != nil {
return err
}
return nil
}
// GetPartitionKey extract hashtag
func GetPartitionKey(key string) string {
beg := strings.Index(key, "{")