redict/tests/unit/moduleapi
Meir Shpilraien (Spielrein) abc345ad28
Module API to allow writes after key space notification hooks (#11199)
### Summary of API additions

* `RedisModule_AddPostNotificationJob` - new API to call inside a key space
  notification (and on more locations in the future) and allow to add a post job as describe above.
* New module option, `REDISMODULE_OPTIONS_ALLOW_NESTED_KEYSPACE_NOTIFICATIONS`,
  allows to disable Redis protection of nested key-space notifications.
* `RedisModule_GetModuleOptionsAll` - gets the mask of all supported module options so a module
  will be able to check if a given option is supported by the current running Redis instance.

### Background

The following PR is a proposal of handling write operations inside module key space notifications.
After a lot of discussions we came to a conclusion that module should not perform any write
operations on key space notification.

Some examples of issues that such write operation can cause are describe on the following links:

* Bad replication oreder - https://github.com/redis/redis/pull/10969
* Used after free - https://github.com/redis/redis/pull/10969#issuecomment-1223771006
* Used after free - https://github.com/redis/redis/pull/9406#issuecomment-1221684054

There are probably more issues that are yet to be discovered. The underline problem with writing
inside key space notification is that the notification runs synchronously, this means that the notification
code will be executed in the middle on Redis logic (commands logic, eviction, expire).
Redis **do not assume** that the data might change while running the logic and such changes
can crash Redis or cause unexpected behaviour.

The solution is to state that modules **should not** perform any write command inside key space
notification (we can chose whether or not we want to force it). To still cover the use-case where
module wants to perform a write operation as a reaction to key space notifications, we introduce
a new API , `RedisModule_AddPostNotificationJob`, that allows to register a callback that will be
called by Redis when the following conditions hold:

* It is safe to perform any write operation.
* The job will be called atomically along side the operation that triggers it (in our case, key
  space notification).

Module can use this new API to safely perform any write operation and still achieve atomicity
between the notification and the write.

Although currently the API is supported on key space notifications, the API is written in a generic
way so that in the future we will be able to use it on other places (server events for example).

### Technical Details

Whenever a module uses `RedisModule_AddPostNotificationJob` the callback is added to a list
of callbacks (called `modulePostExecUnitJobs`) that need to be invoke after the current execution
unit ends (whether its a command, eviction, or active expire). In order to trigger those callback
atomically with the notification effect, we call those callbacks on `postExecutionUnitOperations`
(which was `propagatePendingCommands` before this PR). The new function fires the post jobs
and then calls `propagatePendingCommands`.

If the callback perform more operations that triggers more key space notifications. Those keys
space notifications might register more callbacks. Those callbacks will be added to the end
of `modulePostExecUnitJobs` list and will be invoke atomically after the current callback ends.
This raises a concerns of entering an infinite loops, we consider infinite loops as a logical bug
that need to be fixed in the module, an attempt to protect against infinite loops by halting the
execution could result in violation of the feature correctness and so **Redis will make no attempt
to protect the module from infinite loops**

In addition, currently key space notifications are not nested. Some modules might want to allow
nesting key-space notifications. To allow that and keep backward compatibility, we introduce a
new module option called `REDISMODULE_OPTIONS_ALLOW_NESTED_KEYSPACE_NOTIFICATIONS`.
Setting this option will disable the Redis key-space notifications nesting protection and will
pass this responsibility to the module.

### Redis infrastructure

This PR promotes the existing `propagatePendingCommands` to an "Execution Unit" concept,
which is called after each atomic unit of execution,

Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
Co-authored-by: Madelyn Olson <34459052+madolson@users.noreply.github.com>
2022-11-24 19:00:04 +02:00
..
aclcheck.tcl Unify ACL failure error messaging. (#11160) 2022-10-16 09:01:37 +03:00
auth.tcl Fix module redact test for valgrind (#10432) 2022-03-16 08:53:57 +02:00
basics.tcl Fix crash on RM_Call inside module load (#11346) 2022-10-12 13:09:51 +03:00
blockedclient.tcl Fix crash on RM_Call with script mode. (#10886) 2022-06-21 10:01:13 +03:00
blockonbackground.tcl Changed latency histogram output to omit trailing 0s and periods (#10075) 2022-01-09 17:04:18 -08:00
blockonkeys.tcl Blocked module clients should be aware when a key is deleted (#11310) 2022-10-18 19:50:02 +03:00
cluster.tcl Fix wrong replication on cluster slotmap changes with module KSN propagation (#11377) 2022-10-16 08:30:01 +03:00
cmdintrospection.tcl Fix memory leak in moduleFreeCommand (#11147) 2022-08-18 12:36:01 +03:00
commandfilter.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00
datatype2.tcl Tests: don't rely on the response of MEMORY USAGE when mem_allocator is not jemalloc (#10010) 2021-12-27 21:37:21 +02:00
datatype.tcl Adding module api for processing commands during busy jobs and allow flagging the commands that should be handled at this status (#9963) 2022-01-20 09:05:53 +02:00
defrag.tcl Tests: fix new defrag test to be skipped when not supported (#8185) 2020-12-14 11:13:46 +02:00
eventloop.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00
fork.tcl Fix race in module fork kill test (#10717) 2022-05-12 20:10:38 +03:00
getchannels.tcl Implemented module getchannels api and renamed channel keyspec (#10299) 2022-02-22 11:00:03 +02:00
getkeys.tcl Unify ACL failure error messaging. (#11160) 2022-10-16 09:01:37 +03:00
hash.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00
hooks.tcl Module CLIENT_CHANGE, Fix crash on free blocked client with DB!=0 (#11500) 2022-11-14 14:40:35 -08:00
infotest.tcl Fix missing sections for INFO ALL with module (#11291) 2022-09-21 08:10:03 +03:00
infra.tcl Build TLS as a loadable module 2022-08-23 12:37:56 +03:00
keyspace_events.tcl Module API to allow writes after key space notification hooks (#11199) 2022-11-24 19:00:04 +02:00
keyspecs.tcl Unify ACL failure error messaging. (#11160) 2022-10-16 09:01:37 +03:00
list.tcl Add listpack encoding for list (#11303) 2022-11-16 20:29:46 +02:00
mallocsize.tcl Add RM_MallocSizeString, RM_MallocSizeDict (#10542) 2022-04-17 08:31:57 +03:00
misc.tcl RM_Call - only enforce OOM on scripts if 'M' flag is sent (#11425) 2022-10-27 09:29:43 +03:00
moduleconfigs.tcl Build TLS as a loadable module 2022-08-23 12:37:56 +03:00
postnotifications.tcl Module API to allow writes after key space notification hooks (#11199) 2022-11-24 19:00:04 +02:00
propagate.tcl Reverts most of the changes of #10969 (#11178) 2022-08-24 12:51:36 +03:00
publish.tcl Sharded pubsub publish messagebulk as smessage (#10792) 2022-05-31 08:03:59 +03:00
reply.tcl Fix double negative nan test, ignoring sign (#11506) 2022-11-15 17:18:21 +02:00
scan.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00
stream.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00
subcommands.tcl Fix regression not aborting transaction on error, and re-edit some error responses (#10612) 2022-04-25 13:08:13 +03:00
test_lazyfree.tcl Sort out mess around propagation and MULTI/EXEC (#9890) 2021-12-23 00:03:48 +02:00
testrdb.tcl Avoid saving module aux on RDB if no aux data was saved by the module. (#11374) 2022-10-18 19:45:46 +03:00
timer.tcl forbid module to unload when it holds ongoing timer (#10187) 2022-02-01 14:54:11 +02:00
usercall.tcl Unify ACL failure error messaging. (#11160) 2022-10-16 09:01:37 +03:00
zset.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00