11local test = require (' luatest' )
22local group = test .group (' cluster' )
3- local fiber = require (' fiber' )
43
54local helper = require (' test.helper' )
65local cluster_helpers = require (' cartridge.test-helpers' )
76
7+ local app = ' example-app'
8+ local log_level = 6
9+
10+ local cluster_alias = {
11+ tnt_router = ' tnt_router' ,
12+ tnt_storage_1_master = ' tnt_storage_1_master' ,
13+ tnt_storage_1_replica = ' tnt_storage_1_replica' ,
14+ tnt_storage_2_master = ' tnt_storage_2_master' ,
15+ tnt_storage_2_replica = ' tnt_storage_2_replica' ,
16+ }
17+
818local replicasets = {
919 {
1020 uuid = cluster_helpers .uuid (' a' ),
1121 roles = { ' vshard-router' , ' app.roles.custom' },
1222 servers = {
1323 {
1424 instance_uuid = cluster_helpers .uuid (' a' , 1 ),
15- alias = ' tnt-router ' ,
25+ alias = cluster_alias . tnt_router ,
1626 env = {
17- [' TARANTOOL_APP_NAME' ] = ' example- app' ,
18- [' TARANTOOL_LOG_LEVEL' ] = 6 ,
27+ [' TARANTOOL_APP_NAME' ] = app ,
28+ [' TARANTOOL_LOG_LEVEL' ] = log_level ,
1929 }
2030 }
2131 },
@@ -26,18 +36,18 @@ local replicasets = {
2636 servers = {
2737 {
2838 instance_uuid = cluster_helpers .uuid (' b' , 1 ),
29- alias = ' tnt-storage-1-master ' ,
39+ alias = cluster_alias . tnt_storage_1_master ,
3040 env = {
31- [' TARANTOOL_APP_NAME' ] = ' example- app' ,
32- [' TARANTOOL_LOG_LEVEL' ] = 6 ,
41+ [' TARANTOOL_APP_NAME' ] = app ,
42+ [' TARANTOOL_LOG_LEVEL' ] = log_level ,
3343 }
3444 },
3545 {
3646 instance_uuid = cluster_helpers .uuid (' b' , 2 ),
37- alias = ' tnt-storage-1-replica ' ,
47+ alias = cluster_alias . tnt_storage_1_replica ,
3848 env = {
39- [' TARANTOOL_APP_NAME' ] = ' example- app' ,
40- [' TARANTOOL_LOG_LEVEL' ] = 6 ,
49+ [' TARANTOOL_APP_NAME' ] = app ,
50+ [' TARANTOOL_LOG_LEVEL' ] = log_level ,
4151 }
4252 },
4353 }
@@ -48,31 +58,24 @@ local replicasets = {
4858 servers = {
4959 {
5060 instance_uuid = cluster_helpers .uuid (' c' , 1 ),
51- alias = ' tnt-storage-2-master ' ,
61+ alias = cluster_alias . tnt_storage_2_master ,
5262 env = {
53- [' TARANTOOL_APP_NAME' ] = ' example- app' ,
54- [' TARANTOOL_LOG_LEVEL' ] = 6 ,
63+ [' TARANTOOL_APP_NAME' ] = app ,
64+ [' TARANTOOL_LOG_LEVEL' ] = log_level ,
5565 }
5666 },
5767 {
5868 instance_uuid = cluster_helpers .uuid (' c' , 2 ),
59- alias = ' tnt-storage-2-replica ' ,
69+ alias = cluster_alias . tnt_storage_2_replica ,
6070 env = {
61- [' TARANTOOL_APP_NAME' ] = ' example- app' ,
62- [' TARANTOOL_LOG_LEVEL' ] = 6 ,
71+ [' TARANTOOL_APP_NAME' ] = app ,
72+ [' TARANTOOL_LOG_LEVEL' ] = log_level ,
6373 }
6474 }
6575 },
6676 }
6777}
6878
69- local instances = {}
70- for _ , replicaset in ipairs (replicasets ) do
71- for _ , server in ipairs (replicaset .servers ) do
72- table.insert (instances , server .alias )
73- end
74- end
75-
7679test .before_suite (function ()
7780 group .cluster = cluster_helpers .Cluster :new ({
7881 datadir = helper .datadir ,
@@ -102,25 +105,128 @@ test.after_suite(function()
102105 group .cluster :stop ()
103106end )
104107
105- local function http_request (alias , count )
108+ local tnt_router = group .cluster :server (cluster_alias .tnt_router )
109+ local tnt_storage_1_master = group .cluster :server (cluster_alias .tnt_storage_1_master )
110+ local tnt_storage_1_replica = group .cluster :server (cluster_alias .tnt_storage_1_replica )
111+ local tnt_storage_2_master = group .cluster :server (cluster_alias .tnt_storage_2_master )
112+ local tnt_storage_2_replica = group .cluster :server (cluster_alias .tnt_storage_2_replica )
113+
114+ -- Space operations constants
115+ local SELECT = ' select'
116+ local INSERT = ' insert'
117+ local UPDATE = ' update'
118+ local UPSERT = ' upsert'
119+ local REPLACE = ' replace'
120+ local DELETE = ' delete'
121+
122+ -- HTTP methods constants
123+ local GET = ' get'
124+ local POST = ' post'
125+
126+ local function http_request (server , method , endpoint , count )
106127 if count <= 0 then
107128 return
108129 end
109130
110131 for _ = 1 , count do
111- group .cluster :server (alias ):http_request (' get' , ' /hello' )
132+ server :http_request (method , endpoint , { raise = false })
133+ end
134+ end
135+
136+ local last_key = 1
137+
138+ local charset = {} -- [0-9a-zA-Z]
139+ for c = 48 , 57 do table.insert (charset , string.char (c )) end
140+ for c = 65 , 90 do table.insert (charset , string.char (c )) end
141+ for c = 97 , 122 do table.insert (charset , string.char (c )) end
142+
143+ local function random_string (length )
144+ if not length or length <= 0 then return ' ' end
145+ math.randomseed (os.clock ()^ 5 )
146+ return random_string (length - 1 ) .. charset [math.random (1 , # charset )]
147+ end
148+
149+ local function space_operations (server , operation , count )
150+ if count <= 0 then
151+ return
152+ end
153+
154+ local space = server .net_box .space .MY_SPACE
155+
156+ if operation == SELECT then
157+ for _ = 1 , count do
158+ space :select ({}, { limit = 1 })
159+ end
160+
161+ elseif operation == INSERT then
162+ for _ = 1 , count do
163+ space :insert { last_key , random_string (5 ) }
164+ last_key = last_key + 1
165+ end
166+
167+ elseif operation == UPDATE then
168+ for _ = 1 , count do
169+ local key = space :select ({}, { limit = 1 })[1 ][1 ]
170+ space :update (key , {{ ' =' , 2 , random_string (5 ) }})
171+ end
172+
173+ elseif operation == UPSERT then
174+ for _ = 1 , count do
175+ local tuple = space :select ({}, { limit = 1 })[1 ]
176+ space :upsert (tuple , {{ ' =' , 2 , random_string (5 ) }})
177+ end
178+
179+ elseif operation == REPLACE then
180+ for _ = 1 , count do
181+ local key = space :select ({}, { limit = 1 })[1 ][1 ]
182+ space :replace { key , random_string (5 ) }
183+ end
184+
185+ elseif operation == DELETE then
186+ for _ = 1 , count do
187+ local key = space :select ({}, { limit = 1 })[1 ][1 ]
188+ space :delete { key }
189+ end
112190 end
113191end
114192
115193group .test_cluster = function ()
116194 test .helpers .retrying ({ timeout = math.huge },
117195 function ()
118- fiber .sleep (10 )
196+ -- Generate some HTTP traffic
197+ http_request (tnt_router , GET , ' /hello' , math.random (5 , 10 ))
198+ http_request (tnt_router , GET , ' /hell0' , math.random (1 , 2 ))
199+ http_request (tnt_router , POST , ' /goodbye' , math.random (0 , 1 ))
200+ http_request (tnt_storage_1_master , GET , ' /hello' , math.random (2 , 5 ))
201+ http_request (tnt_storage_1_master , GET , ' /hell0' , math.random (0 , 1 ))
202+ http_request (tnt_storage_1_master , POST , ' /goodbye' , math.random (0 , 1 ))
203+ http_request (tnt_storage_1_replica , GET , ' /hello' , math.random (1 , 3 ))
204+ http_request (tnt_storage_1_replica , GET , ' /hell0' , math.random (0 , 1 ))
205+ http_request (tnt_storage_1_replica , POST , ' /goodbye' , math.random (0 , 1 ))
206+ http_request (tnt_storage_2_master , GET , ' /hello' , math.random (2 , 5 ))
207+ http_request (tnt_storage_2_master , GET , ' /hell0' , math.random (0 , 1 ))
208+ http_request (tnt_storage_2_master , POST , ' /goodbye' , math.random (0 , 1 ))
209+ http_request (tnt_storage_2_replica , GET , ' /hello' , math.random (1 , 3 ))
210+ http_request (tnt_storage_2_replica , GET , ' /hell0' , math.random (0 , 1 ))
211+ http_request (tnt_storage_2_replica , POST , ' /goodbye' , math.random (0 , 1 ))
119212
120- -- Generate some traffic
121- for _ , alias in ipairs (instances ) do
122- http_request (alias , math.random (0 , 5 ))
123- end
213+ -- Generate some space traffic
214+ space_operations (tnt_router , INSERT , math.random (1 , 3 ))
215+ space_operations (tnt_router , UPDATE , math.random (1 , 3 ))
216+ space_operations (tnt_storage_1_master , INSERT , math.random (5 , 10 ))
217+ space_operations (tnt_storage_1_master , SELECT , math.random (10 , 20 ))
218+ space_operations (tnt_storage_1_master , UPDATE , math.random (5 , 10 ))
219+ space_operations (tnt_storage_1_master , UPSERT , math.random (5 , 10 ))
220+ space_operations (tnt_storage_1_master , REPLACE , math.random (5 , 10 ))
221+ space_operations (tnt_storage_1_master , DELETE , math.random (1 , 2 ))
222+ space_operations (tnt_storage_1_replica , SELECT , math.random (3 , 5 ))
223+ space_operations (tnt_storage_2_master , INSERT , math.random (5 , 10 ))
224+ space_operations (tnt_storage_2_master , SELECT , math.random (10 , 20 ))
225+ space_operations (tnt_storage_2_master , UPDATE , math.random (5 , 10 ))
226+ space_operations (tnt_storage_2_master , UPSERT , math.random (5 , 10 ))
227+ space_operations (tnt_storage_2_master , REPLACE , math.random (5 , 10 ))
228+ space_operations (tnt_storage_2_master , DELETE , math.random (1 , 2 ))
229+ space_operations (tnt_storage_2_replica , SELECT , math.random (3 , 5 ))
124230
125231 -- Fail this function so cluster don't stop
126232 error (' running cluster' )
0 commit comments