Skip to content

Commit f25d4d8

Browse files
Start a cluster of Tarantools in docker example
Closes #20 Start a cluster of Tarantools in docker example instead of a single instance with luatest. Generate some traffic for HTTP panels with luatest.
1 parent 37d5eb3 commit f25d4d8

File tree

9 files changed

+254
-50
lines changed

9 files changed

+254
-50
lines changed

docker-compose.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ services:
44
build:
55
context: ./example/project
66
dockerfile: Dockerfile
7-
environment:
8-
- TARANTOOL_ALIAS=instance
9-
entrypoint: ["/usr/local/bin/tarantool"]
10-
command: ["init.lua"]
7+
entrypoint: ["/app/.rocks/bin/luatest"]
8+
command: ["./cluster"]
119
networks:
1210
tarantool_dashboard_dev:
1311
ports:
12+
- 13301:13301
13+
- 13302:13302
14+
- 13303:13303
15+
- 13304:13304
16+
- 13305:13305
1417
- 8081:8081
18+
- 8082:8082
19+
- 8083:8083
20+
- 8084:8084
21+
- 8085:8085
1522

1623
telegraf:
1724
image: telegraf:1.13-alpine

example/project/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ RUN yum install -y git \
77
make \
88
gcc
99
COPY . .
10-
RUN tarantoolctl rocks make
1110
RUN mkdir -p tmp
1211

13-
CMD ["tarantool", "init.lua"]
12+
RUN tarantoolctl rocks make
13+
RUN tarantoolctl rocks install luatest 0.5.0

example/project/app/roles/custom.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ end
3838

3939
return {
4040
role_name = 'app.roles.custom',
41+
dependencies = { 'cartridge.roles.metrics' },
4142
init = init,
4243
stop = stop,
4344
validate_config = validate_config,

example/project/cluster/helper.lua

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
local fio = require('fio')
2+
local lt = require('luatest')
3+
local json = require('json')
4+
local Server = require('cartridge.test-helpers.server')
5+
6+
local helper = {}
7+
8+
helper.root = fio.dirname(fio.abspath(package.search('init')))
9+
helper.datadir = fio.pathjoin(helper.root, 'dev')
10+
helper.server_command = fio.pathjoin(helper.root, 'cluster/init.lua')
11+
12+
lt.before_suite(function()
13+
fio.rmtree(helper.datadir)
14+
fio.mktree(helper.datadir)
15+
end)
16+
17+
function Server:build_env()
18+
return {
19+
TARANTOOL_ALIAS = self.alias,
20+
TARANTOOL_WORKDIR = self.workdir,
21+
TARANTOOL_HTTP_PORT = self.http_port,
22+
TARANTOOL_ADVERTISE_URI = self.advertise_uri,
23+
TARANTOOL_CLUSTER_COOKIE = self.cluster_cookie,
24+
TARANTOOL_LOG_LEVEL = 6,
25+
TZ = 'Europe/Moscow'
26+
}
27+
end
28+
29+
Server.constructor_checks.api_host = '?string'
30+
Server.constructor_checks.api_port = '?string'
31+
function Server:api_request(method, path, options)
32+
method = method and string.upper(method)
33+
if not self.http_client then
34+
error('http_port not configured')
35+
end
36+
options = options or {}
37+
local body = options.body or (options.json and json.encode(options.json))
38+
local http_options = options.http or {}
39+
local url = self.api_host .. ':' .. self.api_port .. path
40+
local response = self.http_client:request(method, url, body, http_options)
41+
local ok, json_body = pcall(json.decode, response.body)
42+
if ok then
43+
response.json = json_body
44+
end
45+
if not options.raw and response.status ~= 200 then
46+
error({type = 'HTTPRequest', response = response})
47+
end
48+
return response
49+
end
50+
51+
return helper

example/project/cluster/init.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env tarantool
2+
3+
local fio = require('fio')
4+
if os.getenv('ENABLE_TEST_COVERAGE') == 'true' then
5+
local cfg_luacov = dofile(fio.pathjoin('.luacov'))
6+
cfg_luacov.statsfile = fio.pathjoin('luacov.stats.out')
7+
8+
local coverage = require('luacov.runner')
9+
coverage.init(cfg_luacov)
10+
rawset(_G, 'coverage', coverage)
11+
end
12+
13+
local function get_base_dir()
14+
return fio.abspath(fio.dirname(arg[0]) .. '/app/')
15+
end
16+
17+
local function extend_path(path)
18+
package.path = package.path .. ';' .. path
19+
end
20+
21+
local function extend_cpath(path)
22+
package.cpath = package.cpath .. ';' .. path
23+
end
24+
25+
local function set_base_load_paths(base_dir)
26+
extend_path(base_dir .. '/?.lua')
27+
extend_path(base_dir .. '/?/init.lua')
28+
extend_cpath(base_dir .. '/?.dylib')
29+
extend_cpath(base_dir .. '/?.so')
30+
end
31+
32+
local function set_rocks_load_paths(base_dir)
33+
extend_path(base_dir..'/.rocks/share/tarantool/?.lua')
34+
extend_path(base_dir..'/.rocks/share/tarantool/?/init.lua')
35+
extend_cpath(base_dir..'/.rocks/lib/tarantool/?.dylib')
36+
extend_cpath(base_dir..'/.rocks/lib/tarantool/?.so')
37+
end
38+
39+
local function set_load_paths(base_dir)
40+
set_base_load_paths(base_dir)
41+
set_rocks_load_paths(base_dir)
42+
end
43+
44+
set_load_paths(get_base_dir())
45+
46+
require('init')
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
local test = require('luatest')
2+
local group = test.group('cluster')
3+
local fiber = require('fiber')
4+
5+
local helper = require('test.helper')
6+
local cluster_helpers = require('cartridge.test-helpers')
7+
8+
local replicasets = {
9+
{
10+
uuid = cluster_helpers.uuid('a'),
11+
roles = { 'vshard-router', 'app.roles.custom' },
12+
servers = {
13+
{
14+
instance_uuid = cluster_helpers.uuid('a', 1),
15+
alias = 'tnt-router',
16+
env = {
17+
['TARANTOOL_APP_NAME'] = 'example-app',
18+
['TARANTOOL_LOG_LEVEL'] = 6,
19+
}
20+
}
21+
},
22+
},
23+
{
24+
uuid = cluster_helpers.uuid('b'),
25+
roles = { 'vshard-storage', 'app.roles.custom' },
26+
servers = {
27+
{
28+
instance_uuid = cluster_helpers.uuid('b', 1),
29+
alias = 'tnt-storage-1-master',
30+
env = {
31+
['TARANTOOL_APP_NAME'] = 'example-app',
32+
['TARANTOOL_LOG_LEVEL'] = 6,
33+
}
34+
},
35+
{
36+
instance_uuid = cluster_helpers.uuid('b', 2),
37+
alias = 'tnt-storage-1-replica',
38+
env = {
39+
['TARANTOOL_APP_NAME'] = 'example-app',
40+
['TARANTOOL_LOG_LEVEL'] = 6,
41+
}
42+
},
43+
}
44+
},
45+
{
46+
uuid = cluster_helpers.uuid('c'),
47+
roles = { 'vshard-storage', 'app.roles.custom' },
48+
servers = {
49+
{
50+
instance_uuid = cluster_helpers.uuid('c', 1),
51+
alias = 'tnt-storage-2-master',
52+
env = {
53+
['TARANTOOL_APP_NAME'] = 'example-app',
54+
['TARANTOOL_LOG_LEVEL'] = 6,
55+
}
56+
},
57+
{
58+
instance_uuid = cluster_helpers.uuid('c', 2),
59+
alias = 'tnt-storage-2-replica',
60+
env = {
61+
['TARANTOOL_APP_NAME'] = 'example-app',
62+
['TARANTOOL_LOG_LEVEL'] = 6,
63+
}
64+
}
65+
},
66+
}
67+
}
68+
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+
76+
test.before_suite(function()
77+
group.cluster = cluster_helpers.Cluster:new({
78+
datadir = helper.datadir,
79+
server_command = helper.server_command,
80+
use_vshard = true,
81+
replicasets = replicasets
82+
})
83+
84+
group.cluster:start()
85+
group.cluster:upload_config({
86+
metrics = {
87+
export = {
88+
{
89+
path = '/metrics',
90+
format = 'json'
91+
},
92+
{
93+
path = '/metrics/prometheus',
94+
format = 'prometheus'
95+
}
96+
}
97+
}
98+
})
99+
end)
100+
101+
test.after_suite(function()
102+
group.cluster:stop()
103+
end)
104+
105+
local function http_request(alias, count)
106+
if count <= 0 then
107+
return
108+
end
109+
110+
for _ = 1, count do
111+
group.cluster:server(alias):http_request('get', '/hello')
112+
end
113+
end
114+
115+
group.test_cluster = function()
116+
test.helpers.retrying({ timeout = math.huge },
117+
function()
118+
fiber.sleep(10)
119+
120+
-- Generate some traffic
121+
for _, alias in ipairs(instances) do
122+
http_request(alias, math.random(0, 5))
123+
end
124+
125+
-- Fail this function so cluster don't stop
126+
error('running cluster')
127+
end
128+
)
129+
end

example/project/init.lua

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ else
2727
end
2828

2929
local cartridge = require('cartridge')
30-
local membership = require('membership')
31-
local log = require("log")
30+
3231
local ok, err = cartridge.cfg({
3332
workdir = 'tmp/db',
3433
roles = {
@@ -41,43 +40,3 @@ local ok, err = cartridge.cfg({
4140
})
4241

4342
assert(ok, tostring(err))
44-
45-
local all = {
46-
'vshard-storage',
47-
'vshard-router',
48-
'metrics',
49-
'app.roles.custom'
50-
}
51-
52-
local _, err = cartridge.admin_join_server({
53-
uri = membership.myself().uri,
54-
roles = all,
55-
})
56-
57-
if err ~= nil then
58-
log.warn('%s', tostring(err))
59-
else
60-
local _, err = cartridge.admin_bootstrap_vshard()
61-
if err ~= nil then
62-
log.error('%s', tostring(err))
63-
os.exit(1)
64-
end
65-
66-
-- This code is only for example purposes
67-
-- DO NOT USE IT IN PROD CLUSTERS WITH MORE THAN 1 INSTANCE!
68-
cartridge.config_patch_clusterwide({
69-
metrics = {
70-
export = {
71-
{
72-
path = '/metrics',
73-
format = 'json'
74-
},
75-
{
76-
path = '/metrics/prometheus',
77-
format = 'prometheus'
78-
}
79-
}
80-
}
81-
})
82-
end
83-

example/prometheus/prometheus.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ scrape_configs:
3030

3131
- job_name: "example_project"
3232
static_configs:
33-
- targets: ["example_project:8081"]
33+
- targets:
34+
- "example_project:8081"
35+
- "example_project:8082"
36+
- "example_project:8083"
37+
- "example_project:8084"
38+
- "example_project:8085"
3439
metrics_path: "/metrics/prometheus"

example/telegraf/telegraf.conf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
[[inputs.http]]
2-
urls = ["http://example_project:8081/metrics"]
2+
urls = [
3+
"http://example_project:8081/metrics",
4+
"http://example_project:8082/metrics",
5+
"http://example_project:8083/metrics",
6+
"http://example_project:8084/metrics",
7+
"http://example_project:8085/metrics"
8+
]
39
timeout = "30s"
410
tag_keys = ["metric_name", "label_pairs_alias", "label_pairs_path", "label_pairs_method", "label_pairs_status", "label_pairs_operation"]
511
insecure_skip_verify = true

0 commit comments

Comments
 (0)