Skip to content

Commit d7e3c3e

Browse files
Merge pull request #22 from tarantool/20-docker-cluster
Start a cluster of Tarantools in docker example
2 parents 37d5eb3 + 8f72fc9 commit d7e3c3e

File tree

11 files changed

+417
-56
lines changed

11 files changed

+417
-56
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Added
99
- Publish dashboard on Grafana official and community built dashboards
10+
- Generate HTTP and space operations traffic for example cluster with luatest
1011

1112
### Changed
1213
- Update metrics version to 0.3.0 in experimental cluster
1314
- Rework example Tarantool instance on "cartridge.roles.metrics"
15+
- Rework example Tarantool docker to start multiple instances with luatest
1416
- Documentation improvements and fixes
1517

1618
## [0.1.0] - 2020-06-30

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/.luacheckrc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
include_files = {'**/*.lua', '*.luacheckrc', '*.rockspec'}
2-
exclude_files = {'.rocks/', 'tmp/'}
3-
max_line_length = 120
1+
std = {
2+
read_globals = {'require', 'debug', 'pcall', 'xpcall', 'tostring',
3+
'tonumber', 'type', 'assert', 'ipairs', 'math', 'error', 'string',
4+
'table', 'pairs', 'os', 'io', 'select', 'unpack', 'dofile', 'next',
5+
'loadstring', 'setfenv',
6+
'rawget', 'rawset', '_G',
7+
'getmetatable', 'setmetatable',
8+
'print', 'tonumber64', 'arg'
9+
10+
},
11+
globals = {'box', 'vshard', 'package',
12+
'applier'
13+
}
14+
}
15+
redefined = false

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: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
local cartridge = require('cartridge')
2+
local config = require('cartridge.argparse')
23

34
local function init(opts) -- luacheck: no unused args
4-
-- if opts.is_master then
5-
-- end
5+
local local_cfg = config.get_opts({
6+
user = 'string',
7+
password = 'string'
8+
})
9+
610
local metrics = cartridge.service_get('metrics')
711
local http_middleware = metrics.http_middleware
812

@@ -13,11 +17,44 @@ local function init(opts) -- luacheck: no unused args
1317
{ method = 'GET', path = '/hello' },
1418
http_middleware.v1(
1519
function()
16-
return { body = 'Hello world!' }
20+
return { status = 200, body = 'Hello world!' }
21+
end,
22+
http_collector
23+
)
24+
)
25+
httpd:route(
26+
{ method = 'GET', path = '/hell0' },
27+
http_middleware.v1(
28+
function()
29+
return { status = 400, body = 'Hell0 world!' }
1730
end,
1831
http_collector
1932
)
2033
)
34+
httpd:route(
35+
{ method = 'POST', path = '/goodbye' },
36+
http_middleware.v1(
37+
function()
38+
return { status = 500, body = 'Goodbye cruel world!' }
39+
end,
40+
http_collector
41+
)
42+
)
43+
44+
if opts.is_master then
45+
local sp = box.schema.space.create('MY_SPACE', { if_not_exists = true })
46+
sp:format({
47+
{ name = 'key', type = 'number', is_nullable = false },
48+
{ name = 'value', type = 'string', is_nullable = false },
49+
})
50+
sp:create_index('pk', { parts = { 'key' }, if_not_exists = true })
51+
52+
if local_cfg.user and local_cfg.password then
53+
-- cluster-wide user privileges
54+
box.schema.user.create(local_cfg.user, { password = local_cfg.password, if_not_exists = true })
55+
box.schema.user.grant(local_cfg.user, 'read,write,execute', 'universe', nil, { if_not_exists = true })
56+
end
57+
end
2158

2259
return true
2360
end
@@ -38,6 +75,7 @@ end
3875

3976
return {
4077
role_name = 'app.roles.custom',
78+
dependencies = { 'cartridge.roles.metrics' },
4179
init = init,
4280
stop = stop,
4381
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')

0 commit comments

Comments
 (0)