From b173af7bd2f00475fe575e11720bf77a57544c5e Mon Sep 17 00:00:00 2001 From: Michael Okoko <10512379+idoqo@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:20:17 +0100 Subject: [PATCH] PMM-12510 correctly recognize arbiter nodes (#755) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add networks * accept exit code * use replica set status for node states * drop external links * fix formatting * convert to table tests * fix topology tests * include hello command for arbiter nodes * use parallel tests * add tests for hello metrics * attempt to satisfy golint * use single command for replica setup * improve naming * reduce external calls * add method comments * use isMaster for compatibility * PMM-12712 Shards collector. (#762) * PMM-12712 Shard ID and count. * PMM-12712 Name and count. * PMM-12712 Sharded collector. * PMM-12712 Remove missed print. * PMM-12712 Revert test changes. * PMM-12712 Revert old not related changes. * PMM-12712 Another changes. * PMM-12712 Missed print. * PMM-12712 Align naming with task description. * PMM-12712 Add mongos test client. * PMM-12712 Lint. * PMM-12712 Lint. * PMM-12712 Typo. * PMM-12712 Lint. * PMM-12712 Test higher sleep. * Revert "PMM-12712 Test higher sleep." This reverts commit 88451ba6b8a01d7c9b97daf4ac17a1c19a28f82b. * PMM-12712 Test of pipeline. * PMM-12712 Change. * PMM-12712 Test. * PMM-12712 Test. * PMM-12712 Test. * PMM-12712 Correct aggregation to get chunks info. * PMM-12712 Another progress. * PMM-12702 Small refactor. * PMM-12712 Test. * PMM-12712 Fix, tests. * PMM-12712 Temp. * PMM-12712 Improve sharded test. * PMM-12712 Change in workflow. * PMM-12712 Remove print. * PMM-12712 Init script changes. * PMM-12712 Remove print. * PMM-12712 Bigger sleep. * PMM-12712 Refactor. * PMM-12712 Small refactor. * PMM-12712 Another refactor. * PMM-12712 Fix after refactor. * PMM-12712 Alias. * PMM-12712 Alias for shell in older versions. * PMM-12712 Static test shard. * PMM-12712 Fix another tests, mongo 4 shell script. * PMM-12712 Big sleep test. * PMM-12712 Michael's fix. * PMM-12712 Sleep. * PMM-12712 Env. * PMM-12712 Test. * PMM-12712 Fix test. * PMM-12712 Bigger sleep after changes. * PMM-12712 Remove duplicate lines. * PMM-12712 Renaming from sharded to shards. * PMM-12712 Better script to detect proper mongo client. * PMM-12712 Init script refactor. * PMM-12712 Fix. * PMM-12712 Small refactor. * Update main.go Co-authored-by: Michael Okoko <10512379+idoqo@users.noreply.github.com> * Update exporter/shards_collector.go Co-authored-by: Michael Okoko <10512379+idoqo@users.noreply.github.com> * PMM-12712 Remove mongo 6 for now, skip test for shards. * PMM-12712 Revert sleep length. * PMM-12712 Mongo client based on vendor. --------- Co-authored-by: Michael Okoko <10512379+idoqo@users.noreply.github.com> * add wait to docker compose command * remove wait from docker compose command --------- Co-authored-by: Jiří Čtvrtka <62988319+JiriCtvrtka@users.noreply.github.com> --- Makefile | 2 +- docker-compose.yml | 91 ++++++---- docker/scripts/init-shard.sh | 20 ++- docker/scripts/setup.sh | 42 +++-- exporter/diagnostic_data_collector.go | 6 +- exporter/diagnostic_data_collector_test.go | 147 ++++++++++------ exporter/exporter.go | 10 ++ exporter/shards_collector.go | 189 +++++++++++++++++++++ exporter/shards_collector_test.go | 69 ++++++++ exporter/topology_info.go | 2 +- exporter/topology_info_test.go | 32 ++-- exporter/v1_compatibility.go | 65 +++++-- exporter/v1_compatibility_test.go | 92 ++++++++-- internal/proto/replstatus.go | 20 ++- internal/tu/testutils.go | 18 +- internal/util/util.go | 22 ++- main.go | 2 + 17 files changed, 667 insertions(+), 162 deletions(-) create mode 100644 exporter/shards_collector.go create mode 100644 exporter/shards_collector_test.go diff --git a/Makefile b/Makefile index 65d91fcc1..25866ddcd 100644 --- a/Makefile +++ b/Makefile @@ -104,7 +104,7 @@ test-race: env ## Run all tests with race flag. go test -race -v -timeout 30s ./... test-cluster: env ## Starts MongoDB test cluster. Use env var TEST_MONGODB_IMAGE to set flavor and version. Example: TEST_MONGODB_IMAGE=mongo:3.6 make test-cluster - docker compose up -d --wait + docker compose up -d test-cluster-clean: env ## Stops MongoDB test cluster. docker compose down --remove-orphans diff --git a/docker-compose.yml b/docker-compose.yml index 3fb2743f9..92e51f9ef 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,9 +6,12 @@ services: ports: - "${TEST_MONGODB_S1_PRIMARY_PORT:-17001}:27017" command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 - links: - - mongo-1-2:mongo-1-2 - - mongo-1-3:mongo-1-3 + networks: + - rs1 + depends_on: + - "mongo-1-2" + - "mongo-1-3" + - "mongo-1-arbiter" mongo-1-2: container_name: "mongo-1-2" @@ -16,6 +19,8 @@ services: ports: - "${TEST_MONGODB_S1_SECONDARY1_PORT:-17002}:27017" command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 + networks: + - rs1 mongo-1-3: container_name: "mongo-1-3" @@ -23,6 +28,8 @@ services: ports: - "${TEST_MONGODB_S1_SECONDARY2_PORT:-17003}:27017" command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 + networks: + - rs1 mongo-1-arbiter: container_name: "mongo-1-arbiter" @@ -30,6 +37,8 @@ services: ports: - "${TEST_MONGODB_S1_ARBITER:-17011}:27017" command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 + networks: + - rs1 mongo-rs1-setup: container_name: "mongo-rs1-setup" @@ -39,11 +48,6 @@ services: - "mongo-1-2" - "mongo-1-3" - "mongo-1-arbiter" - links: - - mongo-1-1:mongo-1-1 - - mongo-1-2:mongo-1-2 - - mongo-1-3:mongo-1-3 - - mongo-1-arbiter:mongo-1-arbiter volumes: - ./docker/scripts:/scripts environment: @@ -52,7 +56,10 @@ services: - MONGO3=mongo-1-3 - ARBITER=mongo-1-arbiter - RS=rs1 + - VERSION=${TEST_MONGODB_IMAGE} entrypoint: [ "/scripts/setup.sh" ] + networks: + - rs1 mongo-2-2: container_name: "mongo-2-2" @@ -60,6 +67,8 @@ services: ports: - "${TEST_MONGODB_S2_PRIMARY_PORT:-17004}:27017" command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 + networks: + - rs2 mongo-2-3: container_name: "mongo-2-3" @@ -67,6 +76,8 @@ services: ports: - "${TEST_MONGODB_S2_SECONDARY1_PORT:-17005}:27017" command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 + networks: + - rs2 mongo-2-1: container_name: "mongo-2-1" @@ -74,9 +85,8 @@ services: ports: - "${TEST_MONGODB_S2_SECONDARY2_PORT:-17006}:27017" command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 - links: - - mongo-2-2:mongo-2-2 - - mongo-2-3:mongo-2-3 + networks: + - rs2 mongo-2-arbiter: container_name: "mongo-2-arbiter" @@ -84,6 +94,8 @@ services: ports: - "${TEST_MONGODB_S2_ARBITER:-17012}:27017" command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 + networks: + - rs2 mongo-rs2-setup: container_name: "mongo-rs2-setup" @@ -93,19 +105,18 @@ services: - "mongo-2-2" - "mongo-2-3" - "mongo-2-arbiter" - links: - - mongo-2-1:mongo-2-1 - - mongo-2-2:mongo-2-2 - - mongo-2-3:mongo-2-3 volumes: - ./docker/scripts:/scripts environment: - - MONGO1=mongo-2-1 - - MONGO2=mongo-2-2 + - MONGO1=mongo-2-2 + - MONGO2=mongo-2-1 - MONGO3=mongo-2-3 - ARBITER=mongo-2-arbiter - RS=rs2 + - VERSION=${TEST_MONGODB_IMAGE} entrypoint: [ "/scripts/setup.sh" ] + networks: + - rs2 # Config servers mongo-cnf-2: @@ -114,6 +125,8 @@ services: ports: - "${TEST_MONGODB_CONFIGSVR1_PORT:-17007}:27017" command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16 + networks: + - cnf-serv mongo-cnf-3: container_name: "mongo-cnf-3" @@ -121,6 +134,8 @@ services: ports: - "${TEST_MONGODB_CONFIGSVR2_PORT:-17008}:27017" command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16 + networks: + - cnf-serv mongo-cnf-1: container_name: "mongo-cnf-1" @@ -128,9 +143,11 @@ services: ports: - "${TEST_MONGODB_CONFIGSVR3_PORT:-17009}:27017" command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16 - links: - - mongo-cnf-2:mongo-cnf-2 - - mongo-cnf-3:mongo-cnf-3 + networks: + - cnf-serv + depends_on: + - mongo-cnf-2 + - mongo-cnf-3 mongo-cnf-setup: container_name: "mongo-cnf-setup" @@ -139,10 +156,6 @@ services: - "mongo-cnf-1" - "mongo-cnf-2" - "mongo-cnf-3" - links: - - mongo-cnf-1:mongo-cnf-1 - - mongo-cnf-2:mongo-cnf-2 - - mongo-cnf-3:mongo-cnf-3 volumes: - ./docker/scripts:/scripts environment: @@ -151,27 +164,25 @@ services: - MONGO3=mongo-cnf-3 - RS=cnf-serv - PORT=27017 + - VERSION=${TEST_MONGODB_IMAGE} entrypoint: [ "/scripts/setup.sh","cnf_servers" ] + networks: + - cnf-serv mongos: container_name: "mongos" image: ${TEST_MONGODB_IMAGE:-mongo:4.2} ports: - "${TEST_MONGODB_MONGOS_PORT:-17000}:27017" + networks: + - mongo-shard + - rs1 + - rs2 + - cnf-serv depends_on: - "mongo-rs1-setup" - "mongo-rs2-setup" - "mongo-cnf-setup" - external_links: - - mongo-cnf-1:mongo-cnf-1 - - mongo-cnf-2:mongo-cnf-2 - - mongo-cnf-3:mongo-cnf-3 - - mongo-1-1:mongo-1-1 - - mongo-1-2:mongo-1-2 - - mongo-1-3:mongo-1-3 - - mongo-2-1:mongo-2-1 - - mongo-2-2:mongo-2-2 - - mongo-2-3:mongo-2-3 command: mongos --configdb cnf-serv/mongo-cnf-1:27017,mongo-cnf-2:27017,mongo-cnf-3:27017 --port 27017 --bind_ip 0.0.0.0 mongo-shard-setup: @@ -179,8 +190,8 @@ services: image: ${TEST_MONGODB_IMAGE:-mongo:4.2} depends_on: - "mongos" - links: - - mongos:mongos + networks: + - mongo-shard volumes: - ./docker/scripts:/scripts environment: @@ -197,6 +208,7 @@ services: - PORT1=27017 - PORT2=27017 - PORT3=27017 + - VERSION=${TEST_MONGODB_IMAGE} entrypoint: [ "/scripts/init-shard.sh" ] restart: on-failure:20 @@ -208,6 +220,7 @@ services: command: mongod --port 27017 --oplogSize 16 standalone-encrypted: + user: root container_name: "standalone-encrypted" image: percona/percona-server-mongodb:5.0.13-11 ports: @@ -216,3 +229,9 @@ services: - ./docker/secret/mongodb_secrets.txt:/secret/mongodb_secrets.txt - ./docker/scripts:/scripts command: /scripts/run-mongodb-encrypted.sh + +networks: + rs1: + rs2: + cnf-serv: + mongo-shard: diff --git a/docker/scripts/init-shard.sh b/docker/scripts/init-shard.sh index 8467b74f8..f5de49498 100755 --- a/docker/scripts/init-shard.sh +++ b/docker/scripts/init-shard.sh @@ -1,4 +1,16 @@ #!/bin/bash +# `mongosh` is used starting from MongoDB 5.x +MONGODB_CLIENT="mongosh --quiet" +PARSED=(${VERSION//:/ }) +MONGODB_VERSION=${PARSED[1]} +MONGODB_VENDOR=${PARSED[0]} +if [ "`echo ${MONGODB_VERSION} | cut -c 1`" = "4" ]; then + MONGODB_CLIENT="mongo" +fi +if [ "`echo ${MONGODB_VERSION} | cut -c 1`" = "5" ] && [ ${MONGODB_VENDOR} == "percona/percona-server-mongodb" ]; then + MONGODB_CLIENT="mongo" +fi +echo "MongoDB vendor, client and version: ${MONGODB_VENDOR} ${MONGODB_CLIENT} ${MONGODB_VERSION}" mongodb1=`getent hosts ${MONGOS} | awk '{ print $1 }'` @@ -17,7 +29,7 @@ mongodb33=`getent hosts ${MONGO33} | awk '{ print $1 }'` port=${PORT:-27017} echo "Waiting for startup.." -until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do +until ${MONGODB_CLIENT} --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do printf '.' sleep 1 done @@ -25,8 +37,12 @@ done echo "Started.." echo init-shard.sh time now: `date +"%T" ` -mongo --host ${mongodb1}:${port} </dev/null; do +until ${MONGODB_CLIENT} --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do printf '.' sleep 1 done @@ -19,10 +31,11 @@ echo setup.sh time now: `date +"%T" ` function cnf_servers() { - echo "setup cnf servers" - mongo --host ${mongodb1}:${port} <