Skip to content

Commit 2c1d4ce

Browse files
crud: introduce basic tarantool 3 roles
This patch introduce basic crud-router and crud-storage roles for Tarantool 3. The roles are similar to Cartridge ones. Roles support Tarantool 3.0.2, Tarantool 3.1.0 and newer due to [1, 2]. This commit makes all existing config tests run with roles enabled instead of manual bootstrap. This commit does not yet introduce metrics configuration through roles config. [1] tarantool/tarantool#9643 [2] tarantool/tarantool#9649 Part of #415
1 parent 6cd51a1 commit 2c1d4ce

File tree

15 files changed

+542
-78
lines changed

15 files changed

+542
-78
lines changed

.github/workflows/test_on_push.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ jobs:
4343
external-keydef-version: "0.0.4"
4444
- tarantool-version: "3.0.0"
4545
vshard-version: "0.1.25"
46+
- tarantool-version: "master"
47+
vshard-version: "0.1.26"
4648
fail-fast: false
4749
# Can't install older versions on 22.04,
4850
# see https://github.com/tarantool/setup-tarantool/issues/36

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Added
1111
* Asynchronous bootstrap support for storages (#412).
12+
* Tarantool 3 roles for setting up crud routers and storages (#415).
1213

1314
### Changed
1415
* Explicitly forbid datetime interval conditions (#373).

README.md

Lines changed: 269 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ It also provides the `crud-storage` and `crud-router` roles for
1414
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1515

1616
- [Quickstart](#quickstart)
17+
- [Install](#install)
18+
- [Manual install](#manual-install)
19+
- [Application dependency](#application-dependency)
20+
- [Repository clone](#repository-clone)
21+
- [Usage](#usage)
22+
- [Sandbox](#sandbox)
1723
- [API](#api)
1824
- [Package info](#package-info)
1925
- [Insert](#insert)
@@ -44,8 +50,10 @@ It also provides the `crud-storage` and `crud-router` roles for
4450
- [Read view select conditions](#read-view-select-conditions)
4551
- [Read view pairs](#read-view-pairs)
4652
- [Schema](#schema)
53+
- [Tarantool 3 roles](#tarantool-3-roles)
54+
- [Usage](#usage-1)
4755
- [Cartridge roles](#cartridge-roles)
48-
- [Usage](#usage)
56+
- [Usage](#usage-2)
4957
- [License](#license)
5058

5159
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
@@ -54,38 +62,84 @@ It also provides the `crud-storage` and `crud-router` roles for
5462

5563
First, [install Tarantool](https://www.tarantool.io/en/download).
5664

57-
Now you have the following options for learning the crud API and using it in a
58-
project:
59-
60-
* Play with crud on a test dataset on a single instance:
61-
62-
```shell
63-
$ git clone https://github.com/tarantool/crud.git
64-
$ cd crud
65-
$ tt rocks make
66-
$ ./doc/playground.lua
67-
tarantool> crud.select('customers', {{'<=', 'age', 35}}, {first = 10})
68-
tarantool> crud.select('developers', nil, {first = 6})
69-
```
70-
* Install crud into the current directory:
71-
72-
```shell
73-
$ tt rocks install crud
74-
```
75-
* Add the [crud initialization code](#API) to router and storage instances
76-
initialization code for [VShard](https://github.com/tarantool/vshard).
77-
* Add crud into dependencies of a Cartridge application and add crud roles into
78-
dependencies of your roles (see [Cartridge roles](#cartridge-roles) section).
79-
* Add crud into dependencies of your application (rockspec, RPM spec -- depends
80-
on your choice) and call crud initialization code from storage and router
81-
code (see [API](#api) section).
65+
### Install
66+
67+
#### Manual install
68+
69+
To try `crud` in your application, you may install it manually fron web
70+
with `tt rocks` rock management tool.
71+
72+
```bash
73+
tt rocks install crud
74+
```
75+
76+
#### Application dependency
77+
78+
To use crud in your application, set it as a rockspec dependency.
79+
80+
```lua
81+
package = 'myapp'
82+
83+
version = 'scm-1'
84+
85+
source = {
86+
url = '/dev/null',
87+
}
88+
89+
dependencies = {
90+
'tarantool >= 3.1.0',
91+
'crud == <the-latest-tag>-1',
92+
}
93+
94+
build = {
95+
type = 'none';
96+
}
97+
```
98+
99+
#### Repository clone
100+
101+
You can also clone the repository to explore crud and try it inside a sandbox.
102+
103+
```bash
104+
git clone https://github.com/tarantool/crud.git
105+
cd crud
106+
tt rocks make
107+
```
108+
109+
### Usage
110+
111+
For Tarantool 3.x, enable crud roles on your application instances in a configuration
112+
(see [Tarantool 3 roles](#tarantool-3-roles) section).
113+
Roles support Tarantool 3.0.2, Tarantool 3.1.0 and newer.
114+
Older versions are not supported due to
115+
[tarantool/tarantool#9643](https://github.com/tarantool/tarantool/issues/9643) and
116+
[tarantool/tarantool#9649](https://github.com/tarantool/tarantool/issues/9649)
117+
issues.
118+
119+
For Tarantool 1.10 and 2.x, add crud roles into dependencies of your roles
120+
(see [Cartridge roles](#cartridge-roles) section).
121+
122+
For Tarantool 1.10, 2.x and 3.x you can also manually call
123+
the [crud initialization code](#api) on [VShard](https://github.com/tarantool/vshard)
124+
router and storage instances.
125+
126+
### Sandbox
127+
128+
The repository provide a simple sandbox application with a test dataset on a single instance.
129+
130+
```bash
131+
./doc/playground.lua
132+
tarantool> crud.select('customers', {{'<=', 'age', 35}}, {first = 10})
133+
tarantool> crud.select('developers', nil, {first = 6})
134+
```
82135

83136
## API
84137

85138
The CRUD operations should be called from router.
86139

87140
All VShard storages should call `crud.init_storage()` after
88-
`vshard.storage.cfg()` (or enable the `crud-storage` role for Cartridge)
141+
`vshard.storage.cfg()` (or enable the `roles.crud-storage` role for Tarantool 3
142+
or the `crud-storage` role for Cartridge)
89143
first to initialize storage-side functions that are used to manipulate data
90144
across the cluster. The storage-side functions have the same access
91145
as a user calling `crud.init_storage()`. Therefore, if `crud` do not have
@@ -98,7 +152,8 @@ asynchronous bootstrap is used for Tarantool 3.x and
98152
synchronous bootstrap is used for Tarantool 1.10 and 2.x.
99153

100154
All VShard routers should call `crud.init_router()` after `vshard.router.cfg()`
101-
(or enable the `crud-router` role for Cartridge) to make `crud` functions
155+
(or enable the `roles.crud-storage` role for Tarantool 3
156+
or the `crud-router` role for Cartridge) to make `crud` functions
102157
callable via `net.box`. If a user is allowed to execute `crud` functions on
103158
the router-side then the user does not need additional access on storages.
104159

@@ -1833,6 +1888,192 @@ crud.schema()
18331888
indexes: ...
18341889
```
18351890

1891+
## Tarantool 3 roles
1892+
1893+
`roles.crud-storage` is a Tarantool 3 role that initializes functions that
1894+
are used on the storage side to perform CRUD operations. Role must be enabled
1895+
on sharding storages.
1896+
1897+
`cartridge.roles.crud-router` is a role that exposes public `crud` functions
1898+
to the global scope so that you can call them via `net.box` or with connectors.
1899+
Role must be enabled on sharding routers.
1900+
1901+
Roles support Tarantool 3.0.2, Tarantool 3.1.0 and newer.
1902+
Older versions are not supported due to
1903+
[tarantool/tarantool#9643](https://github.com/tarantool/tarantool/issues/9643) and
1904+
[tarantool/tarantool#9649](https://github.com/tarantool/tarantool/issues/9649)
1905+
issues.
1906+
1907+
### Usage
1908+
1909+
1. Add `crud` to dependencies in the project rockspec.
1910+
1911+
**Note**: it's better to use tagged version than `scm-1`.
1912+
Check the latest available [release](https://github.com/tarantool/crud/releases) tag and use it.
1913+
1914+
```lua
1915+
-- <project-name>-scm-1.rockspec
1916+
dependencies = {
1917+
...
1918+
'crud == <the-latest-tag>-1',
1919+
...
1920+
}
1921+
```
1922+
1923+
2. Add crud roles to your application configuration.
1924+
Application must be a sharded one.
1925+
It is required that `roles.crud-storage` is enabled on each
1926+
sharding storage.
1927+
1928+
```yaml
1929+
groups:
1930+
routers:
1931+
sharding:
1932+
roles:
1933+
- router
1934+
roles:
1935+
- roles.crud-router
1936+
replicasets:
1937+
router:
1938+
1939+
storages:
1940+
sharding:
1941+
roles:
1942+
- storage
1943+
roles:
1944+
- roles.crud-storage
1945+
replicasets:
1946+
s-1:
1947+
s-2:
1948+
```
1949+
1950+
<details>
1951+
<summary>Full configuration example</summary>
1952+
1953+
```yaml
1954+
credentials:
1955+
users:
1956+
replicator:
1957+
password: replicating
1958+
roles:
1959+
- replication
1960+
storage:
1961+
password: storing-buckets
1962+
roles:
1963+
- sharding
1964+
guest:
1965+
roles:
1966+
- super
1967+
1968+
sharding:
1969+
bucket_count: 30000
1970+
1971+
replication:
1972+
failover: manual
1973+
1974+
iproto:
1975+
advertise:
1976+
peer:
1977+
login: replicator
1978+
sharding:
1979+
login: storage
1980+
1981+
groups:
1982+
routers:
1983+
sharding:
1984+
roles:
1985+
- router
1986+
roles:
1987+
- roles.crud-router
1988+
replicasets:
1989+
router:
1990+
leader: router
1991+
instances:
1992+
router:
1993+
iproto:
1994+
listen:
1995+
- uri: localhost:3301
1996+
storages:
1997+
sharding:
1998+
roles:
1999+
- storage
2000+
roles:
2001+
- roles.crud-storage
2002+
app:
2003+
module: mystorage
2004+
replicasets:
2005+
s-1:
2006+
leader: s1-master
2007+
instances:
2008+
s1-master:
2009+
iproto:
2010+
listen:
2011+
- uri: localhost:3302
2012+
s1-replica:
2013+
iproto:
2014+
listen:
2015+
- uri: localhost:3303
2016+
s-2:
2017+
leader: s2-master
2018+
instances:
2019+
s2-replica:
2020+
iproto:
2021+
listen:
2022+
- uri: localhost:3304
2023+
s2-master:
2024+
iproto:
2025+
listen:
2026+
- uri: localhost:3305
2027+
```
2028+
</details>
2029+
2030+
3. Set up your schema on storages (for example, through `app.module` section in Tarantool 3 configuration.)
2031+
2032+
```lua
2033+
-- mystorage.lua
2034+
2035+
-- Schema setup is idempotent.
2036+
box.watch('box.status', function()
2037+
if box.info.ro then
2038+
return
2039+
end
2040+
2041+
local customers_space = box.schema.space.create('customers', {
2042+
format = {
2043+
{name = 'id', type = 'unsigned'},
2044+
{name = 'bucket_id', type = 'unsigned'},
2045+
{name = 'name', type = 'string'},
2046+
{name = 'age', type = 'number'},
2047+
},
2048+
if_not_exists = true,
2049+
})
2050+
2051+
customers_space:create_index('id', {
2052+
parts = { {field ='id', is_nullable = false} },
2053+
if_not_exists = true,
2054+
})
2055+
2056+
customers_space:create_index('bucket_id', {
2057+
parts = { {field ='bucket_id', is_nullable = false} },
2058+
if_not_exists = true,
2059+
})
2060+
2061+
customers_space:create_index('age', {
2062+
parts = { {field ='age'} },
2063+
unique = false,
2064+
if_not_exists = true,
2065+
})
2066+
end)
2067+
```
2068+
2069+
4. Start the application cluster. You can check whether asynchronous bootstrap
2070+
had finished through `crud.storage_info()` calls on router.
2071+
2072+
Now your cluster contains storages that are configured to be used for
2073+
CRUD-operations.
2074+
You can simply call CRUD functions on the router to insert, select, and update
2075+
data across the cluster.
2076+
18362077
## Cartridge roles
18372078

18382079
`cartridge.roles.crud-storage` is a Tarantool Cartridge role that depends on the

crud/common/roles.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local config = require('config')
2+
3+
local function is_sharding_role_enabled(expected_sharding_role)
4+
-- Works only for versions newer than 3.0.1-10 (3.0.2 and following)
5+
-- and newer than 3.1.0-entrypoint-77 (3.1.0 and following).
6+
-- https://github.com/tarantool/tarantool/commit/ebb170cb8cf2b9c4634bcf0178665909f578c335
7+
local actual_sharding_roles = config:get('sharding.roles')
8+
9+
for _, actual_sharding_role in ipairs(actual_sharding_roles or {}) do
10+
if actual_sharding_role == expected_sharding_role then
11+
return true
12+
end
13+
end
14+
15+
return false
16+
end
17+
18+
return {
19+
is_sharding_role_enabled = is_sharding_role_enabled,
20+
}

0 commit comments

Comments
 (0)