2014-04-24 05:08:22 -04:00
|
|
|
# Copyright (C) 2014 Salvatore Sanfilippo antirez@gmail.com
|
2024-03-21 09:30:47 -04:00
|
|
|
# SPDX-FileCopyrightText: 2024 Redict Contributors
|
|
|
|
# SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
2024-03-21 15:11:44 -04:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only
|
2014-02-17 11:37:56 -05:00
|
|
|
|
|
|
|
package require Tcl 8.5
|
|
|
|
|
|
|
|
set tcl_precision 17
|
2024-03-21 05:56:59 -04:00
|
|
|
source ../support/redict.tcl
|
2014-04-24 05:08:22 -04:00
|
|
|
source ../support/util.tcl
|
Implement Multi Part AOF mechanism to avoid AOFRW overheads. (#9788)
Implement Multi-Part AOF mechanism to avoid overheads during AOFRW.
Introducing a folder with multiple AOF files tracked by a manifest file.
The main issues with the the original AOFRW mechanism are:
* buffering of commands that are processed during rewrite (consuming a lot of RAM)
* freezes of the main process when the AOFRW completes to drain the remaining part of the buffer and fsync it.
* double disk IO for the data that arrives during AOFRW (had to be written to both the old and new AOF files)
The main modifications of this PR:
1. Remove the AOF rewrite buffer and related code.
2. Divide the AOF into multiple files, they are classified as two types, one is the the `BASE` type,
it represents the full amount of data (Maybe AOF or RDB format) after each AOFRW, there is only
one `BASE` file at most. The second is `INCR` type, may have more than one. They represent the
incremental commands since the last AOFRW.
3. Use a AOF manifest file to record and manage these AOF files mentioned above.
4. The original configuration of `appendfilename` will be the base part of the new file name, for example:
`appendonly.aof.1.base.rdb` and `appendonly.aof.2.incr.aof`
5. Add manifest-related TCL tests, and modified some existing tests that depend on the `appendfilename`
6. Remove the `aof_rewrite_buffer_length` field in info.
7. Add `aof-disable-auto-gc` configuration. By default we're automatically deleting HISTORY type AOFs.
It also gives users the opportunity to preserve the history AOFs. just for testing use now.
8. Add AOFRW limiting measure. When the AOFRW failures reaches the threshold (3 times now),
we will delay the execution of the next AOFRW by 1 minute. If the next AOFRW also fails, it will be
delayed by 2 minutes. The next is 4, 8, 16, the maximum delay is 60 minutes (1 hour). During the limit
period, we can still use the 'bgrewriteaof' command to execute AOFRW immediately.
9. Support upgrade (load) data from old version redis.
10. Add `appenddirname` configuration, as the directory name of the append only files. All AOF files and
manifest file will be placed in this directory.
11. Only the last AOF file (BASE or INCR) can be truncated. Otherwise redis will exit even if
`aof-load-truncated` is enabled.
Co-authored-by: Oran Agra <oran@redislabs.com>
2022-01-03 12:14:13 -05:00
|
|
|
source ../support/aofmanifest.tcl
|
2014-04-24 05:08:22 -04:00
|
|
|
source ../support/server.tcl
|
|
|
|
source ../support/test.tcl
|
2014-02-17 11:37:56 -05:00
|
|
|
|
|
|
|
set ::verbose 0
|
2015-01-09 17:43:48 -05:00
|
|
|
set ::valgrind 0
|
2019-09-12 03:56:54 -04:00
|
|
|
set ::tls 0
|
Build TLS as a loadable module
* Support BUILD_TLS=module to be loaded as a module via config file or
command line. e.g. redis-server --loadmodule redis-tls.so
* Updates to redismodule.h to allow it to be used side by side with
server.h by defining REDISMODULE_CORE_MODULE
* Changes to server.h, redismodule.h and module.c to avoid repeated
type declarations (gcc 4.8 doesn't like these)
* Add a mechanism for non-ABI neutral modules (ones who include
server.h) to refuse loading if they detect not being built together with
redis (release.c)
* Fix wrong signature of RedisModuleDefragFunc, this could break
compilation of a module, but not the ABI
* Move initialization of listeners in server.c to be after loading
the modules
* Config TLS after initialization of listeners
* Init cluster after initialization of listeners
* Add TLS module to CI
* Fix a test suite race conditions:
Now that the listeners are initialized later, it's not sufficient to
wait for the PID message in the log, we need to wait for the "Server
Initialized" message.
* Fix issues with moduleconfigs test as a result from start_server
waiting for "Server Initialized"
* Fix issues with modules/infra test as a result of an additional module
present
Notes about Sentinel:
Sentinel can't really rely on the tls module, since it uses hiredis to
initiate connections and depends on OpenSSL (won't be able to use any
other connection modules for that), so it was decided that when TLS is
built as a module, sentinel does not support TLS at all.
This means that it keeps using redis_tls_ctx and redis_tls_client_ctx directly.
Example code of config in redis-tls.so(may be use in the future):
RedisModuleString *tls_cfg = NULL;
void tlsInfo(RedisModuleInfoCtx *ctx, int for_crash_report) {
UNUSED(for_crash_report);
RedisModule_InfoAddSection(ctx, "");
RedisModule_InfoAddFieldLongLong(ctx, "var", 42);
}
int tlsCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
if (argc != 2) return RedisModule_WrongArity(ctx);
return RedisModule_ReplyWithString(ctx, argv[1]);
}
RedisModuleString *getStringConfigCommand(const char *name, void *privdata) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(privdata);
return tls_cfg;
}
int setStringConfigCommand(const char *name, RedisModuleString *new, void *privdata, RedisModuleString **err) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(err);
REDISMODULE_NOT_USED(privdata);
if (tls_cfg) RedisModule_FreeString(NULL, tls_cfg);
RedisModule_RetainString(NULL, new);
tls_cfg = new;
return REDISMODULE_OK;
}
int RedisModule_OnLoad(void *ctx, RedisModuleString **argv, int argc)
{
....
if (RedisModule_CreateCommand(ctx,"tls",tlsCommand,"",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_RegisterStringConfig(ctx, "cfg", "", REDISMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_LoadConfigs(ctx) == REDISMODULE_ERR) {
if (tls_cfg) {
RedisModule_FreeString(ctx, tls_cfg);
tls_cfg = NULL;
}
return REDISMODULE_ERR;
}
...
}
Co-authored-by: zhenwei pi <pizhenwei@bytedance.com>
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-08-22 03:53:56 -04:00
|
|
|
set ::tls_module 0
|
2014-02-23 11:57:53 -05:00
|
|
|
set ::pause_on_error 0
|
2020-09-06 04:11:49 -04:00
|
|
|
set ::dont_clean 0
|
2014-03-04 06:05:49 -05:00
|
|
|
set ::simulate_error 0
|
2015-03-30 08:29:01 -04:00
|
|
|
set ::failed 0
|
2014-02-17 11:37:56 -05:00
|
|
|
set ::sentinel_instances {}
|
2024-03-21 05:56:59 -04:00
|
|
|
set ::redict_instances {}
|
2021-01-17 08:48:48 -05:00
|
|
|
set ::global_config {}
|
2014-02-17 11:37:56 -05:00
|
|
|
set ::sentinel_base_port 20000
|
2024-03-21 05:56:59 -04:00
|
|
|
set ::redict_base_port 30000
|
|
|
|
set ::redict_port_count 1024
|
Add hostname support in Sentinel. (#8282)
This is both a bugfix and an enhancement.
Internally, Sentinel relies entirely on IP addresses to identify
instances. When configured with a new master, it also requires users to
specify and IP and not hostname.
However, replicas may use the replica-announce-ip configuration to
announce a hostname. When that happens, Sentinel fails to match the
announced hostname with the expected IP and considers that a different
instance, triggering reconfiguration, etc.
Another use case is where TLS is used and clients are expected to match
the hostname to connect to with the certificate's SAN attribute. To
properly implement this configuration, it is necessary for Sentinel to
redirect clients to a hostname rather than an IP address.
The new 'resolve-hostnames' configuration parameter determines if
Sentinel is willing to accept hostnames. It is set by default to no,
which maintains backwards compatibility and avoids unexpected DNS
resolution delays on systems with DNS configuration issues.
Internally, Sentinel continues to identify instances by their resolved
IP address and will also report the IP by default. The new
'announce-hostnames' parameter determines if Sentinel should prefer to
announce a hostname, when available, rather than an IP address. This
applies to addresses returned to clients, as well as their
representation in the configuration file, REPLICAOF configuration
commands, etc.
This commit also introduces SENTINEL CONFIG GET and SENTINEL CONFIG SET
which can be used to introspect or configure global Sentinel
configuration that was previously was only possible by directly
accessing the configuration file and possibly restarting the instance.
Co-authored-by: myl1024 <myl92916@qq.com>
Co-authored-by: sundb <sundbcn@gmail.com>
2021-01-28 05:09:11 -05:00
|
|
|
set ::host "127.0.0.1"
|
2021-02-08 10:02:46 -05:00
|
|
|
set ::leaked_fds_file [file normalize "tmp/leaked_fds.txt"]
|
2014-02-17 11:37:56 -05:00
|
|
|
set ::pids {} ; # We kill everything at exit
|
|
|
|
set ::dirs {} ; # We remove all the temp dirs at exit
|
2014-02-20 10:28:38 -05:00
|
|
|
set ::run_matching {} ; # If non empty, only tests matching pattern are run.
|
2022-08-01 03:12:27 -04:00
|
|
|
set ::stop_on_failure 0
|
|
|
|
set ::loop 0
|
2014-02-17 11:37:56 -05:00
|
|
|
|
2014-04-24 05:08:22 -04:00
|
|
|
if {[catch {cd tmp}]} {
|
|
|
|
puts "tmp directory not found."
|
2024-03-21 05:56:59 -04:00
|
|
|
puts "Please run this test from the Redict source root."
|
2014-02-17 11:37:56 -05:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2015-01-22 12:57:45 -05:00
|
|
|
# Execute the specified instance of the server specified by 'type', using
|
|
|
|
# the provided configuration file. Returns the PID of the process.
|
2020-09-06 04:11:49 -04:00
|
|
|
proc exec_instance {type dirname cfgfile} {
|
2024-03-21 05:56:59 -04:00
|
|
|
if {$type eq "redict"} {
|
|
|
|
set prgname redict-server
|
2015-01-22 12:57:45 -05:00
|
|
|
} elseif {$type eq "sentinel"} {
|
2024-03-21 05:56:59 -04:00
|
|
|
set prgname redict-sentinel
|
2015-01-22 12:57:45 -05:00
|
|
|
} else {
|
|
|
|
error "Unknown instance type."
|
|
|
|
}
|
|
|
|
|
2020-09-06 04:11:49 -04:00
|
|
|
set errfile [file join $dirname err.txt]
|
2015-01-22 12:57:45 -05:00
|
|
|
if {$::valgrind} {
|
2020-09-06 04:11:49 -04:00
|
|
|
set pid [exec valgrind --track-origins=yes --suppressions=../../../src/valgrind.sup --show-reachable=no --show-possibly-lost=no --leak-check=full ../../../src/${prgname} $cfgfile 2>> $errfile &]
|
2015-01-22 12:57:45 -05:00
|
|
|
} else {
|
2020-09-07 09:26:11 -04:00
|
|
|
set pid [exec ../../../src/${prgname} $cfgfile 2>> $errfile &]
|
2015-01-22 12:57:45 -05:00
|
|
|
}
|
|
|
|
return $pid
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:56:59 -04:00
|
|
|
# Spawn a redict or sentinel instance, depending on 'type'.
|
Sentinel: Fix Config Dependency and Rewrite Sequence (#8271)
This commit fixes a well known and an annoying issue in Sentinel mode.
Cause of this issue:
Currently, Redis rewrite process works well in server mode, however in sentinel mode,
the sentinel config has variant semantics for different configurations, in example configuration
https://github.com/redis/redis/blob/unstable/sentinel.conf, we put comments on these.
However the rewrite process only treat the sentinel config as a single option. During rewrite
process, it will mess up with the lines and comments.
Approaches:
In order to solve this issue, we need to differentiate different subconfig options in sentinel separately,
for example, sentinel monitor <master-name> <ip> <redis-port> <quorum>
we can treat it as sentinel monitor option, instead of the sentinel option.
This commit also fixes the dependency issue when putting configurations in sentinel.conf.
For example before this commit,we must put
`sentinel monitor <master-name> <ip> <redis-port> <quorum>` before
`sentinel auth-pass <master-name> <password>` for a single master,
otherwise the server cannot start and will return error. This commit fixes this issue, as long as
the monitoring master was configured, no matter the sequence is, the sentinel can start and run properly.
2021-01-26 02:31:54 -05:00
|
|
|
proc spawn_instance {type base_port count {conf {}} {base_conf_file ""}} {
|
2014-02-17 11:37:56 -05:00
|
|
|
for {set j 0} {$j < $count} {incr j} {
|
2024-03-21 05:56:59 -04:00
|
|
|
set port [find_available_port $base_port $::redict_port_count]
|
2021-03-30 16:11:32 -04:00
|
|
|
# plaintext port (only used for TLS cluster)
|
|
|
|
set pport 0
|
2014-04-24 04:50:51 -04:00
|
|
|
# Create a directory for this instance.
|
2014-02-17 11:37:56 -05:00
|
|
|
set dirname "${type}_${j}"
|
|
|
|
lappend ::dirs $dirname
|
|
|
|
catch {exec rm -rf $dirname}
|
|
|
|
file mkdir $dirname
|
|
|
|
|
2014-04-24 04:50:51 -04:00
|
|
|
# Write the instance config file.
|
2014-02-17 11:37:56 -05:00
|
|
|
set cfgfile [file join $dirname $type.conf]
|
Sentinel: Fix Config Dependency and Rewrite Sequence (#8271)
This commit fixes a well known and an annoying issue in Sentinel mode.
Cause of this issue:
Currently, Redis rewrite process works well in server mode, however in sentinel mode,
the sentinel config has variant semantics for different configurations, in example configuration
https://github.com/redis/redis/blob/unstable/sentinel.conf, we put comments on these.
However the rewrite process only treat the sentinel config as a single option. During rewrite
process, it will mess up with the lines and comments.
Approaches:
In order to solve this issue, we need to differentiate different subconfig options in sentinel separately,
for example, sentinel monitor <master-name> <ip> <redis-port> <quorum>
we can treat it as sentinel monitor option, instead of the sentinel option.
This commit also fixes the dependency issue when putting configurations in sentinel.conf.
For example before this commit,we must put
`sentinel monitor <master-name> <ip> <redis-port> <quorum>` before
`sentinel auth-pass <master-name> <password>` for a single master,
otherwise the server cannot start and will return error. This commit fixes this issue, as long as
the monitoring master was configured, no matter the sequence is, the sentinel can start and run properly.
2021-01-26 02:31:54 -05:00
|
|
|
if {$base_conf_file ne ""} {
|
|
|
|
file copy -- $base_conf_file $cfgfile
|
|
|
|
set cfg [open $cfgfile a+]
|
|
|
|
} else {
|
|
|
|
set cfg [open $cfgfile w]
|
|
|
|
}
|
|
|
|
|
2019-09-12 03:56:54 -04:00
|
|
|
if {$::tls} {
|
Build TLS as a loadable module
* Support BUILD_TLS=module to be loaded as a module via config file or
command line. e.g. redis-server --loadmodule redis-tls.so
* Updates to redismodule.h to allow it to be used side by side with
server.h by defining REDISMODULE_CORE_MODULE
* Changes to server.h, redismodule.h and module.c to avoid repeated
type declarations (gcc 4.8 doesn't like these)
* Add a mechanism for non-ABI neutral modules (ones who include
server.h) to refuse loading if they detect not being built together with
redis (release.c)
* Fix wrong signature of RedisModuleDefragFunc, this could break
compilation of a module, but not the ABI
* Move initialization of listeners in server.c to be after loading
the modules
* Config TLS after initialization of listeners
* Init cluster after initialization of listeners
* Add TLS module to CI
* Fix a test suite race conditions:
Now that the listeners are initialized later, it's not sufficient to
wait for the PID message in the log, we need to wait for the "Server
Initialized" message.
* Fix issues with moduleconfigs test as a result from start_server
waiting for "Server Initialized"
* Fix issues with modules/infra test as a result of an additional module
present
Notes about Sentinel:
Sentinel can't really rely on the tls module, since it uses hiredis to
initiate connections and depends on OpenSSL (won't be able to use any
other connection modules for that), so it was decided that when TLS is
built as a module, sentinel does not support TLS at all.
This means that it keeps using redis_tls_ctx and redis_tls_client_ctx directly.
Example code of config in redis-tls.so(may be use in the future):
RedisModuleString *tls_cfg = NULL;
void tlsInfo(RedisModuleInfoCtx *ctx, int for_crash_report) {
UNUSED(for_crash_report);
RedisModule_InfoAddSection(ctx, "");
RedisModule_InfoAddFieldLongLong(ctx, "var", 42);
}
int tlsCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
if (argc != 2) return RedisModule_WrongArity(ctx);
return RedisModule_ReplyWithString(ctx, argv[1]);
}
RedisModuleString *getStringConfigCommand(const char *name, void *privdata) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(privdata);
return tls_cfg;
}
int setStringConfigCommand(const char *name, RedisModuleString *new, void *privdata, RedisModuleString **err) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(err);
REDISMODULE_NOT_USED(privdata);
if (tls_cfg) RedisModule_FreeString(NULL, tls_cfg);
RedisModule_RetainString(NULL, new);
tls_cfg = new;
return REDISMODULE_OK;
}
int RedisModule_OnLoad(void *ctx, RedisModuleString **argv, int argc)
{
....
if (RedisModule_CreateCommand(ctx,"tls",tlsCommand,"",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_RegisterStringConfig(ctx, "cfg", "", REDISMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_LoadConfigs(ctx) == REDISMODULE_ERR) {
if (tls_cfg) {
RedisModule_FreeString(ctx, tls_cfg);
tls_cfg = NULL;
}
return REDISMODULE_ERR;
}
...
}
Co-authored-by: zhenwei pi <pizhenwei@bytedance.com>
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-08-22 03:53:56 -04:00
|
|
|
if {$::tls_module} {
|
2024-03-21 05:56:59 -04:00
|
|
|
puts $cfg [format "loadmodule %s/../../../src/redict-tls.so" [pwd]]
|
Build TLS as a loadable module
* Support BUILD_TLS=module to be loaded as a module via config file or
command line. e.g. redis-server --loadmodule redis-tls.so
* Updates to redismodule.h to allow it to be used side by side with
server.h by defining REDISMODULE_CORE_MODULE
* Changes to server.h, redismodule.h and module.c to avoid repeated
type declarations (gcc 4.8 doesn't like these)
* Add a mechanism for non-ABI neutral modules (ones who include
server.h) to refuse loading if they detect not being built together with
redis (release.c)
* Fix wrong signature of RedisModuleDefragFunc, this could break
compilation of a module, but not the ABI
* Move initialization of listeners in server.c to be after loading
the modules
* Config TLS after initialization of listeners
* Init cluster after initialization of listeners
* Add TLS module to CI
* Fix a test suite race conditions:
Now that the listeners are initialized later, it's not sufficient to
wait for the PID message in the log, we need to wait for the "Server
Initialized" message.
* Fix issues with moduleconfigs test as a result from start_server
waiting for "Server Initialized"
* Fix issues with modules/infra test as a result of an additional module
present
Notes about Sentinel:
Sentinel can't really rely on the tls module, since it uses hiredis to
initiate connections and depends on OpenSSL (won't be able to use any
other connection modules for that), so it was decided that when TLS is
built as a module, sentinel does not support TLS at all.
This means that it keeps using redis_tls_ctx and redis_tls_client_ctx directly.
Example code of config in redis-tls.so(may be use in the future):
RedisModuleString *tls_cfg = NULL;
void tlsInfo(RedisModuleInfoCtx *ctx, int for_crash_report) {
UNUSED(for_crash_report);
RedisModule_InfoAddSection(ctx, "");
RedisModule_InfoAddFieldLongLong(ctx, "var", 42);
}
int tlsCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
if (argc != 2) return RedisModule_WrongArity(ctx);
return RedisModule_ReplyWithString(ctx, argv[1]);
}
RedisModuleString *getStringConfigCommand(const char *name, void *privdata) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(privdata);
return tls_cfg;
}
int setStringConfigCommand(const char *name, RedisModuleString *new, void *privdata, RedisModuleString **err) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(err);
REDISMODULE_NOT_USED(privdata);
if (tls_cfg) RedisModule_FreeString(NULL, tls_cfg);
RedisModule_RetainString(NULL, new);
tls_cfg = new;
return REDISMODULE_OK;
}
int RedisModule_OnLoad(void *ctx, RedisModuleString **argv, int argc)
{
....
if (RedisModule_CreateCommand(ctx,"tls",tlsCommand,"",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_RegisterStringConfig(ctx, "cfg", "", REDISMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_LoadConfigs(ctx) == REDISMODULE_ERR) {
if (tls_cfg) {
RedisModule_FreeString(ctx, tls_cfg);
tls_cfg = NULL;
}
return REDISMODULE_ERR;
}
...
}
Co-authored-by: zhenwei pi <pizhenwei@bytedance.com>
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-08-22 03:53:56 -04:00
|
|
|
}
|
|
|
|
|
2019-09-12 03:56:54 -04:00
|
|
|
puts $cfg "tls-port $port"
|
|
|
|
puts $cfg "tls-replication yes"
|
|
|
|
puts $cfg "tls-cluster yes"
|
2021-03-30 16:11:32 -04:00
|
|
|
# plaintext port, only used by plaintext clients in a TLS cluster
|
2024-03-21 05:56:59 -04:00
|
|
|
set pport [find_available_port $base_port $::redict_port_count]
|
2021-03-30 16:11:32 -04:00
|
|
|
puts $cfg "port $pport"
|
2020-12-11 11:31:40 -05:00
|
|
|
puts $cfg [format "tls-cert-file %s/../../tls/server.crt" [pwd]]
|
|
|
|
puts $cfg [format "tls-key-file %s/../../tls/server.key" [pwd]]
|
|
|
|
puts $cfg [format "tls-client-cert-file %s/../../tls/client.crt" [pwd]]
|
|
|
|
puts $cfg [format "tls-client-key-file %s/../../tls/client.key" [pwd]]
|
2024-03-21 05:56:59 -04:00
|
|
|
puts $cfg [format "tls-dh-params-file %s/../../tls/redict.dh" [pwd]]
|
2019-09-12 03:56:54 -04:00
|
|
|
puts $cfg [format "tls-ca-cert-file %s/../../tls/ca.crt" [pwd]]
|
|
|
|
} else {
|
|
|
|
puts $cfg "port $port"
|
|
|
|
}
|
Add reply_schema to command json files (internal for now) (#10273)
Work in progress towards implementing a reply schema as part of COMMAND DOCS, see #9845
Since ironing the details of the reply schema of each and every command can take a long time, we
would like to merge this PR when the infrastructure is ready, and let this mature in the unstable branch.
Meanwhile the changes of this PR are internal, they are part of the repo, but do not affect the produced build.
### Background
In #9656 we add a lot of information about Redis commands, but we are missing information about the replies
### Motivation
1. Documentation. This is the primary goal.
2. It should be possible, based on the output of COMMAND, to be able to generate client code in typed
languages. In order to do that, we need Redis to tell us, in detail, what each reply looks like.
3. We would like to build a fuzzer that verifies the reply structure (for now we use the existing
testsuite, see the "Testing" section)
### Schema
The idea is to supply some sort of schema for the various replies of each command.
The schema will describe the conceptual structure of the reply (for generated clients), as defined in RESP3.
Note that the reply structure itself may change, depending on the arguments (e.g. `XINFO STREAM`, with
and without the `FULL` modifier)
We decided to use the standard json-schema (see https://json-schema.org/) as the reply-schema.
Example for `BZPOPMIN`:
```
"reply_schema": {
"oneOf": [
{
"description": "Timeout reached and no elements were popped.",
"type": "null"
},
{
"description": "The keyname, popped member, and its score.",
"type": "array",
"minItems": 3,
"maxItems": 3,
"items": [
{
"description": "Keyname",
"type": "string"
},
{
"description": "Member",
"type": "string"
},
{
"description": "Score",
"type": "number"
}
]
}
]
}
```
#### Notes
1. It is ok that some commands' reply structure depends on the arguments and it's the caller's responsibility
to know which is the relevant one. this comes after looking at other request-reply systems like OpenAPI,
where the reply schema can also be oneOf and the caller is responsible to know which schema is the relevant one.
2. The reply schemas will describe RESP3 replies only. even though RESP3 is structured, we want to use reply
schema for documentation (and possibly to create a fuzzer that validates the replies)
3. For documentation, the description field will include an explanation of the scenario in which the reply is sent,
including any relation to arguments. for example, for `ZRANGE`'s two schemas we will need to state that one
is with `WITHSCORES` and the other is without.
4. For documentation, there will be another optional field "notes" in which we will add a short description of
the representation in RESP2, in case it's not trivial (RESP3's `ZRANGE`'s nested array vs. RESP2's flat
array, for example)
Given the above:
1. We can generate the "return" section of all commands in [redis-doc](https://redis.io/commands/)
(given that "description" and "notes" are comprehensive enough)
2. We can generate a client in a strongly typed language (but the return type could be a conceptual
`union` and the caller needs to know which schema is relevant). see the section below for RESP2 support.
3. We can create a fuzzer for RESP3.
### Limitations (because we are using the standard json-schema)
The problem is that Redis' replies are more diverse than what the json format allows. This means that,
when we convert the reply to a json (in order to validate the schema against it), we lose information (see
the "Testing" section below).
The other option would have been to extend the standard json-schema (and json format) to include stuff
like sets, bulk-strings, error-string, etc. but that would mean also extending the schema-validator - and that
seemed like too much work, so we decided to compromise.
Examples:
1. We cannot tell the difference between an "array" and a "set"
2. We cannot tell the difference between simple-string and bulk-string
3. we cannot verify true uniqueness of items in commands like ZRANGE: json-schema doesn't cover the
case of two identical members with different scores (e.g. `[["m1",6],["m1",7]]`) because `uniqueItems`
compares (member,score) tuples and not just the member name.
### Testing
This commit includes some changes inside Redis in order to verify the schemas (existing and future ones)
are indeed correct (i.e. describe the actual response of Redis).
To do that, we added a debugging feature to Redis that causes it to produce a log of all the commands
it executed and their replies.
For that, Redis needs to be compiled with `-DLOG_REQ_RES` and run with
`--reg-res-logfile <file> --client-default-resp 3` (the testsuite already does that if you run it with
`--log-req-res --force-resp3`)
You should run the testsuite with the above args (and `--dont-clean`) in order to make Redis generate
`.reqres` files (same dir as the `stdout` files) which contain request-response pairs.
These files are later on processed by `./utils/req-res-log-validator.py` which does:
1. Goes over req-res files, generated by redis-servers, spawned by the testsuite (see logreqres.c)
2. For each request-response pair, it validates the response against the request's reply_schema
(obtained from the extended COMMAND DOCS)
5. In order to get good coverage of the Redis commands, and all their different replies, we chose to use
the existing redis test suite, rather than attempt to write a fuzzer.
#### Notes about RESP2
1. We will not be able to use the testing tool to verify RESP2 replies (we are ok with that, it's time to
accept RESP3 as the future RESP)
2. Since the majority of the test suite is using RESP2, and we want the server to reply with RESP3
so that we can validate it, we will need to know how to convert the actual reply to the one expected.
- number and boolean are always strings in RESP2 so the conversion is easy
- objects (maps) are always a flat array in RESP2
- others (nested array in RESP3's `ZRANGE` and others) will need some special per-command
handling (so the client will not be totally auto-generated)
Example for ZRANGE:
```
"reply_schema": {
"anyOf": [
{
"description": "A list of member elements",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string"
}
},
{
"description": "Members and their scores. Returned in case `WITHSCORES` was used.",
"notes": "In RESP2 this is returned as a flat array",
"type": "array",
"uniqueItems": true,
"items": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"items": [
{
"description": "Member",
"type": "string"
},
{
"description": "Score",
"type": "number"
}
]
}
}
]
}
```
### Other changes
1. Some tests that behave differently depending on the RESP are now being tested for both RESP,
regardless of the special log-req-res mode ("Pub/Sub PING" for example)
2. Update the history field of CLIENT LIST
3. Added basic tests for commands that were not covered at all by the testsuite
### TODO
- [x] (maybe a different PR) add a "condition" field to anyOf/oneOf schemas that refers to args. e.g.
when `SET` return NULL, the condition is `arguments.get||arguments.condition`, for `OK` the condition
is `!arguments.get`, and for `string` the condition is `arguments.get` - https://github.com/redis/redis/issues/11896
- [x] (maybe a different PR) also run `runtest-cluster` in the req-res logging mode
- [x] add the new tests to GH actions (i.e. compile with `-DLOG_REQ_RES`, run the tests, and run the validator)
- [x] (maybe a different PR) figure out a way to warn about (sub)schemas that are uncovered by the output
of the tests - https://github.com/redis/redis/issues/11897
- [x] (probably a separate PR) add all missing schemas
- [x] check why "SDOWN is triggered by misconfigured instance replying with errors" fails with --log-req-res
- [x] move the response transformers to their own file (run both regular, cluster, and sentinel tests - need to
fight with the tcl including mechanism a bit)
- [x] issue: module API - https://github.com/redis/redis/issues/11898
- [x] (probably a separate PR): improve schemas: add `required` to `object`s - https://github.com/redis/redis/issues/11899
Co-authored-by: Ozan Tezcan <ozantezcan@gmail.com>
Co-authored-by: Hanna Fadida <hanna.fadida@redislabs.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Shaya Potter <shaya@redislabs.com>
2023-03-11 03:14:16 -05:00
|
|
|
|
|
|
|
if {$::log_req_res} {
|
|
|
|
puts $cfg "req-res-logfile stdout.reqres"
|
|
|
|
}
|
|
|
|
|
|
|
|
if {$::force_resp3} {
|
|
|
|
puts $cfg "client-default-resp 3"
|
|
|
|
}
|
|
|
|
|
Set repl-diskless-sync to yes by default, add repl-diskless-sync-max-replicas (#10092)
1. enable diskless replication by default
2. add a new config named repl-diskless-sync-max-replicas that enables
replication to start before the full repl-diskless-sync-delay was
reached.
3. put replica online sooner on the master (see below)
4. test suite uses repl-diskless-sync-delay of 0 to be faster
5. a few tests that use multiple replica on a pre-populated master, are
now using the new repl-diskless-sync-max-replicas
6. fix possible timing issues in a few cluster tests (see below)
put replica online sooner on the master
----------------------------------------------------
there were two tests that failed because they needed for the master to
realize that the replica is online, but the test code was actually only
waiting for the replica to realize it's online, and in diskless it could
have been before the master realized it.
changes include two things:
1. the tests wait on the right thing
2. issues in the master, putting the replica online in two steps.
the master used to put the replica as online in 2 steps. the first
step was to mark it as online, and the second step was to enable the
write event (only after getting ACK), but in fact the first step didn't
contains some of the tasks to put it online (like updating good slave
count, and sending the module event). this meant that if a test was
waiting to see that the replica is online form the point of view of the
master, and then confirm that the module got an event, or that the
master has enough good replicas, it could fail due to timing issues.
so now the full effect of putting the replica online, happens at once,
and only the part about enabling the writes is delayed till the ACK.
fix cluster tests
--------------------
I added some code to wait for the replica to sync and avoid race
conditions.
later realized the sentinel and cluster tests where using the original 5
seconds delay, so changed it to 0.
this means the other changes are probably not needed, but i suppose
they're still better (avoid race conditions)
2022-01-17 07:11:11 -05:00
|
|
|
puts $cfg "repl-diskless-sync-delay 0"
|
2014-02-17 11:37:56 -05:00
|
|
|
puts $cfg "dir ./$dirname"
|
|
|
|
puts $cfg "logfile log.txt"
|
2014-04-24 04:50:51 -04:00
|
|
|
# Add additional config files
|
|
|
|
foreach directive $conf {
|
|
|
|
puts $cfg $directive
|
|
|
|
}
|
2021-01-17 08:48:48 -05:00
|
|
|
dict for {name val} $::global_config {
|
|
|
|
puts $cfg "$name $val"
|
|
|
|
}
|
2014-02-17 11:37:56 -05:00
|
|
|
close $cfg
|
|
|
|
|
|
|
|
# Finally exec it and remember the pid for later cleanup.
|
2020-08-08 23:08:00 -04:00
|
|
|
set retry 100
|
|
|
|
while {$retry} {
|
2020-09-06 04:11:49 -04:00
|
|
|
set pid [exec_instance $type $dirname $cfgfile]
|
2014-02-17 11:37:56 -05:00
|
|
|
|
2020-08-08 23:08:00 -04:00
|
|
|
# Check availability
|
|
|
|
if {[server_is_up 127.0.0.1 $port 100] == 0} {
|
|
|
|
puts "Starting $type #$j at port $port failed, try another"
|
|
|
|
incr retry -1
|
2024-03-21 05:56:59 -04:00
|
|
|
set port [find_available_port $base_port $::redict_port_count]
|
2020-08-08 23:08:00 -04:00
|
|
|
set cfg [open $cfgfile a+]
|
|
|
|
if {$::tls} {
|
|
|
|
puts $cfg "tls-port $port"
|
2024-03-21 05:56:59 -04:00
|
|
|
set pport [find_available_port $base_port $::redict_port_count]
|
2021-03-30 16:11:32 -04:00
|
|
|
puts $cfg "port $pport"
|
2020-08-08 23:08:00 -04:00
|
|
|
} else {
|
|
|
|
puts $cfg "port $port"
|
|
|
|
}
|
|
|
|
close $cfg
|
|
|
|
} else {
|
|
|
|
puts "Starting $type #$j at port $port"
|
|
|
|
lappend ::pids $pid
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check availability finally
|
Add hostname support in Sentinel. (#8282)
This is both a bugfix and an enhancement.
Internally, Sentinel relies entirely on IP addresses to identify
instances. When configured with a new master, it also requires users to
specify and IP and not hostname.
However, replicas may use the replica-announce-ip configuration to
announce a hostname. When that happens, Sentinel fails to match the
announced hostname with the expected IP and considers that a different
instance, triggering reconfiguration, etc.
Another use case is where TLS is used and clients are expected to match
the hostname to connect to with the certificate's SAN attribute. To
properly implement this configuration, it is necessary for Sentinel to
redirect clients to a hostname rather than an IP address.
The new 'resolve-hostnames' configuration parameter determines if
Sentinel is willing to accept hostnames. It is set by default to no,
which maintains backwards compatibility and avoids unexpected DNS
resolution delays on systems with DNS configuration issues.
Internally, Sentinel continues to identify instances by their resolved
IP address and will also report the IP by default. The new
'announce-hostnames' parameter determines if Sentinel should prefer to
announce a hostname, when available, rather than an IP address. This
applies to addresses returned to clients, as well as their
representation in the configuration file, REPLICAOF configuration
commands, etc.
This commit also introduces SENTINEL CONFIG GET and SENTINEL CONFIG SET
which can be used to introspect or configure global Sentinel
configuration that was previously was only possible by directly
accessing the configuration file and possibly restarting the instance.
Co-authored-by: myl1024 <myl92916@qq.com>
Co-authored-by: sundb <sundbcn@gmail.com>
2021-01-28 05:09:11 -05:00
|
|
|
if {[server_is_up $::host $port 100] == 0} {
|
2020-08-20 01:59:02 -04:00
|
|
|
set logfile [file join $dirname log.txt]
|
|
|
|
puts [exec tail $logfile]
|
|
|
|
abort_sentinel_test "Problems starting $type #$j: ping timeout, maybe server start failed, check $logfile"
|
2014-02-17 11:37:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# Push the instance into the right list
|
2024-03-21 05:56:59 -04:00
|
|
|
set link [redict $::host $port 0 $::tls]
|
2014-06-18 09:54:55 -04:00
|
|
|
$link reconnect 1
|
2014-02-18 05:04:01 -05:00
|
|
|
lappend ::${type}_instances [list \
|
2014-02-22 11:26:30 -05:00
|
|
|
pid $pid \
|
Add hostname support in Sentinel. (#8282)
This is both a bugfix and an enhancement.
Internally, Sentinel relies entirely on IP addresses to identify
instances. When configured with a new master, it also requires users to
specify and IP and not hostname.
However, replicas may use the replica-announce-ip configuration to
announce a hostname. When that happens, Sentinel fails to match the
announced hostname with the expected IP and considers that a different
instance, triggering reconfiguration, etc.
Another use case is where TLS is used and clients are expected to match
the hostname to connect to with the certificate's SAN attribute. To
properly implement this configuration, it is necessary for Sentinel to
redirect clients to a hostname rather than an IP address.
The new 'resolve-hostnames' configuration parameter determines if
Sentinel is willing to accept hostnames. It is set by default to no,
which maintains backwards compatibility and avoids unexpected DNS
resolution delays on systems with DNS configuration issues.
Internally, Sentinel continues to identify instances by their resolved
IP address and will also report the IP by default. The new
'announce-hostnames' parameter determines if Sentinel should prefer to
announce a hostname, when available, rather than an IP address. This
applies to addresses returned to clients, as well as their
representation in the configuration file, REPLICAOF configuration
commands, etc.
This commit also introduces SENTINEL CONFIG GET and SENTINEL CONFIG SET
which can be used to introspect or configure global Sentinel
configuration that was previously was only possible by directly
accessing the configuration file and possibly restarting the instance.
Co-authored-by: myl1024 <myl92916@qq.com>
Co-authored-by: sundb <sundbcn@gmail.com>
2021-01-28 05:09:11 -05:00
|
|
|
host $::host \
|
2014-02-17 11:37:56 -05:00
|
|
|
port $port \
|
2021-03-30 16:11:32 -04:00
|
|
|
plaintext-port $pport \
|
2014-06-18 09:54:55 -04:00
|
|
|
link $link \
|
2014-02-17 11:37:56 -05:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:19:28 -05:00
|
|
|
proc log_crashes {} {
|
2024-03-25 07:49:40 -04:00
|
|
|
set start_pattern {*REDICT BUG REPORT START*}
|
2016-01-15 04:19:28 -05:00
|
|
|
set logs [glob */log.txt]
|
|
|
|
foreach log $logs {
|
|
|
|
set fd [open $log]
|
|
|
|
set found 0
|
|
|
|
while {[gets $fd line] >= 0} {
|
|
|
|
if {[string match $start_pattern $line]} {
|
|
|
|
puts "\n*** Crash report found in $log ***"
|
|
|
|
set found 1
|
|
|
|
}
|
2020-09-06 04:11:49 -04:00
|
|
|
if {$found} {
|
|
|
|
puts $line
|
|
|
|
incr ::failed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set logs [glob */err.txt]
|
|
|
|
foreach log $logs {
|
2020-08-14 09:05:34 -04:00
|
|
|
set res [find_valgrind_errors $log true]
|
2020-09-06 04:11:49 -04:00
|
|
|
if {$res != ""} {
|
|
|
|
puts $res
|
|
|
|
incr ::failed
|
2016-01-15 04:19:28 -05:00
|
|
|
}
|
|
|
|
}
|
2021-11-11 06:51:33 -05:00
|
|
|
|
|
|
|
set logs [glob */err.txt]
|
|
|
|
foreach log $logs {
|
|
|
|
set res [sanitizer_errors_from_file $log]
|
|
|
|
if {$res != ""} {
|
|
|
|
puts $res
|
|
|
|
incr ::failed
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 04:19:28 -05:00
|
|
|
}
|
|
|
|
|
2020-09-06 04:11:49 -04:00
|
|
|
proc is_alive pid {
|
|
|
|
if {[catch {exec ps -p $pid} err]} {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc stop_instance pid {
|
|
|
|
catch {exec kill $pid}
|
2021-02-24 17:26:16 -05:00
|
|
|
# Node might have been stopped in the test
|
|
|
|
catch {exec kill -SIGCONT $pid}
|
2020-09-06 04:11:49 -04:00
|
|
|
if {$::valgrind} {
|
2021-10-26 01:34:30 -04:00
|
|
|
set max_wait 120000
|
2020-09-06 04:11:49 -04:00
|
|
|
} else {
|
|
|
|
set max_wait 10000
|
|
|
|
}
|
|
|
|
while {[is_alive $pid]} {
|
|
|
|
incr wait 10
|
|
|
|
|
2021-10-26 01:34:30 -04:00
|
|
|
if {$wait == $max_wait} {
|
2021-10-31 13:22:21 -04:00
|
|
|
puts [colorstr red "Forcing process $pid to crash..."]
|
2021-10-26 01:34:30 -04:00
|
|
|
catch {exec kill -SEGV $pid}
|
|
|
|
} elseif {$wait >= $max_wait * 2} {
|
2021-10-31 13:22:21 -04:00
|
|
|
puts [colorstr red "Forcing process $pid to exit..."]
|
2020-09-06 04:11:49 -04:00
|
|
|
catch {exec kill -KILL $pid}
|
|
|
|
} elseif {$wait % 1000 == 0} {
|
|
|
|
puts "Waiting for process $pid to exit..."
|
|
|
|
}
|
|
|
|
after 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 11:37:56 -05:00
|
|
|
proc cleanup {} {
|
|
|
|
puts "Cleaning up..."
|
|
|
|
foreach pid $::pids {
|
2020-09-06 04:11:49 -04:00
|
|
|
puts "killing stale instance $pid"
|
|
|
|
stop_instance $pid
|
|
|
|
}
|
|
|
|
log_crashes
|
|
|
|
if {$::dont_clean} {
|
|
|
|
return
|
2014-02-17 11:37:56 -05:00
|
|
|
}
|
|
|
|
foreach dir $::dirs {
|
|
|
|
catch {exec rm -rf $dir}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc abort_sentinel_test msg {
|
2016-01-15 04:19:28 -05:00
|
|
|
incr ::failed
|
2014-02-17 11:37:56 -05:00
|
|
|
puts "WARNING: Aborting the test."
|
|
|
|
puts ">>>>>>>> $msg"
|
2015-01-21 10:18:34 -05:00
|
|
|
if {$::pause_on_error} pause_on_error
|
2014-02-17 11:37:56 -05:00
|
|
|
cleanup
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:28:38 -05:00
|
|
|
proc parse_options {} {
|
|
|
|
for {set j 0} {$j < [llength $::argv]} {incr j} {
|
|
|
|
set opt [lindex $::argv $j]
|
|
|
|
set val [lindex $::argv [expr $j+1]]
|
|
|
|
if {$opt eq "--single"} {
|
|
|
|
incr j
|
2022-01-30 18:47:58 -05:00
|
|
|
lappend ::run_matching "*${val}*"
|
2014-02-23 11:57:53 -05:00
|
|
|
} elseif {$opt eq "--pause-on-error"} {
|
|
|
|
set ::pause_on_error 1
|
2020-09-06 04:11:49 -04:00
|
|
|
} elseif {$opt eq {--dont-clean}} {
|
|
|
|
set ::dont_clean 1
|
2014-03-04 06:05:49 -05:00
|
|
|
} elseif {$opt eq "--fail"} {
|
|
|
|
set ::simulate_error 1
|
2015-01-09 17:43:48 -05:00
|
|
|
} elseif {$opt eq {--valgrind}} {
|
|
|
|
set ::valgrind 1
|
Add hostname support in Sentinel. (#8282)
This is both a bugfix and an enhancement.
Internally, Sentinel relies entirely on IP addresses to identify
instances. When configured with a new master, it also requires users to
specify and IP and not hostname.
However, replicas may use the replica-announce-ip configuration to
announce a hostname. When that happens, Sentinel fails to match the
announced hostname with the expected IP and considers that a different
instance, triggering reconfiguration, etc.
Another use case is where TLS is used and clients are expected to match
the hostname to connect to with the certificate's SAN attribute. To
properly implement this configuration, it is necessary for Sentinel to
redirect clients to a hostname rather than an IP address.
The new 'resolve-hostnames' configuration parameter determines if
Sentinel is willing to accept hostnames. It is set by default to no,
which maintains backwards compatibility and avoids unexpected DNS
resolution delays on systems with DNS configuration issues.
Internally, Sentinel continues to identify instances by their resolved
IP address and will also report the IP by default. The new
'announce-hostnames' parameter determines if Sentinel should prefer to
announce a hostname, when available, rather than an IP address. This
applies to addresses returned to clients, as well as their
representation in the configuration file, REPLICAOF configuration
commands, etc.
This commit also introduces SENTINEL CONFIG GET and SENTINEL CONFIG SET
which can be used to introspect or configure global Sentinel
configuration that was previously was only possible by directly
accessing the configuration file and possibly restarting the instance.
Co-authored-by: myl1024 <myl92916@qq.com>
Co-authored-by: sundb <sundbcn@gmail.com>
2021-01-28 05:09:11 -05:00
|
|
|
} elseif {$opt eq {--host}} {
|
|
|
|
incr j
|
|
|
|
set ::host ${val}
|
Build TLS as a loadable module
* Support BUILD_TLS=module to be loaded as a module via config file or
command line. e.g. redis-server --loadmodule redis-tls.so
* Updates to redismodule.h to allow it to be used side by side with
server.h by defining REDISMODULE_CORE_MODULE
* Changes to server.h, redismodule.h and module.c to avoid repeated
type declarations (gcc 4.8 doesn't like these)
* Add a mechanism for non-ABI neutral modules (ones who include
server.h) to refuse loading if they detect not being built together with
redis (release.c)
* Fix wrong signature of RedisModuleDefragFunc, this could break
compilation of a module, but not the ABI
* Move initialization of listeners in server.c to be after loading
the modules
* Config TLS after initialization of listeners
* Init cluster after initialization of listeners
* Add TLS module to CI
* Fix a test suite race conditions:
Now that the listeners are initialized later, it's not sufficient to
wait for the PID message in the log, we need to wait for the "Server
Initialized" message.
* Fix issues with moduleconfigs test as a result from start_server
waiting for "Server Initialized"
* Fix issues with modules/infra test as a result of an additional module
present
Notes about Sentinel:
Sentinel can't really rely on the tls module, since it uses hiredis to
initiate connections and depends on OpenSSL (won't be able to use any
other connection modules for that), so it was decided that when TLS is
built as a module, sentinel does not support TLS at all.
This means that it keeps using redis_tls_ctx and redis_tls_client_ctx directly.
Example code of config in redis-tls.so(may be use in the future):
RedisModuleString *tls_cfg = NULL;
void tlsInfo(RedisModuleInfoCtx *ctx, int for_crash_report) {
UNUSED(for_crash_report);
RedisModule_InfoAddSection(ctx, "");
RedisModule_InfoAddFieldLongLong(ctx, "var", 42);
}
int tlsCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
if (argc != 2) return RedisModule_WrongArity(ctx);
return RedisModule_ReplyWithString(ctx, argv[1]);
}
RedisModuleString *getStringConfigCommand(const char *name, void *privdata) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(privdata);
return tls_cfg;
}
int setStringConfigCommand(const char *name, RedisModuleString *new, void *privdata, RedisModuleString **err) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(err);
REDISMODULE_NOT_USED(privdata);
if (tls_cfg) RedisModule_FreeString(NULL, tls_cfg);
RedisModule_RetainString(NULL, new);
tls_cfg = new;
return REDISMODULE_OK;
}
int RedisModule_OnLoad(void *ctx, RedisModuleString **argv, int argc)
{
....
if (RedisModule_CreateCommand(ctx,"tls",tlsCommand,"",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_RegisterStringConfig(ctx, "cfg", "", REDISMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_LoadConfigs(ctx) == REDISMODULE_ERR) {
if (tls_cfg) {
RedisModule_FreeString(ctx, tls_cfg);
tls_cfg = NULL;
}
return REDISMODULE_ERR;
}
...
}
Co-authored-by: zhenwei pi <pizhenwei@bytedance.com>
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-08-22 03:53:56 -04:00
|
|
|
} elseif {$opt eq {--tls} || $opt eq {--tls-module}} {
|
2019-09-12 03:56:54 -04:00
|
|
|
package require tls 1.6
|
|
|
|
::tls::init \
|
|
|
|
-cafile "$::tlsdir/ca.crt" \
|
2020-12-11 11:31:40 -05:00
|
|
|
-certfile "$::tlsdir/client.crt" \
|
|
|
|
-keyfile "$::tlsdir/client.key"
|
2019-09-12 03:56:54 -04:00
|
|
|
set ::tls 1
|
Build TLS as a loadable module
* Support BUILD_TLS=module to be loaded as a module via config file or
command line. e.g. redis-server --loadmodule redis-tls.so
* Updates to redismodule.h to allow it to be used side by side with
server.h by defining REDISMODULE_CORE_MODULE
* Changes to server.h, redismodule.h and module.c to avoid repeated
type declarations (gcc 4.8 doesn't like these)
* Add a mechanism for non-ABI neutral modules (ones who include
server.h) to refuse loading if they detect not being built together with
redis (release.c)
* Fix wrong signature of RedisModuleDefragFunc, this could break
compilation of a module, but not the ABI
* Move initialization of listeners in server.c to be after loading
the modules
* Config TLS after initialization of listeners
* Init cluster after initialization of listeners
* Add TLS module to CI
* Fix a test suite race conditions:
Now that the listeners are initialized later, it's not sufficient to
wait for the PID message in the log, we need to wait for the "Server
Initialized" message.
* Fix issues with moduleconfigs test as a result from start_server
waiting for "Server Initialized"
* Fix issues with modules/infra test as a result of an additional module
present
Notes about Sentinel:
Sentinel can't really rely on the tls module, since it uses hiredis to
initiate connections and depends on OpenSSL (won't be able to use any
other connection modules for that), so it was decided that when TLS is
built as a module, sentinel does not support TLS at all.
This means that it keeps using redis_tls_ctx and redis_tls_client_ctx directly.
Example code of config in redis-tls.so(may be use in the future):
RedisModuleString *tls_cfg = NULL;
void tlsInfo(RedisModuleInfoCtx *ctx, int for_crash_report) {
UNUSED(for_crash_report);
RedisModule_InfoAddSection(ctx, "");
RedisModule_InfoAddFieldLongLong(ctx, "var", 42);
}
int tlsCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
if (argc != 2) return RedisModule_WrongArity(ctx);
return RedisModule_ReplyWithString(ctx, argv[1]);
}
RedisModuleString *getStringConfigCommand(const char *name, void *privdata) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(privdata);
return tls_cfg;
}
int setStringConfigCommand(const char *name, RedisModuleString *new, void *privdata, RedisModuleString **err) {
REDISMODULE_NOT_USED(name);
REDISMODULE_NOT_USED(err);
REDISMODULE_NOT_USED(privdata);
if (tls_cfg) RedisModule_FreeString(NULL, tls_cfg);
RedisModule_RetainString(NULL, new);
tls_cfg = new;
return REDISMODULE_OK;
}
int RedisModule_OnLoad(void *ctx, RedisModuleString **argv, int argc)
{
....
if (RedisModule_CreateCommand(ctx,"tls",tlsCommand,"",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_RegisterStringConfig(ctx, "cfg", "", REDISMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_LoadConfigs(ctx) == REDISMODULE_ERR) {
if (tls_cfg) {
RedisModule_FreeString(ctx, tls_cfg);
tls_cfg = NULL;
}
return REDISMODULE_ERR;
}
...
}
Co-authored-by: zhenwei pi <pizhenwei@bytedance.com>
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-08-22 03:53:56 -04:00
|
|
|
if {$opt eq {--tls-module}} {
|
|
|
|
set ::tls_module 1
|
|
|
|
}
|
2021-01-17 08:48:48 -05:00
|
|
|
} elseif {$opt eq {--config}} {
|
|
|
|
set val2 [lindex $::argv [expr $j+2]]
|
|
|
|
dict set ::global_config $val $val2
|
|
|
|
incr j 2
|
2022-08-01 03:12:27 -04:00
|
|
|
} elseif {$opt eq {--stop}} {
|
|
|
|
set ::stop_on_failure 1
|
|
|
|
} elseif {$opt eq {--loop}} {
|
|
|
|
set ::loop 1
|
Add reply_schema to command json files (internal for now) (#10273)
Work in progress towards implementing a reply schema as part of COMMAND DOCS, see #9845
Since ironing the details of the reply schema of each and every command can take a long time, we
would like to merge this PR when the infrastructure is ready, and let this mature in the unstable branch.
Meanwhile the changes of this PR are internal, they are part of the repo, but do not affect the produced build.
### Background
In #9656 we add a lot of information about Redis commands, but we are missing information about the replies
### Motivation
1. Documentation. This is the primary goal.
2. It should be possible, based on the output of COMMAND, to be able to generate client code in typed
languages. In order to do that, we need Redis to tell us, in detail, what each reply looks like.
3. We would like to build a fuzzer that verifies the reply structure (for now we use the existing
testsuite, see the "Testing" section)
### Schema
The idea is to supply some sort of schema for the various replies of each command.
The schema will describe the conceptual structure of the reply (for generated clients), as defined in RESP3.
Note that the reply structure itself may change, depending on the arguments (e.g. `XINFO STREAM`, with
and without the `FULL` modifier)
We decided to use the standard json-schema (see https://json-schema.org/) as the reply-schema.
Example for `BZPOPMIN`:
```
"reply_schema": {
"oneOf": [
{
"description": "Timeout reached and no elements were popped.",
"type": "null"
},
{
"description": "The keyname, popped member, and its score.",
"type": "array",
"minItems": 3,
"maxItems": 3,
"items": [
{
"description": "Keyname",
"type": "string"
},
{
"description": "Member",
"type": "string"
},
{
"description": "Score",
"type": "number"
}
]
}
]
}
```
#### Notes
1. It is ok that some commands' reply structure depends on the arguments and it's the caller's responsibility
to know which is the relevant one. this comes after looking at other request-reply systems like OpenAPI,
where the reply schema can also be oneOf and the caller is responsible to know which schema is the relevant one.
2. The reply schemas will describe RESP3 replies only. even though RESP3 is structured, we want to use reply
schema for documentation (and possibly to create a fuzzer that validates the replies)
3. For documentation, the description field will include an explanation of the scenario in which the reply is sent,
including any relation to arguments. for example, for `ZRANGE`'s two schemas we will need to state that one
is with `WITHSCORES` and the other is without.
4. For documentation, there will be another optional field "notes" in which we will add a short description of
the representation in RESP2, in case it's not trivial (RESP3's `ZRANGE`'s nested array vs. RESP2's flat
array, for example)
Given the above:
1. We can generate the "return" section of all commands in [redis-doc](https://redis.io/commands/)
(given that "description" and "notes" are comprehensive enough)
2. We can generate a client in a strongly typed language (but the return type could be a conceptual
`union` and the caller needs to know which schema is relevant). see the section below for RESP2 support.
3. We can create a fuzzer for RESP3.
### Limitations (because we are using the standard json-schema)
The problem is that Redis' replies are more diverse than what the json format allows. This means that,
when we convert the reply to a json (in order to validate the schema against it), we lose information (see
the "Testing" section below).
The other option would have been to extend the standard json-schema (and json format) to include stuff
like sets, bulk-strings, error-string, etc. but that would mean also extending the schema-validator - and that
seemed like too much work, so we decided to compromise.
Examples:
1. We cannot tell the difference between an "array" and a "set"
2. We cannot tell the difference between simple-string and bulk-string
3. we cannot verify true uniqueness of items in commands like ZRANGE: json-schema doesn't cover the
case of two identical members with different scores (e.g. `[["m1",6],["m1",7]]`) because `uniqueItems`
compares (member,score) tuples and not just the member name.
### Testing
This commit includes some changes inside Redis in order to verify the schemas (existing and future ones)
are indeed correct (i.e. describe the actual response of Redis).
To do that, we added a debugging feature to Redis that causes it to produce a log of all the commands
it executed and their replies.
For that, Redis needs to be compiled with `-DLOG_REQ_RES` and run with
`--reg-res-logfile <file> --client-default-resp 3` (the testsuite already does that if you run it with
`--log-req-res --force-resp3`)
You should run the testsuite with the above args (and `--dont-clean`) in order to make Redis generate
`.reqres` files (same dir as the `stdout` files) which contain request-response pairs.
These files are later on processed by `./utils/req-res-log-validator.py` which does:
1. Goes over req-res files, generated by redis-servers, spawned by the testsuite (see logreqres.c)
2. For each request-response pair, it validates the response against the request's reply_schema
(obtained from the extended COMMAND DOCS)
5. In order to get good coverage of the Redis commands, and all their different replies, we chose to use
the existing redis test suite, rather than attempt to write a fuzzer.
#### Notes about RESP2
1. We will not be able to use the testing tool to verify RESP2 replies (we are ok with that, it's time to
accept RESP3 as the future RESP)
2. Since the majority of the test suite is using RESP2, and we want the server to reply with RESP3
so that we can validate it, we will need to know how to convert the actual reply to the one expected.
- number and boolean are always strings in RESP2 so the conversion is easy
- objects (maps) are always a flat array in RESP2
- others (nested array in RESP3's `ZRANGE` and others) will need some special per-command
handling (so the client will not be totally auto-generated)
Example for ZRANGE:
```
"reply_schema": {
"anyOf": [
{
"description": "A list of member elements",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string"
}
},
{
"description": "Members and their scores. Returned in case `WITHSCORES` was used.",
"notes": "In RESP2 this is returned as a flat array",
"type": "array",
"uniqueItems": true,
"items": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"items": [
{
"description": "Member",
"type": "string"
},
{
"description": "Score",
"type": "number"
}
]
}
}
]
}
```
### Other changes
1. Some tests that behave differently depending on the RESP are now being tested for both RESP,
regardless of the special log-req-res mode ("Pub/Sub PING" for example)
2. Update the history field of CLIENT LIST
3. Added basic tests for commands that were not covered at all by the testsuite
### TODO
- [x] (maybe a different PR) add a "condition" field to anyOf/oneOf schemas that refers to args. e.g.
when `SET` return NULL, the condition is `arguments.get||arguments.condition`, for `OK` the condition
is `!arguments.get`, and for `string` the condition is `arguments.get` - https://github.com/redis/redis/issues/11896
- [x] (maybe a different PR) also run `runtest-cluster` in the req-res logging mode
- [x] add the new tests to GH actions (i.e. compile with `-DLOG_REQ_RES`, run the tests, and run the validator)
- [x] (maybe a different PR) figure out a way to warn about (sub)schemas that are uncovered by the output
of the tests - https://github.com/redis/redis/issues/11897
- [x] (probably a separate PR) add all missing schemas
- [x] check why "SDOWN is triggered by misconfigured instance replying with errors" fails with --log-req-res
- [x] move the response transformers to their own file (run both regular, cluster, and sentinel tests - need to
fight with the tcl including mechanism a bit)
- [x] issue: module API - https://github.com/redis/redis/issues/11898
- [x] (probably a separate PR): improve schemas: add `required` to `object`s - https://github.com/redis/redis/issues/11899
Co-authored-by: Ozan Tezcan <ozantezcan@gmail.com>
Co-authored-by: Hanna Fadida <hanna.fadida@redislabs.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Shaya Potter <shaya@redislabs.com>
2023-03-11 03:14:16 -05:00
|
|
|
} elseif {$opt eq {--log-req-res}} {
|
|
|
|
set ::log_req_res 1
|
|
|
|
} elseif {$opt eq {--force-resp3}} {
|
|
|
|
set ::force_resp3 1
|
2014-02-20 10:28:38 -05:00
|
|
|
} elseif {$opt eq "--help"} {
|
2014-03-04 05:17:27 -05:00
|
|
|
puts "--single <pattern> Only runs tests specified by pattern."
|
2020-09-06 04:11:49 -04:00
|
|
|
puts "--dont-clean Keep log files on exit."
|
2014-03-04 05:17:27 -05:00
|
|
|
puts "--pause-on-error Pause for manual inspection on error."
|
2014-03-04 06:05:49 -05:00
|
|
|
puts "--fail Simulate a test failure."
|
2015-12-29 09:27:26 -05:00
|
|
|
puts "--valgrind Run with valgrind."
|
2020-09-15 01:27:42 -04:00
|
|
|
puts "--tls Run tests in TLS mode."
|
2024-03-21 05:56:59 -04:00
|
|
|
puts "--tls-module Run tests in TLS mode with Redict module."
|
Add hostname support in Sentinel. (#8282)
This is both a bugfix and an enhancement.
Internally, Sentinel relies entirely on IP addresses to identify
instances. When configured with a new master, it also requires users to
specify and IP and not hostname.
However, replicas may use the replica-announce-ip configuration to
announce a hostname. When that happens, Sentinel fails to match the
announced hostname with the expected IP and considers that a different
instance, triggering reconfiguration, etc.
Another use case is where TLS is used and clients are expected to match
the hostname to connect to with the certificate's SAN attribute. To
properly implement this configuration, it is necessary for Sentinel to
redirect clients to a hostname rather than an IP address.
The new 'resolve-hostnames' configuration parameter determines if
Sentinel is willing to accept hostnames. It is set by default to no,
which maintains backwards compatibility and avoids unexpected DNS
resolution delays on systems with DNS configuration issues.
Internally, Sentinel continues to identify instances by their resolved
IP address and will also report the IP by default. The new
'announce-hostnames' parameter determines if Sentinel should prefer to
announce a hostname, when available, rather than an IP address. This
applies to addresses returned to clients, as well as their
representation in the configuration file, REPLICAOF configuration
commands, etc.
This commit also introduces SENTINEL CONFIG GET and SENTINEL CONFIG SET
which can be used to introspect or configure global Sentinel
configuration that was previously was only possible by directly
accessing the configuration file and possibly restarting the instance.
Co-authored-by: myl1024 <myl92916@qq.com>
Co-authored-by: sundb <sundbcn@gmail.com>
2021-01-28 05:09:11 -05:00
|
|
|
puts "--host <host> Use hostname instead of 127.0.0.1."
|
2021-01-17 08:48:48 -05:00
|
|
|
puts "--config <k> <v> Extra config argument(s)."
|
2022-08-01 03:12:27 -04:00
|
|
|
puts "--stop Blocks once the first test fails."
|
|
|
|
puts "--loop Execute the specified set of tests forever."
|
2014-03-04 05:17:27 -05:00
|
|
|
puts "--help Shows this help."
|
2014-02-20 10:28:38 -05:00
|
|
|
exit 0
|
|
|
|
} else {
|
|
|
|
puts "Unknown option $opt"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 11:57:53 -05:00
|
|
|
# If --pause-on-error option was passed at startup this function is called
|
|
|
|
# on error in order to give the developer a chance to understand more about
|
|
|
|
# the error condition while the instances are still running.
|
|
|
|
proc pause_on_error {} {
|
2014-02-23 12:01:30 -05:00
|
|
|
puts ""
|
2014-02-23 11:57:53 -05:00
|
|
|
puts [colorstr yellow "*** Please inspect the error now ***"]
|
2014-03-04 09:55:36 -05:00
|
|
|
puts "\nType \"continue\" to resume the test, \"help\" for help screen.\n"
|
2014-02-23 12:01:30 -05:00
|
|
|
while 1 {
|
|
|
|
puts -nonewline "> "
|
2014-02-23 11:57:53 -05:00
|
|
|
flush stdout
|
2014-03-04 06:05:49 -05:00
|
|
|
set line [gets stdin]
|
|
|
|
set argv [split $line " "]
|
|
|
|
set cmd [lindex $argv 0]
|
|
|
|
if {$cmd eq {continue}} {
|
|
|
|
break
|
2024-03-21 05:56:59 -04:00
|
|
|
} elseif {$cmd eq {show-redict-logs}} {
|
2014-06-10 09:19:35 -04:00
|
|
|
set count 10
|
|
|
|
if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}
|
2024-03-21 05:56:59 -04:00
|
|
|
foreach_redict_id id {
|
2024-03-25 07:49:40 -04:00
|
|
|
puts "=== REDICT $id ===="
|
2024-03-21 05:56:59 -04:00
|
|
|
puts [exec tail -$count redict_$id/log.txt]
|
2014-06-10 09:19:35 -04:00
|
|
|
puts "---------------------\n"
|
|
|
|
}
|
2014-03-04 06:05:49 -05:00
|
|
|
} elseif {$cmd eq {show-sentinel-logs}} {
|
|
|
|
set count 10
|
|
|
|
if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}
|
|
|
|
foreach_sentinel_id id {
|
|
|
|
puts "=== SENTINEL $id ===="
|
|
|
|
puts [exec tail -$count sentinel_$id/log.txt]
|
|
|
|
puts "---------------------\n"
|
|
|
|
}
|
2014-03-04 09:55:36 -05:00
|
|
|
} elseif {$cmd eq {ls}} {
|
2024-03-21 05:56:59 -04:00
|
|
|
foreach_redict_id id {
|
|
|
|
puts -nonewline "Redict $id"
|
2014-03-04 09:55:36 -05:00
|
|
|
set errcode [catch {
|
|
|
|
set str {}
|
|
|
|
append str "@[RI $id tcp_port]: "
|
|
|
|
append str "[RI $id role] "
|
|
|
|
if {[RI $id role] eq {slave}} {
|
|
|
|
append str "[RI $id master_host]:[RI $id master_port]"
|
|
|
|
}
|
|
|
|
set str
|
|
|
|
} retval]
|
|
|
|
if {$errcode} {
|
|
|
|
puts " -- $retval"
|
|
|
|
} else {
|
|
|
|
puts $retval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach_sentinel_id id {
|
|
|
|
puts -nonewline "Sentinel $id"
|
|
|
|
set errcode [catch {
|
|
|
|
set str {}
|
|
|
|
append str "@[SI $id tcp_port]: "
|
|
|
|
append str "[join [S $id sentinel get-master-addr-by-name mymaster]]"
|
|
|
|
set str
|
|
|
|
} retval]
|
|
|
|
if {$errcode} {
|
|
|
|
puts " -- $retval"
|
|
|
|
} else {
|
|
|
|
puts $retval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif {$cmd eq {help}} {
|
2024-03-21 05:56:59 -04:00
|
|
|
puts "ls List Sentinel and Redict instances."
|
2014-03-04 09:55:36 -05:00
|
|
|
puts "show-sentinel-logs \[N\] Show latest N lines of logs."
|
2024-03-21 05:56:59 -04:00
|
|
|
puts "show-redict-logs \[N\] Show latest N lines of logs."
|
2014-03-04 09:55:36 -05:00
|
|
|
puts "S <id> cmd ... arg Call command in Sentinel <id>."
|
2024-03-21 05:56:59 -04:00
|
|
|
puts "R <id> cmd ... arg Call command in Redict <id>."
|
2014-03-04 09:55:36 -05:00
|
|
|
puts "SI <id> <field> Show Sentinel <id> INFO <field>."
|
2024-03-21 05:56:59 -04:00
|
|
|
puts "RI <id> <field> Show Redict <id> INFO <field>."
|
2014-03-04 09:55:36 -05:00
|
|
|
puts "continue Resume test."
|
2014-03-04 06:05:49 -05:00
|
|
|
} else {
|
|
|
|
set errcode [catch {eval $line} retval]
|
2014-03-04 09:55:36 -05:00
|
|
|
if {$retval ne {}} {puts "$retval"}
|
2014-03-04 06:05:49 -05:00
|
|
|
}
|
2014-02-23 11:57:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 11:37:56 -05:00
|
|
|
# We redefine 'test' as for Sentinel we don't use the server-client
|
|
|
|
# architecture for the test, everything is sequential.
|
|
|
|
proc test {descr code} {
|
2014-03-04 05:17:27 -05:00
|
|
|
set ts [clock format [clock seconds] -format %H:%M:%S]
|
|
|
|
puts -nonewline "$ts> $descr: "
|
2014-02-17 11:37:56 -05:00
|
|
|
flush stdout
|
|
|
|
|
|
|
|
if {[catch {set retval [uplevel 1 $code]} error]} {
|
2015-03-30 08:29:01 -04:00
|
|
|
incr ::failed
|
2014-02-17 11:37:56 -05:00
|
|
|
if {[string match "assertion:*" $error]} {
|
2021-10-31 13:22:21 -04:00
|
|
|
set msg "FAILED: [string range $error 10 end]"
|
2014-02-17 11:37:56 -05:00
|
|
|
puts [colorstr red $msg]
|
2014-02-23 11:57:53 -05:00
|
|
|
if {$::pause_on_error} pause_on_error
|
2021-10-31 13:22:21 -04:00
|
|
|
puts [colorstr red "(Jumping to next unit after error)"]
|
2014-02-25 02:33:41 -05:00
|
|
|
return -code continue
|
2014-02-17 11:37:56 -05:00
|
|
|
} else {
|
|
|
|
# Re-raise, let handler up the stack take care of this.
|
|
|
|
error $error $::errorInfo
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
puts [colorstr green OK]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 07:14:23 -05:00
|
|
|
# Check memory leaks when running on OSX using the "leaks" utility.
|
|
|
|
proc check_leaks instance_types {
|
|
|
|
if {[string match {*Darwin*} [exec uname -a]]} {
|
|
|
|
puts -nonewline "Testing for memory leaks..."; flush stdout
|
|
|
|
foreach type $instance_types {
|
|
|
|
foreach_instance_id [set ::${type}_instances] id {
|
|
|
|
if {[instance_is_killed $type $id]} continue
|
|
|
|
set pid [get_instance_attrib $type $id pid]
|
|
|
|
set output {0 leaks}
|
|
|
|
catch {exec leaks $pid} output
|
|
|
|
if {[string match {*process does not exist*} $output] ||
|
|
|
|
[string match {*cannot examine*} $output]} {
|
|
|
|
# In a few tests we kill the server process.
|
|
|
|
set output "0 leaks"
|
|
|
|
} else {
|
|
|
|
puts -nonewline "$type/$pid "
|
|
|
|
flush stdout
|
|
|
|
}
|
|
|
|
if {![string match {*0 leaks*} $output]} {
|
|
|
|
puts [colorstr red "=== MEMORY LEAK DETECTED ==="]
|
|
|
|
puts "Instance type $type, ID $id:"
|
|
|
|
puts $output
|
|
|
|
puts "==="
|
|
|
|
incr ::failed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
puts ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:29:01 -04:00
|
|
|
# Execute all the units inside the 'tests' directory.
|
2014-02-17 11:37:56 -05:00
|
|
|
proc run_tests {} {
|
2014-04-24 05:08:22 -04:00
|
|
|
set tests [lsort [glob ../tests/*]]
|
2022-08-01 03:12:27 -04:00
|
|
|
|
|
|
|
while 1 {
|
2014-02-17 11:37:56 -05:00
|
|
|
foreach test $tests {
|
2021-02-08 10:02:46 -05:00
|
|
|
# Remove leaked_fds file before starting
|
|
|
|
if {$::leaked_fds_file != "" && [file exists $::leaked_fds_file]} {
|
|
|
|
file delete $::leaked_fds_file
|
|
|
|
}
|
|
|
|
|
2022-05-25 11:25:38 -04:00
|
|
|
if {[llength $::run_matching] != 0 && ![search_pattern_list $test $::run_matching true]} {
|
2014-02-20 10:28:38 -05:00
|
|
|
continue
|
|
|
|
}
|
2014-02-20 10:57:51 -05:00
|
|
|
if {[file isdirectory $test]} continue
|
2014-02-18 10:31:52 -05:00
|
|
|
puts [colorstr yellow "Testing unit: [lindex [file split $test] end]"]
|
2022-01-30 04:30:19 -05:00
|
|
|
if {[catch { source $test } err]} {
|
|
|
|
puts "FAILED: caught an error in the test $err"
|
|
|
|
puts $::errorInfo
|
|
|
|
incr ::failed
|
|
|
|
# letting the tests resume, so we'll eventually reach the cleanup and report crashes
|
2022-08-01 03:12:27 -04:00
|
|
|
|
|
|
|
if {$::stop_on_failure} {
|
|
|
|
puts -nonewline "(Test stopped, press enter to resume the tests)"
|
|
|
|
flush stdout
|
|
|
|
gets stdin
|
|
|
|
}
|
2022-01-30 04:30:19 -05:00
|
|
|
}
|
2024-03-21 05:56:59 -04:00
|
|
|
check_leaks {redict sentinel}
|
2021-02-08 10:02:46 -05:00
|
|
|
|
|
|
|
# Check if a leaked fds file was created and abort the test.
|
|
|
|
if {$::leaked_fds_file != "" && [file exists $::leaked_fds_file]} {
|
|
|
|
puts [colorstr red "ERROR: Sentinel has leaked fds to scripts:"]
|
|
|
|
puts [exec cat $::leaked_fds_file]
|
|
|
|
puts "----"
|
|
|
|
incr ::failed
|
|
|
|
}
|
2014-02-17 11:37:56 -05:00
|
|
|
}
|
2022-08-01 03:12:27 -04:00
|
|
|
|
|
|
|
if {$::loop == 0} { break }
|
|
|
|
} ;# while 1
|
2014-02-17 11:37:56 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 08:29:01 -04:00
|
|
|
# Print a message and exists with 0 / 1 according to zero or more failures.
|
|
|
|
proc end_tests {} {
|
2021-01-19 15:57:30 -05:00
|
|
|
if {$::failed == 0 } {
|
2021-10-31 13:22:21 -04:00
|
|
|
puts [colorstr green "GOOD! No errors."]
|
2015-03-30 08:29:01 -04:00
|
|
|
exit 0
|
|
|
|
} else {
|
2021-10-31 13:22:21 -04:00
|
|
|
puts [colorstr red "WARNING $::failed test(s) failed."]
|
2015-03-30 08:29:01 -04:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 05:04:01 -05:00
|
|
|
# The "S" command is used to interact with the N-th Sentinel.
|
|
|
|
# The general form is:
|
|
|
|
#
|
|
|
|
# S <sentinel-id> command arg arg arg ...
|
|
|
|
#
|
|
|
|
# Example to ping the Sentinel 0 (first instance): S 0 PING
|
|
|
|
proc S {n args} {
|
|
|
|
set s [lindex $::sentinel_instances $n]
|
|
|
|
[dict get $s link] {*}$args
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:56:59 -04:00
|
|
|
# Returns a Redict instance by index.
|
2020-09-09 02:35:42 -04:00
|
|
|
# Example:
|
|
|
|
# [Rn 0] info
|
|
|
|
proc Rn {n} {
|
2024-03-21 05:56:59 -04:00
|
|
|
return [dict get [lindex $::redict_instances $n] link]
|
2020-09-09 02:35:42 -04:00
|
|
|
}
|
|
|
|
|
2024-03-21 05:56:59 -04:00
|
|
|
# Like R but to chat with Redict instances.
|
2014-02-18 05:04:01 -05:00
|
|
|
proc R {n args} {
|
2020-09-09 02:35:42 -04:00
|
|
|
[Rn $n] {*}$args
|
2014-02-18 05:04:01 -05:00
|
|
|
}
|
|
|
|
|
2014-02-18 05:38:49 -05:00
|
|
|
proc get_info_field {info field} {
|
|
|
|
set fl [string length $field]
|
|
|
|
append field :
|
|
|
|
foreach line [split $info "\n"] {
|
|
|
|
set line [string trim $line "\r\n "]
|
|
|
|
if {[string range $line 0 $fl] eq $field} {
|
|
|
|
return [string range $line [expr {$fl+1}] end]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc SI {n field} {
|
|
|
|
get_info_field [S $n info] $field
|
|
|
|
}
|
|
|
|
|
|
|
|
proc RI {n field} {
|
|
|
|
get_info_field [R $n info] $field
|
|
|
|
}
|
|
|
|
|
2021-04-01 02:44:44 -04:00
|
|
|
proc RPort {n} {
|
|
|
|
if {$::tls} {
|
|
|
|
return [lindex [R $n config get tls-port] 1]
|
|
|
|
} else {
|
|
|
|
return [lindex [R $n config get port] 1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:56:59 -04:00
|
|
|
# Iterate over IDs of sentinel or redict instances.
|
2014-02-18 10:31:52 -05:00
|
|
|
proc foreach_instance_id {instances idvar code} {
|
2014-02-18 05:04:01 -05:00
|
|
|
upvar 1 $idvar id
|
2014-02-18 10:31:52 -05:00
|
|
|
for {set id 0} {$id < [llength $instances]} {incr id} {
|
|
|
|
set errcode [catch {uplevel 1 $code} result]
|
|
|
|
if {$errcode == 1} {
|
|
|
|
error $result $::errorInfo $::errorCode
|
2014-03-03 07:23:32 -05:00
|
|
|
} elseif {$errcode == 4} {
|
|
|
|
continue
|
2014-03-18 10:06:52 -04:00
|
|
|
} elseif {$errcode == 3} {
|
|
|
|
break
|
2014-02-18 10:31:52 -05:00
|
|
|
} elseif {$errcode != 0} {
|
|
|
|
return -code $errcode $result
|
|
|
|
}
|
2014-02-18 05:04:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 10:31:52 -05:00
|
|
|
proc foreach_sentinel_id {idvar code} {
|
|
|
|
set errcode [catch {uplevel 1 [list foreach_instance_id $::sentinel_instances $idvar $code]} result]
|
|
|
|
return -code $errcode $result
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:56:59 -04:00
|
|
|
proc foreach_redict_id {idvar code} {
|
|
|
|
set errcode [catch {uplevel 1 [list foreach_instance_id $::redict_instances $idvar $code]} result]
|
2014-02-18 10:31:52 -05:00
|
|
|
return -code $errcode $result
|
2014-02-18 05:04:01 -05:00
|
|
|
}
|
|
|
|
|
2014-02-18 05:38:49 -05:00
|
|
|
# Get the specific attribute of the specified instance type, id.
|
|
|
|
proc get_instance_attrib {type id attrib} {
|
|
|
|
dict get [lindex [set ::${type}_instances] $id] $attrib
|
|
|
|
}
|
|
|
|
|
2014-02-22 11:26:30 -05:00
|
|
|
# Set the specific attribute of the specified instance type, id.
|
|
|
|
proc set_instance_attrib {type id attrib newval} {
|
|
|
|
set d [lindex [set ::${type}_instances] $id]
|
|
|
|
dict set d $attrib $newval
|
|
|
|
lset ::${type}_instances $id $d
|
|
|
|
}
|
|
|
|
|
2014-02-18 05:38:49 -05:00
|
|
|
# Create a master-slave cluster of the given number of total instances.
|
|
|
|
# The first instance "0" is the master, all others are configured as
|
|
|
|
# slaves.
|
2024-03-21 05:56:59 -04:00
|
|
|
proc create_redict_master_slave_cluster n {
|
|
|
|
foreach_redict_id id {
|
2014-02-18 05:38:49 -05:00
|
|
|
if {$id == 0} {
|
|
|
|
# Our master.
|
|
|
|
R $id slaveof no one
|
2014-02-22 11:26:30 -05:00
|
|
|
R $id flushall
|
2014-02-18 05:38:49 -05:00
|
|
|
} elseif {$id < $n} {
|
2024-03-21 05:56:59 -04:00
|
|
|
R $id slaveof [get_instance_attrib redict 0 host] \
|
|
|
|
[get_instance_attrib redict 0 port]
|
2014-02-18 05:38:49 -05:00
|
|
|
} else {
|
|
|
|
# Instances not part of the cluster.
|
|
|
|
R $id slaveof no one
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Wait for all the slaves to sync.
|
2014-03-04 05:20:53 -05:00
|
|
|
wait_for_condition 1000 50 {
|
2014-02-18 05:38:49 -05:00
|
|
|
[RI 0 connected_slaves] == ($n-1)
|
|
|
|
} else {
|
|
|
|
fail "Unable to create a master-slaves cluster."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 10:31:52 -05:00
|
|
|
proc get_instance_id_by_port {type port} {
|
|
|
|
foreach_${type}_id id {
|
|
|
|
if {[get_instance_attrib $type $id port] == $port} {
|
|
|
|
return $id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fail "Instance $type port $port not found."
|
|
|
|
}
|
|
|
|
|
2021-10-31 13:22:21 -04:00
|
|
|
# Kill an instance of the specified type/id with SIGKILL.
|
2014-02-22 11:26:30 -05:00
|
|
|
# This function will mark the instance PID as -1 to remember that this instance
|
|
|
|
# is no longer running and will remove its PID from the list of pids that
|
|
|
|
# we kill at cleanup.
|
|
|
|
#
|
|
|
|
# The instance can be restarted with restart-instance.
|
|
|
|
proc kill_instance {type id} {
|
|
|
|
set pid [get_instance_attrib $type $id pid]
|
2015-01-21 10:46:51 -05:00
|
|
|
set port [get_instance_attrib $type $id port]
|
|
|
|
|
2014-02-25 02:23:48 -05:00
|
|
|
if {$pid == -1} {
|
|
|
|
error "You tried to kill $type $id twice."
|
|
|
|
}
|
2015-01-21 10:46:51 -05:00
|
|
|
|
2020-09-06 04:11:49 -04:00
|
|
|
stop_instance $pid
|
2014-02-22 11:26:30 -05:00
|
|
|
set_instance_attrib $type $id pid -1
|
|
|
|
set_instance_attrib $type $id link you_tried_to_talk_with_killed_instance
|
|
|
|
|
|
|
|
# Remove the PID from the list of pids to kill at exit.
|
|
|
|
set ::pids [lsearch -all -inline -not -exact $::pids $pid]
|
2015-01-21 10:46:51 -05:00
|
|
|
|
|
|
|
# Wait for the port it was using to be available again, so that's not
|
|
|
|
# an issue to start a new server ASAP with the same port.
|
2020-08-20 01:59:02 -04:00
|
|
|
set retry 100
|
2015-01-21 10:46:51 -05:00
|
|
|
while {[incr retry -1]} {
|
2020-08-20 01:59:02 -04:00
|
|
|
set port_is_free [catch {set s [socket 127.0.0.1 $port]}]
|
2015-01-21 10:46:51 -05:00
|
|
|
if {$port_is_free} break
|
|
|
|
catch {close $s}
|
2020-08-20 01:59:02 -04:00
|
|
|
after 100
|
2015-01-21 10:46:51 -05:00
|
|
|
}
|
|
|
|
if {$retry == 0} {
|
|
|
|
error "Port $port does not return available after killing instance."
|
|
|
|
}
|
2014-02-22 11:26:30 -05:00
|
|
|
}
|
|
|
|
|
2014-03-18 09:58:27 -04:00
|
|
|
# Return true of the instance of the specified type/id is killed.
|
|
|
|
proc instance_is_killed {type id} {
|
|
|
|
set pid [get_instance_attrib $type $id pid]
|
2014-05-19 05:24:05 -04:00
|
|
|
expr {$pid == -1}
|
2014-03-18 09:58:27 -04:00
|
|
|
}
|
|
|
|
|
2014-02-22 11:26:30 -05:00
|
|
|
# Restart an instance previously killed by kill_instance
|
|
|
|
proc restart_instance {type id} {
|
|
|
|
set dirname "${type}_${id}"
|
|
|
|
set cfgfile [file join $dirname $type.conf]
|
|
|
|
set port [get_instance_attrib $type $id port]
|
|
|
|
|
|
|
|
# Execute the instance with its old setup and append the new pid
|
|
|
|
# file for cleanup.
|
2020-09-06 04:11:49 -04:00
|
|
|
set pid [exec_instance $type $dirname $cfgfile]
|
2014-02-25 02:23:48 -05:00
|
|
|
set_instance_attrib $type $id pid $pid
|
2014-02-22 11:26:30 -05:00
|
|
|
lappend ::pids $pid
|
|
|
|
|
|
|
|
# Check that the instance is running
|
|
|
|
if {[server_is_up 127.0.0.1 $port 100] == 0} {
|
2020-08-20 01:59:02 -04:00
|
|
|
set logfile [file join $dirname log.txt]
|
|
|
|
puts [exec tail $logfile]
|
|
|
|
abort_sentinel_test "Problems starting $type #$id: ping timeout, maybe server start failed, check $logfile"
|
2014-02-22 11:26:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# Connect with it with a fresh link
|
2024-03-21 05:56:59 -04:00
|
|
|
set link [redict 127.0.0.1 $port 0 $::tls]
|
2014-06-18 09:54:55 -04:00
|
|
|
$link reconnect 1
|
|
|
|
set_instance_attrib $type $id link $link
|
2016-01-15 10:50:35 -05:00
|
|
|
|
|
|
|
# Make sure the instance is not loading the dataset when this
|
|
|
|
# function returns.
|
|
|
|
while 1 {
|
|
|
|
catch {[$link ping]} retval
|
|
|
|
if {[string match {*LOADING*} $retval]} {
|
|
|
|
after 100
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-02-22 11:26:30 -05:00
|
|
|
}
|
|
|
|
|
2024-03-21 05:56:59 -04:00
|
|
|
proc redict_deferring_client {type id} {
|
2020-10-06 14:43:30 -04:00
|
|
|
set port [get_instance_attrib $type $id port]
|
|
|
|
set host [get_instance_attrib $type $id host]
|
2024-03-21 05:56:59 -04:00
|
|
|
set client [redict $host $port 1 $::tls]
|
2020-10-06 14:43:30 -04:00
|
|
|
return $client
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:56:59 -04:00
|
|
|
proc redict_deferring_client_by_addr {host port} {
|
|
|
|
set client [redict $host $port 1 $::tls]
|
2022-01-02 19:54:47 -05:00
|
|
|
return $client
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:56:59 -04:00
|
|
|
proc redict_client {type id} {
|
2020-10-06 14:43:30 -04:00
|
|
|
set port [get_instance_attrib $type $id port]
|
|
|
|
set host [get_instance_attrib $type $id host]
|
2024-03-21 05:56:59 -04:00
|
|
|
set client [redict $host $port 0 $::tls]
|
2020-10-06 14:43:30 -04:00
|
|
|
return $client
|
|
|
|
}
|
2022-01-02 19:54:47 -05:00
|
|
|
|
2024-03-21 05:56:59 -04:00
|
|
|
proc redict_client_by_addr {host port} {
|
|
|
|
set client [redict $host $port 0 $::tls]
|
2022-01-02 19:54:47 -05:00
|
|
|
return $client
|
2022-05-25 11:25:38 -04:00
|
|
|
}
|