1. 06 Feb, 2024 3 commits
    • Binbin's avatar
      Re-compute active_defrag_running after adjusting defrag configurations (#13020) · 13bd3643
      Binbin authored
      Currently, once active defrag starts, we can not adjust
      active_defrag_running
      downwards. This is because active_defrag_running will be dynamically
      compute
      based on the fragmentation, we think we should not lower the effort when
      the
      fragmentation drops.
      
      However, we need to note that active_defrag_running will also be
      dynamically
      computed based on configurations. In this case, we are not respecting
      cycle-min
      or cycle-max. Some people may realize halfway through that defrag
      consumes a
      lot and want to adjust it.
      
      Previously we could only turn off activedefrag and then turn it on again
      to
      adjust active_defrag_running downwards. So in this PR, when a active
      defrag
      configuration change is made, we will re-compute it.
      
      These configuration items are:
      - active-defrag-cycle-min
      - active-defrag-cycle-max
      - active-defrag-threshold-upper
      13bd3643
    • Binbin's avatar
      Minor optimization for expire dict in defragKey (#13027) · 87eaf119
      Binbin authored
      Since now a DB in cluster mode is divided into 16384 dicts, here
      we directly check kvstoreDictSize instead of kvstoreSize, which
      may have a higher probability that we can save the lookup.
      
      The other change is a cleanup, obviously kvstoreGetHash should be
      applied to the db->expires dicts.
      87eaf119
    • Binbin's avatar
      Fix kvstore unable to push resize_cursor for resize when dict is NULL (#13031) · 84fd745d
      Binbin authored
      
      
      When the dict is NULL, we also need to push resize_cursor, otherwise it
      will keep doing useless continue here, and there is no way to resize the
      other dict behind it.
      
      Introduced in #12822.
      
      ---------
      Co-authored-by: default avatarOran Agra <oran@redislabs.com>
      84fd745d
  2. 05 Feb, 2024 2 commits
    • guybe7's avatar
      Refactor the per-slot dict-array db.c into a new kvstore data structure (#12822) · 8cd62f82
      guybe7 authored
      # Description
      Gather most of the scattered `redisDb`-related code from the per-slot
      dict PR (#11695) and turn it to a new data structure, `kvstore`. i.e.
      it's a class that represents an array of dictionaries.
      
      # Motivation
      The main motivation is code cleanliness, the idea of using an array of
      dictionaries is very well-suited to becoming a self-contained data
      structure.
      This allowed cleaning some ugly code, among others: loops that run twice
      on the main dict and expires dict, and duplicate code for allocating and
      releasing this data structure.
      
      # Notes
      1. This PR reverts the part of https://github.com/redis/redis/pull/12848
      where the `rehashing` list is global (handling rehashing `dict`s is
      under the responsibility of `kvstore`, and should not be managed by the
      server)
      2. This PR also replaces the type of `server.pubsubshard_channels` from
      `dict**` to `kvstore` (original PR:
      https://github.com/redis/redis/pull/12804). After that was done,
      server.pubsub_channels was also chosen to be a `kvstore` (with only one
      `dict`, which seems odd) just to make the code cleaner by making it the
      same type as `server.pubsubshard_channels`, see
      `pubsubtype.serverPubSubChannels`
      3. the keys and expires kvstores are currenlty configured to allocate
      the individual dicts only when the first key is added (unlike before, in
      which they allocated them in advance), but they won't release them when
      the last key is deleted.
      
      Worth mentioning that due to the recent change the reply of DEBUG
      HTSTATS changed, in case no keys were ever added to the db.
      
      before:
      ```
      127.0.0.1:6379> DEBUG htstats 9
      [Dictionary HT]
      Hash table 0 stats (main hash table):
      No stats available for empty dictionaries
      [Expires HT]
      Hash table 0 stats (main hash table):
      No stats available for empty dictionaries
      ```
      
      after:
      ```
      127.0.0.1:6379> DEBUG htstats 9
      [Dictionary HT]
      [Expires HT]
      ```
      8cd62f82
    • Binbin's avatar
      Fix active expire timeout when db done the scanning (#13030) · f20774ec
      Binbin authored
      When db->expires_cursor==0, it means the DB is done the scanning,
      we should exit the loop to avoid the useless scanning.
      
      It is easy to see the active expire timeout in the modified test,
      for example, let's assume that there is only 1 expired key in the
      DB, and the size / buckets ratio is less than 1%, which means that
      we will skip it in isExpiryDictValidForSamplingCb, and the return
      value of expires_cursor is 0.
      
      Because `data.sampled == 0` is always true, so `repeat` is also
      always true, we will keep scanning the DB, but every time it is
      skipped by the previous judgment (expires_cursor = 0), until the
      timelimit is finally exhausted.
      f20774ec
  3. 04 Feb, 2024 2 commits
    • Daz's avatar
      Add missing structural API changes to JSON file (#12434) · 02a87885
      Daz authored
      
      
      The JSON file lacks the following structural API changes:
      
      - GEORADIUSBYMEMBER: add the ANY option for COUNT since 6.2.0.
      - GEORADIUSBYMEMBER_RO: add the ANY option for COUNT since 6.2.0.
      - GEORADIUS_RO: Added support for uppercase unit names since 7.0.0.
      - GEORADIUSBYMEMBER_RO: Added support for uppercase unit names since
      7.0.0.
      
      ---------
      Signed-off-by: default avatardaz-3ux <daz-3ux@proton.me>
      Co-authored-by: default avatarbodong.ybd <bodong.ybd@alibaba-inc.com>
      Co-authored-by: default avatarViktor Söderqvist <viktor.soderqvist@est.tech>
      Co-authored-by: default avataryangpengda.333 <yangpengda.333@bytedance.com>
      Co-authored-by: default avatarOran Agra <oran@redislabs.com>
      02a87885
    • Yanqi Lv's avatar
      Make db->avg_ttl more precise (#12949) · c1041c2c
      Yanqi Lv authored
      
      
      Currently, We compute `db->avg_ttl` after each short `dbScan` sweep (a
      few buckets without checking the time limit). But after each `dbScan`
      sweep, we don't have much data and this makes the db->avg_ttl less
      precise. For example, even if we scan the whole db, we can't get the
      exact avg_ttl because we separate the data.
      i.e. because of the running average, if we issue 16 calls to scan, we'll
      give lower weight to the first one, and higher weight to the last one.
      I think we should calculate `db->avg_ttl` until completing more of the
      db iteration (judgement of time limit or the beginning of iterating next
      db) because we have more sample data in this db and can get more
      accurate result. In the best case, if we scan the whole db, we can get
      the exact avg_ttl.
      
      In this PR, we postpone the avg_ttl calculation until the judgement of
      time limit or iteration of next db, so we can accumulate more data to
      get more precise avg_ttl.
      Note that we still need to make sure to decay the old TTLs at the same
      speed as before, which is why we want to run the decay mechanism several
      times, or use the Pow formula, see the comment in the code.
      
      In my experiment, this PR can improve 89% or 52% accuracy in different
      workload.
      Co-authored-by: default avatarOran Agra <oran@redislabs.com>
      c1041c2c
  4. 01 Feb, 2024 1 commit
    • Yanqi Lv's avatar
      Refine the purpose of rdb saving with accurate flags (#12925) · 62153b3b
      Yanqi Lv authored
      In Redis, rdb is produced in three scenarios mainly.
      
      - backup, such as `bgsave` and `save` command
      - full sync in replication
      - aof rewrite if `aof-use-rdb-preamble` is yes
      
      We also have some RDB flags to identify the purpose of rdb saving.
      ```C
      /* flags on the purpose of rdb save or load */
      #define RDBFLAGS_NONE 0                 /* No special RDB loading. */
      #define RDBFLAGS_AOF_PREAMBLE (1<<0)    /* Load/save the RDB as AOF preamble. */
      #define RDBFLAGS_REPLICATION (1<<1)     /* Load/save for SYNC. */
      ```
      
      But currently, it seems that these flags and purposes of rdb saving
      don't exactly match. I find it in `rdbSaveRioWithEOFMark` which calls
      `startSaving` with `RDBFLAGS_REPLICATION` but `rdbSaveRio` with
      `RDBFLAGS_NONE`.
      ```C
      int rdbSaveRioWithEOFMark(int req, rio *rdb, int *error, rdbSaveInfo *rsi) {
          char eofmark[RDB_EOF_MARK_SIZE];
      
          startSaving(RDBFLAGS_REPLICATION);
          getRandomHexChars(eofmark,RDB_EOF_MARK_SIZE);
          if (error) *error = 0;
          if (rioWrite(rdb,"$EOF:",5) == 0) goto werr;
          if (rioWrite(rdb,eofmark,RDB_EOF_MARK_SIZE) == 0) goto werr;
          if (rioWrite(rdb,"\r\n",2) == 0) goto werr;
          if (rdbSaveRio(req,rdb,error,RDBFLAGS_NONE,rsi) == C_ERR) goto werr;
          if (rioWrite(rdb,eofmark,RDB_EOF_MARK_SIZE) == 0) goto werr;
          stopSaving(1);
          return C_OK;
      
      werr: /* Write error. */
          /* Set 'error' only if not already set by rdbSaveRio() call. */
          if (error && *error == 0) *error = errno;
          stopSaving(0);
          return C_ERR;
      }
      ```
      
      In this PR, I refine the purpose of rdb saving with accurate flags.
      62153b3b
  5. 31 Jan, 2024 3 commits
    • Binbin's avatar
      Fix dict resize allow test (#13016) · 9a7d3118
      Binbin authored
      Ci report this failure:
      ```
      *** [err]: Don't rehash if used memory exceeds maxmemory after rehash in tests/unit/maxmemory.tcl
      Expected '4098' to equal or match '4002'
      
      WARNING: the new maxmemory value set via CONFIG SET (1176088) is smaller than the current memory usage (1231083)
      ```
      
      It can be seen from the log that used_memory changed before we set
      maxmemory.
      The reason is that in #12819, in cron, in addition to trying to shrink,
      we will
      also tyring to expand. The dict was expanded by cron before we set
      maxmemory,
      causing the test to fail.
      
      Before setting maxmemory, we only add 4095 keys to avoid triggering
      resize.
      9a7d3118
    • Binbin's avatar
      Fix module assertion crash when timer and timeout are unlocked in the same event loop (#13015) · 6016973a
      Binbin authored
      When we use a timer to unblock a client in module, if the timer
      period and the block timeout are very close, they will unblock the
      client in the same event loop, and it will trigger the assertion.
      The reason is that in moduleBlockedClientTimedOut we will protect
      against re-processing, so we don't actually call updateStatsOnUnblock
      (see #12817), so we are not able to reset the c->duration. 
      
      The reason is unblockClientOnTimeout() didn't realize that bc had
      been unblocked. We add a function to the module to determine if bc
      is blocked, and then use it in unblockClientOnTimeout() to exit.
      
      There is the stack:
      ```
      beforeSleep
      blockedBeforeSleep
      handleBlockedClientsTimeout
      checkBlockedClientTimeout
      unblockClientOnTimeout
      unblockClient
      resetClient
      -- assertion, crash the server
      'c->duration == 0' is not true
      ```
      6016973a
    • Binbin's avatar
      Fix module unblock crash due to no timeout_callback (#13017) · 74a6e48a
      Binbin authored
      The block timeout is passed in the test case, but we do not pass
      in the timeout_callback, and it will crash when unlocking. In this
      case, in moduleBlockedClientTimedOut we will check timeout_callback.
      There is the stack:
      ```
      beforeSleep
      blockedBeforeSleep
      handleBlockedClientsTimeout
      checkBlockedClientTimeout
      unblockClientOnTimeout
      replyToBlockedClientTimedOut
      moduleBlockedClientTimedOut
      -- timeout_callback is NULL, invalidFunctionWasCalled
      bc->timeout_callback(&ctx,(void**)c->argv,c->argc);
      ```
      74a6e48a
  6. 30 Jan, 2024 6 commits
    • Chen Tianjie's avatar
      Add novalues option to command HSCAN. (#12765) · f469dd8c
      Chen Tianjie authored
      
      
      Add a way to HSCAN a hash key, and get only the filed names.
      Command syntax is now:
      ```
      HSCAN key cursor [MATCH pattern] [COUNT count] [NOVALUES]
      ```
      when `NOVALUES` is on, the command will only return keys in the hash.
      
      ---------
      Co-authored-by: default avatarViktor Söderqvist <viktor.soderqvist@est.tech>
      f469dd8c
    • Slava Koyfman's avatar
      Implement `CLIENT KILL MAXAGE <maxage>` (#12299) · 24f6d08b
      Slava Koyfman authored
      
      
      Adds an ability to kill clients older than a specified age.
      
      Also, fixed the age calculation in `catClientInfoString` to use
      `commandTimeSnapshot`
      instead of the old `server.unixtime`, and added missing documentation
      for
      `CLIENT KILL ID` to output of `CLIENT help`.
      
      ---------
      Co-authored-by: default avatarOran Agra <oran@redislabs.com>
      24f6d08b
    • Oran Agra's avatar
      fix dict rehash tests introduced by #12802 broken by #12819 (#13009) · 7c9f41b5
      Oran Agra authored
      tests consistently fail on timeout (sleep that's too short).
      it now takes more time because in #12819 we iterate on all dicts, not
      just non-empty ones.
      it passed the PR's CI because it skips the `slow` tag, which might have
      been misplaced, but now it is probably required.
      with the fix, the tests take quite a lot of time:
      ```
      [ok]: Redis can trigger resizing (1860 ms)
      [ok]: Redis can rewind and trigger smaller slot resizing (744 ms)
      ```
      before #12819:
      ```
      [ok]: Redis can trigger resizing (309 ms)
      [ok]: Redis can rewind and trigger smaller slot resizing (295 ms)
      ```
      
      failure:
      https://github.com/redis/redis/actions/runs/7704158180/job/20995931735
      ```
      *** [err]: expire scan should skip dictionaries with lot's of empty buckets in tests/unit/expire.tcl
      scan didn't handle slot skipping logic.
      *** [err]: Redis can trigger resizing in tests/unit/other.tcl
      Expected '[Dictionary HT]
      Hash table 0 stats (main hash table):
       table size: 128
       number of elements: 5
      [Expires HT]
      Hash table 0 stats (main hash table):
      No stats available for empty dictionaries
      ' to match '*table size: 8*' (context: type eval line 29 cmd {assert_match "*table size: 8*" [r debug HTSTATS 0]} proc ::test) 
      *** [err]: Redis can rewind and trigger smaller slot resizing in tests/unit/other.tcl
      Expected '[Dictionary HT]
      Hash table 0 stats (main hash table):
       table size: 256
       number of elements: 10
      [Expires HT]
      Hash table 0 stats (main hash table):
      No stats available for empty dictionaries
      ' to match '*table size: 16*' (context: type eval line 27 cmd {assert_match "*table size: 16*" [r debug HTSTATS 0]} proc ::test) 
      ```
      7c9f41b5
    • Binbin's avatar
      Fix timeout not being set in module blockClient case (#13011) · 45a35a79
      Binbin authored
      This was introduced in #13004, missing this assignment.
      It causes timeout to be a random value (may be less than now),
      and then in `Unblock by timer` test, the client is unblocked
      and then it call timeout_callback, since the callback is NULL,
      the server will crash.
      
      The crash stack is:
      ```
      beforesleep
      handleBlockedClientsTimeout
      checkBlockedClientTimeout
      unblockClientOnTimeout
      replyToBlockedClientTimedOut
      moduleBlockedClientTimedOut
      -- the timeout_callback is NULL, invalidFunctionWasCalled
      bc->timeout_callback(&ctx,(void**)c->argv,c->argc);
      ```
      45a35a79
    • Binbin's avatar
      Adds connection timeout option to redis-cli (#10609) · 76adbf6f
      Binbin authored
      
      
      This allows specifying the timeout value for opening the TCP
      connection to a server. The timeout, default 0 means no limit,
      depending on the OS. It can be specified using the new `-t` switch.
      
      revive #3764, fixes #3763
      
      ---------
      Co-authored-by: default avatarItamar Haber <itamar@redislabs.com>
      Co-authored-by: default avataryoav-steinberg <yoav@redislabs.com>
      76adbf6f
    • Binbin's avatar
      Fix blocking commands timeout is reset due to re-processing command (#13004) · 492021db
      Binbin authored
      In #11012, we will reprocess command when client is unblocked on keys,
      in some blocking commands, for example, in the XREADGROUP BLOCK
      scenario,
      because of the re-processing command, we will recalculate the block
      timeout,
      causing the blocking time to be reset.
      
      This commit add a new CLIENT_REPROCESSING_COMMAND clent flag, explicitly
      let the command know that it is being re-processed, later in
      blockForKeys
      we will not reset the timeout.
      
      Affected BLOCK cases: 
      - list / zset / stream, added test cases for each.
      
      Unaffected cases:
      - module (never re-process the commands).
      - WAIT / WAITAOF (never re-process the commands).
      
      Fixes #12998.
      492021db
  7. 29 Jan, 2024 3 commits
    • Chen Tianjie's avatar
      Optimize resizing hash table to resize not only non-empty dicts. (#12819) · af7ceeb7
      Chen Tianjie authored
      The function `tryResizeHashTables` only attempts to shrink the dicts
      that has keys (change from #11695), this was a serious problem until the
      change in #12850 since it meant if all keys are deleted, we won't shrink
      the dick.
      But still, both dictShrink and dictExpand may be blocked by a fork child
      process, therefore, the cron job needs to perform both dictShrink and
      dictExpand, for not just non-empty dicts, but all dicts in DBs.
      
      What this PR does:
      
      1. Try to resize all dicts in DBs (not just non-empty ones, as it was
      since #12850)
      2. handle both shrink and expand (not just shrink, as it was since
      forever)
      3. Refactor some APIs about dict resizing (get rid of `htNeedsShrink`
      `htNeedsShrink` `dictShrinkToFit`, and expose `dictShrinkIfNeeded`
      `dictExpandIfNeeded` which already contains all the code of those
      functions we get rid of, to make APIs more neat)
      4. In the `Don't rehash if redis has child process` test, now that cron
      would do resizing, we no longer need to write to DB after the child
      process got killed, and can wait for the cron to expand the hash table.
      af7ceeb7
    • Ozan Tezcan's avatar
      Add RM_TryCalloc() and RM_TryRealloc() (#12985) · c5273cae
      Ozan Tezcan authored
      Modules may want to handle allocation failures gracefully. Adding
      RM_TryCalloc() and RM_TryRealloc() for it.
      RM_TryAlloc() was added before:
      https://github.com/redis/redis/pull/10541
      c5273cae
    • Binbin's avatar
      Fix maxmemory-samples stack overflow crash in evictionPoolPopulate, limit its... · acd96052
      Binbin authored
      Fix maxmemory-samples stack overflow crash in evictionPoolPopulate, limit its value to [1,64] (#13000)
      
      We have not limited the value of maxmemory-samples in the past, it can
      be set very large. If it is set very large, we will have stack overflow
      in evictionPoolPopulate when we trigger the key eviction.
      
      There is no reason for this config to be set too high, so just limit its
      range to [1,64].
      acd96052
  8. 27 Jan, 2024 1 commit
    • Roshan Khatri's avatar
      Reduce performance impact of dict rehashing and make it shorter. (#12899) · 5358bd7c
      Roshan Khatri authored
      
      
      #### Problem Statement:
      For any read/update operation during rehashing, we're doing ~10+ random
      DRAM lookups to do the rehashing, as we are using the `rehashidx` to
      rehash 10 buckets, whose dict entries most likely aren't cached in the
      CPU or near the bucket we are operating on. If these random bucket are
      empty, the rehashing process during that command execution is skipped.
      
      #### Implementation:
      For reducing the performance recession while dict is rehashing, we
      determine the index at which the key would be stored in the 0th HT, we
      check if that index has already been rehashed, if not we will rehash the
      bucket containing the key and the bucket will be moved from 0th HT to
      the 1st HT.
      
      If the key has already been rehashed, we perform the random access
      bucket rehash (using `rehashidx`) and we again verify if rehashing is
      still ongoing and look up the key in the respective HT.
      
      This ensures rehashing is not skipped in any command call and that we
      rehash a particular bucket or random bucket in each call.
      
      #### Changes in this PR:
      - Added a new method `dictBucketRehash` to perform rehash on a single
      bucket.
      - Helper function `moveKeysInBucketOldtoNew` for `dictRehash` and
      `dictBucketRehash` to move all the keys in a bucket from the old to the
      new hash HT.
      - Helper function `verifyMoreRehashRequired` for `dictRehash` and
      `dictBucketRehash` to check if we have already rehashed the whole table
      and if more rehashing is required.
      
      ### Benchmark:
      - This PR still shows **~13%** improvement in the latency during
      rehashing.
      
      - Rehashing is now **~2%** faster for this PR when compared to unstable.
      
      ---------
      Co-authored-by: default avatarOran Agra <oran@redislabs.com>
      Co-authored-by: default avatarMadelyn Olson <34459052+madolson@users.noreply.github.com>
      5358bd7c
  9. 26 Jan, 2024 1 commit
  10. 25 Jan, 2024 2 commits
    • Binbin's avatar
      Fix unauthenticated client query buffer 1MB limit (#12989) · 4cb5ad85
      Binbin authored
      Code incorrectly set the limit value to 1024MB.
      Introduced in #12961.
      4cb5ad85
    • zhaozhao.zz's avatar
      Revert multi OOM limit and add multi buffer limit (#12961) · 85a834bf
      zhaozhao.zz authored
      Fix #9926 , and introduce an alternative method to prevent abuse of
      transactions:
      
      1. revert #5454 (which was blocking read-only transactions in OOM
      state), and break the tie of MULTI state memory usage and the server OOM
      state. Meaning that we'll limit the total memory a single client can
      queue, and do that unconditionally regardless of the server being OOM or
      not.
      2. to prevent abuse of transactions, we use the
      `client-query-buffer-limit` to restrict the size of the transaction.
      Because the commands cached in the MULTI/EXEC queue have not been
      executed yet, so they are also considered a part of the "query buffer"
      in a broader sense. In other words, the commands in the MULTI queue and
      the `querybuf` of the client together constitute the "query buffer".
      When they exceed the limit, the connection will be disconnected.
      
      The reasoning is that it's sensible to sends a single command with a
      huge (1GB) argument, and it's sensible to sends a transaction with many
      small commands, but it's probably not common to sends a long transaction
      with many huge arguments (will consume a lot of memory before even being
      executed).
      
      If anyone runs into that, they can simply increase the
      `client-query-buffer-limit` config.
      
      P.S. To prevent DDoS attacks, unauthenticated clients have a separate
      hard limit. Their query buffer should not exceed a maximum of 1MB. In
      other words, if the query buffer of an unauthenticated client exceeds
      1MB or the `client-query-buffer-limit` (if it is set to a value smaller
      than 1MB,), the connection will be disconnected.
      85a834bf
  11. 23 Jan, 2024 7 commits
  12. 22 Jan, 2024 2 commits
    • Brennan's avatar
      Prevent nodes with invalid IDs from being propagated through gossip (#12921) · e12f2dec
      Brennan authored
      
      
      There have been occasional instances of memory corruption (though code bugs or bit flips) leading to invalid node information being gossiped around. To prevent this invalid information spreading, we verify the node IDs in received gossip are in an acceptable format, and disregard any gossiped nodes with invalid IDs. This PR uses the existing verifyClusterNodeId function to check the validity of the gossiped node IDs and if an invalid one is encountered, logs raw byte information to help debug the corruption.
      
      ---------
      Co-authored-by: default avatarMadelyn Olson <madelyneolson@gmail.com>
      e12f2dec
    • zhaozhao.zz's avatar
      Set the correct id for tempDb (#12947) · 8d0156eb
      zhaozhao.zz authored
      background: some modules need to know the `dbid` information, such as
      the function used during RDB loading:
      
      ```
      robj *rdbLoadObject(int rdbtype, rio *rdb, sds key, int dbid, int *error) {
      ....
              moduleInitIOContext(io,mt,rdb,&keyobj,dbid);
      ```
      
      However, during replication, the "tempDb" created for diskless RDB
      loading is not correctly set with the dbid. This leads to passing the
      wrong dbid to the `rdbLoadObject` function (as tempDb uses zcalloc, all
      ids are 0).
      
      ```
      disklessLoadInitTempDb()->rdbLoadRioWithLoadingCtx()->
              /* Read value */
              val = rdbLoadObject(type,rdb,key,db->id,&error);
      ```
      
      To fix it, set the correct ID (relative index) for the tempdb.
      8d0156eb
  13. 19 Jan, 2024 3 commits
    • Yanqi Lv's avatar
      Change dictGetSafeIterator to dictGetIterator in pubsub (#12931) · 85a239b3
      Yanqi Lv authored
      In #12838, we misuse the safe iterator of the client dict, so we can't
      catch the synchronous release of the client if there is a bug.
      
      Since we realize that clients (even subscribers) are released with async
      free, we change the safe iterators of the client dict into unsafe
      iterators in `pubsub.c`. And I also remove redundant code.
      85a239b3
    • Yanqi Lv's avatar
      Change the threshold of dict expand, shrink and rehash (#12948) · b07174af
      Yanqi Lv authored
      Before this change (most recently modified in
      https://github.com/redis/redis/pull/12850#discussion_r1421406393), The
      trigger for normal expand threshold was 100% utilization and the trigger
      for normal shrink threshold was 10% (HASHTABLE_MIN_FILL).
      While during fork (DICT_RESIZE_AVOID), when we want to avoid rehash, the
      trigger thresholds were multiplied by 5 (`dict_force_resize_ratio`),
      meaning 500% for expand and 2% (100/10/5) for shrink.
      
      However, in `dictRehash` (the incremental rehashing), the rehashing
      threshold for shrinking during fork (DICT_RESIZE_AVOID) was 20% by
      mistake.
      This meant that if a shrinking is triggered when `dict_can_resize` is
      `DICT_RESIZE_ENABLE` which the threshold is 10%, the rehashing can
      continue when `dict_can_resize` is `DICT_RESIZE_AVOID`.
      This would cause unwanted CopyOnWrite damage.
      
      It'll make sense to change the thresholds of the rehash trigger and the
      thresholds of the incremental rehashing the same, however, in one we
      compare the size of the hash table to the number of records, and in the
      other we compare the size of ht[0] to the size of ht[1], so the formula
      is not exactly the same.
      
      to make things easier we change all the thresholds to powers of 2, so
      the normal shrinking threshold is changed from 100/10 (i.e. 10%) to
      100/8 (i.e. 12.5%), and we change the threshold during forks from 5 to
      4, i.e. from 500% to 400% for expand, and from 2% (100/10/5) to 3.125%
      (100/8/4)
      b07174af
    • debing.sun's avatar
      Fix race condition issues between the main thread and module threads (#12817) · d0640029
      debing.sun authored
      Fix #12785 and other race condition issues.
      See the following isolated comments.
      
      The following report was obtained using SANITIZER thread.
      ```sh
      make SANITIZER=thread
      ./runtest-moduleapi --config io-threads 4 --config io-threads-do-reads yes --accurate
      ```
      
      1. Fixed thread-safe issue in RM_UnblockClient()
      Related discussion:
      https://github.com/redis/redis/pull/12817#issuecomment-1831181220
      * When blocking a client in a module using `RM_BlockClientOnKeys()` or
      `RM_BlockClientOnKeysWithFlags()`
      with a timeout_callback, calling RM_UnblockClient() in module threads
      can lead to race conditions
           in `updateStatsOnUnblock()`.
      
           - Introduced: 
              Version: 6.2
              PR: #7491
      
           - Touch:
      `server.stat_numcommands`, `cmd->latency_histogram`, `server.slowlog`,
      and `server.latency_events`
           
           - Harm Level: High
      Potentially corrupts the memory data of `cmd->latency_histogram`,
      `server.slowlog`, and `server.latency_events`
      
           - Solution:
      Differentiate whether the call to moduleBlockedClientTimedOut() comes
      from the module or the main thread.
      Since we can't know if RM_UnblockClient() comes from module threads, we
      always assume it does and
      let `updateStatsOnUnblock()` asynchronously update the unblock status.
           
      * When error reply is called in timeout_callback(), ctx is not
      thread-safe, eventually lead to race conditions in `afterErrorReply`.
      
           - Introduced: 
              Version: 6.2
              PR: #8217
      
           - Touch
             `server.stat_total_error_replies`, `server.errors`, 
      
           - Harm Level: High
             Potentially corrupts the memory data of `server.errors`
         
            - Solution: 
      Make the ctx in `timeout_callback()` with `REDISMODULE_CTX_THREAD_SAFE`,
      and asynchronously reply errors to the client.
      
      2. Made RM_Reply*() family API thread-safe
      Related discussion:
      https://github.com/redis/redis/pull/12817#discussion_r1408707239
      Call chain: `RM_Reply*()` -> `_addReplyToBufferOrList()` -> touch
      server.current_client
      
          - Introduced: 
             Version: 7.2.0
             PR: #12326
      
         - Harm Level: None
      Since the module fake client won't have the `CLIENT_PUSHING` flag, even
      if we touch server.current_client,
           we can still exit after `c->flags & CLIENT_PUSHING`.
      
         - Solution
            Checking `c->flags & CLIENT_PUSHING` earlier.
      
      3. Made freeClient() thread-safe
          Fix #12785
      
          - Introduced: 
             Version: 4.0
      Commit:
      https://github.com/redis/redis/commit/3fcf959e609e850a114d4016843e4c991066ebac
      
          - Harm Level: Moderate
             * Trigger assertion
      It happens when the module thread calls freeClient while the io-thread
      is in progress,
      which just triggers an assertion, and doesn't make any race condiaions.
      
      * Touch `server.current_client`, `server.stat_clients_type_memory`, and
      `clientMemUsageBucket->clients`.
      It happens between the main thread and the module threads, may cause
      data corruption.
      1. Error reset `server.current_client` to NULL, but theoretically this
      won't happen,
      because the module has already reset `server.current_client` to old
      value before entering freeClient.
      2. corrupts `clientMemUsageBucket->clients` in
      updateClientMemUsageAndBucket().
      3. Causes server.stat_clients_type_memory memory statistics to be
      inaccurate.
          
          - Solution:
      * No longer counts memory usage on fake clients, to avoid updating
      `server.stat_clients_type_memory` in freeClient.
      * No longer resetting `server.current_client` in unlinkClient, because
      the fake client won't be evicted or disconnected in the mid of the
      process.
      * Judgment assertion `io_threads_op == IO_THREADS_OP_IDLE` only if c is
      not a fake client.
      
      4. Fixed free client args without GIL
      Related discussion:
      https://github.com/redis/redis/pull/12817#discussion_r1408706695
      When freeing retained strings in the module thread (refcount decr), or
      using them in some way (refcount incr), we should do so while holding
      the GIL,
      otherwise, they might be simultaneously freed while the main thread is
      processing the unblock client state.
      
          - Introduced: 
             Version: 6.2.0
             PR: #8141
      
         - Harm Level: Low
           Trigger assertion or double free or memory leak. 
      
         - Solution:
      Documenting that module API users need to ensure any access to these
      retained strings is done with the GIL locked
      
      5. Fix adding fake client to server.clients_pending_write
          It will incorrectly log the memory usage for the fake client.
      Related discussion:
      https://github.com/redis/redis/pull/12817#issuecomment-1851899163
      
          - Introduced: 
             Version: 4.0
      Commit:
      https://github.com/redis/redis/commit/9b01b64430fbc1487429144d2e4e72a4a7fd9db2
      
      
      
          - Harm Level: None
            Only result in NOP
      
          - Solution:
             * Don't add fake client into server.clients_pending_write
      * Add c->conn assertion for updateClientMemUsageAndBucket() and
      updateClientMemoryUsage() to avoid same
               issue in the future.
      So now it will be the responsibility of the caller of both of them to
      avoid passing in fake client.
      
      6. Fix calling RM_BlockedClientMeasureTimeStart() and
      RM_BlockedClientMeasureTimeEnd() without GIL
          - Introduced: 
             Version: 6.2
             PR: #7491
      
         - Harm Level: Low
      Causes inaccuracies in command latency histogram and slow logs, but does
      not corrupt memory.
      
         - Solution:
      Module API users, if know that non-thread-safe APIs will be used in
      multi-threading, need to take responsibility for protecting them with
      their own locks instead of the GIL, as using the GIL is too expensive.
      
      ### Other issue
      1. RM_Yield is not thread-safe, fixed via #12905.
      
      ### Summarize
      1. Fix thread-safe issues for `RM_UnblockClient()`, `freeClient()` and
      `RM_Yield`, potentially preventing memory corruption, data disorder, or
      assertion.
      2. Updated docs and module test to clarify module API users'
      responsibility for locking non-thread-safe APIs in multi-threading, such
      as RM_BlockedClientMeasureTimeStart/End(), RM_FreeString(),
      RM_RetainString(), and RM_HoldString().
      
      ### About backpot to 7.2
      1. The implement of (1) is not too satisfying, would like to get more
      eyes.
      2. (2), (3) can be safely for backport
      3. (4), (6) just modifying the module tests and updating the
      documentation, no need for a backpot.
      4. (5) is harmless, no need for a backpot.
      
      ---------
      Co-authored-by: default avatarOran Agra <oran@redislabs.com>
      d0640029
  14. 18 Jan, 2024 4 commits
    • Chen Tianjie's avatar
      Optimize dictTypeResizeAllowed to avoid mistaken OOM judgement. (#12950) · f81c3fd8
      Chen Tianjie authored
      When doing dict resizing, dictTypeResizeAllowed is used to judge whether
      the new allocated memory for rehashing would cause OOM.
      
      However when shrinking, we alloc `_dictNextExp(d->ht_used[0])` bytes of
      memory, while in `dictTypeResizeAllowed` we still use
      `_dictNextExp(d->ht_used[0]+1)` as the new allocated memory size. This
      will overestimate the memory used by shrinking at special conditions,
      causing a false OOM judgement.
      f81c3fd8
    • Binbin's avatar
      Fix minor memory leaks in dictTest (#12962) · 1c7eb0ad
      Binbin authored
      Introduced in #12952, reported by valgrind.
      1c7eb0ad
    • Binbin's avatar
      Call emptyData when disk-based sync rdbLoad fails (#12510) · 0e5a4a27
      Binbin authored
      We doing this in diskless on-empty-db mode, when diskless
      loading fails, we will call emptyData to remove the half-loaded
      data in case we started with an empty replica.
      
      Now when a disk-based sync rdbLoad fails, we will call emptyData
      too in case it loads partially incomplete data.
      
      when the replica attempts another re-sync, it'll empty the dataset
      again anyway, so this affects two things:
      1. memory consumption in the time gap until the next rdb loading begins
      2. if the unsynced replica is for some reason promoted, it would have kept
        the partial dataset instead of being empty.
      0e5a4a27
    • Binbin's avatar
      Fix unexpected resize causing test failure (#12960) · 29e6245a
      Binbin authored
      Before #12850, we will only try to shrink the dict in serverCron,
      which we can control by using a child process, but now every time
      we delete a key, the shrink check will be called.
      
      In these test (added in #12802), we meant to disable the resizing,
      but druing the delete, the dict will meet the force shrink, like
      2 / 128 = 0.015 < 0.2, the delete will trigger a force resize and
      will cause the test to fail.
      
      In this commit, we try to keep the load factor at 3 / 128 = 0.023,
      that is, do not meet the force shrink.
      29e6245a