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

Refactor the internal function parse_cluster_nodes #119

Merged
merged 9 commits into from
Oct 28, 2024

Conversation

bjosv
Copy link
Collaborator

@bjosv bjosv commented Oct 14, 2024

This is a 2 step/2 commit refactor PR.

  • Refactor the internal function parse_cluster_nodes.
    Parse the CLUSTER NODES reply by a simpler string search using strchr and string modification instead of using sdssplitlen, which allocates memory for each element.
    Parse each line once using a new function parse_cluster_nodes_line, which returns a valkeyClusterNode.
    Replicas are only parsed when configured, like legacy.

  • Refactor the replica handling in CLUSTER NODES parsing.
    Store parsed replicas in a separate dict during parsing, and move them to their primary when all lines are parsed.
    This dict owns the memory for replicas until moved to the related primary node.
    Previously the dict contained references to both replicas and primaries and a bit harder to handle.
    The function cluster_master_slave_mapping_with_name is removed and its dual purpose handling is replaced by functions store_replica_node and move_replica_nodes.

Parse the string without using sdssplitlen which allocates memory
for each element. Parse each line once to get a valkeyClusterNode.

Signed-off-by: Björn Svensson <[email protected]>
Store parsed replicas in a separate dict during parsing, and move
them to their primary when all lines are parsed.
This dict owns the memory for added nodes until moved.

Previously the dict contained references to both replicas and primaries
and a bit harder to handle.

Signed-off-by: Björn Svensson <[email protected]>
Copy link
Collaborator

@zuiderkwast zuiderkwast left a comment

Choose a reason for hiding this comment

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

I didn't review everything very carefully, but I believe you just refactored existing logic and we have tests covering it, so it's fine by me.

We use the words master, slave, primary, replica a little bit mixed everywhere, in macros, functions, struct fieds, variables and comments. I guess we should strive towards the new terminology, but not change all in the same PR?

src/cluster.c Outdated Show resolved Hide resolved
src/cluster.c Outdated Show resolved Hide resolved
src/cluster.c Outdated Show resolved Hide resolved
src/cluster.c Outdated Show resolved Hide resolved
Use primary and replica in added code.

Signed-off-by: Björn Svensson <[email protected]>
Probably less odd.

Signed-off-by: Björn Svensson <[email protected]>
Signed-off-by: Björn Svensson <[email protected]>
@bjosv
Copy link
Collaborator Author

bjosv commented Oct 15, 2024

I didn't review everything very carefully, but I believe you just refactored existing logic and we have tests covering it, so it's fine by me.

Yes, the newly added testcases in ut_slotmap_update.c should cover this change.

We use the words master, slave, primary, replica a little bit mixed everywhere, in macros, functions, struct fieds, variables and comments. I guess we should strive towards the new terminology, but not change all in the same PR?

The plan was to change the define and struct in a separate PR since it also affects CLUSTER SLOTS parsing, but there was some new code in this PR that was a bit inconsistent, this is now changed.

src/cluster.c Outdated Show resolved Hide resolved
src/cluster.c Outdated Show resolved Hide resolved
Signed-off-by: Björn Svensson <[email protected]>
Change old way of storing replicas while parsing cluster nodes.
Simply keep lists of cluster nodes in the replica dict,
which then can be moved to the nodes dict.

Signed-off-by: Björn Svensson <[email protected]>
Copy link
Collaborator

@zuiderkwast zuiderkwast left a comment

Choose a reason for hiding this comment

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

Great, this is much less weird.

src/cluster.c Outdated Show resolved Hide resolved
assert(primary->slaves == NULL);
/* Move replica list from replicas dict to nodes dict. */
primary->slaves = dictGetEntryVal(der);
dictSetHashVal(replicas, der, NULL);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't recognize the function name dictSetHashVal. It must be copied from a very old Redis version. 😄

Co-authored-by: Viktor Söderqvist <[email protected]>
Signed-off-by: Björn Svensson <[email protected]>
src/cluster.c Outdated Show resolved Hide resolved
src/cluster.c Outdated Show resolved Hide resolved
Signed-off-by: Björn Svensson <[email protected]>
@bjosv bjosv merged commit 6f2ba3a into valkey-io:main Oct 28, 2024
43 checks passed
@bjosv bjosv deleted the refactor-cluster-nodes branch October 28, 2024 12:37
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