Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of the Hyperloglog data structure #2142

Open
wants to merge 25 commits into
base: unstable
Choose a base branch
from

Conversation

tutububug
Copy link

@tutububug tutububug commented Mar 7, 2024

Copy link
Member

@mapleFU mapleFU left a comment

Choose a reason for hiding this comment

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

I'll take a careful review this week

src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved
src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved
@torwig
Copy link
Contributor

torwig commented Mar 7, 2024

@tutububug Thank you for your contribution. Running ./x.py format should eliminate the linting issues.

@Yangsx-1
Copy link
Contributor

Yangsx-1 commented Mar 7, 2024

Thank you for your contribution! Maybe you need to add a command parser to use the data structure with actual command like redis.

@PragmaTwice
Copy link
Member

Thank you for your contribution!

Could you include your design in the PR description? For example, explain how to encode the metadata and HLL data (subkeys), similar to what is shown on https://kvrocks.apache.org/community/data-structure-on-rocksdb.

src/storage/redis_db.cc Outdated Show resolved Hide resolved
@tutububug
Copy link
Author

tutububug commented Mar 7, 2024

Thank you for your contribution! Maybe you need to add a command parser to use the data structure with actual command like redis.

Yes, I will give the commit later.

Thank you for your contribution!

Could you include your design in the PR description? For example, explain how to encode the metadata and HLL data (subkeys), similar to what is shown on https://kvrocks.apache.org/community/data-structure-on-rocksdb.

OK.

@tutububug
Copy link
Author

@PragmaTwice I create a PR(apache/kvrocks-website#207) for describe hyperloglog storage format.

@PragmaTwice
Copy link
Member

PragmaTwice commented Mar 9, 2024

@PragmaTwice I create a PR(apache/kvrocks-website#207) for describe hyperloglog storage format.

Thank you!
Currently, it is only necessary to include your design in the PR description, not on the website as the design is not finalized yet.

Regarding your design, I have some questions:

  1. Based on the current design, typically one Redis key will introduce a maximum of 16384 RocksDB keys (registers). Each value corresponding to a RocksDB key contains only one integer. This may be inefficient; merging multiple registers onto one key could reduce the number of keys introduced. WDYT?
  2. I noticed that integers are stored as string representations (std::to_string) rather than their binary form (e.g., subkey and register value). What is the reason for this approach?
  3. Having a constant size seems illogical since the number of subkeys linked to this Redis key varies. (However, if every write operation modifies the metadata, it may lead to a decrease in performance. I don't have a clear idea about this aspect.)

Concerning the code, although I haven't reviewed it thoroughly yet, there are some points worth mentioning:

  1. The complete source code of MurmurHash can be placed in the vendor directory.
  2. It appears that using PFADD in the code leads to an increasing number of RocksDB keys (registers). There seems to be no operation that reduces these keys until deleting this Redis key. How can we prevent an increase in RocksDB keys without a mechanism to decrease them?

cc @git-hulk @mapleFU

@PragmaTwice PragmaTwice changed the title Support Hyperloglog Add support of the Hyperloglog data structure Mar 9, 2024
@git-hulk
Copy link
Member

git-hulk commented Mar 9, 2024

@tutububug As @PragmaTwice mentioned in #2142 (comment), it's unnecessary to use a static number of 16384 since it may heavily affect the read performance while using PFMERGE, I guess a smaller one like 16 is enough and every subkey has 1000 integers.

@git-hulk git-hulk self-requested a review March 9, 2024 07:25
src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved
src/common/status.h Outdated Show resolved Hide resolved
@PragmaTwice
Copy link
Member

PragmaTwice commented Mar 9, 2024

I suggest that we can store the number of registers in one rocksdb key in the metadata (for example, referred to as register_number_in_one_key), so that even if adjustments are made later for performance reasons, the compatibility of kvrocks data can be maintained.

Currently, a fixed value for register_number_in_one_key can be hardcoded in the code.

cc @tutububug

@mapleFU
Copy link
Member

mapleFU commented Mar 9, 2024

Based on the current design, typically one Redis key will introduce a maximum of 16384 RocksDB keys (registers). Each value corresponding to a RocksDB key contains only one integer. This may be inefficient; merging multiple registers onto one key could reduce the number of keys introduced. WDYT?

There're two parts of things.

  1. Bitmap type can be used to do the dense logic, which would reducing the keycount
  2. sparse hll type can be introduced to save some overhead here.

@PragmaTwice
Copy link
Member

Based on the current design, typically one Redis key will introduce a maximum of 16384 RocksDB keys (registers). Each value corresponding to a RocksDB key contains only one integer. This may be inefficient; merging multiple registers onto one key could reduce the number of keys introduced. WDYT?

There're two parts of things.

  1. Bitmap type can be used to do the dense logic, which would reducing the keycount
  2. sparse hll type can be introduced to save some overhead here.

So I think the question is, should we also introduce two mode of hll encoding (sparse and dense layout) and an auto switching policy between these two layout?

@mapleFU
Copy link
Member

mapleFU commented Mar 9, 2024

So I think the question is, should we also introduce two mode of hll encoding (sparse and dense layout) and an auto switching policy between these two layout?

I prefer to do this :-) But we can regard it as a further optimization

src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved
src/types/redis_hyperloglog.h Outdated Show resolved Hide resolved
src/types/redis_hyperloglog.h Outdated Show resolved Hide resolved
src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved
src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved
src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved

/* Store the value of the register at position 'regnum' into variable 'target'.
* 'p' is an array of unsigned bytes. */
#define HLL_DENSE_GET_REGISTER(target, p, regnum) \
Copy link
Member

Choose a reason for hiding this comment

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

can you change it to a function?

src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved
src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved
@mapleFU
Copy link
Member

mapleFU commented Mar 9, 2024

Other Looks ok to me

@tutububug tutububug closed this Mar 9, 2024
@tutububug tutububug reopened this Mar 9, 2024
@tutububug
Copy link
Author

Regarding your design, I have some questions:

1. Based on the current design, typically one Redis key will introduce a maximum of 16384 RocksDB keys (registers). Each value corresponding to a RocksDB key contains only one integer. This may be inefficient; merging multiple registers onto one key could reduce the number of keys introduced. WDYT?

Not really. The register(subkey) only be stored which its count is not zero. This point is different from the memory implementation with static array as dense encode. On disk, I think its sparse encode naturally.

2. I noticed that integers are stored as string representations (`std::to_string`) rather than their binary form (e.g., subkey and register value). What is the reason for this approach?

The number of consecutive 0s is calculated from the last 50 digits of the hash value, so the maximum value is 50, and the maximum value stored in a string is 2 bytes. It should not waste a lot of space, and at the same time save the overhead of integer encoding and decoding. For keys, it may be more efficient, but the largest index is only 5 bytes (16383).

3. Having a constant `size` seems illogical since the number of subkeys linked to this Redis key varies. (However, if every write operation modifies the metadata, it may lead to a decrease in performance. I don't have a clear idea about this aspect.)

Currently, the ‘size’ variable has no practical purpose; the only requirement is that it be non-zero. Due to the implementation of kvrocks getmetadata, non-string type data structures with a size of 0 are judged to be expired, and the HLL add parameter that redis has implemented allows no parameters but the key will be stored. For compatibility, size is used as a constant to prevent expiration.

Concerning the code, although I haven't reviewed it thoroughly yet, there are some points worth mentioning:

1. The complete source code of MurmurHash can be placed in the `vendor` directory.

OK.

2. It appears that using `PFADD` in the code leads to an increasing number of RocksDB keys (registers). There seems to be no operation that reduces these keys until deleting this Redis key. How can we prevent an increase in RocksDB keys without a mechanism to decrease them?

For an HLL user key, the maximum register value is 16384, and it cannot be larger. In fact, I think this should be considered controllable compared to data structures such as hash, set, list, etc. whose size is determined by user input.

@PragmaTwice cc @git-hulk @mapleFU

@mapleFU
Copy link
Member

mapleFU commented Mar 9, 2024

The number of consecutive 0s is calculated from the last 50 digits of the hash value, so the maximum value is 50, and the maximum value stored in a string is 2 bytes. It should not waste a lot of space, and at the same time save the overhead of integer encoding and decoding. For keys, it may be more efficient, but the largest index is only 5 bytes (16383).

For rocksdb value, it's should be 1-2 bytes payload, the value size is also included. So, it introduce an extremly huge overhead. So I prefer the impl of bitmap/bitfield.

Get 2^14 times in rocksdb would also be heavy, and might break some statistics in rocksdb, which making it tent to compaction more or caching the unneccessary blocks.

Copy link

sonarcloud bot commented Apr 20, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot

See analysis details on SonarCloud

Copy link
Member

@mapleFU mapleFU left a comment

Choose a reason for hiding this comment

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

This looks much better than previous version. The remaining are mostly code-style problem

src/storage/redis_metadata.cc Show resolved Hide resolved
src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved
src/types/redis_hyperloglog.cc Outdated Show resolved Hide resolved

private:
rocksdb::Status GetMetadata(Database::GetOptions get_options, const Slice &ns_key, HyperloglogMetadata *metadata);
rocksdb::Status getRegisters(const Slice &user_key, std::vector<uint8_t> *registers);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: you can add a todo, and optimizing the return value for getRegisters

Copy link
Author

Choose a reason for hiding this comment

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

Do you mean the output argument 'registers' needs to be modified?

src/types/redis_hyperloglog.cc Show resolved Hide resolved
src/types/hyperloglog.cc Outdated Show resolved Hide resolved
src/types/hyperloglog.cc Outdated Show resolved Hide resolved
src/types/hyperloglog.cc Outdated Show resolved Hide resolved
src/types/hyperloglog.cc Outdated Show resolved Hide resolved
src/types/hyperloglog.cc Outdated Show resolved Hide resolved
@tutububug
Copy link
Author

This looks much better than previous version. The remaining are mostly code-style problem

fixed.

Copy link
Member

@PragmaTwice PragmaTwice left a comment

Choose a reason for hiding this comment

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

There is a lot of strange type casting in the code.

It is advisable to minimize the use of type casting, particularly const_cast and reinterpret_cast. It is recommended to consider modifying the function signature instead of relying solely on casts.

@tutububug
Copy link
Author

There is a lot of strange type casting in the code.

It is advisable to minimize the use of type casting, particularly const_cast and reinterpret_cast. It is recommended to consider modifying the function signature instead of relying solely on casts.

fixed.

Copy link
Author

@tutububug tutububug left a comment

Choose a reason for hiding this comment

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

Had fixed.

@mapleFU
Copy link
Member

mapleFU commented May 15, 2024

I just back from holidays and catch a cold. I'll take a carefully around this week. Would you mind me edit directly on this patch @tutububug

@tutububug
Copy link
Author

tutububug commented May 16, 2024

I just back from holidays and catch a cold. I'll take a carefully around this week. Would you mind me edit directly on this patch @tutububug

OK, feel free to modify if needed. And also the description at https://github.com/apache/kvrocks-website/pull/207/files.

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.

None yet

6 participants