Skip to content

Commit 1afe790

Browse files
better0fdeadDifferentialOrange
authored andcommitted
crud: add readview support
Added read view support for select and pairs. Closes #343
1 parent e75be26 commit 1afe790

File tree

13 files changed

+4081
-10
lines changed

13 files changed

+4081
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
10+
### Added
11+
* Read view support for select and pairs (#343).
12+
813
## [1.2.0] - 07-06-23
914

1015
### Added

README.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ It also provides the `crud-storage` and `crud-router` roles for
3737
- [Count](#count)
3838
- [Call options for crud methods](#call-options-for-crud-methods)
3939
- [Statistics](#statistics)
40+
- [Read view](#read-view)
41+
- [Creating a read view](#creating-a-read-view)
42+
- [Closing a read view](#closing-a-read-view)
43+
- [Read view select](#read-view-select)
44+
- [Read view select conditions](#read-view-select-conditions)
45+
- [Read view pairs](#read-view-pairs)
4046
- [Cartridge roles](#cartridge-roles)
4147
- [Usage](#usage)
4248
- [License](#license)
@@ -1541,6 +1547,170 @@ support preserving stats between role reload
15411547
(see [tarantool/metrics#334](https://github.com/tarantool/metrics/issues/334)),
15421548
thus this feature will be unsupported for `metrics` driver.
15431549

1550+
### Read view
1551+
1552+
A read view is an in-memory snapshot of data on instance that isn’t affected by future data modifications. Read views allow you to retrieve data using the `read_view_object:select()` and `read_view_object:pairs()` operations.
1553+
1554+
Read views can be used to make complex analytical queries. This reduces the load on the main database and improves RPS for a single Tarantool instance.
1555+
1556+
Read views have the following limitations:
1557+
1558+
* Only the memtx engine is supported.
1559+
* Read view can be used starting from Tarantool Enterprise v2.11.0.
1560+
* There is no clusterwide readview support. For a sharded cluster, we open a readview on each storage. Due to a cluster's distributed nature, it is not guaranteed that they will open simultaneously.
1561+
1562+
#### Creating a read view
1563+
1564+
To create a read view, call the `crud.readview()` function.
1565+
1566+
```lua
1567+
local rv = crud.readview(opts)
1568+
```
1569+
1570+
where:
1571+
1572+
* `opts`:
1573+
* `name` (`?string`) - name of the read view
1574+
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
1575+
1576+
**Example:**
1577+
1578+
```lua
1579+
local rv = crud.readview({name = 'foo', timeout = 3})
1580+
```
1581+
1582+
#### Closing a read view
1583+
1584+
When a read view is no longer needed, close it using the `read_view_object:close()` method because a read view may consume a substantial amount of memory.
1585+
1586+
```lua
1587+
local rv = crud.readview()
1588+
rv:close(opts)
1589+
```
1590+
1591+
where:
1592+
1593+
* `opts`:
1594+
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
1595+
1596+
A read view is also closed implicitly when the read view object is collected by the Lua garbage collector.
1597+
1598+
**Example:**
1599+
1600+
```lua
1601+
local rv = crud.readview()
1602+
rv:close({timeout = 3})
1603+
```
1604+
1605+
#### Read view select
1606+
1607+
`read_view_object:select()` supports multi-conditional selects, treating a cluster as a single space, same as `crud.select`.
1608+
1609+
```lua
1610+
local rv = crud.readview()
1611+
local objects, err = rv:select(space_name, conditions, opts)
1612+
rv:close()
1613+
```
1614+
1615+
Opts are the same as [select opts](#select), except `balance`, `prefer_replica` and `mode` are not supported.
1616+
1617+
Returns metadata and array of rows, error.
1618+
1619+
**Example:**
1620+
1621+
```lua
1622+
local rv = crud.readview()
1623+
rv:select('customers', nil, {batch_size=1, fullscan=true})
1624+
---
1625+
- metadata: [{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'},
1626+
{'name': 'name', 'type': 'string'}, {'name': 'age', 'type': 'number'}]
1627+
rows:
1628+
- [1, 477, 'Elizabeth', 12]
1629+
- [2, 401, 'Mary', 46]
1630+
- [3, 2804, 'David', 33]
1631+
- [4, 1161, 'William', 81]
1632+
- [5, 1172, 'Jack', 35]
1633+
- [6, 1064, 'William', 25]
1634+
- [7, 693, 'Elizabeth', 18]
1635+
- null
1636+
...
1637+
crud.insert('customers', {8, box.NULL, 'Elizabeth', 23})
1638+
---
1639+
- rows:
1640+
- [8, 185, 'Elizabeth', 23]
1641+
metadata: [{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'},
1642+
{'name': 'name', 'type': 'string'}, {'name': 'age', 'type': 'number'}]
1643+
- null
1644+
...
1645+
rv:select('customers', nil, {batch_size=1, fullscan=true})
1646+
---
1647+
- metadata: [{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'},
1648+
{'name': 'name', 'type': 'string'}, {'name': 'age', 'type': 'number'}]
1649+
rows:
1650+
- [1, 477, 'Elizabeth', 12]
1651+
- [2, 401, 'Mary', 46]
1652+
- [3, 2804, 'David', 33]
1653+
- [4, 1161, 'William', 81]
1654+
- [5, 1172, 'Jack', 35]
1655+
- [6, 1064, 'William', 25]
1656+
- [7, 693, 'Elizabeth', 18]
1657+
- null
1658+
...
1659+
rv:close()
1660+
```
1661+
1662+
##### Read view select conditions
1663+
1664+
Select conditions for `read_view_object:select()` are the same as [select conditions](#select-conditions) for `crud.select`.
1665+
1666+
**Example:**
1667+
1668+
```lua
1669+
rv = crud.readview()
1670+
rv:select('customers', {{'<=', 'age', 35}}, {first = 10})
1671+
---
1672+
- metadata:
1673+
- {'name': 'id', 'type': 'unsigned'}
1674+
- {'name': 'bucket_id', 'type': 'unsigned'}
1675+
- {'name': 'name', 'type': 'string'}
1676+
- {'name': 'age', 'type': 'number'}
1677+
rows:
1678+
- [5, 1172, 'Jack', 35]
1679+
- [3, 2804, 'David', 33]
1680+
- [6, 1064, 'William', 25]
1681+
- [7, 693, 'Elizabeth', 18]
1682+
- [1, 477, 'Elizabeth', 12]
1683+
...
1684+
rv.close()
1685+
```
1686+
1687+
#### Read view pairs
1688+
1689+
You can iterate across a distributed space using the `read_view_object:pairs()` method.
1690+
Its arguments are the same as [`crud.readview.select`](#read-view-select) arguments except
1691+
`fullscan` (it does not exist because `crud.pairs` does not generate a critical
1692+
log entry on potentially long requests) and negative `first` values aren't
1693+
allowed.
1694+
User could pass `use_tomap` flag (`false` by default) to iterate over flat tuples or objects.
1695+
1696+
**Example:**
1697+
1698+
```lua
1699+
rv = crud.readview()
1700+
local tuples = {}
1701+
for _, tuple in rv:pairs('customers', {{'<=', 'age', 35}}, {use_tomap = false}) do
1702+
-- {5, 1172, 'Jack', 35}
1703+
table.insert(tuples, tuple)
1704+
end
1705+
1706+
local objects = {}
1707+
for _, object in rv:pairs('customers', {{'<=', 'age', 35}}, {use_tomap = true}) do
1708+
-- {id = 5, name = 'Jack', bucket_id = 1172, age = 35}
1709+
table.insert(objects, object)
1710+
end
1711+
rv:close()
1712+
```
1713+
15441714
## Cartridge roles
15451715

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

crud.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local borders = require('crud.borders')
2020
local sharding_metadata = require('crud.common.sharding.sharding_metadata')
2121
local utils = require('crud.common.utils')
2222
local stats = require('crud.stats')
23+
local readview = require('crud.readview')
2324

2425
local crud = {}
2526

@@ -147,6 +148,10 @@ crud.reset_stats = stats.reset
147148
-- @function storage_info
148149
crud.storage_info = utils.storage_info
149150

151+
-- @refer readview.new
152+
-- @function readview
153+
crud.readview = readview.new
154+
150155
--- Initializes crud on node
151156
--
152157
-- Exports all functions that are used for calls
@@ -174,6 +179,7 @@ function crud.init_storage()
174179
count.init()
175180
borders.init()
176181
sharding_metadata.init()
182+
readview.init()
177183

178184
_G._crud.storage_info_on_storage = utils.storage_info_on_storage
179185
end

crud/common/stash.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ stash.name = {
3030
stats_metrics_registry = '__crud_stats_metrics_registry',
3131
ddl_triggers = '__crud_ddl_spaces_triggers',
3232
select_module_compat_info = '__select_module_compat_info',
33+
storage_readview = '__crud_storage_readview',
3334
}
3435

3536
--- Setup Tarantool Cartridge reload.

0 commit comments

Comments
 (0)