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 multi-database support to cluster mode #1671

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

Conversation

xbasel
Copy link
Member

@xbasel xbasel commented Feb 5, 2025

This commit introduces multi-database support in cluster mode while maintaining backward compatibility and requiring no API changes. Key features include:

  • Database-agnostic hashing: The hashing algorithm is unchanged. Identical keys map to the same slot across all databases. No changes to slot calculation. This ensures consistency in key distribution and maintains compatibility with existing single-database setups.

  • Implementation is fully backward compatible with no API changes.

  • The core structure remains an array of databases, each containing a list of hashtables (one per slot).

Cluster management commands are global commands, except for GETKEYSINSLOT and COUNTKEYSINSLOT, which run in selected-DB context.

MIGRATE command operates a selected-db context. Please note that MIGRATE command parameter destination-db is used, when migrating keys they can be migrated to a different database in the target, like in non-cluster mode.

Slot migration process changes when multiple databases are used:

	Iterate through all databases
 		SELECT database
 		keys = GETKEYSINSLOT
 		MIGRATE source target keys

Valkey-cli has been updated to support resharding across all databases.

#1319

This commit introduces multi-database support in cluster mode while
maintaining backward compatibility and requiring no API changes. Key
features include:

- Database-agnostic hashing: The hashing algorithm is unchanged.
  Identical keys map to the same slot across all databases. No changes
  to slot calculation. This ensures consistency in key distribution
  and maintains compatibility with existing single-database setups.

- Implementation is fully backward compatible with no API changes.

- The core structure remains an array of databases, each containing a
  list of hashtables (one per slot).

Cluster management commands are global commands, except for
GETKEYSINSLOT and COUNTKEYSINSLOT, which run in selected-DB context.

MIGRATE command operates a selected-db context. Please note that
MIGRATE command parameter destination-db is used, when migrating keys
they can be migrated to a different database in the target, like in
non-cluster mode.

Slot migration process changes when multiple databases are used:
	Iterate through all databases
 		SELECT database
 		keys = GETKEYSINSLOT
 		MIGRATE source target keys

Valkey-cli has been updated to support resharding across all
databases.

Signed-off-by: xbasel <[email protected]>
Copy link

codecov bot commented Feb 5, 2025

Codecov Report

Attention: Patch coverage is 89.23077% with 7 lines in your changes missing coverage. Please review.

Project coverage is 71.12%. Comparing base (2eac2cc) to head (0cf9b2d).
Report is 32 commits behind head on unstable.

Files with missing lines Patch % Lines
src/valkey-cli.c 80.64% 6 Missing ⚠️
src/cluster.c 88.88% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #1671      +/-   ##
============================================
+ Coverage     70.97%   71.12%   +0.14%     
============================================
  Files           121      123       +2     
  Lines         65238    65543     +305     
============================================
+ Hits          46305    46619     +314     
+ Misses        18933    18924       -9     
Files with missing lines Coverage Δ
src/cluster_legacy.c 86.24% <100.00%> (+0.34%) ⬆️
src/config.c 78.35% <ø> (-0.06%) ⬇️
src/db.c 89.94% <ø> (+0.37%) ⬆️
src/valkey-benchmark.c 61.83% <ø> (+1.69%) ⬆️
src/cluster.c 89.17% <88.88%> (-0.07%) ⬇️
src/valkey-cli.c 56.10% <80.64%> (+0.22%) ⬆️

... and 33 files with indirect coverage changes

@xbasel xbasel marked this pull request as ready for review February 10, 2025 21:37
@xbasel xbasel requested a review from zuiderkwast February 10, 2025 22:13
@@ -1728,12 +1714,6 @@ void swapMainDbWithTempDb(serverDb *tempDb) {
void swapdbCommand(client *c) {
int id1, id2;

/* Not allowed in cluster mode: we have just DB 0 there. */

Choose a reason for hiding this comment

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

Would that be enough for swapdb to work in cluster mode? What will happen in setup with 2 shards, each responsible for half of slots in db's?

Copy link
Member Author

@xbasel xbasel Feb 11, 2025

Choose a reason for hiding this comment

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

With this implementation SWAPDB must be executed in all primary nodes. There are three options:

  1. Allow SWAPDB and shift responsibility to the user – Risky, non-atomic, can cause temporary inconsistency and data corruption. Needs strong warnings.
  2. Keep SWAPDB disabled in cluster mode – Safest, avoids inconsistency.
  3. Make SWAPDB cluster-wide and atomic or – Complex, unclear feasibility.

I think option 2 is the safest bet. @JoBeR007 wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is SWAPDB replicated as a single command? Then it's atomic.

If it's risky, it's risky in standslone mode with replicas too, right?

I think we can allow it. Swapping the data can only be done in some non-realtime workloads anyway I think.

Choose a reason for hiding this comment

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

I think risky because of replication and risky because of the need to execute SWAPDB on all primary nodes are unrelated just because as a user you can't control first, but user is the main risk in the second case.
I would keep SWAPDB disabled in cluster mode, if we decide to continue with this implementation

Copy link
Contributor

Choose a reason for hiding this comment

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

In cluster mode, consistency is per slot.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is SWAPDB replicated as a single command? Then it's atomic.

If it's risky, it's risky in standslone mode with replicas too, right?

I think we can allow it. Swapping the data can only be done in some non-realtime workloads anyway I think.

I don’t think it’s very risky with standalone replicas. The only downside is if SWAPDB propagation to the replica takes time, a client might still access the wrong database. At least the client won’t be able to modify the wrong database, as they can only read.
In cluster mode, the same (logical) DB can be DB0 on one node and DB1 on another, but similar issues already exist today, FLUSHDB on one node doesn’t clear the entire DB since data exists in other slots/nodes. But as you said, consistency is per slot.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, FLUSHDB is very similar in this regard. If a failover happens just before this command has been propagated to replicas, it's a big thing, but it's no surprise I think. The client can use WAIT or check replication offset to make sure the FLUSHDB or SWAPDB was successful on the replicas.

@soloestoy soloestoy requested review from soloestoy and removed request for zuiderkwast February 12, 2025 06:28
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.

3 participants