First, preparation

1. Get the code

Fort Redis repository github.com/redis/redis pull the code and switch to branch 5.0. If github download is slow, you can directly switch to branch 5.0 on the page and download the code zip package, but the zip package does not contain git information.

2. Environment preparation

CMake is not supported in CLion. CMake is supported by default. There are many tutorials on how to write CMakeLists files.

  • Kknews. Cc/code/mn5zz6…
  • www.jianshu.com/p/904b44911…

There are also solutions to install PYTHon3 and install compiledB:

  • My.oschina.net/icebergxty/…

Clion can’t support the Make project.

  • www.jetbrains.com/help/clion/…
  • Blog.jetbrains.com/zh-hans/202…

The Makefile support plugin is also available, so I’m going to give CLion version 2020.3.2 a try.

Clion compiles Redis

1) Code jump plug-in Makefile support

Open the Redis project directly, open the Makefile file, find CLion and IDEA can only, do not know the file will be prompted.

Install plugins. Plugins are small, quick to install and do not require a Clion reboot.

2) compilation

  1. Open the menu bar View -> Tool Windows -> Messages
  2. Build -> Rebuild Project

Then I saw the console compile and quickly generate various *. O files

3) Debug runs the program

As shown in the figure, redis-server is selected for both 1 and 2

Click the Debug button and it’s time to witness the miracle.

The client can also connect, but the server does not print logs, probably redis does not have logs. Redis code is written very elegant, we have not seen C code for a long time, slowly learn it.

3) the Debug code

Want to hit a breakpoint in the code debugging, fortunately, a little knowledge of the north figure, once heard of the String structure code in SDS.C (Simple Dynamic String, Simple Dynamic String).

Execute the commandset soul gateway

The breakpoint is working, but I’m not going to look at the code

1. Close the project, go to the redis/Makefile, right-click CLion to open the project, and recompile it.

Second, the code

Online copy of a Redis4.0 code reading list, although want to see 5, but little change. Github.com/tracenow/re… My more interesting parts are:

  1. Redis as a small and beautiful distributed component, its high availability, high performance, high extensible trade-off and code implementation
  2. Redis network how to achieve, want to learn how C is to achieve non-blocking network IO
  3. Maybe not so much interested in the specific data structure, understand the principle of data structure and time complexity, not too concerned about how to implement

1. Basic data structure

  • H, zmalloc.c, and sdsalloc.h
  • Simple dynamic strings SDS.h, SDS.c
  • Adlist.h, adlist.c
  • Dictionaries dict.h, dict.c
  • H (zskiplist and zskiplistNode structure), t_zset.c(ZSL starting function)
  • Integer sets intset.h, intset.c
  • Ziplist. h and ziplist.c
  • Zip dictionaries zipmap.h and zipmap.c
  • Quicklist. H, quickList. C
  • Hyperloglog. C (HLLHDR structure, function starting with HLL)
  • Geohash geohash. H, geohash. C

Second, Redis data type

  • Object system Object.c
  • String key T_string.c
  • List building t_list. C
  • Hash keys t_hash. C
  • Set key t_set. C
  • Ordered set key t_zset.c(except for all functions starting with ZSL)
  • HyperLogLog key HyperLogLog. C (pf function)
  • Geo-h, geo-c

Redis database

  • The database implements DB.c
  • Notify. c
  • RDB Persists rdb.h and rdb.c
  • AOF persists aof.c

Iv. Client/server

  • Event processing modules Ae.h, Ae.c, AE_epoll. c, AE_evport. c, AE_kqueue
  • H, anet. C, networking. C
  • H and server.c on the server
  • The client redis – cli. C

Distributed Redis

  • Replication function replication.c
  • Redis sentry sentinel. C
  • Redis clusters cluster.h, cluster.c

6. Independent functional modules

  • Publish and subscribe to Pubsub.c
  • Transaction multi. C

Seven, test,

  • Memory check memtest.c
  • Redis performance test redis_benchmark.c
  • Update log check redis_check_aof.c
  • Local database check redis_check_rdb.c
  • Testhelp.c is a small c-style testing framework

8. Tools

  • The binary operation command bitops.c
  • Debug debug. C
  • Endianconv.h, endianconV.c (for different systems)
  • The prompt help.h to assist the command
  • Lzf.h, LZF_C.c, LZF_D.c, LZFP.h
  • Random number rand. H, rand. C
  • Release release.c
  • Sha Encryption algorithms SHA1. h and sha1.c
  • Util. H, util. C
  • Cyclic redundancy check CRC16.c, CRC64.h, crC64.c
  • The SORT command SORT. C
  • Pseudo random function siphash.c
  • The process information operation setProcTitle.c
  • Geo utility classes geohash_helper.h and geohash_helper.c

Ninth, encapsulation class

  • Background thread I/O bio. H, bio. C
  • Latency classes latency. H, latency. C
  • Sorting algorithm class pqsort.h, pqsort.c
  • Redis defines I/O classes rio.h, rio.c
  • Synchronize Socket and file I/O operations syncio.c

10 and other

  • Sparkline. h, sparkline.c
  • Slowlogs slowlog.h and slowlog.c
  • Script scripting. C
  • Expire.c