preface
Redis is one of the most popular cache middleware at present. Every major company will consider Redis as a key part of the interview. But because Redis is developed using C language, it is not like we usually use Java, C# and other high-level languages, in the editor you can directly see the source code for learning, learning Redis can only be through daily use, or when you encounter problems to go to the online Google search, And this level can learn the knowledge is not enough, to better grasp the principle of Redis, through the source code learning is a better path, and Redis source code is not difficult, is an excellent textbook for advanced learning C language.
C Primer Plus is recommended if you don’t have a basic knowledge of C. If you have a basic knowledge of a high-level language, C is much simpler than any of these languages. The only thing to watch out for is what we call Pointers. As a programmer, knowing C will also make it easier for you to learn how computers work, network, and operate systems.
I once heard a big shot in the industry say:
A programmer who does not know C is not a qualified programmer.
Tool is introduced
To do a good job, he must sharpen his tools
We first need to have some handy tools to help us read and debug the source code, just like developing Java applications using IDEA, a handy tool can achieve half the result with twice the effort.
Since the official Redis only supports Linux, we need a Linux environment. Here we use VMware to start a virtual machine, Ubuntu 18.04 as the operating system, CLion as a reading and debugging tool (students who have used IDEA, certainly can quickly master the use of CLion, basically no difference).
VMware, Ubuntu 18.04, and CLion are easy steps to follow, but there are plenty of tutorials to help you get over them.
Source code can be cloned directly from Github. If cloning from Github is slow, you can clone from this link: gitee.com/lhd951220/r…
As we all know, cloning projects directly from Github is slow, so we recommend that you can open a Gitee account, and then import the source code from Github into Gitee, and use the link on Gitee to download, you will experience what is called flying speed. Tutorials are easy to follow, and there are plenty of resources on the web that you can search for yourself.
The official start of the
First of all, CLion uses CMake as a project build tool. You don’t need to know what CMake is (I don’t really know what it is).
CMake builds the project from a cmakelists. TXT file, so we need to create one first.
Note: When we finished cloning the project, we finally switched branches to commitId 767977C because I found some branches were missing files and could not build. To switch, simply execute Git Checkout 767977c directly from the command line
The first step
Create a cmakelists.txt file in the project’s home directory.
After creating the file will be shown below, then we need to click the arrow pointing in the direction of the text (at this point it will prompt the build fails, but it doesn’t matter, we don’t have to tube) so that it will be the project as a CMake project, and the bottom will also be a quick build window, convenient to build the project behind us.
Then copy the following into the file:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(redis VERSION 5.0)
set(CMAKE_BUILD_TYPE "Debug")
get_filename_component(REDIS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE)
execute_process(
COMMAND sh -c ./mkreleasehdr.sh
WORKING_DIRECTORY ${REDIS_ROOT}/src/
)
add_subdirectory(deps)
add_subdirectory(src/modules)
set(SRC_SERVER_TMP src/adlist.c src/acl.c src/ae.c src/anet.c src/ae_epoll.c src/dict.c src/sds.c src/zmalloc.c src/lzf_c.c src/lzf_d.c src/pqsort.c src/zipmap.c src/sha1.c src/sha256.c src/ziplist.c src/release.c src/networking.c src/util.c src/object.c src/db.c src/replication.c src/rdb.c src/t_string.c src/t_stream.c src/t_list.c src/t_set.c src/t_zset.c src/timeout.c src/tls.c src/evict.c src/defrag.c src/module.c src/quicklist.c src/expire.c src/childinfo.c src/redis-check-aof.c src/redis-check-rdb.c src/lazyfree.c src/geohash.c src/gopher.c src/rax.c src/geohash_helper.c src/siphash.c src/geo.c src/t_hash.c src/config.c src/connection.c src/aof.c src/pubsub.c src/multi.c src/debug.c src/sort.c src/intset.c src/syncio.c src/cluster.c src/crc16.c src/endianconv.c src/slowlog.c src/scripting.c src/bio.c src/rio.c src/rand.c src/memtest.c src/crc64.c src/bitops.c src/sentinel.c src/notify.c src/setproctitle.c src/blocked.c src/hyperloglog.c src/latency.c src/sparkline.c src/t_stream.c src/lolwut.c src/tracking.c src/lolwut5.c src/lolwut6.c src/listpack.c src/localtime.c )set(SRC_SERVER src/server.c ${SRC_SERVER_TMP})
set(SRC_CLI
src/anet.c
src/sds.c
src/adlist.c
src/redis-cli.c
src/zmalloc.c
src/release.c
src/anet.c
src/ae.c
src/crc64.c
src/crc16.c
src/dict.c
src/siphash.c
)
set(EXECUTABLE_OUTPUT_PATH src)
link_directories(deps/linenoise/ deps/lua/src deps/hiredis)
add_executable(redis-server ${SRC_SERVER})
target_include_directories(redis-server
PRIVATE ${REDIS_ROOT}/deps/linenoise
PRIVATE ${REDIS_ROOT}/deps/hiredis
PRIVATE ${REDIS_ROOT}/deps/lua/src)
target_link_libraries(redis-server
PRIVATE dl
PRIVATE pthread
PRIVATE m
PRIVATE lua
PRIVATE linenoise
PRIVATE hiredis)
add_executable(redis-cli ${SRC_CLI})
target_include_directories(redis-cli
PRIVATE ${REDIS_ROOT}/deps/linenoise
PRIVATE ${REDIS_ROOT}/deps/hiredis
PRIVATE ${REDIS_ROOT}/deps/lua/src)
target_link_libraries(redis-cli
PRIVATE pthread
PRIVATE m
PRIVATE linenoise
PRIVATE hiredis
)
Copy the code
The second step
Create a cmakelists.txt file under dept/ as well
Then copy the following into the file:
add_subdirectory(hiredis)
add_subdirectory(linenoise)
add_subdirectory(lua)
Copy the code
The third step
Create a cmakelists.txt file under dept/lineboise/
Then copy the following into the file:
add_library(linenoise linenoise.c)
Copy the code
The fourth step
Create a cmakelists.txt file under dept/lua/
Then copy the following into the file:
set(LUA_SRC
src/lapi.c src/lcode.c src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c
src/lgc.c src/llex.c src/lmem.c
src/lobject.c src/lopcodes.c src/lparser.c src/lstate.c src/lstring.c
src/ltable.c src/ltm.c
src/lundump.c src/lvm.c src/lzio.c src/strbuf.c src/fpconv.c
src/lauxlib.c src/lbaselib.c src/ldblib.c src/liolib.c src/lmathlib.c
src/loslib.c src/ltablib.c
src/lstrlib.c src/loadlib.c src/linit.c src/lua_cjson.c
src/lua_struct.c
src/lua_cmsgpack.c
src/lua_bit.c
)
add_library(lua STATIC ${LUA_SRC})
Copy the code
At this point, we’re more than halfway there.
Step 5
Create a cmakelists.txt file in the SRC /modules/ directory
Then copy the following into the file:
cmake_minimum_required(VERSION 3.9)
set(CMAKE_BUILD_TYPE "Debug")
add_library(helloworld SHARED helloworld.c)
set_target_properties(helloworld PROPERTIES PREFIX "" SUFFIX ".so")
add_library(hellotype SHARED hellotype.c)
set_target_properties(hellotype PROPERTIES PREFIX "" SUFFIX ".so")
add_library(helloblock SHARED helloblock.c)
set_target_properties(helloblock PROPERTIES PREFIX "" SUFFIX ".so")
add_library(testmodule SHARED testmodule.c)
set_target_properties(testmodule PROPERTIES PREFIX "" SUFFIX ".so")
Copy the code
Step 6
Add the following code under the SRC /ae_epoll.c file
#include "ae.h"
#include <zconf.h>
#include "zmalloc.h"
Copy the code
The final figure looks like this:
Start debugging
At this point, all the steps are complete and you can start debugging.
Install the steps as shown in the figure. Click and see the information displayed in the box in the figure. The list shown below appears in the upper right corner of the compiler
Then, we select Redis-Server, click the small green arrow to start the Redis service, and then we can debug the source code at the break point.
conclusion
Redis source code contains a large number of data structures, we can learn in the SDS, skip table, dictionary, hash table, Bloom filter and so on the realization of data structures, and these data structures will not be more complex than we usually contact with the knowledge, Take a closer look at how Redis uses ePoll for multiplexing.
Learn Redis source code to match this tutorial effect better.
In short, source code is the second best way to learn a tool, the first best of course documentation!