Skip to content

Commit 9a8dab1

Browse files
committed
get sharding keys from a space
1 parent 0b9e6b7 commit 9a8dab1

File tree

6 files changed

+22
-63
lines changed

6 files changed

+22
-63
lines changed

crud/common/sharding.lua

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ local ShardingKeyError = errors.new_class("ShardingKeyError", {capture_stack = f
1111
local dev_checks = require('crud.common.dev_checks')
1212
local utils = require('crud.common.utils')
1313

14-
-- TODO: invalidate ddl_schema_cache
15-
local ddl_schema_cache = nil
14+
local ddl_sharding_keys_cache = {}
1615
local sharding_key_in_primary_index = {}
1716

1817
local sharding = {}
@@ -32,7 +31,7 @@ function sharding.tuple_get_bucket_id(tuple, space, specified_bucket_id)
3231

3332
local primary_index = space.index[0]
3433
local key
35-
local sharding_key = sharding.get_ddl_sharding_key(space.name)
34+
local sharding_key = sharding.get_ddl_sharding_key(space.name, vshard.router.routeall())
3635
if sharding_key ~= nil then
3736
local sharding_keys_fieldnos = utils.get_keys_fieldnos(space:format(), sharding_key)
3837
if sharding_keys_fieldnos ~= nil then
@@ -74,23 +73,23 @@ function sharding.tuple_set_and_return_bucket_id(tuple, space, specified_bucket_
7473
return bucket_id
7574
end
7675

77-
-- Get sharding key (actually field names) using DDL module.
78-
function sharding.get_ddl_sharding_key(space_name)
79-
dev_checks('string')
80-
if ddl == false then
81-
return nil
82-
end
83-
if ddl_schema_cache == nil then
84-
ddl_schema_cache = ddl.get_schema()
85-
end
86-
assert(type(ddl_schema_cache.spaces), 'table')
87-
local space_schema = ddl_schema_cache.spaces[space_name]
88-
if space_schema == nil or
89-
space_schema.sharding_key == nil then
90-
return nil
76+
-- Get sharding key (actually field names).
77+
-- Returns a table with field names or nil.
78+
function sharding.get_ddl_sharding_key(space_name, replicasets)
79+
dev_checks('string', 'table')
80+
81+
if ddl_sharding_keys_cache[space_name] == nil then
82+
local replicaset = select(2, next(replicasets))
83+
local conn_master = replicaset.master.conn
84+
local sharding_keys = conn_master.space._ddl_sharding_key
85+
if sharding_keys == nil then
86+
return nil
87+
end
88+
local record = conn_master.space._ddl_sharding_key:get{space_name}
89+
ddl_sharding_keys_cache[space_name] = record and record.sharding_keys
9190
end
9291

93-
return space_schema.sharding_key
92+
return ddl_sharding_keys_cache[space_name]
9493
end
9594

9695
-- Make sure fields used in sharding key are present in primary index.

crud/delete.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ local function call_delete_on_router(space_name, key, opts)
5555
key = key:totable()
5656
end
5757

58-
local ddl_sharding_key = sharding.get_ddl_sharding_key(space_name)
58+
local ddl_sharding_key = sharding.get_ddl_sharding_key(space_name, vshard.router.routeall())
5959
local sharding_key = key
6060
if ddl_sharding_key ~= nil then
6161
local err

crud/get.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ local function call_get_on_router(space_name, key, opts)
5858
key = key:totable()
5959
end
6060

61-
local ddl_sharding_key = sharding.get_ddl_sharding_key(space_name)
61+
local ddl_sharding_key = sharding.get_ddl_sharding_key(space_name, vshard.router.routeall())
6262
local sharding_key = key
6363
if ddl_sharding_key ~= nil then
6464
local err

crud/select/plan.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local compare_conditions = require('crud.compare.conditions')
44
local utils = require('crud.common.utils')
55
local dev_checks = require('crud.common.dev_checks')
66
local sharding = require('crud.common.sharding')
7+
local vshard = require('vshard')
78

89
local compat = require('crud.common.compat')
910
local has_keydef = compat.exists('tuple.keydef', 'key_def')
@@ -66,7 +67,7 @@ local function extract_sharding_key_from_scan_value(scan_value, scan_index, shar
6667
-- check that sharding key is included in the scan index fields
6768
local sharding_key = {}
6869
local sharding_key_fieldno_map
69-
local ddl_sharding_key = sharding.get_ddl_sharding_key(space_name)
70+
local ddl_sharding_key = sharding.get_ddl_sharding_key(space_name, vshard.router.routeall())
7071
if ddl_sharding_key ~= nil then
7172
sharding_key_fieldno_map = utils.get_keys_fieldno_map(space_format, ddl_sharding_key)
7273
end

crud/update.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ local function call_update_on_router(space_name, key, user_operations, opts)
8383
key = key:totable()
8484
end
8585

86-
local ddl_sharding_key = sharding.get_ddl_sharding_key(space_name)
86+
local ddl_sharding_key = sharding.get_ddl_sharding_key(space_name, vshard.router.routeall())
8787
local sharding_key = key
8888
if ddl_sharding_key ~= nil then
8989
sharding_key, err = sharding.build_ddl_sharding_key(space, sharding_key, ddl_sharding_key, opts.bucket_id)

test/integration/ddl_sharding_key_test.lua

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ if not ok then
99
t.skip('Lua module ddl is required to run test')
1010
end
1111

12-
local function srv_call(srv, fn_name, ...)
13-
return srv.net_box:eval([[
14-
local fn_name, args = ...
15-
local cartridge = require('cartridge')
16-
local ddl_manager = cartridge.service_get('ddl-manager')
17-
return ddl_manager[fn_name](unpack(args))
18-
]], {fn_name, {...}})
19-
end
20-
2112
local pgroup = helpers.pgroup.new('ddl_sharding_key', {
2213
engine = {'memtx', 'vinyl'},
2314
})
@@ -42,38 +33,6 @@ pgroup:set_before_all(function(g)
4233
t.assert_equals(type(result), 'table')
4334
t.assert_equals(err, nil)
4435

45-
local schema = {
46-
spaces = {
47-
customers = {
48-
engine = g.params.engine,
49-
is_local = true,
50-
temporary = false,
51-
format = {
52-
{name = 'id', is_nullable = false, type = 'unsigned'},
53-
{name = 'bucket_id', is_nullable = false, type = 'unsigned'},
54-
{name = 'name', is_nullable = false, type = 'string'},
55-
{name = 'age', is_nullable = false, type = 'number'},
56-
},
57-
indexes = {{
58-
name = 'id',
59-
type = 'TREE',
60-
unique = true,
61-
parts = {
62-
{path = 'id', is_nullable = false, type = 'unsigned'},
63-
},
64-
}, {
65-
name = 'bucket_id',
66-
type = 'TREE',
67-
unique = false,
68-
parts = {
69-
{path = 'bucket_id', is_nullable = false, type = 'unsigned'},
70-
}
71-
}},
72-
sharding_key = {'id', 'name'},
73-
},
74-
}
75-
}
76-
srv_call(g.cluster.main_server, 'set_clusterwide_schema_lua', schema)
7736
g.space_format = g.cluster.servers[2].net_box.space.customers:format()
7837
end)
7938

0 commit comments

Comments
 (0)