You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sh...@apache.org on 2021/06/24 01:45:09 UTC

[apisix] branch master updated: feat(control-api): add a way to trigger gc (#4472)

This is an automated email from the ASF dual-hosted git repository.

shuyangw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 74cc5c3  feat(control-api): add a way to trigger gc (#4472)
74cc5c3 is described below

commit 74cc5c3420aaa102274370b70ac5ee2e31876591
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Thu Jun 24 09:45:00 2021 +0800

    feat(control-api): add a way to trigger gc (#4472)
    
    Signed-off-by: spacewander <sp...@gmail.com>
---
 apisix/control/v1.lua         | 16 ++++++++++-
 docs/en/latest/control-api.md |  7 +++++
 t/control/gc.t                | 66 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 1 deletion(-)

diff --git a/apisix/control/v1.lua b/apisix/control/v1.lua
index 7ad9e05..7ec5f47 100644
--- a/apisix/control/v1.lua
+++ b/apisix/control/v1.lua
@@ -20,6 +20,7 @@ local get_routes = require("apisix.router").http_routes
 local get_services = require("apisix.http.service").services
 local upstream_mod = require("apisix.upstream")
 local get_upstreams = upstream_mod.upstreams
+local collectgarbage = collectgarbage
 local ipairs = ipairs
 local str_format = string.format
 local ngx_var = ngx.var
@@ -154,6 +155,13 @@ function _M.get_health_checker()
 end
 
 
+function _M.trigger_gc()
+    -- TODO: find a way to trigger GC in the stream subsystem
+    collectgarbage()
+    return 200
+end
+
+
 return {
     -- /v1/schema
     {
@@ -172,5 +180,11 @@ return {
         methods = {"GET"},
         uris = {"/healthcheck/*"},
         handler = _M.get_health_checker,
-    }
+    },
+    -- /v1/gc
+    {
+        methods = {"POST"},
+        uris = {"/gc"},
+        handler = _M.trigger_gc,
+    },
 }
diff --git a/docs/en/latest/control-api.md b/docs/en/latest/control-api.md
index d346163..41243a7 100644
--- a/docs/en/latest/control-api.md
+++ b/docs/en/latest/control-api.md
@@ -191,3 +191,10 @@ For example, `GET /v1/healthcheck/upstreams/1` returns:
     "src_type": "upstreams"
 }
 ```
+
+### POST /v1/gc
+
+Introduced since `v2.8`.
+
+Trigger a full GC in the http subsystem.
+Note that when you enable stream proxy, APISIX will run another Lua VM for the stream subsystem. It won't trigger a full GC in this Lua VM .
diff --git a/t/control/gc.t b/t/control/gc.t
new file mode 100644
index 0000000..bafb574
--- /dev/null
+++ b/t/control/gc.t
@@ -0,0 +1,66 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->no_error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: trigger full gc
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+            local before = collectgarbage("count")
+            do
+                local tab = {}
+                for i = 1, 10000 do
+                    tab[i] = {"a", 1}
+                end
+            end
+            local after_alloc = collectgarbage("count")
+            local code = t.test('/v1/gc',
+                ngx.HTTP_POST
+            )
+            local after_gc = collectgarbage("count")
+            if code == 200 then
+                if after_alloc - after_gc > 0.9 * (after_alloc - before) then
+                    ngx.say("ok")
+                else
+                    ngx.say(before, " ", after_alloc, " ", after_gc)
+                end
+            end
+        }
+    }
+--- response_body
+ok