[tarantool-patches] [PATCH 4/4] swim: default generation is timestamp

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Fri Jun 28 02:25:46 MSK 2019


Generation is supposed to be a persistent counter to distinguish
between different installations of the same SWIM instance. By
default it was set to 0, which was quite unsafe.

Kostja proposed an easy and bright solution - generation could be
set to timestamp by default. In such a case on each restart it is
almost 100% will be different.

Follow up #4280
---
 src/lua/swim.lua        |  4 +++-
 test/swim/swim.result   | 24 ++++++++++++------------
 test/swim/swim.test.lua | 24 ++++++++++++------------
 3 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/src/lua/swim.lua b/src/lua/swim.lua
index 69d077c37..01eeb7eae 100644
--- a/src/lua/swim.lua
+++ b/src/lua/swim.lua
@@ -969,7 +969,7 @@ end
 -- provided.
 --
 local function swim_new(cfg)
-    local generation = 0
+    local generation
     if cfg and type(cfg) == 'table' and cfg.generation then
         generation = cfg.generation
         if type(generation) ~= 'number' or generation < 0 or
@@ -985,6 +985,8 @@ local function swim_new(cfg)
         if next(cfg) == nil then
             cfg = nil
         end
+    else
+        generation = fiber.time64()
     end
     local ptr = internal.swim_new(generation)
     if ptr == nil then
diff --git a/test/swim/swim.result b/test/swim/swim.result
index 26f74f9f4..c7d539f5d 100644
--- a/test/swim/swim.result
+++ b/test/swim/swim.result
@@ -208,10 +208,10 @@ s:delete()
 --
 -- Basic member table manipulations.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
+s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01, generation = 0})
 ---
 ...
-s2 = swim.new({uuid = uuid(2), uri = listen_uri, heartbeat_rate = 0.01})
+s2 = swim.new({uuid = uuid(2), uri = listen_uri, heartbeat_rate = 0.01, generation = 0})
 ---
 ...
 s1.broadcast()
@@ -381,7 +381,7 @@ s2:delete()
 --
 -- Member API.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri()})
+s1 = swim.new({uuid = uuid(1), uri = uri(), generation = 0})
 ---
 ...
 s = s1:self()
@@ -699,10 +699,10 @@ self:is_dropped()
 --
 -- Check payload dissemination.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
+s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01, generation = 0})
 ---
 ...
-s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
+s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01, generation = 0})
 ---
 ...
 s1:add_member({uuid = uuid(2), uri = s2:self():uri()})
@@ -757,7 +757,7 @@ s2:delete()
 function iterate() local t = {} for k, v in s:pairs() do table.insert(t, {k, v}) end return t end
 ---
 ...
-s = swim.new()
+s = swim.new({generation = 0})
 ---
 ...
 iterate()
@@ -832,10 +832,10 @@ s:delete()
 --
 -- Payload caching.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
+s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01, generation = 0})
 ---
 ...
-s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
+s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01, generation = 0})
 ---
 ...
 s1_self = s1:self()
@@ -1014,10 +1014,10 @@ s:delete()
 --
 -- Encryption.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri()})
+s1 = swim.new({uuid = uuid(1), uri = uri(), generation = 0})
 ---
 ...
-s2 = swim.new({uuid = uuid(2), uri = uri()})
+s2 = swim.new({uuid = uuid(2), uri = uri(), generation = 0})
 ---
 ...
 s1.set_codec()
@@ -1224,7 +1224,7 @@ s2:delete()
 -- gh-4250: allow to set triggers on a new member appearance, old
 -- member drop, member update.
 --
-s1 = swim.new()
+s1 = swim.new({generation = 0})
 ---
 ...
 s1.on_member_event()
@@ -1313,7 +1313,7 @@ t_yield_id = s1:on_member_event(t_yield, 'ctx2')
 f_need_sleep = true
 ---
 ...
-s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
+s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01, generation = 0})
 ---
 ...
 s2:add_member({uuid = s1:self():uuid(), uri = s1:self():uri()})
diff --git a/test/swim/swim.test.lua b/test/swim/swim.test.lua
index 496beced5..c949b3be2 100644
--- a/test/swim/swim.test.lua
+++ b/test/swim/swim.test.lua
@@ -71,8 +71,8 @@ s:delete()
 --
 -- Basic member table manipulations.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
-s2 = swim.new({uuid = uuid(2), uri = listen_uri, heartbeat_rate = 0.01})
+s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01, generation = 0})
+s2 = swim.new({uuid = uuid(2), uri = listen_uri, heartbeat_rate = 0.01, generation = 0})
 
 s1.broadcast()
 s1:broadcast('wrong port')
@@ -132,7 +132,7 @@ s2:delete()
 --
 -- Member API.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri()})
+s1 = swim.new({uuid = uuid(1), uri = uri(), generation = 0})
 s = s1:self()
 s
 s:status()
@@ -233,8 +233,8 @@ self:is_dropped()
 --
 -- Check payload dissemination.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
-s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
+s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01, generation = 0})
+s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01, generation = 0})
 s1:add_member({uuid = uuid(2), uri = s2:self():uri()})
 while s2:size() ~= 2 do fiber.sleep(0.01) end
 s1_view = s2:member_by_uuid(uuid(1))
@@ -256,7 +256,7 @@ s2:delete()
 -- Iterators.
 --
 function iterate() local t = {} for k, v in s:pairs() do table.insert(t, {k, v}) end return t end
-s = swim.new()
+s = swim.new({generation = 0})
 iterate()
 s:cfg({uuid = uuid(1), uri = uri(), gc_mode = 'off'})
 s.pairs()
@@ -270,8 +270,8 @@ s:delete()
 --
 -- Payload caching.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01})
-s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
+s1 = swim.new({uuid = uuid(1), uri = uri(), heartbeat_rate = 0.01, generation = 0})
+s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01, generation = 0})
 s1_self = s1:self()
 _ = s1:add_member({uuid = s2:self():uuid(), uri = s2:self():uri()})
 _ = s2:add_member({uuid = s1_self:uuid(), uri = s1_self:uri()})
@@ -344,8 +344,8 @@ s:delete()
 --
 -- Encryption.
 --
-s1 = swim.new({uuid = uuid(1), uri = uri()})
-s2 = swim.new({uuid = uuid(2), uri = uri()})
+s1 = swim.new({uuid = uuid(1), uri = uri(), generation = 0})
+s2 = swim.new({uuid = uuid(2), uri = uri(), generation = 0})
 
 s1.set_codec()
 s1:set_codec(100)
@@ -418,7 +418,7 @@ s2:delete()
 -- gh-4250: allow to set triggers on a new member appearance, old
 -- member drop, member update.
 --
-s1 = swim.new()
+s1 = swim.new({generation = 0})
 s1.on_member_event()
 
 m_list = {}
@@ -452,7 +452,7 @@ m_list = {} e_list = {} ctx_list = {}
 
 t_yield_id = s1:on_member_event(t_yield, 'ctx2')
 f_need_sleep = true
-s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01})
+s2 = swim.new({uuid = uuid(2), uri = uri(), heartbeat_rate = 0.01, generation = 0})
 s2:add_member({uuid = s1:self():uuid(), uri = s1:self():uri()})
 while s1:size() ~= 2 do fiber.sleep(0.01) end
 -- Only first trigger worked. Second is waiting, because first
-- 
2.20.1 (Apple Git-117)





More information about the Tarantool-patches mailing list