Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Oleg Babin <olegrok@tarantool.org>,
	tarantool-patches@dev.tarantool.org,
	yaroslav.dynnikov@tarantool.org
Subject: Re: [Tarantool-patches] [PATCH vshard 09/11] ref: introduce vshard.storage.ref module
Date: Fri, 5 Mar 2021 23:06:29 +0100	[thread overview]
Message-ID: <4afd6986-fa28-c0d0-cba4-f9d56fd19146@tarantool.org> (raw)
In-Reply-To: <0522c6f4-7c8a-b424-eeb1-0bb0b10084d7@tarantool.org>

Hi! Thanks for the review!

>> +local function ref_session_new(sid)
>> +    -- Session object does store its internal hot attributes in a table. Because
>> +    -- it would mean access to any session attribute would cost at least one
>> +    -- table indexing operation. Instead, all internal fields are stored as
>> +    -- upvalues referenced by the methods defined as closures.
>> +    --
>> +    -- This means session creation may not very suitable for jitting, but it is
>> +    -- very rare and attempts to optimize the most common case.
>> +    --
>> +    -- Still the public functions take 'self' object to make it look normally.
>> +    -- They even use it a bit.
>> +
>> +    -- Ref map to get ref object by its ID.
>> +    local ref_map = {}
>> +    -- Ref heap sorted by their deadlines.
>> +    local ref_heap = lheap.new(heap_min_deadline_cmp)
>> +    -- Total number of refs of the session. Is used to drop the session without
>> +    -- fullscan of the ref map. Heap size can't be used because not all refs are
>> +    -- stored here. See more on that below.
>> +    local count = 0
> 
> Maybe it's better to rename it to "global_count". Sometimes it's quite confusing to see `M.count +=` near `count += `.
> 
> Also you have "global_map" and "global_heap" so no reasons to call it just "count".

I have global_map and global_heap variables because I also have normal map and
heap, local to the session. To distinguish between them I added 'global_'
prefix to the global ones.

The count here is not global. It is local to the session. But I see the point.
I renamed it to `ref_count` to be consistent with `ref_map` and `ref_heap`.

====================
diff --git a/vshard/storage/ref.lua b/vshard/storage/ref.lua
index 7589cb9..27f7804 100644
--- a/vshard/storage/ref.lua
+++ b/vshard/storage/ref.lua
@@ -84,7 +84,7 @@ local function ref_session_new(sid)
     -- Total number of refs of the session. Is used to drop the session without
     -- fullscan of the ref map. Heap size can't be used because not all refs are
     -- stored here. See more on that below.
-    local count = 0
+    local ref_count = 0
     -- Cache global session storages as upvalues to save on M indexing.
     local global_heap = M.session_heap
     local global_map = M.session_map
@@ -94,9 +94,9 @@ local function ref_session_new(sid)
         assert(new_count >= 0)
         M.count = new_count
 
-        new_count = count - del_count
+        new_count = ref_count - del_count
         assert(new_count >= 0)
-        count = new_count
+        ref_count = new_count
     end
 
     local function ref_session_update_deadline(self)
@@ -224,7 +224,7 @@ local function ref_session_new(sid)
             self.deadline = deadline
             global_heap:update(self)
         end
-        count = count + 1
+        ref_count = ref_count + 1
         M.count = M.count + 1
         return true
     end
@@ -260,7 +260,7 @@ local function ref_session_new(sid)
     local function ref_session_kill(self)
         global_map[sid] = nil
         global_heap:remove(self)
-        ref_session_discount(self, count)
+        ref_session_discount(self, ref_count)
     end
 
     -- Don't use __index. It is useless since all sessions use closures as
====================

>> +
>> +    --
>> +    -- GC expired refs until they end or the limit on the number of iterations
>> +    -- is exhausted. The limit is supposed to prevent too long GC which would
>> +    -- occupy TX thread unfairly.
>> +    --
>> +    -- Returns false if nothing to GC, or number of iterations left from the
>> +    -- limit. The caller is supposed to yield when 0 is returned, and retry GC
>> +    -- until it returns false.
>> +    -- The function itself does not yield, because it is used from a more
>> +    -- generic function GCing all sessions. It would not ever yield if all
>> +    -- sessions would have less than limit refs, even if total ref count would
>> +    -- be much bigger.
>> +    --
>> +    -- Besides, the session might be killed during general GC. There must not be
>> +    -- any yields in session methods so as not to introduce a support of dead
>> +    -- sessions.
>> +    --
>> +    local function ref_session_gc(self, limit, now)
>> +        if self.deadline >= now then
>> +            return false
>> +        end
> 
> Here you mix "booleans" and "numbers" as return values. Maybe it's better to return "nil" here?

No problem:

====================
diff --git a/vshard/storage/ref.lua b/vshard/storage/ref.lua
index 27f7804..d31e3ed 100644
--- a/vshard/storage/ref.lua
+++ b/vshard/storage/ref.lua
@@ -164,9 +164,9 @@ local function ref_session_new(sid)
     -- is exhausted. The limit is supposed to prevent too long GC which would
     -- occupy TX thread unfairly.
     --
-    -- Returns false if nothing to GC, or number of iterations left from the
+    -- Returns nil if nothing to GC, or number of iterations left from the
     -- limit. The caller is supposed to yield when 0 is returned, and retry GC
-    -- until it returns false.
+    -- until it returns nil.
     -- The function itself does not yield, because it is used from a more
     -- generic function GCing all sessions. It would not ever yield if all
     -- sessions would have less than limit refs, even if total ref count would
@@ -178,7 +178,7 @@ local function ref_session_new(sid)
     --
     local function ref_session_gc(self, limit, now)
         if self.deadline >= now then
-            return false
+            return nil
         end
         local top = ref_heap:top()
         local del = 1
====================

>> +
>> +    -- Don't use __index. It is useless since all sessions use closures as
>> +    -- methods. Also it is probably slower because on each method call would
>> +    -- need to get the metatable, get __index, find the method here. While now
>> +    -- it is only an index operation on the session object.
> 
> Side note: for heap you still use "__index" even heap uses closures as methods.

Indeed, I should have thought of this. I updated the part1 branch, and rebased the
part2 branch. See the part1 email thread for the diff.

  reply	other threads:[~2021-03-05 22:06 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-23  0:15 [Tarantool-patches] [PATCH vshard 00/11] VShard Map-Reduce, part 2: Ref, Sched, Map Vladislav Shpilevoy via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 01/11] error: introduce vshard.error.timeout() Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:27   ` Oleg Babin via Tarantool-patches
2021-02-24 21:46     ` Vladislav Shpilevoy via Tarantool-patches
2021-02-25 12:42       ` Oleg Babin via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 10/11] sched: introduce vshard.storage.sched module Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:28   ` Oleg Babin via Tarantool-patches
2021-02-24 21:50     ` Vladislav Shpilevoy via Tarantool-patches
2021-03-04 21:02   ` Oleg Babin via Tarantool-patches
2021-03-05 22:06     ` Vladislav Shpilevoy via Tarantool-patches
2021-03-09  8:03       ` Oleg Babin via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 11/11] router: introduce map_callrw() Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:28   ` Oleg Babin via Tarantool-patches
2021-02-24 22:04     ` Vladislav Shpilevoy via Tarantool-patches
2021-02-25 12:43       ` Oleg Babin via Tarantool-patches
2021-02-26 23:58         ` Vladislav Shpilevoy via Tarantool-patches
2021-03-01 10:58           ` Oleg Babin via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 02/11] storage: add helper for local functions invocation Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:27   ` Oleg Babin via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 03/11] storage: cache bucket count Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:27   ` Oleg Babin via Tarantool-patches
2021-02-24 21:47     ` Vladislav Shpilevoy via Tarantool-patches
2021-02-25 12:42       ` Oleg Babin via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 04/11] registry: module for circular deps resolution Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:27   ` Oleg Babin via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 05/11] util: introduce safe fiber_cond_wait() Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:27   ` Oleg Babin via Tarantool-patches
2021-02-24 21:48     ` Vladislav Shpilevoy via Tarantool-patches
2021-02-25 12:42       ` Oleg Babin via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 06/11] util: introduce fiber_is_self_canceled() Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:27   ` Oleg Babin via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 07/11] storage: introduce bucket_generation_wait() Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:27   ` Oleg Babin via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 08/11] storage: introduce bucket_are_all_rw() Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:27   ` Oleg Babin via Tarantool-patches
2021-02-24 21:48     ` Vladislav Shpilevoy via Tarantool-patches
2021-02-23  0:15 ` [Tarantool-patches] [PATCH vshard 09/11] ref: introduce vshard.storage.ref module Vladislav Shpilevoy via Tarantool-patches
2021-02-24 10:28   ` Oleg Babin via Tarantool-patches
2021-02-24 21:49     ` Vladislav Shpilevoy via Tarantool-patches
2021-02-25 12:42       ` Oleg Babin via Tarantool-patches
2021-03-04 21:22   ` Oleg Babin via Tarantool-patches
2021-03-05 22:06     ` Vladislav Shpilevoy via Tarantool-patches [this message]
2021-03-09  8:03       ` Oleg Babin via Tarantool-patches
2021-03-21 18:49   ` Vladislav Shpilevoy via Tarantool-patches
2021-03-12 23:13 ` [Tarantool-patches] [PATCH vshard 00/11] VShard Map-Reduce, part 2: Ref, Sched, Map Vladislav Shpilevoy via Tarantool-patches
2021-03-15  7:05   ` Oleg Babin via Tarantool-patches
2021-03-28 18:17 ` Vladislav Shpilevoy via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4afd6986-fa28-c0d0-cba4-f9d56fd19146@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=olegrok@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --cc=yaroslav.dynnikov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH vshard 09/11] ref: introduce vshard.storage.ref module' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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