-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathreadview.lua
336 lines (277 loc) · 10.3 KB
/
readview.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
local fiber = require('fiber')
local checks = require('checks')
local errors = require('errors')
local tarantool = require('tarantool')
local const = require('crud.common.const')
local stash = require('crud.common.stash')
local utils = require('crud.common.utils')
local sharding = require('crud.common.sharding')
local select_executor = require('crud.select.executor')
local select_filters = require('crud.compare.filters')
local dev_checks = require('crud.common.dev_checks')
local schema = require('crud.common.schema')
local stats = require('crud.stats')
local ReadviewError = errors.new_class('ReadviewError', {capture_stack = false})
local OPEN_FUNC_NAME = 'readview_open_on_storage'
local CRUD_OPEN_FUNC_NAME = utils.get_storage_call(OPEN_FUNC_NAME)
local SELECT_FUNC_NAME = 'select_readview_on_storage'
local CLOSE_FUNC_NAME = 'readview_close_on_storage'
local CRUD_CLOSE_FUNC_NAME = utils.get_storage_call(CLOSE_FUNC_NAME)
if (not utils.tarantool_version_at_least(2, 11, 0))
or (tarantool.package ~= 'Tarantool Enterprise') then
return {
new = function() return nil,
ReadviewError:new("Tarantool does not support readview") end,
init = function() return nil end}
end
local select = require('crud.select.module')
local readview = {}
local function readview_open_on_storage(readview_name)
if not utils.tarantool_version_at_least(2, 11, 0) or
tarantool.package ~= 'Tarantool Enterprise' then
ReadviewError:assert(false, ("Tarantool does not support readview"))
end
-- We store readview in stash because otherwise gc will delete it.
-- e.g master switch.
local read_view = box.read_view.open({name = readview_name})
local stash_readview = stash.get(stash.name.storage_readview)
stash_readview[read_view.id] = read_view
if read_view == nil then
ReadviewError:assert(false, ("Error creating readview"))
end
local replica_info = {}
replica_info.uuid = box.info().uuid
replica_info.id = read_view.id
return replica_info, nil
end
local function readview_close_on_storage(readview_uuid)
dev_checks('table')
local list = box.read_view.list()
local readview_id
for _, replica_info in pairs(readview_uuid) do
if replica_info.uuid == box.info().uuid then
readview_id = replica_info.id
end
end
for k,v in pairs(list) do
if v.id == readview_id then
list[k]:close()
local stash_readview = stash.get(stash.name.storage_readview)
stash_readview[readview_id] = nil
return true
end
end
return false
end
local function select_readview_on_storage(space_name, index_id, conditions, opts)
dev_checks('string', 'number', '?table', {
scan_value = 'table',
after_tuple = '?table',
tarantool_iter = 'number',
limit = 'number',
scan_condition_num = '?number',
field_names = '?table',
sharding_key_hash = '?number',
sharding_func_hash = '?number',
skip_sharding_hash_check = '?boolean',
yield_every = '?number',
fetch_latest_metadata = '?boolean',
readview_id = 'number',
})
local cursor = {}
if opts.fetch_latest_metadata then
local replica_schema_version
if box.info.schema_version ~= nil then
replica_schema_version = box.info.schema_version
else
replica_schema_version = box.internal.schema_version()
end
cursor.storage_info = {
replica_uuid = box.info().uuid,
replica_schema_version = replica_schema_version,
}
end
local list = box.read_view.list()
local space_readview
for k,v in pairs(list) do
if v.id == opts.readview_id then
space_readview = list[k].space[space_name]
end
end
if space_readview == nil then
return cursor, ReadviewError:new("Space %q doesn't exist", space_name)
end
local space = box.space[space_name]
if space == nil then
return cursor, ReadviewError:new("Space %q doesn't exist", space_name)
end
space_readview.format = space:format()
local index_readview = space_readview.index[index_id]
if index_readview == nil then
return cursor, ReadviewError:new("Index with ID %s doesn't exist", index_id)
end
local index = space.index[index_id]
if index == nil then
return cursor, ReadviewError:new("Index with ID %s doesn't exist", index_id)
end
local _, err = sharding.check_sharding_hash(space_name,
opts.sharding_func_hash,
opts.sharding_key_hash,
opts.skip_sharding_hash_check)
if err ~= nil then
return nil, err
end
local filter_func, err = select_filters.gen_func(space, conditions, {
tarantool_iter = opts.tarantool_iter,
scan_condition_num = opts.scan_condition_num,
})
if err ~= nil then
return cursor, ReadviewError:new("Failed to generate tuples filter: %s", err)
end
-- execute select
local resp, err = select_executor.execute(space, index, filter_func, {
scan_value = opts.scan_value,
after_tuple = opts.after_tuple,
tarantool_iter = opts.tarantool_iter,
limit = opts.limit,
yield_every = opts.yield_every,
readview = true,
readview_index = index_readview,
})
if err ~= nil then
return cursor, ReadviewError:new("Failed to execute select: %s", err)
end
if resp.tuples_fetched < opts.limit or opts.limit == 0 then
cursor.is_end = true
else
local last_tuple = resp.tuples[#resp.tuples]
cursor.after_tuple = last_tuple:totable()
end
cursor.stats = {
tuples_lookup = resp.tuples_lookup,
tuples_fetched = resp.tuples_fetched,
}
-- getting tuples with user defined fields (if `fields` option is specified)
-- and fields that are needed for comparison on router (primary key + scan key)
local filtered_tuples = schema.filter_tuples_fields(resp.tuples, opts.field_names)
local result = {cursor, filtered_tuples}
return unpack(result)
end
local Readview_obj = {}
Readview_obj.__index = Readview_obj
local select_call = stats.wrap(select.call, stats.op.SELECT)
function Readview_obj:select(space_name, user_conditions, opts)
opts = opts or {}
opts.readview = true
opts.readview_uuid = self._uuid
if self.opened == false then
return nil, ReadviewError:new("Read view is closed")
end
return select_call(space_name, user_conditions, opts)
end
local pairs_call = stats.wrap(select.pairs, stats.op.SELECT, {pairs = true})
function Readview_obj:pairs(space_name, user_conditions, opts)
opts = opts or {}
opts.readview = true
opts.readview_uuid = self._uuid
if self.opened == false then
return nil, ReadviewError:new("Read view is closed")
end
return pairs_call(space_name, user_conditions, opts)
end
function readview.init(user)
utils.init_storage_call(user, OPEN_FUNC_NAME, readview_open_on_storage)
utils.init_storage_call(user, CLOSE_FUNC_NAME, readview_close_on_storage)
utils.init_storage_call(user, SELECT_FUNC_NAME, select_readview_on_storage)
end
function Readview_obj:close(opts)
checks('table', {
timeout = '?number',
})
opts = opts or {}
if self.opened == false then
return
end
local vshard_router, err = utils.get_vshard_router_instance(nil)
if err ~= nil then
return ReadviewError:new(err)
end
local replicasets, err = vshard_router:routeall()
if err ~= nil then
return ReadviewError:new(err)
end
if opts.timeout == nil then
opts.timeout = const.DEFAULT_VSHARD_CALL_TIMEOUT
end
local errors = {}
for _, replicaset in pairs(replicasets) do
for replica_uuid, replica in pairs(replicaset.replicas) do
for _, value in pairs(self._uuid) do
if replica_uuid == value.uuid then
local replica_result, replica_err = replica.conn:call(CRUD_CLOSE_FUNC_NAME,
{self._uuid}, {timeout = opts.timeout})
if replica_err ~= nil then
table.insert(errors, ReadviewError:new("Failed to close Readview on storage: %s", replica_err))
end
if replica_err == nil and (not replica_result) then
table.insert(errors, ReadviewError:new("Readview was not found on storage: %s", replica_uuid))
end
end
end
end
end
if next(errors) ~= nil then
return errors
end
self.opened = false
return nil
end
function Readview_obj:__gc()
fiber.new(self.close, self)
end
function Readview_obj.create(vshard_router, opts)
local readview = {}
-- For tarantool lua (and lua 5.1) __gc metamethod only works for cdata types.
-- So in order to create a proper GC hook, we need to create cdata with
-- __gc call.
-- __gc call for this cdata will be a __gc call for our readview.
-- https://github.com/tarantool/tarantool/issues/5770
local proxy = newproxy(true)
getmetatable(proxy).__gc = function(_) Readview_obj.__gc(readview) end
readview[proxy] = true
setmetatable(readview, Readview_obj)
readview._name = opts.name
local results, err, err_uuid = vshard_router:map_callrw(CRUD_OPEN_FUNC_NAME,
{readview._name}, {timeout = opts.timeout})
if err ~= nil then
return nil,
ReadviewError:new("Failed to call readview_open_on_storage on storage-side: storage uuid: %s err: %s",
err_uuid, err)
end
local uuid = {}
local errors = {}
for _, replicaset_results in pairs(results) do
for _, replica_result in pairs(replicaset_results) do
table.insert(uuid, replica_result)
end
end
readview._uuid = uuid
readview.opened = true
if next(errors) ~= nil then
return nil, errors
end
return readview, nil
end
function readview.new(opts)
checks({
name = '?string',
timeout = '?number',
})
opts = opts or {}
local vshard_router, err = utils.get_vshard_router_instance(nil)
if err ~= nil then
return nil, ReadviewError:new(err)
end
return Readview_obj.create(vshard_router, opts)
end
return readview