ewma.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. use t::APISIX 'no_plan';
  18. repeat_each(1);
  19. #no_long_string();
  20. no_root_location();
  21. log_level('info');
  22. worker_connections(256);
  23. run_tests;
  24. __DATA__
  25. === TEST 1: add upstream
  26. --- config
  27. location /t {
  28. content_by_lua_block {
  29. local t = require("lib.test_admin").test
  30. local code, body = t('/apisix/admin/routes/1',
  31. ngx.HTTP_PUT,
  32. [[{
  33. "upstream": {
  34. "nodes": {
  35. "127.0.0.1:1980": 100,
  36. "127.0.0.1:1981": 100
  37. },
  38. "type": "ewma"
  39. },
  40. "uri": "/ewma"
  41. }]]
  42. )
  43. if code >= 300 then
  44. ngx.status = code
  45. end
  46. ngx.say(body)
  47. }
  48. }
  49. --- request
  50. GET /t
  51. --- response_body
  52. passed
  53. === TEST 2: about latency
  54. --- timeout: 5
  55. --- config
  56. location /t {
  57. content_by_lua_block {
  58. --node: "127.0.0.1:1980": latency is 0.001
  59. --node: "127.0.0.1:1981": latency is 0.005
  60. local http = require "resty.http"
  61. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  62. .. "/ewma"
  63. local ports_count = {}
  64. for i = 1, 12 do
  65. local httpc = http.new()
  66. httpc:set_timeout(1000)
  67. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  68. if not res then
  69. ngx.say(err)
  70. return
  71. end
  72. ports_count[res.body] = (ports_count[res.body] or 0) + 1
  73. end
  74. local ports_arr = {}
  75. for port, count in pairs(ports_count) do
  76. table.insert(ports_arr, {port = port, count = count})
  77. end
  78. local function cmd(a, b)
  79. return a.port > b.port
  80. end
  81. table.sort(ports_arr, cmd)
  82. ngx.say(require("toolkit.json").encode(ports_arr))
  83. ngx.exit(200)
  84. }
  85. }
  86. --- request
  87. GET /t
  88. --- response_body
  89. [{"count":1,"port":"1981"},{"count":11,"port":"1980"}]
  90. --- error_code: 200
  91. === TEST 3: about frequency
  92. --- timeout: 30
  93. --- config
  94. location /t {
  95. content_by_lua_block {
  96. local t = require("lib.test_admin").test
  97. local http = require "resty.http"
  98. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  99. .. "/ewma"
  100. --node: "127.0.0.1:1980": latency is 0.001
  101. --node: "127.0.0.1:1981": latency is 0.005
  102. local ports_count = {}
  103. for i = 1, 2 do
  104. local httpc = http.new()
  105. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  106. if not res then
  107. ngx.say(err)
  108. return
  109. end
  110. end
  111. --remove the 1981 node,
  112. --add the 1982 node
  113. --keep two nodes for triggering ewma logic in server_picker function of balancer phase
  114. local code, body = t('/apisix/admin/routes/1',
  115. ngx.HTTP_PUT,
  116. [[{
  117. "upstream": {
  118. "nodes": {
  119. "127.0.0.1:1980": 100,
  120. "127.0.0.1:1982": 100
  121. },
  122. "type": "ewma"
  123. },
  124. "uri": "/ewma"
  125. }]]
  126. )
  127. if code ~= 200 then
  128. ngx.say("update route failed")
  129. return
  130. end
  131. ngx.sleep(11)
  132. --keep the node 1980 hot
  133. for i = 1, 12 do
  134. local httpc = http.new()
  135. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  136. if not res then
  137. ngx.say(err)
  138. return
  139. end
  140. end
  141. --recover the 1981 node
  142. local code, body = t('/apisix/admin/routes/1',
  143. ngx.HTTP_PUT,
  144. [[{
  145. "upstream": {
  146. "nodes": {
  147. "127.0.0.1:1980": 100,
  148. "127.0.0.1:1981": 100
  149. },
  150. "type": "ewma"
  151. },
  152. "uri": "/ewma"
  153. }]]
  154. )
  155. if code ~= 200 then
  156. ngx.say("update route failed")
  157. return
  158. end
  159. --should select the 1981 node,because it is idle
  160. local httpc = http.new()
  161. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  162. if not res then
  163. ngx.say(err)
  164. return
  165. end
  166. ngx.say(require("toolkit.json").encode({port = res.body, count = 1}))
  167. ngx.exit(200)
  168. }
  169. }
  170. --- request
  171. GET /t
  172. --- response_body
  173. {"count":1,"port":"1981"}
  174. --- error_code: 200
  175. === TEST 4: about filter tried servers
  176. --- timeout: 10
  177. --- config
  178. location /t {
  179. content_by_lua_block {
  180. local t = require("lib.test_admin").test
  181. --remove the 1981 node,
  182. --add the 9527 node (invalid node)
  183. --keep two nodes for triggering ewma logic in server_picker function of balancer phase
  184. local code, body = t('/apisix/admin/routes/1',
  185. ngx.HTTP_PUT,
  186. [[{
  187. "upstream": {
  188. "nodes": {
  189. "127.0.0.1:1980": 1,
  190. "127.0.0.1:9527": 1
  191. },
  192. "type": "ewma",
  193. "timeout": {
  194. "connect": 0.1,
  195. "send": 0.5,
  196. "read": 0.5
  197. }
  198. },
  199. "uri": "/ewma"
  200. }]]
  201. )
  202. if code ~= 200 then
  203. ngx.say("update route failed")
  204. return
  205. end
  206. local http = require "resty.http"
  207. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  208. .. "/ewma"
  209. --should always select the 1980 node, because 0 is invalid
  210. local t = {}
  211. local ports_count = {}
  212. for i = 1, 12 do
  213. local th = assert(ngx.thread.spawn(function(i)
  214. local httpc = http.new()
  215. httpc:set_timeout(2000)
  216. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  217. if not res then
  218. ngx.say(err)
  219. return
  220. end
  221. ports_count[res.body] = (ports_count[res.body] or 0) + 1
  222. end, i))
  223. table.insert(t, th)
  224. end
  225. for i, th in ipairs(t) do
  226. ngx.thread.wait(th)
  227. end
  228. local ports_arr = {}
  229. for port, count in pairs(ports_count) do
  230. table.insert(ports_arr, {port = port, count = count})
  231. end
  232. local function cmd(a, b)
  233. return a.port > b.port
  234. end
  235. table.sort(ports_arr, cmd)
  236. ngx.say(require("toolkit.json").encode(ports_arr))
  237. ngx.exit(200)
  238. }
  239. }
  240. --- request
  241. GET /t
  242. --- response_body
  243. [{"count":12,"port":"1980"}]
  244. --- error_code: 200
  245. --- error_log
  246. Connection refused) while connecting to upstream
  247. === TEST 5: about all endpoints have been retried
  248. --- timeout: 10
  249. --- config
  250. location /t {
  251. content_by_lua_block {
  252. local t = require("lib.test_admin").test
  253. --add the 9527 node (invalid node)
  254. --add the 9528 node (invalid node)
  255. --keep two nodes for triggering ewma logic in server_picker function of balancer phase
  256. local code, body = t('/apisix/admin/routes/1',
  257. ngx.HTTP_PUT,
  258. [[{
  259. "upstream": {
  260. "nodes": {
  261. "127.0.0.1:9527": 1,
  262. "127.0.0.1:9528": 1
  263. },
  264. "type": "ewma",
  265. "timeout": {
  266. "connect": 0.1,
  267. "send": 0.5,
  268. "read": 0.5
  269. }
  270. },
  271. "uri": "/ewma"
  272. }]]
  273. )
  274. if code ~= 200 then
  275. ngx.say("update route failed")
  276. return
  277. end
  278. local http = require "resty.http"
  279. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  280. .. "/ewma"
  281. --should always return 502, because both 9527 and 9528 are invalid
  282. local t = {}
  283. local ports_count = {}
  284. for i = 1, 12 do
  285. local th = assert(ngx.thread.spawn(function(i)
  286. local httpc = http.new()
  287. httpc:set_timeout(2000)
  288. local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
  289. if not res then
  290. ngx.say(err)
  291. return
  292. end
  293. ports_count[res.status] = (ports_count[res.status] or 0) + 1
  294. end, i))
  295. table.insert(t, th)
  296. end
  297. for i, th in ipairs(t) do
  298. ngx.thread.wait(th)
  299. end
  300. local ports_arr = {}
  301. for port, count in pairs(ports_count) do
  302. table.insert(ports_arr, {port = port, count = count})
  303. end
  304. local function cmd(a, b)
  305. return a.port > b.port
  306. end
  307. table.sort(ports_arr, cmd)
  308. ngx.say(require("toolkit.json").encode(ports_arr))
  309. ngx.exit(200)
  310. }
  311. }
  312. --- request
  313. GET /t
  314. --- response_body
  315. [{"count":12,"port":502}]
  316. --- error_code: 200
  317. --- error_log
  318. Connection refused) while connecting to upstream