diff --git a/README.md b/README.md index 29eae2f..2de555c 100644 --- a/README.md +++ b/README.md @@ -126,22 +126,22 @@ MSET (10 keys): 65487.89 requests per second If you want to read my code in this repository, here is a simple guidance. -- github.com/hdt3213/godis/cmd: only the entry point -- github.com/hdt3213/godis/config: config parser -- github.com/hdt3213/godis/interface: some interface definitions -- github.com/hdt3213/godis/lib: some utils, such as logger, sync utils and wildcard +- project root: only the entry point +- config: config parser +- interface: some interface definitions +- lib: some utils, such as logger, sync utils and wildcard I suggest focusing on the following directories: -- github.com/hdt3213/godis/tcp: the tcp server -- github.com/hdt3213/godis/redis: the redis protocol parser -- github.com/hdt3213/godis/datastruct: the implements of data structures +- tcp: the tcp server +- redis: the redis protocol parser +- datastruct: the implements of data structures - dict: a concurrent hash map - list: a linked list - lock: it is used to lock keys to ensure thread safety - set: a hash set based on map - sortedset: a sorted set implements based on skiplist -- github.com/hdt3213/godis: the core of storage engine +- database: the core of storage engine - server.go: a standalone redis server, - db.go: the basement function of database - exec.go: the gateway of database @@ -155,6 +155,19 @@ I suggest focusing on the following directories: - pubsub.go: implements of publish / subscribe - aof.go: implements of AOF persistence and rewrite - geo.go: implements of geography features + - sys.go: authentication and other system function + - transaction.go: local transaction +- cluster: 集群 + - cluster.go: entrance of cluster mode + - com.go: communication within nodes + - del.go: atomic implementation of `delete` command in cluster + - keys.go: keys command + - mset.go: atomic implementation of `mset` command in cluster + - multi.go: entrance of distributed transaction + - pubsub.go: pub/sub in cluster + - rename.go: `rename` command in cluster + - tcc.go: try-commit-catch distributed transaction implementation +- aof: AOF persistence # License diff --git a/README_CN.md b/README_CN.md index e095a6d..76643ff 100644 --- a/README_CN.md +++ b/README_CN.md @@ -116,22 +116,22 @@ MSET (10 keys): 65487.89 requests per second 本项目的目录结构: -- github.com/hdt3213/godis/cmd: main 函数,执行入口 -- github.com/hdt3213/godis/config: 配置文件解析 -- github.com/hdt3213/godis/interface: 一些模块间的接口定义 -- github.com/hdt3213/godis/lib: 各种工具,比如logger、同步和通配符 +- 根目录: main 函数,执行入口 +- config: 配置文件解析 +- interface: 一些模块间的接口定义 +- lib: 各种工具,比如logger、同步和通配符 建议按照下列顺序阅读各包: -- github.com/hdt3213/godis/tcp: tcp 服务器实现 -- github.com/hdt3213/godis/redis: redis 协议解析器 -- github.com/hdt3213/godis/datastruct: redis 的各类数据结构实现 +- tcp: tcp 服务器实现 +- redis: redis 协议解析器 +- datastruct: redis 的各类数据结构实现 - dict: hash 表 - list: 链表 - lock: 用于锁定 key 的锁组件 - set: 基于hash表的集合 - sortedset: 基于跳表实现的有序集合 -- github.com/hdt3213/godis: 存储引擎核心 +- database: 存储引擎核心 - db.go: 引擎的基础功能 - router.go: 将命令路由给响应的处理函数 - keys.go: del、ttl、expire 等通用命令实现 @@ -141,4 +141,17 @@ MSET (10 keys): 65487.89 requests per second - set.go: sadd 等集合命令实现 - sortedset.go: zadd 等有序集合命令实现 - pubsub.go: 发布订阅命令实现 - - aof.go: aof持久化实现 \ No newline at end of file + - geo.go: GEO 相关命令实现 + - sys.go: Auth 等系统功能实现 + - transaction.go: 单机事务实现 +- cluster: 集群 + - cluster.go: 集群入口 + - com.go: 节点间通信 + - del.go: delete 命令原子性实现 + - keys.go: key 相关命令集群中实现 + - mset.go: mset 命令原子性实现 + - multi.go: 集群内事务实现 + - pubsub.go: 发布订阅实现 + - rename.go: rename 命令集群实现 + - tcc.go: tcc 分布式事务底层实现 +- aof: AOF 持久化实现 \ No newline at end of file diff --git a/build-darwin.sh b/build-darwin.sh index 4f4cbf0..517e4fe 100755 --- a/build-darwin.sh +++ b/build-darwin.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -go build -o target/godis-darwin ./cmd +go build -o target/godis-darwin ./ diff --git a/build-linux.sh b/build-linux.sh index 2631615..de21df8 100755 --- a/build-linux.sh +++ b/build-linux.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o target/godis-linux ./cmd \ No newline at end of file +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o target/godis-linux ./ \ No newline at end of file diff --git a/cluster/cluster.go b/cluster/cluster.go index 60a0cfa..11986f6 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -4,8 +4,8 @@ package cluster import ( "context" "fmt" - "github.com/hdt3213/godis" "github.com/hdt3213/godis/config" + database2 "github.com/hdt3213/godis/database" "github.com/hdt3213/godis/datastruct/dict" "github.com/hdt3213/godis/interface/database" "github.com/hdt3213/godis/interface/redis" @@ -47,7 +47,7 @@ func MakeCluster() *Cluster { cluster := &Cluster{ self: config.Properties.Self, - db: godis.NewStandaloneServer(), + db: database2.NewStandaloneServer(), transactions: dict.MakeSimple(), peerPicker: consistenthash.New(replicas, nil), peerConnection: make(map[string]*pool.ObjectPool), @@ -102,7 +102,7 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis }() cmdName := strings.ToLower(string(cmdLine[0])) if cmdName == "auth" { - return godis.Auth(c, cmdLine[1:]) + return database2.Auth(c, cmdLine[1:]) } if !isAuthenticated(c) { return reply.MakeErrReply("NOAUTH Authentication required") @@ -112,12 +112,12 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis if len(cmdLine) != 1 { return reply.MakeArgNumErrReply(cmdName) } - return godis.StartMulti(c) + return database2.StartMulti(c) } else if cmdName == "discard" { if len(cmdLine) != 1 { return reply.MakeArgNumErrReply(cmdName) } - return godis.DiscardMulti(c) + return database2.DiscardMulti(c) } else if cmdName == "exec" { if len(cmdLine) != 1 { return reply.MakeArgNumErrReply(cmdName) @@ -130,7 +130,7 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis return execSelect(c, cmdLine) } if c != nil && c.InMultiState() { - return godis.EnqueueCmd(c, cmdLine) + return database2.EnqueueCmd(c, cmdLine) } cmdFunc, ok := router[cmdName] if !ok { diff --git a/cluster/multi.go b/cluster/multi.go index 32848c8..3146f62 100644 --- a/cluster/multi.go +++ b/cluster/multi.go @@ -1,7 +1,7 @@ package cluster import ( - "github.com/hdt3213/godis" + "github.com/hdt3213/godis/database" "github.com/hdt3213/godis/interface/redis" "github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/redis/reply" @@ -24,7 +24,7 @@ func execMulti(cluster *Cluster, conn redis.Connection, cmdLine CmdLine) redis.R // analysis related keys keys := make([]string, 0) // may contains duplicate for _, cl := range cmdLines { - wKeys, rKeys := godis.GetRelatedKeys(cl) + wKeys, rKeys := database.GetRelatedKeys(cl) keys = append(keys, wKeys...) keys = append(keys, rKeys...) } diff --git a/cluster/tcc.go b/cluster/tcc.go index d25aef7..c36ee5f 100644 --- a/cluster/tcc.go +++ b/cluster/tcc.go @@ -2,7 +2,7 @@ package cluster import ( "fmt" - "github.com/hdt3213/godis" + "github.com/hdt3213/godis/database" "github.com/hdt3213/godis/interface/redis" "github.com/hdt3213/godis/lib/logger" "github.com/hdt3213/godis/lib/timewheel" @@ -77,7 +77,7 @@ func (tx *Transaction) prepare() error { tx.mu.Lock() defer tx.mu.Unlock() - tx.writeKeys, tx.readKeys = godis.GetRelatedKeys(tx.cmdLine) + tx.writeKeys, tx.readKeys = database.GetRelatedKeys(tx.cmdLine) // lock writeKeys tx.lockKeys() diff --git a/aof_test.go b/database/aof_test.go similarity index 99% rename from aof_test.go rename to database/aof_test.go index 5c95fad..c20154d 100644 --- a/aof_test.go +++ b/database/aof_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/config" diff --git a/db.go b/database/db.go similarity index 99% rename from db.go rename to database/db.go index eb65c65..0a3269f 100644 --- a/db.go +++ b/database/db.go @@ -1,5 +1,5 @@ // Package godis is a memory database with redis compatible interface -package godis +package database import ( "github.com/hdt3213/godis/datastruct/dict" diff --git a/geo.go b/database/geo.go similarity index 99% rename from geo.go rename to database/geo.go index 06ee6c1..141eb06 100644 --- a/geo.go +++ b/database/geo.go @@ -1,4 +1,4 @@ -package godis +package database import ( "fmt" diff --git a/geo_test.go b/database/geo_test.go similarity index 99% rename from geo_test.go rename to database/geo_test.go index 232d69c..408ab32 100644 --- a/geo_test.go +++ b/database/geo_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "fmt" diff --git a/hash.go b/database/hash.go similarity index 99% rename from hash.go rename to database/hash.go index b8e851c..f62218d 100644 --- a/hash.go +++ b/database/hash.go @@ -1,4 +1,4 @@ -package godis +package database import ( Dict "github.com/hdt3213/godis/datastruct/dict" diff --git a/hash_test.go b/database/hash_test.go similarity index 99% rename from hash_test.go rename to database/hash_test.go index 26e6d32..7969f98 100644 --- a/hash_test.go +++ b/database/hash_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "fmt" diff --git a/keys.go b/database/keys.go similarity index 99% rename from keys.go rename to database/keys.go index 33407bc..52ce88b 100644 --- a/keys.go +++ b/database/keys.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/aof" diff --git a/keys_test.go b/database/keys_test.go similarity index 99% rename from keys_test.go rename to database/keys_test.go index 95f15ec..5fc0d2c 100644 --- a/keys_test.go +++ b/database/keys_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "fmt" diff --git a/list.go b/database/list.go similarity index 99% rename from list.go rename to database/list.go index e482460..13a5b46 100644 --- a/list.go +++ b/database/list.go @@ -1,4 +1,4 @@ -package godis +package database import ( List "github.com/hdt3213/godis/datastruct/list" diff --git a/list_test.go b/database/list_test.go similarity index 99% rename from list_test.go rename to database/list_test.go index 2b29037..576cfc8 100644 --- a/list_test.go +++ b/database/list_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "fmt" diff --git a/router.go b/database/router.go similarity index 97% rename from router.go rename to database/router.go index 3c4f813..eaed909 100644 --- a/router.go +++ b/database/router.go @@ -1,4 +1,4 @@ -package godis +package database import ( "strings" diff --git a/server.go b/database/server.go similarity index 99% rename from server.go rename to database/server.go index 8e2d9f0..fa4c4cb 100644 --- a/server.go +++ b/database/server.go @@ -1,4 +1,4 @@ -package godis +package database import ( "fmt" diff --git a/set.go b/database/set.go similarity index 99% rename from set.go rename to database/set.go index b253c8f..cc8ce4e 100644 --- a/set.go +++ b/database/set.go @@ -1,4 +1,4 @@ -package godis +package database import ( HashSet "github.com/hdt3213/godis/datastruct/set" diff --git a/set_test.go b/database/set_test.go similarity index 99% rename from set_test.go rename to database/set_test.go index f8b6716..5216753 100644 --- a/set_test.go +++ b/database/set_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "fmt" diff --git a/sortedset.go b/database/sortedset.go similarity index 99% rename from sortedset.go rename to database/sortedset.go index bf5a8c3..bf53f07 100644 --- a/sortedset.go +++ b/database/sortedset.go @@ -1,4 +1,4 @@ -package godis +package database import ( SortedSet "github.com/hdt3213/godis/datastruct/sortedset" diff --git a/sortedset_test.go b/database/sortedset_test.go similarity index 99% rename from sortedset_test.go rename to database/sortedset_test.go index 7394d0c..9449806 100644 --- a/sortedset_test.go +++ b/database/sortedset_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/lib/utils" diff --git a/string.go b/database/string.go similarity index 99% rename from string.go rename to database/string.go index ec6d8e1..bf00f26 100644 --- a/string.go +++ b/database/string.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/aof" diff --git a/string_test.go b/database/string_test.go similarity index 99% rename from string_test.go rename to database/string_test.go index 7761813..45fdbc2 100644 --- a/string_test.go +++ b/database/string_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "fmt" diff --git a/sys.go b/database/sys.go similarity index 98% rename from sys.go rename to database/sys.go index a06449f..26409e2 100644 --- a/sys.go +++ b/database/sys.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/config" diff --git a/sys_test.go b/database/sys_test.go similarity index 98% rename from sys_test.go rename to database/sys_test.go index 73bb3ec..24419d3 100644 --- a/sys_test.go +++ b/database/sys_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/config" diff --git a/transaction.go b/database/transaction.go similarity index 99% rename from transaction.go rename to database/transaction.go index 58d5994..fce2dbd 100644 --- a/transaction.go +++ b/database/transaction.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/datastruct/set" diff --git a/transaction_test.go b/database/transaction_test.go similarity index 99% rename from transaction_test.go rename to database/transaction_test.go index e841f79..6c56c0b 100644 --- a/transaction_test.go +++ b/database/transaction_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/lib/utils" diff --git a/tx_utils.go b/database/tx_utils.go similarity index 99% rename from tx_utils.go rename to database/tx_utils.go index 5ca6277..1853209 100644 --- a/tx_utils.go +++ b/database/tx_utils.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/aof" diff --git a/tx_utils_test.go b/database/tx_utils_test.go similarity index 99% rename from tx_utils_test.go rename to database/tx_utils_test.go index 3833820..77ca8e9 100644 --- a/tx_utils_test.go +++ b/database/tx_utils_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/lib/utils" diff --git a/util_test.go b/database/util_test.go similarity index 95% rename from util_test.go rename to database/util_test.go index 2bd2666..43db35b 100644 --- a/util_test.go +++ b/database/util_test.go @@ -1,4 +1,4 @@ -package godis +package database import ( "github.com/hdt3213/godis/datastruct/dict" diff --git a/cmd/main.go b/main.go similarity index 100% rename from cmd/main.go rename to main.go diff --git a/redis/server/server.go b/redis/server/server.go index 63faa14..21eae36 100644 --- a/redis/server/server.go +++ b/redis/server/server.go @@ -6,9 +6,9 @@ package server import ( "context" - "github.com/hdt3213/godis" "github.com/hdt3213/godis/cluster" "github.com/hdt3213/godis/config" + database2 "github.com/hdt3213/godis/database" "github.com/hdt3213/godis/interface/database" "github.com/hdt3213/godis/lib/logger" "github.com/hdt3213/godis/lib/sync/atomic" @@ -39,7 +39,7 @@ func MakeHandler() *Handler { len(config.Properties.Peers) > 0 { db = cluster.MakeCluster() } else { - db = godis.NewStandaloneServer() + db = database2.NewStandaloneServer() } return &Handler{ db: db,