Skip to content

Commit fe042e7

Browse files
Александр Ваховvakhov
authored andcommitted
[TNTP-3536] validate bucket_id in CRUD operations
(cherry picked from commit 3ef295df0e726568905fd5f67deff5feb50f406e)
1 parent bcff806 commit fe042e7

22 files changed

+474
-36
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9-
* Обеспечена поддержка ролевой модели в CRUD (https://jira.vk.team/browse/TNTP-2177)
9+
10+
### Changed
11+
* bumped: metrics version in rockspec
12+
* bumped: cartridge version in rockspec
13+
* bumped: vshard version in rockspec
14+
* Role model support in CRUD is provided (https://jira.vk.team/browse/TNTP-2177)
15+
16+
### Added
17+
* Validation of `bucket_id`. Invalid values now raise `BucketIDError`
18+
before routing. [TNTP-3536](https://jira.vk.team/browse/TNTP-3536)
1019

1120
### Fixed
21+
* Fixed compatibility with cartridge `2.16.0` ([TNTP-3598](https://jira.vk.team/browse/TNTP-3598))
1222
* `crud.schema` no longer returns TCF system space `_cdc_state`.
1323
* `crud.schema` no longer returns system space `_gc_consumers` with Tarantool 3.2+.
1424
* `crud.schema` no longer returns `tt` system space `_tt_migrations`.

Makefile

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
SHELL := /bin/bash
22

3-
S3_TARANTOOL_SDK_3_PATH := s3://packages/enterprise/release/linux/x86_64/3.3/tarantool-enterprise-sdk-gc64-3.3.1-0-r55.linux.x86_64.tar.gz
4-
S3_TARANTOOL_SDK_2_PATH := s3://packages/enterprise/release/linux/x86_64/2.11/tarantool-enterprise-sdk-gc64-2.11.7-0-r689.linux.x86_64.tar.gz
3+
S3_TARANTOOL_SDK_3_PATH := s3://packages/enterprise/release/linux/x86_64/3.3/tarantool-enterprise-sdk-gc64-3.3.2-0-r62.linux.x86_64.tar.gz
4+
S3_TARANTOOL_SDK_2_PATH := s3://packages/enterprise/release/linux/x86_64/2.11/tarantool-enterprise-sdk-gc64-2.11.7-0-r691.linux.x86_64.tar.gz
55
S3_ENDPOINT_URL := $(if $(S3_ENDPOINT_URL),$(S3_ENDPOINT_URL),https://hb.vkcs.cloud)
66

7-
SDK_TEST := $(if $(SDK_TEST),$(SDK_TEST),sdk-3)
8-
97
.rocks: sdk
108
# в sdk-2 есть все нужные роки, в sdk-3 нет
119
source ./sdk-2/env.sh && \
1210
tt rocks install luacheck 0.26.0 --only-server=sdk-2/rocks && \
1311
tt rocks install luacov 0.13.0 --only-server=sdk-2/rocks && \
1412
tt rocks install luacov-reporters 0.1.0 --only-server=sdk-2/rocks && \
15-
tt rocks install metrics 1.1.0 --only-server=sdk-2/rocks && \
13+
tt rocks install metrics 1.4.0 --only-server=sdk-2/rocks && \
1614
tt rocks install ddl-ee 1.8.0 --only-server=sdk-2/rocks && \
17-
tt rocks install cartridge 2.16.0 --only-server=sdk-2/rocks && \
15+
tt rocks install cartridge 2.16.2 --only-server=sdk-2/rocks && \
1816
tt rocks install migrations-ee 1.3.2 --only-server=sdk-2/rocks && \
1917
tt rocks make
2018

@@ -38,10 +36,28 @@ lint: .rocks
3836

3937
.PHONY: test
4038
test:
41-
@echo "RUN TESTS WITH $(SDK_TEST)"
42-
# luatest будет свой для каждого sdk
43-
source $(SDK_TEST)/env.sh && \
44-
tt rocks install luatest 1.0.1 --only-server=$(SDK_TEST)/rocks && \
39+
@if [ -z "$(SDK_TEST)" ]; then \
40+
echo "Select SDK:"; \
41+
echo "1) SDK with Tarantool 2.x"; \
42+
echo "2) SDK with Tarantool 3.x"; \
43+
read -p "Enter number (1 or 2): " choice; \
44+
case $$choice in \
45+
1) SDK_TEST=sdk-2; SDK_LABEL="SDK with Tarantool 2.x" ;; \
46+
2) SDK_TEST=sdk-3; SDK_LABEL="SDK with Tarantool 3.x" ;; \
47+
*) echo "Invalid selection" >&2; exit 1 ;; \
48+
esac; \
49+
else \
50+
if [ "$(SDK_TEST)" = "sdk-2" ]; then \
51+
SDK_LABEL="SDK with Tarantool 2.x"; \
52+
elif [ "$(SDK_TEST)" = "sdk-3" ]; then \
53+
SDK_LABEL="SDK with Tarantool 3.x"; \
54+
else \
55+
SDK_LABEL="Custom SDK ($(SDK_TEST))"; \
56+
fi; \
57+
fi; \
58+
echo "Running tests with $$SDK_LABEL..."; \
59+
source $$SDK_TEST/env.sh && \
60+
tt rocks install luatest 1.0.1 --only-server=$$SDK_TEST/rocks && \
4561
.rocks/bin/luatest -v --coverage test/
4662

4763
coverage:

crud/common/sharding/init.lua

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ function sharding.get_replicasets_by_bucket_id(vshard_router, bucket_id)
2929
end
3030

3131
function sharding.key_get_bucket_id(vshard_router, space_name, key, specified_bucket_id)
32-
dev_checks('table', 'string', '?', '?number|cdata')
32+
dev_checks('table', 'string', '?', '?')
3333

3434
if specified_bucket_id ~= nil then
35+
local err = sharding.validate_bucket_id(specified_bucket_id)
36+
if err ~= nil then
37+
return nil, err
38+
end
39+
3540
return { bucket_id = specified_bucket_id }
3641
end
3742

@@ -77,6 +82,15 @@ function sharding.tuple_get_bucket_id(vshard_router, tuple, space, specified_buc
7782
}
7883
end
7984

85+
function sharding.validate_bucket_id(bucket_id)
86+
if not utils.is_uint(bucket_id) or bucket_id < 1 then
87+
return BucketIDError:new(
88+
"Invalid bucket_id: expected unsigned, got %s",
89+
type(bucket_id)
90+
)
91+
end
92+
end
93+
8094
function sharding.tuple_set_and_return_bucket_id(vshard_router, tuple, space, specified_bucket_id)
8195
local bucket_id_fieldno, err = utils.get_bucket_id_fieldno(space)
8296
if err ~= nil then
@@ -108,6 +122,11 @@ function sharding.tuple_set_and_return_bucket_id(vshard_router, tuple, space, sp
108122
sharding_data.skip_sharding_hash_check = true
109123
end
110124

125+
err = sharding.validate_bucket_id(sharding_data.bucket_id)
126+
if err ~= nil then
127+
return nil, err
128+
end
129+
111130
return sharding_data
112131
end
113132

crud/common/utils.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,4 +1338,15 @@ function utils.append_array(array_src, array_dst)
13381338
return array_src
13391339
end
13401340

1341+
function utils.is_uint(value)
1342+
if type(value) == 'number' then
1343+
return value >= 0 and math.floor(value) == value
1344+
elseif type(value) == 'cdata' then
1345+
local ok, casted = pcall(tonumber, value)
1346+
return ok and type(casted) == 'number' and casted >= 0 and math.floor(casted) == casted
1347+
end
1348+
1349+
return false
1350+
end
1351+
13411352
return utils

crud/compare/plan.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function plan.new(space, conditions, opts)
168168
field_names = '?table',
169169
force_map_call = '?boolean',
170170
sharding_key_as_index_obj = '?table',
171-
bucket_id = '?number|cdata',
171+
bucket_id = '?',
172172
})
173173

174174
conditions = conditions ~= nil and conditions or {}

crud/count.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ end
110110
local function call_count_on_router(vshard_router, space_name, user_conditions, opts)
111111
checks('table', 'string', '?table', {
112112
timeout = '?number',
113-
bucket_id = '?number|cdata',
113+
bucket_id = '?',
114114
force_map_call = '?boolean',
115115
fullscan = '?boolean',
116116
yield_every = '?number',
@@ -317,7 +317,7 @@ end
317317
function count.call(space_name, user_conditions, opts)
318318
checks('string', '?table', {
319319
timeout = '?number',
320-
bucket_id = '?number|cdata',
320+
bucket_id = '?',
321321
force_map_call = '?boolean',
322322
fullscan = '?boolean',
323323
yield_every = '?number',

crud/delete.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ delete.storage_api = {[DELETE_FUNC_NAME] = delete_on_storage}
6060
local function call_delete_on_router(vshard_router, space_name, key, opts)
6161
dev_checks('table', 'string', '?', {
6262
timeout = '?number',
63-
bucket_id = '?number|cdata',
63+
bucket_id = '?',
6464
fields = '?table',
6565
vshard_router = '?string|table',
6666
noreturn = '?boolean',
@@ -194,7 +194,7 @@ end
194194
function delete.call(space_name, key, opts)
195195
checks('string', '?', {
196196
timeout = '?number',
197-
bucket_id = '?number|cdata',
197+
bucket_id = '?',
198198
fields = '?table',
199199
vshard_router = '?string|table',
200200
noreturn = '?boolean',

crud/get.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ get.storage_api = {[GET_FUNC_NAME] = get_on_storage}
5858
local function call_get_on_router(vshard_router, space_name, key, opts)
5959
dev_checks('table', 'string', '?', {
6060
timeout = '?number',
61-
bucket_id = '?number|cdata',
61+
bucket_id = '?',
6262
fields = '?table',
6363
prefer_replica = '?boolean',
6464
balance = '?boolean',
@@ -196,7 +196,7 @@ end
196196
function get.call(space_name, key, opts)
197197
checks('string', '?', {
198198
timeout = '?number',
199-
bucket_id = '?number|cdata',
199+
bucket_id = '?',
200200
fields = '?table',
201201
prefer_replica = '?boolean',
202202
balance = '?boolean',

crud/insert.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ insert.storage_api = {[INSERT_FUNC_NAME] = insert_on_storage}
6161
local function call_insert_on_router(vshard_router, space_name, original_tuple, opts)
6262
dev_checks('table', 'string', 'table', {
6363
timeout = '?number',
64-
bucket_id = '?number|cdata',
64+
bucket_id = '?',
6565
add_space_schema_hash = '?boolean',
6666
fields = '?table',
6767
vshard_router = '?string|table',
@@ -175,7 +175,7 @@ end
175175
function insert.tuple(space_name, tuple, opts)
176176
checks('string', 'table', {
177177
timeout = '?number',
178-
bucket_id = '?number|cdata',
178+
bucket_id = '?',
179179
add_space_schema_hash = '?boolean',
180180
fields = '?table',
181181
vshard_router = '?string|table',
@@ -214,7 +214,7 @@ end
214214
function insert.object(space_name, obj, opts)
215215
checks('string', 'table', {
216216
timeout = '?number',
217-
bucket_id = '?number|cdata',
217+
bucket_id = '?',
218218
add_space_schema_hash = '?boolean',
219219
fields = '?table',
220220
vshard_router = '?string|table',

crud/replace.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ replace.storage_api = {[REPLACE_FUNC_NAME] = replace_on_storage}
6161
local function call_replace_on_router(vshard_router, space_name, original_tuple, opts)
6262
dev_checks('table', 'string', 'table', {
6363
timeout = '?number',
64-
bucket_id = '?number|cdata',
64+
bucket_id = '?',
6565
add_space_schema_hash = '?boolean',
6666
fields = '?table',
6767
vshard_router = '?string|table',
@@ -174,7 +174,7 @@ end
174174
function replace.tuple(space_name, tuple, opts)
175175
checks('string', 'table', {
176176
timeout = '?number',
177-
bucket_id = '?number|cdata',
177+
bucket_id = '?',
178178
add_space_schema_hash = '?boolean',
179179
fields = '?table',
180180
vshard_router = '?string|table',
@@ -213,7 +213,7 @@ end
213213
function replace.object(space_name, obj, opts)
214214
checks('string', 'table', {
215215
timeout = '?number',
216-
bucket_id = '?number|cdata',
216+
bucket_id = '?',
217217
add_space_schema_hash = '?boolean',
218218
fields = '?table',
219219
vshard_router = '?string|table',

0 commit comments

Comments
 (0)