You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by "Tangruilin (via GitHub)" <gi...@apache.org> on 2026/03/31 17:04:02 UTC

[PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Tangruilin opened a new pull request, #3412:
URL: https://github.com/apache/kvrocks/pull/3412

   WIP: fix TDIGEST.MERGE to match Redis behavior
   
   Issue: #3410
   
   When destination key already exists without OVERRIDE flag:
   - Redis: merge dest + sources together
   - Kvrocks (before): return error
   
   Changes:
   1. Remove error return when dest exists without OVERRIDE
   2. Read dest centroids and add to merge list
   3. Add check to prevent duplicate merge if dest in sources
   4. Update tests to verify merge and override behavior


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "jihuayu (via GitHub)" <gi...@apache.org>.
jihuayu commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4191573832

   @Tangruilin Yes, you are right!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4200599560

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=3412) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=3412&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=3412&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=3412&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [62.3% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=3412&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=3412&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=3412)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4229828215

   > @Tangruilin It looks like the code incorrectly overrides the COMPRESSION parameter in the following scenario. Could you add more test cases for this?
   > 
   > ```
   > TDIGEST.CREATE src COMPRESSION 200
   > TDIGEST.ADD src 1
   > 
   > TDIGEST.CREATE dest COMPRESSION 100
   > TDIGEST.ADD dest 2
   > 
   > TDIGEST.INFO dest
   > TDIGEST.MERGE dest 1 src
   > TDIGEST.INFO dest
   > ```
   > 
   > The Redis results are as follows:
   > 
   > ```
   > 127.0.0.1:6379> TDIGEST.INFO dest
   >  1) Compression
   >  2) (integer) 100
   > ```
   
   Done


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4189113489

   @jihuayu 
   "The lint issue was introduced by MR [https://github.com/apache/kvrocks/pull/3419](https://github.com/apache/kvrocks/pull/3419.)[.](https://github.com/apache/kvrocks/pull/3419.) It seems this MR was merged without passing the format check.
   
    Should kvrocks consider enforcing a rule that blocks merging when CI doesn't pass, to avoid similar CI pollution issues?"
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3046198809


##########
src/types/redis_tdigest.cc:
##########
@@ -473,21 +472,8 @@ rocksdb::Status TDigest::Merge(engine::Context& ctx, const Slice& dest_digest,
       return status;
     }
 
-    if (metadata.unmerged_nodes > 0) {
-      if (auto status = mergeCurrentBuffer(ctx, source_ns_key, batch, &metadata, nullptr, &source_centroids);
-          !status.ok()) {
-        return status;
-      }
-
-      std::string metadata_bytes;
-      metadata.Encode(&metadata_bytes);
-      if (auto status = batch->Put(metadata_cf_handle_, source_ns_key, metadata_bytes); !status.ok()) {
-        return status;
-      }
-    } else if (metadata.merged_nodes > 0) {
-      if (auto status = dumpCentroids(ctx, source_ns_key, metadata, &source_centroids); !status.ok()) {
-        return status;
-      }
+    if (auto status = getCentroidsForMerge(ctx, source_ns_key, batch, &metadata, &source_centroids); !status.ok()) {

Review Comment:
   Okey



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "LindaSummer (via GitHub)" <gi...@apache.org>.
LindaSummer merged PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3080819641


##########
src/types/redis_tdigest.h:
##########
@@ -124,6 +124,23 @@ class TDigest : public SubKeyScanner {
                                          const TDigestMetadata& metadata, std::vector<Centroid>* centroids,
                                          std::vector<double>* buffer,
                                          ObserverOrUniquePtr<rocksdb::WriteBatchBase>* clean_after_dump_batch);
+
+  /**
+   * @brief Get centroids for merge operation.
+   *
+   * If the tdigest has unmerged buffer, merge it first and update metadata to batch.
+   * Otherwise, just dump existing centroids.
+   * @param ctx The context of the operation.
+   * @param ns_key The namespace key of the t-digest.
+   * @param batch The write batch to store metadata updates.
+   * @param metadata The metadata of the t-digest (may be updated if buffer is merged).
+   * @param centroids The output vector to store the centroids.
+   * @return rocksdb::Status

Review Comment:
   Ths



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3068322981


##########
src/types/tdigest.cc:
##########
@@ -362,7 +362,7 @@ class TDigest {
   TDigestImpl impl_;
 };
 
-TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset({}); }
+TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset(); }

Review Comment:
   Actually, kvrocks already has a struct for initialization:
    ```cpp
     struct CentroidsWithDelta {
       std::vector<Centroid> centroids;
       uint64_t delta;
       double min;
       double max;
       double total_weight;
     };
   ```
     Unfortunately, the min and max initialization in this struct is also incorrect. I'll submit a follow-up PR to fix the initialization values of this struct and disable implicit deduction for this function to prevent future misuse.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4189052940

   @jihuayu  "I'm sorry the CI didn't pass.
   
    However, it seems the CI format configuration is not using the default configuration. I ran ./x.py check format locally and it passed, but the CI command is ./x.py check format
    --clang-format-path clang-format-18.
   
    This can be confusing. Below is a screenshot of my local run. Perhaps we should keep the CI consistent with the default configuration and use configuration files instead of command-line
    arguments."
    
   <img width="1268" height="729" alt="image" src="https://github.com/user-attachments/assets/5370c92c-b441-4baa-803a-11c5dcdcb6c6" />
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3037951742


##########
tests/gocase/unit/type/tdigest/tdigest_test.go:
##########
@@ -463,16 +463,44 @@ func tdigestTests(t *testing.T, configs util.KvrocksServerConfigs) {
 		require.NoError(t, rdb.Do(ctx, "TDIGEST.CREATE", sourceKey2, "compression", "30").Err())
 		require.NoError(t, rdb.Do(ctx, "TDIGEST.ADD", sourceKey2, "4.0", "5.0", "6.0", "100", "-200").Err())
 
-		// create a destination digest
+		// create a destination digest and add some data (for testing merge into existing dest)
 		destKey := keyPrefix + "dest"
 		require.NoError(t, rdb.Do(ctx, "TDIGEST.CREATE", destKey, "compression", "100").Err())
+		require.NoError(t, rdb.Do(ctx, "TDIGEST.ADD", destKey, "7.0", "8.0", "9.0").Err())
 
-		// merge the source into the destination without override
-		require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.MERGE", destKey, 2, sourceKey1, sourceKey2).Err(), errMsgKeyAlreadyExists)
+		// merge the source into the existing destination without override
+		// should merge dest + sources together (not return error)
+		require.NoError(t, rdb.Do(ctx, "TDIGEST.MERGE", destKey, 2, sourceKey1, sourceKey2).Err())
 
-		// merge the source into the destination with override
+		// verify the merged result contains data from both dest and sources
+		rsp := rdb.Do(ctx, "TDIGEST.INFO", destKey)

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4189116473

   > @jihuayu "The lint issue was introduced by MR #3419 It seems this MR was merged without passing the format check.
   > 
   > Should kvrocks consider enforcing a rule that blocks merging when CI doesn't pass, to avoid similar CI pollution issues?"
   
   <img width="1476" height="441" alt="image" src="https://github.com/user-attachments/assets/41142ccb-5d6c-47b0-b2f0-80de25ec37ad" />
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4200388047

   > Hi @Tangruilin ,
   > 
   > Generally LGTM with some comments. 😊
   > 
   > Could we add some cpp unit tests for this change?
   
   Sure. I will add cpp unit before merge


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "jihuayu (via GitHub)" <gi...@apache.org>.
jihuayu commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4203190480

   @Tangruilin  It looks like the code incorrectly overrides the COMPRESSION parameter in the following scenario. Could you add more test cases for this?
   ```txt
   TDIGEST.CREATE src COMPRESSION 200
   TDIGEST.ADD src 1
   
   TDIGEST.CREATE dest COMPRESSION 100
   TDIGEST.ADD dest 2
   
   TDIGEST.INFO dest
   TDIGEST.MERGE dest 1 src
   TDIGEST.INFO dest
   ```
   The Redis results are as follows:
   ```txt
   127.0.0.1:6379> TDIGEST.INFO dest
    1) Compression
    2) (integer) 100
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3036817810


##########
tests/gocase/unit/type/tdigest/tdigest_test.go:
##########
@@ -463,16 +463,44 @@ func tdigestTests(t *testing.T, configs util.KvrocksServerConfigs) {
 		require.NoError(t, rdb.Do(ctx, "TDIGEST.CREATE", sourceKey2, "compression", "30").Err())
 		require.NoError(t, rdb.Do(ctx, "TDIGEST.ADD", sourceKey2, "4.0", "5.0", "6.0", "100", "-200").Err())
 
-		// create a destination digest
+		// create a destination digest and add some data (for testing merge into existing dest)
 		destKey := keyPrefix + "dest"
 		require.NoError(t, rdb.Do(ctx, "TDIGEST.CREATE", destKey, "compression", "100").Err())
+		require.NoError(t, rdb.Do(ctx, "TDIGEST.ADD", destKey, "7.0", "8.0", "9.0").Err())
 
-		// merge the source into the destination without override
-		require.ErrorContains(t, rdb.Do(ctx, "TDIGEST.MERGE", destKey, 2, sourceKey1, sourceKey2).Err(), errMsgKeyAlreadyExists)
+		// merge the source into the existing destination without override
+		// should merge dest + sources together (not return error)
+		require.NoError(t, rdb.Do(ctx, "TDIGEST.MERGE", destKey, 2, sourceKey1, sourceKey2).Err())
 
-		// merge the source into the destination with override
+		// verify the merged result contains data from both dest and sources
+		rsp := rdb.Do(ctx, "TDIGEST.INFO", destKey)

Review Comment:
   The new merge behavior includes a guard to avoid merging the destination twice when `dest` is also present in the `source_digests` list, but the updated test only covers the `dest`-exists-without-OVERRIDE path where `dest` is not included as a source. Add a test case like `TDIGEST.MERGE dest 3 dest src1 src2` (and/or with `OVERRIDE`) and assert observations/quantiles are not double-counted, to lock in the intended behavior and prevent regressions.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3046198809


##########
src/types/redis_tdigest.cc:
##########
@@ -473,21 +472,8 @@ rocksdb::Status TDigest::Merge(engine::Context& ctx, const Slice& dest_digest,
       return status;
     }
 
-    if (metadata.unmerged_nodes > 0) {
-      if (auto status = mergeCurrentBuffer(ctx, source_ns_key, batch, &metadata, nullptr, &source_centroids);
-          !status.ok()) {
-        return status;
-      }
-
-      std::string metadata_bytes;
-      metadata.Encode(&metadata_bytes);
-      if (auto status = batch->Put(metadata_cf_handle_, source_ns_key, metadata_bytes); !status.ok()) {
-        return status;
-      }
-    } else if (metadata.merged_nodes > 0) {
-      if (auto status = dumpCentroids(ctx, source_ns_key, metadata, &source_centroids); !status.ok()) {
-        return status;
-      }
+    if (auto status = getCentroidsForMerge(ctx, source_ns_key, batch, &metadata, &source_centroids); !status.ok()) {

Review Comment:
   Okey



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3046195074


##########
src/types/redis_tdigest.cc:
##########
@@ -473,21 +472,8 @@ rocksdb::Status TDigest::Merge(engine::Context& ctx, const Slice& dest_digest,
       return status;
     }
 
-    if (metadata.unmerged_nodes > 0) {
-      if (auto status = mergeCurrentBuffer(ctx, source_ns_key, batch, &metadata, nullptr, &source_centroids);
-          !status.ok()) {
-        return status;
-      }
-
-      std::string metadata_bytes;
-      metadata.Encode(&metadata_bytes);
-      if (auto status = batch->Put(metadata_cf_handle_, source_ns_key, metadata_bytes); !status.ok()) {
-        return status;
-      }
-    } else if (metadata.merged_nodes > 0) {
-      if (auto status = dumpCentroids(ctx, source_ns_key, metadata, &source_centroids); !status.ok()) {
-        return status;
-      }
+    if (auto status = getCentroidsForMerge(ctx, source_ns_key, batch, &metadata, &source_centroids); !status.ok()) {

Review Comment:
    I discovered this issue while adding a related test case. If this fix isn't included in this PR, my test won't pass.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4248751093

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=3412) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [3 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=3412&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=3412&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=3412&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [66.8% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=3412&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1.1% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=3412&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=3412)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4164830837

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=3412) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [3 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=3412&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=3412&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=3412&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [55.7% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=3412&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=3412&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=3412)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3046197022


##########
src/types/tdigest.cc:
##########
@@ -362,7 +362,7 @@ class TDigest {
   TDigestImpl impl_;
 };
 
-TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset({}); }
+TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset(); }

Review Comment:
   this isn't a refactor but rather a bugfix. When calling Reset({}), both min and max are initialized to 0. This causes a problem: if the source's min is greater than 0, the actual minimum value during merge would be incorrectly updated to 0.
   
   I discovered this issue while adding a related test case. If this fix isn't included in this PR, my test won't pass.



##########
src/types/redis_tdigest.cc:
##########
@@ -473,21 +472,8 @@ rocksdb::Status TDigest::Merge(engine::Context& ctx, const Slice& dest_digest,
       return status;
     }
 
-    if (metadata.unmerged_nodes > 0) {
-      if (auto status = mergeCurrentBuffer(ctx, source_ns_key, batch, &metadata, nullptr, &source_centroids);
-          !status.ok()) {
-        return status;
-      }
-
-      std::string metadata_bytes;
-      metadata.Encode(&metadata_bytes);
-      if (auto status = batch->Put(metadata_cf_handle_, source_ns_key, metadata_bytes); !status.ok()) {
-        return status;
-      }
-    } else if (metadata.merged_nodes > 0) {
-      if (auto status = dumpCentroids(ctx, source_ns_key, metadata, &source_centroids); !status.ok()) {
-        return status;
-      }
+    if (auto status = getCentroidsForMerge(ctx, source_ns_key, batch, &metadata, &source_centroids); !status.ok()) {

Review Comment:
   this isn't a refactor but rather a bugfix. When calling Reset({}), both min and max are initialized to 0. This causes a problem: if the source's min is greater than 0, the actual minimum value during merge would be incorrectly updated to 0.



##########
src/types/redis_tdigest.cc:
##########
@@ -473,21 +472,8 @@ rocksdb::Status TDigest::Merge(engine::Context& ctx, const Slice& dest_digest,
       return status;
     }
 
-    if (metadata.unmerged_nodes > 0) {
-      if (auto status = mergeCurrentBuffer(ctx, source_ns_key, batch, &metadata, nullptr, &source_centroids);
-          !status.ok()) {
-        return status;
-      }
-
-      std::string metadata_bytes;
-      metadata.Encode(&metadata_bytes);
-      if (auto status = batch->Put(metadata_cf_handle_, source_ns_key, metadata_bytes); !status.ok()) {
-        return status;
-      }
-    } else if (metadata.merged_nodes > 0) {
-      if (auto status = dumpCentroids(ctx, source_ns_key, metadata, &source_centroids); !status.ok()) {
-        return status;
-      }
+    if (auto status = getCentroidsForMerge(ctx, source_ns_key, batch, &metadata, &source_centroids); !status.ok()) {

Review Comment:
    I discovered this issue while adding a related test case. If this fix isn't included in this PR, my test won't pass.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3046191696


##########
src/types/redis_tdigest.cc:
##########
@@ -473,21 +472,8 @@ rocksdb::Status TDigest::Merge(engine::Context& ctx, const Slice& dest_digest,
       return status;
     }
 
-    if (metadata.unmerged_nodes > 0) {
-      if (auto status = mergeCurrentBuffer(ctx, source_ns_key, batch, &metadata, nullptr, &source_centroids);
-          !status.ok()) {
-        return status;
-      }
-
-      std::string metadata_bytes;
-      metadata.Encode(&metadata_bytes);
-      if (auto status = batch->Put(metadata_cf_handle_, source_ns_key, metadata_bytes); !status.ok()) {
-        return status;
-      }
-    } else if (metadata.merged_nodes > 0) {
-      if (auto status = dumpCentroids(ctx, source_ns_key, metadata, &source_centroids); !status.ok()) {
-        return status;
-      }
+    if (auto status = getCentroidsForMerge(ctx, source_ns_key, batch, &metadata, &source_centroids); !status.ok()) {

Review Comment:
   this isn't a refactor but rather a bugfix. When calling Reset({}), both min and max are initialized to 0. This causes a problem: if the source's min is greater than 0, the actual minimum value during merge would be incorrectly updated to 0.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3080775265


##########
src/types/redis_tdigest.h:
##########
@@ -124,6 +124,23 @@ class TDigest : public SubKeyScanner {
                                          const TDigestMetadata& metadata, std::vector<Centroid>* centroids,
                                          std::vector<double>* buffer,
                                          ObserverOrUniquePtr<rocksdb::WriteBatchBase>* clean_after_dump_batch);
+
+  /**
+   * @brief Get centroids for merge operation.
+   *
+   * If the tdigest has unmerged buffer, merge it first and update metadata to batch.
+   * Otherwise, just dump existing centroids.
+   * @param ctx The context of the operation.
+   * @param ns_key The namespace key of the t-digest.
+   * @param batch The write batch to store metadata updates.
+   * @param metadata The metadata of the t-digest (may be updated if buffer is merged).
+   * @param centroids The output vector to store the centroids.
+   * @return rocksdb::Status

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4188564310

   @jihuayu jiHi, this PR is ready for review. Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3068322981


##########
src/types/tdigest.cc:
##########
@@ -362,7 +362,7 @@ class TDigest {
   TDigestImpl impl_;
 };
 
-TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset({}); }
+TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset(); }

Review Comment:
   > ** [LindaSummer](/LindaSummer)**
   
   Actually, kvrocks already has a struct for initialization:
    ```cpp
     struct CentroidsWithDelta {
       std::vector<Centroid> centroids;
       uint64_t delta;
       double min;
       double max;
       double total_weight;
     };
   ```
     Unfortunately, the min and max initialization in this struct is also incorrect. I'll submit a follow-up PR to fix the initialization values of this struct and disable implicit deduction for this function to prevent future misuse.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "Tangruilin (via GitHub)" <gi...@apache.org>.
Tangruilin commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4231961226

   @jihuayu ji
   The CI seems to fail because of  env


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "LindaSummer (via GitHub)" <gi...@apache.org>.
LindaSummer commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3045936165


##########
src/types/redis_tdigest.cc:
##########
@@ -473,21 +472,8 @@ rocksdb::Status TDigest::Merge(engine::Context& ctx, const Slice& dest_digest,
       return status;
     }
 
-    if (metadata.unmerged_nodes > 0) {
-      if (auto status = mergeCurrentBuffer(ctx, source_ns_key, batch, &metadata, nullptr, &source_centroids);
-          !status.ok()) {
-        return status;
-      }
-
-      std::string metadata_bytes;
-      metadata.Encode(&metadata_bytes);
-      if (auto status = batch->Put(metadata_cf_handle_, source_ns_key, metadata_bytes); !status.ok()) {
-        return status;
-      }
-    } else if (metadata.merged_nodes > 0) {
-      if (auto status = dumpCentroids(ctx, source_ns_key, metadata, &source_centroids); !status.ok()) {
-        return status;
-      }
+    if (auto status = getCentroidsForMerge(ctx, source_ns_key, batch, &metadata, &source_centroids); !status.ok()) {

Review Comment:
   This is great to reduce redundancy of this action.👍



##########
src/types/tdigest.cc:
##########
@@ -362,7 +362,7 @@ class TDigest {
   TDigestImpl impl_;
 };
 
-TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset({}); }
+TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset(); }

Review Comment:
   Maybe we could do refactor in a new separate PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "jihuayu (via GitHub)" <gi...@apache.org>.
jihuayu commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4188858633

   Hi @Tangruilin You need to make sure the format, tidy and test CI pass before!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "LindaSummer (via GitHub)" <gi...@apache.org>.
LindaSummer commented on code in PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#discussion_r3058760723


##########
src/types/tdigest.cc:
##########
@@ -362,7 +362,7 @@ class TDigest {
   TDigestImpl impl_;
 };
 
-TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset({}); }
+TDigest::TDigest(uint64_t delta) : impl_(TDigestImpl(delta)) { Reset(); }

Review Comment:
   Nice catch!
   Could we create a new PR after this one and make it reset with `nan` or `+/-inf` to avoid this problem? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] fix(tdigest): merge into existing dest without OVERRIDE [kvrocks]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3412:
URL: https://github.com/apache/kvrocks/pull/3412#issuecomment-4233291894

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=3412) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [3 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=3412&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=3412&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=3412&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [66.9% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=3412&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_kvrocks&pullRequest=3412&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=3412)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org