Merge branch 'unstable' into RELEASE_6

Former-commit-id: 32b11ab809c53275e7b2d56e242b3c3149987f35
This commit is contained in:
John Sully 2021-01-31 21:24:07 +00:00
commit 9c59979955
184 changed files with 9669 additions and 3111 deletions

View File

@ -7,7 +7,7 @@ jobs:
test-ubuntu-latest: test-ubuntu-latest:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- name: make - name: make
run: | run: |
sudo apt-get update sudo apt-get update
@ -17,7 +17,7 @@ jobs:
run: ./utils/gen-test-certs.sh run: ./utils/gen-test-certs.sh
- name: test-tls - name: test-tls
run: | run: |
sudo apt-get -y install tcl8.5 tcl-tls sudo apt-get -y install tcl tcl-tls
./runtest --clients 2 --verbose --tls ./runtest --clients 2 --verbose --tls
- name: cluster-test - name: cluster-test
run: | run: |
@ -42,14 +42,14 @@ jobs:
build-macos-latest: build-macos-latest:
runs-on: macos-latest runs-on: macos-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- name: make - name: make
run: make -j2 run: make -j2
build-libc-malloc: build-libc-malloc:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- name: make - name: make
run: | run: |
sudo apt-get update sudo apt-get update

View File

@ -1,16 +1,21 @@
name: Daily name: Daily
on: on:
pull_request:
branches:
# any PR to a release branch.
- '[0-9].[0-9]'
schedule: schedule:
- cron: '0 7 * * *' - cron: '0 0 * * *'
jobs: jobs:
test-jemalloc: test-ubuntu-jemalloc:
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 1200 if: github.repository == 'redis/redis'
timeout-minutes: 14400
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- name: make - name: make
run: | run: |
sudo apt-get -y install uuid-dev libcurl4-openssl-dev sudo apt-get -y install uuid-dev libcurl4-openssl-dev
@ -21,12 +26,17 @@ jobs:
./runtest --accurate --verbose ./runtest --accurate --verbose
- name: module api test - name: module api test
run: ./runtest-moduleapi --verbose run: ./runtest-moduleapi --verbose
- name: sentinel tests
run: ./runtest-sentinel
- name: cluster tests
run: ./runtest-cluster
test-libc-malloc: test-ubuntu-libc-malloc:
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 1200 if: github.repository == 'redis/redis'
timeout-minutes: 14400
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- name: make - name: make
run: | run: |
sudo apt-get -y install uuid-dev libcurl4-openssl-dev sudo apt-get -y install uuid-dev libcurl4-openssl-dev
@ -37,11 +47,17 @@ jobs:
./runtest --accurate --verbose ./runtest --accurate --verbose
- name: module api test - name: module api test
run: ./runtest-moduleapi --verbose run: ./runtest-moduleapi --verbose
- name: sentinel tests
run: ./runtest-sentinel
- name: cluster tests
run: ./runtest-cluster
test: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.repository == 'redis/redis'
timeout-minutes: 14400
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- name: make - name: make
run: | run: |
sudo apt-get -y install uuid-dev libcurl4-openssl-dev sudo apt-get -y install uuid-dev libcurl4-openssl-dev
@ -59,7 +75,7 @@ jobs:
test-ubuntu-arm: test-ubuntu-arm:
runs-on: [self-hosted, linux, arm] runs-on: [self-hosted, linux, arm]
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- name: make - name: make
run: | run: |
sudo apt-get -y install uuid-dev libcurl4-openssl-dev sudo apt-get -y install uuid-dev libcurl4-openssl-dev
@ -74,16 +90,91 @@ jobs:
test-valgrind: test-valgrind:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.repository == 'redis/redis'
timeout-minutes: 14400 timeout-minutes: 14400
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- name: make - name: make
run: | run: |
sudo apt-get -y install uuid-dev libcurl4-openssl-dev sudo apt-get -y install uuid-dev libcurl4-openssl-dev
make valgrind make valgrind
- name: test - name: test
run: | run: |
sudo apt-get update
sudo apt-get install tcl8.5 valgrind -y sudo apt-get install tcl8.5 valgrind -y
./runtest --valgrind --verbose --clients 1 ./runtest --valgrind --verbose --clients 1
- name: module api test - name: module api test
run: ./runtest-moduleapi --valgrind --verbose --clients 1 run: ./runtest-moduleapi --valgrind --verbose --clients 1
test-centos7-jemalloc:
runs-on: ubuntu-latest
if: github.repository == 'redis/redis'
container: centos:7
timeout-minutes: 14400
steps:
- uses: actions/checkout@v2
- name: make
run: |
yum -y install centos-release-scl
yum -y install devtoolset-7
scl enable devtoolset-7 "make"
- name: test
run: |
yum -y install tcl
./runtest --accurate --verbose
- name: module api test
run: ./runtest-moduleapi --verbose
- name: sentinel tests
run: ./runtest-sentinel
- name: cluster tests
run: ./runtest-cluster
test-centos7-tls:
runs-on: ubuntu-latest
if: github.repository == 'redis/redis'
container: centos:7
timeout-minutes: 14400
steps:
- uses: actions/checkout@v2
- name: make
run: |
yum -y install centos-release-scl epel-release
yum -y install devtoolset-7 openssl-devel openssl
scl enable devtoolset-7 "make BUILD_TLS=yes"
- name: test
run: |
yum -y install tcl tcltls
./utils/gen-test-certs.sh
./runtest --accurate --verbose --tls
./runtest --accurate --verbose
- name: module api test
run: |
./runtest-moduleapi --verbose --tls
./runtest-moduleapi --verbose
- name: sentinel tests
run: |
./runtest-sentinel --tls
./runtest-sentinel
- name: cluster tests
run: |
./runtest-cluster --tls
./runtest-cluster
test-macos-latest:
runs-on: macos-latest
if: github.repository == 'redis/redis'
timeout-minutes: 14400
steps:
- uses: actions/checkout@v2
- name: make
run: make
- name: test
run: |
./runtest --accurate --verbose --no-latency
- name: module api test
run: ./runtest-moduleapi --verbose
- name: sentinel tests
run: ./runtest-sentinel
- name: cluster tests
run: ./runtest-cluster

4
.gitignore vendored
View File

@ -48,9 +48,13 @@ src/nodes.conf
deps/lua/src/lua deps/lua/src/lua
deps/lua/src/luac deps/lua/src/luac
deps/lua/src/liblua.a deps/lua/src/liblua.a
tests/tls/*
.make-* .make-*
.prerequisites .prerequisites
*.dSYM *.dSYM
Makefile.dep Makefile.dep
.vscode/* .vscode/*
.idea/* .idea/*
.ccls
.ccls-cache/*
compile_commands.json

File diff suppressed because it is too large Load Diff

2
BUGS
View File

@ -1 +1 @@
Please check https://github.com/antirez/redis/issues Please check https://github.com/redis/redis/issues

View File

@ -1,50 +0,0 @@
Note: by contributing code to the Redis project in any form, including sending
a pull request via Github, a code fragment or patch via private email or
public discussion groups, you agree to release your code under the terms
of the BSD license that you can find in the COPYING file included in the Redis
source distribution. You will include BSD license in the COPYING file within
each source file that you contribute.
# IMPORTANT: HOW TO USE REDIS GITHUB ISSUES
* Github issues SHOULD ONLY BE USED to report bugs, and for DETAILED feature
requests. Everything else belongs to the Redis Google Group:
https://groups.google.com/forum/m/#!forum/Redis-db
PLEASE DO NOT POST GENERAL QUESTIONS that are not about bugs or suspected
bugs in the Github issues system. We'll be very happy to help you and provide
all the support in the mailing list.
There is also an active community of Redis users at Stack Overflow:
http://stackoverflow.com/questions/tagged/redis
# How to provide a patch for a new feature
1. If it is a major feature or a semantical change, please don't start coding
straight away: if your feature is not a conceptual fit you'll lose a lot of
time writing the code without any reason. Start by posting in the mailing list
and creating an issue at Github with the description of, exactly, what you want
to accomplish and why. Use cases are important for features to be accepted.
Here you'll see if there is consensus about your idea.
2. If in step 1 you get an acknowledgment from the project leaders, use the
following procedure to submit a patch:
a. Fork Redis on github ( http://help.github.com/fork-a-repo/ )
b. Create a topic branch (git checkout -b my_branch)
c. Push to your branch (git push origin my_branch)
d. Initiate a pull request on github ( https://help.github.com/articles/creating-a-pull-request/ )
e. Done :)
3. Keep in mind that we are very overloaded, so issues and PRs sometimes wait
for a *very* long time. However this is not lack of interest, as the project
gets more and more users, we find ourselves in a constant need to prioritize
certain issues/PRs over others. If you think your issue/PR is very important
try to popularize it, have other users commenting and sharing their point of
view and so forth. This helps.
4. For minor fixes just open a pull request on Github.
Thanks!

View File

@ -1,5 +1,5 @@
Copyright (c) 2006-2015, Salvatore Sanfilippo Copyright (c) 2006-2020, Salvatore Sanfilippo
Copyright (C) 2019, John Sully Copyright (C) 2019-2020, John Sully
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

View File

@ -8,6 +8,8 @@
##### Have feedback? Take our quick survey: https://www.surveymonkey.com/r/Y9XNS93 ##### Have feedback? Take our quick survey: https://www.surveymonkey.com/r/Y9XNS93
##### KeyDB is Hiring! We are currently building out our dev team. If you are interested please see the posting here: https://keydb.dev/careers.html
What is KeyDB? What is KeyDB?
-------------- --------------
@ -15,11 +17,13 @@ KeyDB is a high performance fork of Redis with a focus on multithreading, memory
KeyDB maintains full compatibility with the Redis protocol, modules, and scripts. This includes the atomicity guarantees for scripts and transactions. Because KeyDB keeps in sync with Redis development KeyDB is a superset of Redis functionality, making KeyDB a drop in replacement for existing Redis deployments. KeyDB maintains full compatibility with the Redis protocol, modules, and scripts. This includes the atomicity guarantees for scripts and transactions. Because KeyDB keeps in sync with Redis development KeyDB is a superset of Redis functionality, making KeyDB a drop in replacement for existing Redis deployments.
On the same hardware KeyDB can perform twice as many queries per second as Redis, with 60% lower latency. Active-Replication simplifies hot-spare failover allowing you to easily distribute writes over replicas and use simple TCP based load balancing/failover. KeyDB's higher performance allows you to do more on less hardware which reduces operation costs and complexity. On the same hardware KeyDB can achieve significantly higher throughput than Redis. Active-Replication simplifies hot-spare failover allowing you to easily distribute writes over replicas and use simple TCP based load balancing/failover. KeyDB's higher performance allows you to do more on less hardware which reduces operation costs and complexity.
<img src="https://keydb.dev/assets/img/blog/5x_opspersecVSdatasize.PNG"/> The chart below compares several KeyDB and Redis setups, including the latest Redis6 io-threads option, and TLS benchmarks.
See the full benchmark results and setup information here: https://docs.keydb.dev/blog/2019/10/07/blog-post/ <img src="https://docs.keydb.dev/img/blog/2020-09-15/ops_comparison.png"/>
See the full benchmark results and setup information here: https://docs.keydb.dev/blog/2020/09/29/blog-post/
Why fork Redis? Why fork Redis?
--------------- ---------------
@ -82,6 +86,8 @@ Building KeyDB
KeyDB can be compiled and is tested for use on Linux. KeyDB currently relies on SO_REUSEPORT's load balancing behavior which is available only in Linux. When we support marshalling connections across threads we plan to support other operating systems such as FreeBSD. KeyDB can be compiled and is tested for use on Linux. KeyDB currently relies on SO_REUSEPORT's load balancing behavior which is available only in Linux. When we support marshalling connections across threads we plan to support other operating systems such as FreeBSD.
More on CentOS/Archlinux/Alpine/Debian/Ubuntu dependencies and builds can be found here: https://docs.keydb.dev/docs/build/
Install dependencies: Install dependencies:
% sudo apt install build-essential nasm autotools-dev autoconf libjemalloc-dev tcl tcl-dev uuid-dev libcurl4-openssl-dev % sudo apt install build-essential nasm autotools-dev autoconf libjemalloc-dev tcl tcl-dev uuid-dev libcurl4-openssl-dev
@ -95,9 +101,14 @@ libssl-dev on Debian/Ubuntu) and run:
% make BUILD_TLS=yes % make BUILD_TLS=yes
You can enable flash support with: To build with systemd support, you'll need systemd development libraries (such
as libsystemd-dev on Debian/Ubuntu or systemd-devel on CentOS) and run:
% make MALLOC=memkind % make USE_SYSTEMD=yes
To append a suffix to KeyDB program names, use:
% make PROG_SUFFIX="-alt"
***Note that the following dependencies may be needed: ***Note that the following dependencies may be needed:
% sudo apt-get install autoconf autotools-dev libnuma-dev libtool % sudo apt-get install autoconf autotools-dev libnuma-dev libtool
@ -112,7 +123,7 @@ installed):
Fixing build problems with dependencies or cached build options Fixing build problems with dependencies or cached build options
--------- ---------
KeyDB has some dependencies which are included into the `deps` directory. KeyDB has some dependencies which are included in the `deps` directory.
`make` does not automatically rebuild dependencies even if something in `make` does not automatically rebuild dependencies even if something in
the source code of dependencies changes. the source code of dependencies changes.
@ -139,7 +150,7 @@ with a 64 bit target, or the other way around, you need to perform a
In case of build errors when trying to build a 32 bit binary of KeyDB, try In case of build errors when trying to build a 32 bit binary of KeyDB, try
the following steps: the following steps:
* Install the packages libc6-dev-i386 (also try g++-multilib). * Install the package libc6-dev-i386 (also try g++-multilib).
* Try using the following command line instead of `make 32bit`: * Try using the following command line instead of `make 32bit`:
`make CFLAGS="-m32 -march=native" LDFLAGS="-m32"` `make CFLAGS="-m32 -march=native" LDFLAGS="-m32"`
@ -164,14 +175,14 @@ Verbose build
------------- -------------
KeyDB will build with a user friendly colorized output by default. KeyDB will build with a user friendly colorized output by default.
If you want to see a more verbose output use the following: If you want to see a more verbose output, use the following:
% make V=1 % make V=1
Running KeyDB Running KeyDB
------------- -------------
To run KeyDB with the default configuration just type: To run KeyDB with the default configuration, just type:
% cd src % cd src
% ./keydb-server % ./keydb-server
@ -224,7 +235,7 @@ You can find the list of all the available commands at https://docs.keydb.dev/do
Installing KeyDB Installing KeyDB
----------------- -----------------
In order to install KeyDB binaries into /usr/local/bin just use: In order to install KeyDB binaries into /usr/local/bin, just use:
% make install % make install
@ -233,8 +244,8 @@ different destination.
Make install will just install binaries in your system, but will not configure Make install will just install binaries in your system, but will not configure
init scripts and configuration files in the appropriate place. This is not init scripts and configuration files in the appropriate place. This is not
needed if you want just to play a bit with KeyDB, but if you are installing needed if you just want to play a bit with KeyDB, but if you are installing
it the proper way for a production system, we have a script doing this it the proper way for a production system, we have a script that does this
for Ubuntu and Debian systems: for Ubuntu and Debian systems:
% cd utils % cd utils

10
deps/README.md vendored
View File

@ -21,7 +21,7 @@ just following tose steps:
1. Remove the jemalloc directory. 1. Remove the jemalloc directory.
2. Substitute it with the new jemalloc source tree. 2. Substitute it with the new jemalloc source tree.
3. Edit the Makefile localted in the same directory as the README you are 3. Edit the Makefile located in the same directory as the README you are
reading, and change the --with-version in the Jemalloc configure script reading, and change the --with-version in the Jemalloc configure script
options with the version you are using. This is required because otherwise options with the version you are using. This is required because otherwise
Jemalloc configuration script is broken and will not work nested in another Jemalloc configuration script is broken and will not work nested in another
@ -33,7 +33,7 @@ If you want to upgrade Jemalloc while also providing support for
active defragmentation, in addition to the above steps you need to perform active defragmentation, in addition to the above steps you need to perform
the following additional steps: the following additional steps:
5. In Jemalloc three, file `include/jemalloc/jemalloc_macros.h.in`, make sure 5. In Jemalloc tree, file `include/jemalloc/jemalloc_macros.h.in`, make sure
to add `#define JEMALLOC_FRAG_HINT`. to add `#define JEMALLOC_FRAG_HINT`.
6. Implement the function `je_get_defrag_hint()` inside `src/jemalloc.c`. You 6. Implement the function `je_get_defrag_hint()` inside `src/jemalloc.c`. You
can see how it is implemented in the current Jemalloc source tree shipped can see how it is implemented in the current Jemalloc source tree shipped
@ -47,9 +47,9 @@ Hiredis
Hiredis uses the SDS string library, that must be the same version used inside Redis itself. Hiredis is also very critical for Sentinel. Historically Redis often used forked versions of hiredis in a way or the other. In order to upgrade it is advised to take a lot of care: Hiredis uses the SDS string library, that must be the same version used inside Redis itself. Hiredis is also very critical for Sentinel. Historically Redis often used forked versions of hiredis in a way or the other. In order to upgrade it is advised to take a lot of care:
1. Check with diff if hiredis API changed and what impact it could have in Redis. 1. Check with diff if hiredis API changed and what impact it could have in Redis.
2. Make sure thet the SDS library inside Hiredis and inside Redis are compatible. 2. Make sure that the SDS library inside Hiredis and inside Redis are compatible.
3. After the upgrade, run the Redis Sentinel test. 3. After the upgrade, run the Redis Sentinel test.
4. Check manually that redis-cli and redis-benchmark behave as expecteed, since we have no tests for CLI utilities currently. 4. Check manually that redis-cli and redis-benchmark behave as expected, since we have no tests for CLI utilities currently.
Linenoise Linenoise
--- ---
@ -77,6 +77,6 @@ and our version:
1. Makefile is modified to allow a different compiler than GCC. 1. Makefile is modified to allow a different compiler than GCC.
2. We have the implementation source code, and directly link to the following external libraries: `lua_cjson.o`, `lua_struct.o`, `lua_cmsgpack.o` and `lua_bit.o`. 2. We have the implementation source code, and directly link to the following external libraries: `lua_cjson.o`, `lua_struct.o`, `lua_cmsgpack.o` and `lua_bit.o`.
3. There is a security fix in `ldo.c`, line 498: The check for `LUA_SIGNATURE[0]` is removed in order toa void direct bytecode execution. 3. There is a security fix in `ldo.c`, line 498: The check for `LUA_SIGNATURE[0]` is removed in order to avoid direct bytecode execution.

View File

@ -625,7 +625,7 @@ static void refreshMultiLine(struct linenoiseState *l) {
rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */ rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
lndebug("rpos2 %d", rpos2); lndebug("rpos2 %d", rpos2);
/* Go up till we reach the expected positon. */ /* Go up till we reach the expected position. */
if (rows-rpos2 > 0) { if (rows-rpos2 > 0) {
lndebug("go-up %d", rows-rpos2); lndebug("go-up %d", rows-rpos2);
snprintf(seq,64,"\x1b[%dA", rows-rpos2); snprintf(seq,64,"\x1b[%dA", rows-rpos2);
@ -767,7 +767,7 @@ void linenoiseEditBackspace(struct linenoiseState *l) {
} }
} }
/* Delete the previosu word, maintaining the cursor at the start of the /* Delete the previous word, maintaining the cursor at the start of the
* current word. */ * current word. */
void linenoiseEditDeletePrevWord(struct linenoiseState *l) { void linenoiseEditDeletePrevWord(struct linenoiseState *l) {
size_t old_pos = l->pos; size_t old_pos = l->pos;

2
deps/lua/src/ldo.c vendored
View File

@ -274,7 +274,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
CallInfo *ci; CallInfo *ci;
StkId st, base; StkId st, base;
Proto *p = cl->p; Proto *p = cl->p;
luaD_checkstack(L, p->maxstacksize); luaD_checkstack(L, p->maxstacksize + p->numparams);
func = restorestack(L, funcr); func = restorestack(L, funcr);
if (!p->is_vararg) { /* no varargs? */ if (!p->is_vararg) { /* no varargs? */
base = func + 1; base = func + 1;

View File

@ -24,7 +24,7 @@
# to customize a few per-server settings. Include files can include # to customize a few per-server settings. Include files can include
# other files, so use this wisely. # other files, so use this wisely.
# #
# Notice option "include" won't be rewritten by command "CONFIG REWRITE" # Note that option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or KeyDB Sentinel. Since KeyDB always uses the last processed # from admin or KeyDB Sentinel. Since KeyDB always uses the last processed
# line as value of a configuration directive, you'd better put includes # line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime. # at the beginning of this file to avoid overwriting config change at runtime.
@ -46,7 +46,7 @@
################################## NETWORK ##################################### ################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, KeyDB listens # By default, if no "bind" configuration directive is specified, KeyDB listens
# for connections from all the network interfaces available on the server. # for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using # It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses. # the "bind" configuration directive, followed by one or more IP addresses.
# #
@ -58,13 +58,12 @@
# ~~~ WARNING ~~~ If the computer running KeyDB is directly exposed to the # ~~~ WARNING ~~~ If the computer running KeyDB is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the # internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the # instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force KeyDB to listen only into # following bind directive, that will force KeyDB to listen only on the
# the IPv4 loopback interface address (this means KeyDB will be able to # IPv4 loopback interface address (this means KeyDB will only be able to
# accept connections only from clients running into the same computer it # accept client connections from the same host that it is running on).
# is running).
# #
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE. # JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 bind 127.0.0.1
@ -93,8 +92,8 @@ port 6379
# TCP listen() backlog. # TCP listen() backlog.
# #
# In high requests-per-second environments you need an high backlog in order # In high requests-per-second environments you need a high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel # to avoid slow clients connection issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so # will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog # make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect. # in order to get the desired effect.
@ -118,8 +117,8 @@ timeout 0
# of communication. This is useful for two reasons: # of communication. This is useful for two reasons:
# #
# 1) Detect dead peers. # 1) Detect dead peers.
# 2) Take the connection alive from the point of view of network # 2) Force network equipment in the middle to consider the connection to be
# equipment in the middle. # alive.
# #
# On Linux, the specified value (in seconds) is the period used to send ACKs. # On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed. # Note that to close the connection the double of the time is needed.
@ -159,11 +158,14 @@ tcp-keepalive 300
# By default, clients (including replica servers) on a TLS port are required # By default, clients (including replica servers) on a TLS port are required
# to authenticate using valid client side certificates. # to authenticate using valid client side certificates.
# #
# It is possible to disable authentication using this directive. # If "no" is specified, client certificates are not required and not accepted.
# If "optional" is specified, client certificates are accepted and must be
# valid if provided, but are not required.
# #
# tls-auth-clients no # tls-auth-clients no
# tls-auth-clients optional
# By default, a Redis replica does not attempt to establish a TLS connection # By default, a KeyDB replica does not attempt to establish a TLS connection
# with its master. # with its master.
# #
# Use the following directive to enable TLS on replication links. # Use the following directive to enable TLS on replication links.
@ -225,11 +227,12 @@ daemonize no
# supervision tree. Options: # supervision tree. Options:
# supervised no - no supervision interaction # supervised no - no supervision interaction
# supervised upstart - signal upstart by putting KeyDB into SIGSTOP mode # supervised upstart - signal upstart by putting KeyDB into SIGSTOP mode
# requires "expect stop" in your upstart job config
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on # supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables # UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready." # Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor. # They do not enable continuous pings back to your supervisor.
supervised no supervised no
# If a pid file is specified, KeyDB writes it where specified at startup # If a pid file is specified, KeyDB writes it where specified at startup
@ -279,6 +282,9 @@ databases 16
# ASCII art logo in startup logs by setting the following option to yes. # ASCII art logo in startup logs by setting the following option to yes.
always-show-logo yes always-show-logo yes
# Retrieving "message of today" using CURL requests.
#enable-motd yes
################################ SNAPSHOTTING ################################ ################################ SNAPSHOTTING ################################
# #
# Save the DB on disk: # Save the DB on disk:
@ -288,7 +294,7 @@ always-show-logo yes
# Will save the DB if both the given number of seconds and the given # Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred. # number of write operations against the DB occurred.
# #
# In the example below the behaviour will be to save: # In the example below the behavior will be to save:
# after 900 sec (15 min) if at least 1 key changed # after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed # after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed # after 60 sec if at least 10000 keys changed
@ -321,7 +327,7 @@ save 60 10000
stop-writes-on-bgsave-error yes stop-writes-on-bgsave-error yes
# Compress string objects using LZF when dump .rdb databases? # Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win. # By default compression is enabled as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but # If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys. # the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes rdbcompression yes
@ -409,11 +415,11 @@ dir ./
# still reply to client requests, possibly with out of date data, or the # still reply to client requests, possibly with out of date data, or the
# data set may just be empty if this is the first synchronization. # data set may just be empty if this is the first synchronization.
# #
# 2) if replica-serve-stale-data is set to 'no' the replica will reply with # 2) If replica-serve-stale-data is set to 'no' the replica will reply with
# an error "SYNC with master in progress" to all the kind of commands # an error "SYNC with master in progress" to all commands except:
# but to INFO, replicaOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, # INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE,
# SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, # UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST,
# COMMAND, POST, HOST: and LATENCY. # HOST and LATENCY.
# #
replica-serve-stale-data yes replica-serve-stale-data yes
@ -491,14 +497,14 @@ repl-diskless-sync-delay 5
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# WARNING: RDB diskless load is experimental. Since in this setup the replica # WARNING: RDB diskless load is experimental. Since in this setup the replica
# does not immediately store an RDB on disk, it may cause data loss during # does not immediately store an RDB on disk, it may cause data loss during
# failovers. RDB diskless load + Redis modules not handling I/O reads may also # failovers. RDB diskless load + KeyDB modules not handling I/O reads may also
# cause Redis to abort in case of I/O errors during the initial synchronization # cause KeyDB to abort in case of I/O errors during the initial synchronization
# stage with the master. Use only if your do what you are doing. # stage with the master. Use only if your do what you are doing.
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# #
# Replica can load the RDB it reads from the replication link directly from the # Replica can load the RDB it reads from the replication link directly from the
# socket, or store the RDB to a file and read that file after it was completely # socket, or store the RDB to a file and read that file after it was completely
# recived from the master. # received from the master.
# #
# In many cases the disk is slower than the network, and storing and loading # In many cases the disk is slower than the network, and storing and loading
# the RDB file may increase replication time (and even increase the master's # the RDB file may increase replication time (and even increase the master's
@ -528,7 +534,8 @@ repl-diskless-load disabled
# #
# It is important to make sure that this value is greater than the value # It is important to make sure that this value is greater than the value
# specified for repl-ping-replica-period otherwise a timeout will be detected # specified for repl-ping-replica-period otherwise a timeout will be detected
# every time there is low traffic between the master and the replica. # every time there is low traffic between the master and the replica. The default
# value is 60 seconds.
# #
# repl-timeout 60 # repl-timeout 60
@ -553,28 +560,28 @@ repl-disable-tcp-nodelay no
# partial resync is enough, just passing the portion of data the replica # partial resync is enough, just passing the portion of data the replica
# missed while disconnected. # missed while disconnected.
# #
# The bigger the replication backlog, the longer the time the replica can be # The bigger the replication backlog, the longer the replica can endure the
# disconnected and later be able to perform a partial resynchronization. # disconnect and later be able to perform a partial resynchronization.
# #
# The backlog is only allocated once there is at least a replica connected. # The backlog is only allocated if there is at least one replica connected.
# #
# repl-backlog-size 1mb # repl-backlog-size 1mb
# After a master has no longer connected replicas for some time, the backlog # After a master has no connected replicas for some time, the backlog will be
# will be freed. The following option configures the amount of seconds that # freed. The following option configures the amount of seconds that need to
# need to elapse, starting from the time the last replica disconnected, for # elapse, starting from the time the last replica disconnected, for the backlog
# the backlog buffer to be freed. # buffer to be freed.
# #
# Note that replicas never free the backlog for timeout, since they may be # Note that replicas never free the backlog for timeout, since they may be
# promoted to masters later, and should be able to correctly "partially # promoted to masters later, and should be able to correctly "partially
# resynchronize" with the replicas: hence they should always accumulate backlog. # resynchronize" with other replicas: hence they should always accumulate backlog.
# #
# A value of 0 means to never release the backlog. # A value of 0 means to never release the backlog.
# #
# repl-backlog-ttl 3600 # repl-backlog-ttl 3600
# The replica priority is an integer number published by KeyDB in the INFO # The replica priority is an integer number published by KeyDB in the INFO
# output. It is used by Redis Sentinel in order to select a replica to promote # output. It is used by KeyDB Sentinel in order to select a replica to promote
# into a master if the master is no longer working correctly. # into a master if the master is no longer working correctly.
# #
# A replica with a low priority number is considered better for promotion, so # A replica with a low priority number is considered better for promotion, so
@ -617,8 +624,8 @@ replica-priority 100
# Another place where this info is available is in the output of the # Another place where this info is available is in the output of the
# "ROLE" command of a master. # "ROLE" command of a master.
# #
# The listed IP and address normally reported by a replica is obtained # The listed IP address and port normally reported by a replica is
# in the following way: # obtained in the following way:
# #
# IP: The address is auto detected by checking the peer address # IP: The address is auto detected by checking the peer address
# of the socket used by the replica to connect with the master. # of the socket used by the replica to connect with the master.
@ -628,7 +635,7 @@ replica-priority 100
# listen for connections. # listen for connections.
# #
# However when port forwarding or Network Address Translation (NAT) is # However when port forwarding or Network Address Translation (NAT) is
# used, the replica may be actually reachable via different IP and port # used, the replica may actually be reachable via different IP and port
# pairs. The following two options can be used by a replica in order to # pairs. The following two options can be used by a replica in order to
# report to its master a specific set of IP and port, so that both INFO # report to its master a specific set of IP and port, so that both INFO
# and ROLE will report those values. # and ROLE will report those values.
@ -641,31 +648,31 @@ replica-priority 100
############################### KEYS TRACKING ################################# ############################### KEYS TRACKING #################################
# Redis implements server assisted support for client side caching of values. # KeyDB implements server assisted support for client side caching of values.
# This is implemented using an invalidation table that remembers, using # This is implemented using an invalidation table that remembers, using
# 16 millions of slots, what clients may have certain subsets of keys. In turn # 16 millions of slots, what clients may have certain subsets of keys. In turn
# this is used in order to send invalidation messages to clients. Please # this is used in order to send invalidation messages to clients. Please
# to understand more about the feature check this page: # check this page to understand more about the feature:
# #
# https://redis.io/topics/client-side-caching # https://redis.io/topics/client-side-caching
# #
# When tracking is enabled for a client, all the read only queries are assumed # When tracking is enabled for a client, all the read only queries are assumed
# to be cached: this will force Redis to store information in the invalidation # to be cached: this will force KeyDB to store information in the invalidation
# table. When keys are modified, such information is flushed away, and # table. When keys are modified, such information is flushed away, and
# invalidation messages are sent to the clients. However if the workload is # invalidation messages are sent to the clients. However if the workload is
# heavily dominated by reads, Redis could use more and more memory in order # heavily dominated by reads, KeyDB could use more and more memory in order
# to track the keys fetched by many clients. # to track the keys fetched by many clients.
# #
# For this reason it is possible to configure a maximum fill value for the # For this reason it is possible to configure a maximum fill value for the
# invalidation table. By default it is set to 1M of keys, and once this limit # invalidation table. By default it is set to 1M of keys, and once this limit
# is reached, Redis will start to evict keys in the invalidation table # is reached, KeyDB will start to evict keys in the invalidation table
# even if they were not modified, just to reclaim memory: this will in turn # even if they were not modified, just to reclaim memory: this will in turn
# force the clients to invalidate the cached values. Basically the table # force the clients to invalidate the cached values. Basically the table
# maximum size is a trade off between the memory you want to spend server # maximum size is a trade off between the memory you want to spend server
# side to track information about who cached what, and the ability of clients # side to track information about who cached what, and the ability of clients
# to retain cached objects in memory. # to retain cached objects in memory.
# #
# If you set the value to 0, it means there are no limits, and Redis will # If you set the value to 0, it means there are no limits, and KeyDB will
# retain as many keys as needed in the invalidation table. # retain as many keys as needed in the invalidation table.
# In the "stats" INFO section, you can find information about the number of # In the "stats" INFO section, you can find information about the number of
# keys in the invalidation table at every given moment. # keys in the invalidation table at every given moment.
@ -677,7 +684,7 @@ replica-priority 100
################################## SECURITY ################################### ################################## SECURITY ###################################
# Warning: since KeyDB is pretty fast an outside user can try up to # Warning: since KeyDB is pretty fast, an outside user can try up to
# 1 million passwords per second against a modern box. This means that you # 1 million passwords per second against a modern box. This means that you
# should use very strong passwords, otherwise they will be very easy to break. # should use very strong passwords, otherwise they will be very easy to break.
# Note that because the password is really a shared secret between the client # Note that because the password is really a shared secret between the client
@ -701,7 +708,7 @@ replica-priority 100
# AUTH (or the HELLO command AUTH option) in order to be authenticated and # AUTH (or the HELLO command AUTH option) in order to be authenticated and
# start to work. # start to work.
# #
# The ACL rules that describe what an user can do are the following: # The ACL rules that describe what a user can do are the following:
# #
# on Enable the user: it is possible to authenticate as this user. # on Enable the user: it is possible to authenticate as this user.
# off Disable the user: it's no longer possible to authenticate # off Disable the user: it's no longer possible to authenticate
@ -711,7 +718,7 @@ replica-priority 100
# -<command> Disallow the execution of that command # -<command> Disallow the execution of that command
# +@<category> Allow the execution of all the commands in such category # +@<category> Allow the execution of all the commands in such category
# with valid categories are like @admin, @set, @sortedset, ... # with valid categories are like @admin, @set, @sortedset, ...
# and so forth, see the full list in the server.c file where # and so forth, see the full list in the server.cpp file where
# the KeyDB command table is described and defined. # the KeyDB command table is described and defined.
# The special category @all means all the commands, but currently # The special category @all means all the commands, but currently
# present in the server, and that will be loaded in the future # present in the server, and that will be loaded in the future
@ -729,7 +736,7 @@ replica-priority 100
# It is possible to specify multiple patterns. # It is possible to specify multiple patterns.
# allkeys Alias for ~* # allkeys Alias for ~*
# resetkeys Flush the list of allowed keys patterns. # resetkeys Flush the list of allowed keys patterns.
# ><password> Add this passowrd to the list of valid password for the user. # ><password> Add this password to the list of valid password for the user.
# For example >mypass will add "mypass" to the list. # For example >mypass will add "mypass" to the list.
# This directive clears the "nopass" flag (see later). # This directive clears the "nopass" flag (see later).
# <<password> Remove this password from the list of valid passwords. # <<password> Remove this password from the list of valid passwords.
@ -775,16 +782,15 @@ replica-priority 100
# #
# The ACL Log tracks failed commands and authentication events associated # The ACL Log tracks failed commands and authentication events associated
# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked # with ACLs. The ACL Log is useful to troubleshoot failed commands blocked
# by ACLs. The ACL Log is stored in and consumes memory. There is no limit # by ACLs. The ACL Log is stored in memory. You can reclaim memory with
# to its length.You can reclaim memory with ACL LOG RESET or set a maximum # ACL LOG RESET. Define the maximum entry length of the ACL Log below.
# length below.
acllog-max-len 128 acllog-max-len 128
# Using an external ACL file # Using an external ACL file
# #
# Instead of configuring users here in this file, it is possible to use # Instead of configuring users here in this file, it is possible to use
# a stand-alone file just listing users. The two methods cannot be mixed: # a stand-alone file just listing users. The two methods cannot be mixed:
# if you configure users here and at the same time you activate the exteranl # if you configure users here and at the same time you activate the external
# ACL file, the server will refuse to start. # ACL file, the server will refuse to start.
# #
# The format of the external ACL user file is exactly the same as the # The format of the external ACL user file is exactly the same as the
@ -792,7 +798,7 @@ acllog-max-len 128
# #
# aclfile /etc/keydb/users.acl # aclfile /etc/keydb/users.acl
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatiblity # IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting # layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using # the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password> # AUTH <password> as usually, or more explicitly with AUTH default <password>
@ -836,6 +842,11 @@ acllog-max-len 128
# Once the limit is reached KeyDB will close all the new connections sending # Once the limit is reached KeyDB will close all the new connections sending
# an error 'max number of clients reached'. # an error 'max number of clients reached'.
# #
# IMPORTANT: When Redis Cluster is used, the max number of connections is also
# shared with the cluster bus: every node in the cluster will use two
# connections, one incoming and another outgoing. It is important to size the
# limit accordingly in case of very large clusters.
#
# maxclients 10000 # maxclients 10000
############################## MEMORY MANAGEMENT ################################ ############################## MEMORY MANAGEMENT ################################
@ -898,8 +909,8 @@ acllog-max-len 128
# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated # LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can tune it for speed or # algorithms (in order to save memory), so you can tune it for speed or
# accuracy. For default KeyDB will check five keys and pick the one that was # accuracy. By default KeyDB will check five keys and pick the one that was
# used less recently, you can change the sample size using the following # used least recently, you can change the sample size using the following
# configuration directive. # configuration directive.
# #
# The default of 5 produces good enough results. 10 Approximates very closely # The default of 5 produces good enough results. 10 Approximates very closely
@ -927,7 +938,7 @@ acllog-max-len 128
# #
# replica-ignore-maxmemory yes # replica-ignore-maxmemory yes
# Redis reclaims expired keys in two ways: upon access when those keys are # KeyDB reclaims expired keys in two ways: upon access when those keys are
# found to be expired, and also in background, in what is called the # found to be expired, and also in background, in what is called the
# "active expire key". The key space is slowly and interactively scanned # "active expire key". The key space is slowly and interactively scanned
# looking for expired keys to reclaim, so that it is possible to free memory # looking for expired keys to reclaim, so that it is possible to free memory
@ -939,8 +950,8 @@ acllog-max-len 128
# it is possible to increase the expire "effort" that is normally set to # it is possible to increase the expire "effort" that is normally set to
# "1", to a greater value, up to the value "10". At its maximum value the # "1", to a greater value, up to the value "10". At its maximum value the
# system will use more CPU, longer cycles (and technically may introduce # system will use more CPU, longer cycles (and technically may introduce
# more latency), and will tollerate less already expired keys still present # more latency), and will tolerate less already expired keys still present
# in the system. It's a tradeoff betweeen memory, CPU and latecy. # in the system. It's a tradeoff between memory, CPU and latency.
# #
# active-expire-effort 1 # active-expire-effort 1
@ -1000,51 +1011,36 @@ replica-lazy-flush no
lazyfree-lazy-user-del no lazyfree-lazy-user-del no
################################ THREADED I/O ################################# ############################ KERNEL OOM CONTROL ##############################
# Redis is mostly single threaded, however there are certain threaded # On Linux, it is possible to hint the kernel OOM killer on what processes
# operations such as UNLINK, slow I/O accesses and other things that are # should be killed first when out of memory.
# performed on side threads.
# #
# Now it is also possible to handle Redis clients socket reads and writes # Enabling this feature makes KeyDB actively control the oom_score_adj value
# in different I/O threads. Since especially writing is so slow, normally # for all its processes, depending on their role. The default scores will
# Redis users use pipelining in order to speedup the Redis performances per # attempt to have background child processes killed before all others, and
# core, and spawn multiple instances in order to scale more. Using I/O # replicas killed before masters.
# threads it is possible to easily speedup two times Redis without resorting
# to pipelining nor sharding of the instance.
# #
# By default threading is disabled, we suggest enabling it only in machines # KeyDB supports three options:
# that have at least 4 or more cores, leaving at least one spare core.
# Using more than 8 threads is unlikely to help much. We also recommend using
# threaded I/O only if you actually have performance problems, with Redis
# instances being able to use a quite big percentage of CPU time, otherwise
# there is no point in using this feature.
# #
# So for instance if you have a four cores boxes, try to use 2 or 3 I/O # no: Don't make changes to oom-score-adj (default).
# threads, if you have a 8 cores, try to use 6 threads. In order to # yes: Alias to "relative" see below.
# enable I/O threads use the following configuration directive: # absolute: Values in oom-score-adj-values are written as is to the kernel.
# relative: Values are used relative to the initial value of oom_score_adj when
# the server starts and are then clamped to a range of -1000 to 1000.
# Because typically the initial value is 0, they will often match the
# absolute values.
oom-score-adj no
# When oom-score-adj is used, this directive controls the specific values used
# for master, replica and background child processes. Values range -2000 to
# 2000 (higher means more likely to be killed).
# #
# io-threads 4 # Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities)
# # can freely increase their value, but not decrease it below its initial
# Setting io-threads to 1 will just use the main thread as usually. # settings. This means that setting oom-score-adj to "relative" and setting the
# When I/O threads are enabled, we only use threads for writes, that is # oom-score-adj-values to positive values will always succeed.
# to thread the write(2) syscall and transfer the client buffers to the oom-score-adj-values 0 200 800
# socket. However it is also possible to enable threading of reads and
# protocol parsing using the following configuration directive, by setting
# it to yes:
#
# io-threads-do-reads no
#
# Usually threading reads doesn't help much.
#
# NOTE 1: This configuration directive cannot be changed at runtime via
# CONFIG SET. Aso this feature currently does not work when SSL is
# enabled.
#
# NOTE 2: If you want to test the Redis speedup using redis-benchmark, make
# sure you also run the benchmark itself in threaded mode, using the
# --threads option to match the number of Redis theads, otherwise you'll not
# be able to notice the improvements.
############################## APPEND ONLY MODE ############################### ############################## APPEND ONLY MODE ###############################
@ -1170,8 +1166,8 @@ aof-load-truncated yes
# #
# [RDB file][AOF tail] # [RDB file][AOF tail]
# #
# When loading KeyDB recognizes that the AOF file starts with the "REDIS" # When loading, KeyDB recognizes that the AOF file starts with the "REDIS"
# string and loads the prefixed RDB file, and continues loading the AOF # string and loads the prefixed RDB file, then continues loading the AOF
# tail. # tail.
aof-use-rdb-preamble yes aof-use-rdb-preamble yes
@ -1185,7 +1181,7 @@ aof-use-rdb-preamble yes
# #
# When a long running script exceeds the maximum execution time only the # When a long running script exceeds the maximum execution time only the
# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be # SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be
# used to stop a script that did not yet called write commands. The second # used to stop a script that did not yet call any write commands. The second
# is the only way to shut down the server in the case a write command was # is the only way to shut down the server in the case a write command was
# already issued by the script but the user doesn't want to wait for the natural # already issued by the script but the user doesn't want to wait for the natural
# termination of the script. # termination of the script.
@ -1211,7 +1207,7 @@ lua-time-limit 5000
# Cluster node timeout is the amount of milliseconds a node must be unreachable # Cluster node timeout is the amount of milliseconds a node must be unreachable
# for it to be considered in failure state. # for it to be considered in failure state.
# Most other internal time limits are multiple of the node timeout. # Most other internal time limits are a multiple of the node timeout.
# #
# cluster-node-timeout 15000 # cluster-node-timeout 15000
@ -1238,18 +1234,18 @@ lua-time-limit 5000
# the failover if, since the last interaction with the master, the time # the failover if, since the last interaction with the master, the time
# elapsed is greater than: # elapsed is greater than:
# #
# (node-timeout * replica-validity-factor) + repl-ping-replica-period # (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period
# #
# So for example if node-timeout is 30 seconds, and the replica-validity-factor # So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor
# is 10, and assuming a default repl-ping-replica-period of 10 seconds, the # is 10, and assuming a default repl-ping-replica-period of 10 seconds, the
# replica will not try to failover if it was not able to talk with the master # replica will not try to failover if it was not able to talk with the master
# for longer than 310 seconds. # for longer than 310 seconds.
# #
# A large replica-validity-factor may allow replicas with too old data to failover # A large cluster-replica-validity-factor may allow replicas with too old data to failover
# a master, while a too small value may prevent the cluster from being able to # a master, while a too small value may prevent the cluster from being able to
# elect a replica at all. # elect a replica at all.
# #
# For maximum availability, it is possible to set the replica-validity-factor # For maximum availability, it is possible to set the cluster-replica-validity-factor
# to a value of 0, which means, that replicas will always try to failover the # to a value of 0, which means, that replicas will always try to failover the
# master regardless of the last time they interacted with the master. # master regardless of the last time they interacted with the master.
# (However they'll always try to apply a delay proportional to their # (However they'll always try to apply a delay proportional to their
@ -1280,7 +1276,7 @@ lua-time-limit 5000
# cluster-migration-barrier 1 # cluster-migration-barrier 1
# By default KeyDB Cluster nodes stop accepting queries if they detect there # By default KeyDB Cluster nodes stop accepting queries if they detect there
# is at least an hash slot uncovered (no available node is serving it). # is at least a hash slot uncovered (no available node is serving it).
# This way if the cluster is partially down (for example a range of hash slots # This way if the cluster is partially down (for example a range of hash slots
# are no longer covered) all the cluster becomes, eventually, unavailable. # are no longer covered) all the cluster becomes, eventually, unavailable.
# It automatically returns available as soon as all the slots are covered again. # It automatically returns available as soon as all the slots are covered again.
@ -1335,7 +1331,7 @@ lua-time-limit 5000
# * cluster-announce-port # * cluster-announce-port
# * cluster-announce-bus-port # * cluster-announce-bus-port
# #
# Each instruct the node about its address, client port, and cluster message # Each instructs the node about its address, client port, and cluster message
# bus port. The information is then published in the header of the bus packets # bus port. The information is then published in the header of the bus packets
# so that other nodes will be able to correctly map the address of the node # so that other nodes will be able to correctly map the address of the node
# publishing the information. # publishing the information.
@ -1346,7 +1342,7 @@ lua-time-limit 5000
# Note that when remapped, the bus port may not be at the fixed offset of # Note that when remapped, the bus port may not be at the fixed offset of
# clients port + 10000, so you can specify any port and bus-port depending # clients port + 10000, so you can specify any port and bus-port depending
# on how they get remapped. If the bus-port is not set, a fixed offset of # on how they get remapped. If the bus-port is not set, a fixed offset of
# 10000 will be used as usually. # 10000 will be used as usual.
# #
# Example: # Example:
# #
@ -1449,61 +1445,6 @@ latency-monitor-threshold 0
# specify at least one of K or E, no events will be delivered. # specify at least one of K or E, no events will be delivered.
notify-keyspace-events "" notify-keyspace-events ""
############################### GOPHER SERVER #################################
# KeyDB contains an implementation of the Gopher protocol, as specified in
# the RFC 1436 (https://www.ietf.org/rfc/rfc1436.txt).
#
# The Gopher protocol was very popular in the late '90s. It is an alternative
# to the web, and the implementation both server and client side is so simple
# that the KeyDB server has just 100 lines of code in order to implement this
# support.
#
# What do you do with Gopher nowadays? Well Gopher never *really* died, and
# lately there is a movement in order for the Gopher more hierarchical content
# composed of just plain text documents to be resurrected. Some want a simpler
# internet, others believe that the mainstream internet became too much
# controlled, and it's cool to create an alternative space for people that
# want a bit of fresh air.
#
# Anyway for the 10nth birthday of the KeyDB, we gave it the Gopher protocol
# as a gift.
#
# --- HOW IT WORKS? ---
#
# The KeyDB Gopher support uses the inline protocol of KeyDB, and specifically
# two kind of inline requests that were anyway illegal: an empty request
# or any request that starts with "/" (there are no KeyDB commands starting
# with such a slash). Normal RESP2/RESP3 requests are completely out of the
# path of the Gopher protocol implementation and are served as usually as well.
#
# If you open a connection to KeyDB when Gopher is enabled and send it
# a string like "/foo", if there is a key named "/foo" it is served via the
# Gopher protocol.
#
# In order to create a real Gopher "hole" (the name of a Gopher site in Gopher
# talking), you likely need a script like the following:
#
# https://github.com/antirez/gopher2redis
#
# --- SECURITY WARNING ---
#
# If you plan to put KeyDB on the internet in a publicly accessible address
# to server Gopher pages MAKE SURE TO SET A PASSWORD to the instance.
# Once a password is set:
#
# 1. The Gopher server (when enabled, not by default) will still serve
# content via Gopher.
# 2. However other commands cannot be called before the client will
# authenticate.
#
# So use the 'requirepass' option to protect your instance.
#
# To enable Gopher support uncomment the following line and set
# the option from no (the default) to yes.
#
# gopher-enabled no
############################### ADVANCED CONFIG ############################### ############################### ADVANCED CONFIG ###############################
# Hashes are encoded using a memory efficient data structure when they have a # Hashes are encoded using a memory efficient data structure when they have a
@ -1647,8 +1588,8 @@ client-output-buffer-limit pubsub 32mb 8mb 60
# client-query-buffer-limit 1gb # client-query-buffer-limit 1gb
# In the KeyDB protocol, bulk requests, that are, elements representing single # In the KeyDB protocol, bulk requests, that are, elements representing single
# strings, are normally limited ot 512 mb. However you can change this limit # strings, are normally limited to 512 mb. However you can change this limit
# here. # here, but must be 1mb or greater
# #
# proto-max-bulk-len 512mb # proto-max-bulk-len 512mb
@ -1676,7 +1617,7 @@ hz 10
# #
# Since the default HZ value by default is conservatively set to 10, KeyDB # Since the default HZ value by default is conservatively set to 10, KeyDB
# offers, and enables by default, the ability to use an adaptive HZ value # offers, and enables by default, the ability to use an adaptive HZ value
# which will temporary raise when there are many connected clients. # which will temporarily raise when there are many connected clients.
# #
# When dynamic HZ is enabled, the actual configured HZ will be used # When dynamic HZ is enabled, the actual configured HZ will be used
# as a baseline, but multiples of the configured HZ value will be actually # as a baseline, but multiples of the configured HZ value will be actually
@ -1743,7 +1684,7 @@ rdb-save-incremental-fsync yes
# for the key counter to be divided by two (or decremented if it has a value # for the key counter to be divided by two (or decremented if it has a value
# less <= 10). # less <= 10).
# #
# The default value for the lfu-decay-time is 1. A Special value of 0 means to # The default value for the lfu-decay-time is 1. A special value of 0 means to
# decay the counter every time it happens to be scanned. # decay the counter every time it happens to be scanned.
# #
# lfu-log-factor 10 # lfu-log-factor 10
@ -1763,7 +1704,7 @@ rdb-save-incremental-fsync yes
# restart is needed in order to lower the fragmentation, or at least to flush # restart is needed in order to lower the fragmentation, or at least to flush
# away all the data and create it again. However thanks to this feature # away all the data and create it again. However thanks to this feature
# implemented by Oran Agra for Redis 4.0 this process can happen at runtime # implemented by Oran Agra for Redis 4.0 this process can happen at runtime
# in an "hot" way, while the server is running. # in a "hot" way, while the server is running.
# #
# Basically when the fragmentation is over a certain level (see the # Basically when the fragmentation is over a certain level (see the
# configuration options below) KeyDB will start to create new copies of the # configuration options below) KeyDB will start to create new copies of the
@ -1816,14 +1757,14 @@ rdb-save-incremental-fsync yes
# Jemalloc background thread for purging will be enabled by default # Jemalloc background thread for purging will be enabled by default
jemalloc-bg-thread yes jemalloc-bg-thread yes
# It is possible to pin different threads and processes of Redis to specific # It is possible to pin different threads and processes of KeyDB to specific
# CPUs in your system, in order to maximize the performances of the server. # CPUs in your system, in order to maximize the performances of the server.
# This is useful both in order to pin different Redis threads in different # This is useful both in order to pin different KeyDB threads in different
# CPUs, but also in order to make sure that multiple Redis instances running # CPUs, but also in order to make sure that multiple KeyDB instances running
# in the same host will be pinned to different CPUs. # in the same host will be pinned to different CPUs.
# #
# Normally you can do this using the "taskset" command, however it is also # Normally you can do this using the "taskset" command, however it is also
# possible to this via Redis configuration directly, both in Linux and FreeBSD. # possible to this via KeyDB configuration directly, both in Linux and FreeBSD.
# #
# You can pin the server/IO threads, bio threads, aof rewrite child process, and # You can pin the server/IO threads, bio threads, aof rewrite child process, and
# the bgsave child process. The syntax to specify the cpu list is the same as # the bgsave child process. The syntax to specify the cpu list is the same as
@ -1841,10 +1782,25 @@ jemalloc-bg-thread yes
# Set bgsave child process to cpu affinity 1,10,11 # Set bgsave child process to cpu affinity 1,10,11
# bgsave_cpulist 1,10-11 # bgsave_cpulist 1,10-11
# In some cases KeyDB will emit warnings and even refuse to start if it detects
# that the system is in bad state, it is possible to suppress these warnings
# by setting the following config which takes a space delimited list of warnings
# to suppress
#
# ignore-warnings ARM64-COW-BUG
# The minimum number of clients on a thread before KeyDB assigns new connections to a different thread # The minimum number of clients on a thread before KeyDB assigns new connections to a different thread
# Tuning this parameter is a tradeoff between locking overhead and distributing the workload over multiple cores # Tuning this parameter is a tradeoff between locking overhead and distributing the workload over multiple cores
# min-clients-per-thread 50 # min-clients-per-thread 50
# How often to run RDB load progress callback?
# The callback runs during key load to ping other servers and prevent timeouts.
# It also updates load time estimates.
# Change these values to run it more or less often. It will run when either condition is true.
# Either when x bytes have been processed, or when x keys have been loaded.
# loading-process-events-interval-bytes 2097152
# loading-process-events-interval-keys 8192
# Avoid forwarding RREPLAY messages to other masters? # Avoid forwarding RREPLAY messages to other masters?
# WARNING: This setting is dangerous! You must be certain all masters are connected to each # WARNING: This setting is dangerous! You must be certain all masters are connected to each
# other in a true mesh topology or data loss will occur! # other in a true mesh topology or data loss will occur!
@ -1859,6 +1815,8 @@ jemalloc-bg-thread yes
# Number of worker threads serving requests. This number should be related to the performance # Number of worker threads serving requests. This number should be related to the performance
# of your network hardware, not the number of cores on your machine. We don't recommend going # of your network hardware, not the number of cores on your machine. We don't recommend going
# above 4 at this time. By default this is set 1. # above 4 at this time. By default this is set 1.
#
# Note: KeyDB does not use io-threads, but io-threads is a config alias for server-threads
server-threads 2 server-threads 2
# Should KeyDB pin threads to CPUs? By default this is disabled, and KeyDB will not bind threads. # Should KeyDB pin threads to CPUs? By default this is disabled, and KeyDB will not bind threads.

View File

@ -25,4 +25,8 @@ $TCLSH tests/test_helper.tcl \
--single unit/moduleapi/scan \ --single unit/moduleapi/scan \
--single unit/moduleapi/datatype \ --single unit/moduleapi/datatype \
--single unit/moduleapi/auth \ --single unit/moduleapi/auth \
--single unit/moduleapi/keyspace_events \
--single unit/moduleapi/blockedclient \
--single unit/moduleapi/moduleloadsave \
--single unit/moduleapi/getkeys \
"${@}" "${@}"

View File

@ -259,6 +259,6 @@ sentinel deny-scripts-reconfig yes
# SENTINEL SET can also be used in order to perform this configuration at runtime. # SENTINEL SET can also be used in order to perform this configuration at runtime.
# #
# In order to set a command back to its original name (undo the renaming), it # In order to set a command back to its original name (undo the renaming), it
# is possible to just rename a command to itsef: # is possible to just rename a command to itself:
# #
# SENTINEL rename-command mymaster CONFIG CONFIG # SENTINEL rename-command mymaster CONFIG CONFIG

1
src/.gitignore vendored
View File

@ -2,4 +2,5 @@
*.gcno *.gcno
*.gcov *.gcov
redis.info redis.info
KeyDB.info
lcov-html lcov-html

View File

@ -47,7 +47,7 @@ endif
USEASM?=true USEASM?=true
ifneq ($(SANITIZE),) ifneq ($(strip $(SANITIZE)),)
CFLAGS+= -fsanitize=$(SANITIZE) -DSANITIZE CFLAGS+= -fsanitize=$(SANITIZE) -DSANITIZE
CXXFLAGS+= -fsanitize=$(SANITIZE) -DSANITIZE CXXFLAGS+= -fsanitize=$(SANITIZE) -DSANITIZE
LDFLAGS+= -fsanitize=$(SANITIZE) LDFLAGS+= -fsanitize=$(SANITIZE)
@ -107,7 +107,7 @@ endif
FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS) FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS)
FINAL_CXXFLAGS=$(CXX_STD) $(WARN) $(OPT) $(DEBUG) $(CXXFLAGS) $(REDIS_CFLAGS) FINAL_CXXFLAGS=$(CXX_STD) $(WARN) $(OPT) $(DEBUG) $(CXXFLAGS) $(REDIS_CFLAGS)
FINAL_LDFLAGS=$(LDFLAGS) $(REDIS_LDFLAGS) $(DEBUG) FINAL_LDFLAGS=$(LDFLAGS) $(REDIS_LDFLAGS) $(DEBUG)
FINAL_LIBS+=-lm -lcurl FINAL_LIBS+=-lm
DEBUG=-g -ggdb DEBUG=-g -ggdb
ifneq ($(uname_S),Darwin) ifneq ($(uname_S),Darwin)
@ -152,12 +152,21 @@ ifeq ($(uname_S),OpenBSD)
endif endif
else else
ifeq ($(uname_S),NetBSD)
# NetBSD
FINAL_LIBS+= -lpthread
ifeq ($(USE_BACKTRACE),yes)
FINAL_CFLAGS+= -DUSE_BACKTRACE -I/usr/pkg/include
FINAL_LDFLAGS+= -L/usr/pkg/lib
FINAL_LIBS+= -lexecinfo
endif
else
ifeq ($(uname_S),FreeBSD) ifeq ($(uname_S),FreeBSD)
# FreeBSD # FreeBSD
FINAL_LIBS+= -lpthread -lexecinfo FINAL_LIBS+= -lpthread -lexecinfo
else else
ifeq ($(uname_S),DragonFly) ifeq ($(uname_S),DragonFly)
# FreeBSD # DragonFly
FINAL_LIBS+= -lpthread -lexecinfo FINAL_LIBS+= -lpthread -lexecinfo
else else
ifeq ($(uname_S),OpenBSD) ifeq ($(uname_S),OpenBSD)
@ -167,12 +176,23 @@ else
ifeq ($(uname_S),NetBSD) ifeq ($(uname_S),NetBSD)
# NetBSD # NetBSD
FINAL_LIBS+= -lpthread -lexecinfo FINAL_LIBS+= -lpthread -lexecinfo
else
ifeq ($(uname_S),Haiku)
# Haiku
FINAL_CFLAGS+= -DBSD_SOURCE
FINAL_LDFLAGS+= -lbsd -lnetwork
FINAL_LIBS+= -lpthread
else else
# All the other OSes (notably Linux) # All the other OSes (notably Linux)
FINAL_LDFLAGS+= -rdynamic FINAL_LDFLAGS+= -rdynamic
FINAL_LIBS+=-ldl -pthread -lrt -luuid FINAL_LIBS+=-ldl -pthread -lrt -luuid
ifneq ($(NO_MOTD),yes)
FINAL_CFLAGS += -DMOTD FINAL_CFLAGS += -DMOTD
FINAL_CXXFLAGS += -DMOTD FINAL_CXXFLAGS += -DMOTD
FINAL_LIBS+=-lcurl
endif
endif
endif
endif endif
endif endif
endif endif
@ -236,10 +256,23 @@ ifeq ($(MALLOC),memkind)
endif endif
ifeq ($(BUILD_TLS),yes) ifeq ($(BUILD_TLS),yes)
FINAL_CFLAGS+=-DUSE_OPENSSL $(OPENSSL_CXXFLAGS) FINAL_CFLAGS+=-DUSE_OPENSSL $(OPENSSL_CXXFLAGS)
FINAL_CXXFLAGS+=-DUSE_OPENSSL $(OPENSSL_CXXFLAGS) FINAL_CXXFLAGS+=-DUSE_OPENSSL $(OPENSSL_CXXFLAGS)
FINAL_LDFLAGS+=$(OPENSSL_LDFLAGS) FINAL_LDFLAGS+=$(OPENSSL_LDFLAGS)
FINAL_LIBS += ../deps/hiredis/libhiredis_ssl.a -lssl -lcrypto FINAL_LIBS += ../deps/hiredis/libhiredis_ssl.a -lssl -lcrypto
LIBSSL_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libssl && echo $$?)
ifeq ($(LIBSSL_PKGCONFIG),0)
LIBSSL_LIBS=$(shell $(PKG_CONFIG) --libs libssl)
else
LIBSSL_LIBS=-lssl
endif
LIBCRYPTO_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libcrypto && echo $$?)
ifeq ($(LIBCRYPTO_PKGCONFIG),0)
LIBCRYPTO_LIBS=$(shell $(PKG_CONFIG) --libs libcrypto)
else
LIBCRYPTO_LIBS=-lcrypto
endif
FINAL_LIBS += ../deps/hiredis/libhiredis_ssl.a $(LIBSSL_LIBS) $(LIBCRYPTO_LIBS)
endif endif
REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS) REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS)
@ -261,15 +294,15 @@ QUIET_LINK = @printf ' %b %b\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$@$(EN
QUIET_INSTALL = @printf ' %b %b\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR); QUIET_INSTALL = @printf ' %b %b\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR);
endif endif
REDIS_SERVER_NAME=keydb-server REDIS_SERVER_NAME=keydb-server$(PROG_SUFFIX)
REDIS_SENTINEL_NAME=keydb-sentinel REDIS_SENTINEL_NAME=keydb-sentinel$(PROG_SUFFIX)
REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crcspeed.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o connection.o tls.o sha256.o motd.o timeout.o setcpuaffinity.o $(ASM_OBJ) REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o t_nhash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crcspeed.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o connection.o tls.o sha256.o motd.o timeout.o setcpuaffinity.o $(ASM_OBJ)
REDIS_CLI_NAME=keydb-cli REDIS_CLI_NAME=keydb-cli$(PROG_SUFFIX)
REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o redis-cli-cpphelper.o zmalloc.o release.o anet.o ae.o crcspeed.o crc64.o siphash.o crc16.o storage-lite.o fastlock.o new.o motd.o $(ASM_OBJ) REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o redis-cli-cpphelper.o zmalloc.o release.o anet.o ae.o crcspeed.o crc64.o siphash.o crc16.o storage-lite.o fastlock.o new.o motd.o $(ASM_OBJ)
REDIS_BENCHMARK_NAME=keydb-benchmark REDIS_BENCHMARK_NAME=keydb-benchmark$(PROG_SUFFIX)
REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o dict.o zmalloc.o siphash.o redis-benchmark.o storage-lite.o fastlock.o new.o $(ASM_OBJ) REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o dict.o zmalloc.o siphash.o redis-benchmark.o storage-lite.o fastlock.o new.o $(ASM_OBJ)
REDIS_CHECK_RDB_NAME=keydb-check-rdb REDIS_CHECK_RDB_NAME=keydb-check-rdb$(PROG_SUFFIX)
REDIS_CHECK_AOF_NAME=keydb-check-aof REDIS_CHECK_AOF_NAME=keydb-check-aof$(PROG_SUFFIX)
all: $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME) all: $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME)
@echo "" @echo ""
@ -290,6 +323,8 @@ persist-settings: distclean
echo WARN=$(WARN) >> .make-settings echo WARN=$(WARN) >> .make-settings
echo OPT=$(OPT) >> .make-settings echo OPT=$(OPT) >> .make-settings
echo MALLOC=$(MALLOC) >> .make-settings echo MALLOC=$(MALLOC) >> .make-settings
echo BUILD_TLS=$(BUILD_TLS) >> .make-settings
echo USE_SYSTEMD=$(USE_SYSTEMD) >> .make-settings
echo CFLAGS=$(CFLAGS) >> .make-settings echo CFLAGS=$(CFLAGS) >> .make-settings
echo CXXFLAGS=$(CXXFLAGS) >> .make-settings echo CXXFLAGS=$(CXXFLAGS) >> .make-settings
echo LDFLAGS=$(LDFLAGS) >> .make-settings echo LDFLAGS=$(LDFLAGS) >> .make-settings
@ -360,7 +395,7 @@ DEP = $(REDIS_SERVER_OBJ:%.o=%.d) $(REDIS_CLI_OBJ:%.o=%.d) $(REDIS_BENCHMARK_OBJ
$(KEYDB_AS) $< -o $@ $(KEYDB_AS) $< -o $@
clean: clean:
rm -rf $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME) *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark rm -rf $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME) *.o *.gcda *.gcno *.gcov KeyDB.info lcov-html Makefile.dep dict-benchmark
rm -f $(DEP) rm -f $(DEP)
.PHONY: clean .PHONY: clean
@ -382,9 +417,10 @@ check: test
lcov: lcov:
$(MAKE) gcov $(MAKE) gcov
@(set -e; cd ..; ./runtest --clients 1) @(set -e; cd ..; ./runtest --config server-threads 3; ./runtest-sentinel; ./runtest-cluster; ./runtest-moduleapi)
@geninfo -o redis.info . @geninfo -o KeyDB.info --no-external .
@genhtml --legend -o lcov-html redis.info @genhtml --legend -o lcov-html KeyDB.info
@genhtml --legend -o lcov-html KeyDB.info | grep lines | awk '{print $$2;}' | sed 's/%//g'
test-sds: sds.c sds.h test-sds: sds.c sds.h
$(REDIS_CC) sds.c zmalloc.cpp -DSDS_TEST_MAIN $(FINAL_LIBS) -o /tmp/sds_test $(REDIS_CC) sds.c zmalloc.cpp -DSDS_TEST_MAIN $(FINAL_LIBS) -o /tmp/sds_test

View File

@ -300,7 +300,13 @@ void ACLFreeUserAndKillClients(user *u) {
* it in non authenticated mode. */ * it in non authenticated mode. */
c->puser = DefaultUser; c->puser = DefaultUser;
c->authenticated = 0; c->authenticated = 0;
freeClientAsync(c); /* We will write replies to this client later, so we can't
* close it directly even if async. */
if (c == serverTL->current_client) {
c->flags |= CLIENT_CLOSE_AFTER_COMMAND;
} else {
freeClientAsync(c);
}
} }
} }
ACLFreeUser(u); ACLFreeUser(u);
@ -377,7 +383,7 @@ int ACLUserCanExecuteFutureCommands(user *u) {
* zero, the user flag ALLCOMMANDS is cleared since it is no longer possible * zero, the user flag ALLCOMMANDS is cleared since it is no longer possible
* to skip the command bit explicit test. */ * to skip the command bit explicit test. */
void ACLSetUserCommandBit(user *u, unsigned long id, int value) { void ACLSetUserCommandBit(user *u, unsigned long id, int value) {
uint64_t word, bit; uint64_t word=0, bit=0;
if (ACLGetCommandBitCoordinates(id,&word,&bit) == C_ERR) return; if (ACLGetCommandBitCoordinates(id,&word,&bit) == C_ERR) return;
if (value) { if (value) {
u->allowed_commands[word] |= bit; u->allowed_commands[word] |= bit;
@ -472,21 +478,68 @@ sds ACLDescribeUserCommandRules(user *u) {
ACLSetUser(fakeuser,"-@all",-1); ACLSetUser(fakeuser,"-@all",-1);
} }
/* Try to add or subtract each category one after the other. Often a /* Attempt to find a good approximation for categories and commands
* single category will not perfectly match the set of commands into * based on the current bits used, by looping over the category list
* it, so at the end we do a final pass adding/removing the single commands * and applying the best fit each time. Often a set of categories will not
* needed to make the bitmap exactly match. */ * perfectly match the set of commands into it, so at the end we do a
for (int j = 0; ACLCommandCategories[j].flag != 0; j++) { * final pass adding/removing the single commands needed to make the bitmap
unsigned long on, off; * exactly match. A temp user is maintained to keep track of categories
ACLCountCategoryBitsForUser(u,&on,&off,ACLCommandCategories[j].name); * already applied. */
if ((additive && on > off) || (!additive && off > on)) { user tu = {0};
sds op = sdsnewlen(additive ? "+@" : "-@", 2); user *tempuser = &tu;
op = sdscat(op,ACLCommandCategories[j].name);
ACLSetUser(fakeuser,op,-1); /* Keep track of the categories that have been applied, to prevent
rules = sdscatsds(rules,op); * applying them twice. */
rules = sdscatlen(rules," ",1); char applied[sizeof(ACLCommandCategories)/sizeof(ACLCommandCategories[0])];
sdsfree(op); memset(applied, 0, sizeof(applied));
memcpy(tempuser->allowed_commands,
u->allowed_commands,
sizeof(u->allowed_commands));
while (1) {
int best = -1;
unsigned long mindiff = INT_MAX, maxsame = 0;
for (int j = 0; ACLCommandCategories[j].flag != 0; j++) {
if (applied[j]) continue;
unsigned long on, off, diff, same;
ACLCountCategoryBitsForUser(tempuser,&on,&off,ACLCommandCategories[j].name);
/* Check if the current category is the best this loop:
* * It has more commands in common with the user than commands
* that are different.
* AND EITHER
* * It has the fewest number of differences
* than the best match we have found so far.
* * OR it matches the fewest number of differences
* that we've seen but it has more in common. */
diff = additive ? off : on;
same = additive ? on : off;
if (same > diff &&
((diff < mindiff) || (diff == mindiff && same > maxsame)))
{
best = j;
mindiff = diff;
maxsame = same;
}
} }
/* We didn't find a match */
if (best == -1) break;
sds op = sdsnewlen(additive ? "+@" : "-@", 2);
op = sdscat(op,ACLCommandCategories[best].name);
ACLSetUser(fakeuser,op,-1);
sds invop = sdsnewlen(additive ? "-@" : "+@", 2);
invop = sdscat(invop,ACLCommandCategories[best].name);
ACLSetUser(tempuser,invop,-1);
rules = sdscatsds(rules,op);
rules = sdscatlen(rules," ",1);
sdsfree(op);
sdsfree(invop);
applied[best] = 1;
} }
/* Fix the final ACLs with single commands differences. */ /* Fix the final ACLs with single commands differences. */
@ -670,8 +723,8 @@ void ACLAddAllowedSubcommand(user *u, unsigned long id, const char *sub) {
* -<command> Disallow the execution of that command * -<command> Disallow the execution of that command
* +@<category> Allow the execution of all the commands in such category * +@<category> Allow the execution of all the commands in such category
* with valid categories are like @admin, @set, @sortedset, ... * with valid categories are like @admin, @set, @sortedset, ...
* and so forth, see the full list in the server.c file where * and so forth, see the full list in the server.cpp file where
* the Redis command table is described and defined. * the KeyDB command table is described and defined.
* The special category @all means all the commands, but currently * The special category @all means all the commands, but currently
* present in the server, and that will be loaded in the future * present in the server, and that will be loaded in the future
* via modules. * via modules.
@ -1099,8 +1152,9 @@ int ACLCheckCommandPerm(client *c, int *keyidxptr) {
if (!(c->puser->flags & USER_FLAG_ALLKEYS) && if (!(c->puser->flags & USER_FLAG_ALLKEYS) &&
(c->cmd->getkeys_proc || c->cmd->firstkey)) (c->cmd->getkeys_proc || c->cmd->firstkey))
{ {
int numkeys; getKeysResult result = GETKEYS_RESULT_INIT;
int *keyidx = getKeysFromCommand(c->cmd,c->argv,c->argc,&numkeys); int numkeys = getKeysFromCommand(c->cmd,c->argv,c->argc,&result);
int *keyidx = result.keys;
for (int j = 0; j < numkeys; j++) { for (int j = 0; j < numkeys; j++) {
listIter li; listIter li;
listNode *ln; listNode *ln;
@ -1121,11 +1175,11 @@ int ACLCheckCommandPerm(client *c, int *keyidxptr) {
} }
if (!match) { if (!match) {
if (keyidxptr) *keyidxptr = keyidx[j]; if (keyidxptr) *keyidxptr = keyidx[j];
getKeysFreeResult(keyidx); getKeysFreeResult(&result);
return ACL_DENIED_KEY; return ACL_DENIED_KEY;
} }
} }
getKeysFreeResult(keyidx); getKeysFreeResult(&result);
} }
/* If we survived all the above checks, the user can execute the /* If we survived all the above checks, the user can execute the
@ -1330,6 +1384,7 @@ sds ACLLoadFromFile(const char *filename) {
errors = sdscatprintf(errors, errors = sdscatprintf(errors,
"'%s:%d: username '%s' contains invalid characters. ", "'%s:%d: username '%s' contains invalid characters. ",
g_pserver->acl_filename, linenum, argv[1]); g_pserver->acl_filename, linenum, argv[1]);
sdsfreesplitres(argv,argc);
continue; continue;
} }
@ -1914,7 +1969,7 @@ void aclCommand(client *c) {
addReplyBulkCString(c,"client-info"); addReplyBulkCString(c,"client-info");
addReplyBulkCBuffer(c,le->cinfo,sdslen(le->cinfo)); addReplyBulkCBuffer(c,le->cinfo,sdslen(le->cinfo));
} }
} else if (!strcasecmp(sub,"help")) { } else if (c->argc == 2 && !strcasecmp(sub,"help")) {
const char *help[] = { const char *help[] = {
"LOAD -- Reload users from the ACL file.", "LOAD -- Reload users from the ACL file.",
"SAVE -- Save the current config to the ACL file.", "SAVE -- Save the current config to the ACL file.",

View File

@ -34,8 +34,9 @@
#include "zmalloc.h" #include "zmalloc.h"
/* Create a new list. The created list can be freed with /* Create a new list. The created list can be freed with
* AlFreeList(), but private value of every node need to be freed * listRelease(), but private value of every node need to be freed
* by the user before to call AlFreeList(). * by the user before to call listRelease(), or by setting a free method using
* listSetFreeMethod.
* *
* On error, NULL is returned. Otherwise the pointer to the new list. */ * On error, NULL is returned. Otherwise the pointer to the new list. */
list *listCreate(void) list *listCreate(void)
@ -217,8 +218,8 @@ void listRewindTail(list *list, listIter *li) {
* listDelNode(), but not to remove other elements. * listDelNode(), but not to remove other elements.
* *
* The function returns a pointer to the next element of the list, * The function returns a pointer to the next element of the list,
* or NULL if there are no more elements, so the classical usage patter * or NULL if there are no more elements, so the classical usage
* is: * pattern is:
* *
* iter = listGetIterator(list,<direction>); * iter = listGetIterator(list,<direction>);
* while ((node = listNext(iter)) != NULL) { * while ((node = listNext(iter)) != NULL) {

View File

@ -48,6 +48,7 @@
#include "fastlock.h" #include "fastlock.h"
#include "zmalloc.h" #include "zmalloc.h"
#include "config.h" #include "config.h"
#include "serverassert.h"
#ifdef USE_MUTEX #ifdef USE_MUTEX
thread_local int cOwnLock = 0; thread_local int cOwnLock = 0;
@ -84,8 +85,6 @@ fastlock g_lock("AE (global)");
#endif #endif
thread_local aeEventLoop *g_eventLoopThisThread = NULL; thread_local aeEventLoop *g_eventLoopThisThread = NULL;
#define AE_ASSERT(x) if (!(x)) do { fprintf(stderr, "AE_ASSERT FAILURE %s: %d\n", __FILE__, __LINE__); *((volatile int*)1) = 1; } while(0)
/* Include the best multiplexing layer supported by this system. /* Include the best multiplexing layer supported by this system.
* The following should be ordered by performances, descending. */ * The following should be ordered by performances, descending. */
#ifdef HAVE_EVPORT #ifdef HAVE_EVPORT
@ -140,7 +139,7 @@ void aeProcessCmd(aeEventLoop *eventLoop, int fd, void *, int )
auto cb = read(fd, &cmd, sizeof(aeCommand)); auto cb = read(fd, &cmd, sizeof(aeCommand));
if (cb != sizeof(cmd)) if (cb != sizeof(cmd))
{ {
AE_ASSERT(errno == EAGAIN); serverAssert(errno == EAGAIN);
break; break;
} }
switch (cmd.op) switch (cmd.op)
@ -251,8 +250,8 @@ int aeCreateRemoteFileEvent(aeEventLoop *eventLoop, int fd, int mask,
auto size = safe_write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd)); auto size = safe_write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd));
if (size != sizeof(cmd)) if (size != sizeof(cmd))
{ {
AE_ASSERT(size == sizeof(cmd) || size <= 0); serverAssert(size == sizeof(cmd) || size <= 0);
AE_ASSERT(errno == EAGAIN); serverAssert(errno == EAGAIN);
ret = AE_ERR; ret = AE_ERR;
} }
@ -307,9 +306,14 @@ int aePostFunction(aeEventLoop *eventLoop, std::function<void()> fn, bool fSynch
} }
auto size = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd)); auto size = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd));
if (size != sizeof(cmd)) if (!(!size || size == sizeof(cmd))) {
printf("Last error: %d\n", errno);
}
serverAssert(!size || size == sizeof(cmd));
if (size == 0)
return AE_ERR; return AE_ERR;
AE_ASSERT(size == sizeof(cmd));
int ret = AE_OK; int ret = AE_OK;
if (fSynchronous) if (fSynchronous)
{ {
@ -352,7 +356,7 @@ aeEventLoop *aeCreateEventLoop(int setsize) {
goto err; goto err;
eventLoop->fdCmdRead = rgfd[0]; eventLoop->fdCmdRead = rgfd[0];
eventLoop->fdCmdWrite = rgfd[1]; eventLoop->fdCmdWrite = rgfd[1];
fcntl(eventLoop->fdCmdWrite, F_SETFL, O_NONBLOCK); //fcntl(eventLoop->fdCmdWrite, F_SETFL, O_NONBLOCK);
fcntl(eventLoop->fdCmdRead, F_SETFL, O_NONBLOCK); fcntl(eventLoop->fdCmdRead, F_SETFL, O_NONBLOCK);
eventLoop->cevents = 0; eventLoop->cevents = 0;
aeCreateFileEvent(eventLoop, eventLoop->fdCmdRead, AE_READABLE|AE_READ_THREADSAFE, aeProcessCmd, NULL); aeCreateFileEvent(eventLoop, eventLoop->fdCmdRead, AE_READABLE|AE_READ_THREADSAFE, aeProcessCmd, NULL);
@ -373,6 +377,11 @@ int aeGetSetSize(aeEventLoop *eventLoop) {
return eventLoop->setsize; return eventLoop->setsize;
} }
/* Return the current EventLoop. */
aeEventLoop *aeGetCurrentEventLoop(){
return g_eventLoopThisThread;
}
/* Tells the next iteration/s of the event processing to set timeout of 0. */ /* Tells the next iteration/s of the event processing to set timeout of 0. */
void aeSetDontWait(aeEventLoop *eventLoop, int noWait) { void aeSetDontWait(aeEventLoop *eventLoop, int noWait) {
if (noWait) if (noWait)
@ -389,7 +398,7 @@ void aeSetDontWait(aeEventLoop *eventLoop, int noWait) {
* *
* Otherwise AE_OK is returned and the operation is successful. */ * Otherwise AE_OK is returned and the operation is successful. */
int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) { int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) {
AE_ASSERT(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop); serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);
int i; int i;
if (setsize == eventLoop->setsize) return AE_OK; if (setsize == eventLoop->setsize) return AE_OK;
@ -427,14 +436,14 @@ extern "C" void aeDeleteEventLoop(aeEventLoop *eventLoop) {
} }
extern "C" void aeStop(aeEventLoop *eventLoop) { extern "C" void aeStop(aeEventLoop *eventLoop) {
AE_ASSERT(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop); serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);
eventLoop->stop = 1; eventLoop->stop = 1;
} }
extern "C" int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, extern "C" int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData) aeFileProc *proc, void *clientData)
{ {
AE_ASSERT(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop); serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);
if (fd >= eventLoop->setsize) { if (fd >= eventLoop->setsize) {
errno = ERANGE; errno = ERANGE;
return AE_ERR; return AE_ERR;
@ -463,12 +472,12 @@ void aeDeleteFileEventAsync(aeEventLoop *eventLoop, int fd, int mask)
cmd.mask = mask; cmd.mask = mask;
cmd.fLock = true; cmd.fLock = true;
auto cb = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd)); auto cb = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd));
AE_ASSERT(cb == sizeof(cmd)); serverAssert(cb == sizeof(cmd));
} }
extern "C" void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask) extern "C" void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)
{ {
AE_ASSERT(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop); serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);
if (fd >= eventLoop->setsize) return; if (fd >= eventLoop->setsize) return;
aeFileEvent *fe = &eventLoop->events[fd]; aeFileEvent *fe = &eventLoop->events[fd];
if (fe->mask == AE_NONE) return; if (fe->mask == AE_NONE) return;
@ -526,7 +535,7 @@ extern "C" long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long millise
aeTimeProc *proc, void *clientData, aeTimeProc *proc, void *clientData,
aeEventFinalizerProc *finalizerProc) aeEventFinalizerProc *finalizerProc)
{ {
AE_ASSERT(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop); serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);
long long id = eventLoop->timeEventNextId++; long long id = eventLoop->timeEventNextId++;
aeTimeEvent *te; aeTimeEvent *te;
@ -548,7 +557,7 @@ extern "C" long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long millise
extern "C" int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id) extern "C" int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id)
{ {
AE_ASSERT(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop); serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);
aeTimeEvent *te = eventLoop->timeEventHead; aeTimeEvent *te = eventLoop->timeEventHead;
while(te) { while(te) {
if (te->id == id) { if (te->id == id) {
@ -573,7 +582,7 @@ extern "C" int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id)
*/ */
static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop) static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop)
{ {
AE_ASSERT(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop); serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);
aeTimeEvent *te = eventLoop->timeEventHead; aeTimeEvent *te = eventLoop->timeEventHead;
aeTimeEvent *nearest = NULL; aeTimeEvent *nearest = NULL;
@ -589,7 +598,7 @@ static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop)
/* Process time events */ /* Process time events */
static int processTimeEvents(aeEventLoop *eventLoop) { static int processTimeEvents(aeEventLoop *eventLoop) {
std::unique_lock<decltype(g_lock)> ulock(g_lock); std::unique_lock<decltype(g_lock)> ulock(g_lock, std::defer_lock);
int processed = 0; int processed = 0;
aeTimeEvent *te; aeTimeEvent *te;
long long maxId; long long maxId;
@ -634,8 +643,10 @@ static int processTimeEvents(aeEventLoop *eventLoop) {
eventLoop->timeEventHead = te->next; eventLoop->timeEventHead = te->next;
if (te->next) if (te->next)
te->next->prev = te->prev; te->next->prev = te->prev;
if (te->finalizerProc) if (te->finalizerProc) {
if (!ulock.owns_lock()) ulock.lock();
te->finalizerProc(eventLoop, te->clientData); te->finalizerProc(eventLoop, te->clientData);
}
zfree(te); zfree(te);
te = next; te = next;
continue; continue;
@ -654,6 +665,7 @@ static int processTimeEvents(aeEventLoop *eventLoop) {
if (now_sec > te->when_sec || if (now_sec > te->when_sec ||
(now_sec == te->when_sec && now_ms >= te->when_ms)) (now_sec == te->when_sec && now_ms >= te->when_ms))
{ {
if (!ulock.owns_lock()) ulock.lock();
int retval; int retval;
id = te->id; id = te->id;
@ -746,7 +758,7 @@ extern "C" void ProcessEventCore(aeEventLoop *eventLoop, aeFileEvent *fe, int ma
* The function returns the number of events processed. */ * The function returns the number of events processed. */
int aeProcessEvents(aeEventLoop *eventLoop, int flags) int aeProcessEvents(aeEventLoop *eventLoop, int flags)
{ {
AE_ASSERT(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop); serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);
int processed = 0, numevents; int processed = 0, numevents;
/* Nothing to do? return ASAP */ /* Nothing to do? return ASAP */
@ -870,9 +882,9 @@ void aeMain(aeEventLoop *eventLoop) {
ulock.lock(); ulock.lock();
eventLoop->beforesleep(eventLoop); eventLoop->beforesleep(eventLoop);
} }
AE_ASSERT(!aeThreadOwnsLock()); // we should have relinquished it after processing serverAssert(!aeThreadOwnsLock()); // we should have relinquished it after processing
aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_AFTER_SLEEP); aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_AFTER_SLEEP);
AE_ASSERT(!aeThreadOwnsLock()); // we should have relinquished it after processing serverAssert(!aeThreadOwnsLock()); // we should have relinquished it after processing
} }
} }

View File

@ -160,6 +160,7 @@ const char *aeGetApiName(void);
void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep, int flags); void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep, int flags);
void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep, int flags); void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep, int flags);
int aeGetSetSize(aeEventLoop *eventLoop); int aeGetSetSize(aeEventLoop *eventLoop);
aeEventLoop *aeGetCurrentEventLoop();
int aeResizeSetSize(aeEventLoop *eventLoop, int setsize); int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
void aeSetDontWait(aeEventLoop *eventLoop, int noWait); void aeSetDontWait(aeEventLoop *eventLoop, int noWait);

View File

@ -232,7 +232,7 @@ static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
/* /*
* ENOMEM is a potentially transient condition, but the kernel won't * ENOMEM is a potentially transient condition, but the kernel won't
* generally return it unless things are really bad. EAGAIN indicates * generally return it unless things are really bad. EAGAIN indicates
* we've reached an resource limit, for which it doesn't make sense to * we've reached a resource limit, for which it doesn't make sense to
* retry (counter-intuitively). All other errors indicate a bug. In any * retry (counter-intuitively). All other errors indicate a bug. In any
* of these cases, the best we can do is to abort. * of these cases, the best we can do is to abort.
*/ */

View File

@ -9,7 +9,7 @@ public:
{ {
} }
void arm(client *c, bool fIfNeeded = false) // if a client is passed, then the client is already locked void arm(client *c = nullptr, bool fIfNeeded = false) // if a client is passed, then the client is already locked
{ {
if (m_fArmed) if (m_fArmed)
return; return;

View File

@ -566,7 +566,7 @@ sds catAppendOnlyGenericCommand(sds dst, int argc, robj **argv) {
return dst; return dst;
} }
/* Create the sds representation of an PEXPIREAT command, using /* Create the sds representation of a PEXPIREAT command, using
* 'seconds' as time to live and 'cmd' to understand what command * 'seconds' as time to live and 'cmd' to understand what command
* we are translating into a PEXPIREAT. * we are translating into a PEXPIREAT.
* *
@ -642,7 +642,7 @@ sds catAppendOnlyExpireMemberAtCommand(sds buf, struct redisCommand *cmd, robj *
when += mstime(); when += mstime();
robj *argvNew[4]; robj *argvNew[4];
argvNew[0] = createStringObject("PEXPIREMEMBERAT",15); argvNew[0] = shared.pexpirememberat;
argvNew[1] = argv[1]; argvNew[1] = argv[1];
argvNew[2] = argv[2]; argvNew[2] = argv[2];
argvNew[3] = createStringObjectFromLongLong(when); argvNew[3] = createStringObjectFromLongLong(when);
@ -752,6 +752,7 @@ struct client *createAOFClient(void) {
c->querybuf_peak = 0; c->querybuf_peak = 0;
c->argc = 0; c->argc = 0;
c->argv = NULL; c->argv = NULL;
c->argv_len_sum = 0;
c->bufpos = 0; c->bufpos = 0;
c->flags = 0; c->flags = 0;
c->fPendingAsyncWrite = FALSE; c->fPendingAsyncWrite = FALSE;
@ -781,6 +782,7 @@ void freeFakeClientArgv(struct client *c) {
for (j = 0; j < c->argc; j++) for (j = 0; j < c->argc; j++)
decrRefCount(c->argv[j]); decrRefCount(c->argv[j]);
zfree(c->argv); zfree(c->argv);
c->argv_len_sum = 0;
} }
void freeFakeClient(struct client *c) { void freeFakeClient(struct client *c) {
@ -1159,7 +1161,7 @@ int rewriteSortedSetObject(rio *r, robj *key, robj *o) {
} }
} else if (o->encoding == OBJ_ENCODING_SKIPLIST) { } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {
zset *zs = (zset*)ptrFromObj(o); zset *zs = (zset*)ptrFromObj(o);
dictIterator *di = dictGetIterator(zs->pdict); dictIterator *di = dictGetIterator(zs->dict);
dictEntry *de; dictEntry *de;
while((de = dictNext(di)) != NULL) { while((de = dictNext(di)) != NULL) {
@ -1292,16 +1294,24 @@ int rewriteStreamObject(rio *r, robj *key, robj *o) {
* the ID, the second is an array of field-value pairs. */ * the ID, the second is an array of field-value pairs. */
/* Emit the XADD <key> <id> ...fields... command. */ /* Emit the XADD <key> <id> ...fields... command. */
if (rioWriteBulkCount(r,'*',3+numfields*2) == 0) return 0; if (!rioWriteBulkCount(r,'*',3+numfields*2) ||
if (rioWriteBulkString(r,"XADD",4) == 0) return 0; !rioWriteBulkString(r,"XADD",4) ||
if (rioWriteBulkObject(r,key) == 0) return 0; !rioWriteBulkObject(r,key) ||
if (rioWriteBulkStreamID(r,&id) == 0) return 0; !rioWriteBulkStreamID(r,&id))
{
streamIteratorStop(&si);
return 0;
}
while(numfields--) { while(numfields--) {
unsigned char *field, *value; unsigned char *field, *value;
int64_t field_len, value_len; int64_t field_len, value_len;
streamIteratorGetField(&si,&field,&value,&field_len,&value_len); streamIteratorGetField(&si,&field,&value,&field_len,&value_len);
if (rioWriteBulkString(r,(char*)field,field_len) == 0) return 0; if (!rioWriteBulkString(r,(char*)field,field_len) ||
if (rioWriteBulkString(r,(char*)value,value_len) == 0) return 0; !rioWriteBulkString(r,(char*)value,value_len))
{
streamIteratorStop(&si);
return 0;
}
} }
} }
} else { } else {
@ -1309,22 +1319,30 @@ int rewriteStreamObject(rio *r, robj *key, robj *o) {
* the key we are serializing is an empty string, which is possible * the key we are serializing is an empty string, which is possible
* for the Stream type. */ * for the Stream type. */
id.ms = 0; id.seq = 1; id.ms = 0; id.seq = 1;
if (rioWriteBulkCount(r,'*',7) == 0) return 0; if (!rioWriteBulkCount(r,'*',7) ||
if (rioWriteBulkString(r,"XADD",4) == 0) return 0; !rioWriteBulkString(r,"XADD",4) ||
if (rioWriteBulkObject(r,key) == 0) return 0; !rioWriteBulkObject(r,key) ||
if (rioWriteBulkString(r,"MAXLEN",6) == 0) return 0; !rioWriteBulkString(r,"MAXLEN",6) ||
if (rioWriteBulkString(r,"0",1) == 0) return 0; !rioWriteBulkString(r,"0",1) ||
if (rioWriteBulkStreamID(r,&id) == 0) return 0; !rioWriteBulkStreamID(r,&id) ||
if (rioWriteBulkString(r,"x",1) == 0) return 0; !rioWriteBulkString(r,"x",1) ||
if (rioWriteBulkString(r,"y",1) == 0) return 0; !rioWriteBulkString(r,"y",1))
{
streamIteratorStop(&si);
return 0;
}
} }
/* Append XSETID after XADD, make sure lastid is correct, /* Append XSETID after XADD, make sure lastid is correct,
* in case of XDEL lastid. */ * in case of XDEL lastid. */
if (rioWriteBulkCount(r,'*',3) == 0) return 0; if (!rioWriteBulkCount(r,'*',3) ||
if (rioWriteBulkString(r,"XSETID",6) == 0) return 0; !rioWriteBulkString(r,"XSETID",6) ||
if (rioWriteBulkObject(r,key) == 0) return 0; !rioWriteBulkObject(r,key) ||
if (rioWriteBulkStreamID(r,&s->last_id) == 0) return 0; !rioWriteBulkStreamID(r,&s->last_id))
{
streamIteratorStop(&si);
return 0;
}
/* Create all the stream consumer groups. */ /* Create all the stream consumer groups. */
@ -1335,12 +1353,17 @@ int rewriteStreamObject(rio *r, robj *key, robj *o) {
while(raxNext(&ri)) { while(raxNext(&ri)) {
streamCG *group = (streamCG*)ri.data; streamCG *group = (streamCG*)ri.data;
/* Emit the XGROUP CREATE in order to create the group. */ /* Emit the XGROUP CREATE in order to create the group. */
if (rioWriteBulkCount(r,'*',5) == 0) return 0; if (!rioWriteBulkCount(r,'*',5) ||
if (rioWriteBulkString(r,"XGROUP",6) == 0) return 0; !rioWriteBulkString(r,"XGROUP",6) ||
if (rioWriteBulkString(r,"CREATE",6) == 0) return 0; !rioWriteBulkString(r,"CREATE",6) ||
if (rioWriteBulkObject(r,key) == 0) return 0; !rioWriteBulkObject(r,key) ||
if (rioWriteBulkString(r,(char*)ri.key,ri.key_len) == 0) return 0; !rioWriteBulkString(r,(char*)ri.key,ri.key_len) ||
if (rioWriteBulkStreamID(r,&group->last_id) == 0) return 0; !rioWriteBulkStreamID(r,&group->last_id))
{
raxStop(&ri);
streamIteratorStop(&si);
return 0;
}
/* Generate XCLAIMs for each consumer that happens to /* Generate XCLAIMs for each consumer that happens to
* have pending entries. Empty consumers have no semantical * have pending entries. Empty consumers have no semantical
@ -1361,6 +1384,10 @@ int rewriteStreamObject(rio *r, robj *key, robj *o) {
ri.key_len,consumer, ri.key_len,consumer,
ri_pel.key,nack) == 0) ri_pel.key,nack) == 0)
{ {
raxStop(&ri_pel);
raxStop(&ri_cons);
raxStop(&ri);
streamIteratorStop(&si);
return 0; return 0;
} }
} }
@ -1415,7 +1442,7 @@ int rewriteAppendOnlyFileRio(rio *aof) {
for (j = 0; j < cserver.dbnum; j++) { for (j = 0; j < cserver.dbnum; j++) {
char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n"; char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n";
redisDb *db = g_pserver->db+j; redisDb *db = g_pserver->db+j;
dict *d = db->pdict; dict *d = db->dict;
if (dictSize(d) == 0) continue; if (dictSize(d) == 0) continue;
di = dictGetSafeIterator(d); di = dictGetSafeIterator(d);
@ -1502,7 +1529,7 @@ werr:
* are inserted using a single command. */ * are inserted using a single command. */
int rewriteAppendOnlyFile(char *filename) { int rewriteAppendOnlyFile(char *filename) {
rio aof; rio aof;
FILE *fp; FILE *fp = NULL;
char tmpfile[256]; char tmpfile[256];
char byte; char byte;
int nodata = 0; int nodata = 0;
@ -1580,9 +1607,10 @@ int rewriteAppendOnlyFile(char *filename) {
goto werr; goto werr;
/* Make sure data will not remain on the OS's output buffers */ /* Make sure data will not remain on the OS's output buffers */
if (fflush(fp) == EOF) goto werr; if (fflush(fp)) goto werr;
if (fsync(fileno(fp)) == -1) goto werr; if (fsync(fileno(fp))) goto werr;
if (fclose(fp) == EOF) goto werr; if (fclose(fp)) { fp = NULL; goto werr; }
fp = NULL;
/* Use RENAME to make sure the DB file is changed atomically only /* Use RENAME to make sure the DB file is changed atomically only
* if the generate DB file is ok. */ * if the generate DB file is ok. */
@ -1598,7 +1626,7 @@ int rewriteAppendOnlyFile(char *filename) {
werr: werr:
serverLog(LL_WARNING,"Write error writing append only file on disk: %s", strerror(errno)); serverLog(LL_WARNING,"Write error writing append only file on disk: %s", strerror(errno));
fclose(fp); if (fp) fclose(fp);
unlink(tmpfile); unlink(tmpfile);
stopSaving(0); stopSaving(0);
return C_ERR; return C_ERR;
@ -1712,7 +1740,7 @@ int rewriteAppendOnlyFileBackground(void) {
if (hasActiveChildProcess()) return C_ERR; if (hasActiveChildProcess()) return C_ERR;
if (aofCreatePipes() != C_OK) return C_ERR; if (aofCreatePipes() != C_OK) return C_ERR;
openChildInfoPipe(); openChildInfoPipe();
if ((childpid = redisFork()) == 0) { if ((childpid = redisFork(CHILD_TYPE_AOF)) == 0) {
char tmpfile[256]; char tmpfile[256];
/* Child */ /* Child */
@ -1720,7 +1748,7 @@ int rewriteAppendOnlyFileBackground(void) {
redisSetCpuAffinity(g_pserver->aof_rewrite_cpulist); redisSetCpuAffinity(g_pserver->aof_rewrite_cpulist);
snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid()); snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
if (rewriteAppendOnlyFile(tmpfile) == C_OK) { if (rewriteAppendOnlyFile(tmpfile) == C_OK) {
sendChildCOWInfo(CHILD_INFO_TYPE_AOF, "AOF rewrite"); sendChildCOWInfo(CHILD_TYPE_AOF, "AOF rewrite");
exitFromChild(0); exitFromChild(0);
} else { } else {
exitFromChild(1); exitFromChild(1);
@ -1740,6 +1768,7 @@ int rewriteAppendOnlyFileBackground(void) {
g_pserver->aof_rewrite_scheduled = 0; g_pserver->aof_rewrite_scheduled = 0;
g_pserver->aof_rewrite_time_start = time(NULL); g_pserver->aof_rewrite_time_start = time(NULL);
g_pserver->aof_child_pid = childpid; g_pserver->aof_child_pid = childpid;
updateDictResizePolicy();
/* We set appendseldb to -1 in order to force the next call to the /* We set appendseldb to -1 in order to force the next call to the
* feedAppendOnlyFile() to issue a SELECT command, so the differences * feedAppendOnlyFile() to issue a SELECT command, so the differences
* accumulated by the parent into g_pserver->aof_rewrite_buf will start * accumulated by the parent into g_pserver->aof_rewrite_buf will start
@ -1769,10 +1798,10 @@ void aofRemoveTempFile(pid_t childpid) {
char tmpfile[256]; char tmpfile[256];
snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid); snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid);
unlink(tmpfile); bg_unlink(tmpfile);
snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) childpid); snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) childpid);
unlink(tmpfile); bg_unlink(tmpfile);
} }
/* Update the g_pserver->aof_current_size field explicitly using stat(2) /* Update the g_pserver->aof_current_size field explicitly using stat(2)
@ -1927,7 +1956,7 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
"Background AOF rewrite terminated with error"); "Background AOF rewrite terminated with error");
} else { } else {
/* SIGUSR1 is whitelisted, so we have a way to kill a child without /* SIGUSR1 is whitelisted, so we have a way to kill a child without
* tirggering an error condition. */ * triggering an error condition. */
if (bysignal != SIGUSR1) if (bysignal != SIGUSR1)
g_pserver->aof_lastbgrewrite_status = C_ERR; g_pserver->aof_lastbgrewrite_status = C_ERR;

View File

@ -28,14 +28,21 @@
*/ */
const char *ascii_logo = const char *ascii_logo =
" \n" " \n"
" \n" " _ \n"
" KeyDB %s (%s/%d) %s bit\n" " _-(+)-_ \n"
" \n" " _-- / \\ --_ \n"
" Running in %s mode\n" " _-- / \\ --_ KeyDB %s (%s/%d) %s bit \n"
" Port: %d\n" " __-- / \\ --__ \n"
" PID: %ld\n" " (+) _ / \\ _ (+) Running in %s mode\n"
" \n" " | -- / \\ -- | Port: %d\n"
" %s\n" " | /--_ _ _--\\ | PID: %ld\n"
" \n" " | / -(+)- \\ | \n"
" | / | \\ | https://docs.keydb.dev \n"
" | / | \\ | \n"
" | / | \\ | \n"
" (+)_ -- -- -- | -- -- -- _(+) \n"
" --_ | _-- \n"
" --_ | _-- \n"
" -(+)- %s\n"
" \n"; " \n";

View File

@ -21,7 +21,7 @@
* *
* Never use return value from the macros, instead use the AtomicGetIncr() * Never use return value from the macros, instead use the AtomicGetIncr()
* if you need to get the current value and increment it atomically, like * if you need to get the current value and increment it atomically, like
* in the followign example: * in the following example:
* *
* long oldvalue; * long oldvalue;
* atomicGetIncr(myvar,oldvalue,1); * atomicGetIncr(myvar,oldvalue,1);

View File

@ -168,10 +168,7 @@ void *bioProcessBackgroundJobs(void *arg) {
redisSetCpuAffinity(g_pserver->bio_cpulist); redisSetCpuAffinity(g_pserver->bio_cpulist);
/* Make the thread killable at any time, so that bioKillThreads() makeThreadKillable();
* can work reliably. */
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
pthread_mutex_lock(&bio_mutex[type]); pthread_mutex_lock(&bio_mutex[type]);
/* Block SIGALRM so we are sure that only the main thread will /* Block SIGALRM so we are sure that only the main thread will
@ -206,7 +203,7 @@ void *bioProcessBackgroundJobs(void *arg) {
/* What we free changes depending on what arguments are set: /* What we free changes depending on what arguments are set:
* arg1 -> free the object at pointer. * arg1 -> free the object at pointer.
* arg2 & arg3 -> free two dictionaries (a Redis DB). * arg2 & arg3 -> free two dictionaries (a Redis DB).
* only arg3 -> free the skiplist. */ * only arg3 -> free the radix tree. */
if (job->arg1) if (job->arg1)
lazyfreeFreeObjectFromBioThread((robj*)job->arg1); lazyfreeFreeObjectFromBioThread((robj*)job->arg1);
else if (job->arg2 && job->arg3) else if (job->arg2 && job->arg3)
@ -268,10 +265,11 @@ void bioKillThreads(void) {
int err, j; int err, j;
for (j = 0; j < BIO_NUM_OPS; j++) { for (j = 0; j < BIO_NUM_OPS; j++) {
if (bio_threads[j] == pthread_self()) continue;
if (bio_threads[j] && pthread_cancel(bio_threads[j]) == 0) { if (bio_threads[j] && pthread_cancel(bio_threads[j]) == 0) {
if ((err = pthread_join(bio_threads[j],NULL)) != 0) { if ((err = pthread_join(bio_threads[j],NULL)) != 0) {
serverLog(LL_WARNING, serverLog(LL_WARNING,
"Bio thread for job type #%d can be joined: %s", "Bio thread for job type #%d can not be joined: %s",
j, strerror(err)); j, strerror(err));
} else { } else {
serverLog(LL_WARNING, serverLog(LL_WARNING,

View File

@ -36,7 +36,7 @@
/* Count number of bits set in the binary array pointed by 's' and long /* Count number of bits set in the binary array pointed by 's' and long
* 'count' bytes. The implementation of this function is required to * 'count' bytes. The implementation of this function is required to
* work with a input string length up to 512 MB. */ * work with an input string length up to 512 MB. */
size_t redisPopcount(const void *s, long count) { size_t redisPopcount(const void *s, long count) {
size_t bits = 0; size_t bits = 0;
unsigned char *p = (unsigned char*)s; unsigned char *p = (unsigned char*)s;
@ -107,7 +107,7 @@ long redisBitpos(const void *s, unsigned long count, int bit) {
int found; int found;
/* Process whole words first, seeking for first word that is not /* Process whole words first, seeking for first word that is not
* all ones or all zeros respectively if we are lookig for zeros * all ones or all zeros respectively if we are looking for zeros
* or ones. This is much faster with large strings having contiguous * or ones. This is much faster with large strings having contiguous
* blocks of 1 or 0 bits compared to the vanilla bit per bit processing. * blocks of 1 or 0 bits compared to the vanilla bit per bit processing.
* *
@ -257,7 +257,7 @@ int64_t getSignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits) {
/* If the top significant bit is 1, propagate it to all the /* If the top significant bit is 1, propagate it to all the
* higher bits for two's complement representation of signed * higher bits for two's complement representation of signed
* integers. */ * integers. */
if (value & ((uint64_t)1 << (bits-1))) if (bits < 64 && (value & ((uint64_t)1 << (bits-1))))
value |= ((uint64_t)-1) << bits; value |= ((uint64_t)-1) << bits;
return value; return value;
} }
@ -356,7 +356,6 @@ int checkSignedBitfieldOverflow(int64_t value, int64_t incr, uint64_t bits, int
handle_wrap: handle_wrap:
{ {
uint64_t mask = ((uint64_t)-1) << bits;
uint64_t msb = (uint64_t)1 << (bits-1); uint64_t msb = (uint64_t)1 << (bits-1);
uint64_t a = value, b = incr, c; uint64_t a = value, b = incr, c;
c = a+b; /* Perform addition as unsigned so that's defined. */ c = a+b; /* Perform addition as unsigned so that's defined. */
@ -364,10 +363,13 @@ handle_wrap:
/* If the sign bit is set, propagate to all the higher order /* If the sign bit is set, propagate to all the higher order
* bits, to cap the negative value. If it's clear, mask to * bits, to cap the negative value. If it's clear, mask to
* the positive integer limit. */ * the positive integer limit. */
if (c & msb) { if (bits < 64) {
c |= mask; uint64_t mask = ((uint64_t)-1) << bits;
} else { if (c & msb) {
c &= ~mask; c |= mask;
} else {
c &= ~mask;
}
} }
*limit = c; *limit = c;
} }
@ -496,7 +498,7 @@ robj *lookupStringForBitCommand(client *c, size_t maxbit) {
* in 'len'. The user is required to pass (likely stack allocated) buffer * in 'len'. The user is required to pass (likely stack allocated) buffer
* 'llbuf' of at least LONG_STR_SIZE bytes. Such a buffer is used in the case * 'llbuf' of at least LONG_STR_SIZE bytes. Such a buffer is used in the case
* the object is integer encoded in order to provide the representation * the object is integer encoded in order to provide the representation
* without usign heap allocation. * without using heap allocation.
* *
* The function returns the pointer to the object array of bytes representing * The function returns the pointer to the object array of bytes representing
* the string it contains, that may be a pointer to 'llbuf' or to the * the string it contains, that may be a pointer to 'llbuf' or to the
@ -831,11 +833,12 @@ void bitopCommand(client *c) {
setKey(c,c->db,targetkey,o); setKey(c,c->db,targetkey,o);
notifyKeyspaceEvent(NOTIFY_STRING,"set",targetkey,c->db->id); notifyKeyspaceEvent(NOTIFY_STRING,"set",targetkey,c->db->id);
decrRefCount(o); decrRefCount(o);
g_pserver->dirty++;
} else if (dbDelete(c->db,targetkey)) { } else if (dbDelete(c->db,targetkey)) {
signalModifiedKey(c,c->db,targetkey); signalModifiedKey(c,c->db,targetkey);
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",targetkey,c->db->id); notifyKeyspaceEvent(NOTIFY_GENERIC,"del",targetkey,c->db->id);
g_pserver->dirty++;
} }
g_pserver->dirty++;
addReplyLongLong(c,maxlen); /* Return the output string length in bytes. */ addReplyLongLong(c,maxlen); /* Return the output string length in bytes. */
} }

View File

@ -53,7 +53,7 @@
* to 0, no timeout is processed). * to 0, no timeout is processed).
* It usually just needs to send a reply to the client. * It usually just needs to send a reply to the client.
* *
* When implementing a new type of blocking opeation, the implementation * When implementing a new type of blocking operation, the implementation
* should modify unblockClient() and replyToBlockedClientTimedOut() in order * should modify unblockClient() and replyToBlockedClientTimedOut() in order
* to handle the btype-specific behavior of this two functions. * to handle the btype-specific behavior of this two functions.
* If the blocking operation waits for certain keys to change state, the * If the blocking operation waits for certain keys to change state, the
@ -128,7 +128,7 @@ void processUnblockedClients(int iel) {
/* This function will schedule the client for reprocessing at a safe time. /* This function will schedule the client for reprocessing at a safe time.
* *
* This is useful when a client was blocked for some reason (blocking opeation, * This is useful when a client was blocked for some reason (blocking operation,
* CLIENT PAUSE, or whatever), because it may end with some accumulated query * CLIENT PAUSE, or whatever), because it may end with some accumulated query
* buffer that needs to be processed ASAP: * buffer that needs to be processed ASAP:
* *
@ -188,9 +188,9 @@ void replyToBlockedClientTimedOut(client *c) {
if (c->btype == BLOCKED_LIST || if (c->btype == BLOCKED_LIST ||
c->btype == BLOCKED_ZSET || c->btype == BLOCKED_ZSET ||
c->btype == BLOCKED_STREAM) { c->btype == BLOCKED_STREAM) {
addReplyNullArrayAsync(c); addReplyNullArray(c);
} else if (c->btype == BLOCKED_WAIT) { } else if (c->btype == BLOCKED_WAIT) {
addReplyLongLongAsync(c,replicationCountAcksByOffset(c->bpop.reploffset)); addReplyLongLong(c,replicationCountAcksByOffset(c->bpop.reploffset));
} else if (c->btype == BLOCKED_MODULE) { } else if (c->btype == BLOCKED_MODULE) {
moduleBlockedClientTimedOut(c); moduleBlockedClientTimedOut(c);
} else { } else {
@ -216,7 +216,7 @@ void disconnectAllBlockedClients(void) {
fastlock_lock(&c->lock); fastlock_lock(&c->lock);
if (c->flags & CLIENT_BLOCKED) { if (c->flags & CLIENT_BLOCKED) {
addReplySdsAsync(c,sdsnew( addReplySds(c,sdsnew(
"-UNBLOCKED force unblock from blocking operation, " "-UNBLOCKED force unblock from blocking operation, "
"instance state changed (master -> replica?)\r\n")); "instance state changed (master -> replica?)\r\n"));
unblockClient(c); unblockClient(c);
@ -373,7 +373,7 @@ void serveClientsBlockedOnStreamKey(robj *o, readyList *rl) {
/* If the group was not found, send an error /* If the group was not found, send an error
* to the consumer. */ * to the consumer. */
if (!group) { if (!group) {
addReplyErrorAsync(receiver, addReplyError(receiver,
"-NOGROUP the consumer group this client " "-NOGROUP the consumer group this client "
"was blocked on no longer exists"); "was blocked on no longer exists");
unblockClient(receiver); unblockClient(receiver);
@ -404,12 +404,12 @@ void serveClientsBlockedOnStreamKey(robj *o, readyList *rl) {
* extracted from it. Wrapped in a single-item * extracted from it. Wrapped in a single-item
* array, since we have just one key. */ * array, since we have just one key. */
if (receiver->resp == 2) { if (receiver->resp == 2) {
addReplyArrayLenAsync(receiver,1); addReplyArrayLen(receiver,1);
addReplyArrayLenAsync(receiver,2); addReplyArrayLen(receiver,2);
} else { } else {
addReplyMapLenAsync(receiver,1); addReplyMapLen(receiver,1);
} }
addReplyBulkAsync(receiver,rl->key); addReplyBulk(receiver,rl->key);
streamPropInfo pi = { streamPropInfo pi = {
rl->key, rl->key,
@ -522,7 +522,7 @@ void handleClientsBlockedOnKeys(void) {
serverTL->fixed_time_expire++; serverTL->fixed_time_expire++;
updateCachedTime(0); updateCachedTime(0);
/* Serve clients blocked on list key. */ /* Serve clients blocked on the key. */
robj *o = lookupKeyWrite(rl->db,rl->key); robj *o = lookupKeyWrite(rl->db,rl->key);
if (o != NULL) { if (o != NULL) {
@ -672,6 +672,13 @@ void signalKeyAsReady(redisDb *db, robj *key) {
/* Key was already signaled? No need to queue it again. */ /* Key was already signaled? No need to queue it again. */
if (dictFind(db->ready_keys,key) != NULL) return; if (dictFind(db->ready_keys,key) != NULL) return;
if (key->getrefcount() == OBJ_STATIC_REFCOUNT) {
// Sometimes a key may be stack allocated, we'll need to dupe it
robj *newKey = createStringObject(szFromObj(key), sdslen(szFromObj(key)));
newKey->setrefcount(0); // Start with 0 but don't free
key = newKey;
}
/* Ok, we need to queue this key into g_pserver->ready_keys. */ /* Ok, we need to queue this key into g_pserver->ready_keys. */
rl = (readyList*)zmalloc(sizeof(*rl), MALLOC_SHARED); rl = (readyList*)zmalloc(sizeof(*rl), MALLOC_SHARED);
rl->key = key; rl->key = key;

View File

@ -76,11 +76,11 @@ void receiveChildInfo(void) {
if (read(g_pserver->child_info_pipe[0],&g_pserver->child_info_data,wlen) == wlen && if (read(g_pserver->child_info_pipe[0],&g_pserver->child_info_data,wlen) == wlen &&
g_pserver->child_info_data.magic == CHILD_INFO_MAGIC) g_pserver->child_info_data.magic == CHILD_INFO_MAGIC)
{ {
if (g_pserver->child_info_data.process_type == CHILD_INFO_TYPE_RDB) { if (g_pserver->child_info_data.process_type == CHILD_TYPE_RDB) {
g_pserver->stat_rdb_cow_bytes = g_pserver->child_info_data.cow_size; g_pserver->stat_rdb_cow_bytes = g_pserver->child_info_data.cow_size;
} else if (g_pserver->child_info_data.process_type == CHILD_INFO_TYPE_AOF) { } else if (g_pserver->child_info_data.process_type == CHILD_TYPE_AOF) {
g_pserver->stat_aof_cow_bytes = g_pserver->child_info_data.cow_size; g_pserver->stat_aof_cow_bytes = g_pserver->child_info_data.cow_size;
} else if (g_pserver->child_info_data.process_type == CHILD_INFO_TYPE_MODULE) { } else if (g_pserver->child_info_data.process_type == CHILD_TYPE_MODULE) {
g_pserver->stat_module_cow_bytes = g_pserver->child_info_data.cow_size; g_pserver->stat_module_cow_bytes = g_pserver->child_info_data.cow_size;
} }
} }

View File

@ -77,6 +77,9 @@ uint64_t clusterGetMaxEpoch(void);
int clusterBumpConfigEpochWithoutConsensus(void); int clusterBumpConfigEpochWithoutConsensus(void);
void moduleCallClusterReceivers(const char *sender_id, uint64_t module_id, uint8_t type, const unsigned char *payload, uint32_t len); void moduleCallClusterReceivers(const char *sender_id, uint64_t module_id, uint8_t type, const unsigned char *payload, uint32_t len);
#define RCVBUF_INIT_LEN 1024
#define RCVBUF_MAX_PREALLOC (1<<20) /* 1MB */
struct redisMaster *getFirstMaster() struct redisMaster *getFirstMaster()
{ {
serverAssert(listLength(g_pserver->masters) <= 1); serverAssert(listLength(g_pserver->masters) <= 1);
@ -394,7 +397,7 @@ void clusterSaveConfigOrDie(int do_fsync) {
} }
} }
/* Lock the cluster config using flock(), and leaks the file descritor used to /* Lock the cluster config using flock(), and leaks the file descriptor used to
* acquire the lock so that the file will be locked forever. * acquire the lock so that the file will be locked forever.
* *
* This works because we always update nodes.conf with a new version * This works because we always update nodes.conf with a new version
@ -435,7 +438,15 @@ int clusterLockConfig(char *filename) {
return C_ERR; return C_ERR;
} }
/* Lock acquired: leak the 'fd' by not closing it, so that we'll retain the /* Lock acquired: leak the 'fd' by not closing it, so that we'll retain the
* lock to the file as long as the process exists. */ * lock to the file as long as the process exists.
*
* After fork, the child process will get the fd opened by the parent process,
* we need save `fd` to `cluster_config_file_lock_fd`, so that in redisFork(),
* it will be closed in the child process.
* If it is not closed, when the main process is killed -9, but the child process
* (redis-aof-rewrite) is still alive, the fd(lock) will still be held by the
* child process, and the main process will fail to get lock, means fail to start. */
g_pserver->cluster_config_file_lock_fd = fd;
#endif /* __sun */ #endif /* __sun */
return C_OK; return C_OK;
@ -490,6 +501,7 @@ void clusterInit(void) {
/* Lock the cluster config file to make sure every node uses /* Lock the cluster config file to make sure every node uses
* its own nodes.conf. */ * its own nodes.conf. */
g_pserver->cluster_config_file_lock_fd = -1;
if (clusterLockConfig(g_pserver->cluster_configfile) == C_ERR) if (clusterLockConfig(g_pserver->cluster_configfile) == C_ERR)
exit(1); exit(1);
@ -557,13 +569,13 @@ void clusterInit(void) {
/* Reset a node performing a soft or hard reset: /* Reset a node performing a soft or hard reset:
* *
* 1) All other nodes are forget. * 1) All other nodes are forgotten.
* 2) All the assigned / open slots are released. * 2) All the assigned / open slots are released.
* 3) If the node is a slave, it turns into a master. * 3) If the node is a slave, it turns into a master.
* 5) Only for hard reset: a new Node ID is generated. * 4) Only for hard reset: a new Node ID is generated.
* 6) Only for hard reset: currentEpoch and configEpoch are set to 0. * 5) Only for hard reset: currentEpoch and configEpoch are set to 0.
* 7) The new configuration is saved and the cluster state updated. * 6) The new configuration is saved and the cluster state updated.
* 8) If the node was a slave, the whole data set is flushed away. */ * 7) If the node was a slave, the whole data set is flushed away. */
void clusterReset(int hard) { void clusterReset(int hard) {
dictIterator *di; dictIterator *di;
dictEntry *de; dictEntry *de;
@ -630,7 +642,8 @@ clusterLink *createClusterLink(clusterNode *node) {
clusterLink *link = (clusterLink*)zmalloc(sizeof(*link), MALLOC_LOCAL); clusterLink *link = (clusterLink*)zmalloc(sizeof(*link), MALLOC_LOCAL);
link->ctime = mstime(); link->ctime = mstime();
link->sndbuf = sdsempty(); link->sndbuf = sdsempty();
link->rcvbuf = sdsempty(); link->rcvbuf = (char*)zmalloc(link->rcvbuf_alloc = RCVBUF_INIT_LEN);
link->rcvbuf_len = 0;
link->node = node; link->node = node;
link->conn = NULL; link->conn = NULL;
return link; return link;
@ -657,7 +670,7 @@ void freeClusterLink(clusterLink *link) {
link->conn = NULL; link->conn = NULL;
} }
sdsfree(link->sndbuf); sdsfree(link->sndbuf);
sdsfree(link->rcvbuf); zfree(link->rcvbuf);
if (link->node) if (link->node)
link->node->link = NULL; link->node->link = NULL;
zfree(link); zfree(link);
@ -675,7 +688,7 @@ static void clusterConnAcceptHandler(connection *conn) {
/* Create a link object we use to handle the connection. /* Create a link object we use to handle the connection.
* It gets passed to the readable handler when data is available. * It gets passed to the readable handler when data is available.
* Initiallly the link->node pointer is set to NULL as we don't know * Initially the link->node pointer is set to NULL as we don't know
* which node is, but the right node is references once we know the * which node is, but the right node is references once we know the
* node identity. */ * node identity. */
link = createClusterLink(NULL); link = createClusterLink(NULL);
@ -708,7 +721,17 @@ void clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
return; return;
} }
connection *conn = g_pserver->tls_cluster ? connCreateAcceptedTLS(cfd,1) : connCreateAcceptedSocket(cfd); connection *conn = g_pserver->tls_cluster ?
connCreateAcceptedTLS(cfd, TLS_CLIENT_AUTH_YES) : connCreateAcceptedSocket(cfd);
/* Make sure connection is not in an error state */
if (connGetState(conn) != CONN_STATE_ACCEPTING) {
serverLog(LL_VERBOSE,
"Error creating an accepting connection for cluster node: %s",
connGetLastError(conn));
connClose(conn);
return;
}
connNonBlock(conn); connNonBlock(conn);
connEnableTcpNoDelay(conn); connEnableTcpNoDelay(conn);
@ -729,6 +752,16 @@ void clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
} }
} }
/* Return the approximated number of sockets we are using in order to
* take the cluster bus connections. */
unsigned long getClusterConnectionsCount(void) {
/* We decrement the number of nodes by one, since there is the
* "myself" node too in the list. Each node uses two file descriptors,
* one incoming and one outgoing, thus the multiplication by 2. */
return g_pserver->cluster_enabled ?
((dictSize(g_pserver->cluster->nodes)-1)*2) : 0;
}
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* Key space handling * Key space handling
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
@ -1069,7 +1102,7 @@ uint64_t clusterGetMaxEpoch(void) {
* 3) Persist the configuration on disk before sending packets with the * 3) Persist the configuration on disk before sending packets with the
* new configuration. * new configuration.
* *
* If the new config epoch is generated and assigend, C_OK is returned, * If the new config epoch is generated and assigned, C_OK is returned,
* otherwise C_ERR is returned (since the node has already the greatest * otherwise C_ERR is returned (since the node has already the greatest
* configuration around) and no operation is performed. * configuration around) and no operation is performed.
* *
@ -1142,7 +1175,7 @@ int clusterBumpConfigEpochWithoutConsensus(void) {
* *
* In general we want a system that eventually always ends with different * In general we want a system that eventually always ends with different
* masters having different configuration epochs whatever happened, since * masters having different configuration epochs whatever happened, since
* nothign is worse than a split-brain condition in a distributed system. * nothing is worse than a split-brain condition in a distributed system.
* *
* BEHAVIOR * BEHAVIOR
* *
@ -1201,7 +1234,7 @@ void clusterHandleConfigEpochCollision(clusterNode *sender) {
* entries from the black list. This is an O(N) operation but it is not a * entries from the black list. This is an O(N) operation but it is not a
* problem since add / exists operations are called very infrequently and * problem since add / exists operations are called very infrequently and
* the hash table is supposed to contain very little elements at max. * the hash table is supposed to contain very little elements at max.
* However without the cleanup during long uptimes and with some automated * However without the cleanup during long uptime and with some automated
* node add/removal procedures, entries could accumulate. */ * node add/removal procedures, entries could accumulate. */
void clusterBlacklistCleanup(void) { void clusterBlacklistCleanup(void) {
dictIterator *di; dictIterator *di;
@ -1292,8 +1325,11 @@ void markNodeAsFailingIfNeeded(clusterNode *node) {
node->fail_time = mstime(); node->fail_time = mstime();
/* Broadcast the failing node name to everybody, forcing all the other /* Broadcast the failing node name to everybody, forcing all the other
* reachable nodes to flag the node as FAIL. */ * reachable nodes to flag the node as FAIL.
if (nodeIsMaster(myself)) clusterSendFail(node->name); * We do that even if this node is a replica and not a master: anyway
* the failing state is triggered collecting failure reports from masters,
* so here the replica is only helping propagating this status. */
clusterSendFail(node->name);
clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG); clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG);
} }
@ -1352,12 +1388,12 @@ int clusterHandshakeInProgress(char *ip, int port, int cport) {
return de != NULL; return de != NULL;
} }
/* Start an handshake with the specified address if there is not one /* Start a handshake with the specified address if there is not one
* already in progress. Returns non-zero if the handshake was actually * already in progress. Returns non-zero if the handshake was actually
* started. On error zero is returned and errno is set to one of the * started. On error zero is returned and errno is set to one of the
* following values: * following values:
* *
* EAGAIN - There is already an handshake in progress for this address. * EAGAIN - There is already a handshake in progress for this address.
* EINVAL - IP or port are not valid. */ * EINVAL - IP or port are not valid. */
int clusterStartHandshake(char *ip, int port, int cport) { int clusterStartHandshake(char *ip, int port, int cport) {
clusterNode *n; clusterNode *n;
@ -1738,7 +1774,7 @@ int clusterProcessPacket(clusterLink *link) {
/* Perform sanity checks */ /* Perform sanity checks */
if (totlen < 16) return 1; /* At least signature, version, totlen, count. */ if (totlen < 16) return 1; /* At least signature, version, totlen, count. */
if (totlen > sdslen(link->rcvbuf)) return 1; if (totlen > link->rcvbuf_len) return 1;
if (ntohs(hdr->ver) != CLUSTER_PROTO_VER) { if (ntohs(hdr->ver) != CLUSTER_PROTO_VER) {
/* Can't handle messages of different versions. */ /* Can't handle messages of different versions. */
@ -1786,7 +1822,7 @@ int clusterProcessPacket(clusterLink *link) {
} else if (type == CLUSTERMSG_TYPE_MODULE) { } else if (type == CLUSTERMSG_TYPE_MODULE) {
uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData); uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);
explen += sizeof(clusterMsgDataPublish) - explen += sizeof(clusterMsgModule) -
3 + ntohl(hdr->data.module.msg.len); 3 + ntohl(hdr->data.module.msg.len);
if (totlen != explen) return 1; if (totlen != explen) return 1;
} }
@ -1803,7 +1839,7 @@ int clusterProcessPacket(clusterLink *link) {
if (sender) sender->data_received = now; if (sender) sender->data_received = now;
if (sender && !nodeInHandshake(sender)) { if (sender && !nodeInHandshake(sender)) {
/* Update our curretEpoch if we see a newer epoch in the cluster. */ /* Update our currentEpoch if we see a newer epoch in the cluster. */
senderCurrentEpoch = ntohu64(hdr->currentEpoch); senderCurrentEpoch = ntohu64(hdr->currentEpoch);
senderConfigEpoch = ntohu64(hdr->configEpoch); senderConfigEpoch = ntohu64(hdr->configEpoch);
if (senderCurrentEpoch > g_pserver->cluster->currentEpoch) if (senderCurrentEpoch > g_pserver->cluster->currentEpoch)
@ -2295,7 +2331,7 @@ void clusterReadHandler(connection *conn) {
unsigned int readlen, rcvbuflen; unsigned int readlen, rcvbuflen;
while(1) { /* Read as long as there is data to read. */ while(1) { /* Read as long as there is data to read. */
rcvbuflen = sdslen(link->rcvbuf); rcvbuflen = link->rcvbuf_len;
if (rcvbuflen < 8) { if (rcvbuflen < 8) {
/* First, obtain the first 8 bytes to get the full message /* First, obtain the first 8 bytes to get the full message
* length. */ * length. */
@ -2331,7 +2367,15 @@ void clusterReadHandler(connection *conn) {
return; return;
} else { } else {
/* Read data and recast the pointer to the new buffer. */ /* Read data and recast the pointer to the new buffer. */
link->rcvbuf = sdscatlen(link->rcvbuf,buf,nread); size_t unused = link->rcvbuf_alloc - link->rcvbuf_len;
if ((size_t)nread > unused) {
size_t required = link->rcvbuf_len + nread;
/* If less than 1mb, grow to twice the needed size, if larger grow by 1mb. */
link->rcvbuf_alloc = required < RCVBUF_MAX_PREALLOC ? required * 2: required + RCVBUF_MAX_PREALLOC;
link->rcvbuf = (char*)zrealloc(link->rcvbuf, link->rcvbuf_alloc);
}
memcpy(link->rcvbuf + link->rcvbuf_len, buf, nread);
link->rcvbuf_len += nread;
hdr = (clusterMsg*) link->rcvbuf; hdr = (clusterMsg*) link->rcvbuf;
rcvbuflen += nread; rcvbuflen += nread;
} }
@ -2339,8 +2383,11 @@ void clusterReadHandler(connection *conn) {
/* Total length obtained? Process this packet. */ /* Total length obtained? Process this packet. */
if (rcvbuflen >= 8 && rcvbuflen == ntohl(hdr->totlen)) { if (rcvbuflen >= 8 && rcvbuflen == ntohl(hdr->totlen)) {
if (clusterProcessPacket(link)) { if (clusterProcessPacket(link)) {
sdsfree(link->rcvbuf); if (link->rcvbuf_alloc > RCVBUF_INIT_LEN) {
link->rcvbuf = sdsempty(); zfree(link->rcvbuf);
link->rcvbuf = (char*)zmalloc(link->rcvbuf_alloc = RCVBUF_INIT_LEN);
}
link->rcvbuf_len = 0;
} else { } else {
return; /* Link no longer valid. */ return; /* Link no longer valid. */
} }
@ -2421,7 +2468,7 @@ void clusterBuildMessageHdr(clusterMsg *hdr, int type) {
* first byte is zero, they'll do auto discovery. */ * first byte is zero, they'll do auto discovery. */
memset(hdr->myip,0,NET_IP_STR_LEN); memset(hdr->myip,0,NET_IP_STR_LEN);
if (g_pserver->cluster_announce_ip) { if (g_pserver->cluster_announce_ip) {
strncpy(hdr->myip,g_pserver->cluster_announce_ip,NET_IP_STR_LEN); strncpy(hdr->myip,g_pserver->cluster_announce_ip,NET_IP_STR_LEN-1);
hdr->myip[NET_IP_STR_LEN-1] = '\0'; hdr->myip[NET_IP_STR_LEN-1] = '\0';
} }
@ -2498,7 +2545,7 @@ void clusterSetGossipEntry(clusterMsg *hdr, int i, clusterNode *n) {
} }
/* Send a PING or PONG packet to the specified node, making sure to add enough /* Send a PING or PONG packet to the specified node, making sure to add enough
* gossip informations. */ * gossip information. */
void clusterSendPing(clusterLink *link, int type) { void clusterSendPing(clusterLink *link, int type) {
unsigned char *buf; unsigned char *buf;
clusterMsg *hdr; clusterMsg *hdr;
@ -2518,7 +2565,7 @@ void clusterSendPing(clusterLink *link, int type) {
* node_timeout we exchange with each other node at least 4 packets * node_timeout we exchange with each other node at least 4 packets
* (we ping in the worst case in node_timeout/2 time, and we also * (we ping in the worst case in node_timeout/2 time, and we also
* receive two pings from the host), we have a total of 8 packets * receive two pings from the host), we have a total of 8 packets
* in the node_timeout*2 falure reports validity time. So we have * in the node_timeout*2 failure reports validity time. So we have
* that, for a single PFAIL node, we can expect to receive the following * that, for a single PFAIL node, we can expect to receive the following
* number of failure reports (in the specified window of time): * number of failure reports (in the specified window of time):
* *
@ -2545,7 +2592,7 @@ void clusterSendPing(clusterLink *link, int type) {
* faster to propagate to go from PFAIL to FAIL state. */ * faster to propagate to go from PFAIL to FAIL state. */
int pfail_wanted = g_pserver->cluster->stats_pfail_nodes; int pfail_wanted = g_pserver->cluster->stats_pfail_nodes;
/* Compute the maxium totlen to allocate our buffer. We'll fix the totlen /* Compute the maximum totlen to allocate our buffer. We'll fix the totlen
* later according to the number of gossip sections we really were able * later according to the number of gossip sections we really were able
* to put inside the packet. */ * to put inside the packet. */
totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData); totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);
@ -2582,7 +2629,7 @@ void clusterSendPing(clusterLink *link, int type) {
if (thisNode->flags & (CLUSTER_NODE_HANDSHAKE|CLUSTER_NODE_NOADDR) || if (thisNode->flags & (CLUSTER_NODE_HANDSHAKE|CLUSTER_NODE_NOADDR) ||
(thisNode->link == NULL && thisNode->numslots == 0)) (thisNode->link == NULL && thisNode->numslots == 0))
{ {
freshnodes--; /* Tecnically not correct, but saves CPU. */ freshnodes--; /* Technically not correct, but saves CPU. */
continue; continue;
} }
@ -3167,7 +3214,7 @@ void clusterHandleSlaveFailover(void) {
} }
} }
/* If the previous failover attempt timedout and the retry time has /* If the previous failover attempt timeout and the retry time has
* elapsed, we can setup a new one. */ * elapsed, we can setup a new one. */
if (auth_age > auth_retry_time) { if (auth_age > auth_retry_time) {
g_pserver->cluster->failover_auth_time = mstime() + g_pserver->cluster->failover_auth_time = mstime() +
@ -3273,7 +3320,7 @@ void clusterHandleSlaveFailover(void) {
* *
* Slave migration is the process that allows a slave of a master that is * Slave migration is the process that allows a slave of a master that is
* already covered by at least another slave, to "migrate" to a master that * already covered by at least another slave, to "migrate" to a master that
* is orpaned, that is, left with no working slaves. * is orphaned, that is, left with no working slaves.
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
/* This function is responsible to decide if this replica should be migrated /* This function is responsible to decide if this replica should be migrated
@ -3290,7 +3337,7 @@ void clusterHandleSlaveFailover(void) {
* the nodes anyway, so we spend time into clusterHandleSlaveMigration() * the nodes anyway, so we spend time into clusterHandleSlaveMigration()
* if definitely needed. * if definitely needed.
* *
* The fuction is called with a pre-computed max_slaves, that is the max * The function is called with a pre-computed max_slaves, that is the max
* number of working (not in FAIL state) slaves for a single master. * number of working (not in FAIL state) slaves for a single master.
* *
* Additional conditions for migration are examined inside the function. * Additional conditions for migration are examined inside the function.
@ -3409,7 +3456,7 @@ void clusterHandleSlaveMigration(int max_slaves) {
* data loss due to the asynchronous master-slave replication. * data loss due to the asynchronous master-slave replication.
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
/* Reset the manual failover state. This works for both masters and slavesa /* Reset the manual failover state. This works for both masters and slaves
* as all the state about manual failover is cleared. * as all the state about manual failover is cleared.
* *
* The function can be used both to initialize the manual failover state at * The function can be used both to initialize the manual failover state at
@ -3495,7 +3542,7 @@ void clusterCron(void) {
* duplicating the string. This way later we can check if * duplicating the string. This way later we can check if
* the address really changed. */ * the address really changed. */
prev_ip = zstrdup(prev_ip); prev_ip = zstrdup(prev_ip);
strncpy(myself->ip,g_pserver->cluster_announce_ip,NET_IP_STR_LEN); strncpy(myself->ip,g_pserver->cluster_announce_ip,NET_IP_STR_LEN-1);
myself->ip[NET_IP_STR_LEN-1] = '\0'; myself->ip[NET_IP_STR_LEN-1] = '\0';
} else { } else {
myself->ip[0] = '\0'; /* Force autodetection. */ myself->ip[0] = '\0'; /* Force autodetection. */
@ -3701,7 +3748,7 @@ void clusterCron(void) {
replicationAddMaster(myself->slaveof->ip, myself->slaveof->port); replicationAddMaster(myself->slaveof->ip, myself->slaveof->port);
} }
/* Abourt a manual failover if the timeout is reached. */ /* Abort a manual failover if the timeout is reached. */
manualFailoverCheckTimeout(); manualFailoverCheckTimeout();
if (nodeIsSlave(myself)) { if (nodeIsSlave(myself)) {
@ -3806,12 +3853,12 @@ int clusterNodeSetSlotBit(clusterNode *n, int slot) {
* target for replicas migration, if and only if at least one of * target for replicas migration, if and only if at least one of
* the other masters has slaves right now. * the other masters has slaves right now.
* *
* Normally masters are valid targerts of replica migration if: * Normally masters are valid targets of replica migration if:
* 1. The used to have slaves (but no longer have). * 1. The used to have slaves (but no longer have).
* 2. They are slaves failing over a master that used to have slaves. * 2. They are slaves failing over a master that used to have slaves.
* *
* However new masters with slots assigned are considered valid * However new masters with slots assigned are considered valid
* migration tagets if the rest of the cluster is not a slave-less. * migration targets if the rest of the cluster is not a slave-less.
* *
* See https://github.com/antirez/redis/issues/3043 for more info. */ * See https://github.com/antirez/redis/issues/3043 for more info. */
if (n->numslots == 1 && clusterMastersHaveSlaves()) if (n->numslots == 1 && clusterMastersHaveSlaves())
@ -3995,7 +4042,7 @@ void clusterUpdateState(void) {
* A) If no other node is in charge according to the current cluster * A) If no other node is in charge according to the current cluster
* configuration, we add these slots to our node. * configuration, we add these slots to our node.
* B) If according to our config other nodes are already in charge for * B) If according to our config other nodes are already in charge for
* this lots, we set the slots as IMPORTING from our point of view * this slots, we set the slots as IMPORTING from our point of view
* in order to justify we have those slots, and in order to make * in order to justify we have those slots, and in order to make
* keydb-trib aware of the issue, so that it can try to fix it. * keydb-trib aware of the issue, so that it can try to fix it.
* 2) If we find data in a DB different than DB0 we return C_ERR to * 2) If we find data in a DB different than DB0 we return C_ERR to
@ -4024,7 +4071,7 @@ int verifyClusterConfigWithData(void) {
/* Make sure we only have keys in DB0. */ /* Make sure we only have keys in DB0. */
for (j = 1; j < cserver.dbnum; j++) { for (j = 1; j < cserver.dbnum; j++) {
if (dictSize(g_pserver->db[j].pdict)) return C_ERR; if (dictSize(g_pserver->db[j].dict)) return C_ERR;
} }
/* Check that all the slots we see populated memory have a corresponding /* Check that all the slots we see populated memory have a corresponding
@ -4141,11 +4188,15 @@ sds clusterGenNodeDescription(clusterNode *node) {
else else
ci = sdscatlen(ci," - ",3); ci = sdscatlen(ci," - ",3);
unsigned long long nodeEpoch = node->configEpoch;
if (nodeIsSlave(node) && node->slaveof) {
nodeEpoch = node->slaveof->configEpoch;
}
/* Latency from the POV of this node, config epoch, link status */ /* Latency from the POV of this node, config epoch, link status */
ci = sdscatprintf(ci,"%lld %lld %llu %s", ci = sdscatprintf(ci,"%lld %lld %llu %s",
(long long) node->ping_sent, (long long) node->ping_sent,
(long long) node->pong_received, (long long) node->pong_received,
(unsigned long long) node->configEpoch, nodeEpoch,
(node->link || node->flags & CLUSTER_NODE_MYSELF) ? (node->link || node->flags & CLUSTER_NODE_MYSELF) ?
"connected" : "disconnected"); "connected" : "disconnected");
@ -4401,7 +4452,7 @@ NULL
clusterReplyMultiBulkSlots(c); clusterReplyMultiBulkSlots(c);
} else if (!strcasecmp(szFromObj(c->argv[1]),"flushslots") && c->argc == 2) { } else if (!strcasecmp(szFromObj(c->argv[1]),"flushslots") && c->argc == 2) {
/* CLUSTER FLUSHSLOTS */ /* CLUSTER FLUSHSLOTS */
if (dictSize(g_pserver->db[0].pdict) != 0) { if (dictSize(g_pserver->db[0].dict) != 0) {
addReplyError(c,"DB must be empty to perform CLUSTER FLUSHSLOTS."); addReplyError(c,"DB must be empty to perform CLUSTER FLUSHSLOTS.");
return; return;
} }
@ -4521,7 +4572,7 @@ NULL
} }
/* If this slot is in migrating status but we have no keys /* If this slot is in migrating status but we have no keys
* for it assigning the slot to another node will clear * for it assigning the slot to another node will clear
* the migratig status. */ * the migrating status. */
if (countKeysInSlot(slot) == 0 && if (countKeysInSlot(slot) == 0 &&
g_pserver->cluster->migrating_slots_to[slot]) g_pserver->cluster->migrating_slots_to[slot])
g_pserver->cluster->migrating_slots_to[slot] = NULL; g_pserver->cluster->migrating_slots_to[slot] = NULL;
@ -4734,7 +4785,7 @@ NULL
* slots nor keys to accept to replicate some other node. * slots nor keys to accept to replicate some other node.
* Slaves can switch to another master without issues. */ * Slaves can switch to another master without issues. */
if (nodeIsMaster(myself) && if (nodeIsMaster(myself) &&
(myself->numslots != 0 || dictSize(g_pserver->db[0].pdict) != 0)) { (myself->numslots != 0 || dictSize(g_pserver->db[0].dict) != 0)) {
addReplyError(c, addReplyError(c,
"To set a master the node must be empty and " "To set a master the node must be empty and "
"without assigned slots."); "without assigned slots.");
@ -4866,7 +4917,7 @@ NULL
g_pserver->cluster->currentEpoch = epoch; g_pserver->cluster->currentEpoch = epoch;
/* No need to fsync the config here since in the unlucky event /* No need to fsync the config here since in the unlucky event
* of a failure to persist the config, the conflict resolution code * of a failure to persist the config, the conflict resolution code
* will assign an unique config to this node. */ * will assign a unique config to this node. */
clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE| clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|
CLUSTER_TODO_SAVE_CONFIG); CLUSTER_TODO_SAVE_CONFIG);
addReply(c,shared.ok); addReply(c,shared.ok);
@ -4891,7 +4942,7 @@ NULL
/* Slaves can be reset while containing data, but not master nodes /* Slaves can be reset while containing data, but not master nodes
* that must be empty. */ * that must be empty. */
if (nodeIsMaster(myself) && dictSize(c->db->pdict) != 0) { if (nodeIsMaster(myself) && dictSize(c->db->dict) != 0) {
addReplyError(c,"CLUSTER RESET can't be called with " addReplyError(c,"CLUSTER RESET can't be called with "
"master nodes containing keys"); "master nodes containing keys");
return; return;
@ -4914,7 +4965,7 @@ void createDumpPayload(rio *payload, robj_roptr o, robj *key) {
unsigned char buf[2]; unsigned char buf[2];
uint64_t crc; uint64_t crc;
/* Serialize the object in a RDB-like format. It consist of an object type /* Serialize the object in an RDB-like format. It consist of an object type
* byte followed by the serialized object. This is understood by RESTORE. */ * byte followed by the serialized object. This is understood by RESTORE. */
rioInitWithBuffer(payload,sdsempty()); rioInitWithBuffer(payload,sdsempty());
serverAssert(rdbSaveObjectType(payload,o)); serverAssert(rdbSaveObjectType(payload,o));
@ -4983,6 +5034,48 @@ void dumpCommand(client *c) {
return; return;
} }
/* KEYDB.MVCCRESTORE key mvcc expire serialized-value */
void mvccrestoreCommand(client *c) {
long long mvcc, expire;
robj *key = c->argv[1], *obj = nullptr;
int type;
if (getLongLongFromObjectOrReply(c, c->argv[2], &mvcc, "Invalid MVCC Tstamp") != C_OK)
return;
if (getLongLongFromObjectOrReply(c, c->argv[3], &expire, "Invalid expire") != C_OK)
return;
/* Verify RDB version and data checksum unles the client is already a replica or master */
if (!(c->flags & (CLIENT_SLAVE | CLIENT_MASTER))) {
if (verifyDumpPayload((unsigned char*)ptrFromObj(c->argv[4]),sdslen(szFromObj(c->argv[4]))) == C_ERR)
{
addReplyError(c,"DUMP payload version or checksum are wrong");
return;
}
}
rio payload;
rioInitWithBuffer(&payload,szFromObj(c->argv[4]));
if (((type = rdbLoadObjectType(&payload)) == -1) ||
((obj = rdbLoadObject(type,&payload,szFromObj(key), OBJ_MVCC_INVALID)) == NULL))
{
addReplyError(c,"Bad data format");
return;
}
setMvccTstamp(obj, mvcc);
/* Create the key and set the TTL if any */
dbMerge(c->db,key,obj,true);
if (expire >= 0) {
setExpire(c,c->db,key,nullptr,expire);
}
signalModifiedKey(c,c->db,key);
notifyKeyspaceEvent(NOTIFY_GENERIC,"restore",key,c->db->id);
addReply(c,shared.ok);
g_pserver->dirty++;
}
/* RESTORE key ttl serialized-value [REPLACE] */ /* RESTORE key ttl serialized-value [REPLACE] */
void restoreCommand(client *c) { void restoreCommand(client *c) {
long long ttl, lfu_freq = -1, lru_idle = -1, lru_clock = -1; long long ttl, lfu_freq = -1, lru_idle = -1, lru_clock = -1;
@ -5025,7 +5118,8 @@ void restoreCommand(client *c) {
} }
/* Make sure this key does not already exist here... */ /* Make sure this key does not already exist here... */
if (!replace && lookupKeyWrite(c->db,c->argv[1]) != NULL) { robj *key = c->argv[1];
if (!replace && lookupKeyWrite(c->db,key) != NULL) {
addReply(c,shared.busykeyerr); addReply(c,shared.busykeyerr);
return; return;
} }
@ -5047,24 +5141,38 @@ void restoreCommand(client *c) {
rioInitWithBuffer(&payload,szFromObj(c->argv[3])); rioInitWithBuffer(&payload,szFromObj(c->argv[3]));
if (((type = rdbLoadObjectType(&payload)) == -1) || if (((type = rdbLoadObjectType(&payload)) == -1) ||
((obj = rdbLoadObject(type,&payload,szFromObj(c->argv[1]), OBJ_MVCC_INVALID)) == NULL)) ((obj = rdbLoadObject(type,&payload,szFromObj(key), OBJ_MVCC_INVALID)) == NULL))
{ {
addReplyError(c,"Bad data format"); addReplyError(c,"Bad data format");
return; return;
} }
/* Remove the old key if needed. */ /* Remove the old key if needed. */
if (replace) dbDelete(c->db,c->argv[1]); int deleted = 0;
if (replace)
deleted = dbDelete(c->db,key);
if (ttl && !absttl) ttl+=mstime();
if (ttl && checkAlreadyExpired(ttl)) {
if (deleted) {
rewriteClientCommandVector(c,2,shared.del,key);
signalModifiedKey(c,c->db,key);
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",key,c->db->id);
g_pserver->dirty++;
}
decrRefCount(obj);
addReply(c, shared.ok);
return;
}
/* Create the key and set the TTL if any */ /* Create the key and set the TTL if any */
dbAdd(c->db,c->argv[1],obj); dbAdd(c->db,key,obj);
if (ttl) { if (ttl) {
if (!absttl) ttl+=mstime(); setExpire(c,c->db,key,nullptr,ttl);
setExpire(c,c->db,c->argv[1],nullptr,ttl);
} }
objectSetLRUOrLFU(obj,lfu_freq,lru_idle,lru_clock,1000); objectSetLRUOrLFU(obj,lfu_freq,lru_idle,lru_clock,1000);
signalModifiedKey(c,c->db,c->argv[1]); signalModifiedKey(c,c->db,key);
notifyKeyspaceEvent(NOTIFY_GENERIC,"restore",c->argv[1],c->db->id); notifyKeyspaceEvent(NOTIFY_GENERIC,"restore",key,c->db->id);
addReply(c,shared.ok); addReply(c,shared.ok);
g_pserver->dirty++; g_pserver->dirty++;
} }
@ -5572,7 +5680,7 @@ void readwriteCommand(client *c) {
* resharding in progress). * resharding in progress).
* *
* On success the function returns the node that is able to serve the request. * On success the function returns the node that is able to serve the request.
* If the node is not 'myself' a redirection must be perfomed. The kind of * If the node is not 'myself' a redirection must be performed. The kind of
* redirection is specified setting the integer passed by reference * redirection is specified setting the integer passed by reference
* 'error_code', which will be set to CLUSTER_REDIR_ASK or * 'error_code', which will be set to CLUSTER_REDIR_ASK or
* CLUSTER_REDIR_MOVED. * CLUSTER_REDIR_MOVED.
@ -5650,7 +5758,10 @@ clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, in
margc = ms->commands[i].argc; margc = ms->commands[i].argc;
margv = ms->commands[i].argv; margv = ms->commands[i].argv;
keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys); getKeysResult result = GETKEYS_RESULT_INIT;
numkeys = getKeysFromCommand(mcmd,margv,margc,&result);
keyindex = result.keys;
for (j = 0; j < numkeys; j++) { for (j = 0; j < numkeys; j++) {
robj *thiskey = margv[keyindex[j]]; robj *thiskey = margv[keyindex[j]];
int thisslot = keyHashSlot((char*)ptrFromObj(thiskey), int thisslot = keyHashSlot((char*)ptrFromObj(thiskey),
@ -5668,7 +5779,7 @@ clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, in
* not trapped earlier in processCommand(). Report the same * not trapped earlier in processCommand(). Report the same
* error to the client. */ * error to the client. */
if (n == NULL) { if (n == NULL) {
getKeysFreeResult(keyindex); getKeysFreeResult(&result);
if (error_code) if (error_code)
*error_code = CLUSTER_REDIR_DOWN_UNBOUND; *error_code = CLUSTER_REDIR_DOWN_UNBOUND;
return NULL; return NULL;
@ -5692,7 +5803,7 @@ clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, in
if (!equalStringObjects(firstkey,thiskey)) { if (!equalStringObjects(firstkey,thiskey)) {
if (slot != thisslot) { if (slot != thisslot) {
/* Error: multiple keys from different slots. */ /* Error: multiple keys from different slots. */
getKeysFreeResult(keyindex); getKeysFreeResult(&result);
if (error_code) if (error_code)
*error_code = CLUSTER_REDIR_CROSS_SLOT; *error_code = CLUSTER_REDIR_CROSS_SLOT;
return NULL; return NULL;
@ -5704,14 +5815,14 @@ clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, in
} }
} }
/* Migarting / Improrting slot? Count keys we don't have. */ /* Migrating / Importing slot? Count keys we don't have. */
if ((migrating_slot || importing_slot) && if ((migrating_slot || importing_slot) &&
lookupKeyRead(&g_pserver->db[0],thiskey) == nullptr) lookupKeyRead(&g_pserver->db[0],thiskey) == nullptr)
{ {
missing_keys++; missing_keys++;
} }
} }
getKeysFreeResult(keyindex); getKeysFreeResult(&result);
} }
/* No key at all in command? then we can serve the request /* No key at all in command? then we can serve the request
@ -5773,10 +5884,12 @@ clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, in
} }
/* Handle the read-only client case reading from a slave: if this /* Handle the read-only client case reading from a slave: if this
* node is a slave and the request is about an hash slot our master * node is a slave and the request is about a hash slot our master
* is serving, we can reply without redirection. */ * is serving, we can reply without redirection. */
int is_readonly_command = (c->cmd->flags & CMD_READONLY) ||
(c->cmd->proc == execCommand && !(c->mstate.cmd_inv_flags & CMD_READONLY));
if (c->flags & CLIENT_READONLY && if (c->flags & CLIENT_READONLY &&
(cmd->flags & CMD_READONLY || cmd->proc == evalCommand || (is_readonly_command || cmd->proc == evalCommand ||
cmd->proc == evalShaCommand) && cmd->proc == evalShaCommand) &&
nodeIsSlave(myself) && nodeIsSlave(myself) &&
myself->slaveof == n) myself->slaveof == n)
@ -5785,7 +5898,7 @@ clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, in
} }
/* Base case: just return the right node. However if this node is not /* Base case: just return the right node. However if this node is not
* myself, set error_code to MOVED since we need to issue a rediretion. */ * myself, set error_code to MOVED since we need to issue a redirection. */
if (n != myself && error_code) *error_code = CLUSTER_REDIR_MOVED; if (n != myself && error_code) *error_code = CLUSTER_REDIR_MOVED;
return n; return n;
} }
@ -5831,7 +5944,7 @@ void clusterRedirectClient(client *c, clusterNode *n, int hashslot, int error_co
* 3) The client may remain blocked forever (or up to the max timeout time) * 3) The client may remain blocked forever (or up to the max timeout time)
* waiting for a key change that will never happen. * waiting for a key change that will never happen.
* *
* If the client is found to be blocked into an hash slot this node no * If the client is found to be blocked into a hash slot this node no
* longer handles, the client is sent a redirection error, and the function * longer handles, the client is sent a redirection error, and the function
* returns 1. Otherwise 0 is returned and no operation is performed. */ * returns 1. Otherwise 0 is returned and no operation is performed. */
int clusterRedirectBlockedClientIfNeeded(client *c) { int clusterRedirectBlockedClientIfNeeded(client *c) {
@ -5860,6 +5973,15 @@ int clusterRedirectBlockedClientIfNeeded(client *c) {
int slot = keyHashSlot((char*)ptrFromObj(key), sdslen(szFromObj(key))); int slot = keyHashSlot((char*)ptrFromObj(key), sdslen(szFromObj(key)));
clusterNode *node = g_pserver->cluster->slots[slot]; clusterNode *node = g_pserver->cluster->slots[slot];
/* if the client is read-only and attempting to access key that our
* replica can handle, allow it. */
if ((c->flags & CLIENT_READONLY) &&
(c->lastcmd->flags & CMD_READONLY) &&
nodeIsSlave(myself) && myself->slaveof == node)
{
node = myself;
}
/* We send an error and unblock the client if: /* We send an error and unblock the client if:
* 1) The slot is unassigned, emitting a cluster down error. * 1) The slot is unassigned, emitting a cluster down error.
* 2) The slot is not handled by this node, nor being imported. */ * 2) The slot is not handled by this node, nor being imported. */

View File

@ -42,7 +42,9 @@ typedef struct clusterLink {
mstime_t ctime; /* Link creation time */ mstime_t ctime; /* Link creation time */
connection *conn; /* Connection to remote node */ connection *conn; /* Connection to remote node */
sds sndbuf; /* Packet send buffer */ sds sndbuf; /* Packet send buffer */
sds rcvbuf; /* Packet reception buffer */ char *rcvbuf; /* Packet reception buffer */
size_t rcvbuf_len; /* Used size of rcvbuf */
size_t rcvbuf_alloc; /* Used size of rcvbuf */
struct clusterNode *node; /* Node related to this link if any, or NULL */ struct clusterNode *node; /* Node related to this link if any, or NULL */
} clusterLink; } clusterLink;
@ -55,8 +57,8 @@ typedef struct clusterLink {
#define CLUSTER_NODE_HANDSHAKE 32 /* We have still to exchange the first ping */ #define CLUSTER_NODE_HANDSHAKE 32 /* We have still to exchange the first ping */
#define CLUSTER_NODE_NOADDR 64 /* We don't know the address of this node */ #define CLUSTER_NODE_NOADDR 64 /* We don't know the address of this node */
#define CLUSTER_NODE_MEET 128 /* Send a MEET message to this node */ #define CLUSTER_NODE_MEET 128 /* Send a MEET message to this node */
#define CLUSTER_NODE_MIGRATE_TO 256 /* Master elegible for replica migration. */ #define CLUSTER_NODE_MIGRATE_TO 256 /* Master eligible for replica migration. */
#define CLUSTER_NODE_NOFAILOVER 512 /* Slave will not try to failver. */ #define CLUSTER_NODE_NOFAILOVER 512 /* Slave will not try to failover. */
#define CLUSTER_NODE_NULL_NAME "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" #define CLUSTER_NODE_NULL_NAME "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
#define nodeIsMaster(n) ((n)->flags & CLUSTER_NODE_MASTER) #define nodeIsMaster(n) ((n)->flags & CLUSTER_NODE_MASTER)
@ -168,10 +170,10 @@ typedef struct clusterState {
clusterNode *mf_slave; /* Slave performing the manual failover. */ clusterNode *mf_slave; /* Slave performing the manual failover. */
/* Manual failover state of slave. */ /* Manual failover state of slave. */
long long mf_master_offset; /* Master offset the slave needs to start MF long long mf_master_offset; /* Master offset the slave needs to start MF
or zero if stil not received. */ or zero if still not received. */
int mf_can_start; /* If non-zero signal that the manual failover int mf_can_start; /* If non-zero signal that the manual failover
can start requesting masters vote. */ can start requesting masters vote. */
/* The followign fields are used by masters to take state on elections. */ /* The following fields are used by masters to take state on elections. */
uint64_t lastVoteEpoch; /* Epoch of the last vote granted. */ uint64_t lastVoteEpoch; /* Epoch of the last vote granted. */
int todo_before_sleep; /* Things to do in clusterBeforeSleep(). */ int todo_before_sleep; /* Things to do in clusterBeforeSleep(). */
/* Messages received and sent by type. */ /* Messages received and sent by type. */
@ -287,6 +289,7 @@ typedef struct {
clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *ask); clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *ask);
int clusterRedirectBlockedClientIfNeeded(client *c); int clusterRedirectBlockedClientIfNeeded(client *c);
void clusterRedirectClient(client *c, clusterNode *n, int hashslot, int error_code); void clusterRedirectClient(client *c, clusterNode *n, int hashslot, int error_code);
unsigned long getClusterConnectionsCount(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -100,6 +100,21 @@ configEnum repl_diskless_load_enum[] = {
{NULL, 0} {NULL, 0}
}; };
configEnum tls_auth_clients_enum[] = {
{"no", TLS_CLIENT_AUTH_NO},
{"yes", TLS_CLIENT_AUTH_YES},
{"optional", TLS_CLIENT_AUTH_OPTIONAL},
{NULL, 0}
};
configEnum oom_score_adj_enum[] = {
{"no", OOM_SCORE_ADJ_NO},
{"yes", OOM_SCORE_RELATIVE},
{"relative", OOM_SCORE_RELATIVE},
{"absolute", OOM_SCORE_ADJ_ABSOLUTE},
{NULL, 0}
};
/* Output buffer limits presets. */ /* Output buffer limits presets. */
clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT] = { clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT] = {
{0, 0, 0}, /* normal */ {0, 0, 0}, /* normal */
@ -107,6 +122,9 @@ clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT] = {
{1024*1024*32, 1024*1024*8, 60} /* pubsub */ {1024*1024*32, 1024*1024*8, 60} /* pubsub */
}; };
/* OOM Score defaults */
int configOOMScoreAdjValuesDefaults[CONFIG_OOM_COUNT] = { 0, 200, 800 };
/* Generic config infrastructure function pointers /* Generic config infrastructure function pointers
* int is_valid_fn(val, err) * int is_valid_fn(val, err)
* Return 1 when val is valid, and 0 when invalid. * Return 1 when val is valid, and 0 when invalid.
@ -289,6 +307,63 @@ void queueLoadModule(sds path, sds *argv, int argc) {
listAddNodeTail(g_pserver->loadmodule_queue,loadmod); listAddNodeTail(g_pserver->loadmodule_queue,loadmod);
} }
/* Parse an array of CONFIG_OOM_COUNT sds strings, validate and populate
* g_pserver->oom_score_adj_values if valid.
*/
static int updateOOMScoreAdjValues(sds *args, const char **err, int apply) {
int i;
int values[CONFIG_OOM_COUNT];
for (i = 0; i < CONFIG_OOM_COUNT; i++) {
char *eptr;
long long val = strtoll(args[i], &eptr, 10);
if (*eptr != '\0' || val < -2000 || val > 2000) {
if (err) *err = "Invalid oom-score-adj-values, elements must be between -2000 and 2000.";
return C_ERR;
}
values[i] = val;
}
/* Verify that the values make sense. If they don't omit a warning but
* keep the configuration, which may still be valid for privileged processes.
*/
if (values[CONFIG_OOM_REPLICA] < values[CONFIG_OOM_MASTER] ||
values[CONFIG_OOM_BGCHILD] < values[CONFIG_OOM_REPLICA]) {
serverLog(LOG_WARNING,
"The oom-score-adj-values configuration may not work for non-privileged processes! "
"Please consult the documentation.");
}
/* Store values, retain previous config for rollback in case we fail. */
int old_values[CONFIG_OOM_COUNT];
for (i = 0; i < CONFIG_OOM_COUNT; i++) {
old_values[i] = g_pserver->oom_score_adj_values[i];
g_pserver->oom_score_adj_values[i] = values[i];
}
/* When parsing the config file, we want to apply only when all is done. */
if (!apply)
return C_OK;
/* Update */
if (setOOMScoreAdj(-1) == C_ERR) {
/* Roll back */
for (i = 0; i < CONFIG_OOM_COUNT; i++)
g_pserver->oom_score_adj_values[i] = old_values[i];
if (err)
*err = "Failed to apply oom-score-adj-values configuration, check server logs.";
return C_ERR;
}
return C_OK;
}
void initConfigValues() { void initConfigValues() {
for (standardConfig *config = configs; config->name != NULL; config++) { for (standardConfig *config = configs; config->name != NULL; config++) {
config->interface.init(config->data); config->interface.init(config->data);
@ -411,7 +486,30 @@ void loadServerConfigFromString(char *config) {
} else if ((!strcasecmp(argv[0],"slaveof") || } else if ((!strcasecmp(argv[0],"slaveof") ||
!strcasecmp(argv[0],"replicaof")) && argc == 3) { !strcasecmp(argv[0],"replicaof")) && argc == 3) {
slaveof_linenum = linenum; slaveof_linenum = linenum;
replicationAddMaster(argv[1], atoi(argv[2])); if (!strcasecmp(argv[1], "no") && !strcasecmp(argv[2], "one")) {
if (listLength(g_pserver->masters)) {
listIter li;
listNode *ln;
listRewind(g_pserver->masters, &li);
while ((ln = listNext(&li)))
{
struct redisMaster *mi = (struct redisMaster*)listNodeValue(ln);
zfree(mi->masterauth);
zfree(mi->masteruser);
zfree(mi->repl_transfer_tmpfile);
delete mi->staleKeyMap;
zfree(mi);
listDelNode(g_pserver->masters, ln);
}
}
continue;
}
char *ptr;
int port = strtol(argv[2], &ptr, 10);
if (port < 0 || port > 65535 || *ptr != '\0') {
err= "Invalid master port"; goto loaderr;
}
replicationAddMaster(argv[1], port);
} else if (!strcasecmp(argv[0],"requirepass") && argc == 2) { } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
if (strlen(argv[1]) > CONFIG_AUTHPASS_MAX_LEN) { if (strlen(argv[1]) > CONFIG_AUTHPASS_MAX_LEN) {
err = "Password is longer than CONFIG_AUTHPASS_MAX_LEN"; err = "Password is longer than CONFIG_AUTHPASS_MAX_LEN";
@ -422,11 +520,16 @@ void loadServerConfigFromString(char *config) {
* additionally is to remember the cleartext password in this * additionally is to remember the cleartext password in this
* case, for backward compatibility with Redis <= 5. */ * case, for backward compatibility with Redis <= 5. */
ACLSetUser(DefaultUser,"resetpass",-1); ACLSetUser(DefaultUser,"resetpass",-1);
sds aclop = sdscatprintf(sdsempty(),">%s",argv[1]);
ACLSetUser(DefaultUser,aclop,sdslen(aclop));
sdsfree(aclop);
sdsfree(g_pserver->requirepass); sdsfree(g_pserver->requirepass);
g_pserver->requirepass = sdsnew(argv[1]); g_pserver->requirepass = NULL;
if (sdslen(argv[1])) {
sds aclop = sdscatprintf(sdsempty(),">%s",argv[1]);
ACLSetUser(DefaultUser,aclop,sdslen(aclop));
sdsfree(aclop);
g_pserver->requirepass = sdsnew(argv[1]);
} else {
ACLSetUser(DefaultUser,"nopass",-1);
}
} else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){ } else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){
/* DEAD OPTION */ /* DEAD OPTION */
} else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) { } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2) {
@ -480,6 +583,8 @@ void loadServerConfigFromString(char *config) {
cserver.client_obuf_limits[type].hard_limit_bytes = hard; cserver.client_obuf_limits[type].hard_limit_bytes = hard;
cserver.client_obuf_limits[type].soft_limit_bytes = soft; cserver.client_obuf_limits[type].soft_limit_bytes = soft;
cserver.client_obuf_limits[type].soft_limit_seconds = soft_seconds; cserver.client_obuf_limits[type].soft_limit_seconds = soft_seconds;
} else if (!strcasecmp(argv[0],"oom-score-adj-values") && argc == 1 + CONFIG_OOM_COUNT) {
if (updateOOMScoreAdjValues(&argv[1], &err, 0) == C_ERR) goto loaderr;
} else if (!strcasecmp(argv[0],"notify-keyspace-events") && argc == 2) { } else if (!strcasecmp(argv[0],"notify-keyspace-events") && argc == 2) {
int flags = keyspaceEventsStringToFlags(argv[1]); int flags = keyspaceEventsStringToFlags(argv[1]);
@ -541,7 +646,7 @@ void loadServerConfigFromString(char *config) {
} }
} else if (!strcasecmp(argv[0], "active-replica") && argc == 2) { } else if (!strcasecmp(argv[0], "active-replica") && argc == 2) {
g_pserver->fActiveReplica = yesnotoi(argv[1]); g_pserver->fActiveReplica = yesnotoi(argv[1]);
if (g_pserver->repl_slave_ro) { if (g_pserver->fActiveReplica && g_pserver->repl_slave_ro) {
g_pserver->repl_slave_ro = FALSE; g_pserver->repl_slave_ro = FALSE;
serverLog(LL_NOTICE, "Notice: \"active-replica yes\" implies \"replica-read-only no\""); serverLog(LL_NOTICE, "Notice: \"active-replica yes\" implies \"replica-read-only no\"");
} }
@ -577,7 +682,7 @@ void loadServerConfigFromString(char *config) {
return; return;
loaderr: loaderr:
fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR (Redis %s) ***\n", fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR (KeyDB %s) ***\n",
KEYDB_REAL_VERSION); KEYDB_REAL_VERSION);
fprintf(stderr, "Reading the configuration file, at line %d\n", linenum); fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
fprintf(stderr, ">>> '%s'\n", lines[i]); fprintf(stderr, ">>> '%s'\n", lines[i]);
@ -605,7 +710,8 @@ void loadServerConfig(char *filename, char *options) {
} else { } else {
if ((fp = fopen(filename,"r")) == NULL) { if ((fp = fopen(filename,"r")) == NULL) {
serverLog(LL_WARNING, serverLog(LL_WARNING,
"Fatal error, can't open config file '%s'", filename); "Fatal error, can't open config file '%s': %s",
filename, strerror(errno));
exit(1); exit(1);
} }
} }
@ -686,11 +792,16 @@ void configSetCommand(client *c) {
* additionally is to remember the cleartext password in this * additionally is to remember the cleartext password in this
* case, for backward compatibility with Redis <= 5. */ * case, for backward compatibility with Redis <= 5. */
ACLSetUser(DefaultUser,"resetpass",-1); ACLSetUser(DefaultUser,"resetpass",-1);
sds aclop = sdscatprintf(sdsempty(),">%s",(char*)ptrFromObj(o));
ACLSetUser(DefaultUser,aclop,sdslen(aclop));
sdsfree(aclop);
sdsfree(g_pserver->requirepass); sdsfree(g_pserver->requirepass);
g_pserver->requirepass = sdsnew(szFromObj(o)); g_pserver->requirepass = NULL;
if (sdslen(szFromObj(o))) {
sds aclop = sdscatprintf(sdsempty(),">%s",(char*)ptrFromObj(o));
ACLSetUser(DefaultUser,aclop,sdslen(aclop));
sdsfree(aclop);
g_pserver->requirepass = sdsnew(szFromObj(o));
} else {
ACLSetUser(DefaultUser,"nopass",-1);
}
} config_set_special_field("save") { } config_set_special_field("save") {
int vlen, j; int vlen, j;
sds *v = sdssplitlen(szFromObj(o),sdslen(szFromObj(o))," ",1,&vlen); sds *v = sdssplitlen(szFromObj(o),sdslen(szFromObj(o))," ",1,&vlen);
@ -775,6 +886,17 @@ void configSetCommand(client *c) {
cserver.client_obuf_limits[type].soft_limit_seconds = soft_seconds; cserver.client_obuf_limits[type].soft_limit_seconds = soft_seconds;
} }
sdsfreesplitres(v,vlen); sdsfreesplitres(v,vlen);
} config_set_special_field("oom-score-adj-values") {
int vlen;
int success = 1;
sds *v = sdssplitlen(szFromObj(o), sdslen(szFromObj(o)), " ", 1, &vlen);
if (vlen != CONFIG_OOM_COUNT || updateOOMScoreAdjValues(v, &errstr, 1) == C_ERR)
success = 0;
sdsfreesplitres(v, vlen);
if (!success)
goto badfmt;
} config_set_special_field("notify-keyspace-events") { } config_set_special_field("notify-keyspace-events") {
int flags = keyspaceEventsStringToFlags(szFromObj(o)); int flags = keyspaceEventsStringToFlags(szFromObj(o));
@ -987,6 +1109,26 @@ void configGetCommand(client *c) {
} }
matches++; matches++;
} }
if (stringmatch(pattern,"oom-score-adj-values",0)) {
sds buf = sdsempty();
int j;
for (j = 0; j < CONFIG_OOM_COUNT; j++) {
buf = sdscatprintf(buf,"%d", g_pserver->oom_score_adj_values[j]);
if (j != CONFIG_OOM_COUNT-1)
buf = sdscatlen(buf," ",1);
}
addReplyBulkCString(c,"oom-score-adj-values");
addReplyBulkCString(c,buf);
sdsfree(buf);
matches++;
}
if (stringmatch(pattern,"active-replica",1)) {
addReplyBulkCString(c,"active-replica");
addReplyBulkCString(c, g_pserver->fActiveReplica ? "yes" : "no");
matches++;
}
setDeferredMapLen(c,replylen,matches); setDeferredMapLen(c,replylen,matches);
} }
@ -1035,6 +1177,8 @@ struct rewriteConfigState {
sds *lines; /* Current lines as an array of sds strings */ sds *lines; /* Current lines as an array of sds strings */
int has_tail; /* True if we already added directives that were int has_tail; /* True if we already added directives that were
not present in the original config file. */ not present in the original config file. */
int force_all; /* True if we want all keywords to be force
written. Currently only used for testing. */
}; };
/* Append the new line to the current configuration state. */ /* Append the new line to the current configuration state. */
@ -1082,6 +1226,7 @@ struct rewriteConfigState *rewriteConfigReadOldFile(char *path) {
state->numlines = 0; state->numlines = 0;
state->lines = NULL; state->lines = NULL;
state->has_tail = 0; state->has_tail = 0;
state->force_all = 0;
if (fp == NULL) return state; if (fp == NULL) return state;
/* Read the old file line by line, populate the state. */ /* Read the old file line by line, populate the state. */
@ -1160,7 +1305,7 @@ void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *opti
rewriteConfigMarkAsProcessed(state,option); rewriteConfigMarkAsProcessed(state,option);
if (!l && !force) { if (!l && !force && !state->force_all) {
/* Option not used previously, and we are not forced to use it. */ /* Option not used previously, and we are not forced to use it. */
sdsfree(line); sdsfree(line);
sdsfree(o); sdsfree(o);
@ -1257,7 +1402,7 @@ void rewriteConfigNumericalOption(struct rewriteConfigState *state, const char *
rewriteConfigRewriteLine(state,option,line,force); rewriteConfigRewriteLine(state,option,line,force);
} }
/* Rewrite a octal option. */ /* Rewrite an octal option. */
void rewriteConfigOctalOption(struct rewriteConfigState *state, const char *option, int value, int defvalue) { void rewriteConfigOctalOption(struct rewriteConfigState *state, const char *option, int value, int defvalue) {
int force = value != defvalue; int force = value != defvalue;
sds line = sdscatprintf(sdsempty(),"%s %o",option,value); sds line = sdscatprintf(sdsempty(),"%s %o",option,value);
@ -1282,6 +1427,12 @@ void rewriteConfigSaveOption(struct rewriteConfigState *state) {
int j; int j;
sds line; sds line;
/* In Sentinel mode we don't need to rewrite the save parameters */
if (g_pserver->sentinel_mode) {
rewriteConfigMarkAsProcessed(state,"save");
return;
}
/* Note that if there are no save parameters at all, all the current /* Note that if there are no save parameters at all, all the current
* config line with "save" will be detected as orphaned and deleted, * config line with "save" will be detected as orphaned and deleted,
* resulting into no RDB persistence as expected. */ * resulting into no RDB persistence as expected. */
@ -1404,6 +1555,26 @@ void rewriteConfigClientoutputbufferlimitOption(struct rewriteConfigState *state
} }
} }
/* Rewrite the oom-score-adj-values option. */
void rewriteConfigOOMScoreAdjValuesOption(struct rewriteConfigState *state) {
int force = 0;
int j;
const char *option = "oom-score-adj-values";
sds line;
line = sdsnew(option);
line = sdscatlen(line, " ", 1);
for (j = 0; j < CONFIG_OOM_COUNT; j++) {
if (g_pserver->oom_score_adj_values[j] != configOOMScoreAdjValuesDefaults[j])
force = 1;
line = sdscatprintf(line, "%d", g_pserver->oom_score_adj_values[j]);
if (j+1 != CONFIG_OOM_COUNT)
line = sdscatlen(line, " ", 1);
}
rewriteConfigRewriteLine(state,option,line,force);
}
/* Rewrite the bind option. */ /* Rewrite the bind option. */
void rewriteConfigBindOption(struct rewriteConfigState *state) { void rewriteConfigBindOption(struct rewriteConfigState *state) {
int force = 1; int force = 1;
@ -1509,60 +1680,62 @@ void rewriteConfigRemoveOrphaned(struct rewriteConfigState *state) {
dictReleaseIterator(di); dictReleaseIterator(di);
} }
/* This function overwrites the old configuration file with the new content. /* This function replaces the old configuration file with the new content
* * in an atomic manner.
* 1) The old file length is obtained.
* 2) If the new content is smaller, padding is added.
* 3) A single write(2) call is used to replace the content of the file.
* 4) Later the file is truncated to the length of the new content.
*
* This way we are sure the file is left in a consistent state even if the
* process is stopped between any of the four operations.
* *
* The function returns 0 on success, otherwise -1 is returned and errno * The function returns 0 on success, otherwise -1 is returned and errno
* set accordingly. */ * is set accordingly. */
int rewriteConfigOverwriteFile(char *configfile, sds content) { int rewriteConfigOverwriteFile(char *configfile, sds content) {
int retval = 0; int fd = -1;
int fd = open(configfile,O_RDWR|O_CREAT,0644); int retval = -1;
int content_size = sdslen(content), padding = 0; char tmp_conffile[PATH_MAX];
struct stat sb; const char *tmp_suffix = ".XXXXXX";
sds content_padded; size_t offset = 0;
ssize_t written_bytes = 0;
/* 1) Open the old file (or create a new one if it does not int tmp_path_len = snprintf(tmp_conffile, sizeof(tmp_conffile), "%s%s", configfile, tmp_suffix);
* exist), get the size. */ if (tmp_path_len <= 0 || (unsigned int)tmp_path_len >= sizeof(tmp_conffile)) {
if (fd == -1) return -1; /* errno set by open(). */ serverLog(LL_WARNING, "Config file full path is too long");
if (fstat(fd,&sb) == -1) { errno = ENAMETOOLONG;
close(fd); return retval;
return -1; /* errno set by fstat(). */
} }
/* 2) Pad the content at least match the old file size. */ #ifdef _GNU_SOURCE
content_padded = sdsdup(content); fd = mkostemp(tmp_conffile, O_CLOEXEC);
if (content_size < sb.st_size) { #else
/* If the old file was bigger, pad the content with /* There's a theoretical chance here to leak the FD if a module thread forks & execv in the middle */
* a newline plus as many "#" chars as required. */ fd = mkstemp(tmp_conffile);
padding = sb.st_size - content_size; #endif
content_padded = sdsgrowzero(content_padded,sb.st_size);
content_padded[content_size] = '\n'; if (fd == -1) {
memset(content_padded+content_size+1,'#',padding-1); serverLog(LL_WARNING, "Could not create tmp config file (%s)", strerror(errno));
return retval;
} }
/* 3) Write the new content using a single write(2). */ while (offset < sdslen(content)) {
if (write(fd,content_padded,strlen(content_padded)) == -1) { written_bytes = write(fd, content + offset, sdslen(content) - offset);
retval = -1; if (written_bytes <= 0) {
goto cleanup; if (errno == EINTR) continue; /* FD is blocking, no other retryable errors */
serverLog(LL_WARNING, "Failed after writing (%zd) bytes to tmp config file (%s)", offset, strerror(errno));
goto cleanup;
}
offset+=written_bytes;
} }
/* 4) Truncate the file to the right length if we used padding. */ if (fsync(fd))
if (padding) { serverLog(LL_WARNING, "Could not sync tmp config file to disk (%s)", strerror(errno));
if (ftruncate(fd,content_size) == -1) { else if (fchmod(fd, 0644) == -1)
/* Non critical error... */ serverLog(LL_WARNING, "Could not chmod config file (%s)", strerror(errno));
} else if (rename(tmp_conffile, configfile) == -1)
serverLog(LL_WARNING, "Could not rename tmp config file (%s)", strerror(errno));
else {
retval = 0;
serverLog(LL_DEBUG, "Rewritten config file (%s) successfully", configfile);
} }
cleanup: cleanup:
sdsfree(content_padded);
close(fd); close(fd);
if (retval) unlink(tmp_conffile);
return retval; return retval;
} }
@ -1572,15 +1745,18 @@ cleanup:
* *
* Configuration parameters that are at their default value, unless already * Configuration parameters that are at their default value, unless already
* explicitly included in the old configuration file, are not rewritten. * explicitly included in the old configuration file, are not rewritten.
* The force_all flag overrides this behavior and forces everything to be
* written. This is currently only used for testing purposes.
* *
* On error -1 is returned and errno is set accordingly, otherwise 0. */ * On error -1 is returned and errno is set accordingly, otherwise 0. */
int rewriteConfig(char *path) { int rewriteConfig(char *path, int force_all) {
struct rewriteConfigState *state; struct rewriteConfigState *state;
sds newcontent; sds newcontent;
int retval; int retval;
/* Step 1: read the old config into our rewrite state. */ /* Step 1: read the old config into our rewrite state. */
if ((state = rewriteConfigReadOldFile(path)) == NULL) return -1; if ((state = rewriteConfigReadOldFile(path)) == NULL) return -1;
if (force_all) state->force_all = 1;
/* Step 2: rewrite every single option, replacing or appending it inside /* Step 2: rewrite every single option, replacing or appending it inside
* the rewrite state. */ * the rewrite state. */
@ -1604,6 +1780,7 @@ int rewriteConfig(char *path) {
rewriteConfigClientoutputbufferlimitOption(state); rewriteConfigClientoutputbufferlimitOption(state);
rewriteConfigYesNoOption(state,"active-replica",g_pserver->fActiveReplica,CONFIG_DEFAULT_ACTIVE_REPLICA); rewriteConfigYesNoOption(state,"active-replica",g_pserver->fActiveReplica,CONFIG_DEFAULT_ACTIVE_REPLICA);
rewriteConfigStringOption(state, "version-override",KEYDB_SET_VERSION,KEYDB_REAL_VERSION); rewriteConfigStringOption(state, "version-override",KEYDB_SET_VERSION,KEYDB_REAL_VERSION);
rewriteConfigOOMScoreAdjValuesOption(state);
/* Rewrite Sentinel config if in Sentinel mode. */ /* Rewrite Sentinel config if in Sentinel mode. */
if (g_pserver->sentinel_mode) rewriteConfigSentinelOption(state); if (g_pserver->sentinel_mode) rewriteConfigSentinelOption(state);
@ -1767,7 +1944,7 @@ static int enumConfigSet(typeData data, sds value, int update, const char **err)
} }
sdsrange(enumerr,0,-3); /* Remove final ", ". */ sdsrange(enumerr,0,-3); /* Remove final ", ". */
strncpy(loadbuf, enumerr, LOADBUF_SIZE); strncpy(loadbuf, enumerr, LOADBUF_SIZE-1);
loadbuf[LOADBUF_SIZE - 1] = '\0'; loadbuf[LOADBUF_SIZE - 1] = '\0';
sdsfree(enumerr); sdsfree(enumerr);
@ -2072,7 +2249,7 @@ static int isValidAOFfilename(char *val, const char **err) {
static int updateHZ(long long val, long long prev, const char **err) { static int updateHZ(long long val, long long prev, const char **err) {
UNUSED(prev); UNUSED(prev);
UNUSED(err); UNUSED(err);
/* Hz is more an hint from the user, so we accept values out of range /* Hz is more a hint from the user, so we accept values out of range
* but cap them to reasonable values. */ * but cap them to reasonable values. */
g_pserver->config_hz = val; g_pserver->config_hz = val;
if (g_pserver->config_hz < CONFIG_MIN_HZ) g_pserver->config_hz = CONFIG_MIN_HZ; if (g_pserver->config_hz < CONFIG_MIN_HZ) g_pserver->config_hz = CONFIG_MIN_HZ;
@ -2090,7 +2267,7 @@ static int updateJemallocBgThread(int val, int prev, const char **err) {
static int updateReplBacklogSize(long long val, long long prev, const char **err) { static int updateReplBacklogSize(long long val, long long prev, const char **err) {
/* resizeReplicationBacklog sets g_pserver->repl_backlog_size, and relies on /* resizeReplicationBacklog sets g_pserver->repl_backlog_size, and relies on
* being able to tell when the size changes, so restore prev becore calling it. */ * being able to tell when the size changes, so restore prev before calling it. */
UNUSED(err); UNUSED(err);
g_pserver->repl_backlog_size = prev; g_pserver->repl_backlog_size = prev;
resizeReplicationBacklog(val); resizeReplicationBacklog(val);
@ -2150,17 +2327,42 @@ static int updateMaxclients(long long val, long long prev, const char **err) {
} }
return 0; return 0;
} }
for (int iel = 0; iel < MAX_EVENT_LOOPS; ++iel) /* Change the SetSize for the current thread first. If any error, return the error message to the client,
* otherwise, continue to do the same for other threads */
if ((unsigned int) aeGetSetSize(aeGetCurrentEventLoop()) <
g_pserver->maxclients + CONFIG_FDSET_INCR)
{ {
if (aeResizeSetSize(aeGetCurrentEventLoop(),
g_pserver->maxclients + CONFIG_FDSET_INCR) == AE_ERR)
{
*err = "The event loop API used by Redis is not able to handle the specified number of clients";
return 0;
}
serverLog(LL_DEBUG,"Successfully changed the setsize for current thread %d", ielFromEventLoop(aeGetCurrentEventLoop()));
}
for (int iel = 0; iel < cserver.cthreads; ++iel)
{
if (g_pserver->rgthreadvar[iel].el == aeGetCurrentEventLoop()){
continue;
}
if ((unsigned int) aeGetSetSize(g_pserver->rgthreadvar[iel].el) < if ((unsigned int) aeGetSetSize(g_pserver->rgthreadvar[iel].el) <
g_pserver->maxclients + CONFIG_FDSET_INCR) g_pserver->maxclients + CONFIG_FDSET_INCR)
{ {
if (aeResizeSetSize(g_pserver->rgthreadvar[iel].el, int res = aePostFunction(g_pserver->rgthreadvar[iel].el, [iel] {
g_pserver->maxclients + CONFIG_FDSET_INCR) == AE_ERR) if (aeResizeSetSize(g_pserver->rgthreadvar[iel].el, g_pserver->maxclients + CONFIG_FDSET_INCR) == AE_ERR) {
{ serverLog(LL_WARNING,"Failed to change the setsize for Thread %d", iel);
*err = "The event loop API used by Redis is not able to handle the specified number of clients"; }
});
if (res != AE_OK){
static char msg[128];
sprintf(msg, "Failed to post the request to change setsize for Thread %d", iel);
*err = msg;
return 0; return 0;
} }
serverLog(LL_DEBUG,"Successfully post the request to change the setsize for thread %d", iel);
} }
} }
} }
@ -2174,12 +2376,28 @@ static int validateMultiMasterNoForward(int val, const char **) {
return 1; return 1;
} }
static int updateOOMScoreAdj(int val, int prev, const char **err) {
UNUSED(prev);
if (val) {
if (setOOMScoreAdj(-1) == C_ERR) {
*err = "Failed to set current oom_score_adj. Check server logs.";
return 0;
}
}
return 1;
}
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
static int updateTlsCfg(char *val, char *prev, const char **err) { static int updateTlsCfg(char *val, char *prev, const char **err) {
UNUSED(val); UNUSED(val);
UNUSED(prev); UNUSED(prev);
UNUSED(err); UNUSED(err);
if (tlsConfigure(&g_pserver->tls_ctx_config) == C_ERR) {
/* If TLS is enabled, try to configure OpenSSL. */
if ((g_pserver->tls_port || g_pserver->tls_replication || g_pserver->tls_cluster)
&& tlsConfigure(&g_pserver->tls_ctx_config) == C_ERR) {
*err = "Unable to update TLS configuration. Check server logs."; *err = "Unable to update TLS configuration. Check server logs.";
return 0; return 0;
} }
@ -2205,6 +2423,7 @@ standardConfig configs[] = {
createBoolConfig("daemonize", NULL, IMMUTABLE_CONFIG, cserver.daemonize, 0, NULL, NULL), createBoolConfig("daemonize", NULL, IMMUTABLE_CONFIG, cserver.daemonize, 0, NULL, NULL),
createBoolConfig("lua-replicate-commands", NULL, MODIFIABLE_CONFIG, g_pserver->lua_always_replicate_commands, 1, NULL, NULL), createBoolConfig("lua-replicate-commands", NULL, MODIFIABLE_CONFIG, g_pserver->lua_always_replicate_commands, 1, NULL, NULL),
createBoolConfig("always-show-logo", NULL, IMMUTABLE_CONFIG, g_pserver->always_show_logo, 0, NULL, NULL), createBoolConfig("always-show-logo", NULL, IMMUTABLE_CONFIG, g_pserver->always_show_logo, 0, NULL, NULL),
createBoolConfig("enable-motd", NULL, IMMUTABLE_CONFIG, cserver.enable_motd, 1, NULL, NULL),
createBoolConfig("protected-mode", NULL, MODIFIABLE_CONFIG, g_pserver->protected_mode, 1, NULL, NULL), createBoolConfig("protected-mode", NULL, MODIFIABLE_CONFIG, g_pserver->protected_mode, 1, NULL, NULL),
createBoolConfig("rdbcompression", NULL, MODIFIABLE_CONFIG, g_pserver->rdb_compression, 1, NULL, NULL), createBoolConfig("rdbcompression", NULL, MODIFIABLE_CONFIG, g_pserver->rdb_compression, 1, NULL, NULL),
createBoolConfig("rdb-del-sync-files", NULL, MODIFIABLE_CONFIG, g_pserver->rdb_del_sync_files, 0, NULL, NULL), createBoolConfig("rdb-del-sync-files", NULL, MODIFIABLE_CONFIG, g_pserver->rdb_del_sync_files, 0, NULL, NULL),
@ -2254,6 +2473,7 @@ standardConfig configs[] = {
createStringConfig("bio_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->bio_cpulist, NULL, NULL, NULL), createStringConfig("bio_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->bio_cpulist, NULL, NULL, NULL),
createStringConfig("aof_rewrite_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->aof_rewrite_cpulist, NULL, NULL, NULL), createStringConfig("aof_rewrite_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->aof_rewrite_cpulist, NULL, NULL, NULL),
createStringConfig("bgsave_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->bgsave_cpulist, NULL, NULL, NULL), createStringConfig("bgsave_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->bgsave_cpulist, NULL, NULL, NULL),
createStringConfig("ignore-warnings", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, g_pserver->ignore_warnings, "ARM64-COW-BUG", NULL, NULL),
/* Enum Configs */ /* Enum Configs */
createEnumConfig("supervised", NULL, IMMUTABLE_CONFIG, supervised_mode_enum, cserver.supervised_mode, SUPERVISED_NONE, NULL, NULL), createEnumConfig("supervised", NULL, IMMUTABLE_CONFIG, supervised_mode_enum, cserver.supervised_mode, SUPERVISED_NONE, NULL, NULL),
@ -2262,6 +2482,7 @@ standardConfig configs[] = {
createEnumConfig("loglevel", NULL, MODIFIABLE_CONFIG, loglevel_enum, cserver.verbosity, LL_NOTICE, NULL, NULL), createEnumConfig("loglevel", NULL, MODIFIABLE_CONFIG, loglevel_enum, cserver.verbosity, LL_NOTICE, NULL, NULL),
createEnumConfig("maxmemory-policy", NULL, MODIFIABLE_CONFIG, maxmemory_policy_enum, g_pserver->maxmemory_policy, MAXMEMORY_NO_EVICTION, NULL, NULL), createEnumConfig("maxmemory-policy", NULL, MODIFIABLE_CONFIG, maxmemory_policy_enum, g_pserver->maxmemory_policy, MAXMEMORY_NO_EVICTION, NULL, NULL),
createEnumConfig("appendfsync", NULL, MODIFIABLE_CONFIG, aof_fsync_enum, g_pserver->aof_fsync, AOF_FSYNC_EVERYSEC, NULL, NULL), createEnumConfig("appendfsync", NULL, MODIFIABLE_CONFIG, aof_fsync_enum, g_pserver->aof_fsync, AOF_FSYNC_EVERYSEC, NULL, NULL),
createEnumConfig("oom-score-adj", NULL, MODIFIABLE_CONFIG, oom_score_adj_enum, g_pserver->oom_score_adj, OOM_SCORE_ADJ_NO, NULL, updateOOMScoreAdj),
/* Integer configs */ /* Integer configs */
createIntConfig("databases", NULL, IMMUTABLE_CONFIG, 1, INT_MAX, cserver.dbnum, 16, INTEGER_CONFIG, NULL, NULL), createIntConfig("databases", NULL, IMMUTABLE_CONFIG, 1, INT_MAX, cserver.dbnum, 16, INTEGER_CONFIG, NULL, NULL),
@ -2298,8 +2519,10 @@ standardConfig configs[] = {
createIntConfig("replica-quorum", NULL, MODIFIABLE_CONFIG, -1, INT_MAX, g_pserver->repl_quorum, -1, INTEGER_CONFIG, NULL, NULL), createIntConfig("replica-quorum", NULL, MODIFIABLE_CONFIG, -1, INT_MAX, g_pserver->repl_quorum, -1, INTEGER_CONFIG, NULL, NULL),
/* Unsigned int configs */ /* Unsigned int configs */
createUIntConfig("maxclients", NULL, MODIFIABLE_CONFIG, 1, UINT_MAX, g_pserver->maxclients, 10000, INTEGER_CONFIG, NULL, updateMaxclients), createUIntConfig("maxclients", NULL, MODIFIABLE_CONFIG, 1, UINT_MAX, g_pserver->maxclients, 10000, INTEGER_CONFIG, NULL, updateMaxclients),
createUIntConfig("loading-process-events-interval-keys", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->loading_process_events_interval_keys, 8192, MEMORY_CONFIG, NULL, NULL),
/* Unsigned Long configs */ /* Unsigned Long configs */
createULongConfig("loading-process-events-interval-bytes", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->loading_process_events_interval_bytes, 2*1024*1024, MEMORY_CONFIG, NULL, NULL),
createULongConfig("active-defrag-max-scan-fields", NULL, MODIFIABLE_CONFIG, 1, LONG_MAX, cserver.active_defrag_max_scan_fields, 1000, INTEGER_CONFIG, NULL, NULL), /* Default: keys with more than 1000 fields will be processed separately */ createULongConfig("active-defrag-max-scan-fields", NULL, MODIFIABLE_CONFIG, 1, LONG_MAX, cserver.active_defrag_max_scan_fields, 1000, INTEGER_CONFIG, NULL, NULL), /* Default: keys with more than 1000 fields will be processed separately */
createULongConfig("slowlog-max-len", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->slowlog_max_len, 128, INTEGER_CONFIG, NULL, NULL), createULongConfig("slowlog-max-len", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->slowlog_max_len, 128, INTEGER_CONFIG, NULL, NULL),
createULongConfig("acllog-max-len", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->acllog_max_len, 128, INTEGER_CONFIG, NULL, NULL), createULongConfig("acllog-max-len", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->acllog_max_len, 128, INTEGER_CONFIG, NULL, NULL),
@ -2309,7 +2532,7 @@ standardConfig configs[] = {
createLongLongConfig("cluster-node-timeout", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->cluster_node_timeout, 15000, INTEGER_CONFIG, NULL, NULL), createLongLongConfig("cluster-node-timeout", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->cluster_node_timeout, 15000, INTEGER_CONFIG, NULL, NULL),
createLongLongConfig("slowlog-log-slower-than", NULL, MODIFIABLE_CONFIG, -1, LLONG_MAX, g_pserver->slowlog_log_slower_than, 10000, INTEGER_CONFIG, NULL, NULL), createLongLongConfig("slowlog-log-slower-than", NULL, MODIFIABLE_CONFIG, -1, LLONG_MAX, g_pserver->slowlog_log_slower_than, 10000, INTEGER_CONFIG, NULL, NULL),
createLongLongConfig("latency-monitor-threshold", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->latency_monitor_threshold, 0, INTEGER_CONFIG, NULL, NULL), createLongLongConfig("latency-monitor-threshold", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->latency_monitor_threshold, 0, INTEGER_CONFIG, NULL, NULL),
createLongLongConfig("proto-max-bulk-len", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->proto_max_bulk_len, 512ll*1024*1024, MEMORY_CONFIG, NULL, NULL), /* Bulk request max size */ createLongLongConfig("proto-max-bulk-len", NULL, MODIFIABLE_CONFIG, 1024*1024, LLONG_MAX, g_pserver->proto_max_bulk_len, 512ll*1024*1024, MEMORY_CONFIG, NULL, NULL), /* Bulk request max size */
createLongLongConfig("stream-node-max-entries", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->stream_node_max_entries, 100, INTEGER_CONFIG, NULL, NULL), createLongLongConfig("stream-node-max-entries", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->stream_node_max_entries, 100, INTEGER_CONFIG, NULL, NULL),
createLongLongConfig("repl-backlog-size", NULL, MODIFIABLE_CONFIG, 1, LLONG_MAX, g_pserver->repl_backlog_size, 1024*1024, MEMORY_CONFIG, NULL, updateReplBacklogSize), /* Default: 1mb */ createLongLongConfig("repl-backlog-size", NULL, MODIFIABLE_CONFIG, 1, LLONG_MAX, g_pserver->repl_backlog_size, 1024*1024, MEMORY_CONFIG, NULL, updateReplBacklogSize), /* Default: 1mb */
@ -2337,7 +2560,7 @@ standardConfig configs[] = {
createIntConfig("tls-session-cache-timeout", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->tls_ctx_config.session_cache_timeout, 300, INTEGER_CONFIG, NULL, updateTlsCfgInt), createIntConfig("tls-session-cache-timeout", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->tls_ctx_config.session_cache_timeout, 300, INTEGER_CONFIG, NULL, updateTlsCfgInt),
createBoolConfig("tls-cluster", NULL, MODIFIABLE_CONFIG, g_pserver->tls_cluster, 0, NULL, NULL), createBoolConfig("tls-cluster", NULL, MODIFIABLE_CONFIG, g_pserver->tls_cluster, 0, NULL, NULL),
createBoolConfig("tls-replication", NULL, MODIFIABLE_CONFIG, g_pserver->tls_replication, 0, NULL, NULL), createBoolConfig("tls-replication", NULL, MODIFIABLE_CONFIG, g_pserver->tls_replication, 0, NULL, NULL),
createBoolConfig("tls-auth-clients", NULL, MODIFIABLE_CONFIG, g_pserver->tls_auth_clients, 1, NULL, NULL), createEnumConfig("tls-auth-clients", NULL, MODIFIABLE_CONFIG, tls_auth_clients_enum, g_pserver->tls_auth_clients, TLS_CLIENT_AUTH_YES, NULL, NULL),
createBoolConfig("tls-prefer-server-ciphers", NULL, MODIFIABLE_CONFIG, g_pserver->tls_ctx_config.prefer_server_ciphers, 0, NULL, updateTlsCfgBool), createBoolConfig("tls-prefer-server-ciphers", NULL, MODIFIABLE_CONFIG, g_pserver->tls_ctx_config.prefer_server_ciphers, 0, NULL, updateTlsCfgBool),
createBoolConfig("tls-session-caching", NULL, MODIFIABLE_CONFIG, g_pserver->tls_ctx_config.session_caching, 1, NULL, updateTlsCfgBool), createBoolConfig("tls-session-caching", NULL, MODIFIABLE_CONFIG, g_pserver->tls_ctx_config.session_caching, 1, NULL, updateTlsCfgBool),
createStringConfig("tls-cert-file", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.cert_file, NULL, NULL, updateTlsCfg), createStringConfig("tls-cert-file", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.cert_file, NULL, NULL, updateTlsCfg),
@ -2387,7 +2610,7 @@ NULL
addReplyError(c,"The server is running without a config file"); addReplyError(c,"The server is running without a config file");
return; return;
} }
if (rewriteConfig(cserver.configfile) == -1) { if (rewriteConfig(cserver.configfile, 0) == -1) {
serverLog(LL_WARNING,"CONFIG REWRITE failed: %s", strerror(errno)); serverLog(LL_WARNING,"CONFIG REWRITE failed: %s", strerror(errno));
addReplyErrorFormat(c,"Rewriting config file: %s", strerror(errno)); addReplyErrorFormat(c,"Rewriting config file: %s", strerror(errno));
} else { } else {

View File

@ -54,6 +54,7 @@
#define HAVE_PROC_MAPS 1 #define HAVE_PROC_MAPS 1
#define HAVE_PROC_SMAPS 1 #define HAVE_PROC_SMAPS 1
#define HAVE_PROC_SOMAXCONN 1 #define HAVE_PROC_SOMAXCONN 1
#define HAVE_PROC_OOM_SCORE_ADJ 1
#endif #endif
/* Test for task_info() */ /* Test for task_info() */
@ -63,7 +64,7 @@
/* Test for backtrace() */ /* Test for backtrace() */
#if defined(__APPLE__) || (defined(__linux__) && defined(__GLIBC__)) || \ #if defined(__APPLE__) || (defined(__linux__) && defined(__GLIBC__)) || \
defined(__FreeBSD__) || (defined(__OpenBSD__) && defined(USE_BACKTRACE))\ defined(__FreeBSD__) || ((defined(__OpenBSD__) || defined(__NetBSD__)) && defined(USE_BACKTRACE))\
|| defined(__DragonFly__) || defined(__DragonFly__)
#define HAVE_BACKTRACE 1 #define HAVE_BACKTRACE 1
#endif #endif
@ -123,6 +124,10 @@
#define USE_SETPROCTITLE #define USE_SETPROCTITLE
#endif #endif
#if defined(__HAIKU__)
#define ESOCKTNOSUPPORT 0
#endif
#if ((defined __linux && defined(__GLIBC__)) || defined __APPLE__) #if ((defined __linux && defined(__GLIBC__)) || defined __APPLE__)
#define USE_SETPROCTITLE #define USE_SETPROCTITLE
#define INIT_SETPROCTITLE_REPLACEMENT #define INIT_SETPROCTITLE_REPLACEMENT
@ -171,7 +176,7 @@ void setproctitle(const char *fmt, ...);
#endif /* BYTE_ORDER */ #endif /* BYTE_ORDER */
/* Sometimes after including an OS-specific header that defines the /* Sometimes after including an OS-specific header that defines the
* endianess we end with __BYTE_ORDER but not with BYTE_ORDER that is what * endianness we end with __BYTE_ORDER but not with BYTE_ORDER that is what
* the Redis code uses. In this case let's define everything without the * the Redis code uses. In this case let's define everything without the
* underscores. */ * underscores. */
#ifndef BYTE_ORDER #ifndef BYTE_ORDER
@ -241,7 +246,7 @@ void setproctitle(const char *fmt, ...);
#define redis_set_thread_title(name) pthread_set_name_np(pthread_self(), name) #define redis_set_thread_title(name) pthread_set_name_np(pthread_self(), name)
#elif defined __NetBSD__ #elif defined __NetBSD__
#include <pthread.h> #include <pthread.h>
#define redis_set_thread_title(name) pthread_setname_np(pthread_self(), name, NULL) #define redis_set_thread_title(name) pthread_setname_np(pthread_self(), "%s", name)
#else #else
#if (defined __APPLE__ && defined(MAC_OS_X_VERSION_10_7)) #if (defined __APPLE__ && defined(MAC_OS_X_VERSION_10_7))
#ifdef __cplusplus #ifdef __cplusplus
@ -257,7 +262,7 @@ int pthread_setname_np(const char *name);
#endif #endif
/* Check if we can use setcpuaffinity(). */ /* Check if we can use setcpuaffinity(). */
#if (defined __linux || defined __NetBSD__ || defined __FreeBSD__) #if (defined __linux || defined __NetBSD__ || defined __FreeBSD__ || defined __DragonFly__)
#define USE_SETCPUAFFINITY #define USE_SETCPUAFFINITY
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"

View File

@ -85,8 +85,12 @@ connection *connCreateSocket() {
/* Create a new socket-type connection that is already associated with /* Create a new socket-type connection that is already associated with
* an accepted connection. * an accepted connection.
* *
* The socket is not read for I/O until connAccept() was called and * The socket is not ready for I/O until connAccept() was called and
* invoked the connection-level accept handler. * invoked the connection-level accept handler.
*
* Callers should use connGetState() and verify the created connection
* is not in an error state (which is not possible for a socket connection,
* but could but possible with other protocols).
*/ */
connection *connCreateAcceptedSocket(int fd) { connection *connCreateAcceptedSocket(int fd) {
connection *conn = connCreateSocket(); connection *conn = connCreateSocket();
@ -164,7 +168,12 @@ static int connSocketWrite(connection *conn, const void *data, size_t data_len)
int ret = write(conn->fd, data, data_len); int ret = write(conn->fd, data, data_len);
if (ret < 0 && errno != EAGAIN) { if (ret < 0 && errno != EAGAIN) {
conn->last_errno = errno; conn->last_errno = errno;
conn->state.store(CONN_STATE_ERROR, std::memory_order_relaxed);
/* Don't overwrite the state of a connection that is not already
* connected, not to mess with handler callbacks.
*/
ConnectionState expected = CONN_STATE_CONNECTED;
conn->state.compare_exchange_strong(expected, CONN_STATE_ERROR, std::memory_order_relaxed);
} }
return ret; return ret;
@ -176,7 +185,12 @@ static int connSocketRead(connection *conn, void *buf, size_t buf_len) {
conn->state.store(CONN_STATE_CLOSED, std::memory_order_release); conn->state.store(CONN_STATE_CLOSED, std::memory_order_release);
} else if (ret < 0 && errno != EAGAIN) { } else if (ret < 0 && errno != EAGAIN) {
conn->last_errno = errno; conn->last_errno = errno;
conn->state.store(CONN_STATE_ERROR, std::memory_order_release);
/* Don't overwrite the state of a connection that is not already
* connected, not to mess with handler callbacks.
*/
ConnectionState expected = CONN_STATE_CONNECTED;
conn->state.compare_exchange_strong(expected, CONN_STATE_ERROR, std::memory_order_release);
} }
return ret; return ret;
@ -256,8 +270,9 @@ static void connSocketEventHandler(struct aeEventLoop *el, int fd, void *clientD
if (conn->state.load(std::memory_order_relaxed) == CONN_STATE_CONNECTING && if (conn->state.load(std::memory_order_relaxed) == CONN_STATE_CONNECTING &&
(mask & AE_WRITABLE) && conn->conn_handler) { (mask & AE_WRITABLE) && conn->conn_handler) {
if (connGetSocketError(conn)) { int conn_error = connGetSocketError(conn);
conn->last_errno = errno; if (conn_error) {
conn->last_errno = conn_error;
conn->state.store(CONN_STATE_ERROR, std::memory_order_release); conn->state.store(CONN_STATE_ERROR, std::memory_order_release);
} else { } else {
conn->state.store(CONN_STATE_CONNECTED, std::memory_order_release); conn->state.store(CONN_STATE_CONNECTED, std::memory_order_release);
@ -334,6 +349,11 @@ static ssize_t connSocketSyncReadLine(connection *conn, char *ptr, ssize_t size,
return syncReadLine(conn->fd, ptr, size, timeout); return syncReadLine(conn->fd, ptr, size, timeout);
} }
static int connSocketGetType(struct connection *conn) {
(void) conn;
return CONN_TYPE_SOCKET;
}
ConnectionType CT_Socket = { ConnectionType CT_Socket = {
connSocketEventHandler, connSocketEventHandler,
@ -348,7 +368,9 @@ ConnectionType CT_Socket = {
connSocketBlockingConnect, connSocketBlockingConnect,
connSocketSyncWrite, connSocketSyncWrite,
connSocketSyncRead, connSocketSyncRead,
connSocketSyncReadLine connSocketSyncReadLine,
nullptr,
connSocketGetType
}; };

View File

@ -52,6 +52,9 @@ typedef enum {
#define CONN_FLAG_READ_THREADSAFE (1<<2) #define CONN_FLAG_READ_THREADSAFE (1<<2)
#define CONN_FLAG_WRITE_THREADSAFE (1<<3) #define CONN_FLAG_WRITE_THREADSAFE (1<<3)
#define CONN_TYPE_SOCKET 1
#define CONN_TYPE_TLS 2
typedef void (*ConnectionCallbackFunc)(struct connection *conn); typedef void (*ConnectionCallbackFunc)(struct connection *conn);
typedef struct ConnectionType { typedef struct ConnectionType {
@ -69,6 +72,7 @@ typedef struct ConnectionType {
ssize_t (*sync_read)(struct connection *conn, char *ptr, ssize_t size, long long timeout); ssize_t (*sync_read)(struct connection *conn, char *ptr, ssize_t size, long long timeout);
ssize_t (*sync_readline)(struct connection *conn, char *ptr, ssize_t size, long long timeout); ssize_t (*sync_readline)(struct connection *conn, char *ptr, ssize_t size, long long timeout);
void (*marshal_thread)(struct connection *conn); void (*marshal_thread)(struct connection *conn);
int (*get_type)(struct connection *conn);
} ConnectionType; } ConnectionType;
struct connection { struct connection {
@ -107,7 +111,7 @@ static inline int connAccept(connection *conn, ConnectionCallbackFunc accept_han
} }
/* Establish a connection. The connect_handler will be called when the connection /* Establish a connection. The connect_handler will be called when the connection
* is established, or if an error has occured. * is established, or if an error has occurred.
* *
* The connection handler will be responsible to set up any read/write handlers * The connection handler will be responsible to set up any read/write handlers
* as needed. * as needed.
@ -169,7 +173,7 @@ static inline int connSetReadHandler(connection *conn, ConnectionCallbackFunc fu
/* Set a write handler, and possibly enable a write barrier, this flag is /* Set a write handler, and possibly enable a write barrier, this flag is
* cleared when write handler is changed or removed. * cleared when write handler is changed or removed.
* With barroer enabled, we never fire the event if the read handler already * With barrier enabled, we never fire the event if the read handler already
* fired in the same event loop iteration. Useful when you want to persist * fired in the same event loop iteration. Useful when you want to persist
* things to disk before sending replies, and want to do that in a group fashion. */ * things to disk before sending replies, and want to do that in a group fashion. */
static inline int connSetWriteHandlerWithBarrier(connection *conn, ConnectionCallbackFunc func, int barrier, bool fThreadSafe = false) { static inline int connSetWriteHandlerWithBarrier(connection *conn, ConnectionCallbackFunc func, int barrier, bool fThreadSafe = false) {
@ -204,6 +208,11 @@ static inline void connMarshalThread(connection *conn) {
conn->type->marshal_thread(conn); conn->type->marshal_thread(conn);
} }
/* Return CONN_TYPE_* for the specified connection */
static inline int connGetType(connection *conn) {
return conn->type->get_type(conn);
}
connection *connCreateSocket(); connection *connCreateSocket();
connection *connCreateAcceptedSocket(int fd); connection *connCreateAcceptedSocket(int fd);
@ -232,6 +241,7 @@ int connSockName(connection *conn, char *ip, size_t ip_len, int *port);
const char *connGetInfo(connection *conn, char *buf, size_t buf_len); const char *connGetInfo(connection *conn, char *buf, size_t buf_len);
/* Helpers for tls special considerations */ /* Helpers for tls special considerations */
sds connTLSGetPeerCert(connection *conn);
int tlsHasPendingData(); int tlsHasPendingData();
int tlsProcessPendingData(); int tlsProcessPendingData();

View File

@ -35,7 +35,8 @@ void crcspeed64little_init(crcfn64 crcfn, uint64_t table[8][256]) {
/* generate CRCs for all single byte sequences */ /* generate CRCs for all single byte sequences */
for (int n = 0; n < 256; n++) { for (int n = 0; n < 256; n++) {
table[0][n] = crcfn(0, &n, 1); unsigned char v = n;
table[0][n] = crcfn(0, &v, 1);
} }
/* generate nested CRC table for future slice-by-8 lookup */ /* generate nested CRC table for future slice-by-8 lookup */

View File

@ -35,6 +35,13 @@
#include <signal.h> #include <signal.h>
#include <ctype.h> #include <ctype.h>
/* Database backup. */
struct dbBackup {
redisDb *dbarray;
rax *slots_to_keys;
uint64_t slots_keys_count[CLUSTER_SLOTS];
};
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
* C-level DB API * C-level DB API
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
@ -86,7 +93,7 @@ void updateDbValAccess(dictEntry *de, int flags)
* implementations that should instead rely on lookupKeyRead(), * implementations that should instead rely on lookupKeyRead(),
* lookupKeyWrite() and lookupKeyReadWithFlags(). */ * lookupKeyWrite() and lookupKeyReadWithFlags(). */
static robj *lookupKey(redisDb *db, robj *key, int flags) { static robj *lookupKey(redisDb *db, robj *key, int flags) {
dictEntry *de = dictFind(db->pdict,ptrFromObj(key)); dictEntry *de = dictFind(db->dict,ptrFromObj(key));
if (de) { if (de) {
robj *val = (robj*)dictGetVal(de); robj *val = (robj*)dictGetVal(de);
@ -131,11 +138,8 @@ robj_roptr lookupKeyReadWithFlags(redisDb *db, robj *key, int flags) {
/* Key expired. If we are in the context of a master, expireIfNeeded() /* Key expired. If we are in the context of a master, expireIfNeeded()
* returns 0 only when the key does not exist at all, so it's safe * returns 0 only when the key does not exist at all, so it's safe
* to return NULL ASAP. */ * to return NULL ASAP. */
if (listLength(g_pserver->masters) == 0) { if (listLength(g_pserver->masters) == 0)
g_pserver->stat_keyspace_misses++; goto keymiss;
notifyKeyspaceEvent(NOTIFY_KEY_MISS, "keymiss", key, db->id);
return NULL;
}
/* However if we are in the context of a replica, expireIfNeeded() will /* However if we are in the context of a replica, expireIfNeeded() will
* not really try to expire the key, it only returns information * not really try to expire the key, it only returns information
@ -145,7 +149,7 @@ robj_roptr lookupKeyReadWithFlags(redisDb *db, robj *key, int flags) {
* However, if the command caller is not the master, and as additional * However, if the command caller is not the master, and as additional
* safety measure, the command invoked is a read-only command, we can * safety measure, the command invoked is a read-only command, we can
* safely return NULL here, and provide a more consistent behavior * safely return NULL here, and provide a more consistent behavior
* to clients accessign expired values in a read-only fashion, that * to clients accessing expired values in a read-only fashion, that
* will say the key as non existing. * will say the key as non existing.
* *
* Notably this covers GETs when slaves are used to scale reads. */ * Notably this covers GETs when slaves are used to scale reads. */
@ -154,19 +158,21 @@ robj_roptr lookupKeyReadWithFlags(redisDb *db, robj *key, int flags) {
serverTL->current_client->cmd && serverTL->current_client->cmd &&
serverTL->current_client->cmd->flags & CMD_READONLY) serverTL->current_client->cmd->flags & CMD_READONLY)
{ {
g_pserver->stat_keyspace_misses++; goto keymiss;
notifyKeyspaceEvent(NOTIFY_KEY_MISS, "keymiss", key, db->id);
return NULL;
} }
} }
val = lookupKey(db,key,flags); val = lookupKey(db,key,flags);
if (val == NULL) { if (val == NULL)
goto keymiss;
g_pserver->stat_keyspace_hits++;
return val;
keymiss:
if (!(flags & LOOKUP_NONOTIFY)) {
g_pserver->stat_keyspace_misses++; g_pserver->stat_keyspace_misses++;
notifyKeyspaceEvent(NOTIFY_KEY_MISS, "keymiss", key, db->id); notifyKeyspaceEvent(NOTIFY_KEY_MISS, "keymiss", key, db->id);
} }
else return NULL;
g_pserver->stat_keyspace_hits++;
return val;
} }
/* Like lookupKeyReadWithFlags(), but does not use any flag, which is the /* Like lookupKeyReadWithFlags(), but does not use any flag, which is the
@ -202,13 +208,15 @@ robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply) {
return o; return o;
} }
int dbAddCore(redisDb *db, robj *key, robj *val) { int dbAddCore(redisDb *db, robj *key, robj *val, bool fUpdateMvcc) {
serverAssert(!val->FExpires()); serverAssert(!val->FExpires());
sds copy = sdsdup(szFromObj(key)); sds copy = sdsdup(szFromObj(key));
int retval = dictAdd(db->pdict, copy, val); int retval = dictAdd(db->dict, copy, val);
uint64_t mvcc = getMvccTstamp(); uint64_t mvcc = getMvccTstamp();
setMvccTstamp(key, mvcc); if (fUpdateMvcc) {
setMvccTstamp(val, mvcc); setMvccTstamp(key, mvcc);
setMvccTstamp(val, mvcc);
}
if (retval == DICT_OK) if (retval == DICT_OK)
{ {
@ -232,7 +240,7 @@ int dbAddCore(redisDb *db, robj *key, robj *val) {
* The program is aborted if the key already exists. */ * The program is aborted if the key already exists. */
void dbAdd(redisDb *db, robj *key, robj *val) void dbAdd(redisDb *db, robj *key, robj *val)
{ {
int retval = dbAddCore(db, key, val); int retval = dbAddCore(db, key, val, true /* fUpdateMvcc */);
serverAssertWithInfo(NULL,key,retval == DICT_OK); serverAssertWithInfo(NULL,key,retval == DICT_OK);
} }
@ -261,14 +269,14 @@ void dbOverwriteCore(redisDb *db, dictEntry *de, robj *key, robj *val, bool fUpd
setMvccTstamp(val, getMvccTstamp()); setMvccTstamp(val, getMvccTstamp());
} }
dictSetVal(db->pdict, de, val); dictSetVal(db->dict, de, val);
if (g_pserver->lazyfree_lazy_server_del) { if (g_pserver->lazyfree_lazy_server_del) {
freeObjAsync(old); freeObjAsync(old);
dictSetVal(db->pdict, &auxentry, NULL); dictSetVal(db->dict, &auxentry, NULL);
} }
dictFreeVal(db->pdict, &auxentry); dictFreeVal(db->dict, &auxentry);
} }
/* Overwrite an existing key with a new value. Incrementing the reference /* Overwrite an existing key with a new value. Incrementing the reference
@ -277,7 +285,7 @@ void dbOverwriteCore(redisDb *db, dictEntry *de, robj *key, robj *val, bool fUpd
* *
* The program is aborted if the key was not already present. */ * The program is aborted if the key was not already present. */
void dbOverwrite(redisDb *db, robj *key, robj *val) { void dbOverwrite(redisDb *db, robj *key, robj *val) {
dictEntry *de = dictFind(db->pdict,ptrFromObj(key)); dictEntry *de = dictFind(db->dict,ptrFromObj(key));
serverAssertWithInfo(NULL,key,de != NULL); serverAssertWithInfo(NULL,key,de != NULL);
dbOverwriteCore(db, de, key, val, !!g_pserver->fActiveReplica, false); dbOverwriteCore(db, de, key, val, !!g_pserver->fActiveReplica, false);
@ -288,9 +296,9 @@ int dbMerge(redisDb *db, robj *key, robj *val, int fReplace)
{ {
if (fReplace) if (fReplace)
{ {
dictEntry *de = dictFind(db->pdict, ptrFromObj(key)); dictEntry *de = dictFind(db->dict, ptrFromObj(key));
if (de == nullptr) if (de == nullptr)
return (dbAddCore(db, key, val) == DICT_OK); return (dbAddCore(db, key, val, false /* fUpdateMvcc */) == DICT_OK);
robj *old = (robj*)dictGetVal(de); robj *old = (robj*)dictGetVal(de);
if (mvccFromObj(old) <= mvccFromObj(val)) if (mvccFromObj(old) <= mvccFromObj(val))
@ -303,7 +311,7 @@ int dbMerge(redisDb *db, robj *key, robj *val, int fReplace)
} }
else else
{ {
return (dbAddCore(db, key, val) == DICT_OK); return (dbAddCore(db, key, val, true /* fUpdateMvcc */) == DICT_OK);
} }
} }
@ -319,7 +327,7 @@ int dbMerge(redisDb *db, robj *key, robj *val, int fReplace)
* The client 'c' argument may be set to NULL if the operation is performed * The client 'c' argument may be set to NULL if the operation is performed
* in a context where there is no clear client performing the operation. */ * in a context where there is no clear client performing the operation. */
void genericSetKey(client *c, redisDb *db, robj *key, robj *val, int keepttl, int signal) { void genericSetKey(client *c, redisDb *db, robj *key, robj *val, int keepttl, int signal) {
dictEntry *de = dictFind(db->pdict, ptrFromObj(key)); dictEntry *de = dictFind(db->dict, ptrFromObj(key));
if (de == NULL) { if (de == NULL) {
dbAdd(db,key,val); dbAdd(db,key,val);
} else { } else {
@ -338,7 +346,7 @@ void setKey(client *c, redisDb *db, robj *key, robj *val) {
/* Return true if the specified key exists in the specified database. /* Return true if the specified key exists in the specified database.
* LRU/LFU info is not updated in any way. */ * LRU/LFU info is not updated in any way. */
int dbExists(redisDb *db, robj *key) { int dbExists(redisDb *db, robj *key) {
return dictFind(db->pdict,ptrFromObj(key)) != NULL; return dictFind(db->dict,ptrFromObj(key)) != NULL;
} }
/* Return a random key, in form of a Redis object. /* Return a random key, in form of a Redis object.
@ -348,13 +356,13 @@ int dbExists(redisDb *db, robj *key) {
robj *dbRandomKey(redisDb *db) { robj *dbRandomKey(redisDb *db) {
dictEntry *de; dictEntry *de;
int maxtries = 100; int maxtries = 100;
int allvolatile = dictSize(db->pdict) == db->setexpire->size(); int allvolatile = dictSize(db->dict) == db->setexpire->size();
while(1) { while(1) {
sds key; sds key;
robj *keyobj; robj *keyobj;
de = dictGetRandomKey(db->pdict); de = dictGetRandomKey(db->dict);
if (de == NULL) return NULL; if (de == NULL) return NULL;
key = (sds)dictGetKey(de); key = (sds)dictGetKey(de);
@ -392,10 +400,10 @@ int dbSyncDelete(redisDb *db, robj *key) {
/* Deleting an entry from the expires dict will not free the sds of /* Deleting an entry from the expires dict will not free the sds of
* the key, because it is shared with the main dictionary. */ * the key, because it is shared with the main dictionary. */
dictEntry *de = dictFind(db->pdict, szFromObj(key)); dictEntry *de = dictFind(db->dict, szFromObj(key));
if (de != nullptr && ((robj*)dictGetVal(de))->FExpires()) if (de != nullptr && ((robj*)dictGetVal(de))->FExpires())
removeExpireCore(db, key, de); removeExpireCore(db, key, de);
if (dictDelete(db->pdict,ptrFromObj(key)) == DICT_OK) { if (dictDelete(db->dict,ptrFromObj(key)) == DICT_OK) {
if (g_pserver->cluster_enabled) slotToKeyDel(szFromObj(key)); if (g_pserver->cluster_enabled) slotToKeyDel(szFromObj(key));
return 1; return 1;
} else { } else {
@ -448,48 +456,18 @@ robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o) {
return o; return o;
} }
/* Remove all keys from all the databases in a Redis g_pserver-> /* Remove all keys from the database(s) structure. The dbarray argument
* If callback is given the function is called from time to time to * may not be the server main DBs (could be a backup).
* signal that work is in progress.
* *
* The dbnum can be -1 if all the DBs should be flushed, or the specified * The dbnum can be -1 if all the DBs should be emptied, or the specified
* DB number if we want to flush only a single Redis database number. * DB index if we want to empty only a single database.
* * The function returns the number of keys removed from the database(s). */
* Flags are be EMPTYDB_NO_FLAGS if no special flags are specified or long long emptyDbStructure(redisDb *dbarray, int dbnum, int async,
* 1. EMPTYDB_ASYNC if we want the memory to be freed in a different thread. void(callback)(void*))
* 2. EMPTYDB_BACKUP if we want to empty the backup dictionaries created by {
* disklessLoadMakeBackups. In that case we only free memory and avoid
* firing module events.
* and the function to return ASAP.
*
* On success the fuction returns the number of keys removed from the
* database(s). Otherwise -1 is returned in the specific case the
* DB number is out of range, and errno is set to EINVAL. */
long long emptyDbGeneric(redisDb *dbarray, int dbnum, int flags, void(callback)(void*)) {
int async = (flags & EMPTYDB_ASYNC);
int backup = (flags & EMPTYDB_BACKUP); /* Just free the memory, nothing else */
RedisModuleFlushInfoV1 fi = {REDISMODULE_FLUSHINFO_VERSION,!async,dbnum};
long long removed = 0; long long removed = 0;
if (dbnum < -1 || dbnum >= cserver.dbnum) {
errno = EINVAL;
return -1;
}
/* Pre-flush actions */
if (!backup) {
/* Fire the flushdb modules event. */
moduleFireServerEvent(REDISMODULE_EVENT_FLUSHDB,
REDISMODULE_SUBEVENT_FLUSHDB_START,
&fi);
/* Make sure the WATCHed keys are affected by the FLUSH* commands.
* Note that we need to call the function while the keys are still
* there. */
signalFlushedDb(dbnum);
}
int startdb, enddb; int startdb, enddb;
if (dbnum == -1) { if (dbnum == -1) {
startdb = 0; startdb = 0;
enddb = cserver.dbnum-1; enddb = cserver.dbnum-1;
@ -498,38 +476,147 @@ long long emptyDbGeneric(redisDb *dbarray, int dbnum, int flags, void(callback)(
} }
for (int j = startdb; j <= enddb; j++) { for (int j = startdb; j <= enddb; j++) {
removed += dictSize(dbarray[j].pdict); removed += dictSize(dbarray[j].dict);
if (async) { if (async) {
emptyDbAsync(&dbarray[j]); emptyDbAsync(&dbarray[j]);
} else { } else {
dictEmpty(dbarray[j].pdict,callback); dictEmpty(dbarray[j].dict,callback);
dbarray[j].setexpire->clear(); dbarray[j].setexpire->clear();
} }
} /* Because all keys of database are removed, reset average ttl. */
dbarray[j].avg_ttl = 0;
/* Post-flush actions */ dbarray[j].last_expire_set = 0;
if (!backup) {
if (g_pserver->cluster_enabled) {
if (async) {
slotToKeyFlushAsync();
} else {
slotToKeyFlush();
}
}
if (dbnum == -1) flushSlaveKeysWithExpireList();
/* Also fire the end event. Note that this event will fire almost
* immediately after the start event if the flush is asynchronous. */
moduleFireServerEvent(REDISMODULE_EVENT_FLUSHDB,
REDISMODULE_SUBEVENT_FLUSHDB_END,
&fi);
} }
return removed; return removed;
} }
/* Remove all keys from all the databases in a Redis server.
* If callback is given the function is called from time to time to
* signal that work is in progress.
*
* The dbnum can be -1 if all the DBs should be flushed, or the specified
* DB number if we want to flush only a single Redis database number.
*
* Flags are be EMPTYDB_NO_FLAGS if no special flags are specified or
* EMPTYDB_ASYNC if we want the memory to be freed in a different thread
* and the function to return ASAP.
*
* On success the function returns the number of keys removed from the
* database(s). Otherwise -1 is returned in the specific case the
* DB number is out of range, and errno is set to EINVAL. */
long long emptyDb(int dbnum, int flags, void(callback)(void*)) { long long emptyDb(int dbnum, int flags, void(callback)(void*)) {
return emptyDbGeneric(g_pserver->db, dbnum, flags, callback); int async = (flags & EMPTYDB_ASYNC);
RedisModuleFlushInfoV1 fi = {REDISMODULE_FLUSHINFO_VERSION,!async,dbnum};
long long removed = 0;
if (dbnum < -1 || dbnum >= cserver.dbnum) {
errno = EINVAL;
return -1;
}
/* Fire the flushdb modules event. */
moduleFireServerEvent(REDISMODULE_EVENT_FLUSHDB,
REDISMODULE_SUBEVENT_FLUSHDB_START,
&fi);
/* Make sure the WATCHed keys are affected by the FLUSH* commands.
* Note that we need to call the function while the keys are still
* there. */
signalFlushedDb(dbnum);
/* Empty redis database structure. */
removed = emptyDbStructure(g_pserver->db, dbnum, async, callback);
/* Flush slots to keys map if enable cluster, we can flush entire
* slots to keys map whatever dbnum because only support one DB
* in cluster mode. */
if (g_pserver->cluster_enabled) slotToKeyFlush(async);
if (dbnum == -1) flushSlaveKeysWithExpireList();
/* Also fire the end event. Note that this event will fire almost
* immediately after the start event if the flush is asynchronous. */
moduleFireServerEvent(REDISMODULE_EVENT_FLUSHDB,
REDISMODULE_SUBEVENT_FLUSHDB_END,
&fi);
return removed;
}
/* Store a backup of the database for later use, and put an empty one
* instead of it. */
dbBackup *backupDb(void) {
dbBackup *backup = (dbBackup*)zmalloc(sizeof(dbBackup));
/* Backup main DBs. */
backup->dbarray = (redisDb*)zmalloc(sizeof(redisDb)*cserver.dbnum);
for (int i=0; i<cserver.dbnum; i++) {
backup->dbarray[i] = g_pserver->db[i];
g_pserver->db[i].dict = dictCreate(&dbDictType,NULL);
g_pserver->db[i].setexpire = new(MALLOC_LOCAL) expireset;
g_pserver->db[i].expireitr = g_pserver->db[i].setexpire->end();
}
/* Backup cluster slots to keys map if enable cluster. */
if (g_pserver->cluster_enabled) {
backup->slots_to_keys = g_pserver->cluster->slots_to_keys;
memcpy(backup->slots_keys_count, g_pserver->cluster->slots_keys_count,
sizeof(g_pserver->cluster->slots_keys_count));
g_pserver->cluster->slots_to_keys = raxNew();
memset(g_pserver->cluster->slots_keys_count, 0,
sizeof(g_pserver->cluster->slots_keys_count));
}
return backup;
}
/* Discard a previously created backup, this can be slow (similar to FLUSHALL)
* Arguments are similar to the ones of emptyDb, see EMPTYDB_ flags. */
void discardDbBackup(dbBackup *buckup, int flags, void(callback)(void*)) {
int async = (flags & EMPTYDB_ASYNC);
/* Release main DBs backup . */
emptyDbStructure(buckup->dbarray, -1, async, callback);
for (int i=0; i<cserver.dbnum; i++) {
dictRelease(buckup->dbarray[i].dict);
delete buckup->dbarray[i].setexpire;
}
/* Release slots to keys map backup if enable cluster. */
if (g_pserver->cluster_enabled) freeSlotsToKeysMap(buckup->slots_to_keys, async);
/* Release buckup. */
zfree(buckup->dbarray);
zfree(buckup);
}
/* Restore the previously created backup (discarding what currently resides
* in the db).
* This function should be called after the current contents of the database
* was emptied with a previous call to emptyDb (possibly using the async mode). */
void restoreDbBackup(dbBackup *buckup) {
/* Restore main DBs. */
for (int i=0; i<cserver.dbnum; i++) {
serverAssert(dictSize(g_pserver->db[i].dict) == 0);
serverAssert(g_pserver->db[i].setexpire->empty());
dictRelease(g_pserver->db[i].dict);
delete g_pserver->db[i].setexpire;
g_pserver->db[i] = buckup->dbarray[i];
}
/* Restore slots to keys map backup if enable cluster. */
if (g_pserver->cluster_enabled) {
serverAssert(g_pserver->cluster->slots_to_keys->numele == 0);
raxFree(g_pserver->cluster->slots_to_keys);
g_pserver->cluster->slots_to_keys = buckup->slots_to_keys;
memcpy(g_pserver->cluster->slots_keys_count, buckup->slots_keys_count,
sizeof(g_pserver->cluster->slots_keys_count));
}
/* Release buckup. */
zfree(buckup->dbarray);
zfree(buckup);
} }
int selectDb(client *c, int id) { int selectDb(client *c, int id) {
@ -543,7 +630,7 @@ long long dbTotalServerKeyCount() {
long long total = 0; long long total = 0;
int j; int j;
for (j = 0; j < cserver.dbnum; j++) { for (j = 0; j < cserver.dbnum; j++) {
total += dictSize(g_pserver->db[j].pdict); total += dictSize(g_pserver->db[j].dict);
} }
return total; return total;
} }
@ -565,7 +652,18 @@ void signalModifiedKey(client *c, redisDb *db, robj *key) {
} }
void signalFlushedDb(int dbid) { void signalFlushedDb(int dbid) {
touchWatchedKeysOnFlush(dbid); int startdb, enddb;
if (dbid == -1) {
startdb = 0;
enddb = cserver.dbnum-1;
} else {
startdb = enddb = dbid;
}
for (int j = startdb; j <= enddb; j++) {
touchAllWatchedKeysInDb(&g_pserver->db[j], NULL);
}
trackingInvalidateKeysOnFlush(dbid); trackingInvalidateKeysOnFlush(dbid);
} }
@ -680,7 +778,7 @@ void existsCommand(client *c) {
int j; int j;
for (j = 1; j < c->argc; j++) { for (j = 1; j < c->argc; j++) {
if (lookupKeyRead(c->db,c->argv[j])) count++; if (lookupKeyReadWithFlags(c->db,c->argv[j],LOOKUP_NOTOUCH)) count++;
} }
addReplyLongLong(c,count); addReplyLongLong(c,count);
} }
@ -730,7 +828,7 @@ void keysCommand(client *c) {
unsigned long numkeys = 0; unsigned long numkeys = 0;
void *replylen = addReplyDeferredLen(c); void *replylen = addReplyDeferredLen(c);
di = dictGetSafeIterator(c->db->pdict); di = dictGetSafeIterator(c->db->dict);
allkeys = (pattern[0] == '*' && plen == 1); allkeys = (pattern[0] == '*' && plen == 1);
while((de = dictNext(di)) != NULL) { while((de = dictNext(di)) != NULL) {
sds key = (sds)dictGetKey(de); sds key = (sds)dictGetKey(de);
@ -874,7 +972,7 @@ void scanGenericCommand(client *c, robj_roptr o, unsigned long cursor) {
/* Handle the case of a hash table. */ /* Handle the case of a hash table. */
ht = NULL; ht = NULL;
if (o == nullptr) { if (o == nullptr) {
ht = c->db->pdict; ht = c->db->dict;
} else if (o->type == OBJ_SET && o->encoding == OBJ_ENCODING_HT) { } else if (o->type == OBJ_SET && o->encoding == OBJ_ENCODING_HT) {
ht = (dict*)ptrFromObj(o); ht = (dict*)ptrFromObj(o);
} else if (o->type == OBJ_HASH && o->encoding == OBJ_ENCODING_HT) { } else if (o->type == OBJ_HASH && o->encoding == OBJ_ENCODING_HT) {
@ -882,7 +980,7 @@ void scanGenericCommand(client *c, robj_roptr o, unsigned long cursor) {
count *= 2; /* We return key / value for this type. */ count *= 2; /* We return key / value for this type. */
} else if (o->type == OBJ_ZSET && o->encoding == OBJ_ENCODING_SKIPLIST) { } else if (o->type == OBJ_ZSET && o->encoding == OBJ_ENCODING_SKIPLIST) {
zset *zs = (zset*)ptrFromObj(o); zset *zs = (zset*)ptrFromObj(o);
ht = zs->pdict; ht = zs->dict;
count *= 2; /* We return key / value for this type. */ count *= 2; /* We return key / value for this type. */
} }
@ -961,7 +1059,7 @@ void scanGenericCommand(client *c, robj_roptr o, unsigned long cursor) {
/* Filter element if it is an expired key. */ /* Filter element if it is an expired key. */
if (!filter && o == nullptr && expireIfNeeded(c->db, kobj)) filter = 1; if (!filter && o == nullptr && expireIfNeeded(c->db, kobj)) filter = 1;
/* Remove the element and its associted value if needed. */ /* Remove the element and its associated value if needed. */
if (filter) { if (filter) {
decrRefCount(kobj); decrRefCount(kobj);
listDelNode(keys, node); listDelNode(keys, node);
@ -1007,7 +1105,7 @@ void scanCommand(client *c) {
} }
void dbsizeCommand(client *c) { void dbsizeCommand(client *c) {
addReplyLongLong(c,dictSize(c->db->pdict)); addReplyLongLong(c,dictSize(c->db->dict));
} }
void lastsaveCommand(client *c) { void lastsaveCommand(client *c) {
@ -1057,14 +1155,6 @@ void shutdownCommand(client *c) {
return; return;
} }
} }
/* When SHUTDOWN is called while the server is loading a dataset in
* memory we need to make sure no attempt is performed to save
* the dataset on shutdown (otherwise it could overwrite the current DB
* with half-read data).
*
* Also when in Sentinel mode clear the SAVE flag and force NOSAVE. */
if (g_pserver->loading || g_pserver->sentinel_mode)
flags = (flags & ~SHUTDOWN_SAVE) | SHUTDOWN_NOSAVE;
if (prepareForShutdown(flags) == C_OK) throw ShutdownException(); if (prepareForShutdown(flags) == C_OK) throw ShutdownException();
addReplyError(c,"Errors trying to SHUTDOWN. Check logs."); addReplyError(c,"Errors trying to SHUTDOWN. Check logs.");
} }
@ -1228,23 +1318,16 @@ int dbSwapDatabases(long id1, long id2) {
if (id1 < 0 || id1 >= cserver.dbnum || if (id1 < 0 || id1 >= cserver.dbnum ||
id2 < 0 || id2 >= cserver.dbnum) return C_ERR; id2 < 0 || id2 >= cserver.dbnum) return C_ERR;
if (id1 == id2) return C_OK; if (id1 == id2) return C_OK;
redisDb aux(g_pserver->db[id1]);
redisDb *db1 = &g_pserver->db[id1], *db2 = &g_pserver->db[id2]; redisDb *db1 = &g_pserver->db[id1], *db2 = &g_pserver->db[id2];
/* Swap hash tables. Note that we don't swap blocking_keys, /* Swap hash tables. Note that we don't swap blocking_keys,
* ready_keys and watched_keys, since we want clients to * ready_keys and watched_keys, since we want clients to
* remain in the same DB they were. */ * remain in the same DB they were. */
db1->pdict = db2->pdict; std::swap(db1->dict, db2->dict);
db1->setexpire = db2->setexpire; std::swap(db1->setexpire, db2->setexpire);
db1->expireitr = db2->expireitr; std::swap(db1->expireitr, db2->expireitr);
db1->avg_ttl = db2->avg_ttl; std::swap(db1->avg_ttl, db2->avg_ttl);
db1->last_expire_set = db2->last_expire_set; std::swap(db1->last_expire_set, db2->last_expire_set);
db2->pdict = aux.pdict;
db2->setexpire = aux.setexpire;
db2->expireitr = aux.expireitr;
db2->avg_ttl = aux.avg_ttl;
db2->last_expire_set = aux.last_expire_set;
/* Now we need to handle clients blocked on lists: as an effect /* Now we need to handle clients blocked on lists: as an effect
* of swapping the two DBs, a client that was waiting for list * of swapping the two DBs, a client that was waiting for list
@ -1254,9 +1337,14 @@ int dbSwapDatabases(long id1, long id2) {
* However normally we only do this check for efficiency reasons * However normally we only do this check for efficiency reasons
* in dbAdd() when a list is created. So here we need to rescan * in dbAdd() when a list is created. So here we need to rescan
* the list of clients blocked on lists and signal lists as ready * the list of clients blocked on lists and signal lists as ready
* if needed. */ * if needed.
*
* Also the swapdb should make transaction fail if there is any
* client watching keys */
scanDatabaseForReadyLists(db1); scanDatabaseForReadyLists(db1);
touchAllWatchedKeysInDb(db1, db2);
scanDatabaseForReadyLists(db2); scanDatabaseForReadyLists(db2);
touchAllWatchedKeysInDb(db2, db1);
return C_OK; return C_OK;
} }
@ -1284,6 +1372,8 @@ void swapdbCommand(client *c) {
addReplyError(c,"DB index is out of range"); addReplyError(c,"DB index is out of range");
return; return;
} else { } else {
RedisModuleSwapDbInfo si = {REDISMODULE_SWAPDBINFO_VERSION,(int32_t)id1,(int32_t)id2};
moduleFireServerEvent(REDISMODULE_EVENT_SWAPDB,0,&si);
g_pserver->dirty++; g_pserver->dirty++;
addReply(c,shared.ok); addReply(c,shared.ok);
} }
@ -1293,7 +1383,7 @@ void swapdbCommand(client *c) {
* Expires API * Expires API
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
int removeExpire(redisDb *db, robj *key) { int removeExpire(redisDb *db, robj *key) {
dictEntry *de = dictFind(db->pdict,ptrFromObj(key)); dictEntry *de = dictFind(db->dict,ptrFromObj(key));
return removeExpireCore(db, key, de); return removeExpireCore(db, key, de);
} }
int removeExpireCore(redisDb *db, robj *key, dictEntry *de) { int removeExpireCore(redisDb *db, robj *key, dictEntry *de) {
@ -1314,7 +1404,7 @@ int removeExpireCore(redisDb *db, robj *key, dictEntry *de) {
} }
int removeSubkeyExpire(redisDb *db, robj *key, robj *subkey) { int removeSubkeyExpire(redisDb *db, robj *key, robj *subkey) {
dictEntry *de = dictFind(db->pdict,ptrFromObj(key)); dictEntry *de = dictFind(db->dict,ptrFromObj(key));
serverAssertWithInfo(NULL,key,de != NULL); serverAssertWithInfo(NULL,key,de != NULL);
robj *val = (robj*)dictGetVal(de); robj *val = (robj*)dictGetVal(de);
@ -1356,13 +1446,13 @@ void setExpire(client *c, redisDb *db, robj *key, robj *subkey, long long when)
serverAssert(GlobalLocksAcquired()); serverAssert(GlobalLocksAcquired());
/* Reuse the sds from the main dict in the expire dict */ /* Reuse the sds from the main dict in the expire dict */
kde = dictFind(db->pdict,ptrFromObj(key)); kde = dictFind(db->dict,ptrFromObj(key));
serverAssertWithInfo(NULL,key,kde != NULL); serverAssertWithInfo(NULL,key,kde != NULL);
if (((robj*)dictGetVal(kde))->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT) if (((robj*)dictGetVal(kde))->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT)
{ {
// shared objects cannot have the expire bit set, create a real object // shared objects cannot have the expire bit set, create a real object
dictSetVal(db->pdict, kde, dupStringObject((robj*)dictGetVal(kde))); dictSetVal(db->dict, kde, dupStringObject((robj*)dictGetVal(kde)));
} }
/* Update TTL stats (exponential moving average) */ /* Update TTL stats (exponential moving average) */
@ -1415,13 +1505,13 @@ void setExpire(client *c, redisDb *db, robj *key, expireEntry &&e)
serverAssert(GlobalLocksAcquired()); serverAssert(GlobalLocksAcquired());
/* Reuse the sds from the main dict in the expire dict */ /* Reuse the sds from the main dict in the expire dict */
kde = dictFind(db->pdict,ptrFromObj(key)); kde = dictFind(db->dict,ptrFromObj(key));
serverAssertWithInfo(NULL,key,kde != NULL); serverAssertWithInfo(NULL,key,kde != NULL);
if (((robj*)dictGetVal(kde))->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT) if (((robj*)dictGetVal(kde))->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT)
{ {
// shared objects cannot have the expire bit set, create a real object // shared objects cannot have the expire bit set, create a real object
dictSetVal(db->pdict, kde, dupStringObject((robj*)dictGetVal(kde))); dictSetVal(db->dict, kde, dupStringObject((robj*)dictGetVal(kde)));
} }
if (((robj*)dictGetVal(kde))->FExpires()) if (((robj*)dictGetVal(kde))->FExpires())
@ -1446,7 +1536,7 @@ expireEntry *getExpire(redisDb *db, robj_roptr key) {
if (db->setexpire->size() == 0) if (db->setexpire->size() == 0)
return nullptr; return nullptr;
de = dictFind(db->pdict, ptrFromObj(key)); de = dictFind(db->dict, ptrFromObj(key));
if (de == NULL) if (de == NULL)
return nullptr; return nullptr;
robj *obj = (robj*)dictGetVal(de); robj *obj = (robj*)dictGetVal(de);
@ -1623,27 +1713,54 @@ int expireIfNeeded(redisDb *db, robj *key) {
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* API to get key arguments from commands * API to get key arguments from commands
* ---------------------------------------------------------------------------*/ * ---------------------------------------------------------------------------*/
#define MAX_KEYS_BUFFER 256
thread_local static int getKeysTempBuffer[MAX_KEYS_BUFFER]; /* Prepare the getKeysResult struct to hold numkeys, either by using the
* pre-allocated keysbuf or by allocating a new array on the heap.
*
* This function must be called at least once before starting to populate
* the result, and can be called repeatedly to enlarge the result array.
*/
int *getKeysPrepareResult(getKeysResult *result, int numkeys) {
/* GETKEYS_RESULT_INIT initializes keys to NULL, point it to the pre-allocated stack
* buffer here. */
if (!result->keys) {
serverAssert(!result->numkeys);
result->keys = result->keysbuf;
}
/* Resize if necessary */
if (numkeys > result->size) {
if (result->keys != result->keysbuf) {
/* We're not using a static buffer, just (re)alloc */
result->keys = (int*)zrealloc(result->keys, numkeys * sizeof(int));
} else {
/* We are using a static buffer, copy its contents */
result->keys = (int*)zmalloc(numkeys * sizeof(int));
if (result->numkeys)
memcpy(result->keys, result->keysbuf, result->numkeys * sizeof(int));
}
result->size = numkeys;
}
return result->keys;
}
/* The base case is to use the keys position as given in the command table /* The base case is to use the keys position as given in the command table
* (firstkey, lastkey, step). */ * (firstkey, lastkey, step). */
int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, int *numkeys) { int getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result) {
int j, i = 0, last, *keys; int j, i = 0, last, *keys;
UNUSED(argv); UNUSED(argv);
if (cmd->firstkey == 0) { if (cmd->firstkey == 0) {
*numkeys = 0; result->numkeys = 0;
return NULL; return 0;
} }
last = cmd->lastkey; last = cmd->lastkey;
if (last < 0) last = argc+last; if (last < 0) last = argc+last;
int count = ((last - cmd->firstkey)+1); int count = ((last - cmd->firstkey)+1);
keys = getKeysTempBuffer; keys = getKeysPrepareResult(result, count);
if (count > MAX_KEYS_BUFFER)
keys = (int*)zmalloc(sizeof(int)*count);
for (j = cmd->firstkey; j <= last; j += cmd->keystep) { for (j = cmd->firstkey; j <= last; j += cmd->keystep) {
if (j >= argc) { if (j >= argc) {
@ -1654,23 +1771,23 @@ int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, in
* return no keys and expect the command implementation to report * return no keys and expect the command implementation to report
* an arity or syntax error. */ * an arity or syntax error. */
if (cmd->flags & CMD_MODULE || cmd->arity < 0) { if (cmd->flags & CMD_MODULE || cmd->arity < 0) {
getKeysFreeResult(keys); getKeysFreeResult(result);
*numkeys = 0; result->numkeys = 0;
return NULL; return 0;
} else { } else {
serverPanic("Redis built-in command declared keys positions not matching the arity requirements."); serverPanic("Redis built-in command declared keys positions not matching the arity requirements.");
} }
} }
keys[i++] = j; keys[i++] = j;
} }
*numkeys = i; result->numkeys = i;
return keys; return i;
} }
/* Return all the arguments that are keys in the command passed via argc / argv. /* Return all the arguments that are keys in the command passed via argc / argv.
* *
* The command returns the positions of all the key arguments inside the array, * The command returns the positions of all the key arguments inside the array,
* so the actual return value is an heap allocated array of integers. The * so the actual return value is a heap allocated array of integers. The
* length of the array is returned by reference into *numkeys. * length of the array is returned by reference into *numkeys.
* *
* 'cmd' must be point to the corresponding entry into the redisCommand * 'cmd' must be point to the corresponding entry into the redisCommand
@ -1678,26 +1795,26 @@ int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, in
* *
* This function uses the command table if a command-specific helper function * This function uses the command table if a command-specific helper function
* is not required, otherwise it calls the command-specific function. */ * is not required, otherwise it calls the command-specific function. */
int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { int getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
if (cmd->flags & CMD_MODULE_GETKEYS) { if (cmd->flags & CMD_MODULE_GETKEYS) {
return moduleGetCommandKeysViaAPI(cmd,argv,argc,numkeys); return moduleGetCommandKeysViaAPI(cmd,argv,argc,result);
} else if (!(cmd->flags & CMD_MODULE) && cmd->getkeys_proc) { } else if (!(cmd->flags & CMD_MODULE) && cmd->getkeys_proc) {
return cmd->getkeys_proc(cmd,argv,argc,numkeys); return cmd->getkeys_proc(cmd,argv,argc,result);
} else { } else {
return getKeysUsingCommandTable(cmd,argv,argc,numkeys); return getKeysUsingCommandTable(cmd,argv,argc,result);
} }
} }
/* Free the result of getKeysFromCommand. */ /* Free the result of getKeysFromCommand. */
void getKeysFreeResult(int *result) { void getKeysFreeResult(getKeysResult *result) {
if (result != getKeysTempBuffer) if (result && result->keys != result->keysbuf)
zfree(result); zfree(result->keys);
} }
/* Helper function to extract keys from following commands: /* Helper function to extract keys from following commands:
* ZUNIONSTORE <destkey> <num-keys> <key> <key> ... <key> <options> * ZUNIONSTORE <destkey> <num-keys> <key> <key> ... <key> <options>
* ZINTERSTORE <destkey> <num-keys> <key> <key> ... <key> <options> */ * ZINTERSTORE <destkey> <num-keys> <key> <key> ... <key> <options> */
int *zunionInterGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { int zunionInterGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
int i, num, *keys; int i, num, *keys;
UNUSED(cmd); UNUSED(cmd);
@ -1705,30 +1822,30 @@ int *zunionInterGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *nu
/* Sanity check. Don't return any key if the command is going to /* Sanity check. Don't return any key if the command is going to
* reply with syntax error. */ * reply with syntax error. */
if (num < 1 || num > (argc-3)) { if (num < 1 || num > (argc-3)) {
*numkeys = 0; result->numkeys = 0;
return NULL; return 0;
} }
/* Keys in z{union,inter}store come from two places: /* Keys in z{union,inter}store come from two places:
* argv[1] = storage key, * argv[1] = storage key,
* argv[3...n] = keys to intersect */ * argv[3...n] = keys to intersect */
keys = getKeysTempBuffer; /* Total keys = {union,inter} keys + storage key */
if (num+1>MAX_KEYS_BUFFER) keys = getKeysPrepareResult(result, num+1);
keys = (int*)zmalloc(sizeof(int)*(num+1)); result->numkeys = num+1;
/* Add all key positions for argv[3...n] to keys[] */ /* Add all key positions for argv[3...n] to keys[] */
for (i = 0; i < num; i++) keys[i] = 3+i; for (i = 0; i < num; i++) keys[i] = 3+i;
/* Finally add the argv[1] key position (the storage key target). */ /* Finally add the argv[1] key position (the storage key target). */
keys[num] = 1; keys[num] = 1;
*numkeys = num+1; /* Total keys = {union,inter} keys + storage key */
return keys; return result->numkeys;
} }
/* Helper function to extract keys from the following commands: /* Helper function to extract keys from the following commands:
* EVAL <script> <num-keys> <key> <key> ... <key> [more stuff] * EVAL <script> <num-keys> <key> <key> ... <key> [more stuff]
* EVALSHA <script> <num-keys> <key> <key> ... <key> [more stuff] */ * EVALSHA <script> <num-keys> <key> <key> ... <key> [more stuff] */
int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { int evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
int i, num, *keys; int i, num, *keys;
UNUSED(cmd); UNUSED(cmd);
@ -1736,20 +1853,17 @@ int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys)
/* Sanity check. Don't return any key if the command is going to /* Sanity check. Don't return any key if the command is going to
* reply with syntax error. */ * reply with syntax error. */
if (num <= 0 || num > (argc-3)) { if (num <= 0 || num > (argc-3)) {
*numkeys = 0; result->numkeys = 0;
return NULL; return 0;
} }
keys = getKeysTempBuffer; keys = getKeysPrepareResult(result, num);
if (num>MAX_KEYS_BUFFER) result->numkeys = num;
keys = (int*)zmalloc(sizeof(int)*num);
*numkeys = num;
/* Add all key positions for argv[3...n] to keys[] */ /* Add all key positions for argv[3...n] to keys[] */
for (i = 0; i < num; i++) keys[i] = 3+i; for (i = 0; i < num; i++) keys[i] = 3+i;
return keys; return result->numkeys;
} }
/* Helper function to extract keys from the SORT command. /* Helper function to extract keys from the SORT command.
@ -1759,13 +1873,12 @@ int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys)
* The first argument of SORT is always a key, however a list of options * The first argument of SORT is always a key, however a list of options
* follow in SQL-alike style. Here we parse just the minimum in order to * follow in SQL-alike style. Here we parse just the minimum in order to
* correctly identify keys in the "STORE" option. */ * correctly identify keys in the "STORE" option. */
int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { int sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
int i, j, num, *keys, found_store = 0; int i, j, num, *keys, found_store = 0;
UNUSED(cmd); UNUSED(cmd);
num = 0; num = 0;
keys = getKeysTempBuffer; /* Alloc 2 places for the worst case. */ keys = getKeysPrepareResult(result, 2); /* Alloc 2 places for the worst case. */
keys[num++] = 1; /* <sort-key> is always present. */ keys[num++] = 1; /* <sort-key> is always present. */
/* Search for STORE option. By default we consider options to don't /* Search for STORE option. By default we consider options to don't
@ -1797,11 +1910,11 @@ int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys)
} }
} }
} }
*numkeys = num + found_store; result->numkeys = num + found_store;
return keys; return result->numkeys;
} }
int *migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { int migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
int i, num, first, *keys; int i, num, first, *keys;
UNUSED(cmd); UNUSED(cmd);
@ -1822,20 +1935,17 @@ int *migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkey
} }
} }
keys = getKeysTempBuffer; keys = getKeysPrepareResult(result, num);
if (num>MAX_KEYS_BUFFER)
keys = (int*)zmalloc(sizeof(int)*num);
for (i = 0; i < num; i++) keys[i] = first+i; for (i = 0; i < num; i++) keys[i] = first+i;
*numkeys = num; result->numkeys = num;
return keys; return num;
} }
/* Helper function to extract keys from following commands: /* Helper function to extract keys from following commands:
* GEORADIUS key x y radius unit [WITHDIST] [WITHHASH] [WITHCOORD] [ASC|DESC] * GEORADIUS key x y radius unit [WITHDIST] [WITHHASH] [WITHCOORD] [ASC|DESC]
* [COUNT count] [STORE key] [STOREDIST key] * [COUNT count] [STORE key] [STOREDIST key]
* GEORADIUSBYMEMBER key member radius unit ... options ... */ * GEORADIUSBYMEMBER key member radius unit ... options ... */
int *georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { int georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
int i, num, *keys; int i, num, *keys;
UNUSED(cmd); UNUSED(cmd);
@ -1858,24 +1968,21 @@ int *georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numk
* argv[1] = key, * argv[1] = key,
* argv[5...n] = stored key if present * argv[5...n] = stored key if present
*/ */
keys = getKeysTempBuffer; keys = getKeysPrepareResult(result, num);
if (num>MAX_KEYS_BUFFER)
keys = (int*)zmalloc(sizeof(int) * num);
/* Add all key positions to keys[] */ /* Add all key positions to keys[] */
keys[0] = 1; keys[0] = 1;
if(num > 1) { if(num > 1) {
keys[1] = stored_key; keys[1] = stored_key;
} }
*numkeys = num; result->numkeys = num;
return keys; return num;
} }
/* LCS ... [KEYS <key1> <key2>] ... */ /* LCS ... [KEYS <key1> <key2>] ... */
int *lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) int lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
{
int i; int i;
int *keys = getKeysTempBuffer; int *keys = getKeysPrepareResult(result, 2);
UNUSED(cmd); UNUSED(cmd);
/* We need to parse the options of the command in order to check for the /* We need to parse the options of the command in order to check for the
@ -1889,33 +1996,32 @@ int *lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys)
} else if (!strcasecmp(arg, "keys") && moreargs >= 2) { } else if (!strcasecmp(arg, "keys") && moreargs >= 2) {
keys[0] = i+1; keys[0] = i+1;
keys[1] = i+2; keys[1] = i+2;
*numkeys = 2; result->numkeys = 2;
return keys; return result->numkeys;
} }
} }
*numkeys = 0; result->numkeys = 0;
return keys; return result->numkeys;
} }
/* Helper function to extract keys from memory command. /* Helper function to extract keys from memory command.
* MEMORY USAGE <key> */ * MEMORY USAGE <key> */
int *memoryGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { int memoryGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
int *keys;
UNUSED(cmd); UNUSED(cmd);
getKeysPrepareResult(result, 1);
if (argc >= 3 && !strcasecmp(szFromObj(argv[1]),"usage")) { if (argc >= 3 && !strcasecmp(szFromObj(argv[1]),"usage")) {
keys = getKeysTempBuffer; result->keys[0] = 2;
keys[0] = 2; result->numkeys = 1;
*numkeys = 1; return result->numkeys;
return keys;
} }
*numkeys = 0; result->numkeys = 0;
return NULL; return 0;
} }
/* XREAD [BLOCK <milliseconds>] [COUNT <count>] [GROUP <groupname> <ttl>] /* XREAD [BLOCK <milliseconds>] [COUNT <count>] [GROUP <groupname> <ttl>]
* STREAMS key_1 key_2 ... key_N ID_1 ID_2 ... ID_N */ * STREAMS key_1 key_2 ... key_N ID_1 ID_2 ... ID_N */
int *xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { int xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
int i, num = 0, *keys; int i, num = 0, *keys;
UNUSED(cmd); UNUSED(cmd);
@ -1945,19 +2051,16 @@ int *xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys)
/* Syntax error. */ /* Syntax error. */
if (streams_pos == -1 || num == 0 || num % 2 != 0) { if (streams_pos == -1 || num == 0 || num % 2 != 0) {
*numkeys = 0; result->numkeys = 0;
return NULL; return 0;
} }
num /= 2; /* We have half the keys as there are arguments because num /= 2; /* We have half the keys as there are arguments because
there are also the IDs, one per key. */ there are also the IDs, one per key. */
keys = getKeysTempBuffer; keys = getKeysPrepareResult(result, num);
if (num>MAX_KEYS_BUFFER)
keys = (int*)zmalloc(sizeof(int) * num);
for (i = streams_pos+1; i < argc-num; i++) keys[i-streams_pos-1] = i; for (i = streams_pos+1; i < argc-num; i++) keys[i-streams_pos-1] = i;
*numkeys = num; result->numkeys = num;
return keys; return num;
} }
/* Slot to Key API. This is used by Redis Cluster in order to obtain in /* Slot to Key API. This is used by Redis Cluster in order to obtain in
@ -1995,13 +2098,25 @@ void slotToKeyDel(sds key) {
slotToKeyUpdateKey(key,0); slotToKeyUpdateKey(key,0);
} }
void slotToKeyFlush(void) { /* Release the radix tree mapping Redis Cluster keys to slots. If 'async'
serverAssert(GlobalLocksAcquired()); * is true, we release it asynchronously. */
void freeSlotsToKeysMap(rax *rt, int async) {
if (async) {
freeSlotsToKeysMapAsync(rt);
} else {
raxFree(rt);
}
}
/* Empty the slots-keys map of Redis CLuster by creating a new empty one and
* freeing the old one. */
void slotToKeyFlush(int async) {
rax *old = g_pserver->cluster->slots_to_keys;
raxFree(g_pserver->cluster->slots_to_keys);
g_pserver->cluster->slots_to_keys = raxNew(); g_pserver->cluster->slots_to_keys = raxNew();
memset(g_pserver->cluster->slots_keys_count,0, memset(g_pserver->cluster->slots_keys_count,0,
sizeof(g_pserver->cluster->slots_keys_count)); sizeof(g_pserver->cluster->slots_keys_count));
freeSlotsToKeysMap(old, async);
} }
/* Pupulate the specified array of objects with keys in the specified slot. /* Pupulate the specified array of objects with keys in the specified slot.

View File

@ -189,7 +189,7 @@ void xorObjectDigest(redisDb *db, robj_roptr keyobj, unsigned char *digest, robj
} }
} else if (o->encoding == OBJ_ENCODING_SKIPLIST) { } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {
zset *zs = (zset*)ptrFromObj(o); zset *zs = (zset*)ptrFromObj(o);
dictIterator *di = dictGetIterator(zs->pdict); dictIterator *di = dictGetIterator(zs->dict);
dictEntry *de; dictEntry *de;
while((de = dictNext(di)) != NULL) { while((de = dictNext(di)) != NULL) {
@ -281,8 +281,8 @@ void computeDatasetDigest(unsigned char *final) {
for (j = 0; j < cserver.dbnum; j++) { for (j = 0; j < cserver.dbnum; j++) {
redisDb *db = g_pserver->db+j; redisDb *db = g_pserver->db+j;
if (dictSize(db->pdict) == 0) continue; if (dictSize(db->dict) == 0) continue;
di = dictGetSafeIterator(db->pdict); di = dictGetSafeIterator(db->dict);
/* hash the DB id, so the same dataset moved in a different /* hash the DB id, so the same dataset moved in a different
* DB will lead to a different digest */ * DB will lead to a different digest */
@ -392,6 +392,7 @@ void debugCommand(client *c) {
"DEBUG PROTOCOL [string|integer|double|bignum|null|array|set|map|attrib|push|verbatim|true|false]", "DEBUG PROTOCOL [string|integer|double|bignum|null|array|set|map|attrib|push|verbatim|true|false]",
"ERROR <string> -- Return a Redis protocol error with <string> as message. Useful for clients unit tests to simulate Redis errors.", "ERROR <string> -- Return a Redis protocol error with <string> as message. Useful for clients unit tests to simulate Redis errors.",
"LOG <message> -- write message to the server log.", "LOG <message> -- write message to the server log.",
"LEAK <string> -- Create a memory leak of the input string.",
"HTSTATS <dbid> -- Return hash table statistics of the specified Redis database.", "HTSTATS <dbid> -- Return hash table statistics of the specified Redis database.",
"HTSTATS-KEY <key> -- Like htstats but for the hash table stored as key's value.", "HTSTATS-KEY <key> -- Like htstats but for the hash table stored as key's value.",
"LOADAOF -- Flush the AOF buffers on disk and reload the AOF in memory.", "LOADAOF -- Flush the AOF buffers on disk and reload the AOF in memory.",
@ -400,7 +401,7 @@ void debugCommand(client *c) {
"OOM -- Crash the server simulating an out-of-memory error.", "OOM -- Crash the server simulating an out-of-memory error.",
"PANIC -- Crash the server simulating a panic.", "PANIC -- Crash the server simulating a panic.",
"POPULATE <count> [prefix] [size] -- Create <count> string keys named key:<num>. If a prefix is specified is used instead of the 'key' prefix.", "POPULATE <count> [prefix] [size] -- Create <count> string keys named key:<num>. If a prefix is specified is used instead of the 'key' prefix.",
"RELOAD [MERGE] [NOFLUSH] [NOSAVE] -- Save the RDB on disk and reload it back in memory. By default it will save the RDB file and load it back. With the NOFLUSH option the current database is not removed before loading the new one, but conficts in keys will kill the server with an exception. When MERGE is used, conflicting keys will be loaded (the key in the loaded RDB file will win). When NOSAVE is used, the server will not save the current dataset in the RDB file before loading. Use DEBUG RELOAD NOSAVE when you want just to load the RDB file you placed in the Redis working directory in order to replace the current dataset in memory. Use DEBUG RELOAD NOSAVE NOFLUSH MERGE when you want to add what is in the current RDB file placed in the Redis current directory, with the current memory content. Use DEBUG RELOAD when you want to verify Redis is able to persist the current dataset in the RDB file, flush the memory content, and load it back.", "RELOAD [MERGE] [NOFLUSH] [NOSAVE] -- Save the RDB on disk and reload it back in memory. By default it will save the RDB file and load it back. With the NOFLUSH option the current database is not removed before loading the new one, but conflicts in keys will kill the server with an exception. When MERGE is used, conflicting keys will be loaded (the key in the loaded RDB file will win). When NOSAVE is used, the server will not save the current dataset in the RDB file before loading. Use DEBUG RELOAD NOSAVE when you want just to load the RDB file you placed in the Redis working directory in order to replace the current dataset in memory. Use DEBUG RELOAD NOSAVE NOFLUSH MERGE when you want to add what is in the current RDB file placed in the Redis current directory, with the current memory content. Use DEBUG RELOAD when you want to verify Redis is able to persist the current dataset in the RDB file, flush the memory content, and load it back.",
"RESTART -- Graceful restart: save config, db, restart.", "RESTART -- Graceful restart: save config, db, restart.",
"SDSLEN <key> -- Show low level SDS string info representing key and value.", "SDSLEN <key> -- Show low level SDS string info representing key and value.",
"SEGFAULT -- Crash the server with sigsegv.", "SEGFAULT -- Crash the server with sigsegv.",
@ -410,6 +411,7 @@ void debugCommand(client *c) {
"STRUCTSIZE -- Return the size of different Redis core C structures.", "STRUCTSIZE -- Return the size of different Redis core C structures.",
"ZIPLIST <key> -- Show low level info about the ziplist encoding.", "ZIPLIST <key> -- Show low level info about the ziplist encoding.",
"STRINGMATCH-TEST -- Run a fuzz tester against the stringmatchlen() function.", "STRINGMATCH-TEST -- Run a fuzz tester against the stringmatchlen() function.",
"CONFIG-REWRITE-FORCE-ALL -- Like CONFIG REWRITE but writes all configuration options, including keywords not listed in original configuration file or default values.",
#ifdef USE_JEMALLOC #ifdef USE_JEMALLOC
"MALLCTL <key> [<val>] -- Get or set a malloc tunning integer.", "MALLCTL <key> [<val>] -- Get or set a malloc tunning integer.",
"MALLCTL-STR <key> [<val>] -- Get or set a malloc tunning string.", "MALLCTL-STR <key> [<val>] -- Get or set a malloc tunning string.",
@ -444,6 +446,9 @@ NULL
} else if (!strcasecmp(szFromObj(c->argv[1]),"log") && c->argc == 3) { } else if (!strcasecmp(szFromObj(c->argv[1]),"log") && c->argc == 3) {
serverLog(LL_WARNING, "DEBUG LOG: %s", (char*)ptrFromObj(c->argv[2])); serverLog(LL_WARNING, "DEBUG LOG: %s", (char*)ptrFromObj(c->argv[2]));
addReply(c,shared.ok); addReply(c,shared.ok);
} else if (!strcasecmp(szFromObj(c->argv[1]),"leak") && c->argc == 3) {
sdsdup(szFromObj(c->argv[2]));
addReply(c,shared.ok);
} else if (!strcasecmp(szFromObj(c->argv[1]),"reload")) { } else if (!strcasecmp(szFromObj(c->argv[1]),"reload")) {
int flush = 1, save = 1; int flush = 1, save = 1;
int flags = RDBFLAGS_NONE; int flags = RDBFLAGS_NONE;
@ -465,7 +470,7 @@ NULL
} }
} }
/* The default beahvior is to save the RDB file before loading /* The default behavior is to save the RDB file before loading
* it back. */ * it back. */
if (save) { if (save) {
rdbSaveInfo rsi, *rsiptr; rdbSaveInfo rsi, *rsiptr;
@ -508,7 +513,7 @@ NULL
robj *val; robj *val;
const char *strenc; const char *strenc;
if ((de = dictFind(c->db->pdict,ptrFromObj(c->argv[2]))) == NULL) { if ((de = dictFind(c->db->dict,ptrFromObj(c->argv[2]))) == NULL) {
addReply(c,shared.nokeyerr); addReply(c,shared.nokeyerr);
return; return;
} }
@ -560,7 +565,7 @@ NULL
robj *val; robj *val;
sds key; sds key;
if ((de = dictFind(c->db->pdict,ptrFromObj(c->argv[2]))) == NULL) { if ((de = dictFind(c->db->dict,ptrFromObj(c->argv[2]))) == NULL) {
addReply(c,shared.nokeyerr); addReply(c,shared.nokeyerr);
return; return;
} }
@ -581,10 +586,10 @@ NULL
(long long) getStringObjectSdsUsedMemory(val)); (long long) getStringObjectSdsUsedMemory(val));
} }
} else if (!strcasecmp(szFromObj(c->argv[1]),"ziplist") && c->argc == 3) { } else if (!strcasecmp(szFromObj(c->argv[1]),"ziplist") && c->argc == 3) {
robj *o; robj_roptr o;
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nokeyerr)) if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nokeyerr))
== NULL) return; == nullptr) return;
if (o->encoding != OBJ_ENCODING_ZIPLIST) { if (o->encoding != OBJ_ENCODING_ZIPLIST) {
addReplyError(c,"Not an sds encoded string."); addReplyError(c,"Not an sds encoded string.");
@ -600,15 +605,14 @@ NULL
if (getLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != C_OK) if (getLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != C_OK)
return; return;
dictExpand(c->db->pdict,keys); dictExpand(c->db->dict,keys);
long valsize = 0;
if ( c->argc == 5 && getLongFromObjectOrReply(c, c->argv[4], &valsize, NULL) != C_OK )
return;
for (j = 0; j < keys; j++) { for (j = 0; j < keys; j++) {
long valsize = 0;
snprintf(buf,sizeof(buf),"%s:%lu", snprintf(buf,sizeof(buf),"%s:%lu",
(c->argc == 3) ? "key" : (char*)ptrFromObj(c->argv[3]), j); (c->argc == 3) ? "key" : (char*)ptrFromObj(c->argv[3]), j);
key = createStringObject(buf,strlen(buf)); key = createStringObject(buf,strlen(buf));
if (c->argc == 5)
if (getLongFromObjectOrReply(c, c->argv[4], &valsize, NULL) != C_OK)
return;
if (lookupKeyWrite(c->db,key) != NULL) { if (lookupKeyWrite(c->db,key) != NULL) {
decrRefCount(key); decrRefCount(key);
continue; continue;
@ -641,7 +645,11 @@ NULL
for (int j = 2; j < c->argc; j++) { for (int j = 2; j < c->argc; j++) {
unsigned char digest[20]; unsigned char digest[20];
memset(digest,0,20); /* Start with a clean result */ memset(digest,0,20); /* Start with a clean result */
robj_roptr o = lookupKeyReadWithFlags(c->db,c->argv[j],LOOKUP_NOTOUCH);
/* We don't use lookupKey because a debug command should
* work on logically expired keys */
dictEntry *de;
robj* o = (robj*)((de = dictFind(c->db->dict,ptrFromObj(c->argv[j]))) == NULL ? NULL : dictGetVal(de));
if (o) xorObjectDigest(c->db,c->argv[j],digest,o); if (o) xorObjectDigest(c->db,c->argv[j],digest,o);
sds d = sdsempty(); sds d = sdsempty();
@ -759,7 +767,7 @@ NULL
} }
stats = sdscatprintf(stats,"[Dictionary HT]\n"); stats = sdscatprintf(stats,"[Dictionary HT]\n");
dictGetStats(buf,sizeof(buf),g_pserver->db[dbid].pdict); dictGetStats(buf,sizeof(buf),g_pserver->db[dbid].dict);
stats = sdscat(stats,buf); stats = sdscat(stats,buf);
stats = sdscatprintf(stats,"[Expires set]\n"); stats = sdscatprintf(stats,"[Expires set]\n");
@ -769,18 +777,18 @@ NULL
addReplyVerbatim(c,stats,sdslen(stats),"txt"); addReplyVerbatim(c,stats,sdslen(stats),"txt");
sdsfree(stats); sdsfree(stats);
} else if (!strcasecmp(szFromObj(c->argv[1]),"htstats-key") && c->argc == 3) { } else if (!strcasecmp(szFromObj(c->argv[1]),"htstats-key") && c->argc == 3) {
robj *o; robj_roptr o;
dict *ht = NULL; dict *ht = NULL;
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nokeyerr)) if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nokeyerr))
== NULL) return; == nullptr) return;
/* Get the hash table reference from the object, if possible. */ /* Get the hash table reference from the object, if possible. */
switch (o->encoding) { switch (o->encoding) {
case OBJ_ENCODING_SKIPLIST: case OBJ_ENCODING_SKIPLIST:
{ {
zset *zs = (zset*)ptrFromObj(o); zset *zs = (zset*)ptrFromObj(o);
ht = zs->pdict; ht = zs->dict;
} }
break; break;
case OBJ_ENCODING_HT: case OBJ_ENCODING_HT:
@ -825,6 +833,19 @@ NULL
c->flags &= ~(CLIENT_MASTER | CLIENT_MASTER_FORCE_REPLY); c->flags &= ~(CLIENT_MASTER | CLIENT_MASTER_FORCE_REPLY);
} }
addReply(c, shared.ok); addReply(c, shared.ok);
} else if (!strcasecmp(szFromObj(c->argv[1]),"truncate-repl-backlog") && c->argc == 2) {
g_pserver->repl_backlog_idx = 0;
g_pserver->repl_backlog_off = g_pserver->master_repl_offset+1;
g_pserver->repl_backlog_histlen = 0;
if (g_pserver->repl_batch_idxStart >= 0) g_pserver->repl_batch_idxStart = -1;
if (g_pserver->repl_batch_offStart >= 0) g_pserver->repl_batch_offStart = -1;
addReply(c, shared.ok);
} else if (!strcasecmp(szFromObj(c->argv[1]),"config-rewrite-force-all") && c->argc == 2)
{
if (rewriteConfig(cserver.configfile, 1) == -1)
addReplyError(c, "CONFIG-REWRITE-FORCE-ALL failed");
else
addReply(c, shared.ok);
#ifdef USE_JEMALLOC #ifdef USE_JEMALLOC
} else if(!strcasecmp(szFromObj(c->argv[1]),"mallctl") && c->argc >= 3) { } else if(!strcasecmp(szFromObj(c->argv[1]),"mallctl") && c->argc >= 3) {
mallctl_int(c, c->argv+2, c->argc-2); mallctl_int(c, c->argv+2, c->argc-2);
@ -958,12 +979,15 @@ static void *getMcontextEip(ucontext_t *uc) {
/* OSX >= 10.6 */ /* OSX >= 10.6 */
#if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__) #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
return (void*) uc->uc_mcontext->__ss.__rip; return (void*) uc->uc_mcontext->__ss.__rip;
#else #elif defined(__i386__)
return (void*) uc->uc_mcontext->__ss.__eip; return (void*) uc->uc_mcontext->__ss.__eip;
#else
/* OSX ARM64 */
return (void*) arm_thread_state64_get_pc(uc->uc_mcontext->__ss);
#endif #endif
#elif defined(__linux__) #elif defined(__linux__)
/* Linux */ /* Linux */
#if defined(__i386__) || defined(__ILP32__) #if defined(__i386__) || ((defined(__X86_64__) || defined(__x86_64__)) && defined(__ILP32__))
return (void*) uc->uc_mcontext.gregs[14]; /* Linux 32 */ return (void*) uc->uc_mcontext.gregs[14]; /* Linux 32 */
#elif defined(__X86_64__) || defined(__x86_64__) #elif defined(__X86_64__) || defined(__x86_64__)
return (void*) uc->uc_mcontext.gregs[16]; /* Linux 64 */ return (void*) uc->uc_mcontext.gregs[16]; /* Linux 64 */
@ -988,6 +1012,12 @@ static void *getMcontextEip(ucontext_t *uc) {
#elif defined(__x86_64__) #elif defined(__x86_64__)
return (void*) uc->sc_rip; return (void*) uc->sc_rip;
#endif #endif
#elif defined(__NetBSD__)
#if defined(__i386__)
return (void*) uc->uc_mcontext.__gregs[_REG_EIP];
#elif defined(__x86_64__)
return (void*) uc->uc_mcontext.__gregs[_REG_RIP];
#endif
#elif defined(__DragonFly__) #elif defined(__DragonFly__)
return (void*) uc->uc_mcontext.mc_rip; return (void*) uc->uc_mcontext.mc_rip;
#else #else
@ -1045,7 +1075,7 @@ void logRegisters(ucontext_t *uc) {
(unsigned long) uc->uc_mcontext->__ss.__gs (unsigned long) uc->uc_mcontext->__ss.__gs
); );
logStackContent((void**)uc->uc_mcontext->__ss.__rsp); logStackContent((void**)uc->uc_mcontext->__ss.__rsp);
#else #elif defined(__i386__)
/* OSX x86 */ /* OSX x86 */
serverLog(LL_WARNING, serverLog(LL_WARNING,
"\n" "\n"
@ -1071,11 +1101,60 @@ void logRegisters(ucontext_t *uc) {
(unsigned long) uc->uc_mcontext->__ss.__gs (unsigned long) uc->uc_mcontext->__ss.__gs
); );
logStackContent((void**)uc->uc_mcontext->__ss.__esp); logStackContent((void**)uc->uc_mcontext->__ss.__esp);
#else
/* OSX ARM64 */
serverLog(LL_WARNING,
"\n"
"x0:%016lx x1:%016lx x2:%016lx x3:%016lx\n"
"x4:%016lx x5:%016lx x6:%016lx x7:%016lx\n"
"x8:%016lx x9:%016lx x10:%016lx x11:%016lx\n"
"x12:%016lx x13:%016lx x14:%016lx x15:%016lx\n"
"x16:%016lx x17:%016lx x18:%016lx x19:%016lx\n"
"x20:%016lx x21:%016lx x22:%016lx x23:%016lx\n"
"x24:%016lx x25:%016lx x26:%016lx x27:%016lx\n"
"x28:%016lx fp:%016lx lr:%016lx\n"
"sp:%016lx pc:%016lx cpsr:%08lx\n",
(unsigned long) uc->uc_mcontext->__ss.__x[0],
(unsigned long) uc->uc_mcontext->__ss.__x[1],
(unsigned long) uc->uc_mcontext->__ss.__x[2],
(unsigned long) uc->uc_mcontext->__ss.__x[3],
(unsigned long) uc->uc_mcontext->__ss.__x[4],
(unsigned long) uc->uc_mcontext->__ss.__x[5],
(unsigned long) uc->uc_mcontext->__ss.__x[6],
(unsigned long) uc->uc_mcontext->__ss.__x[7],
(unsigned long) uc->uc_mcontext->__ss.__x[8],
(unsigned long) uc->uc_mcontext->__ss.__x[9],
(unsigned long) uc->uc_mcontext->__ss.__x[10],
(unsigned long) uc->uc_mcontext->__ss.__x[11],
(unsigned long) uc->uc_mcontext->__ss.__x[12],
(unsigned long) uc->uc_mcontext->__ss.__x[13],
(unsigned long) uc->uc_mcontext->__ss.__x[14],
(unsigned long) uc->uc_mcontext->__ss.__x[15],
(unsigned long) uc->uc_mcontext->__ss.__x[16],
(unsigned long) uc->uc_mcontext->__ss.__x[17],
(unsigned long) uc->uc_mcontext->__ss.__x[18],
(unsigned long) uc->uc_mcontext->__ss.__x[19],
(unsigned long) uc->uc_mcontext->__ss.__x[20],
(unsigned long) uc->uc_mcontext->__ss.__x[21],
(unsigned long) uc->uc_mcontext->__ss.__x[22],
(unsigned long) uc->uc_mcontext->__ss.__x[23],
(unsigned long) uc->uc_mcontext->__ss.__x[24],
(unsigned long) uc->uc_mcontext->__ss.__x[25],
(unsigned long) uc->uc_mcontext->__ss.__x[26],
(unsigned long) uc->uc_mcontext->__ss.__x[27],
(unsigned long) uc->uc_mcontext->__ss.__x[28],
(unsigned long) arm_thread_state64_get_fp(uc->uc_mcontext->__ss),
(unsigned long) arm_thread_state64_get_lr(uc->uc_mcontext->__ss),
(unsigned long) arm_thread_state64_get_sp(uc->uc_mcontext->__ss),
(unsigned long) arm_thread_state64_get_pc(uc->uc_mcontext->__ss),
(unsigned long) uc->uc_mcontext->__ss.__cpsr
);
logStackContent((void**) arm_thread_state64_get_sp(uc->uc_mcontext->__ss));
#endif #endif
/* Linux */ /* Linux */
#elif defined(__linux__) #elif defined(__linux__)
/* Linux x86 */ /* Linux x86 */
#if defined(__i386__) || defined(__ILP32__) #if defined(__i386__) || ((defined(__X86_64__) || defined(__x86_64__)) && defined(__ILP32__))
serverLog(LL_WARNING, serverLog(LL_WARNING,
"\n" "\n"
"EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n" "EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n"
@ -1163,7 +1242,7 @@ void logRegisters(ucontext_t *uc) {
"R10:%016lx R9 :%016lx\nR8 :%016lx R7 :%016lx\n" "R10:%016lx R9 :%016lx\nR8 :%016lx R7 :%016lx\n"
"R6 :%016lx R5 :%016lx\nR4 :%016lx R3 :%016lx\n" "R6 :%016lx R5 :%016lx\nR4 :%016lx R3 :%016lx\n"
"R2 :%016lx R1 :%016lx\nR0 :%016lx EC :%016lx\n" "R2 :%016lx R1 :%016lx\nR0 :%016lx EC :%016lx\n"
"fp: %016lx ip:%016lx\n", "fp: %016lx ip:%016lx\n"
"pc:%016lx sp:%016lx\ncpsr:%016lx fault_address:%016lx\n", "pc:%016lx sp:%016lx\ncpsr:%016lx fault_address:%016lx\n",
(unsigned long) uc->uc_mcontext.arm_r10, (unsigned long) uc->uc_mcontext.arm_r10,
(unsigned long) uc->uc_mcontext.arm_r9, (unsigned long) uc->uc_mcontext.arm_r9,
@ -1296,6 +1375,59 @@ void logRegisters(ucontext_t *uc) {
); );
logStackContent((void**)uc->sc_esp); logStackContent((void**)uc->sc_esp);
#endif #endif
#elif defined(__NetBSD__)
#if defined(__x86_64__)
serverLog(LL_WARNING,
"\n"
"RAX:%016lx RBX:%016lx\nRCX:%016lx RDX:%016lx\n"
"RDI:%016lx RSI:%016lx\nRBP:%016lx RSP:%016lx\n"
"R8 :%016lx R9 :%016lx\nR10:%016lx R11:%016lx\n"
"R12:%016lx R13:%016lx\nR14:%016lx R15:%016lx\n"
"RIP:%016lx EFL:%016lx\nCSGSFS:%016lx",
(unsigned long) uc->uc_mcontext.__gregs[_REG_RAX],
(unsigned long) uc->uc_mcontext.__gregs[_REG_RBX],
(unsigned long) uc->uc_mcontext.__gregs[_REG_RCX],
(unsigned long) uc->uc_mcontext.__gregs[_REG_RDX],
(unsigned long) uc->uc_mcontext.__gregs[_REG_RDI],
(unsigned long) uc->uc_mcontext.__gregs[_REG_RSI],
(unsigned long) uc->uc_mcontext.__gregs[_REG_RBP],
(unsigned long) uc->uc_mcontext.__gregs[_REG_RSP],
(unsigned long) uc->uc_mcontext.__gregs[_REG_R8],
(unsigned long) uc->uc_mcontext.__gregs[_REG_R9],
(unsigned long) uc->uc_mcontext.__gregs[_REG_R10],
(unsigned long) uc->uc_mcontext.__gregs[_REG_R11],
(unsigned long) uc->uc_mcontext.__gregs[_REG_R12],
(unsigned long) uc->uc_mcontext.__gregs[_REG_R13],
(unsigned long) uc->uc_mcontext.__gregs[_REG_R14],
(unsigned long) uc->uc_mcontext.__gregs[_REG_R15],
(unsigned long) uc->uc_mcontext.__gregs[_REG_RIP],
(unsigned long) uc->uc_mcontext.__gregs[_REG_RFLAGS],
(unsigned long) uc->uc_mcontext.__gregs[_REG_CS]
);
logStackContent((void**)uc->uc_mcontext.__gregs[_REG_RSP]);
#elif defined(__i386__)
serverLog(LL_WARNING,
"\n"
"EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\n"
"EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\n"
"SS :%08lx EFL:%08lx EIP:%08lx CS:%08lx\n"
"DS :%08lx ES :%08lx FS :%08lx GS:%08lx",
(unsigned long) uc->uc_mcontext.__gregs[_REG_EAX],
(unsigned long) uc->uc_mcontext.__gregs[_REG_EBX],
(unsigned long) uc->uc_mcontext.__gregs[_REG_EDX],
(unsigned long) uc->uc_mcontext.__gregs[_REG_EDI],
(unsigned long) uc->uc_mcontext.__gregs[_REG_ESI],
(unsigned long) uc->uc_mcontext.__gregs[_REG_EBP],
(unsigned long) uc->uc_mcontext.__gregs[_REG_ESP],
(unsigned long) uc->uc_mcontext.__gregs[_REG_SS],
(unsigned long) uc->uc_mcontext.__gregs[_REG_EFLAGS],
(unsigned long) uc->uc_mcontext.__gregs[_REG_EIP],
(unsigned long) uc->uc_mcontext.__gregs[_REG_CS],
(unsigned long) uc->uc_mcontext.__gregs[_REG_ES],
(unsigned long) uc->uc_mcontext.__gregs[_REG_FS],
(unsigned long) uc->uc_mcontext.__gregs[_REG_GS]
);
#endif
#elif defined(__DragonFly__) #elif defined(__DragonFly__)
serverLog(LL_WARNING, serverLog(LL_WARNING,
"\n" "\n"
@ -1457,12 +1589,12 @@ void logCurrentClient(void) {
} }
/* Check if the first argument, usually a key, is found inside the /* Check if the first argument, usually a key, is found inside the
* selected DB, and if so print info about the associated object. */ * selected DB, and if so print info about the associated object. */
if (cc->argc >= 1) { if (cc->argc > 1) {
robj *val, *key; robj *val, *key;
dictEntry *de; dictEntry *de;
key = getDecodedObject(cc->argv[1]); key = getDecodedObject(cc->argv[1]);
de = dictFind(cc->db->pdict, ptrFromObj(key)); de = dictFind(cc->db->dict, ptrFromObj(key));
if (de) { if (de) {
val = (robj*)dictGetVal(de); val = (robj*)dictGetVal(de);
serverLog(LL_WARNING,"key '%s' found in DB containing the following object:", (char*)ptrFromObj(key)); serverLog(LL_WARNING,"key '%s' found in DB containing the following object:", (char*)ptrFromObj(key));
@ -1476,7 +1608,7 @@ void logCurrentClient(void) {
#define MEMTEST_MAX_REGIONS 128 #define MEMTEST_MAX_REGIONS 128
/* A non destructive memory test executed during segfauls. */ /* A non destructive memory test executed during segfault. */
int memtest_test_linux_anonymous_maps(void) { int memtest_test_linux_anonymous_maps(void) {
FILE *fp; FILE *fp;
char line[1024]; char line[1024];
@ -1537,7 +1669,32 @@ int memtest_test_linux_anonymous_maps(void) {
closeDirectLogFiledes(fd); closeDirectLogFiledes(fd);
return errors; return errors;
} }
#endif #endif /* HAVE_PROC_MAPS */
static void killServerThreads(void) {
int err;
for (int i = 0; i < cserver.cthreads; i++) {
if (g_pserver->rgthread[i] != pthread_self()) {
pthread_cancel(g_pserver->rgthread[i]);
}
}
if (pthread_self() != cserver.main_thread_id && pthread_cancel(cserver.main_thread_id) == 0) {
if ((err = pthread_join(cserver.main_thread_id,NULL)) != 0) {
serverLog(LL_WARNING, "main thread can not be joined: %s", strerror(err));
} else {
serverLog(LL_WARNING, "main thread terminated");
}
}
}
/* Kill the running threads (other than current) in an unclean way. This function
* should be used only when it's critical to stop the threads for some reason.
* Currently Redis does this only on crash (for instance on SIGSEGV) in order
* to perform a fast memory check without other threads messing with memory. */
void killThreads(void) {
killServerThreads();
bioKillThreads();
}
/* Scans the (assumed) x86 code starting at addr, for a max of `len` /* Scans the (assumed) x86 code starting at addr, for a max of `len`
* bytes, searching for E8 (callq) opcodes, and dumping the symbols * bytes, searching for E8 (callq) opcodes, and dumping the symbols
@ -1575,7 +1732,7 @@ void sigsegvHandler(int sig, siginfo_t *info, void *secret) {
bugReportStart(); bugReportStart();
serverLog(LL_WARNING, serverLog(LL_WARNING,
"KeyDB %s crashed by signal: %d", KEYDB_REAL_VERSION, sig); "KeyDB %s crashed by signal: %d, si_code: %d", KEYDB_REAL_VERSION, sig, info->si_code);
if (eip != NULL) { if (eip != NULL) {
serverLog(LL_WARNING, serverLog(LL_WARNING,
"Crashed running the instruction at: %p", eip); "Crashed running the instruction at: %p", eip);
@ -1584,6 +1741,9 @@ void sigsegvHandler(int sig, siginfo_t *info, void *secret) {
serverLog(LL_WARNING, serverLog(LL_WARNING,
"Accessing address: %p", (void*)info->si_addr); "Accessing address: %p", (void*)info->si_addr);
} }
if (info->si_pid != -1) {
serverLog(LL_WARNING, "Killed by PID: %d, UID: %d", info->si_pid, info->si_uid);
}
serverLog(LL_WARNING, serverLog(LL_WARNING,
"Failed assertion: %s (%s:%d)", g_pserver->assert_failed, "Failed assertion: %s (%s:%d)", g_pserver->assert_failed,
g_pserver->assert_file, g_pserver->assert_line); g_pserver->assert_file, g_pserver->assert_line);
@ -1617,7 +1777,7 @@ void sigsegvHandler(int sig, siginfo_t *info, void *secret) {
#if defined(HAVE_PROC_MAPS) #if defined(HAVE_PROC_MAPS)
/* Test memory */ /* Test memory */
serverLogRaw(LL_WARNING|LL_RAW, "\n------ FAST MEMORY TEST ------\n"); serverLogRaw(LL_WARNING|LL_RAW, "\n------ FAST MEMORY TEST ------\n");
bioKillThreads(); killThreads();
if (memtest_test_linux_anonymous_maps()) { if (memtest_test_linux_anonymous_maps()) {
serverLogRaw(LL_WARNING|LL_RAW, serverLogRaw(LL_WARNING|LL_RAW,
"!!! MEMORY ERROR DETECTED! Check your memory ASAP !!!\n"); "!!! MEMORY ERROR DETECTED! Check your memory ASAP !!!\n");
@ -1645,13 +1805,14 @@ void sigsegvHandler(int sig, siginfo_t *info, void *secret) {
/* Find the address of the next page, which is our "safety" /* Find the address of the next page, which is our "safety"
* limit when dumping. Then try to dump just 128 bytes more * limit when dumping. Then try to dump just 128 bytes more
* than EIP if there is room, or stop sooner. */ * than EIP if there is room, or stop sooner. */
void *base = (void *)info.dli_saddr;
unsigned long next = ((unsigned long)eip + sz) & ~(sz-1); unsigned long next = ((unsigned long)eip + sz) & ~(sz-1);
unsigned long end = (unsigned long)eip + 128; unsigned long end = (unsigned long)eip + 128;
if (end > next) end = next; if (end > next) end = next;
len = end - (unsigned long)info.dli_saddr; len = end - (unsigned long)base;
serverLogHexDump(LL_WARNING, "dump of function", serverLogHexDump(LL_WARNING, "dump of function",
info.dli_saddr ,len); base ,len);
dumpX86Calls(info.dli_saddr,len); dumpX86Calls(base,len);
} }
} }
} }
@ -1664,7 +1825,7 @@ void sigsegvHandler(int sig, siginfo_t *info, void *secret) {
); );
/* free(messages); Don't call free() with possibly corrupted memory. */ /* free(messages); Don't call free() with possibly corrupted memory. */
if (cserver.daemonize && cserver.supervised == 0) unlink(cserver.pidfile); if (cserver.daemonize && cserver.supervised == 0 && cserver.pidfile) unlink(cserver.pidfile);
/* Make sure we exit with the right signal at the end. So for instance /* Make sure we exit with the right signal at the end. So for instance
* the core will be dumped if enabled. */ * the core will be dumped if enabled. */

View File

@ -47,12 +47,12 @@ extern "C" int je_get_defrag_hint(void* ptr);
/* forward declarations*/ /* forward declarations*/
void defragDictBucketCallback(void *privdata, dictEntry **bucketref); void defragDictBucketCallback(void *privdata, dictEntry **bucketref);
dictEntry* replaceSateliteDictKeyPtrAndOrDefragDictEntry(dict *d, sds oldkey, sds newkey, uint64_t hash, long *defragged); dictEntry* replaceSatelliteDictKeyPtrAndOrDefragDictEntry(dict *d, sds oldkey, sds newkey, uint64_t hash, long *defragged);
bool replaceSateliteOSetKeyPtr(expireset &set, sds oldkey, sds newkey); bool replaceSatelliteOSetKeyPtr(expireset &set, sds oldkey, sds newkey);
/* Defrag helper for generic allocations. /* Defrag helper for generic allocations.
* *
* returns NULL in case the allocatoin wasn't moved. * returns NULL in case the allocation wasn't moved.
* when it returns a non-null value, the old pointer was already released * when it returns a non-null value, the old pointer was already released
* and should NOT be accessed. */ * and should NOT be accessed. */
template<typename TPTR> template<typename TPTR>
@ -83,7 +83,7 @@ robj* activeDefragAlloc(robj *o) {
/*Defrag helper for sds strings /*Defrag helper for sds strings
* *
* returns NULL in case the allocatoin wasn't moved. * returns NULL in case the allocation wasn't moved.
* when it returns a non-null value, the old pointer was already released * when it returns a non-null value, the old pointer was already released
* and should NOT be accessed. */ * and should NOT be accessed. */
sds activeDefragSds(sds sdsptr) { sds activeDefragSds(sds sdsptr) {
@ -99,7 +99,7 @@ sds activeDefragSds(sds sdsptr) {
/* Defrag helper for robj and/or string objects /* Defrag helper for robj and/or string objects
* *
* returns NULL in case the allocatoin wasn't moved. * returns NULL in case the allocation wasn't moved.
* when it returns a non-null value, the old pointer was already released * when it returns a non-null value, the old pointer was already released
* and should NOT be accessed. */ * and should NOT be accessed. */
robj *activeDefragStringOb(robj* ob, long *defragged) { robj *activeDefragStringOb(robj* ob, long *defragged) {
@ -137,11 +137,11 @@ robj *activeDefragStringOb(robj* ob, long *defragged) {
} }
/* Defrag helper for dictEntries to be used during dict iteration (called on /* Defrag helper for dictEntries to be used during dict iteration (called on
* each step). Teturns a stat of how many pointers were moved. */ * each step). Returns a stat of how many pointers were moved. */
long dictIterDefragEntry(dictIterator *iter) { long dictIterDefragEntry(dictIterator *iter) {
/* This function is a little bit dirty since it messes with the internals /* This function is a little bit dirty since it messes with the internals
* of the dict and it's iterator, but the benefit is that it is very easy * of the dict and it's iterator, but the benefit is that it is very easy
* to use, and require no other chagnes in the dict. */ * to use, and require no other changes in the dict. */
long defragged = 0; long defragged = 0;
dictht *ht; dictht *ht;
/* Handle the next entry (if there is one), and update the pointer in the /* Handle the next entry (if there is one), and update the pointer in the
@ -245,7 +245,7 @@ double *zslDefrag(zskiplist *zsl, double score, sds oldele, sds newele) {
return NULL; return NULL;
} }
/* Defrag helpler for sorted set. /* Defrag helper for sorted set.
* Defrag a single dict entry key name, and corresponding skiplist struct */ * Defrag a single dict entry key name, and corresponding skiplist struct */
long activeDefragZsetEntry(zset *zs, dictEntry *de) { long activeDefragZsetEntry(zset *zs, dictEntry *de) {
sds newsds; sds newsds;
@ -256,7 +256,7 @@ long activeDefragZsetEntry(zset *zs, dictEntry *de) {
defragged++, de->key = newsds; defragged++, de->key = newsds;
newscore = zslDefrag(zs->zsl, *(double*)dictGetVal(de), sdsele, newsds); newscore = zslDefrag(zs->zsl, *(double*)dictGetVal(de), sdsele, newsds);
if (newscore) { if (newscore) {
dictSetVal(zs->pdict, de, newscore); dictSetVal(zs->dict, de, newscore);
defragged++; defragged++;
} }
return defragged; return defragged;
@ -355,8 +355,8 @@ long activeDefragSdsListAndDict(list *l, dict *d, int dict_val_type) {
sdsele = (sds)ln->value; sdsele = (sds)ln->value;
if ((newsds = activeDefragSds(sdsele))) { if ((newsds = activeDefragSds(sdsele))) {
/* When defragging an sds value, we need to update the dict key */ /* When defragging an sds value, we need to update the dict key */
uint64_t hash = dictGetHash(d, sdsele); uint64_t hash = dictGetHash(d, newsds);
replaceSateliteDictKeyPtrAndOrDefragDictEntry(d, sdsele, newsds, hash, &defragged); replaceSatelliteDictKeyPtrAndOrDefragDictEntry(d, sdsele, newsds, hash, &defragged);
ln->value = newsds; ln->value = newsds;
defragged++; defragged++;
} }
@ -392,7 +392,7 @@ long activeDefragSdsListAndDict(list *l, dict *d, int dict_val_type) {
* moved. Return value is the the dictEntry if found, or NULL if not found. * moved. Return value is the the dictEntry if found, or NULL if not found.
* NOTE: this is very ugly code, but it let's us avoid the complication of * NOTE: this is very ugly code, but it let's us avoid the complication of
* doing a scan on another dict. */ * doing a scan on another dict. */
dictEntry* replaceSateliteDictKeyPtrAndOrDefragDictEntry(dict *d, sds oldkey, sds newkey, uint64_t hash, long *defragged) { dictEntry* replaceSatelliteDictKeyPtrAndOrDefragDictEntry(dict *d, sds oldkey, sds newkey, uint64_t hash, long *defragged) {
dictEntry **deref = dictFindEntryRefByPtrAndHash(d, oldkey, hash); dictEntry **deref = dictFindEntryRefByPtrAndHash(d, oldkey, hash);
if (deref) { if (deref) {
dictEntry *de = *deref; dictEntry *de = *deref;
@ -408,7 +408,7 @@ dictEntry* replaceSateliteDictKeyPtrAndOrDefragDictEntry(dict *d, sds oldkey, sd
return NULL; return NULL;
} }
bool replaceSateliteOSetKeyPtr(expireset &set, sds oldkey, sds newkey) { bool replaceSatelliteOSetKeyPtr(expireset &set, sds oldkey, sds newkey) {
auto itr = set.find(oldkey); auto itr = set.find(oldkey);
if (itr != set.end()) if (itr != set.end())
{ {
@ -454,7 +454,7 @@ long activeDefragQuickListNodes(quicklist *ql) {
} }
/* when the value has lots of elements, we want to handle it later and not as /* when the value has lots of elements, we want to handle it later and not as
* oart of the main dictionary scan. this is needed in order to prevent latency * part of the main dictionary scan. this is needed in order to prevent latency
* spikes when handling large items */ * spikes when handling large items */
void defragLater(redisDb *db, dictEntry *kde) { void defragLater(redisDb *db, dictEntry *kde) {
sds key = sdsdup((sds)dictGetKey(kde)); sds key = sdsdup((sds)dictGetKey(kde));
@ -521,7 +521,7 @@ long scanLaterZset(robj *ob, unsigned long *cursor) {
if (ob->type != OBJ_ZSET || ob->encoding != OBJ_ENCODING_SKIPLIST) if (ob->type != OBJ_ZSET || ob->encoding != OBJ_ENCODING_SKIPLIST)
return 0; return 0;
zset *zs = (zset*)ptrFromObj(ob); zset *zs = (zset*)ptrFromObj(ob);
dict *d = zs->pdict; dict *d = zs->dict;
scanLaterZsetData data = {zs, 0}; scanLaterZsetData data = {zs, 0};
*cursor = dictScan(d, *cursor, scanLaterZsetCallback, defragDictBucketCallback, &data); *cursor = dictScan(d, *cursor, scanLaterZsetCallback, defragDictBucketCallback, &data);
return data.defragged; return data.defragged;
@ -596,20 +596,20 @@ long defragZsetSkiplist(redisDb *db, dictEntry *kde) {
defragged++, zs->zsl = newzsl; defragged++, zs->zsl = newzsl;
if ((newheader = (zskiplistNode*)activeDefragAlloc(zs->zsl->header))) if ((newheader = (zskiplistNode*)activeDefragAlloc(zs->zsl->header)))
defragged++, zs->zsl->header = newheader; defragged++, zs->zsl->header = newheader;
if (dictSize(zs->pdict) > cserver.active_defrag_max_scan_fields) if (dictSize(zs->dict) > cserver.active_defrag_max_scan_fields)
defragLater(db, kde); defragLater(db, kde);
else { else {
dictIterator *di = dictGetIterator(zs->pdict); dictIterator *di = dictGetIterator(zs->dict);
while((de = dictNext(di)) != NULL) { while((de = dictNext(di)) != NULL) {
defragged += activeDefragZsetEntry(zs, de); defragged += activeDefragZsetEntry(zs, de);
} }
dictReleaseIterator(di); dictReleaseIterator(di);
} }
/* handle the dict struct */ /* handle the dict struct */
if ((newdict = (dict*)activeDefragAlloc(zs->pdict))) if ((newdict = (dict*)activeDefragAlloc(zs->dict)))
defragged++, zs->pdict = newdict; defragged++, zs->dict = newdict;
/* defrag the dict tables */ /* defrag the dict tables */
defragged += dictDefragTables(zs->pdict); defragged += dictDefragTables(zs->dict);
return defragged; return defragged;
} }
@ -683,6 +683,7 @@ int scanLaterStraemListpacks(robj *ob, unsigned long *cursor, long long endtime,
/* if cursor is non-zero, we seek to the static 'last' */ /* if cursor is non-zero, we seek to the static 'last' */
if (!raxSeek(&ri,">", last, sizeof(last))) { if (!raxSeek(&ri,">", last, sizeof(last))) {
*cursor = 0; *cursor = 0;
raxStop(&ri);
return 0; return 0;
} }
/* assign the iterator node callback after the seek, so that the /* assign the iterator node callback after the seek, so that the
@ -833,7 +834,7 @@ long defragKey(redisDb *db, dictEntry *de) {
{ {
defragged++, de->key = newsds; defragged++, de->key = newsds;
if (!db->setexpire->empty()) { if (!db->setexpire->empty()) {
bool fReplaced = replaceSateliteOSetKeyPtr(*db->setexpire, keysds, newsds); bool fReplaced = replaceSatelliteOSetKeyPtr(*db->setexpire, keysds, newsds);
serverAssert(fReplaced == ob->FExpires()); serverAssert(fReplaced == ob->FExpires());
} else { } else {
serverAssert(!ob->FExpires()); serverAssert(!ob->FExpires());
@ -908,7 +909,7 @@ void defragScanCallback(void *privdata, const dictEntry *de) {
g_pserver->stat_active_defrag_scanned++; g_pserver->stat_active_defrag_scanned++;
} }
/* Defrag scan callback for each hash table bicket, /* Defrag scan callback for each hash table bucket,
* used in order to defrag the dictEntry allocations. */ * used in order to defrag the dictEntry allocations. */
void defragDictBucketCallback(void *privdata, dictEntry **bucketref) { void defragDictBucketCallback(void *privdata, dictEntry **bucketref) {
UNUSED(privdata); /* NOTE: this function is also used by both activeDefragCycle and scanLaterHash, etc. don't use privdata */ UNUSED(privdata); /* NOTE: this function is also used by both activeDefragCycle and scanLaterHash, etc. don't use privdata */
@ -942,7 +943,7 @@ float getAllocatorFragmentation(size_t *out_frag_bytes) {
return frag_pct; return frag_pct;
} }
/* We may need to defrag other globals, one small allcation can hold a full allocator run. /* We may need to defrag other globals, one small allocation can hold a full allocator run.
* so although small, it is still important to defrag these */ * so although small, it is still important to defrag these */
long defragOtherGlobals() { long defragOtherGlobals() {
long defragged = 0; long defragged = 0;
@ -1014,7 +1015,7 @@ int defragLaterStep(redisDb *db, long long endtime) {
} }
/* each time we enter this function we need to fetch the key from the dict again (if it still exists) */ /* each time we enter this function we need to fetch the key from the dict again (if it still exists) */
dictEntry *de = dictFind(db->pdict, defrag_later_current_key); dictEntry *de = dictFind(db->dict, defrag_later_current_key);
key_defragged = g_pserver->stat_active_defrag_hits; key_defragged = g_pserver->stat_active_defrag_hits;
do { do {
int quit = 0; int quit = 0;
@ -1113,7 +1114,7 @@ void activeDefragCycle(void) {
if (hasActiveChildProcess()) if (hasActiveChildProcess())
return; /* Defragging memory while there's a fork will just do damage. */ return; /* Defragging memory while there's a fork will just do damage. */
/* Once a second, check if we the fragmentation justfies starting a scan /* Once a second, check if the fragmentation justfies starting a scan
* or making it more aggressive. */ * or making it more aggressive. */
run_with_period(1000) { run_with_period(1000) {
computeDefragCycles(); computeDefragCycles();
@ -1177,13 +1178,13 @@ void activeDefragCycle(void) {
break; /* this will exit the function and we'll continue on the next cycle */ break; /* this will exit the function and we'll continue on the next cycle */
} }
cursor = dictScan(db->pdict, cursor, defragScanCallback, defragDictBucketCallback, db); cursor = dictScan(db->dict, cursor, defragScanCallback, defragDictBucketCallback, db);
/* Once in 16 scan iterations, 512 pointer reallocations. or 64 keys /* Once in 16 scan iterations, 512 pointer reallocations. or 64 keys
* (if we have a lot of pointers in one hash bucket or rehasing), * (if we have a lot of pointers in one hash bucket or rehasing),
* check if we reached the time limit. * check if we reached the time limit.
* But regardless, don't start a new db in this loop, this is because after * But regardless, don't start a new db in this loop, this is because after
* the last db we call defragOtherGlobals, which must be done in once cycle */ * the last db we call defragOtherGlobals, which must be done in one cycle */
if (!cursor || (++iterations > 16 || if (!cursor || (++iterations > 16 ||
g_pserver->stat_active_defrag_hits - prev_defragged > 512 || g_pserver->stat_active_defrag_hits - prev_defragged > 512 ||
g_pserver->stat_active_defrag_scanned - prev_scanned > 64)) { g_pserver->stat_active_defrag_scanned - prev_scanned > 64)) {

View File

@ -237,7 +237,9 @@ long long timeInMilliseconds(void) {
return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000); return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);
} }
/* Rehash for an amount of time between ms milliseconds and ms+1 milliseconds */ /* Rehash in ms+"delta" milliseconds. The value of "delta" is larger
* than 0, and is smaller than 1 in most cases. The exact upper bound
* depends on the running time of dictRehash(d,100).*/
int dictRehashMilliseconds(dict *d, int ms) { int dictRehashMilliseconds(dict *d, int ms) {
long long start = timeInMilliseconds(); long long start = timeInMilliseconds();
int rehashes = 0; int rehashes = 0;
@ -749,7 +751,7 @@ unsigned int dictGetSomeKeys(dict *d, dictEntry **des, unsigned int count) {
* this function instead what we do is to consider a "linear" range of the table * this function instead what we do is to consider a "linear" range of the table
* that may be constituted of N buckets with chains of different lengths * that may be constituted of N buckets with chains of different lengths
* appearing one after the other. Then we report a random element in the range. * appearing one after the other. Then we report a random element in the range.
* In this way we smooth away the problem of different chain lenghts. */ * In this way we smooth away the problem of different chain lengths. */
#define GETFAIR_NUM_ENTRIES 15 #define GETFAIR_NUM_ENTRIES 15
dictEntry *dictGetFairRandomKey(dict *d) { dictEntry *dictGetFairRandomKey(dict *d) {
dictEntry *entries[GETFAIR_NUM_ENTRIES]; dictEntry *entries[GETFAIR_NUM_ENTRIES];
@ -1119,7 +1121,7 @@ size_t _dictGetStatsHt(char *buf, size_t bufsize, dictht *ht, int tableid) {
i, clvector[i], ((float)clvector[i]/ht->size)*100); i, clvector[i], ((float)clvector[i]/ht->size)*100);
} }
/* Unlike snprintf(), teturn the number of characters actually written. */ /* Unlike snprintf(), return the number of characters actually written. */
if (bufsize) buf[bufsize-1] = '\0'; if (bufsize) buf[bufsize-1] = '\0';
return strlen(buf); return strlen(buf);
} }

View File

@ -8,7 +8,7 @@
* to be backward compatible are still in big endian) because most of the * to be backward compatible are still in big endian) because most of the
* production environments are little endian, and we have a lot of conversions * production environments are little endian, and we have a lot of conversions
* in a few places because ziplists, intsets, zipmaps, need to be endian-neutral * in a few places because ziplists, intsets, zipmaps, need to be endian-neutral
* even in memory, since they are serialied on RDB files directly with a single * even in memory, since they are serialized on RDB files directly with a single
* write(2) without other additional steps. * write(2) without other additional steps.
* *
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------

View File

@ -42,7 +42,7 @@
/* To improve the quality of the LRU approximation we take a set of keys /* To improve the quality of the LRU approximation we take a set of keys
* that are good candidate for eviction across freeMemoryIfNeeded() calls. * that are good candidate for eviction across freeMemoryIfNeeded() calls.
* *
* Entries inside the eviciton pool are taken ordered by idle time, putting * Entries inside the eviction pool are taken ordered by idle time, putting
* greater idle times to the right (ascending order). * greater idle times to the right (ascending order).
* *
* When an LFU policy is used instead, a reverse frequency indication is used * When an LFU policy is used instead, a reverse frequency indication is used
@ -88,7 +88,7 @@ unsigned int LRU_CLOCK(void) {
/* Given an object returns the min number of milliseconds the object was never /* Given an object returns the min number of milliseconds the object was never
* requested, using an approximated LRU algorithm. */ * requested, using an approximated LRU algorithm. */
unsigned long long estimateObjectIdleTime(robj *o) { unsigned long long estimateObjectIdleTime(robj_roptr o) {
unsigned long long lruclock = LRU_CLOCK(); unsigned long long lruclock = LRU_CLOCK();
if (lruclock >= o->lru) { if (lruclock >= o->lru) {
return (lruclock - o->lru) * LRU_CLOCK_RESOLUTION; return (lruclock - o->lru) * LRU_CLOCK_RESOLUTION;
@ -216,7 +216,7 @@ void processEvictionCandidate(int dbid, sds key, robj *o, const expireEntry *e,
/* Try to reuse the cached SDS string allocated in the pool entry, /* Try to reuse the cached SDS string allocated in the pool entry,
* because allocating and deallocating this object is costly * because allocating and deallocating this object is costly
* (according to the profiler, not my fantasy. Remember: * (according to the profiler, not my fantasy. Remember:
* premature optimizbla bla bla bla. */ * premature optimization bla bla bla bla. */
int klen = sdslen(key); int klen = sdslen(key);
if (klen > EVPOOL_CACHED_SDS_SIZE) { if (klen > EVPOOL_CACHED_SDS_SIZE) {
pool[k].key = sdsdup(key); pool[k].key = sdsdup(key);
@ -357,7 +357,7 @@ unsigned long LFUDecrAndReturn(robj *o) {
} }
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* The external API for eviction: freeMemroyIfNeeded() is called by the * The external API for eviction: freeMemoryIfNeeded() is called by the
* server when there is data to add in order to make space if needed. * server when there is data to add in order to make space if needed.
* --------------------------------------------------------------------------*/ * --------------------------------------------------------------------------*/
@ -458,7 +458,7 @@ int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *lev
* *
* The function returns C_OK if we are under the memory limit or if we * The function returns C_OK if we are under the memory limit or if we
* were over the limit, but the attempt to free memory was successful. * were over the limit, but the attempt to free memory was successful.
* Otehrwise if we are over the memory limit, but not enough memory * Otherwise if we are over the memory limit, but not enough memory
* was freed to return back under the limit, the function returns C_ERR. */ * was freed to return back under the limit, the function returns C_ERR. */
int freeMemoryIfNeeded(void) { int freeMemoryIfNeeded(void) {
serverAssert(GlobalLocksAcquired()); serverAssert(GlobalLocksAcquired());
@ -508,8 +508,8 @@ int freeMemoryIfNeeded(void) {
db = g_pserver->db+i; db = g_pserver->db+i;
if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS) if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS)
{ {
if ((keys = dictSize(db->pdict)) != 0) { if ((keys = dictSize(db->dict)) != 0) {
evictionPoolPopulate(i, db->pdict, nullptr, pool); evictionPoolPopulate(i, db->dict, nullptr, pool);
total_keys += keys; total_keys += keys;
} }
} }
@ -517,7 +517,7 @@ int freeMemoryIfNeeded(void) {
{ {
keys = db->setexpire->size(); keys = db->setexpire->size();
if (keys != 0) if (keys != 0)
evictionPoolPopulate(i, db->pdict, db->setexpire, pool); evictionPoolPopulate(i, db->dict, db->setexpire, pool);
total_keys += keys; total_keys += keys;
} }
} }
@ -529,7 +529,7 @@ int freeMemoryIfNeeded(void) {
bestdbid = pool[k].dbid; bestdbid = pool[k].dbid;
sds key = nullptr; sds key = nullptr;
dictEntry *de = dictFind(g_pserver->db[pool[k].dbid].pdict,pool[k].key); dictEntry *de = dictFind(g_pserver->db[pool[k].dbid].dict,pool[k].key);
if (de != nullptr && (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS || ((robj*)dictGetVal(de))->FExpires())) if (de != nullptr && (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS || ((robj*)dictGetVal(de))->FExpires()))
key = (sds)dictGetKey(de); key = (sds)dictGetKey(de);
@ -563,8 +563,8 @@ int freeMemoryIfNeeded(void) {
db = g_pserver->db+j; db = g_pserver->db+j;
if (g_pserver->maxmemory_policy == MAXMEMORY_ALLKEYS_RANDOM) if (g_pserver->maxmemory_policy == MAXMEMORY_ALLKEYS_RANDOM)
{ {
if (dictSize(db->pdict) != 0) { if (dictSize(db->dict) != 0) {
dictEntry *de = dictGetRandomKey(db->pdict); dictEntry *de = dictGetRandomKey(db->dict);
bestkey = (sds)dictGetKey(de); bestkey = (sds)dictGetKey(de);
bestdbid = j; bestdbid = j;
break; break;
@ -593,6 +593,8 @@ int freeMemoryIfNeeded(void) {
* we are freeing removing the key, but we can't account for * we are freeing removing the key, but we can't account for
* that otherwise we would never exit the loop. * that otherwise we would never exit the loop.
* *
* Same for CSC invalidation messages generated by signalModifiedKey.
*
* AOF and Output buffer memory will be freed eventually so * AOF and Output buffer memory will be freed eventually so
* we only care about memory used by the key space. */ * we only care about memory used by the key space. */
delta = (long long) zmalloc_used_memory(); delta = (long long) zmalloc_used_memory();
@ -601,12 +603,12 @@ int freeMemoryIfNeeded(void) {
dbAsyncDelete(db,keyobj); dbAsyncDelete(db,keyobj);
else else
dbSyncDelete(db,keyobj); dbSyncDelete(db,keyobj);
signalModifiedKey(NULL,db,keyobj);
latencyEndMonitor(eviction_latency); latencyEndMonitor(eviction_latency);
latencyAddSampleIfNeeded("eviction-del",eviction_latency); latencyAddSampleIfNeeded("eviction-del",eviction_latency);
delta -= (long long) zmalloc_used_memory(); delta -= (long long) zmalloc_used_memory();
mem_freed += delta; mem_freed += delta;
g_pserver->stat_evictedkeys++; g_pserver->stat_evictedkeys++;
signalModifiedKey(NULL,db,keyobj);
notifyKeyspaceEvent(NOTIFY_EVICTED, "evicted", notifyKeyspaceEvent(NOTIFY_EVICTED, "evicted",
keyobj, db->id); keyobj, db->id);
decrRefCount(keyobj); decrRefCount(keyobj);

View File

@ -54,7 +54,7 @@ void activeExpireCycleExpireFullKey(redisDb *db, const char *key) {
dbSyncDelete(db,keyobj); dbSyncDelete(db,keyobj);
notifyKeyspaceEvent(NOTIFY_EXPIRED, notifyKeyspaceEvent(NOTIFY_EXPIRED,
"expired",keyobj,db->id); "expired",keyobj,db->id);
trackingInvalidateKey(NULL, keyobj); signalModifiedKey(NULL, db, keyobj);
decrRefCount(keyobj); decrRefCount(keyobj);
g_pserver->stat_expiredkeys++; g_pserver->stat_expiredkeys++;
} }
@ -76,7 +76,7 @@ void activeExpireCycleExpire(redisDb *db, expireEntry &e, long long now) {
} }
expireEntryFat *pfat = e.pfatentry(); expireEntryFat *pfat = e.pfatentry();
dictEntry *de = dictFind(db->pdict, e.key()); dictEntry *de = dictFind(db->dict, e.key());
robj *val = (robj*)dictGetVal(de); robj *val = (robj*)dictGetVal(de);
int deleted = 0; int deleted = 0;
@ -297,14 +297,14 @@ void pexpireMemberAtCommand(client *c)
* Expire cycle type: * Expire cycle type:
* *
* If type is ACTIVE_EXPIRE_CYCLE_FAST the function will try to run a * If type is ACTIVE_EXPIRE_CYCLE_FAST the function will try to run a
* "fast" expire cycle that takes no longer than EXPIRE_FAST_CYCLE_DURATION * "fast" expire cycle that takes no longer than ACTIVE_EXPIRE_CYCLE_FAST_DURATION
* microseconds, and is not repeated again before the same amount of time. * microseconds, and is not repeated again before the same amount of time.
* *
* If type is ACTIVE_EXPIRE_CYCLE_SLOW, that normal expire cycle is * If type is ACTIVE_EXPIRE_CYCLE_SLOW, that normal expire cycle is
* executed, where the time limit is a percentage of the REDIS_HZ period * executed, where the time limit is a percentage of the REDIS_HZ period
* as specified by the ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC define. */ * as specified by the ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC define. */
void activeExpireCycle(int type) { void activeExpireCycleCore(int type) {
/* This function has some global state in order to continue the work /* This function has some global state in order to continue the work
* incrementally across calls. */ * incrementally across calls. */
static unsigned int current_db = 0; /* Last DB tested. */ static unsigned int current_db = 0; /* Last DB tested. */
@ -420,6 +420,11 @@ void activeExpireCycle(int type) {
(g_pserver->stat_expired_stale_perc*0.95); (g_pserver->stat_expired_stale_perc*0.95);
} }
void activeExpireCycle(int type)
{
runAndPropogateToReplicas(activeExpireCycleCore, type);
}
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
* Expires of keys created in writable slaves * Expires of keys created in writable slaves
* *
@ -479,7 +484,7 @@ void expireSlaveKeys(void) {
redisDb *db = g_pserver->db+dbid; redisDb *db = g_pserver->db+dbid;
// the expire is hashed based on the key pointer, so we need the point in the main db // the expire is hashed based on the key pointer, so we need the point in the main db
dictEntry *deMain = dictFind(db->pdict, keyname); dictEntry *deMain = dictFind(db->dict, keyname);
auto itr = db->setexpire->end(); auto itr = db->setexpire->end();
if (deMain != nullptr) if (deMain != nullptr)
itr = db->setexpire->find((sds)dictGetKey(deMain)); itr = db->setexpire->find((sds)dictGetKey(deMain));
@ -514,7 +519,7 @@ void expireSlaveKeys(void) {
else else
dictDelete(slaveKeysWithExpire,keyname); dictDelete(slaveKeysWithExpire,keyname);
/* Stop conditions: found 3 keys we cna't expire in a row or /* Stop conditions: found 3 keys we can't expire in a row or
* time limit was reached. */ * time limit was reached. */
cycles++; cycles++;
if (noexpire > 3) break; if (noexpire > 3) break;
@ -566,7 +571,7 @@ size_t getSlaveKeyWithExpireCount(void) {
* *
* Note: technically we should handle the case of a single DB being flushed * Note: technically we should handle the case of a single DB being flushed
* but it is not worth it since anyway race conditions using the same set * but it is not worth it since anyway race conditions using the same set
* of key names in a wriatable replica and in its master will lead to * of key names in a writable replica and in its master will lead to
* inconsistencies. This is just a best-effort thing we do. */ * inconsistencies. This is just a best-effort thing we do. */
void flushSlaveKeysWithExpireList(void) { void flushSlaveKeysWithExpireList(void) {
if (slaveKeysWithExpire) { if (slaveKeysWithExpire) {
@ -575,12 +580,22 @@ void flushSlaveKeysWithExpireList(void) {
} }
} }
int checkAlreadyExpired(long long when) {
/* EXPIRE with negative TTL, or EXPIREAT with a timestamp into the past
* should never be executed as a DEL when load the AOF or in the context
* of a slave instance.
*
* Instead we add the already expired key to the database with expire time
* (possibly in the past) and wait for an explicit DEL from the master. */
return (when <= mstime() && !g_pserver->loading && (!listLength(g_pserver->masters) || g_pserver->fActiveReplica));
}
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
* Expires Commands * Expires Commands
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
/* This is the generic command implementation for EXPIRE, PEXPIRE, EXPIREAT /* This is the generic command implementation for EXPIRE, PEXPIRE, EXPIREAT
* and PEXPIREAT. Because the commad second argument may be relative or absolute * and PEXPIREAT. Because the command second argument may be relative or absolute
* the "basetime" argument is used to signal what the base time is (either 0 * the "basetime" argument is used to signal what the base time is (either 0
* for *AT variants of the command, or the current time for relative expires). * for *AT variants of the command, or the current time for relative expires).
* *
@ -602,13 +617,7 @@ void expireGenericCommand(client *c, long long basetime, int unit) {
return; return;
} }
/* EXPIRE with negative TTL, or EXPIREAT with a timestamp into the past if (checkAlreadyExpired(when)) {
* should never be executed as a DEL when load the AOF or in the context
* of a replica instance.
*
* Instead we take the other branch of the IF statement setting an expire
* (possibly in the past) and wait for an explicit DEL from the master. */
if (when <= mstime() && !g_pserver->loading && (!listLength(g_pserver->masters) || g_pserver->fActiveReplica)) {
robj *aux; robj *aux;
int deleted = g_pserver->lazyfree_lazy_expire ? dbAsyncDelete(c->db,key) : int deleted = g_pserver->lazyfree_lazy_expire ? dbAsyncDelete(c->db,key) :
@ -715,6 +724,7 @@ void persistCommand(client *c) {
if (lookupKeyWrite(c->db,c->argv[1])) { if (lookupKeyWrite(c->db,c->argv[1])) {
if (c->argc == 2) { if (c->argc == 2) {
if (removeExpire(c->db,c->argv[1])) { if (removeExpire(c->db,c->argv[1])) {
signalModifiedKey(c,c->db,c->argv[1]);
notifyKeyspaceEvent(NOTIFY_GENERIC,"persist",c->argv[1],c->db->id); notifyKeyspaceEvent(NOTIFY_GENERIC,"persist",c->argv[1],c->db->id);
addReply(c,shared.cone); addReply(c,shared.cone);
g_pserver->dirty++; g_pserver->dirty++;
@ -723,6 +733,7 @@ void persistCommand(client *c) {
} }
} else if (c->argc == 3) { } else if (c->argc == 3) {
if (removeSubkeyExpire(c->db, c->argv[1], c->argv[2])) { if (removeSubkeyExpire(c->db, c->argv[1], c->argv[2])) {
signalModifiedKey(c,c->db,c->argv[1]);
notifyKeyspaceEvent(NOTIFY_GENERIC,"persist",c->argv[1],c->db->id); notifyKeyspaceEvent(NOTIFY_GENERIC,"persist",c->argv[1],c->db->id);
addReply(c,shared.cone); addReply(c,shared.cone);
g_pserver->dirty++; g_pserver->dirty++;

View File

@ -85,4 +85,6 @@ struct fastlock
#endif #endif
}; };
#ifdef __cplusplus
static_assert(offsetof(struct fastlock, m_ticket) == 64, "ensure padding is correct"); static_assert(offsetof(struct fastlock, m_ticket) == 64, "ensure padding is correct");
#endif

View File

@ -143,8 +143,8 @@ double extractUnitOrReply(client *c, robj *unit) {
} }
/* Input Argument Helper. /* Input Argument Helper.
* Extract the dinstance from the specified two arguments starting at 'argv' * Extract the distance from the specified two arguments starting at 'argv'
* that shouldbe in the form: <number> <unit> and return the dinstance in the * that should be in the form: <number> <unit>, and return the distance in the
* specified unit on success. *conversions is populated with the coefficient * specified unit on success. *conversions is populated with the coefficient
* to use in order to convert meters to the unit. * to use in order to convert meters to the unit.
* *
@ -651,7 +651,7 @@ void georadiusGeneric(client *c, int flags) {
if (maxelelen < elelen) maxelelen = elelen; if (maxelelen < elelen) maxelelen = elelen;
znode = zslInsert(zs->zsl,score,gp->member); znode = zslInsert(zs->zsl,score,gp->member);
serverAssert(dictAdd(zs->pdict,gp->member,&znode->score) == DICT_OK); serverAssert(dictAdd(zs->dict,gp->member,&znode->score) == DICT_OK);
gp->member = NULL; gp->member = NULL;
} }
@ -788,7 +788,7 @@ void geoposCommand(client *c) {
/* GEODIST key ele1 ele2 [unit] /* GEODIST key ele1 ele2 [unit]
* *
* Return the distance, in meters by default, otherwise accordig to "unit", * Return the distance, in meters by default, otherwise according to "unit",
* between points ele1 and ele2. If one or more elements are missing NULL * between points ele1 and ele2. If one or more elements are missing NULL
* is returned. */ * is returned. */
void geodistCommand(client *c) { void geodistCommand(client *c) {

View File

@ -65,7 +65,7 @@ uint8_t geohashEstimateStepsByRadius(double range_meters, double lat) {
} }
step -= 2; /* Make sure range is included in most of the base cases. */ step -= 2; /* Make sure range is included in most of the base cases. */
/* Wider range torwards the poles... Note: it is possible to do better /* Wider range towards the poles... Note: it is possible to do better
* than this approximation by computing the distance between meridians * than this approximation by computing the distance between meridians
* at this latitude, but this does the trick for now. */ * at this latitude, but this does the trick for now. */
if (lat > 66 || lat < -66) { if (lat > 66 || lat < -66) {
@ -81,7 +81,7 @@ uint8_t geohashEstimateStepsByRadius(double range_meters, double lat) {
/* Return the bounding box of the search area centered at latitude,longitude /* Return the bounding box of the search area centered at latitude,longitude
* having a radius of radius_meter. bounds[0] - bounds[2] is the minimum * having a radius of radius_meter. bounds[0] - bounds[2] is the minimum
* and maxium longitude, while bounds[1] - bounds[3] is the minimum and * and maximum longitude, while bounds[1] - bounds[3] is the minimum and
* maximum latitude. * maximum latitude.
* *
* This function does not behave correctly with very large radius values, for * This function does not behave correctly with very large radius values, for

View File

@ -1,4 +1,4 @@
/* Automatically generated by generate-command-help.rb, do not edit. */ /* Automatically generated by ./utils/generate-command-help.rb, do not edit. */
#ifndef __REDIS_HELP_H #ifndef __REDIS_HELP_H
#define __REDIS_HELP_H #define __REDIS_HELP_H
@ -44,6 +44,16 @@ struct commandHelp {
"Generate a pseudorandom secure password to use for ACL users", "Generate a pseudorandom secure password to use for ACL users",
9, 9,
"6.0.0" }, "6.0.0" },
{ "ACL GETUSER",
"username",
"Get the rules for a specific ACL user",
9,
"6.0.0" },
{ "ACL HELP",
"-",
"Show helpful text about the different subcommands",
9,
"6.0.0" },
{ "ACL LIST", { "ACL LIST",
"-", "-",
"List the current ACL rules in ACL config file format", "List the current ACL rules in ACL config file format",
@ -65,7 +75,7 @@ struct commandHelp {
9, 9,
"6.0.0" }, "6.0.0" },
{ "ACL SETUSER", { "ACL SETUSER",
"rule [rule ...]", "username [rule [rule ...]]",
"Modify or create the rules for a specific ACL user", "Modify or create the rules for a specific ACL user",
9, 9,
"6.0.0" }, "6.0.0" },
@ -85,7 +95,7 @@ struct commandHelp {
1, 1,
"2.0.0" }, "2.0.0" },
{ "AUTH", { "AUTH",
"password", "[username] password",
"Authenticate to the server", "Authenticate to the server",
8, 8,
"1.0.0" }, "1.0.0" },
@ -165,7 +175,7 @@ struct commandHelp {
8, 8,
"5.0.0" }, "5.0.0" },
{ "CLIENT KILL", { "CLIENT KILL",
"[ip:port] [ID client-id] [TYPE normal|master|slave|pubsub] [ADDR ip:port] [SKIPME yes/no]", "[ip:port] [ID client-id] [TYPE normal|master|slave|pubsub] [USER username] [ADDR ip:port] [SKIPME yes/no]",
"Kill the connection of a client", "Kill the connection of a client",
8, 8,
"2.4.0" }, "2.4.0" },
@ -183,14 +193,14 @@ struct commandHelp {
"ON|OFF|SKIP", "ON|OFF|SKIP",
"Instruct the server whether to reply to commands", "Instruct the server whether to reply to commands",
8, 8,
"3.2" }, "3.2.0" },
{ "CLIENT SETNAME", { "CLIENT SETNAME",
"connection-name", "connection-name",
"Set the current connection name", "Set the current connection name",
8, 8,
"2.6.9" }, "2.6.9" },
{ "CLIENT TRACKING", { "CLIENT TRACKING",
"ON|OFF [REDIRECT client-id] [PREFIX prefix] [BCAST] [OPTIN] [OPTOUT] [NOLOOP]", "ON|OFF [REDIRECT client-id] [PREFIX prefix [PREFIX prefix ...]] [BCAST] [OPTIN] [OPTOUT] [NOLOOP]",
"Enable or disable server assisted client side caching support", "Enable or disable server assisted client side caching support",
8, 8,
"6.0.0" }, "6.0.0" },
@ -626,7 +636,7 @@ struct commandHelp {
9, 9,
"2.8.13" }, "2.8.13" },
{ "LATENCY RESET", { "LATENCY RESET",
"[event]", "[event [event ...]]",
"Reset latency data for one or more events.", "Reset latency data for one or more events.",
9, 9,
"2.8.13" }, "2.8.13" },
@ -655,6 +665,11 @@ struct commandHelp {
"Remove and get the first element in a list", "Remove and get the first element in a list",
2, 2,
"1.0.0" }, "1.0.0" },
{ "LPOS",
"key element [RANK rank] [COUNT num-matches] [MAXLEN len]",
"Return the index of matching elements on a list",
2,
"6.0.6" },
{ "LPUSH", { "LPUSH",
"key element [element ...]", "key element [element ...]",
"Prepend one or multiple elements to a list", "Prepend one or multiple elements to a list",
@ -721,7 +736,7 @@ struct commandHelp {
1, 1,
"1.0.0" }, "1.0.0" },
{ "MIGRATE", { "MIGRATE",
"host port key|"" destination-db timeout [COPY] [REPLACE] [AUTH password] [KEYS key]", "host port key|"" destination-db timeout [COPY] [REPLACE] [AUTH password] [AUTH2 username password] [KEYS key]",
"Atomically transfer a key from a Redis instance to another one.", "Atomically transfer a key from a Redis instance to another one.",
0, 0,
"2.6.0" }, "2.6.0" },
@ -966,7 +981,7 @@ struct commandHelp {
8, 8,
"1.0.0" }, "1.0.0" },
{ "SET", { "SET",
"key value [EX seconds|PX milliseconds] [NX|XX] [KEEPTTL]", "key value [EX seconds|PX milliseconds|KEEPTTL] [NX|XX]",
"Set the string value of a key", "Set the string value of a key",
1, 1,
"1.0.0" }, "1.0.0" },

View File

@ -36,9 +36,9 @@
/* The Redis HyperLogLog implementation is based on the following ideas: /* The Redis HyperLogLog implementation is based on the following ideas:
* *
* * The use of a 64 bit hash function as proposed in [1], in order to don't * * The use of a 64 bit hash function as proposed in [1], in order to estimate
* limited to cardinalities up to 10^9, at the cost of just 1 additional * cardinalities larger than 10^9, at the cost of just 1 additional bit per
* bit per register. * register.
* * The use of 16384 6-bit registers for a great level of accuracy, using * * The use of 16384 6-bit registers for a great level of accuracy, using
* a total of 12k per key. * a total of 12k per key.
* * The use of the Redis string data type. No new type is introduced. * * The use of the Redis string data type. No new type is introduced.
@ -281,7 +281,7 @@ static const char *invalid_hll_err = "-INVALIDOBJ Corrupted HLL object detected\
* So we right shift of 0 bits (no shift in practice) and * So we right shift of 0 bits (no shift in practice) and
* left shift the next byte of 8 bits, even if we don't use it, * left shift the next byte of 8 bits, even if we don't use it,
* but this has the effect of clearing the bits so the result * but this has the effect of clearing the bits so the result
* will not be affacted after the OR. * will not be affected after the OR.
* *
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
* *
@ -299,7 +299,7 @@ static const char *invalid_hll_err = "-INVALIDOBJ Corrupted HLL object detected\
* |11000000| <- Our byte at b0 * |11000000| <- Our byte at b0
* +--------+ * +--------+
* *
* To create a AND-mask to clear the bits about this position, we just * To create an AND-mask to clear the bits about this position, we just
* initialize the mask with the value 63, left shift it of "fs" bits, * initialize the mask with the value 63, left shift it of "fs" bits,
* and finally invert the result. * and finally invert the result.
* *
@ -775,7 +775,7 @@ int hllSparseSet(robj *o, long index, uint8_t count) {
* by a ZERO opcode with len > 1, or by an XZERO opcode. * by a ZERO opcode with len > 1, or by an XZERO opcode.
* *
* In those cases the original opcode must be split into multiple * In those cases the original opcode must be split into multiple
* opcodes. The worst case is an XZERO split in the middle resuling into * opcodes. The worst case is an XZERO split in the middle resulting into
* XZERO - VAL - XZERO, so the resulting sequence max length is * XZERO - VAL - XZERO, so the resulting sequence max length is
* 5 bytes. * 5 bytes.
* *
@ -907,7 +907,7 @@ promote: /* Promote to dense representation. */
* the element belongs to is incremented if needed. * the element belongs to is incremented if needed.
* *
* This function is actually a wrapper for hllSparseSet(), it only performs * This function is actually a wrapper for hllSparseSet(), it only performs
* the hashshing of the elmenet to obtain the index and zeros run length. */ * the hashshing of the element to obtain the index and zeros run length. */
int hllSparseAdd(robj *o, unsigned char *ele, size_t elesize) { int hllSparseAdd(robj *o, unsigned char *ele, size_t elesize) {
long index; long index;
uint8_t count = hllPatLen(ele,elesize,&index); uint8_t count = hllPatLen(ele,elesize,&index);
@ -1022,7 +1022,7 @@ uint64_t hllCount(struct hllhdr *hdr, int *invalid) {
double m = HLL_REGISTERS; double m = HLL_REGISTERS;
double E; double E;
int j; int j;
/* Note that reghisto size could be just HLL_Q+2, becuase HLL_Q+1 is /* Note that reghisto size could be just HLL_Q+2, because HLL_Q+1 is
* the maximum frequency of the "000...1" sequence the hash function is * the maximum frequency of the "000...1" sequence the hash function is
* able to return. However it is slow to check for sanity of the * able to return. However it is slow to check for sanity of the
* input: instead we history array at a safe size: overflows will * input: instead we history array at a safe size: overflows will

View File

@ -71,7 +71,7 @@ int THPIsEnabled(void) {
return 0; return 0;
} }
fclose(fp); fclose(fp);
return (strstr(buf,"[never]") == NULL) ? 1 : 0; return (strstr(buf,"[always]") != NULL) ? 1 : 0;
} }
#endif #endif
@ -85,7 +85,7 @@ int THPGetAnonHugePagesSize(void) {
/* ---------------------------- Latency API --------------------------------- */ /* ---------------------------- Latency API --------------------------------- */
/* Latency monitor initialization. We just need to create the dictionary /* Latency monitor initialization. We just need to create the dictionary
* of time series, each time serie is created on demand in order to avoid * of time series, each time series is created on demand in order to avoid
* having a fixed list to maintain. */ * having a fixed list to maintain. */
void latencyMonitorInit(void) { void latencyMonitorInit(void) {
g_pserver->latency_events = dictCreate(&latencyTimeSeriesDictType,NULL); g_pserver->latency_events = dictCreate(&latencyTimeSeriesDictType,NULL);
@ -154,7 +154,7 @@ int latencyResetEvent(char *event_to_reset) {
/* Analyze the samples available for a given event and return a structure /* Analyze the samples available for a given event and return a structure
* populate with different metrics, average, MAD, min, max, and so forth. * populate with different metrics, average, MAD, min, max, and so forth.
* Check latency.h definition of struct latenctStat for more info. * Check latency.h definition of struct latencyStats for more info.
* If the specified event has no elements the structure is populate with * If the specified event has no elements the structure is populate with
* zero values. */ * zero values. */
void analyzeLatencyForEvent(char *event, struct latencyStats *ls) { void analyzeLatencyForEvent(char *event, struct latencyStats *ls) {
@ -343,7 +343,7 @@ sds createLatencyReport(void) {
} }
if (!strcasecmp(event,"aof-fstat") || if (!strcasecmp(event,"aof-fstat") ||
!strcasecmp(event,"rdb-unlik-temp-file")) { !strcasecmp(event,"rdb-unlink-temp-file")) {
advise_disk_contention = 1; advise_disk_contention = 1;
advise_local_disk = 1; advise_local_disk = 1;
advices += 2; advices += 2;
@ -396,7 +396,7 @@ sds createLatencyReport(void) {
/* Better VM. */ /* Better VM. */
report = sdscat(report,"\nI have a few advices for you:\n\n"); report = sdscat(report,"\nI have a few advices for you:\n\n");
if (advise_better_vm) { if (advise_better_vm) {
report = sdscat(report,"- If you are using a virtual machine, consider upgrading it with a faster one using an hypervisior that provides less latency during fork() calls. Xen is known to have poor fork() performance. Even in the context of the same VM provider, certain kinds of instances can execute fork faster than others.\n"); report = sdscat(report,"- If you are using a virtual machine, consider upgrading it with a faster one using a hypervisior that provides less latency during fork() calls. Xen is known to have poor fork() performance. Even in the context of the same VM provider, certain kinds of instances can execute fork faster than others.\n");
} }
/* Slow log. */ /* Slow log. */
@ -416,7 +416,7 @@ sds createLatencyReport(void) {
if (advise_scheduler) { if (advise_scheduler) {
report = sdscat(report,"- The system is slow to execute Redis code paths not containing system calls. This usually means the system does not provide Redis CPU time to run for long periods. You should try to:\n" report = sdscat(report,"- The system is slow to execute Redis code paths not containing system calls. This usually means the system does not provide Redis CPU time to run for long periods. You should try to:\n"
" 1) Lower the system load.\n" " 1) Lower the system load.\n"
" 2) Use a computer / VM just for Redis if you are running other softawre in the same system.\n" " 2) Use a computer / VM just for Redis if you are running other software in the same system.\n"
" 3) Check if you have a \"noisy neighbour\" problem.\n" " 3) Check if you have a \"noisy neighbour\" problem.\n"
" 4) Check with 'keydb-cli --intrinsic-latency 100' what is the intrinsic latency in your system.\n" " 4) Check with 'keydb-cli --intrinsic-latency 100' what is the intrinsic latency in your system.\n"
" 5) Check if the problem is allocator-related by recompiling Redis with MALLOC=libc, if you are using Jemalloc. However this may create fragmentation problems.\n"); " 5) Check if the problem is allocator-related by recompiling Redis with MALLOC=libc, if you are using Jemalloc. However this may create fragmentation problems.\n");
@ -432,7 +432,7 @@ sds createLatencyReport(void) {
} }
if (advise_data_writeback) { if (advise_data_writeback) {
report = sdscat(report,"- Mounting ext3/4 filesystems with data=writeback can provide a performance boost compared to data=ordered, however this mode of operation provides less guarantees, and sometimes it can happen that after a hard crash the AOF file will have an half-written command at the end and will require to be repaired before Redis restarts.\n"); report = sdscat(report,"- Mounting ext3/4 filesystems with data=writeback can provide a performance boost compared to data=ordered, however this mode of operation provides less guarantees, and sometimes it can happen that after a hard crash the AOF file will have a half-written command at the end and will require to be repaired before Redis restarts.\n");
} }
if (advise_disk_contention) { if (advise_disk_contention) {
@ -621,7 +621,7 @@ NULL
resets += latencyResetEvent(szFromObj(c->argv[j])); resets += latencyResetEvent(szFromObj(c->argv[j]));
addReplyLongLong(c,resets); addReplyLongLong(c,resets);
} }
} else if (!strcasecmp(szFromObj(c->argv[1]),"help") && c->argc >= 2) { } else if (!strcasecmp(szFromObj(c->argv[1]),"help") && c->argc == 2) {
addReplyHelp(c, help); addReplyHelp(c, help);
} else { } else {
addReplySubcommandSyntaxError(c); addReplySubcommandSyntaxError(c);

View File

@ -15,7 +15,7 @@ size_t lazyfreeGetPendingObjectsCount(void) {
/* Return the amount of work needed in order to free an object. /* Return the amount of work needed in order to free an object.
* The return value is not always the actual number of allocations the * The return value is not always the actual number of allocations the
* object is compoesd of, but a number proportional to it. * object is composed of, but a number proportional to it.
* *
* For strings the function always returns 1. * For strings the function always returns 1.
* *
@ -41,6 +41,30 @@ size_t lazyfreeGetFreeEffort(robj *obj) {
} else if (obj->type == OBJ_HASH && obj->encoding == OBJ_ENCODING_HT) { } else if (obj->type == OBJ_HASH && obj->encoding == OBJ_ENCODING_HT) {
dict *ht = (dict*)ptrFromObj(obj); dict *ht = (dict*)ptrFromObj(obj);
return dictSize(ht); return dictSize(ht);
} else if (obj->type == OBJ_STREAM) {
size_t effort = 0;
stream *s = (stream*)ptrFromObj(obj);
/* Make a best effort estimate to maintain constant runtime. Every macro
* node in the Stream is one allocation. */
effort += s->prax->numnodes;
/* Every consumer group is an allocation and so are the entries in its
* PEL. We use size of the first group's PEL as an estimate for all
* others. */
if (s->cgroups) {
raxIterator ri;
streamCG *cg;
raxStart(&ri,s->cgroups);
raxSeek(&ri,"^",NULL,0);
/* There must be at least one group so the following should always
* work. */
serverAssert(raxNext(&ri));
cg = (streamCG*)ri.data;
effort += raxSize(s->cgroups)*(1+raxSize(cg->pel));
raxStop(&ri);
}
return effort;
} else { } else {
return 1; /* Everything else is a single allocation. */ return 1; /* Everything else is a single allocation. */
} }
@ -55,7 +79,7 @@ int dbAsyncDelete(redisDb *db, robj *key) {
/* If the value is composed of a few allocations, to free in a lazy way /* If the value is composed of a few allocations, to free in a lazy way
* is actually just slower... So under a certain limit we just free * is actually just slower... So under a certain limit we just free
* the object synchronously. */ * the object synchronously. */
dictEntry *de = dictUnlink(db->pdict,ptrFromObj(key)); dictEntry *de = dictUnlink(db->dict,ptrFromObj(key));
if (de) { if (de) {
robj *val = (robj*)dictGetVal(de); robj *val = (robj*)dictGetVal(de);
if (val->FExpires()) if (val->FExpires())
@ -78,14 +102,14 @@ int dbAsyncDelete(redisDb *db, robj *key) {
if (free_effort > LAZYFREE_THRESHOLD && val->getrefcount(std::memory_order_relaxed) == 1) { if (free_effort > LAZYFREE_THRESHOLD && val->getrefcount(std::memory_order_relaxed) == 1) {
atomicIncr(lazyfree_objects,1); atomicIncr(lazyfree_objects,1);
bioCreateBackgroundJob(BIO_LAZY_FREE,val,NULL,NULL); bioCreateBackgroundJob(BIO_LAZY_FREE,val,NULL,NULL);
dictSetVal(db->pdict,de,NULL); dictSetVal(db->dict,de,NULL);
} }
} }
/* Release the key-val pair, or just the key if we set the val /* Release the key-val pair, or just the key if we set the val
* field to NULL in order to lazy free it later. */ * field to NULL in order to lazy free it later. */
if (de) { if (de) {
dictFreeUnlinkedEntry(db->pdict,de); dictFreeUnlinkedEntry(db->dict,de);
if (g_pserver->cluster_enabled) slotToKeyDel(szFromObj(key)); if (g_pserver->cluster_enabled) slotToKeyDel(szFromObj(key));
return 1; return 1;
} else { } else {
@ -108,25 +132,19 @@ void freeObjAsync(robj *o) {
* create a new empty set of hash tables and scheduling the old ones for * create a new empty set of hash tables and scheduling the old ones for
* lazy freeing. */ * lazy freeing. */
void emptyDbAsync(redisDb *db) { void emptyDbAsync(redisDb *db) {
dict *oldht1 = db->pdict; dict *oldht1 = db->dict;
auto *set = db->setexpire; auto *set = db->setexpire;
db->setexpire = new (MALLOC_LOCAL) expireset(); db->setexpire = new (MALLOC_LOCAL) expireset();
db->expireitr = db->setexpire->end(); db->expireitr = db->setexpire->end();
db->pdict = dictCreate(&dbDictType,NULL); db->dict = dictCreate(&dbDictType,NULL);
atomicIncr(lazyfree_objects,dictSize(oldht1)); atomicIncr(lazyfree_objects,dictSize(oldht1));
bioCreateBackgroundJob(BIO_LAZY_FREE,NULL,oldht1,set); bioCreateBackgroundJob(BIO_LAZY_FREE,NULL,oldht1,set);
} }
/* Empty the slots-keys map of Redis CLuster by creating a new empty one /* Release the radix tree mapping Redis Cluster keys to slots asynchronously. */
* and scheduiling the old for lazy freeing. */ void freeSlotsToKeysMapAsync(rax *rt) {
void slotToKeyFlushAsync(void) { atomicIncr(lazyfree_objects,rt->numele);
rax *old = g_pserver->cluster->slots_to_keys; bioCreateBackgroundJob(BIO_LAZY_FREE,NULL,NULL,rt);
g_pserver->cluster->slots_to_keys = raxNew();
memset(g_pserver->cluster->slots_keys_count,0,
sizeof(g_pserver->cluster->slots_keys_count));
atomicIncr(lazyfree_objects,old->numele);
bioCreateBackgroundJob(BIO_LAZY_FREE,NULL,NULL,old);
} }
/* Release objects from the lazyfree thread. It's just decrRefCount() /* Release objects from the lazyfree thread. It's just decrRefCount()
@ -137,10 +155,8 @@ void lazyfreeFreeObjectFromBioThread(robj *o) {
} }
/* Release a database from the lazyfree thread. The 'db' pointer is the /* Release a database from the lazyfree thread. The 'db' pointer is the
* database which was substitutied with a fresh one in the main thread * database which was substituted with a fresh one in the main thread
* when the database was logically deleted. 'sl' is a skiplist used by * when the database was logically deleted. */
* Redis Cluster in order to take the hash slots -> keys mapping. This
* may be NULL if Redis Cluster is disabled. */
void lazyfreeFreeDatabaseFromBioThread(dict *ht1, expireset *set) { void lazyfreeFreeDatabaseFromBioThread(dict *ht1, expireset *set) {
size_t numkeys = dictSize(ht1); size_t numkeys = dictSize(ht1);
dictRelease(ht1); dictRelease(ht1);
@ -148,7 +164,7 @@ void lazyfreeFreeDatabaseFromBioThread(dict *ht1, expireset *set) {
atomicDecr(lazyfree_objects,numkeys); atomicDecr(lazyfree_objects,numkeys);
} }
/* Release the skiplist mapping Redis Cluster keys to slots in the /* Release the radix tree mapping Redis Cluster keys to slots in the
* lazyfree thread. */ * lazyfree thread. */
void lazyfreeFreeSlotsMapFromBioThread(rax *rt) { void lazyfreeFreeSlotsMapFromBioThread(rax *rt) {
size_t len = rt->numele; size_t len = rt->numele;

View File

@ -405,7 +405,7 @@ unsigned char *lpNext(unsigned char *lp, unsigned char *p) {
} }
/* If 'p' points to an element of the listpack, calling lpPrev() will return /* If 'p' points to an element of the listpack, calling lpPrev() will return
* the pointer to the preivous element (the one on the left), or NULL if 'p' * the pointer to the previous element (the one on the left), or NULL if 'p'
* already pointed to the first element of the listpack. */ * already pointed to the first element of the listpack. */
unsigned char *lpPrev(unsigned char *lp, unsigned char *p) { unsigned char *lpPrev(unsigned char *lp, unsigned char *p) {
if (p-lp == LP_HDR_SIZE) return NULL; if (p-lp == LP_HDR_SIZE) return NULL;
@ -768,18 +768,18 @@ unsigned char *lpSeek(unsigned char *lp, long index) {
if (numele != LP_HDR_NUMELE_UNKNOWN) { if (numele != LP_HDR_NUMELE_UNKNOWN) {
if (index < 0) index = (long)numele+index; if (index < 0) index = (long)numele+index;
if (index < 0) return NULL; /* Index still < 0 means out of range. */ if (index < 0) return NULL; /* Index still < 0 means out of range. */
if (index >= numele) return NULL; /* Out of range the other side. */ if (index >= (long)numele) return NULL; /* Out of range the other side. */
/* We want to scan right-to-left if the element we are looking for /* We want to scan right-to-left if the element we are looking for
* is past the half of the listpack. */ * is past the half of the listpack. */
if (index > numele/2) { if (index > (long)numele/2) {
forward = 0; forward = 0;
/* Left to right scanning always expects a negative index. Convert /* Right to left scanning always expects a negative index. Convert
* our index to negative form. */ * our index to negative form. */
index -= numele; index -= numele;
} }
} else { } else {
/* If the listpack length is unspecified, for negative indexes we /* If the listpack length is unspecified, for negative indexes we
* want to always scan left-to-right. */ * want to always scan right-to-left. */
if (index < 0) forward = 0; if (index < 0) forward = 0;
} }

View File

@ -85,7 +85,7 @@ void lolwutCommand(client *c) {
} }
/* ========================== LOLWUT Canvase =============================== /* ========================== LOLWUT Canvase ===============================
* Many LOWUT versions will likely print some computer art to the screen. * Many LOLWUT versions will likely print some computer art to the screen.
* This is the case with LOLWUT 5 and LOLWUT 6, so here there is a generic * This is the case with LOLWUT 5 and LOLWUT 6, so here there is a generic
* canvas implementation that can be reused. */ * canvas implementation that can be reused. */
@ -106,7 +106,7 @@ void lwFreeCanvas(lwCanvas *canvas) {
} }
/* Set a pixel to the specified color. Color is 0 or 1, where zero means no /* Set a pixel to the specified color. Color is 0 or 1, where zero means no
* dot will be displyed, and 1 means dot will be displayed. * dot will be displayed, and 1 means dot will be displayed.
* Coordinates are arranged so that left-top corner is 0,0. You can write * Coordinates are arranged so that left-top corner is 0,0. You can write
* out of the size of the canvas without issues. */ * out of the size of the canvas without issues. */
void lwDrawPixel(lwCanvas *canvas, int x, int y, int color) { void lwDrawPixel(lwCanvas *canvas, int x, int y, int color) {

View File

@ -156,7 +156,7 @@ void lolwut5Command(client *c) {
return; return;
/* Limits. We want LOLWUT to be always reasonably fast and cheap to execute /* Limits. We want LOLWUT to be always reasonably fast and cheap to execute
* so we have maximum number of columns, rows, and output resulution. */ * so we have maximum number of columns, rows, and output resolution. */
if (cols < 1) cols = 1; if (cols < 1) cols = 1;
if (cols > 1000) cols = 1000; if (cols > 1000) cols = 1000;
if (squares_per_row < 1) squares_per_row = 1; if (squares_per_row < 1) squares_per_row = 1;

View File

@ -127,7 +127,7 @@
/* /*
* Whether to store pointers or offsets inside the hash table. On * Whether to store pointers or offsets inside the hash table. On
* 64 bit architetcures, pointers take up twice as much space, * 64 bit architectures, pointers take up twice as much space,
* and might also be slower. Default is to autodetect. * and might also be slower. Default is to autodetect.
*/ */
/*#define LZF_USER_OFFSETS autodetect */ /*#define LZF_USER_OFFSETS autodetect */

View File

@ -347,10 +347,15 @@ void memtest_alloc_and_test(size_t megabytes, int passes) {
} }
void memtest(size_t megabytes, int passes) { void memtest(size_t megabytes, int passes) {
#if !defined(__HAIKU__)
if (ioctl(1, TIOCGWINSZ, &ws) == -1) { if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
ws.ws_col = 80; ws.ws_col = 80;
ws.ws_row = 20; ws.ws_row = 20;
} }
#else
ws.ws_col = 80;
ws.ws_row = 20;
#endif
memtest_alloc_and_test(megabytes,passes); memtest_alloc_and_test(megabytes,passes);
printf("\nYour memory passed this test.\n"); printf("\nYour memory passed this test.\n");
printf("Please if you are still in doubt use the following two tools:\n"); printf("Please if you are still in doubt use the following two tools:\n");

File diff suppressed because it is too large Load Diff

View File

@ -125,7 +125,7 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
cmd_KEYRANGE,"readonly",1,1,0) == REDISMODULE_ERR) cmd_KEYRANGE,"readonly",1,1,0) == REDISMODULE_ERR)
return REDISMODULE_ERR; return REDISMODULE_ERR;
/* Create our global dictionray. Here we'll set our keys and values. */ /* Create our global dictionary. Here we'll set our keys and values. */
Keyspace = RedisModule_CreateDict(NULL); Keyspace = RedisModule_CreateDict(NULL);
return REDISMODULE_OK; return REDISMODULE_OK;

View File

@ -40,7 +40,7 @@
/* Timer callback. */ /* Timer callback. */
void timerHandler(RedisModuleCtx *ctx, void *data) { void timerHandler(RedisModuleCtx *ctx, void *data) {
REDISMODULE_NOT_USED(ctx); REDISMODULE_NOT_USED(ctx);
printf("Fired %s!\n", (const char *) data); printf("Fired %s!\n", (const char *)data);
RedisModule_Free(data); RedisModule_Free(data);
} }

View File

@ -91,7 +91,7 @@ int HelloPushCall_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, in
} }
/* HELLO.PUSH.CALL2 /* HELLO.PUSH.CALL2
* This is exaxctly as HELLO.PUSH.CALL, but shows how we can reply to the * This is exactly as HELLO.PUSH.CALL, but shows how we can reply to the
* client using directly a reply object that Call() returned. */ * client using directly a reply object that Call() returned. */
int HelloPushCall2_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) int HelloPushCall2_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{ {
@ -345,7 +345,7 @@ int HelloToggleCase_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
/* HELLO.MORE.EXPIRE key milliseconds. /* HELLO.MORE.EXPIRE key milliseconds.
* *
* If they key has already an associated TTL, extends it by "milliseconds" * If the key has already an associated TTL, extends it by "milliseconds"
* milliseconds. Otherwise no operation is performed. */ * milliseconds. Otherwise no operation is performed. */
int HelloMoreExpire_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { int HelloMoreExpire_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ RedisModule_AutoMemory(ctx); /* Use automatic memory management. */

View File

@ -71,12 +71,16 @@ static void setMOTDCache(const char *sz)
fclose(pf); fclose(pf);
} }
extern "C" char *fetchMOTD(int cache) extern "C" char *fetchMOTD(int cache, int enable_motd)
{ {
sds str; sds str;
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
/* Do not try the CURL if the motd is disabled*/
if (!enable_motd) {
return NULL;
}
/* First try and get the string from the cache */ /* First try and get the string from the cache */
if (cache) { if (cache) {
str = fetchMOTDFromCache(); str = fetchMOTDFromCache();
@ -124,9 +128,9 @@ extern "C" char *fetchMOTD(int cache)
#else #else
extern "C" char *fetchMOTD(int /* cache */) extern "C" char *fetchMOTD(int /* cache */, int /* enable_motd */)
{ {
return NULL; return NULL;
} }
#endif #endif

View File

@ -6,9 +6,8 @@ extern const char *motd_cache_file;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
char *fetchMOTD(int fCache, int enable_motd);
char *fetchMOTD(int fCache);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -37,6 +37,7 @@ void initClientMultiState(client *c) {
c->mstate.commands = NULL; c->mstate.commands = NULL;
c->mstate.count = 0; c->mstate.count = 0;
c->mstate.cmd_flags = 0; c->mstate.cmd_flags = 0;
c->mstate.cmd_inv_flags = 0;
} }
/* Release all the resources associated with MULTI/EXEC state */ /* Release all the resources associated with MULTI/EXEC state */
@ -77,6 +78,7 @@ void queueMultiCommand(client *c) {
incrRefCount(mc->argv[j]); incrRefCount(mc->argv[j]);
c->mstate.count++; c->mstate.count++;
c->mstate.cmd_flags |= c->cmd->flags; c->mstate.cmd_flags |= c->cmd->flags;
c->mstate.cmd_inv_flags |= ~c->cmd->flags;
} }
void discardTransaction(client *c) { void discardTransaction(client *c) {
@ -87,7 +89,7 @@ void discardTransaction(client *c) {
unwatchAllKeys(c); unwatchAllKeys(c);
} }
/* Flag the transacation as DIRTY_EXEC so that EXEC will fail. /* Flag the transaction as DIRTY_EXEC so that EXEC will fail.
* Should be called every time there is an error while queueing a command. */ * Should be called every time there is an error while queueing a command. */
void flagTransaction(client *c) { void flagTransaction(client *c) {
if (c->flags & CLIENT_MULTI) if (c->flags & CLIENT_MULTI)
@ -125,6 +127,24 @@ void execCommandPropagateExec(client *c) {
PROPAGATE_AOF|PROPAGATE_REPL); PROPAGATE_AOF|PROPAGATE_REPL);
} }
/* Aborts a transaction, with a specific error message.
* The transaction is always aboarted with -EXECABORT so that the client knows
* the server exited the multi state, but the actual reason for the abort is
* included too.
* Note: 'error' may or may not end with \r\n. see addReplyErrorFormat. */
void execCommandAbort(client *c, sds error) {
discardTransaction(c);
if (error[0] == '-') error++;
addReplyErrorFormat(c, "-EXECABORT Transaction discarded because of: %s", error);
/* Send EXEC to clients waiting data from MONITOR. We did send a MULTI
* already, and didn't send any of the queued commands, now we'll just send
* EXEC so it is clear that the transaction is over. */
if (listLength(g_pserver->monitors) && !g_pserver->loading)
replicationFeedMonitors(c,g_pserver->monitors,c->db->id,c->argv,c->argc);
}
void execCommand(client *c) { void execCommand(client *c) {
int j; int j;
robj **orig_argv; robj **orig_argv;
@ -138,15 +158,6 @@ void execCommand(client *c) {
return; return;
} }
/* If we are in -BUSY state, flag the transaction and return the
* -BUSY error, like Redis <= 5. This is a temporary fix, may be changed
* ASAP, see issue #7353 on Github. */
if (g_pserver->lua_timedout) {
flagTransaction(c);
addReply(c, shared.slowscripterr);
return;
}
/* Check if we need to abort the EXEC because: /* Check if we need to abort the EXEC because:
* 1) Some WATCHed key was touched. * 1) Some WATCHed key was touched.
* 2) There was a previous error while queueing commands. * 2) There was a previous error while queueing commands.
@ -160,21 +171,6 @@ void execCommand(client *c) {
goto handle_monitor; goto handle_monitor;
} }
/* If there are write commands inside the transaction, and this is a read
* only replica, we want to send an error. This happens when the transaction
* was initiated when the instance was a master or a writable replica and
* then the configuration changed (for example instance was turned into
* a replica). */
if (!g_pserver->loading && listLength(g_pserver->masters) && g_pserver->repl_slave_ro &&
!(c->flags & CLIENT_MASTER) && c->mstate.cmd_flags & CMD_WRITE)
{
addReplyError(c,
"Transaction contains write commands but instance "
"is now a read-only replica. EXEC aborted.");
discardTransaction(c);
goto handle_monitor;
}
/* Exec all the queued commands */ /* Exec all the queued commands */
unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */ unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */
orig_argv = c->argv; orig_argv = c->argv;
@ -352,32 +348,38 @@ void touchWatchedKey(redisDb *db, robj *key) {
} }
} }
/* On FLUSHDB or FLUSHALL all the watched keys that are present before the /* Set CLIENT_DIRTY_CAS to all clients of DB when DB is dirty.
* flush but will be deleted as effect of the flushing operation should * It may happen in the following situations:
* be touched. "dbid" is the DB that's getting the flush. -1 if it is * FLUSHDB, FLUSHALL, SWAPDB
* a FLUSHALL operation (all the DBs flushed). */ *
void touchWatchedKeysOnFlush(int dbid) { * replaced_with: for SWAPDB, the WATCH should be invalidated if
listIter li1, li2; * the key exists in either of them, and skipped only if it
* doesn't exist in both. */
void touchAllWatchedKeysInDb(redisDb *emptied, redisDb *replaced_with) {
listIter li;
listNode *ln; listNode *ln;
dictEntry *de;
serverAssert(GlobalLocksAcquired()); serverAssert(GlobalLocksAcquired());
/* For every client, check all the waited keys */ if (dictSize(emptied->watched_keys) == 0) return;
listRewind(g_pserver->clients,&li1);
while((ln = listNext(&li1))) {
client *c = (client*)listNodeValue(ln);
listRewind(c->watched_keys,&li2);
while((ln = listNext(&li2))) {
watchedKey *wk = (watchedKey*)listNodeValue(ln);
/* For every watched key matching the specified DB, if the dictIterator *di = dictGetSafeIterator(emptied->watched_keys);
* key exists, mark the client as dirty, as the key will be while((de = dictNext(di)) != NULL) {
* removed. */ robj *key = (robj*)dictGetKey(de);
if (dbid == -1 || wk->db->id == dbid) { list *clients = (list*)dictGetVal(de);
if (dictFind(wk->db->pdict, ptrFromObj(wk->key)) != NULL) if (!clients) continue;
c->flags |= CLIENT_DIRTY_CAS; listRewind(clients,&li);
while((ln = listNext(&li))) {
client *c = (client*)listNodeValue(ln);
if (dictFind(emptied->dict, ptrFromObj(key))) {
c->flags |= CLIENT_DIRTY_CAS;
} else if (replaced_with && dictFind(replaced_with->dict, ptrFromObj(key))) {
c->flags |= CLIENT_DIRTY_CAS;
} }
} }
} }
dictReleaseIterator(di);
} }
void watchCommand(client *c) { void watchCommand(client *c) {

File diff suppressed because it is too large Load Diff

View File

@ -62,7 +62,7 @@ int keyspaceEventsStringToFlags(char *classes) {
return flags; return flags;
} }
/* This function does exactly the revese of the function above: it gets /* This function does exactly the reverse of the function above: it gets
* as input an integer with the xored flags and returns a string representing * as input an integer with the xored flags and returns a string representing
* the selected classes. The string returned is an sds string that needs to * the selected classes. The string returned is an sds string that needs to
* be released with sdsfree(). */ * be released with sdsfree(). */

View File

@ -30,6 +30,7 @@
#include "server.h" #include "server.h"
#include "cron.h" #include "cron.h"
#include "t_nhash.h"
#include <math.h> #include <math.h>
#include <ctype.h> #include <ctype.h>
#include <mutex> #include <mutex>
@ -148,7 +149,7 @@ robj *createStringObject(const char *ptr, size_t len) {
/* Create a string object from a long long value. When possible returns a /* Create a string object from a long long value. When possible returns a
* shared integer object, or at least an integer encoded one. * shared integer object, or at least an integer encoded one.
* *
* If valueobj is non zero, the function avoids returning a a shared * If valueobj is non zero, the function avoids returning a shared
* integer, because the object is going to be used as value in the Redis key * integer, because the object is going to be used as value in the Redis key
* space (for instance when the INCR command is used), so we want LFU/LRU * space (for instance when the INCR command is used), so we want LFU/LRU
* values specific for each key. */ * values specific for each key. */
@ -272,7 +273,7 @@ robj *createZsetObject(void) {
zset *zs = (zset*)zmalloc(sizeof(*zs), MALLOC_SHARED); zset *zs = (zset*)zmalloc(sizeof(*zs), MALLOC_SHARED);
robj *o; robj *o;
zs->pdict = dictCreate(&zsetDictType,NULL); zs->dict = dictCreate(&zsetDictType,NULL);
zs->zsl = zslCreate(); zs->zsl = zslCreate();
o = createObject(OBJ_ZSET,zs); o = createObject(OBJ_ZSET,zs);
o->encoding = OBJ_ENCODING_SKIPLIST; o->encoding = OBJ_ENCODING_SKIPLIST;
@ -309,8 +310,10 @@ void freeStringObject(robj_roptr o) {
void freeListObject(robj_roptr o) { void freeListObject(robj_roptr o) {
if (o->encoding == OBJ_ENCODING_QUICKLIST) { if (o->encoding == OBJ_ENCODING_QUICKLIST) {
quicklistRelease((quicklist*)ptrFromObj(o)); quicklistRelease((quicklist*)ptrFromObj(o));
} else if (o->encoding == OBJ_ENCODING_ZIPLIST) {
zfree(ptrFromObj(o));
} else { } else {
serverPanic("Unknown list encoding type"); serverPanic("Unknown list encoding type: %d", o->encoding);
} }
} }
@ -332,7 +335,7 @@ void freeZsetObject(robj_roptr o) {
switch (o->encoding) { switch (o->encoding) {
case OBJ_ENCODING_SKIPLIST: case OBJ_ENCODING_SKIPLIST:
zs = (zset*)ptrFromObj(o); zs = (zset*)ptrFromObj(o);
dictRelease(zs->pdict); dictRelease(zs->dict);
zslFree(zs->zsl); zslFree(zs->zsl);
zfree(zs); zfree(zs);
break; break;
@ -395,6 +398,7 @@ void decrRefCount(robj_roptr o) {
case OBJ_MODULE: freeModuleObject(o); break; case OBJ_MODULE: freeModuleObject(o); break;
case OBJ_STREAM: freeStreamObject(o); break; case OBJ_STREAM: freeStreamObject(o); break;
case OBJ_CRON: freeCronObject(o); break; case OBJ_CRON: freeCronObject(o); break;
case OBJ_NESTEDHASH: freeNestedHashObject(o); break;
default: serverPanic("Unknown object type"); break; default: serverPanic("Unknown object type"); break;
} }
if (g_pserver->fActiveReplica) { if (g_pserver->fActiveReplica) {
@ -433,7 +437,7 @@ robj *resetRefCount(robj *obj) {
int checkType(client *c, robj_roptr o, int type) { int checkType(client *c, robj_roptr o, int type) {
if (o->type != type) { if (o->type != type) {
addReplyAsync(c,shared.wrongtypeerr); addReply(c,shared.wrongtypeerr);
return 1; return 1;
} }
return 0; return 0;
@ -801,6 +805,7 @@ const char *strEncoding(int encoding) {
case OBJ_ENCODING_INTSET: return "intset"; case OBJ_ENCODING_INTSET: return "intset";
case OBJ_ENCODING_SKIPLIST: return "skiplist"; case OBJ_ENCODING_SKIPLIST: return "skiplist";
case OBJ_ENCODING_EMBSTR: return "embstr"; case OBJ_ENCODING_EMBSTR: return "embstr";
case OBJ_ENCODING_STREAM: return "stream";
default: return "unknown"; default: return "unknown";
} }
} }
@ -847,7 +852,7 @@ size_t objectComputeSize(robj *o, size_t sample_size) {
if(o->encoding == OBJ_ENCODING_INT) { if(o->encoding == OBJ_ENCODING_INT) {
asize = sizeof(*o); asize = sizeof(*o);
} else if(o->encoding == OBJ_ENCODING_RAW) { } else if(o->encoding == OBJ_ENCODING_RAW) {
asize = sdsAllocSize(szFromObj(o))+sizeof(*o); asize = sdsZmallocSize(szFromObj(o))+sizeof(*o);
} else if(o->encoding == OBJ_ENCODING_EMBSTR) { } else if(o->encoding == OBJ_ENCODING_EMBSTR) {
asize = sdslen(szFromObj(o))+2+sizeof(*o); asize = sdslen(szFromObj(o))+2+sizeof(*o);
} else { } else {
@ -875,7 +880,7 @@ size_t objectComputeSize(robj *o, size_t sample_size) {
asize = sizeof(*o)+sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d)); asize = sizeof(*o)+sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
while((de = dictNext(di)) != NULL && samples < sample_size) { while((de = dictNext(di)) != NULL && samples < sample_size) {
ele = (sds)dictGetKey(de); ele = (sds)dictGetKey(de);
elesize += sizeof(struct dictEntry) + sdsAllocSize(ele); elesize += sizeof(struct dictEntry) + sdsZmallocSize(ele);
samples++; samples++;
} }
dictReleaseIterator(di); dictReleaseIterator(di);
@ -890,14 +895,14 @@ size_t objectComputeSize(robj *o, size_t sample_size) {
if (o->encoding == OBJ_ENCODING_ZIPLIST) { if (o->encoding == OBJ_ENCODING_ZIPLIST) {
asize = sizeof(*o)+(ziplistBlobLen((unsigned char*)ptrFromObj(o))); asize = sizeof(*o)+(ziplistBlobLen((unsigned char*)ptrFromObj(o)));
} else if (o->encoding == OBJ_ENCODING_SKIPLIST) { } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {
d = ((zset*)ptrFromObj(o))->pdict; d = ((zset*)ptrFromObj(o))->dict;
zskiplist *zsl = ((zset*)ptrFromObj(o))->zsl; zskiplist *zsl = ((zset*)ptrFromObj(o))->zsl;
zskiplistNode *znode = zsl->header->level(0)->forward; zskiplistNode *znode = zsl->header->level(0)->forward;
asize = sizeof(*o)+sizeof(zset)+sizeof(zskiplist)+sizeof(dict)+ asize = sizeof(*o)+sizeof(zset)+sizeof(zskiplist)+sizeof(dict)+
(sizeof(struct dictEntry*)*dictSlots(d))+ (sizeof(struct dictEntry*)*dictSlots(d))+
zmalloc_size(zsl->header); zmalloc_size(zsl->header);
while(znode != NULL && samples < sample_size) { while(znode != NULL && samples < sample_size) {
elesize += sdsAllocSize(znode->ele); elesize += sdsZmallocSize(znode->ele);
elesize += sizeof(struct dictEntry) + zmalloc_size(znode); elesize += sizeof(struct dictEntry) + zmalloc_size(znode);
samples++; samples++;
znode = znode->level(0)->forward; znode = znode->level(0)->forward;
@ -916,7 +921,7 @@ size_t objectComputeSize(robj *o, size_t sample_size) {
while((de = dictNext(di)) != NULL && samples < sample_size) { while((de = dictNext(di)) != NULL && samples < sample_size) {
ele = (sds)dictGetKey(de); ele = (sds)dictGetKey(de);
ele2 = (sds)dictGetVal(de); ele2 = (sds)dictGetVal(de);
elesize += sdsAllocSize(ele) + sdsAllocSize(ele2); elesize += sdsZmallocSize(ele) + sdsZmallocSize(ele2);
elesize += sizeof(struct dictEntry); elesize += sizeof(struct dictEntry);
samples++; samples++;
} }
@ -1057,7 +1062,7 @@ struct redisMemOverhead *getMemoryOverheadData(void) {
mem = 0; mem = 0;
if (g_pserver->aof_state != AOF_OFF) { if (g_pserver->aof_state != AOF_OFF) {
mem += sdsalloc(g_pserver->aof_buf); mem += sdsZmallocSize(g_pserver->aof_buf);
mem += aofRewriteBufferSize(); mem += aofRewriteBufferSize();
} }
mh->aof_buffer = mem; mh->aof_buffer = mem;
@ -1077,16 +1082,16 @@ struct redisMemOverhead *getMemoryOverheadData(void) {
for (j = 0; j < cserver.dbnum; j++) { for (j = 0; j < cserver.dbnum; j++) {
redisDb *db = g_pserver->db+j; redisDb *db = g_pserver->db+j;
long long keyscount = dictSize(db->pdict); long long keyscount = dictSize(db->dict);
if (keyscount==0) continue; if (keyscount==0) continue;
mh->total_keys += keyscount; mh->total_keys += keyscount;
mh->db = (decltype(mh->db))zrealloc(mh->db,sizeof(mh->db[0])*(mh->num_dbs+1), MALLOC_LOCAL); mh->db = (decltype(mh->db))zrealloc(mh->db,sizeof(mh->db[0])*(mh->num_dbs+1), MALLOC_LOCAL);
mh->db[mh->num_dbs].dbid = j; mh->db[mh->num_dbs].dbid = j;
mem = dictSize(db->pdict) * sizeof(dictEntry) + mem = dictSize(db->dict) * sizeof(dictEntry) +
dictSlots(db->pdict) * sizeof(dictEntry*) + dictSlots(db->dict) * sizeof(dictEntry*) +
dictSize(db->pdict) * sizeof(robj); dictSize(db->dict) * sizeof(robj);
mh->db[mh->num_dbs].overhead_ht_main = mem; mh->db[mh->num_dbs].overhead_ht_main = mem;
mem_total+=mem; mem_total+=mem;
@ -1272,24 +1277,21 @@ int objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,
/* This is a helper function for the OBJECT command. We need to lookup keys /* This is a helper function for the OBJECT command. We need to lookup keys
* without any modification of LRU or other parameters. */ * without any modification of LRU or other parameters. */
robj *objectCommandLookup(client *c, robj *key) { robj_roptr objectCommandLookup(client *c, robj *key) {
dictEntry *de; return lookupKeyReadWithFlags(c->db,key,LOOKUP_NOTOUCH|LOOKUP_NONOTIFY);
if ((de = dictFind(c->db->pdict,ptrFromObj(key))) == NULL) return NULL;
return (robj*) dictGetVal(de);
} }
robj *objectCommandLookupOrReply(client *c, robj *key, robj *reply) { robj_roptr objectCommandLookupOrReply(client *c, robj *key, robj *reply) {
robj *o = objectCommandLookup(c,key); robj_roptr o = objectCommandLookup(c,key);
if (!o) addReply(c, reply); if (!o) addReply(c, reply);
return o; return o;
} }
/* Object command allows to inspect the internals of an Redis Object. /* Object command allows to inspect the internals of a Redis Object.
* Usage: OBJECT <refcount|encoding|idletime|freq> <key> */ * Usage: OBJECT <refcount|encoding|idletime|freq> <key> */
void objectCommand(client *c) { void objectCommand(client *c) {
robj *o; robj_roptr o;
if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),"help")) { if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),"help")) {
const char *help[] = { const char *help[] = {
@ -1302,15 +1304,15 @@ NULL
addReplyHelp(c, help); addReplyHelp(c, help);
} else if (!strcasecmp(szFromObj(c->argv[1]),"refcount") && c->argc == 3) { } else if (!strcasecmp(szFromObj(c->argv[1]),"refcount") && c->argc == 3) {
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp])) if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))
== NULL) return; == nullptr) return;
addReplyLongLong(c,o->getrefcount(std::memory_order_relaxed)); addReplyLongLong(c,o->getrefcount(std::memory_order_relaxed));
} else if (!strcasecmp(szFromObj(c->argv[1]),"encoding") && c->argc == 3) { } else if (!strcasecmp(szFromObj(c->argv[1]),"encoding") && c->argc == 3) {
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp])) if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))
== NULL) return; == nullptr) return;
addReplyBulkCString(c,strEncoding(o->encoding)); addReplyBulkCString(c,strEncoding(o->encoding));
} else if (!strcasecmp(szFromObj(c->argv[1]),"idletime") && c->argc == 3) { } else if (!strcasecmp(szFromObj(c->argv[1]),"idletime") && c->argc == 3) {
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp])) if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))
== NULL) return; == nullptr) return;
if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) { if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) {
addReplyError(c,"An LFU maxmemory policy is selected, idle time not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust."); addReplyError(c,"An LFU maxmemory policy is selected, idle time not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.");
return; return;
@ -1318,7 +1320,7 @@ NULL
addReplyLongLong(c,estimateObjectIdleTime(o)/1000); addReplyLongLong(c,estimateObjectIdleTime(o)/1000);
} else if (!strcasecmp(szFromObj(c->argv[1]),"freq") && c->argc == 3) { } else if (!strcasecmp(szFromObj(c->argv[1]),"freq") && c->argc == 3) {
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp])) if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))
== NULL) return; == nullptr) return;
if (!(g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU)) { if (!(g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU)) {
addReplyError(c,"An LFU maxmemory policy is not selected, access frequency not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust."); addReplyError(c,"An LFU maxmemory policy is not selected, access frequency not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.");
return; return;
@ -1327,10 +1329,10 @@ NULL
* in case of the key has not been accessed for a long time, * in case of the key has not been accessed for a long time,
* because we update the access time only * because we update the access time only
* when the key is read or overwritten. */ * when the key is read or overwritten. */
addReplyLongLong(c,LFUDecrAndReturn(o)); addReplyLongLong(c,LFUDecrAndReturn(o.unsafe_robjcast()));
} else if (!strcasecmp(szFromObj(c->argv[1]), "lastmodified") && c->argc == 3) { } else if (!strcasecmp(szFromObj(c->argv[1]), "lastmodified") && c->argc == 3) {
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp])) if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))
== NULL) return; == nullptr) return;
uint64_t mvcc = mvccFromObj(o); uint64_t mvcc = mvccFromObj(o);
addReplyLongLong(c, (g_pserver->mstime - (mvcc >> MVCC_MS_SHIFT)) / 1000); addReplyLongLong(c, (g_pserver->mstime - (mvcc >> MVCC_MS_SHIFT)) / 1000);
} else { } else {
@ -1373,12 +1375,12 @@ NULL
return; return;
} }
} }
if ((de = dictFind(c->db->pdict,ptrFromObj(c->argv[2]))) == NULL) { if ((de = dictFind(c->db->dict,ptrFromObj(c->argv[2]))) == NULL) {
addReplyNull(c); addReplyNull(c);
return; return;
} }
size_t usage = objectComputeSize((robj*)dictGetVal(de),samples); size_t usage = objectComputeSize((robj*)dictGetVal(de),samples);
usage += sdsAllocSize((sds)dictGetKey(de)); usage += sdsZmallocSize((sds)dictGetKey(de));
usage += sizeof(dictEntry); usage += sizeof(dictEntry);
addReplyLongLong(c,usage); addReplyLongLong(c,usage);
} else if (!strcasecmp(szFromObj(c->argv[1]),"stats") && c->argc == 2) { } else if (!strcasecmp(szFromObj(c->argv[1]),"stats") && c->argc == 2) {
@ -1526,7 +1528,7 @@ void *allocPtrFromObj(robj_roptr o) {
} }
robj *objFromAllocPtr(void *pv) { robj *objFromAllocPtr(void *pv) {
if (g_pserver->fActiveReplica) { if (pv != nullptr && g_pserver->fActiveReplica) {
return reinterpret_cast<robj*>(reinterpret_cast<redisObjectExtended*>(pv)+1); return reinterpret_cast<robj*>(reinterpret_cast<redisObjectExtended*>(pv)+1);
} }
return reinterpret_cast<robj*>(pv); return reinterpret_cast<robj*>(pv);

View File

@ -43,12 +43,12 @@ int clientSubscriptionsCount(client *c);
* addReply*() API family. */ * addReply*() API family. */
void addReplyPubsubMessage(client *c, robj *channel, robj *msg) { void addReplyPubsubMessage(client *c, robj *channel, robj *msg) {
if (c->resp == 2) if (c->resp == 2)
addReplyAsync(c,shared.mbulkhdr[3]); addReply(c,shared.mbulkhdr[3]);
else else
addReplyPushLenAsync(c,3); addReplyPushLen(c,3);
addReplyAsync(c,shared.messagebulk); addReply(c,shared.messagebulk);
addReplyBulkAsync(c,channel); addReplyBulk(c,channel);
if (msg) addReplyBulkAsync(c,msg); if (msg) addReplyBulk(c,msg);
} }
/* Send a pubsub message of type "pmessage" to the client. The difference /* Send a pubsub message of type "pmessage" to the client. The difference
@ -56,13 +56,13 @@ void addReplyPubsubMessage(client *c, robj *channel, robj *msg) {
* this message format also includes the pattern that matched the message. */ * this message format also includes the pattern that matched the message. */
void addReplyPubsubPatMessage(client *c, robj *pat, robj *channel, robj *msg) { void addReplyPubsubPatMessage(client *c, robj *pat, robj *channel, robj *msg) {
if (c->resp == 2) if (c->resp == 2)
addReplyAsync(c,shared.mbulkhdr[4]); addReply(c,shared.mbulkhdr[4]);
else else
addReplyPushLenAsync(c,4); addReplyPushLen(c,4);
addReplyAsync(c,shared.pmessagebulk); addReply(c,shared.pmessagebulk);
addReplyBulkAsync(c,pat); addReplyBulk(c,pat);
addReplyBulkAsync(c,channel); addReplyBulk(c,channel);
addReplyBulkAsync(c,msg); addReplyBulk(c,msg);
} }
/* Send the pubsub subscription notification to the client. */ /* Send the pubsub subscription notification to the client. */

View File

@ -46,7 +46,7 @@
* count: 16 bits, max 65536 (max zl bytes is 65k, so max count actually < 32k). * count: 16 bits, max 65536 (max zl bytes is 65k, so max count actually < 32k).
* encoding: 2 bits, RAW=1, LZF=2. * encoding: 2 bits, RAW=1, LZF=2.
* container: 2 bits, NONE=1, ZIPLIST=2. * container: 2 bits, NONE=1, ZIPLIST=2.
* recompress: 1 bit, bool, true if node is temporarry decompressed for usage. * recompress: 1 bit, bool, true if node is temporary decompressed for usage.
* attempted_compress: 1 bit, boolean, used for verifying during testing. * attempted_compress: 1 bit, boolean, used for verifying during testing.
* extra: 10 bits, free for future use; pads out the remainder of 32 bits */ * extra: 10 bits, free for future use; pads out the remainder of 32 bits */
typedef struct quicklistNode { typedef struct quicklistNode {
@ -105,7 +105,7 @@ typedef struct quicklistBookmark {
/* quicklist is a 40 byte struct (on 64-bit systems) describing a quicklist. /* quicklist is a 40 byte struct (on 64-bit systems) describing a quicklist.
* 'count' is the number of total entries. * 'count' is the number of total entries.
* 'len' is the number of quicklist nodes. * 'len' is the number of quicklist nodes.
* 'compress' is: -1 if compression disabled, otherwise it's the number * 'compress' is: 0 if compression disabled, otherwise it's the number
* of quicklistNodes to leave uncompressed at ends of quicklist. * of quicklistNodes to leave uncompressed at ends of quicklist.
* 'fill' is the user-requested (or default) fill factor. * 'fill' is the user-requested (or default) fill factor.
* 'bookmakrs are an optional feature that is used by realloc this struct, * 'bookmakrs are an optional feature that is used by realloc this struct,

View File

@ -487,8 +487,8 @@ static inline size_t raxLowWalk(rax *rax, unsigned char *s, size_t len, raxNode
if (h->iscompr) j = 0; /* Compressed node only child is at index 0. */ if (h->iscompr) j = 0; /* Compressed node only child is at index 0. */
memcpy(&h,children+j,sizeof(h)); memcpy(&h,children+j,sizeof(h));
parentlink = children+j; parentlink = children+j;
j = 0; /* If the new node is compressed and we do not j = 0; /* If the new node is non compressed and we do not
iterate again (since i == l) set the split iterate again (since i == len) set the split
position to 0 to signal this node represents position to 0 to signal this node represents
the searched key. */ the searched key. */
} }
@ -628,7 +628,7 @@ int raxGenericInsert(rax *rax, unsigned char *s, size_t len, void *data, void **
* *
* 3b. IF $SPLITPOS != 0: * 3b. IF $SPLITPOS != 0:
* Trim the compressed node (reallocating it as well) in order to * Trim the compressed node (reallocating it as well) in order to
* contain $splitpos characters. Change chilid pointer in order to link * contain $splitpos characters. Change child pointer in order to link
* to the split node. If new compressed node len is just 1, set * to the split node. If new compressed node len is just 1, set
* iscompr to 0 (layout is the same). Fix parent's reference. * iscompr to 0 (layout is the same). Fix parent's reference.
* *
@ -1082,7 +1082,7 @@ int raxRemove(rax *rax, unsigned char *s, size_t len, void **old) {
} }
} else if (h->size == 1) { } else if (h->size == 1) {
/* If the node had just one child, after the removal of the key /* If the node had just one child, after the removal of the key
* further compression with adjacent nodes is pontentially possible. */ * further compression with adjacent nodes is potentially possible. */
trycompress = 1; trycompress = 1;
} }
@ -1329,7 +1329,7 @@ int raxIteratorNextStep(raxIterator *it, int noup) {
if (!noup && children) { if (!noup && children) {
debugf("GO DEEPER\n"); debugf("GO DEEPER\n");
/* Seek the lexicographically smaller key in this subtree, which /* Seek the lexicographically smaller key in this subtree, which
* is the first one found always going torwards the first child * is the first one found always going towards the first child
* of every successive node. */ * of every successive node. */
if (!raxStackPush(&it->stack,it->node)) return 0; if (!raxStackPush(&it->stack,it->node)) return 0;
raxNode **cp = raxNodeFirstChildPtr(it->node); raxNode **cp = raxNodeFirstChildPtr(it->node);
@ -1348,7 +1348,7 @@ int raxIteratorNextStep(raxIterator *it, int noup) {
return 1; return 1;
} }
} else { } else {
/* If we finished exporing the previous sub-tree, switch to the /* If we finished exploring the previous sub-tree, switch to the
* new one: go upper until a node is found where there are * new one: go upper until a node is found where there are
* children representing keys lexicographically greater than the * children representing keys lexicographically greater than the
* current key. */ * current key. */
@ -1510,7 +1510,7 @@ int raxIteratorPrevStep(raxIterator *it, int noup) {
int raxSeek(raxIterator *it, const char *op, unsigned char *ele, size_t len) { int raxSeek(raxIterator *it, const char *op, unsigned char *ele, size_t len) {
int eq = 0, lt = 0, gt = 0, first = 0, last = 0; int eq = 0, lt = 0, gt = 0, first = 0, last = 0;
it->stack.items = 0; /* Just resetting. Intialized by raxStart(). */ it->stack.items = 0; /* Just resetting. Initialized by raxStart(). */
it->flags |= RAX_ITER_JUST_SEEKED; it->flags |= RAX_ITER_JUST_SEEKED;
it->flags &= ~RAX_ITER_EOF; it->flags &= ~RAX_ITER_EOF;
it->key_len = 0; it->key_len = 0;
@ -1731,7 +1731,7 @@ int raxPrev(raxIterator *it) {
* tree, expect a disappointing distribution. A random walk produces good * tree, expect a disappointing distribution. A random walk produces good
* random elements if the tree is not sparse, however in the case of a radix * random elements if the tree is not sparse, however in the case of a radix
* tree certain keys will be reported much more often than others. At least * tree certain keys will be reported much more often than others. At least
* this function should be able to expore every possible element eventually. */ * this function should be able to explore every possible element eventually. */
int raxRandomWalk(raxIterator *it, size_t steps) { int raxRandomWalk(raxIterator *it, size_t steps) {
if (it->rt->numele == 0) { if (it->rt->numele == 0) {
it->flags |= RAX_ITER_EOF; it->flags |= RAX_ITER_EOF;
@ -1825,7 +1825,7 @@ uint64_t raxSize(rax *rax) {
/* ----------------------------- Introspection ------------------------------ */ /* ----------------------------- Introspection ------------------------------ */
/* This function is mostly used for debugging and learning purposes. /* This function is mostly used for debugging and learning purposes.
* It shows an ASCII representation of a tree on standard output, outling * It shows an ASCII representation of a tree on standard output, outline
* all the nodes and the contained keys. * all the nodes and the contained keys.
* *
* The representation is as follow: * The representation is as follow:
@ -1835,7 +1835,7 @@ uint64_t raxSize(rax *rax) {
* [abc]=0x12345678 (node is a key, pointing to value 0x12345678) * [abc]=0x12345678 (node is a key, pointing to value 0x12345678)
* [] (a normal empty node) * [] (a normal empty node)
* *
* Children are represented in new idented lines, each children prefixed by * Children are represented in new indented lines, each children prefixed by
* the "`-(x)" string, where "x" is the edge byte. * the "`-(x)" string, where "x" is the edge byte.
* *
* [abc] * [abc]

View File

@ -68,7 +68,7 @@ extern "C" {
* successive nodes having a single child are "compressed" into the node * successive nodes having a single child are "compressed" into the node
* itself as a string of characters, each representing a next-level child, * itself as a string of characters, each representing a next-level child,
* and only the link to the node representing the last character node is * and only the link to the node representing the last character node is
* provided inside the representation. So the above representation is turend * provided inside the representation. So the above representation is turned
* into: * into:
* *
* ["foo"] "" * ["foo"] ""
@ -133,7 +133,7 @@ typedef struct raxNode {
* nodes). * nodes).
* *
* If the node has an associated key (iskey=1) and is not NULL * If the node has an associated key (iskey=1) and is not NULL
* (isnull=0), then after the raxNode pointers poiting to the * (isnull=0), then after the raxNode pointers pointing to the
* children, an additional value pointer is present (as you can see * children, an additional value pointer is present (as you can see
* in the representation above as "value-ptr" field). * in the representation above as "value-ptr" field).
*/ */

View File

@ -36,6 +36,7 @@
#include "cron.h" #include "cron.h"
#include <math.h> #include <math.h>
#include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
@ -54,6 +55,9 @@ extern int rdbCheckMode;
void rdbCheckError(const char *fmt, ...); void rdbCheckError(const char *fmt, ...);
void rdbCheckSetError(const char *fmt, ...); void rdbCheckSetError(const char *fmt, ...);
#ifdef __GNUC__
void rdbReportError(int corruption_error, int linenum, const char *reason, ...) __attribute__ ((format (printf, 3, 4)));
#endif
void rdbReportError(int corruption_error, int linenum, const char *reason, ...) { void rdbReportError(int corruption_error, int linenum, const char *reason, ...) {
va_list ap; va_list ap;
char msg[1024]; char msg[1024];
@ -82,7 +86,7 @@ void rdbReportError(int corruption_error, int linenum, const char *reason, ...)
exit(1); exit(1);
} }
static int rdbWriteRaw(rio *rdb, void *p, size_t len) { static ssize_t rdbWriteRaw(rio *rdb, void *p, size_t len) {
if (rdb && rioWrite(rdb,p,len) == 0) if (rdb && rioWrite(rdb,p,len) == 0)
return -1; return -1;
return len; return len;
@ -489,7 +493,7 @@ void *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr) {
int plain = flags & RDB_LOAD_PLAIN; int plain = flags & RDB_LOAD_PLAIN;
int sds = flags & RDB_LOAD_SDS; int sds = flags & RDB_LOAD_SDS;
int isencoded; int isencoded;
uint64_t len; unsigned long long len;
len = rdbLoadLen(rdb,&isencoded); len = rdbLoadLen(rdb,&isencoded);
if (isencoded) { if (isencoded) {
@ -501,8 +505,8 @@ void *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr) {
case RDB_ENC_LZF: case RDB_ENC_LZF:
return rdbLoadLzfStringObject(rdb,flags,lenptr); return rdbLoadLzfStringObject(rdb,flags,lenptr);
default: default:
rdbExitReportCorruptRDB("Unknown RDB string encoding type %d",len); rdbExitReportCorruptRDB("Unknown RDB string encoding type %llu",len);
return nullptr; /* Never reached. */ return nullptr;
} }
} }
@ -706,15 +710,23 @@ ssize_t rdbSaveStreamPEL(rio *rdb, rax *pel, int nacks) {
while(raxNext(&ri)) { while(raxNext(&ri)) {
/* We store IDs in raw form as 128 big big endian numbers, like /* We store IDs in raw form as 128 big big endian numbers, like
* they are inside the radix tree key. */ * they are inside the radix tree key. */
if ((n = rdbWriteRaw(rdb,ri.key,sizeof(streamID))) == -1) return -1; if ((n = rdbWriteRaw(rdb,ri.key,sizeof(streamID))) == -1) {
raxStop(&ri);
return -1;
}
nwritten += n; nwritten += n;
if (nacks) { if (nacks) {
streamNACK *nack = (streamNACK*)ri.data; streamNACK *nack = (streamNACK*)ri.data;
if ((n = rdbSaveMillisecondTime(rdb,nack->delivery_time)) == -1) if ((n = rdbSaveMillisecondTime(rdb,nack->delivery_time)) == -1) {
raxStop(&ri);
return -1; return -1;
}
nwritten += n; nwritten += n;
if ((n = rdbSaveLen(rdb,nack->delivery_count)) == -1) return -1; if ((n = rdbSaveLen(rdb,nack->delivery_count)) == -1) {
raxStop(&ri);
return -1;
}
nwritten += n; nwritten += n;
/* We don't save the consumer name: we'll save the pending IDs /* We don't save the consumer name: we'll save the pending IDs
* for each consumer in the consumer PEL, and resolve the consumer * for each consumer in the consumer PEL, and resolve the consumer
@ -743,20 +755,27 @@ size_t rdbSaveStreamConsumers(rio *rdb, streamCG *cg) {
streamConsumer *consumer = (streamConsumer*)ri.data; streamConsumer *consumer = (streamConsumer*)ri.data;
/* Consumer name. */ /* Consumer name. */
if ((n = rdbSaveRawString(rdb,ri.key,ri.key_len)) == -1) return -1; if ((n = rdbSaveRawString(rdb,ri.key,ri.key_len)) == -1) {
raxStop(&ri);
return -1;
}
nwritten += n; nwritten += n;
/* Last seen time. */ /* Last seen time. */
if ((n = rdbSaveMillisecondTime(rdb,consumer->seen_time)) == -1) if ((n = rdbSaveMillisecondTime(rdb,consumer->seen_time)) == -1) {
raxStop(&ri);
return -1; return -1;
}
nwritten += n; nwritten += n;
/* Consumer PEL, without the ACKs (see last parameter of the function /* Consumer PEL, without the ACKs (see last parameter of the function
* passed with value of 0), at loading time we'll lookup the ID * passed with value of 0), at loading time we'll lookup the ID
* in the consumer group global PEL and will put a reference in the * in the consumer group global PEL and will put a reference in the
* consumer local PEL. */ * consumer local PEL. */
if ((n = rdbSaveStreamPEL(rdb,consumer->pel,0)) == -1) if ((n = rdbSaveStreamPEL(rdb,consumer->pel,0)) == -1) {
raxStop(&ri);
return -1; return -1;
}
nwritten += n; nwritten += n;
} }
raxStop(&ri); raxStop(&ri);
@ -921,9 +940,15 @@ ssize_t rdbSaveObject(rio *rdb, robj_roptr o, robj *key) {
while (raxNext(&ri)) { while (raxNext(&ri)) {
unsigned char *lp = (unsigned char*)ri.data; unsigned char *lp = (unsigned char*)ri.data;
size_t lp_bytes = lpBytes(lp); size_t lp_bytes = lpBytes(lp);
if ((n = rdbSaveRawString(rdb,ri.key,ri.key_len)) == -1) return -1; if ((n = rdbSaveRawString(rdb,ri.key,ri.key_len)) == -1) {
raxStop(&ri);
return -1;
}
nwritten += n; nwritten += n;
if ((n = rdbSaveRawString(rdb,lp,lp_bytes)) == -1) return -1; if ((n = rdbSaveRawString(rdb,lp,lp_bytes)) == -1) {
raxStop(&ri);
return -1;
}
nwritten += n; nwritten += n;
} }
raxStop(&ri); raxStop(&ri);
@ -955,22 +980,36 @@ ssize_t rdbSaveObject(rio *rdb, robj_roptr o, robj *key) {
streamCG *cg = (streamCG*)ri.data; streamCG *cg = (streamCG*)ri.data;
/* Save the group name. */ /* Save the group name. */
if ((n = rdbSaveRawString(rdb,ri.key,ri.key_len)) == -1) if ((n = rdbSaveRawString(rdb,ri.key,ri.key_len)) == -1) {
raxStop(&ri);
return -1; return -1;
}
nwritten += n; nwritten += n;
/* Last ID. */ /* Last ID. */
if ((n = rdbSaveLen(rdb,cg->last_id.ms)) == -1) return -1; if ((n = rdbSaveLen(rdb,cg->last_id.ms)) == -1) {
raxStop(&ri);
return -1;
}
nwritten += n; nwritten += n;
if ((n = rdbSaveLen(rdb,cg->last_id.seq)) == -1) return -1; if ((n = rdbSaveLen(rdb,cg->last_id.seq)) == -1) {
raxStop(&ri);
return -1;
}
nwritten += n; nwritten += n;
/* Save the global PEL. */ /* Save the global PEL. */
if ((n = rdbSaveStreamPEL(rdb,cg->pel,1)) == -1) return -1; if ((n = rdbSaveStreamPEL(rdb,cg->pel,1)) == -1) {
raxStop(&ri);
return -1;
}
nwritten += n; nwritten += n;
/* Save the consumers of this group. */ /* Save the consumers of this group. */
if ((n = rdbSaveStreamConsumers(rdb,cg)) == -1) return -1; if ((n = rdbSaveStreamConsumers(rdb,cg)) == -1) {
raxStop(&ri);
return -1;
}
nwritten += n; nwritten += n;
} }
raxStop(&ri); raxStop(&ri);
@ -1170,6 +1209,8 @@ ssize_t rdbSaveSingleModuleAux(rio *rdb, int when, moduleType *mt) {
/* Save a module-specific aux value. */ /* Save a module-specific aux value. */
RedisModuleIO io; RedisModuleIO io;
int retval = rdbSaveType(rdb, RDB_OPCODE_MODULE_AUX); int retval = rdbSaveType(rdb, RDB_OPCODE_MODULE_AUX);
if (retval == -1) return -1;
io.bytes += retval;
/* Write the "module" identifier as prefix, so that we'll be able /* Write the "module" identifier as prefix, so that we'll be able
* to call the right module during loading. */ * to call the right module during loading. */
@ -1230,7 +1271,7 @@ int rdbSaveRio(rio *rdb, int *error, int rdbflags, rdbSaveInfo *rsi) {
for (j = 0; j < cserver.dbnum; j++) { for (j = 0; j < cserver.dbnum; j++) {
redisDb *db = g_pserver->db+j; redisDb *db = g_pserver->db+j;
dict *d = db->pdict; dict *d = db->dict;
if (dictSize(d) == 0) continue; if (dictSize(d) == 0) continue;
di = dictGetSafeIterator(d); di = dictGetSafeIterator(d);
@ -1240,7 +1281,7 @@ int rdbSaveRio(rio *rdb, int *error, int rdbflags, rdbSaveInfo *rsi) {
/* Write the RESIZE DB opcode. */ /* Write the RESIZE DB opcode. */
uint64_t db_size, expires_size; uint64_t db_size, expires_size;
db_size = dictSize(db->pdict); db_size = dictSize(db->dict);
expires_size = db->setexpire->size(); expires_size = db->setexpire->size();
if (rdbSaveType(rdb,RDB_OPCODE_RESIZEDB) == -1) goto werr; if (rdbSaveType(rdb,RDB_OPCODE_RESIZEDB) == -1) goto werr;
if (rdbSaveLen(rdb,db_size) == -1) goto werr; if (rdbSaveLen(rdb,db_size) == -1) goto werr;
@ -1357,7 +1398,7 @@ int rdbSave(rdbSaveInfo *rsi)
int rdbSaveFile(char *filename, rdbSaveInfo *rsi) { int rdbSaveFile(char *filename, rdbSaveInfo *rsi) {
char tmpfile[256]; char tmpfile[256];
char cwd[MAXPATHLEN]; /* Current working dir path for error messages. */ char cwd[MAXPATHLEN]; /* Current working dir path for error messages. */
FILE *fp; FILE *fp = NULL;
rio rdb; rio rdb;
int error = 0; int error = 0;
@ -1386,10 +1427,11 @@ int rdbSaveFile(char *filename, rdbSaveInfo *rsi) {
} }
/* Make sure data will not remain on the OS's output buffers */ /* Make sure data will not remain on the OS's output buffers */
if (fflush(fp) == EOF) goto werr; if (fflush(fp)) goto werr;
if (fsync(fileno(fp)) == -1) goto werr; if (fsync(fileno(fp))) goto werr;
if (fclose(fp) == EOF) goto werr; if (fclose(fp)) { fp = NULL; goto werr; }
fp = NULL;
/* Use RENAME to make sure the DB file is changed atomically only /* Use RENAME to make sure the DB file is changed atomically only
* if the generate DB file is ok. */ * if the generate DB file is ok. */
if (rename(tmpfile,filename) == -1) { if (rename(tmpfile,filename) == -1) {
@ -1415,7 +1457,7 @@ int rdbSaveFile(char *filename, rdbSaveInfo *rsi) {
werr: werr:
serverLog(LL_WARNING,"Write error saving DB on disk: %s", strerror(errno)); serverLog(LL_WARNING,"Write error saving DB on disk: %s", strerror(errno));
fclose(fp); if (fp) fclose(fp);
unlink(tmpfile); unlink(tmpfile);
stopSaving(0); stopSaving(0);
return C_ERR; return C_ERR;
@ -1430,7 +1472,7 @@ int rdbSaveBackground(rdbSaveInfo *rsi) {
g_pserver->lastbgsave_try = time(NULL); g_pserver->lastbgsave_try = time(NULL);
openChildInfoPipe(); openChildInfoPipe();
if ((childpid = redisFork()) == 0) { if ((childpid = redisFork(CHILD_TYPE_RDB)) == 0) {
int retval; int retval;
/* Child */ /* Child */
@ -1438,7 +1480,7 @@ int rdbSaveBackground(rdbSaveInfo *rsi) {
redisSetCpuAffinity(g_pserver->bgsave_cpulist); redisSetCpuAffinity(g_pserver->bgsave_cpulist);
retval = rdbSave(rsi); retval = rdbSave(rsi);
if (retval == C_OK) { if (retval == C_OK) {
sendChildCOWInfo(CHILD_INFO_TYPE_RDB, "RDB"); sendChildCOWInfo(CHILD_TYPE_RDB, "RDB");
} }
exitFromChild((retval == C_OK) ? 0 : 1); exitFromChild((retval == C_OK) ? 0 : 1);
} else { } else {
@ -1454,16 +1496,35 @@ int rdbSaveBackground(rdbSaveInfo *rsi) {
g_pserver->rdb_save_time_start = time(NULL); g_pserver->rdb_save_time_start = time(NULL);
g_pserver->rdb_child_pid = childpid; g_pserver->rdb_child_pid = childpid;
g_pserver->rdb_child_type = RDB_CHILD_TYPE_DISK; g_pserver->rdb_child_type = RDB_CHILD_TYPE_DISK;
updateDictResizePolicy();
return C_OK; return C_OK;
} }
return C_OK; /* unreached */ return C_OK; /* unreached */
} }
void rdbRemoveTempFile(pid_t childpid) { /* Note that we may call this function in signal handle 'sigShutdownHandler',
* so we need guarantee all functions we call are async-signal-safe.
* If we call this function from signal handle, we won't call bg_unlik that
* is not async-signal-safe. */
void rdbRemoveTempFile(pid_t childpid, int from_signal) {
char tmpfile[256]; char tmpfile[256];
char pid[32];
snprintf(tmpfile,sizeof(tmpfile),"temp-%d.rdb", (int) childpid); /* Generate temp rdb file name using aync-signal safe functions. */
unlink(tmpfile); int pid_len = ll2string(pid, sizeof(pid), childpid);
strcpy(tmpfile, "temp-");
strncpy(tmpfile+5, pid, pid_len);
strcpy(tmpfile+5+pid_len, ".rdb");
if (from_signal) {
/* bg_unlink is not async-signal-safe, but in this case we don't really
* need to close the fd, it'll be released when the process exists. */
int fd = open(tmpfile, O_RDONLY|O_NONBLOCK);
UNUSED(fd);
unlink(tmpfile);
} else {
bg_unlink(tmpfile);
}
} }
/* This function is called by rdbLoadObject() when the code is in RDB-check /* This function is called by rdbLoadObject() when the code is in RDB-check
@ -1590,7 +1651,7 @@ robj *rdbLoadObject(int rdbtype, rio *rdb, sds key, uint64_t mvcc_tstamp) {
zs = (zset*)ptrFromObj(o); zs = (zset*)ptrFromObj(o);
if (zsetlen > DICT_HT_INITIAL_SIZE) if (zsetlen > DICT_HT_INITIAL_SIZE)
dictExpand(zs->pdict,zsetlen); dictExpand(zs->dict,zsetlen);
/* Load every single element of the sorted set. */ /* Load every single element of the sorted set. */
while(zsetlen--) { while(zsetlen--) {
@ -1621,7 +1682,7 @@ robj *rdbLoadObject(int rdbtype, rio *rdb, sds key, uint64_t mvcc_tstamp) {
if (sdslen(sdsele) > maxelelen) maxelelen = sdslen(sdsele); if (sdslen(sdsele) > maxelelen) maxelelen = sdslen(sdsele);
znode = zslInsert(zs->zsl,score,sdsele); znode = zslInsert(zs->zsl,score,sdsele);
dictAdd(zs->pdict,sdsele,&znode->score); dictAdd(zs->dict,sdsele,&znode->score);
} }
/* Convert *after* loading, since sorted sets are not stored ordered. */ /* Convert *after* loading, since sorted sets are not stored ordered. */
@ -2130,8 +2191,11 @@ void stopSaving(int success) {
void rdbLoadProgressCallback(rio *r, const void *buf, size_t len) { void rdbLoadProgressCallback(rio *r, const void *buf, size_t len) {
if (g_pserver->rdb_checksum) if (g_pserver->rdb_checksum)
rioGenericUpdateChecksum(r, buf, len); rioGenericUpdateChecksum(r, buf, len);
if (g_pserver->loading_process_events_interval_bytes &&
(r->processed_bytes + len)/g_pserver->loading_process_events_interval_bytes > r->processed_bytes/g_pserver->loading_process_events_interval_bytes) if ((g_pserver->loading_process_events_interval_bytes &&
(r->processed_bytes + len)/g_pserver->loading_process_events_interval_bytes > r->processed_bytes/g_pserver->loading_process_events_interval_bytes) ||
(g_pserver->loading_process_events_interval_keys &&
(r->keys_since_last_callback >= g_pserver->loading_process_events_interval_keys)))
{ {
/* The DB can take some non trivial amount of time to load. Update /* The DB can take some non trivial amount of time to load. Update
* our cached time since it is used to create and update the last * our cached time since it is used to create and update the last
@ -2149,6 +2213,14 @@ void rdbLoadProgressCallback(rio *r, const void *buf, size_t len) {
loadingProgress(r->processed_bytes); loadingProgress(r->processed_bytes);
processEventsWhileBlocked(serverTL - g_pserver->rgthreadvar); processEventsWhileBlocked(serverTL - g_pserver->rgthreadvar);
processModuleLoadingProgressEvent(0); processModuleLoadingProgressEvent(0);
robj *ping_argv[1];
ping_argv[0] = createStringObject("PING",4);
replicationFeedSlaves(g_pserver->slaves, g_pserver->replicaseldb, ping_argv, 1);
decrRefCount(ping_argv[0]);
r->keys_since_last_callback = 0;
} }
} }
@ -2242,12 +2314,12 @@ int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
goto eoferr; goto eoferr;
if ((expires_size = rdbLoadLen(rdb,NULL)) == RDB_LENERR) if ((expires_size = rdbLoadLen(rdb,NULL)) == RDB_LENERR)
goto eoferr; goto eoferr;
dictExpand(db->pdict,db_size); dictExpand(db->dict,db_size);
continue; /* Read next opcode. */ continue; /* Read next opcode. */
} else if (type == RDB_OPCODE_AUX) { } else if (type == RDB_OPCODE_AUX) {
/* AUX: generic string-string fields. Use to add state to RDB /* AUX: generic string-string fields. Use to add state to RDB
* which is backward compatible. Implementations of RDB loading * which is backward compatible. Implementations of RDB loading
* are requierd to skip AUX fields they don't understand. * are required to skip AUX fields they don't understand.
* *
* An AUX field is composed of two strings: key and value. */ * An AUX field is composed of two strings: key and value. */
robj *auxkey, *auxval; robj *auxkey, *auxval;
@ -2275,7 +2347,7 @@ int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
if (luaCreateFunction(NULL,g_pserver->lua,auxval) == NULL) { if (luaCreateFunction(NULL,g_pserver->lua,auxval) == NULL) {
rdbExitReportCorruptRDB( rdbExitReportCorruptRDB(
"Can't load Lua script from RDB file! " "Can't load Lua script from RDB file! "
"BODY: %s", ptrFromObj(auxval)); "BODY: %s", (char*)ptrFromObj(auxval));
} }
} else if (!strcasecmp(szFromObj(auxkey),"redis-ver")) { } else if (!strcasecmp(szFromObj(auxkey),"redis-ver")) {
serverLog(LL_NOTICE,"Loading RDB produced by version %s", serverLog(LL_NOTICE,"Loading RDB produced by version %s",
@ -2318,7 +2390,9 @@ int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
else { else {
redisObjectStack keyobj; redisObjectStack keyobj;
initStaticStringObject(keyobj,key); initStaticStringObject(keyobj,key);
setExpire(NULL, db, &keyobj, subexpireKey, strtoll(szFromObj(auxval), nullptr, 10)); long long expireT = strtoll(szFromObj(auxval), nullptr, 10);
setExpire(NULL, db, &keyobj, subexpireKey, expireT);
replicateSubkeyExpire(db, &keyobj, subexpireKey, expireT);
decrRefCount(subexpireKey); decrRefCount(subexpireKey);
subexpireKey = nullptr; subexpireKey = nullptr;
} }
@ -2437,6 +2511,11 @@ int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
/* Set usage information (for eviction). */ /* Set usage information (for eviction). */
objectSetLRUOrLFU(val,lfu_freq,lru_idle,lru_clock,1000); objectSetLRUOrLFU(val,lfu_freq,lru_idle,lru_clock,1000);
/* call key space notification on key loaded for modules only */
moduleNotifyKeyspaceEvent(NOTIFY_LOADED, "loaded", &keyobj, db->id);
replicationNotifyLoadedKey(db, &keyobj, val, expiretime);
} }
else else
{ {
@ -2448,6 +2527,8 @@ int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
if (g_pserver->key_load_delay) if (g_pserver->key_load_delay)
usleep(g_pserver->key_load_delay); usleep(g_pserver->key_load_delay);
rdb->keys_since_last_callback++;
/* Reset the state that is key-specified and is populated by /* Reset the state that is key-specified and is populated by
* opcodes before the key, so that we start from scratch again. */ * opcodes before the key, so that we start from scratch again. */
expiretime = -1; expiretime = -1;
@ -2545,7 +2626,7 @@ int rdbLoadFile(const char *filename, rdbSaveInfo *rsi, int rdbflags) {
/* A background saving child (BGSAVE) terminated its work. Handle this. /* A background saving child (BGSAVE) terminated its work. Handle this.
* This function covers the case of actual BGSAVEs. */ * This function covers the case of actual BGSAVEs. */
void backgroundSaveDoneHandlerDisk(int exitcode, int bysignal) { static void backgroundSaveDoneHandlerDisk(int exitcode, int bysignal) {
if (!bysignal && exitcode == 0) { if (!bysignal && exitcode == 0) {
serverLog(LL_NOTICE, serverLog(LL_NOTICE,
"Background saving terminated with success"); "Background saving terminated with success");
@ -2561,27 +2642,20 @@ void backgroundSaveDoneHandlerDisk(int exitcode, int bysignal) {
serverLog(LL_WARNING, serverLog(LL_WARNING,
"Background saving terminated by signal %d", bysignal); "Background saving terminated by signal %d", bysignal);
latencyStartMonitor(latency); latencyStartMonitor(latency);
rdbRemoveTempFile(g_pserver->rdb_child_pid); rdbRemoveTempFile(g_pserver->rdb_child_pid, 0);
latencyEndMonitor(latency); latencyEndMonitor(latency);
latencyAddSampleIfNeeded("rdb-unlink-temp-file",latency); latencyAddSampleIfNeeded("rdb-unlink-temp-file",latency);
/* SIGUSR1 is whitelisted, so we have a way to kill a child without /* SIGUSR1 is whitelisted, so we have a way to kill a child without
* tirggering an error condition. */ * triggering an error condition. */
if (bysignal != SIGUSR1) if (bysignal != SIGUSR1)
g_pserver->lastbgsave_status = C_ERR; g_pserver->lastbgsave_status = C_ERR;
} }
g_pserver->rdb_child_pid = -1;
g_pserver->rdb_child_type = RDB_CHILD_TYPE_NONE;
g_pserver->rdb_save_time_last = time(NULL)-g_pserver->rdb_save_time_start;
g_pserver->rdb_save_time_start = -1;
/* Possibly there are slaves waiting for a BGSAVE in order to be served
* (the first stage of SYNC is a bulk transfer of dump.rdb) */
updateSlavesWaitingBgsave((!bysignal && exitcode == 0) ? C_OK : C_ERR, RDB_CHILD_TYPE_DISK);
} }
/* A background saving child (BGSAVE) terminated its work. Handle this. /* A background saving child (BGSAVE) terminated its work. Handle this.
* This function covers the case of RDB -> Slaves socket transfers for * This function covers the case of RDB -> Slaves socket transfers for
* diskless replication. */ * diskless replication. */
void backgroundSaveDoneHandlerSocket(int exitcode, int bysignal) { static void backgroundSaveDoneHandlerSocket(int exitcode, int bysignal) {
serverAssert(GlobalLocksAcquired()); serverAssert(GlobalLocksAcquired());
if (!bysignal && exitcode == 0) { if (!bysignal && exitcode == 0) {
@ -2593,15 +2667,11 @@ void backgroundSaveDoneHandlerSocket(int exitcode, int bysignal) {
serverLog(LL_WARNING, serverLog(LL_WARNING,
"Background transfer terminated by signal %d", bysignal); "Background transfer terminated by signal %d", bysignal);
} }
g_pserver->rdb_child_pid = -1;
g_pserver->rdb_child_type = RDB_CHILD_TYPE_NONE;
g_pserver->rdb_save_time_start = -1;
updateSlavesWaitingBgsave((!bysignal && exitcode == 0) ? C_OK : C_ERR, RDB_CHILD_TYPE_SOCKET);
} }
/* When a background RDB saving/transfer terminates, call the right handler. */ /* When a background RDB saving/transfer terminates, call the right handler. */
void backgroundSaveDoneHandler(int exitcode, int bysignal) { void backgroundSaveDoneHandler(int exitcode, int bysignal) {
int type = g_pserver->rdb_child_type;
switch(g_pserver->rdb_child_type) { switch(g_pserver->rdb_child_type) {
case RDB_CHILD_TYPE_DISK: case RDB_CHILD_TYPE_DISK:
backgroundSaveDoneHandlerDisk(exitcode,bysignal); backgroundSaveDoneHandlerDisk(exitcode,bysignal);
@ -2613,6 +2683,14 @@ void backgroundSaveDoneHandler(int exitcode, int bysignal) {
serverPanic("Unknown RDB child type."); serverPanic("Unknown RDB child type.");
break; break;
} }
g_pserver->rdb_child_pid = -1;
g_pserver->rdb_child_type = RDB_CHILD_TYPE_NONE;
g_pserver->rdb_save_time_last = time(NULL)-g_pserver->rdb_save_time_start;
g_pserver->rdb_save_time_start = -1;
/* Possibly there are slaves waiting for a BGSAVE in order to be served
* (the first stage of SYNC is a bulk transfer of dump.rdb) */
updateSlavesWaitingBgsave((!bysignal && exitcode == 0) ? C_OK : C_ERR, type);
} }
/* Kill the RDB saving child using SIGUSR1 (so that the parent will know /* Kill the RDB saving child using SIGUSR1 (so that the parent will know
@ -2620,7 +2698,7 @@ void backgroundSaveDoneHandler(int exitcode, int bysignal) {
* the cleanup needed. */ * the cleanup needed. */
void killRDBChild(void) { void killRDBChild(void) {
kill(g_pserver->rdb_child_pid,SIGUSR1); kill(g_pserver->rdb_child_pid,SIGUSR1);
rdbRemoveTempFile(g_pserver->rdb_child_pid); rdbRemoveTempFile(g_pserver->rdb_child_pid, 0);
closeChildInfoPipe(); closeChildInfoPipe();
updateDictResizePolicy(); updateDictResizePolicy();
} }
@ -2665,7 +2743,7 @@ int rdbSaveToSlavesSockets(rdbSaveInfo *rsi) {
/* Create the child process. */ /* Create the child process. */
openChildInfoPipe(); openChildInfoPipe();
if ((childpid = redisFork()) == 0) { if ((childpid = redisFork(CHILD_TYPE_RDB)) == 0) {
/* Child */ /* Child */
int retval; int retval;
rio rdb; rio rdb;
@ -2680,7 +2758,7 @@ int rdbSaveToSlavesSockets(rdbSaveInfo *rsi) {
retval = C_ERR; retval = C_ERR;
if (retval == C_OK) { if (retval == C_OK) {
sendChildCOWInfo(CHILD_INFO_TYPE_RDB, "RDB"); sendChildCOWInfo(CHILD_TYPE_RDB, "RDB");
} }
rioFreeFd(&rdb); rioFreeFd(&rdb);
@ -2715,6 +2793,7 @@ int rdbSaveToSlavesSockets(rdbSaveInfo *rsi) {
g_pserver->rdb_save_time_start = time(NULL); g_pserver->rdb_save_time_start = time(NULL);
g_pserver->rdb_child_pid = childpid; g_pserver->rdb_child_pid = childpid;
g_pserver->rdb_child_type = RDB_CHILD_TYPE_SOCKET; g_pserver->rdb_child_type = RDB_CHILD_TYPE_SOCKET;
updateDictResizePolicy();
close(g_pserver->rdb_pipe_write); /* close write in parent so that it can detect the close on the child. */ close(g_pserver->rdb_pipe_write); /* close write in parent so that it can detect the close on the child. */
aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, []{ aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, []{
if (aeCreateFileEvent(serverTL->el, g_pserver->rdb_pipe_read, AE_READABLE, rdbPipeReadHandler,NULL) == AE_ERR) { if (aeCreateFileEvent(serverTL->el, g_pserver->rdb_pipe_read, AE_READABLE, rdbPipeReadHandler,NULL) == AE_ERR) {

View File

@ -145,7 +145,7 @@ int rdbLoad(rdbSaveInfo *rsi, int rdbflags);
int rdbLoadFile(const char *filename, rdbSaveInfo *rsi, int rdbflags); int rdbLoadFile(const char *filename, rdbSaveInfo *rsi, int rdbflags);
int rdbSaveBackground(rdbSaveInfo *rsi); int rdbSaveBackground(rdbSaveInfo *rsi);
int rdbSaveToSlavesSockets(rdbSaveInfo *rsi); int rdbSaveToSlavesSockets(rdbSaveInfo *rsi);
void rdbRemoveTempFile(pid_t childpid); void rdbRemoveTempFile(pid_t childpid, int from_signal);
int rdbSave(rdbSaveInfo *rsi); int rdbSave(rdbSaveInfo *rsi);
int rdbSaveFile(char *filename, rdbSaveInfo *rsi); int rdbSaveFile(char *filename, rdbSaveInfo *rsi);
int rdbSaveFp(FILE *pf, rdbSaveInfo *rsi); int rdbSaveFp(FILE *pf, rdbSaveInfo *rsi);
@ -157,6 +157,7 @@ robj *rdbLoadObject(int type, rio *rdb, sds key, uint64_t mvcc_tstamp);
void backgroundSaveDoneHandler(int exitcode, int bysignal); void backgroundSaveDoneHandler(int exitcode, int bysignal);
int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, long long expiretime); int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, long long expiretime);
ssize_t rdbSaveSingleModuleAux(rio *rdb, int when, moduleType *mt); ssize_t rdbSaveSingleModuleAux(rio *rdb, int when, moduleType *mt);
robj *rdbLoadCheckModuleValue(rio *rdb, char *modulename);
robj *rdbLoadStringObject(rio *rdb); robj *rdbLoadStringObject(rio *rdb);
ssize_t rdbSaveStringObject(rio *rdb, robj_roptr obj); ssize_t rdbSaveStringObject(rio *rdb, robj_roptr obj);
ssize_t rdbSaveRawString(rio *rdb, const unsigned char *s, size_t len); ssize_t rdbSaveRawString(rio *rdb, const unsigned char *s, size_t len);

View File

@ -1,4 +1,4 @@
/* Redis benchmark utility. /* Redis benchmark utility.
* *
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com> * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved. * All rights reserved.
@ -189,6 +189,8 @@ static void *execBenchmarkThread(void *ptr);
static clusterNode *createClusterNode(char *ip, int port); static clusterNode *createClusterNode(char *ip, int port);
static redisConfig *getRedisConfig(const char *ip, int port, static redisConfig *getRedisConfig(const char *ip, int port,
const char *hostsocket); const char *hostsocket);
static redisContext *getRedisContext(const char *ip, int port,
const char *hostsocket);
static void freeRedisConfig(redisConfig *cfg); static void freeRedisConfig(redisConfig *cfg);
static int fetchClusterSlotsConfiguration(client c); static int fetchClusterSlotsConfiguration(client c);
static void updateClusterSlotsConfiguration(); static void updateClusterSlotsConfiguration();
@ -244,46 +246,69 @@ extern "C" void _serverAssert(const char *estr, const char *file, int line) {
*((char*)-1) = 'x'; *((char*)-1) = 'x';
} }
static redisContext *getRedisContext(const char *ip, int port,
const char *hostsocket)
{
redisContext *ctx = NULL;
redisReply *reply = NULL;
if (hostsocket == NULL)
ctx = redisConnect(ip, port);
else
ctx = redisConnectUnix(hostsocket);
if (ctx == NULL || ctx->err) {
fprintf(stderr,"Could not connect to Redis at ");
const char *err = (ctx != NULL ? ctx->errstr : "");
if (hostsocket == NULL)
fprintf(stderr,"%s:%d: %s\n",ip,port,err);
else
fprintf(stderr,"%s: %s\n",hostsocket,err);
goto cleanup;
}
if (config.auth == NULL)
return ctx;
if (config.user == NULL)
reply = (redisReply*)redisCommand(ctx,"AUTH %s", config.auth);
else
reply = (redisReply*)redisCommand(ctx,"AUTH %s %s", config.user, config.auth);
if (reply != NULL) {
if (reply->type == REDIS_REPLY_ERROR) {
if (hostsocket == NULL)
fprintf(stderr, "Node %s:%d replied with error:\n%s\n", ip, port, reply->str);
else
fprintf(stderr, "Node %s replied with error:\n%s\n", hostsocket, reply->str);
goto cleanup;
}
freeReplyObject(reply);
return ctx;
}
fprintf(stderr, "ERROR: failed to fetch reply from ");
if (hostsocket == NULL)
fprintf(stderr, "%s:%d\n", ip, port);
else
fprintf(stderr, "%s\n", hostsocket);
cleanup:
freeReplyObject(reply);
redisFree(ctx);
return NULL;
}
static redisConfig *getRedisConfig(const char *ip, int port, static redisConfig *getRedisConfig(const char *ip, int port,
const char *hostsocket) const char *hostsocket)
{ {
redisConfig *cfg = (redisConfig*)zcalloc(sizeof(*cfg), MALLOC_LOCAL); redisConfig *cfg = (redisConfig*)zcalloc(sizeof(*cfg));
int i = 0;
void *r = NULL;
if (!cfg) return NULL; if (!cfg) return NULL;
redisContext *c = NULL; redisContext *c = NULL;
redisReply *reply = NULL, *sub_reply = NULL; redisReply *reply = NULL, *sub_reply = NULL;
if (hostsocket == NULL) c = getRedisContext(ip, port, hostsocket);
c = redisConnect(ip, port); if (c == NULL) {
else freeRedisConfig(cfg);
c = redisConnectUnix(hostsocket); return NULL;
if (c == NULL || c->err) {
fprintf(stderr,"Could not connect to Redis at ");
const char *err = (c != NULL ? c->errstr : "");
if (hostsocket == NULL) fprintf(stderr,"%s:%d: %s\n",ip,port,err);
else fprintf(stderr,"%s: %s\n",hostsocket,err);
goto fail;
} }
if(config.auth) {
void *authReply = NULL;
if (config.user == NULL)
redisAppendCommand(c, "AUTH %s", config.auth);
else
redisAppendCommand(c, "AUTH %s %s", config.user, config.auth);
if (REDIS_OK != redisGetReply(c, &authReply)) goto fail;
if (reply) freeReplyObject(reply);
reply = ((redisReply *) authReply);
if (reply->type == REDIS_REPLY_ERROR) {
fprintf(stderr, "ERROR: %s\n", reply->str);
goto fail;
}
}
redisAppendCommand(c, "CONFIG GET %s", "save"); redisAppendCommand(c, "CONFIG GET %s", "save");
redisAppendCommand(c, "CONFIG GET %s", "appendonly"); redisAppendCommand(c, "CONFIG GET %s", "appendonly");
for (; i < 2; i++) { void *r;
for (int i=0; i < 2; i++) {
int res = redisGetReply(c, &r); int res = redisGetReply(c, &r);
if (reply) freeReplyObject(reply); if (reply) freeReplyObject(reply);
reply = res == REDIS_OK ? ((redisReply *) r) : NULL; reply = res == REDIS_OK ? ((redisReply *) r) : NULL;
@ -740,7 +765,7 @@ static client createClient(const char *cmd, size_t len, client from, int thread_
} }
c->stagptr[c->staglen++] = p; c->stagptr[c->staglen++] = p;
c->stagfree--; c->stagfree--;
p += 5; /* 12 is strlen("{tag}"). */ p += 5; /* 5 is strlen("{tag}"). */
} }
} }
} }
@ -1001,17 +1026,11 @@ static int fetchClusterConfiguration() {
int success = 1; int success = 1;
redisContext *ctx = NULL; redisContext *ctx = NULL;
redisReply *reply = NULL; redisReply *reply = NULL;
char *lines = reply->str, *p, *line; char *lines = NULL;
if (config.hostsocket == NULL) char *line = NULL;
ctx = redisConnect(config.hostip,config.hostport); char *p = NULL;
else ctx = getRedisContext(config.hostip, config.hostport, config.hostsocket);
ctx = redisConnectUnix(config.hostsocket); if (ctx == NULL) {
if (ctx->err) {
fprintf(stderr,"Could not connect to Redis at ");
if (config.hostsocket == NULL) {
fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,
ctx->errstr);
} else fprintf(stderr,"%s: %s\n",config.hostsocket,ctx->errstr);
exit(1); exit(1);
} }
clusterNode *firstNode = createClusterNode((char *) config.hostip, clusterNode *firstNode = createClusterNode((char *) config.hostip,
@ -1207,11 +1226,9 @@ static int fetchClusterSlotsConfiguration(client c) {
assert(node->port); assert(node->port);
/* Use first node as entry point to connect to. */ /* Use first node as entry point to connect to. */
if (ctx == NULL) { if (ctx == NULL) {
ctx = redisConnect(node->ip, node->port); ctx = getRedisContext(node->ip, node->port, NULL);
if (!ctx || ctx->err) { if (!ctx) {
success = 0; success = 0;
if (ctx && ctx->err)
fprintf(stderr, "REDIS CONNECTION ERROR: %s\n", ctx->errstr);
goto cleanup; goto cleanup;
} }
} }
@ -1425,7 +1442,8 @@ usage:
" --cluster Enable cluster mode.\n" " --cluster Enable cluster mode.\n"
" --enable-tracking Send CLIENT TRACKING on before starting benchmark.\n" " --enable-tracking Send CLIENT TRACKING on before starting benchmark.\n"
" -k <boolean> 1=keep alive 0=reconnect (default 1)\n" " -k <boolean> 1=keep alive 0=reconnect (default 1)\n"
" -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD\n" " -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD,\n"
" random members and scores for ZADD.\n"
" Using this option the benchmark will expand the string __rand_int__\n" " Using this option the benchmark will expand the string __rand_int__\n"
" inside an argument with a 12 digits number in the specified range\n" " inside an argument with a 12 digits number in the specified range\n"
" from 0 to keyspacelen-1. The substitution changes every time a command\n" " from 0 to keyspacelen-1. The substitution changes every time a command\n"
@ -1507,6 +1525,7 @@ int test_is_selected(const char *name) {
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
int i; int i;
char *data, *cmd; char *data, *cmd;
const char *tag;
int len; int len;
client c; client c;
@ -1558,7 +1577,12 @@ int main(int argc, const char **argv) {
config.latency = (long long*)zmalloc(sizeof(long long)*config.requests, MALLOC_LOCAL); config.latency = (long long*)zmalloc(sizeof(long long)*config.requests, MALLOC_LOCAL);
tag = "";
if (config.cluster_mode) { if (config.cluster_mode) {
// We only include the slot placeholder {tag} if cluster mode is enabled
tag = ":{tag}";
/* Fetch cluster configuration. */ /* Fetch cluster configuration. */
if (!fetchClusterConfiguration() || !config.cluster_nodes) { if (!fetchClusterConfiguration() || !config.cluster_nodes) {
if (!config.hostsocket) { if (!config.hostsocket) {
@ -1672,112 +1696,129 @@ int main(int argc, const char **argv) {
} }
if (test_is_selected("set")) { if (test_is_selected("set")) {
len = redisFormatCommand(&cmd,"SET key:{tag}:__rand_int__ %s",data); len = redisFormatCommand(&cmd,"SET key%s:__rand_int__ %s",tag,data);
benchmark("SET",cmd,len); benchmark("SET",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("get")) { if (test_is_selected("get")) {
len = redisFormatCommand(&cmd,"GET key:{tag}:__rand_int__"); len = redisFormatCommand(&cmd,"GET key%s:__rand_int__",tag);
benchmark("GET",cmd,len); benchmark("GET",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("incr")) { if (test_is_selected("incr")) {
len = redisFormatCommand(&cmd,"INCR counter:{tag}:__rand_int__"); len = redisFormatCommand(&cmd,"INCR counter%s:__rand_int__",tag);
benchmark("INCR",cmd,len); benchmark("INCR",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("lpush")) { if (test_is_selected("lpush")) {
len = redisFormatCommand(&cmd,"LPUSH mylist:{tag} %s",data); len = redisFormatCommand(&cmd,"LPUSH mylist%s %s",tag,data);
benchmark("LPUSH",cmd,len); benchmark("LPUSH",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("rpush")) { if (test_is_selected("rpush")) {
len = redisFormatCommand(&cmd,"RPUSH mylist:{tag} %s",data); len = redisFormatCommand(&cmd,"RPUSH mylist%s %s",tag,data);
benchmark("RPUSH",cmd,len); benchmark("RPUSH",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("lpop")) { if (test_is_selected("lpop")) {
len = redisFormatCommand(&cmd,"LPOP mylist:{tag}"); len = redisFormatCommand(&cmd,"LPOP mylist%s",tag);
benchmark("LPOP",cmd,len); benchmark("LPOP",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("rpop")) { if (test_is_selected("rpop")) {
len = redisFormatCommand(&cmd,"RPOP mylist:{tag}"); len = redisFormatCommand(&cmd,"RPOP mylist%s",tag);
benchmark("RPOP",cmd,len); benchmark("RPOP",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("sadd")) { if (test_is_selected("sadd")) {
len = redisFormatCommand(&cmd, len = redisFormatCommand(&cmd,
"SADD myset:{tag} element:__rand_int__"); "SADD myset%s element:__rand_int__",tag);
benchmark("SADD",cmd,len); benchmark("SADD",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("hset")) { if (test_is_selected("hset")) {
len = redisFormatCommand(&cmd, len = redisFormatCommand(&cmd,
"HSET myhash:{tag}:__rand_int__ element:__rand_int__ %s",data); "HSET myhash%s element:__rand_int__ %s",tag,data);
benchmark("HSET",cmd,len); benchmark("HSET",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("spop")) { if (test_is_selected("spop")) {
len = redisFormatCommand(&cmd,"SPOP myset:{tag}"); len = redisFormatCommand(&cmd,"SPOP myset%s",tag);
benchmark("SPOP",cmd,len); benchmark("SPOP",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("zadd")) {
const char *score = "0";
if (config.randomkeys) score = "__rand_int__";
len = redisFormatCommand(&cmd,
"ZADD myzset%s %s element:__rand_int__",tag,score);
benchmark("ZADD",cmd,len);
free(cmd);
}
if (test_is_selected("zpopmin")) {
len = redisFormatCommand(&cmd,"ZPOPMIN myzset%s",tag);
benchmark("ZPOPMIN",cmd,len);
free(cmd);
}
if (test_is_selected("lrange") || if (test_is_selected("lrange") ||
test_is_selected("lrange_100") || test_is_selected("lrange_100") ||
test_is_selected("lrange_300") || test_is_selected("lrange_300") ||
test_is_selected("lrange_500") || test_is_selected("lrange_500") ||
test_is_selected("lrange_600")) test_is_selected("lrange_600"))
{ {
len = redisFormatCommand(&cmd,"LPUSH mylist:{tag} %s",data); len = redisFormatCommand(&cmd,"LPUSH mylist%s %s",tag,data);
benchmark("LPUSH (needed to benchmark LRANGE)",cmd,len); benchmark("LPUSH (needed to benchmark LRANGE)",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("lrange") || test_is_selected("lrange_100")) { if (test_is_selected("lrange") || test_is_selected("lrange_100")) {
len = redisFormatCommand(&cmd,"LRANGE mylist:{tag} 0 99"); len = redisFormatCommand(&cmd,"LRANGE mylist%s 0 99",tag);
benchmark("LRANGE_100 (first 100 elements)",cmd,len); benchmark("LRANGE_100 (first 100 elements)",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("lrange") || test_is_selected("lrange_300")) { if (test_is_selected("lrange") || test_is_selected("lrange_300")) {
len = redisFormatCommand(&cmd,"LRANGE mylist:{tag} 0 299"); len = redisFormatCommand(&cmd,"LRANGE mylist%s 0 299",tag);
benchmark("LRANGE_300 (first 300 elements)",cmd,len); benchmark("LRANGE_300 (first 300 elements)",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("lrange") || test_is_selected("lrange_500")) { if (test_is_selected("lrange") || test_is_selected("lrange_500")) {
len = redisFormatCommand(&cmd,"LRANGE mylist:{tag} 0 449"); len = redisFormatCommand(&cmd,"LRANGE mylist%s 0 449",tag);
benchmark("LRANGE_500 (first 450 elements)",cmd,len); benchmark("LRANGE_500 (first 450 elements)",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("lrange") || test_is_selected("lrange_600")) { if (test_is_selected("lrange") || test_is_selected("lrange_600")) {
len = redisFormatCommand(&cmd,"LRANGE mylist:{tag} 0 599"); len = redisFormatCommand(&cmd,"LRANGE mylist%s 0 599",tag);
benchmark("LRANGE_600 (first 600 elements)",cmd,len); benchmark("LRANGE_600 (first 600 elements)",cmd,len);
free(cmd); free(cmd);
} }
if (test_is_selected("mset")) { if (test_is_selected("mset")) {
const char *argv[21]; const char *cmd_argv[21];
argv[0] = "MSET"; cmd_argv[0] = "MSET";
sds key_placeholder = sdscatprintf(sdsnew(""),"key%s:__rand_int__",tag);
for (i = 1; i < 21; i += 2) { for (i = 1; i < 21; i += 2) {
argv[i] = "key:{tag}:__rand_int__"; cmd_argv[i] = key_placeholder;
argv[i+1] = data; cmd_argv[i+1] = data;
} }
len = redisFormatCommandArgv(&cmd,21,argv,NULL); len = redisFormatCommandArgv(&cmd,21,cmd_argv,NULL);
benchmark("MSET (10 keys)",cmd,len); benchmark("MSET (10 keys)",cmd,len);
free(cmd); free(cmd);
sdsfree(key_placeholder);
} }
if (!config.csv) printf("\n"); if (!config.csv) printf("\n");

View File

@ -58,6 +58,7 @@ struct {
#define RDB_CHECK_DOING_CHECK_SUM 5 #define RDB_CHECK_DOING_CHECK_SUM 5
#define RDB_CHECK_DOING_READ_LEN 6 #define RDB_CHECK_DOING_READ_LEN 6
#define RDB_CHECK_DOING_READ_AUX 7 #define RDB_CHECK_DOING_READ_AUX 7
#define RDB_CHECK_DOING_READ_MODULE_AUX 8
const char *rdb_check_doing_string[] = { const char *rdb_check_doing_string[] = {
"start", "start",
@ -67,7 +68,8 @@ const char *rdb_check_doing_string[] = {
"read-object-value", "read-object-value",
"check-sum", "check-sum",
"read-len", "read-len",
"read-aux" "read-aux",
"read-module-aux"
}; };
const char *rdb_type_string[] = { const char *rdb_type_string[] = {
@ -272,6 +274,21 @@ int redis_check_rdb(const char *rdbfilename, FILE *fp) {
decrRefCount(auxkey); decrRefCount(auxkey);
decrRefCount(auxval); decrRefCount(auxval);
continue; /* Read type again. */ continue; /* Read type again. */
} else if (type == RDB_OPCODE_MODULE_AUX) {
/* AUX: Auxiliary data for modules. */
uint64_t moduleid, when_opcode, when;
rdbstate.doing = RDB_CHECK_DOING_READ_MODULE_AUX;
if ((moduleid = rdbLoadLen(&rdb,NULL)) == RDB_LENERR) goto eoferr;
if ((when_opcode = rdbLoadLen(&rdb,NULL)) == RDB_LENERR) goto eoferr;
if ((when = rdbLoadLen(&rdb,NULL)) == RDB_LENERR) goto eoferr;
char name[10];
moduleTypeNameByID(name,moduleid);
rdbCheckInfo("MODULE AUX for: %s", name);
robj *o = rdbLoadCheckModuleValue(&rdb,name);
decrRefCount(o);
continue; /* Read type again. */
} else { } else {
if (!rdbIsObjectType(type)) { if (!rdbIsObjectType(type)) {
rdbCheckError("Invalid object type: %d", type); rdbCheckError("Invalid object type: %d", type);
@ -331,7 +348,7 @@ err:
return 1; return 1;
} }
/* RDB check main: called form redis.c when Redis is executed with the /* RDB check main: called form server.c when Redis is executed with the
* keydb-check-rdb alias, on during RDB loading errors. * keydb-check-rdb alias, on during RDB loading errors.
* *
* The function works in two ways: can be called with argc/argv as a * The function works in two ways: can be called with argc/argv as a
@ -354,6 +371,7 @@ int redis_check_rdb_main(int argc, const char **argv, FILE *fp) {
if (shared.integers[0] == NULL) if (shared.integers[0] == NULL)
createSharedObjects(); createSharedObjects();
g_pserver->loading_process_events_interval_bytes = 0; g_pserver->loading_process_events_interval_bytes = 0;
g_pserver->loading_process_events_interval_keys = 0;
rdbCheckMode = 1; rdbCheckMode = 1;
rdbCheckInfo("Checking RDB file %s", argv[1]); rdbCheckInfo("Checking RDB file %s", argv[1]);
rdbCheckSetupSignals(); rdbCheckSetupSignals();

View File

@ -109,7 +109,6 @@ extern "C" void freeClusterManager(void) {
} }
/* This function returns a random master node, return NULL if none */ /* This function returns a random master node, return NULL if none */
static clusterManagerNode *clusterManagerNodeMasterRandom() { static clusterManagerNode *clusterManagerNodeMasterRandom() {
int master_count = 0; int master_count = 0;
int idx; int idx;
@ -140,7 +139,7 @@ static int clusterManagerFixSlotsCoverage(char *all_slots) {
int force_fix = config.cluster_manager_command.flags & int force_fix = config.cluster_manager_command.flags &
CLUSTER_MANAGER_CMD_FLAG_FIX_WITH_UNREACHABLE_MASTERS; CLUSTER_MANAGER_CMD_FLAG_FIX_WITH_UNREACHABLE_MASTERS;
/* we want explicit manual confirmation from users for all the fix cases */ /* we want explicit manual confirmation from users for all the fix cases */
int force = 0; int ignore_force = 1;
dictIterator *iter = nullptr; dictIterator *iter = nullptr;
@ -213,7 +212,7 @@ static int clusterManagerFixSlotsCoverage(char *all_slots) {
"across the cluster:\n"); "across the cluster:\n");
clusterManagerPrintSlotsList(none); clusterManagerPrintSlotsList(none);
if (confirmWithYes("Fix these slots by covering with a random node?", if (confirmWithYes("Fix these slots by covering with a random node?",
force)) { ignore_force)) {
listIter li; listIter li;
listNode *ln; listNode *ln;
listRewind(none, &li); listRewind(none, &li);
@ -240,7 +239,7 @@ static int clusterManagerFixSlotsCoverage(char *all_slots) {
printf("The following uncovered slots have keys in just one node:\n"); printf("The following uncovered slots have keys in just one node:\n");
clusterManagerPrintSlotsList(single); clusterManagerPrintSlotsList(single);
if (confirmWithYes("Fix these slots by covering with those nodes?", if (confirmWithYes("Fix these slots by covering with those nodes?",
force)) { ignore_force)) {
listIter li; listIter li;
listNode *ln; listNode *ln;
listRewind(single, &li); listRewind(single, &li);
@ -272,7 +271,7 @@ static int clusterManagerFixSlotsCoverage(char *all_slots) {
printf("The following uncovered slots have keys in multiple nodes:\n"); printf("The following uncovered slots have keys in multiple nodes:\n");
clusterManagerPrintSlotsList(multi); clusterManagerPrintSlotsList(multi);
if (confirmWithYes("Fix these slots by moving keys " if (confirmWithYes("Fix these slots by moving keys "
"into a single node?", force)) { "into a single node?", ignore_force)) {
listIter li; listIter li;
listNode *ln; listNode *ln;
listRewind(multi, &li); listRewind(multi, &li);
@ -336,6 +335,7 @@ cleanup:
return fixed; return fixed;
} }
/* Return the anti-affinity score, which is a measure of the amount of /* Return the anti-affinity score, which is a measure of the amount of
* violations of anti-affinity in the current cluster layout, that is, how * violations of anti-affinity in the current cluster layout, that is, how
* badly the masters and slaves are distributed in the different IP * badly the masters and slaves are distributed in the different IP
@ -759,7 +759,9 @@ static void getKeyTypes(dict *types_dict, redisReply *keys, typeinfo **types) {
/* Pipeline TYPE commands */ /* Pipeline TYPE commands */
for(i=0;i<keys->elements;i++) { for(i=0;i<keys->elements;i++) {
redisAppendCommand(context, "TYPE %s", keys->element[i]->str); const char* argv[] = {"TYPE", keys->element[i]->str};
size_t lens[] = {4, keys->element[i]->len};
redisAppendCommandArgv(context, 2, argv, lens);
} }
/* Retrieve types */ /* Retrieve types */
@ -856,20 +858,20 @@ void findBigKeys(int memkeys, unsigned memkeys_samples) {
sampled++; sampled++;
if(type->biggest<sizes[i]) { if(type->biggest<sizes[i]) {
printf(
"[%05.2f%%] Biggest %-6s found so far '%s' with %llu %s\n",
pct, type->name, keys->element[i]->str, sizes[i],
!memkeys? type->sizeunit: "bytes");
/* Keep track of biggest key name for this type */ /* Keep track of biggest key name for this type */
if (type->biggest_key) if (type->biggest_key)
sdsfree(type->biggest_key); sdsfree(type->biggest_key);
type->biggest_key = sdsnew(keys->element[i]->str); type->biggest_key = sdscatrepr(sdsempty(), keys->element[i]->str, keys->element[i]->len);
if(!type->biggest_key) { if(!type->biggest_key) {
fprintf(stderr, "Failed to allocate memory for key!\n"); fprintf(stderr, "Failed to allocate memory for key!\n");
exit(1); exit(1);
} }
printf(
"[%05.2f%%] Biggest %-6s found so far '%s' with %llu %s\n",
pct, type->name, type->biggest_key, sizes[i],
!memkeys? type->sizeunit: "bytes");
/* Keep track of the biggest size for this type */ /* Keep track of the biggest size for this type */
type->biggest = sizes[i]; type->biggest = sizes[i];
} }

View File

@ -146,7 +146,7 @@ static void cliRefreshPrompt(void) {
/* Return the name of the dotfile for the specified 'dotfilename'. /* Return the name of the dotfile for the specified 'dotfilename'.
* Normally it just concatenates user $HOME to the file specified * Normally it just concatenates user $HOME to the file specified
* in 'dotfilename'. However if the environment varialbe 'envoverride' * in 'dotfilename'. However if the environment variable 'envoverride'
* is set, its value is taken as the path. * is set, its value is taken as the path.
* *
* The function returns NULL (if the file is /dev/null or cannot be * The function returns NULL (if the file is /dev/null or cannot be
@ -220,12 +220,20 @@ static sds percentDecode(const char *pe, size_t len) {
static void parseRedisUri(const char *uri) { static void parseRedisUri(const char *uri) {
const char *scheme = "redis://"; const char *scheme = "redis://";
const char *tlsscheme = "rediss://";
const char *curr = uri; const char *curr = uri;
const char *end = uri + strlen(uri); const char *end = uri + strlen(uri);
const char *userinfo, *username, *port, *host, *path; const char *userinfo, *username, *port, *host, *path;
/* URI must start with a valid scheme. */ /* URI must start with a valid scheme. */
if (strncasecmp(scheme, curr, strlen(scheme))) { if (!strncasecmp(tlsscheme, curr, strlen(tlsscheme))) {
#ifdef USE_OPENSSL
config.tls = 1;
#else
fprintf(stderr,"rediss:// is only supported when redis-cli is compiled with OpenSSL\n");
exit(1);
#endif
} else if (strncasecmp(scheme, curr, strlen(scheme))) {
fprintf(stderr,"Invalid URI scheme\n"); fprintf(stderr,"Invalid URI scheme\n");
exit(1); exit(1);
} }
@ -1060,7 +1068,7 @@ static int cliReadReply(int output_raw_strings) {
} else { } else {
if (config.output == OUTPUT_RAW) { if (config.output == OUTPUT_RAW) {
out = cliFormatReplyRaw(reply); out = cliFormatReplyRaw(reply);
out = sdscat(out,"\n"); out = sdscatsds(out, config.cmd_delim);
} else if (config.output == OUTPUT_STANDARD) { } else if (config.output == OUTPUT_STANDARD) {
out = cliFormatReplyTTY(reply,""); out = cliFormatReplyTTY(reply,"");
} else if (config.output == OUTPUT_CSV) { } else if (config.output == OUTPUT_CSV) {
@ -1342,6 +1350,9 @@ static int parseOptions(int argc, char **argv) {
} else if (!strcmp(argv[i],"-d") && !lastarg) { } else if (!strcmp(argv[i],"-d") && !lastarg) {
sdsfree(config.mb_delim); sdsfree(config.mb_delim);
config.mb_delim = sdsnew(argv[++i]); config.mb_delim = sdsnew(argv[++i]);
} else if (!strcmp(argv[i],"-D") && !lastarg) {
sdsfree(config.cmd_delim);
config.cmd_delim = sdsnew(argv[++i]);
} else if (!strcmp(argv[i],"--verbose")) { } else if (!strcmp(argv[i],"--verbose")) {
config.verbose = 1; config.verbose = 1;
} else if (!strcmp(argv[i],"--cluster") && !lastarg) { } else if (!strcmp(argv[i],"--cluster") && !lastarg) {
@ -1354,6 +1365,12 @@ static int parseOptions(int argc, char **argv) {
i = j; i = j;
} else if (!strcmp(argv[i],"--cluster") && lastarg) { } else if (!strcmp(argv[i],"--cluster") && lastarg) {
usage(); usage();
} else if ((!strcmp(argv[i],"--cluster-only-masters"))) {
config.cluster_manager_command.flags |=
CLUSTER_MANAGER_CMD_FLAG_MASTERS_ONLY;
} else if ((!strcmp(argv[i],"--cluster-only-replicas"))) {
config.cluster_manager_command.flags |=
CLUSTER_MANAGER_CMD_FLAG_SLAVES_ONLY;
} else if (!strcmp(argv[i],"--cluster-replicas") && !lastarg) { } else if (!strcmp(argv[i],"--cluster-replicas") && !lastarg) {
config.cluster_manager_command.replicas = atoi(argv[++i]); config.cluster_manager_command.replicas = atoi(argv[++i]);
} else if (!strcmp(argv[i],"--cluster-master-id") && !lastarg) { } else if (!strcmp(argv[i],"--cluster-master-id") && !lastarg) {
@ -1435,6 +1452,8 @@ static int parseOptions(int argc, char **argv) {
printf("keydb-cli %s\n", version); printf("keydb-cli %s\n", version);
sdsfree(version); sdsfree(version);
exit(0); exit(0);
} else if (!strcmp(argv[i],"--no-motd")) {
config.disable_motd = 1;
} else if (!strcmp(argv[i],"-3")) { } else if (!strcmp(argv[i],"-3")) {
config.resp3 = 1; config.resp3 = 1;
} else if (CLUSTER_MANAGER_MODE() && argv[i][0] != '-') { } else if (CLUSTER_MANAGER_MODE() && argv[i][0] != '-') {
@ -1480,6 +1499,11 @@ static void parseEnv() {
if (auth != NULL && config.auth == NULL) { if (auth != NULL && config.auth == NULL) {
config.auth = auth; config.auth = auth;
} }
char *cluster_yes = getenv(REDIS_CLI_CLUSTER_YES_ENV);
if (cluster_yes != NULL && !strcmp(cluster_yes, "1")) {
config.cluster_manager_command.flags |= CLUSTER_MANAGER_CMD_FLAG_YES;
}
} }
static sds readArgFromStdin(void) { static sds readArgFromStdin(void) {
@ -1511,7 +1535,7 @@ static void usage(void) {
" -a <password> Password to use when connecting to the server.\n" " -a <password> Password to use when connecting to the server.\n"
" You can also use the " REDIS_CLI_AUTH_ENV " environment\n" " You can also use the " REDIS_CLI_AUTH_ENV " environment\n"
" variable to pass this password more safely\n" " variable to pass this password more safely\n"
" (if both are used, this argument takes predecence).\n" " (if both are used, this argument takes precedence).\n"
" --user <username> Used to send ACL style 'AUTH username pass'. Needs -a.\n" " --user <username> Used to send ACL style 'AUTH username pass'. Needs -a.\n"
" --pass <password> Alias of -a for consistency with the new --user option.\n" " --pass <password> Alias of -a for consistency with the new --user option.\n"
" --askpass Force user to input password with mask from STDIN.\n" " --askpass Force user to input password with mask from STDIN.\n"
@ -1524,7 +1548,8 @@ static void usage(void) {
" -n <db> Database number.\n" " -n <db> Database number.\n"
" -3 Start session in RESP3 protocol mode.\n" " -3 Start session in RESP3 protocol mode.\n"
" -x Read last argument from STDIN.\n" " -x Read last argument from STDIN.\n"
" -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n).\n" " -d <delimiter> Delimiter between response bulks for raw formatting (default: \\n).\n"
" -D <delimiter> Delimiter between responses for raw formatting (default: \\n).\n"
" -c Enable cluster mode (follow -ASK and -MOVED redirections).\n" " -c Enable cluster mode (follow -ASK and -MOVED redirections).\n"
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
" --tls Establish a secure TLS connection.\n" " --tls Establish a secure TLS connection.\n"
@ -1570,7 +1595,8 @@ static void usage(void) {
" --hotkeys Sample Redis keys looking for hot keys.\n" " --hotkeys Sample Redis keys looking for hot keys.\n"
" only works when maxmemory-policy is *lfu.\n" " only works when maxmemory-policy is *lfu.\n"
" --scan List all keys using the SCAN command.\n" " --scan List all keys using the SCAN command.\n"
" --pattern <pat> Useful with --scan to specify a SCAN pattern.\n" " --pattern <pat> Keys pattern when using the --scan, --bigkeys or --hotkeys\n"
" options (default: *).\n"
" --intrinsic-latency <sec> Run a test to measure intrinsic system latency.\n" " --intrinsic-latency <sec> Run a test to measure intrinsic system latency.\n"
" The test will run for the specified amount of seconds.\n" " The test will run for the specified amount of seconds.\n"
" --eval <file> Send an EVAL command using the Lua script at <file>.\n" " --eval <file> Send an EVAL command using the Lua script at <file>.\n"
@ -1609,10 +1635,10 @@ static void usage(void) {
exit(1); exit(1);
} }
int confirmWithYes(const char *msg, int force) { int confirmWithYes(const char *msg, int ignore_force) {
/* if force is true and --cluster-yes option is on, /* if force is true and --cluster-yes option is on,
* do not prompt for an answer */ * do not prompt for an answer */
if (force && if (!ignore_force &&
(config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_YES)) { (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_YES)) {
return 1; return 1;
} }
@ -1801,6 +1827,7 @@ static void repl(void) {
if (argv == NULL) { if (argv == NULL) {
printf("Invalid argument(s)\n"); printf("Invalid argument(s)\n");
fflush(stdout);
linenoiseFree(line); linenoiseFree(line);
continue; continue;
} else if (argc > 0) { } else if (argc > 0) {
@ -1863,14 +1890,15 @@ static void repl(void) {
exit(0); exit(0);
} }
static int noninteractive(int argc, char **argv) { static int noninteractive(int argc, char ***argv) {
int retval = 0; int retval = 0;
if (config.stdinarg) { if (config.stdinarg) {
argv = zrealloc(argv, (argc+1)*sizeof(char*), MALLOC_LOCAL); *argv = zrealloc(*argv, (argc+1)*sizeof(char*), MALLOC_LOCAL);
argv[argc] = readArgFromStdin(); (*argv)[argc] = readArgFromStdin();
retval = issueCommand(argc+1, argv); retval = issueCommand(argc+1, *argv);
sdsfree((*argv)[argc]);
} else { } else {
retval = issueCommand(argc, argv); retval = issueCommand(argc, *argv);
} }
return retval; return retval;
} }
@ -1938,7 +1966,7 @@ static int evalMode(int argc, char **argv) {
argv2[2] = sdscatprintf(sdsempty(),"%d",keys); argv2[2] = sdscatprintf(sdsempty(),"%d",keys);
/* Call it */ /* Call it */
int eval_ldb = config.eval_ldb; /* Save it, may be reverteed. */ int eval_ldb = config.eval_ldb; /* Save it, may be reverted. */
retval = issueCommand(argc+3-got_comma, argv2); retval = issueCommand(argc+3-got_comma, argv2);
if (eval_ldb) { if (eval_ldb) {
if (!config.eval_ldb) { if (!config.eval_ldb) {
@ -2045,7 +2073,7 @@ clusterManagerCommandDef clusterManagerCommands[] = {
"new_host:new_port existing_host:existing_port", "slave,master-id <arg>"}, "new_host:new_port existing_host:existing_port", "slave,master-id <arg>"},
{"del-node", clusterManagerCommandDeleteNode, 2, "host:port node_id",NULL}, {"del-node", clusterManagerCommandDeleteNode, 2, "host:port node_id",NULL},
{"call", clusterManagerCommandCall, -2, {"call", clusterManagerCommandCall, -2,
"host:port command arg arg .. arg", NULL}, "host:port command arg arg .. arg", "only-masters,only-replicas"},
{"set-timeout", clusterManagerCommandSetTimeout, 2, {"set-timeout", clusterManagerCommandSetTimeout, 2,
"host:port milliseconds", NULL}, "host:port milliseconds", NULL},
{"import", clusterManagerCommandImport, 1, "host:port", {"import", clusterManagerCommandImport, 1, "host:port",
@ -3037,6 +3065,7 @@ static redisReply *clusterManagerMigrateKeysInReply(clusterManagerNode *source,
size_t *argv_len = NULL; size_t *argv_len = NULL;
int c = (replace ? 8 : 7); int c = (replace ? 8 : 7);
if (config.auth) c += 2; if (config.auth) c += 2;
if (config.user) c += 1;
size_t argc = c + reply->elements; size_t argc = c + reply->elements;
size_t i, offset = 6; // Keys Offset size_t i, offset = 6; // Keys Offset
argv = zcalloc(argc * sizeof(char *), MALLOC_LOCAL); argv = zcalloc(argc * sizeof(char *), MALLOC_LOCAL);
@ -3063,12 +3092,24 @@ static redisReply *clusterManagerMigrateKeysInReply(clusterManagerNode *source,
offset++; offset++;
} }
if (config.auth) { if (config.auth) {
argv[offset] = "AUTH"; if (config.user) {
argv_len[offset] = 4; argv[offset] = "AUTH2";
offset++; argv_len[offset] = 5;
argv[offset] = config.auth; offset++;
argv_len[offset] = strlen(config.auth); argv[offset] = config.user;
offset++; argv_len[offset] = strlen(config.user);
offset++;
argv[offset] = config.auth;
argv_len[offset] = strlen(config.auth);
offset++;
} else {
argv[offset] = "AUTH";
argv_len[offset] = 4;
offset++;
argv[offset] = config.auth;
argv_len[offset] = strlen(config.auth);
offset++;
}
} }
argv[offset] = "KEYS"; argv[offset] = "KEYS";
argv_len[offset] = 4; argv_len[offset] = 4;
@ -4416,8 +4457,6 @@ static void clusterManagerMode(clusterManagerCommandProc *proc) {
exit(0); exit(0);
cluster_manager_err: cluster_manager_err:
freeClusterManager(); freeClusterManager();
sdsfree(config.hostip);
sdsfree(config.mb_delim);
exit(1); exit(1);
} }
@ -4595,8 +4634,8 @@ assign_replicas:
} }
clusterManagerOptimizeAntiAffinity(ip_nodes, ip_count); clusterManagerOptimizeAntiAffinity(ip_nodes, ip_count);
clusterManagerShowNodes(); clusterManagerShowNodes();
int force = 1; int ignore_force = 0;
if (confirmWithYes("Can I set the above configuration?", force)) { if (confirmWithYes("Can I set the above configuration?", ignore_force)) {
listRewind(cluster_manager.nodes, &li); listRewind(cluster_manager.nodes, &li);
while ((ln = listNext(&li)) != NULL) { while ((ln = listNext(&li)) != NULL) {
clusterManagerNode *node = ln->value; clusterManagerNode *node = ln->value;
@ -5487,6 +5526,10 @@ static int clusterManagerCommandCall(int argc, char **argv) {
listRewind(cluster_manager.nodes, &li); listRewind(cluster_manager.nodes, &li);
while ((ln = listNext(&li)) != NULL) { while ((ln = listNext(&li)) != NULL) {
clusterManagerNode *n = ln->value; clusterManagerNode *n = ln->value;
if ((config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_MASTERS_ONLY)
&& (n->replicate != NULL)) continue; // continue if node is slave
if ((config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_SLAVES_ONLY)
&& (n->replicate == NULL)) continue; // continue if node is master
if (!n->context && !clusterManagerNodeConnect(n)) continue; if (!n->context && !clusterManagerNodeConnect(n)) continue;
redisReply *reply = NULL; redisReply *reply = NULL;
redisAppendCommandArgv(n->context, argc, (const char **) argv, argvlen); redisAppendCommandArgv(n->context, argc, (const char **) argv, argvlen);
@ -5710,13 +5753,13 @@ struct distsamples {
* samples greater than the previous one, and is also the stop sentinel. * samples greater than the previous one, and is also the stop sentinel.
* *
* "tot' is the total number of samples in the different buckets, so it * "tot' is the total number of samples in the different buckets, so it
* is the SUM(samples[i].conut) for i to 0 up to the max sample. * is the SUM(samples[i].count) for i to 0 up to the max sample.
* *
* As a side effect the function sets all the buckets count to 0. */ * As a side effect the function sets all the buckets count to 0. */
void showLatencyDistSamples(struct distsamples *samples, long long tot) { void showLatencyDistSamples(struct distsamples *samples, long long tot) {
int j; int j;
/* We convert samples into a index inside the palette /* We convert samples into an index inside the palette
* proportional to the percentage a given bucket represents. * proportional to the percentage a given bucket represents.
* This way intensity of the different parts of the spectrum * This way intensity of the different parts of the spectrum
* don't change relative to the number of requests, which avoids to * don't change relative to the number of requests, which avoids to
@ -5856,10 +5899,84 @@ void sendCapa() {
sendReplconf("capa", "eof"); sendReplconf("capa", "eof");
} }
/* Wrapper around hiredis to allow arbitrary reads and writes.
*
* We piggybacks on top of hiredis to achieve transparent TLS support,
* and use its internal buffers so it can co-exist with commands
* previously/later issued on the connection.
*
* Interface is close to enough to read()/write() so things should mostly
* work transparently.
*/
/* Write a raw buffer through a redisContext. If we already have something
* in the buffer (leftovers from hiredis operations) it will be written
* as well.
*/
static ssize_t writeConn(redisContext *c, const char *buf, size_t buf_len)
{
int done = 0;
/* Append data to buffer which is *usually* expected to be empty
* but we don't assume that, and write.
*/
c->obuf = sdscatlen(c->obuf, buf, buf_len);
if (redisBufferWrite(c, &done) == REDIS_ERR) {
if (!(c->flags & REDIS_BLOCK))
errno = EAGAIN;
/* On error, we assume nothing was written and we roll back the
* buffer to its original state.
*/
if (sdslen(c->obuf) > buf_len)
sdsrange(c->obuf, 0, -(buf_len+1));
else
sdsclear(c->obuf);
return -1;
}
/* If we're done, free up everything. We may have written more than
* buf_len (if c->obuf was not initially empty) but we don't have to
* tell.
*/
if (done) {
sdsclear(c->obuf);
return buf_len;
}
/* Write was successful but we have some leftovers which we should
* remove from the buffer.
*
* Do we still have data that was there prior to our buf? If so,
* restore buffer to it's original state and report no new data was
* writen.
*/
if (sdslen(c->obuf) > buf_len) {
sdsrange(c->obuf, 0, -(buf_len+1));
return 0;
}
/* At this point we're sure no prior data is left. We flush the buffer
* and report how much we've written.
*/
size_t left = sdslen(c->obuf);
sdsclear(c->obuf);
return buf_len - left;
}
/* Read raw bytes through a redisContext. The read operation is not greedy
* and may not fill the buffer entirely.
*/
static ssize_t readConn(redisContext *c, char *buf, size_t len)
{
return c->funcs->read(c, buf, len);
}
/* Sends SYNC and reads the number of bytes in the payload. Used both by /* Sends SYNC and reads the number of bytes in the payload. Used both by
* slaveMode() and getRDB(). * slaveMode() and getRDB().
* returns 0 in case an EOF marker is used. */ * returns 0 in case an EOF marker is used. */
unsigned long long sendSync(int fd, char *out_eof) { unsigned long long sendSync(redisContext *c, char *out_eof) {
/* To start we need to send the SYNC command and return the payload. /* To start we need to send the SYNC command and return the payload.
* The hiredis client lib does not understand this part of the protocol * The hiredis client lib does not understand this part of the protocol
* and we don't want to mess with its buffers, so everything is performed * and we don't want to mess with its buffers, so everything is performed
@ -5868,7 +5985,7 @@ unsigned long long sendSync(int fd, char *out_eof) {
ssize_t nread; ssize_t nread;
/* Send the SYNC command. */ /* Send the SYNC command. */
if (write(fd,"SYNC\r\n",6) != 6) { if (writeConn(c, "SYNC\r\n", 6) != 6) {
fprintf(stderr,"Error writing to master\n"); fprintf(stderr,"Error writing to master\n");
exit(1); exit(1);
} }
@ -5876,7 +5993,7 @@ unsigned long long sendSync(int fd, char *out_eof) {
/* Read $<payload>\r\n, making sure to read just up to "\n" */ /* Read $<payload>\r\n, making sure to read just up to "\n" */
p = buf; p = buf;
while(1) { while(1) {
nread = read(fd,p,1); nread = readConn(c,p,1);
if (nread <= 0) { if (nread <= 0) {
fprintf(stderr,"Error reading bulk length while SYNCing\n"); fprintf(stderr,"Error reading bulk length while SYNCing\n");
exit(1); exit(1);
@ -5897,11 +6014,10 @@ unsigned long long sendSync(int fd, char *out_eof) {
} }
static void slaveMode(void) { static void slaveMode(void) {
int fd = context->fd;
static char eofmark[RDB_EOF_MARK_SIZE]; static char eofmark[RDB_EOF_MARK_SIZE];
static char lastbytes[RDB_EOF_MARK_SIZE]; static char lastbytes[RDB_EOF_MARK_SIZE];
static int usemark = 0; static int usemark = 0;
unsigned long long payload = sendSync(fd, eofmark); unsigned long long payload = sendSync(context,eofmark);
char buf[1024]; char buf[1024];
int original_output = config.output; int original_output = config.output;
@ -5921,7 +6037,7 @@ static void slaveMode(void) {
while(payload) { while(payload) {
ssize_t nread; ssize_t nread;
nread = read(fd,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload); nread = readConn(context,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
if (nread <= 0) { if (nread <= 0) {
fprintf(stderr,"Error reading RDB payload while SYNCing\n"); fprintf(stderr,"Error reading RDB payload while SYNCing\n");
exit(1); exit(1);
@ -5964,14 +6080,15 @@ static void slaveMode(void) {
/* This function implements --rdb, so it uses the replication protocol in order /* This function implements --rdb, so it uses the replication protocol in order
* to fetch the RDB file from a remote server. */ * to fetch the RDB file from a remote server. */
static void getRDB(clusterManagerNode *node) { static void getRDB(clusterManagerNode *node) {
int s, fd; int fd;
redisContext *s;
char *filename; char *filename;
if (node != NULL) { if (node != NULL) {
assert(node->context); assert(node->context);
s = node->context->fd; s = node->context;
filename = clusterManagerGetNodeRDBFilename(node); filename = clusterManagerGetNodeRDBFilename(node);
} else { } else {
s = context->fd; s = context;
filename = config.rdb_filename; filename = config.rdb_filename;
} }
static char eofmark[RDB_EOF_MARK_SIZE]; static char eofmark[RDB_EOF_MARK_SIZE];
@ -6006,7 +6123,7 @@ static void getRDB(clusterManagerNode *node) {
while(payload) { while(payload) {
ssize_t nread, nwritten; ssize_t nread, nwritten;
nread = read(s,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload); nread = readConn(s,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
if (nread <= 0) { if (nread <= 0) {
fprintf(stderr,"I/O Error reading RDB payload from socket\n"); fprintf(stderr,"I/O Error reading RDB payload from socket\n");
exit(1); exit(1);
@ -6040,7 +6157,9 @@ static void getRDB(clusterManagerNode *node) {
} else { } else {
fprintf(stderr,"Transfer finished with success.\n"); fprintf(stderr,"Transfer finished with success.\n");
} }
close(s); /* Close the file descriptor ASAP as fsync() may take time. */ redisFree(s); /* Close the connection ASAP as fsync() may take time. */
if (node)
node->context = NULL;
fsync(fd); fsync(fd);
close(fd); close(fd);
fprintf(stderr,"Transfer finished with success.\n"); fprintf(stderr,"Transfer finished with success.\n");
@ -6057,11 +6176,9 @@ static void getRDB(clusterManagerNode *node) {
#define PIPEMODE_WRITE_LOOP_MAX_BYTES (128*1024) #define PIPEMODE_WRITE_LOOP_MAX_BYTES (128*1024)
static void pipeMode(void) { static void pipeMode(void) {
int fd = context->fd;
long long errors = 0, replies = 0, obuf_len = 0, obuf_pos = 0; long long errors = 0, replies = 0, obuf_len = 0, obuf_pos = 0;
char ibuf[1024*16], obuf[1024*16]; /* Input and output buffers */ char obuf[1024*16]; /* Output buffer */
char aneterr[ANET_ERR_LEN]; char aneterr[ANET_ERR_LEN];
redisReader *reader = redisReaderCreate();
redisReply *reply; redisReply *reply;
int eof = 0; /* True once we consumed all the standard input. */ int eof = 0; /* True once we consumed all the standard input. */
int done = 0; int done = 0;
@ -6071,47 +6188,38 @@ static void pipeMode(void) {
srand(time(NULL)); srand(time(NULL));
/* Use non blocking I/O. */ /* Use non blocking I/O. */
if (anetNonBlock(aneterr,fd) == ANET_ERR) { if (anetNonBlock(aneterr,context->fd) == ANET_ERR) {
fprintf(stderr, "Can't set the socket in non blocking mode: %s\n", fprintf(stderr, "Can't set the socket in non blocking mode: %s\n",
aneterr); aneterr);
exit(1); exit(1);
} }
context->flags &= ~REDIS_BLOCK;
/* Transfer raw protocol and read replies from the server at the same /* Transfer raw protocol and read replies from the server at the same
* time. */ * time. */
while(!done) { while(!done) {
int mask = AE_READABLE; int mask = AE_READABLE;
if (!eof || obuf_len != 0) mask |= AE_WRITABLE; if (!eof || obuf_len != 0) mask |= AE_WRITABLE;
mask = aeWait(fd,mask,1000); mask = aeWait(context->fd,mask,1000);
/* Handle the readable state: we can read replies from the server. */ /* Handle the readable state: we can read replies from the server. */
if (mask & AE_READABLE) { if (mask & AE_READABLE) {
ssize_t nread;
int read_error = 0; int read_error = 0;
/* Read from socket and feed the hiredis reader. */
do { do {
nread = read(fd,ibuf,sizeof(ibuf)); if (!read_error && redisBufferRead(context) == REDIS_ERR) {
if (nread == -1 && errno != EAGAIN && errno != EINTR) {
fprintf(stderr, "Error reading from the server: %s\n",
strerror(errno));
read_error = 1; read_error = 1;
break;
} }
if (nread > 0) {
redisReaderFeed(reader,ibuf,nread);
last_read_time = time(NULL);
}
} while(nread > 0);
/* Consume replies. */ reply = NULL;
do { if (redisGetReply(context, (void **) &reply) == REDIS_ERR) {
if (redisReaderGetReply(reader,(void**)&reply) == REDIS_ERR) {
fprintf(stderr, "Error reading replies from server\n"); fprintf(stderr, "Error reading replies from server\n");
exit(1); exit(1);
} }
if (reply) { if (reply) {
last_read_time = time(NULL);
if (reply->type == REDIS_REPLY_ERROR) { if (reply->type == REDIS_REPLY_ERROR) {
fprintf(stderr,"%s\n", reply->str); fprintf(stderr,"%s\n", reply->str);
errors++; errors++;
@ -6144,7 +6252,7 @@ static void pipeMode(void) {
while(1) { while(1) {
/* Transfer current buffer to server. */ /* Transfer current buffer to server. */
if (obuf_len != 0) { if (obuf_len != 0) {
ssize_t nwritten = write(fd,obuf+obuf_pos,obuf_len); ssize_t nwritten = writeConn(context,obuf+obuf_pos,obuf_len);
if (nwritten == -1) { if (nwritten == -1) {
if (errno != EAGAIN && errno != EINTR) { if (errno != EAGAIN && errno != EINTR) {
@ -6160,6 +6268,10 @@ static void pipeMode(void) {
loop_nwritten += nwritten; loop_nwritten += nwritten;
if (obuf_len != 0) break; /* Can't accept more data. */ if (obuf_len != 0) break; /* Can't accept more data. */
} }
if (context->err) {
fprintf(stderr, "Server I/O Error: %s\n", context->errstr);
exit(1);
}
/* If buffer is empty, load from stdin. */ /* If buffer is empty, load from stdin. */
if (obuf_len == 0 && !eof) { if (obuf_len == 0 && !eof) {
ssize_t nread = read(STDIN_FILENO,obuf,sizeof(obuf)); ssize_t nread = read(STDIN_FILENO,obuf,sizeof(obuf));
@ -6210,7 +6322,6 @@ static void pipeMode(void) {
break; break;
} }
} }
redisReaderFree(reader);
printf("errors: %lld, replies: %lld\n", errors, replies); printf("errors: %lld, replies: %lld\n", errors, replies);
if (errors) if (errors)
exit(1); exit(1);
@ -6223,7 +6334,13 @@ static void pipeMode(void) {
*--------------------------------------------------------------------------- */ *--------------------------------------------------------------------------- */
redisReply *sendScan(unsigned long long *it) { redisReply *sendScan(unsigned long long *it) {
redisReply *reply = redisCommand(context, "SCAN %llu", *it); redisReply *reply;
if (config.pattern)
reply = redisCommand(context,"SCAN %llu MATCH %s",
*it,config.pattern);
else
reply = redisCommand(context,"SCAN %llu",*it);
/* Handle any error conditions */ /* Handle any error conditions */
if(reply == NULL) { if(reply == NULL) {
@ -6298,15 +6415,21 @@ void getKeySizes(redisReply *keys, typeinfo **types,
if(!types[i] || (!types[i]->sizecmd && !memkeys)) if(!types[i] || (!types[i]->sizecmd && !memkeys))
continue; continue;
if (!memkeys) if (!memkeys) {
redisAppendCommand(context, "%s %s", const char* argv[] = {types[i]->sizecmd, keys->element[i]->str};
types[i]->sizecmd, keys->element[i]->str); size_t lens[] = {strlen(types[i]->sizecmd), keys->element[i]->len};
else if (memkeys_samples==0) redisAppendCommandArgv(context, 2, argv, lens);
redisAppendCommand(context, "%s %s %s", } else if (memkeys_samples==0) {
"MEMORY", "USAGE", keys->element[i]->str); const char* argv[] = {"MEMORY", "USAGE", keys->element[i]->str};
else size_t lens[] = {6, 5, keys->element[i]->len};
redisAppendCommand(context, "%s %s %s SAMPLES %u", redisAppendCommandArgv(context, 3, argv, lens);
"MEMORY", "USAGE", keys->element[i]->str, memkeys_samples); } else {
sds samplesstr = sdsfromlonglong(memkeys_samples);
const char* argv[] = {"MEMORY", "USAGE", keys->element[i]->str, "SAMPLES", samplesstr};
size_t lens[] = {6, 5, keys->element[i]->len, 7, sdslen(samplesstr)};
redisAppendCommandArgv(context, 5, argv, lens);
sdsfree(samplesstr);
}
} }
/* Retrieve sizes */ /* Retrieve sizes */
@ -6344,21 +6467,27 @@ static void getKeyFreqs(redisReply *keys, unsigned long long *freqs) {
/* Pipeline OBJECT freq commands */ /* Pipeline OBJECT freq commands */
for(i=0;i<keys->elements;i++) { for(i=0;i<keys->elements;i++) {
redisAppendCommand(context, "OBJECT freq %s", keys->element[i]->str); const char* argv[] = {"OBJECT", "FREQ", keys->element[i]->str};
size_t lens[] = {6, 4, keys->element[i]->len};
redisAppendCommandArgv(context, 3, argv, lens);
} }
/* Retrieve freqs */ /* Retrieve freqs */
for(i=0;i<keys->elements;i++) { for(i=0;i<keys->elements;i++) {
if(redisGetReply(context, (void**)&reply)!=REDIS_OK) { if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {
sds keyname = sdscatrepr(sdsempty(), keys->element[i]->str, keys->element[i]->len);
fprintf(stderr, "Error getting freq for key '%s' (%d: %s)\n", fprintf(stderr, "Error getting freq for key '%s' (%d: %s)\n",
keys->element[i]->str, context->err, context->errstr); keyname, context->err, context->errstr);
sdsfree(keyname);
exit(1); exit(1);
} else if(reply->type != REDIS_REPLY_INTEGER) { } else if(reply->type != REDIS_REPLY_INTEGER) {
if(reply->type == REDIS_REPLY_ERROR) { if(reply->type == REDIS_REPLY_ERROR) {
fprintf(stderr, "Error: %s\n", reply->str); fprintf(stderr, "Error: %s\n", reply->str);
exit(1); exit(1);
} else { } else {
fprintf(stderr, "Warning: OBJECT freq on '%s' failed (may have been deleted)\n", keys->element[i]->str); sds keyname = sdscatrepr(sdsempty(), keys->element[i]->str, keys->element[i]->len);
fprintf(stderr, "Warning: OBJECT freq on '%s' failed (may have been deleted)\n", keyname);
sdsfree(keyname);
freqs[i] = 0; freqs[i] = 0;
} }
} else { } else {
@ -6429,10 +6558,10 @@ static void findHotKeys(void) {
memmove(hotkeys,hotkeys+1,sizeof(hotkeys[0])*k); memmove(hotkeys,hotkeys+1,sizeof(hotkeys[0])*k);
} }
counters[k] = freqs[i]; counters[k] = freqs[i];
hotkeys[k] = sdsnew(keys->element[i]->str); hotkeys[k] = sdscatrepr(sdsempty(), keys->element[i]->str, keys->element[i]->len);
printf( printf(
"[%05.2f%%] Hot key '%s' found so far with counter %llu\n", "[%05.2f%%] Hot key '%s' found so far with counter %llu\n",
pct, keys->element[i]->str, freqs[i]); pct, hotkeys[k], freqs[i]);
} }
/* Sleep if we've been directed to do so */ /* Sleep if we've been directed to do so */
@ -6736,7 +6865,7 @@ static void LRUTestMode(void) {
* Intrisic latency mode. * Intrisic latency mode.
* *
* Measure max latency of a running process that does not result from * Measure max latency of a running process that does not result from
* syscalls. Basically this software should provide an hint about how much * syscalls. Basically this software should provide a hint about how much
* time the kernel leaves the process without a chance to run. * time the kernel leaves the process without a chance to run.
*--------------------------------------------------------------------------- */ *--------------------------------------------------------------------------- */
@ -6884,6 +7013,7 @@ int main(int argc, char **argv) {
else else
config.output = OUTPUT_STANDARD; config.output = OUTPUT_STANDARD;
config.mb_delim = sdsnew("\n"); config.mb_delim = sdsnew("\n");
config.cmd_delim = sdsnew("\n");
firstarg = parseOptions(argc,argv); firstarg = parseOptions(argc,argv);
argc -= firstarg; argc -= firstarg;
@ -6907,8 +7037,6 @@ int main(int argc, char **argv) {
if (CLUSTER_MANAGER_MODE()) { if (CLUSTER_MANAGER_MODE()) {
clusterManagerCommandProc *proc = validateClusterManagerCommand(); clusterManagerCommandProc *proc = validateClusterManagerCommand();
if (!proc) { if (!proc) {
sdsfree(config.hostip);
sdsfree(config.mb_delim);
exit(1); exit(1);
} }
clusterManagerMode(proc); clusterManagerMode(proc);
@ -6995,8 +7123,9 @@ int main(int argc, char **argv) {
/* Start interactive mode when no command is provided */ /* Start interactive mode when no command is provided */
if (argc == 0 && !config.eval) { if (argc == 0 && !config.eval) {
/* Show the message of the day if we are interactive */ /* Show the message of the day if we are interactive */
if (config.output == OUTPUT_STANDARD) { if (config.output == OUTPUT_STANDARD && !config.disable_motd) {
char *szMotd = fetchMOTD(1 /* cache */); /*enable_motd=1 will retrieve the message of today using CURL*/
char *szMotd = fetchMOTD(1 /* cache */, 1 /* enable_motd */);
if (szMotd != NULL) { if (szMotd != NULL) {
printf("Message of the day:\n %s\n", szMotd); printf("Message of the day:\n %s\n", szMotd);
sdsfree(szMotd); sdsfree(szMotd);
@ -7018,7 +7147,11 @@ int main(int argc, char **argv) {
if (config.eval) { if (config.eval) {
return evalMode(argc,argv); return evalMode(argc,argv);
} else { } else {
return noninteractive(argc,convertToSds(argc,argv)); sds *sdsArgs = convertToSds(argc,argv);
int rval = noninteractive(argc,&sdsArgs);
for (int i = 0; i < argc; ++i)
sdsfree(sdsArgs[i]);
zfree(sdsArgs);
return rval;
} }
} }

View File

@ -16,6 +16,7 @@ extern "C" {
#define REDIS_CLI_RCFILE_ENV "REDISCLI_RCFILE" #define REDIS_CLI_RCFILE_ENV "REDISCLI_RCFILE"
#define REDIS_CLI_RCFILE_DEFAULT ".redisclirc" #define REDIS_CLI_RCFILE_DEFAULT ".redisclirc"
#define REDIS_CLI_AUTH_ENV "REDISCLI_AUTH" #define REDIS_CLI_AUTH_ENV "REDISCLI_AUTH"
#define REDIS_CLI_CLUSTER_YES_ENV "REDISCLI_CLUSTER_YES"
#define CLUSTER_MANAGER_SLOTS 16384 #define CLUSTER_MANAGER_SLOTS 16384
#define CLUSTER_MANAGER_MIGRATE_TIMEOUT 60000 #define CLUSTER_MANAGER_MIGRATE_TIMEOUT 60000
@ -67,6 +68,8 @@ extern "C" {
#define CLUSTER_MANAGER_CMD_FLAG_COLOR 1 << 8 #define CLUSTER_MANAGER_CMD_FLAG_COLOR 1 << 8
#define CLUSTER_MANAGER_CMD_FLAG_CHECK_OWNERS 1 << 9 #define CLUSTER_MANAGER_CMD_FLAG_CHECK_OWNERS 1 << 9
#define CLUSTER_MANAGER_CMD_FLAG_FIX_WITH_UNREACHABLE_MASTERS 1 << 10 #define CLUSTER_MANAGER_CMD_FLAG_FIX_WITH_UNREACHABLE_MASTERS 1 << 10
#define CLUSTER_MANAGER_CMD_FLAG_MASTERS_ONLY 1 << 11
#define CLUSTER_MANAGER_CMD_FLAG_SLAVES_ONLY 1 << 12
#define CLUSTER_MANAGER_OPT_GETFRIENDS 1 << 0 #define CLUSTER_MANAGER_OPT_GETFRIENDS 1 << 0
#define CLUSTER_MANAGER_OPT_COLD 1 << 1 #define CLUSTER_MANAGER_OPT_COLD 1 << 1
@ -168,6 +171,7 @@ extern struct config {
char *user; char *user;
int output; /* output mode, see OUTPUT_* defines */ int output; /* output mode, see OUTPUT_* defines */
sds mb_delim; sds mb_delim;
sds cmd_delim;
char prompt[128]; char prompt[128];
char *eval; char *eval;
int eval_ldb; int eval_ldb;
@ -179,6 +183,7 @@ extern struct config {
clusterManagerCommand cluster_manager_command; clusterManagerCommand cluster_manager_command;
int no_auth_warning; int no_auth_warning;
int resp3; int resp3;
int disable_motd;
} config; } config;
struct clusterManager { struct clusterManager {

View File

@ -116,6 +116,13 @@ extern "C" {
#define REDISMODULE_CTX_FLAGS_ACTIVE_CHILD (1<<18) #define REDISMODULE_CTX_FLAGS_ACTIVE_CHILD (1<<18)
/* The next EXEC will fail due to dirty CAS (touched keys). */ /* The next EXEC will fail due to dirty CAS (touched keys). */
#define REDISMODULE_CTX_FLAGS_MULTI_DIRTY (1<<19) #define REDISMODULE_CTX_FLAGS_MULTI_DIRTY (1<<19)
/* Redis is currently running inside background child process. */
#define REDISMODULE_CTX_FLAGS_IS_CHILD (1<<20)
/* Next context flag, must be updated when adding new flags above!
This flag should not be used directly by the module.
* Use RedisModule_GetContextFlagsAll instead. */
#define _REDISMODULE_CTX_FLAGS_NEXT (1<<21)
/* Keyspace changes notification classes. Every class is associated with a /* Keyspace changes notification classes. Every class is associated with a
* character for configuration purposes. * character for configuration purposes.
@ -132,8 +139,14 @@ extern "C" {
#define REDISMODULE_NOTIFY_EVICTED (1<<9) /* e */ #define REDISMODULE_NOTIFY_EVICTED (1<<9) /* e */
#define REDISMODULE_NOTIFY_STREAM (1<<10) /* t */ #define REDISMODULE_NOTIFY_STREAM (1<<10) /* t */
#define REDISMODULE_NOTIFY_KEY_MISS (1<<11) /* m (Note: This one is excluded from REDISMODULE_NOTIFY_ALL on purpose) */ #define REDISMODULE_NOTIFY_KEY_MISS (1<<11) /* m (Note: This one is excluded from REDISMODULE_NOTIFY_ALL on purpose) */
#define REDISMODULE_NOTIFY_ALL (REDISMODULE_NOTIFY_GENERIC | REDISMODULE_NOTIFY_STRING | REDISMODULE_NOTIFY_LIST | REDISMODULE_NOTIFY_SET | REDISMODULE_NOTIFY_HASH | REDISMODULE_NOTIFY_ZSET | REDISMODULE_NOTIFY_EXPIRED | REDISMODULE_NOTIFY_EVICTED | REDISMODULE_NOTIFY_STREAM) /* A */ #define REDISMODULE_NOTIFY_LOADED (1<<12) /* module only key space notification, indicate a key loaded from rdb */
/* Next notification flag, must be updated when adding new flags above!
This flag should not be used directly by the module.
* Use RedisModule_GetKeyspaceNotificationFlagsAll instead. */
#define _REDISMODULE_NOTIFY_NEXT (1<<13)
#define REDISMODULE_NOTIFY_ALL (REDISMODULE_NOTIFY_GENERIC | REDISMODULE_NOTIFY_STRING | REDISMODULE_NOTIFY_LIST | REDISMODULE_NOTIFY_SET | REDISMODULE_NOTIFY_HASH | REDISMODULE_NOTIFY_ZSET | REDISMODULE_NOTIFY_EXPIRED | REDISMODULE_NOTIFY_EVICTED | REDISMODULE_NOTIFY_STREAM) /* A */
/* A special pointer that we can use between the core and the module to signal /* A special pointer that we can use between the core and the module to signal
* field deletion, and that is impossible to be a valid pointer. */ * field deletion, and that is impossible to be a valid pointer. */
@ -182,7 +195,9 @@ typedef uint64_t RedisModuleTimerID;
* are modified from the user's sperspective, to invalidate WATCH. */ * are modified from the user's sperspective, to invalidate WATCH. */
#define REDISMODULE_OPTION_NO_IMPLICIT_SIGNAL_MODIFIED (1<<1) #define REDISMODULE_OPTION_NO_IMPLICIT_SIGNAL_MODIFIED (1<<1)
/* Server events definitions. */ /* Server events definitions.
* Those flags should not be used directly by the module, instead
* the module should use RedisModuleEvent_* variables */
#define REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED 0 #define REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED 0
#define REDISMODULE_EVENT_PERSISTENCE 1 #define REDISMODULE_EVENT_PERSISTENCE 1
#define REDISMODULE_EVENT_FLUSHDB 2 #define REDISMODULE_EVENT_FLUSHDB 2
@ -194,6 +209,10 @@ typedef uint64_t RedisModuleTimerID;
#define REDISMODULE_EVENT_CRON_LOOP 8 #define REDISMODULE_EVENT_CRON_LOOP 8
#define REDISMODULE_EVENT_MODULE_CHANGE 9 #define REDISMODULE_EVENT_MODULE_CHANGE 9
#define REDISMODULE_EVENT_LOADING_PROGRESS 10 #define REDISMODULE_EVENT_LOADING_PROGRESS 10
#define REDISMODULE_EVENT_SWAPDB 11
/* Next event flag, should be updated if a new event added. */
#define _REDISMODULE_EVENT_NEXT 12
typedef struct RedisModuleEvent { typedef struct RedisModuleEvent {
uint64_t id; /* REDISMODULE_EVENT_... defines. */ uint64_t id; /* REDISMODULE_EVENT_... defines. */
@ -247,6 +266,10 @@ static const RedisModuleEvent
RedisModuleEvent_LoadingProgress = { RedisModuleEvent_LoadingProgress = {
REDISMODULE_EVENT_LOADING_PROGRESS, REDISMODULE_EVENT_LOADING_PROGRESS,
1 1
},
RedisModuleEvent_SwapDB = {
REDISMODULE_EVENT_SWAPDB,
1
}; };
/* Those are values that are used for the 'subevent' callback argument. */ /* Those are values that are used for the 'subevent' callback argument. */
@ -255,33 +278,47 @@ static const RedisModuleEvent
#define REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START 2 #define REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START 2
#define REDISMODULE_SUBEVENT_PERSISTENCE_ENDED 3 #define REDISMODULE_SUBEVENT_PERSISTENCE_ENDED 3
#define REDISMODULE_SUBEVENT_PERSISTENCE_FAILED 4 #define REDISMODULE_SUBEVENT_PERSISTENCE_FAILED 4
#define _REDISMODULE_SUBEVENT_PERSISTENCE_NEXT 5
#define REDISMODULE_SUBEVENT_LOADING_RDB_START 0 #define REDISMODULE_SUBEVENT_LOADING_RDB_START 0
#define REDISMODULE_SUBEVENT_LOADING_AOF_START 1 #define REDISMODULE_SUBEVENT_LOADING_AOF_START 1
#define REDISMODULE_SUBEVENT_LOADING_REPL_START 2 #define REDISMODULE_SUBEVENT_LOADING_REPL_START 2
#define REDISMODULE_SUBEVENT_LOADING_ENDED 3 #define REDISMODULE_SUBEVENT_LOADING_ENDED 3
#define REDISMODULE_SUBEVENT_LOADING_FAILED 4 #define REDISMODULE_SUBEVENT_LOADING_FAILED 4
#define _REDISMODULE_SUBEVENT_LOADING_NEXT 5
#define REDISMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED 0 #define REDISMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED 0
#define REDISMODULE_SUBEVENT_CLIENT_CHANGE_DISCONNECTED 1 #define REDISMODULE_SUBEVENT_CLIENT_CHANGE_DISCONNECTED 1
#define _REDISMODULE_SUBEVENT_CLIENT_CHANGE_NEXT 2
#define REDISMODULE_SUBEVENT_MASTER_LINK_UP 0 #define REDISMODULE_SUBEVENT_MASTER_LINK_UP 0
#define REDISMODULE_SUBEVENT_MASTER_LINK_DOWN 1 #define REDISMODULE_SUBEVENT_MASTER_LINK_DOWN 1
#define _REDISMODULE_SUBEVENT_MASTER_NEXT 2
#define REDISMODULE_SUBEVENT_REPLICA_CHANGE_ONLINE 0 #define REDISMODULE_SUBEVENT_REPLICA_CHANGE_ONLINE 0
#define REDISMODULE_SUBEVENT_REPLICA_CHANGE_OFFLINE 1 #define REDISMODULE_SUBEVENT_REPLICA_CHANGE_OFFLINE 1
#define _REDISMODULE_SUBEVENT_REPLICA_CHANGE_NEXT 2
#define REDISMODULE_EVENT_REPLROLECHANGED_NOW_MASTER 0 #define REDISMODULE_EVENT_REPLROLECHANGED_NOW_MASTER 0
#define REDISMODULE_EVENT_REPLROLECHANGED_NOW_REPLICA 1 #define REDISMODULE_EVENT_REPLROLECHANGED_NOW_REPLICA 1
#define _REDISMODULE_EVENT_REPLROLECHANGED_NEXT 2
#define REDISMODULE_SUBEVENT_FLUSHDB_START 0 #define REDISMODULE_SUBEVENT_FLUSHDB_START 0
#define REDISMODULE_SUBEVENT_FLUSHDB_END 1 #define REDISMODULE_SUBEVENT_FLUSHDB_END 1
#define _REDISMODULE_SUBEVENT_FLUSHDB_NEXT 2
#define REDISMODULE_SUBEVENT_MODULE_LOADED 0 #define REDISMODULE_SUBEVENT_MODULE_LOADED 0
#define REDISMODULE_SUBEVENT_MODULE_UNLOADED 1 #define REDISMODULE_SUBEVENT_MODULE_UNLOADED 1
#define _REDISMODULE_SUBEVENT_MODULE_NEXT 2
#define REDISMODULE_SUBEVENT_LOADING_PROGRESS_RDB 0 #define REDISMODULE_SUBEVENT_LOADING_PROGRESS_RDB 0
#define REDISMODULE_SUBEVENT_LOADING_PROGRESS_AOF 1 #define REDISMODULE_SUBEVENT_LOADING_PROGRESS_AOF 1
#define _REDISMODULE_SUBEVENT_LOADING_PROGRESS_NEXT 2
#define _REDISMODULE_SUBEVENT_SHUTDOWN_NEXT 0
#define _REDISMODULE_SUBEVENT_CRON_LOOP_NEXT 0
#define _REDISMODULE_SUBEVENT_SWAPDB_NEXT 0
/* RedisModuleClientInfo flags. */ /* RedisModuleClientInfo flags. */
#define REDISMODULE_CLIENTINFO_FLAG_SSL (1<<0) #define REDISMODULE_CLIENTINFO_FLAG_SSL (1<<0)
@ -378,12 +415,48 @@ typedef struct RedisModuleLoadingProgressInfo {
#define RedisModuleLoadingProgress RedisModuleLoadingProgressV1 #define RedisModuleLoadingProgress RedisModuleLoadingProgressV1
#define REDISMODULE_SWAPDBINFO_VERSION 1
typedef struct RedisModuleSwapDbInfo {
uint64_t version; /* Not used since this structure is never passed
from the module to the core right now. Here
for future compatibility. */
int32_t dbnum_first; /* Swap Db first dbnum */
int32_t dbnum_second; /* Swap Db second dbnum */
} RedisModuleSwapDbInfoV1;
#define RedisModuleSwapDbInfo RedisModuleSwapDbInfoV1
/* ------------------------- End of common defines ------------------------ */ /* ------------------------- End of common defines ------------------------ */
#ifndef REDISMODULE_CORE #ifndef REDISMODULE_CORE
typedef long long mstime_t; typedef long long mstime_t;
/* Macro definitions specific to individual compilers */
#ifndef REDISMODULE_ATTR_UNUSED
# ifdef __GNUC__
# define REDISMODULE_ATTR_UNUSED __attribute__((unused))
# else
# define REDISMODULE_ATTR_UNUSED
# endif
#endif
#ifndef REDISMODULE_ATTR_PRINTF
# ifdef __GNUC__
# define REDISMODULE_ATTR_PRINTF(idx,cnt) __attribute__((format(printf,idx,cnt)))
# else
# define REDISMODULE_ATTR_PRINTF(idx,cnt)
# endif
#endif
#ifndef REDISMODULE_ATTR_COMMON
# if defined(__GNUC__) && !defined(__clang__)
# define REDISMODULE_ATTR_COMMON __attribute__((__common__))
# else
# define REDISMODULE_ATTR_COMMON
# endif
#endif
/* Incomplete structures for compiler checks but opaque access. */ /* Incomplete structures for compiler checks but opaque access. */
typedef struct RedisModuleCtx RedisModuleCtx; typedef struct RedisModuleCtx RedisModuleCtx;
typedef struct RedisModuleKey RedisModuleKey; typedef struct RedisModuleKey RedisModuleKey;
@ -440,256 +513,264 @@ typedef struct RedisModuleTypeMethods {
#define REDISMODULE_GET_API(name) \ #define REDISMODULE_GET_API(name) \
RedisModule_GetApi("RedisModule_" #name, ((void **)&RedisModule_ ## name)) RedisModule_GetApi("RedisModule_" #name, ((void **)&RedisModule_ ## name))
#define REDISMODULE_API_FUNC(x) (*x) /* Default API declaration prefix (not 'extern' for backwards compatibility) */
#ifndef REDISMODULE_API
#define REDISMODULE_API
void *REDISMODULE_API_FUNC(RedisModule_Alloc)(size_t bytes);
void *REDISMODULE_API_FUNC(RedisModule_Realloc)(void *ptr, size_t bytes);
void REDISMODULE_API_FUNC(RedisModule_Free)(void *ptr);
void *REDISMODULE_API_FUNC(RedisModule_Calloc)(size_t nmemb, size_t size);
char *REDISMODULE_API_FUNC(RedisModule_Strdup)(const char *str);
int REDISMODULE_API_FUNC(RedisModule_GetApi)(const char *, void *);
int REDISMODULE_API_FUNC(RedisModule_CreateCommand)(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep);
void REDISMODULE_API_FUNC(RedisModule_SetModuleAttribs)(RedisModuleCtx *ctx, const char *name, int ver, int apiver);
int REDISMODULE_API_FUNC(RedisModule_IsModuleNameBusy)(const char *name);
int REDISMODULE_API_FUNC(RedisModule_WrongArity)(RedisModuleCtx *ctx);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithLongLong)(RedisModuleCtx *ctx, long long ll);
int REDISMODULE_API_FUNC(RedisModule_GetSelectedDb)(RedisModuleCtx *ctx);
int REDISMODULE_API_FUNC(RedisModule_SelectDb)(RedisModuleCtx *ctx, int newid);
void *REDISMODULE_API_FUNC(RedisModule_OpenKey)(RedisModuleCtx *ctx, RedisModuleString *keyname, int mode);
void REDISMODULE_API_FUNC(RedisModule_CloseKey)(RedisModuleKey *kp);
int REDISMODULE_API_FUNC(RedisModule_KeyType)(RedisModuleKey *kp);
size_t REDISMODULE_API_FUNC(RedisModule_ValueLength)(RedisModuleKey *kp);
int REDISMODULE_API_FUNC(RedisModule_ListPush)(RedisModuleKey *kp, int where, RedisModuleString *ele);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_ListPop)(RedisModuleKey *key, int where);
RedisModuleCallReply *REDISMODULE_API_FUNC(RedisModule_Call)(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...);
const char *REDISMODULE_API_FUNC(RedisModule_CallReplyProto)(RedisModuleCallReply *reply, size_t *len);
void REDISMODULE_API_FUNC(RedisModule_FreeCallReply)(RedisModuleCallReply *reply);
int REDISMODULE_API_FUNC(RedisModule_CallReplyType)(RedisModuleCallReply *reply);
long long REDISMODULE_API_FUNC(RedisModule_CallReplyInteger)(RedisModuleCallReply *reply);
size_t REDISMODULE_API_FUNC(RedisModule_CallReplyLength)(RedisModuleCallReply *reply);
RedisModuleCallReply *REDISMODULE_API_FUNC(RedisModule_CallReplyArrayElement)(RedisModuleCallReply *reply, size_t idx);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateString)(RedisModuleCtx *ctx, const char *ptr, size_t len);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringFromLongLong)(RedisModuleCtx *ctx, long long ll);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringFromDouble)(RedisModuleCtx *ctx, double d);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringFromLongDouble)(RedisModuleCtx *ctx, long double ld, int humanfriendly);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringFromString)(RedisModuleCtx *ctx, const RedisModuleString *str);
#ifdef __GNUC__
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringPrintf)(RedisModuleCtx *ctx, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
#else
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringPrintf)(RedisModuleCtx *ctx, const char *fmt, ...);
#endif #endif
void REDISMODULE_API_FUNC(RedisModule_FreeString)(RedisModuleCtx *ctx, RedisModuleString *str);
const char *REDISMODULE_API_FUNC(RedisModule_StringPtrLen)(const RedisModuleString *str, size_t *len); /* Default API declaration suffix (compiler attributes) */
int REDISMODULE_API_FUNC(RedisModule_ReplyWithError)(RedisModuleCtx *ctx, const char *err); #ifndef REDISMODULE_ATTR
int REDISMODULE_API_FUNC(RedisModule_ReplyWithSimpleString)(RedisModuleCtx *ctx, const char *msg); #define REDISMODULE_ATTR REDISMODULE_ATTR_COMMON
int REDISMODULE_API_FUNC(RedisModule_ReplyWithArray)(RedisModuleCtx *ctx, long len);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithNullArray)(RedisModuleCtx *ctx);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithEmptyArray)(RedisModuleCtx *ctx);
void REDISMODULE_API_FUNC(RedisModule_ReplySetArrayLength)(RedisModuleCtx *ctx, long len);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithStringBuffer)(RedisModuleCtx *ctx, const char *buf, size_t len);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithCString)(RedisModuleCtx *ctx, const char *buf);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithString)(RedisModuleCtx *ctx, RedisModuleString *str);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithEmptyString)(RedisModuleCtx *ctx);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithVerbatimString)(RedisModuleCtx *ctx, const char *buf, size_t len);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithNull)(RedisModuleCtx *ctx);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithDouble)(RedisModuleCtx *ctx, double d);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithLongDouble)(RedisModuleCtx *ctx, long double d);
int REDISMODULE_API_FUNC(RedisModule_ReplyWithCallReply)(RedisModuleCtx *ctx, RedisModuleCallReply *reply);
int REDISMODULE_API_FUNC(RedisModule_StringToLongLong)(const RedisModuleString *str, long long *ll);
int REDISMODULE_API_FUNC(RedisModule_StringToDouble)(const RedisModuleString *str, double *d);
int REDISMODULE_API_FUNC(RedisModule_StringToLongDouble)(const RedisModuleString *str, long double *d);
void REDISMODULE_API_FUNC(RedisModule_AutoMemory)(RedisModuleCtx *ctx);
int REDISMODULE_API_FUNC(RedisModule_Replicate)(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...);
int REDISMODULE_API_FUNC(RedisModule_ReplicateVerbatim)(RedisModuleCtx *ctx);
const char *REDISMODULE_API_FUNC(RedisModule_CallReplyStringPtr)(RedisModuleCallReply *reply, size_t *len);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CreateStringFromCallReply)(RedisModuleCallReply *reply);
int REDISMODULE_API_FUNC(RedisModule_DeleteKey)(RedisModuleKey *key);
int REDISMODULE_API_FUNC(RedisModule_UnlinkKey)(RedisModuleKey *key);
int REDISMODULE_API_FUNC(RedisModule_StringSet)(RedisModuleKey *key, RedisModuleString *str);
char *REDISMODULE_API_FUNC(RedisModule_StringDMA)(RedisModuleKey *key, size_t *len, int mode);
int REDISMODULE_API_FUNC(RedisModule_StringTruncate)(RedisModuleKey *key, size_t newlen);
mstime_t REDISMODULE_API_FUNC(RedisModule_GetExpire)(RedisModuleKey *key);
int REDISMODULE_API_FUNC(RedisModule_SetExpire)(RedisModuleKey *key, mstime_t expire);
void REDISMODULE_API_FUNC(RedisModule_ResetDataset)(int restart_aof, int async);
unsigned long long REDISMODULE_API_FUNC(RedisModule_DbSize)(RedisModuleCtx *ctx);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_RandomKey)(RedisModuleCtx *ctx);
int REDISMODULE_API_FUNC(RedisModule_ZsetAdd)(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr);
int REDISMODULE_API_FUNC(RedisModule_ZsetIncrby)(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr, double *newscore);
int REDISMODULE_API_FUNC(RedisModule_ZsetScore)(RedisModuleKey *key, RedisModuleString *ele, double *score);
int REDISMODULE_API_FUNC(RedisModule_ZsetRem)(RedisModuleKey *key, RedisModuleString *ele, int *deleted);
void REDISMODULE_API_FUNC(RedisModule_ZsetRangeStop)(RedisModuleKey *key);
int REDISMODULE_API_FUNC(RedisModule_ZsetFirstInScoreRange)(RedisModuleKey *key, double min, double max, int minex, int maxex);
int REDISMODULE_API_FUNC(RedisModule_ZsetLastInScoreRange)(RedisModuleKey *key, double min, double max, int minex, int maxex);
int REDISMODULE_API_FUNC(RedisModule_ZsetFirstInLexRange)(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max);
int REDISMODULE_API_FUNC(RedisModule_ZsetLastInLexRange)(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_ZsetRangeCurrentElement)(RedisModuleKey *key, double *score);
int REDISMODULE_API_FUNC(RedisModule_ZsetRangeNext)(RedisModuleKey *key);
int REDISMODULE_API_FUNC(RedisModule_ZsetRangePrev)(RedisModuleKey *key);
int REDISMODULE_API_FUNC(RedisModule_ZsetRangeEndReached)(RedisModuleKey *key);
int REDISMODULE_API_FUNC(RedisModule_HashSet)(RedisModuleKey *key, int flags, ...);
int REDISMODULE_API_FUNC(RedisModule_HashGet)(RedisModuleKey *key, int flags, ...);
int REDISMODULE_API_FUNC(RedisModule_IsKeysPositionRequest)(RedisModuleCtx *ctx);
void REDISMODULE_API_FUNC(RedisModule_KeyAtPos)(RedisModuleCtx *ctx, int pos);
unsigned long long REDISMODULE_API_FUNC(RedisModule_GetClientId)(RedisModuleCtx *ctx);
int REDISMODULE_API_FUNC(RedisModule_GetClientInfoById)(void *ci, uint64_t id);
int REDISMODULE_API_FUNC(RedisModule_PublishMessage)(RedisModuleCtx *ctx, RedisModuleString *channel, RedisModuleString *message);
int REDISMODULE_API_FUNC(RedisModule_GetContextFlags)(RedisModuleCtx *ctx);
int REDISMODULE_API_FUNC(RedisModule_AvoidReplicaTraffic)();
void *REDISMODULE_API_FUNC(RedisModule_PoolAlloc)(RedisModuleCtx *ctx, size_t bytes);
RedisModuleType *REDISMODULE_API_FUNC(RedisModule_CreateDataType)(RedisModuleCtx *ctx, const char *name, int encver, RedisModuleTypeMethods *typemethods);
int REDISMODULE_API_FUNC(RedisModule_ModuleTypeSetValue)(RedisModuleKey *key, RedisModuleType *mt, void *value);
int REDISMODULE_API_FUNC(RedisModule_ModuleTypeReplaceValue)(RedisModuleKey *key, RedisModuleType *mt, void *new_value, void **old_value);
RedisModuleType *REDISMODULE_API_FUNC(RedisModule_ModuleTypeGetType)(RedisModuleKey *key);
void *REDISMODULE_API_FUNC(RedisModule_ModuleTypeGetValue)(RedisModuleKey *key);
int REDISMODULE_API_FUNC(RedisModule_IsIOError)(RedisModuleIO *io);
void REDISMODULE_API_FUNC(RedisModule_SetModuleOptions)(RedisModuleCtx *ctx, int options);
int REDISMODULE_API_FUNC(RedisModule_SignalModifiedKey)(RedisModuleCtx *ctx, RedisModuleString *keyname);
void REDISMODULE_API_FUNC(RedisModule_SaveUnsigned)(RedisModuleIO *io, uint64_t value);
uint64_t REDISMODULE_API_FUNC(RedisModule_LoadUnsigned)(RedisModuleIO *io);
void REDISMODULE_API_FUNC(RedisModule_SaveSigned)(RedisModuleIO *io, int64_t value);
int64_t REDISMODULE_API_FUNC(RedisModule_LoadSigned)(RedisModuleIO *io);
void REDISMODULE_API_FUNC(RedisModule_EmitAOF)(RedisModuleIO *io, const char *cmdname, const char *fmt, ...);
void REDISMODULE_API_FUNC(RedisModule_SaveString)(RedisModuleIO *io, RedisModuleString *s);
void REDISMODULE_API_FUNC(RedisModule_SaveStringBuffer)(RedisModuleIO *io, const char *str, size_t len);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_LoadString)(RedisModuleIO *io);
char *REDISMODULE_API_FUNC(RedisModule_LoadStringBuffer)(RedisModuleIO *io, size_t *lenptr);
void REDISMODULE_API_FUNC(RedisModule_SaveDouble)(RedisModuleIO *io, double value);
double REDISMODULE_API_FUNC(RedisModule_LoadDouble)(RedisModuleIO *io);
void REDISMODULE_API_FUNC(RedisModule_SaveFloat)(RedisModuleIO *io, float value);
float REDISMODULE_API_FUNC(RedisModule_LoadFloat)(RedisModuleIO *io);
void REDISMODULE_API_FUNC(RedisModule_SaveLongDouble)(RedisModuleIO *io, long double value);
long double REDISMODULE_API_FUNC(RedisModule_LoadLongDouble)(RedisModuleIO *io);
void *REDISMODULE_API_FUNC(RedisModule_LoadDataTypeFromString)(const RedisModuleString *str, const RedisModuleType *mt);
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_SaveDataTypeToString)(RedisModuleCtx *ctx, void *data, const RedisModuleType *mt);
#ifdef __GNUC__
void REDISMODULE_API_FUNC(RedisModule_Log)(RedisModuleCtx *ctx, const char *level, const char *fmt, ...) __attribute__ ((format (printf, 3, 4)));
void REDISMODULE_API_FUNC(RedisModule_LogIOError)(RedisModuleIO *io, const char *levelstr, const char *fmt, ...) __attribute__ ((format (printf, 3, 4)));
#else
void REDISMODULE_API_FUNC(RedisModule_Log)(RedisModuleCtx *ctx, const char *level, const char *fmt, ...);
void REDISMODULE_API_FUNC(RedisModule_LogIOError)(RedisModuleIO *io, const char *levelstr, const char *fmt, ...);
#endif #endif
void REDISMODULE_API_FUNC(RedisModule__Assert)(const char *estr, const char *file, int line);
void REDISMODULE_API_FUNC(RedisModule_LatencyAddSample)(const char *event, mstime_t latency); REDISMODULE_API void * (*RedisModule_Alloc)(size_t bytes) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_StringAppendBuffer)(RedisModuleCtx *ctx, RedisModuleString *str, const char *buf, size_t len); REDISMODULE_API void * (*RedisModule_Realloc)(void *ptr, size_t bytes) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_RetainString)(RedisModuleCtx *ctx, RedisModuleString *str); REDISMODULE_API void (*RedisModule_Free)(void *ptr) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_StringCompare)(RedisModuleString *a, RedisModuleString *b); REDISMODULE_API void * (*RedisModule_Calloc)(size_t nmemb, size_t size) REDISMODULE_ATTR;
RedisModuleCtx *REDISMODULE_API_FUNC(RedisModule_GetContextFromIO)(RedisModuleIO *io); REDISMODULE_API char * (*RedisModule_Strdup)(const char *str) REDISMODULE_ATTR;
const RedisModuleString *REDISMODULE_API_FUNC(RedisModule_GetKeyNameFromIO)(RedisModuleIO *io); REDISMODULE_API int (*RedisModule_GetApi)(const char *, void *) REDISMODULE_ATTR;
const RedisModuleString *REDISMODULE_API_FUNC(RedisModule_GetKeyNameFromModuleKey)(RedisModuleKey *key); REDISMODULE_API int (*RedisModule_CreateCommand)(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep) REDISMODULE_ATTR;
long long REDISMODULE_API_FUNC(RedisModule_Milliseconds)(void); REDISMODULE_API void (*RedisModule_SetModuleAttribs)(RedisModuleCtx *ctx, const char *name, int ver, int apiver) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_DigestAddStringBuffer)(RedisModuleDigest *md, unsigned char *ele, size_t len); REDISMODULE_API int (*RedisModule_IsModuleNameBusy)(const char *name) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_DigestAddLongLong)(RedisModuleDigest *md, long long ele); REDISMODULE_API int (*RedisModule_WrongArity)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_DigestEndSequence)(RedisModuleDigest *md); REDISMODULE_API int (*RedisModule_ReplyWithLongLong)(RedisModuleCtx *ctx, long long ll) REDISMODULE_ATTR;
RedisModuleDict *REDISMODULE_API_FUNC(RedisModule_CreateDict)(RedisModuleCtx *ctx); REDISMODULE_API int (*RedisModule_GetSelectedDb)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_FreeDict)(RedisModuleCtx *ctx, RedisModuleDict *d); REDISMODULE_API int (*RedisModule_SelectDb)(RedisModuleCtx *ctx, int newid) REDISMODULE_ATTR;
uint64_t REDISMODULE_API_FUNC(RedisModule_DictSize)(RedisModuleDict *d); REDISMODULE_API void * (*RedisModule_OpenKey)(RedisModuleCtx *ctx, RedisModuleString *keyname, int mode) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictSetC)(RedisModuleDict *d, void *key, size_t keylen, void *ptr); REDISMODULE_API void (*RedisModule_CloseKey)(RedisModuleKey *kp) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictReplaceC)(RedisModuleDict *d, void *key, size_t keylen, void *ptr); REDISMODULE_API int (*RedisModule_KeyType)(RedisModuleKey *kp) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictSet)(RedisModuleDict *d, RedisModuleString *key, void *ptr); REDISMODULE_API size_t (*RedisModule_ValueLength)(RedisModuleKey *kp) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictReplace)(RedisModuleDict *d, RedisModuleString *key, void *ptr); REDISMODULE_API int (*RedisModule_ListPush)(RedisModuleKey *kp, int where, RedisModuleString *ele) REDISMODULE_ATTR;
void *REDISMODULE_API_FUNC(RedisModule_DictGetC)(RedisModuleDict *d, void *key, size_t keylen, int *nokey); REDISMODULE_API RedisModuleString * (*RedisModule_ListPop)(RedisModuleKey *key, int where) REDISMODULE_ATTR;
void *REDISMODULE_API_FUNC(RedisModule_DictGet)(RedisModuleDict *d, RedisModuleString *key, int *nokey); REDISMODULE_API RedisModuleCallReply * (*RedisModule_Call)(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictDelC)(RedisModuleDict *d, void *key, size_t keylen, void *oldval); REDISMODULE_API const char * (*RedisModule_CallReplyProto)(RedisModuleCallReply *reply, size_t *len) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictDel)(RedisModuleDict *d, RedisModuleString *key, void *oldval); REDISMODULE_API void (*RedisModule_FreeCallReply)(RedisModuleCallReply *reply) REDISMODULE_ATTR;
RedisModuleDictIter *REDISMODULE_API_FUNC(RedisModule_DictIteratorStartC)(RedisModuleDict *d, const char *op, void *key, size_t keylen); REDISMODULE_API int (*RedisModule_CallReplyType)(RedisModuleCallReply *reply) REDISMODULE_ATTR;
RedisModuleDictIter *REDISMODULE_API_FUNC(RedisModule_DictIteratorStart)(RedisModuleDict *d, const char *op, RedisModuleString *key); REDISMODULE_API long long (*RedisModule_CallReplyInteger)(RedisModuleCallReply *reply) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_DictIteratorStop)(RedisModuleDictIter *di); REDISMODULE_API size_t (*RedisModule_CallReplyLength)(RedisModuleCallReply *reply) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictIteratorReseekC)(RedisModuleDictIter *di, const char *op, void *key, size_t keylen); REDISMODULE_API RedisModuleCallReply * (*RedisModule_CallReplyArrayElement)(RedisModuleCallReply *reply, size_t idx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictIteratorReseek)(RedisModuleDictIter *di, const char *op, RedisModuleString *key); REDISMODULE_API RedisModuleString * (*RedisModule_CreateString)(RedisModuleCtx *ctx, const char *ptr, size_t len) REDISMODULE_ATTR;
void *REDISMODULE_API_FUNC(RedisModule_DictNextC)(RedisModuleDictIter *di, size_t *keylen, void **dataptr); REDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromLongLong)(RedisModuleCtx *ctx, long long ll) REDISMODULE_ATTR;
void *REDISMODULE_API_FUNC(RedisModule_DictPrevC)(RedisModuleDictIter *di, size_t *keylen, void **dataptr); REDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromDouble)(RedisModuleCtx *ctx, double d) REDISMODULE_ATTR;
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_DictNext)(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr); REDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromLongDouble)(RedisModuleCtx *ctx, long double ld, int humanfriendly) REDISMODULE_ATTR;
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_DictPrev)(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr); REDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromString)(RedisModuleCtx *ctx, const RedisModuleString *str) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictCompareC)(RedisModuleDictIter *di, const char *op, void *key, size_t keylen); REDISMODULE_API RedisModuleString * (*RedisModule_CreateStringPrintf)(RedisModuleCtx *ctx, const char *fmt, ...) REDISMODULE_ATTR_PRINTF(2,3) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_DictCompare)(RedisModuleDictIter *di, const char *op, RedisModuleString *key); REDISMODULE_API void (*RedisModule_FreeString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_RegisterInfoFunc)(RedisModuleCtx *ctx, RedisModuleInfoFunc cb); REDISMODULE_API const char * (*RedisModule_StringPtrLen)(const RedisModuleString *str, size_t *len) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_InfoAddSection)(RedisModuleInfoCtx *ctx, char *name); REDISMODULE_API int (*RedisModule_ReplyWithError)(RedisModuleCtx *ctx, const char *err) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_InfoBeginDictField)(RedisModuleInfoCtx *ctx, char *name); REDISMODULE_API int (*RedisModule_ReplyWithSimpleString)(RedisModuleCtx *ctx, const char *msg) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_InfoEndDictField)(RedisModuleInfoCtx *ctx); REDISMODULE_API int (*RedisModule_ReplyWithArray)(RedisModuleCtx *ctx, long len) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_InfoAddFieldString)(RedisModuleInfoCtx *ctx, char *field, RedisModuleString *value); REDISMODULE_API int (*RedisModule_ReplyWithNullArray)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_InfoAddFieldCString)(RedisModuleInfoCtx *ctx, char *field, char *value); REDISMODULE_API int (*RedisModule_ReplyWithEmptyArray)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_InfoAddFieldDouble)(RedisModuleInfoCtx *ctx, char *field, double value); REDISMODULE_API void (*RedisModule_ReplySetArrayLength)(RedisModuleCtx *ctx, long len) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_InfoAddFieldLongLong)(RedisModuleInfoCtx *ctx, char *field, long long value); REDISMODULE_API int (*RedisModule_ReplyWithStringBuffer)(RedisModuleCtx *ctx, const char *buf, size_t len) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_InfoAddFieldULongLong)(RedisModuleInfoCtx *ctx, char *field, unsigned long long value); REDISMODULE_API int (*RedisModule_ReplyWithCString)(RedisModuleCtx *ctx, const char *buf) REDISMODULE_ATTR;
RedisModuleServerInfoData *REDISMODULE_API_FUNC(RedisModule_GetServerInfo)(RedisModuleCtx *ctx, const char *section); REDISMODULE_API int (*RedisModule_ReplyWithString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_FreeServerInfo)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data); REDISMODULE_API int (*RedisModule_ReplyWithEmptyString)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_ServerInfoGetField)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field); REDISMODULE_API int (*RedisModule_ReplyWithVerbatimString)(RedisModuleCtx *ctx, const char *buf, size_t len) REDISMODULE_ATTR;
const char *REDISMODULE_API_FUNC(RedisModule_ServerInfoGetFieldC)(RedisModuleServerInfoData *data, const char* field); REDISMODULE_API int (*RedisModule_ReplyWithNull)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
long long REDISMODULE_API_FUNC(RedisModule_ServerInfoGetFieldSigned)(RedisModuleServerInfoData *data, const char* field, int *out_err); REDISMODULE_API int (*RedisModule_ReplyWithDouble)(RedisModuleCtx *ctx, double d) REDISMODULE_ATTR;
unsigned long long REDISMODULE_API_FUNC(RedisModule_ServerInfoGetFieldUnsigned)(RedisModuleServerInfoData *data, const char* field, int *out_err); REDISMODULE_API int (*RedisModule_ReplyWithLongDouble)(RedisModuleCtx *ctx, long double d) REDISMODULE_ATTR;
double REDISMODULE_API_FUNC(RedisModule_ServerInfoGetFieldDouble)(RedisModuleServerInfoData *data, const char* field, int *out_err); REDISMODULE_API int (*RedisModule_ReplyWithCallReply)(RedisModuleCtx *ctx, RedisModuleCallReply *reply) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_SubscribeToServerEvent)(RedisModuleCtx *ctx, RedisModuleEvent event, RedisModuleEventCallback callback); REDISMODULE_API int (*RedisModule_StringToLongLong)(const RedisModuleString *str, long long *ll) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_SetLRU)(RedisModuleKey *key, mstime_t lru_idle); REDISMODULE_API int (*RedisModule_StringToDouble)(const RedisModuleString *str, double *d) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_GetLRU)(RedisModuleKey *key, mstime_t *lru_idle); REDISMODULE_API int (*RedisModule_StringToLongDouble)(const RedisModuleString *str, long double *d) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_SetLFU)(RedisModuleKey *key, long long lfu_freq); REDISMODULE_API void (*RedisModule_AutoMemory)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_GetLFU)(RedisModuleKey *key, long long *lfu_freq); REDISMODULE_API int (*RedisModule_Replicate)(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...) REDISMODULE_ATTR;
RedisModuleBlockedClient *REDISMODULE_API_FUNC(RedisModule_BlockClientOnKeys)(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms, RedisModuleString **keys, int numkeys, void *privdata); REDISMODULE_API int (*RedisModule_ReplicateVerbatim)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_SignalKeyAsReady)(RedisModuleCtx *ctx, RedisModuleString *key); REDISMODULE_API const char * (*RedisModule_CallReplyStringPtr)(RedisModuleCallReply *reply, size_t *len) REDISMODULE_ATTR;
RedisModuleString *REDISMODULE_API_FUNC(RedisModule_GetBlockedClientReadyKey)(RedisModuleCtx *ctx); REDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromCallReply)(RedisModuleCallReply *reply) REDISMODULE_ATTR;
RedisModuleScanCursor *REDISMODULE_API_FUNC(RedisModule_ScanCursorCreate)(); REDISMODULE_API int (*RedisModule_DeleteKey)(RedisModuleKey *key) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_ScanCursorRestart)(RedisModuleScanCursor *cursor); REDISMODULE_API int (*RedisModule_UnlinkKey)(RedisModuleKey *key) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_ScanCursorDestroy)(RedisModuleScanCursor *cursor); REDISMODULE_API int (*RedisModule_StringSet)(RedisModuleKey *key, RedisModuleString *str) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_Scan)(RedisModuleCtx *ctx, RedisModuleScanCursor *cursor, RedisModuleScanCB fn, void *privdata); REDISMODULE_API char * (*RedisModule_StringDMA)(RedisModuleKey *key, size_t *len, int mode) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_ScanKey)(RedisModuleKey *key, RedisModuleScanCursor *cursor, RedisModuleScanKeyCB fn, void *privdata); REDISMODULE_API int (*RedisModule_StringTruncate)(RedisModuleKey *key, size_t newlen) REDISMODULE_ATTR;
REDISMODULE_API mstime_t (*RedisModule_GetExpire)(RedisModuleKey *key) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_SetExpire)(RedisModuleKey *key, mstime_t expire) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_ResetDataset)(int restart_aof, int async) REDISMODULE_ATTR;
REDISMODULE_API unsigned long long (*RedisModule_DbSize)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_RandomKey)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetAdd)(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetIncrby)(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr, double *newscore) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetScore)(RedisModuleKey *key, RedisModuleString *ele, double *score) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetRem)(RedisModuleKey *key, RedisModuleString *ele, int *deleted) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_ZsetRangeStop)(RedisModuleKey *key) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetFirstInScoreRange)(RedisModuleKey *key, double min, double max, int minex, int maxex) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetLastInScoreRange)(RedisModuleKey *key, double min, double max, int minex, int maxex) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetFirstInLexRange)(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetLastInLexRange)(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_ZsetRangeCurrentElement)(RedisModuleKey *key, double *score) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetRangeNext)(RedisModuleKey *key) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetRangePrev)(RedisModuleKey *key) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ZsetRangeEndReached)(RedisModuleKey *key) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_HashSet)(RedisModuleKey *key, int flags, ...) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_HashGet)(RedisModuleKey *key, int flags, ...) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_IsKeysPositionRequest)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_KeyAtPos)(RedisModuleCtx *ctx, int pos) REDISMODULE_ATTR;
REDISMODULE_API unsigned long long (*RedisModule_GetClientId)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_GetClientInfoById)(void *ci, uint64_t id) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_PublishMessage)(RedisModuleCtx *ctx, RedisModuleString *channel, RedisModuleString *message) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_GetContextFlags)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_AvoidReplicaTraffic)() REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_PoolAlloc)(RedisModuleCtx *ctx, size_t bytes) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleType * (*RedisModule_CreateDataType)(RedisModuleCtx *ctx, const char *name, int encver, RedisModuleTypeMethods *typemethods) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ModuleTypeSetValue)(RedisModuleKey *key, RedisModuleType *mt, void *value) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ModuleTypeReplaceValue)(RedisModuleKey *key, RedisModuleType *mt, void *new_value, void **old_value) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleType * (*RedisModule_ModuleTypeGetType)(RedisModuleKey *key) REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_ModuleTypeGetValue)(RedisModuleKey *key) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_IsIOError)(RedisModuleIO *io) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_SetModuleOptions)(RedisModuleCtx *ctx, int options) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_SignalModifiedKey)(RedisModuleCtx *ctx, RedisModuleString *keyname) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_SaveUnsigned)(RedisModuleIO *io, uint64_t value) REDISMODULE_ATTR;
REDISMODULE_API uint64_t (*RedisModule_LoadUnsigned)(RedisModuleIO *io) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_SaveSigned)(RedisModuleIO *io, int64_t value) REDISMODULE_ATTR;
REDISMODULE_API int64_t (*RedisModule_LoadSigned)(RedisModuleIO *io) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_EmitAOF)(RedisModuleIO *io, const char *cmdname, const char *fmt, ...) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_SaveString)(RedisModuleIO *io, RedisModuleString *s) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_SaveStringBuffer)(RedisModuleIO *io, const char *str, size_t len) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_LoadString)(RedisModuleIO *io) REDISMODULE_ATTR;
REDISMODULE_API char * (*RedisModule_LoadStringBuffer)(RedisModuleIO *io, size_t *lenptr) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_SaveDouble)(RedisModuleIO *io, double value) REDISMODULE_ATTR;
REDISMODULE_API double (*RedisModule_LoadDouble)(RedisModuleIO *io) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_SaveFloat)(RedisModuleIO *io, float value) REDISMODULE_ATTR;
REDISMODULE_API float (*RedisModule_LoadFloat)(RedisModuleIO *io) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_SaveLongDouble)(RedisModuleIO *io, long double value) REDISMODULE_ATTR;
REDISMODULE_API long double (*RedisModule_LoadLongDouble)(RedisModuleIO *io) REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_LoadDataTypeFromString)(const RedisModuleString *str, const RedisModuleType *mt) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_SaveDataTypeToString)(RedisModuleCtx *ctx, void *data, const RedisModuleType *mt) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_Log)(RedisModuleCtx *ctx, const char *level, const char *fmt, ...) REDISMODULE_ATTR REDISMODULE_ATTR_PRINTF(3,4);
REDISMODULE_API void (*RedisModule_LogIOError)(RedisModuleIO *io, const char *levelstr, const char *fmt, ...) REDISMODULE_ATTR REDISMODULE_ATTR_PRINTF(3,4);
REDISMODULE_API void (*RedisModule__Assert)(const char *estr, const char *file, int line) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_LatencyAddSample)(const char *event, mstime_t latency) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_StringAppendBuffer)(RedisModuleCtx *ctx, RedisModuleString *str, const char *buf, size_t len) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_RetainString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_HoldString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_StringCompare)(RedisModuleString *a, RedisModuleString *b) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleCtx * (*RedisModule_GetContextFromIO)(RedisModuleIO *io) REDISMODULE_ATTR;
REDISMODULE_API const RedisModuleString * (*RedisModule_GetKeyNameFromIO)(RedisModuleIO *io) REDISMODULE_ATTR;
REDISMODULE_API const RedisModuleString * (*RedisModule_GetKeyNameFromModuleKey)(RedisModuleKey *key) REDISMODULE_ATTR;
REDISMODULE_API long long (*RedisModule_Milliseconds)(void) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_DigestAddStringBuffer)(RedisModuleDigest *md, unsigned char *ele, size_t len) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_DigestAddLongLong)(RedisModuleDigest *md, long long ele) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_DigestEndSequence)(RedisModuleDigest *md) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleDict * (*RedisModule_CreateDict)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_FreeDict)(RedisModuleCtx *ctx, RedisModuleDict *d) REDISMODULE_ATTR;
REDISMODULE_API uint64_t (*RedisModule_DictSize)(RedisModuleDict *d) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictSetC)(RedisModuleDict *d, void *key, size_t keylen, void *ptr) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictReplaceC)(RedisModuleDict *d, void *key, size_t keylen, void *ptr) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictSet)(RedisModuleDict *d, RedisModuleString *key, void *ptr) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictReplace)(RedisModuleDict *d, RedisModuleString *key, void *ptr) REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_DictGetC)(RedisModuleDict *d, void *key, size_t keylen, int *nokey) REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_DictGet)(RedisModuleDict *d, RedisModuleString *key, int *nokey) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictDelC)(RedisModuleDict *d, void *key, size_t keylen, void *oldval) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictDel)(RedisModuleDict *d, RedisModuleString *key, void *oldval) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleDictIter * (*RedisModule_DictIteratorStartC)(RedisModuleDict *d, const char *op, void *key, size_t keylen) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleDictIter * (*RedisModule_DictIteratorStart)(RedisModuleDict *d, const char *op, RedisModuleString *key) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_DictIteratorStop)(RedisModuleDictIter *di) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictIteratorReseekC)(RedisModuleDictIter *di, const char *op, void *key, size_t keylen) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictIteratorReseek)(RedisModuleDictIter *di, const char *op, RedisModuleString *key) REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_DictNextC)(RedisModuleDictIter *di, size_t *keylen, void **dataptr) REDISMODULE_ATTR;
REDISMODULE_API void * (*RedisModule_DictPrevC)(RedisModuleDictIter *di, size_t *keylen, void **dataptr) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_DictNext)(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_DictPrev)(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictCompareC)(RedisModuleDictIter *di, const char *op, void *key, size_t keylen) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DictCompare)(RedisModuleDictIter *di, const char *op, RedisModuleString *key) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_RegisterInfoFunc)(RedisModuleCtx *ctx, RedisModuleInfoFunc cb) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_InfoAddSection)(RedisModuleInfoCtx *ctx, char *name) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_InfoBeginDictField)(RedisModuleInfoCtx *ctx, char *name) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_InfoEndDictField)(RedisModuleInfoCtx *ctx) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_InfoAddFieldString)(RedisModuleInfoCtx *ctx, char *field, RedisModuleString *value) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_InfoAddFieldCString)(RedisModuleInfoCtx *ctx, char *field, char *value) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_InfoAddFieldDouble)(RedisModuleInfoCtx *ctx, char *field, double value) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_InfoAddFieldLongLong)(RedisModuleInfoCtx *ctx, char *field, long long value) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_InfoAddFieldULongLong)(RedisModuleInfoCtx *ctx, char *field, unsigned long long value) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleServerInfoData * (*RedisModule_GetServerInfo)(RedisModuleCtx *ctx, const char *section) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_FreeServerInfo)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_ServerInfoGetField)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field) REDISMODULE_ATTR;
REDISMODULE_API const char * (*RedisModule_ServerInfoGetFieldC)(RedisModuleServerInfoData *data, const char* field) REDISMODULE_ATTR;
REDISMODULE_API long long (*RedisModule_ServerInfoGetFieldSigned)(RedisModuleServerInfoData *data, const char* field, int *out_err) REDISMODULE_ATTR;
REDISMODULE_API unsigned long long (*RedisModule_ServerInfoGetFieldUnsigned)(RedisModuleServerInfoData *data, const char* field, int *out_err) REDISMODULE_ATTR;
REDISMODULE_API double (*RedisModule_ServerInfoGetFieldDouble)(RedisModuleServerInfoData *data, const char* field, int *out_err) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_SubscribeToServerEvent)(RedisModuleCtx *ctx, RedisModuleEvent event, RedisModuleEventCallback callback) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_SetLRU)(RedisModuleKey *key, mstime_t lru_idle) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_GetLRU)(RedisModuleKey *key, mstime_t *lru_idle) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_SetLFU)(RedisModuleKey *key, long long lfu_freq) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_GetLFU)(RedisModuleKey *key, long long *lfu_freq) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleBlockedClient * (*RedisModule_BlockClientOnKeys)(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms, RedisModuleString **keys, int numkeys, void *privdata) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_SignalKeyAsReady)(RedisModuleCtx *ctx, RedisModuleString *key) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_GetBlockedClientReadyKey)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleScanCursor * (*RedisModule_ScanCursorCreate)() REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_ScanCursorRestart)(RedisModuleScanCursor *cursor) REDISMODULE_ATTR;
REDISMODULE_API void (*RedisModule_ScanCursorDestroy)(RedisModuleScanCursor *cursor) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_Scan)(RedisModuleCtx *ctx, RedisModuleScanCursor *cursor, RedisModuleScanCB fn, void *privdata) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_ScanKey)(RedisModuleKey *key, RedisModuleScanCursor *cursor, RedisModuleScanKeyCB fn, void *privdata) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_GetContextFlagsAll)() REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_GetKeyspaceNotificationFlagsAll)() REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_IsSubEventSupported)(RedisModuleEvent event, uint64_t subevent) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_GetServerVersion)() REDISMODULE_ATTR;
/* Experimental APIs */ /* Experimental APIs */
#ifdef REDISMODULE_EXPERIMENTAL_API #ifdef REDISMODULE_EXPERIMENTAL_API
#define REDISMODULE_EXPERIMENTAL_API_VERSION 3 #define REDISMODULE_EXPERIMENTAL_API_VERSION 3
RedisModuleBlockedClient *REDISMODULE_API_FUNC(RedisModule_BlockClient)(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms); REDISMODULE_API RedisModuleBlockedClient * (*RedisModule_BlockClient)(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_UnblockClient)(RedisModuleBlockedClient *bc, void *privdata); REDISMODULE_API int (*RedisModule_UnblockClient)(RedisModuleBlockedClient *bc, void *privdata) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_IsBlockedReplyRequest)(RedisModuleCtx *ctx); REDISMODULE_API int (*RedisModule_IsBlockedReplyRequest)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_IsBlockedTimeoutRequest)(RedisModuleCtx *ctx); REDISMODULE_API int (*RedisModule_IsBlockedTimeoutRequest)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
void *REDISMODULE_API_FUNC(RedisModule_GetBlockedClientPrivateData)(RedisModuleCtx *ctx); REDISMODULE_API void * (*RedisModule_GetBlockedClientPrivateData)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
RedisModuleBlockedClient *REDISMODULE_API_FUNC(RedisModule_GetBlockedClientHandle)(RedisModuleCtx *ctx); REDISMODULE_API RedisModuleBlockedClient * (*RedisModule_GetBlockedClientHandle)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_AbortBlock)(RedisModuleBlockedClient *bc); REDISMODULE_API int (*RedisModule_AbortBlock)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;
RedisModuleCtx *REDISMODULE_API_FUNC(RedisModule_GetThreadSafeContext)(RedisModuleBlockedClient *bc); REDISMODULE_API RedisModuleCtx * (*RedisModule_GetThreadSafeContext)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_FreeThreadSafeContext)(RedisModuleCtx *ctx); REDISMODULE_API RedisModuleCtx * (*RedisModule_GetDetachedThreadSafeContext)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_ThreadSafeContextLock)(RedisModuleCtx *ctx); REDISMODULE_API void (*RedisModule_FreeThreadSafeContext)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_ThreadSafeContextUnlock)(RedisModuleCtx *ctx); REDISMODULE_API void (*RedisModule_ThreadSafeContextLock)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_SubscribeToKeyspaceEvents)(RedisModuleCtx *ctx, int types, RedisModuleNotificationFunc cb); REDISMODULE_API int (*RedisModule_ThreadSafeContextTryLock)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_NotifyKeyspaceEvent)(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key); REDISMODULE_API void (*RedisModule_ThreadSafeContextUnlock)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_GetNotifyKeyspaceEvents)(); REDISMODULE_API int (*RedisModule_SubscribeToKeyspaceEvents)(RedisModuleCtx *ctx, int types, RedisModuleNotificationFunc cb) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_BlockedClientDisconnected)(RedisModuleCtx *ctx); REDISMODULE_API int (*RedisModule_NotifyKeyspaceEvent)(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_RegisterClusterMessageReceiver)(RedisModuleCtx *ctx, uint8_t type, RedisModuleClusterMessageReceiver callback); REDISMODULE_API int (*RedisModule_GetNotifyKeyspaceEvents)() REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_SendClusterMessage)(RedisModuleCtx *ctx, char *target_id, uint8_t type, unsigned char *msg, uint32_t len); REDISMODULE_API int (*RedisModule_BlockedClientDisconnected)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_GetClusterNodeInfo)(RedisModuleCtx *ctx, const char *id, char *ip, char *master_id, int *port, int *flags); REDISMODULE_API void (*RedisModule_RegisterClusterMessageReceiver)(RedisModuleCtx *ctx, uint8_t type, RedisModuleClusterMessageReceiver callback) REDISMODULE_ATTR;
char **REDISMODULE_API_FUNC(RedisModule_GetClusterNodesList)(RedisModuleCtx *ctx, size_t *numnodes); REDISMODULE_API int (*RedisModule_SendClusterMessage)(RedisModuleCtx *ctx, char *target_id, uint8_t type, unsigned char *msg, uint32_t len) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_FreeClusterNodesList)(char **ids); REDISMODULE_API int (*RedisModule_GetClusterNodeInfo)(RedisModuleCtx *ctx, const char *id, char *ip, char *master_id, int *port, int *flags) REDISMODULE_ATTR;
RedisModuleTimerID REDISMODULE_API_FUNC(RedisModule_CreateTimer)(RedisModuleCtx *ctx, mstime_t period, RedisModuleTimerProc callback, void *data); REDISMODULE_API char ** (*RedisModule_GetClusterNodesList)(RedisModuleCtx *ctx, size_t *numnodes) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_StopTimer)(RedisModuleCtx *ctx, RedisModuleTimerID id, void **data); REDISMODULE_API void (*RedisModule_FreeClusterNodesList)(char **ids) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_GetTimerInfo)(RedisModuleCtx *ctx, RedisModuleTimerID id, uint64_t *remaining, void **data); REDISMODULE_API RedisModuleTimerID (*RedisModule_CreateTimer)(RedisModuleCtx *ctx, mstime_t period, RedisModuleTimerProc callback, void *data) REDISMODULE_ATTR;
const char *REDISMODULE_API_FUNC(RedisModule_GetMyClusterID)(void); REDISMODULE_API int (*RedisModule_StopTimer)(RedisModuleCtx *ctx, RedisModuleTimerID id, void **data) REDISMODULE_ATTR;
size_t REDISMODULE_API_FUNC(RedisModule_GetClusterSize)(void); REDISMODULE_API int (*RedisModule_GetTimerInfo)(RedisModuleCtx *ctx, RedisModuleTimerID id, uint64_t *remaining, void **data) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_GetRandomBytes)(unsigned char *dst, size_t len); REDISMODULE_API const char * (*RedisModule_GetMyClusterID)(void) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_GetRandomHexChars)(char *dst, size_t len); REDISMODULE_API size_t (*RedisModule_GetClusterSize)(void) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_SetDisconnectCallback)(RedisModuleBlockedClient *bc, RedisModuleDisconnectFunc callback); REDISMODULE_API void (*RedisModule_GetRandomBytes)(unsigned char *dst, size_t len) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_SetClusterFlags)(RedisModuleCtx *ctx, uint64_t flags); REDISMODULE_API void (*RedisModule_GetRandomHexChars)(char *dst, size_t len) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_ExportSharedAPI)(RedisModuleCtx *ctx, const char *apiname, void *func); REDISMODULE_API void (*RedisModule_SetDisconnectCallback)(RedisModuleBlockedClient *bc, RedisModuleDisconnectFunc callback) REDISMODULE_ATTR;
void *REDISMODULE_API_FUNC(RedisModule_GetSharedAPI)(RedisModuleCtx *ctx, const char *apiname); REDISMODULE_API void (*RedisModule_SetClusterFlags)(RedisModuleCtx *ctx, uint64_t flags) REDISMODULE_ATTR;
RedisModuleCommandFilter *REDISMODULE_API_FUNC(RedisModule_RegisterCommandFilter)(RedisModuleCtx *ctx, RedisModuleCommandFilterFunc cb, int flags); REDISMODULE_API int (*RedisModule_ExportSharedAPI)(RedisModuleCtx *ctx, const char *apiname, void *func) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_UnregisterCommandFilter)(RedisModuleCtx *ctx, RedisModuleCommandFilter *filter); REDISMODULE_API void * (*RedisModule_GetSharedAPI)(RedisModuleCtx *ctx, const char *apiname) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_CommandFilterArgsCount)(RedisModuleCommandFilterCtx *fctx); REDISMODULE_API RedisModuleCommandFilter * (*RedisModule_RegisterCommandFilter)(RedisModuleCtx *ctx, RedisModuleCommandFilterFunc cb, int flags) REDISMODULE_ATTR;
const RedisModuleString *REDISMODULE_API_FUNC(RedisModule_CommandFilterArgGet)(RedisModuleCommandFilterCtx *fctx, int pos); REDISMODULE_API int (*RedisModule_UnregisterCommandFilter)(RedisModuleCtx *ctx, RedisModuleCommandFilter *filter) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_CommandFilterArgInsert)(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg); REDISMODULE_API int (*RedisModule_CommandFilterArgsCount)(RedisModuleCommandFilterCtx *fctx) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_CommandFilterArgReplace)(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg); REDISMODULE_API const RedisModuleString * (*RedisModule_CommandFilterArgGet)(RedisModuleCommandFilterCtx *fctx, int pos) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_CommandFilterArgDelete)(RedisModuleCommandFilterCtx *fctx, int pos); REDISMODULE_API int (*RedisModule_CommandFilterArgInsert)(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_Fork)(RedisModuleForkDoneHandler cb, void *user_data); REDISMODULE_API int (*RedisModule_CommandFilterArgReplace)(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_ExitFromChild)(int retcode); REDISMODULE_API int (*RedisModule_CommandFilterArgDelete)(RedisModuleCommandFilterCtx *fctx, int pos) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_KillForkChild)(int child_pid); REDISMODULE_API int (*RedisModule_Fork)(RedisModuleForkDoneHandler cb, void *user_data) REDISMODULE_ATTR;
float REDISMODULE_API_FUNC(RedisModule_GetUsedMemoryRatio)(); REDISMODULE_API int (*RedisModule_ExitFromChild)(int retcode) REDISMODULE_ATTR;
size_t REDISMODULE_API_FUNC(RedisModule_MallocSize)(void* ptr); REDISMODULE_API int (*RedisModule_KillForkChild)(int child_pid) REDISMODULE_ATTR;
RedisModuleUser *REDISMODULE_API_FUNC(RedisModule_CreateModuleUser)(const char *name); REDISMODULE_API float (*RedisModule_GetUsedMemoryRatio)() REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_FreeModuleUser)(RedisModuleUser *user); REDISMODULE_API size_t (*RedisModule_MallocSize)(void* ptr) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_SetModuleUserACL)(RedisModuleUser *user, const char* acl); REDISMODULE_API RedisModuleUser * (*RedisModule_CreateModuleUser)(const char *name) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_AuthenticateClientWithACLUser)(RedisModuleCtx *ctx, const char *name, size_t len, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id); REDISMODULE_API void (*RedisModule_FreeModuleUser)(RedisModuleUser *user) REDISMODULE_ATTR;
int REDISMODULE_API_FUNC(RedisModule_AuthenticateClientWithUser)(RedisModuleCtx *ctx, RedisModuleUser *user, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id); REDISMODULE_API int (*RedisModule_SetModuleUserACL)(RedisModuleUser *user, const char* acl) REDISMODULE_ATTR;
void REDISMODULE_API_FUNC(RedisModule_DeauthenticateAndCloseClient)(RedisModuleCtx *ctx, uint64_t client_id); REDISMODULE_API int (*RedisModule_AuthenticateClientWithACLUser)(RedisModuleCtx *ctx, const char *name, size_t len, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_AuthenticateClientWithUser)(RedisModuleCtx *ctx, RedisModuleUser *user, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id) REDISMODULE_ATTR;
REDISMODULE_API int (*RedisModule_DeauthenticateAndCloseClient)(RedisModuleCtx *ctx, uint64_t client_id) REDISMODULE_ATTR;
REDISMODULE_API RedisModuleString * (*RedisModule_GetClientCertificate)(RedisModuleCtx *ctx, uint64_t id) REDISMODULE_ATTR;
REDISMODULE_API int *(*RedisModule_GetCommandKeys)(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int *num_keys) REDISMODULE_ATTR;
#endif #endif
#define RedisModule_IsAOFClient(id) ((id) == CLIENT_ID_AOF) #define RedisModule_IsAOFClient(id) ((id) == CLIENT_ID_AOF)
/* This is included inline inside each Redis module. */ /* This is included inline inside each Redis module. */
static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) __attribute__((unused)); static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) REDISMODULE_ATTR_UNUSED;
static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) { static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) {
void *getapifuncptr = ((void**)ctx)[0]; void *getapifuncptr = ((void**)ctx)[0];
RedisModule_GetApi = (int (*)(const char *, void *)) (unsigned long)getapifuncptr; RedisModule_GetApi = (int (*)(const char *, void *)) (unsigned long)getapifuncptr;
@ -811,6 +892,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
REDISMODULE_GET_API(LatencyAddSample); REDISMODULE_GET_API(LatencyAddSample);
REDISMODULE_GET_API(StringAppendBuffer); REDISMODULE_GET_API(StringAppendBuffer);
REDISMODULE_GET_API(RetainString); REDISMODULE_GET_API(RetainString);
REDISMODULE_GET_API(HoldString);
REDISMODULE_GET_API(StringCompare); REDISMODULE_GET_API(StringCompare);
REDISMODULE_GET_API(GetContextFromIO); REDISMODULE_GET_API(GetContextFromIO);
REDISMODULE_GET_API(GetKeyNameFromIO); REDISMODULE_GET_API(GetKeyNameFromIO);
@ -872,11 +954,17 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
REDISMODULE_GET_API(ScanCursorDestroy); REDISMODULE_GET_API(ScanCursorDestroy);
REDISMODULE_GET_API(Scan); REDISMODULE_GET_API(Scan);
REDISMODULE_GET_API(ScanKey); REDISMODULE_GET_API(ScanKey);
REDISMODULE_GET_API(GetContextFlagsAll);
REDISMODULE_GET_API(GetKeyspaceNotificationFlagsAll);
REDISMODULE_GET_API(IsSubEventSupported);
REDISMODULE_GET_API(GetServerVersion);
#ifdef REDISMODULE_EXPERIMENTAL_API #ifdef REDISMODULE_EXPERIMENTAL_API
REDISMODULE_GET_API(GetThreadSafeContext); REDISMODULE_GET_API(GetThreadSafeContext);
REDISMODULE_GET_API(GetDetachedThreadSafeContext);
REDISMODULE_GET_API(FreeThreadSafeContext); REDISMODULE_GET_API(FreeThreadSafeContext);
REDISMODULE_GET_API(ThreadSafeContextLock); REDISMODULE_GET_API(ThreadSafeContextLock);
REDISMODULE_GET_API(ThreadSafeContextTryLock);
REDISMODULE_GET_API(ThreadSafeContextUnlock); REDISMODULE_GET_API(ThreadSafeContextUnlock);
REDISMODULE_GET_API(BlockClient); REDISMODULE_GET_API(BlockClient);
REDISMODULE_GET_API(UnblockClient); REDISMODULE_GET_API(UnblockClient);
@ -923,6 +1011,8 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
REDISMODULE_GET_API(DeauthenticateAndCloseClient); REDISMODULE_GET_API(DeauthenticateAndCloseClient);
REDISMODULE_GET_API(AuthenticateClientWithACLUser); REDISMODULE_GET_API(AuthenticateClientWithACLUser);
REDISMODULE_GET_API(AuthenticateClientWithUser); REDISMODULE_GET_API(AuthenticateClientWithUser);
REDISMODULE_GET_API(GetClientCertificate);
REDISMODULE_GET_API(GetCommandKeys);
#endif #endif
if (RedisModule_IsModuleNameBusy && RedisModule_IsModuleNameBusy(name)) return REDISMODULE_ERR; if (RedisModule_IsModuleNameBusy && RedisModule_IsModuleNameBusy(name)) return REDISMODULE_ERR;
@ -932,6 +1022,8 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
#define RedisModule_Assert(_e) ((_e)?(void)0 : (RedisModule__Assert(#_e,__FILE__,__LINE__),exit(1))) #define RedisModule_Assert(_e) ((_e)?(void)0 : (RedisModule__Assert(#_e,__FILE__,__LINE__),exit(1)))
#define RMAPI_FUNC_SUPPORTED(func) (func != NULL)
#else #else
/* Things only defined for the modules core, not exported to modules /* Things only defined for the modules core, not exported to modules
@ -944,4 +1036,4 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
} }
#endif #endif
#endif /* REDISMOUDLE_H */ #endif /* REDISMODULE_H */

View File

@ -160,16 +160,16 @@ client *replicaFromMaster(redisMaster *mi)
* the file deletion to the filesystem. This call removes the file in a * the file deletion to the filesystem. This call removes the file in a
* background thread instead. We actually just do close() in the thread, * background thread instead. We actually just do close() in the thread,
* by using the fact that if there is another instance of the same file open, * by using the fact that if there is another instance of the same file open,
* the foreground unlink() will not really do anything, and deleting the * the foreground unlink() will only remove the fs name, and deleting the
* file will only happen once the last reference is lost. */ * file's storage space will only happen once the last reference is lost. */
int bg_unlink(const char *filename) { int bg_unlink(const char *filename) {
int fd = open(filename,O_RDONLY|O_NONBLOCK); int fd = open(filename,O_RDONLY|O_NONBLOCK);
if (fd == -1) { if (fd == -1) {
/* Can't open the file? Fall back to unlinking in the main thread. */ /* Can't open the file? Fall back to unlinking in the main thread. */
return unlink(filename); return unlink(filename);
} else { } else {
/* The following unlink() will not do anything since file /* The following unlink() removes the name but doesn't free the
* is still open. */ * file contents because a process still has it open. */
int retval = unlink(filename); int retval = unlink(filename);
if (retval == -1) { if (retval == -1) {
/* If we got an unlink error, we just return it, closing the /* If we got an unlink error, we just return it, closing the
@ -196,6 +196,10 @@ void createReplicationBacklog(void) {
* byte we have is the next byte that will be generated for the * byte we have is the next byte that will be generated for the
* replication stream. */ * replication stream. */
g_pserver->repl_backlog_off = g_pserver->master_repl_offset+1; g_pserver->repl_backlog_off = g_pserver->master_repl_offset+1;
/* Allow transmission to clients */
g_pserver->repl_batch_idxStart = 0;
g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;
} }
/* This function is called when the user modifies the replication backlog /* This function is called when the user modifies the replication backlog
@ -209,20 +213,44 @@ void resizeReplicationBacklog(long long newsize) {
newsize = CONFIG_REPL_BACKLOG_MIN_SIZE; newsize = CONFIG_REPL_BACKLOG_MIN_SIZE;
if (g_pserver->repl_backlog_size == newsize) return; if (g_pserver->repl_backlog_size == newsize) return;
g_pserver->repl_backlog_size = newsize;
if (g_pserver->repl_backlog != NULL) { if (g_pserver->repl_backlog != NULL) {
/* What we actually do is to flush the old buffer and realloc a new /* What we actually do is to flush the old buffer and realloc a new
* empty one. It will refill with new data incrementally. * empty one. It will refill with new data incrementally.
* The reason is that copying a few gigabytes adds latency and even * The reason is that copying a few gigabytes adds latency and even
* worse often we need to alloc additional space before freeing the * worse often we need to alloc additional space before freeing the
* old buffer. */ * old buffer. */
zfree(g_pserver->repl_backlog);
g_pserver->repl_backlog = (char*)zmalloc(g_pserver->repl_backlog_size, MALLOC_LOCAL); if (g_pserver->repl_batch_idxStart >= 0) {
g_pserver->repl_backlog_histlen = 0; // We need to keep critical data so we can't shrink less than the hot data in the buffer
g_pserver->repl_backlog_idx = 0; newsize = std::max(newsize, g_pserver->master_repl_offset - g_pserver->repl_batch_offStart);
/* Next byte we have is... the next since the buffer is empty. */ char *backlog = (char*)zmalloc(newsize);
g_pserver->repl_backlog_off = g_pserver->master_repl_offset+1; g_pserver->repl_backlog_histlen = g_pserver->master_repl_offset - g_pserver->repl_batch_offStart;
if (g_pserver->repl_backlog_idx >= g_pserver->repl_batch_idxStart) {
auto cbActiveBacklog = g_pserver->repl_backlog_idx - g_pserver->repl_batch_idxStart;
memcpy(backlog, g_pserver->repl_backlog + g_pserver->repl_batch_idxStart, cbActiveBacklog);
serverAssert(g_pserver->repl_backlog_histlen == cbActiveBacklog);
} else {
auto cbPhase1 = g_pserver->repl_backlog_size - g_pserver->repl_batch_idxStart;
memcpy(backlog, g_pserver->repl_backlog + g_pserver->repl_batch_idxStart, cbPhase1);
memcpy(backlog + cbPhase1, g_pserver->repl_backlog, g_pserver->repl_backlog_idx);
auto cbActiveBacklog = cbPhase1 + g_pserver->repl_backlog_idx;
serverAssert(g_pserver->repl_backlog_histlen == cbActiveBacklog);
}
zfree(g_pserver->repl_backlog);
g_pserver->repl_backlog = backlog;
g_pserver->repl_backlog_idx = g_pserver->repl_backlog_histlen;
g_pserver->repl_batch_idxStart = 0;
} else {
zfree(g_pserver->repl_backlog);
g_pserver->repl_backlog = (char*)zmalloc(newsize);
g_pserver->repl_backlog_histlen = 0;
g_pserver->repl_backlog_idx = 0;
/* Next byte we have is... the next since the buffer is empty. */
g_pserver->repl_backlog_off = g_pserver->master_repl_offset+1;
}
} }
g_pserver->repl_backlog_size = newsize;
} }
void freeReplicationBacklog(void) { void freeReplicationBacklog(void) {
@ -247,6 +275,21 @@ void feedReplicationBacklog(const void *ptr, size_t len) {
serverAssert(GlobalLocksAcquired()); serverAssert(GlobalLocksAcquired());
const unsigned char *p = (const unsigned char*)ptr; const unsigned char *p = (const unsigned char*)ptr;
if (g_pserver->repl_batch_idxStart >= 0) {
long long minimumsize = g_pserver->master_repl_offset + len - g_pserver->repl_batch_offStart+1;
if (minimumsize > g_pserver->repl_backlog_size) {
flushReplBacklogToClients();
minimumsize = g_pserver->master_repl_offset + len - g_pserver->repl_batch_offStart+1;
if (minimumsize > g_pserver->repl_backlog_size) {
// This is an emergency overflow, we better resize to fit
long long newsize = std::max(g_pserver->repl_backlog_size*2, minimumsize);
serverLog(LL_WARNING, "Replication backlog is too small, resizing to: %lld", newsize);
resizeReplicationBacklog(newsize);
}
}
}
g_pserver->master_repl_offset += len; g_pserver->master_repl_offset += len;
/* This is a circular buffer, so write as much data we can at every /* This is a circular buffer, so write as much data we can at every
@ -315,7 +358,7 @@ void replicationFeedSlave(client *replica, int dictid, robj **argv, int argc, bo
if (g_pserver->repl_backlog && fSendRaw) feedReplicationBacklogWithObject(selectcmd); if (g_pserver->repl_backlog && fSendRaw) feedReplicationBacklogWithObject(selectcmd);
/* Send it to slaves */ /* Send it to slaves */
addReplyAsync(replica,selectcmd); addReply(replica,selectcmd);
if (dictid < 0 || dictid >= PROTO_SHARED_SELECT_CMDS) if (dictid < 0 || dictid >= PROTO_SHARED_SELECT_CMDS)
decrRefCount(selectcmd); decrRefCount(selectcmd);
@ -329,18 +372,18 @@ void replicationFeedSlave(client *replica, int dictid, robj **argv, int argc, bo
if (fSendRaw) if (fSendRaw)
{ {
/* Add the multi bulk length. */ /* Add the multi bulk length. */
addReplyArrayLenAsync(replica,argc); addReplyArrayLen(replica,argc);
/* Finally any additional argument that was not stored inside the /* Finally any additional argument that was not stored inside the
* static buffer if any (from j to argc). */ * static buffer if any (from j to argc). */
for (int j = 0; j < argc; j++) for (int j = 0; j < argc; j++)
addReplyBulkAsync(replica,argv[j]); addReplyBulk(replica,argv[j]);
} }
else else
{ {
struct redisCommand *cmd = lookupCommand(szFromObj(argv[0])); struct redisCommand *cmd = lookupCommand(szFromObj(argv[0]));
sds buf = catCommandForAofAndActiveReplication(sdsempty(), cmd, argv, argc); sds buf = catCommandForAofAndActiveReplication(sdsempty(), cmd, argv, argc);
addReplyProtoAsync(replica, buf, sdslen(buf)); addReplyProto(replica, buf, sdslen(buf));
sdsfree(buf); sdsfree(buf);
} }
} }
@ -369,13 +412,11 @@ static int writeProtoNum(char *dst, const size_t cchdst, long long num)
* as well. This function is used if the instance is a master: we use * as well. This function is used if the instance is a master: we use
* the commands received by our clients in order to create the replication * the commands received by our clients in order to create the replication
* stream. Instead if the instance is a replica and has sub-slaves attached, * stream. Instead if the instance is a replica and has sub-slaves attached,
* we use replicationFeedSlavesFromMaster() */ * we use replicationFeedSlavesFromMasterStream() */
void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) { void replicationFeedSlavesCore(list *slaves, int dictid, robj **argv, int argc) {
listNode *ln, *lnReply; int j;
listIter li, liReply;
int j, len;
serverAssert(GlobalLocksAcquired()); serverAssert(GlobalLocksAcquired());
static client *fake = nullptr; serverAssert(g_pserver->repl_batch_offStart >= 0);
if (dictid < 0) if (dictid < 0)
dictid = 0; // this can happen if we send a PING before any real operation dictid = 0; // this can happen if we send a PING before any real operation
@ -394,58 +435,34 @@ void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) {
/* We can't have slaves attached and no backlog. */ /* We can't have slaves attached and no backlog. */
serverAssert(!(listLength(slaves) != 0 && g_pserver->repl_backlog == NULL)); serverAssert(!(listLength(slaves) != 0 && g_pserver->repl_backlog == NULL));
if (fake == nullptr)
{
fake = createClient(nullptr, serverTL - g_pserver->rgthreadvar);
fake->flags |= CLIENT_FORCE_REPLY;
}
bool fSendRaw = !g_pserver->fActiveReplica; bool fSendRaw = !g_pserver->fActiveReplica;
replicationFeedSlave(fake, dictid, argv, argc, fSendRaw); // Note: updates the repl log, keep above the repl update code below
/* Send SELECT command to every replica if needed. */
if (g_pserver->replicaseldb != dictid) {
char llstr[LONG_STR_SIZE];
robj *selectcmd;
long long cchbuf = fake->bufpos; /* For a few DBs we have pre-computed SELECT command. */
listRewind(fake->reply, &liReply); if (dictid >= 0 && dictid < PROTO_SHARED_SELECT_CMDS) {
while ((lnReply = listNext(&liReply))) selectcmd = shared.select[dictid];
{ } else {
clientReplyBlock* reply = (clientReplyBlock*)listNodeValue(lnReply); int dictid_len;
cchbuf += reply->used;
dictid_len = ll2string(llstr,sizeof(llstr),dictid);
selectcmd = createObject(OBJ_STRING,
sdscatprintf(sdsempty(),
"*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n",
dictid_len, llstr));
}
/* Add the SELECT command into the backlog. */
/* We don't do this for advanced replication because this will be done later when it adds the whole RREPLAY command */
if (g_pserver->repl_backlog && fSendRaw) feedReplicationBacklogWithObject(selectcmd);
if (dictid < 0 || dictid >= PROTO_SHARED_SELECT_CMDS)
decrRefCount(selectcmd);
} }
g_pserver->replicaseldb = dictid;
serverAssert(argc > 0);
serverAssert(cchbuf > 0);
// The code below used to be: snprintf(proto, sizeof(proto), "*5\r\n$7\r\nRREPLAY\r\n$%d\r\n%s\r\n$%lld\r\n", (int)strlen(uuid), uuid, cchbuf);
// but that was much too slow
static const char *protoRREPLAY = "*5\r\n$7\r\nRREPLAY\r\n$36\r\n00000000-0000-0000-0000-000000000000\r\n$";
char proto[1024];
int cchProto = 0;
if (!fSendRaw)
{
char uuid[37];
uuid_unparse(cserver.uuid, uuid);
cchProto = strlen(protoRREPLAY);
memcpy(proto, protoRREPLAY, strlen(protoRREPLAY));
memcpy(proto + 22, uuid, 36); // Note UUID_STR_LEN includes the \0 trailing byte which we don't want
cchProto += ll2string(proto + cchProto, sizeof(proto)-cchProto, cchbuf);
memcpy(proto + cchProto, "\r\n", 3);
cchProto += 2;
}
long long master_repl_offset_start = g_pserver->master_repl_offset;
char szDbNum[128];
int cchDbNum = 0;
if (!fSendRaw)
cchDbNum = writeProtoNum(szDbNum, sizeof(szDbNum), dictid);
char szMvcc[128];
int cchMvcc = 0;
incrementMvccTstamp(); // Always increment MVCC tstamp so we're consistent with active and normal replication
if (!fSendRaw)
cchMvcc = writeProtoNum(szMvcc, sizeof(szMvcc), getMvccTstamp());
/* Write the command to the replication backlog if any. */ /* Write the command to the replication backlog if any. */
if (g_pserver->repl_backlog) if (g_pserver->repl_backlog)
@ -456,10 +473,11 @@ void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) {
/* Add the multi bulk reply length. */ /* Add the multi bulk reply length. */
aux[0] = '*'; aux[0] = '*';
len = ll2string(aux+1,sizeof(aux)-1,argc); int multilen = ll2string(aux+1,sizeof(aux)-1,argc);
aux[len+1] = '\r'; aux[multilen+1] = '\r';
aux[len+2] = '\n'; aux[multilen+2] = '\n';
feedReplicationBacklog(aux,len+3);
feedReplicationBacklog(aux,multilen+3);
for (j = 0; j < argc; j++) { for (j = 0; j < argc; j++) {
long objlen = stringObjectLen(argv[j]); long objlen = stringObjectLen(argv[j]);
@ -468,7 +486,7 @@ void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) {
* not just as a plain string, so create the $..CRLF payload len * not just as a plain string, so create the $..CRLF payload len
* and add the final CRLF */ * and add the final CRLF */
aux[0] = '$'; aux[0] = '$';
len = ll2string(aux+1,sizeof(aux)-1,objlen); int len = ll2string(aux+1,sizeof(aux)-1,objlen);
aux[len+1] = '\r'; aux[len+1] = '\r';
aux[len+2] = '\n'; aux[len+2] = '\n';
feedReplicationBacklog(aux,len+3); feedReplicationBacklog(aux,len+3);
@ -478,67 +496,57 @@ void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) {
} }
else else
{ {
feedReplicationBacklog(proto, cchProto); char szDbNum[128];
feedReplicationBacklog(fake->buf, fake->bufpos); int cchDbNum = 0;
listRewind(fake->reply, &liReply); if (!fSendRaw)
while ((lnReply = listNext(&liReply))) cchDbNum = writeProtoNum(szDbNum, sizeof(szDbNum), dictid);
char szMvcc[128];
int cchMvcc = 0;
incrementMvccTstamp(); // Always increment MVCC tstamp so we're consistent with active and normal replication
if (!fSendRaw)
cchMvcc = writeProtoNum(szMvcc, sizeof(szMvcc), getMvccTstamp());
//size_t cchlen = multilen+3;
struct redisCommand *cmd = lookupCommand(szFromObj(argv[0]));
sds buf = catCommandForAofAndActiveReplication(sdsempty(), cmd, argv, argc);
size_t cchlen = sdslen(buf);
// The code below used to be: snprintf(proto, sizeof(proto), "*5\r\n$7\r\nRREPLAY\r\n$%d\r\n%s\r\n$%lld\r\n", (int)strlen(uuid), uuid, cchbuf);
// but that was much too slow
static const char *protoRREPLAY = "*5\r\n$7\r\nRREPLAY\r\n$36\r\n00000000-0000-0000-0000-000000000000\r\n$";
char proto[1024];
int cchProto = 0;
if (!fSendRaw)
{ {
clientReplyBlock* reply = (clientReplyBlock*)listNodeValue(lnReply); char uuid[37];
feedReplicationBacklog(reply->buf(), reply->used); uuid_unparse(cserver.uuid, uuid);
cchProto = strlen(protoRREPLAY);
memcpy(proto, protoRREPLAY, strlen(protoRREPLAY));
memcpy(proto + 22, uuid, 36); // Note UUID_STR_LEN includes the \0 trailing byte which we don't want
cchProto += ll2string(proto + cchProto, sizeof(proto)-cchProto, cchlen);
memcpy(proto + cchProto, "\r\n", 3);
cchProto += 2;
} }
feedReplicationBacklog(proto, cchProto);
feedReplicationBacklog(buf, sdslen(buf));
const char *crlf = "\r\n"; const char *crlf = "\r\n";
feedReplicationBacklog(crlf, 2); feedReplicationBacklog(crlf, 2);
feedReplicationBacklog(szDbNum, cchDbNum); feedReplicationBacklog(szDbNum, cchDbNum);
feedReplicationBacklog(szMvcc, cchMvcc); feedReplicationBacklog(szMvcc, cchMvcc);
sdsfree(buf);
} }
} }
}
/* Write the command to every replica. */ void replicationFeedSlaves(list *replicas, int dictid, robj **argv, int argc) {
listRewind(slaves,&li); runAndPropogateToReplicas(replicationFeedSlavesCore, replicas, dictid, argv, argc);
while((ln = listNext(&li))) {
client *replica = (client*)ln->value;
/* Don't feed slaves that are still waiting for BGSAVE to start */
if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_START) continue;
if (replica->flags & CLIENT_CLOSE_ASAP) continue;
std::unique_lock<decltype(replica->lock)> lock(replica->lock, std::defer_lock);
// When writing to clients on other threads the global lock is sufficient provided we only use AddReply*Async()
if (FCorrectThread(replica))
lock.lock();
if (serverTL->current_client && FSameHost(serverTL->current_client, replica))
{
replica->reploff_skipped += g_pserver->master_repl_offset - master_repl_offset_start;
continue;
}
/* Feed slaves that are waiting for the initial SYNC (so these commands
* are queued in the output buffer until the initial SYNC completes),
* or are already in sync with the master. */
if (!fSendRaw)
addReplyProtoAsync(replica, proto, cchProto);
addReplyProtoAsync(replica,fake->buf,fake->bufpos);
listRewind(fake->reply, &liReply);
while ((lnReply = listNext(&liReply)))
{
clientReplyBlock* reply = (clientReplyBlock*)listNodeValue(lnReply);
addReplyProtoAsync(replica, reply->buf(), reply->used);
}
if (!fSendRaw)
{
addReplyAsync(replica,shared.crlf);
addReplyProtoAsync(replica, szDbNum, cchDbNum);
addReplyProtoAsync(replica, szMvcc, cchMvcc);
}
}
// Cleanup cached fake client output buffers
fake->bufpos = 0;
fake->sentlen = 0;
fake->reply_bytes = 0;
listEmpty(fake->reply);
} }
/* This is a debugging function that gets called when we detect something /* This is a debugging function that gets called when we detect something
@ -578,10 +586,7 @@ void showLatestBacklog(void) {
/* This function is used in order to proxy what we receive from our master /* This function is used in order to proxy what we receive from our master
* to our sub-slaves. */ * to our sub-slaves. */
#include <ctype.h> #include <ctype.h>
void replicationFeedSlavesFromMasterStream(list *slaves, char *buf, size_t buflen) { void replicationFeedSlavesFromMasterStream(char *buf, size_t buflen) {
listNode *ln;
listIter li;
/* Debugging: this is handy to see the stream sent from master /* Debugging: this is handy to see the stream sent from master
* to slaves. Disabled with if(0). */ * to slaves. Disabled with if(0). */
if (0) { if (0) {
@ -593,23 +598,6 @@ void replicationFeedSlavesFromMasterStream(list *slaves, char *buf, size_t bufle
} }
if (g_pserver->repl_backlog) feedReplicationBacklog(buf,buflen); if (g_pserver->repl_backlog) feedReplicationBacklog(buf,buflen);
listRewind(slaves,&li);
while((ln = listNext(&li))) {
client *replica = (client*)ln->value;
std::unique_lock<decltype(replica->lock)> ulock(replica->lock, std::defer_lock);
if (FCorrectThread(replica))
ulock.lock();
if (FMasterHost(replica))
continue; // Active Active case, don't feed back
/* Don't feed slaves that are still waiting for BGSAVE to start */
if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_START) continue;
addReplyProtoAsync(replica,buf,buflen);
}
if (listLength(slaves))
ProcessPendingAsyncWrites(); // flush them to their respective threads
} }
void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc) { void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc) {
@ -651,7 +639,7 @@ void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv,
// When writing to clients on other threads the global lock is sufficient provided we only use AddReply*Async() // When writing to clients on other threads the global lock is sufficient provided we only use AddReply*Async()
if (FCorrectThread(c)) if (FCorrectThread(c))
lock.lock(); lock.lock();
addReplyAsync(monitor,cmdobj); addReply(monitor,cmdobj);
} }
decrRefCount(cmdobj); decrRefCount(cmdobj);
} }
@ -784,7 +772,7 @@ int masterTryPartialResynchronization(client *c) {
(strcasecmp(master_replid, g_pserver->replid2) || (strcasecmp(master_replid, g_pserver->replid2) ||
psync_offset > g_pserver->second_replid_offset)) psync_offset > g_pserver->second_replid_offset))
{ {
/* Run id "?" is used by slaves that want to force a full resync. */ /* Replid "?" is used by slaves that want to force a full resync. */
if (master_replid[0] != '?') { if (master_replid[0] != '?') {
if (strcasecmp(master_replid, g_pserver->replid) && if (strcasecmp(master_replid, g_pserver->replid) &&
strcasecmp(master_replid, g_pserver->replid2)) strcasecmp(master_replid, g_pserver->replid2))
@ -963,7 +951,7 @@ int startBgsaveForReplication(int mincapa) {
return retval; return retval;
} }
/* SYNC and PSYNC command implemenation. */ /* SYNC and PSYNC command implementation. */
void syncCommand(client *c) { void syncCommand(client *c) {
/* ignore SYNC if already replica or in monitor mode */ /* ignore SYNC if already replica or in monitor mode */
if (c->flags & CLIENT_SLAVE) return; if (c->flags & CLIENT_SLAVE) return;
@ -1609,6 +1597,16 @@ void updateSlavesWaitingBgsave(int bgsaveerr, int type)
} else if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_END) { } else if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_END) {
struct redis_stat buf; struct redis_stat buf;
if (bgsaveerr != C_OK) {
ul.unlock();
if (FCorrectThread(replica))
freeClient(replica);
else
freeClientAsync(replica);
serverLog(LL_WARNING,"SYNC failed. BGSAVE child returned an error");
continue;
}
/* If this was an RDB on disk save, we have to prepare to send /* If this was an RDB on disk save, we have to prepare to send
* the RDB from disk to the replica socket. Otherwise if this was * the RDB from disk to the replica socket. Otherwise if this was
* already an RDB -> Slaves socket transfer, used in the case of * already an RDB -> Slaves socket transfer, used in the case of
@ -1647,15 +1645,6 @@ void updateSlavesWaitingBgsave(int bgsaveerr, int type)
replica->repl_put_online_on_ack = 1; replica->repl_put_online_on_ack = 1;
replica->repl_ack_time = g_pserver->unixtime; /* Timeout otherwise. */ replica->repl_ack_time = g_pserver->unixtime; /* Timeout otherwise. */
} else { } else {
if (bgsaveerr != C_OK) {
ul.unlock();
if (FCorrectThread(replica))
freeClient(replica);
else
freeClientAsync(replica);
serverLog(LL_WARNING,"SYNC failed. BGSAVE child returned an error");
continue;
}
if ((replica->repldbfd = open(g_pserver->rdb_filename,O_RDONLY)) == -1 || if ((replica->repldbfd = open(g_pserver->rdb_filename,O_RDONLY)) == -1 ||
redis_fstat(replica->repldbfd,&buf) == -1) { redis_fstat(replica->repldbfd,&buf) == -1) {
ul.unlock(); ul.unlock();
@ -1792,7 +1781,7 @@ void replicationEmptyDbCallback(void *privdata) {
} }
} }
/* Once we have a link with the master and the synchroniziation was /* Once we have a link with the master and the synchronization was
* performed, this function materializes the master client we store * performed, this function materializes the master client we store
* at g_pserver->master, starting from the specified file descriptor. */ * at g_pserver->master, starting from the specified file descriptor. */
void replicationCreateMasterClient(redisMaster *mi, connection *conn, int dbid) { void replicationCreateMasterClient(redisMaster *mi, connection *conn, int dbid) {
@ -1857,16 +1846,10 @@ static int useDisklessLoad() {
} }
/* Helper function for readSyncBulkPayload() to make backups of the current /* Helper function for readSyncBulkPayload() to make backups of the current
* DBs before socket-loading the new ones. The backups may be restored later * databases before socket-loading the new ones. The backups may be restored
* or freed by disklessLoadRestoreBackups(). */ * by disklessLoadRestoreBackup or freed by disklessLoadDiscardBackup later. */
redisDb *disklessLoadMakeBackups(void) { dbBackup *disklessLoadMakeBackup(void) {
redisDb *backups = (redisDb*)zmalloc(sizeof(redisDb)*cserver.dbnum); return backupDb();
for (int i=0; i<cserver.dbnum; i++) {
backups[i] = g_pserver->db[i];
g_pserver->db[i].pdict = dictCreate(&dbDictType,NULL);
g_pserver->db[i].setexpire = new (MALLOC_LOCAL) expireset();
}
return backups;
} }
/* Helper function for readSyncBulkPayload(): when replica-side diskless /* Helper function for readSyncBulkPayload(): when replica-side diskless
@ -1874,30 +1857,15 @@ redisDb *disklessLoadMakeBackups(void) {
* before loading the new ones from the socket. * before loading the new ones from the socket.
* *
* If the socket loading went wrong, we want to restore the old backups * If the socket loading went wrong, we want to restore the old backups
* into the server databases. This function does just that in the case * into the server databases. */
* the 'restore' argument (the number of DBs to replace) is non-zero. void disklessLoadRestoreBackup(dbBackup *buckup) {
* restoreDbBackup(buckup);
* When instead the loading succeeded we want just to free our old backups, }
* in that case the funciton will do just that when 'restore' is 0. */
void disklessLoadRestoreBackups(redisDb *backup, int restore, int empty_db_flags) /* Helper function for readSyncBulkPayload() to discard our old backups
{ * when the loading succeeded. */
if (restore) { void disklessLoadDiscardBackup(dbBackup *buckup, int flag) {
/* Restore. */ discardDbBackup(buckup, flag, replicationEmptyDbCallback);
emptyDbGeneric(g_pserver->db,-1,empty_db_flags,replicationEmptyDbCallback);
for (int i=0; i<cserver.dbnum; i++) {
dictRelease(g_pserver->db[i].pdict);
delete g_pserver->db[i].setexpire;
g_pserver->db[i] = backup[i];
}
} else {
/* Delete (Pass EMPTYDB_BACKUP in order to avoid firing module events) . */
emptyDbGeneric(backup,-1,empty_db_flags|EMPTYDB_BACKUP,replicationEmptyDbCallback);
for (int i=0; i<cserver.dbnum; i++) {
dictRelease(backup[i].pdict);
delete backup[i].setexpire;
}
}
zfree(backup);
} }
/* Asynchronously read the SYNC payload we receive from a master */ /* Asynchronously read the SYNC payload we receive from a master */
@ -1906,7 +1874,7 @@ void readSyncBulkPayload(connection *conn) {
char buf[PROTO_IOBUF_LEN]; char buf[PROTO_IOBUF_LEN];
ssize_t nread, readlen, nwritten; ssize_t nread, readlen, nwritten;
int use_diskless_load = useDisklessLoad(); int use_diskless_load = useDisklessLoad();
redisDb *diskless_load_backup = NULL; dbBackup *diskless_load_backup = NULL;
rdbSaveInfo rsi = RDB_SAVE_INFO_INIT; rdbSaveInfo rsi = RDB_SAVE_INFO_INIT;
int empty_db_flags = g_pserver->repl_slave_lazy_flush ? EMPTYDB_ASYNC : int empty_db_flags = g_pserver->repl_slave_lazy_flush ? EMPTYDB_ASYNC :
EMPTYDB_NO_FLAGS; EMPTYDB_NO_FLAGS;
@ -1918,7 +1886,7 @@ void readSyncBulkPayload(connection *conn) {
serverAssert(GlobalLocksAcquired()); serverAssert(GlobalLocksAcquired());
/* Static vars used to hold the EOF mark, and the last bytes received /* Static vars used to hold the EOF mark, and the last bytes received
* form the server: when they match, we reached the end of the transfer. */ * from the server: when they match, we reached the end of the transfer. */
static char eofmark[CONFIG_RUN_ID_SIZE]; static char eofmark[CONFIG_RUN_ID_SIZE];
static char lastbytes[CONFIG_RUN_ID_SIZE]; static char lastbytes[CONFIG_RUN_ID_SIZE];
static int usemark = 0; static int usemark = 0;
@ -2092,11 +2060,11 @@ void readSyncBulkPayload(connection *conn) {
g_pserver->repl_diskless_load == REPL_DISKLESS_LOAD_SWAPDB) g_pserver->repl_diskless_load == REPL_DISKLESS_LOAD_SWAPDB)
{ {
/* Create a backup of server.db[] and initialize to empty /* Create a backup of server.db[] and initialize to empty
* dictionaries */ * dictionaries. */
diskless_load_backup = disklessLoadMakeBackups(); diskless_load_backup = disklessLoadMakeBackup();
} }
/* We call to emptyDb even in case of REPL_DISKLESS_LOAD_SWAPDB /* We call to emptyDb even in case of REPL_DISKLESS_LOAD_SWAPDB
* (Where disklessLoadMakeBackups left server.db empty) because we * (Where disklessLoadMakeBackup left server.db empty) because we
* want to execute all the auxiliary logic of emptyDb (Namely, * want to execute all the auxiliary logic of emptyDb (Namely,
* fire module events) */ * fire module events) */
if (!fUpdate) { if (!fUpdate) {
@ -2129,14 +2097,14 @@ void readSyncBulkPayload(connection *conn) {
"from socket"); "from socket");
cancelReplicationHandshake(mi); cancelReplicationHandshake(mi);
rioFreeConn(&rdb, NULL); rioFreeConn(&rdb, NULL);
/* Remove the half-loaded data in case we started with
* an empty replica. */
emptyDb(-1,empty_db_flags,replicationEmptyDbCallback);
if (g_pserver->repl_diskless_load == REPL_DISKLESS_LOAD_SWAPDB) { if (g_pserver->repl_diskless_load == REPL_DISKLESS_LOAD_SWAPDB) {
/* Restore the backed up databases. */ /* Restore the backed up databases. */
disklessLoadRestoreBackups(diskless_load_backup,1, disklessLoadRestoreBackup(diskless_load_backup);
empty_db_flags);
} else {
/* Remove the half-loaded data in case we started with
* an empty replica. */
emptyDb(-1,empty_db_flags,replicationEmptyDbCallback);
} }
/* Note that there's no point in restarting the AOF on SYNC /* Note that there's no point in restarting the AOF on SYNC
@ -2151,7 +2119,7 @@ void readSyncBulkPayload(connection *conn) {
/* Delete the backup databases we created before starting to load /* Delete the backup databases we created before starting to load
* the new RDB. Now the RDB was loaded with success so the old * the new RDB. Now the RDB was loaded with success so the old
* data is useless. */ * data is useless. */
disklessLoadRestoreBackups(diskless_load_backup,0,empty_db_flags); disklessLoadDiscardBackup(diskless_load_backup, empty_db_flags);
} }
/* Verify the end mark is correct. */ /* Verify the end mark is correct. */
@ -2185,6 +2153,17 @@ void readSyncBulkPayload(connection *conn) {
const char *rdb_filename = mi->repl_transfer_tmpfile; const char *rdb_filename = mi->repl_transfer_tmpfile;
/* Make sure the new file (also used for persistence) is fully synced
* (not covered by earlier calls to rdb_fsync_range). */
if (fsync(mi->repl_transfer_fd) == -1) {
serverLog(LL_WARNING,
"Failed trying to sync the temp DB to disk in "
"MASTER <-> REPLICA synchronization: %s",
strerror(errno));
cancelReplicationHandshake(mi);
return;
}
/* Rename rdb like renaming rewrite aof asynchronously. */ /* Rename rdb like renaming rewrite aof asynchronously. */
if (!fUpdate) { if (!fUpdate) {
int old_rdb_fd = open(g_pserver->rdb_filename,O_RDONLY|O_NONBLOCK); int old_rdb_fd = open(g_pserver->rdb_filename,O_RDONLY|O_NONBLOCK);
@ -2254,7 +2233,7 @@ void readSyncBulkPayload(connection *conn) {
REDISMODULE_SUBEVENT_MASTER_LINK_UP, REDISMODULE_SUBEVENT_MASTER_LINK_UP,
NULL); NULL);
/* After a full resynchroniziation we use the replication ID and /* After a full resynchronization we use the replication ID and
* offset of the master. The secondary ID / offset are cleared since * offset of the master. The secondary ID / offset are cleared since
* we are starting a new history. */ * we are starting a new history. */
if (fUpdate) if (fUpdate)
@ -2268,6 +2247,8 @@ void readSyncBulkPayload(connection *conn) {
* we are starting a new history. */ * we are starting a new history. */
memcpy(g_pserver->replid,mi->master->replid,sizeof(g_pserver->replid)); memcpy(g_pserver->replid,mi->master->replid,sizeof(g_pserver->replid));
g_pserver->master_repl_offset = mi->master->reploff; g_pserver->master_repl_offset = mi->master->reploff;
if (g_pserver->repl_batch_offStart >= 0)
g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;
} }
clearReplicationId2(); clearReplicationId2();
@ -2275,7 +2256,7 @@ void readSyncBulkPayload(connection *conn) {
* accumulate the backlog regardless of the fact they have sub-slaves * accumulate the backlog regardless of the fact they have sub-slaves
* or not, in order to behave correctly if they are promoted to * or not, in order to behave correctly if they are promoted to
* masters after a failover. */ * masters after a failover. */
if (g_pserver->repl_backlog == NULL) createReplicationBacklog(); if (g_pserver->repl_backlog == NULL) runAndPropogateToReplicas(createReplicationBacklog);
serverLog(LL_NOTICE, "MASTER <-> REPLICA sync: Finished with success"); serverLog(LL_NOTICE, "MASTER <-> REPLICA sync: Finished with success");
if (cserver.supervised_mode == SUPERVISED_SYSTEMD) { if (cserver.supervised_mode == SUPERVISED_SYSTEMD) {
@ -2360,7 +2341,7 @@ char *sendSynchronousCommand(redisMaster *mi, int flags, connection *conn, ...)
/* Try a partial resynchronization with the master if we are about to reconnect. /* Try a partial resynchronization with the master if we are about to reconnect.
* If there is no cached master structure, at least try to issue a * If there is no cached master structure, at least try to issue a
* "PSYNC ? -1" command in order to trigger a full resync using the PSYNC * "PSYNC ? -1" command in order to trigger a full resync using the PSYNC
* command in order to obtain the master run id and the master replication * command in order to obtain the master replid and the master replication
* global offset. * global offset.
* *
* This function is designed to be called from syncWithMaster(), so the * This function is designed to be called from syncWithMaster(), so the
@ -2388,7 +2369,7 @@ char *sendSynchronousCommand(redisMaster *mi, int flags, connection *conn, ...)
* *
* PSYNC_CONTINUE: If the PSYNC command succeeded and we can continue. * PSYNC_CONTINUE: If the PSYNC command succeeded and we can continue.
* PSYNC_FULLRESYNC: If PSYNC is supported but a full resync is needed. * PSYNC_FULLRESYNC: If PSYNC is supported but a full resync is needed.
* In this case the master run_id and global replication * In this case the master replid and global replication
* offset is saved. * offset is saved.
* PSYNC_NOT_SUPPORTED: If the server does not understand PSYNC at all and * PSYNC_NOT_SUPPORTED: If the server does not understand PSYNC at all and
* the caller should fall back to SYNC. * the caller should fall back to SYNC.
@ -2419,7 +2400,7 @@ int slaveTryPartialResynchronization(redisMaster *mi, connection *conn, int read
/* Writing half */ /* Writing half */
if (!read_reply) { if (!read_reply) {
/* Initially set master_initial_offset to -1 to mark the current /* Initially set master_initial_offset to -1 to mark the current
* master run_id and offset as not valid. Later if we'll be able to do * master replid and offset as not valid. Later if we'll be able to do
* a FULL resync using the PSYNC command we'll set the offset at the * a FULL resync using the PSYNC command we'll set the offset at the
* right value, so that this information will be propagated to the * right value, so that this information will be propagated to the
* client structure representing the master into g_pserver->master. */ * client structure representing the master into g_pserver->master. */
@ -2460,7 +2441,7 @@ int slaveTryPartialResynchronization(redisMaster *mi, connection *conn, int read
if (!strncmp(reply,"+FULLRESYNC",11)) { if (!strncmp(reply,"+FULLRESYNC",11)) {
char *replid = NULL, *offset = NULL; char *replid = NULL, *offset = NULL;
/* FULL RESYNC, parse the reply in order to extract the run id /* FULL RESYNC, parse the reply in order to extract the replid
* and the replication offset. */ * and the replication offset. */
replid = strchr(reply,' '); replid = strchr(reply,' ');
if (replid) { if (replid) {
@ -2535,7 +2516,7 @@ int slaveTryPartialResynchronization(redisMaster *mi, connection *conn, int read
/* If this instance was restarted and we read the metadata to /* If this instance was restarted and we read the metadata to
* PSYNC from the persistence file, our replication backlog could * PSYNC from the persistence file, our replication backlog could
* be still not initialized. Create it. */ * be still not initialized. Create it. */
if (g_pserver->repl_backlog == NULL) createReplicationBacklog(); if (g_pserver->repl_backlog == NULL) runAndPropogateToReplicas(createReplicationBacklog);
return PSYNC_CONTINUE; return PSYNC_CONTINUE;
} }
@ -2645,6 +2626,7 @@ void syncWithMaster(connection *conn) {
* both. */ * both. */
if (err[0] != '+' && if (err[0] != '+' &&
strncmp(err,"-NOAUTH",7) != 0 && strncmp(err,"-NOAUTH",7) != 0 &&
strncmp(err,"-NOPERM",7) != 0 &&
strncmp(err,"-ERR operation not permitted",28) != 0) strncmp(err,"-ERR operation not permitted",28) != 0)
{ {
serverLog(LL_WARNING,"Error reply to PING from master: '%s'",err); serverLog(LL_WARNING,"Error reply to PING from master: '%s'",err);
@ -2812,7 +2794,7 @@ void syncWithMaster(connection *conn) {
/* Try a partial resynchonization. If we don't have a cached master /* Try a partial resynchonization. If we don't have a cached master
* slaveTryPartialResynchronization() will at least try to use PSYNC * slaveTryPartialResynchronization() will at least try to use PSYNC
* to start a full resynchronization so that we get the master run id * to start a full resynchronization so that we get the master replid
* and the global offset, to try a partial resync at the next * and the global offset, to try a partial resync at the next
* reconnection attempt. */ * reconnection attempt. */
if (mi->repl_state == REPL_STATE_SEND_PSYNC) { if (mi->repl_state == REPL_STATE_SEND_PSYNC) {
@ -2981,7 +2963,7 @@ void replicationAbortSyncTransfer(redisMaster *mi) {
undoConnectWithMaster(mi); undoConnectWithMaster(mi);
if (mi->repl_transfer_fd!=-1) { if (mi->repl_transfer_fd!=-1) {
close(mi->repl_transfer_fd); close(mi->repl_transfer_fd);
unlink(mi->repl_transfer_tmpfile); bg_unlink(mi->repl_transfer_tmpfile);
zfree(mi->repl_transfer_tmpfile); zfree(mi->repl_transfer_tmpfile);
mi->repl_transfer_tmpfile = NULL; mi->repl_transfer_tmpfile = NULL;
mi->repl_transfer_fd = -1; mi->repl_transfer_fd = -1;
@ -2995,7 +2977,7 @@ void replicationAbortSyncTransfer(redisMaster *mi) {
* If there was a replication handshake in progress 1 is returned and * If there was a replication handshake in progress 1 is returned and
* the replication state (g_pserver->repl_state) set to REPL_STATE_CONNECT. * the replication state (g_pserver->repl_state) set to REPL_STATE_CONNECT.
* *
* Otherwise zero is returned and no operation is perforemd at all. */ * Otherwise zero is returned and no operation is performed at all. */
int cancelReplicationHandshake(redisMaster *mi) { int cancelReplicationHandshake(redisMaster *mi) {
if (mi->repl_state == REPL_STATE_TRANSFER) { if (mi->repl_state == REPL_STATE_TRANSFER) {
replicationAbortSyncTransfer(mi); replicationAbortSyncTransfer(mi);
@ -3047,7 +3029,11 @@ struct redisMaster *replicationAddMaster(char *ip, int port) {
else else
freeClientAsync(mi->master); freeClientAsync(mi->master);
} }
disconnectAllBlockedClients(); /* Clients blocked in master, now replica. */ if (!g_pserver->fActiveReplica)
disconnectAllBlockedClients(); /* Clients blocked in master, now replica. */
/* Update oom_score_adj */
setOOMScoreAdj(-1);
/* Force our slaves to resync with us as well. They may hopefully be able /* Force our slaves to resync with us as well. They may hopefully be able
* to partially resync with us, but we can notify the replid change. */ * to partially resync with us, but we can notify the replid change. */
@ -3145,6 +3131,9 @@ void replicationUnsetMaster(redisMaster *mi) {
listDelNode(g_pserver->masters, ln); listDelNode(g_pserver->masters, ln);
freeMasterInfo(mi); freeMasterInfo(mi);
/* Update oom_score_adj */
setOOMScoreAdj(-1);
/* Fire the role change modules event. */ /* Fire the role change modules event. */
moduleFireServerEvent(REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED, moduleFireServerEvent(REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED,
REDISMODULE_EVENT_REPLROLECHANGED_NOW_MASTER, REDISMODULE_EVENT_REPLROLECHANGED_NOW_MASTER,
@ -3259,7 +3248,7 @@ void replicaofCommand(client *c) {
miNew->masterhost, miNew->masterport, client); miNew->masterhost, miNew->masterport, client);
sdsfree(client); sdsfree(client);
} }
addReplyAsync(c,shared.ok); addReply(c,shared.ok);
} }
/* ROLE command: provide information about the role of the instance /* ROLE command: provide information about the role of the instance
@ -3487,6 +3476,11 @@ void replicationResurrectCachedMaster(redisMaster *mi, connection *conn) {
but this client was unlinked so its OK here */ but this client was unlinked so its OK here */
mi->master->iel = serverTL - g_pserver->rgthreadvar; // martial to this thread mi->master->iel = serverTL - g_pserver->rgthreadvar; // martial to this thread
/* Fire the master link modules event. */
moduleFireServerEvent(REDISMODULE_EVENT_MASTER_LINK_CHANGE,
REDISMODULE_SUBEVENT_MASTER_LINK_UP,
NULL);
/* Re-add to the list of clients. */ /* Re-add to the list of clients. */
linkClient(mi->master); linkClient(mi->master);
serverAssert(connGetPrivateData(mi->master->conn) == mi->master); serverAssert(connGetPrivateData(mi->master->conn) == mi->master);
@ -3540,7 +3534,7 @@ void refreshGoodSlavesCount(void) {
* *
* We don't care about taking a different cache for every different replica * We don't care about taking a different cache for every different replica
* since to fill the cache again is not very costly, the goal of this code * since to fill the cache again is not very costly, the goal of this code
* is to avoid that the same big script is trasmitted a big number of times * is to avoid that the same big script is transmitted a big number of times
* per second wasting bandwidth and processor speed, but it is not a problem * per second wasting bandwidth and processor speed, but it is not a problem
* if we need to rebuild the cache from scratch from time to time, every used * if we need to rebuild the cache from scratch from time to time, every used
* script will need to be transmitted a single time to reappear in the cache. * script will need to be transmitted a single time to reappear in the cache.
@ -3550,7 +3544,7 @@ void refreshGoodSlavesCount(void) {
* 1) Every time a new replica connects, we flush the whole script cache. * 1) Every time a new replica connects, we flush the whole script cache.
* 2) We only send as EVALSHA what was sent to the master as EVALSHA, without * 2) We only send as EVALSHA what was sent to the master as EVALSHA, without
* trying to convert EVAL into EVALSHA specifically for slaves. * trying to convert EVAL into EVALSHA specifically for slaves.
* 3) Every time we trasmit a script as EVAL to the slaves, we also add the * 3) Every time we transmit a script as EVAL to the slaves, we also add the
* corresponding SHA1 of the script into the cache as we are sure every * corresponding SHA1 of the script into the cache as we are sure every
* replica knows about the script starting from now. * replica knows about the script starting from now.
* 4) On SCRIPT FLUSH command, we replicate the command to all the slaves * 4) On SCRIPT FLUSH command, we replicate the command to all the slaves
@ -3641,7 +3635,7 @@ int replicationScriptCacheExists(sds sha1) {
/* This just set a flag so that we broadcast a REPLCONF GETACK command /* This just set a flag so that we broadcast a REPLCONF GETACK command
* to all the slaves in the beforeSleep() function. Note that this way * to all the slaves in the beforeSleep() function. Note that this way
* we "group" all the clients that want to wait for synchronouns replication * we "group" all the clients that want to wait for synchronous replication
* in a given event loop iteration, and send a single GETACK for them all. */ * in a given event loop iteration, and send a single GETACK for them all. */
void replicationRequestAckFromSlaves(void) { void replicationRequestAckFromSlaves(void) {
g_pserver->get_ack_from_slaves = 1; g_pserver->get_ack_from_slaves = 1;
@ -3734,7 +3728,7 @@ void processClientsWaitingReplicas(void) {
last_numreplicas > c->bpop.numreplicas) last_numreplicas > c->bpop.numreplicas)
{ {
unblockClient(c); unblockClient(c);
addReplyLongLongAsync(c,last_numreplicas); addReplyLongLong(c,last_numreplicas);
} else { } else {
int numreplicas = replicationCountAcksByOffset(c->bpop.reploffset); int numreplicas = replicationCountAcksByOffset(c->bpop.reploffset);
@ -3742,7 +3736,7 @@ void processClientsWaitingReplicas(void) {
last_offset = c->bpop.reploffset; last_offset = c->bpop.reploffset;
last_numreplicas = numreplicas; last_numreplicas = numreplicas;
unblockClient(c); unblockClient(c);
addReplyLongLongAsync(c,numreplicas); addReplyLongLong(c,numreplicas);
} }
} }
fastlock_unlock(&c->lock); fastlock_unlock(&c->lock);
@ -4353,3 +4347,90 @@ static void propagateMasterStaleKeys()
decrRefCount(rgobj[0]); decrRefCount(rgobj[0]);
} }
void replicationNotifyLoadedKey(redisDb *db, robj_roptr key, robj_roptr val, long long expire) {
if (!g_pserver->fActiveReplica || listLength(g_pserver->slaves) == 0)
return;
// Send a digest over to the replicas
rio r;
createDumpPayload(&r, val, key.unsafe_robjcast());
redisObjectStack objPayload;
initStaticStringObject(objPayload, r.io.buffer.ptr);
redisObjectStack objTtl;
initStaticStringObject(objTtl, sdscatprintf(sdsempty(), "%lld", expire));
redisObjectStack objMvcc;
initStaticStringObject(objMvcc, sdscatprintf(sdsempty(), "%lu", mvccFromObj(val)));
redisObject *argv[5] = {shared.mvccrestore, key.unsafe_robjcast(), &objMvcc, &objTtl, &objPayload};
replicationFeedSlaves(g_pserver->slaves, db - g_pserver->db, argv, 5);
sdsfree(szFromObj(&objTtl));
sdsfree(szFromObj(&objMvcc));
sdsfree(r.io.buffer.ptr);
}
void replicateSubkeyExpire(redisDb *db, robj_roptr key, robj_roptr subkey, long long expire) {
if (!g_pserver->fActiveReplica || listLength(g_pserver->slaves) == 0)
return;
redisObjectStack objTtl;
initStaticStringObject(objTtl, sdscatprintf(sdsempty(), "%lld", expire));
redisObject *argv[4] = {shared.pexpirememberat, key.unsafe_robjcast(), subkey.unsafe_robjcast(), &objTtl};
replicationFeedSlaves(g_pserver->slaves, db - g_pserver->db, argv, 4);
sdsfree(szFromObj(&objTtl));
}
void flushReplBacklogToClients()
{
serverAssert(GlobalLocksAcquired());
if (g_pserver->repl_batch_offStart < 0)
return;
if (g_pserver->repl_batch_offStart != g_pserver->master_repl_offset) {
bool fAsyncWrite = false;
// Ensure no overflow
serverAssert(g_pserver->repl_batch_offStart < g_pserver->master_repl_offset);
serverAssert(g_pserver->master_repl_offset - g_pserver->repl_batch_offStart <= g_pserver->repl_backlog_size);
serverAssert(g_pserver->repl_batch_idxStart != g_pserver->repl_backlog_idx);
listIter li;
listNode *ln;
listRewind(g_pserver->slaves, &li);
while ((ln = listNext(&li))) {
client *replica = (client*)listNodeValue(ln);
if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_START) continue;
if (replica->flags & CLIENT_CLOSE_ASAP) continue;
std::unique_lock<fastlock> ul(replica->lock, std::defer_lock);
if (FCorrectThread(replica))
ul.lock();
else
fAsyncWrite = true;
if (g_pserver->repl_backlog_idx >= g_pserver->repl_batch_idxStart) {
long long cbCopy = g_pserver->repl_backlog_idx - g_pserver->repl_batch_idxStart;
serverAssert((g_pserver->master_repl_offset - g_pserver->repl_batch_offStart) == cbCopy);
serverAssert((g_pserver->repl_backlog_size - g_pserver->repl_batch_idxStart) >= (cbCopy));
serverAssert((g_pserver->repl_batch_idxStart + cbCopy) <= g_pserver->repl_backlog_size);
addReplyProto(replica, g_pserver->repl_backlog + g_pserver->repl_batch_idxStart, cbCopy);
} else {
auto cbPhase1 = g_pserver->repl_backlog_size - g_pserver->repl_batch_idxStart;
addReplyProto(replica, g_pserver->repl_backlog + g_pserver->repl_batch_idxStart, cbPhase1);
addReplyProto(replica, g_pserver->repl_backlog, g_pserver->repl_backlog_idx);
serverAssert((cbPhase1 + g_pserver->repl_backlog_idx) == (g_pserver->master_repl_offset - g_pserver->repl_batch_offStart));
}
}
if (fAsyncWrite)
ProcessPendingAsyncWrites();
// This may be called multiple times per "frame" so update with our progress flushing to clients
g_pserver->repl_batch_idxStart = g_pserver->repl_backlog_idx;
g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;
}
}

View File

@ -94,6 +94,7 @@ static const rio rioBufferIO = {
0, /* current checksum */ 0, /* current checksum */
0, /* flags */ 0, /* flags */
0, /* bytes read or written */ 0, /* bytes read or written */
0, /* keys since last callback */
0, /* read/write chunk size */ 0, /* read/write chunk size */
{ { NULL, 0 } } /* union for io-specific vars */ { { NULL, 0 } } /* union for io-specific vars */
}; };
@ -148,6 +149,7 @@ static const rio rioFileIO = {
0, /* current checksum */ 0, /* current checksum */
0, /* flags */ 0, /* flags */
0, /* bytes read or written */ 0, /* bytes read or written */
0, /* keys since last callback */
0, /* read/write chunk size */ 0, /* read/write chunk size */
{ { NULL, 0 } } /* union for io-specific vars */ { { NULL, 0 } } /* union for io-specific vars */
}; };
@ -243,6 +245,7 @@ static const rio rioConnIO = {
0, /* current checksum */ 0, /* current checksum */
0, /* flags */ 0, /* flags */
0, /* bytes read or written */ 0, /* bytes read or written */
0, /* keys since last callback */
0, /* read/write chunk size */ 0, /* read/write chunk size */
{ { NULL, 0 } } /* union for io-specific vars */ { { NULL, 0 } } /* union for io-specific vars */
}; };
@ -361,6 +364,7 @@ static const rio rioFdIO = {
0, /* current checksum */ 0, /* current checksum */
0, /* flags */ 0, /* flags */
0, /* bytes read or written */ 0, /* bytes read or written */
0, /* keys since last callback */
0, /* read/write chunk size */ 0, /* read/write chunk size */
{ { NULL, 0 } } /* union for io-specific vars */ { { NULL, 0 } } /* union for io-specific vars */
}; };

View File

@ -62,6 +62,9 @@ struct _rio {
/* The current checksum and flags (see RIO_FLAG_*) */ /* The current checksum and flags (see RIO_FLAG_*) */
uint64_t cksum, flags; uint64_t cksum, flags;
/* number of keys loaded since last rdbLoadProgressCallback */
long int keys_since_last_callback;
/* number of bytes read or written */ /* number of bytes read or written */
size_t processed_bytes; size_t processed_bytes;

View File

@ -72,7 +72,7 @@ struct ldbState {
list *children; /* All forked debugging sessions pids. */ list *children; /* All forked debugging sessions pids. */
int bp[LDB_BREAKPOINTS_MAX]; /* An array of breakpoints line numbers. */ int bp[LDB_BREAKPOINTS_MAX]; /* An array of breakpoints line numbers. */
int bpcount; /* Number of valid entries inside bp. */ int bpcount; /* Number of valid entries inside bp. */
int step; /* Stop at next line ragardless of breakpoints. */ int step; /* Stop at next line regardless of breakpoints. */
int luabp; /* Stop at next line because redis.breakpoint() was called. */ int luabp; /* Stop at next line because redis.breakpoint() was called. */
sds *src; /* Lua script source code split by line. */ sds *src; /* Lua script source code split by line. */
int lines; /* Number of lines in 'src'. */ int lines; /* Number of lines in 'src'. */
@ -413,9 +413,9 @@ void luaReplyToRedisReply(client *c, lua_State *lua) {
lua_pushnil(lua); /* Use nil to start iteration. */ lua_pushnil(lua); /* Use nil to start iteration. */
while (lua_next(lua,-2)) { while (lua_next(lua,-2)) {
/* Stack now: table, key, value */ /* Stack now: table, key, value */
luaReplyToRedisReply(c, lua); /* Return value. */ lua_pushvalue(lua,-2); /* Dup key before consuming. */
lua_pushvalue(lua,-1); /* Dup key before consuming. */
luaReplyToRedisReply(c, lua); /* Return key. */ luaReplyToRedisReply(c, lua); /* Return key. */
luaReplyToRedisReply(c, lua); /* Return value. */
/* Stack now: table, key. */ /* Stack now: table, key. */
maplen++; maplen++;
} }
@ -899,7 +899,7 @@ int luaRedisReplicateCommandsCommand(lua_State *lua) {
/* redis.breakpoint() /* redis.breakpoint()
* *
* Allows to stop execution during a debuggign session from within * Allows to stop execution during a debugging session from within
* the Lua code implementation, like if a breakpoint was set in the code * the Lua code implementation, like if a breakpoint was set in the code
* immediately after the function. */ * immediately after the function. */
int luaRedisBreakpointCommand(lua_State *lua) { int luaRedisBreakpointCommand(lua_State *lua) {
@ -1509,7 +1509,7 @@ void evalGenericCommand(client *c, int evalsha) {
/* Hash the code if this is an EVAL call */ /* Hash the code if this is an EVAL call */
sha1hex(funcname+2,(char*)ptrFromObj(c->argv[1]),sdslen((sds)ptrFromObj(c->argv[1]))); sha1hex(funcname+2,(char*)ptrFromObj(c->argv[1]),sdslen((sds)ptrFromObj(c->argv[1])));
} else { } else {
/* We already have the SHA if it is a EVALSHA */ /* We already have the SHA if it is an EVALSHA */
int j; int j;
char *sha = (char*)ptrFromObj(c->argv[1]); char *sha = (char*)ptrFromObj(c->argv[1]);
@ -1645,7 +1645,7 @@ void evalGenericCommand(client *c, int evalsha) {
* To do so we use a cache of SHA1s of scripts that we already propagated * To do so we use a cache of SHA1s of scripts that we already propagated
* as full EVAL, that's called the Replication Script Cache. * as full EVAL, that's called the Replication Script Cache.
* *
* For repliation, everytime a new replica attaches to the master, we need to * For replication, everytime a new replica attaches to the master, we need to
* flush our cache of scripts that can be replicated as EVALSHA, while * flush our cache of scripts that can be replicated as EVALSHA, while
* for AOF we need to do so every time we rewrite the AOF file. */ * for AOF we need to do so every time we rewrite the AOF file. */
if (evalsha && !g_pserver->lua_replicate_commands) { if (evalsha && !g_pserver->lua_replicate_commands) {
@ -1818,7 +1818,7 @@ void ldbLog(sds entry) {
} }
/* A version of ldbLog() which prevents producing logs greater than /* A version of ldbLog() which prevents producing logs greater than
* ldb.maxlen. The first time the limit is reached an hint is generated * ldb.maxlen. The first time the limit is reached a hint is generated
* to inform the user that reply trimming can be disabled using the * to inform the user that reply trimming can be disabled using the
* debugger "maxlen" command. */ * debugger "maxlen" command. */
void ldbLogWithMaxLen(sds entry) { void ldbLogWithMaxLen(sds entry) {
@ -1859,7 +1859,7 @@ void ldbSendLogs(void) {
} }
/* Start a debugging session before calling EVAL implementation. /* Start a debugging session before calling EVAL implementation.
* The techique we use is to capture the client socket file descriptor, * The technique we use is to capture the client socket file descriptor,
* in order to perform direct I/O with it from within Lua hooks. This * in order to perform direct I/O with it from within Lua hooks. This
* way we don't have to re-enter Redis in order to handle I/O. * way we don't have to re-enter Redis in order to handle I/O.
* *
@ -1873,7 +1873,7 @@ void ldbSendLogs(void) {
int ldbStartSession(client *c) { int ldbStartSession(client *c) {
ldb.forked = (c->flags & CLIENT_LUA_DEBUG_SYNC) == 0; ldb.forked = (c->flags & CLIENT_LUA_DEBUG_SYNC) == 0;
if (ldb.forked) { if (ldb.forked) {
pid_t cp = redisFork(); pid_t cp = redisFork(CHILD_TYPE_LDB);
if (cp == -1) { if (cp == -1) {
addReplyError(c,"Fork() failed: can't run EVAL in debugging mode."); addReplyError(c,"Fork() failed: can't run EVAL in debugging mode.");
return 0; return 0;
@ -1942,7 +1942,7 @@ void ldbEndSession(client *c) {
connNonBlock(ldb.conn); connNonBlock(ldb.conn);
connSendTimeout(ldb.conn,0); connSendTimeout(ldb.conn,0);
/* Close the client connectin after sending the final EVAL reply /* Close the client connection after sending the final EVAL reply
* in order to signal the end of the debugging session. */ * in order to signal the end of the debugging session. */
c->flags |= CLIENT_CLOSE_AFTER_REPLY; c->flags |= CLIENT_CLOSE_AFTER_REPLY;
@ -2112,7 +2112,7 @@ void ldbLogSourceLine(int lnum) {
/* Implement the "list" command of the Lua debugger. If around is 0 /* Implement the "list" command of the Lua debugger. If around is 0
* the whole file is listed, otherwise only a small portion of the file * the whole file is listed, otherwise only a small portion of the file
* around the specified line is shown. When a line number is specified * around the specified line is shown. When a line number is specified
* the amonut of context (lines before/after) is specified via the * the amount of context (lines before/after) is specified via the
* 'context' argument. */ * 'context' argument. */
void ldbList(int around, int context) { void ldbList(int around, int context) {
int j; int j;
@ -2123,7 +2123,7 @@ void ldbList(int around, int context) {
} }
} }
/* Append an human readable representation of the Lua value at position 'idx' /* Append a human readable representation of the Lua value at position 'idx'
* on the stack of the 'lua' state, to the SDS string passed as argument. * on the stack of the 'lua' state, to the SDS string passed as argument.
* The new SDS string with the represented value attached is returned. * The new SDS string with the represented value attached is returned.
* Used in order to implement ldbLogStackValue(). * Used in order to implement ldbLogStackValue().
@ -2367,7 +2367,7 @@ char *ldbRedisProtocolToHuman_Double(sds *o, char *reply) {
return p+2; return p+2;
} }
/* Log a Redis reply as debugger output, in an human readable format. /* Log a Redis reply as debugger output, in a human readable format.
* If the resulting string is longer than 'len' plus a few more chars * If the resulting string is longer than 'len' plus a few more chars
* used as prefix, it gets truncated. */ * used as prefix, it gets truncated. */
void ldbLogRedisReply(char *reply) { void ldbLogRedisReply(char *reply) {
@ -2551,7 +2551,7 @@ void ldbTrace(lua_State *lua) {
} }
} }
/* Impleemnts the debugger "maxlen" command. It just queries or sets the /* Implements the debugger "maxlen" command. It just queries or sets the
* ldb.maxlen variable. */ * ldb.maxlen variable. */
void ldbMaxlen(sds *argv, int argc) { void ldbMaxlen(sds *argv, int argc) {
if (argc == 2) { if (argc == 2) {
@ -2624,8 +2624,8 @@ ldbLog(sdsnew(" mode dataset changes will be retained."));
ldbLog(sdsnew("")); ldbLog(sdsnew(""));
ldbLog(sdsnew("Debugger functions you can call from Lua scripts:")); ldbLog(sdsnew("Debugger functions you can call from Lua scripts:"));
ldbLog(sdsnew("redis.debug() Produce logs in the debugger console.")); ldbLog(sdsnew("redis.debug() Produce logs in the debugger console."));
ldbLog(sdsnew("redis.breakpoint() Stop execution like if there was a breakpoing.")); ldbLog(sdsnew("redis.breakpoint() Stop execution like if there was a breakpoint in the"));
ldbLog(sdsnew(" in the next line of code.")); ldbLog(sdsnew(" next line of code."));
ldbSendLogs(); ldbSendLogs();
} else if (!strcasecmp(argv[0],"s") || !strcasecmp(argv[0],"step") || } else if (!strcasecmp(argv[0],"s") || !strcasecmp(argv[0],"step") ||
!strcasecmp(argv[0],"n") || !strcasecmp(argv[0],"next")) { !strcasecmp(argv[0],"n") || !strcasecmp(argv[0],"next")) {

View File

@ -444,7 +444,7 @@ sds sdscatlen(sds s, const void *t, size_t len) {
return s; return s;
} }
/* Append the specified null termianted C string to the sds string 's'. /* Append the specified null terminated C string to the sds string 's'.
* *
* After the call, the passed sds string is no longer valid and all the * After the call, the passed sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call. */ * references must be substituted with the new pointer returned by the call. */
@ -492,7 +492,7 @@ int sdsll2str(char *s, long long value) {
size_t l; size_t l;
/* Generate the string representation, this method produces /* Generate the string representation, this method produces
* an reversed string. */ * a reversed string. */
v = (value < 0) ? -value : value; v = (value < 0) ? -value : value;
p = s; p = s;
do { do {
@ -523,7 +523,7 @@ int sdsull2str(char *s, unsigned long long v) {
size_t l; size_t l;
/* Generate the string representation, this method produces /* Generate the string representation, this method produces
* an reversed string. */ * a reversed string. */
p = s; p = s;
do { do {
*p++ = '0'+(v%10); *p++ = '0'+(v%10);
@ -562,6 +562,7 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
va_list cpy; va_list cpy;
char staticbuf[1024], *buf = staticbuf, *t; char staticbuf[1024], *buf = staticbuf, *t;
size_t buflen = strlen(fmt)*2; size_t buflen = strlen(fmt)*2;
int bufstrlen;
/* We try to start using a static buffer for speed. /* We try to start using a static buffer for speed.
* If not possible we revert to heap allocation. */ * If not possible we revert to heap allocation. */
@ -572,16 +573,19 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
buflen = sizeof(staticbuf); buflen = sizeof(staticbuf);
} }
/* Try with buffers two times bigger every time we fail to /* Alloc enough space for buffer and \0 after failing to
* fit the string in the current buffer size. */ * fit the string in the current buffer size. */
while(1) { while(1) {
buf[buflen-2] = '\0';
va_copy(cpy,ap); va_copy(cpy,ap);
vsnprintf(buf, buflen, fmt, cpy); bufstrlen = vsnprintf(buf, buflen, fmt, cpy);
va_end(cpy); va_end(cpy);
if (buf[buflen-2] != '\0') { if (bufstrlen < 0) {
if (buf != staticbuf) s_free(buf); if (buf != staticbuf) s_free(buf);
buflen *= 2; return NULL;
}
if (((size_t)bufstrlen) >= buflen) {
if (buf != staticbuf) s_free(buf);
buflen = ((size_t)bufstrlen) + 1;
buf = s_malloc(buflen, MALLOC_SHARED); buf = s_malloc(buflen, MALLOC_SHARED);
if (buf == NULL) return NULL; if (buf == NULL) return NULL;
continue; continue;
@ -590,7 +594,7 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
} }
/* Finally concat the obtained string to the SDS string and return it. */ /* Finally concat the obtained string to the SDS string and return it. */
t = sdscat(s, buf); t = sdscatlen(s, buf, bufstrlen);
if (buf != staticbuf) s_free(buf); if (buf != staticbuf) s_free(buf);
return t; return t;
} }
@ -645,7 +649,7 @@ sds sdscatfmt(sds s, char const *fmt, ...) {
/* To avoid continuous reallocations, let's start with a buffer that /* To avoid continuous reallocations, let's start with a buffer that
* can hold at least two times the format string itself. It's not the * can hold at least two times the format string itself. It's not the
* best heuristic but seems to work in practice. */ * best heuristic but seems to work in practice. */
s = sdsMakeRoomFor(s, initlen + strlen(fmt)*2); s = sdsMakeRoomFor(s, strlen(fmt)*2);
va_start(ap,fmt); va_start(ap,fmt);
f = fmt; /* Next format specifier byte to process. */ f = fmt; /* Next format specifier byte to process. */
i = initlen; /* Position of the next byte to write to dest str. */ i = initlen; /* Position of the next byte to write to dest str. */
@ -1198,6 +1202,22 @@ int sdsTest(void) {
test_cond("sdscatprintf() seems working in the base case", test_cond("sdscatprintf() seems working in the base case",
sdslen(x) == 3 && memcmp(x,"123\0",4) == 0) sdslen(x) == 3 && memcmp(x,"123\0",4) == 0)
sdsfree(x);
x = sdscatprintf(sdsempty(),"a%cb",0);
test_cond("sdscatprintf() seems working with \\0 inside of result",
sdslen(x) == 3 && memcmp(x,"a\0""b\0",4) == 0)
{
sdsfree(x);
char etalon[1024*1024];
for (size_t i = 0; i < sizeof(etalon); i++) {
etalon[i] = '0';
}
x = sdscatprintf(sdsempty(),"%0*d",(int)sizeof(etalon),0);
test_cond("sdscatprintf() can print 1MB",
sdslen(x) == sizeof(etalon) && memcmp(x,etalon,sizeof(etalon)) == 0)
}
sdsfree(x); sdsfree(x);
x = sdsnew("--"); x = sdsnew("--");
x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX); x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX);

View File

@ -390,6 +390,10 @@ public:
: sdsview(sdsdup(other.m_str)) : sdsview(sdsdup(other.m_str))
{} {}
sdsstring(const char *rgch, size_t cch)
: sdsview(sdsnewlen(rgch, cch))
{}
sdsstring(sdsstring &&other) sdsstring(sdsstring &&other)
: sdsview(other.m_str) : sdsview(other.m_str)
{ {
@ -410,6 +414,12 @@ public:
return *this; return *this;
} }
sds release() {
sds sdsT = m_str;
m_str = nullptr;
return sdsT;
}
~sdsstring() ~sdsstring()
{ {
sdsfree(m_str); sdsfree(m_str);

View File

@ -133,13 +133,13 @@ typedef struct sentinelAddr {
/* The link to a sentinelRedisInstance. When we have the same set of Sentinels /* The link to a sentinelRedisInstance. When we have the same set of Sentinels
* monitoring many masters, we have different instances representing the * monitoring many masters, we have different instances representing the
* same Sentinels, one per master, and we need to share the hiredis connections * same Sentinels, one per master, and we need to share the hiredis connections
* among them. Oherwise if 5 Sentinels are monitoring 100 masters we create * among them. Otherwise if 5 Sentinels are monitoring 100 masters we create
* 500 outgoing connections instead of 5. * 500 outgoing connections instead of 5.
* *
* So this structure represents a reference counted link in terms of the two * So this structure represents a reference counted link in terms of the two
* hiredis connections for commands and Pub/Sub, and the fields needed for * hiredis connections for commands and Pub/Sub, and the fields needed for
* failure detection, since the ping/pong time are now local to the link: if * failure detection, since the ping/pong time are now local to the link: if
* the link is available, the instance is avaialbe. This way we don't just * the link is available, the instance is available. This way we don't just
* have 5 connections instead of 500, we also send 5 pings instead of 500. * have 5 connections instead of 500, we also send 5 pings instead of 500.
* *
* Links are shared only for Sentinels: master and slave instances have * Links are shared only for Sentinels: master and slave instances have
@ -988,7 +988,7 @@ instanceLink *createInstanceLink(void) {
return link; return link;
} }
/* Disconnect an hiredis connection in the context of an instance link. */ /* Disconnect a hiredis connection in the context of an instance link. */
void instanceLinkCloseConnection(instanceLink *link, redisAsyncContext *c) { void instanceLinkCloseConnection(instanceLink *link, redisAsyncContext *c) {
if (c == NULL) return; if (c == NULL) return;
@ -1078,6 +1078,7 @@ int sentinelTryConnectionSharing(sentinelRedisInstance *ri) {
releaseInstanceLink(ri->link,NULL); releaseInstanceLink(ri->link,NULL);
ri->link = match->link; ri->link = match->link;
match->link->refcount++; match->link->refcount++;
dictReleaseIterator(di);
return C_OK; return C_OK;
} }
dictReleaseIterator(di); dictReleaseIterator(di);
@ -1126,7 +1127,7 @@ int sentinelUpdateSentinelAddressInAllMasters(sentinelRedisInstance *ri) {
return reconfigured; return reconfigured;
} }
/* This function is called when an hiredis connection reported an error. /* This function is called when a hiredis connection reported an error.
* We set it to NULL and mark the link as disconnected so that it will be * We set it to NULL and mark the link as disconnected so that it will be
* reconnected again. * reconnected again.
* *
@ -1955,7 +1956,7 @@ void sentinelFlushConfig(void) {
int rewrite_status; int rewrite_status;
g_pserver->hz = CONFIG_DEFAULT_HZ; g_pserver->hz = CONFIG_DEFAULT_HZ;
rewrite_status = rewriteConfig(cserver.configfile); rewrite_status = rewriteConfig(cserver.configfile, 0);
g_pserver->hz = saved_hz; g_pserver->hz = saved_hz;
if (rewrite_status == -1) goto werr; if (rewrite_status == -1) goto werr;
@ -2016,7 +2017,7 @@ void sentinelSendAuthIfNeeded(sentinelRedisInstance *ri, redisAsyncContext *c) {
* The connection type is "cmd" or "pubsub" as specified by 'type'. * The connection type is "cmd" or "pubsub" as specified by 'type'.
* *
* This makes it possible to list all the sentinel instances connected * This makes it possible to list all the sentinel instances connected
* to a Redis servewr with CLIENT LIST, grepping for a specific name format. */ * to a Redis server with CLIENT LIST, grepping for a specific name format. */
void sentinelSetClientName(sentinelRedisInstance *ri, redisAsyncContext *c, const char *type) { void sentinelSetClientName(sentinelRedisInstance *ri, redisAsyncContext *c, const char *type) {
char name[64]; char name[64];
@ -2219,8 +2220,8 @@ void sentinelRefreshInstanceInfo(sentinelRedisInstance *ri, const char *info) {
} }
/* role:<role> */ /* role:<role> */
if (!memcmp(l,"role:master",11)) role = SRI_MASTER; if (sdslen(l) >= 11 && !memcmp(l,"role:master",11)) role = SRI_MASTER;
else if (!memcmp(l,"role:slave",10)) role = SRI_SLAVE; else if (sdslen(l) >= 10 && !memcmp(l,"role:slave",10)) role = SRI_SLAVE;
if (role == SRI_SLAVE) { if (role == SRI_SLAVE) {
/* master_host:<host> */ /* master_host:<host> */
@ -2471,7 +2472,7 @@ void sentinelPublishReplyCallback(redisAsyncContext *c, void *reply, void *privd
ri->last_pub_time = mstime(); ri->last_pub_time = mstime();
} }
/* Process an hello message received via Pub/Sub in master or slave instance, /* Process a hello message received via Pub/Sub in master or slave instance,
* or sent directly to this sentinel via the (fake) PUBLISH command of Sentinel. * or sent directly to this sentinel via the (fake) PUBLISH command of Sentinel.
* *
* If the master name specified in the message is not known, the message is * If the master name specified in the message is not known, the message is
@ -2608,7 +2609,7 @@ void sentinelReceiveHelloMessages(redisAsyncContext *c, void *reply, void *privd
sentinelProcessHelloMessage(r->element[2]->str, r->element[2]->len); sentinelProcessHelloMessage(r->element[2]->str, r->element[2]->len);
} }
/* Send an "Hello" message via Pub/Sub to the specified 'ri' Redis /* Send a "Hello" message via Pub/Sub to the specified 'ri' Redis
* instance in order to broadcast the current configuration for this * instance in order to broadcast the current configuration for this
* master, and to advertise the existence of this Sentinel at the same time. * master, and to advertise the existence of this Sentinel at the same time.
* *
@ -2662,7 +2663,7 @@ int sentinelSendHello(sentinelRedisInstance *ri) {
} }
/* Reset last_pub_time in all the instances in the specified dictionary /* Reset last_pub_time in all the instances in the specified dictionary
* in order to force the delivery of an Hello update ASAP. */ * in order to force the delivery of a Hello update ASAP. */
void sentinelForceHelloUpdateDictOfRedisInstances(dict *instances) { void sentinelForceHelloUpdateDictOfRedisInstances(dict *instances) {
dictIterator *di; dictIterator *di;
dictEntry *de; dictEntry *de;
@ -2676,13 +2677,13 @@ void sentinelForceHelloUpdateDictOfRedisInstances(dict *instances) {
dictReleaseIterator(di); dictReleaseIterator(di);
} }
/* This function forces the delivery of an "Hello" message (see /* This function forces the delivery of a "Hello" message (see
* sentinelSendHello() top comment for further information) to all the Redis * sentinelSendHello() top comment for further information) to all the Redis
* and Sentinel instances related to the specified 'master'. * and Sentinel instances related to the specified 'master'.
* *
* It is technically not needed since we send an update to every instance * It is technically not needed since we send an update to every instance
* with a period of SENTINEL_PUBLISH_PERIOD milliseconds, however when a * with a period of SENTINEL_PUBLISH_PERIOD milliseconds, however when a
* Sentinel upgrades a configuration it is a good idea to deliever an update * Sentinel upgrades a configuration it is a good idea to deliver an update
* to the other Sentinels ASAP. */ * to the other Sentinels ASAP. */
int sentinelForceHelloUpdateForMaster(sentinelRedisInstance *master) { int sentinelForceHelloUpdateForMaster(sentinelRedisInstance *master) {
if (!(master->flags & SRI_MASTER)) return C_ERR; if (!(master->flags & SRI_MASTER)) return C_ERR;
@ -3083,7 +3084,7 @@ void sentinelCommand(client *c) {
* ip and port are the ip and port of the master we want to be * ip and port are the ip and port of the master we want to be
* checked by Sentinel. Note that the command will not check by * checked by Sentinel. Note that the command will not check by
* name but just by master, in theory different Sentinels may monitor * name but just by master, in theory different Sentinels may monitor
* differnet masters with the same name. * different masters with the same name.
* *
* current-epoch is needed in order to understand if we are allowed * current-epoch is needed in order to understand if we are allowed
* to vote for a failover leader or not. Each Sentinel can vote just * to vote for a failover leader or not. Each Sentinel can vote just
@ -3510,14 +3511,13 @@ void sentinelSetCommand(client *c) {
"Reconfiguration of scripts path is denied for " "Reconfiguration of scripts path is denied for "
"security reasons. Check the deny-scripts-reconfig " "security reasons. Check the deny-scripts-reconfig "
"configuration directive in your Sentinel configuration"); "configuration directive in your Sentinel configuration");
return; goto seterr;
} }
if (strlen(value) && access(value,X_OK) == -1) { if (strlen(value) && access(value,X_OK) == -1) {
addReplyError(c, addReplyError(c,
"Notification script seems non existing or non executable"); "Notification script seems non existing or non executable");
if (changes) sentinelFlushConfig(); goto seterr;
return;
} }
sdsfree(ri->notification_script); sdsfree(ri->notification_script);
ri->notification_script = strlen(value) ? sdsnew(value) : NULL; ri->notification_script = strlen(value) ? sdsnew(value) : NULL;
@ -3530,15 +3530,14 @@ void sentinelSetCommand(client *c) {
"Reconfiguration of scripts path is denied for " "Reconfiguration of scripts path is denied for "
"security reasons. Check the deny-scripts-reconfig " "security reasons. Check the deny-scripts-reconfig "
"configuration directive in your Sentinel configuration"); "configuration directive in your Sentinel configuration");
return; goto seterr;
} }
if (strlen(value) && access(value,X_OK) == -1) { if (strlen(value) && access(value,X_OK) == -1) {
addReplyError(c, addReplyError(c,
"Client reconfiguration script seems non existing or " "Client reconfiguration script seems non existing or "
"non executable"); "non executable");
if (changes) sentinelFlushConfig(); goto seterr;
return;
} }
sdsfree(ri->client_reconfig_script); sdsfree(ri->client_reconfig_script);
ri->client_reconfig_script = strlen(value) ? sdsnew(value) : NULL; ri->client_reconfig_script = strlen(value) ? sdsnew(value) : NULL;
@ -3588,8 +3587,7 @@ void sentinelSetCommand(client *c) {
} else { } else {
addReplyErrorFormat(c,"Unknown option or number of arguments for " addReplyErrorFormat(c,"Unknown option or number of arguments for "
"SENTINEL SET '%s'", option); "SENTINEL SET '%s'", option);
if (changes) sentinelFlushConfig(); goto seterr;
return;
} }
/* Log the event. */ /* Log the event. */
@ -3615,9 +3613,11 @@ void sentinelSetCommand(client *c) {
return; return;
badfmt: /* Bad format errors */ badfmt: /* Bad format errors */
if (changes) sentinelFlushConfig();
addReplyErrorFormat(c,"Invalid argument '%s' for SENTINEL SET '%s'", addReplyErrorFormat(c,"Invalid argument '%s' for SENTINEL SET '%s'",
(char*)ptrFromObj(c->argv[badarg]),option); (char*)ptrFromObj(c->argv[badarg]),option);
seterr:
if (changes) sentinelFlushConfig();
return;
} }
/* Our fake PUBLISH command: it is actually useful only to receive hello messages /* Our fake PUBLISH command: it is actually useful only to receive hello messages
@ -3996,7 +3996,7 @@ int sentinelSendSlaveOf(sentinelRedisInstance *ri, const char *host, int port) {
* the following tasks: * the following tasks:
* 1) Reconfigure the instance according to the specified host/port params. * 1) Reconfigure the instance according to the specified host/port params.
* 2) Rewrite the configuration. * 2) Rewrite the configuration.
* 3) Disconnect all clients (but this one sending the commnad) in order * 3) Disconnect all clients (but this one sending the command) in order
* to trigger the ask-master-on-reconnection protocol for connected * to trigger the ask-master-on-reconnection protocol for connected
* clients. * clients.
* *
@ -4551,7 +4551,7 @@ void sentinelHandleDictOfRedisInstances(dict *instances) {
* difference bigger than SENTINEL_TILT_TRIGGER milliseconds if one of the * difference bigger than SENTINEL_TILT_TRIGGER milliseconds if one of the
* following conditions happen: * following conditions happen:
* *
* 1) The Sentiel process for some time is blocked, for every kind of * 1) The Sentinel process for some time is blocked, for every kind of
* random reason: the load is huge, the computer was frozen for some time * random reason: the load is huge, the computer was frozen for some time
* in I/O or alike, the process was stopped by a signal. Everything. * in I/O or alike, the process was stopped by a signal. Everything.
* 2) The system clock was altered significantly. * 2) The system clock was altered significantly.

File diff suppressed because it is too large Load Diff

View File

@ -327,6 +327,7 @@ inline bool operator!=(const void *p, const robj_sharedptr &rhs)
#define PROTO_MAX_QUERYBUF_LEN (1024*1024*1024) /* 1GB max query buffer. */ #define PROTO_MAX_QUERYBUF_LEN (1024*1024*1024) /* 1GB max query buffer. */
#define PROTO_IOBUF_LEN (1024*16) /* Generic I/O buffer size */ #define PROTO_IOBUF_LEN (1024*16) /* Generic I/O buffer size */
#define PROTO_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */ #define PROTO_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */
#define PROTO_ASYNC_REPLY_CHUNK_BYTES (1024)
#define PROTO_INLINE_MAX_SIZE (1024*64) /* Max size of inline reads */ #define PROTO_INLINE_MAX_SIZE (1024*64) /* Max size of inline reads */
#define PROTO_MBULK_BIG_ARG (1024*32) #define PROTO_MBULK_BIG_ARG (1024*32)
#define LONG_STR_SIZE 21 /* Bytes needed for long -> str + '\0' */ #define LONG_STR_SIZE 21 /* Bytes needed for long -> str + '\0' */
@ -340,10 +341,18 @@ inline bool operator!=(const void *p, const robj_sharedptr &rhs)
* in order to make sure of not over provisioning more than 128 fds. */ * in order to make sure of not over provisioning more than 128 fds. */
#define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96) #define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96)
/* OOM Score Adjustment classes. */
#define CONFIG_OOM_MASTER 0
#define CONFIG_OOM_REPLICA 1
#define CONFIG_OOM_BGCHILD 2
#define CONFIG_OOM_COUNT 3
extern int configOOMScoreAdjValuesDefaults[CONFIG_OOM_COUNT];
/* Hash table parameters */ /* Hash table parameters */
#define HASHTABLE_MIN_FILL 10 /* Minimal hash table fill 10% */ #define HASHTABLE_MIN_FILL 10 /* Minimal hash table fill 10% */
/* Command flags. Please check the command table defined in the redis.c file /* Command flags. Please check the command table defined in the server.cpp file
* for more information about the meaning of every flag. */ * for more information about the meaning of every flag. */
#define CMD_WRITE (1ULL<<0) /* "write" flag */ #define CMD_WRITE (1ULL<<0) /* "write" flag */
#define CMD_READONLY (1ULL<<1) /* "read-only" flag */ #define CMD_READONLY (1ULL<<1) /* "read-only" flag */
@ -448,7 +457,9 @@ inline bool operator!=(const void *p, const robj_sharedptr &rhs)
about writes performed by myself.*/ about writes performed by myself.*/
#define CLIENT_IN_TO_TABLE (1ULL<<38) /* This client is in the timeout table. */ #define CLIENT_IN_TO_TABLE (1ULL<<38) /* This client is in the timeout table. */
#define CLIENT_PROTOCOL_ERROR (1ULL<<39) /* Protocol error chatting with it. */ #define CLIENT_PROTOCOL_ERROR (1ULL<<39) /* Protocol error chatting with it. */
#define CLIENT_FORCE_REPLY (1ULL<<40) /* Should addReply be forced to write the text? */ #define CLIENT_CLOSE_AFTER_COMMAND (1ULL<<40) /* Close after executing commands
* and writing entire reply. */
#define CLIENT_FORCE_REPLY (1ULL<<41) /* Should addReply be forced to write the text? */
/* Client block type (btype field in client structure) /* Client block type (btype field in client structure)
* if CLIENT_BLOCKED flag is set. */ * if CLIENT_BLOCKED flag is set. */
@ -532,6 +543,12 @@ inline bool operator!=(const void *p, const robj_sharedptr &rhs)
#define LL_WARNING 3 #define LL_WARNING 3
#define LL_RAW (1<<10) /* Modifier to log without timestamp */ #define LL_RAW (1<<10) /* Modifier to log without timestamp */
/* Error severity levels */
#define ERR_CRITICAL 0
#define ERR_ERROR 1
#define ERR_WARNING 2
#define ERR_NOTICE 3
/* Supervision options */ /* Supervision options */
#define SUPERVISED_NONE 0 #define SUPERVISED_NONE 0
#define SUPERVISED_AUTODETECT 1 #define SUPERVISED_AUTODETECT 1
@ -554,11 +571,21 @@ inline bool operator!=(const void *p, const robj_sharedptr &rhs)
#define REPL_DISKLESS_LOAD_WHEN_DB_EMPTY 1 #define REPL_DISKLESS_LOAD_WHEN_DB_EMPTY 1
#define REPL_DISKLESS_LOAD_SWAPDB 2 #define REPL_DISKLESS_LOAD_SWAPDB 2
/* TLS Client Authentication */
#define TLS_CLIENT_AUTH_NO 0
#define TLS_CLIENT_AUTH_YES 1
#define TLS_CLIENT_AUTH_OPTIONAL 2
/* Sets operations codes */ /* Sets operations codes */
#define SET_OP_UNION 0 #define SET_OP_UNION 0
#define SET_OP_DIFF 1 #define SET_OP_DIFF 1
#define SET_OP_INTER 2 #define SET_OP_INTER 2
/* oom-score-adj defines */
#define OOM_SCORE_ADJ_NO 0
#define OOM_SCORE_RELATIVE 1
#define OOM_SCORE_ADJ_ABSOLUTE 2
/* Redis maxmemory strategies. Instead of using just incremental number /* Redis maxmemory strategies. Instead of using just incremental number
* for this defines, we use a set of flags so that testing for certain * for this defines, we use a set of flags so that testing for certain
* properties common to multiple policies is faster. */ * properties common to multiple policies is faster. */
@ -622,6 +649,7 @@ inline bool operator!=(const void *p, const robj_sharedptr &rhs)
#define NOTIFY_EVICTED (1<<9) /* e */ #define NOTIFY_EVICTED (1<<9) /* e */
#define NOTIFY_STREAM (1<<10) /* t */ #define NOTIFY_STREAM (1<<10) /* t */
#define NOTIFY_KEY_MISS (1<<11) /* m (Note: This one is excluded from NOTIFY_ALL on purpose) */ #define NOTIFY_KEY_MISS (1<<11) /* m (Note: This one is excluded from NOTIFY_ALL on purpose) */
#define NOTIFY_LOADED (1<<12) /* module only key space notification, indicate a key loaded from rdb */
#define NOTIFY_ALL (NOTIFY_GENERIC | NOTIFY_STRING | NOTIFY_LIST | NOTIFY_SET | NOTIFY_HASH | NOTIFY_ZSET | NOTIFY_EXPIRED | NOTIFY_EVICTED | NOTIFY_STREAM) /* A flag */ #define NOTIFY_ALL (NOTIFY_GENERIC | NOTIFY_STRING | NOTIFY_LIST | NOTIFY_SET | NOTIFY_HASH | NOTIFY_ZSET | NOTIFY_EXPIRED | NOTIFY_EVICTED | NOTIFY_STREAM) /* A flag */
/* Get the first bind addr or NULL */ /* Get the first bind addr or NULL */
@ -639,11 +667,11 @@ inline bool operator!=(const void *p, const robj_sharedptr &rhs)
/* A redis object, that is a type able to hold a string / list / set */ /* A redis object, that is a type able to hold a string / list / set */
/* The actual Redis Object */ /* The actual Redis Object */
#define OBJ_STRING 0 /* String object. */ #define OBJ_STRING 0 /* String object. */
#define OBJ_LIST 1 /* List object. */ #define OBJ_LIST 1 /* List object. */
#define OBJ_SET 2 /* Set object. */ #define OBJ_SET 2 /* Set object. */
#define OBJ_ZSET 3 /* Sorted set object. */ #define OBJ_ZSET 3 /* Sorted set object. */
#define OBJ_HASH 4 /* Hash object. */ #define OBJ_HASH 4 /* Hash object. */
/* The "module" object type is a special one that signals that the object /* The "module" object type is a special one that signals that the object
* is one directly managed by a Redis module. In this case the value points * is one directly managed by a Redis module. In this case the value points
@ -656,10 +684,10 @@ inline bool operator!=(const void *p, const robj_sharedptr &rhs)
* by a 64 bit module type ID, which has a 54 bits module-specific signature * by a 64 bit module type ID, which has a 54 bits module-specific signature
* in order to dispatch the loading to the right module, plus a 10 bits * in order to dispatch the loading to the right module, plus a 10 bits
* encoding version. */ * encoding version. */
#define OBJ_MODULE 5 /* Module object. */ #define OBJ_MODULE 5 /* Module object. */
#define OBJ_STREAM 6 /* Stream object. */ #define OBJ_STREAM 6 /* Stream object. */
#define OBJ_CRON 7 /* CRON job */ #define OBJ_CRON 7 /* CRON job */
#define OBJ_NESTEDHASH 8 /* Nested Hash Object */
/* Extract encver / signature from a module type ID. */ /* Extract encver / signature from a module type ID. */
#define REDISMODULE_TYPE_ENCVER_BITS 10 #define REDISMODULE_TYPE_ENCVER_BITS 10
@ -805,7 +833,7 @@ struct redisObjectExtended {
uint64_t mvcc_tstamp; uint64_t mvcc_tstamp;
}; };
typedef class redisObject { typedef struct redisObject {
protected: protected:
redisObject() {} redisObject() {}
@ -907,19 +935,24 @@ struct redisDb {
~redisDb(); ~redisDb();
dict *pdict; /* The keyspace for this DB */ ::dict *dict; /* The keyspace for this DB */
expireset *setexpire; expireset *setexpire;
expireset::setiter expireitr; expireset::setiter expireitr;
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/ ::dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/
dict *ready_keys; /* Blocked keys that received a PUSH */ ::dict *ready_keys; /* Blocked keys that received a PUSH */
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */ ::dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
int id; /* Database ID */ int id; /* Database ID */
long long last_expire_set; /* when the last expire was set */ long long last_expire_set; /* when the last expire was set */
double avg_ttl; /* Average TTL, just for stats */ double avg_ttl; /* Average TTL, just for stats */
list *defrag_later; /* List of key names to attempt to defrag one by one, gradually. */ list *defrag_later; /* List of key names to attempt to defrag one by one, gradually. */
}; };
/* Declare database backup that include redis main DBs and slots to keys map.
* Definition is in db.c. We can't define it here since we define CLUSTER_SLOTS
* in cluster.h. */
typedef struct dbBackup dbBackup;
/* Client MULTI/EXEC state */ /* Client MULTI/EXEC state */
typedef struct multiCmd { typedef struct multiCmd {
robj **argv; robj **argv;
@ -933,6 +966,9 @@ typedef struct multiState {
int cmd_flags; /* The accumulated command flags OR-ed together. int cmd_flags; /* The accumulated command flags OR-ed together.
So if at least a command has a given flag, it So if at least a command has a given flag, it
will be set in this field. */ will be set in this field. */
int cmd_inv_flags; /* Same as cmd_flags, OR-ing the ~flags. so that it
is possible to know if all the commands have a
certain flag. */
int minreplicas; /* MINREPLICAS for synchronous replication */ int minreplicas; /* MINREPLICAS for synchronous replication */
time_t minreplicas_timeout; /* MINREPLICAS timeout as unixtime. */ time_t minreplicas_timeout; /* MINREPLICAS timeout as unixtime. */
} multiState; } multiState;
@ -945,7 +981,7 @@ typedef struct blockingState {
* is > timeout then the operation timed out. */ * is > timeout then the operation timed out. */
/* BLOCKED_LIST, BLOCKED_ZSET and BLOCKED_STREAM */ /* BLOCKED_LIST, BLOCKED_ZSET and BLOCKED_STREAM */
dict *keys; /* The keys we are waiting to terminate a blocking ::dict *keys; /* The keys we are waiting to terminate a blocking
* operation such as BLPOP or XREAD. Or NULL. */ * operation such as BLPOP or XREAD. Or NULL. */
robj *target; /* The key that should receive the element, robj *target; /* The key that should receive the element,
* for BRPOPLPUSH. */ * for BRPOPLPUSH. */
@ -1047,6 +1083,7 @@ typedef struct client {
size_t querybuf_peak; /* Recent (100ms or more) peak of querybuf size. */ size_t querybuf_peak; /* Recent (100ms or more) peak of querybuf size. */
int argc; /* Num of arguments of current command. */ int argc; /* Num of arguments of current command. */
robj **argv; /* Arguments of current command. */ robj **argv; /* Arguments of current command. */
size_t argv_len_sum; /* Sum of lengths of objects in argv list. */
struct redisCommand *cmd, *lastcmd; /* Last command executed. */ struct redisCommand *cmd, *lastcmd; /* Last command executed. */
user *puser; /* User associated with this connection. If the user *puser; /* User associated with this connection. If the
user is set to NULL the connection can do user is set to NULL the connection can do
@ -1065,7 +1102,7 @@ typedef struct client {
std::atomic<uint64_t> flags; /* Client flags: CLIENT_* macros. */ std::atomic<uint64_t> flags; /* Client flags: CLIENT_* macros. */
int casyncOpsPending; int casyncOpsPending;
int fPendingAsyncWrite; /* NOTE: Not a flag because it is written to outside of the client lock (locked by the global lock instead) */ int fPendingAsyncWrite; /* NOTE: Not a flag because it is written to outside of the client lock (locked by the global lock instead) */
int fPendingAsyncWriteHandler; std::atomic<bool> fPendingAsyncWriteHandler;
int authenticated; /* Needed when the default user requires auth. */ int authenticated; /* Needed when the default user requires auth. */
int replstate; /* Replication state if this is a replica. */ int replstate; /* Replication state if this is a replica. */
int repl_put_online_on_ack; /* Install replica write handler on ACK. */ int repl_put_online_on_ack; /* Install replica write handler on ACK. */
@ -1082,7 +1119,7 @@ typedef struct client {
copying this replica output buffer copying this replica output buffer
should use. */ should use. */
char replid[CONFIG_RUN_ID_SIZE+1]; /* Master replication ID (if master). */ char replid[CONFIG_RUN_ID_SIZE+1]; /* Master replication ID (if master). */
int slave_listening_port; /* As configured with: SLAVECONF listening-port */ int slave_listening_port; /* As configured with: REPLCONF listening-port */
char slave_ip[NET_IP_STR_LEN]; /* Optionally given by REPLCONF ip-address */ char slave_ip[NET_IP_STR_LEN]; /* Optionally given by REPLCONF ip-address */
int slave_capa; /* Slave capabilities: SLAVE_CAPA_* bitwise OR. */ int slave_capa; /* Slave capabilities: SLAVE_CAPA_* bitwise OR. */
multiState mstate; /* MULTI/EXEC state */ multiState mstate; /* MULTI/EXEC state */
@ -1090,7 +1127,7 @@ typedef struct client {
blockingState bpop; /* blocking state */ blockingState bpop; /* blocking state */
long long woff; /* Last write global replication offset. */ long long woff; /* Last write global replication offset. */
list *watched_keys; /* Keys WATCHED for MULTI/EXEC CAS */ list *watched_keys; /* Keys WATCHED for MULTI/EXEC CAS */
dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */ ::dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */
list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */ list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */
sds peerid; /* Cached peer ID. */ sds peerid; /* Cached peer ID. */
listNode *client_list_node; /* list node in client list */ listNode *client_list_node; /* list node in client list */
@ -1128,16 +1165,14 @@ typedef struct client {
char buf[PROTO_REPLY_CHUNK_BYTES]; char buf[PROTO_REPLY_CHUNK_BYTES];
/* Async Response Buffer - other threads write here */ /* Async Response Buffer - other threads write here */
int bufposAsync; clientReplyBlock *replyAsync;
int buflenAsync;
char *bufAsync;
int iel; /* the event loop index we're registered with */ int iel; /* the event loop index we're registered with */
struct fastlock lock; struct fastlock lock;
int master_error; int master_error;
// post a function from a non-client thread to run on its client thread // post a function from a non-client thread to run on its client thread
bool postFunction(std::function<void(client *)> fn); bool postFunction(std::function<void(client *)> fn, bool fLock = true);
} client; } client;
struct saveparam { struct saveparam {
@ -1160,7 +1195,7 @@ struct sharedObjectsStruct {
*busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk, *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,
*unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink, *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink,
*rpop, *lpop, *lpush, *rpoplpush, *zpopmin, *zpopmax, *emptyscan, *rpop, *lpop, *lpush, *rpoplpush, *zpopmin, *zpopmax, *emptyscan,
*multi, *exec, *srem, *hdel, *zrem, *multi, *exec, *srem, *hdel, *zrem, *mvccrestore, *pexpirememberat,
*select[PROTO_SHARED_SELECT_CMDS], *select[PROTO_SHARED_SELECT_CMDS],
*integers[OBJ_SHARED_INTEGERS], *integers[OBJ_SHARED_INTEGERS],
*mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */ *mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */
@ -1194,7 +1229,7 @@ typedef struct zskiplist {
} zskiplist; } zskiplist;
typedef struct zset { typedef struct zset {
dict *pdict; ::dict *dict;
zskiplist *zsl; zskiplist *zsl;
} zset; } zset;
@ -1219,7 +1254,7 @@ typedef struct redisOp {
} redisOp; } redisOp;
/* Defines an array of Redis operations. There is an API to add to this /* Defines an array of Redis operations. There is an API to add to this
* structure in a easy way. * structure in an easy way.
* *
* redisOpArrayInit(); * redisOpArrayInit();
* redisOpArrayAppend(); * redisOpArrayAppend();
@ -1326,9 +1361,11 @@ struct clusterState;
#endif #endif
#define CHILD_INFO_MAGIC 0xC17DDA7A12345678LL #define CHILD_INFO_MAGIC 0xC17DDA7A12345678LL
#define CHILD_INFO_TYPE_RDB 0 #define CHILD_TYPE_NONE 0
#define CHILD_INFO_TYPE_AOF 1 #define CHILD_TYPE_RDB 1
#define CHILD_INFO_TYPE_MODULE 3 #define CHILD_TYPE_AOF 2
#define CHILD_TYPE_LDB 3
#define CHILD_TYPE_MODULE 4
#define MAX_EVENT_LOOPS 16 #define MAX_EVENT_LOOPS 16
#define IDX_EVENT_LOOP_MAIN 0 #define IDX_EVENT_LOOP_MAIN 0
@ -1393,6 +1430,7 @@ struct redisMaster {
struct redisServerConst { struct redisServerConst {
pid_t pid; /* Main process pid. */ pid_t pid; /* Main process pid. */
time_t stat_starttime; /* Server start time */ time_t stat_starttime; /* Server start time */
pthread_t main_thread_id; /* Main thread id */
char *configfile; /* Absolute config file path, or NULL */ char *configfile; /* Absolute config file path, or NULL */
char *executable; /* Absolute executable file path. */ char *executable; /* Absolute executable file path. */
char **exec_argv; /* Executable argv vector (copy). */ char **exec_argv; /* Executable argv vector (copy). */
@ -1440,6 +1478,7 @@ struct redisServerConst {
bool fUsePro = false; bool fUsePro = false;
int thread_min_client_threshold = 50; int thread_min_client_threshold = 50;
int multimaster_no_forward; int multimaster_no_forward;
int enable_motd; /* Flag to retrieve the Message of today using CURL request*/
}; };
struct redisServer { struct redisServer {
@ -1448,12 +1487,14 @@ struct redisServer {
int config_hz; /* Configured HZ value. May be different than int config_hz; /* Configured HZ value. May be different than
the actual 'hz' field value if dynamic-hz the actual 'hz' field value if dynamic-hz
is enabled. */ is enabled. */
std::atomic<int> hz; /* serverCron() calls frequency in hertz */ std::atomic<int> hz; /* serverCron() calls frequency in hertz */
int in_fork_child; /* indication that this is a fork child */
redisDb *db; redisDb *db;
dict *commands; /* Command table */ ::dict *commands; /* Command table */
dict *orig_commands; /* Command table before command renaming. */ ::dict *orig_commands; /* Command table before command renaming. */
struct redisServerThreadVars rgthreadvar[MAX_EVENT_LOOPS]; struct redisServerThreadVars rgthreadvar[MAX_EVENT_LOOPS];
pthread_t rgthread[MAX_EVENT_LOOPS];
std::atomic<unsigned int> lruclock; /* Clock for LRU eviction */ std::atomic<unsigned int> lruclock; /* Clock for LRU eviction */
std::atomic<int> shutdown_asap; /* SHUTDOWN needed ASAP */ std::atomic<int> shutdown_asap; /* SHUTDOWN needed ASAP */
@ -1464,9 +1505,10 @@ struct redisServer {
int sentinel_mode; /* True if this instance is a Sentinel. */ int sentinel_mode; /* True if this instance is a Sentinel. */
size_t initial_memory_usage; /* Bytes used after initialization. */ size_t initial_memory_usage; /* Bytes used after initialization. */
int always_show_logo; /* Show logo even for non-stdout logging. */ int always_show_logo; /* Show logo even for non-stdout logging. */
char *ignore_warnings; /* Config: warnings that should be ignored. */
/* Modules */ /* Modules */
dict *moduleapi; /* Exported core APIs dictionary for modules. */ ::dict *moduleapi; /* Exported core APIs dictionary for modules. */
dict *sharedapi; /* Like moduleapi but containing the APIs that ::dict *sharedapi; /* Like moduleapi but containing the APIs that
modules share with each other. */ modules share with each other. */
list *loadmodule_queue; /* List of modules to load at startup. */ list *loadmodule_queue; /* List of modules to load at startup. */
pid_t module_child_pid; /* PID of module child */ pid_t module_child_pid; /* PID of module child */
@ -1487,7 +1529,7 @@ struct redisServer {
rax *clients_timeout_table; /* Radix tree for blocked clients timeouts. */ rax *clients_timeout_table; /* Radix tree for blocked clients timeouts. */
rax *clients_index; /* Active clients dictionary by client ID. */ rax *clients_index; /* Active clients dictionary by client ID. */
mstime_t clients_pause_end_time; /* Time when we undo clients_paused */ mstime_t clients_pause_end_time; /* Time when we undo clients_paused */
dict *migrate_cached_sockets;/* MIGRATE cached sockets */ ::dict *migrate_cached_sockets;/* MIGRATE cached sockets */
std::atomic<uint64_t> next_client_id; /* Next client unique ID. Incremental. */ std::atomic<uint64_t> next_client_id; /* Next client unique ID. Incremental. */
int protected_mode; /* Don't accept external connections. */ int protected_mode; /* Don't accept external connections. */
long long events_processed_while_blocked; /* processEventsWhileBlocked() */ long long events_processed_while_blocked; /* processEventsWhileBlocked() */
@ -1497,7 +1539,8 @@ struct redisServer {
off_t loading_total_bytes; off_t loading_total_bytes;
off_t loading_loaded_bytes; off_t loading_loaded_bytes;
time_t loading_start_time; time_t loading_start_time;
off_t loading_process_events_interval_bytes; unsigned long loading_process_events_interval_bytes;
unsigned int loading_process_events_interval_keys;
int active_expire_enabled; /* Can be disabled for testing purposes. */ int active_expire_enabled; /* Can be disabled for testing purposes. */
@ -1535,6 +1578,10 @@ struct redisServer {
size_t stat_module_cow_bytes; /* Copy on write bytes during module fork. */ size_t stat_module_cow_bytes; /* Copy on write bytes during module fork. */
uint64_t stat_clients_type_memory[CLIENT_TYPE_COUNT];/* Mem usage by type */ uint64_t stat_clients_type_memory[CLIENT_TYPE_COUNT];/* Mem usage by type */
long long stat_unexpected_error_replies; /* Number of unexpected (aof-loading, replica to master, etc.) error replies */ long long stat_unexpected_error_replies; /* Number of unexpected (aof-loading, replica to master, etc.) error replies */
long long stat_io_reads_processed; /* Number of read events processed by IO / Main threads */
long long stat_io_writes_processed; /* Number of write events processed by IO / Main threads */
std::atomic<long long> stat_total_reads_processed; /* Total number of read events processed */
std::atomic<long long> stat_total_writes_processed; /* Total number of write events processed */
/* The following two are used to track instantaneous metrics, like /* The following two are used to track instantaneous metrics, like
* number of operations per second, network traffic. */ * number of operations per second, network traffic. */
struct { struct {
@ -1670,7 +1717,7 @@ struct redisServer {
char *slave_announce_ip; /* Give the master this ip address. */ char *slave_announce_ip; /* Give the master this ip address. */
int repl_slave_lazy_flush; /* Lazy FLUSHALL before loading DB? */ int repl_slave_lazy_flush; /* Lazy FLUSHALL before loading DB? */
/* Replication script cache. */ /* Replication script cache. */
dict *repl_scriptcache_dict; /* SHA1 all slaves are aware of. */ ::dict *repl_scriptcache_dict; /* SHA1 all slaves are aware of. */
list *repl_scriptcache_fifo; /* First in, first out LRU eviction. */ list *repl_scriptcache_fifo; /* First in, first out LRU eviction. */
unsigned int repl_scriptcache_size; /* Max number of elements. */ unsigned int repl_scriptcache_size; /* Max number of elements. */
/* Synchronous replication. */ /* Synchronous replication. */
@ -1680,10 +1727,13 @@ struct redisServer {
unsigned int maxclients; /* Max number of simultaneous clients */ unsigned int maxclients; /* Max number of simultaneous clients */
unsigned long long maxmemory; /* Max number of memory bytes to use */ unsigned long long maxmemory; /* Max number of memory bytes to use */
int maxmemory_policy; /* Policy for key eviction */ int maxmemory_policy; /* Policy for key eviction */
int maxmemory_samples; /* Pricision of random sampling */ int maxmemory_samples; /* Precision of random sampling */
int lfu_log_factor; /* LFU logarithmic counter factor. */ int lfu_log_factor; /* LFU logarithmic counter factor. */
int lfu_decay_time; /* LFU counter decay factor. */ int lfu_decay_time; /* LFU counter decay factor. */
long long proto_max_bulk_len; /* Protocol bulk length maximum size. */ long long proto_max_bulk_len; /* Protocol bulk length maximum size. */
int oom_score_adj_base; /* Base oom_score_adj value, as observed on startup */
int oom_score_adj_values[CONFIG_OOM_COUNT]; /* Linux oom_score_adj configuration */
int oom_score_adj; /* If true, oom_score_adj is managed */
/* Blocked clients */ /* Blocked clients */
unsigned int blocked_clients; /* # of clients executing a blocking cmd.*/ unsigned int blocked_clients; /* # of clients executing a blocking cmd.*/
unsigned int blocked_clients_by_type[BLOCKED_NUM]; unsigned int blocked_clients_by_type[BLOCKED_NUM];
@ -1716,9 +1766,9 @@ struct redisServer {
mstime_t mstime; /* 'unixtime' in milliseconds. */ mstime_t mstime; /* 'unixtime' in milliseconds. */
ustime_t ustime; /* 'unixtime' in microseconds. */ ustime_t ustime; /* 'unixtime' in microseconds. */
/* Pubsub */ /* Pubsub */
dict *pubsub_channels; /* Map channels to list of subscribed clients */ ::dict *pubsub_channels; /* Map channels to list of subscribed clients */
list *pubsub_patterns; /* A list of pubsub_patterns */ list *pubsub_patterns; /* A list of pubsub_patterns */
dict *pubsub_patterns_dict; /* A dict of pubsub_patterns */ ::dict *pubsub_patterns_dict; /* A dict of pubsub_patterns */
int notify_keyspace_events; /* Events to propagate via Pub/Sub. This is an int notify_keyspace_events; /* Events to propagate via Pub/Sub. This is an
xor of NOTIFY_... flags. */ xor of NOTIFY_... flags. */
/* Cluster */ /* Cluster */
@ -1741,11 +1791,12 @@ struct redisServer {
REDISMODULE_CLUSTER_FLAG_*. */ REDISMODULE_CLUSTER_FLAG_*. */
int cluster_allow_reads_when_down; /* Are reads allowed when the cluster int cluster_allow_reads_when_down; /* Are reads allowed when the cluster
is down? */ is down? */
int cluster_config_file_lock_fd; /* cluster config fd, will be flock */
/* Scripting */ /* Scripting */
lua_State *lua; /* The Lua interpreter. We use just one for all clients */ lua_State *lua; /* The Lua interpreter. We use just one for all clients */
client *lua_caller = nullptr; /* The client running EVAL right now, or NULL */ client *lua_caller = nullptr; /* The client running EVAL right now, or NULL */
char* lua_cur_script = nullptr; /* SHA1 of the script currently running, or NULL */ char* lua_cur_script = nullptr; /* SHA1 of the script currently running, or NULL */
dict *lua_scripts; /* A dictionary of SHA1 -> Lua scripts */ ::dict *lua_scripts; /* A dictionary of SHA1 -> Lua scripts */
unsigned long long lua_scripts_mem; /* Cached scripts' memory + oh */ unsigned long long lua_scripts_mem; /* Cached scripts' memory + oh */
mstime_t lua_time_limit; /* Script timeout in milliseconds */ mstime_t lua_time_limit; /* Script timeout in milliseconds */
mstime_t lua_time_start; /* Start time of script, milliseconds time */ mstime_t lua_time_start; /* Start time of script, milliseconds time */
@ -1754,7 +1805,7 @@ struct redisServer {
int lua_random_dirty; /* True if a random command was called during the int lua_random_dirty; /* True if a random command was called during the
execution of the current script. */ execution of the current script. */
int lua_replicate_commands; /* True if we are doing single commands repl. */ int lua_replicate_commands; /* True if we are doing single commands repl. */
int lua_multi_emitted;/* True if we already proagated MULTI. */ int lua_multi_emitted;/* True if we already propagated MULTI. */
int lua_repl; /* Script replication flags for redis.set_repl(). */ int lua_repl; /* Script replication flags for redis.set_repl(). */
int lua_timedout; /* True if we reached the time limit for script int lua_timedout; /* True if we reached the time limit for script
execution. */ execution. */
@ -1768,7 +1819,7 @@ struct redisServer {
int lazyfree_lazy_user_del; int lazyfree_lazy_user_del;
/* Latency monitor */ /* Latency monitor */
long long latency_monitor_threshold; long long latency_monitor_threshold;
dict *latency_events; ::dict *latency_events;
/* ACLs */ /* ACLs */
char *acl_filename; /* ACL Users file. NULL if not configured. */ char *acl_filename; /* ACL Users file. NULL if not configured. */
unsigned long acllog_max_len; /* Maximum length of the ACL LOG list. */ unsigned long acllog_max_len; /* Maximum length of the ACL LOG list. */
@ -1802,6 +1853,10 @@ struct redisServer {
char *bio_cpulist; /* cpu affinity list of bio thread. */ char *bio_cpulist; /* cpu affinity list of bio thread. */
char *aof_rewrite_cpulist; /* cpu affinity list of aof rewrite process. */ char *aof_rewrite_cpulist; /* cpu affinity list of aof rewrite process. */
char *bgsave_cpulist; /* cpu affinity list of bgsave process. */ char *bgsave_cpulist; /* cpu affinity list of bgsave process. */
long long repl_batch_offStart = -1;
long long repl_batch_idxStart = -1;
}; };
typedef struct pubsubPattern { typedef struct pubsubPattern {
@ -1809,8 +1864,21 @@ typedef struct pubsubPattern {
robj *pattern; robj *pattern;
} pubsubPattern; } pubsubPattern;
#define MAX_KEYS_BUFFER 256
/* A result structure for the various getkeys function calls. It lists the
* keys as indices to the provided argv.
*/
typedef struct {
int keysbuf[MAX_KEYS_BUFFER]; /* Pre-allocated buffer, to save heap allocations */
int *keys; /* Key indices array, points to keysbuf or heap */
int numkeys; /* Number of key indices return */
int size; /* Available array size */
} getKeysResult;
#define GETKEYS_RESULT_INIT { {0}, NULL, 0, MAX_KEYS_BUFFER }
typedef void redisCommandProc(client *c); typedef void redisCommandProc(client *c);
typedef int *redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); typedef int redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
struct redisCommand { struct redisCommand {
const char *name; const char *name;
redisCommandProc *proc; redisCommandProc *proc;
@ -1922,7 +1990,7 @@ extern dictType modulesDictType;
void moduleInitModulesSystem(void); void moduleInitModulesSystem(void);
int moduleLoad(const char *path, void **argv, int argc); int moduleLoad(const char *path, void **argv, int argc);
void moduleLoadFromQueue(void); void moduleLoadFromQueue(void);
int *moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); int moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
moduleType *moduleTypeLookupModuleByID(uint64_t id); moduleType *moduleTypeLookupModuleByID(uint64_t id);
void moduleTypeNameByID(char *name, uint64_t moduleid); void moduleTypeNameByID(char *name, uint64_t moduleid);
void moduleFreeContext(struct RedisModuleCtx *ctx); void moduleFreeContext(struct RedisModuleCtx *ctx);
@ -1932,6 +2000,7 @@ void moduleBlockedClientTimedOut(client *c);
void moduleBlockedClientPipeReadable(aeEventLoop *el, int fd, void *privdata, int mask); void moduleBlockedClientPipeReadable(aeEventLoop *el, int fd, void *privdata, int mask);
size_t moduleCount(void); size_t moduleCount(void);
void moduleAcquireGIL(int fServerThread); void moduleAcquireGIL(int fServerThread);
int moduleTryAcquireGIL(bool fServerThread);
void moduleReleaseGIL(int fServerThread); void moduleReleaseGIL(int fServerThread);
void moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid); void moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid);
void moduleCallCommandFilters(client *c); void moduleCallCommandFilters(client *c);
@ -1979,30 +2048,29 @@ void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask);
void acceptTLSHandler(aeEventLoop *el, int fd, void *privdata, int mask); void acceptTLSHandler(aeEventLoop *el, int fd, void *privdata, int mask);
void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask); void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask);
void readQueryFromClient(connection *conn); void readQueryFromClient(connection *conn);
void addReplyNull(client *c, robj_roptr objOldProtocol = nullptr); void addReplyNull(client *c);
void addReplyNullArray(client *c); void addReplyNullArray(client *c);
void addReplyNullArrayAsync(client *c);
void addReplyBool(client *c, int b); void addReplyBool(client *c, int b);
void addReplyVerbatim(client *c, const char *s, size_t len, const char *ext); void addReplyVerbatim(client *c, const char *s, size_t len, const char *ext);
void addReplyVerbatimAsync(client *c, const char *s, size_t len, const char *ext);
void addReplyProto(client *c, const char *s, size_t len); void addReplyProto(client *c, const char *s, size_t len);
void addReplyProtoCString(client *c, const char *s);
void addReplyBulk(client *c, robj_roptr obj); void addReplyBulk(client *c, robj_roptr obj);
void AddReplyFromClient(client *c, client *src); void AddReplyFromClient(client *c, client *src);
void addReplyBulkCString(client *c, const char *s); void addReplyBulkCString(client *c, const char *s);
void addReplyBulkCStringAsync(client *c, const char *s);
void addReplyBulkCBuffer(client *c, const void *p, size_t len); void addReplyBulkCBuffer(client *c, const void *p, size_t len);
void addReplyBulkLongLong(client *c, long long ll); void addReplyBulkLongLong(client *c, long long ll);
void addReply(client *c, robj_roptr obj); void addReply(client *c, robj_roptr obj);
void addReplySds(client *c, sds s); void addReplySds(client *c, sds s);
void addReplyBulkSds(client *c, sds s); void addReplyBulkSds(client *c, sds s);
void addReplyErrorObject(client *c, robj *err, int severity);
void addReplyErrorSds(client *c, sds err);
void addReplyError(client *c, const char *err); void addReplyError(client *c, const char *err);
void addReplyStatus(client *c, const char *status); void addReplyStatus(client *c, const char *status);
void addReplyDouble(client *c, double d); void addReplyDouble(client *c, double d);
void addReplyHumanLongDouble(client *c, long double d); void addReplyHumanLongDouble(client *c, long double d);
void addReplyHumanLongDoubleAsync(client *c, long double d);
void addReplyLongLong(client *c, long long ll); void addReplyLongLong(client *c, long long ll);
#ifdef __cplusplus #ifdef __cplusplus
void addReplyLongLongWithPrefixCore(client *c, long long ll, char prefix, bool fAsync); void addReplyLongLongWithPrefixCore(client *c, long long ll, char prefix);
#endif #endif
void addReplyArrayLen(client *c, long length); void addReplyArrayLen(client *c, long length);
void addReplyMapLen(client *c, long length); void addReplyMapLen(client *c, long length);
@ -2047,23 +2115,6 @@ void linkClient(client *c);
void protectClient(client *c); void protectClient(client *c);
void unprotectClient(client *c); void unprotectClient(client *c);
// Special Thread-safe addReply() commands for posting messages to clients from a different thread
void addReplyAsync(client *c, robj_roptr obj);
void addReplyArrayLenAsync(client *c, long length);
void addReplyProtoAsync(client *c, const char *s, size_t len);
void addReplyBulkAsync(client *c, robj_roptr obj);
void addReplyBulkCBufferAsync(client *c, const void *p, size_t len);
void addReplyErrorAsync(client *c, const char *err);
void addReplyMapLenAsync(client *c, long length);
void addReplyNullAsync(client *c);
void addReplyDoubleAsync(client *c, double d);
void *addReplyDeferredLenAsync(client *c);
void setDeferredArrayLenAsync(client *c, void *node, long length);
void addReplySdsAsync(client *c, sds s);
void addReplyBulkSdsAsync(client *c, sds s);
void addReplyPushLenAsync(client *c, long length);
void addReplyLongLongAsync(client *c, long long ll);
void ProcessPendingAsyncWrites(void); void ProcessPendingAsyncWrites(void);
client *lookupClientByID(uint64_t id); client *lookupClientByID(uint64_t id);
@ -2111,9 +2162,10 @@ void initClientMultiState(client *c);
void freeClientMultiState(client *c); void freeClientMultiState(client *c);
void queueMultiCommand(client *c); void queueMultiCommand(client *c);
void touchWatchedKey(redisDb *db, robj *key); void touchWatchedKey(redisDb *db, robj *key);
void touchWatchedKeysOnFlush(int dbid); void touchAllWatchedKeysInDb(redisDb *emptied, redisDb *replaced_with);
void discardTransaction(client *c); void discardTransaction(client *c);
void flagTransaction(client *c); void flagTransaction(client *c);
void execCommandAbort(client *c, sds error);
void execCommandPropagateMulti(client *c); void execCommandPropagateMulti(client *c);
void execCommandPropagateExec(client *c); void execCommandPropagateExec(client *c);
@ -2165,7 +2217,7 @@ const char *strEncoding(int encoding);
int compareStringObjects(robj *a, robj *b); int compareStringObjects(robj *a, robj *b);
int collateStringObjects(robj *a, robj *b); int collateStringObjects(robj *a, robj *b);
int equalStringObjects(robj *a, robj *b); int equalStringObjects(robj *a, robj *b);
unsigned long long estimateObjectIdleTime(robj *o); unsigned long long estimateObjectIdleTime(robj_roptr o);
void trimStringObjectIfNeeded(robj *o); void trimStringObjectIfNeeded(robj *o);
#define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR) #define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR)
@ -2177,7 +2229,7 @@ ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout);
/* Replication */ /* Replication */
void initMasterInfo(struct redisMaster *master); void initMasterInfo(struct redisMaster *master);
void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc); void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
void replicationFeedSlavesFromMasterStream(list *slaves, char *buf, size_t buflen); void replicationFeedSlavesFromMasterStream(char *buf, size_t buflen);
void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc); void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc);
void updateSlavesWaitingBgsave(int bgsaveerr, int type); void updateSlavesWaitingBgsave(int bgsaveerr, int type);
void replicationCron(void); void replicationCron(void);
@ -2209,6 +2261,8 @@ void updateMasterAuth();
void showLatestBacklog(); void showLatestBacklog();
void rdbPipeReadHandler(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask); void rdbPipeReadHandler(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
void rdbPipeWriteHandlerConnRemoved(struct connection *conn); void rdbPipeWriteHandlerConnRemoved(struct connection *conn);
void replicationNotifyLoadedKey(redisDb *db, robj_roptr key, robj_roptr val, long long expire);
void replicateSubkeyExpire(redisDb *db, robj_roptr key, robj_roptr subkey, long long expire);
/* Generic persistence functions */ /* Generic persistence functions */
void startLoadingFile(FILE* fp, const char * filename, int rdbflags); void startLoadingFile(FILE* fp, const char * filename, int rdbflags);
@ -2227,6 +2281,7 @@ int writeCommandsDeniedByDiskError(void);
/* RDB persistence */ /* RDB persistence */
#include "rdb.h" #include "rdb.h"
void killRDBChild(void); void killRDBChild(void);
int bg_unlink(const char *filename);
/* AOF persistence */ /* AOF persistence */
void flushAppendOnlyFile(int force); void flushAppendOnlyFile(int force);
@ -2250,7 +2305,7 @@ void sendChildInfo(int process_type);
void receiveChildInfo(void); void receiveChildInfo(void);
/* Fork helpers */ /* Fork helpers */
int redisFork(); int redisFork(int type);
int hasActiveChildProcess(); int hasActiveChildProcess();
void sendChildCOWInfo(int ptype, const char *pname); void sendChildCOWInfo(int ptype, const char *pname);
@ -2298,7 +2353,7 @@ void addACLLogEntry(client *c, int reason, int keypos, sds username);
/* Flags only used by the ZADD command but not by zsetAdd() API: */ /* Flags only used by the ZADD command but not by zsetAdd() API: */
#define ZADD_CH (1<<16) /* Return num of elements added or updated. */ #define ZADD_CH (1<<16) /* Return num of elements added or updated. */
/* Struct to hold a inclusive/exclusive range spec by score comparison. */ /* Struct to hold an inclusive/exclusive range spec by score comparison. */
typedef struct { typedef struct {
double min, max; double min, max;
int minex, maxex; /* are min or max exclusive? */ int minex, maxex; /* are min or max exclusive? */
@ -2389,6 +2444,7 @@ const char *evictPolicyToString(void);
struct redisMemOverhead *getMemoryOverheadData(void); struct redisMemOverhead *getMemoryOverheadData(void);
void freeMemoryOverheadData(struct redisMemOverhead *mh); void freeMemoryOverheadData(struct redisMemOverhead *mh);
void checkChildrenDone(void); void checkChildrenDone(void);
int setOOMScoreAdj(int process_class);
#define RESTART_SERVER_NONE 0 #define RESTART_SERVER_NONE 0
#define RESTART_SERVER_GRACEFULLY (1<<0) /* Do proper shutdown. */ #define RESTART_SERVER_GRACEFULLY (1<<0) /* Do proper shutdown. */
@ -2452,7 +2508,7 @@ void appendServerSaveParams(time_t seconds, int changes);
void resetServerSaveParams(void); void resetServerSaveParams(void);
struct rewriteConfigState; /* Forward declaration to export API. */ struct rewriteConfigState; /* Forward declaration to export API. */
void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force); void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force);
int rewriteConfig(char *path); int rewriteConfig(char *path, int force_all);
void initConfigValues(); void initConfigValues();
/* db.c -- Keyspace access API */ /* db.c -- Keyspace access API */
@ -2466,18 +2522,20 @@ expireEntry *getExpire(redisDb *db, robj_roptr key);
void setExpire(client *c, redisDb *db, robj *key, robj *subkey, long long when); void setExpire(client *c, redisDb *db, robj *key, robj *subkey, long long when);
void setExpire(client *c, redisDb *db, robj *key, expireEntry &&entry); void setExpire(client *c, redisDb *db, robj *key, expireEntry &&entry);
robj_roptr lookupKeyRead(redisDb *db, robj *key); robj_roptr lookupKeyRead(redisDb *db, robj *key);
int checkAlreadyExpired(long long when);
robj *lookupKeyWrite(redisDb *db, robj *key); robj *lookupKeyWrite(redisDb *db, robj *key);
robj_roptr lookupKeyReadOrReply(client *c, robj *key, robj *reply); robj_roptr lookupKeyReadOrReply(client *c, robj *key, robj *reply);
robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply); robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply);
robj_roptr lookupKeyReadWithFlags(redisDb *db, robj *key, int flags); robj_roptr lookupKeyReadWithFlags(redisDb *db, robj *key, int flags);
robj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags); robj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags);
robj *objectCommandLookup(client *c, robj *key); robj_roptr objectCommandLookup(client *c, robj *key);
robj *objectCommandLookupOrReply(client *c, robj *key, robj *reply); robj_roptr objectCommandLookupOrReply(client *c, robj *key, robj *reply);
int objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle, int objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,
long long lru_clock, int lru_multiplier); long long lru_clock, int lru_multiplier);
#define LOOKUP_NONE 0 #define LOOKUP_NONE 0
#define LOOKUP_NOTOUCH (1<<0) #define LOOKUP_NOTOUCH (1<<0)
#define LOOKUP_UPDATEMVCC (1<<1) #define LOOKUP_NONOTIFY (1<<1)
#define LOOKUP_UPDATEMVCC (1<<2)
void dbAdd(redisDb *db, robj *key, robj *val); void dbAdd(redisDb *db, robj *key, robj *val);
int dbAddRDBLoad(redisDb *db, sds key, robj *val); int dbAddRDBLoad(redisDb *db, sds key, robj *val);
void dbOverwrite(redisDb *db, robj *key, robj *val); void dbOverwrite(redisDb *db, robj *key, robj *val);
@ -2492,11 +2550,14 @@ robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o);
#define EMPTYDB_NO_FLAGS 0 /* No flags. */ #define EMPTYDB_NO_FLAGS 0 /* No flags. */
#define EMPTYDB_ASYNC (1<<0) /* Reclaim memory in another thread. */ #define EMPTYDB_ASYNC (1<<0) /* Reclaim memory in another thread. */
#define EMPTYDB_BACKUP (1<<2) /* DB array is a backup for REPL_DISKLESS_LOAD_SWAPDB. */
long long emptyDb(int dbnum, int flags, void(callback)(void*)); long long emptyDb(int dbnum, int flags, void(callback)(void*));
long long emptyDbGeneric(redisDb *dbarray, int dbnum, int flags, void(callback)(void*)); long long emptyDbStructure(redisDb *dbarray, int dbnum, int async, void(callback)(void*));
void flushAllDataAndResetRDB(int flags); void flushAllDataAndResetRDB(int flags);
long long dbTotalServerKeyCount(); long long dbTotalServerKeyCount();
dbBackup *backupDb(void);
void restoreDbBackup(dbBackup *buckup);
void discardDbBackup(dbBackup *buckup, int flags, void(callback)(void*));
int selectDb(client *c, int id); int selectDb(client *c, int id);
void signalModifiedKey(client *c, redisDb *db, robj *key); void signalModifiedKey(client *c, redisDb *db, robj *key);
@ -2509,24 +2570,27 @@ void scanGenericCommand(client *c, robj_roptr o, unsigned long cursor);
int parseScanCursorOrReply(client *c, robj *o, unsigned long *cursor); int parseScanCursorOrReply(client *c, robj *o, unsigned long *cursor);
void slotToKeyAdd(sds key); void slotToKeyAdd(sds key);
void slotToKeyDel(sds key); void slotToKeyDel(sds key);
void slotToKeyFlush(void);
int dbAsyncDelete(redisDb *db, robj *key); int dbAsyncDelete(redisDb *db, robj *key);
void emptyDbAsync(redisDb *db); void emptyDbAsync(redisDb *db);
void slotToKeyFlushAsync(void); void slotToKeyFlush(int async);
size_t lazyfreeGetPendingObjectsCount(void); size_t lazyfreeGetPendingObjectsCount(void);
void freeObjAsync(robj *o); void freeObjAsync(robj *obj);
void freeSlotsToKeysMapAsync(rax *rt);
void freeSlotsToKeysMap(rax *rt, int async);
/* API to get key arguments from commands */ /* API to get key arguments from commands */
int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); int *getKeysPrepareResult(getKeysResult *result, int numkeys);
void getKeysFreeResult(int *result); int getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys); void getKeysFreeResult(getKeysResult *result);
int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); int zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); int evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
int *migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); int sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
int *georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); int migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
int *xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); int georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
int *memoryGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); int xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
int *lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); int memoryGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
int lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
/* Cluster */ /* Cluster */
void clusterInit(void); void clusterInit(void);
@ -2537,6 +2601,7 @@ void clusterPropagatePublish(robj *channel, robj *message);
void migrateCloseTimedoutSockets(void); void migrateCloseTimedoutSockets(void);
void clusterBeforeSleep(void); void clusterBeforeSleep(void);
int clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, unsigned char *payload, uint32_t len); int clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, unsigned char *payload, uint32_t len);
void createDumpPayload(rio *payload, robj_roptr o, robj *key);
/* Sentinel */ /* Sentinel */
void initSentinelConfig(void); void initSentinelConfig(void);
@ -2673,6 +2738,7 @@ void flushdbCommand(client *c);
void flushallCommand(client *c); void flushallCommand(client *c);
void sortCommand(client *c); void sortCommand(client *c);
void lremCommand(client *c); void lremCommand(client *c);
void lposCommand(client *c);
void rpoplpushCommand(client *c); void rpoplpushCommand(client *c);
void infoCommand(client *c); void infoCommand(client *c);
void mgetCommand(client *c); void mgetCommand(client *c);
@ -2753,6 +2819,7 @@ void watchCommand(client *c);
void unwatchCommand(client *c); void unwatchCommand(client *c);
void clusterCommand(client *c); void clusterCommand(client *c);
void restoreCommand(client *c); void restoreCommand(client *c);
void mvccrestoreCommand(client *c);
void migrateCommand(client *c); void migrateCommand(client *c);
void askingCommand(client *c); void askingCommand(client *c);
void readonlyCommand(client *c); void readonlyCommand(client *c);
@ -2868,6 +2935,30 @@ inline int FCorrectThread(client *c)
} }
#define AssertCorrectThread(c) serverAssert(FCorrectThread(c)) #define AssertCorrectThread(c) serverAssert(FCorrectThread(c))
void flushReplBacklogToClients();
template<typename FN_PTR, class ...TARGS>
void runAndPropogateToReplicas(FN_PTR *pfn, TARGS... args) {
// Store the replication backlog starting params, we use this to know how much data was written.
// these are TLS in case we need to expand the buffer and therefore need to update them
bool fNestedProcess = (g_pserver->repl_batch_idxStart >= 0);
if (!fNestedProcess) {
g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;
g_pserver->repl_batch_idxStart = g_pserver->repl_backlog_idx;
}
pfn(args...);
if (!fNestedProcess) {
flushReplBacklogToClients();
g_pserver->repl_batch_offStart = -1;
g_pserver->repl_batch_idxStart = -1;
}
}
void killThreads(void);
void makeThreadKillable(void);
/* TLS stuff */ /* TLS stuff */
void tlsInit(void); void tlsInit(void);
void tlsInitThread(); void tlsInitThread();

View File

@ -36,6 +36,10 @@
#include <sys/param.h> #include <sys/param.h>
#include <sys/cpuset.h> #include <sys/cpuset.h>
#endif #endif
#ifdef __DragonFly__
#include <pthread.h>
#include <pthread_np.h>
#endif
#ifdef __NetBSD__ #ifdef __NetBSD__
#include <pthread.h> #include <pthread.h>
#include <sched.h> #include <sched.h>
@ -72,7 +76,7 @@ void setcpuaffinity(const char *cpulist) {
#ifdef __linux__ #ifdef __linux__
cpu_set_t cpuset; cpu_set_t cpuset;
#endif #endif
#ifdef __FreeBSD__ #if defined (__FreeBSD__) || defined(__DragonFly__)
cpuset_t cpuset; cpuset_t cpuset;
#endif #endif
#ifdef __NetBSD__ #ifdef __NetBSD__
@ -139,6 +143,9 @@ void setcpuaffinity(const char *cpulist) {
#ifdef __FreeBSD__ #ifdef __FreeBSD__
cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset), &cpuset); cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset), &cpuset);
#endif #endif
#ifdef __DragonFly__
pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
#endif
#ifdef __NetBSD__ #ifdef __NetBSD__
pthread_setaffinity_np(pthread_self(), cpuset_size(cpuset), cpuset); pthread_setaffinity_np(pthread_self(), cpuset_size(cpuset), cpuset);
cpuset_destroy(cpuset); cpuset_destroy(cpuset);

View File

@ -50,6 +50,10 @@
#if !HAVE_SETPROCTITLE #if !HAVE_SETPROCTITLE
#if (defined __linux || defined __APPLE__) #if (defined __linux || defined __APPLE__)
#ifdef __GLIBC__
#define HAVE_CLEARENV
#endif
extern char **environ; extern char **environ;
static struct { static struct {
@ -80,11 +84,9 @@ static inline size_t spt_min(size_t a, size_t b) {
* For discussion on the portability of the various methods, see * For discussion on the portability of the various methods, see
* http://lists.freebsd.org/pipermail/freebsd-stable/2008-June/043136.html * http://lists.freebsd.org/pipermail/freebsd-stable/2008-June/043136.html
*/ */
static int spt_clearenv(void) { int spt_clearenv(void) {
#if __GLIBC__ #ifdef HAVE_CLEARENV
clearenv(); return clearenv();
return 0;
#else #else
extern char **environ; extern char **environ;
static char **tmp; static char **tmp;
@ -100,34 +102,62 @@ static int spt_clearenv(void) {
} /* spt_clearenv() */ } /* spt_clearenv() */
static int spt_copyenv(char *oldenv[]) { static int spt_copyenv(int envc, char *oldenv[]) {
extern char **environ; extern char **environ;
char **envcopy = NULL;
char *eq; char *eq;
int i, error; int i, error;
int envsize;
if (environ != oldenv) if (environ != oldenv)
return 0; return 0;
if ((error = spt_clearenv())) /* Copy environ into envcopy before clearing it. Shallow copy is
goto error; * enough as clearenv() only clears the environ array.
*/
envsize = (envc + 1) * sizeof(char *);
envcopy = malloc(envsize);
if (!envcopy)
return ENOMEM;
memcpy(envcopy, oldenv, envsize);
for (i = 0; oldenv[i]; i++) { /* Note that the state after clearenv() failure is undefined, but we'll
if (!(eq = strchr(oldenv[i], '='))) * just assume an error means it was left unchanged.
*/
if ((error = spt_clearenv())) {
environ = oldenv;
free(envcopy);
return error;
}
/* Set environ from envcopy */
for (i = 0; envcopy[i]; i++) {
if (!(eq = strchr(envcopy[i], '=')))
continue; continue;
*eq = '\0'; *eq = '\0';
error = (0 != setenv(oldenv[i], eq + 1, 1))? errno : 0; error = (0 != setenv(envcopy[i], eq + 1, 1))? errno : 0;
*eq = '='; *eq = '=';
if (error) /* On error, do our best to restore state */
goto error; if (error) {
#ifdef HAVE_CLEARENV
/* We don't assume it is safe to free environ, so we
* may leak it. As clearenv() was shallow using envcopy
* here is safe.
*/
environ = envcopy;
#else
free(envcopy);
free(environ); /* Safe to free, we have just alloc'd it */
environ = oldenv;
#endif
return error;
}
} }
free(envcopy);
return 0; return 0;
error:
environ = oldenv;
return error;
} /* spt_copyenv() */ } /* spt_copyenv() */
@ -148,32 +178,57 @@ static int spt_copyargs(int argc, char *argv[]) {
return 0; return 0;
} /* spt_copyargs() */ } /* spt_copyargs() */
/* Initialize and populate SPT to allow a future setproctitle()
* call.
*
* As setproctitle() basically needs to overwrite argv[0], we're
* trying to determine what is the largest contiguous block
* starting at argv[0] we can use for this purpose.
*
* As this range will overwrite some or all of the argv and environ
* strings, a deep copy of these two arrays is performed.
*/
void spt_init(int argc, char *argv[]) { void spt_init(int argc, char *argv[]) {
char **envp = environ; char **envp = environ;
char *base, *end, *nul, *tmp; char *base, *end, *nul, *tmp;
int i, error; int i, error, envc;
if (!(base = argv[0])) if (!(base = argv[0]))
return; return;
/* We start with end pointing at the end of argv[0] */
nul = &base[strlen(base)]; nul = &base[strlen(base)];
end = nul + 1; end = nul + 1;
/* Attempt to extend end as far as we can, while making sure
* that the range between base and end is only allocated to
* argv, or anything that immediately follows argv (presumably
* envp).
*/
for (i = 0; i < argc || (i >= argc && argv[i]); i++) { for (i = 0; i < argc || (i >= argc && argv[i]); i++) {
if (!argv[i] || argv[i] < end) if (!argv[i] || argv[i] < end)
continue; continue;
end = argv[i] + strlen(argv[i]) + 1; if (end >= argv[i] && end <= argv[i] + strlen(argv[i]))
end = argv[i] + strlen(argv[i]) + 1;
} }
/* In case the envp array was not an immediate extension to argv,
* scan it explicitly.
*/
for (i = 0; envp[i]; i++) { for (i = 0; envp[i]; i++) {
if (envp[i] < end) if (envp[i] < end)
continue; continue;
end = envp[i] + strlen(envp[i]) + 1; if (end >= envp[i] && end <= envp[i] + strlen(envp[i]))
end = envp[i] + strlen(envp[i]) + 1;
} }
envc = i;
/* We're going to deep copy argv[], but argv[0] will still point to
* the old memory for the purpose of updating the title so we need
* to keep the original value elsewhere.
*/
if (!(SPT.arg0 = strdup(argv[0]))) if (!(SPT.arg0 = strdup(argv[0])))
goto syerr; goto syerr;
@ -194,8 +249,8 @@ void spt_init(int argc, char *argv[]) {
setprogname(tmp); setprogname(tmp);
#endif #endif
/* Now make a full deep copy of the environment and argv[] */
if ((error = spt_copyenv(envp))) if ((error = spt_copyenv(envc, envp)))
goto error; goto error;
if ((error = spt_copyargs(argc, argv))) if ((error = spt_copyargs(argc, argv)))
@ -263,3 +318,14 @@ error:
#endif /* __linux || __APPLE__ */ #endif /* __linux || __APPLE__ */
#endif /* !HAVE_SETPROCTITLE */ #endif /* !HAVE_SETPROCTITLE */
#ifdef SETPROCTITLE_TEST_MAIN
int main(int argc, char *argv[]) {
spt_init(argc, argv);
printf("SPT.arg0: [%p] '%s'\n", SPT.arg0, SPT.arg0);
printf("SPT.base: [%p] '%s'\n", SPT.base, SPT.base);
printf("SPT.end: [%p] (%d bytes after base)'\n", SPT.end, (int) (SPT.end - SPT.base));
return 0;
}
#endif

View File

@ -22,7 +22,7 @@
1. We use SipHash 1-2. This is not believed to be as strong as the 1. We use SipHash 1-2. This is not believed to be as strong as the
suggested 2-4 variant, but AFAIK there are not trivial attacks suggested 2-4 variant, but AFAIK there are not trivial attacks
against this reduced-rounds version, and it runs at the same speed against this reduced-rounds version, and it runs at the same speed
as Murmurhash2 that we used previously, why the 2-4 variant slowed as Murmurhash2 that we used previously, while the 2-4 variant slowed
down Redis by a 4% figure more or less. down Redis by a 4% figure more or less.
2. Hard-code rounds in the hope the compiler can optimize it more 2. Hard-code rounds in the hope the compiler can optimize it more
in this raw from. Anyway we always want the standard 2-4 variant. in this raw from. Anyway we always want the standard 2-4 variant.
@ -36,7 +36,7 @@
perform a text transformation in some temporary buffer, which is costly. perform a text transformation in some temporary buffer, which is costly.
5. Remove debugging code. 5. Remove debugging code.
6. Modified the original test.c file to be a stand-alone function testing 6. Modified the original test.c file to be a stand-alone function testing
the function in the new form (returing an uint64_t) using just the the function in the new form (returning an uint64_t) using just the
relevant test vector. relevant test vector.
*/ */
#include <assert.h> #include <assert.h>
@ -46,7 +46,7 @@
#include <ctype.h> #include <ctype.h>
/* Fast tolower() alike function that does not care about locale /* Fast tolower() alike function that does not care about locale
* but just returns a-z insetad of A-Z. */ * but just returns a-z instead of A-Z. */
int siptlw(int c) { int siptlw(int c) {
if (c >= 'A' && c <= 'Z') { if (c >= 'A' && c <= 'Z') {
return c+('a'-'A'); return c+('a'-'A');

View File

@ -75,7 +75,7 @@ slowlogEntry *slowlogCreateEntry(client *c, robj **argv, int argc, long long dur
} else if (argv[j]->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT) { } else if (argv[j]->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT) {
se->argv[j] = argv[j]; se->argv[j] = argv[j];
} else { } else {
/* Here we need to dupliacate the string objects composing the /* Here we need to duplicate the string objects composing the
* argument vector of the command, because those may otherwise * argument vector of the command, because those may otherwise
* end shared with string objects stored into keys. Having * end shared with string objects stored into keys. Having
* shared objects between any part of Redis, and the data * shared objects between any part of Redis, and the data

View File

@ -116,7 +116,7 @@ robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst, int writeflag)
if (fieldobj) { if (fieldobj) {
if (o->type != OBJ_HASH) goto noobj; if (o->type != OBJ_HASH) goto noobj;
/* Retrieve value from hash by the field name. The returend object /* Retrieve value from hash by the field name. The returned object
* is a new object with refcount already incremented. */ * is a new object with refcount already incremented. */
o = hashTypeGetValueObject(o, szFromObj(fieldobj)); o = hashTypeGetValueObject(o, szFromObj(fieldobj));
} else { } else {
@ -271,7 +271,7 @@ void sortCommand(client *c) {
} }
/* Lookup the key to sort. It must be of the right types */ /* Lookup the key to sort. It must be of the right types */
if (storekey) if (!storekey)
sortval = lookupKeyRead(c->db,c->argv[1]).unsafe_robjcast(); sortval = lookupKeyRead(c->db,c->argv[1]).unsafe_robjcast();
else else
sortval = lookupKeyWrite(c->db,c->argv[1]); sortval = lookupKeyWrite(c->db,c->argv[1]);
@ -317,7 +317,7 @@ void sortCommand(client *c) {
switch(sortval->type) { switch(sortval->type) {
case OBJ_LIST: vectorlen = listTypeLength(sortval); break; case OBJ_LIST: vectorlen = listTypeLength(sortval); break;
case OBJ_SET: vectorlen = setTypeSize(sortval); break; case OBJ_SET: vectorlen = setTypeSize(sortval); break;
case OBJ_ZSET: vectorlen = dictSize(((zset*)ptrFromObj(sortval))->pdict); break; case OBJ_ZSET: vectorlen = dictSize(((zset*)ptrFromObj(sortval))->dict); break;
default: vectorlen = 0; serverPanic("Bad SORT type"); /* Avoid GCC warning */ default: vectorlen = 0; serverPanic("Bad SORT type"); /* Avoid GCC warning */
} }
@ -412,7 +412,7 @@ void sortCommand(client *c) {
/* Check if starting point is trivial, before doing log(N) lookup. */ /* Check if starting point is trivial, before doing log(N) lookup. */
if (desc) { if (desc) {
long zsetlen = dictSize(((zset*)ptrFromObj(sortval))->pdict); long zsetlen = dictSize(((zset*)ptrFromObj(sortval))->dict);
ln = zsl->tail; ln = zsl->tail;
if (start > 0) if (start > 0)
@ -436,7 +436,7 @@ void sortCommand(client *c) {
end -= start; end -= start;
start = 0; start = 0;
} else if (sortval->type == OBJ_ZSET) { } else if (sortval->type == OBJ_ZSET) {
dict *set = ((zset*)ptrFromObj(sortval))->pdict; dict *set = ((zset*)ptrFromObj(sortval))->dict;
dictIterator *di; dictIterator *di;
dictEntry *setele; dictEntry *setele;
sds sdsele; sds sdsele;

View File

@ -92,7 +92,7 @@ void freeSparklineSequence(struct sequence *seq) {
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
/* Render part of a sequence, so that render_sequence() call call this function /* Render part of a sequence, so that render_sequence() call call this function
* with differnent parts in order to create the full output without overflowing * with different parts in order to create the full output without overflowing
* the current terminal columns. */ * the current terminal columns. */
sds sparklineRenderRange(sds output, struct sequence *seq, int rows, int offset, int len, int flags) { sds sparklineRenderRange(sds output, struct sequence *seq, int rows, int offset, int len, int flags) {
int j; int j;

View File

@ -74,7 +74,7 @@ typedef struct streamConsumer {
consumer not yet acknowledged. Keys are consumer not yet acknowledged. Keys are
big endian message IDs, while values are big endian message IDs, while values are
the same streamNACK structure referenced the same streamNACK structure referenced
in the "pel" of the conumser group structure in the "pel" of the consumer group structure
itself, so the value is shared. */ itself, so the value is shared. */
} streamConsumer; } streamConsumer;

View File

@ -532,7 +532,7 @@ void hsetCommand(client *c) {
robj *o; robj *o;
if ((c->argc % 2) == 1) { if ((c->argc % 2) == 1) {
addReplyError(c,"wrong number of arguments for HMSET"); addReplyErrorFormat(c,"wrong number of arguments for '%s' command",c->cmd->name);
return; return;
} }
@ -630,7 +630,7 @@ void hincrbyfloatCommand(client *c) {
g_pserver->dirty++; g_pserver->dirty++;
/* Always replicate HINCRBYFLOAT as an HSET command with the final value /* Always replicate HINCRBYFLOAT as an HSET command with the final value
* in order to make sure that differences in float pricision or formatting * in order to make sure that differences in float precision or formatting
* will not create differences in replicas or after an AOF restart. */ * will not create differences in replicas or after an AOF restart. */
robj *aux, *newobj; robj *aux, *newobj;
aux = createStringObject("HSET",4); aux = createStringObject("HSET",4);
@ -772,7 +772,9 @@ void genericHgetallCommand(client *c, int flags) {
hashTypeIterator *hi; hashTypeIterator *hi;
int length, count = 0; int length, count = 0;
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymap[c->resp])) robj *emptyResp = (flags & OBJ_HASH_KEY && flags & OBJ_HASH_VALUE) ?
shared.emptymap[c->resp] : shared.emptyarray;
if ((o = lookupKeyReadOrReply(c,c->argv[1],emptyResp))
== nullptr || checkType(c,o,OBJ_HASH)) return; == nullptr || checkType(c,o,OBJ_HASH)) return;
/* We return a map if the user requested keys and values, like in the /* We return a map if the user requested keys and values, like in the

View File

@ -487,6 +487,124 @@ void ltrimCommand(client *c) {
addReply(c,shared.ok); addReply(c,shared.ok);
} }
/* LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
*
* The "rank" is the position of the match, so if it is 1, the first match
* is returned, if it is 2 the second match is returned and so forth.
* It is 1 by default. If negative has the same meaning but the search is
* performed starting from the end of the list.
*
* If COUNT is given, instead of returning the single element, a list of
* all the matching elements up to "num-matches" are returned. COUNT can
* be combiled with RANK in order to returning only the element starting
* from the Nth. If COUNT is zero, all the matching elements are returned.
*
* MAXLEN tells the command to scan a max of len elements. If zero (the
* default), all the elements in the list are scanned if needed.
*
* The returned elements indexes are always referring to what LINDEX
* would return. So first element from head is 0, and so forth. */
void lposCommand(client *c) {
robj *ele;
robj_roptr o;
ele = c->argv[2];
int direction = LIST_TAIL;
long rank = 1, count = -1, maxlen = 0; /* Count -1: option not given. */
/* Parse the optional arguments. */
for (int j = 3; j < c->argc; j++) {
char *opt = szFromObj(c->argv[j]);
int moreargs = (c->argc-1)-j;
if (!strcasecmp(opt,"RANK") && moreargs) {
j++;
if (getLongFromObjectOrReply(c, c->argv[j], &rank, NULL) != C_OK)
return;
if (rank == 0) {
addReplyError(c,"RANK can't be zero: use 1 to start from "
"the first match, 2 from the second, ...");
return;
}
} else if (!strcasecmp(opt,"COUNT") && moreargs) {
j++;
if (getLongFromObjectOrReply(c, c->argv[j], &count, NULL) != C_OK)
return;
if (count < 0) {
addReplyError(c,"COUNT can't be negative");
return;
}
} else if (!strcasecmp(opt,"MAXLEN") && moreargs) {
j++;
if (getLongFromObjectOrReply(c, c->argv[j], &maxlen, NULL) != C_OK)
return;
if (maxlen < 0) {
addReplyError(c,"MAXLEN can't be negative");
return;
}
} else {
addReply(c,shared.syntaxerr);
return;
}
}
/* A negative rank means start from the tail. */
if (rank < 0) {
rank = -rank;
direction = LIST_HEAD;
}
/* We return NULL or an empty array if there is no such key (or
* if we find no matches, depending on the presence of the COUNT option. */
if ((o = lookupKeyRead(c->db,c->argv[1])) == nullptr) {
if (count != -1)
addReply(c,shared.emptyarray);
else
addReply(c,shared.null[c->resp]);
return;
}
if (checkType(c,o,OBJ_LIST)) return;
/* If we got the COUNT option, prepare to emit an array. */
void *arraylenptr = NULL;
if (count != -1) arraylenptr = addReplyDeferredLen(c);
/* Seek the element. */
listTypeIterator *li;
li = listTypeInitIterator(o,direction == LIST_HEAD ? -1 : 0,direction);
listTypeEntry entry;
long llen = listTypeLength(o);
long index = 0, matches = 0, matchindex = -1, arraylen = 0;
while (listTypeNext(li,&entry) && (maxlen == 0 || index < maxlen)) {
if (listTypeEqual(&entry,ele)) {
matches++;
matchindex = (direction == LIST_TAIL) ? index : llen - index - 1;
if (matches >= rank) {
if (arraylenptr) {
arraylen++;
addReplyLongLong(c,matchindex);
if (count && matches-rank+1 >= count) break;
} else {
break;
}
}
}
index++;
matchindex = -1; /* Remember if we exit the loop without a match. */
}
listTypeReleaseIterator(li);
/* Reply to the client. Note that arraylenptr is not NULL only if
* the COUNT option was selected. */
if (arraylenptr != NULL) {
setDeferredArrayLen(c,arraylenptr,arraylen);
} else {
if (matchindex != -1)
addReplyLongLong(c,matchindex);
else
addReply(c,shared.null[c->resp]);
}
}
void lremCommand(client *c) { void lremCommand(client *c) {
robj *subject, *obj; robj *subject, *obj;
obj = c->argv[3]; obj = c->argv[3];
@ -559,7 +677,7 @@ static void rpoplpushHandlePush(client *c, robj *dstkey, robj *dstobj, robj *val
listTypePush(dstobj,value,LIST_HEAD); listTypePush(dstobj,value,LIST_HEAD);
notifyKeyspaceEvent(NOTIFY_LIST,"lpush",dstkey,c->db->id); notifyKeyspaceEvent(NOTIFY_LIST,"lpush",dstkey,c->db->id);
/* Always send the pushed value to the client. */ /* Always send the pushed value to the client. */
addReplyBulkAsync(c,value); addReplyBulk(c,value);
} }
void rpoplpushCommand(client *c) { void rpoplpushCommand(client *c) {
@ -606,7 +724,7 @@ void rpoplpushCommand(client *c) {
* Blocking POP operations * Blocking POP operations
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
/* This is a helper function for handleClientsBlockedOnKeys(). It's work /* This is a helper function for handleClientsBlockedOnKeys(). Its work
* is to serve a specific client (receiver) that is blocked on 'key' * is to serve a specific client (receiver) that is blocked on 'key'
* in the context of the specified 'db', doing the following: * in the context of the specified 'db', doing the following:
* *
@ -640,9 +758,9 @@ int serveClientBlockedOnList(client *receiver, robj *key, robj *dstkey, redisDb
db->id,argv,2,PROPAGATE_AOF|PROPAGATE_REPL); db->id,argv,2,PROPAGATE_AOF|PROPAGATE_REPL);
/* BRPOP/BLPOP */ /* BRPOP/BLPOP */
addReplyArrayLenAsync(receiver,2); addReplyArrayLen(receiver,2);
addReplyBulkAsync(receiver,key); addReplyBulk(receiver,key);
addReplyBulkAsync(receiver,value); addReplyBulk(receiver,value);
/* Notify event. */ /* Notify event. */
const char *event = (where == LIST_HEAD) ? "lpop" : "rpop"; const char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
@ -697,7 +815,7 @@ void blockingPopGenericCommand(client *c, int where) {
return; return;
} else { } else {
if (listTypeLength(o) != 0) { if (listTypeLength(o) != 0) {
/* Non empty list, this is like a non normal [LR]POP. */ /* Non empty list, this is like a normal [LR]POP. */
const char *event = (where == LIST_HEAD) ? "lpop" : "rpop"; const char *event = (where == LIST_HEAD) ? "lpop" : "rpop";
robj *value = listTypePop(o,where); robj *value = listTypePop(o,where);
serverAssert(value != NULL); serverAssert(value != NULL);
@ -733,7 +851,7 @@ void blockingPopGenericCommand(client *c, int where) {
return; return;
} }
/* If the list is empty or the key does not exists we must block */ /* If the keys do not exist we must block */
blockForKeys(c,BLOCKED_LIST,c->argv + 1,c->argc - 2,timeout,NULL,NULL); blockForKeys(c,BLOCKED_LIST,c->argv + 1,c->argc - 2,timeout,NULL,NULL);
} }

353
src/t_nhash.cpp Normal file
View File

@ -0,0 +1,353 @@
/*
* Copyright (c) 2020, EQ Alpha Technology Ltd. <john at eqalpha dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "server.h"
#include <math.h>
void dictObjectDestructor(void *privdata, void *val);
dictType nestedHashDictType {
dictSdsHash, /* hash function */
NULL, /* key dup */
NULL, /* val dup */
dictSdsKeyCompare, /* key compare */
dictSdsDestructor, /* key destructor */
dictObjectDestructor, /* val destructor */
};
robj *createNestHashBucket() {
dict *d = dictCreate(&nestedHashDictType, nullptr);
return createObject(OBJ_NESTEDHASH, d);
}
void freeNestedHashObject(robj_roptr o) {
dictRelease((dict*)ptrFromObj(o));
}
robj *fetchFromKey(redisDb *db, robj_roptr key) {
const char *pchCur = szFromObj(key);
const char *pchStart = pchCur;
const char *pchMax = pchCur + sdslen(pchCur);
robj *o = nullptr;
while (pchCur <= pchMax) {
if (pchCur == pchMax || *pchCur == '.') {
// WARNING: Don't deref pchCur as it may be pchMax
// New word
if ((pchCur - pchStart) < 1) {
throw shared.syntaxerr; // malformed
}
dict *d = nullptr;
if (o == nullptr)
d = db->dict;
else
d = (dict*)ptrFromObj(o);
sdsstring str(pchStart, pchCur - pchStart);
dictEntry *de = dictFind(d, str.get());
o = (de != nullptr) ? (robj*)dictGetVal(de) : nullptr;
if (o == nullptr) throw shared.nokeyerr; // Not Found
serverAssert(o->type == OBJ_NESTEDHASH || o->type == OBJ_STRING || o->type == OBJ_LIST);
if (o->type == OBJ_STRING && pchCur != pchMax)
throw shared.nokeyerr; // Past the end
pchStart = pchCur + 1;
}
++pchCur;
}
return o;
}
// Returns one if we overwrote a value
bool setWithKey(redisDb *db, robj_roptr key, robj *val, bool fCreateBuckets) {
const char *pchCur = szFromObj(key);
const char *pchStart = pchCur;
const char *pchMax = pchCur + sdslen(pchCur);
robj *o = nullptr;
while (pchCur <= pchMax) {
if (pchCur == pchMax || *pchCur == '.') {
// WARNING: Don't deref pchCur as it may be pchMax
// New word
if ((pchCur - pchStart) < 1) {
throw shared.syntaxerr; // malformed
}
dict *d = nullptr;
if (o == nullptr)
d = db->dict;
else
d = (dict*)ptrFromObj(o);
sdsstring str(pchStart, pchCur - pchStart);
dictEntry *de = dictFind(d, str.get());
if (pchCur == pchMax) {
val->addref();
if (de != nullptr) {
decrRefCount((robj*)dictGetVal(de));
dictSetVal(d, de, val);
return true;
} else {
dictAdd(d, str.release(), val);
return false;
}
} else {
o = (de != nullptr) ? (robj*)dictGetVal(de) : nullptr;
if (o == nullptr) {
if (!fCreateBuckets)
throw shared.nokeyerr; // Not Found
o = createNestHashBucket();
serverAssert(dictAdd(d, str.release(), o) == DICT_OK);
} else if (o->type != OBJ_NESTEDHASH) {
decrRefCount(o);
o = createNestHashBucket();
de->v.val = o;
}
}
pchStart = pchCur + 1;
}
++pchCur;
}
throw "Internal Error";
}
void writeNestedHashToClient(client *c, robj_roptr o) {
if (o == nullptr) {
addReply(c, shared.null[c->resp]);
} else if (o->type == OBJ_STRING) {
addReplyBulk(c, o);
} else if (o->type == OBJ_LIST) {
unsigned char *zl = (unsigned char*)ptrFromObj(o);
addReplyArrayLen(c, ziplistLen(zl));
unsigned char *p = ziplistIndex(zl, ZIPLIST_HEAD);
while (p != nullptr) {
unsigned char *str;
unsigned int len;
long long lval;
if (ziplistGet(p, &str, &len, &lval)) {
char rgT[128];
if (str == nullptr) {
len = ll2string(rgT, 128, lval);
str = (unsigned char*)rgT;
}
addReplyBulkCBuffer(c, (const char*)str, len);
}
p = ziplistNext(zl, p);
}
} else {
serverAssert(o->type == OBJ_NESTEDHASH );
dict *d = (dict*)ptrFromObj(o);
if (dictSize(d) > 1)
addReplyArrayLen(c, dictSize(d));
dictIterator *di = dictGetIterator(d);
dictEntry *de;
while ((de = dictNext(di))) {
robj_roptr oT = (robj*)dictGetVal(de);
addReplyArrayLen(c, 2);
addReplyBulkCBuffer(c, (sds)dictGetKey(de), sdslen((sds)dictGetKey(de)));
if (oT->type == OBJ_STRING) {
addReplyBulk(c, oT);
} else {
writeNestedHashToClient(c, oT);
}
}
dictReleaseIterator(di);
}
}
inline bool FSimpleJsonEscapeCh(char ch) {
return (ch == '"' || ch == '\\');
}
inline bool FExtendedJsonEscapeCh(char ch) {
return ch <= 0x1F;
}
sds writeJsonValue(sds output, const char *valIn, size_t cchIn) {
const char *val = valIn;
size_t cch = cchIn;
int cchEscapeExtra = 0;
// First scan for escaped chars
for (size_t ich = 0; ich < cchIn; ++ich) {
if (FSimpleJsonEscapeCh(valIn[ich])) {
++cchEscapeExtra;
} else if (FExtendedJsonEscapeCh(valIn[ich])) {
cchEscapeExtra += 5;
}
}
if (cchEscapeExtra > 0) {
size_t ichDst = 0;
sds dst = sdsnewlen(SDS_NOINIT, cchIn+cchEscapeExtra);
for (size_t ich = 0; ich < cchIn; ++ich) {
switch (valIn[ich]) {
case '"':
dst[ichDst++] = '\\'; dst[ichDst++] = '"';
break;
case '\\':
dst[ichDst++] = '\\'; dst[ichDst++] = '\\';
break;
default:
serverAssert(!FSimpleJsonEscapeCh(valIn[ich]));
if (FExtendedJsonEscapeCh(valIn[ich])) {
dst[ichDst++] = '\\'; dst[ichDst++] = 'u';
sprintf(dst + ichDst, "%4x", valIn[ich]);
ichDst += 4;
} else {
dst[ichDst++] = valIn[ich];
}
break;
}
}
val = (const char*)dst;
serverAssert(ichDst == (cchIn+cchEscapeExtra));
cch = ichDst;
}
output = sdscat(output, "\"");
output = sdscatlen(output, val, cch);
output = sdscat(output, "\"");
if (val != valIn)
sdsfree(val);
return output;
}
sds writeJsonValue(sds output, sds val) {
return writeJsonValue(output, (const char*)val, sdslen(val));
}
sds writeNestedHashAsJson(sds output, robj_roptr o) {
if (o->type == OBJ_STRING) {
output = writeJsonValue(output, (sds)szFromObj(o));
} else if (o->type == OBJ_LIST) {
unsigned char *zl = (unsigned char*)ptrFromObj(o);
output = sdscat(output, "[");
unsigned char *p = ziplistIndex(zl, ZIPLIST_HEAD);
bool fFirst = true;
while (p != nullptr) {
unsigned char *str;
unsigned int len;
long long lval;
if (ziplistGet(p, &str, &len, &lval)) {
char rgT[128];
if (str == nullptr) {
len = ll2string(rgT, 128, lval);
str = (unsigned char*)rgT;
}
if (!fFirst)
output = sdscat(output, ",");
fFirst = false;
output = writeJsonValue(output, (const char*)str, len);
}
p = ziplistNext(zl, p);
}
output = sdscat(output, "]");
} else {
output = sdscat(output, "{");
dictIterator *di = dictGetIterator((dict*)ptrFromObj(o));
dictEntry *de;
bool fFirst = true;
while ((de = dictNext(di))) {
robj_roptr oT = (robj*)dictGetVal(de);
if (!fFirst)
output = sdscat(output, ",");
fFirst = false;
output = writeJsonValue(output, (sds)dictGetKey(de));
output = sdscat(output, " : ");
output = writeNestedHashAsJson(output, oT);
}
dictReleaseIterator(di);
output = sdscat(output, "}");
}
return output;
}
void nhsetCommand(client *c) {
if (c->argc < 3)
throw shared.syntaxerr;
robj *val = c->argv[2];
if (c->argc > 3) {
// Its a list, we'll store as a ziplist
val = createZiplistObject();
for (int iarg = 2; iarg < c->argc; ++iarg) {
sds arg = (sds)szFromObj(c->argv[iarg]);
val->m_ptr = ziplistPush((unsigned char*)ptrFromObj(val), (unsigned char*)arg, sdslen(arg), ZIPLIST_TAIL);
}
}
try {
if (setWithKey(c->db, c->argv[1], val, true)) {
addReplyLongLong(c, 1); // we replaced a value
} else {
addReplyLongLong(c, 0); // we added a new value
}
} catch (...) {
if (val != c->argv[2])
decrRefCount(val);
throw;
}
if (val != c->argv[2])
decrRefCount(val);
}
void nhgetCommand(client *c) {
if (c->argc != 2 && c->argc != 3)
throw shared.syntaxerr;
bool fJson = false;
int argOffset = 0;
if (c->argc == 3) {
argOffset++;
if (strcasecmp(szFromObj(c->argv[1]), "json") == 0) {
fJson = true;
} else if (strcasecmp(szFromObj(c->argv[1]), "resp") != 0) {
throw shared.syntaxerr;
}
}
robj *o = fetchFromKey(c->db, c->argv[argOffset + 1]);
if (fJson) {
sds val = writeNestedHashAsJson(sdsnew(nullptr), o);
addReplyBulkSds(c, val);
} else {
writeNestedHashToClient(c, o);
}
}

6
src/t_nhash.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
void freeNestedHashObject(robj_roptr o);
void nhsetCommand(client *c);
void nhgetCommand(client *c);

View File

@ -193,7 +193,7 @@ sds setTypeNextObject(setTypeIterator *si) {
} }
/* Return random element from a non empty set. /* Return random element from a non empty set.
* The returned element can be a int64_t value if the set is encoded * The returned element can be an int64_t value if the set is encoded
* as an "intset" blob of integers, or an SDS string if the set * as an "intset" blob of integers, or an SDS string if the set
* is a regular set. * is a regular set.
* *
@ -447,7 +447,7 @@ void spopWithCountCommand(client *c) {
dbDelete(c->db,c->argv[1]); dbDelete(c->db,c->argv[1]);
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id); notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);
/* Propagate this command as an DEL operation */ /* Propagate this command as a DEL operation */
rewriteClientCommandVector(c,2,shared.del,c->argv[1]); rewriteClientCommandVector(c,2,shared.del,c->argv[1]);
signalModifiedKey(c,c->db,c->argv[1]); signalModifiedKey(c,c->db,c->argv[1]);
g_pserver->dirty++; g_pserver->dirty++;
@ -681,7 +681,7 @@ void srandmemberWithCountCommand(client *c) {
* In this case we create a set from scratch with all the elements, and * In this case we create a set from scratch with all the elements, and
* subtract random elements to reach the requested number of elements. * subtract random elements to reach the requested number of elements.
* *
* This is done because if the number of requsted elements is just * This is done because if the number of requested elements is just
* a bit less than the number of elements in the set, the natural approach * a bit less than the number of elements in the set, the natural approach
* used into CASE 3 is highly inefficient. */ * used into CASE 3 is highly inefficient. */
if (count*SRANDMEMBER_SUB_STRATEGY_MUL > size) { if (count*SRANDMEMBER_SUB_STRATEGY_MUL > size) {

View File

@ -43,6 +43,7 @@
void streamFreeCG(streamCG *cg); void streamFreeCG(streamCG *cg);
void streamFreeNACK(streamNACK *na); void streamFreeNACK(streamNACK *na);
size_t streamReplyWithRangeFromConsumerPEL(client *c, stream *s, streamID *start, streamID *end, size_t count, streamConsumer *consumer); size_t streamReplyWithRangeFromConsumerPEL(client *c, stream *s, streamID *start, streamID *end, size_t count, streamConsumer *consumer);
bool FInReplicaReplay();
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
* Low level stream encoding: a radix tree of listpacks. * Low level stream encoding: a radix tree of listpacks.
@ -817,7 +818,7 @@ static void addReplyStreamID(client *c, streamID *id) {
static void addReplyStreamIDAsync(client *c, streamID *id) { static void addReplyStreamIDAsync(client *c, streamID *id) {
sds replyid = sdscatfmt(sdsempty(),"%U-%U",id->ms,id->seq); sds replyid = sdscatfmt(sdsempty(),"%U-%U",id->ms,id->seq);
addReplyBulkSdsAsync(c,replyid); addReplyBulkSds(c,replyid);
} }
/* Similar to the above function, but just creates an object, usually useful /* Similar to the above function, but just creates an object, usually useful
@ -838,6 +839,9 @@ void streamPropagateXCLAIM(client *c, robj *key, streamCG *group, robj *groupnam
* *
* Note that JUSTID is useful in order to avoid that XCLAIM will do * Note that JUSTID is useful in order to avoid that XCLAIM will do
* useless work in the replica side, trying to fetch the stream item. */ * useless work in the replica side, trying to fetch the stream item. */
if (FInReplicaReplay())
return;
robj *argv[14]; robj *argv[14];
argv[0] = createStringObject("XCLAIM",6); argv[0] = createStringObject("XCLAIM",6);
argv[1] = key; argv[1] = key;
@ -964,7 +968,7 @@ size_t streamReplyWithRange(client *c, stream *s, streamID *start, streamID *end
} }
if (!(flags & STREAM_RWR_RAWENTRIES)) if (!(flags & STREAM_RWR_RAWENTRIES))
arraylen_ptr = addReplyDeferredLenAsync(c); arraylen_ptr = addReplyDeferredLen(c);
streamIteratorStart(&si,s,start,end,rev); streamIteratorStart(&si,s,start,end,rev);
while(streamIteratorGetID(&si,&id,&numfields)) { while(streamIteratorGetID(&si,&id,&numfields)) {
/* Update the group last_id if needed. */ /* Update the group last_id if needed. */
@ -978,18 +982,18 @@ size_t streamReplyWithRange(client *c, stream *s, streamID *start, streamID *end
/* Emit a two elements array for each item. The first is /* Emit a two elements array for each item. The first is
* the ID, the second is an array of field-value pairs. */ * the ID, the second is an array of field-value pairs. */
addReplyArrayLenAsync(c,2); addReplyArrayLen(c,2);
addReplyStreamIDAsync(c,&id); addReplyStreamIDAsync(c,&id);
addReplyArrayLenAsync(c,numfields*2); addReplyArrayLen(c,numfields*2);
/* Emit the field-value pairs. */ /* Emit the field-value pairs. */
while(numfields--) { while(numfields--) {
unsigned char *key, *value; unsigned char *key, *value;
int64_t key_len, value_len; int64_t key_len, value_len;
streamIteratorGetField(&si,&key,&value,&key_len,&value_len); streamIteratorGetField(&si,&key,&value,&key_len,&value_len);
addReplyBulkCBufferAsync(c,key,key_len); addReplyBulkCBuffer(c,key,key_len);
addReplyBulkCBufferAsync(c,value,value_len); addReplyBulkCBuffer(c,value,value_len);
} }
/* If a group is passed, we need to create an entry in the /* If a group is passed, we need to create an entry in the
@ -1048,7 +1052,7 @@ size_t streamReplyWithRange(client *c, stream *s, streamID *start, streamID *end
streamPropagateGroupID(c,spi->keyname,group,spi->groupname); streamPropagateGroupID(c,spi->keyname,group,spi->groupname);
streamIteratorStop(&si); streamIteratorStop(&si);
if (arraylen_ptr) setDeferredArrayLenAsync(c,arraylen_ptr,arraylen); if (arraylen_ptr) setDeferredArrayLen(c,arraylen_ptr,arraylen);
return arraylen; return arraylen;
} }
@ -1203,7 +1207,7 @@ void xaddCommand(client *c) {
int id_given = 0; /* Was an ID different than "*" specified? */ int id_given = 0; /* Was an ID different than "*" specified? */
long long maxlen = -1; /* If left to -1 no trimming is performed. */ long long maxlen = -1; /* If left to -1 no trimming is performed. */
int approx_maxlen = 0; /* If 1 only delete whole radix tree nodes, so int approx_maxlen = 0; /* If 1 only delete whole radix tree nodes, so
the maxium length is not applied verbatim. */ the maximum length is not applied verbatim. */
int maxlen_arg_idx = 0; /* Index of the count in MAXLEN, for rewriting. */ int maxlen_arg_idx = 0; /* Index of the count in MAXLEN, for rewriting. */
/* Parse options. */ /* Parse options. */
@ -1847,6 +1851,7 @@ NULL
o = createStreamObject(); o = createStreamObject();
dbAdd(c->db,c->argv[2],o); dbAdd(c->db,c->argv[2],o);
s = (stream*)ptrFromObj(o); s = (stream*)ptrFromObj(o);
signalModifiedKey(c,c->db,c->argv[2]);
} }
streamCG *cg = streamCreateCG(s,grpname,sdslen(grpname),&id); streamCG *cg = streamCreateCG(s,grpname,sdslen(grpname),&id);
@ -1891,14 +1896,14 @@ NULL
g_pserver->dirty++; g_pserver->dirty++;
notifyKeyspaceEvent(NOTIFY_STREAM,"xgroup-delconsumer", notifyKeyspaceEvent(NOTIFY_STREAM,"xgroup-delconsumer",
c->argv[2],c->db->id); c->argv[2],c->db->id);
} else if (!strcasecmp(opt,"HELP")) { } else if (c->argc == 2 && !strcasecmp(opt,"HELP")) {
addReplyHelp(c, help); addReplyHelp(c, help);
} else { } else {
addReplySubcommandSyntaxError(c); addReplySubcommandSyntaxError(c);
} }
} }
/* XSETID <stream> <groupname> <id> /* XSETID <stream> <id>
* *
* Set the internal "last ID" of a stream. */ * Set the internal "last ID" of a stream. */
void xsetidCommand(client *c) { void xsetidCommand(client *c) {
@ -1987,7 +1992,7 @@ void xackCommand(client *c) {
* *
* If start and stop are omitted, the command just outputs information about * If start and stop are omitted, the command just outputs information about
* the amount of pending messages for the key/group pair, together with * the amount of pending messages for the key/group pair, together with
* the minimum and maxium ID of pending messages. * the minimum and maximum ID of pending messages.
* *
* If start and stop are provided instead, the pending messages are returned * If start and stop are provided instead, the pending messages are returned
* with informations about the current owner, number of deliveries and last * with informations about the current owner, number of deliveries and last

View File

@ -35,8 +35,8 @@
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
static int checkStringLength(client *c, long long size) { static int checkStringLength(client *c, long long size) {
if (size > 512*1024*1024) { if (!(c->flags & CLIENT_MASTER) && size > g_pserver->proto_max_bulk_len) {
addReplyError(c,"string exceeds maximum allowed size (512MB)"); addReplyError(c,"string exceeds maximum allowed size (proto-max-bulk-len)");
return C_ERR; return C_ERR;
} }
return C_OK; return C_OK;
@ -317,7 +317,7 @@ void msetGenericCommand(client *c, int nx) {
} }
/* Handle the NX flag. The MSETNX semantic is to return zero and don't /* Handle the NX flag. The MSETNX semantic is to return zero and don't
* set anything if at least one key alerady exists. */ * set anything if at least one key already exists. */
if (nx) { if (nx) {
for (j = 1; j < c->argc; j += 2) { for (j = 1; j < c->argc; j += 2) {
if (lookupKeyWrite(c->db,c->argv[j]) != NULL) { if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
@ -505,6 +505,12 @@ void stralgoLCS(client *c) {
const char *b = NULL; const char *b = NULL;
int getlen = 0, getidx = 0, withmatchlen = 0; int getlen = 0, getidx = 0, withmatchlen = 0;
robj_roptr obja, objb; robj_roptr obja, objb;
uint32_t arraylen = 0; /* Number of ranges emitted in the array. */
uint32_t alen, blen, *lcs, idx;
int computelcs;
sds result = NULL; /* Resulting LCS string. */
void *arraylenptr = NULL; /* Deffered length of the array for IDX. */
uint32_t arange_start, arange_end, brange_start = 0, brange_end = 0;
for (j = 2; j < (uint32_t)c->argc; j++) { for (j = 2; j < (uint32_t)c->argc; j++) {
char *opt = szFromObj(c->argv[j]); char *opt = szFromObj(c->argv[j]);
@ -518,13 +524,13 @@ void stralgoLCS(client *c) {
withmatchlen = 1; withmatchlen = 1;
} else if (!strcasecmp(opt,"MINMATCHLEN") && moreargs) { } else if (!strcasecmp(opt,"MINMATCHLEN") && moreargs) {
if (getLongLongFromObjectOrReply(c,c->argv[j+1],&minmatchlen,NULL) if (getLongLongFromObjectOrReply(c,c->argv[j+1],&minmatchlen,NULL)
!= C_OK) return; != C_OK) goto cleanup;
if (minmatchlen < 0) minmatchlen = 0; if (minmatchlen < 0) minmatchlen = 0;
j++; j++;
} else if (!strcasecmp(opt,"STRINGS") && moreargs > 1) { } else if (!strcasecmp(opt,"STRINGS") && moreargs > 1) {
if (a != NULL) { if (a != NULL) {
addReplyError(c,"Either use STRINGS or KEYS"); addReplyError(c,"Either use STRINGS or KEYS");
return; goto cleanup;
} }
a = szFromObj(c->argv[j+1]); a = szFromObj(c->argv[j+1]);
b = szFromObj(c->argv[j+2]); b = szFromObj(c->argv[j+2]);
@ -532,10 +538,21 @@ void stralgoLCS(client *c) {
} else if (!strcasecmp(opt,"KEYS") && moreargs > 1) { } else if (!strcasecmp(opt,"KEYS") && moreargs > 1) {
if (a != NULL) { if (a != NULL) {
addReplyError(c,"Either use STRINGS or KEYS"); addReplyError(c,"Either use STRINGS or KEYS");
return; goto cleanup;
} }
obja = lookupKeyRead(c->db,c->argv[j+1]); obja = lookupKeyRead(c->db,c->argv[j+1]);
objb = lookupKeyRead(c->db,c->argv[j+2]); objb = lookupKeyRead(c->db,c->argv[j+2]);
if ((obja && obja->type != OBJ_STRING) ||
(objb && objb->type != OBJ_STRING))
{
addReplyError(c,
"The specified keys must contain string values");
/* Don't cleanup the objects, we need to do that
* only after callign getDecodedObject(). */
obja = NULL;
objb = NULL;
goto cleanup;
}
obja = obja ? getDecodedObject(obja) : createStringObject("",0); obja = obja ? getDecodedObject(obja) : createStringObject("",0);
objb = objb ? getDecodedObject(objb) : createStringObject("",0); objb = objb ? getDecodedObject(objb) : createStringObject("",0);
a = szFromObj(obja); a = szFromObj(obja);
@ -543,7 +560,7 @@ void stralgoLCS(client *c) {
j += 2; j += 2;
} else { } else {
addReply(c,shared.syntaxerr); addReply(c,shared.syntaxerr);
return; goto cleanup;
} }
} }
@ -551,23 +568,23 @@ void stralgoLCS(client *c) {
if (a == NULL) { if (a == NULL) {
addReplyError(c,"Please specify two strings: " addReplyError(c,"Please specify two strings: "
"STRINGS or KEYS options are mandatory"); "STRINGS or KEYS options are mandatory");
return; goto cleanup;
} else if (getlen && getidx) { } else if (getlen && getidx) {
addReplyError(c, addReplyError(c,
"If you want both the length and indexes, please " "If you want both the length and indexes, please "
"just use IDX."); "just use IDX.");
return; goto cleanup;
} }
/* Compute the LCS using the vanilla dynamic programming technique of /* Compute the LCS using the vanilla dynamic programming technique of
* building a table of LCS(x,y) substrings. */ * building a table of LCS(x,y) substrings. */
uint32_t alen = sdslen(a); alen = sdslen(a);
uint32_t blen = sdslen(b); blen = sdslen(b);
/* Setup an uint32_t array to store at LCS[i,j] the length of the /* Setup an uint32_t array to store at LCS[i,j] the length of the
* LCS A0..i-1, B0..j-1. Note that we have a linear array here, so * LCS A0..i-1, B0..j-1. Note that we have a linear array here, so
* we index it as LCS[j+(blen+1)*j] */ * we index it as LCS[j+(blen+1)*j] */
uint32_t *lcs = (uint32_t*)zmalloc((alen+1)*(blen+1)*sizeof(uint32_t)); lcs = (uint32_t*)zmalloc((alen+1)*(blen+1)*sizeof(uint32_t));
#define LCS(A,B) lcs[(B)+((A)*(blen+1))] #define LCS(A,B) lcs[(B)+((A)*(blen+1))]
/* Start building the LCS table. */ /* Start building the LCS table. */
@ -596,20 +613,18 @@ void stralgoLCS(client *c) {
/* Store the actual LCS string in "result" if needed. We create /* Store the actual LCS string in "result" if needed. We create
* it backward, but the length is already known, we store it into idx. */ * it backward, but the length is already known, we store it into idx. */
uint32_t idx = LCS(alen,blen); idx = LCS(alen,blen);
sds result = NULL; /* Resulting LCS string. */ arange_start = alen; /* alen signals that values are not set. */
void *arraylenptr = NULL; /* Deffered length of the array for IDX. */ arange_end = 0;
uint32_t arange_start = alen, /* alen signals that values are not set. */ brange_start = 0;
arange_end = 0, brange_end = 0;
brange_start = 0,
brange_end = 0;
/* Do we need to compute the actual LCS string? Allocate it in that case. */ /* Do we need to compute the actual LCS string? Allocate it in that case. */
int computelcs = getidx || !getlen; computelcs = getidx || !getlen;
if (computelcs) result = sdsnewlen(SDS_NOINIT,idx); if (computelcs) result = sdsnewlen(SDS_NOINIT,idx);
/* Start with a deferred array if we have to emit the ranges. */ /* Start with a deferred array if we have to emit the ranges. */
uint32_t arraylen = 0; /* Number of ranges emitted in the array. */ arraylen = 0; /* Number of ranges emitted in the array. */
if (getidx) { if (getidx) {
addReplyMapLen(c,2); addReplyMapLen(c,2);
addReplyBulkCString(c,"matches"); addReplyBulkCString(c,"matches");
@ -691,10 +706,12 @@ void stralgoLCS(client *c) {
} }
/* Cleanup. */ /* Cleanup. */
if (obja) decrRefCount(obja);
if (objb) decrRefCount(objb);
sdsfree(result); sdsfree(result);
zfree(lcs); zfree(lcs);
cleanup:
if (obja) decrRefCount(obja);
if (objb) decrRefCount(objb);
return; return;
} }

Some files were not shown because too many files have changed in this diff Show More