Using multiple Upstreams problems

128 views
Skip to first unread message

周伟

unread,
Feb 2, 2016, 4:13:08 AM2/2/16
to openresty
我在使用openresy配置单个upstrem healthchek获取在状态是正确。在使用多个upstrem  时,status 一直是 up状态。求解惑。
附配置详细:

init_worker_by_lua_block {
   init_worker_by_lua_block {
            local hc = require "resty.upstream.healthcheck"
            local ok, err = hc.spawn_checker{
            shm = "healthcheck", 
            upstream = "api",
            type = "http",
            http_req = "GET /api/index.jsp HTTP/1.0\r\nHost: api\r\n\r\n",
            interval = 2000,
            timeout = 1000,
            fall = 3,
            rise = 2,
            valid_statuses = {200, 302},
            concurrency = 10,
          }
            local ok, err = hc.spawn_checker{
            shm = "healthcheck",
            upstream = "web",
            type = "http",
            http_req = "GET /web/index.jsp HTTP/1.0\r\nHost: web\r\n\r\n",
            interval = 2000,
            timeout = 1000,
            fall = 3,
            rise = 2,
            valid_statuses = {200, 302},
            concurrency = 10,
          }
}
}

Yichun Zhang (agentzh)

unread,
Feb 3, 2016, 7:51:39 PM2/3/16
to openresty
Hello!

2016-02-02 1:13 GMT-08:00 周伟:
> 我在使用openresy配置单个upstrem healthchek获取在状态是正确。在使用多个upstrem 时,status 一直是
> up状态。求解惑。

你可以尝试为每个 upstream 使用不同的 lua_shared_dict?

Regards,
-agentzh

Guanglin Lv

unread,
Feb 4, 2016, 5:00:26 AM2/4/16
to openresty


在 2016年2月2日星期二 UTC+8下午5:13:08,Kenvin写道:
我在使用openresy配置单个upstrem healthchek获取在状态是正确。在使用多个upstrem  时,status 一直是 up状态。求解惑。


  我之前也遇到过类似的情况,一般是由于reload nginx导致的,你可以在init_bu_lua阶段flush一下lua_shared_dict,但这样你之前的统计就没有了;

  还可以改下peer_ok和peer_fail函数,把peer.down的获取改成获取全局的值,即从lua_shared_dict中获取;


 thanks.

Kenvin

unread,
Feb 4, 2016, 5:41:59 AM2/4/16
to openresty
我为每个upstream分配不同的dict 还是不行,全是up状态

在 2016年2月4日星期四 UTC+8上午8:51:39,agentzh写道:
Message has been deleted

Kenvin

unread,
Feb 4, 2016, 5:47:05 AM2/4/16
to openresty
我刚接触lua,不太熟悉,能告诉具体改哪个位置,多谢了
local function peer_ok(ctx, is_backup, id, peer)
    debug("peer ", peer.name, " was checked to be ok")

    local u = ctx.upstream
    local dict = ctx.dict

    local key = gen_peer_key("ok:", u, is_backup, id)
    local succ, err = dict:get(key)
    if not succ then
        if err then
            errlog("failed to get peer ok key: ", err)
            return
        end
        succ = 1

        -- below may have a race condition, but it is fine for our
        -- purpose here.
        local ok, err = dict:set(key, 1)
        if not ok then
            errlog("failed to set peer ok key: ", err)
        end
    else
        succ = succ + 1
        local ok, err = dict:incr(key, 1)
        if not ok then
            errlog("failed to incr peer ok key: ", err)
        end
    end

    if succ == 1 then
        key = gen_peer_key("nok:", u, is_backup, id)
        local fails, err = dict:get(key)
        if not fails or fails == 0 then
            if err then
                errlog("failed to get peer nok key: ", err)
                return
            end
        else
            local ok, err = dict:set(key, 0)
            if not ok then
                errlog("failed to set peer nok key: ", err)
            end
        end
    end

    if peer.down and succ >= ctx.rise then
        warn("peer ", peer.name, " is turned up after ", succ,
                " success(es)")
        peer.down = nil
        set_peer_down_globally(ctx, is_backup, id, nil)
    end
end

在 2016年2月4日星期四 UTC+8下午6:00:26,Guanglin Lv写道:

Yichun Zhang (agentzh)

unread,
Feb 4, 2016, 2:57:38 PM2/4/16
to openresty
Hello!

2016-02-04 2:41 GMT-08:00 Kenvin:
> 我为每个upstream分配不同的dict 还是不行,全是up状态
>

从实现上讲,多个 upstream 共享一个 shared dict 是不会有问题的,因为 key 里面总是编码 upstream 的名字。

我刚刚给 lua-resty-upstream-heathcheck 库的官方测试集添加了一个多 upstream 作健康检查的测试用例,它工作得很好:

从你描述的现象上看,很可能是因为你的 init_worker_by_lua_block {} 里面有语法错误或者其他 Lua
错误而导致健康检查的背景轻线程压根就没有运行。建议你仔细检查你的 NGINX 错误日志文件中的错误信息(你需要确保你的 error_log
配置指令没有使用过高的日志过滤级别,否则的话,你几乎永远看不到错误输出)。

我会给 lua-resty-upstream-healthcheck 库添加一个检查线程是否启动的基本检查,这样 status_page 会给出错误提示。

Regards,
-agentzh

Yichun Zhang (agentzh)

unread,
Feb 4, 2016, 3:27:51 PM2/4/16
to openresty
Hello!

2016-02-04 11:57 GMT-08:00 Yichun Zhang (agentzh):
> 我刚刚给 lua-resty-upstream-heathcheck 库的官方测试集添加了一个多 upstream 作健康检查的测试用例,它工作得很好:
>

忘了贴测试用例的地址了:

https://github.com/openresty/lua-resty-upstream-healthcheck/blob/master/t/sanity.t#L1082

该测试用例在我本地运行通过。

> 我会给 lua-resty-upstream-healthcheck 库添加一个检查线程是否启动的基本检查,这样 status_page 会给出错误提示。
>

刚刚给 status_page() 添加了是否有活着的 checker 的检查:

https://github.com/openresty/lua-resty-upstream-healthcheck#status_page

对应的测试用例在这里:

https://github.com/openresty/lua-resty-upstream-healthcheck/blob/master/t/sanity.t#L1195

Regards,
-agentzh

Guanglin Lv

unread,
Feb 4, 2016, 9:21:54 PM2/4/16
to openresty


在 2016年2月4日星期四 UTC+8下午6:47:05,Kenvin写道:
我刚接触lua,不太熟悉,能告诉具体改哪个位置,多谢了


  我不确定你是怎么产生的这种情况,我的场景里upstream是做成了动态增加及更新的,所以遇到了和你类似的问题; 不过现在也一时记不得是如何产生的了;

  把peer.down的值动态从共享内存里获取,如何获取可参考https://github.com/openresty/lua-resty-upstream-healthcheck/blob/master/lib/resty/upstream/healthcheck.lua#L382
 
  建议你先按照春哥说验证一下,是否开启了checker;另外最好能尝试分析下可能原因,能给出复现步骤就最好了;
  
 thanks.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages