• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / crud / 25398424078

05 May 2026 07:44PM UTC coverage: 88.337% (-0.01%) from 88.349%
25398424078

push

github

p0rtale
fix: handle nil bucket_id in storage get/update/delete operations

Starting from crud 1.7.0, storage asserts if `opts.bucket_id` is nil during `get`, `update`, or `delete` operations. Since older routers (< 1.7.0) do not pass `bucket_id`, this causes protocol incompatibility.

This commit adds a nil check to skip bucket ref when `bucket_id` is missing, restoring backward compatibility with old routers.

24 of 30 new or added lines in 3 files covered. (80.0%)

2 existing lines in 1 file now uncovered.

5264 of 5959 relevant lines covered (88.34%)

12845.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/crud/delete.lua
1
local checks = require('checks')
633✔
2
local errors = require('errors')
633✔
3

4
local call = require('crud.common.call')
633✔
5
local const = require('crud.common.const')
633✔
6
local utils = require('crud.common.utils')
633✔
7
local sharding = require('crud.common.sharding')
633✔
8
local sharding_key_module = require('crud.common.sharding.sharding_key')
633✔
9
local sharding_metadata_module = require('crud.common.sharding.sharding_metadata')
633✔
10
local dev_checks = require('crud.common.dev_checks')
633✔
11
local schema = require('crud.common.schema')
633✔
12
local bucket_ref_unref = require('crud.common.sharding.bucket_ref_unref')
633✔
13

14
local DeleteError = errors.new_class('DeleteError', {capture_stack = false})
633✔
15

16
local delete = {}
633✔
17

18
local DELETE_FUNC_NAME = 'delete_on_storage'
633✔
19
local CRUD_DELETE_FUNC_NAME = utils.get_storage_call(DELETE_FUNC_NAME)
633✔
20

21
local function delete_on_storage(space_name, key, field_names, opts)
22
    dev_checks('string', '?', '?table', {
150✔
23
        bucket_id = '?number|cdata',
24
        sharding_key_hash = '?number',
25
        sharding_func_hash = '?number',
26
        skip_sharding_hash_check = '?boolean',
27
        noreturn = '?boolean',
28
        fetch_latest_metadata = '?boolean',
29
    })
30

31
    opts = opts or {}
150✔
32

33
    local space = box.space[space_name]
150✔
34
    if space == nil then
150✔
35
        return nil, DeleteError:new("Space %q doesn't exist", space_name)
×
36
    end
37

38
    local _, err = sharding.check_sharding_hash(space_name,
300✔
39
                                                opts.sharding_func_hash,
150✔
40
                                                opts.sharding_key_hash,
150✔
41
                                                opts.skip_sharding_hash_check)
150✔
42

43
    if err ~= nil then
150✔
44
        return nil, err
4✔
45
    end
46

47
    -- Skip bucket reference if bucket_id is not provided to support old routers.
48
    local unref = nil
146✔
49
    if opts.bucket_id ~= nil then
146✔
50
        local ref_ok, bucket_ref_err, unref_func = bucket_ref_unref.bucket_refrw(opts.bucket_id, space.engine)
146✔
51
        if not ref_ok then
146✔
NEW
52
            return nil, bucket_ref_err
×
53
        end
54
        unref = unref_func
146✔
55
    end
56

57
    -- add_space_schema_hash is false because
58
    -- reloading space format on router can't avoid delete error on storage
59
    local result =  schema.wrap_func_result(space, space.delete, {
292✔
60
        add_space_schema_hash = false,
61
        field_names = field_names,
146✔
62
        noreturn = opts.noreturn,
146✔
63
        fetch_latest_metadata = opts.fetch_latest_metadata,
146✔
64
    }, space, key)
146✔
65

66
    if unref ~= nil then
146✔
67
        local unref_ok, err_unref = unref(opts.bucket_id, space.engine)
146✔
68
        if not unref_ok then
146✔
NEW
69
            return nil, err_unref
×
70
        end
71
    end
72

73
    return result
146✔
74
end
75

76
delete.storage_api = {[DELETE_FUNC_NAME] = delete_on_storage}
633✔
77

78
-- returns result, err, need_reload
79
-- need_reload indicates if reloading schema could help
80
-- see crud.common.schema.wrap_func_reload()
81
local function call_delete_on_router(vshard_router, space_name, key, opts)
82
    dev_checks('table', 'string', '?', {
170✔
83
        timeout = '?number',
84
        bucket_id = '?',
85
        fields = '?table',
86
        vshard_router = '?string|table',
87
        noreturn = '?boolean',
88
        fetch_latest_metadata = '?boolean',
89
    })
90

91
    local space, err, netbox_schema_version = utils.get_space(space_name, vshard_router, opts.timeout)
170✔
92
    if err ~= nil then
170✔
93
        return nil, DeleteError:new("An error occurred during the operation: %s", err), const.NEED_SCHEMA_RELOAD
×
94
    end
95
    if space == nil then
170✔
96
        return nil, DeleteError:new("Space %q doesn't exist", space_name), const.NEED_SCHEMA_RELOAD
32✔
97
    end
98

99
    if box.tuple.is(key) then
308✔
100
        key = key:totable()
×
101
    end
102

103
    local sharding_key_hash = nil
154✔
104
    local skip_sharding_hash_check = nil
105

106
    local sharding_key = key
154✔
107
    if opts.bucket_id == nil then
154✔
108
        if space.index[0] == nil then
142✔
109
            return nil, DeleteError:new("Cannot fetch primary index parts"), const.NEED_SCHEMA_RELOAD
×
110
        end
111
        local primary_index_parts = space.index[0].parts
142✔
112

113
        local sharding_key_data, err = sharding_metadata_module.fetch_sharding_key_on_router(vshard_router, space_name)
142✔
114
        if err ~= nil then
142✔
115
            return nil, err
×
116
        end
117

118
        sharding_key, err = sharding_key_module.extract_from_pk(vshard_router,
284✔
119
                                                                space_name,
142✔
120
                                                                sharding_key_data.value,
142✔
121
                                                                primary_index_parts, key)
284✔
122
        if err ~= nil then
142✔
123
            return nil, err
4✔
124
        end
125

126
        sharding_key_hash = sharding_key_data.hash
138✔
127
    else
128
        skip_sharding_hash_check = true
12✔
129
    end
130

131
    local bucket_id_data, err = sharding.key_get_bucket_id(vshard_router, space_name, sharding_key, opts.bucket_id)
150✔
132
    if err ~= nil then
150✔
133
        return nil, err
2✔
134
    end
135

136
    -- When the sharding index (bucket_id) is the primary index, bucket_id can be passed as box.NULL.
137
    sharding.fill_bucket_id_pk(space, key, bucket_id_data.bucket_id)
148✔
138

139
    local delete_on_storage_opts = {
148✔
140
        bucket_id = bucket_id_data.bucket_id,
148✔
141
        sharding_func_hash = bucket_id_data.sharding_func_hash,
148✔
142
        sharding_key_hash = sharding_key_hash,
148✔
143
        skip_sharding_hash_check = skip_sharding_hash_check,
148✔
144
        noreturn = opts.noreturn,
148✔
145
        fetch_latest_metadata = opts.fetch_latest_metadata,
148✔
146
    }
147

148
    local call_opts = {
148✔
149
        mode = 'write',
150
        timeout = opts.timeout,
148✔
151
    }
152

153
    local storage_result, err = call.single(vshard_router,
296✔
154
        bucket_id_data.bucket_id, CRUD_DELETE_FUNC_NAME,
148✔
155
        {space_name, key, opts.fields, delete_on_storage_opts},
148✔
156
        call_opts
157
    )
148✔
158

159
    if err ~= nil then
148✔
160
        local err_wrapped = DeleteError:new("Failed to call delete on storage-side: %s", err)
2✔
161

162
        if sharding.result_needs_sharding_reload(err) then
4✔
163
            return nil, err_wrapped, const.NEED_SHARDING_RELOAD
2✔
164
        end
165

166
        return nil, err_wrapped
×
167
    end
168

169
    if storage_result.err ~= nil then
146✔
170
        return nil, DeleteError:new("Failed to delete: %s", storage_result.err)
68✔
171
    end
172

173
    if opts.noreturn == true then
112✔
174
        return nil
2✔
175
    end
176

177
    local tuple = storage_result.res
110✔
178

179
    if opts.fetch_latest_metadata == true then
110✔
180
        -- This option is temporary and is related to [1], [2].
181
        -- [1] https://github.com/tarantool/crud/issues/236
182
        -- [2] https://github.com/tarantool/crud/issues/361
183
        space = utils.fetch_latest_metadata_when_single_storage(space, space_name, netbox_schema_version,
4✔
184
                                                                vshard_router, opts, storage_result.storage_info)
4✔
185
    end
186

187
    return utils.format_result({tuple}, space, opts.fields)
110✔
188
end
189

190
--- Deletes tuple from the specified space by key
191
--
192
-- @function call
193
--
194
-- @param string space_name
195
--  A space name
196
--
197
-- @param key
198
--  Primary key value
199
--
200
-- @tparam ?number opts.timeout
201
--  Function call timeout
202
--
203
-- @tparam ?number opts.bucket_id
204
--  Bucket ID
205
--  (by default, it's vshard.router.bucket_id_strcrc32 of primary key)
206
--
207
-- @tparam ?string|table opts.vshard_router
208
--  Cartridge vshard group name or vshard router instance.
209
--  Set this parameter if your space is not a part of the
210
--  default vshard cluster.
211
--
212
-- @tparam ?boolean opts.noreturn
213
--  Suppress returning successfully processed tuple.
214
--
215
-- @return[1] object
216
-- @treturn[2] nil
217
-- @treturn[2] table Error description
218
--
219
function delete.call(space_name, key, opts)
633✔
220
    checks('string', '?', {
162✔
221
        timeout = '?number',
222
        bucket_id = '?',
223
        fields = '?table',
224
        vshard_router = '?string|table',
225
        noreturn = '?boolean',
226
        fetch_latest_metadata = '?boolean',
227
    })
228

229
    opts = opts or {}
162✔
230

231
    local vshard_router, err = utils.get_vshard_router_instance(opts.vshard_router)
162✔
232
    if err ~= nil then
162✔
233
        return nil, DeleteError:new(err)
8✔
234
    end
235

236
    return schema.wrap_func_reload(vshard_router, sharding.wrap_method, call_delete_on_router,
158✔
237
                                   space_name, key, opts)
158✔
238
end
239

240
return delete
633✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc