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

bugfix: set random seed for each worker process at init_worker phase, only init phase is not enough. #2357

Merged
merged 3 commits into from
Oct 9, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bugfix: set random seed for each worker process at init_worker phas…
…e, only `init` phase is not enough.
membphis committed Oct 5, 2020

Verified

This commit was signed with the committer’s verified signature.
ronnnnn Seiya Kokushi
commit 597ff2970d605a431dbfefcbcfad5997906de638
8 changes: 8 additions & 0 deletions apisix/init.lua
Original file line number Diff line number Diff line change
@@ -82,6 +82,14 @@ end


function _M.http_init_worker()
local seed, err = core.utils.get_seed_from_urandom()
if not seed then
core.log.warn('failed to get seed from urandom: ', err)
seed = ngx_now() * 1000 + ngx.worker.pid()
end
math.randomseed(seed)
core.log.info("random test in [1, 10000]: ", math.random(1, 1000000))

local we = require("resty.worker.events")
local ok, err = we.configure({shm = "worker-events", interval = 0.1})
if not ok then
76 changes: 76 additions & 0 deletions t/core/random.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# 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';

master_on();
workers(4);
repeat_each(1);
no_long_string();
no_root_location();
log_level("info");

run_tests;

__DATA__

=== TEST 1: generate different random number in different worker process
--- config
location /t {
content_by_lua_block {
local log_file = ngx.config.prefix() .. "logs/error.log"
local file = io.open(log_file, "r")
local log = file:read("*a")

local it, err = ngx.re.gmatch(log, [[random test in \[1, 10000\]: (\d+)]], "jom")
if not it then
ngx.log(ngx.ERR, "failed to gmatch: ", err)
return
end

local random_nums = {}
while true do
local m, err = it()
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end

if not m then
break
end

-- found a match
table.insert(random_nums, m[1])
end

for i = 2, #random_nums do
local pre = random_nums[i - 1]
local cur = random_nums[i]
ngx.say("random[", i - 1, "] == random[", i, "]: ", pre == cur)
end
}
}
--- request
GET /t
--- response_body
random[1] == random[2]: false
random[2] == random[3]: false
random[3] == random[4]: false
random[4] == random[5]: false
--- no_error_log
[error]