-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
290 lines (251 loc) · 8.12 KB
/
server.lua
File metadata and controls
290 lines (251 loc) · 8.12 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
------------------------------------------------------------------------------
-- MASTER
------------------------------------------------------------------------------
if package.setsearchroot ~= nil then
package.setsearchroot()
end
------------------------------------------------------------------------------
-- TARANTOOL CONFIGURATION
------------------------------------------------------------------------------
box.cfg {
listen = 3301,
memtx_memory = 128 * 1024 * 1024,
log_level = 6
}
------------------------------------------------------------------------------
-- IMPORTS AND MODULE LOCALS
------------------------------------------------------------------------------
local log = require('log')
local fiber = require('fiber')
local socket = require('socket')
local utils = dofile('/data/utils.lua')
local LIMIT = 512
local BIND = '0.0.0.0'
local call_counter = 0
local session_counter = 0
local connects_registered = {}
pipe_lock = false
------------------------------------------------------------------------------
-- UTILITY FUNCTIONS
------------------------------------------------------------------------------
local get_version = utils.get_version
local create_kv_space = utils.create_kv_space
local create_complex_space = utils.create_complex_space
local fail = utils.fail
------------------------------------------------------------------------------
-- SCHEMA
------------------------------------------------------------------------------
box.once('schema', function()
create_kv_space('test')
create_kv_space('space_a')
create_kv_space('space_b')
create_complex_space('person')
box.space.space_b:on_replace(function(old, new, s, op)
box.session.push(old)
end)
box.schema.user.create('service_user', {
password = '',
if_not_exists = true
})
box.schema.user.grant('service_user', 'super')
box.schema.user.create('user_a', {
password = 'secret_a',
if_not_exists = true
})
box.schema.user.create('user_b', {
password = 'secret_b',
if_not_exists = true
})
box.schema.user.create('user_c', {
password = 'secret_c',
if_not_exists = true
})
box.schema.user.create('user_d', {
password = 'secret_d',
if_not_exists = true
})
box.schema.user.create('replicator', {
password = 'password'
})
-- need for manual testing when tarantool is run just as
-- `tarantool server.lua`.
-- When this user created via env variables there will be no effect
box.schema.user.create('api_user', {
password = 'secret',
if_not_exists = true
})
box.schema.user.grant('api_user', 'super') -- created via env variables when testcontainer is starting
box.schema.user.grant('user_a', 'read,write', 'space', 'space_a')
box.schema.user.grant('user_a', 'execute', 'universe')
box.schema.user.grant('user_b', 'read,write', 'space', 'space_b')
box.schema.user.grant('user_b', 'read', 'space', 'space_a')
box.schema.user.revoke('user_c', 'session', 'universe')
box.schema.user.revoke('user_d', 'usage', 'universe')
box.schema.user.grant('user_d', 'execute', 'universe')
box.schema.user.grant('replicator', 'replication')
log.info('schema created on master')
end)
------------------------------------------------------------------------------
-- PRIVATE FUNCTIONS
------------------------------------------------------------------------------
local function same(data)
return data
end
local function same_with_lock(data)
if pipe_lock then
return
end
return data
end
local function corruptor(data)
return string.char(math.random(0, 255)):rep(#data)
end
local function pipe(stopper, from, to, convertor)
while not stopper.active do
if from:readable(0.1) then
local chunk = from:sysread(LIMIT)
if chunk == '' or chunk == nil then
stopper.active = true
return
end
local converted = convertor(chunk)
if converted ~= nil then
to:send(converted)
end
end
end
end
-- Makes socket handler for generic proxy. It accepts two callbacks - one for
-- handling data transmitting from client ot server, the second one is for data
-- transmitting from server to client. It allows perform different kind of
-- checks, for example delays of packets, corrupting packets (to check how
-- corrupted packed will be processed on connector side) and so on.
local function create_proxy(client2srv, srv2client)
return function(client)
local stopper = { active = false }
local proxy = socket.tcp_connect('127.0.0.1', 3301)
local p1 = fiber.create(pipe, stopper, client, proxy, client2srv)
local p2 = fiber.create(pipe, stopper, proxy, client, srv2client)
p1:set_joinable(true)
p2:set_joinable(true)
p1:join()
p2:join()
proxy:close()
end
end
-- This server closes listening socket when it gets any incoming connection.
-- It needed for checks about "Connection closed by remote side / server"
-- errors on client side.
local function closing_server()
while true do
local srv_sock = socket('AF_INET', 'SOCK_STREAM', 'tcp')
srv_sock:bind(BIND, 3302)
srv_sock:listen()
log.info('listening')
-- waits till data arrived
if srv_sock:wait() == 'R' then
log.info('close suddenly socket')
srv_sock:close()
end
end
end
-- This server does not call acept for any incoming connection. It needed for
-- checks about tcp connect timeout (when we set coninect timeout for tcp
-- sockect in netty Bootstrap)
local function non_accepting_server()
local srv_sock
srv_sock = socket('AF_INET', 'SOCK_STREAM', 'tcp')
srv_sock:bind(BIND, 3303)
srv_sock:listen()
end
------------------------------------------------------------------------------
-- PUBLIC FUNCTIONS
------------------------------------------------------------------------------
_G.get_version = get_version
_G.fail = fail
function echo_with_wrapping(...)
-- we use table packing because it's more popular than multi return value
return { ... }
end
function echo(...)
return ...
end
function echo_to_map(...)
return (...):tomap()
end
function return_true()
return true
end
function return_number()
return 2008
end
function slow_echo(...)
fiber.sleep(1.5)
return ...
end
function nonslow_echo(...)
fiber.sleep(0.5)
return ...
end
function wrong_ret()
return function()
return 1
end
end
function fail_by_box_error()
box.error({ reason = 'fail' })
end
function wrapped_fail_by_box_error()
local _, err = pcall(fail_by_box_error)
local exception = box.error.new({ reason = 'wrapped failure' })
exception:set_prev(err)
box.error(exception)
end
function echo_with_push(...)
box.session.push({ "push1", "out of band" })
box.session.push({ "push2", "out of band" })
return ...
end
function insert(space, tuple)
local cond = fiber.cond()
fiber.create(function()
box.space[space]:insert(tuple)
cond:broadcast()
end)
return cond:wait()
end
function inc()
local ssid = box.session.id()
if connects_registered[ssid] == nil then
session_counter = session_counter + 1
connects_registered[ssid] = true
end
call_counter = call_counter + 1
end
function get_call_counter()
return call_counter
end
function get_session_counter()
return session_counter
end
function reset_call_counter()
call_counter = 0
end
function lock_pipe(flag)
pipe_lock = flag
end
function stuck()
fiber.create(function()
fiber.sleep(1)
while true do
end
end)
end
------------------------------------------------------------------------------
-- FIBERS, TASKS, SERVERS
------------------------------------------------------------------------------
fiber.create(closing_server)
fiber.create(non_accepting_server)
socket.tcp_server(BIND, 3304, create_proxy(same, corruptor))
socket.tcp_server(BIND, 3305, create_proxy(same_with_lock, same))
socket.tcp_server(BIND, 3306, create_proxy(same, same_with_lock))