Skip to content

Commit 6617272

Browse files
committed
refine code
1 parent 721d9c3 commit 6617272

File tree

15 files changed

+160
-129
lines changed

15 files changed

+160
-129
lines changed

cluster/client_pool.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"github.com/jolestar/go-commons-pool/v2"
1010
)
1111

12-
type ConnectionFactory struct {
12+
type connectionFactory struct {
1313
Peer string
1414
}
1515

16-
func (f *ConnectionFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) {
16+
func (f *connectionFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) {
1717
c, err := client.MakeClient(f.Peer)
1818
if err != nil {
1919
return nil, err
@@ -26,7 +26,7 @@ func (f *ConnectionFactory) MakeObject(ctx context.Context) (*pool.PooledObject,
2626
return pool.NewPooledObject(c), nil
2727
}
2828

29-
func (f *ConnectionFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error {
29+
func (f *connectionFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error {
3030
c, ok := object.Object.(*client.Client)
3131
if !ok {
3232
return errors.New("type mismatch")
@@ -35,17 +35,17 @@ func (f *ConnectionFactory) DestroyObject(ctx context.Context, object *pool.Pool
3535
return nil
3636
}
3737

38-
func (f *ConnectionFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool {
38+
func (f *connectionFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool {
3939
// do validate
4040
return true
4141
}
4242

43-
func (f *ConnectionFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error {
43+
func (f *connectionFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error {
4444
// do activate
4545
return nil
4646
}
4747

48-
func (f *ConnectionFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error {
48+
func (f *connectionFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error {
4949
// do passivate
5050
return nil
5151
}

cluster/cluster.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package cluster provides a server side cluster which is transparent to client. You can connect to any node in the cluster to access all data in the cluster
12
package cluster
23

34
import (
@@ -16,6 +17,8 @@ import (
1617
"strings"
1718
)
1819

20+
// Cluster represents a node of godis cluster
21+
// it holds part of data and coordinates other nodes to finish transactions
1922
type Cluster struct {
2023
self string
2124

@@ -37,7 +40,7 @@ const (
3740
// if only one node involved in a transaction, just execute the command don't apply tcc procedure
3841
var allowFastTransaction = true
3942

40-
// start current processing as a node of cluster
43+
// MakeCluster creates and starts a node of cluster
4144
func MakeCluster() *Cluster {
4245
cluster := &Cluster{
4346
self: config.Properties.Self,
@@ -62,7 +65,7 @@ func MakeCluster() *Cluster {
6265
cluster.peerPicker.AddNode(nodes...)
6366
ctx := context.Background()
6467
for _, peer := range config.Properties.Peers {
65-
cluster.peerConnection[peer] = pool.NewObjectPoolWithDefaultConfig(ctx, &ConnectionFactory{
68+
cluster.peerConnection[peer] = pool.NewObjectPoolWithDefaultConfig(ctx, &connectionFactory{
6669
Peer: peer,
6770
})
6871
}
@@ -73,11 +76,12 @@ func MakeCluster() *Cluster {
7376
// CmdFunc represents the handler of a redis command
7477
type CmdFunc func(cluster *Cluster, c redis.Connection, cmdAndArgs [][]byte) redis.Reply
7578

79+
// Close stops current node of cluster
7680
func (cluster *Cluster) Close() {
7781
cluster.db.Close()
7882
}
7983

80-
var router = MakeRouter()
84+
var router = makeRouter()
8185

8286
func isAuthenticated(c redis.Connection) bool {
8387
if config.Properties.RequirePass == "" {
@@ -86,16 +90,17 @@ func isAuthenticated(c redis.Connection) bool {
8690
return c.GetPassword() == config.Properties.RequirePass
8791
}
8892

89-
func (cluster *Cluster) Exec(c redis.Connection, args [][]byte) (result redis.Reply) {
93+
// Exec executes command on cluster
94+
func (cluster *Cluster) Exec(c redis.Connection, cmdArgs [][]byte) (result redis.Reply) {
9095
defer func() {
9196
if err := recover(); err != nil {
9297
logger.Warn(fmt.Sprintf("error occurs: %v\n%s", err, string(debug.Stack())))
9398
result = &reply.UnknownErrReply{}
9499
}
95100
}()
96-
cmd := strings.ToLower(string(args[0]))
101+
cmd := strings.ToLower(string(cmdArgs[0]))
97102
if cmd == "auth" {
98-
return godis.Auth(cluster.db, c, args[1:])
103+
return godis.Auth(cluster.db, c, cmdArgs[1:])
99104
}
100105
if !isAuthenticated(c) {
101106
return reply.MakeErrReply("NOAUTH Authentication required")
@@ -104,14 +109,16 @@ func (cluster *Cluster) Exec(c redis.Connection, args [][]byte) (result redis.Re
104109
if !ok {
105110
return reply.MakeErrReply("ERR unknown command '" + cmd + "', or not supported in cluster mode")
106111
}
107-
result = cmdFunc(cluster, c, args)
112+
result = cmdFunc(cluster, c, cmdArgs)
108113
return
109114
}
110115

116+
// AfterClientClose does some clean after client close connection
111117
func (cluster *Cluster) AfterClientClose(c redis.Connection) {
118+
cluster.db.AfterClientClose(c)
112119
}
113120

114-
func Ping(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
121+
func ping(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
115122
return godis.Ping(cluster.db, args[1:])
116123
}
117124

cluster/com.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// communicate with peers within cluster
21
package cluster
32

43
import (
@@ -33,29 +32,28 @@ func (cluster *Cluster) returnPeerClient(peer string, peerClient *client.Client)
3332
return connectionFactory.ReturnObject(context.Background(), peerClient)
3433
}
3534

36-
// relay command to peer
35+
// relay relays command to peer
3736
// cannot call Prepare, Commit, Rollback of self node
38-
func (cluster *Cluster) Relay(peer string, c redis.Connection, args [][]byte) redis.Reply {
37+
func (cluster *Cluster) relay(peer string, c redis.Connection, args [][]byte) redis.Reply {
3938
if peer == cluster.self {
4039
// to self db
4140
return cluster.db.Exec(c, args)
42-
} else {
43-
peerClient, err := cluster.getPeerClient(peer)
44-
if err != nil {
45-
return reply.MakeErrReply(err.Error())
46-
}
47-
defer func() {
48-
_ = cluster.returnPeerClient(peer, peerClient)
49-
}()
50-
return peerClient.Send(args)
5141
}
42+
peerClient, err := cluster.getPeerClient(peer)
43+
if err != nil {
44+
return reply.MakeErrReply(err.Error())
45+
}
46+
defer func() {
47+
_ = cluster.returnPeerClient(peer, peerClient)
48+
}()
49+
return peerClient.Send(args)
5250
}
5351

54-
// broadcast command to all node in cluster
55-
func (cluster *Cluster) Broadcast(c redis.Connection, args [][]byte) map[string]redis.Reply {
52+
// broadcast broadcasts command to all node in cluster
53+
func (cluster *Cluster) broadcast(c redis.Connection, args [][]byte) map[string]redis.Reply {
5654
result := make(map[string]redis.Reply)
5755
for _, node := range cluster.nodes {
58-
reply := cluster.Relay(node, c, args)
56+
reply := cluster.relay(node, c, args)
5957
result[node] = reply
6058
}
6159
return result

cluster/com_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ func TestRelay(t *testing.T) {
3838
testCluster2 := MakeTestCluster([]string{"127.0.0.1:6379"})
3939
key := RandString(4)
4040
value := RandString(4)
41-
ret := testCluster2.Relay("127.0.0.1:6379", nil, toArgs("SET", key, value))
41+
ret := testCluster2.relay("127.0.0.1:6379", nil, toArgs("SET", key, value))
4242
asserts.AssertNotError(t, ret)
43-
ret = testCluster2.Relay("127.0.0.1:6379", nil, toArgs("GET", key))
43+
ret = testCluster2.relay("127.0.0.1:6379", nil, toArgs("GET", key))
4444
asserts.AssertBulkReply(t, ret, value)
4545
}
4646

4747
func TestBroadcast(t *testing.T) {
4848
testCluster2 := MakeTestCluster([]string{"127.0.0.1:6379"})
4949
key := RandString(4)
5050
value := RandString(4)
51-
rets := testCluster2.Broadcast(nil, toArgs("SET", key, value))
51+
rets := testCluster2.broadcast(nil, toArgs("SET", key, value))
5252
for _, v := range rets {
5353
asserts.AssertNotError(t, v)
5454
}

cluster/del.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strconv"
77
)
88

9+
// Del atomically removes given keys from cluster, keys can be distributed on any node
10+
// if the given keys are distributed on different node, Del will use try-commit-catch to remove them
911
func Del(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
1012
if len(args) < 2 {
1113
return reply.MakeErrReply("ERR wrong number of arguments for 'del' command")
@@ -17,22 +19,22 @@ func Del(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
1719
groupMap := cluster.groupBy(keys)
1820
if len(groupMap) == 1 && allowFastTransaction { // do fast
1921
for peer, group := range groupMap { // only one group
20-
return cluster.Relay(peer, c, makeArgs("DEL", group...))
22+
return cluster.relay(peer, c, makeArgs("DEL", group...))
2123
}
2224
}
2325
// prepare
2426
var errReply redis.Reply
25-
txId := cluster.idGenerator.NextId()
26-
txIdStr := strconv.FormatInt(txId, 10)
27+
txID := cluster.idGenerator.NextId()
28+
txIDStr := strconv.FormatInt(txID, 10)
2729
rollback := false
2830
for peer, group := range groupMap {
29-
args := []string{txIdStr}
31+
args := []string{txIDStr}
3032
args = append(args, group...)
3133
var resp redis.Reply
3234
if peer == cluster.self {
33-
resp = PrepareDel(cluster, c, makeArgs("PrepareDel", args...))
35+
resp = prepareDel(cluster, c, makeArgs("PrepareDel", args...))
3436
} else {
35-
resp = cluster.Relay(peer, c, makeArgs("PrepareDel", args...))
37+
resp = cluster.relay(peer, c, makeArgs("PrepareDel", args...))
3638
}
3739
if reply.IsErrorReply(resp) {
3840
errReply = resp
@@ -43,10 +45,10 @@ func Del(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
4345
var respList []redis.Reply
4446
if rollback {
4547
// rollback
46-
RequestRollback(cluster, c, txId, groupMap)
48+
requestRollback(cluster, c, txID, groupMap)
4749
} else {
4850
// commit
49-
respList, errReply = RequestCommit(cluster, c, txId, groupMap)
51+
respList, errReply = requestCommit(cluster, c, txID, groupMap)
5052
if errReply != nil {
5153
rollback = true
5254
}
@@ -63,19 +65,19 @@ func Del(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
6365
}
6466

6567
// args: PrepareDel id keys...
66-
func PrepareDel(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
68+
func prepareDel(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
6769
if len(args) < 3 {
6870
return reply.MakeErrReply("ERR wrong number of arguments for 'preparedel' command")
6971
}
70-
txId := string(args[1])
72+
txID := string(args[1])
7173
keys := make([]string, 0, len(args)-2)
7274
for i := 2; i < len(args); i++ {
7375
arg := args[i]
7476
keys = append(keys, string(arg))
7577
}
7678
txArgs := makeArgs("DEL", keys...) // actual args for cluster.db
77-
tx := NewTransaction(cluster, c, txId, txArgs, keys)
78-
cluster.transactions.Put(txId, tx)
79+
tx := NewTransaction(cluster, c, txID, txArgs, keys)
80+
cluster.transactions.Put(txID, tx)
7981
err := tx.prepare()
8082
if err != nil {
8183
return reply.MakeErrReply(err.Error())
@@ -84,7 +86,7 @@ func PrepareDel(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply
8486
}
8587

8688
// invoker should provide lock
87-
func CommitDel(cluster *Cluster, c redis.Connection, tx *Transaction) redis.Reply {
89+
func commitDel(cluster *Cluster, c redis.Connection, tx *Transaction) redis.Reply {
8890
keys := make([]string, len(tx.args))
8991
for i, v := range tx.args {
9092
keys[i] = string(v)

cluster/keys.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"github.com/hdt3213/godis/redis/reply"
66
)
77

8+
// FlushDB removes all data in current database
89
func FlushDB(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
9-
replies := cluster.Broadcast(c, args)
10+
replies := cluster.broadcast(c, args)
1011
var errReply reply.ErrorReply
1112
for _, v := range replies {
1213
if reply.IsErrorReply(v) {
@@ -20,6 +21,7 @@ func FlushDB(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
2021
return reply.MakeErrReply("error occurs: " + errReply.Error())
2122
}
2223

24+
// FlushAll removes all data in cluster
2325
func FlushAll(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
2426
return FlushDB(cluster, c, args)
2527
}

0 commit comments

Comments
 (0)