Skip to content

Commit

Permalink
Add sharding verification steps (#2)
Browse files Browse the repository at this point in the history
* Add sharding verification steps

* Change default bind ip to loopback address
  • Loading branch information
teddyphreak authored Oct 17, 2023
1 parent 3c30a32 commit c09e86a
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ fileignoreconfig:
ignore_detectors: [filecontent]
- filename: templates/systemd.service.j2
ignore_detectors: [filecontent]
- filename: molecule/default/verify.yml
ignore_detectors: [filecontent]
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mongos_service_port: 27017
mongos_service_state: started
mongos_service_enabled: "{{ mongos_service_state != 'stopped' }}"
mongos_config_file: /etc/mongos.yaml
mongos_config_verbosity: 2
mongos_config_bindip: 127.0.0.1
mongos_storage_path: /var/lib/mongodb
mongos_replicaset_config_name: config
mongos_replicaset_config_port: 27017
Expand Down
100 changes: 97 additions & 3 deletions molecule/default/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
roles:
- nephelaiio.plugins
tasks:

- name: Retrieve MongoDB replicaset status
community.mongodb.mongodb_status:
replica_set: "{{ mongos_replicaset_config_name }}"
Expand All @@ -27,7 +26,6 @@
roles:
- nephelaiio.plugins
tasks:

- name: Retrieve MongoDB replicaset status
community.mongodb.mongodb_status:
replica_set: "{{ mongos_replicaset_shard_name }}"
Expand All @@ -48,8 +46,10 @@
become: true
roles:
- nephelaiio.plugins
vars:
test_db: test
test_collection: data
tasks:

- name: Check service state
ansible.builtin.command: "systemctl status {{ mongos_service_name }}"

Expand Down Expand Up @@ -100,3 +100,97 @@
_shard: "{{ _shard_query. }}"
_host_not_found: "{{ }}"
when: _id_not_found

- name: Data load and sharding tests
tags: data
block:

- name: List databases
community.mongodb.mongodb_shell:
db: "admin"
eval: "db.runCommand({listDatabases: 1})"
register: _db_query

- name: Drop test databases
community.mongodb.mongodb_shell:
db: "{{ test_db }}"
eval: "db.dropDatabase()"
vars:
_db_names: "{{ _db_query.transformed_output.databases | map(attribute='name') | list }}"
when: test_db in _db_names

- name: Download test data
ansible.builtin.uri:
url: https://raw.githubusercontent.com/MicrosoftEdge/Demos/main/json-dummy-data/64KB.json
return_content: true
register: _test_data

- name: Transform test data
ansible.builtin.set_fact:
test_json: "{{ _test_json }}"
test_size: "{{ _test_json | length }}"
vars:
_aliases:
id: _id
_test_json: "{{ _test_data.content | from_json | map('alias_keys', _aliases) }}"

- name: Debug test data
ansible.builtin.debug:
msg: "{{ test_json[0] }}"

- name: Write test data
community.mongodb.mongodb_shell:
db: "{{ test_db }}"
eval: "db.{{ test_collection }}.insertMany({{ test_json | to_json }})"

- name: Shard test data
community.mongodb.mongodb_shell:
db: "{{ test_db }}"
eval: |
sh.shardCollection("{{ test_db }}.{{ test_collection }}", { _id: 1 })
vars:
_collection: "{{ test_db }}.{{ test_collection }}"

- name: Query test data size
community.mongodb.mongodb_shell:
db: "{{ test_db }}"
eval: "db.{{ test_collection }}.countDocuments()"
register: _data_query

- name: Verify test data size
ansible.builtin.fail:
msg: "Failed data size check; got {{ _data_size }}, expected {{ test_size }}"
vars:
_data_size: "{{ _data_query.transformed_output | first }}"
when: _data_size != test_size

- name: Query shard status
community.mongodb.mongodb_shell:
db: "admin"
eval: sh.status()
register: _status_query

- name: Set shard facts
ansible.builtin.set_fact:
shards_dbs: "{{ _shard_dbs }}"
shards_data: "{{ _shard_dbs | selectattr('database._id', 'equalto', test_db) | list }}"
vars:
_shard_dbs: "{{ _status_query.transformed_output.value.databases }}"

- name: Debug shard facts
ansible.builtin.debug:
msg: "{{ shards_data | first }}"

- name: Verify shard database info
ansible.builtin.fail:
msg: "Unable to retrieve shard information for db {{ test_db }}"
when: shards_data | length != 1

- name: Verify shard collection info
ansible.builtin.fail:
msg: "Unable to retrieve shard information for collection {{ _collection }}"
vars:
_shard_data: "{{ shards_data | first }}"
_collection_data: "{{ _shard_data.collections }}"
_collection: "{{ test_db }}.{{ test_collection }}"
when: _collection not in _collection_data
2 changes: 2 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
_format: "%s:{{ mongos_replicaset_config_port }}"
_members: "{{ mongos_replicaset_config_members | map('map_format', _format) }}"
_replset_config_members: "{{ _members | join(',') }}"
_verbosity: "{{ mongos_config_verbosity }}"
_bindip: "{{ mongos_config_bindip }}"
register: mongos_config
notify: daemon_restart

Expand Down
4 changes: 2 additions & 2 deletions templates/mongos.j2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ sharding:

net:
port: {{ _port }}
bindIpAll: true
bindIp: "{{ _bindip }}"
ipv6: false

processManagement:
fork: false

systemLog:
verbosity: 2
verbosity: {{ _verbosity }}
traceAllExceptions: true
timeStampFormat: iso8601-utc

0 comments on commit c09e86a

Please sign in to comment.