redict/tests/unit/moduleapi
Meir Shpilraien (Spielrein) 508a138885
Fix replication inconsistency on modules that uses key space notifications (#10969)
Fix replication inconsistency on modules that uses key space notifications.

### The Problem

In general, key space notifications are invoked after the command logic was
executed (this is not always the case, we will discuss later about specific
command that do not follow this rules). For example, the `set x 1` will trigger
a `set` notification that will be invoked after the `set` logic was performed, so
if the notification logic will try to fetch `x`, it will see the new data that was written.
Consider the scenario on which the notification logic performs some write
commands. for example, the notification logic increase some counter,
`incr x{counter}`, indicating how many times `x` was changed.
The logical order by which the logic was executed is has follow:

```
set x 1
incr x{counter}
```

The issue is that the `set x 1` command is added to the replication buffer
at the end of the command invocation (specifically after the key space
notification logic was invoked and performed the `incr` command).
The replication/aof sees the commands in the wrong order:

```
incr x{counter}
set x 1
```

In this specific example the order is less important.
But if, for example, the notification would have deleted `x` then we would
end up with primary-replica inconsistency.

### The Solution

Put the command that cause the notification in its rightful place. In the
above example, the `set x 1` command logic was executed before the
notification logic, so it should be added to the replication buffer before
the commands that is invoked by the notification logic. To achieve this,
without a major code refactoring, we save a placeholder in the replication
buffer, when finishing invoking the command logic we check if the command
need to be replicated, and if it does, we use the placeholder to add it to the
replication buffer instead of appending it to the end.

To be efficient and not allocating memory on each command to save the
placeholder, the replication buffer array was modified to reuse memory
(instead of allocating it each time we want to replicate commands).
Also, to avoid saving a placeholder when not needed, we do it only for
WRITE or MAY_REPLICATE commands.

#### Additional Fixes

* Expire and Eviction notifications:
  * Expire/Eviction logical order was to first perform the Expire/Eviction
    and then the notification logic. The replication buffer got this in the
    other way around (first notification effect and then the `del` command).
    The PR fixes this issue.
  * The notification effect and the `del` command was not wrap with
    `multi-exec` (if needed). The PR also fix this issue.
* SPOP command:
  * On spop, the `spop` notification was fired before the command logic
    was executed. The change in this PR would have cause the replication
    order to be change (first `spop` command and then notification `logic`)
    although the logical order is first the notification logic and then the
    `spop` logic. The right fix would have been to move the notification to
    be fired after the command was executed (like all the other commands),
    but this can be considered a breaking change. To overcome this, the PR
    keeps the current behavior and changes the `spop` code to keep the right
    logical order when pushing commands to the replication buffer. Another PR
    will follow to fix the SPOP properly and match it to the other command (we
    split it to 2 separate PR's so it will be easy to cherry-pick this PR to 7.0 if
    we chose to).

#### Unhanded Known Limitations

* key miss event:
  * On key miss event, if a module performed some write command on the
    event (using `RM_Call`), the `dirty` counter would increase and the read
    command that cause the key miss event would be replicated to the replication
    and aof. This problem can also happened on a write command that open
    some keys but eventually decides not to perform any action. We decided
    not to handle this problem on this PR because the solution is complex
    and will cause additional risks in case we will want to cherry-pick this PR.
    We should decide if we want to handle it in future PR's. For now, modules
    writers is advice not to perform any write commands on key miss event.

#### Testing

* We already have tests to cover cases where a notification is invoking write
  commands that are also added to the replication buffer, the tests was modified
  to verify that the replica gets the command in the correct logical order.
* Test was added to verify that `spop` behavior was kept unchanged.
* Test was added to verify key miss event behave as expected.
* Test was added to verify the changes do not break lazy expiration.

#### Additional Changes

* `propagateNow` function can accept a special dbid, -1, indicating not
  to replicate `select`. We use this to replicate `multi/exec` on `propagatePendingCommands`
  function. The side effect of this change is that now the `select` command
  will appear inside the `multi/exec` block on the replication stream (instead of
  outside of the `multi/exec` block). Tests was modified to match this new behavior.
2022-08-18 10:16:32 +03:00
..
aclcheck.tcl Allow specifying ACL reason for module log entry (#10559) 2022-04-11 22:16:17 -07:00
auth.tcl Fix module redact test for valgrind (#10432) 2022-03-16 08:53:57 +02:00
basics.tcl Fix incorrect error code for eval scripts and fix test error checking (#10575) 2022-04-14 11:18:32 +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 XREADGROUP: Unblock client if stream is deleted (#10306) 2022-03-08 17:10:36 +02:00
cluster.tcl Fix timing issue in cluster test (#11008) 2022-07-18 20:35:13 -07:00
cmdintrospection.tcl COMMAND DOCS shows module name, where applicable (#10544) 2022-04-10 11:41:31 +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 Handle key-spec flags with modules (#10237) 2022-02-08 10:01:35 +02: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 Modules: Add REDISMODULE_EVENT_CONFIG (#10311) 2022-03-07 17:37:57 +02:00
infotest.tcl Make INFO command variadic (#6891) 2022-02-08 13:14:42 +02:00
infra.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00
keyspace_events.tcl Test: RM_Call from within "expired" notification (#10613) 2022-04-25 13:05:06 +03:00
keyspecs.tcl Fixed SET and BITFIELD commands being wrongly marked movablekeys (#10837) 2022-06-12 08:22:18 +03:00
list.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00
mallocsize.tcl Add RM_MallocSizeString, RM_MallocSizeDict (#10542) 2022-04-17 08:31:57 +03:00
misc.tcl Support conversion between RedisModuleString and unsigned long long (#10889) 2022-06-26 15:02:52 +03:00
moduleconfigs.tcl Bug fixes for enum configs with overlapping bit flags (module API) (#10661) 2022-05-09 13:36:53 +03:00
propagate.tcl Fix replication inconsistency on modules that uses key space notifications (#10969) 2022-08-18 10:16:32 +03:00
publish.tcl Sharded pubsub publish messagebulk as smessage (#10792) 2022-05-31 08:03:59 +03:00
reply.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +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 Fix duplicate module options define (#10284) 2022-02-11 20:15:52 +02:00
timer.tcl forbid module to unload when it holds ongoing timer (#10187) 2022-02-01 14:54:11 +02: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