Skip to content

Commit

Permalink
Rewrite ddoc_cache to improve performance
Browse files Browse the repository at this point in the history
There were a couple issues with the previous ddoc_cache implementation
that made it possible to tip over the ddoc_cache_opener process. First,
there were a lot of messages flowing through a single gen_server. And
second, the cache relied on periodically evicting entries to ensure
proper behavior in not caching an entry forever after it had changed on
disk.

The new version makes two important changes. First, entries now have an
associated process that manages the cache entry. This process will
periodically refresh the entry and if the entry has changed or no longer
exists the process will remove its entry from cache.

The second major change is that the cache entry process directly mutates
the related ets table entries so that our performance is not dependent
on the speed of ets table mutations. Using a custom entry that does no
work the cache can now sustain roughly one million operations a second
with a twenty thousand clients fighting over a cache limited to one
thousand items. In production this means that cache performance will
likely be rate limited by other factors like loading design documents
from disk.
  • Loading branch information
davisp committed Jul 25, 2017
1 parent 22fe0b5 commit 3821de7
Show file tree
Hide file tree
Showing 29 changed files with 2,380 additions and 356 deletions.
8 changes: 8 additions & 0 deletions rel/overlay/etc/default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ credentials = false
; List of accepted methods
; methods =

; Configuration for the design document cache
;[ddoc_cache]
; The maximum size of the cache in bytes
;max_size = 104857600 ; 100MiB
; The period each cache entry should wait before
; automatically refreshing in milliseconds
;refresh_timeout = 67000

[x_frame_options]
; Settings same-origin will return X-Frame-Options: SAMEORIGIN.
; If same origin is set, it will ignore the hosts setting
Expand Down
2 changes: 1 addition & 1 deletion src/couch/src/couch_db_updater.erl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ handle_info({update_docs, Client, GroupedDocs, NonRepDocs, MergeConflicts,
couch_event:notify(Db2#db.name, {ddoc_updated, DDocId})
end, UpdatedDDocIds),
couch_event:notify(Db2#db.name, ddoc_updated),
ddoc_cache:evict(Db2#db.name, UpdatedDDocIds),
ddoc_cache:refresh(Db2#db.name, UpdatedDDocIds),
refresh_validate_doc_funs(Db2);
false ->
Db2
Expand Down
22 changes: 5 additions & 17 deletions src/ddoc_cache/src/ddoc_cache.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
{application, ddoc_cache, [
{description, "Design Document Cache"},
{vsn, git},
{modules, [
ddoc_cache,
ddoc_cache_app,
ddoc_cache_opener,
ddoc_cache_sup,
ddoc_cache_util
]},
{registered, [
ddoc_cache_lru,
ddoc_cache_opener
Expand All @@ -28,17 +21,12 @@
kernel,
stdlib,
crypto,
config,
couch_event,
ets_lru,
mem3,
fabric,
couch_log,
couch_stats
couch_stats,
mem3,
fabric
]},
{mod, {ddoc_cache_app, []}},
{env, [
{max_objects, unlimited},
{max_size, 104857600}, % 100M
{max_lifetime, 60000} % 1m
]}
{mod, {ddoc_cache_app, []}}
]}.
79 changes: 18 additions & 61 deletions src/ddoc_cache/src/ddoc_cache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,86 +12,43 @@

-module(ddoc_cache).

-export([
start/0,
stop/0
]).

-export([
open_doc/2,
open_doc/3,
open_validation_funs/1,
evict/2,
open_custom/2,
refresh/2,

%% deprecated
open/2
]).

start() ->
application:start(ddoc_cache).

stop() ->
application:stop(ddoc_cache).

open_doc(DbName, DocId) ->
Key = {DbName, DocId, '_'},
case ddoc_cache_opener:match_newest(Key) of
{ok, _} = Resp ->
couch_stats:increment_counter([ddoc_cache, hit]),
Resp;
missing ->
couch_stats:increment_counter([ddoc_cache, miss]),
ddoc_cache_opener:open_doc(DbName, DocId);
recover ->
couch_stats:increment_counter([ddoc_cache, recovery]),
ddoc_cache_opener:recover_doc(DbName, DocId)
end.
Key = {ddoc_cache_entry_ddocid, {DbName, DocId}},
ddoc_cache_lru:open(Key).


open_doc(DbName, DocId, RevId) ->
Key = {DbName, DocId, RevId},
case ddoc_cache_opener:lookup(Key) of
{ok, _} = Resp ->
couch_stats:increment_counter([ddoc_cache, hit]),
Resp;
missing ->
couch_stats:increment_counter([ddoc_cache, miss]),
ddoc_cache_opener:open_doc(DbName, DocId, RevId);
recover ->
couch_stats:increment_counter([ddoc_cache, recovery]),
ddoc_cache_opener:recover_doc(DbName, DocId, RevId)
end.
Key = {ddoc_cache_entry_ddocid_rev, {DbName, DocId, RevId}},
ddoc_cache_lru:open(Key).


open_validation_funs(DbName) ->
Key = {DbName, validation_funs},
case ddoc_cache_opener:lookup(Key) of
{ok, _} = Resp ->
couch_stats:increment_counter([ddoc_cache, hit]),
Resp;
missing ->
couch_stats:increment_counter([ddoc_cache, miss]),
ddoc_cache_opener:open_validation_funs(DbName);
recover ->
couch_stats:increment_counter([ddoc_cache, recovery]),
ddoc_cache_opener:recover_validation_funs(DbName)
end.
Key = {ddoc_cache_entry_validation_funs, DbName},
ddoc_cache_lru:open(Key).


open_custom(DbName, Mod) ->
Key = {DbName, Mod},
case ddoc_cache_opener:lookup(Key) of
{ok, _} = Resp ->
couch_stats:increment_counter([ddoc_cache, hit]),
Resp;
missing ->
couch_stats:increment_counter([ddoc_cache, miss]),
ddoc_cache_opener:open_doc(DbName, Mod);
recover ->
couch_stats:increment_counter([ddoc_cache, recovery]),
Mod:recover(DbName)
end.

evict(ShardDbName, DDocIds) ->
Key = {ddoc_cache_entry_custom, {DbName, Mod}},
ddoc_cache_lru:open(Key).


refresh(ShardDbName, DDocIds) when is_list(DDocIds) ->
DbName = mem3:dbname(ShardDbName),
ddoc_cache_opener:evict_docs(DbName, DDocIds).
ddoc_cache_lru:refresh(DbName, DDocIds).


open(DbName, validation_funs) ->
open_validation_funs(DbName);
Expand Down
40 changes: 40 additions & 0 deletions src/ddoc_cache/src/ddoc_cache.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
% 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.

-type dbname() :: iodata().
-type docid() :: iodata().
-type doc_hash() :: <<_:128>>.
-type revision() :: {pos_integer(), doc_hash()}.

-define(CACHE, ddoc_cache_entries).
-define(LRU, ddoc_cache_lru).
-define(REFRESH_TIMEOUT, 67000).
-define(SHUTDOWN_TIMEOUT, 1000).

-record(entry, {
key,
val,
pid
}).

-record(opener, {
key,
pid,
clients
}).


-ifdef(TEST).
-define(EVENT(Name, Arg), ddoc_cache_ev:event(Name, Arg)).
-else.
-define(EVENT(Name, Arg), ignore).
-endif.

0 comments on commit 3821de7

Please sign in to comment.