chewbranca commented on a change in pull request #610: Optimize ddoc cache
URL: https://github.com/apache/couchdb/pull/610#discussion_r123586231
 
 

 ##########
 File path: src/ddoc_cache/src/ddoc_cache_opener.erl
 ##########
 @@ -32,195 +32,96 @@
 ]).
 
 -export([
-    open_doc/2,
-    open_doc/3,
-    open_validation_funs/1,
-    evict_docs/2,
-    lookup/1,
-    match_newest/1,
-    recover_doc/2,
-    recover_doc/3,
-    recover_validation_funs/1
+    open/1
 ]).
--export([
-    handle_db_event/3
-]).
--export([
-    fetch_doc_data/1
-]).
-
--define(CACHE, ddoc_cache_lru).
--define(OPENING, ddoc_cache_opening).
 
--type dbname() :: iodata().
--type docid() :: iodata().
--type doc_hash() :: <<_:128>>.
--type revision() :: {pos_integer(), doc_hash()}.
+-include("ddoc_cache.hrl").
 
--record(opener, {
-    key,
-    pid,
-    clients
-}).
 
 -record(st, {
-    db_ddocs,
-    evictor
+    db_ddocs
 }).
 
 start_link() ->
     gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
 
--spec open_doc(dbname(), docid()) -> {ok, #doc{}}.
-open_doc(DbName, DocId) ->
-    Resp = gen_server:call(?MODULE, {open, {DbName, DocId}}, infinity),
-    handle_open_response(Resp).
 
--spec open_doc(dbname(), docid(), revision()) -> {ok, #doc{}}.
-open_doc(DbName, DocId, Rev) ->
-    Resp = gen_server:call(?MODULE, {open, {DbName, DocId, Rev}}, infinity),
-    handle_open_response(Resp).
-
--spec open_validation_funs(dbname()) -> {ok, [fun()]}.
-open_validation_funs(DbName) ->
-    Resp = gen_server:call(?MODULE, {open, {DbName, validation_funs}}, 
infinity),
-    handle_open_response(Resp).
-
--spec evict_docs(dbname(), [docid()]) -> ok.
-evict_docs(DbName, DocIds) ->
-    gen_server:cast(?MODULE, {evict, DbName, DocIds}).
-
-lookup(Key) ->
-    try ets_lru:lookup_d(?CACHE, Key) of
-        {ok, _} = Resp ->
-            Resp;
-        _ ->
-            missing
-    catch
-        error:badarg ->
-            recover
-    end.
-
-match_newest(Key) ->
-    try ets_lru:match_object(?CACHE, Key, '_') of
+open(Key) ->
+    try ets:lookup(?CACHE, Key) of
         [] ->
-            missing;
-        Docs ->
-            Sorted = lists:sort(
-                fun (#doc{deleted=DelL, revs=L}, #doc{deleted=DelR, revs=R}) ->
-                    {not DelL, L} > {not DelR, R}
-                end, Docs),
-            {ok, hd(Sorted)}
-    catch
-        error:badarg ->
-            recover
+            couch_stats:increment_counter([ddoc_cache, miss]),
+            Resp = gen_server:call(?MODULE, {open, Key}, infinity),
+            ddoc_cache_entry:handle_resp(Resp);
+        [#entry{val = Val}] ->
+            couch_stats:increment_counter([ddoc_cache, hit]),
+            ddoc_cache_lru:accessed(Key),
 
 Review comment:
   I'm concerned about sending a message here with every successful lookup. 
We've seen this to be problematic in couch_server, and I think we might run 
into similar issues here. Every single time `ddoc_cache_lru:accessed/1` is 
called it can trigger 1 khash read, 1 khash write, one ets lookup, one 
`is_process_alive` check, one ets delete, and one ets insert. I could see this 
getting overwhelmed in a hurry.
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to