Skip to content

Commit

Permalink
Merge pull request #56 from stream1080/feat/cluster
Browse files Browse the repository at this point in the history
feat: add cluster com and databases
  • Loading branch information
stream1080 authored Oct 1, 2023
2 parents 751462c + 3a2751c commit 5fa0701
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cluster/cluster_database.go
Original file line number Diff line number Diff line change
@@ -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 (cluster *ClusterDatabases) Exec(client resp.Connection, args [][]byte) resp.Reply {
panic("implement me")
}

func (cluster *ClusterDatabases) Close() {
panic("implement me")
}

func (cluster *ClusterDatabases) AfterClientClose(conn resp.Connection) {
panic("implement me")
}
35 changes: 35 additions & 0 deletions cluster/com.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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
}

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)
}

0 comments on commit 5fa0701

Please sign in to comment.