From 0c2e2155e4d99945c9fb19850dfc41cc6a2e5bb8 Mon Sep 17 00:00:00 2001 From: stream1080 <77672403+stream1080@users.noreply.github.com> Date: Sun, 1 Oct 2023 12:03:21 +0800 Subject: [PATCH 1/4] feat: impl MakeClusterDatabases() --- cluster/cluster_database.go | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 cluster/cluster_database.go diff --git a/cluster/cluster_database.go b/cluster/cluster_database.go new file mode 100644 index 0000000..8781a3c --- /dev/null +++ b/cluster/cluster_database.go @@ -0,0 +1,55 @@ +package cluster + +import ( + "context" + pool "github.com/jolestar/go-commons-pool/v2" + + "github.com/stream1080/godis/config" + database2 "github.com/stream1080/godis/database" + "github.com/stream1080/godis/interface/database" + "github.com/stream1080/godis/interface/resp" + "github.com/stream1080/godis/lib/consistenthash" +) + +type ClusterDatabases struct { + self string + nodes []string + peerPick *consistenthash.NodeMap + peerConn map[string]*pool.ObjectPool + db database.Database +} + +func MakeClusterDatabases() *ClusterDatabases { + cluster := &ClusterDatabases{ + self: config.Properties.Self, + db: database2.NewStandaloneDatabase(), + peerPick: consistenthash.NewNodeMap(nil), + peerConn: make(map[string]*pool.ObjectPool), + } + nodes := make([]string, 0, len(config.Properties.Peers)+1) + for _, peer := range config.Properties.Peers { + nodes = append(nodes, peer) + } + nodes = append(nodes, config.Properties.Self) + cluster.peerPick.AddNode(nodes...) + ctx := context.Background() + for _, peer := range config.Properties.Peers { + cluster.peerConn[peer] = pool.NewObjectPoolWithDefaultConfig(ctx, &ConnFactory{ + Peer: peer, + }) + } + cluster.nodes = nodes + return cluster +} + +func (c *ClusterDatabases) Exec(client resp.Connection, args [][]byte) resp.Reply { + panic("implement me") +} + +func (c *ClusterDatabases) Close() { + panic("implement me") +} + +func (c *ClusterDatabases) AfterClientClose(conn resp.Connection) { + panic("implement me") +} From 7f0878e67bf522679efbe061e81293e3f2e1e5a8 Mon Sep 17 00:00:00 2001 From: stream1080 <77672403+stream1080@users.noreply.github.com> Date: Sun, 1 Oct 2023 12:09:02 +0800 Subject: [PATCH 2/4] style: update receivers name --- cluster/cluster_database.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cluster/cluster_database.go b/cluster/cluster_database.go index 8781a3c..55979c2 100644 --- a/cluster/cluster_database.go +++ b/cluster/cluster_database.go @@ -42,14 +42,14 @@ func MakeClusterDatabases() *ClusterDatabases { return cluster } -func (c *ClusterDatabases) Exec(client resp.Connection, args [][]byte) resp.Reply { +func (cluster *ClusterDatabases) Exec(client resp.Connection, args [][]byte) resp.Reply { panic("implement me") } -func (c *ClusterDatabases) Close() { +func (cluster *ClusterDatabases) Close() { panic("implement me") } -func (c *ClusterDatabases) AfterClientClose(conn resp.Connection) { +func (cluster *ClusterDatabases) AfterClientClose(conn resp.Connection) { panic("implement me") } From 3d868e09f54217cfa8615c65781b6c7be25c4c0e Mon Sep 17 00:00:00 2001 From: stream1080 <77672403+stream1080@users.noreply.github.com> Date: Sun, 1 Oct 2023 12:09:27 +0800 Subject: [PATCH 3/4] feat: add cluster getPeerClient() func --- cluster/com.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cluster/com.go diff --git a/cluster/com.go b/cluster/com.go new file mode 100644 index 0000000..95d8ccc --- /dev/null +++ b/cluster/com.go @@ -0,0 +1,26 @@ +package cluster + +import ( + "context" + "errors" + "github.com/stream1080/godis/resp/client" +) + +func (cluster *ClusterDatabases) getPeerClient(peer string) (*client.Client, error) { + pool, ok := cluster.peerConn[peer] + if !ok { + return nil, errors.New("conn not found") + } + + object, err := pool.BorrowObject(context.Background()) + if err != nil { + return nil, err + } + + c, ok := object.(*client.Client) + if !ok { + return nil, errors.New("wrong type") + } + + return c, nil +} From 3a2751cd3133825b6ecd623dfe7dbd2335bedef5 Mon Sep 17 00:00:00 2001 From: stream1080 <77672403+stream1080@users.noreply.github.com> Date: Sun, 1 Oct 2023 12:13:14 +0800 Subject: [PATCH 4/4] feat: add cluster returnPeerClient() func --- cluster/com.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cluster/com.go b/cluster/com.go index 95d8ccc..a215758 100644 --- a/cluster/com.go +++ b/cluster/com.go @@ -24,3 +24,12 @@ func (cluster *ClusterDatabases) getPeerClient(peer string) (*client.Client, err return c, nil } + +func (cluster *ClusterDatabases) returnPeerClient(peer string, client *client.Client) error { + pool, ok := cluster.peerConn[peer] + if !ok { + return errors.New("conn not found") + } + + return pool.ReturnObject(context.Background(), client) +}