Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ngx_http_lua_upstream with tengine RBTREE performance optimization #1882

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

lhanjian
Copy link
Collaborator

@lhanjian lhanjian commented Nov 2, 2023

Fork the ngx_http_lua_upstream

And optimize performance O(n) search to O(logn) RBT search


umcf = ngx_http_lua_upstream_get_upstream_main_conf(L);

#if (NGX_HTTP_UPSTREAM_RBTREE)
Copy link
Collaborator Author

@lhanjian lhanjian Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimization of mine.

diff --git a/src/ngx_http_lua_upstream_module.c b/src/ngx_http_lua_upstream_module.c
index 7b23787..c5b4c80 100644
--- a/src/ngx_http_lua_upstream_module.c
+++ b/src/ngx_http_lua_upstream_module.c
@@ -517,10 +517,41 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host)
     ngx_http_upstream_main_conf_t        *umcf;
 
     umcf = ngx_http_lua_upstream_get_upstream_main_conf(L);
+
+#if (NGX_HTTP_UPSTREAM_RBTREE)
+
+    ngx_list_part_t                *part;
+
+    uscf = ngx_http_upstream_rbtree_lookup(umcf, host);
+
+    if (uscf != NULL)
+    {
+        return uscf;
+    }
+
+    part = &umcf->implicit_upstreams.part;
+    uscfp = part->elts;
+
+    for (i = 0; /* void */ ; i++) {
+
+        if (i >= part->nelts) {
+            if (part->next == NULL) {
+                break;
+            }
+
+            part = part->next;
+            uscfp = part->elts;
+            i = 0;
+        }
+
+#else
+
     uscfp = umcf->upstreams.elts;
 
     for (i = 0; i < umcf->upstreams.nelts; i++) {
 
+#endif
+
         uscf = uscfp[i];
 
         if (uscf->host.len == host->len
@@ -542,7 +573,30 @@ ngx_http_lua_upstream_find_upstream(lua_State *L, ngx_str_t *host)
 
         len = port - host->data - 1;
 
+#if (NGX_HTTP_UPSTREAM_RBTREE)
+        ngx_str_t addr;
+        addr.data = host->data;
+        addr.len = len;
+        uscf = ngx_http_upstream_rbtree_lookup(umcf, &addr);
+        if (uscf != NULL && uscf->port
+            && uscf->port == n)
+        {
+            return uscf;
+        }
+        part = &umcf->implicit_upstreams.part;
+        uscfp = part->elts;
+        for (i = 0; /* void */ ; i++) {
+            if (i >= part->nelts) {
+                if (part->next == NULL) {
+                    break;
+                }
+                part = part->next;
+                uscfp = part->elts;
+                i = 0;
+            }
+#else
         for (i = 0; i < umcf->upstreams.nelts; i++) {
+#endif
 
             uscf = uscfp[i];

@lhanjian
Copy link
Collaborator Author

lhanjian commented Nov 2, 2023

@drawing @chobits

Thanks for review.

It has been running on our production environment for years

@lhanjian lhanjian requested a review from chobits November 8, 2023 07:51
@@ -68,6 +68,7 @@ jobs:
--add-module=./modules/ngx_http_concat_module \
--add-module=./modules/ngx_http_footer_filter_module \
--add-module=./modules/ngx_http_lua_module \
--add-module=./modules/ngx_http_lua_upstream \
Copy link
Member

@chobits chobits Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @lhanjian I have examined the test cases introduced by this module. I believe it is easy to run and pass, it does not depend on some other module or library. Could you try to make it work in our CI/CD workflow? only for lua-upstream module

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chobits OK. ok i will try it soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants