From b48607b7f1beaa76700ef20388df4c95f428b5d5 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 09:04:41 +0300 Subject: [PATCH 1/2] osd: Don't exit on finding own state key, arm lease timers only after the grant When the reply to the /osd/state creation transaction was lost (timeout) but etcd applied it, the retry found the key with the OSD's own lease and the OSD exited with "already exists, OSD N is still up". Treat a key holding our own lease as created. acquire_lease() armed the periodic renewal and statistics timers on every call, so each failed lease grant added one more of each. Arm them once, when the lease is granted. --- .gitea/workflows/test.yml | 18 +++++++++++ src/osd/osd_cluster.cpp | 20 ++++++------ tests/run_tests.sh | 1 + tests/test_osd_state_reply_lost.sh | 49 ++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 9 deletions(-) create mode 100755 tests/test_osd_state_reply_lost.sh diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index ad781d0c..d99072ad 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -239,6 +239,24 @@ jobs: echo "" done + test_osd_state_reply_lost: + runs-on: ubuntu-latest + needs: build + container: ${{env.TEST_IMAGE}}:${{github.sha}} + steps: + - name: Run test + id: test + timeout-minutes: 3 + run: /root/vitastor/tests/test_osd_state_reply_lost.sh + - name: Print logs + if: always() && steps.test.outcome == 'failure' + run: | + for i in /root/vitastor/testdata/*.log /root/vitastor/testdata/*.txt; do + echo "-------- $i --------" + cat $i + echo "" + done + test_etcd_fail: runs-on: ubuntu-latest needs: build diff --git a/src/osd/osd_cluster.cpp b/src/osd/osd_cluster.cpp index cdf82c75..89141ec6 100644 --- a/src/osd/osd_cluster.cpp +++ b/src/osd/osd_cluster.cpp @@ -550,6 +550,14 @@ void osd_t::acquire_lease() return; } etcd_lease_id = data["ID"].string_value(); + tfd->set_timer(etcd_report_interval*1000, true, [this](int timer_id) + { + renew_lease(false); + }); + tfd->set_timer(etcd_stats_interval*1000, true, [this](int timer_id) + { + report_statistics(); + }); create_osd_state(); // Flush PG state reports that were held back until the lease existed report_pg_states(); @@ -559,14 +567,6 @@ void osd_t::acquire_lease() (config["etcd_address"].is_string() ? config["etcd_address"].string_value() : config["etcd_address"].dump()).c_str(), etcd_report_interval, etcd_stats_interval ); - tfd->set_timer(etcd_report_interval*1000, true, [this](int timer_id) - { - renew_lease(false); - }); - tfd->set_timer(etcd_stats_interval*1000, true, [this](int timer_id) - { - report_statistics(); - }); } // Report "up" state once, then keep it alive using the lease @@ -618,7 +618,9 @@ void osd_t::create_osd_state() }); return; } - if (!data["succeeded"].bool_value()) + // A key with our own lease means an earlier attempt went through but its reply was lost + if (!data["succeeded"].bool_value() && + data["responses"][0]["response_range"]["kvs"][0]["lease"].string_value() != etcd_lease_id) { // OSD is already up auto kv = st_cli->parse_etcd_kv(data["responses"][0]["response_range"]["kvs"][0]); diff --git a/tests/run_tests.sh b/tests/run_tests.sh index caac41b7..74c2b3e8 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -22,6 +22,7 @@ SCHEME=ec ./test_change_pg_count.sh ./test_etcd_fail.sh ANTIETCD=1 ./test_etcd_fail.sh +./test_osd_state_reply_lost.sh ETCD_SCHEME=https ./test_etcd_fail.sh ETCD_SCHEME=https ANTIETCD=1 ./test_etcd_fail.sh diff --git a/tests/test_osd_state_reply_lost.sh b/tests/test_osd_state_reply_lost.sh new file mode 100755 index 00000000..000632ee --- /dev/null +++ b/tests/test_osd_state_reply_lost.sh @@ -0,0 +1,49 @@ +#!/bin/bash -ex + +# etcd applies an OSD's /osd/state/N creation but the reply comes after the OSD's request +# timeout: the retry finds the key with the OSD's own lease, and the OSD must not exit + +PROXY_PORT=${PROXY_PORT:-12399} + +# OSDs reach etcd through a proxy that holds back the reply to the first /osd/state/N +# creation of each OSD for 1.5 s +node -e ' +const [port, target] = process.argv.slice(1), seen = {}; +require("net").createServer(c => { + const e = require("net").connect(+target, "127.0.0.1"), q = []; + let until = 0, timer = null; + const drain = () => { + timer = null; + while (q.length && q[0][0] <= Date.now()) c.write(q.shift()[1]); + if (q.length) timer = setTimeout(drain, q[0][0] - Date.now()); + }; + c.on("data", d => { + for (const m of d.toString().matchAll(/"key": *"([A-Za-z0-9+\/=]+)"/g)) { + const key = Buffer.from(m[1], "base64").toString(); + if (/\/osd\/state\/\d+$/.test(key) && !seen[key]) { + seen[key] = true; + until = Date.now() + 1500; + } + } + e.write(d); + }); + e.on("data", d => { + q.push([ Math.max(q.length ? q[q.length-1][0] : 0, until, Date.now()), d ]); + if (!timer) timer = setTimeout(drain, q[0][0] - Date.now()); + }); + c.on("error", () => e.destroy()); + e.on("error", () => c.destroy()); + c.on("close", () => e.destroy()); + e.on("close", () => c.destroy()); +}).listen(+port, "127.0.0.1"); +' $PROXY_PORT ${ETCD_PORT:-12379} & +OSD_ARGS="$OSD_ARGS --etcd_address http://127.0.0.1:$PROXY_PORT" + +. `dirname $0`/run_3osds.sh + +for i in $(seq 1 $OSD_COUNT); do + kill -0 $(eval echo \$OSD${i}_PID) || format_error "OSD $i exited" + grep -q 'Error creating OSD state key' ./testdata/osd$i.log || format_error "OSD $i did not lose the reply" +done + +format_green OK -- 2.47.3 From 8483f86940683838e02ca72c25666eabb16e73d0 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 22:48:25 +0300 Subject: [PATCH 2/2] Drop test_osd_state_reply_lost from the PR as asked in review --- .gitea/workflows/test.yml | 18 ----------- tests/run_tests.sh | 1 - tests/test_osd_state_reply_lost.sh | 49 ------------------------------ 3 files changed, 68 deletions(-) delete mode 100755 tests/test_osd_state_reply_lost.sh diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index d99072ad..ad781d0c 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -239,24 +239,6 @@ jobs: echo "" done - test_osd_state_reply_lost: - runs-on: ubuntu-latest - needs: build - container: ${{env.TEST_IMAGE}}:${{github.sha}} - steps: - - name: Run test - id: test - timeout-minutes: 3 - run: /root/vitastor/tests/test_osd_state_reply_lost.sh - - name: Print logs - if: always() && steps.test.outcome == 'failure' - run: | - for i in /root/vitastor/testdata/*.log /root/vitastor/testdata/*.txt; do - echo "-------- $i --------" - cat $i - echo "" - done - test_etcd_fail: runs-on: ubuntu-latest needs: build diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 74c2b3e8..caac41b7 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -22,7 +22,6 @@ SCHEME=ec ./test_change_pg_count.sh ./test_etcd_fail.sh ANTIETCD=1 ./test_etcd_fail.sh -./test_osd_state_reply_lost.sh ETCD_SCHEME=https ./test_etcd_fail.sh ETCD_SCHEME=https ANTIETCD=1 ./test_etcd_fail.sh diff --git a/tests/test_osd_state_reply_lost.sh b/tests/test_osd_state_reply_lost.sh deleted file mode 100755 index 000632ee..00000000 --- a/tests/test_osd_state_reply_lost.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash -ex - -# etcd applies an OSD's /osd/state/N creation but the reply comes after the OSD's request -# timeout: the retry finds the key with the OSD's own lease, and the OSD must not exit - -PROXY_PORT=${PROXY_PORT:-12399} - -# OSDs reach etcd through a proxy that holds back the reply to the first /osd/state/N -# creation of each OSD for 1.5 s -node -e ' -const [port, target] = process.argv.slice(1), seen = {}; -require("net").createServer(c => { - const e = require("net").connect(+target, "127.0.0.1"), q = []; - let until = 0, timer = null; - const drain = () => { - timer = null; - while (q.length && q[0][0] <= Date.now()) c.write(q.shift()[1]); - if (q.length) timer = setTimeout(drain, q[0][0] - Date.now()); - }; - c.on("data", d => { - for (const m of d.toString().matchAll(/"key": *"([A-Za-z0-9+\/=]+)"/g)) { - const key = Buffer.from(m[1], "base64").toString(); - if (/\/osd\/state\/\d+$/.test(key) && !seen[key]) { - seen[key] = true; - until = Date.now() + 1500; - } - } - e.write(d); - }); - e.on("data", d => { - q.push([ Math.max(q.length ? q[q.length-1][0] : 0, until, Date.now()), d ]); - if (!timer) timer = setTimeout(drain, q[0][0] - Date.now()); - }); - c.on("error", () => e.destroy()); - e.on("error", () => c.destroy()); - c.on("close", () => e.destroy()); - e.on("close", () => c.destroy()); -}).listen(+port, "127.0.0.1"); -' $PROXY_PORT ${ETCD_PORT:-12379} & -OSD_ARGS="$OSD_ARGS --etcd_address http://127.0.0.1:$PROXY_PORT" - -. `dirname $0`/run_3osds.sh - -for i in $(seq 1 $OSD_COUNT); do - kill -0 $(eval echo \$OSD${i}_PID) || format_error "OSD $i exited" - grep -q 'Error creating OSD state key' ./testdata/osd$i.log || format_error "OSD $i did not lose the reply" -done - -format_green OK -- 2.47.3