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

Fix Sharding Initialization with Dynamic Replica Set Configuration #1087

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

emplam27
Copy link
Contributor

@emplam27 emplam27 commented Dec 3, 2024

What this PR does / why we need it:

When you change the settings to use mongodb sharding, the configmap changes like below.

  • fix rs.initiate for configsrv and shardsvr. secondary members were not contained in replicaset
  • primary has priority value only.
# values.yaml

...
sharded:
  enabled: true
  shards: &shard 2
  replicaCount:
    configsvr: &configSvrReplicaCount 3
    shardsvr: &shardSvrReplicaCount 3
    mongos: &mongosReplicaCount 3
...
  • as-is
# configmap.yaml

echo "Wait until config server is ready..."
...
mongosh $configsvrAddr --eval 'rs.initiate({"_id":"mongodb-configsvr", "members":[{"_id":0,"host":"mongodb-configsvr-0.mongodb-headless.mongodb.svc.cluster.local:27017","priority":5}]})'
 
echo "Wait until shard0 is ready..."
...
mongosh $shardsvrAddr --eval 'rs.initiate({"_id":"mongodb-shard-0", "members":[{"_id":0,"host":"mongodb-shard0-data-0.mongodb-headless.mongodb.svc.cluster.local:27017","priority":5}]})'
 
echo "Wait until shard1 is ready..."
...
mongosh $shardsvrAddr --eval 'rs.initiate({"_id":"mongodb-shard-1", "members":[{"_id":0,"host":"mongodb-shard1-data-0.mongodb-headless.mongodb.svc.cluster.local:27017","priority":5}]})'

echo "Wait until mongos is ready..."
...
mongosh $mongosAddr --eval <<EOF
    sh.addShard("mongodb-shard-0/mongodb-shard0-data-0.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-0/mongodb-shard0-data-1.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-0/mongodb-shard0-data-2.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-1/mongodb-shard1-data-0.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-1/mongodb-shard1-data-1.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-1/mongodb-shard1-data-2.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.enableSharding("yorkie-meta");
  • to-be
# configmap.yaml

echo "Wait until config server is ready..."
...
mongosh $configsvrAddr --eval 'rs.initiate({"_id":"mongodb-configsvr", "members":[
    {"_id":0,"host":"mongodb-configsvr-0.mongodb-headless.mongodb.svc.cluster.local:27017","priority":5 }
    {"_id":1,"host":"mongodb-configsvr-1.mongodb-headless.mongodb.svc.cluster.local:27017", }
    {"_id":2,"host":"mongodb-configsvr-2.mongodb-headless.mongodb.svc.cluster.local:27017", }
]})' 

echo "Wait until shard0 is ready..."
...
mongosh $shardsvrAddr --eval 'rs.initiate({"_id":"mongodb-shard-0", "members":[
    {"_id":0,"host":"mongodb-shard0-data-0.mongodb-headless.mongodb.svc.cluster.local:27017","priority":5 }
    {"_id":1,"host":"mongodb-shard0-data-1.mongodb-headless.mongodb.svc.cluster.local:27017", }
    {"_id":2,"host":"mongodb-shard0-data-2.mongodb-headless.mongodb.svc.cluster.local:27017", }
]})' 

echo "Wait until shard1 is ready..."
...
mongosh $shardsvrAddr --eval 'rs.initiate({"_id":"mongodb-shard-1", "members":[
    {"_id":0,"host":"mongodb-shard1-data-0.mongodb-headless.mongodb.svc.cluster.local:27017","priority":5 }
    {"_id":1,"host":"mongodb-shard1-data-1.mongodb-headless.mongodb.svc.cluster.local:27017", }
    {"_id":2,"host":"mongodb-shard1-data-2.mongodb-headless.mongodb.svc.cluster.local:27017", }
]})'

echo "Wait until mongos is ready..."
...
mongosh $mongosAddr --eval <<EOF
    sh.addShard("mongodb-shard-0/mongodb-shard0-data-0.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-0/mongodb-shard0-data-1.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-0/mongodb-shard0-data-2.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-1/mongodb-shard1-data-0.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-1/mongodb-shard1-data-1.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.addShard("mongodb-shard-1/mongodb-shard1-data-2.mongodb-headless.mongodb.svc.cluster.local:27017");
    sh.enableSharding("yorkie-meta");

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Addressed and resolved all CodeRabbit review comments
  • Didn't break anything

Summary by CodeRabbit

  • New Features

    • Enhanced initialization for MongoDB sharded cluster setup, allowing dynamic configuration of replica sets for both config and shard servers.
    • Improved flexibility in member definitions based on user-defined values.
  • Bug Fixes

    • Added readiness checks to ensure proper setup of config server, shard servers, and mongos components.

@emplam27 emplam27 self-assigned this Dec 3, 2024
Copy link

coderabbitai bot commented Dec 3, 2024

Walkthrough

The changes in this pull request modify a Kubernetes ConfigMap template for a MongoDB sharded cluster setup. The updates introduce dynamic initialization for replica sets of both the config server and shard servers, replacing static commands with loops that generate member definitions based on configuration values. This allows for a flexible number of replicas defined by the user. The overall control flow remains the same, maintaining readiness checks for all components during the setup process.

Changes

File Change Summary
build/charts/yorkie-cluster/charts/yorkie-mongodb/templates/sharded/configmap.yaml Enhanced initialization of replica sets for config and shard servers with dynamic member construction using loops based on replicaCount values.

Possibly related PRs

  • Include yorkie-mongodb to yorkie-cluster Helm Chart #1031: This PR includes the yorkie-mongodb chart as a subchart within the yorkie-cluster Helm chart, which relates to the changes in the main PR that enhance the initialization of MongoDB replica sets, as both involve the configuration and deployment of MongoDB in a Kubernetes environment.

Suggested labels

enhancement 🌟

Suggested reviewers

  • krapie
  • hackerwins

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (1)
build/charts/yorkie-cluster/charts/yorkie-mongodb/templates/sharded/configmap.yaml (1)

Line range hint 16-24: Enhance initialization robustness

The current waitUntilReady function could be improved to handle initialization failures more gracefully and provide better validation:

Consider enhancing the initialization process with these improvements:

 function waitUntilReady {
+  local max_attempts=30
+  local attempt=1
+  local wait_time=5
   while true; do
+    if [ $attempt -gt $max_attempts ]; then
+      echo "Failed to initialize after $max_attempts attempts"
+      exit 1
+    fi
     mongosh $1 --quiet --eval "db.runCommand('ping').ok"
     if [ $? -eq 0 ]; then
+      # Validate replica set status
+      if [[ $1 == *"configsvr"* || $1 == *"shardsvr"* ]]; then
+        replStatus=$(mongosh $1 --quiet --eval "rs.status().ok")
+        if [ "$replStatus" == "1" ]; then
           break
+        fi
+      else
+        break
+      fi
     fi    
-    sleep 5
+    wait_time=$(( wait_time * 2 ))
+    if [ $wait_time -gt 60 ]; then
+      wait_time=60
+    fi
+    echo "Attempt $attempt of $max_attempts failed, waiting ${wait_time}s..."
+    sleep $wait_time
+    attempt=$(( attempt + 1 ))
   done       
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e6cfb15 and 90e2352.

📒 Files selected for processing (1)
  • build/charts/yorkie-cluster/charts/yorkie-mongodb/templates/sharded/configmap.yaml (2 hunks)

Comment on lines +44 to +48
mongosh $shardsvrAddr --eval 'rs.initiate({"_id":"{{ include "shardReplName" (list $.Values.name $i) }}", "members":[
{{- range $j, $e := until ($.Values.sharded.replicaCount.shardsvr | int) }}
{"_id":{{ printf "%d" $j }},"host":"{{ printf "%s" (include "shardReplAddr" (list $.Values.name $i $j $domainSuffix)) }}",{{- if eq $j 0 }}"priority":5{{- end }} }
{{- end }}
]})'
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance shard server replica set configuration

The shard server replica set initialization has similar issues to the config server, plus some shard-specific concerns:

  1. Priority value of 5 is too high
  2. Missing vote configurations
  3. No zone/tag awareness for data locality
  4. No handling of even number of replicas

Consider applying this improved configuration:

 mongosh $shardsvrAddr --eval 'rs.initiate({"_id":"{{ include "shardReplName" (list $.Values.name $i) }}", "members":[
   {{- range $j, $e := until ($.Values.sharded.replicaCount.shardsvr | int) }}
-  {"_id":{{ printf "%d" $j }},"host":"{{ printf "%s" (include "shardReplAddr" (list $.Values.name $i $j $domainSuffix)) }}",{{- if eq $j 0 }}"priority":5{{- end }} }
+  {"_id":{{ printf "%d" $j }},"host":"{{ printf "%s" (include "shardReplAddr" (list $.Values.name $i $j $domainSuffix)) }}","votes":1,
+    {{- if eq $j 0 }}
+    "priority":1,
+    "tags": {"zone": "{{ $.Values.sharded.zones.primary | default "primary" }}"}
+    {{- else }}
+    "priority":0,
+    "tags": {"zone": "{{ $.Values.sharded.zones.secondary | default "secondary" }}"}
+    {{- end }} }
   {{- end }}
+  {{- if eq ($.Values.sharded.replicaCount.shardsvr | int | mod 2) 0 }}
+  ,{"_id":{{ $.Values.sharded.replicaCount.shardsvr | int }},"host":"{{ printf "%s" (include "shardReplAddr" (list $.Values.name $i ($.Values.sharded.replicaCount.shardsvr | int) $domainSuffix)) }}","arbiterOnly":true}
+  {{- end }}
 ]})'

Consider adding the following to your values.yaml to support zone awareness:

sharded:
  zones:
    primary: "zone1"
    secondary: "zone2"

Comment on lines +31 to +35
mongosh $configsvrAddr --eval 'rs.initiate({"_id":"{{ include "configReplName" (list $.Values.name) }}", "members":[
{{- range $i, $e := until ($.Values.sharded.replicaCount.configsvr | int) }}
{"_id":{{ printf "%d" $i }},"host":"{{ printf "%s" (include "configReplAddr" (list $.Values.name $i $domainSuffix)) }}",{{- if eq $i 0 }}"priority":5{{- end }} }
{{- end }}
]})'
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Review replica set configuration parameters

The config server replica set initialization has several areas for improvement:

  1. The priority value of 5 is unusually high. MongoDB typically uses priority values between 0 and 1.
  2. The configuration lacks vote settings which are important for election mechanics.
  3. There's no handling for even number of replicas which could lead to split-brain scenarios.

Consider applying this improved configuration:

 mongosh $configsvrAddr --eval 'rs.initiate({"_id":"{{ include "configReplName" (list $.Values.name) }}", "members":[
   {{- range $i, $e := until ($.Values.sharded.replicaCount.configsvr | int) }}
-  {"_id":{{ printf "%d" $i }},"host":"{{ printf "%s" (include "configReplAddr" (list $.Values.name $i $domainSuffix)) }}",{{- if eq $i 0 }}"priority":5{{- end }} }
+  {"_id":{{ printf "%d" $i }},"host":"{{ printf "%s" (include "configReplAddr" (list $.Values.name $i $domainSuffix)) }}","votes":1,{{- if eq $i 0 }}"priority":1{{- else }}"priority":0{{- end }} }
   {{- end }}
+  {{- if eq ($.Values.sharded.replicaCount.configsvr | int | mod 2) 0 }}
+  ,{"_id":{{ $.Values.sharded.replicaCount.configsvr | int }},"host":"{{ printf "%s" (include "configReplAddr" (list $.Values.name ($.Values.sharded.replicaCount.configsvr | int) $domainSuffix)) }}","arbiterOnly":true}
+  {{- end }}
 ]})'
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
mongosh $configsvrAddr --eval 'rs.initiate({"_id":"{{ include "configReplName" (list $.Values.name) }}", "members":[
{{- range $i, $e := until ($.Values.sharded.replicaCount.configsvr | int) }}
{"_id":{{ printf "%d" $i }},"host":"{{ printf "%s" (include "configReplAddr" (list $.Values.name $i $domainSuffix)) }}",{{- if eq $i 0 }}"priority":5{{- end }} }
{{- end }}
]})'
mongosh $configsvrAddr --eval 'rs.initiate({"_id":"{{ include "configReplName" (list $.Values.name) }}", "members":[
{{- range $i, $e := until ($.Values.sharded.replicaCount.configsvr | int) }}
{"_id":{{ printf "%d" $i }},"host":"{{ printf "%s" (include "configReplAddr" (list $.Values.name $i $domainSuffix)) }}","votes":1,{{- if eq $i 0 }}"priority":1{{- else }}"priority":0{{- end }} }
{{- end }}
{{- if eq ($.Values.sharded.replicaCount.configsvr | int | mod 2) 0 }}
,{"_id":{{ $.Values.sharded.replicaCount.configsvr | int }},"host":"{{ printf "%s" (include "configReplAddr" (list $.Values.name ($.Values.sharded.replicaCount.configsvr | int) $domainSuffix)) }}","arbiterOnly":true}
{{- end }}
]})'

Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution.

@hackerwins hackerwins changed the title Update MongoDB sharding configs in yorkie-mongodb chart configmap Fix Sharding Initialization with Dynamic Replica Set Configuration Dec 5, 2024
@hackerwins hackerwins merged commit 571cf4b into main Dec 5, 2024
1 check passed
@hackerwins hackerwins deleted the emplam27-mongodb-configmap branch December 5, 2024 01:38
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