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
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions apisix/core/id.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function _M.init()
return
end

uuid.seed()
apisix_uid = uuid.generate_v4()
log.notice("not found apisix uid, generate a new one: ", apisix_uid)

Expand Down
25 changes: 18 additions & 7 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ function _M.http_init(args)
"maxrecord=8000", "sizemcode=64",
"maxmcode=4000", "maxirconst=1000")

--
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)
parse_args(args)
core.id.init()

Expand All @@ -82,6 +75,15 @@ 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)
-- for testing only
core.log.info("random test in [1, 10000]: ", math.random(1, 1000000))
membphis marked this conversation as resolved.
Show resolved Hide resolved

local we = require("resty.worker.events")
local ok, err = we.configure({shm = "worker-events", interval = 0.1})
if not ok then
Expand Down Expand Up @@ -759,6 +761,15 @@ end

function _M.stream_init_worker()
core.log.info("enter stream_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)
-- for testing only
core.log.info("random stream test in [1, 10000]: ", math.random(1, 1000000))
membphis marked this conversation as resolved.
Show resolved Hide resolved

router.stream_init_worker()
plugin.init_worker()

Expand Down
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]
76 changes: 76 additions & 0 deletions t/stream-node/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);
log_level('info');
repeat_each(1);
no_long_string();
no_root_location();

run_tests();

__DATA__

=== TEST 1: generate different random number in different worker process
--- stream_enable
--- config
location /test {
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 stream 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 /test
--- 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]