Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH] Cache find_sharded_spaces result
@ 2018-06-04 12:52 AKhatskevich
  2018-06-04 14:34 ` [tarantool-patches] " Vladislav Shpilevoy
  0 siblings, 1 reply; 2+ messages in thread
From: AKhatskevich @ 2018-06-04 12:52 UTC (permalink / raw)
  To: v.shpilevoy, tarantool-patches

`find_sharded_spaces` result is cached.
Cache result is updated only when `schema_version` changes.

User-visible `sharded_spaces` function returns a deep copy of the cached
result and user cannot affect a result of a future call to the function.

Closes: #111
---
Branch: https://github.com/tarantool/vshard/tree/kh/gh-111-cache_spaces
Issue: https://github.com/tarantool/vshard/issues/111

 test/unit/garbage.result   | 24 ++++++++++++++++++++++--
 test/unit/garbage.test.lua |  9 +++++++++
 vshard/storage/init.lua    | 15 +++++++++++++--
 3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/test/unit/garbage.result b/test/unit/garbage.result
index 270949c..0da8ee1 100644
--- a/test/unit/garbage.result
+++ b/test/unit/garbage.result
@@ -106,8 +106,8 @@ sk2 = s2:create_index('bucket_id', {parts = {{2, 'unsigned'}}, unique = false})
 ...
 show_sharded_spaces()
 ---
-- - test2
-  - test
+- - test
+  - test2
 ...
 s:drop()
 ---
@@ -116,6 +116,26 @@ s2:drop()
 ---
 ...
 --
+-- gh-111: cache sharded spaces based on schema version
+--
+cached_spaces = vshard.storage.internal.cached_find_sharded_spaces()
+---
+...
+cached_spaces == vshard.storage.internal.cached_find_sharded_spaces()
+---
+- true
+...
+s = box.schema.create_space('test')
+---
+...
+cached_spaces == vshard.storage.internal.cached_find_sharded_spaces()
+---
+- false
+...
+s:drop()
+---
+...
+--
 -- Test garbage buckets detection.
 --
 find_garbage = vshard.storage.internal.find_garbage_bucket
diff --git a/test/unit/garbage.test.lua b/test/unit/garbage.test.lua
index 12a59c5..db7821f 100644
--- a/test/unit/garbage.test.lua
+++ b/test/unit/garbage.test.lua
@@ -58,6 +58,15 @@ show_sharded_spaces()
 s:drop()
 s2:drop()
 
+--
+-- gh-111: cache sharded spaces based on schema version
+--
+cached_spaces = vshard.storage.internal.cached_find_sharded_spaces()
+cached_spaces == vshard.storage.internal.cached_find_sharded_spaces()
+s = box.schema.create_space('test')
+cached_spaces == vshard.storage.internal.cached_find_sharded_spaces()
+s:drop()
+
 --
 -- Test garbage buckets detection.
 --
diff --git a/vshard/storage/init.lua b/vshard/storage/init.lua
index f3f1bab..d5e7b57 100644
--- a/vshard/storage/init.lua
+++ b/vshard/storage/init.lua
@@ -552,10 +552,16 @@ local function bucket_recv(bucket_id, from, data)
 end
 
 --
--- Find spaces with index having the specified name.
+-- Find spaces with index having the specified (in cfg) name.
+-- The function result is cached using `schema_version`.
 -- @retval Map of type {space_id = <space object>}.
 --
+local sharded_spaces_cache_schema_version = nil
+local sharded_spaces_cache = nil
 local function find_sharded_spaces()
+    if sharded_spaces_cache_schema_version == box.internal.schema_version() then
+        return sharded_spaces_cache
+    end
     local spaces = {}
     local idx = M.shard_index
     for k, space in pairs(box.space) do
@@ -567,6 +573,8 @@ local function find_sharded_spaces()
             end
         end
     end
+    sharded_spaces_cache_schema_version = box.internal.schema_version()
+    sharded_spaces_cache = spaces
     return spaces
 end
 
@@ -1817,6 +1825,7 @@ M.collect_garbage_step = collect_garbage_step
 M.collect_garbage_f = collect_garbage_f
 M.rebalancer_build_routes = rebalancer_build_routes
 M.rebalancer_calculate_metrics = rebalancer_calculate_metrics
+M.cached_find_sharded_spaces = find_sharded_spaces
 
 if not rawget(_G, '__module_vshard_storage') then
     rawset(_G, '__module_vshard_storage', M)
@@ -1853,6 +1862,8 @@ return {
     internal = M,
     on_master_enable = on_master_enable,
     on_master_disable = on_master_disable,
-    sharded_spaces = find_sharded_spaces,
+    sharded_spaces = function()
+        return table.deepcopy(find_sharded_spaces())
+    end,
     module_version = function() return M.module_version end,
 }
-- 
2.14.1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-06-04 14:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-04 12:52 [tarantool-patches] [PATCH] Cache find_sharded_spaces result AKhatskevich
2018-06-04 14:34 ` [tarantool-patches] " Vladislav Shpilevoy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox