On Thursday, 12 May 2016 19:19:50 UTC+3, Roman Alexeev wrote:
On Thursday, May 12, 2016 at 10:25:23 AM UTC-4, Roman Alexeev wrote:
Please suggest me how to share a big table (the table contains a graph size of several gigabytes) across client requests within one worker process?
Can you explain how the data will be used? ie the purpose or Read vs Write, number of lookups, etc. Someone may know of another approach.
0. I will start several servers (each with single worker) with similar copy of the graph for accept parallel requests.
1. Each server initializes with same dataset and this dataset is READONLY all the lifetime. When i need to make changes - i successively reinitialize servers with new dataset.
2. Users request operations that discover the graph (can take a few seconds). Most operations require a lot (thousands/millions) of table lookups and can create temporary local variables (they will not be shared across client requests).
Did you try Lua Module Level variable?
e.g. in init_worker_by_lua you do something like this:
require "data"
And in your data.lua you have (you may run code to initialize this, e.g. read it from file, deserialize, lookup from db etc.)
return {
here = "is",
my = "data"
}
-- this return value gets cached automatically by Lua to package.loaded
Now in your normal code path (e.g. in content_by_lua) you just do:
local data = "" "data"
-- I need 'here' data:
local here = data.here
-- do whatever you want with that data
If you have a lot of string lookups etc. then check out this patch (it makes a huge difference!!!):
https://github.com/LuaJIT/LuaJIT/pull/174
Now, if you cannot fit this in memory, this is not a solution. Also if you hit LuaJIT memory limits, you have to use FFI and allocate your owen memory in C structs etc.
Regards
Aapo