-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.lua
234 lines (194 loc) · 7.55 KB
/
config.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
#!/usr/bin/env tarantool
require('strict').on()
local uuid = require('uuid')
local vshard = require 'vshard'
-- Get instance name
local NAME = os.getenv("TEST_TNT_WORK_DIR")
local fiber = require('fiber')
-- Check if we are running under test-run
if os.getenv('ADMIN') then
test_run = require('test_run').new()
require('console').listen(os.getenv('ADMIN'))
end
-- Call a configuration provider
local cfg = {
sharding = {
['cbf06940-0790-498b-948d-042b62cf3d29'] = { -- replicaset #1
replicas = {
['8a274925-a26d-47fc-9e1b-af88ce939412'] = {
uri = 'storage:[email protected]:3301',
name = 'storage_1_a',
master = true,
},
['3de2e3e1-9ebe-4d0d-abb1-26d301b84633'] = {
uri = 'storage:[email protected]:3302',
name = 'storage_1_b'
},
},
}, -- replicaset #1
['ac522f65-aa94-4134-9f64-51ee384f1a54'] = { -- replicaset #2
replicas = {
['1e02ae8a-afc0-4e91-ba34-843a356b8ed7'] = {
uri = 'storage:[email protected]:3303',
name = 'storage_2_a',
master = true,
},
['001688c3-66f8-4a31-8e19-036c17d489c2'] = {
uri = 'storage:[email protected]:3304',
name = 'storage_2_b'
}
},
}, -- replicaset #2
}, -- sharding
replication_connect_quorum = 0, -- its oke if we havent some replicas
}
-- Name to uuid map
local names = {
['storage_1_a'] = '8a274925-a26d-47fc-9e1b-af88ce939412',
['storage_1_b'] = '3de2e3e1-9ebe-4d0d-abb1-26d301b84633',
['storage_1_c'] = '3de2e3e1-9ebe-4d0d-abb1-26d301b84635',
['storage_2_a'] = '1e02ae8a-afc0-4e91-ba34-843a356b8ed7',
['storage_2_b'] = '001688c3-66f8-4a31-8e19-036c17d489c2',
}
replicasets = {'cbf06940-0790-498b-948d-042b62cf3d29',
'ac522f65-aa94-4134-9f64-51ee384f1a54'}
rawset(_G, 'vshard', vshard) -- set as global variable
-- Если мы являемся роутером, то применяем другой конфиг
if NAME == "router" then
cfg.listen = "0.0.0.0:12000"
cfg['bucket_count'] = 100
local router = vshard.router.new('router', cfg)
rawset(_G, 'router', router) -- set as global variable
local api = {}
rawset(_G, 'api', api)
router:bootstrap({timeout = 4, if_not_bootstrapped = true})
function api.add_product(product)
local bucket_id = router:bucket_id_strcrc32(product.id)
product.bucket_id = bucket_id
return router:call(bucket_id, 'write', 'product_add', {product})
end
function api.get_product(req)
local bucket_id = router:bucket_id_strcrc32(req.id)
return router:call(bucket_id, 'read', 'product_get', {req})
end
else
vshard.storage.cfg(cfg, names[NAME])
-- everything below is copypasted from storage.lua in vshard example:
-- https://github.com/tarantool/vshard/blob/dfa2cc8a2aff221d5f421298851a9a229b2e0434/example/storage.lua
box.once("testapp:schema:1", function()
local customer = box.schema.space.create('customer')
customer:format({
{'customer_id', 'unsigned'},
{'bucket_id', 'unsigned'},
{'name', 'string'},
})
customer:create_index('customer_id', {parts = {'customer_id'}})
customer:create_index('bucket_id', {parts = {'bucket_id'}, unique = false})
-- create products for easy bench
local products = box.schema.space.create('products')
products:format({
{'id', 'uuid'},
{'bucket_id', 'unsigned'},
{'name', 'string'},
{'count', 'unsigned'},
})
products:create_index('id', {parts = {'id'}})
products:create_index('bucket_id', {parts = {'bucket_id'}, unique = false})
local account = box.schema.space.create('account')
account:format({
{'account_id', 'unsigned'},
{'customer_id', 'unsigned'},
{'bucket_id', 'unsigned'},
{'balance', 'unsigned'},
{'name', 'string'},
})
account:create_index('account_id', {parts = {'account_id'}})
account:create_index('customer_id', {parts = {'customer_id'}, unique = false})
account:create_index('bucket_id', {parts = {'bucket_id'}, unique = false})
box.snapshot()
box.schema.func.create('customer_lookup')
box.schema.role.grant('public', 'execute', 'function', 'customer_lookup')
box.schema.func.create('customer_add')
box.schema.role.grant('public', 'execute', 'function', 'customer_add')
box.schema.func.create('echo')
box.schema.role.grant('public', 'execute', 'function', 'echo')
box.schema.func.create('sleep')
box.schema.role.grant('public', 'execute', 'function', 'sleep')
box.schema.func.create('raise_luajit_error')
box.schema.role.grant('public', 'execute', 'function', 'raise_luajit_error')
box.schema.func.create('raise_client_error')
box.schema.role.grant('public', 'execute', 'function', 'raise_client_error')
box.schema.user.grant('storage', 'super')
box.schema.user.create('tarantool')
box.schema.user.grant('tarantool', 'super')
end)
local function insert_customer(customer)
box.space.customer:insert({customer.customer_id, customer.bucket_id, customer.name})
for _, account in ipairs(customer.accounts) do
box.space.account:insert({
account.account_id,
customer.customer_id,
customer.bucket_id,
0,
account.name
})
end
end
function customer_add(customer)
local ares = box.atomic(insert_customer, customer)
return {res = ares }
end
function customer_lookup(customer_id)
if type(customer_id) ~= 'number' then
error('Usage: customer_lookup(customer_id)')
end
local customer = box.space.customer:get(customer_id)
if customer == nil then
return nil
end
customer = {
customer_id = customer.customer_id;
name = customer.name;
}
local accounts = {}
for _, account in box.space.account.index.customer_id:pairs(customer_id) do
table.insert(accounts, {
account_id = account.account_id;
name = account.name;
balance = account.balance;
})
end
customer.accounts = accounts;
return customer, {test=123}
end
function echo(...)
return ...
end
function sleep(time)
fiber.sleep(time)
return true
end
function raise_luajit_error()
assert(1 == 2)
end
function raise_client_error()
box.error(box.error.UNKNOWN)
end
-- product_add - simple add some product to storage
function product_add(product)
local id = uuid.fromstr(product.id)
box.space.products:insert({ id, product.bucket_id, product.name, product.count})
return true
end
-- product_get - simple select for benches
function product_get(req)
local product = box.space.products:get(uuid.fromstr(req.id))
return {
name = product.name,
id = product.id:str()
}
end
end
box.once('access:v1', function()
box.schema.user.grant('guest', 'read,write,execute', 'universe')
end)