Skip to content

Commit fb2dd55

Browse files
Docs for roles/role_cfg (#4221)
1 parent 8521c33 commit fb2dd55

File tree

24 files changed

+995
-204
lines changed

24 files changed

+995
-204
lines changed

doc/book/admin/instance_config.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The main steps of creating and preparing the application for deployment are:
2020
In this section, a `sharded_cluster <https://github.com/tarantool/doc/tree/latest/doc/code_snippets/snippets/sharding/instances.enabled/sharded_cluster>`_ application is used as an example.
2121
This cluster includes 5 instances: one router and 4 storages, which constitute two replica sets.
2222

23-
.. image:: admin_instances_dev.png
23+
.. image:: /book/admin/admin_instances_dev.png
2424
:align: left
2525
:width: 700
2626
:alt: Cluster topology
@@ -171,7 +171,7 @@ define instances to run on each machine by changing the content of the ``instanc
171171

172172
- On the developer's machine, this file might include all the instances defined in the cluster configuration.
173173

174-
.. image:: admin_instances_dev.png
174+
.. image:: /book/admin/admin_instances_dev.png
175175
:align: left
176176
:width: 700
177177
:alt: Cluster topology
@@ -184,7 +184,7 @@ define instances to run on each machine by changing the content of the ``instanc
184184

185185
- In the production environment, this file includes instances to run on the specific machine.
186186

187-
.. image:: admin_instances_prod.png
187+
.. image:: /book/admin/admin_instances_prod.png
188188
:align: left
189189
:width: 700
190190
:alt: Cluster topology

doc/book/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ User's Guide
1717
box/index
1818
admin/index
1919
monitoring/index
20+
cluster_app
2021
connectors
2122
../reference/reference_sql/index
2223
faq
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
groups:
2+
group001:
3+
replicasets:
4+
replicaset001:
5+
instances:
6+
instance001:
7+
roles: [ greeter ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- greeter.lua --
2+
return {
3+
validate = function() end,
4+
apply = function() require('log').info("Hi from the 'greeter' role!") end,
5+
stop = function() end,
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
instance001:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- byeer.lua --
2+
local log = require('log').new("byeer")
3+
4+
return {
5+
dependencies = { 'greeter' },
6+
validate = function() end,
7+
apply = function() log.info("Bye from the 'byeer' role!") end,
8+
stop = function() end,
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
groups:
2+
group001:
3+
replicasets:
4+
replicaset001:
5+
instances:
6+
instance001:
7+
roles: [ greeter ]
8+
roles_cfg:
9+
greeter:
10+
greeting: 'Hi'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- greeter.lua --
2+
local log = require('log').new("greeter")
3+
4+
local function validate(cfg)
5+
if cfg.greeting then
6+
assert(type(cfg.greeting) == "string", "'greeting' should be a string")
7+
assert(cfg.greeting == "Hi" or cfg.greeting == "Hello", "'greeting' should be 'Hi' or 'Hello'")
8+
end
9+
end
10+
11+
local function apply(cfg)
12+
log.info("%s from the 'greeter' role!", cfg.greeting)
13+
end
14+
15+
local function stop()
16+
log.info("The 'greeter' role is stopped")
17+
end
18+
19+
return {
20+
validate = validate,
21+
apply = apply,
22+
stop = stop,
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
instance001:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# HTTP API
2+
3+
A sample application showing how to implement a custom `http-api` role.
4+
5+
## Running
6+
7+
To start an application, execute the following command in the [config](../../../config) directory:
8+
9+
```console
10+
$ tt start application_role_http_api
11+
```
12+
13+
## Making test requests
14+
15+
To test the API, make the following requests:
16+
17+
```console
18+
$ curl -X GET --location "http://0.0.0.0:8080/band?limit=7" \
19+
-H "Accept: application/json"
20+
$ curl -X GET --location "http://0.0.0.0:8080/band/5" \
21+
-H "Accept: application/json"
22+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
groups:
2+
group001:
3+
replicasets:
4+
replicaset001:
5+
instances:
6+
instance001:
7+
roles: [ http-api ]
8+
roles_cfg:
9+
http-api:
10+
host: '127.0.0.1'
11+
port: 8080
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local data = {}
2+
3+
data.add_sample_data = function()
4+
box.watch('box.status', function()
5+
if box.info.ro then
6+
return
7+
end
8+
9+
box.schema.space.create('bands', { if_not_exists = true })
10+
box.space.bands:format({
11+
{ name = 'id', type = 'unsigned' },
12+
{ name = 'band_name', type = 'string' },
13+
{ name = 'year', type = 'unsigned' }
14+
})
15+
box.space.bands:create_index('primary', { parts = { 'id' } })
16+
box.space.bands:create_index('band', { parts = { 'band_name' } })
17+
box.space.bands:create_index('year_band', { parts = { { 'year' }, { 'band_name' } } })
18+
19+
box.space.bands:insert { 1, 'Roxette', 1986 }
20+
box.space.bands:insert { 2, 'Scorpions', 1965 }
21+
box.space.bands:insert { 3, 'Ace of Base', 1987 }
22+
box.space.bands:insert { 4, 'The Beatles', 1960 }
23+
box.space.bands:insert { 5, 'Pink Floyd', 1965 }
24+
box.space.bands:insert { 6, 'The Rolling Stones', 1962 }
25+
box.space.bands:insert { 7, 'The Doors', 1965 }
26+
box.space.bands:insert { 8, 'Nirvana', 1987 }
27+
box.space.bands:insert { 9, 'Led Zeppelin', 1968 }
28+
box.space.bands:insert { 10, 'Queen', 1970 }
29+
end)
30+
end
31+
32+
return data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- http-api.lua --
2+
local httpd
3+
local json = require('json')
4+
5+
local function validate(cfg)
6+
if cfg.host then
7+
assert(type(cfg.host) == "string", "'host' should be a string containing a valid IP address")
8+
end
9+
if cfg.port then
10+
assert(type(cfg.port) == "number", "'port' should be a number")
11+
assert(cfg.port >= 1 and cfg.port <= 65535, "'port' should be between 1 and 65535")
12+
end
13+
end
14+
15+
local function apply(cfg)
16+
if httpd then
17+
httpd:stop()
18+
end
19+
httpd = require('http.server').new(cfg.host, cfg.port)
20+
local response_headers = { ['content-type'] = 'application/json' }
21+
httpd:route({ path = '/band/:id', method = 'GET' }, function(req)
22+
local id = req:stash('id')
23+
local band_tuple = box.space.bands:get(tonumber(id))
24+
if not band_tuple then
25+
return { status = 404, body = 'Band not found' }
26+
else
27+
local band = { id = band_tuple['id'],
28+
band_name = band_tuple['band_name'],
29+
year = band_tuple['year'] }
30+
return { status = 200, headers = response_headers, body = json.encode(band) }
31+
end
32+
end)
33+
httpd:route({ path = '/band', method = 'GET' }, function(req)
34+
local limit = req:query_param('limit')
35+
if not limit then
36+
limit = 5
37+
end
38+
local band_tuples = box.space.bands:select({}, { limit = tonumber(limit) })
39+
local bands = {}
40+
for _, tuple in pairs(band_tuples) do
41+
local band = { id = tuple['id'],
42+
band_name = tuple['band_name'],
43+
year = tuple['year'] }
44+
table.insert(bands, band)
45+
end
46+
return { status = 200, headers = response_headers, body = json.encode(bands) }
47+
end)
48+
httpd:start()
49+
end
50+
51+
local function stop()
52+
httpd:stop()
53+
end
54+
55+
local function init()
56+
require('data'):add_sample_data()
57+
end
58+
59+
init()
60+
61+
return {
62+
validate = validate,
63+
apply = apply,
64+
stop = stop,
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
instance001:

doc/code_snippets/snippets/sharding/instances.enabled/sharded_cluster/sharded_cluster-scm-1.rockspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source = {
55
}
66

77
dependencies = {
8-
'vshard == 0.1.26'
8+
'vshard == 0.1.27'
99
}
1010
build = {
1111
type = 'none';

doc/code_snippets/snippets/sharding/instances.enabled/sharded_cluster_crud/config.yaml

+18-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ credentials:
22
users:
33
replicator:
44
password: 'topsecret'
5-
roles: [replication]
5+
roles: [ replication ]
66
storage:
77
password: 'secret'
8-
roles: [sharding]
8+
roles: [ sharding ]
99

1010
iproto:
1111
advertise:
@@ -19,9 +19,11 @@ sharding:
1919

2020
groups:
2121
storages:
22-
roles: [storage]
22+
roles: [ roles.crud-storage ]
23+
app:
24+
module: storage
2325
sharding:
24-
roles: [storage]
26+
roles: [ storage ]
2527
replication:
2628
failover: manual
2729
replicasets:
@@ -48,9 +50,19 @@ groups:
4850
listen:
4951
- uri: '127.0.0.1:3305'
5052
routers:
51-
roles: [router]
53+
roles: [ roles.crud-router ]
54+
roles_cfg:
55+
roles.crud-router:
56+
stats: true
57+
stats_driver: metrics
58+
stats_quantiles: false
59+
stats_quantile_tolerated_error: 0.001
60+
stats_quantile_age_buckets_count: 5
61+
stats_quantile_max_age_time: 180
62+
app:
63+
module: router
5264
sharding:
53-
roles: [router]
65+
roles: [ router ]
5466
replicasets:
5567
router-a:
5668
instances:
Original file line numberDiff line numberDiff line change
@@ -1,17 +1 @@
11
local vshard = require('vshard')
2-
local crud = require('crud')
3-
4-
local M = {}
5-
6-
function M.validate()
7-
end
8-
9-
function M.apply()
10-
crud.init_router()
11-
end
12-
13-
function M.stop()
14-
crud.stop_router()
15-
end
16-
17-
return M

doc/code_snippets/snippets/sharding/instances.enabled/sharded_cluster_crud/sharded_cluster_crud-scm-1.rockspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ source = {
55
}
66

77
dependencies = {
8-
'vshard == 0.1.26',
9-
'crud == 1.4.3'
8+
'vshard == 0.1.27',
9+
'crud == 1.5.2'
1010
}
1111
build = {
1212
type = 'none';
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
1-
local crud = require('crud')
2-
3-
local M = {}
4-
5-
function M.validate()
6-
end
7-
8-
function M.apply()
9-
crud.init_storage()
10-
if box.info.ro ~= true then
11-
box.schema.create_space('bands', {
12-
format = {
13-
{ name = 'id', type = 'unsigned' },
14-
{ name = 'bucket_id', type = 'unsigned' },
15-
{ name = 'band_name', type = 'string' },
16-
{ name = 'year', type = 'unsigned' }
17-
},
18-
if_not_exists = true
19-
})
20-
box.space.bands:create_index('id', { parts = { 'id' }, if_not_exists = true })
21-
box.space.bands:create_index('bucket_id', { parts = { 'bucket_id' }, unique = false, if_not_exists = true })
1+
box.watch('box.status', function()
2+
if box.info.ro then
3+
return
224
end
23-
end
24-
25-
function M.stop()
26-
crud.stop_storage()
27-
end
285

29-
return M
6+
box.schema.create_space('bands', {
7+
format = {
8+
{ name = 'id', type = 'unsigned' },
9+
{ name = 'bucket_id', type = 'unsigned' },
10+
{ name = 'band_name', type = 'string' },
11+
{ name = 'year', type = 'unsigned' }
12+
},
13+
if_not_exists = true
14+
})
15+
box.space.bands:create_index('id', { parts = { 'id' }, if_not_exists = true })
16+
box.space.bands:create_index('bucket_id', { parts = { 'bucket_id' }, unique = false, if_not_exists = true })
17+
end)

doc/code_snippets/snippets/sharding/templates/basic/{{.name}}-scm-1.rockspec.tt.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source = {
55
}
66

77
dependencies = {
8-
'vshard == 0.1.25'
8+
'vshard == 0.1.27'
99
}
1010
build = {
1111
type = 'none';

0 commit comments

Comments
 (0)