davisp commented on a change in pull request #610: Optimize ddoc cache URL: https://github.com/apache/couchdb/pull/610#discussion_r129070306
########## File path: src/ddoc_cache/src/ddoc_cache_lru.erl ########## @@ -0,0 +1,307 @@ +% 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(ddoc_cache_lru). +-behaviour(gen_server). +-vsn(1). + + +-export([ + start_link/0, + open/1, + insert/2, + refresh/2 +]). + +-export([ + init/1, + terminate/2, + handle_call/3, + handle_cast/2, + handle_info/2, + code_change/3 +]). + +-export([ + handle_db_event/3 +]). + + +-include("ddoc_cache.hrl"). + + +-record(st, { + pids, % pid -> key + dbs, % dbname -> docid -> key -> pid + evictor +}). + + +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + + +open(Key) -> + try ets:lookup(?CACHE, Key) of + [] -> + lru_start(Key, true); + [#entry{pid = undefined}] -> + lru_start(Key, false); + [#entry{val = undefined, pid = Pid}] -> + couch_stats:increment_counter([ddoc_cache, miss]), + ddoc_cache_entry:open(Pid, Key); + [#entry{val = Val, pid = Pid}] -> + couch_stats:increment_counter([ddoc_cache, hit]), + ddoc_cache_entry:accessed(Pid), + {ok, Val} + catch _:_ -> + couch_stats:increment_counter([ddoc_cache, recovery]), + ddoc_cache_entry:recover(Key) + end. + + +insert(Key, Value) -> + case ets:lookup(?CACHE, Key) of + [] -> + Wrapped = ddoc_cache_value:wrap(Value), + gen_server:call(?MODULE, {start, Key, Wrapped}, infinity); + [#entry{}] -> + ok + end. + + +refresh(DbName, DDocIds) -> + gen_server:cast(?MODULE, {refresh, DbName, DDocIds}). + + +init(_) -> + process_flag(trap_exit, true), + {ok, Pids} = khash:new(), + {ok, Dbs} = khash:new(), + {ok, Evictor} = couch_event:link_listener( + ?MODULE, handle_db_event, nil, [all_dbs] + ), + ?EVENT(lru_init, nil), + {ok, #st{ + pids = Pids, + dbs = Dbs, + evictor = Evictor + }}. + + +terminate(_Reason, St) -> + case is_pid(St#st.evictor) of + true -> exit(St#st.evictor, kill); + false -> ok + end, + ok. + + +handle_call({start, Key, Default}, _From, St) -> + #st{ + pids = Pids, + dbs = Dbs + } = St, + case ets:lookup(?CACHE, Key) of + [] -> + MaxSize = config:get_integer("ddoc_cache", "max_size", 104857600), + case trim(St, max(0, MaxSize)) of + ok -> + true = ets:insert_new(?CACHE, #entry{key = Key}), + {ok, Pid} = ddoc_cache_entry:start_link(Key, Default), + true = ets:update_element(?CACHE, Key, {#entry.pid, Pid}), Review comment: Its an optimization to start funneling open messages directly to the entry process even before its fully initialized to relieve pressure off the ddoc_cache_lru process. The places you mentioned are creating a full entry. Rather than do an update on the individual elements they just overwrite the entire #entry{} which involves setting the pid again. ---------------------------------------------------------------- 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