问题描述
我将Tarantool与vshard模块一起使用。设置bucket_id时,数据将分布在整个群集中。每个节点都有其自己的bucket_id集。如何创建一个空间,使其在每个节点(词典)上均可完全访问?
解决方法
我使用以下代码段在所有存储上运行功能(也许有更好的方法)。
假设我们在存储上有一个功能:
local function putMyData(rows)
box.atomic(function()
for _,row in ipairs(rows)
box.mySpace:put(row)
end
end)
end
box.schema.func.create("putMyData",{ if_not_exists = true })
box.schema.role.grant("public","execute","function","putMyData",{ if_not_exists = true })
rawset(_G,putMyData)
然后可以使用以下助手来调用该函数:
local function callAll(mode,fnName,args,resHandler,timeoutSec)
local replicaSets,err = vshard.router.routeall()
if err ~= nil then
error(err)
end
local count = 0
for _,_ in pairs(replicaSets) do
count = count + 1
end
local channel = fiber.channel(count)
local method
if mode == "read" then
method = "callro"
else
method = "callrw"
end
for _,replicaSet in pairs(replicaSets) do
fiber.create(
function()
local res,fErr = replicaSet[method](replicaSet,{ timeout = timeoutSec or opts.timeout })
channel:put({ res = res,err = fErr })
end)
end
local results = { }
for i = 1,count do
local val = channel:get()
if val.err ~= nil then
error(val.err)
end
if resHandler == nil then
results[i] = val.res
else
resHandler(val.res,results)
end
end
return results
end