-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
5,082 additions
and
5,091 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
package cluster | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/HDT3213/godis/src/redis/client" | ||
"github.com/jolestar/go-commons-pool/v2" | ||
"context" | ||
"errors" | ||
"github.com/HDT3213/godis/src/redis/client" | ||
"github.com/jolestar/go-commons-pool/v2" | ||
) | ||
|
||
type ConnectionFactory struct { | ||
Peer string | ||
Peer string | ||
} | ||
|
||
func (f *ConnectionFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) { | ||
c, err := client.MakeClient(f.Peer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.Start() | ||
return pool.NewPooledObject(c), nil | ||
c, err := client.MakeClient(f.Peer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.Start() | ||
return pool.NewPooledObject(c), nil | ||
} | ||
|
||
func (f *ConnectionFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error { | ||
c, ok := object.Object.(*client.Client) | ||
if !ok { | ||
return errors.New("type mismatch") | ||
} | ||
c.Close() | ||
return nil | ||
c, ok := object.Object.(*client.Client) | ||
if !ok { | ||
return errors.New("type mismatch") | ||
} | ||
c.Close() | ||
return nil | ||
} | ||
|
||
func (f *ConnectionFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool { | ||
// do validate | ||
return true | ||
// do validate | ||
return true | ||
} | ||
|
||
func (f *ConnectionFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error { | ||
// do activate | ||
return nil | ||
// do activate | ||
return nil | ||
} | ||
|
||
func (f *ConnectionFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error { | ||
// do passivate | ||
return nil | ||
// do passivate | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,99 @@ | ||
package cluster | ||
|
||
import ( | ||
"github.com/HDT3213/godis/src/interface/redis" | ||
"github.com/HDT3213/godis/src/redis/reply" | ||
"strconv" | ||
"github.com/HDT3213/godis/src/interface/redis" | ||
"github.com/HDT3213/godis/src/redis/reply" | ||
"strconv" | ||
) | ||
|
||
func Del(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply { | ||
if len(args) < 2 { | ||
return reply.MakeErrReply("ERR wrong number of arguments for 'del' command") | ||
} | ||
keys := make([]string, len(args)-1) | ||
for i := 1; i < len(args); i++ { | ||
keys[i-1] = string(args[i]) | ||
} | ||
groupMap := cluster.groupBy(keys) | ||
if len(groupMap) == 1 { // do fast | ||
for peer, group := range groupMap { // only one group | ||
return cluster.Relay(peer, c, makeArgs("DEL", group...)) | ||
} | ||
} | ||
// prepare | ||
var errReply redis.Reply | ||
txId := cluster.idGenerator.NextId() | ||
txIdStr := strconv.FormatInt(txId, 10) | ||
rollback := false | ||
for peer, group := range groupMap { | ||
args := []string{txIdStr} | ||
args = append(args, group...) | ||
var resp redis.Reply | ||
if peer == cluster.self { | ||
resp = PrepareDel(cluster, c, makeArgs("PrepareDel", args...)) | ||
} else { | ||
resp = cluster.Relay(peer, c, makeArgs("PrepareDel", args...)) | ||
} | ||
if reply.IsErrorReply(resp) { | ||
errReply = resp | ||
rollback = true | ||
break | ||
} | ||
} | ||
var respList []redis.Reply | ||
if rollback { | ||
// rollback | ||
RequestRollback(cluster, c, txId, groupMap) | ||
} else { | ||
// commit | ||
respList, errReply = RequestCommit(cluster, c, txId, groupMap) | ||
if errReply != nil { | ||
rollback = true | ||
} | ||
} | ||
if !rollback { | ||
var deleted int64 = 0 | ||
for _, resp := range respList { | ||
intResp := resp.(*reply.IntReply) | ||
deleted += intResp.Code | ||
} | ||
return reply.MakeIntReply(int64(deleted)) | ||
} | ||
return errReply | ||
if len(args) < 2 { | ||
return reply.MakeErrReply("ERR wrong number of arguments for 'del' command") | ||
} | ||
keys := make([]string, len(args)-1) | ||
for i := 1; i < len(args); i++ { | ||
keys[i-1] = string(args[i]) | ||
} | ||
groupMap := cluster.groupBy(keys) | ||
if len(groupMap) == 1 { // do fast | ||
for peer, group := range groupMap { // only one group | ||
return cluster.Relay(peer, c, makeArgs("DEL", group...)) | ||
} | ||
} | ||
// prepare | ||
var errReply redis.Reply | ||
txId := cluster.idGenerator.NextId() | ||
txIdStr := strconv.FormatInt(txId, 10) | ||
rollback := false | ||
for peer, group := range groupMap { | ||
args := []string{txIdStr} | ||
args = append(args, group...) | ||
var resp redis.Reply | ||
if peer == cluster.self { | ||
resp = PrepareDel(cluster, c, makeArgs("PrepareDel", args...)) | ||
} else { | ||
resp = cluster.Relay(peer, c, makeArgs("PrepareDel", args...)) | ||
} | ||
if reply.IsErrorReply(resp) { | ||
errReply = resp | ||
rollback = true | ||
break | ||
} | ||
} | ||
var respList []redis.Reply | ||
if rollback { | ||
// rollback | ||
RequestRollback(cluster, c, txId, groupMap) | ||
} else { | ||
// commit | ||
respList, errReply = RequestCommit(cluster, c, txId, groupMap) | ||
if errReply != nil { | ||
rollback = true | ||
} | ||
} | ||
if !rollback { | ||
var deleted int64 = 0 | ||
for _, resp := range respList { | ||
intResp := resp.(*reply.IntReply) | ||
deleted += intResp.Code | ||
} | ||
return reply.MakeIntReply(int64(deleted)) | ||
} | ||
return errReply | ||
} | ||
|
||
// args: PrepareDel id keys... | ||
func PrepareDel(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply { | ||
if len(args) < 3 { | ||
return reply.MakeErrReply("ERR wrong number of arguments for 'preparedel' command") | ||
} | ||
txId := string(args[1]) | ||
keys := make([]string, 0, len(args)-2) | ||
for i := 2; i < len(args); i++ { | ||
arg := args[i] | ||
keys = append(keys, string(arg)) | ||
} | ||
txArgs := makeArgs("DEL", keys...) // actual args for cluster.db | ||
tx := NewTransaction(cluster, c, txId, txArgs, keys) | ||
cluster.transactions.Put(txId, tx) | ||
err := tx.prepare() | ||
if err != nil { | ||
return reply.MakeErrReply(err.Error()) | ||
} | ||
return &reply.OkReply{} | ||
if len(args) < 3 { | ||
return reply.MakeErrReply("ERR wrong number of arguments for 'preparedel' command") | ||
} | ||
txId := string(args[1]) | ||
keys := make([]string, 0, len(args)-2) | ||
for i := 2; i < len(args); i++ { | ||
arg := args[i] | ||
keys = append(keys, string(arg)) | ||
} | ||
txArgs := makeArgs("DEL", keys...) // actual args for cluster.db | ||
tx := NewTransaction(cluster, c, txId, txArgs, keys) | ||
cluster.transactions.Put(txId, tx) | ||
err := tx.prepare() | ||
if err != nil { | ||
return reply.MakeErrReply(err.Error()) | ||
} | ||
return &reply.OkReply{} | ||
} | ||
|
||
// invoker should provide lock | ||
func CommitDel(cluster *Cluster, c redis.Connection, tx *Transaction) redis.Reply { | ||
keys := make([]string, len(tx.args)) | ||
for i, v := range tx.args { | ||
keys[i] = string(v) | ||
} | ||
keys = keys[1:] | ||
keys := make([]string, len(tx.args)) | ||
for i, v := range tx.args { | ||
keys[i] = string(v) | ||
} | ||
keys = keys[1:] | ||
|
||
deleted := cluster.db.Removes(keys...) | ||
if deleted > 0 { | ||
cluster.db.AddAof(reply.MakeMultiBulkReply(tx.args)) | ||
} | ||
return reply.MakeIntReply(int64(deleted)) | ||
deleted := cluster.db.Removes(keys...) | ||
if deleted > 0 { | ||
cluster.db.AddAof(reply.MakeMultiBulkReply(tx.args)) | ||
} | ||
return reply.MakeIntReply(int64(deleted)) | ||
} |
Oops, something went wrong.