From ad24b63f9ace755492bfe59857fcebe68a8e509b Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Fri, 22 Dec 2023 16:50:58 +0800 Subject: [PATCH 01/13] fix: apisix restart failed (#10495) --- apisix/cli/ops.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 0eaebae56c43..a2a6323bcea7 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -805,15 +805,14 @@ local function start(env, ...) local ok, err, err_no = signal.kill(pid, signone) if ok then - print("APISIX is running...") - return + print("APISIX is running... and now kill ...") -- no such process elseif err_no ~= errno.ESRCH then print(err) return end - print("nginx.pid exists but there's no corresponding process with pid ", pid, + print("nginx.pid exists ", pid, ", the file will be overwritten") end From 3251695178bd63579d535514b52f537cb92f8a76 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Fri, 22 Dec 2023 17:45:49 +0800 Subject: [PATCH 02/13] fix: apisix restart failed use sleep method(#10495) --- apisix/cli/ops.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index a2a6323bcea7..f92e8847e5d8 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -805,14 +805,15 @@ local function start(env, ...) local ok, err, err_no = signal.kill(pid, signone) if ok then - print("APISIX is running... and now kill ...") + print("APISIX is running...") + return -- no such process elseif err_no ~= errno.ESRCH then print(err) return end - print("nginx.pid exists ", pid, + print("nginx.pid exists but there's no corresponding process with pid ", pid, ", the file will be overwritten") end @@ -915,10 +916,15 @@ local function stop(env) end +function sleep(n) + os.execute("sleep " .. tonumber(n)) +end + local function restart(env) -- test configuration test(env) stop(env) + sleep(1) start(env) end From cf91b443bf037bdae3d32ce9d588b5697755f7ab Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Fri, 22 Dec 2023 22:02:18 +0800 Subject: [PATCH 03/13] fix: apisix restart failed use sleep method(#10495) Signed-off-by: wuhanqing --- apisix/cli/ops.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index f92e8847e5d8..0e0d565cc747 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -916,7 +916,7 @@ local function stop(env) end -function sleep(n) +local function sleep(n) os.execute("sleep " .. tonumber(n)) end From 13c975bddd1e8d621b271d8f5357791c9dc6a295 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Sat, 23 Dec 2023 15:03:44 +0800 Subject: [PATCH 04/13] fix: apisix restart failed use sleep method(#10495) Signed-off-by: wuhanqing --- apisix/cli/ops.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 0e0d565cc747..073d3ecb2d25 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -917,7 +917,7 @@ end local function sleep(n) - os.execute("sleep " .. tonumber(n)) + execute("sleep " .. tonumber(n)) end local function restart(env) From 7c3ae834d28572e5254a13caab17b5186eb3f275 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Tue, 26 Dec 2023 09:34:17 +0800 Subject: [PATCH 05/13] fix: when disable ipv6 leads to err (#10857) Signed-off-by: wuhanqing --- apisix/cli/ops.lua | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 073d3ecb2d25..5231dcb3e8ca 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -805,7 +805,7 @@ local function start(env, ...) local ok, err, err_no = signal.kill(pid, signone) if ok then - print("APISIX is running...") + print("the old process is still running, the new one won't be started!") return -- no such process elseif err_no ~= errno.ESRCH then @@ -920,15 +920,33 @@ local function sleep(n) execute("sleep " .. tonumber(n)) end +local function wait_stop_finish(env, n) + + for i = 1, n do + -- check running + local pid_path = env.apisix_home .. "/logs/nginx.pid" + local pid = util.read_file(pid_path) + pid = tonumber(pid) + if not pid or pid == 0 then + print("old apisix process has stopped") + return + else + sleep(1) + end + end + +end + local function restart(env) -- test configuration test(env) stop(env) - sleep(1) + wait_stop_finish(env,3) start(env) end + local function reload(env) -- reinit nginx.conf init(env) From e2b15bceecbc3a0ee7e7d1c8628820f031d0ea80 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Tue, 26 Dec 2023 19:14:13 +0800 Subject: [PATCH 06/13] fix: apisix restart failed use sleep method(#10495) Signed-off-by: wuhanqing --- apisix/cli/ops.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 5231dcb3e8ca..446ad614a2a5 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -921,7 +921,6 @@ local function sleep(n) end local function wait_stop_finish(env, n) - for i = 1, n do -- check running local pid_path = env.apisix_home .. "/logs/nginx.pid" @@ -934,7 +933,6 @@ local function wait_stop_finish(env, n) sleep(1) end end - end local function restart(env) From 957e80782e7649182897d8a1e92fac5720319155 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Tue, 26 Dec 2023 19:20:59 +0800 Subject: [PATCH 07/13] fix: apisix restart failed use sleep method(#10495) Signed-off-by: wuhanqing --- apisix/cli/ops.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 446ad614a2a5..b0b5bd77ae3b 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -920,13 +920,14 @@ local function sleep(n) execute("sleep " .. tonumber(n)) end + local function wait_stop_finish(env, n) for i = 1, n do -- check running local pid_path = env.apisix_home .. "/logs/nginx.pid" local pid = util.read_file(pid_path) pid = tonumber(pid) - if not pid or pid == 0 then + if not pid then print("old apisix process has stopped") return else @@ -935,16 +936,16 @@ local function wait_stop_finish(env, n) end end + local function restart(env) -- test configuration test(env) stop(env) - wait_stop_finish(env,3) + wait_stop_finish(env, 3) start(env) end - local function reload(env) -- reinit nginx.conf init(env) From bf1f9c407ccfb84227c840c098746482172a6756 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Wed, 27 Dec 2023 09:09:21 +0800 Subject: [PATCH 08/13] fix: apisix restart failed use sleep method(#10495) Signed-off-by: wuhanqing --- apisix/cli/ops.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index b0b5bd77ae3b..25c70e4ee4c1 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -921,7 +921,7 @@ local function sleep(n) end -local function wait_stop_finish(env, n) +local function wait_stop_finish(env, n) for i = 1, n do -- check running local pid_path = env.apisix_home .. "/logs/nginx.pid" From a4a78b1361c7aabb70c9441bfeba368d5ef15625 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Wed, 27 Dec 2023 09:19:21 +0800 Subject: [PATCH 09/13] fix: apisix restart failed use sleep method(#10495) Signed-off-by: wuhanqing --- apisix/cli/ops.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 25c70e4ee4c1..d81f28fa586d 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -805,6 +805,7 @@ local function start(env, ...) local ok, err, err_no = signal.kill(pid, signone) if ok then + print("APISIX is running...") print("the old process is still running, the new one won't be started!") return -- no such process From 388af950b555cb576f217b41d9c34da3c3a76be8 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Tue, 2 Jan 2024 14:48:57 +0800 Subject: [PATCH 10/13] fix: apisix restart failed use sleep method(#10495) --- apisix/cli/ops.lua | 1 - t/cli/test_cmd.sh | 4 ++-- t/cli/test_validate_config.sh | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index d81f28fa586d..25c70e4ee4c1 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -805,7 +805,6 @@ local function start(env, ...) local ok, err, err_no = signal.kill(pid, signone) if ok then - print("APISIX is running...") print("the old process is still running, the new one won't be started!") return -- no such process diff --git a/t/cli/test_cmd.sh b/t/cli/test_cmd.sh index 2f3a46cd2070..60c2d3535e79 100755 --- a/t/cli/test_cmd.sh +++ b/t/cli/test_cmd.sh @@ -24,7 +24,7 @@ git checkout conf/config.yaml # check restart with old nginx.pid exist echo "-1" > logs/nginx.pid out=$(./bin/apisix start 2>&1 || true) -if echo "$out" | grep "APISIX is running"; then +if echo "$out" | grep "the old process is still running"; then rm logs/nginx.pid echo "failed: should reject bad nginx.pid" exit 1 @@ -50,7 +50,7 @@ echo "pass: no corresponding process" # check running when run repeatedly out=$(make run; make run || true) -if ! echo "$out" | grep "APISIX is running"; then +if ! echo "$out" | grep "the old process is still running"; then echo "failed: should find APISIX running" exit 1 fi diff --git a/t/cli/test_validate_config.sh b/t/cli/test_validate_config.sh index 8db581684332..29bf8df5da85 100755 --- a/t/cli/test_validate_config.sh +++ b/t/cli/test_validate_config.sh @@ -142,7 +142,7 @@ nginx_config: # apisix restart out=$(./bin/apisix restart 2>&1 || true) -if ! (echo "$out" | grep "\[emerg\] unknown directive \"notexist\"") && ! (echo "$out" | grep "APISIX is running"); then +if ! (echo "$out" | grep "\[emerg\] unknown directive \"notexist\"") && ! (echo "$out" | grep "the old process is still running"); then echo "failed: should restart failed when configuration invalid" exit 1 fi From ea12554e5bb5511db78c82935d916caaabdd977a Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Tue, 2 Jan 2024 18:42:36 +0800 Subject: [PATCH 11/13] fix: add test sleep 1 when stop then start immediately (#10495) --- t/cli/test_prometheus_stream.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/t/cli/test_prometheus_stream.sh b/t/cli/test_prometheus_stream.sh index abf960e776a5..0de5ac4f644f 100755 --- a/t/cli/test_prometheus_stream.sh +++ b/t/cli/test_prometheus_stream.sh @@ -64,6 +64,8 @@ make stop echo "passed: prometheus works when both http & stream are enabled" +sleep 1 #wait for stop finish + echo " apisix: proxy_mode: stream From 42e08c9af4dc1ed6288068ec2b4617151dc1f4e7 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Wed, 3 Jan 2024 09:22:45 +0800 Subject: [PATCH 12/13] fix: simplify the logic in wait_stop_finsh and start (#10495) --- apisix/cli/ops.lua | 26 ++++++++++++++++---------- t/cli/test_cmd.sh | 4 ++-- t/cli/test_validate_config.sh | 2 +- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 25c70e4ee4c1..fe82df366d34 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -767,6 +767,17 @@ local function cleanup(env) end +local function check_running(env) + local pid_path = env.apisix_home .. "/logs/nginx.pid" + local pid = util.read_file(pid_path) + pid = tonumber(pid) + if not pid then + return false, -1 + end + return true, pid +end + + local function start(env, ...) cleanup(env) @@ -792,10 +803,8 @@ local function start(env, ...) end -- check running - local pid_path = env.apisix_home .. "/logs/nginx.pid" - local pid = util.read_file(pid_path) - pid = tonumber(pid) - if pid then + local running, pid = check_running(env) + if running then if pid <= 0 then print("invalid pid") return @@ -805,7 +814,7 @@ local function start(env, ...) local ok, err, err_no = signal.kill(pid, signone) if ok then - print("the old process is still running, the new one won't be started!") + print("the old APISIX is still running, the new one will not start") return -- no such process elseif err_no ~= errno.ESRCH then @@ -924,11 +933,8 @@ end local function wait_stop_finish(env, n) for i = 1, n do -- check running - local pid_path = env.apisix_home .. "/logs/nginx.pid" - local pid = util.read_file(pid_path) - pid = tonumber(pid) - if not pid then - print("old apisix process has stopped") + local running = check_running(env) + if not running then return else sleep(1) diff --git a/t/cli/test_cmd.sh b/t/cli/test_cmd.sh index 60c2d3535e79..fa93465a66e7 100755 --- a/t/cli/test_cmd.sh +++ b/t/cli/test_cmd.sh @@ -24,7 +24,7 @@ git checkout conf/config.yaml # check restart with old nginx.pid exist echo "-1" > logs/nginx.pid out=$(./bin/apisix start 2>&1 || true) -if echo "$out" | grep "the old process is still running"; then +if echo "$out" | grep "the old APISIX is still running"; then rm logs/nginx.pid echo "failed: should reject bad nginx.pid" exit 1 @@ -50,7 +50,7 @@ echo "pass: no corresponding process" # check running when run repeatedly out=$(make run; make run || true) -if ! echo "$out" | grep "the old process is still running"; then +if ! echo "$out" | grep "the old APISIX is still running"; then echo "failed: should find APISIX running" exit 1 fi diff --git a/t/cli/test_validate_config.sh b/t/cli/test_validate_config.sh index 29bf8df5da85..0379b986c5b2 100755 --- a/t/cli/test_validate_config.sh +++ b/t/cli/test_validate_config.sh @@ -142,7 +142,7 @@ nginx_config: # apisix restart out=$(./bin/apisix restart 2>&1 || true) -if ! (echo "$out" | grep "\[emerg\] unknown directive \"notexist\"") && ! (echo "$out" | grep "the old process is still running"); then +if ! (echo "$out" | grep "\[emerg\] unknown directive \"notexist\"") && ! (echo "$out" | grep "the old APISIX is still running"); then echo "failed: should restart failed when configuration invalid" exit 1 fi From 59c553c46b180ff37c2beb62f10fd31be3c6d2f7 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Wed, 3 Jan 2024 15:34:50 +0800 Subject: [PATCH 13/13] fix: simplify the logic in wait_stop_finsh and start (#10495) --- apisix/cli/ops.lua | 42 +++++++++++++++------------------ t/cli/test_prometheus_stream.sh | 2 -- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index fe82df366d34..836c87bce521 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -767,12 +767,17 @@ local function cleanup(env) end +local function sleep(n) + execute("sleep " .. tonumber(n)) +end + + local function check_running(env) local pid_path = env.apisix_home .. "/logs/nginx.pid" local pid = util.read_file(pid_path) pid = tonumber(pid) if not pid then - return false, -1 + return false, nil end return true, pid end @@ -802,9 +807,19 @@ local function start(env, ...) util.die(logs_path, " is not directory nor symbol link") end - -- check running - local running, pid = check_running(env) - if running then + -- check running and wait old apisix stop + local pid = nil + for i = 1, 30 do + local running + running, pid = check_running(env) + if not running then + break + else + sleep(0.1) + end + end + + if pid then if pid <= 0 then print("invalid pid") return @@ -925,29 +940,10 @@ local function stop(env) end -local function sleep(n) - execute("sleep " .. tonumber(n)) -end - - -local function wait_stop_finish(env, n) - for i = 1, n do - -- check running - local running = check_running(env) - if not running then - return - else - sleep(1) - end - end -end - - local function restart(env) -- test configuration test(env) stop(env) - wait_stop_finish(env, 3) start(env) end diff --git a/t/cli/test_prometheus_stream.sh b/t/cli/test_prometheus_stream.sh index 0de5ac4f644f..abf960e776a5 100755 --- a/t/cli/test_prometheus_stream.sh +++ b/t/cli/test_prometheus_stream.sh @@ -64,8 +64,6 @@ make stop echo "passed: prometheus works when both http & stream are enabled" -sleep 1 #wait for stop finish - echo " apisix: proxy_mode: stream