Skip to content

Commit 4428f1d

Browse files
Add config.storage_client documentation
This patch adds documentation for the `config.storage_client` module. The module allows one to connect to a remote config.storage cluster and use it as a key-value storage. Examples for working both with Tarantool config.storage server API and Tarantool config.storage client API are provided side by side since they share quite the same API.
1 parent 959c606 commit 4428f1d

File tree

4 files changed

+486
-112
lines changed

4 files changed

+486
-112
lines changed
Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,85 @@
11
net_box = require('net.box')
2-
local conn = net_box.connect('127.0.0.1:4401')
32
local log = require('log')
4-
conn:watch('config.storage:/myapp/config/all', function(key, value)
5-
log.info("Configuration stored by the '/myapp/config/all' key is changed")
6-
end)
3+
config = require('config')
4+
5+
storage_client = config.storage_client.connect({
6+
{
7+
uri = '127.0.0.1:4401',
8+
login = 'sampleuser',
9+
password = '123456',
10+
},
11+
{
12+
uri = '127.0.0.1:4402',
13+
login = 'sampleuser',
14+
password = '123456',
15+
},
16+
{
17+
uri = '127.0.0.1:4403',
18+
login = 'sampleuser',
19+
password = '123456',
20+
},
21+
})
22+
23+
function register_watchers()
24+
storage_client:watch('/myapp/config/all', function(_, revision)
25+
log.info("Configuration stored by the '/myapp/config/all' key is " ..
26+
"changed. New revision number is %d.", revision)
27+
end)
28+
end
29+
30+
register_watchers()
31+
32+
function connect_to_configured_storage()
33+
-- `config.storage.endpoints` configuration section can be
34+
-- passed directly as `connect()` options to connect to the
35+
-- configured config.storage cluster.
36+
local endpoints = config:get('config.storage.endpoints')
37+
local storage_client = config.storage_client.connect(endpoints)
38+
39+
return storage_client
40+
end
41+
42+
function put_config()
43+
local fio = require('fio')
44+
local cluster_config_handle = fio.open('../../source.yaml')
45+
local cluster_config = cluster_config_handle:read()
46+
local response = storage_client:put('/myapp/config/all', cluster_config)
47+
cluster_config_handle:close()
48+
49+
return response
50+
end
51+
52+
function get_config_by_path()
53+
local response = storage_client:get('/myapp/config/all')
54+
55+
return response
56+
end
57+
58+
function get_config_by_prefix()
59+
local response = storage_client:get('/myapp/')
60+
61+
return response
62+
end
63+
64+
function make_txn_request()
65+
-- Execute an atomic request on the config.storage cluster.
66+
local response = storage_client:txn({
67+
predicates = { { 'value', '==', 'v0', '/myapp/config/all' } },
68+
on_success = { { 'put', '/myapp/config/all', 'v1' } },
69+
on_failure = { { 'get', '/myapp/config/all' } }
70+
})
71+
72+
return response
73+
end
74+
75+
function delete_config()
76+
local response = storage_client:delete('/myapp/config/all')
77+
78+
return response
79+
end
80+
81+
function delete_all_configs()
82+
local response = storage_client:delete('/')
83+
84+
return response
85+
end
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
function put_config()
2+
local fio = require('fio')
3+
local cluster_config_handle = fio.open('../../source.yaml')
4+
local cluster_config = cluster_config_handle:read()
5+
6+
-- Connect to a config.storage and put the config.
7+
local storage_client = config.storage_client.connect({
8+
'127.0.0.1:4401',
9+
'127.0.0.1:4402',
10+
'127.0.0.1:4403',
11+
})
12+
local response = storage_client:put('/myapp/config/all', cluster_config)
13+
14+
-- Put a config to a configured config.storage cluster.
15+
response = config.storage_client:put('/myapp/config/all', cluster_config)
16+
17+
cluster_config_handle:close()
18+
19+
return response
20+
end
21+
22+
function get_config_by_path()
23+
-- Connect to a config.storage and get /myapp/config/all.
24+
local storage_client = config.storage_client.connect({
25+
'127.0.0.1:4401',
26+
'127.0.0.1:4402',
27+
'127.0.0.1:4403',
28+
})
29+
local response = storage_client:get('/myapp/config/all')
30+
31+
-- Get /myapp/config/all from a configured config.storage cluster.
32+
response = config.storage_client:get('/myapp/config/all')
33+
return response
34+
end
35+
36+
function get_config_by_prefix()
37+
-- Connect to a config.storage and get values with a /myapp/ prefix.
38+
local storage_client = config.storage_client.connect({
39+
'127.0.0.1:4401',
40+
'127.0.0.1:4402',
41+
'127.0.0.1:4403',
42+
})
43+
local response = storage_client:get('/myapp/')
44+
45+
-- Get values prefixed /myapp/ from a configured config.storage cluster.
46+
response = config.storage_client:get('/myapp/')
47+
return response
48+
end
49+
50+
function make_txn_request()
51+
-- Connect to a config.storage and execute an atomic request.
52+
local storage_client = config.storage_client.connect({
53+
'127.0.0.1:4401',
54+
'127.0.0.1:4402',
55+
'127.0.0.1:4403',
56+
})
57+
local response = storage_client:txn({
58+
predicates = { { 'value', '==', 'v0', '/myapp/config/all' } },
59+
on_success = { { 'put', '/myapp/config/all', 'v1' } },
60+
on_failure = { { 'get', '/myapp/config/all' } }
61+
})
62+
63+
-- Execute an atomic request on a configured config.storage cluster.
64+
response = config.storage_client:txn({
65+
predicates = { { 'value', '==', 'v0', '/myapp/config/all' } },
66+
on_success = { { 'put', '/myapp/config/all', 'v1' } },
67+
on_failure = { { 'get', '/myapp/config/all' } }
68+
})
69+
70+
71+
-- end-marker --
72+
73+
return response
74+
end
75+
76+
function delete_config()
77+
-- Connect to a config.storage and delete a key.
78+
local storage_client = config.storage_client.connect({
79+
'127.0.0.1:4401',
80+
'127.0.0.1:4402',
81+
'127.0.0.1:4403',
82+
})
83+
local response = storage_client:delete('/myapp/config/all')
84+
85+
-- Delete a key on a configured config.storage cluster.
86+
response = config.storage_client:delete('/myapp/config/all')
87+
88+
return response
89+
end
90+
91+
function delete_all_configs()
92+
-- Connect to a config.storage and delete all keys.
93+
local storage_client = config.storage_client.connect({
94+
'127.0.0.1:4401',
95+
'127.0.0.1:4402',
96+
'127.0.0.1:4403',
97+
})
98+
local response = storage_client:delete('/')
99+
100+
-- Delete all keys from a configured config.storage cluster.
101+
response = config.storage_client:delete('/')
102+
return response
103+
end

doc/platform/configuration/configuration_etcd.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ In the example below, the following options are specified:
260260
- ``timeout`` specifies the interval (in seconds) to perform the status check of a configuration storage.
261261
- ``reconnect_after`` specifies how much time to wait (in seconds) before reconnecting to a configuration storage.
262262

263+
You might use :ref:`config.storage_client <config_storage_client_api_reference>` API for connecting and controlling a remote config.storage cluster.
264+
263265
You can find the full example here: `config_storage <https://github.com/tarantool/doc/tree/latest/doc/code_snippets/snippets/centralized_config/instances.enabled/config_storage>`_.
264266

265267

0 commit comments

Comments
 (0)