chewbranca commented on code in PR #5491:
URL: https://github.com/apache/couchdb/pull/5491#discussion_r2059397392


##########
src/couch_stats/src/csrt_server.erl:
##########
@@ -0,0 +1,198 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(csrt_server).
+
+-behaviour(gen_server).
+
+-export([
+    start_link/0,
+    init/1,
+    handle_call/3,
+    handle_cast/2
+]).
+
+-export([
+    create_pid_ref/0,
+    create_resource/1,
+    destroy_resource/1,
+    get_resource/1,
+    get_context_type/1,
+    inc/2,
+    inc/3,
+    new_context/2,
+    set_context_dbname/2,
+    set_context_username/2,
+    set_context_type/2,
+    update_counter/3
+]).
+
+-include_lib("stdlib/include/ms_transform.hrl").
+-include_lib("couch_stats_resource_tracker.hrl").
+
+
+-record(st, {}).
+
+%%
+%% Public API
+%%
+
+-spec create_pid_ref() -> pid_ref().
+create_pid_ref() ->
+    {self(), make_ref()}.
+
+%%
+%%
+%% Context lifecycle API
+%%
+
+-spec new_context(Type :: rctx_type(), Nonce :: nonce()) -> rctx().
+new_context(Type, Nonce) ->
+    #rctx{
+       nonce = Nonce,
+       pid_ref = create_pid_ref(),
+       type = Type
+    }.
+
+-spec set_context_dbname(DbName, PidRef) -> boolean() when
+    DbName :: dbname(), PidRef :: maybe_pid_ref().
+set_context_dbname(_, undefined) ->
+    false;
+set_context_dbname(DbName, PidRef) ->
+    update_element(PidRef, [{#rctx.dbname, DbName}]).
+
+%%set_context_handler_fun(_, undefined) ->
+%%    ok;
+%%set_context_handler_fun(Fun, PidRef) when is_function(Fun) ->
+%%    FProps = erlang:fun_info(Fun),
+%%    Mod = proplists:get_value(module, FProps),
+%%    Func = proplists:get_value(name, FProps),
+%%    #rctx{type=#coordinator{}=Coordinator} = get_resource(PidRef),
+%%    Update = [{#rctx.type, Coordinator#coordinator{mod=Mod, func=Func}}],
+%%    update_element(PidRef, Update).
+
+-spec set_context_username(UserName, PidRef) -> boolean() when
+    UserName :: username(), PidRef :: maybe_pid_ref().
+set_context_username(_, undefined) ->
+    ok;
+set_context_username(UserName, PidRef) ->
+    update_element(PidRef, [{#rctx.username, UserName}]).
+
+-spec get_context_type(Rctx :: rctx()) -> rctx_type().
+get_context_type(#rctx{type=Type}) ->
+    Type.
+
+-spec set_context_type(Type, PidRef) -> boolean() when
+    Type :: rctx_type(), PidRef :: maybe_pid_ref().
+set_context_type(Type, PidRef) ->
+    update_element(PidRef, [{#rctx.type, Type}]).
+
+-spec create_resource(Rctx :: rctx()) -> true.
+create_resource(#rctx{} = Rctx) ->
+    catch ets:insert(?MODULE, Rctx).
+
+-spec destroy_resource(PidRef :: maybe_pid_ref()) -> boolean().
+destroy_resource(undefined) ->
+    false;
+destroy_resource({_,_}=PidRef) ->
+    catch ets:delete(?MODULE, PidRef).
+
+-spec get_resource(PidRef :: maybe_pid_ref()) -> maybe_rctx().
+get_resource(undefined) ->
+    undefined;
+get_resource(PidRef) ->
+    catch case ets:lookup(?MODULE, PidRef) of
+        [#rctx{}=Rctx] ->
+            Rctx;
+        [] ->
+            undefined
+    end.
+
+-spec is_rctx_field(Field :: rctx_field() | atom()) -> boolean().
+is_rctx_field(Field) ->
+    maps:is_key(Field, ?KEYS_TO_FIELDS).
+
+-spec get_rctx_field(Field :: rctx_field()) -> non_neg_integer().
+get_rctx_field(Field) ->
+    maps:get(Field, ?KEYS_TO_FIELDS).
+
+-spec update_counter(PidRef, Field, Count) -> non_neg_integer() when
+        PidRef :: maybe_pid_ref(),
+        Field :: rctx_field(),
+        Count :: non_neg_integer().
+update_counter(undefined, _Field, _Count) ->
+    0;
+update_counter({_Pid,_Ref}=PidRef, Field, Count) when Count >= 0 ->
+    %% TODO: mem3 crashes without catch, why do we lose the stats table?
+    case is_rctx_field(Field) of
+        true ->
+            Update = {get_rctx_field(Field), Count},
+            catch ets:update_counter(?MODULE, PidRef, Update, 
#rctx{pid_ref=PidRef});

Review Comment:
   It's quite intentional that the value is bubbled back up because we rely on 
`ets:update_counter` actually being atomic and isolated!
   
   > This function provides an efficient way to update one or more counters, 
without the trouble of having to look up an object, update the object by 
incrementing an element, and insert the resulting object into the table again. 
The operation is guaranteed to be [atomic and 
isolated](https://www.erlang.org/docs/23/man/ets#concurrency).
   
   Also, return value is: `The new counter value is returned.` so my thought 
process was make sure to bubble the new counter up out of these functions so 
that we can always perform the increment operation while also having the newly 
updated value available without an additional call to ets.
   
   Like you mentioned above, the `catch` clause around the operation prevents 
it from _always_ return a number, so I think that should be swapped with a 
`try` block as you mentioned above so we can always return an expected value.
   
   That said, zero in this case is essentially used as a failure indicator, as 
we strictly deal with positively increasing values, and `inc` with `0` is a 
no-op. It's arguable it would be better to use the `maybe_` pattern used 
elsewhere and switch the zeroes to `undefined` but it seemed like always just 
returning a number and zero being used as an indicator for "nothing happened" 
was a reasonable approach.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to