Skip to content

Conversation

@Hugoch
Copy link

@Hugoch Hugoch commented Jan 21, 2026

This PR adds data structures and commands to support Redis-like Count-Min Sketch (CMS) with a multi-shard merge implementation.

Not a C++ expert here and very new to this codebase. PR was AI-assisted, I may have missed obvious stuff.

Performances

I compared performances against Redis 8.4.0 on a AWS m5d.24xlarge:

  • Initialization performances are way worse than Redis as it uses callocto get kernel pre-zeroed pages.
  • CMS.MERGE needs multiple shard hops to gather the sketches to merge, so lower performances are expected
=========================================================================================================
                           Server Comparison Results
=========================================================================================================
Server 1: Redis 8.4.0 | Server 2: Dragonfly

---------------------------------------------------------------------------------------------------------
                                Throughput (ops/sec)
---------------------------------------------------------------------------------------------------------
Command          Scenario                Redis        Dragonf           Diff      Ratio
---------------------------------------------------------------------------------------------------------
CMS.INITBYDIM    100x5                  19418.00      6835.32      -12582.68      0.35x
CMS.INITBYDIM    1000x7                 20034.71      5403.69      -14631.02      0.27x
CMS.INITBYDIM    10000x10               19868.41      1876.09      -17992.32      0.09x
CMS.INITBYPROB   err=0.01,prob=0.01     18168.57      5925.08      -12243.49      0.33x
CMS.INCRBY       single                 19796.87     11732.67       -8064.20      0.59x
CMS.INCRBY       batch_10               13150.28     10799.49       -2350.79      0.82x
CMS.INCRBY       batch_100               3094.24      2452.17        -642.07      0.79x
CMS.QUERY        single                 20439.92     14141.70       -6298.22      0.69x
CMS.QUERY        batch_10               15295.18      8379.16       -6916.02      0.55x
CMS.QUERY        batch_100               4566.28      4230.23        -336.05      0.93x
CMS.MERGE        2_sources               6482.11      2803.88       -3678.23      0.43x
CMS.MERGE        2_sources_weighted      6355.53      2950.29       -3405.24      0.46x
CMS.INFO         populated              19885.94     11942.68       -7943.26      0.60x

---------------------------------------------------------------------------------------------------------
                                P99 Latency (ms)
---------------------------------------------------------------------------------------------------------
Command          Scenario                Redis         Dragonf           Diff      Ratio
---------------------------------------------------------------------------------------------------------
CMS.INITBYDIM    100x5                    0.0809       0.2935        +0.2126      3.63x
CMS.INITBYDIM    1000x7                   0.0772       0.9192        +0.8420     11.91x
CMS.INITBYDIM    10000x10                 0.0779       1.0432        +0.9653     13.39x
CMS.INITBYPROB   err=0.01,prob=0.01       0.0837       0.3315        +0.2478      3.96x
CMS.INCRBY       single                   0.0755       0.1680        +0.0925      2.23x
CMS.INCRBY       batch_10                 0.1069       0.1366        +0.0297      1.28x
CMS.INCRBY       batch_100                0.3794       0.9174        +0.5380      2.42x
CMS.QUERY        single                   0.0736       0.0996        +0.0260      1.35x
CMS.QUERY        batch_10                 0.0924       0.2425        +0.1501      2.62x
CMS.QUERY        batch_100                0.2706       0.2898        +0.0192      1.07x
CMS.MERGE        2_sources                0.2221       0.6826        +0.4605      3.07x
CMS.MERGE        2_sources_weighted       0.2184       0.6198        +0.4014      2.84x
CMS.INFO         populated                0.0765       0.1315        +0.0550      1.72x
=========================================================================================================

Copilot AI review requested due to automatic review settings January 21, 2026 15:27
# Conflicts:
#	src/core/compact_object.cc
@Hugoch Hugoch force-pushed the feat/add-cms-command branch from d61d4bb to 35806b2 Compare January 21, 2026 15:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Count-Min Sketch (CMS) data structure support to Dragonfly, implementing Redis-compatible CMS commands with multi-shard merge capabilities. The implementation includes core data structures, RDB serialization/deserialization, command handlers, and comprehensive test coverage.

Changes:

  • Added core CMS data structure implementation with hash-based probabilistic counting
  • Integrated CMS as a new object type (OBJ_CMS) in the compact object system
  • Implemented RDB save/load support for CMS persistence
  • Added CMS command family (INITBYDIM, INITBYPROB, INCRBY, QUERY, INFO, MERGE)
  • Enabled multi-shard merge operation with transaction support

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/core/cms.h CMS class interface with creation, increment, query, and merge operations
src/core/cms.cc CMS implementation using xxhash for item hashing
src/core/compact_object.h Added CMS_TAG and GetCMS/SetCMS methods
src/core/compact_object.cc Integrated CMS into object lifecycle (allocation, free, memory tracking)
src/redis/redis_aux.h Added OBJ_CMS type constant and updated OBJ_TYPE_MAX
src/server/rdb_extensions.h Added RDB_TYPE_CMS constant and updated type checking
src/server/rdb_save.h Added SaveCMSObject declaration
src/server/rdb_save.cc Implemented CMS serialization (width, depth, count, counters)
src/server/rdb_load.h Added RdbCMS struct and ReadCMS declaration
src/server/rdb_load.cc Implemented CMS deserialization and restoration
src/server/cms_family.cc Implemented all CMS commands with multi-shard merge logic
src/server/cms_family_test.cc Comprehensive C++ unit tests for CMS commands
src/server/command_families.h Added RegisterCmsFamily declaration
src/server/main_service.cc Registered CMS command family
src/server/transaction.cc Added CMS.MERGE to bonus key handling for variadic keys
src/core/CMakeLists.txt Added cms.cc to build
src/server/CMakeLists.txt Added cms_family.cc and cms_family_test
tests/fakeredis/test/test_stack/test_cms.py Removed dragonfly exclusion marker and reformatted


// Should fail with invalid parameters
resp = Run({"cms.initbyprob", "cms2", "2", "0.01"});
EXPECT_THAT(resp, ErrArg("error must be between 0 and 1"));
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test expects the error message "error must be between 0 and 1" but the implementation sends "error must be between 0 and 1 exclusive". This inconsistency will cause the test to fail. The test expectation should match the actual error message from the implementation.

Suggested change
EXPECT_THAT(resp, ErrArg("error must be between 0 and 1"));
EXPECT_THAT(resp, ErrArg("error must be between 0 and 1 exclusive"));

Copilot uses AI. Check for mistakes.

// Merge multiple CMS structures into a destination key
// This needs to read from multiple shards via a multi-shard transaction
void CmdMerge(CmdArgList args, CommandContext* cmd_cntx) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add here the format of the command

@romange
Copy link
Collaborator

romange commented Jan 22, 2026

FakeRedis tests fail. Seems related to CMS.Merge complexity. The way I like to handle these bugs is to first trying to reproduce in a unit test. Please copy the failing test into cms_family_test and try reproducing there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants