Skip to content

Commit 5c395fe

Browse files
Extract sharding key from conditions
PR #181 introduced support of DDL sharding keys. But if sharding key hasn't got a separate index in schema, select with equal conditions for all required sharding key fields still led to map-reduce instead of a single storage call. This patch introduces impoved support of sharding keys extraction and fixes the issue. Closes #213
1 parent cc2e0e8 commit 5c395fe

File tree

7 files changed

+413
-57
lines changed

7 files changed

+413
-57
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2020

2121
* Use tuple-merger backed select implementation on tarantool 2.10+ (it gives
2222
less pressure on Lua GC).
23+
* DDL sharding key now can be extracted from select conditions even if
24+
there are no separate index.
2325

2426
## [0.9.0] - 20-10-21
2527

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ Current limitations for using custom sharding key:
101101
updated on storages, see
102102
[#212](https://github.com/tarantool/crud/issues/212). However it is possible
103103
to do it manually with `require('crud.sharding_key').update_cache()`.
104-
- CRUD select may lead map reduce in some cases, see
105-
[#213](https://github.com/tarantool/crud/issues/213).
106104
- No support of JSON path for sharding key, see
107105
[#219](https://github.com/tarantool/crud/issues/219).
108106
- `primary_index_fieldno_map` is not cached, see

crud/select/plan.lua

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,49 +48,91 @@ local function get_index_for_condition(space_indexes, space_format, condition)
4848
end
4949
end
5050

51-
local function extract_sharding_key_from_scan_value(scan_value, scan_index, sharding_index)
52-
if #scan_value < #sharding_index.parts then
53-
return nil
54-
end
51+
local function extract_sharding_key_from_conditions(conditions, sharding_index, space_indexes, fieldno_map)
52+
dev_checks('table', 'table', 'table', 'table')
53+
54+
-- If name is both valid index name and field name,
55+
-- it is interpreted as index name.
56+
local filled_fields = {}
57+
for _, condition in ipairs(conditions) do
58+
if condition.operator ~= compare_conditions.operators.EQ then
59+
goto continue
60+
end
5561

56-
if scan_index.id == sharding_index.id then
57-
return scan_value
58-
end
62+
local index = space_indexes[condition.operand]
63+
if index ~= nil then
64+
for i, part in ipairs(index.parts) do
65+
-- Consider the following case:
66+
-- index_0: {'foo', 'bar'},
67+
-- index_1: {'baz', 'bar'},
68+
-- conditions: {{'==', 'index_0', {1, 2}}, {'==', 'index_1', {3, nil}}}.
69+
-- To check that nil parts will not overwrite already filled_fields,
70+
-- we verify that filled_fields[part.fieldno] is empty. If there are
71+
-- more than one non-null different value in conditions with equal operator,
72+
-- request is already in conflict and it doesn't matter what sharding key we
73+
-- will return.
74+
if filled_fields[part.fieldno] == nil then
75+
filled_fields[part.fieldno] = condition.values[i]
76+
end
77+
end
5978

60-
local scan_value_fields_values = {}
61-
for i, scan_index_part in ipairs(scan_index.parts) do
62-
scan_value_fields_values[scan_index_part.fieldno] = scan_value[i]
79+
goto continue
80+
end
81+
82+
local fieldno = fieldno_map[condition.operand]
83+
if fieldno == nil then
84+
goto continue
85+
end
86+
filled_fields[fieldno] = condition.values[1]
87+
88+
::continue::
6389
end
6490

65-
-- check that sharding key is included in the scan index fields
6691
local sharding_key = {}
67-
for _, sharding_key_part in ipairs(sharding_index.parts) do
68-
local fieldno = sharding_key_part.fieldno
69-
70-
-- sharding key isn't included in scan key
71-
if scan_value_fields_values[fieldno] == nil then
92+
for i, v in ipairs(sharding_index.parts) do
93+
if filled_fields[v.fieldno] == nil then
7294
return nil
7395
end
7496

75-
local field_value = scan_value_fields_values[fieldno]
97+
sharding_key[i] = filled_fields[v.fieldno]
98+
end
7699

77-
-- sharding key contains nil values
78-
if field_value == nil then
79-
return nil
100+
return sharding_key
101+
end
102+
103+
local function get_sharding_key_from_scan_value(scan_value, scan_index, scan_iter, sharding_index)
104+
dev_checks('?', 'table', 'number', 'table')
105+
106+
if scan_value == nil then
107+
return nil
108+
end
109+
110+
if scan_iter ~= box.index.EQ and scan_iter ~= box.index.REQ then
111+
return nil
112+
end
113+
114+
if scan_index.id == sharding_index.id then
115+
if type(scan_value) ~= 'table' then
116+
return scan_value
80117
end
81118

82-
table.insert(sharding_key, field_value)
119+
for i, _ in ipairs(sharding_index.parts) do
120+
if scan_value[i] == nil then return nil end
121+
end
122+
return scan_value
83123
end
84124

85-
return sharding_key
125+
return nil
86126
end
87127

88128
-- We need to construct after_tuple by field_names
89129
-- because if `fields` option is specified we have after_tuple with partial fields
90130
-- and these fields are ordered by field_names + primary key + scan key
91131
-- this order can be differ from order in space format
92132
-- so we need to cast after_tuple to space format for scrolling tuples on storage
93-
local function construct_after_tuple_by_fields(space_format, field_names, tuple)
133+
local function construct_after_tuple_by_fields(fieldno_map, field_names, tuple)
134+
dev_checks('table', '?table', '?table|cdata')
135+
94136
if tuple == nil then
95137
return nil
96138
end
@@ -99,15 +141,10 @@ local function construct_after_tuple_by_fields(space_format, field_names, tuple)
99141
return tuple
100142
end
101143

102-
local positions = {}
103144
local transformed_tuple = {}
104145

105-
for i, field in ipairs(space_format) do
106-
positions[field.name] = i
107-
end
108-
109146
for i, field_name in ipairs(field_names) do
110-
local fieldno = positions[field_name]
147+
local fieldno = fieldno_map[field_name]
111148
if fieldno == nil then
112149
return nil, FilterFieldsError:new(
113150
'Space format doesn\'t contain field named %q', field_name
@@ -145,6 +182,8 @@ function select_plan.new(space, conditions, opts)
145182
local scan_value
146183
local scan_condition_num
147184

185+
local fieldno_map = utils.get_format_fieldno_map(space_format)
186+
148187
-- search index to iterate over
149188
for i, condition in ipairs(conditions) do
150189
scan_index = get_index_for_condition(space_indexes, space_format, condition)
@@ -176,9 +215,7 @@ function select_plan.new(space, conditions, opts)
176215

177216
-- handle opts.first
178217
local total_tuples_count
179-
local scan_after_tuple, err = construct_after_tuple_by_fields(
180-
space_format, field_names, opts.after_tuple
181-
)
218+
local scan_after_tuple, err = construct_after_tuple_by_fields(fieldno_map, field_names, opts.after_tuple)
182219
if err ~= nil then
183220
return nil, err
184221
end
@@ -230,9 +267,11 @@ function select_plan.new(space, conditions, opts)
230267
local sharding_index = opts.sharding_key_as_index_obj or primary_index
231268

232269
-- get sharding key value
233-
local sharding_key
234-
if scan_value ~= nil and (scan_iter == box.index.EQ or scan_iter == box.index.REQ) then
235-
sharding_key = extract_sharding_key_from_scan_value(scan_value, scan_index, sharding_index)
270+
local sharding_key = get_sharding_key_from_scan_value(scan_value, scan_index, scan_iter, sharding_index)
271+
272+
if sharding_key == nil then
273+
sharding_key = extract_sharding_key_from_conditions(conditions, sharding_index,
274+
space_indexes, fieldno_map)
236275
end
237276

238277
local plan = {
@@ -251,4 +290,9 @@ function select_plan.new(space, conditions, opts)
251290
return plan
252291
end
253292

293+
select_plan.internal = {
294+
get_sharding_key_from_scan_value = get_sharding_key_from_scan_value,
295+
extract_sharding_key_from_conditions = extract_sharding_key_from_conditions
296+
}
297+
254298
return select_plan

test/entrypoint/srv_ddl.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ package.preload['customers-storage'] = function()
6161
{path = 'name', is_nullable = false, type = 'string'},
6262
},
6363
}
64+
local age_index = {
65+
name = 'age',
66+
type = 'TREE',
67+
unique = false,
68+
parts = {
69+
{path = 'age', is_nullable = false, type = 'number'},
70+
},
71+
}
6472
local secondary_index = {
6573
name = 'secondary',
6674
type = 'TREE',
@@ -71,6 +79,17 @@ package.preload['customers-storage'] = function()
7179
},
7280
}
7381

82+
local three_fields_index = {
83+
name = 'three_fields',
84+
type = 'TREE',
85+
unique = false,
86+
parts = {
87+
{path = 'age', is_nullable = false, type = 'number'},
88+
{path = 'name', is_nullable = false, type = 'string'},
89+
{path = 'id', is_nullable = false, type = 'unsigned'},
90+
},
91+
}
92+
7493
local customers_name_key_schema = table.deepcopy(customers_schema)
7594
customers_name_key_schema.sharding_key = {'name'}
7695
table.insert(customers_name_key_schema.indexes, primary_index)
@@ -100,13 +119,27 @@ package.preload['customers-storage'] = function()
100119
table.insert(customers_age_key_schema.indexes, primary_index)
101120
table.insert(customers_age_key_schema.indexes, bucket_id_index)
102121

122+
local customers_name_age_key_different_indexes_schema = table.deepcopy(customers_schema)
123+
customers_name_age_key_different_indexes_schema.sharding_key = {'name', 'age'}
124+
table.insert(customers_name_age_key_different_indexes_schema.indexes, primary_index)
125+
table.insert(customers_name_age_key_different_indexes_schema.indexes, bucket_id_index)
126+
table.insert(customers_name_age_key_different_indexes_schema.indexes, age_index)
127+
128+
local customers_name_age_key_three_fields_index_schema = table.deepcopy(customers_schema)
129+
customers_name_age_key_three_fields_index_schema.sharding_key = {'name', 'age'}
130+
table.insert(customers_name_age_key_three_fields_index_schema.indexes, primary_index_id)
131+
table.insert(customers_name_age_key_three_fields_index_schema.indexes, bucket_id_index)
132+
table.insert(customers_name_age_key_three_fields_index_schema.indexes, three_fields_index)
133+
103134
local schema = {
104135
spaces = {
105136
customers_name_key = customers_name_key_schema,
106137
customers_name_key_uniq_index = customers_name_key_uniq_index_schema,
107138
customers_name_key_non_uniq_index = customers_name_key_non_uniq_index_schema,
108139
customers_secondary_idx_name_key = customers_secondary_idx_name_key_schema,
109140
customers_age_key = customers_age_key_schema,
141+
customers_name_age_key_different_indexes = customers_name_age_key_different_indexes_schema,
142+
customers_name_age_key_three_fields_index = customers_name_age_key_three_fields_index_schema,
110143
}
111144
}
112145

test/helpers/storage_stat.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,16 @@ function storage_stat.diff(a, b)
9595
return diff
9696
end
9797

98+
-- Accepts collect (or diff) return value and returns
99+
-- total number of select requests across all storages.
100+
function storage_stat.total(stats)
101+
local total = 0
102+
103+
for _, stat in pairs(stats) do
104+
total = total + (stat.select_requests or 0)
105+
end
106+
107+
return total
108+
end
109+
98110
return storage_stat

0 commit comments

Comments
 (0)